d: Fix ICE: in verify_gimple_in_seq on powerpc-darwin9 [PR112270]
[official-gcc.git] / gcc / c / c-typeck.cc
blob0de4662bfc6ce4614a2b892133f9a37b2d14d4d1
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 struct comptypes_data;
94 static bool tagged_types_tu_compatible_p (const_tree, const_tree,
95 struct comptypes_data *);
96 static bool comp_target_types (location_t, tree, tree);
97 static bool function_types_compatible_p (const_tree, const_tree,
98 struct comptypes_data *);
99 static bool type_lists_compatible_p (const_tree, const_tree,
100 struct comptypes_data *);
101 static tree lookup_field (tree, tree);
102 static int convert_arguments (location_t, vec<location_t>, tree,
103 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
104 tree);
105 static tree pointer_diff (location_t, tree, tree, tree *);
106 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
107 enum impl_conv, bool, tree, tree, int,
108 int = 0);
109 static tree valid_compound_expr_initializer (tree, tree);
110 static void push_string (const char *);
111 static void push_member_name (tree);
112 static int spelling_length (void);
113 static char *print_spelling (char *);
114 static void warning_init (location_t, int, const char *);
115 static tree digest_init (location_t, tree, tree, tree, bool, bool, bool, bool,
116 bool, bool);
117 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
118 bool, struct obstack *);
119 static void output_pending_init_elements (int, struct obstack *);
120 static bool set_designator (location_t, bool, struct obstack *);
121 static void push_range_stack (tree, struct obstack *);
122 static void add_pending_init (location_t, tree, tree, tree, bool,
123 struct obstack *);
124 static void set_nonincremental_init (struct obstack *);
125 static void set_nonincremental_init_from_string (tree, struct obstack *);
126 static tree find_init_member (tree, struct obstack *);
127 static void readonly_warning (tree, enum lvalue_use);
128 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
129 static void record_maybe_used_decl (tree);
130 static bool comptypes_internal (const_tree, const_tree,
131 struct comptypes_data *data);
133 /* Return true if EXP is a null pointer constant, false otherwise. */
135 bool
136 null_pointer_constant_p (const_tree expr)
138 /* This should really operate on c_expr structures, but they aren't
139 yet available everywhere required. */
140 tree type = TREE_TYPE (expr);
142 /* An integer constant expression with the value 0, such an expression
143 cast to type void*, or the predefined constant nullptr, are a null
144 pointer constant. */
145 if (expr == nullptr_node)
146 return true;
148 return (TREE_CODE (expr) == INTEGER_CST
149 && !TREE_OVERFLOW (expr)
150 && integer_zerop (expr)
151 && (INTEGRAL_TYPE_P (type)
152 || (TREE_CODE (type) == POINTER_TYPE
153 && VOID_TYPE_P (TREE_TYPE (type))
154 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
157 /* EXPR may appear in an unevaluated part of an integer constant
158 expression, but not in an evaluated part. Wrap it in a
159 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
160 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
162 static tree
163 note_integer_operands (tree expr)
165 tree ret;
166 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
168 ret = copy_node (expr);
169 TREE_OVERFLOW (ret) = 1;
171 else
173 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
174 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
176 return ret;
179 /* Having checked whether EXPR may appear in an unevaluated part of an
180 integer constant expression and found that it may, remove any
181 C_MAYBE_CONST_EXPR noting this fact and return the resulting
182 expression. */
184 static inline tree
185 remove_c_maybe_const_expr (tree expr)
187 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
188 return C_MAYBE_CONST_EXPR_EXPR (expr);
189 else
190 return expr;
193 \f/* This is a cache to hold if two types are compatible or not. */
195 struct tagged_tu_seen_cache {
196 const struct tagged_tu_seen_cache * next;
197 const_tree t1;
198 const_tree t2;
199 /* The return value of tagged_types_tu_compatible_p if we had seen
200 these two types already. */
201 int val;
204 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
205 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
207 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
208 does not have an incomplete type. (That includes void types.)
209 LOC is the location of the use. */
211 tree
212 require_complete_type (location_t loc, tree value)
214 tree type = TREE_TYPE (value);
216 if (error_operand_p (value))
217 return error_mark_node;
219 /* First, detect a valid value with a complete type. */
220 if (COMPLETE_TYPE_P (type))
221 return value;
223 c_incomplete_type_error (loc, value, type);
224 return error_mark_node;
227 /* Print an error message for invalid use of an incomplete type.
228 VALUE is the expression that was used (or 0 if that isn't known)
229 and TYPE is the type that was invalid. LOC is the location for
230 the error. */
232 void
233 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
235 /* Avoid duplicate error message. */
236 if (TREE_CODE (type) == ERROR_MARK)
237 return;
239 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
240 error_at (loc, "%qD has an incomplete type %qT", value, type);
241 else
243 retry:
244 /* We must print an error message. Be clever about what it says. */
246 switch (TREE_CODE (type))
248 case RECORD_TYPE:
249 case UNION_TYPE:
250 case ENUMERAL_TYPE:
251 break;
253 case VOID_TYPE:
254 error_at (loc, "invalid use of void expression");
255 return;
257 case ARRAY_TYPE:
258 if (TYPE_DOMAIN (type))
260 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
262 error_at (loc, "invalid use of flexible array member");
263 return;
265 type = TREE_TYPE (type);
266 goto retry;
268 error_at (loc, "invalid use of array with unspecified bounds");
269 return;
271 default:
272 gcc_unreachable ();
275 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
276 error_at (loc, "invalid use of undefined type %qT", type);
277 else
278 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
279 error_at (loc, "invalid use of incomplete typedef %qT", type);
283 /* Given a type, apply default promotions wrt unnamed function
284 arguments and return the new type. */
286 tree
287 c_type_promotes_to (tree type)
289 tree ret = NULL_TREE;
291 if (TYPE_MAIN_VARIANT (type) == float_type_node)
292 ret = double_type_node;
293 else if (c_promoting_integer_type_p (type))
295 /* Preserve unsignedness if not really getting any wider. */
296 if (TYPE_UNSIGNED (type)
297 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
298 ret = unsigned_type_node;
299 else
300 ret = integer_type_node;
303 if (ret != NULL_TREE)
304 return (TYPE_ATOMIC (type)
305 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
306 : ret);
308 return type;
311 /* Return true if between two named address spaces, whether there is a superset
312 named address space that encompasses both address spaces. If there is a
313 superset, return which address space is the superset. */
315 static bool
316 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
318 if (as1 == as2)
320 *common = as1;
321 return true;
323 else if (targetm.addr_space.subset_p (as1, as2))
325 *common = as2;
326 return true;
328 else if (targetm.addr_space.subset_p (as2, as1))
330 *common = as1;
331 return true;
333 else
334 return false;
337 /* Return a variant of TYPE which has all the type qualifiers of LIKE
338 as well as those of TYPE. */
340 static tree
341 qualify_type (tree type, tree like)
343 addr_space_t as_type = TYPE_ADDR_SPACE (type);
344 addr_space_t as_like = TYPE_ADDR_SPACE (like);
345 addr_space_t as_common;
347 /* If the two named address spaces are different, determine the common
348 superset address space. If there isn't one, raise an error. */
349 if (!addr_space_superset (as_type, as_like, &as_common))
351 as_common = as_type;
352 error ("%qT and %qT are in disjoint named address spaces",
353 type, like);
356 return c_build_qualified_type (type,
357 TYPE_QUALS_NO_ADDR_SPACE (type)
358 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
359 | ENCODE_QUAL_ADDR_SPACE (as_common));
363 /* If NTYPE is a type of a non-variadic function with a prototype
364 and OTYPE is a type of a function without a prototype and ATTRS
365 contains attribute format, diagnosess and removes it from ATTRS.
366 Returns the result of build_type_attribute_variant of NTYPE and
367 the (possibly) modified ATTRS. */
369 static tree
370 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
372 if (!prototype_p (otype)
373 && prototype_p (ntype)
374 && lookup_attribute ("format", attrs))
376 warning_at (input_location, OPT_Wattributes,
377 "%qs attribute cannot be applied to a function that "
378 "does not take variable arguments", "format");
379 attrs = remove_attribute ("format", attrs);
381 return build_type_attribute_variant (ntype, attrs);
384 /* Return the composite type of two compatible types.
386 We assume that comptypes has already been done and returned
387 nonzero; if that isn't so, this may crash. In particular, we
388 assume that qualifiers match. */
390 tree
391 composite_type (tree t1, tree t2)
393 enum tree_code code1;
394 enum tree_code code2;
395 tree attributes;
397 /* Save time if the two types are the same. */
399 if (t1 == t2) return t1;
401 /* If one type is nonsense, use the other. */
402 if (t1 == error_mark_node)
403 return t2;
404 if (t2 == error_mark_node)
405 return t1;
407 code1 = TREE_CODE (t1);
408 code2 = TREE_CODE (t2);
410 /* Merge the attributes. */
411 attributes = targetm.merge_type_attributes (t1, t2);
413 /* If one is an enumerated type and the other is the compatible
414 integer type, the composite type might be either of the two
415 (DR#013 question 3). For consistency, use the enumerated type as
416 the composite type. */
418 if (code1 == ENUMERAL_TYPE
419 && (code2 == INTEGER_TYPE
420 || code2 == BOOLEAN_TYPE))
421 return t1;
422 if (code2 == ENUMERAL_TYPE
423 && (code1 == INTEGER_TYPE
424 || code1 == BOOLEAN_TYPE))
425 return t2;
427 gcc_assert (code1 == code2);
429 switch (code1)
431 case POINTER_TYPE:
432 /* For two pointers, do this recursively on the target type. */
434 tree pointed_to_1 = TREE_TYPE (t1);
435 tree pointed_to_2 = TREE_TYPE (t2);
436 tree target = composite_type (pointed_to_1, pointed_to_2);
437 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
438 t1 = build_type_attribute_variant (t1, attributes);
439 return qualify_type (t1, t2);
442 case ARRAY_TYPE:
444 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
445 int quals;
446 tree unqual_elt;
447 tree d1 = TYPE_DOMAIN (t1);
448 tree d2 = TYPE_DOMAIN (t2);
449 bool d1_variable, d2_variable;
450 bool d1_zero, d2_zero;
451 bool t1_complete, t2_complete;
453 /* We should not have any type quals on arrays at all. */
454 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
455 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
457 t1_complete = COMPLETE_TYPE_P (t1);
458 t2_complete = COMPLETE_TYPE_P (t2);
460 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
461 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
463 d1_variable = (!d1_zero
464 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
465 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
466 d2_variable = (!d2_zero
467 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
468 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
469 d1_variable = d1_variable || (d1_zero && C_TYPE_VARIABLE_SIZE (t1));
470 d2_variable = d2_variable || (d2_zero && C_TYPE_VARIABLE_SIZE (t2));
472 /* Save space: see if the result is identical to one of the args. */
473 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
474 && (d2_variable || d2_zero || !d1_variable))
475 return build_type_attribute_variant (t1, attributes);
476 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
477 && (d1_variable || d1_zero || !d2_variable))
478 return build_type_attribute_variant (t2, attributes);
480 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
481 return build_type_attribute_variant (t1, attributes);
482 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
483 return build_type_attribute_variant (t2, attributes);
485 /* Merge the element types, and have a size if either arg has
486 one. We may have qualifiers on the element types. To set
487 up TYPE_MAIN_VARIANT correctly, we need to form the
488 composite of the unqualified types and add the qualifiers
489 back at the end. */
490 quals = TYPE_QUALS (strip_array_types (elt));
491 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
492 t1 = build_array_type (unqual_elt,
493 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
494 && (d2_variable
495 || d2_zero
496 || !d1_variable))
497 ? t1
498 : t2));
499 /* Ensure a composite type involving a zero-length array type
500 is a zero-length type not an incomplete type. */
501 if (d1_zero && d2_zero
502 && (t1_complete || t2_complete)
503 && !COMPLETE_TYPE_P (t1))
505 TYPE_SIZE (t1) = bitsize_zero_node;
506 TYPE_SIZE_UNIT (t1) = size_zero_node;
508 t1 = c_build_qualified_type (t1, quals);
509 return build_type_attribute_variant (t1, attributes);
512 case ENUMERAL_TYPE:
513 case RECORD_TYPE:
514 case UNION_TYPE:
515 if (attributes != NULL)
517 /* Try harder not to create a new aggregate type. */
518 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
519 return t1;
520 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
521 return t2;
523 return build_type_attribute_variant (t1, attributes);
525 case FUNCTION_TYPE:
526 /* Function types: prefer the one that specified arg types.
527 If both do, merge the arg types. Also merge the return types. */
529 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
530 tree p1 = TYPE_ARG_TYPES (t1);
531 tree p2 = TYPE_ARG_TYPES (t2);
532 int len;
533 tree newargs, n;
534 int i;
536 /* Save space: see if the result is identical to one of the args. */
537 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
538 return build_functype_attribute_variant (t1, t2, attributes);
539 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
540 return build_functype_attribute_variant (t2, t1, attributes);
542 /* Simple way if one arg fails to specify argument types. */
543 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
545 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2),
546 TYPE_NO_NAMED_ARGS_STDARG_P (t2));
547 t1 = build_type_attribute_variant (t1, attributes);
548 return qualify_type (t1, t2);
550 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
552 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1),
553 TYPE_NO_NAMED_ARGS_STDARG_P (t1));
554 t1 = build_type_attribute_variant (t1, attributes);
555 return qualify_type (t1, t2);
558 /* If both args specify argument types, we must merge the two
559 lists, argument by argument. */
561 for (len = 0, newargs = p1;
562 newargs && newargs != void_list_node;
563 len++, newargs = TREE_CHAIN (newargs))
566 for (i = 0; i < len; i++)
567 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
569 n = newargs;
571 for (; p1 && p1 != void_list_node;
572 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
574 /* A null type means arg type is not specified.
575 Take whatever the other function type has. */
576 if (TREE_VALUE (p1) == NULL_TREE)
578 TREE_VALUE (n) = TREE_VALUE (p2);
579 goto parm_done;
581 if (TREE_VALUE (p2) == NULL_TREE)
583 TREE_VALUE (n) = TREE_VALUE (p1);
584 goto parm_done;
587 /* Given wait (union {union wait *u; int *i} *)
588 and wait (union wait *),
589 prefer union wait * as type of parm. */
590 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
591 && TREE_VALUE (p1) != TREE_VALUE (p2))
593 tree memb;
594 tree mv2 = TREE_VALUE (p2);
595 if (mv2 && mv2 != error_mark_node
596 && TREE_CODE (mv2) != ARRAY_TYPE)
597 mv2 = TYPE_MAIN_VARIANT (mv2);
598 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
599 memb; memb = DECL_CHAIN (memb))
601 tree mv3 = TREE_TYPE (memb);
602 if (mv3 && mv3 != error_mark_node
603 && TREE_CODE (mv3) != ARRAY_TYPE)
604 mv3 = TYPE_MAIN_VARIANT (mv3);
605 if (comptypes (mv3, mv2))
607 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
608 TREE_VALUE (p2));
609 pedwarn (input_location, OPT_Wpedantic,
610 "function types not truly compatible in ISO C");
611 goto parm_done;
615 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
616 && TREE_VALUE (p2) != TREE_VALUE (p1))
618 tree memb;
619 tree mv1 = TREE_VALUE (p1);
620 if (mv1 && mv1 != error_mark_node
621 && TREE_CODE (mv1) != ARRAY_TYPE)
622 mv1 = TYPE_MAIN_VARIANT (mv1);
623 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
624 memb; memb = DECL_CHAIN (memb))
626 tree mv3 = TREE_TYPE (memb);
627 if (mv3 && mv3 != error_mark_node
628 && TREE_CODE (mv3) != ARRAY_TYPE)
629 mv3 = TYPE_MAIN_VARIANT (mv3);
630 if (comptypes (mv3, mv1))
632 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
633 TREE_VALUE (p1));
634 pedwarn (input_location, OPT_Wpedantic,
635 "function types not truly compatible in ISO C");
636 goto parm_done;
640 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
641 parm_done: ;
644 t1 = build_function_type (valtype, newargs);
645 t1 = qualify_type (t1, t2);
647 /* FALLTHRU */
649 default:
650 return build_type_attribute_variant (t1, attributes);
655 /* Return the type of a conditional expression between pointers to
656 possibly differently qualified versions of compatible types.
658 We assume that comp_target_types has already been done and returned
659 true; if that isn't so, this may crash. */
661 static tree
662 common_pointer_type (tree t1, tree t2)
664 tree attributes;
665 tree pointed_to_1, mv1;
666 tree pointed_to_2, mv2;
667 tree target;
668 unsigned target_quals;
669 addr_space_t as1, as2, as_common;
670 int quals1, quals2;
672 /* Save time if the two types are the same. */
674 if (t1 == t2) return t1;
676 /* If one type is nonsense, use the other. */
677 if (t1 == error_mark_node)
678 return t2;
679 if (t2 == error_mark_node)
680 return t1;
682 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
683 && TREE_CODE (t2) == POINTER_TYPE);
685 /* Merge the attributes. */
686 attributes = targetm.merge_type_attributes (t1, t2);
688 /* Find the composite type of the target types, and combine the
689 qualifiers of the two types' targets. Do not lose qualifiers on
690 array element types by taking the TYPE_MAIN_VARIANT. */
691 mv1 = pointed_to_1 = TREE_TYPE (t1);
692 mv2 = pointed_to_2 = TREE_TYPE (t2);
693 if (TREE_CODE (mv1) != ARRAY_TYPE)
694 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
695 if (TREE_CODE (mv2) != ARRAY_TYPE)
696 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
697 target = composite_type (mv1, mv2);
699 /* Strip array types to get correct qualifier for pointers to arrays */
700 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
701 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
703 /* For function types do not merge const qualifiers, but drop them
704 if used inconsistently. The middle-end uses these to mark const
705 and noreturn functions. */
706 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
707 target_quals = (quals1 & quals2);
708 else
709 target_quals = (quals1 | quals2);
711 /* If the two named address spaces are different, determine the common
712 superset address space. This is guaranteed to exist due to the
713 assumption that comp_target_type returned true. */
714 as1 = TYPE_ADDR_SPACE (pointed_to_1);
715 as2 = TYPE_ADDR_SPACE (pointed_to_2);
716 if (!addr_space_superset (as1, as2, &as_common))
717 gcc_unreachable ();
719 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
721 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
722 return build_type_attribute_variant (t1, attributes);
725 /* Return the common type for two arithmetic types under the usual
726 arithmetic conversions. The default conversions have already been
727 applied, and enumerated types converted to their compatible integer
728 types. The resulting type is unqualified and has no attributes.
730 This is the type for the result of most arithmetic operations
731 if the operands have the given two types. */
733 static tree
734 c_common_type (tree t1, tree t2)
736 enum tree_code code1;
737 enum tree_code code2;
739 /* If one type is nonsense, use the other. */
740 if (t1 == error_mark_node)
741 return t2;
742 if (t2 == error_mark_node)
743 return t1;
745 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
746 t1 = TYPE_MAIN_VARIANT (t1);
748 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
749 t2 = TYPE_MAIN_VARIANT (t2);
751 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
753 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
754 t1 = build_type_attribute_variant (t1, attrs);
757 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
759 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
760 t2 = build_type_attribute_variant (t2, attrs);
763 /* Save time if the two types are the same. */
765 if (t1 == t2) return t1;
767 code1 = TREE_CODE (t1);
768 code2 = TREE_CODE (t2);
770 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
771 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
772 || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
773 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
774 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
775 || code2 == INTEGER_TYPE || code2 == BITINT_TYPE);
777 /* When one operand is a decimal float type, the other operand cannot be
778 a generic float type or a complex type. We also disallow vector types
779 here. */
780 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
781 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
783 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
785 error ("cannot mix operands of decimal floating and vector types");
786 return error_mark_node;
788 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
790 error ("cannot mix operands of decimal floating and complex types");
791 return error_mark_node;
793 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
795 error ("cannot mix operands of decimal floating "
796 "and other floating types");
797 return error_mark_node;
801 /* If one type is a vector type, return that type. (How the usual
802 arithmetic conversions apply to the vector types extension is not
803 precisely specified.) */
804 if (code1 == VECTOR_TYPE)
805 return t1;
807 if (code2 == VECTOR_TYPE)
808 return t2;
810 /* If one type is complex, form the common type of the non-complex
811 components, then make that complex. Use T1 or T2 if it is the
812 required type. */
813 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
815 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
816 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
817 tree subtype = c_common_type (subtype1, subtype2);
819 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
820 return t1;
821 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
822 return t2;
823 else if (TREE_CODE (subtype) == BITINT_TYPE)
825 sorry ("%<_Complex _BitInt(%d)%> unsupported",
826 TYPE_PRECISION (subtype));
827 return code1 == COMPLEX_TYPE ? t1 : t2;
829 else
830 return build_complex_type (subtype);
833 /* If only one is real, use it as the result. */
835 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
836 return t1;
838 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
839 return t2;
841 /* If both are real and either are decimal floating point types, use
842 the decimal floating point type with the greater precision. */
844 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
846 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
847 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
848 return dfloat128_type_node;
849 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
850 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
851 return dfloat64_type_node;
852 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
853 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
854 return dfloat32_type_node;
857 /* Deal with fixed-point types. */
858 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
860 unsigned int unsignedp = 0, satp = 0;
861 scalar_mode m1, m2;
862 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
864 m1 = SCALAR_TYPE_MODE (t1);
865 m2 = SCALAR_TYPE_MODE (t2);
867 /* If one input type is saturating, the result type is saturating. */
868 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
869 satp = 1;
871 /* If both fixed-point types are unsigned, the result type is unsigned.
872 When mixing fixed-point and integer types, follow the sign of the
873 fixed-point type.
874 Otherwise, the result type is signed. */
875 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
876 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
877 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
878 && TYPE_UNSIGNED (t1))
879 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
880 && TYPE_UNSIGNED (t2)))
881 unsignedp = 1;
883 /* The result type is signed. */
884 if (unsignedp == 0)
886 /* If the input type is unsigned, we need to convert to the
887 signed type. */
888 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
890 enum mode_class mclass = (enum mode_class) 0;
891 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
892 mclass = MODE_FRACT;
893 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
894 mclass = MODE_ACCUM;
895 else
896 gcc_unreachable ();
897 m1 = as_a <scalar_mode>
898 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
900 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
902 enum mode_class mclass = (enum mode_class) 0;
903 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
904 mclass = MODE_FRACT;
905 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
906 mclass = MODE_ACCUM;
907 else
908 gcc_unreachable ();
909 m2 = as_a <scalar_mode>
910 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
914 if (code1 == FIXED_POINT_TYPE)
916 fbit1 = GET_MODE_FBIT (m1);
917 ibit1 = GET_MODE_IBIT (m1);
919 else
921 fbit1 = 0;
922 /* Signed integers need to subtract one sign bit. */
923 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
926 if (code2 == FIXED_POINT_TYPE)
928 fbit2 = GET_MODE_FBIT (m2);
929 ibit2 = GET_MODE_IBIT (m2);
931 else
933 fbit2 = 0;
934 /* Signed integers need to subtract one sign bit. */
935 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
938 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
939 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
940 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
941 satp);
944 /* Both real or both integers; use the one with greater precision. */
946 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
947 return t1;
948 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
949 return t2;
951 /* Same precision. Prefer long longs to longs to ints when the
952 same precision, following the C99 rules on integer type rank
953 (which are equivalent to the C90 rules for C90 types). */
955 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
956 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
957 return long_long_unsigned_type_node;
959 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
960 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
962 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
963 return long_long_unsigned_type_node;
964 else
965 return long_long_integer_type_node;
968 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
969 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
970 return long_unsigned_type_node;
972 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
973 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
975 /* But preserve unsignedness from the other type,
976 since long cannot hold all the values of an unsigned int. */
977 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
978 return long_unsigned_type_node;
979 else
980 return long_integer_type_node;
983 /* For floating types of the same TYPE_PRECISION (which we here
984 assume means either the same set of values, or sets of values
985 neither a subset of the other, with behavior being undefined in
986 the latter case), follow the rules from TS 18661-3: prefer
987 interchange types _FloatN, then standard types long double,
988 double, float, then extended types _FloatNx. For extended types,
989 check them starting with _Float128x as that seems most consistent
990 in spirit with preferring long double to double; for interchange
991 types, also check in that order for consistency although it's not
992 possible for more than one of them to have the same
993 precision. */
994 tree mv1 = TYPE_MAIN_VARIANT (t1);
995 tree mv2 = TYPE_MAIN_VARIANT (t2);
997 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
998 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
999 return FLOATN_TYPE_NODE (i);
1001 /* Likewise, prefer long double to double even if same size. */
1002 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1003 return long_double_type_node;
1005 /* Likewise, prefer double to float even if same size.
1006 We got a couple of embedded targets with 32 bit doubles, and the
1007 pdp11 might have 64 bit floats. */
1008 if (mv1 == double_type_node || mv2 == double_type_node)
1009 return double_type_node;
1011 if (mv1 == float_type_node || mv2 == float_type_node)
1012 return float_type_node;
1014 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1015 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1016 return FLOATNX_TYPE_NODE (i);
1018 if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2)
1020 /* Prefer any other integral types over bit-precise integer types. */
1021 if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
1022 return code1 == BITINT_TYPE ? t2 : t1;
1023 /* If BITINT_TYPE is unsigned and the other type is signed
1024 non-BITINT_TYPE with the same precision, the latter has higher rank.
1025 In that case:
1026 Otherwise, both operands are converted to the unsigned integer type
1027 corresponding to the type of the operand with signed integer type. */
1028 if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
1029 return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
1032 /* Otherwise prefer the unsigned one. */
1034 if (TYPE_UNSIGNED (t1))
1035 return t1;
1036 else
1037 return t2;
1040 /* Wrapper around c_common_type that is used by c-common.cc and other
1041 front end optimizations that remove promotions. ENUMERAL_TYPEs
1042 are allowed here and are converted to their compatible integer types.
1043 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1044 preferably a non-Boolean type as the common type. */
1045 tree
1046 common_type (tree t1, tree t2)
1048 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1049 t1 = ENUM_UNDERLYING_TYPE (t1);
1050 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1051 t2 = ENUM_UNDERLYING_TYPE (t2);
1053 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1054 if (TREE_CODE (t1) == BOOLEAN_TYPE
1055 && TREE_CODE (t2) == BOOLEAN_TYPE)
1056 return boolean_type_node;
1058 /* If either type is BOOLEAN_TYPE, then return the other. */
1059 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1060 return t2;
1061 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1062 return t1;
1064 return c_common_type (t1, t2);
1067 struct comptypes_data {
1069 bool enum_and_int_p;
1070 bool different_types_p;
1071 bool warning_needed;
1074 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1075 or various other operations. Return 2 if they are compatible
1076 but a warning may be needed if you use them together. */
1079 comptypes (tree type1, tree type2)
1081 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1083 struct comptypes_data data = { };
1084 bool ret = comptypes_internal (type1, type2, &data);
1086 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1088 return ret ? (data.warning_needed ? 2 : 1) : 0;
1091 /* Like comptypes, but if it returns non-zero because enum and int are
1092 compatible, it sets *ENUM_AND_INT_P to true. */
1095 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1097 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1099 struct comptypes_data data = { };
1100 bool ret = comptypes_internal (type1, type2, &data);
1101 *enum_and_int_p = data.enum_and_int_p;
1103 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1105 return ret ? (data.warning_needed ? 2 : 1) : 0;
1108 /* Like comptypes, but if it returns nonzero for different types, it
1109 sets *DIFFERENT_TYPES_P to true. */
1112 comptypes_check_different_types (tree type1, tree type2,
1113 bool *different_types_p)
1115 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1117 struct comptypes_data data = { };
1118 bool ret = comptypes_internal (type1, type2, &data);
1119 *different_types_p = data.different_types_p;
1121 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1123 return ret ? (data.warning_needed ? 2 : 1) : 0;
1126 /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1127 or various other operations. If they are compatible but a warning may
1128 be needed if you use them together, 'warning_needed' in DATA is set.
1129 If one type is an enum and the other a compatible integer type, then
1130 this sets 'enum_and_int_p' in DATA to true (it is never set to
1131 false). If the types are compatible but different enough not to be
1132 permitted in C11 typedef redeclarations, then this sets
1133 'different_types_p' in DATA to true; it is never set to
1134 false, but may or may not be set if the types are incompatible.
1135 This differs from comptypes, in that we don't free the seen
1136 types. */
1138 static bool
1139 comptypes_internal (const_tree type1, const_tree type2,
1140 struct comptypes_data *data)
1142 const_tree t1 = type1;
1143 const_tree t2 = type2;
1145 /* Suppress errors caused by previously reported errors. */
1147 if (t1 == t2 || !t1 || !t2
1148 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1149 return true;
1151 /* Enumerated types are compatible with integer types, but this is
1152 not transitive: two enumerated types in the same translation unit
1153 are compatible with each other only if they are the same type. */
1155 if (TREE_CODE (t1) == ENUMERAL_TYPE
1156 && COMPLETE_TYPE_P (t1)
1157 && TREE_CODE (t2) != ENUMERAL_TYPE)
1159 t1 = ENUM_UNDERLYING_TYPE (t1);
1160 if (TREE_CODE (t2) != VOID_TYPE)
1162 data->enum_and_int_p = true;
1163 data->different_types_p = true;
1166 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1167 && COMPLETE_TYPE_P (t2)
1168 && TREE_CODE (t1) != ENUMERAL_TYPE)
1170 t2 = ENUM_UNDERLYING_TYPE (t2);
1171 if (TREE_CODE (t1) != VOID_TYPE)
1173 data->enum_and_int_p = true;
1174 data->different_types_p = true;
1178 if (t1 == t2)
1179 return true;
1181 /* Different classes of types can't be compatible. */
1183 if (TREE_CODE (t1) != TREE_CODE (t2))
1184 return false;
1186 /* Qualifiers must match. C99 6.7.3p9 */
1188 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1189 return false;
1191 /* Allow for two different type nodes which have essentially the same
1192 definition. Note that we already checked for equality of the type
1193 qualifiers (just above). */
1195 if (TREE_CODE (t1) != ARRAY_TYPE
1196 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1197 return true;
1199 int attrval;
1201 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1202 if (!(attrval = comp_type_attributes (t1, t2)))
1203 return false;
1205 if (2 == attrval)
1206 data->warning_needed = true;
1208 switch (TREE_CODE (t1))
1210 case INTEGER_TYPE:
1211 case FIXED_POINT_TYPE:
1212 case REAL_TYPE:
1213 case BITINT_TYPE:
1214 /* With these nodes, we can't determine type equivalence by
1215 looking at what is stored in the nodes themselves, because
1216 two nodes might have different TYPE_MAIN_VARIANTs but still
1217 represent the same type. For example, wchar_t and int could
1218 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1219 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1220 and are distinct types. On the other hand, int and the
1221 following typedef
1223 typedef int INT __attribute((may_alias));
1225 have identical properties, different TYPE_MAIN_VARIANTs, but
1226 represent the same type. The canonical type system keeps
1227 track of equivalence in this case, so we fall back on it. */
1228 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1230 case POINTER_TYPE:
1231 /* Do not remove mode information. */
1232 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1233 return false;
1234 return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1236 case FUNCTION_TYPE:
1237 return function_types_compatible_p (t1, t2, data);
1239 case ARRAY_TYPE:
1241 tree d1 = TYPE_DOMAIN (t1);
1242 tree d2 = TYPE_DOMAIN (t2);
1243 bool d1_variable, d2_variable;
1244 bool d1_zero, d2_zero;
1246 /* Target types must match incl. qualifiers. */
1247 if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1248 return false;
1250 if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1251 data->different_types_p = true;
1252 /* Sizes must match unless one is missing or variable. */
1253 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1254 return true;
1256 d1_zero = !TYPE_MAX_VALUE (d1);
1257 d2_zero = !TYPE_MAX_VALUE (d2);
1259 d1_variable = (!d1_zero
1260 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1261 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1262 d2_variable = (!d2_zero
1263 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1264 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1265 d1_variable = d1_variable || (d1_zero && C_TYPE_VARIABLE_SIZE (t1));
1266 d2_variable = d2_variable || (d2_zero && C_TYPE_VARIABLE_SIZE (t2));
1268 if (d1_variable != d2_variable)
1269 data->different_types_p = true;
1270 if (d1_variable || d2_variable)
1271 return true;
1272 if (d1_zero && d2_zero)
1273 return true;
1274 if (d1_zero || d2_zero
1275 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1276 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1277 return false;
1279 return true;
1282 case ENUMERAL_TYPE:
1283 case RECORD_TYPE:
1284 case UNION_TYPE:
1285 if (false)
1287 return tagged_types_tu_compatible_p (t1, t2, data);
1289 return false;
1291 case VECTOR_TYPE:
1292 return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1293 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1295 default:
1296 return false;
1298 gcc_unreachable ();
1301 /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1302 their qualifiers, except for named address spaces. If the pointers point to
1303 different named addresses, then we must determine if one address space is a
1304 subset of the other. */
1306 static bool
1307 comp_target_types (location_t location, tree ttl, tree ttr)
1309 int val;
1310 int val_ped;
1311 tree mvl = TREE_TYPE (ttl);
1312 tree mvr = TREE_TYPE (ttr);
1313 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1314 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1315 addr_space_t as_common;
1316 bool enum_and_int_p;
1318 /* Fail if pointers point to incompatible address spaces. */
1319 if (!addr_space_superset (asl, asr, &as_common))
1320 return 0;
1322 /* For pedantic record result of comptypes on arrays before losing
1323 qualifiers on the element type below. */
1324 val_ped = 1;
1326 if (TREE_CODE (mvl) == ARRAY_TYPE
1327 && TREE_CODE (mvr) == ARRAY_TYPE)
1328 val_ped = comptypes (mvl, mvr);
1330 /* Qualifiers on element types of array types that are
1331 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1333 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1334 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1335 : TYPE_MAIN_VARIANT (mvl));
1337 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1338 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1339 : TYPE_MAIN_VARIANT (mvr));
1341 enum_and_int_p = false;
1342 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1344 if (val == 1 && val_ped != 1)
1345 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1346 "in ISO C before C2X");
1348 if (val == 2)
1349 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1351 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1352 warning_at (location, OPT_Wc___compat,
1353 "pointer target types incompatible in C++");
1355 return val;
1358 /* Subroutines of `comptypes'. */
1362 /* Allocate the seen two types, assuming that they are compatible. */
1364 static struct tagged_tu_seen_cache *
1365 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1367 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1368 tu->next = tagged_tu_seen_base;
1369 tu->t1 = t1;
1370 tu->t2 = t2;
1372 tagged_tu_seen_base = tu;
1374 /* The C standard says that two structures in different translation
1375 units are compatible with each other only if the types of their
1376 fields are compatible (among other things). We assume that they
1377 are compatible until proven otherwise when building the cache.
1378 An example where this can occur is:
1379 struct a
1381 struct a *next;
1383 If we are comparing this against a similar struct in another TU,
1384 and did not assume they were compatible, we end up with an infinite
1385 loop. */
1386 tu->val = 1;
1387 return tu;
1390 /* Free the seen types until we get to TU_TIL. */
1392 static void
1393 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1395 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1396 while (tu != tu_til)
1398 const struct tagged_tu_seen_cache *const tu1
1399 = (const struct tagged_tu_seen_cache *) tu;
1400 tu = tu1->next;
1401 XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1403 tagged_tu_seen_base = tu_til;
1406 /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1407 compatible. The two types are not the same (which has been
1408 checked earlier in comptypes_internal). */
1410 static bool
1411 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1412 struct comptypes_data *data)
1414 tree s1, s2;
1416 /* We have to verify that the tags of the types are the same. This
1417 is harder than it looks because this may be a typedef, so we have
1418 to go look at the original type. It may even be a typedef of a
1419 typedef...
1420 In the case of compiler-created builtin structs the TYPE_DECL
1421 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1422 while (TYPE_NAME (t1)
1423 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1424 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1425 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1427 while (TYPE_NAME (t2)
1428 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1429 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1430 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1432 /* C90 didn't have the requirement that the two tags be the same. */
1433 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1434 return 0;
1436 /* C90 didn't say what happened if one or both of the types were
1437 incomplete; we choose to follow C99 rules here, which is that they
1438 are compatible. */
1439 if (TYPE_SIZE (t1) == NULL
1440 || TYPE_SIZE (t2) == NULL)
1441 return 1;
1444 const struct tagged_tu_seen_cache * tts_i;
1445 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1446 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1447 return tts_i->val;
1450 switch (TREE_CODE (t1))
1452 case ENUMERAL_TYPE:
1454 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1455 /* Speed up the case where the type values are in the same order. */
1456 tree tv1 = TYPE_VALUES (t1);
1457 tree tv2 = TYPE_VALUES (t2);
1459 if (tv1 == tv2)
1461 return 1;
1464 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1466 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1467 break;
1468 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1470 tu->val = 0;
1471 return 0;
1475 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1477 return 1;
1479 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1481 tu->val = 0;
1482 return 0;
1485 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1487 tu->val = 0;
1488 return 0;
1491 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1493 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1494 if (s2 == NULL
1495 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1497 tu->val = 0;
1498 return 0;
1501 return 1;
1504 case UNION_TYPE:
1506 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1508 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1510 tu->val = 0;
1511 return 0;
1514 /* Speed up the common case where the fields are in the same order. */
1515 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1516 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1518 int result;
1520 if (DECL_NAME (s1) != DECL_NAME (s2))
1521 break;
1522 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), data);
1524 if (result != 1 && !DECL_NAME (s1))
1525 break;
1526 if (result == 0)
1528 tu->val = 0;
1529 return 0;
1532 if (TREE_CODE (s1) == FIELD_DECL
1533 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1534 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1536 tu->val = 0;
1537 return 0;
1540 if (!s1 && !s2)
1542 return tu->val;
1545 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1547 bool ok = false;
1549 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1550 if (DECL_NAME (s1) == DECL_NAME (s2))
1552 int result;
1554 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1555 data);
1557 if (result != 1 && !DECL_NAME (s1))
1558 continue;
1559 if (result == 0)
1561 tu->val = 0;
1562 return 0;
1565 if (TREE_CODE (s1) == FIELD_DECL
1566 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1567 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1568 break;
1570 ok = true;
1571 break;
1573 if (!ok)
1575 tu->val = 0;
1576 return 0;
1579 return tu->val;
1582 case RECORD_TYPE:
1584 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1586 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1588 tu->val = 0;
1589 return 0;
1592 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1593 s1 && s2;
1594 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1596 int result;
1597 if (TREE_CODE (s1) != TREE_CODE (s2)
1598 || DECL_NAME (s1) != DECL_NAME (s2))
1599 break;
1600 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), data);
1601 if (result == 0)
1602 break;
1604 if (TREE_CODE (s1) == FIELD_DECL
1605 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1606 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1607 break;
1609 if (s1 && s2)
1610 tu->val = 0;
1611 else
1612 tu->val = 1;
1613 return tu->val;
1616 default:
1617 gcc_unreachable ();
1621 /* Return true if two function types F1 and F2 are compatible.
1622 If either type specifies no argument types,
1623 the other must specify a fixed number of self-promoting arg types.
1624 Otherwise, if one type specifies only the number of arguments,
1625 the other must specify that number of self-promoting arg types.
1626 Otherwise, the argument types must match. */
1628 static bool
1629 function_types_compatible_p (const_tree f1, const_tree f2,
1630 struct comptypes_data *data)
1632 tree args1, args2;
1633 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1634 int val = 1;
1635 int val1;
1636 tree ret1, ret2;
1638 ret1 = TREE_TYPE (f1);
1639 ret2 = TREE_TYPE (f2);
1641 /* 'volatile' qualifiers on a function's return type used to mean
1642 the function is noreturn. */
1643 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1644 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1645 if (TYPE_VOLATILE (ret1))
1646 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1647 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1648 if (TYPE_VOLATILE (ret2))
1649 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1650 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1651 val = comptypes_internal (ret1, ret2, data);
1652 if (val == 0)
1653 return 0;
1655 args1 = TYPE_ARG_TYPES (f1);
1656 args2 = TYPE_ARG_TYPES (f2);
1658 if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
1659 data->different_types_p = true;
1661 /* An unspecified parmlist matches any specified parmlist
1662 whose argument types don't need default promotions. */
1664 if (args1 == NULL_TREE)
1666 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1667 return 0;
1668 if (!self_promoting_args_p (args2))
1669 return 0;
1670 /* If one of these types comes from a non-prototype fn definition,
1671 compare that with the other type's arglist.
1672 If they don't match, ask for a warning (but no error). */
1673 if (TYPE_ACTUAL_ARG_TYPES (f1)
1674 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1675 data) != 1)
1677 val = 1;
1678 data->warning_needed = true;
1680 return val;
1682 if (args2 == NULL_TREE)
1684 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1685 return 0;
1686 if (!self_promoting_args_p (args1))
1687 return 0;
1688 if (TYPE_ACTUAL_ARG_TYPES (f2)
1689 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1690 data) != 1)
1692 val = 1;
1693 data->warning_needed = true;
1695 return val;
1698 /* Both types have argument lists: compare them and propagate results. */
1699 val1 = type_lists_compatible_p (args1, args2, data);
1700 return val1;
1703 /* Check two lists of types for compatibility, returning false for
1704 incompatible, true for compatible. */
1706 static bool
1707 type_lists_compatible_p (const_tree args1, const_tree args2,
1708 struct comptypes_data *data)
1710 while (1)
1712 tree a1, mv1, a2, mv2;
1713 if (args1 == NULL_TREE && args2 == NULL_TREE)
1714 return true;
1715 /* If one list is shorter than the other,
1716 they fail to match. */
1717 if (args1 == NULL_TREE || args2 == NULL_TREE)
1718 return 0;
1719 mv1 = a1 = TREE_VALUE (args1);
1720 mv2 = a2 = TREE_VALUE (args2);
1721 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1722 mv1 = (TYPE_ATOMIC (mv1)
1723 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1724 TYPE_QUAL_ATOMIC)
1725 : TYPE_MAIN_VARIANT (mv1));
1726 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1727 mv2 = (TYPE_ATOMIC (mv2)
1728 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1729 TYPE_QUAL_ATOMIC)
1730 : TYPE_MAIN_VARIANT (mv2));
1731 /* A null pointer instead of a type
1732 means there is supposed to be an argument
1733 but nothing is specified about what type it has.
1734 So match anything that self-promotes. */
1735 if ((a1 == NULL_TREE) != (a2 == NULL_TREE))
1736 data->different_types_p = true;
1737 if (a1 == NULL_TREE)
1739 if (c_type_promotes_to (a2) != a2)
1740 return 0;
1742 else if (a2 == NULL_TREE)
1744 if (c_type_promotes_to (a1) != a1)
1745 return 0;
1747 /* If one of the lists has an error marker, ignore this arg. */
1748 else if (TREE_CODE (a1) == ERROR_MARK
1749 || TREE_CODE (a2) == ERROR_MARK)
1751 else if (!comptypes_internal (mv1, mv2, data))
1753 data->different_types_p = true;
1754 /* Allow wait (union {union wait *u; int *i} *)
1755 and wait (union wait *) to be compatible. */
1756 if (TREE_CODE (a1) == UNION_TYPE
1757 && (TYPE_NAME (a1) == NULL_TREE
1758 || TYPE_TRANSPARENT_AGGR (a1))
1759 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1760 && tree_int_cst_equal (TYPE_SIZE (a1),
1761 TYPE_SIZE (a2)))
1763 tree memb;
1764 for (memb = TYPE_FIELDS (a1);
1765 memb; memb = DECL_CHAIN (memb))
1767 tree mv3 = TREE_TYPE (memb);
1768 if (mv3 && mv3 != error_mark_node
1769 && TREE_CODE (mv3) != ARRAY_TYPE)
1770 mv3 = (TYPE_ATOMIC (mv3)
1771 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1772 TYPE_QUAL_ATOMIC)
1773 : TYPE_MAIN_VARIANT (mv3));
1774 if (comptypes_internal (mv3, mv2, data))
1775 break;
1777 if (memb == NULL_TREE)
1778 return 0;
1780 else if (TREE_CODE (a2) == UNION_TYPE
1781 && (TYPE_NAME (a2) == NULL_TREE
1782 || TYPE_TRANSPARENT_AGGR (a2))
1783 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1784 && tree_int_cst_equal (TYPE_SIZE (a2),
1785 TYPE_SIZE (a1)))
1787 tree memb;
1788 for (memb = TYPE_FIELDS (a2);
1789 memb; memb = DECL_CHAIN (memb))
1791 tree mv3 = TREE_TYPE (memb);
1792 if (mv3 && mv3 != error_mark_node
1793 && TREE_CODE (mv3) != ARRAY_TYPE)
1794 mv3 = (TYPE_ATOMIC (mv3)
1795 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1796 TYPE_QUAL_ATOMIC)
1797 : TYPE_MAIN_VARIANT (mv3));
1798 if (comptypes_internal (mv3, mv1, data))
1799 break;
1801 if (memb == NULL_TREE)
1802 return 0;
1804 else
1805 return 0;
1808 args1 = TREE_CHAIN (args1);
1809 args2 = TREE_CHAIN (args2);
1813 /* Compute the size to increment a pointer by. When a function type or void
1814 type or incomplete type is passed, size_one_node is returned.
1815 This function does not emit any diagnostics; the caller is responsible
1816 for that. */
1818 static tree
1819 c_size_in_bytes (const_tree type)
1821 enum tree_code code = TREE_CODE (type);
1823 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1824 || !COMPLETE_TYPE_P (type))
1825 return size_one_node;
1827 /* Convert in case a char is more than one unit. */
1828 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1829 size_int (TYPE_PRECISION (char_type_node)
1830 / BITS_PER_UNIT));
1833 /* Return either DECL or its known constant value (if it has one). */
1835 tree
1836 decl_constant_value_1 (tree decl, bool in_init)
1838 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1839 TREE_CODE (decl) != PARM_DECL
1840 && !TREE_THIS_VOLATILE (decl)
1841 && TREE_READONLY (decl)
1842 && DECL_INITIAL (decl) != NULL_TREE
1843 && !error_operand_p (DECL_INITIAL (decl))
1844 /* This is invalid if initial value is not constant.
1845 If it has either a function call, a memory reference,
1846 or a variable, then re-evaluating it could give different results. */
1847 && TREE_CONSTANT (DECL_INITIAL (decl))
1848 /* Check for cases where this is sub-optimal, even though valid. */
1849 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1850 return DECL_INITIAL (decl);
1851 return decl;
1854 /* Return either DECL or its known constant value (if it has one).
1855 Like the above, but always return decl outside of functions. */
1857 tree
1858 decl_constant_value (tree decl)
1860 /* Don't change a variable array bound or initial value to a constant
1861 in a place where a variable is invalid. */
1862 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1865 /* Convert the array expression EXP to a pointer. */
1866 static tree
1867 array_to_pointer_conversion (location_t loc, tree exp)
1869 tree orig_exp = exp;
1870 tree type = TREE_TYPE (exp);
1871 tree adr;
1872 tree restype = TREE_TYPE (type);
1873 tree ptrtype;
1875 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1877 STRIP_TYPE_NOPS (exp);
1879 copy_warning (exp, orig_exp);
1881 ptrtype = build_pointer_type (restype);
1883 if (INDIRECT_REF_P (exp))
1884 return convert (ptrtype, TREE_OPERAND (exp, 0));
1886 /* In C++ array compound literals are temporary objects unless they are
1887 const or appear in namespace scope, so they are destroyed too soon
1888 to use them for much of anything (c++/53220). */
1889 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1891 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1892 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1893 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1894 "converting an array compound literal to a pointer "
1895 "is ill-formed in C++");
1898 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1899 return convert (ptrtype, adr);
1902 /* Convert the function expression EXP to a pointer. */
1903 static tree
1904 function_to_pointer_conversion (location_t loc, tree exp)
1906 tree orig_exp = exp;
1908 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1910 STRIP_TYPE_NOPS (exp);
1912 copy_warning (exp, orig_exp);
1914 return build_unary_op (loc, ADDR_EXPR, exp, false);
1917 /* Mark EXP as read, not just set, for set but not used -Wunused
1918 warning purposes. */
1920 void
1921 mark_exp_read (tree exp)
1923 switch (TREE_CODE (exp))
1925 case VAR_DECL:
1926 case PARM_DECL:
1927 DECL_READ_P (exp) = 1;
1928 break;
1929 case ARRAY_REF:
1930 case COMPONENT_REF:
1931 case MODIFY_EXPR:
1932 case REALPART_EXPR:
1933 case IMAGPART_EXPR:
1934 CASE_CONVERT:
1935 case ADDR_EXPR:
1936 case VIEW_CONVERT_EXPR:
1937 mark_exp_read (TREE_OPERAND (exp, 0));
1938 break;
1939 case COMPOUND_EXPR:
1940 /* Pattern match what build_atomic_assign produces with modifycode
1941 NOP_EXPR. */
1942 if (VAR_P (TREE_OPERAND (exp, 1))
1943 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1944 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1946 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1947 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1948 if (TREE_CODE (t1) == TARGET_EXPR
1949 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1950 && TREE_CODE (t2) == CALL_EXPR)
1952 tree fndecl = get_callee_fndecl (t2);
1953 tree arg = NULL_TREE;
1954 if (fndecl
1955 && TREE_CODE (fndecl) == FUNCTION_DECL
1956 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1957 && call_expr_nargs (t2) >= 2)
1958 switch (DECL_FUNCTION_CODE (fndecl))
1960 case BUILT_IN_ATOMIC_STORE:
1961 arg = CALL_EXPR_ARG (t2, 1);
1962 break;
1963 case BUILT_IN_ATOMIC_STORE_1:
1964 case BUILT_IN_ATOMIC_STORE_2:
1965 case BUILT_IN_ATOMIC_STORE_4:
1966 case BUILT_IN_ATOMIC_STORE_8:
1967 case BUILT_IN_ATOMIC_STORE_16:
1968 arg = CALL_EXPR_ARG (t2, 0);
1969 break;
1970 default:
1971 break;
1973 if (arg)
1975 STRIP_NOPS (arg);
1976 if (TREE_CODE (arg) == ADDR_EXPR
1977 && DECL_P (TREE_OPERAND (arg, 0))
1978 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
1979 mark_exp_read (TREE_OPERAND (arg, 0));
1983 /* FALLTHRU */
1984 case C_MAYBE_CONST_EXPR:
1985 mark_exp_read (TREE_OPERAND (exp, 1));
1986 break;
1987 default:
1988 break;
1992 /* Perform the default conversion of arrays and functions to pointers.
1993 Return the result of converting EXP. For any other expression, just
1994 return EXP.
1996 LOC is the location of the expression. */
1998 struct c_expr
1999 default_function_array_conversion (location_t loc, struct c_expr exp)
2001 tree orig_exp = exp.value;
2002 tree type = TREE_TYPE (exp.value);
2003 enum tree_code code = TREE_CODE (type);
2005 switch (code)
2007 case ARRAY_TYPE:
2009 bool not_lvalue = false;
2010 bool lvalue_array_p;
2012 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2013 || CONVERT_EXPR_P (exp.value))
2014 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2016 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2017 not_lvalue = true;
2018 exp.value = TREE_OPERAND (exp.value, 0);
2021 copy_warning (exp.value, orig_exp);
2023 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2024 if (!flag_isoc99 && !lvalue_array_p)
2026 /* Before C99, non-lvalue arrays do not decay to pointers.
2027 Normally, using such an array would be invalid; but it can
2028 be used correctly inside sizeof or as a statement expression.
2029 Thus, do not give an error here; an error will result later. */
2030 return exp;
2033 exp.value = array_to_pointer_conversion (loc, exp.value);
2035 break;
2036 case FUNCTION_TYPE:
2037 exp.value = function_to_pointer_conversion (loc, exp.value);
2038 break;
2039 default:
2040 break;
2043 return exp;
2046 struct c_expr
2047 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2049 mark_exp_read (exp.value);
2050 return default_function_array_conversion (loc, exp);
2053 /* Return whether EXPR should be treated as an atomic lvalue for the
2054 purposes of load and store handling. */
2056 static bool
2057 really_atomic_lvalue (tree expr)
2059 if (error_operand_p (expr))
2060 return false;
2061 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2062 return false;
2063 if (!lvalue_p (expr))
2064 return false;
2066 /* Ignore _Atomic on register variables, since their addresses can't
2067 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2068 sequences wouldn't work. Ignore _Atomic on structures containing
2069 bit-fields, since accessing elements of atomic structures or
2070 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2071 it's undefined at translation time or execution time, and the
2072 normal atomic sequences again wouldn't work. */
2073 while (handled_component_p (expr))
2075 if (TREE_CODE (expr) == COMPONENT_REF
2076 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2077 return false;
2078 expr = TREE_OPERAND (expr, 0);
2080 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2081 return false;
2082 return true;
2085 /* If EXPR is a named constant (C2x) derived from a constexpr variable
2086 - that is, a reference to such a variable, or a member extracted by
2087 a sequence of structure and union (but not array) member accesses
2088 (where union member accesses must access the same member as
2089 initialized) - then return the corresponding initializer;
2090 otherwise, return NULL_TREE. */
2092 static tree
2093 maybe_get_constexpr_init (tree expr)
2095 tree decl = NULL_TREE;
2096 if (TREE_CODE (expr) == VAR_DECL)
2097 decl = expr;
2098 else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2099 decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2100 if (decl
2101 && C_DECL_DECLARED_CONSTEXPR (decl)
2102 && DECL_INITIAL (decl) != NULL_TREE
2103 && !error_operand_p (DECL_INITIAL (decl)))
2104 return DECL_INITIAL (decl);
2105 if (TREE_CODE (expr) != COMPONENT_REF)
2106 return NULL_TREE;
2107 tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2108 if (inner == NULL_TREE)
2109 return NULL_TREE;
2110 while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2111 && !error_operand_p (inner)
2112 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2113 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2114 inner = TREE_OPERAND (inner, 0);
2115 if (TREE_CODE (inner) != CONSTRUCTOR)
2116 return NULL_TREE;
2117 tree field = TREE_OPERAND (expr, 1);
2118 unsigned HOST_WIDE_INT cidx;
2119 tree cfield, cvalue;
2120 bool have_other_init = false;
2121 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2123 if (cfield == field)
2124 return cvalue;
2125 have_other_init = true;
2127 if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2128 && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2129 return NULL_TREE;
2130 /* Return a default initializer. */
2131 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2132 return build_constructor (TREE_TYPE (expr), NULL);
2133 return build_zero_cst (TREE_TYPE (expr));
2136 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2137 including converting functions and arrays to pointers if CONVERT_P.
2138 If READ_P, also mark the expression as having been read. If
2139 FOR_INIT, constexpr expressions of structure and union type should
2140 be replaced by the corresponding CONSTRUCTOR; otherwise, only
2141 constexpr scalars (including elements of structures and unions) are
2142 replaced by their initializers. */
2144 struct c_expr
2145 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2146 bool convert_p, bool read_p, bool for_init)
2148 bool force_non_npc = false;
2149 if (read_p)
2150 mark_exp_read (exp.value);
2151 if (convert_p)
2152 exp = default_function_array_conversion (loc, exp);
2153 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2154 exp.value = require_complete_type (loc, exp.value);
2155 if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2157 tree init = maybe_get_constexpr_init (exp.value);
2158 if (init != NULL_TREE)
2160 /* A named constant of pointer type or type nullptr_t is not
2161 a null pointer constant even if the initializer is
2162 one. */
2163 if (TREE_CODE (init) == INTEGER_CST
2164 && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2165 && integer_zerop (init))
2166 force_non_npc = true;
2167 exp.value = init;
2170 if (really_atomic_lvalue (exp.value))
2172 vec<tree, va_gc> *params;
2173 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2174 tree expr_type = TREE_TYPE (exp.value);
2175 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2176 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2178 gcc_assert (TYPE_ATOMIC (expr_type));
2180 /* Expansion of a generic atomic load may require an addition
2181 element, so allocate enough to prevent a resize. */
2182 vec_alloc (params, 4);
2184 /* Remove the qualifiers for the rest of the expressions and
2185 create the VAL temp variable to hold the RHS. */
2186 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2187 tmp = create_tmp_var_raw (nonatomic_type);
2188 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2189 TREE_ADDRESSABLE (tmp) = 1;
2190 /* Do not disable warnings for TMP even though it's artificial.
2191 -Winvalid-memory-model depends on it. */
2193 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2194 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2195 params->quick_push (expr_addr);
2196 params->quick_push (tmp_addr);
2197 params->quick_push (seq_cst);
2198 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2200 /* EXPR is always read. */
2201 mark_exp_read (exp.value);
2203 /* Return tmp which contains the value loaded. */
2204 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2205 NULL_TREE, NULL_TREE);
2207 if (convert_p && !error_operand_p (exp.value)
2208 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2209 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2210 if (force_non_npc)
2211 exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2212 return exp;
2215 /* EXP is an expression of integer type. Apply the integer promotions
2216 to it and return the promoted value. */
2218 tree
2219 perform_integral_promotions (tree exp)
2221 tree type = TREE_TYPE (exp);
2222 enum tree_code code = TREE_CODE (type);
2224 gcc_assert (INTEGRAL_TYPE_P (type));
2226 /* Convert enums to the result of applying the integer promotions to
2227 their underlying type. */
2228 if (code == ENUMERAL_TYPE)
2230 type = ENUM_UNDERLYING_TYPE (type);
2231 if (c_promoting_integer_type_p (type))
2233 if (TYPE_UNSIGNED (type)
2234 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2235 type = unsigned_type_node;
2236 else
2237 type = integer_type_node;
2240 return convert (type, exp);
2243 /* ??? This should no longer be needed now bit-fields have their
2244 proper types. */
2245 if (TREE_CODE (exp) == COMPONENT_REF
2246 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2248 if (TREE_CODE (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)))
2249 == BITINT_TYPE)
2250 return convert (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)), exp);
2251 /* If it's thinner than an int, promote it like a
2252 c_promoting_integer_type_p, otherwise leave it alone. */
2253 if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2254 TYPE_PRECISION (integer_type_node)) < 0)
2255 return convert (integer_type_node, exp);
2258 if (c_promoting_integer_type_p (type))
2260 /* Preserve unsignedness if not really getting any wider. */
2261 if (TYPE_UNSIGNED (type)
2262 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2263 return convert (unsigned_type_node, exp);
2265 return convert (integer_type_node, exp);
2268 return exp;
2272 /* Perform default promotions for C data used in expressions.
2273 Enumeral types or short or char are converted to int.
2274 In addition, manifest constants symbols are replaced by their values. */
2276 tree
2277 default_conversion (tree exp)
2279 tree orig_exp;
2280 tree type = TREE_TYPE (exp);
2281 enum tree_code code = TREE_CODE (type);
2282 tree promoted_type;
2284 mark_exp_read (exp);
2286 /* Functions and arrays have been converted during parsing. */
2287 gcc_assert (code != FUNCTION_TYPE);
2288 if (code == ARRAY_TYPE)
2289 return exp;
2291 /* Constants can be used directly unless they're not loadable. */
2292 if (TREE_CODE (exp) == CONST_DECL)
2293 exp = DECL_INITIAL (exp);
2295 /* Strip no-op conversions. */
2296 orig_exp = exp;
2297 STRIP_TYPE_NOPS (exp);
2299 copy_warning (exp, orig_exp);
2301 if (code == VOID_TYPE)
2303 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2304 "void value not ignored as it ought to be");
2305 return error_mark_node;
2308 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2309 if (exp == error_mark_node)
2310 return error_mark_node;
2312 promoted_type = targetm.promoted_type (type);
2313 if (promoted_type)
2314 return convert (promoted_type, exp);
2316 if (INTEGRAL_TYPE_P (type))
2317 return perform_integral_promotions (exp);
2319 return exp;
2322 /* Look up COMPONENT in a structure or union TYPE.
2324 If the component name is not found, returns NULL_TREE. Otherwise,
2325 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2326 stepping down the chain to the component, which is in the last
2327 TREE_VALUE of the list. Normally the list is of length one, but if
2328 the component is embedded within (nested) anonymous structures or
2329 unions, the list steps down the chain to the component. */
2331 static tree
2332 lookup_field (tree type, tree component)
2334 tree field;
2336 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2337 to the field elements. Use a binary search on this array to quickly
2338 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2339 will always be set for structures which have many elements.
2341 Duplicate field checking replaces duplicates with NULL_TREE so
2342 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2343 case just iterate using DECL_CHAIN. */
2345 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2346 && !seen_error ())
2348 int bot, top, half;
2349 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2351 field = TYPE_FIELDS (type);
2352 bot = 0;
2353 top = TYPE_LANG_SPECIFIC (type)->s->len;
2354 while (top - bot > 1)
2356 half = (top - bot + 1) >> 1;
2357 field = field_array[bot+half];
2359 if (DECL_NAME (field) == NULL_TREE)
2361 /* Step through all anon unions in linear fashion. */
2362 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2364 field = field_array[bot++];
2365 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2367 tree anon = lookup_field (TREE_TYPE (field), component);
2369 if (anon)
2370 return tree_cons (NULL_TREE, field, anon);
2372 /* The Plan 9 compiler permits referring
2373 directly to an anonymous struct/union field
2374 using a typedef name. */
2375 if (flag_plan9_extensions
2376 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2377 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2378 == TYPE_DECL)
2379 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2380 == component))
2381 break;
2385 /* Entire record is only anon unions. */
2386 if (bot > top)
2387 return NULL_TREE;
2389 /* Restart the binary search, with new lower bound. */
2390 continue;
2393 if (DECL_NAME (field) == component)
2394 break;
2395 if (DECL_NAME (field) < component)
2396 bot += half;
2397 else
2398 top = bot + half;
2401 if (DECL_NAME (field_array[bot]) == component)
2402 field = field_array[bot];
2403 else if (DECL_NAME (field) != component)
2404 return NULL_TREE;
2406 else
2408 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2410 if (DECL_NAME (field) == NULL_TREE
2411 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2413 tree anon = lookup_field (TREE_TYPE (field), component);
2415 if (anon)
2416 return tree_cons (NULL_TREE, field, anon);
2418 /* The Plan 9 compiler permits referring directly to an
2419 anonymous struct/union field using a typedef
2420 name. */
2421 if (flag_plan9_extensions
2422 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2423 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2424 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2425 == component))
2426 break;
2429 if (DECL_NAME (field) == component)
2430 break;
2433 if (field == NULL_TREE)
2434 return NULL_TREE;
2437 return tree_cons (NULL_TREE, field, NULL_TREE);
2440 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2442 static void
2443 lookup_field_fuzzy_find_candidates (tree type, tree component,
2444 vec<tree> *candidates)
2446 tree field;
2447 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2449 if (DECL_NAME (field) == NULL_TREE
2450 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2451 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2452 candidates);
2454 if (DECL_NAME (field))
2455 candidates->safe_push (DECL_NAME (field));
2459 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2460 rather than returning a TREE_LIST for an exact match. */
2462 static tree
2463 lookup_field_fuzzy (tree type, tree component)
2465 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2467 /* First, gather a list of candidates. */
2468 auto_vec <tree> candidates;
2470 lookup_field_fuzzy_find_candidates (type, component,
2471 &candidates);
2473 return find_closest_identifier (component, &candidates);
2476 /* Support function for build_component_ref's error-handling.
2478 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2479 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2481 static bool
2482 should_suggest_deref_p (tree datum_type)
2484 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2485 allows "." for ptrs; we could be handling a failed attempt
2486 to access a property. */
2487 if (c_dialect_objc ())
2488 return false;
2490 /* Only suggest it for pointers... */
2491 if (TREE_CODE (datum_type) != POINTER_TYPE)
2492 return false;
2494 /* ...to structs/unions. */
2495 tree underlying_type = TREE_TYPE (datum_type);
2496 enum tree_code code = TREE_CODE (underlying_type);
2497 if (code == RECORD_TYPE || code == UNION_TYPE)
2498 return true;
2499 else
2500 return false;
2503 /* Make an expression to refer to the COMPONENT field of structure or
2504 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2505 location of the COMPONENT_REF. COMPONENT_LOC is the location
2506 of COMPONENT. ARROW_LOC is the location of the first -> operand if
2507 it is from -> operator. */
2509 tree
2510 build_component_ref (location_t loc, tree datum, tree component,
2511 location_t component_loc, location_t arrow_loc)
2513 tree type = TREE_TYPE (datum);
2514 enum tree_code code = TREE_CODE (type);
2515 tree field = NULL;
2516 tree ref;
2517 bool datum_lvalue = lvalue_p (datum);
2519 if (!objc_is_public (datum, component))
2520 return error_mark_node;
2522 /* Detect Objective-C property syntax object.property. */
2523 if (c_dialect_objc ()
2524 && (ref = objc_maybe_build_component_ref (datum, component)))
2525 return ref;
2527 /* See if there is a field or component with name COMPONENT. */
2529 if (code == RECORD_TYPE || code == UNION_TYPE)
2531 if (!COMPLETE_TYPE_P (type))
2533 c_incomplete_type_error (loc, NULL_TREE, type);
2534 return error_mark_node;
2537 field = lookup_field (type, component);
2539 if (!field)
2541 tree guessed_id = lookup_field_fuzzy (type, component);
2542 if (guessed_id)
2544 /* Attempt to provide a fixit replacement hint, if
2545 we have a valid range for the component. */
2546 location_t reported_loc
2547 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2548 gcc_rich_location rich_loc (reported_loc);
2549 if (component_loc != UNKNOWN_LOCATION)
2550 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2551 error_at (&rich_loc,
2552 "%qT has no member named %qE; did you mean %qE?",
2553 type, component, guessed_id);
2555 else
2556 error_at (loc, "%qT has no member named %qE", type, component);
2557 return error_mark_node;
2560 /* Accessing elements of atomic structures or unions is undefined
2561 behavior (C11 6.5.2.3#5). */
2562 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2564 if (code == RECORD_TYPE)
2565 warning_at (loc, 0, "accessing a member %qE of an atomic "
2566 "structure %qE", component, datum);
2567 else
2568 warning_at (loc, 0, "accessing a member %qE of an atomic "
2569 "union %qE", component, datum);
2572 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2573 This might be better solved in future the way the C++ front
2574 end does it - by giving the anonymous entities each a
2575 separate name and type, and then have build_component_ref
2576 recursively call itself. We can't do that here. */
2579 tree subdatum = TREE_VALUE (field);
2580 int quals;
2581 tree subtype;
2582 bool use_datum_quals;
2584 if (TREE_TYPE (subdatum) == error_mark_node)
2585 return error_mark_node;
2587 /* If this is an rvalue, it does not have qualifiers in C
2588 standard terms and we must avoid propagating such
2589 qualifiers down to a non-lvalue array that is then
2590 converted to a pointer. */
2591 use_datum_quals = (datum_lvalue
2592 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2594 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2595 if (use_datum_quals)
2596 quals |= TYPE_QUALS (TREE_TYPE (datum));
2597 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2599 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2600 NULL_TREE);
2601 SET_EXPR_LOCATION (ref, loc);
2602 if (TREE_READONLY (subdatum)
2603 || (use_datum_quals && TREE_READONLY (datum)))
2604 TREE_READONLY (ref) = 1;
2605 if (TREE_THIS_VOLATILE (subdatum)
2606 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2607 TREE_THIS_VOLATILE (ref) = 1;
2609 if (TREE_UNAVAILABLE (subdatum))
2610 error_unavailable_use (subdatum, NULL_TREE);
2611 else if (TREE_DEPRECATED (subdatum))
2612 warn_deprecated_use (subdatum, NULL_TREE);
2614 datum = ref;
2616 field = TREE_CHAIN (field);
2618 while (field);
2620 return ref;
2622 else if (should_suggest_deref_p (type))
2624 /* Special-case the error message for "ptr.field" for the case
2625 where the user has confused "." vs "->". */
2626 rich_location richloc (line_table, loc);
2627 if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
2629 richloc.add_fixit_insert_before (arrow_loc, "(*");
2630 richloc.add_fixit_insert_after (arrow_loc, ")");
2631 error_at (&richloc,
2632 "%qE is a pointer to pointer; did you mean to dereference "
2633 "it before applying %<->%> to it?",
2634 TREE_OPERAND (datum, 0));
2636 else
2638 /* "loc" should be the "." token. */
2639 richloc.add_fixit_replace ("->");
2640 error_at (&richloc,
2641 "%qE is a pointer; did you mean to use %<->%>?",
2642 datum);
2644 return error_mark_node;
2646 else if (code != ERROR_MARK)
2647 error_at (loc,
2648 "request for member %qE in something not a structure or union",
2649 component);
2651 return error_mark_node;
2654 /* Given an expression PTR for a pointer, return an expression
2655 for the value pointed to.
2656 ERRORSTRING is the name of the operator to appear in error messages.
2658 LOC is the location to use for the generated tree. */
2660 tree
2661 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2663 tree pointer = default_conversion (ptr);
2664 tree type = TREE_TYPE (pointer);
2665 tree ref;
2667 if (TREE_CODE (type) == POINTER_TYPE)
2669 if (CONVERT_EXPR_P (pointer)
2670 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2672 /* If a warning is issued, mark it to avoid duplicates from
2673 the backend. This only needs to be done at
2674 warn_strict_aliasing > 2. */
2675 if (warn_strict_aliasing > 2)
2676 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2677 type, TREE_OPERAND (pointer, 0)))
2678 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2681 if (TREE_CODE (pointer) == ADDR_EXPR
2682 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2683 == TREE_TYPE (type)))
2685 ref = TREE_OPERAND (pointer, 0);
2686 protected_set_expr_location (ref, loc);
2687 return ref;
2689 else
2691 tree t = TREE_TYPE (type);
2693 ref = build1 (INDIRECT_REF, t, pointer);
2695 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2696 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2698 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2699 so that we get the proper error message if the result is used
2700 to assign to. Also, &* is supposed to be a no-op.
2701 And ANSI C seems to specify that the type of the result
2702 should be the const type. */
2703 /* A de-reference of a pointer to const is not a const. It is valid
2704 to change it via some other pointer. */
2705 TREE_READONLY (ref) = TYPE_READONLY (t);
2706 TREE_SIDE_EFFECTS (ref)
2707 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2708 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2709 protected_set_expr_location (ref, loc);
2710 return ref;
2713 else if (TREE_CODE (pointer) != ERROR_MARK)
2714 invalid_indirection_error (loc, type, errstring);
2716 return error_mark_node;
2719 /* This handles expressions of the form "a[i]", which denotes
2720 an array reference.
2722 This is logically equivalent in C to *(a+i), but we may do it differently.
2723 If A is a variable or a member, we generate a primitive ARRAY_REF.
2724 This avoids forcing the array out of registers, and can work on
2725 arrays that are not lvalues (for example, members of structures returned
2726 by functions).
2728 For vector types, allow vector[i] but not i[vector], and create
2729 *(((type*)&vectortype) + i) for the expression.
2731 LOC is the location to use for the returned expression. */
2733 tree
2734 build_array_ref (location_t loc, tree array, tree index)
2736 tree ret;
2737 bool swapped = false;
2738 if (TREE_TYPE (array) == error_mark_node
2739 || TREE_TYPE (index) == error_mark_node)
2740 return error_mark_node;
2742 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2743 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2744 /* Allow vector[index] but not index[vector]. */
2745 && !gnu_vector_type_p (TREE_TYPE (array)))
2747 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2748 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2750 error_at (loc,
2751 "subscripted value is neither array nor pointer nor vector");
2753 return error_mark_node;
2755 std::swap (array, index);
2756 swapped = true;
2759 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2761 error_at (loc, "array subscript is not an integer");
2762 return error_mark_node;
2765 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2767 error_at (loc, "subscripted value is pointer to function");
2768 return error_mark_node;
2771 /* ??? Existing practice has been to warn only when the char
2772 index is syntactically the index, not for char[array]. */
2773 if (!swapped)
2774 warn_array_subscript_with_type_char (loc, index);
2776 /* Apply default promotions *after* noticing character types. */
2777 index = default_conversion (index);
2778 if (index == error_mark_node)
2779 return error_mark_node;
2781 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
2782 || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
2784 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2785 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2787 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2789 tree rval, type;
2791 /* An array that is indexed by a non-constant
2792 cannot be stored in a register; we must be able to do
2793 address arithmetic on its address.
2794 Likewise an array of elements of variable size. */
2795 if (TREE_CODE (index) != INTEGER_CST
2796 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2797 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2799 if (!c_mark_addressable (array, true))
2800 return error_mark_node;
2802 /* An array that is indexed by a constant value which is not within
2803 the array bounds cannot be stored in a register either; because we
2804 would get a crash in store_bit_field/extract_bit_field when trying
2805 to access a non-existent part of the register. */
2806 if (TREE_CODE (index) == INTEGER_CST
2807 && TYPE_DOMAIN (TREE_TYPE (array))
2808 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2810 if (!c_mark_addressable (array))
2811 return error_mark_node;
2814 if ((pedantic || warn_c90_c99_compat)
2815 && ! was_vector)
2817 tree foo = array;
2818 while (TREE_CODE (foo) == COMPONENT_REF)
2819 foo = TREE_OPERAND (foo, 0);
2820 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2821 pedwarn (loc, OPT_Wpedantic,
2822 "ISO C forbids subscripting %<register%> array");
2823 else if (!lvalue_p (foo))
2824 pedwarn_c90 (loc, OPT_Wpedantic,
2825 "ISO C90 forbids subscripting non-lvalue "
2826 "array");
2829 type = TREE_TYPE (TREE_TYPE (array));
2830 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2831 /* Array ref is const/volatile if the array elements are
2832 or if the array is. */
2833 TREE_READONLY (rval)
2834 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2835 | TREE_READONLY (array));
2836 TREE_SIDE_EFFECTS (rval)
2837 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2838 | TREE_SIDE_EFFECTS (array));
2839 TREE_THIS_VOLATILE (rval)
2840 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2841 /* This was added by rms on 16 Nov 91.
2842 It fixes vol struct foo *a; a->elts[1]
2843 in an inline function.
2844 Hope it doesn't break something else. */
2845 | TREE_THIS_VOLATILE (array));
2846 ret = require_complete_type (loc, rval);
2847 protected_set_expr_location (ret, loc);
2848 if (non_lvalue)
2849 ret = non_lvalue_loc (loc, ret);
2850 return ret;
2852 else
2854 tree ar = default_conversion (array);
2856 if (ar == error_mark_node)
2857 return ar;
2859 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2860 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2862 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2863 index, false),
2864 RO_ARRAY_INDEXING);
2865 if (non_lvalue)
2866 ret = non_lvalue_loc (loc, ret);
2867 return ret;
2871 /* Build an external reference to identifier ID. FUN indicates
2872 whether this will be used for a function call. LOC is the source
2873 location of the identifier. This sets *TYPE to the type of the
2874 identifier, which is not the same as the type of the returned value
2875 for CONST_DECLs defined as enum constants. If the type of the
2876 identifier is not available, *TYPE is set to NULL. */
2877 tree
2878 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2880 tree ref;
2881 tree decl = lookup_name (id);
2883 /* In Objective-C, an instance variable (ivar) may be preferred to
2884 whatever lookup_name() found. */
2885 decl = objc_lookup_ivar (decl, id);
2887 *type = NULL;
2888 if (decl && decl != error_mark_node)
2890 ref = decl;
2891 *type = TREE_TYPE (ref);
2892 if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
2893 error_at (loc, "underspecified %qD referenced in its initializer",
2894 decl);
2896 else if (fun)
2897 /* Implicit function declaration. */
2898 ref = implicitly_declare (loc, id);
2899 else if (decl == error_mark_node)
2900 /* Don't complain about something that's already been
2901 complained about. */
2902 return error_mark_node;
2903 else
2905 undeclared_variable (loc, id);
2906 return error_mark_node;
2909 if (TREE_TYPE (ref) == error_mark_node)
2910 return error_mark_node;
2912 if (TREE_UNAVAILABLE (ref))
2913 error_unavailable_use (ref, NULL_TREE);
2914 else if (TREE_DEPRECATED (ref))
2915 warn_deprecated_use (ref, NULL_TREE);
2917 /* Recursive call does not count as usage. */
2918 if (ref != current_function_decl)
2920 TREE_USED (ref) = 1;
2923 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2925 if (!in_sizeof && !in_typeof)
2926 C_DECL_USED (ref) = 1;
2927 else if (DECL_INITIAL (ref) == NULL_TREE
2928 && DECL_EXTERNAL (ref)
2929 && !TREE_PUBLIC (ref))
2930 record_maybe_used_decl (ref);
2933 if (TREE_CODE (ref) == CONST_DECL)
2935 used_types_insert (TREE_TYPE (ref));
2937 if (warn_cxx_compat
2938 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2939 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2941 warning_at (loc, OPT_Wc___compat,
2942 ("enum constant defined in struct or union "
2943 "is not visible in C++"));
2944 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2947 ref = DECL_INITIAL (ref);
2948 TREE_CONSTANT (ref) = 1;
2950 else if (current_function_decl != NULL_TREE
2951 && !DECL_FILE_SCOPE_P (current_function_decl)
2952 && (VAR_OR_FUNCTION_DECL_P (ref)
2953 || TREE_CODE (ref) == PARM_DECL))
2955 tree context = decl_function_context (ref);
2957 if (context != NULL_TREE && context != current_function_decl)
2958 DECL_NONLOCAL (ref) = 1;
2960 /* C99 6.7.4p3: An inline definition of a function with external
2961 linkage ... shall not contain a reference to an identifier with
2962 internal linkage. */
2963 else if (current_function_decl != NULL_TREE
2964 && DECL_DECLARED_INLINE_P (current_function_decl)
2965 && DECL_EXTERNAL (current_function_decl)
2966 && VAR_OR_FUNCTION_DECL_P (ref)
2967 && (!VAR_P (ref) || TREE_STATIC (ref))
2968 && ! TREE_PUBLIC (ref)
2969 && DECL_CONTEXT (ref) != current_function_decl)
2970 record_inline_static (loc, current_function_decl, ref,
2971 csi_internal);
2973 return ref;
2976 /* Record details of decls possibly used inside sizeof or typeof. */
2977 struct maybe_used_decl
2979 /* The decl. */
2980 tree decl;
2981 /* The level seen at (in_sizeof + in_typeof). */
2982 int level;
2983 /* The next one at this level or above, or NULL. */
2984 struct maybe_used_decl *next;
2987 static struct maybe_used_decl *maybe_used_decls;
2989 /* Record that DECL, an undefined static function reference seen
2990 inside sizeof or typeof, might be used if the operand of sizeof is
2991 a VLA type or the operand of typeof is a variably modified
2992 type. */
2994 static void
2995 record_maybe_used_decl (tree decl)
2997 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2998 t->decl = decl;
2999 t->level = in_sizeof + in_typeof;
3000 t->next = maybe_used_decls;
3001 maybe_used_decls = t;
3004 /* Pop the stack of decls possibly used inside sizeof or typeof. If
3005 USED is false, just discard them. If it is true, mark them used
3006 (if no longer inside sizeof or typeof) or move them to the next
3007 level up (if still inside sizeof or typeof). */
3009 void
3010 pop_maybe_used (bool used)
3012 struct maybe_used_decl *p = maybe_used_decls;
3013 int cur_level = in_sizeof + in_typeof;
3014 while (p && p->level > cur_level)
3016 if (used)
3018 if (cur_level == 0)
3019 C_DECL_USED (p->decl) = 1;
3020 else
3021 p->level = cur_level;
3023 p = p->next;
3025 if (!used || cur_level == 0)
3026 maybe_used_decls = p;
3029 /* Return the result of sizeof applied to EXPR. */
3031 struct c_expr
3032 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
3034 struct c_expr ret;
3035 if (expr.value == error_mark_node)
3037 ret.value = error_mark_node;
3038 ret.original_code = ERROR_MARK;
3039 ret.original_type = NULL;
3040 ret.m_decimal = 0;
3041 pop_maybe_used (false);
3043 else
3045 bool expr_const_operands = true;
3047 if (TREE_CODE (expr.value) == PARM_DECL
3048 && C_ARRAY_PARAMETER (expr.value))
3050 auto_diagnostic_group d;
3051 if (warning_at (loc, OPT_Wsizeof_array_argument,
3052 "%<sizeof%> on array function parameter %qE will "
3053 "return size of %qT", expr.value,
3054 TREE_TYPE (expr.value)))
3055 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3057 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3058 &expr_const_operands);
3059 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3060 c_last_sizeof_arg = expr.value;
3061 c_last_sizeof_loc = loc;
3062 ret.original_code = SIZEOF_EXPR;
3063 ret.original_type = NULL;
3064 ret.m_decimal = 0;
3065 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3067 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3068 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3069 folded_expr, ret.value);
3070 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3071 SET_EXPR_LOCATION (ret.value, loc);
3073 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3075 return ret;
3078 /* Return the result of sizeof applied to T, a structure for the type
3079 name passed to sizeof (rather than the type itself). LOC is the
3080 location of the original expression. */
3082 struct c_expr
3083 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3085 tree type;
3086 struct c_expr ret;
3087 tree type_expr = NULL_TREE;
3088 bool type_expr_const = true;
3089 type = groktypename (t, &type_expr, &type_expr_const);
3090 ret.value = c_sizeof (loc, type);
3091 c_last_sizeof_arg = type;
3092 c_last_sizeof_loc = loc;
3093 ret.original_code = SIZEOF_EXPR;
3094 ret.original_type = NULL;
3095 ret.m_decimal = 0;
3096 if (type == error_mark_node)
3098 ret.value = error_mark_node;
3099 ret.original_code = ERROR_MARK;
3101 else
3102 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3103 && C_TYPE_VARIABLE_SIZE (type))
3105 /* If the type is a [*] array, it is a VLA but is represented as
3106 having a size of zero. In such a case we must ensure that
3107 the result of sizeof does not get folded to a constant by
3108 c_fully_fold, because if the size is evaluated the result is
3109 not constant and so constraints on zero or negative size
3110 arrays must not be applied when this sizeof call is inside
3111 another array declarator. */
3112 if (!type_expr)
3113 type_expr = integer_zero_node;
3114 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3115 type_expr, ret.value);
3116 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3118 pop_maybe_used (type != error_mark_node
3119 ? C_TYPE_VARIABLE_SIZE (type) : false);
3120 return ret;
3123 /* Build a function call to function FUNCTION with parameters PARAMS.
3124 The function call is at LOC.
3125 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3126 TREE_VALUE of each node is a parameter-expression.
3127 FUNCTION's data type may be a function type or a pointer-to-function. */
3129 tree
3130 build_function_call (location_t loc, tree function, tree params)
3132 vec<tree, va_gc> *v;
3133 tree ret;
3135 vec_alloc (v, list_length (params));
3136 for (; params; params = TREE_CHAIN (params))
3137 v->quick_push (TREE_VALUE (params));
3138 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3139 vec_free (v);
3140 return ret;
3143 /* Give a note about the location of the declaration of DECL. */
3145 static void
3146 inform_declaration (tree decl)
3148 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3149 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3150 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3153 /* Build a function call to function FUNCTION with parameters PARAMS.
3154 If FUNCTION is the result of resolving an overloaded target built-in,
3155 ORIG_FUNDECL is the original function decl, otherwise it is null.
3156 ORIGTYPES, if not NULL, is a vector of types; each element is
3157 either NULL or the original type of the corresponding element in
3158 PARAMS. The original type may differ from TREE_TYPE of the
3159 parameter for enums. FUNCTION's data type may be a function type
3160 or pointer-to-function. This function changes the elements of
3161 PARAMS. */
3163 tree
3164 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3165 tree function, vec<tree, va_gc> *params,
3166 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3168 tree fntype, fundecl = NULL_TREE;
3169 tree name = NULL_TREE, result;
3170 tree tem;
3171 int nargs;
3172 tree *argarray;
3175 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3176 STRIP_TYPE_NOPS (function);
3178 /* Convert anything with function type to a pointer-to-function. */
3179 if (TREE_CODE (function) == FUNCTION_DECL)
3181 name = DECL_NAME (function);
3183 if (flag_tm)
3184 tm_malloc_replacement (function);
3185 fundecl = function;
3186 if (!orig_fundecl)
3187 orig_fundecl = fundecl;
3188 /* Atomic functions have type checking/casting already done. They are
3189 often rewritten and don't match the original parameter list. */
3190 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3191 origtypes = NULL;
3193 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3194 function = function_to_pointer_conversion (loc, function);
3196 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3197 expressions, like those used for ObjC messenger dispatches. */
3198 if (params && !params->is_empty ())
3199 function = objc_rewrite_function_call (function, (*params)[0]);
3201 function = c_fully_fold (function, false, NULL);
3203 fntype = TREE_TYPE (function);
3205 if (TREE_CODE (fntype) == ERROR_MARK)
3206 return error_mark_node;
3208 if (!(TREE_CODE (fntype) == POINTER_TYPE
3209 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3211 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3212 error_at (loc,
3213 "called object %qE is not a function or function pointer",
3214 function);
3215 else if (DECL_P (function))
3217 error_at (loc,
3218 "called object %qD is not a function or function pointer",
3219 function);
3220 inform_declaration (function);
3222 else
3223 error_at (loc,
3224 "called object is not a function or function pointer");
3225 return error_mark_node;
3228 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3229 current_function_returns_abnormally = 1;
3231 /* fntype now gets the type of function pointed to. */
3232 fntype = TREE_TYPE (fntype);
3233 tree return_type = TREE_TYPE (fntype);
3235 /* Convert the parameters to the types declared in the
3236 function prototype, or apply default promotions. */
3238 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3239 origtypes, function, fundecl);
3240 if (nargs < 0)
3241 return error_mark_node;
3243 /* Check that the function is called through a compatible prototype.
3244 If it is not, warn. */
3245 if (CONVERT_EXPR_P (function)
3246 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3247 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3248 && !comptypes (fntype, TREE_TYPE (tem)))
3250 /* This situation leads to run-time undefined behavior. We can't,
3251 therefore, simply error unless we can prove that all possible
3252 executions of the program must execute the code. */
3253 warning_at (loc, 0, "function called through a non-compatible type");
3255 if (VOID_TYPE_P (return_type)
3256 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3257 pedwarn (loc, 0,
3258 "function with qualified void return type called");
3261 argarray = vec_safe_address (params);
3263 /* Check that arguments to builtin functions match the expectations. */
3264 if (fundecl
3265 && fndecl_built_in_p (fundecl)
3266 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3267 orig_fundecl, nargs, argarray))
3268 return error_mark_node;
3270 /* Check that the arguments to the function are valid. */
3271 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3272 nargs, argarray, &arg_loc);
3274 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
3275 && !VOID_TYPE_P (return_type))
3276 return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
3277 if (name != NULL_TREE
3278 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3280 if (require_constant_value)
3281 result
3282 = fold_build_call_array_initializer_loc (loc, return_type,
3283 function, nargs, argarray);
3284 else
3285 result = fold_build_call_array_loc (loc, return_type,
3286 function, nargs, argarray);
3287 if (TREE_CODE (result) == NOP_EXPR
3288 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3289 STRIP_TYPE_NOPS (result);
3291 else
3292 result = build_call_array_loc (loc, return_type,
3293 function, nargs, argarray);
3294 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3295 later. */
3296 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3297 suppress_warning (result, OPT_Wnonnull);
3299 /* In this improbable scenario, a nested function returns a VM type.
3300 Create a TARGET_EXPR so that the call always has a LHS, much as
3301 what the C++ FE does for functions returning non-PODs. */
3302 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (fntype)))
3304 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3305 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3306 NULL_TREE, NULL_TREE);
3309 if (VOID_TYPE_P (TREE_TYPE (result)))
3311 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3312 pedwarn (loc, 0,
3313 "function with qualified void return type called");
3314 return result;
3316 return require_complete_type (loc, result);
3319 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3321 tree
3322 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3323 tree function, vec<tree, va_gc> *params,
3324 vec<tree, va_gc> *origtypes)
3326 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3327 STRIP_TYPE_NOPS (function);
3329 /* Convert anything with function type to a pointer-to-function. */
3330 if (TREE_CODE (function) == FUNCTION_DECL)
3332 /* Implement type-directed function overloading for builtins.
3333 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3334 handle all the type checking. The result is a complete expression
3335 that implements this function call. */
3336 tree tem = resolve_overloaded_builtin (loc, function, params);
3337 if (tem)
3338 return tem;
3340 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3343 /* Helper for convert_arguments called to convert the VALue of argument
3344 number ARGNUM from ORIGTYPE to the corresponding parameter number
3345 PARMNUM and TYPE.
3346 PLOC is the location where the conversion is being performed.
3347 FUNCTION and FUNDECL are the same as in convert_arguments.
3348 VALTYPE is the original type of VAL before the conversion and,
3349 for EXCESS_PRECISION_EXPR, the operand of the expression.
3350 NPC is true if VAL represents the null pointer constant (VAL itself
3351 will have been folded to an integer constant).
3352 RNAME is the same as FUNCTION except in Objective C when it's
3353 the function selector.
3354 EXCESS_PRECISION is true when VAL was originally represented
3355 as EXCESS_PRECISION_EXPR.
3356 WARNOPT is the same as in convert_for_assignment. */
3358 static tree
3359 convert_argument (location_t ploc, tree function, tree fundecl,
3360 tree type, tree origtype, tree val, tree valtype,
3361 bool npc, tree rname, int parmnum, int argnum,
3362 bool excess_precision, int warnopt)
3364 /* Formal parm type is specified by a function prototype. */
3366 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3368 error_at (ploc, "type of formal parameter %d is incomplete",
3369 parmnum + 1);
3370 return error_mark_node;
3373 /* Optionally warn about conversions that differ from the default
3374 conversions. */
3375 if (warn_traditional_conversion || warn_traditional)
3377 if (INTEGRAL_TYPE_P (type)
3378 && SCALAR_FLOAT_TYPE_P (valtype))
3379 warning_at (ploc, OPT_Wtraditional_conversion,
3380 "passing argument %d of %qE as integer rather "
3381 "than floating due to prototype",
3382 argnum, rname);
3383 if (INTEGRAL_TYPE_P (type)
3384 && TREE_CODE (valtype) == COMPLEX_TYPE)
3385 warning_at (ploc, OPT_Wtraditional_conversion,
3386 "passing argument %d of %qE as integer rather "
3387 "than complex due to prototype",
3388 argnum, rname);
3389 else if (TREE_CODE (type) == COMPLEX_TYPE
3390 && SCALAR_FLOAT_TYPE_P (valtype))
3391 warning_at (ploc, OPT_Wtraditional_conversion,
3392 "passing argument %d of %qE as complex rather "
3393 "than floating due to prototype",
3394 argnum, rname);
3395 else if (SCALAR_FLOAT_TYPE_P (type)
3396 && INTEGRAL_TYPE_P (valtype))
3397 warning_at (ploc, OPT_Wtraditional_conversion,
3398 "passing argument %d of %qE as floating rather "
3399 "than integer due to prototype",
3400 argnum, rname);
3401 else if (TREE_CODE (type) == COMPLEX_TYPE
3402 && INTEGRAL_TYPE_P (valtype))
3403 warning_at (ploc, OPT_Wtraditional_conversion,
3404 "passing argument %d of %qE as complex rather "
3405 "than integer due to prototype",
3406 argnum, rname);
3407 else if (SCALAR_FLOAT_TYPE_P (type)
3408 && TREE_CODE (valtype) == COMPLEX_TYPE)
3409 warning_at (ploc, OPT_Wtraditional_conversion,
3410 "passing argument %d of %qE as floating rather "
3411 "than complex due to prototype",
3412 argnum, rname);
3413 /* ??? At some point, messages should be written about
3414 conversions between complex types, but that's too messy
3415 to do now. */
3416 else if (SCALAR_FLOAT_TYPE_P (type)
3417 && SCALAR_FLOAT_TYPE_P (valtype))
3419 unsigned int formal_prec = TYPE_PRECISION (type);
3421 /* Warn if any argument is passed as `float',
3422 since without a prototype it would be `double'. */
3423 if (formal_prec == TYPE_PRECISION (float_type_node)
3424 && type != dfloat32_type_node)
3425 warning_at (ploc, 0,
3426 "passing argument %d of %qE as %<float%> "
3427 "rather than %<double%> due to prototype",
3428 argnum, rname);
3430 /* Warn if mismatch between argument and prototype
3431 for decimal float types. Warn of conversions with
3432 binary float types and of precision narrowing due to
3433 prototype. */
3434 else if (type != valtype
3435 && (type == dfloat32_type_node
3436 || type == dfloat64_type_node
3437 || type == dfloat128_type_node
3438 || valtype == dfloat32_type_node
3439 || valtype == dfloat64_type_node
3440 || valtype == dfloat128_type_node)
3441 && (formal_prec
3442 <= TYPE_PRECISION (valtype)
3443 || (type == dfloat128_type_node
3444 && (valtype
3445 != dfloat64_type_node
3446 && (valtype
3447 != dfloat32_type_node)))
3448 || (type == dfloat64_type_node
3449 && (valtype
3450 != dfloat32_type_node))))
3451 warning_at (ploc, 0,
3452 "passing argument %d of %qE as %qT "
3453 "rather than %qT due to prototype",
3454 argnum, rname, type, valtype);
3457 /* Detect integer changing in width or signedness.
3458 These warnings are only activated with
3459 -Wtraditional-conversion, not with -Wtraditional. */
3460 else if (warn_traditional_conversion
3461 && INTEGRAL_TYPE_P (type)
3462 && INTEGRAL_TYPE_P (valtype))
3464 unsigned int formal_prec = TYPE_PRECISION (type);
3465 tree would_have_been = default_conversion (val);
3466 tree type1 = TREE_TYPE (would_have_been);
3468 if (val == error_mark_node)
3469 /* VAL could have been of incomplete type. */;
3470 else if (TREE_CODE (type) == ENUMERAL_TYPE
3471 && (TYPE_MAIN_VARIANT (type)
3472 == TYPE_MAIN_VARIANT (valtype)))
3473 /* No warning if function asks for enum
3474 and the actual arg is that enum type. */
3476 else if (formal_prec != TYPE_PRECISION (type1))
3477 warning_at (ploc, OPT_Wtraditional_conversion,
3478 "passing argument %d of %qE "
3479 "with different width due to prototype",
3480 argnum, rname);
3481 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3483 /* Don't complain if the formal parameter type
3484 is an enum, because we can't tell now whether
3485 the value was an enum--even the same enum. */
3486 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3488 else if (TREE_CODE (val) == INTEGER_CST
3489 && int_fits_type_p (val, type))
3490 /* Change in signedness doesn't matter
3491 if a constant value is unaffected. */
3493 /* If the value is extended from a narrower
3494 unsigned type, it doesn't matter whether we
3495 pass it as signed or unsigned; the value
3496 certainly is the same either way. */
3497 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3498 && TYPE_UNSIGNED (valtype))
3500 else if (TYPE_UNSIGNED (type))
3501 warning_at (ploc, OPT_Wtraditional_conversion,
3502 "passing argument %d of %qE "
3503 "as unsigned due to prototype",
3504 argnum, rname);
3505 else
3506 warning_at (ploc, OPT_Wtraditional_conversion,
3507 "passing argument %d of %qE "
3508 "as signed due to prototype",
3509 argnum, rname);
3513 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3514 sake of better warnings from convert_and_check. */
3515 if (excess_precision)
3516 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3518 tree parmval = convert_for_assignment (ploc, ploc, type,
3519 val, origtype, ic_argpass,
3520 npc, fundecl, function,
3521 parmnum + 1, warnopt);
3523 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3524 && INTEGRAL_TYPE_P (type)
3525 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3526 parmval = default_conversion (parmval);
3528 return parmval;
3531 /* Convert the argument expressions in the vector VALUES
3532 to the types in the list TYPELIST.
3534 If TYPELIST is exhausted, or when an element has NULL as its type,
3535 perform the default conversions.
3537 ORIGTYPES is the original types of the expressions in VALUES. This
3538 holds the type of enum values which have been converted to integral
3539 types. It may be NULL.
3541 FUNCTION is a tree for the called function. It is used only for
3542 error messages, where it is formatted with %qE.
3544 This is also where warnings about wrong number of args are generated.
3546 ARG_LOC are locations of function arguments (if any).
3548 Returns the actual number of arguments processed (which may be less
3549 than the length of VALUES in some error situations), or -1 on
3550 failure. */
3552 static int
3553 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3554 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3555 tree function, tree fundecl)
3557 unsigned int parmnum;
3558 bool error_args = false;
3559 const bool type_generic = fundecl
3560 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3561 bool type_generic_remove_excess_precision = false;
3562 bool type_generic_overflow_p = false;
3563 tree selector;
3565 /* Change pointer to function to the function itself for
3566 diagnostics. */
3567 if (TREE_CODE (function) == ADDR_EXPR
3568 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3569 function = TREE_OPERAND (function, 0);
3571 /* Handle an ObjC selector specially for diagnostics. */
3572 selector = objc_message_selector ();
3574 /* For a call to a built-in function declared without a prototype,
3575 set to the built-in function's argument list. */
3576 tree builtin_typelist = NULL_TREE;
3578 /* For type-generic built-in functions, determine whether excess
3579 precision should be removed (classification) or not
3580 (comparison). */
3581 if (fundecl
3582 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3584 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3585 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3587 /* For a call to a built-in function declared without a prototype
3588 use the types of the parameters of the internal built-in to
3589 match those of the arguments to. */
3590 if (tree bdecl = builtin_decl_explicit (code))
3591 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3594 /* For type-generic built-in functions, determine whether excess
3595 precision should be removed (classification) or not
3596 (comparison). */
3597 if (type_generic)
3598 switch (code)
3600 case BUILT_IN_ISFINITE:
3601 case BUILT_IN_ISINF:
3602 case BUILT_IN_ISINF_SIGN:
3603 case BUILT_IN_ISNAN:
3604 case BUILT_IN_ISNORMAL:
3605 case BUILT_IN_ISSIGNALING:
3606 case BUILT_IN_FPCLASSIFY:
3607 type_generic_remove_excess_precision = true;
3608 break;
3610 case BUILT_IN_ADD_OVERFLOW_P:
3611 case BUILT_IN_SUB_OVERFLOW_P:
3612 case BUILT_IN_MUL_OVERFLOW_P:
3613 /* The last argument of these type-generic builtins
3614 should not be promoted. */
3615 type_generic_overflow_p = true;
3616 break;
3618 default:
3619 break;
3623 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3624 individual converted arguments. */
3626 tree typetail, builtin_typetail, val;
3627 for (typetail = typelist,
3628 builtin_typetail = builtin_typelist,
3629 parmnum = 0;
3630 values && values->iterate (parmnum, &val);
3631 ++parmnum)
3633 /* The type of the function parameter (if it was declared with one). */
3634 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3635 /* The type of the built-in function parameter (if the function
3636 is a built-in). Used to detect type incompatibilities in
3637 calls to built-ins declared without a prototype. */
3638 tree builtin_type = (builtin_typetail
3639 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3640 /* The original type of the argument being passed to the function. */
3641 tree valtype = TREE_TYPE (val);
3642 /* The called function (or function selector in Objective C). */
3643 tree rname = function;
3644 int argnum = parmnum + 1;
3645 const char *invalid_func_diag;
3646 /* Set for EXCESS_PRECISION_EXPR arguments. */
3647 bool excess_precision = false;
3648 /* The value of the argument after conversion to the type
3649 of the function parameter it is passed to. */
3650 tree parmval;
3651 /* Some __atomic_* builtins have additional hidden argument at
3652 position 0. */
3653 location_t ploc
3654 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3655 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3656 : input_location;
3658 if (type == void_type_node)
3660 if (selector)
3661 error_at (loc, "too many arguments to method %qE", selector);
3662 else
3663 error_at (loc, "too many arguments to function %qE", function);
3664 inform_declaration (fundecl);
3665 return error_args ? -1 : (int) parmnum;
3668 if (builtin_type == void_type_node)
3670 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3671 "too many arguments to built-in function %qE "
3672 "expecting %d", function, parmnum))
3673 inform_declaration (fundecl);
3674 builtin_typetail = NULL_TREE;
3677 if (selector && argnum > 2)
3679 rname = selector;
3680 argnum -= 2;
3683 /* Determine if VAL is a null pointer constant before folding it. */
3684 bool npc = null_pointer_constant_p (val);
3686 /* If there is excess precision and a prototype, convert once to
3687 the required type rather than converting via the semantic
3688 type. Likewise without a prototype a float value represented
3689 as long double should be converted once to double. But for
3690 type-generic classification functions excess precision must
3691 be removed here. */
3692 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3693 && (type || !type_generic || !type_generic_remove_excess_precision))
3695 val = TREE_OPERAND (val, 0);
3696 excess_precision = true;
3698 val = c_fully_fold (val, false, NULL);
3699 STRIP_TYPE_NOPS (val);
3701 val = require_complete_type (ploc, val);
3703 /* Some floating-point arguments must be promoted to double when
3704 no type is specified by a prototype. This applies to
3705 arguments of type float, and to architecture-specific types
3706 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3707 bool promote_float_arg = false;
3708 if (type == NULL_TREE
3709 && TREE_CODE (valtype) == REAL_TYPE
3710 && (TYPE_PRECISION (valtype)
3711 <= TYPE_PRECISION (double_type_node))
3712 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3713 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3714 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3716 /* Promote this argument, unless it has a _FloatN or
3717 _FloatNx type. */
3718 promote_float_arg = true;
3719 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3720 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3722 promote_float_arg = false;
3723 break;
3725 /* Don't promote __bf16 either. */
3726 if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
3727 promote_float_arg = false;
3730 if (type != NULL_TREE)
3732 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3733 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3734 val, valtype, npc, rname, parmnum, argnum,
3735 excess_precision, 0);
3737 else if (promote_float_arg)
3739 if (type_generic)
3740 parmval = val;
3741 else
3743 /* Convert `float' to `double'. */
3744 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3745 warning_at (ploc, OPT_Wdouble_promotion,
3746 "implicit conversion from %qT to %qT when passing "
3747 "argument to function",
3748 valtype, double_type_node);
3749 parmval = convert (double_type_node, val);
3752 else if ((excess_precision && !type_generic)
3753 || (type_generic_overflow_p && parmnum == 2))
3754 /* A "double" argument with excess precision being passed
3755 without a prototype or in variable arguments.
3756 The last argument of __builtin_*_overflow_p should not be
3757 promoted. */
3758 parmval = convert (valtype, val);
3759 else if ((invalid_func_diag =
3760 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3762 error (invalid_func_diag);
3763 return -1;
3765 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3767 return -1;
3769 else
3770 /* Convert `short' and `char' to full-size `int'. */
3771 parmval = default_conversion (val);
3773 (*values)[parmnum] = parmval;
3774 if (parmval == error_mark_node)
3775 error_args = true;
3777 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3779 /* For a call to a built-in function declared without a prototype,
3780 perform the conversions from the argument to the expected type
3781 but issue warnings rather than errors for any mismatches.
3782 Ignore the converted argument and use the PARMVAL obtained
3783 above by applying default conversions instead. */
3784 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3785 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3786 val, valtype, npc, rname, parmnum, argnum,
3787 excess_precision,
3788 OPT_Wbuiltin_declaration_mismatch);
3791 if (typetail)
3792 typetail = TREE_CHAIN (typetail);
3794 if (builtin_typetail)
3795 builtin_typetail = TREE_CHAIN (builtin_typetail);
3798 gcc_assert (parmnum == vec_safe_length (values));
3800 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3802 error_at (loc, "too few arguments to function %qE", function);
3803 inform_declaration (fundecl);
3804 return -1;
3807 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3809 unsigned nargs = parmnum;
3810 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3811 ++nargs;
3813 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3814 "too few arguments to built-in function %qE "
3815 "expecting %u", function, nargs - 1))
3816 inform_declaration (fundecl);
3819 return error_args ? -1 : (int) parmnum;
3822 /* This is the entry point used by the parser to build unary operators
3823 in the input. CODE, a tree_code, specifies the unary operator, and
3824 ARG is the operand. For unary plus, the C parser currently uses
3825 CONVERT_EXPR for code.
3827 LOC is the location to use for the tree generated.
3830 struct c_expr
3831 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3833 struct c_expr result;
3835 result.original_code = code;
3836 result.original_type = NULL;
3837 result.m_decimal = 0;
3839 if (reject_gcc_builtin (arg.value))
3841 result.value = error_mark_node;
3843 else
3845 result.value = build_unary_op (loc, code, arg.value, false);
3847 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3848 overflow_warning (loc, result.value, arg.value);
3851 /* We are typically called when parsing a prefix token at LOC acting on
3852 ARG. Reflect this by updating the source range of the result to
3853 start at LOC and end at the end of ARG. */
3854 set_c_expr_source_range (&result,
3855 loc, arg.get_finish ());
3857 return result;
3860 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3862 bool
3863 char_type_p (tree type)
3865 return (type == char_type_node
3866 || type == unsigned_char_type_node
3867 || type == signed_char_type_node
3868 || type == char16_type_node
3869 || type == char32_type_node);
3872 /* This is the entry point used by the parser to build binary operators
3873 in the input. CODE, a tree_code, specifies the binary operator, and
3874 ARG1 and ARG2 are the operands. In addition to constructing the
3875 expression, we check for operands that were written with other binary
3876 operators in a way that is likely to confuse the user.
3878 LOCATION is the location of the binary operator. */
3880 struct c_expr
3881 parser_build_binary_op (location_t location, enum tree_code code,
3882 struct c_expr arg1, struct c_expr arg2)
3884 struct c_expr result;
3885 result.m_decimal = 0;
3887 enum tree_code code1 = arg1.original_code;
3888 enum tree_code code2 = arg2.original_code;
3889 tree type1 = (arg1.original_type
3890 ? arg1.original_type
3891 : TREE_TYPE (arg1.value));
3892 tree type2 = (arg2.original_type
3893 ? arg2.original_type
3894 : TREE_TYPE (arg2.value));
3896 result.value = build_binary_op (location, code,
3897 arg1.value, arg2.value, true);
3898 result.original_code = code;
3899 result.original_type = NULL;
3900 result.m_decimal = 0;
3902 if (TREE_CODE (result.value) == ERROR_MARK)
3904 set_c_expr_source_range (&result,
3905 arg1.get_start (),
3906 arg2.get_finish ());
3907 return result;
3910 if (location != UNKNOWN_LOCATION)
3911 protected_set_expr_location (result.value, location);
3913 set_c_expr_source_range (&result,
3914 arg1.get_start (),
3915 arg2.get_finish ());
3917 /* Check for cases such as x+y<<z which users are likely
3918 to misinterpret. */
3919 if (warn_parentheses)
3920 warn_about_parentheses (location, code, code1, arg1.value, code2,
3921 arg2.value);
3923 if (warn_logical_op)
3924 warn_logical_operator (location, code, TREE_TYPE (result.value),
3925 code1, arg1.value, code2, arg2.value);
3927 if (warn_tautological_compare)
3929 tree lhs = arg1.value;
3930 tree rhs = arg2.value;
3931 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3933 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3934 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3935 lhs = NULL_TREE;
3936 else
3937 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3939 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3941 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3942 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3943 rhs = NULL_TREE;
3944 else
3945 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3947 if (lhs != NULL_TREE && rhs != NULL_TREE)
3948 warn_tautological_cmp (location, code, lhs, rhs);
3951 if (warn_logical_not_paren
3952 && TREE_CODE_CLASS (code) == tcc_comparison
3953 && code1 == TRUTH_NOT_EXPR
3954 && code2 != TRUTH_NOT_EXPR
3955 /* Avoid warning for !!x == y. */
3956 && (TREE_CODE (arg1.value) != NE_EXPR
3957 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3959 /* Avoid warning for !b == y where b has _Bool type. */
3960 tree t = integer_zero_node;
3961 if (TREE_CODE (arg1.value) == EQ_EXPR
3962 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3963 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3965 t = TREE_OPERAND (arg1.value, 0);
3968 if (TREE_TYPE (t) != integer_type_node)
3969 break;
3970 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3971 t = C_MAYBE_CONST_EXPR_EXPR (t);
3972 else if (CONVERT_EXPR_P (t))
3973 t = TREE_OPERAND (t, 0);
3974 else
3975 break;
3977 while (1);
3979 if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
3980 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3983 /* Warn about comparisons against string literals, with the exception
3984 of testing for equality or inequality of a string literal with NULL. */
3985 if (code == EQ_EXPR || code == NE_EXPR)
3987 if ((code1 == STRING_CST
3988 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3989 || (code2 == STRING_CST
3990 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3991 warning_at (location, OPT_Waddress,
3992 "comparison with string literal results in unspecified behavior");
3993 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3994 if (POINTER_TYPE_P (type1)
3995 && null_pointer_constant_p (arg2.value)
3996 && char_type_p (type2))
3998 auto_diagnostic_group d;
3999 if (warning_at (location, OPT_Wpointer_compare,
4000 "comparison between pointer and zero character "
4001 "constant"))
4002 inform (arg1.get_start (),
4003 "did you mean to dereference the pointer?");
4005 else if (POINTER_TYPE_P (type2)
4006 && null_pointer_constant_p (arg1.value)
4007 && char_type_p (type1))
4009 auto_diagnostic_group d;
4010 if (warning_at (location, OPT_Wpointer_compare,
4011 "comparison between pointer and zero character "
4012 "constant"))
4013 inform (arg2.get_start (),
4014 "did you mean to dereference the pointer?");
4017 else if (TREE_CODE_CLASS (code) == tcc_comparison
4018 && (code1 == STRING_CST || code2 == STRING_CST))
4019 warning_at (location, OPT_Waddress,
4020 "comparison with string literal results in unspecified "
4021 "behavior");
4023 if (warn_array_compare
4024 && TREE_CODE_CLASS (code) == tcc_comparison
4025 && TREE_CODE (type1) == ARRAY_TYPE
4026 && TREE_CODE (type2) == ARRAY_TYPE)
4027 do_warn_array_compare (location, code, arg1.value, arg2.value);
4029 if (TREE_OVERFLOW_P (result.value)
4030 && !TREE_OVERFLOW_P (arg1.value)
4031 && !TREE_OVERFLOW_P (arg2.value))
4032 overflow_warning (location, result.value);
4034 /* Warn about comparisons of different enum types. */
4035 if (warn_enum_compare
4036 && TREE_CODE_CLASS (code) == tcc_comparison
4037 && TREE_CODE (type1) == ENUMERAL_TYPE
4038 && TREE_CODE (type2) == ENUMERAL_TYPE
4039 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
4040 warning_at (location, OPT_Wenum_compare,
4041 "comparison between %qT and %qT",
4042 type1, type2);
4044 if (warn_xor_used_as_pow
4045 && code == BIT_XOR_EXPR
4046 && arg1.m_decimal
4047 && arg2.m_decimal)
4048 check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
4049 location,
4050 arg2.get_location (), arg2.value);
4052 return result;
4055 /* Return a tree for the difference of pointers OP0 and OP1.
4056 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
4057 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
4059 static tree
4060 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
4062 tree restype = ptrdiff_type_node;
4063 tree result, inttype;
4065 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
4066 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
4067 tree target_type = TREE_TYPE (TREE_TYPE (op0));
4068 tree orig_op0 = op0;
4069 tree orig_op1 = op1;
4071 /* If the operands point into different address spaces, we need to
4072 explicitly convert them to pointers into the common address space
4073 before we can subtract the numerical address values. */
4074 if (as0 != as1)
4076 addr_space_t as_common;
4077 tree common_type;
4079 /* Determine the common superset address space. This is guaranteed
4080 to exist because the caller verified that comp_target_types
4081 returned non-zero. */
4082 if (!addr_space_superset (as0, as1, &as_common))
4083 gcc_unreachable ();
4085 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4086 op0 = convert (common_type, op0);
4087 op1 = convert (common_type, op1);
4090 /* Determine integer type result of the subtraction. This will usually
4091 be the same as the result type (ptrdiff_t), but may need to be a wider
4092 type if pointers for the address space are wider than ptrdiff_t. */
4093 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4094 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4095 else
4096 inttype = restype;
4098 if (VOID_TYPE_P (target_type))
4099 pedwarn (loc, OPT_Wpointer_arith,
4100 "pointer of type %<void *%> used in subtraction");
4101 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4102 pedwarn (loc, OPT_Wpointer_arith,
4103 "pointer to a function used in subtraction");
4105 if (current_function_decl != NULL_TREE
4106 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4108 op0 = save_expr (op0);
4109 op1 = save_expr (op1);
4111 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4112 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4115 /* First do the subtraction, then build the divide operator
4116 and only convert at the very end.
4117 Do not do default conversions in case restype is a short type. */
4119 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4120 pointers. If some platform cannot provide that, or has a larger
4121 ptrdiff_type to support differences larger than half the address
4122 space, cast the pointers to some larger integer type and do the
4123 computations in that type. */
4124 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4125 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4126 convert (inttype, op1), false);
4127 else
4129 /* Cast away qualifiers. */
4130 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4131 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4132 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4135 /* This generates an error if op1 is pointer to incomplete type. */
4136 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4137 error_at (loc, "arithmetic on pointer to an incomplete type");
4138 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4139 TREE_TYPE (TREE_TYPE (orig_op0))))
4140 verify_type_context (loc, TCTX_POINTER_ARITH,
4141 TREE_TYPE (TREE_TYPE (orig_op1)));
4143 op1 = c_size_in_bytes (target_type);
4145 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4146 error_at (loc, "arithmetic on pointer to an empty aggregate");
4148 /* Divide by the size, in easiest possible way. */
4149 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4150 op0, convert (inttype, op1));
4152 /* Convert to final result type if necessary. */
4153 return convert (restype, result);
4156 /* Expand atomic compound assignments into an appropriate sequence as
4157 specified by the C11 standard section 6.5.16.2.
4159 _Atomic T1 E1
4160 T2 E2
4161 E1 op= E2
4163 This sequence is used for all types for which these operations are
4164 supported.
4166 In addition, built-in versions of the 'fe' prefixed routines may
4167 need to be invoked for floating point (real, complex or vector) when
4168 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4170 T1 newval;
4171 T1 old;
4172 T1 *addr
4173 T2 val
4174 fenv_t fenv
4176 addr = &E1;
4177 val = (E2);
4178 __atomic_load (addr, &old, SEQ_CST);
4179 feholdexcept (&fenv);
4180 loop:
4181 newval = old op val;
4182 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4183 SEQ_CST))
4184 goto done;
4185 feclearexcept (FE_ALL_EXCEPT);
4186 goto loop:
4187 done:
4188 feupdateenv (&fenv);
4190 The compiler will issue the __atomic_fetch_* built-in when possible,
4191 otherwise it will generate the generic form of the atomic operations.
4192 This requires temp(s) and has their address taken. The atomic processing
4193 is smart enough to figure out when the size of an object can utilize
4194 a lock-free version, and convert the built-in call to the appropriate
4195 lock-free routine. The optimizers will then dispose of any temps that
4196 are no longer required, and lock-free implementations are utilized as
4197 long as there is target support for the required size.
4199 If the operator is NOP_EXPR, then this is a simple assignment, and
4200 an __atomic_store is issued to perform the assignment rather than
4201 the above loop. */
4203 /* Build an atomic assignment at LOC, expanding into the proper
4204 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4205 the result of the operation, unless RETURN_OLD_P, in which case
4206 return the old value of LHS (this is only for postincrement and
4207 postdecrement). */
4209 static tree
4210 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4211 tree rhs, bool return_old_p)
4213 tree fndecl, func_call;
4214 vec<tree, va_gc> *params;
4215 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4216 tree old, old_addr;
4217 tree compound_stmt = NULL_TREE;
4218 tree stmt, goto_stmt;
4219 tree loop_label, loop_decl, done_label, done_decl;
4221 tree lhs_type = TREE_TYPE (lhs);
4222 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4223 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4224 tree rhs_semantic_type = TREE_TYPE (rhs);
4225 tree nonatomic_rhs_semantic_type;
4226 tree rhs_type;
4228 gcc_assert (TYPE_ATOMIC (lhs_type));
4230 if (return_old_p)
4231 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4233 /* Allocate enough vector items for a compare_exchange. */
4234 vec_alloc (params, 6);
4236 /* Create a compound statement to hold the sequence of statements
4237 with a loop. */
4238 if (modifycode != NOP_EXPR)
4240 compound_stmt = c_begin_compound_stmt (false);
4242 /* For consistency with build_modify_expr on non-_Atomic,
4243 mark the lhs as read. Also, it would be very hard to match
4244 such expressions in mark_exp_read. */
4245 mark_exp_read (lhs);
4248 /* Remove any excess precision (which is only present here in the
4249 case of compound assignments). */
4250 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4252 gcc_assert (modifycode != NOP_EXPR);
4253 rhs = TREE_OPERAND (rhs, 0);
4255 rhs_type = TREE_TYPE (rhs);
4257 /* Fold the RHS if it hasn't already been folded. */
4258 if (modifycode != NOP_EXPR)
4259 rhs = c_fully_fold (rhs, false, NULL);
4261 /* Remove the qualifiers for the rest of the expressions and create
4262 the VAL temp variable to hold the RHS. */
4263 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4264 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4265 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4266 TYPE_UNQUALIFIED);
4267 val = create_tmp_var_raw (nonatomic_rhs_type);
4268 TREE_ADDRESSABLE (val) = 1;
4269 suppress_warning (val);
4270 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4271 NULL_TREE);
4272 TREE_SIDE_EFFECTS (rhs) = 1;
4273 SET_EXPR_LOCATION (rhs, loc);
4274 if (modifycode != NOP_EXPR)
4275 add_stmt (rhs);
4277 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4278 an atomic_store. */
4279 if (modifycode == NOP_EXPR)
4281 compound_stmt = rhs;
4282 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4283 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4284 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4285 params->quick_push (lhs_addr);
4286 params->quick_push (rhs);
4287 params->quick_push (seq_cst);
4288 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4290 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4291 compound_stmt, func_call);
4293 /* VAL is the value which was stored, return a COMPOUND_STMT of
4294 the statement and that value. */
4295 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4298 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4299 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4300 isn't applicable for such builtins. ??? Do we want to handle enums? */
4301 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4302 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4304 built_in_function fncode;
4305 switch (modifycode)
4307 case PLUS_EXPR:
4308 case POINTER_PLUS_EXPR:
4309 fncode = (return_old_p
4310 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4311 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4312 break;
4313 case MINUS_EXPR:
4314 fncode = (return_old_p
4315 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4316 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4317 break;
4318 case BIT_AND_EXPR:
4319 fncode = (return_old_p
4320 ? BUILT_IN_ATOMIC_FETCH_AND_N
4321 : BUILT_IN_ATOMIC_AND_FETCH_N);
4322 break;
4323 case BIT_IOR_EXPR:
4324 fncode = (return_old_p
4325 ? BUILT_IN_ATOMIC_FETCH_OR_N
4326 : BUILT_IN_ATOMIC_OR_FETCH_N);
4327 break;
4328 case BIT_XOR_EXPR:
4329 fncode = (return_old_p
4330 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4331 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4332 break;
4333 default:
4334 goto cas_loop;
4337 /* We can only use "_1" through "_16" variants of the atomic fetch
4338 built-ins. */
4339 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4340 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4341 goto cas_loop;
4343 /* If this is a pointer type, we need to multiply by the size of
4344 the pointer target type. */
4345 if (POINTER_TYPE_P (lhs_type))
4347 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4348 /* ??? This would introduce -Wdiscarded-qualifiers
4349 warning: __atomic_fetch_* expect volatile void *
4350 type as the first argument. (Assignments between
4351 atomic and non-atomic objects are OK.) */
4352 || TYPE_RESTRICT (lhs_type))
4353 goto cas_loop;
4354 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4355 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4356 convert (ptrdiff_type_node, rhs),
4357 convert (ptrdiff_type_node, sz));
4360 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4361 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4362 fndecl = builtin_decl_explicit (fncode);
4363 params->quick_push (lhs_addr);
4364 params->quick_push (rhs);
4365 params->quick_push (seq_cst);
4366 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4368 newval = create_tmp_var_raw (nonatomic_lhs_type);
4369 TREE_ADDRESSABLE (newval) = 1;
4370 suppress_warning (newval);
4371 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4372 NULL_TREE, NULL_TREE);
4373 SET_EXPR_LOCATION (rhs, loc);
4374 add_stmt (rhs);
4376 /* Finish the compound statement. */
4377 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4379 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4380 the statement and that value. */
4381 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4384 cas_loop:
4385 /* Create the variables and labels required for the op= form. */
4386 old = create_tmp_var_raw (nonatomic_lhs_type);
4387 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4388 TREE_ADDRESSABLE (old) = 1;
4389 suppress_warning (old);
4391 newval = create_tmp_var_raw (nonatomic_lhs_type);
4392 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4393 TREE_ADDRESSABLE (newval) = 1;
4394 suppress_warning (newval);
4396 loop_decl = create_artificial_label (loc);
4397 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4399 done_decl = create_artificial_label (loc);
4400 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4402 /* __atomic_load (addr, &old, SEQ_CST). */
4403 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4404 params->quick_push (lhs_addr);
4405 params->quick_push (old_addr);
4406 params->quick_push (seq_cst);
4407 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4408 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4409 NULL_TREE);
4410 add_stmt (old);
4411 params->truncate (0);
4413 /* Create the expressions for floating-point environment
4414 manipulation, if required. */
4415 bool need_fenv = (flag_trapping_math
4416 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4417 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4418 if (need_fenv)
4419 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4421 if (hold_call)
4422 add_stmt (hold_call);
4424 /* loop: */
4425 add_stmt (loop_label);
4427 /* newval = old + val; */
4428 if (rhs_type != rhs_semantic_type)
4429 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4430 rhs = build_binary_op (loc, modifycode, old, val, true);
4431 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4433 tree eptype = TREE_TYPE (rhs);
4434 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4435 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4437 else
4438 rhs = c_fully_fold (rhs, false, NULL);
4439 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4440 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4441 NULL_TREE, 0);
4442 if (rhs != error_mark_node)
4444 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4445 NULL_TREE);
4446 SET_EXPR_LOCATION (rhs, loc);
4447 add_stmt (rhs);
4450 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4451 goto done; */
4452 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4453 params->quick_push (lhs_addr);
4454 params->quick_push (old_addr);
4455 params->quick_push (newval_addr);
4456 params->quick_push (integer_zero_node);
4457 params->quick_push (seq_cst);
4458 params->quick_push (seq_cst);
4459 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4461 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4462 SET_EXPR_LOCATION (goto_stmt, loc);
4464 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4465 SET_EXPR_LOCATION (stmt, loc);
4466 add_stmt (stmt);
4468 if (clear_call)
4469 add_stmt (clear_call);
4471 /* goto loop; */
4472 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4473 SET_EXPR_LOCATION (goto_stmt, loc);
4474 add_stmt (goto_stmt);
4476 /* done: */
4477 add_stmt (done_label);
4479 if (update_call)
4480 add_stmt (update_call);
4482 /* Finish the compound statement. */
4483 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4485 /* NEWVAL is the value that was successfully stored, return a
4486 COMPOUND_EXPR of the statement and the appropriate value. */
4487 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4488 return_old_p ? old : newval);
4491 /* Construct and perhaps optimize a tree representation
4492 for a unary operation. CODE, a tree_code, specifies the operation
4493 and XARG is the operand.
4494 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4495 promotions (such as from short to int).
4496 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4497 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4498 to pointers in C99.
4500 LOCATION is the location of the operator. */
4502 tree
4503 build_unary_op (location_t location, enum tree_code code, tree xarg,
4504 bool noconvert)
4506 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4507 tree arg = xarg;
4508 tree argtype = NULL_TREE;
4509 enum tree_code typecode;
4510 tree val;
4511 tree ret = error_mark_node;
4512 tree eptype = NULL_TREE;
4513 const char *invalid_op_diag;
4514 bool int_operands;
4516 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4517 if (int_operands)
4518 arg = remove_c_maybe_const_expr (arg);
4520 if (code != ADDR_EXPR)
4521 arg = require_complete_type (location, arg);
4523 typecode = TREE_CODE (TREE_TYPE (arg));
4524 if (typecode == ERROR_MARK)
4525 return error_mark_node;
4526 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4527 typecode = INTEGER_TYPE;
4529 if ((invalid_op_diag
4530 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4532 error_at (location, invalid_op_diag);
4533 return error_mark_node;
4536 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4538 eptype = TREE_TYPE (arg);
4539 arg = TREE_OPERAND (arg, 0);
4542 switch (code)
4544 case CONVERT_EXPR:
4545 /* This is used for unary plus, because a CONVERT_EXPR
4546 is enough to prevent anybody from looking inside for
4547 associativity, but won't generate any code. */
4548 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4549 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4550 || typecode == BITINT_TYPE
4551 || gnu_vector_type_p (TREE_TYPE (arg))))
4553 error_at (location, "wrong type argument to unary plus");
4554 return error_mark_node;
4556 else if (!noconvert)
4557 arg = default_conversion (arg);
4558 arg = non_lvalue_loc (location, arg);
4559 break;
4561 case NEGATE_EXPR:
4562 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4563 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4564 || typecode == BITINT_TYPE
4565 || gnu_vector_type_p (TREE_TYPE (arg))))
4567 error_at (location, "wrong type argument to unary minus");
4568 return error_mark_node;
4570 else if (!noconvert)
4571 arg = default_conversion (arg);
4572 break;
4574 case BIT_NOT_EXPR:
4575 /* ~ works on integer types and non float vectors. */
4576 if (typecode == INTEGER_TYPE
4577 || typecode == BITINT_TYPE
4578 || (gnu_vector_type_p (TREE_TYPE (arg))
4579 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4581 tree e = arg;
4583 /* Warn if the expression has boolean value. */
4584 while (TREE_CODE (e) == COMPOUND_EXPR)
4585 e = TREE_OPERAND (e, 1);
4587 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
4588 || truth_value_p (TREE_CODE (e))))
4590 auto_diagnostic_group d;
4591 if (warning_at (location, OPT_Wbool_operation,
4592 "%<~%> on a boolean expression"))
4594 gcc_rich_location richloc (location);
4595 richloc.add_fixit_insert_before (location, "!");
4596 inform (&richloc, "did you mean to use logical not?");
4599 if (!noconvert)
4600 arg = default_conversion (arg);
4602 else if (typecode == COMPLEX_TYPE)
4604 code = CONJ_EXPR;
4605 pedwarn (location, OPT_Wpedantic,
4606 "ISO C does not support %<~%> for complex conjugation");
4607 if (!noconvert)
4608 arg = default_conversion (arg);
4610 else
4612 error_at (location, "wrong type argument to bit-complement");
4613 return error_mark_node;
4615 break;
4617 case ABS_EXPR:
4618 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4620 error_at (location, "wrong type argument to abs");
4621 return error_mark_node;
4623 else if (!noconvert)
4624 arg = default_conversion (arg);
4625 break;
4627 case ABSU_EXPR:
4628 if (!(typecode == INTEGER_TYPE))
4630 error_at (location, "wrong type argument to absu");
4631 return error_mark_node;
4633 else if (!noconvert)
4634 arg = default_conversion (arg);
4635 break;
4637 case CONJ_EXPR:
4638 /* Conjugating a real value is a no-op, but allow it anyway. */
4639 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4640 || typecode == COMPLEX_TYPE))
4642 error_at (location, "wrong type argument to conjugation");
4643 return error_mark_node;
4645 else if (!noconvert)
4646 arg = default_conversion (arg);
4647 break;
4649 case TRUTH_NOT_EXPR:
4650 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4651 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4652 && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
4653 && typecode != BITINT_TYPE)
4655 error_at (location,
4656 "wrong type argument to unary exclamation mark");
4657 return error_mark_node;
4659 if (int_operands)
4661 arg = c_objc_common_truthvalue_conversion (location, xarg);
4662 arg = remove_c_maybe_const_expr (arg);
4664 else
4665 arg = c_objc_common_truthvalue_conversion (location, arg);
4666 ret = invert_truthvalue_loc (location, arg);
4667 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4668 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4669 location = EXPR_LOCATION (ret);
4670 goto return_build_unary_op;
4672 case REALPART_EXPR:
4673 case IMAGPART_EXPR:
4674 ret = build_real_imag_expr (location, code, arg);
4675 if (ret == error_mark_node)
4676 return error_mark_node;
4677 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4678 eptype = TREE_TYPE (eptype);
4679 goto return_build_unary_op;
4681 case PREINCREMENT_EXPR:
4682 case POSTINCREMENT_EXPR:
4683 case PREDECREMENT_EXPR:
4684 case POSTDECREMENT_EXPR:
4686 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4688 tree inner = build_unary_op (location, code,
4689 C_MAYBE_CONST_EXPR_EXPR (arg),
4690 noconvert);
4691 if (inner == error_mark_node)
4692 return error_mark_node;
4693 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4694 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4695 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4696 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4697 goto return_build_unary_op;
4700 /* Complain about anything that is not a true lvalue. In
4701 Objective-C, skip this check for property_refs. */
4702 if (!objc_is_property_ref (arg)
4703 && !lvalue_or_else (location,
4704 arg, ((code == PREINCREMENT_EXPR
4705 || code == POSTINCREMENT_EXPR)
4706 ? lv_increment
4707 : lv_decrement)))
4708 return error_mark_node;
4710 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4712 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4713 warning_at (location, OPT_Wc___compat,
4714 "increment of enumeration value is invalid in C++");
4715 else
4716 warning_at (location, OPT_Wc___compat,
4717 "decrement of enumeration value is invalid in C++");
4720 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4722 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4723 warning_at (location, OPT_Wbool_operation,
4724 "increment of a boolean expression");
4725 else
4726 warning_at (location, OPT_Wbool_operation,
4727 "decrement of a boolean expression");
4730 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4731 arg = c_fully_fold (arg, false, NULL, true);
4733 bool atomic_op;
4734 atomic_op = really_atomic_lvalue (arg);
4736 /* Increment or decrement the real part of the value,
4737 and don't change the imaginary part. */
4738 if (typecode == COMPLEX_TYPE)
4740 tree real, imag;
4742 pedwarn (location, OPT_Wpedantic,
4743 "ISO C does not support %<++%> and %<--%> on complex types");
4745 if (!atomic_op)
4747 arg = stabilize_reference (arg);
4748 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4749 true);
4750 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4751 true);
4752 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4753 if (real == error_mark_node || imag == error_mark_node)
4754 return error_mark_node;
4755 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4756 real, imag);
4757 goto return_build_unary_op;
4761 /* Report invalid types. */
4763 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4764 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4765 && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
4766 && !gnu_vector_type_p (TREE_TYPE (arg)))
4768 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4769 error_at (location, "wrong type argument to increment");
4770 else
4771 error_at (location, "wrong type argument to decrement");
4773 return error_mark_node;
4777 tree inc;
4779 argtype = TREE_TYPE (arg);
4781 /* Compute the increment. */
4783 if (typecode == POINTER_TYPE)
4785 /* If pointer target is an incomplete type,
4786 we just cannot know how to do the arithmetic. */
4787 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4789 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4790 error_at (location,
4791 "increment of pointer to an incomplete type %qT",
4792 TREE_TYPE (argtype));
4793 else
4794 error_at (location,
4795 "decrement of pointer to an incomplete type %qT",
4796 TREE_TYPE (argtype));
4798 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4799 || VOID_TYPE_P (TREE_TYPE (argtype)))
4801 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4802 pedwarn (location, OPT_Wpointer_arith,
4803 "wrong type argument to increment");
4804 else
4805 pedwarn (location, OPT_Wpointer_arith,
4806 "wrong type argument to decrement");
4808 else
4809 verify_type_context (location, TCTX_POINTER_ARITH,
4810 TREE_TYPE (argtype));
4812 inc = c_size_in_bytes (TREE_TYPE (argtype));
4813 inc = convert_to_ptrofftype_loc (location, inc);
4815 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4817 /* For signed fract types, we invert ++ to -- or
4818 -- to ++, and change inc from 1 to -1, because
4819 it is not possible to represent 1 in signed fract constants.
4820 For unsigned fract types, the result always overflows and
4821 we get an undefined (original) or the maximum value. */
4822 if (code == PREINCREMENT_EXPR)
4823 code = PREDECREMENT_EXPR;
4824 else if (code == PREDECREMENT_EXPR)
4825 code = PREINCREMENT_EXPR;
4826 else if (code == POSTINCREMENT_EXPR)
4827 code = POSTDECREMENT_EXPR;
4828 else /* code == POSTDECREMENT_EXPR */
4829 code = POSTINCREMENT_EXPR;
4831 inc = integer_minus_one_node;
4832 inc = convert (argtype, inc);
4834 else
4836 inc = VECTOR_TYPE_P (argtype)
4837 ? build_one_cst (argtype)
4838 : integer_one_node;
4839 inc = convert (argtype, inc);
4842 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4843 need to ask Objective-C to build the increment or decrement
4844 expression for it. */
4845 if (objc_is_property_ref (arg))
4846 return objc_build_incr_expr_for_property_ref (location, code,
4847 arg, inc);
4849 /* Report a read-only lvalue. */
4850 if (TYPE_READONLY (argtype))
4852 readonly_error (location, arg,
4853 ((code == PREINCREMENT_EXPR
4854 || code == POSTINCREMENT_EXPR)
4855 ? lv_increment : lv_decrement));
4856 return error_mark_node;
4858 else if (TREE_READONLY (arg))
4859 readonly_warning (arg,
4860 ((code == PREINCREMENT_EXPR
4861 || code == POSTINCREMENT_EXPR)
4862 ? lv_increment : lv_decrement));
4864 /* If the argument is atomic, use the special code sequences for
4865 atomic compound assignment. */
4866 if (atomic_op)
4868 arg = stabilize_reference (arg);
4869 ret = build_atomic_assign (location, arg,
4870 ((code == PREINCREMENT_EXPR
4871 || code == POSTINCREMENT_EXPR)
4872 ? PLUS_EXPR
4873 : MINUS_EXPR),
4874 (FRACT_MODE_P (TYPE_MODE (argtype))
4875 ? inc
4876 : integer_one_node),
4877 (code == POSTINCREMENT_EXPR
4878 || code == POSTDECREMENT_EXPR));
4879 goto return_build_unary_op;
4882 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4883 val = boolean_increment (code, arg);
4884 else
4885 val = build2 (code, TREE_TYPE (arg), arg, inc);
4886 TREE_SIDE_EFFECTS (val) = 1;
4887 if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
4888 TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
4889 TYPE_UNQUALIFIED);
4890 ret = val;
4891 goto return_build_unary_op;
4894 case ADDR_EXPR:
4895 /* Note that this operation never does default_conversion. */
4897 /* The operand of unary '&' must be an lvalue (which excludes
4898 expressions of type void), or, in C99, the result of a [] or
4899 unary '*' operator. */
4900 if (VOID_TYPE_P (TREE_TYPE (arg))
4901 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4902 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4903 pedwarn (location, 0, "taking address of expression of type %<void%>");
4905 /* Let &* cancel out to simplify resulting code. */
4906 if (INDIRECT_REF_P (arg))
4908 /* Don't let this be an lvalue. */
4909 if (lvalue_p (TREE_OPERAND (arg, 0)))
4910 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4911 ret = TREE_OPERAND (arg, 0);
4912 goto return_build_unary_op;
4915 /* Anything not already handled and not a true memory reference
4916 or a non-lvalue array is an error. */
4917 if (typecode != FUNCTION_TYPE && !noconvert
4918 && !lvalue_or_else (location, arg, lv_addressof))
4919 return error_mark_node;
4921 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4922 folding later. */
4923 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4925 tree inner = build_unary_op (location, code,
4926 C_MAYBE_CONST_EXPR_EXPR (arg),
4927 noconvert);
4928 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4929 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4930 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4931 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4932 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4933 goto return_build_unary_op;
4936 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4937 argtype = TREE_TYPE (arg);
4939 /* If the lvalue is const or volatile, merge that into the type
4940 to which the address will point. This is only needed
4941 for function types. */
4942 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4943 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4944 && TREE_CODE (argtype) == FUNCTION_TYPE)
4946 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4947 int quals = orig_quals;
4949 if (TREE_READONLY (arg))
4950 quals |= TYPE_QUAL_CONST;
4951 if (TREE_THIS_VOLATILE (arg))
4952 quals |= TYPE_QUAL_VOLATILE;
4954 argtype = c_build_qualified_type (argtype, quals);
4957 switch (TREE_CODE (arg))
4959 case COMPONENT_REF:
4960 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4962 error_at (location, "cannot take address of bit-field %qD",
4963 TREE_OPERAND (arg, 1));
4964 return error_mark_node;
4967 /* fall through */
4969 case ARRAY_REF:
4970 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4972 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4973 && !POINTER_TYPE_P (TREE_TYPE (arg))
4974 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4976 error_at (location, "cannot take address of scalar with "
4977 "reverse storage order");
4978 return error_mark_node;
4981 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4982 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4983 warning_at (location, OPT_Wscalar_storage_order,
4984 "address of array with reverse scalar storage "
4985 "order requested");
4988 default:
4989 break;
4992 if (!c_mark_addressable (arg))
4993 return error_mark_node;
4995 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4996 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4998 argtype = build_pointer_type (argtype);
5000 /* ??? Cope with user tricks that amount to offsetof. Delete this
5001 when we have proper support for integer constant expressions. */
5002 val = get_base_address (arg);
5003 if (val && INDIRECT_REF_P (val)
5004 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5006 ret = fold_offsetof (arg, argtype);
5007 goto return_build_unary_op;
5010 val = build1 (ADDR_EXPR, argtype, arg);
5012 ret = val;
5013 goto return_build_unary_op;
5015 case PAREN_EXPR:
5016 ret = build1 (code, TREE_TYPE (arg), arg);
5017 goto return_build_unary_op;
5019 default:
5020 gcc_unreachable ();
5023 if (argtype == NULL_TREE)
5024 argtype = TREE_TYPE (arg);
5025 if (TREE_CODE (arg) == INTEGER_CST)
5026 ret = (require_constant_value
5027 ? fold_build1_initializer_loc (location, code, argtype, arg)
5028 : fold_build1_loc (location, code, argtype, arg));
5029 else
5030 ret = build1 (code, argtype, arg);
5031 return_build_unary_op:
5032 gcc_assert (ret != error_mark_node);
5033 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
5034 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
5035 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
5036 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
5037 ret = note_integer_operands (ret);
5038 if (eptype)
5039 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5040 protected_set_expr_location (ret, location);
5041 return ret;
5044 /* Return nonzero if REF is an lvalue valid for this language.
5045 Lvalues can be assigned, unless their type has TYPE_READONLY.
5046 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
5048 bool
5049 lvalue_p (const_tree ref)
5051 const enum tree_code code = TREE_CODE (ref);
5053 switch (code)
5055 case REALPART_EXPR:
5056 case IMAGPART_EXPR:
5057 case COMPONENT_REF:
5058 return lvalue_p (TREE_OPERAND (ref, 0));
5060 case C_MAYBE_CONST_EXPR:
5061 return lvalue_p (TREE_OPERAND (ref, 1));
5063 case COMPOUND_LITERAL_EXPR:
5064 case STRING_CST:
5065 return true;
5067 case MEM_REF:
5068 case TARGET_MEM_REF:
5069 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
5070 here as well. */
5071 case INDIRECT_REF:
5072 case ARRAY_REF:
5073 case VAR_DECL:
5074 case PARM_DECL:
5075 case RESULT_DECL:
5076 case ERROR_MARK:
5077 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5078 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5080 case BIND_EXPR:
5081 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5083 default:
5084 return false;
5088 /* Give a warning for storing in something that is read-only in GCC
5089 terms but not const in ISO C terms. */
5091 static void
5092 readonly_warning (tree arg, enum lvalue_use use)
5094 switch (use)
5096 case lv_assign:
5097 warning (0, "assignment of read-only location %qE", arg);
5098 break;
5099 case lv_increment:
5100 warning (0, "increment of read-only location %qE", arg);
5101 break;
5102 case lv_decrement:
5103 warning (0, "decrement of read-only location %qE", arg);
5104 break;
5105 default:
5106 gcc_unreachable ();
5108 return;
5112 /* Return nonzero if REF is an lvalue valid for this language;
5113 otherwise, print an error message and return zero. USE says
5114 how the lvalue is being used and so selects the error message.
5115 LOCATION is the location at which any error should be reported. */
5117 static int
5118 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5120 int win = lvalue_p (ref);
5122 if (!win)
5123 lvalue_error (loc, use);
5125 return win;
5128 /* Mark EXP saying that we need to be able to take the
5129 address of it; it should not be allocated in a register.
5130 Returns true if successful. ARRAY_REF_P is true if this
5131 is for ARRAY_REF construction - in that case we don't want
5132 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5133 it is fine to use ARRAY_REFs for vector subscripts on vector
5134 register variables. */
5136 bool
5137 c_mark_addressable (tree exp, bool array_ref_p)
5139 tree x = exp;
5141 while (1)
5142 switch (TREE_CODE (x))
5144 case VIEW_CONVERT_EXPR:
5145 if (array_ref_p
5146 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5147 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5148 return true;
5149 x = TREE_OPERAND (x, 0);
5150 break;
5152 case COMPONENT_REF:
5153 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5155 error ("cannot take address of bit-field %qD",
5156 TREE_OPERAND (x, 1));
5157 return false;
5159 /* FALLTHRU */
5160 case ADDR_EXPR:
5161 case ARRAY_REF:
5162 case REALPART_EXPR:
5163 case IMAGPART_EXPR:
5164 x = TREE_OPERAND (x, 0);
5165 break;
5167 case COMPOUND_LITERAL_EXPR:
5168 if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
5170 error ("address of register compound literal requested");
5171 return false;
5173 TREE_ADDRESSABLE (x) = 1;
5174 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5175 return true;
5177 case CONSTRUCTOR:
5178 TREE_ADDRESSABLE (x) = 1;
5179 return true;
5181 case VAR_DECL:
5182 case CONST_DECL:
5183 case PARM_DECL:
5184 case RESULT_DECL:
5185 if (C_DECL_REGISTER (x)
5186 && DECL_NONLOCAL (x))
5188 if (TREE_PUBLIC (x) || is_global_var (x))
5190 error
5191 ("global register variable %qD used in nested function", x);
5192 return false;
5194 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5196 else if (C_DECL_REGISTER (x))
5198 if (TREE_PUBLIC (x) || is_global_var (x))
5199 error ("address of global register variable %qD requested", x);
5200 else
5201 error ("address of register variable %qD requested", x);
5202 return false;
5205 /* FALLTHRU */
5206 case FUNCTION_DECL:
5207 TREE_ADDRESSABLE (x) = 1;
5208 /* FALLTHRU */
5209 default:
5210 return true;
5214 /* Convert EXPR to TYPE, warning about conversion problems with
5215 constants. SEMANTIC_TYPE is the type this conversion would use
5216 without excess precision. If SEMANTIC_TYPE is NULL, this function
5217 is equivalent to convert_and_check. This function is a wrapper that
5218 handles conversions that may be different than
5219 the usual ones because of excess precision. */
5221 static tree
5222 ep_convert_and_check (location_t loc, tree type, tree expr,
5223 tree semantic_type)
5225 if (TREE_TYPE (expr) == type)
5226 return expr;
5228 /* For C11, integer conversions may have results with excess
5229 precision. */
5230 if (flag_isoc11 || !semantic_type)
5231 return convert_and_check (loc, type, expr);
5233 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5234 && TREE_TYPE (expr) != semantic_type)
5236 /* For integers, we need to check the real conversion, not
5237 the conversion to the excess precision type. */
5238 expr = convert_and_check (loc, semantic_type, expr);
5240 /* Result type is the excess precision type, which should be
5241 large enough, so do not check. */
5242 return convert (type, expr);
5245 /* If EXPR refers to a built-in declared without a prototype returns
5246 the actual type of the built-in and, if non-null, set *BLTIN to
5247 a pointer to the built-in. Otherwise return the type of EXPR
5248 and clear *BLTIN if non-null. */
5250 static tree
5251 type_or_builtin_type (tree expr, tree *bltin = NULL)
5253 tree dummy;
5254 if (!bltin)
5255 bltin = &dummy;
5257 *bltin = NULL_TREE;
5259 tree type = TREE_TYPE (expr);
5260 if (TREE_CODE (expr) != ADDR_EXPR)
5261 return type;
5263 tree oper = TREE_OPERAND (expr, 0);
5264 if (!DECL_P (oper)
5265 || TREE_CODE (oper) != FUNCTION_DECL
5266 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5267 return type;
5269 built_in_function code = DECL_FUNCTION_CODE (oper);
5270 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5271 return type;
5273 if ((*bltin = builtin_decl_implicit (code)))
5274 type = build_pointer_type (TREE_TYPE (*bltin));
5276 return type;
5279 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5280 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5281 if folded to an integer constant then the unselected half may
5282 contain arbitrary operations not normally permitted in constant
5283 expressions. Set the location of the expression to LOC. */
5285 tree
5286 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5287 tree op1, tree op1_original_type, location_t op1_loc,
5288 tree op2, tree op2_original_type, location_t op2_loc)
5290 tree type1;
5291 tree type2;
5292 enum tree_code code1;
5293 enum tree_code code2;
5294 tree result_type = NULL;
5295 tree semantic_result_type = NULL;
5296 tree orig_op1 = op1, orig_op2 = op2;
5297 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5298 bool ifexp_int_operands;
5299 tree ret;
5301 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5302 if (op1_int_operands)
5303 op1 = remove_c_maybe_const_expr (op1);
5304 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5305 if (op2_int_operands)
5306 op2 = remove_c_maybe_const_expr (op2);
5307 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5308 if (ifexp_int_operands)
5309 ifexp = remove_c_maybe_const_expr (ifexp);
5311 /* Promote both alternatives. */
5313 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5314 op1 = default_conversion (op1);
5315 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5316 op2 = default_conversion (op2);
5318 if (TREE_CODE (ifexp) == ERROR_MARK
5319 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5320 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5321 return error_mark_node;
5323 tree bltin1 = NULL_TREE;
5324 tree bltin2 = NULL_TREE;
5325 type1 = type_or_builtin_type (op1, &bltin1);
5326 code1 = TREE_CODE (type1);
5327 type2 = type_or_builtin_type (op2, &bltin2);
5328 code2 = TREE_CODE (type2);
5330 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5331 return error_mark_node;
5333 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5334 return error_mark_node;
5336 /* C90 does not permit non-lvalue arrays in conditional expressions.
5337 In C99 they will be pointers by now. */
5338 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5340 error_at (colon_loc, "non-lvalue array in conditional expression");
5341 return error_mark_node;
5344 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5345 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5346 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5347 || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
5348 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5349 || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
5351 semantic_result_type = c_common_type (type1, type2);
5352 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5354 op1 = TREE_OPERAND (op1, 0);
5355 type1 = TREE_TYPE (op1);
5356 gcc_assert (TREE_CODE (type1) == code1);
5358 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5360 op2 = TREE_OPERAND (op2, 0);
5361 type2 = TREE_TYPE (op2);
5362 gcc_assert (TREE_CODE (type2) == code2);
5366 if (warn_cxx_compat)
5368 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5369 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5371 if (TREE_CODE (t1) == ENUMERAL_TYPE
5372 && TREE_CODE (t2) == ENUMERAL_TYPE
5373 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5374 warning_at (colon_loc, OPT_Wc___compat,
5375 ("different enum types in conditional is "
5376 "invalid in C++: %qT vs %qT"),
5377 t1, t2);
5380 /* Quickly detect the usual case where op1 and op2 have the same type
5381 after promotion. */
5382 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5384 if (type1 == type2)
5385 result_type = type1;
5386 else
5387 result_type = TYPE_MAIN_VARIANT (type1);
5389 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5390 || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
5391 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5392 || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
5394 /* In C11, a conditional expression between a floating-point
5395 type and an integer type should convert the integer type to
5396 the evaluation format of the floating-point type, with
5397 possible excess precision. */
5398 tree eptype1 = type1;
5399 tree eptype2 = type2;
5400 if (flag_isoc11)
5402 tree eptype;
5403 if (ANY_INTEGRAL_TYPE_P (type1)
5404 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5406 eptype2 = eptype;
5407 if (!semantic_result_type)
5408 semantic_result_type = c_common_type (type1, type2);
5410 else if (ANY_INTEGRAL_TYPE_P (type2)
5411 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5413 eptype1 = eptype;
5414 if (!semantic_result_type)
5415 semantic_result_type = c_common_type (type1, type2);
5418 result_type = c_common_type (eptype1, eptype2);
5419 if (result_type == error_mark_node)
5420 return error_mark_node;
5421 do_warn_double_promotion (result_type, type1, type2,
5422 "implicit conversion from %qT to %qT to "
5423 "match other result of conditional",
5424 colon_loc);
5426 /* If -Wsign-compare, warn here if type1 and type2 have
5427 different signedness. We'll promote the signed to unsigned
5428 and later code won't know it used to be different.
5429 Do this check on the original types, so that explicit casts
5430 will be considered, but default promotions won't. */
5431 if (c_inhibit_evaluation_warnings == 0)
5433 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5434 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5436 if (unsigned_op1 ^ unsigned_op2)
5438 bool ovf;
5440 /* Do not warn if the result type is signed, since the
5441 signed type will only be chosen if it can represent
5442 all the values of the unsigned type. */
5443 if (!TYPE_UNSIGNED (result_type))
5444 /* OK */;
5445 else
5447 bool op1_maybe_const = true;
5448 bool op2_maybe_const = true;
5450 /* Do not warn if the signed quantity is an
5451 unsuffixed integer literal (or some static
5452 constant expression involving such literals) and
5453 it is non-negative. This warning requires the
5454 operands to be folded for best results, so do
5455 that folding in this case even without
5456 warn_sign_compare to avoid warning options
5457 possibly affecting code generation. */
5458 c_inhibit_evaluation_warnings
5459 += (ifexp == truthvalue_false_node);
5460 op1 = c_fully_fold (op1, require_constant_value,
5461 &op1_maybe_const);
5462 c_inhibit_evaluation_warnings
5463 -= (ifexp == truthvalue_false_node);
5465 c_inhibit_evaluation_warnings
5466 += (ifexp == truthvalue_true_node);
5467 op2 = c_fully_fold (op2, require_constant_value,
5468 &op2_maybe_const);
5469 c_inhibit_evaluation_warnings
5470 -= (ifexp == truthvalue_true_node);
5472 if (warn_sign_compare)
5474 if ((unsigned_op2
5475 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5476 || (unsigned_op1
5477 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5478 /* OK */;
5479 else if (unsigned_op2)
5480 warning_at (op1_loc, OPT_Wsign_compare,
5481 "operand of %<?:%> changes signedness from "
5482 "%qT to %qT due to unsignedness of other "
5483 "operand", TREE_TYPE (orig_op1),
5484 TREE_TYPE (orig_op2));
5485 else
5486 warning_at (op2_loc, OPT_Wsign_compare,
5487 "operand of %<?:%> changes signedness from "
5488 "%qT to %qT due to unsignedness of other "
5489 "operand", TREE_TYPE (orig_op2),
5490 TREE_TYPE (orig_op1));
5492 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5493 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5494 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5495 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5500 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5502 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5503 pedwarn (colon_loc, OPT_Wpedantic,
5504 "ISO C forbids conditional expr with only one void side");
5505 result_type = void_type_node;
5507 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5509 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5510 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5511 addr_space_t as_common;
5513 if (comp_target_types (colon_loc, type1, type2))
5514 result_type = common_pointer_type (type1, type2);
5515 else if (null_pointer_constant_p (orig_op1))
5516 result_type = type2;
5517 else if (null_pointer_constant_p (orig_op2))
5518 result_type = type1;
5519 else if (!addr_space_superset (as1, as2, &as_common))
5521 error_at (colon_loc, "pointers to disjoint address spaces "
5522 "used in conditional expression");
5523 return error_mark_node;
5525 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5526 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5527 || (VOID_TYPE_P (TREE_TYPE (type2))
5528 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5530 tree t1 = TREE_TYPE (type1);
5531 tree t2 = TREE_TYPE (type2);
5532 if (!(VOID_TYPE_P (t1)
5533 && !TYPE_ATOMIC (t1)))
5535 /* roles are swapped */
5536 t1 = t2;
5537 t2 = TREE_TYPE (type1);
5539 tree t2_stripped = strip_array_types (t2);
5540 if ((TREE_CODE (t2) == ARRAY_TYPE)
5541 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5543 if (!flag_isoc2x)
5544 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5545 "pointer to array loses qualifier "
5546 "in conditional expression");
5547 else if (warn_c11_c2x_compat > 0)
5548 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5549 "pointer to array loses qualifier "
5550 "in conditional expression in ISO C before C2X");
5552 if (TREE_CODE (t2) == FUNCTION_TYPE)
5553 pedwarn (colon_loc, OPT_Wpedantic,
5554 "ISO C forbids conditional expr between "
5555 "%<void *%> and function pointer");
5556 /* for array, use qualifiers of element type */
5557 if (flag_isoc2x)
5558 t2 = t2_stripped;
5559 result_type = build_pointer_type (qualify_type (t1, t2));
5561 /* Objective-C pointer comparisons are a bit more lenient. */
5562 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5563 result_type = objc_common_type (type1, type2);
5564 else
5566 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5567 if (emit_diagnostic (bltin1 && bltin2 ? DK_WARNING : DK_PEDWARN,
5568 colon_loc, OPT_Wincompatible_pointer_types,
5569 "pointer type mismatch "
5570 "in conditional expression"))
5572 inform (op1_loc, "first expression has type %qT", type1);
5573 inform (op2_loc, "second expression has type %qT", type2);
5575 result_type = build_pointer_type
5576 (build_qualified_type (void_type_node, qual));
5579 else if (code1 == POINTER_TYPE
5580 && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
5582 if (!null_pointer_constant_p (orig_op2))
5583 pedwarn (colon_loc, OPT_Wint_conversion,
5584 "pointer/integer type mismatch in conditional expression");
5585 else
5587 op2 = null_pointer_node;
5589 result_type = type1;
5591 else if (code2 == POINTER_TYPE
5592 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
5594 if (!null_pointer_constant_p (orig_op1))
5595 pedwarn (colon_loc, OPT_Wint_conversion,
5596 "pointer/integer type mismatch in conditional expression");
5597 else
5599 op1 = null_pointer_node;
5601 result_type = type2;
5603 /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
5604 type nullptr_t and the other is a pointer, the result type is the pointer
5605 type." */
5606 else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
5607 result_type = type2;
5608 else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
5609 result_type = type1;
5611 if (!result_type)
5613 if (flag_cond_mismatch)
5614 result_type = void_type_node;
5615 else
5617 error_at (colon_loc, "type mismatch in conditional expression");
5618 return error_mark_node;
5622 /* Merge const and volatile flags of the incoming types. */
5623 result_type
5624 = build_type_variant (result_type,
5625 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5626 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5628 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5629 semantic_result_type);
5630 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5631 semantic_result_type);
5633 if (ifexp_bcp && ifexp == truthvalue_true_node)
5635 op2_int_operands = true;
5636 op1 = c_fully_fold (op1, require_constant_value, NULL);
5638 if (ifexp_bcp && ifexp == truthvalue_false_node)
5640 op1_int_operands = true;
5641 op2 = c_fully_fold (op2, require_constant_value, NULL);
5643 int_const = int_operands = (ifexp_int_operands
5644 && op1_int_operands
5645 && op2_int_operands);
5646 if (int_operands)
5648 int_const = ((ifexp == truthvalue_true_node
5649 && TREE_CODE (orig_op1) == INTEGER_CST
5650 && !TREE_OVERFLOW (orig_op1))
5651 || (ifexp == truthvalue_false_node
5652 && TREE_CODE (orig_op2) == INTEGER_CST
5653 && !TREE_OVERFLOW (orig_op2)));
5656 /* Need to convert condition operand into a vector mask. */
5657 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5659 tree vectype = TREE_TYPE (ifexp);
5660 tree elem_type = TREE_TYPE (vectype);
5661 tree zero = build_int_cst (elem_type, 0);
5662 tree zero_vec = build_vector_from_val (vectype, zero);
5663 tree cmp_type = truth_type_for (vectype);
5664 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5667 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5668 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5669 else
5671 if (int_operands)
5673 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5674 nested inside of the expression. */
5675 op1 = c_fully_fold (op1, false, NULL);
5676 op2 = c_fully_fold (op2, false, NULL);
5678 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5679 if (int_operands)
5680 ret = note_integer_operands (ret);
5682 if (semantic_result_type)
5683 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5685 protected_set_expr_location (ret, colon_loc);
5687 /* If the OP1 and OP2 are the same and don't have side-effects,
5688 warn here, because the COND_EXPR will be turned into OP1. */
5689 if (warn_duplicated_branches
5690 && TREE_CODE (ret) == COND_EXPR
5691 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5692 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5693 "this condition has identical branches");
5695 return ret;
5698 /* EXPR is an expression, location LOC, whose result is discarded.
5699 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5700 whose right-hand operand is such a call, possibly recursively). */
5702 static void
5703 maybe_warn_nodiscard (location_t loc, tree expr)
5705 if (VOID_TYPE_P (TREE_TYPE (expr)))
5706 return;
5707 while (TREE_CODE (expr) == COMPOUND_EXPR)
5709 expr = TREE_OPERAND (expr, 1);
5710 if (EXPR_HAS_LOCATION (expr))
5711 loc = EXPR_LOCATION (expr);
5713 if (TREE_CODE (expr) != CALL_EXPR)
5714 return;
5715 tree fn = CALL_EXPR_FN (expr);
5716 if (!fn)
5717 return;
5718 tree attr;
5719 if (TREE_CODE (fn) == ADDR_EXPR
5720 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5721 && (attr = lookup_attribute ("nodiscard",
5722 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5724 fn = TREE_OPERAND (fn, 0);
5725 tree args = TREE_VALUE (attr);
5726 if (args)
5727 args = TREE_VALUE (args);
5728 auto_diagnostic_group d;
5729 int warned;
5730 if (args)
5731 warned = warning_at (loc, OPT_Wunused_result,
5732 "ignoring return value of %qD, declared with "
5733 "attribute %<nodiscard%>: %E", fn, args);
5734 else
5735 warned = warning_at (loc, OPT_Wunused_result,
5736 "ignoring return value of %qD, declared with "
5737 "attribute %<nodiscard%>", fn);
5738 if (warned)
5739 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5741 else
5743 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5744 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5745 if (!attr)
5746 return;
5747 tree args = TREE_VALUE (attr);
5748 if (args)
5749 args = TREE_VALUE (args);
5750 auto_diagnostic_group d;
5751 int warned;
5752 if (args)
5753 warned = warning_at (loc, OPT_Wunused_result,
5754 "ignoring return value of type %qT, declared "
5755 "with attribute %<nodiscard%>: %E",
5756 rettype, args);
5757 else
5758 warned = warning_at (loc, OPT_Wunused_result,
5759 "ignoring return value of type %qT, declared "
5760 "with attribute %<nodiscard%>", rettype);
5761 if (warned)
5763 if (TREE_CODE (fn) == ADDR_EXPR)
5765 fn = TREE_OPERAND (fn, 0);
5766 if (TREE_CODE (fn) == FUNCTION_DECL)
5767 inform (DECL_SOURCE_LOCATION (fn),
5768 "in call to %qD, declared here", fn);
5774 /* Return a compound expression that performs two expressions and
5775 returns the value of the second of them.
5777 LOC is the location of the COMPOUND_EXPR. */
5779 tree
5780 build_compound_expr (location_t loc, tree expr1, tree expr2)
5782 bool expr1_int_operands, expr2_int_operands;
5783 tree eptype = NULL_TREE;
5784 tree ret;
5786 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5787 if (expr1_int_operands)
5788 expr1 = remove_c_maybe_const_expr (expr1);
5789 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5790 if (expr2_int_operands)
5791 expr2 = remove_c_maybe_const_expr (expr2);
5793 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5794 expr1 = TREE_OPERAND (expr1, 0);
5795 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5797 eptype = TREE_TYPE (expr2);
5798 expr2 = TREE_OPERAND (expr2, 0);
5801 if (!TREE_SIDE_EFFECTS (expr1))
5803 /* The left-hand operand of a comma expression is like an expression
5804 statement: with -Wunused, we should warn if it doesn't have
5805 any side-effects, unless it was explicitly cast to (void). */
5806 if (warn_unused_value)
5808 if (VOID_TYPE_P (TREE_TYPE (expr1))
5809 && CONVERT_EXPR_P (expr1))
5810 ; /* (void) a, b */
5811 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5812 && TREE_CODE (expr1) == COMPOUND_EXPR
5813 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5814 ; /* (void) a, (void) b, c */
5815 else
5816 warning_at (loc, OPT_Wunused_value,
5817 "left-hand operand of comma expression has no effect");
5820 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5821 && warn_unused_value)
5823 tree r = expr1;
5824 location_t cloc = loc;
5825 while (TREE_CODE (r) == COMPOUND_EXPR)
5827 if (EXPR_HAS_LOCATION (r))
5828 cloc = EXPR_LOCATION (r);
5829 r = TREE_OPERAND (r, 1);
5831 if (!TREE_SIDE_EFFECTS (r)
5832 && !VOID_TYPE_P (TREE_TYPE (r))
5833 && !CONVERT_EXPR_P (r))
5834 warning_at (cloc, OPT_Wunused_value,
5835 "right-hand operand of comma expression has no effect");
5838 /* With -Wunused, we should also warn if the left-hand operand does have
5839 side-effects, but computes a value which is not used. For example, in
5840 `foo() + bar(), baz()' the result of the `+' operator is not used,
5841 so we should issue a warning. */
5842 else if (warn_unused_value)
5843 warn_if_unused_value (expr1, loc);
5845 maybe_warn_nodiscard (loc, expr1);
5847 if (expr2 == error_mark_node)
5848 return error_mark_node;
5850 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5852 if (flag_isoc99
5853 && expr1_int_operands
5854 && expr2_int_operands)
5855 ret = note_integer_operands (ret);
5857 if (eptype)
5858 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5860 protected_set_expr_location (ret, loc);
5861 return ret;
5864 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5865 which we are casting. OTYPE is the type of the expression being
5866 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5867 of the cast. -Wcast-qual appeared on the command line. Named
5868 address space qualifiers are not handled here, because they result
5869 in different warnings. */
5871 static void
5872 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5874 tree in_type = type;
5875 tree in_otype = otype;
5876 int added = 0;
5877 int discarded = 0;
5878 bool is_const;
5880 /* Check that the qualifiers on IN_TYPE are a superset of the
5881 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5882 nodes is uninteresting and we stop as soon as we hit a
5883 non-POINTER_TYPE node on either type. */
5886 in_otype = TREE_TYPE (in_otype);
5887 in_type = TREE_TYPE (in_type);
5889 /* GNU C allows cv-qualified function types. 'const' means the
5890 function is very pure, 'volatile' means it can't return. We
5891 need to warn when such qualifiers are added, not when they're
5892 taken away. */
5893 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5894 && TREE_CODE (in_type) == FUNCTION_TYPE)
5895 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5896 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5897 else
5898 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5899 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5901 while (TREE_CODE (in_type) == POINTER_TYPE
5902 && TREE_CODE (in_otype) == POINTER_TYPE);
5904 if (added)
5905 warning_at (loc, OPT_Wcast_qual,
5906 "cast adds %q#v qualifier to function type", added);
5908 if (discarded)
5909 /* There are qualifiers present in IN_OTYPE that are not present
5910 in IN_TYPE. */
5911 warning_at (loc, OPT_Wcast_qual,
5912 "cast discards %qv qualifier from pointer target type",
5913 discarded);
5915 if (added || discarded)
5916 return;
5918 /* A cast from **T to const **T is unsafe, because it can cause a
5919 const value to be changed with no additional warning. We only
5920 issue this warning if T is the same on both sides, and we only
5921 issue the warning if there are the same number of pointers on
5922 both sides, as otherwise the cast is clearly unsafe anyhow. A
5923 cast is unsafe when a qualifier is added at one level and const
5924 is not present at all outer levels.
5926 To issue this warning, we check at each level whether the cast
5927 adds new qualifiers not already seen. We don't need to special
5928 case function types, as they won't have the same
5929 TYPE_MAIN_VARIANT. */
5931 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5932 return;
5933 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5934 return;
5936 in_type = type;
5937 in_otype = otype;
5938 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5941 in_type = TREE_TYPE (in_type);
5942 in_otype = TREE_TYPE (in_otype);
5943 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5944 && !is_const)
5946 warning_at (loc, OPT_Wcast_qual,
5947 "to be safe all intermediate pointers in cast from "
5948 "%qT to %qT must be %<const%> qualified",
5949 otype, type);
5950 break;
5952 if (is_const)
5953 is_const = TYPE_READONLY (in_type);
5955 while (TREE_CODE (in_type) == POINTER_TYPE);
5958 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5960 static bool
5961 c_safe_arg_type_equiv_p (tree t1, tree t2)
5963 if (error_operand_p (t1) || error_operand_p (t2))
5964 return true;
5966 t1 = TYPE_MAIN_VARIANT (t1);
5967 t2 = TYPE_MAIN_VARIANT (t2);
5969 if (TREE_CODE (t1) == POINTER_TYPE
5970 && TREE_CODE (t2) == POINTER_TYPE)
5971 return true;
5973 /* The signedness of the parameter matters only when an integral
5974 type smaller than int is promoted to int, otherwise only the
5975 precision of the parameter matters.
5976 This check should make sure that the callee does not see
5977 undefined values in argument registers. */
5978 if (INTEGRAL_TYPE_P (t1)
5979 && INTEGRAL_TYPE_P (t2)
5980 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5981 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5982 || !targetm.calls.promote_prototypes (NULL_TREE)
5983 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5984 return true;
5986 return comptypes (t1, t2);
5989 /* Check if a type cast between two function types can be considered safe. */
5991 static bool
5992 c_safe_function_type_cast_p (tree t1, tree t2)
5994 if (TREE_TYPE (t1) == void_type_node &&
5995 TYPE_ARG_TYPES (t1) == void_list_node)
5996 return true;
5998 if (TREE_TYPE (t2) == void_type_node &&
5999 TYPE_ARG_TYPES (t2) == void_list_node)
6000 return true;
6002 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
6003 return false;
6005 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
6006 t1 && t2;
6007 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6008 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
6009 return false;
6011 return true;
6014 /* Build an expression representing a cast to type TYPE of expression EXPR.
6015 LOC is the location of the cast-- typically the open paren of the cast. */
6017 tree
6018 build_c_cast (location_t loc, tree type, tree expr)
6020 tree value;
6022 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
6024 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
6025 expr = TREE_OPERAND (expr, 0);
6027 value = expr;
6028 if (int_operands)
6029 value = remove_c_maybe_const_expr (value);
6031 if (type == error_mark_node || expr == error_mark_node)
6032 return error_mark_node;
6034 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
6035 only in <protocol> qualifications. But when constructing cast expressions,
6036 the protocols do matter and must be kept around. */
6037 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
6038 return build1 (NOP_EXPR, type, expr);
6040 type = TYPE_MAIN_VARIANT (type);
6042 if (TREE_CODE (type) == ARRAY_TYPE)
6044 error_at (loc, "cast specifies array type");
6045 return error_mark_node;
6048 if (TREE_CODE (type) == FUNCTION_TYPE)
6050 error_at (loc, "cast specifies function type");
6051 return error_mark_node;
6054 if (!VOID_TYPE_P (type))
6056 value = require_complete_type (loc, value);
6057 if (value == error_mark_node)
6058 return error_mark_node;
6061 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
6063 if (RECORD_OR_UNION_TYPE_P (type)
6064 && pedwarn (loc, OPT_Wpedantic,
6065 "ISO C forbids casting nonscalar to the same type"))
6067 else if (warn_useless_cast)
6068 warning_at (loc, OPT_Wuseless_cast,
6069 "useless cast to type %qT", type);
6071 /* Convert to remove any qualifiers from VALUE's type. */
6072 value = convert (type, value);
6074 else if (TREE_CODE (type) == UNION_TYPE)
6076 tree field;
6078 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6079 if (TREE_TYPE (field) != error_mark_node
6080 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
6081 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
6082 break;
6084 if (field)
6086 tree t;
6087 bool maybe_const = true;
6089 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
6090 t = c_fully_fold (value, false, &maybe_const);
6091 t = build_constructor_single (type, field, t);
6092 if (!maybe_const)
6093 t = c_wrap_maybe_const (t, true);
6094 t = digest_init (loc, type, t,
6095 NULL_TREE, false, false, false, true, false, false);
6096 TREE_CONSTANT (t) = TREE_CONSTANT (value);
6097 return t;
6099 error_at (loc, "cast to union type from type not present in union");
6100 return error_mark_node;
6102 else
6104 tree otype, ovalue;
6106 if (type == void_type_node)
6108 tree t = build1 (CONVERT_EXPR, type, value);
6109 SET_EXPR_LOCATION (t, loc);
6110 return t;
6113 otype = TREE_TYPE (value);
6115 /* Optionally warn about potentially worrisome casts. */
6116 if (warn_cast_qual
6117 && TREE_CODE (type) == POINTER_TYPE
6118 && TREE_CODE (otype) == POINTER_TYPE)
6119 handle_warn_cast_qual (loc, type, otype);
6121 /* Warn about conversions between pointers to disjoint
6122 address spaces. */
6123 if (TREE_CODE (type) == POINTER_TYPE
6124 && TREE_CODE (otype) == POINTER_TYPE
6125 && !null_pointer_constant_p (value))
6127 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6128 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6129 addr_space_t as_common;
6131 if (!addr_space_superset (as_to, as_from, &as_common))
6133 if (ADDR_SPACE_GENERIC_P (as_from))
6134 warning_at (loc, 0, "cast to %qs address space pointer "
6135 "from disjoint generic address space pointer",
6136 c_addr_space_name (as_to));
6138 else if (ADDR_SPACE_GENERIC_P (as_to))
6139 warning_at (loc, 0, "cast to generic address space pointer "
6140 "from disjoint %qs address space pointer",
6141 c_addr_space_name (as_from));
6143 else
6144 warning_at (loc, 0, "cast to %qs address space pointer "
6145 "from disjoint %qs address space pointer",
6146 c_addr_space_name (as_to),
6147 c_addr_space_name (as_from));
6151 /* Warn about possible alignment problems. */
6152 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6153 && TREE_CODE (type) == POINTER_TYPE
6154 && TREE_CODE (otype) == POINTER_TYPE
6155 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6156 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6157 /* Don't warn about opaque types, where the actual alignment
6158 restriction is unknown. */
6159 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6160 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6161 && min_align_of_type (TREE_TYPE (type))
6162 > min_align_of_type (TREE_TYPE (otype)))
6163 warning_at (loc, OPT_Wcast_align,
6164 "cast increases required alignment of target type");
6166 if ((TREE_CODE (type) == INTEGER_TYPE
6167 || TREE_CODE (type) == BITINT_TYPE)
6168 && TREE_CODE (otype) == POINTER_TYPE
6169 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6170 /* Unlike conversion of integers to pointers, where the
6171 warning is disabled for converting constants because
6172 of cases such as SIG_*, warn about converting constant
6173 pointers to integers. In some cases it may cause unwanted
6174 sign extension, and a warning is appropriate. */
6175 warning_at (loc, OPT_Wpointer_to_int_cast,
6176 "cast from pointer to integer of different size");
6178 if (TREE_CODE (value) == CALL_EXPR
6179 && TREE_CODE (type) != TREE_CODE (otype))
6180 warning_at (loc, OPT_Wbad_function_cast,
6181 "cast from function call of type %qT "
6182 "to non-matching type %qT", otype, type);
6184 if (TREE_CODE (type) == POINTER_TYPE
6185 && (TREE_CODE (otype) == INTEGER_TYPE
6186 || TREE_CODE (otype) == BITINT_TYPE)
6187 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6188 /* Don't warn about converting any constant. */
6189 && !TREE_CONSTANT (value))
6190 warning_at (loc,
6191 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6192 "of different size");
6194 if (warn_strict_aliasing <= 2)
6195 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6197 /* If pedantic, warn for conversions between function and object
6198 pointer types, except for converting a null pointer constant
6199 to function pointer type. */
6200 if (pedantic
6201 && TREE_CODE (type) == POINTER_TYPE
6202 && TREE_CODE (otype) == POINTER_TYPE
6203 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6204 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6205 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6206 "conversion of function pointer to object pointer type");
6208 if (pedantic
6209 && TREE_CODE (type) == POINTER_TYPE
6210 && TREE_CODE (otype) == POINTER_TYPE
6211 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6212 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6213 && !null_pointer_constant_p (value))
6214 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6215 "conversion of object pointer to function pointer type");
6217 if (TREE_CODE (type) == POINTER_TYPE
6218 && TREE_CODE (otype) == POINTER_TYPE
6219 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6220 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6221 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6222 TREE_TYPE (otype)))
6223 warning_at (loc, OPT_Wcast_function_type,
6224 "cast between incompatible function types"
6225 " from %qT to %qT", otype, type);
6227 ovalue = value;
6228 value = convert (type, value);
6230 /* Ignore any integer overflow caused by the cast. */
6231 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6233 if (TREE_OVERFLOW_P (ovalue))
6235 if (!TREE_OVERFLOW (value))
6237 /* Avoid clobbering a shared constant. */
6238 value = copy_node (value);
6239 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6242 else if (TREE_OVERFLOW (value))
6243 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6244 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6248 /* Don't let a cast be an lvalue. */
6249 if (lvalue_p (value))
6250 value = non_lvalue_loc (loc, value);
6252 /* Don't allow the results of casting to floating-point or complex
6253 types be confused with actual constants, or casts involving
6254 integer and pointer types other than direct integer-to-integer
6255 and integer-to-pointer be confused with integer constant
6256 expressions and null pointer constants. */
6257 if (TREE_CODE (value) == REAL_CST
6258 || TREE_CODE (value) == COMPLEX_CST
6259 || (TREE_CODE (value) == INTEGER_CST
6260 && !((TREE_CODE (expr) == INTEGER_CST
6261 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6262 || TREE_CODE (expr) == REAL_CST
6263 || TREE_CODE (expr) == COMPLEX_CST)))
6264 value = build1 (NOP_EXPR, type, value);
6266 /* If the expression has integer operands and so can occur in an
6267 unevaluated part of an integer constant expression, ensure the
6268 return value reflects this. */
6269 if (int_operands
6270 && INTEGRAL_TYPE_P (type)
6271 && value != error_mark_node
6272 && !EXPR_INT_CONST_OPERANDS (value))
6273 value = note_integer_operands (value);
6275 protected_set_expr_location (value, loc);
6276 return value;
6279 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6280 location of the open paren of the cast, or the position of the cast
6281 expr. */
6282 tree
6283 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6285 tree type;
6286 tree type_expr = NULL_TREE;
6287 bool type_expr_const = true;
6288 tree ret;
6289 int saved_wsp = warn_strict_prototypes;
6291 /* This avoids warnings about unprototyped casts on
6292 integers. E.g. "#define SIG_DFL (void(*)())0". */
6293 if (TREE_CODE (expr) == INTEGER_CST)
6294 warn_strict_prototypes = 0;
6295 type = groktypename (type_name, &type_expr, &type_expr_const);
6296 warn_strict_prototypes = saved_wsp;
6298 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6299 && reject_gcc_builtin (expr))
6300 return error_mark_node;
6302 ret = build_c_cast (loc, type, expr);
6303 if (type_expr)
6305 bool inner_expr_const = true;
6306 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6307 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6308 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6309 && inner_expr_const);
6310 SET_EXPR_LOCATION (ret, loc);
6313 if (!EXPR_HAS_LOCATION (ret))
6314 protected_set_expr_location (ret, loc);
6316 /* C++ does not permits types to be defined in a cast, but it
6317 allows references to incomplete types. */
6318 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6319 warning_at (loc, OPT_Wc___compat,
6320 "defining a type in a cast is invalid in C++");
6322 return ret;
6325 /* Build an assignment expression of lvalue LHS from value RHS.
6326 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6327 may differ from TREE_TYPE (LHS) for an enum bitfield.
6328 MODIFYCODE is the code for a binary operator that we use
6329 to combine the old value of LHS with RHS to get the new value.
6330 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6331 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6332 which may differ from TREE_TYPE (RHS) for an enum value.
6334 LOCATION is the location of the MODIFYCODE operator.
6335 RHS_LOC is the location of the RHS. */
6337 tree
6338 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6339 enum tree_code modifycode,
6340 location_t rhs_loc, tree rhs, tree rhs_origtype)
6342 tree result;
6343 tree newrhs;
6344 tree rhseval = NULL_TREE;
6345 tree lhstype = TREE_TYPE (lhs);
6346 tree olhstype = lhstype;
6347 bool npc;
6348 bool is_atomic_op;
6350 /* Types that aren't fully specified cannot be used in assignments. */
6351 lhs = require_complete_type (location, lhs);
6353 /* Avoid duplicate error messages from operands that had errors. */
6354 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6355 return error_mark_node;
6357 /* Ensure an error for assigning a non-lvalue array to an array in
6358 C90. */
6359 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6361 error_at (location, "assignment to expression with array type");
6362 return error_mark_node;
6365 /* For ObjC properties, defer this check. */
6366 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6367 return error_mark_node;
6369 is_atomic_op = really_atomic_lvalue (lhs);
6371 newrhs = rhs;
6373 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6375 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6376 lhs_origtype, modifycode, rhs_loc, rhs,
6377 rhs_origtype);
6378 if (inner == error_mark_node)
6379 return error_mark_node;
6380 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6381 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6382 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6383 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6384 protected_set_expr_location (result, location);
6385 return result;
6388 /* If a binary op has been requested, combine the old LHS value with the RHS
6389 producing the value we should actually store into the LHS. */
6391 if (modifycode != NOP_EXPR)
6393 lhs = c_fully_fold (lhs, false, NULL, true);
6394 lhs = stabilize_reference (lhs);
6396 /* Construct the RHS for any non-atomic compound assignemnt. */
6397 if (!is_atomic_op)
6399 /* If in LHS op= RHS the RHS has side-effects, ensure they
6400 are preevaluated before the rest of the assignment expression's
6401 side-effects, because RHS could contain e.g. function calls
6402 that modify LHS. */
6403 if (TREE_SIDE_EFFECTS (rhs))
6405 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6406 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6407 else
6408 newrhs = save_expr (rhs);
6409 rhseval = newrhs;
6410 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6411 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6412 newrhs);
6414 newrhs = build_binary_op (location,
6415 modifycode, lhs, newrhs, true);
6417 /* The original type of the right hand side is no longer
6418 meaningful. */
6419 rhs_origtype = NULL_TREE;
6423 if (c_dialect_objc ())
6425 /* Check if we are modifying an Objective-C property reference;
6426 if so, we need to generate setter calls. */
6427 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6428 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6429 else
6430 result = objc_maybe_build_modify_expr (lhs, newrhs);
6431 if (result)
6432 goto return_result;
6434 /* Else, do the check that we postponed for Objective-C. */
6435 if (!lvalue_or_else (location, lhs, lv_assign))
6436 return error_mark_node;
6439 /* Give an error for storing in something that is 'const'. */
6441 if (TYPE_READONLY (lhstype)
6442 || (RECORD_OR_UNION_TYPE_P (lhstype)
6443 && C_TYPE_FIELDS_READONLY (lhstype)))
6445 readonly_error (location, lhs, lv_assign);
6446 return error_mark_node;
6448 else if (TREE_READONLY (lhs))
6449 readonly_warning (lhs, lv_assign);
6451 /* If storing into a structure or union member,
6452 it has probably been given type `int'.
6453 Compute the type that would go with
6454 the actual amount of storage the member occupies. */
6456 if (TREE_CODE (lhs) == COMPONENT_REF
6457 && (TREE_CODE (lhstype) == INTEGER_TYPE
6458 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6459 || SCALAR_FLOAT_TYPE_P (lhstype)
6460 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6461 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6463 /* If storing in a field that is in actuality a short or narrower than one,
6464 we must store in the field in its actual type. */
6466 if (lhstype != TREE_TYPE (lhs))
6468 lhs = copy_node (lhs);
6469 TREE_TYPE (lhs) = lhstype;
6472 /* Issue -Wc++-compat warnings about an assignment to an enum type
6473 when LHS does not have its original type. This happens for,
6474 e.g., an enum bitfield in a struct. */
6475 if (warn_cxx_compat
6476 && lhs_origtype != NULL_TREE
6477 && lhs_origtype != lhstype
6478 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6480 tree checktype = (rhs_origtype != NULL_TREE
6481 ? rhs_origtype
6482 : TREE_TYPE (rhs));
6483 if (checktype != error_mark_node
6484 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6485 || (is_atomic_op && modifycode != NOP_EXPR)))
6486 warning_at (location, OPT_Wc___compat,
6487 "enum conversion in assignment is invalid in C++");
6490 /* Remove qualifiers. */
6491 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6492 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6494 /* Convert new value to destination type. Fold it first, then
6495 restore any excess precision information, for the sake of
6496 conversion warnings. */
6498 if (!(is_atomic_op && modifycode != NOP_EXPR))
6500 tree rhs_semantic_type = NULL_TREE;
6501 if (!c_in_omp_for)
6503 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6505 rhs_semantic_type = TREE_TYPE (newrhs);
6506 newrhs = TREE_OPERAND (newrhs, 0);
6508 npc = null_pointer_constant_p (newrhs);
6509 newrhs = c_fully_fold (newrhs, false, NULL);
6510 if (rhs_semantic_type)
6511 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6513 else
6514 npc = null_pointer_constant_p (newrhs);
6515 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6516 rhs_origtype, ic_assign, npc,
6517 NULL_TREE, NULL_TREE, 0);
6518 if (TREE_CODE (newrhs) == ERROR_MARK)
6519 return error_mark_node;
6522 /* Emit ObjC write barrier, if necessary. */
6523 if (c_dialect_objc () && flag_objc_gc)
6525 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6526 if (result)
6528 protected_set_expr_location (result, location);
6529 goto return_result;
6533 /* Scan operands. */
6535 if (is_atomic_op)
6536 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6537 else
6539 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6540 TREE_SIDE_EFFECTS (result) = 1;
6541 protected_set_expr_location (result, location);
6544 /* If we got the LHS in a different type for storing in,
6545 convert the result back to the nominal type of LHS
6546 so that the value we return always has the same type
6547 as the LHS argument. */
6549 if (olhstype == TREE_TYPE (result))
6550 goto return_result;
6552 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6553 rhs_origtype, ic_assign, false, NULL_TREE,
6554 NULL_TREE, 0);
6555 protected_set_expr_location (result, location);
6557 return_result:
6558 if (rhseval)
6559 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6560 return result;
6563 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6564 This is used to implement -fplan9-extensions. */
6566 static bool
6567 find_anonymous_field_with_type (tree struct_type, tree type)
6569 tree field;
6570 bool found;
6572 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6573 found = false;
6574 for (field = TYPE_FIELDS (struct_type);
6575 field != NULL_TREE;
6576 field = TREE_CHAIN (field))
6578 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6579 ? c_build_qualified_type (TREE_TYPE (field),
6580 TYPE_QUAL_ATOMIC)
6581 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6582 if (DECL_NAME (field) == NULL
6583 && comptypes (type, fieldtype))
6585 if (found)
6586 return false;
6587 found = true;
6589 else if (DECL_NAME (field) == NULL
6590 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6591 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6593 if (found)
6594 return false;
6595 found = true;
6598 return found;
6601 /* RHS is an expression whose type is pointer to struct. If there is
6602 an anonymous field in RHS with type TYPE, then return a pointer to
6603 that field in RHS. This is used with -fplan9-extensions. This
6604 returns NULL if no conversion could be found. */
6606 static tree
6607 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6609 tree rhs_struct_type, lhs_main_type;
6610 tree field, found_field;
6611 bool found_sub_field;
6612 tree ret;
6614 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6615 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6616 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6618 gcc_assert (POINTER_TYPE_P (type));
6619 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6620 ? c_build_qualified_type (TREE_TYPE (type),
6621 TYPE_QUAL_ATOMIC)
6622 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6624 found_field = NULL_TREE;
6625 found_sub_field = false;
6626 for (field = TYPE_FIELDS (rhs_struct_type);
6627 field != NULL_TREE;
6628 field = TREE_CHAIN (field))
6630 if (DECL_NAME (field) != NULL_TREE
6631 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6632 continue;
6633 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6634 ? c_build_qualified_type (TREE_TYPE (field),
6635 TYPE_QUAL_ATOMIC)
6636 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6637 if (comptypes (lhs_main_type, fieldtype))
6639 if (found_field != NULL_TREE)
6640 return NULL_TREE;
6641 found_field = field;
6643 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6644 lhs_main_type))
6646 if (found_field != NULL_TREE)
6647 return NULL_TREE;
6648 found_field = field;
6649 found_sub_field = true;
6653 if (found_field == NULL_TREE)
6654 return NULL_TREE;
6656 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6657 build_fold_indirect_ref (rhs), found_field,
6658 NULL_TREE);
6659 ret = build_fold_addr_expr_loc (location, ret);
6661 if (found_sub_field)
6663 ret = convert_to_anonymous_field (location, type, ret);
6664 gcc_assert (ret != NULL_TREE);
6667 return ret;
6670 /* Issue an error message for a bad initializer component.
6671 GMSGID identifies the message.
6672 The component name is taken from the spelling stack. */
6674 static void ATTRIBUTE_GCC_DIAG (2,0)
6675 error_init (location_t loc, const char *gmsgid, ...)
6677 char *ofwhat;
6679 auto_diagnostic_group d;
6681 /* The gmsgid may be a format string with %< and %>. */
6682 va_list ap;
6683 va_start (ap, gmsgid);
6684 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6685 va_end (ap);
6687 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6688 if (*ofwhat && warned)
6689 inform (loc, "(near initialization for %qs)", ofwhat);
6692 /* Issue a pedantic warning for a bad initializer component. OPT is
6693 the option OPT_* (from options.h) controlling this warning or 0 if
6694 it is unconditionally given. GMSGID identifies the message. The
6695 component name is taken from the spelling stack. */
6697 static void ATTRIBUTE_GCC_DIAG (3,0)
6698 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6700 /* Use the location where a macro was expanded rather than where
6701 it was defined to make sure macros defined in system headers
6702 but used incorrectly elsewhere are diagnosed. */
6703 location_t exploc = expansion_point_location_if_in_system_header (loc);
6704 auto_diagnostic_group d;
6705 va_list ap;
6706 va_start (ap, gmsgid);
6707 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6708 va_end (ap);
6709 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6710 if (*ofwhat && warned)
6711 inform (exploc, "(near initialization for %qs)", ofwhat);
6714 /* Issue a warning for a bad initializer component.
6716 OPT is the OPT_W* value corresponding to the warning option that
6717 controls this warning. GMSGID identifies the message. The
6718 component name is taken from the spelling stack. */
6720 static void
6721 warning_init (location_t loc, int opt, const char *gmsgid)
6723 char *ofwhat;
6724 bool warned;
6726 auto_diagnostic_group d;
6728 /* Use the location where a macro was expanded rather than where
6729 it was defined to make sure macros defined in system headers
6730 but used incorrectly elsewhere are diagnosed. */
6731 location_t exploc = expansion_point_location_if_in_system_header (loc);
6733 /* The gmsgid may be a format string with %< and %>. */
6734 warned = warning_at (exploc, opt, gmsgid);
6735 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6736 if (*ofwhat && warned)
6737 inform (exploc, "(near initialization for %qs)", ofwhat);
6740 /* If TYPE is an array type and EXPR is a parenthesized string
6741 constant, warn if pedantic that EXPR is being used to initialize an
6742 object of type TYPE. */
6744 void
6745 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6747 if (pedantic
6748 && TREE_CODE (type) == ARRAY_TYPE
6749 && TREE_CODE (expr.value) == STRING_CST
6750 && expr.original_code != STRING_CST)
6751 pedwarn_init (loc, OPT_Wpedantic,
6752 "array initialized from parenthesized string constant");
6755 /* Attempt to locate the parameter with the given index within FNDECL,
6756 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6758 static location_t
6759 get_fndecl_argument_location (tree fndecl, int argnum)
6761 int i;
6762 tree param;
6764 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6765 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6766 i < argnum && param;
6767 i++, param = TREE_CHAIN (param))
6770 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6771 return DECL_SOURCE_LOCATION (FNDECL). */
6772 if (param == NULL)
6773 return DECL_SOURCE_LOCATION (fndecl);
6775 return DECL_SOURCE_LOCATION (param);
6778 /* Issue a note about a mismatching argument for parameter PARMNUM
6779 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6780 Attempt to issue the note at the pertinent parameter of the decl;
6781 failing that issue it at the location of FUNDECL; failing that
6782 issue it at PLOC. */
6784 static void
6785 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6786 tree expected_type, tree actual_type)
6788 location_t loc;
6789 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6790 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6791 else
6792 loc = ploc;
6794 inform (loc,
6795 "expected %qT but argument is of type %qT",
6796 expected_type, actual_type);
6799 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6800 function FUNDECL declared without prototype to parameter PARMNUM of
6801 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6803 static void
6804 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6805 tree parmtype, tree argtype)
6807 tree_code parmcode = TREE_CODE (parmtype);
6808 tree_code argcode = TREE_CODE (argtype);
6809 tree promoted = c_type_promotes_to (argtype);
6811 /* Avoid warning for enum arguments that promote to an integer type
6812 of the same size/mode. */
6813 if (parmcode == INTEGER_TYPE
6814 && argcode == ENUMERAL_TYPE
6815 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6816 return;
6818 if ((parmcode == argcode
6819 || (parmcode == INTEGER_TYPE
6820 && argcode == ENUMERAL_TYPE))
6821 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6822 return;
6824 /* This diagnoses even signed/unsigned mismatches. Those might be
6825 safe in many cases but GCC may emit suboptimal code for them so
6826 warning on those cases drives efficiency improvements. */
6827 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6828 TYPE_MAIN_VARIANT (promoted) == argtype
6829 ? G_("%qD argument %d type is %qT where %qT is expected "
6830 "in a call to built-in function declared without "
6831 "prototype")
6832 : G_("%qD argument %d promotes to %qT where %qT is expected "
6833 "in a call to built-in function declared without "
6834 "prototype"),
6835 fundecl, parmnum, promoted, parmtype))
6836 inform (DECL_SOURCE_LOCATION (fundecl),
6837 "built-in %qD declared here",
6838 fundecl);
6841 /* Convert value RHS to type TYPE as preparation for an assignment to
6842 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6843 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6844 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6845 constant before any folding.
6846 The real work of conversion is done by `convert'.
6847 The purpose of this function is to generate error messages
6848 for assignments that are not allowed in C.
6849 ERRTYPE says whether it is argument passing, assignment,
6850 initialization or return.
6852 In the following example, '~' denotes where EXPR_LOC and '^' where
6853 LOCATION point to:
6855 f (var); [ic_argpass]
6856 ^ ~~~
6857 x = var; [ic_assign]
6858 ^ ~~~;
6859 int x = var; [ic_init]
6861 return x; [ic_return]
6864 FUNCTION is a tree for the function being called.
6865 PARMNUM is the number of the argument, for printing in error messages.
6866 WARNOPT may be set to a warning option to issue the corresponding warning
6867 rather than an error for invalid conversions. Used for calls to built-in
6868 functions declared without a prototype. */
6870 static tree
6871 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6872 tree rhs, tree origtype, enum impl_conv errtype,
6873 bool null_pointer_constant, tree fundecl,
6874 tree function, int parmnum, int warnopt /* = 0 */)
6876 enum tree_code codel = TREE_CODE (type);
6877 tree orig_rhs = rhs;
6878 tree rhstype;
6879 enum tree_code coder;
6880 tree rname = NULL_TREE;
6881 bool objc_ok = false;
6883 /* Use the expansion point location to handle cases such as user's
6884 function returning a wrong-type macro defined in a system header. */
6885 location = expansion_point_location_if_in_system_header (location);
6887 if (errtype == ic_argpass)
6889 tree selector;
6890 /* Change pointer to function to the function itself for
6891 diagnostics. */
6892 if (TREE_CODE (function) == ADDR_EXPR
6893 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6894 function = TREE_OPERAND (function, 0);
6896 /* Handle an ObjC selector specially for diagnostics. */
6897 selector = objc_message_selector ();
6898 rname = function;
6899 if (selector && parmnum > 2)
6901 rname = selector;
6902 parmnum -= 2;
6906 /* This macro is used to emit diagnostics to ensure that all format
6907 strings are complete sentences, visible to gettext and checked at
6908 compile time. */
6909 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6910 do { \
6911 switch (errtype) \
6913 case ic_argpass: \
6915 auto_diagnostic_group d; \
6916 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6917 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6919 break; \
6920 case ic_assign: \
6921 pedwarn (LOCATION, OPT, AS); \
6922 break; \
6923 case ic_init: \
6924 case ic_init_const: \
6925 pedwarn_init (LOCATION, OPT, IN); \
6926 break; \
6927 case ic_return: \
6928 pedwarn (LOCATION, OPT, RE); \
6929 break; \
6930 default: \
6931 gcc_unreachable (); \
6933 } while (0)
6935 /* This macro is used to emit diagnostics to ensure that all format
6936 strings are complete sentences, visible to gettext and checked at
6937 compile time. It can be called with 'pedwarn' or 'warning_at'. */
6938 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6939 do { \
6940 switch (errtype) \
6942 case ic_argpass: \
6944 auto_diagnostic_group d; \
6945 if (PEDWARN) { \
6946 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6947 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6948 } else { \
6949 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6950 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6953 break; \
6954 case ic_assign: \
6955 if (PEDWARN) \
6956 pedwarn (LOCATION, OPT, AS, QUALS); \
6957 else \
6958 warning_at (LOCATION, OPT, AS, QUALS); \
6959 break; \
6960 case ic_init: \
6961 case ic_init_const: \
6962 if (PEDWARN) \
6963 pedwarn (LOCATION, OPT, IN, QUALS); \
6964 else \
6965 warning_at (LOCATION, OPT, IN, QUALS); \
6966 break; \
6967 case ic_return: \
6968 if (PEDWARN) \
6969 pedwarn (LOCATION, OPT, RE, QUALS); \
6970 else \
6971 warning_at (LOCATION, OPT, RE, QUALS); \
6972 break; \
6973 default: \
6974 gcc_unreachable (); \
6976 } while (0)
6978 /* This macro is used to emit diagnostics to ensure that all format
6979 strings are complete sentences, visible to gettext and checked at
6980 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6981 extra parameter to enumerate qualifiers. */
6982 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6983 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
6986 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6987 rhs = TREE_OPERAND (rhs, 0);
6989 rhstype = TREE_TYPE (rhs);
6990 coder = TREE_CODE (rhstype);
6992 if (coder == ERROR_MARK)
6993 return error_mark_node;
6995 if (c_dialect_objc ())
6997 int parmno;
6999 switch (errtype)
7001 case ic_return:
7002 parmno = 0;
7003 break;
7005 case ic_assign:
7006 parmno = -1;
7007 break;
7009 case ic_init:
7010 case ic_init_const:
7011 parmno = -2;
7012 break;
7014 default:
7015 parmno = parmnum;
7016 break;
7019 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
7022 if (warn_cxx_compat)
7024 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7025 if (checktype != error_mark_node
7026 && TREE_CODE (type) == ENUMERAL_TYPE
7027 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7028 switch (errtype)
7030 case ic_argpass:
7031 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
7032 "passing argument %d of %qE is invalid in C++",
7033 parmnum, rname))
7034 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
7035 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
7036 "expected %qT but argument is of type %qT",
7037 type, rhstype);
7038 break;
7039 case ic_assign:
7040 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7041 "%qT in assignment is invalid in C++", rhstype, type);
7042 break;
7043 case ic_init:
7044 case ic_init_const:
7045 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
7046 "%qT to %qT in initialization is invalid in C++",
7047 rhstype, type);
7048 break;
7049 case ic_return:
7050 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7051 "%qT in return is invalid in C++", rhstype, type);
7052 break;
7053 default:
7054 gcc_unreachable ();
7058 if (warn_enum_conversion)
7060 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7061 if (checktype != error_mark_node
7062 && TREE_CODE (checktype) == ENUMERAL_TYPE
7063 && TREE_CODE (type) == ENUMERAL_TYPE
7064 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7066 gcc_rich_location loc (location);
7067 warning_at (&loc, OPT_Wenum_conversion,
7068 "implicit conversion from %qT to %qT",
7069 checktype, type);
7073 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
7075 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7076 return rhs;
7079 if (coder == VOID_TYPE)
7081 /* Except for passing an argument to an unprototyped function,
7082 this is a constraint violation. When passing an argument to
7083 an unprototyped function, it is compile-time undefined;
7084 making it a constraint in that case was rejected in
7085 DR#252. */
7086 const char msg[] = "void value not ignored as it ought to be";
7087 if (warnopt)
7088 warning_at (location, warnopt, msg);
7089 else
7090 error_at (location, msg);
7091 return error_mark_node;
7093 rhs = require_complete_type (location, rhs);
7094 if (rhs == error_mark_node)
7095 return error_mark_node;
7097 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
7098 return error_mark_node;
7100 /* A non-reference type can convert to a reference. This handles
7101 va_start, va_copy and possibly port built-ins. */
7102 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
7104 if (!lvalue_p (rhs))
7106 const char msg[] = "cannot pass rvalue to reference parameter";
7107 if (warnopt)
7108 warning_at (location, warnopt, msg);
7109 else
7110 error_at (location, msg);
7111 return error_mark_node;
7113 if (!c_mark_addressable (rhs))
7114 return error_mark_node;
7115 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7116 SET_EXPR_LOCATION (rhs, location);
7118 rhs = convert_for_assignment (location, expr_loc,
7119 build_pointer_type (TREE_TYPE (type)),
7120 rhs, origtype, errtype,
7121 null_pointer_constant, fundecl, function,
7122 parmnum, warnopt);
7123 if (rhs == error_mark_node)
7124 return error_mark_node;
7126 rhs = build1 (NOP_EXPR, type, rhs);
7127 SET_EXPR_LOCATION (rhs, location);
7128 return rhs;
7130 /* Some types can interconvert without explicit casts. */
7131 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7132 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7133 return convert (type, rhs);
7134 /* Arithmetic types all interconvert, and enum is treated like int. */
7135 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7136 || codel == FIXED_POINT_TYPE
7137 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7138 || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
7139 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7140 || coder == FIXED_POINT_TYPE
7141 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7142 || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
7144 if (warnopt && errtype == ic_argpass)
7145 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7146 rhstype);
7148 bool save = in_late_binary_op;
7149 if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
7150 || (coder == REAL_TYPE
7151 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7152 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7153 in_late_binary_op = true;
7154 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7155 ? expr_loc : location, type, orig_rhs,
7156 errtype == ic_init_const);
7157 in_late_binary_op = save;
7158 return ret;
7161 /* Aggregates in different TUs might need conversion. */
7162 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7163 && codel == coder
7164 && comptypes (type, rhstype))
7165 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7166 ? expr_loc : location, type, rhs);
7168 /* Conversion to a transparent union or record from its member types.
7169 This applies only to function arguments. */
7170 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7171 && TYPE_TRANSPARENT_AGGR (type))
7172 && errtype == ic_argpass)
7174 tree memb, marginal_memb = NULL_TREE;
7176 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7178 tree memb_type = TREE_TYPE (memb);
7180 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7181 TYPE_MAIN_VARIANT (rhstype)))
7182 break;
7184 if (TREE_CODE (memb_type) != POINTER_TYPE)
7185 continue;
7187 if (coder == POINTER_TYPE)
7189 tree ttl = TREE_TYPE (memb_type);
7190 tree ttr = TREE_TYPE (rhstype);
7192 /* Any non-function converts to a [const][volatile] void *
7193 and vice versa; otherwise, targets must be the same.
7194 Meanwhile, the lhs target must have all the qualifiers of
7195 the rhs. */
7196 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7197 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7198 || comp_target_types (location, memb_type, rhstype))
7200 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7201 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7202 /* If this type won't generate any warnings, use it. */
7203 if (lquals == rquals
7204 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7205 && TREE_CODE (ttl) == FUNCTION_TYPE)
7206 ? ((lquals | rquals) == rquals)
7207 : ((lquals | rquals) == lquals)))
7208 break;
7210 /* Keep looking for a better type, but remember this one. */
7211 if (!marginal_memb)
7212 marginal_memb = memb;
7216 /* Can convert integer zero to any pointer type. */
7217 if (null_pointer_constant)
7219 rhs = null_pointer_node;
7220 break;
7224 if (memb || marginal_memb)
7226 if (!memb)
7228 /* We have only a marginally acceptable member type;
7229 it needs a warning. */
7230 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7231 tree ttr = TREE_TYPE (rhstype);
7233 /* Const and volatile mean something different for function
7234 types, so the usual warnings are not appropriate. */
7235 if (TREE_CODE (ttr) == FUNCTION_TYPE
7236 && TREE_CODE (ttl) == FUNCTION_TYPE)
7238 /* Because const and volatile on functions are
7239 restrictions that say the function will not do
7240 certain things, it is okay to use a const or volatile
7241 function where an ordinary one is wanted, but not
7242 vice-versa. */
7243 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7244 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7245 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7246 OPT_Wdiscarded_qualifiers,
7247 G_("passing argument %d of %qE "
7248 "makes %q#v qualified function "
7249 "pointer from unqualified"),
7250 G_("assignment makes %q#v qualified "
7251 "function pointer from "
7252 "unqualified"),
7253 G_("initialization makes %q#v qualified "
7254 "function pointer from "
7255 "unqualified"),
7256 G_("return makes %q#v qualified function "
7257 "pointer from unqualified"),
7258 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7260 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7261 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7262 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7263 OPT_Wdiscarded_qualifiers,
7264 G_("passing argument %d of %qE discards "
7265 "%qv qualifier from pointer target type"),
7266 G_("assignment discards %qv qualifier "
7267 "from pointer target type"),
7268 G_("initialization discards %qv qualifier "
7269 "from pointer target type"),
7270 G_("return discards %qv qualifier from "
7271 "pointer target type"),
7272 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7274 memb = marginal_memb;
7277 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7278 pedwarn (location, OPT_Wpedantic,
7279 "ISO C prohibits argument conversion to union type");
7281 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7282 return build_constructor_single (type, memb, rhs);
7286 /* Conversions among pointers */
7287 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7288 && (coder == codel))
7290 /* If RHS refers to a built-in declared without a prototype
7291 BLTIN is the declaration of the built-in with a prototype
7292 and RHSTYPE is set to the actual type of the built-in. */
7293 tree bltin;
7294 rhstype = type_or_builtin_type (rhs, &bltin);
7296 tree ttl = TREE_TYPE (type);
7297 tree ttr = TREE_TYPE (rhstype);
7298 tree mvl = ttl;
7299 tree mvr = ttr;
7300 bool is_opaque_pointer;
7301 bool target_cmp = false; /* Cache comp_target_types () result. */
7302 addr_space_t asl;
7303 addr_space_t asr;
7305 if (TREE_CODE (mvl) != ARRAY_TYPE)
7306 mvl = (TYPE_ATOMIC (mvl)
7307 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7308 TYPE_QUAL_ATOMIC)
7309 : TYPE_MAIN_VARIANT (mvl));
7310 if (TREE_CODE (mvr) != ARRAY_TYPE)
7311 mvr = (TYPE_ATOMIC (mvr)
7312 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7313 TYPE_QUAL_ATOMIC)
7314 : TYPE_MAIN_VARIANT (mvr));
7315 /* Opaque pointers are treated like void pointers. */
7316 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7318 /* The Plan 9 compiler permits a pointer to a struct to be
7319 automatically converted into a pointer to an anonymous field
7320 within the struct. */
7321 if (flag_plan9_extensions
7322 && RECORD_OR_UNION_TYPE_P (mvl)
7323 && RECORD_OR_UNION_TYPE_P (mvr)
7324 && mvl != mvr)
7326 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7327 if (new_rhs != NULL_TREE)
7329 rhs = new_rhs;
7330 rhstype = TREE_TYPE (rhs);
7331 coder = TREE_CODE (rhstype);
7332 ttr = TREE_TYPE (rhstype);
7333 mvr = TYPE_MAIN_VARIANT (ttr);
7337 /* C++ does not allow the implicit conversion void* -> T*. However,
7338 for the purpose of reducing the number of false positives, we
7339 tolerate the special case of
7341 int *p = NULL;
7343 where NULL is typically defined in C to be '(void *) 0'. */
7344 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7345 warning_at (errtype == ic_argpass ? expr_loc : location,
7346 OPT_Wc___compat,
7347 "request for implicit conversion "
7348 "from %qT to %qT not permitted in C++", rhstype, type);
7350 /* See if the pointers point to incompatible address spaces. */
7351 asl = TYPE_ADDR_SPACE (ttl);
7352 asr = TYPE_ADDR_SPACE (ttr);
7353 if (!null_pointer_constant_p (rhs)
7354 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7356 auto_diagnostic_group d;
7357 bool diagnosed = true;
7358 switch (errtype)
7360 case ic_argpass:
7362 const char msg[] = G_("passing argument %d of %qE from "
7363 "pointer to non-enclosed address space");
7364 if (warnopt)
7365 diagnosed
7366 = warning_at (expr_loc, warnopt, msg, parmnum, rname);
7367 else
7368 error_at (expr_loc, msg, parmnum, rname);
7369 break;
7371 case ic_assign:
7373 const char msg[] = G_("assignment from pointer to "
7374 "non-enclosed address space");
7375 if (warnopt)
7376 diagnosed = warning_at (location, warnopt, msg);
7377 else
7378 error_at (location, msg);
7379 break;
7381 case ic_init:
7382 case ic_init_const:
7384 const char msg[] = G_("initialization from pointer to "
7385 "non-enclosed address space");
7386 if (warnopt)
7387 diagnosed = warning_at (location, warnopt, msg);
7388 else
7389 error_at (location, msg);
7390 break;
7392 case ic_return:
7394 const char msg[] = G_("return from pointer to "
7395 "non-enclosed address space");
7396 if (warnopt)
7397 diagnosed = warning_at (location, warnopt, msg);
7398 else
7399 error_at (location, msg);
7400 break;
7402 default:
7403 gcc_unreachable ();
7405 if (diagnosed)
7407 if (errtype == ic_argpass)
7408 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7409 else
7410 inform (location, "expected %qT but pointer is of type %qT",
7411 type, rhstype);
7413 return error_mark_node;
7416 /* Check if the right-hand side has a format attribute but the
7417 left-hand side doesn't. */
7418 if (warn_suggest_attribute_format
7419 && check_missing_format_attribute (type, rhstype))
7421 switch (errtype)
7423 case ic_argpass:
7424 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7425 "argument %d of %qE might be "
7426 "a candidate for a format attribute",
7427 parmnum, rname);
7428 break;
7429 case ic_assign:
7430 warning_at (location, OPT_Wsuggest_attribute_format,
7431 "assignment left-hand side might be "
7432 "a candidate for a format attribute");
7433 break;
7434 case ic_init:
7435 case ic_init_const:
7436 warning_at (location, OPT_Wsuggest_attribute_format,
7437 "initialization left-hand side might be "
7438 "a candidate for a format attribute");
7439 break;
7440 case ic_return:
7441 warning_at (location, OPT_Wsuggest_attribute_format,
7442 "return type might be "
7443 "a candidate for a format attribute");
7444 break;
7445 default:
7446 gcc_unreachable ();
7450 /* See if the pointers point to incompatible scalar storage orders. */
7451 if (warn_scalar_storage_order
7452 && !null_pointer_constant_p (rhs)
7453 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7454 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7456 tree t;
7458 switch (errtype)
7460 case ic_argpass:
7461 /* Do not warn for built-in functions, for example memcpy, since we
7462 control how they behave and they can be useful in this area. */
7463 if (TREE_CODE (rname) != FUNCTION_DECL
7464 || !fndecl_built_in_p (rname))
7465 warning_at (location, OPT_Wscalar_storage_order,
7466 "passing argument %d of %qE from incompatible "
7467 "scalar storage order", parmnum, rname);
7468 break;
7469 case ic_assign:
7470 /* Do not warn if the RHS is a call to a function that returns a
7471 pointer that is not an alias. */
7472 if (TREE_CODE (rhs) != CALL_EXPR
7473 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7474 || !DECL_IS_MALLOC (t))
7475 warning_at (location, OPT_Wscalar_storage_order,
7476 "assignment to %qT from pointer type %qT with "
7477 "incompatible scalar storage order", type, rhstype);
7478 break;
7479 case ic_init:
7480 case ic_init_const:
7481 /* Likewise. */
7482 if (TREE_CODE (rhs) != CALL_EXPR
7483 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7484 || !DECL_IS_MALLOC (t))
7485 warning_at (location, OPT_Wscalar_storage_order,
7486 "initialization of %qT from pointer type %qT with "
7487 "incompatible scalar storage order", type, rhstype);
7488 break;
7489 case ic_return:
7490 warning_at (location, OPT_Wscalar_storage_order,
7491 "returning %qT from pointer type with incompatible "
7492 "scalar storage order %qT", rhstype, type);
7493 break;
7494 default:
7495 gcc_unreachable ();
7499 /* Any non-function converts to a [const][volatile] void *
7500 and vice versa; otherwise, targets must be the same.
7501 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7502 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7503 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7504 || (target_cmp = comp_target_types (location, type, rhstype))
7505 || is_opaque_pointer
7506 || ((c_common_unsigned_type (mvl)
7507 == c_common_unsigned_type (mvr))
7508 && (c_common_signed_type (mvl)
7509 == c_common_signed_type (mvr))
7510 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7512 /* Warn about loss of qualifers from pointers to arrays with
7513 qualifiers on the element type. */
7514 if (TREE_CODE (ttr) == ARRAY_TYPE)
7516 ttr = strip_array_types (ttr);
7517 ttl = strip_array_types (ttl);
7519 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7520 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7521 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7522 location, expr_loc,
7523 OPT_Wdiscarded_array_qualifiers,
7524 G_("passing argument %d of %qE discards "
7525 "%qv qualifier from pointer target type"),
7526 G_("assignment discards %qv qualifier "
7527 "from pointer target type"),
7528 G_("initialization discards %qv qualifier "
7529 "from pointer target type"),
7530 G_("return discards %qv qualifier from "
7531 "pointer target type"),
7532 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7534 else if (pedantic
7535 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7537 (VOID_TYPE_P (ttr)
7538 && !null_pointer_constant
7539 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7540 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7541 G_("ISO C forbids passing argument %d of "
7542 "%qE between function pointer "
7543 "and %<void *%>"),
7544 G_("ISO C forbids assignment between "
7545 "function pointer and %<void *%>"),
7546 G_("ISO C forbids initialization between "
7547 "function pointer and %<void *%>"),
7548 G_("ISO C forbids return between function "
7549 "pointer and %<void *%>"));
7550 /* Const and volatile mean something different for function types,
7551 so the usual warnings are not appropriate. */
7552 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7553 && TREE_CODE (ttl) != FUNCTION_TYPE)
7555 /* Assignments between atomic and non-atomic objects are OK. */
7556 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7557 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7558 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7559 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7561 /* Don't warn about loss of qualifier for conversions from
7562 qualified void* to pointers to arrays with corresponding
7563 qualifier on the element type (except for pedantic before C23). */
7564 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7565 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7566 OPT_Wdiscarded_qualifiers,
7567 G_("passing argument %d of %qE discards "
7568 "%qv qualifier from pointer target type"),
7569 G_("assignment discards %qv qualifier "
7570 "from pointer target type"),
7571 G_("initialization discards %qv qualifier "
7572 "from pointer target type"),
7573 G_("return discards %qv qualifier from "
7574 "pointer target type"),
7575 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7576 else if (warn_quals_ped)
7577 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7578 "array with qualifier on the element is not qualified before C2X");
7580 /* If this is not a case of ignoring a mismatch in signedness,
7581 no warning. */
7582 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7583 || target_cmp)
7585 /* If there is a mismatch, do warn. */
7586 else if (warn_pointer_sign)
7587 switch (errtype)
7589 case ic_argpass:
7591 auto_diagnostic_group d;
7592 range_label_for_type_mismatch rhs_label (rhstype, type);
7593 gcc_rich_location richloc (expr_loc, &rhs_label);
7594 if (pedwarn (&richloc, OPT_Wpointer_sign,
7595 "pointer targets in passing argument %d of "
7596 "%qE differ in signedness", parmnum, rname))
7597 inform_for_arg (fundecl, expr_loc, parmnum, type,
7598 rhstype);
7600 break;
7601 case ic_assign:
7602 pedwarn (location, OPT_Wpointer_sign,
7603 "pointer targets in assignment from %qT to %qT "
7604 "differ in signedness", rhstype, type);
7605 break;
7606 case ic_init:
7607 case ic_init_const:
7608 pedwarn_init (location, OPT_Wpointer_sign,
7609 "pointer targets in initialization of %qT "
7610 "from %qT differ in signedness", type,
7611 rhstype);
7612 break;
7613 case ic_return:
7614 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7615 "returning %qT from a function with return type "
7616 "%qT differ in signedness", rhstype, type);
7617 break;
7618 default:
7619 gcc_unreachable ();
7622 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7623 && TREE_CODE (ttr) == FUNCTION_TYPE)
7625 /* Because const and volatile on functions are restrictions
7626 that say the function will not do certain things,
7627 it is okay to use a const or volatile function
7628 where an ordinary one is wanted, but not vice-versa. */
7629 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7630 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7631 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7632 OPT_Wdiscarded_qualifiers,
7633 G_("passing argument %d of %qE makes "
7634 "%q#v qualified function pointer "
7635 "from unqualified"),
7636 G_("assignment makes %q#v qualified function "
7637 "pointer from unqualified"),
7638 G_("initialization makes %q#v qualified "
7639 "function pointer from unqualified"),
7640 G_("return makes %q#v qualified function "
7641 "pointer from unqualified"),
7642 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7645 /* Avoid warning about the volatile ObjC EH puts on decls. */
7646 else if (!objc_ok)
7648 switch (errtype)
7650 case ic_argpass:
7652 auto_diagnostic_group d;
7653 range_label_for_type_mismatch rhs_label (rhstype, type);
7654 gcc_rich_location richloc (expr_loc, &rhs_label);
7655 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7656 "passing argument %d of %qE from incompatible "
7657 "pointer type", parmnum, rname))
7658 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7660 break;
7661 case ic_assign:
7662 if (bltin)
7663 pedwarn (location, OPT_Wincompatible_pointer_types,
7664 "assignment to %qT from pointer to "
7665 "%qD with incompatible type %qT",
7666 type, bltin, rhstype);
7667 else
7668 pedwarn (location, OPT_Wincompatible_pointer_types,
7669 "assignment to %qT from incompatible pointer type %qT",
7670 type, rhstype);
7671 break;
7672 case ic_init:
7673 case ic_init_const:
7674 if (bltin)
7675 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7676 "initialization of %qT from pointer to "
7677 "%qD with incompatible type %qT",
7678 type, bltin, rhstype);
7679 else
7680 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7681 "initialization of %qT from incompatible "
7682 "pointer type %qT",
7683 type, rhstype);
7684 break;
7685 case ic_return:
7686 if (bltin)
7687 pedwarn (location, OPT_Wincompatible_pointer_types,
7688 "returning pointer to %qD of type %qT from "
7689 "a function with incompatible type %qT",
7690 bltin, rhstype, type);
7691 else
7692 pedwarn (location, OPT_Wincompatible_pointer_types,
7693 "returning %qT from a function with incompatible "
7694 "return type %qT", rhstype, type);
7695 break;
7696 default:
7697 gcc_unreachable ();
7701 /* If RHS isn't an address, check pointer or array of packed
7702 struct or union. */
7703 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7705 return convert (type, rhs);
7707 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7709 /* ??? This should not be an error when inlining calls to
7710 unprototyped functions. */
7711 const char msg[] = "invalid use of non-lvalue array";
7712 if (warnopt)
7713 warning_at (location, warnopt, msg);
7714 else
7715 error_at (location, msg);
7716 return error_mark_node;
7718 else if (codel == POINTER_TYPE
7719 && (coder == INTEGER_TYPE
7720 || coder == NULLPTR_TYPE
7721 || coder == BITINT_TYPE))
7723 /* An explicit constant 0 or type nullptr_t can convert to a pointer,
7724 or one that results from arithmetic, even including a cast to
7725 integer type. */
7726 if (!null_pointer_constant && coder != NULLPTR_TYPE)
7727 switch (errtype)
7729 case ic_argpass:
7731 auto_diagnostic_group d;
7732 range_label_for_type_mismatch rhs_label (rhstype, type);
7733 gcc_rich_location richloc (expr_loc, &rhs_label);
7734 if (pedwarn (&richloc, OPT_Wint_conversion,
7735 "passing argument %d of %qE makes pointer from "
7736 "integer without a cast", parmnum, rname))
7737 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7739 break;
7740 case ic_assign:
7741 pedwarn (location, OPT_Wint_conversion,
7742 "assignment to %qT from %qT makes pointer from integer "
7743 "without a cast", type, rhstype);
7744 break;
7745 case ic_init:
7746 case ic_init_const:
7747 pedwarn_init (location, OPT_Wint_conversion,
7748 "initialization of %qT from %qT makes pointer from "
7749 "integer without a cast", type, rhstype);
7750 break;
7751 case ic_return:
7752 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7753 "function with return type %qT makes pointer from "
7754 "integer without a cast", rhstype, type);
7755 break;
7756 default:
7757 gcc_unreachable ();
7760 return convert (type, rhs);
7762 else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
7763 && coder == POINTER_TYPE)
7765 switch (errtype)
7767 case ic_argpass:
7769 auto_diagnostic_group d;
7770 range_label_for_type_mismatch rhs_label (rhstype, type);
7771 gcc_rich_location richloc (expr_loc, &rhs_label);
7772 if (pedwarn (&richloc, OPT_Wint_conversion,
7773 "passing argument %d of %qE makes integer from "
7774 "pointer without a cast", parmnum, rname))
7775 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7777 break;
7778 case ic_assign:
7779 pedwarn (location, OPT_Wint_conversion,
7780 "assignment to %qT from %qT makes integer from pointer "
7781 "without a cast", type, rhstype);
7782 break;
7783 case ic_init:
7784 case ic_init_const:
7785 pedwarn_init (location, OPT_Wint_conversion,
7786 "initialization of %qT from %qT makes integer from "
7787 "pointer without a cast", type, rhstype);
7788 break;
7789 case ic_return:
7790 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7791 "function with return type %qT makes integer from "
7792 "pointer without a cast", rhstype, type);
7793 break;
7794 default:
7795 gcc_unreachable ();
7798 return convert (type, rhs);
7800 else if (C_BOOLEAN_TYPE_P (type)
7801 /* The type nullptr_t may be converted to bool. The
7802 result is false. */
7803 && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
7805 tree ret;
7806 bool save = in_late_binary_op;
7807 in_late_binary_op = true;
7808 ret = convert (type, rhs);
7809 in_late_binary_op = save;
7810 return ret;
7812 else if (codel == NULLPTR_TYPE && null_pointer_constant)
7813 return convert (type, rhs);
7815 switch (errtype)
7817 case ic_argpass:
7819 auto_diagnostic_group d;
7820 range_label_for_type_mismatch rhs_label (rhstype, type);
7821 gcc_rich_location richloc (expr_loc, &rhs_label);
7822 const char msg[] = G_("incompatible type for argument %d of %qE");
7823 if (warnopt)
7824 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7825 else
7826 error_at (&richloc, msg, parmnum, rname);
7827 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7829 break;
7830 case ic_assign:
7832 const char msg[]
7833 = G_("incompatible types when assigning to type %qT from type %qT");
7834 if (warnopt)
7835 warning_at (expr_loc, 0, msg, type, rhstype);
7836 else
7837 error_at (expr_loc, msg, type, rhstype);
7838 break;
7840 case ic_init:
7841 case ic_init_const:
7843 const char msg[]
7844 = G_("incompatible types when initializing type %qT using type %qT");
7845 if (warnopt)
7846 warning_at (location, 0, msg, type, rhstype);
7847 else
7848 error_at (location, msg, type, rhstype);
7849 break;
7851 case ic_return:
7853 const char msg[]
7854 = G_("incompatible types when returning type %qT but %qT was expected");
7855 if (warnopt)
7856 warning_at (location, 0, msg, rhstype, type);
7857 else
7858 error_at (location, msg, rhstype, type);
7859 break;
7861 default:
7862 gcc_unreachable ();
7865 return error_mark_node;
7868 /* If VALUE is a compound expr all of whose expressions are constant, then
7869 return its value. Otherwise, return error_mark_node.
7871 This is for handling COMPOUND_EXPRs as initializer elements
7872 which is allowed with a warning when -pedantic is specified. */
7874 static tree
7875 valid_compound_expr_initializer (tree value, tree endtype)
7877 if (TREE_CODE (value) == COMPOUND_EXPR)
7879 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7880 == error_mark_node)
7881 return error_mark_node;
7882 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7883 endtype);
7885 else if (!initializer_constant_valid_p (value, endtype))
7886 return error_mark_node;
7887 else
7888 return value;
7891 /* Perform appropriate conversions on the initial value of a variable,
7892 store it in the declaration DECL,
7893 and print any error messages that are appropriate.
7894 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7895 If the init is invalid, store an ERROR_MARK.
7897 INIT_LOC is the location of the initial value. */
7899 void
7900 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7902 tree value, type;
7903 bool npc = false;
7904 bool int_const_expr = false;
7905 bool arith_const_expr = false;
7907 /* If variable's type was invalidly declared, just ignore it. */
7909 type = TREE_TYPE (decl);
7910 if (TREE_CODE (type) == ERROR_MARK)
7911 return;
7913 /* Digest the specified initializer into an expression. */
7915 if (init)
7917 npc = null_pointer_constant_p (init);
7918 int_const_expr = (TREE_CODE (init) == INTEGER_CST
7919 && !TREE_OVERFLOW (init)
7920 && INTEGRAL_TYPE_P (TREE_TYPE (init)));
7921 /* Not fully determined before folding. */
7922 arith_const_expr = true;
7924 bool constexpr_p = (VAR_P (decl)
7925 && C_DECL_DECLARED_CONSTEXPR (decl));
7926 value = digest_init (init_loc, type, init, origtype, npc, int_const_expr,
7927 arith_const_expr, true,
7928 TREE_STATIC (decl) || constexpr_p, constexpr_p);
7930 /* Store the expression if valid; else report error. */
7932 if (!in_system_header_at (input_location)
7933 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7934 warning (OPT_Wtraditional, "traditional C rejects automatic "
7935 "aggregate initialization");
7937 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7938 DECL_INITIAL (decl) = value;
7940 /* ANSI wants warnings about out-of-range constant initializers. */
7941 STRIP_TYPE_NOPS (value);
7942 if (TREE_STATIC (decl))
7943 constant_expression_warning (value);
7945 /* Check if we need to set array size from compound literal size. */
7946 if (TREE_CODE (type) == ARRAY_TYPE
7947 && TYPE_DOMAIN (type) == NULL_TREE
7948 && value != error_mark_node)
7950 tree inside_init = init;
7952 STRIP_TYPE_NOPS (inside_init);
7953 inside_init = fold (inside_init);
7955 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7957 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7959 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7961 /* For int foo[] = (int [3]){1}; we need to set array size
7962 now since later on array initializer will be just the
7963 brace enclosed list of the compound literal. */
7964 tree etype = strip_array_types (TREE_TYPE (decl));
7965 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7966 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7967 layout_type (type);
7968 layout_decl (cldecl, 0);
7969 TREE_TYPE (decl)
7970 = c_build_qualified_type (type, TYPE_QUALS (etype));
7976 /* Methods for storing and printing names for error messages. */
7978 /* Implement a spelling stack that allows components of a name to be pushed
7979 and popped. Each element on the stack is this structure. */
7981 struct spelling
7983 int kind;
7984 union
7986 unsigned HOST_WIDE_INT i;
7987 const char *s;
7988 } u;
7991 #define SPELLING_STRING 1
7992 #define SPELLING_MEMBER 2
7993 #define SPELLING_BOUNDS 3
7995 static struct spelling *spelling; /* Next stack element (unused). */
7996 static struct spelling *spelling_base; /* Spelling stack base. */
7997 static int spelling_size; /* Size of the spelling stack. */
7999 /* Macros to save and restore the spelling stack around push_... functions.
8000 Alternative to SAVE_SPELLING_STACK. */
8002 #define SPELLING_DEPTH() (spelling - spelling_base)
8003 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
8005 /* Push an element on the spelling stack with type KIND and assign VALUE
8006 to MEMBER. */
8008 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
8010 int depth = SPELLING_DEPTH (); \
8012 if (depth >= spelling_size) \
8014 spelling_size += 10; \
8015 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
8016 spelling_size); \
8017 RESTORE_SPELLING_DEPTH (depth); \
8020 spelling->kind = (KIND); \
8021 spelling->MEMBER = (VALUE); \
8022 spelling++; \
8025 /* Push STRING on the stack. Printed literally. */
8027 static void
8028 push_string (const char *string)
8030 PUSH_SPELLING (SPELLING_STRING, string, u.s);
8033 /* Push a member name on the stack. Printed as '.' STRING. */
8035 static void
8036 push_member_name (tree decl)
8038 const char *const string
8039 = (DECL_NAME (decl)
8040 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
8041 : _("<anonymous>"));
8042 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
8045 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
8047 static void
8048 push_array_bounds (unsigned HOST_WIDE_INT bounds)
8050 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
8053 /* Compute the maximum size in bytes of the printed spelling. */
8055 static int
8056 spelling_length (void)
8058 int size = 0;
8059 struct spelling *p;
8061 for (p = spelling_base; p < spelling; p++)
8063 if (p->kind == SPELLING_BOUNDS)
8064 size += 25;
8065 else
8066 size += strlen (p->u.s) + 1;
8069 return size;
8072 /* Print the spelling to BUFFER and return it. */
8074 static char *
8075 print_spelling (char *buffer)
8077 char *d = buffer;
8078 struct spelling *p;
8080 for (p = spelling_base; p < spelling; p++)
8081 if (p->kind == SPELLING_BOUNDS)
8083 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
8084 d += strlen (d);
8086 else
8088 const char *s;
8089 if (p->kind == SPELLING_MEMBER)
8090 *d++ = '.';
8091 for (s = p->u.s; (*d = *s++); d++)
8094 *d++ = '\0';
8095 return buffer;
8098 /* Check whether INIT, a floating or integer constant, is
8099 representable in TYPE, a real floating type with the same radix or
8100 a decimal floating type initialized with a binary floating
8101 constant. Return true if OK, false if not. */
8102 static bool
8103 constexpr_init_fits_real_type (tree type, tree init)
8105 gcc_assert (SCALAR_FLOAT_TYPE_P (type));
8106 gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
8107 if (TREE_CODE (init) == REAL_CST
8108 && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
8110 /* Same mode, no conversion required except for the case of
8111 signaling NaNs if the types are incompatible (e.g. double and
8112 long double with the same mode). */
8113 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
8114 && !comptypes (TYPE_MAIN_VARIANT (type),
8115 TYPE_MAIN_VARIANT (TREE_TYPE (init))))
8116 return false;
8117 return true;
8119 if (TREE_CODE (init) == INTEGER_CST)
8121 tree converted = build_real_from_int_cst (type, init);
8122 bool fail = false;
8123 wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
8124 TYPE_PRECISION (TREE_TYPE (init)));
8125 return !fail && wi::eq_p (w, wi::to_wide (init));
8127 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
8128 return false;
8129 if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
8130 && MODE_HAS_INFINITIES (TYPE_MODE (type)))
8131 || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
8132 && MODE_HAS_NANS (TYPE_MODE (type))))
8133 return true;
8134 if (DECIMAL_FLOAT_TYPE_P (type)
8135 && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
8137 /* This is valid if the real number represented by the
8138 initializer can be exactly represented in the decimal
8139 type. Compare the values using MPFR. */
8140 REAL_VALUE_TYPE t;
8141 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8142 mpfr_t bin_val, dec_val;
8143 mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
8144 mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
8145 mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
8146 char string[256];
8147 real_to_decimal (string, &t, sizeof string, 0, 1);
8148 bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
8149 && mpfr_equal_p (bin_val, dec_val));
8150 mpfr_clear (bin_val);
8151 mpfr_clear (dec_val);
8152 return res;
8154 /* exact_real_truncate is not quite right here, since it doesn't
8155 allow even an exact conversion to subnormal values. */
8156 REAL_VALUE_TYPE t;
8157 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8158 return real_identical (&t, &TREE_REAL_CST (init));
8161 /* Check whether INIT (location LOC) is valid as a 'constexpr'
8162 initializer for type TYPE, and give an error if not. INIT has
8163 already been folded and verified to be constant. INT_CONST_EXPR
8164 and ARITH_CONST_EXPR say whether it is an integer constant
8165 expression or arithmetic constant expression, respectively. If
8166 TYPE is not a scalar type, this function does nothing. */
8168 static void
8169 check_constexpr_init (location_t loc, tree type, tree init,
8170 bool int_const_expr, bool arith_const_expr)
8172 if (POINTER_TYPE_P (type))
8174 /* The initializer must be null. */
8175 if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
8176 error_at (loc, "%<constexpr%> pointer initializer is not null");
8177 return;
8179 if (INTEGRAL_TYPE_P (type))
8181 /* The initializer must be an integer constant expression,
8182 representable in the target type. */
8183 if (!int_const_expr)
8184 error_at (loc, "%<constexpr%> integer initializer is not an "
8185 "integer constant expression");
8186 if (!int_fits_type_p (init, type))
8187 error_at (loc, "%<constexpr%> initializer not representable in "
8188 "type of object");
8189 return;
8191 /* We don't apply any extra checks to extension types such as vector
8192 or fixed-point types. */
8193 if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
8194 return;
8195 if (!arith_const_expr)
8197 error_at (loc, "%<constexpr%> initializer is not an arithmetic "
8198 "constant expression");
8199 return;
8201 /* We don't apply any extra checks to complex integers. */
8202 if (TREE_CODE (type) == COMPLEX_TYPE
8203 && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8204 return;
8205 /* Following N3082, a real type cannot be initialized from a complex
8206 type and a binary type cannot be initialized from a decimal type
8207 (but initializing a decimal type from a binary type is OK).
8208 Signaling NaN initializers are OK only if the types are
8209 compatible (not just the same mode); all quiet NaN and infinity
8210 initializations are considered to preserve the value. */
8211 if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
8212 && SCALAR_FLOAT_TYPE_P (type))
8214 error_at (loc, "%<constexpr%> initializer for a real type is of "
8215 "complex type");
8216 return;
8218 if (SCALAR_FLOAT_TYPE_P (type)
8219 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
8220 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
8221 && !DECIMAL_FLOAT_TYPE_P (type))
8223 error_at (loc, "%<constexpr%> initializer for a binary "
8224 "floating-point type is of decimal type");
8225 return;
8227 bool fits;
8228 if (TREE_CODE (type) == COMPLEX_TYPE)
8230 switch (TREE_CODE (init))
8232 case INTEGER_CST:
8233 case REAL_CST:
8234 fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
8235 break;
8236 case COMPLEX_CST:
8237 fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
8238 TREE_REALPART (init))
8239 && constexpr_init_fits_real_type (TREE_TYPE (type),
8240 TREE_IMAGPART (init)));
8241 break;
8242 default:
8243 gcc_unreachable ();
8246 else
8247 fits = constexpr_init_fits_real_type (type, init);
8248 if (!fits)
8249 error_at (loc, "%<constexpr%> initializer not representable in "
8250 "type of object");
8253 /* Digest the parser output INIT as an initializer for type TYPE.
8254 Return a C expression of type TYPE to represent the initial value.
8256 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8258 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
8259 INT_CONST_EXPR is true if INIT is an integer constant expression,
8260 and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
8261 constant expression, false if it has already been determined in the
8262 caller that it is not (but folding may have made the value passed here
8263 indistinguishable from an arithmetic constant expression).
8265 If INIT is a string constant, STRICT_STRING is true if it is
8266 unparenthesized or we should not warn here for it being parenthesized.
8267 For other types of INIT, STRICT_STRING is not used.
8269 INIT_LOC is the location of the INIT.
8271 REQUIRE_CONSTANT requests an error if non-constant initializers or
8272 elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
8273 on initializers for 'constexpr' objects apply. */
8275 static tree
8276 digest_init (location_t init_loc, tree type, tree init, tree origtype,
8277 bool null_pointer_constant, bool int_const_expr,
8278 bool arith_const_expr, bool strict_string,
8279 bool require_constant, bool require_constexpr)
8281 enum tree_code code = TREE_CODE (type);
8282 tree inside_init = init;
8283 tree semantic_type = NULL_TREE;
8284 bool maybe_const = true;
8286 if (type == error_mark_node
8287 || !init
8288 || error_operand_p (init))
8289 return error_mark_node;
8291 STRIP_TYPE_NOPS (inside_init);
8293 if (!c_in_omp_for)
8295 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
8297 semantic_type = TREE_TYPE (inside_init);
8298 inside_init = TREE_OPERAND (inside_init, 0);
8300 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
8302 /* TODO: this may not detect all cases of expressions folding to
8303 constants that are not arithmetic constant expressions. */
8304 if (!maybe_const)
8305 arith_const_expr = false;
8306 else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
8307 && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
8308 && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
8309 arith_const_expr = false;
8310 else if (TREE_CODE (inside_init) != INTEGER_CST
8311 && TREE_CODE (inside_init) != REAL_CST
8312 && TREE_CODE (inside_init) != COMPLEX_CST)
8313 arith_const_expr = false;
8314 else if (TREE_OVERFLOW (inside_init))
8315 arith_const_expr = false;
8317 /* Initialization of an array of chars from a string constant
8318 optionally enclosed in braces. */
8320 if (code == ARRAY_TYPE && inside_init
8321 && TREE_CODE (inside_init) == STRING_CST)
8323 tree typ1
8324 = (TYPE_ATOMIC (TREE_TYPE (type))
8325 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8326 TYPE_QUAL_ATOMIC)
8327 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8328 /* Note that an array could be both an array of character type
8329 and an array of wchar_t if wchar_t is signed char or unsigned
8330 char. */
8331 bool char_array = (typ1 == char_type_node
8332 || typ1 == signed_char_type_node
8333 || typ1 == unsigned_char_type_node);
8334 bool wchar_array = !!comptypes (typ1, wchar_type_node);
8335 bool char16_array = !!comptypes (typ1, char16_type_node);
8336 bool char32_array = !!comptypes (typ1, char32_type_node);
8338 if (char_array || wchar_array || char16_array || char32_array)
8340 struct c_expr expr;
8341 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8342 bool incompat_string_cst = false;
8343 expr.value = inside_init;
8344 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8345 expr.original_type = NULL;
8346 expr.m_decimal = 0;
8347 maybe_warn_string_init (init_loc, type, expr);
8349 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8350 pedwarn_init (init_loc, OPT_Wpedantic,
8351 "initialization of a flexible array member");
8353 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8354 TYPE_MAIN_VARIANT (type)))
8355 return inside_init;
8357 if (char_array)
8359 if (typ2 != char_type_node && typ2 != char8_type_node)
8360 incompat_string_cst = true;
8362 else if (!comptypes (typ1, typ2))
8363 incompat_string_cst = true;
8365 if (incompat_string_cst)
8367 error_init (init_loc, "cannot initialize array of %qT from "
8368 "a string literal with type array of %qT",
8369 typ1, typ2);
8370 return error_mark_node;
8373 if (require_constexpr
8374 && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
8376 /* Check if all characters of the string can be
8377 represented in the type of the constexpr object being
8378 initialized. */
8379 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8380 const unsigned char *p =
8381 (const unsigned char *) TREE_STRING_POINTER (inside_init);
8382 gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
8383 for (unsigned i = 0; i < len; i++)
8384 if (p[i] > 127)
8386 error_init (init_loc, "%<constexpr%> initializer not "
8387 "representable in type of object");
8388 break;
8392 if (TYPE_DOMAIN (type) != NULL_TREE
8393 && TYPE_SIZE (type) != NULL_TREE
8394 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8396 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8397 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8399 /* Subtract the size of a single (possibly wide) character
8400 because it's ok to ignore the terminating null char
8401 that is counted in the length of the constant. */
8402 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8403 pedwarn_init (init_loc, 0,
8404 ("initializer-string for array of %qT "
8405 "is too long"), typ1);
8406 else if (warn_cxx_compat
8407 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8408 warning_at (init_loc, OPT_Wc___compat,
8409 ("initializer-string for array of %qT "
8410 "is too long for C++"), typ1);
8411 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8413 unsigned HOST_WIDE_INT size
8414 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8415 const char *p = TREE_STRING_POINTER (inside_init);
8417 inside_init = build_string (size, p);
8421 TREE_TYPE (inside_init) = type;
8422 return inside_init;
8424 else if (INTEGRAL_TYPE_P (typ1))
8426 error_init (init_loc, "array of inappropriate type initialized "
8427 "from string constant");
8428 return error_mark_node;
8432 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8433 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8434 below and handle as a constructor. */
8435 if (code == VECTOR_TYPE
8436 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8437 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8438 && TREE_CONSTANT (inside_init))
8440 if (TREE_CODE (inside_init) == VECTOR_CST
8441 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8442 TYPE_MAIN_VARIANT (type)))
8443 return inside_init;
8445 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8447 unsigned HOST_WIDE_INT ix;
8448 tree value;
8449 bool constant_p = true;
8451 /* Iterate through elements and check if all constructor
8452 elements are *_CSTs. */
8453 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8454 if (!CONSTANT_CLASS_P (value))
8456 constant_p = false;
8457 break;
8460 if (constant_p)
8461 return build_vector_from_ctor (type,
8462 CONSTRUCTOR_ELTS (inside_init));
8466 if (warn_sequence_point)
8467 verify_sequence_points (inside_init);
8469 /* Any type can be initialized
8470 from an expression of the same type, optionally with braces. */
8472 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8473 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8474 TYPE_MAIN_VARIANT (type))
8475 || (code == ARRAY_TYPE
8476 && comptypes (TREE_TYPE (inside_init), type))
8477 || (gnu_vector_type_p (type)
8478 && comptypes (TREE_TYPE (inside_init), type))
8479 || (code == POINTER_TYPE
8480 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8481 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8482 TREE_TYPE (type)))))
8484 if (code == POINTER_TYPE)
8486 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8488 if (TREE_CODE (inside_init) == STRING_CST
8489 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8490 inside_init = array_to_pointer_conversion
8491 (init_loc, inside_init);
8492 else
8494 error_init (init_loc, "invalid use of non-lvalue array");
8495 return error_mark_node;
8500 if (code == VECTOR_TYPE)
8501 /* Although the types are compatible, we may require a
8502 conversion. */
8503 inside_init = convert (type, inside_init);
8505 if (require_constant
8506 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8508 /* As an extension, allow initializing objects with static storage
8509 duration with compound literals (which are then treated just as
8510 the brace enclosed list they contain). Also allow this for
8511 vectors, as we can only assign them with compound literals. */
8512 if (flag_isoc99 && code != VECTOR_TYPE)
8513 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8514 "is not constant");
8515 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8516 inside_init = DECL_INITIAL (decl);
8519 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8520 && TREE_CODE (inside_init) != CONSTRUCTOR)
8522 error_init (init_loc, "array initialized from non-constant array "
8523 "expression");
8524 return error_mark_node;
8527 /* Compound expressions can only occur here if -Wpedantic or
8528 -pedantic-errors is specified. In the later case, we always want
8529 an error. In the former case, we simply want a warning. */
8530 if (require_constant && pedantic
8531 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8533 inside_init
8534 = valid_compound_expr_initializer (inside_init,
8535 TREE_TYPE (inside_init));
8536 if (inside_init == error_mark_node)
8537 error_init (init_loc, "initializer element is not constant");
8538 else
8539 pedwarn_init (init_loc, OPT_Wpedantic,
8540 "initializer element is not constant");
8541 if (flag_pedantic_errors)
8542 inside_init = error_mark_node;
8544 else if (require_constant
8545 && !initializer_constant_valid_p (inside_init,
8546 TREE_TYPE (inside_init)))
8548 error_init (init_loc, "initializer element is not constant");
8549 inside_init = error_mark_node;
8551 else if (require_constant && !maybe_const)
8552 pedwarn_init (init_loc, OPT_Wpedantic,
8553 "initializer element is not a constant expression");
8554 else if (require_constexpr)
8555 check_constexpr_init (init_loc, type, inside_init,
8556 int_const_expr, arith_const_expr);
8558 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8559 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8560 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8561 type, inside_init, origtype,
8562 (require_constant
8563 ? ic_init_const
8564 : ic_init), null_pointer_constant,
8565 NULL_TREE, NULL_TREE, 0);
8566 return inside_init;
8569 /* Handle scalar types, including conversions. */
8571 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8572 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8573 || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
8574 || code == BITINT_TYPE)
8576 tree unconverted_init = inside_init;
8577 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8578 && (TREE_CODE (init) == STRING_CST
8579 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8580 inside_init = init = array_to_pointer_conversion (init_loc, init);
8581 if (semantic_type)
8582 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8583 inside_init);
8584 inside_init
8585 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8586 inside_init, origtype,
8587 require_constant ? ic_init_const : ic_init,
8588 null_pointer_constant, NULL_TREE, NULL_TREE,
8591 /* Check to see if we have already given an error message. */
8592 if (inside_init == error_mark_node)
8594 else if (require_constant && !TREE_CONSTANT (inside_init))
8596 error_init (init_loc, "initializer element is not constant");
8597 inside_init = error_mark_node;
8599 else if (require_constant
8600 && !initializer_constant_valid_p (inside_init,
8601 TREE_TYPE (inside_init)))
8603 error_init (init_loc, "initializer element is not computable at "
8604 "load time");
8605 inside_init = error_mark_node;
8607 else if (require_constant && !maybe_const)
8608 pedwarn_init (init_loc, OPT_Wpedantic,
8609 "initializer element is not a constant expression");
8610 else if (require_constexpr)
8611 check_constexpr_init (init_loc, type, unconverted_init,
8612 int_const_expr, arith_const_expr);
8614 return inside_init;
8617 /* Come here only for records and arrays. */
8619 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8621 error_init (init_loc,
8622 "variable-sized object may not be initialized except "
8623 "with an empty initializer");
8624 return error_mark_node;
8627 error_init (init_loc, "invalid initializer");
8628 return error_mark_node;
8631 /* Handle initializers that use braces. */
8633 /* Type of object we are accumulating a constructor for.
8634 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8635 static tree constructor_type;
8637 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8638 left to fill. */
8639 static tree constructor_fields;
8641 /* For an ARRAY_TYPE, this is the specified index
8642 at which to store the next element we get. */
8643 static tree constructor_index;
8645 /* For an ARRAY_TYPE, this is the maximum index. */
8646 static tree constructor_max_index;
8648 /* For a RECORD_TYPE, this is the first field not yet written out. */
8649 static tree constructor_unfilled_fields;
8651 /* For an ARRAY_TYPE, this is the index of the first element
8652 not yet written out. */
8653 static tree constructor_unfilled_index;
8655 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8656 This is so we can generate gaps between fields, when appropriate. */
8657 static tree constructor_bit_index;
8659 /* If we are saving up the elements rather than allocating them,
8660 this is the list of elements so far (in reverse order,
8661 most recent first). */
8662 static vec<constructor_elt, va_gc> *constructor_elements;
8664 /* 1 if constructor should be incrementally stored into a constructor chain,
8665 0 if all the elements should be kept in AVL tree. */
8666 static int constructor_incremental;
8668 /* 1 if so far this constructor's elements are all compile-time constants. */
8669 static int constructor_constant;
8671 /* 1 if so far this constructor's elements are all valid address constants. */
8672 static int constructor_simple;
8674 /* 1 if this constructor has an element that cannot be part of a
8675 constant expression. */
8676 static int constructor_nonconst;
8678 /* 1 if this constructor is erroneous so far. */
8679 static int constructor_erroneous;
8681 /* 1 if this constructor is the universal zero initializer { 0 }. */
8682 static int constructor_zeroinit;
8684 /* Structure for managing pending initializer elements, organized as an
8685 AVL tree. */
8687 struct init_node
8689 struct init_node *left, *right;
8690 struct init_node *parent;
8691 int balance;
8692 tree purpose;
8693 tree value;
8694 tree origtype;
8697 /* Tree of pending elements at this constructor level.
8698 These are elements encountered out of order
8699 which belong at places we haven't reached yet in actually
8700 writing the output.
8701 Will never hold tree nodes across GC runs. */
8702 static struct init_node *constructor_pending_elts;
8704 /* The SPELLING_DEPTH of this constructor. */
8705 static int constructor_depth;
8707 /* DECL node for which an initializer is being read.
8708 0 means we are reading a constructor expression
8709 such as (struct foo) {...}. */
8710 static tree constructor_decl;
8712 /* Nonzero if there were any member designators in this initializer. */
8713 static int constructor_designated;
8715 /* Nesting depth of designator list. */
8716 static int designator_depth;
8718 /* Nonzero if there were diagnosed errors in this designator list. */
8719 static int designator_erroneous;
8722 /* This stack has a level for each implicit or explicit level of
8723 structuring in the initializer, including the outermost one. It
8724 saves the values of most of the variables above. */
8726 struct constructor_range_stack;
8728 struct constructor_stack
8730 struct constructor_stack *next;
8731 tree type;
8732 tree fields;
8733 tree index;
8734 tree max_index;
8735 tree unfilled_index;
8736 tree unfilled_fields;
8737 tree bit_index;
8738 vec<constructor_elt, va_gc> *elements;
8739 struct init_node *pending_elts;
8740 int offset;
8741 int depth;
8742 /* If value nonzero, this value should replace the entire
8743 constructor at this level. */
8744 struct c_expr replacement_value;
8745 struct constructor_range_stack *range_stack;
8746 char constant;
8747 char simple;
8748 char nonconst;
8749 char implicit;
8750 char erroneous;
8751 char outer;
8752 char incremental;
8753 char designated;
8754 int designator_depth;
8757 static struct constructor_stack *constructor_stack;
8759 /* This stack represents designators from some range designator up to
8760 the last designator in the list. */
8762 struct constructor_range_stack
8764 struct constructor_range_stack *next, *prev;
8765 struct constructor_stack *stack;
8766 tree range_start;
8767 tree index;
8768 tree range_end;
8769 tree fields;
8772 static struct constructor_range_stack *constructor_range_stack;
8774 /* This stack records separate initializers that are nested.
8775 Nested initializers can't happen in ANSI C, but GNU C allows them
8776 in cases like { ... (struct foo) { ... } ... }. */
8778 struct initializer_stack
8780 struct initializer_stack *next;
8781 tree decl;
8782 struct constructor_stack *constructor_stack;
8783 struct constructor_range_stack *constructor_range_stack;
8784 vec<constructor_elt, va_gc> *elements;
8785 struct spelling *spelling;
8786 struct spelling *spelling_base;
8787 int spelling_size;
8788 char require_constant_value;
8789 char require_constant_elements;
8790 char require_constexpr_value;
8791 char designated;
8792 rich_location *missing_brace_richloc;
8795 static struct initializer_stack *initializer_stack;
8797 /* Prepare to parse and output the initializer for variable DECL. */
8799 void
8800 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
8801 bool init_require_constant, bool init_require_constexpr,
8802 rich_location *richloc)
8804 const char *locus;
8805 struct initializer_stack *p = XNEW (struct initializer_stack);
8807 p->decl = constructor_decl;
8808 p->require_constant_value = require_constant_value;
8809 p->require_constant_elements = require_constant_elements;
8810 p->require_constexpr_value = require_constexpr_value;
8811 p->constructor_stack = constructor_stack;
8812 p->constructor_range_stack = constructor_range_stack;
8813 p->elements = constructor_elements;
8814 p->spelling = spelling;
8815 p->spelling_base = spelling_base;
8816 p->spelling_size = spelling_size;
8817 p->next = initializer_stack;
8818 p->missing_brace_richloc = richloc;
8819 p->designated = constructor_designated;
8820 initializer_stack = p;
8822 constructor_decl = decl;
8823 constructor_designated = 0;
8825 require_constant_value = init_require_constant;
8826 require_constexpr_value = init_require_constexpr;
8827 if (decl != NULL_TREE && decl != error_mark_node)
8829 require_constant_elements
8830 = ((init_require_constant || (pedantic && !flag_isoc99))
8831 /* For a scalar, you can always use any value to initialize,
8832 even within braces. */
8833 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8834 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8836 else
8838 require_constant_elements = false;
8839 locus = _("(anonymous)");
8842 constructor_stack = 0;
8843 constructor_range_stack = 0;
8845 found_missing_braces = 0;
8847 spelling_base = 0;
8848 spelling_size = 0;
8849 RESTORE_SPELLING_DEPTH (0);
8851 if (locus)
8852 push_string (locus);
8855 void
8856 finish_init (void)
8858 struct initializer_stack *p = initializer_stack;
8860 /* Free the whole constructor stack of this initializer. */
8861 while (constructor_stack)
8863 struct constructor_stack *q = constructor_stack;
8864 constructor_stack = q->next;
8865 XDELETE (q);
8868 gcc_assert (!constructor_range_stack);
8870 /* Pop back to the data of the outer initializer (if any). */
8871 XDELETE (spelling_base);
8873 constructor_decl = p->decl;
8874 require_constant_value = p->require_constant_value;
8875 require_constant_elements = p->require_constant_elements;
8876 require_constexpr_value = p->require_constexpr_value;
8877 constructor_stack = p->constructor_stack;
8878 constructor_designated = p->designated;
8879 constructor_range_stack = p->constructor_range_stack;
8880 constructor_elements = p->elements;
8881 spelling = p->spelling;
8882 spelling_base = p->spelling_base;
8883 spelling_size = p->spelling_size;
8884 initializer_stack = p->next;
8885 XDELETE (p);
8888 /* Call here when we see the initializer is surrounded by braces.
8889 This is instead of a call to push_init_level;
8890 it is matched by a call to pop_init_level.
8892 TYPE is the type to initialize, for a constructor expression.
8893 For an initializer for a decl, TYPE is zero. */
8895 void
8896 really_start_incremental_init (tree type)
8898 struct constructor_stack *p = XNEW (struct constructor_stack);
8900 if (type == NULL_TREE)
8901 type = TREE_TYPE (constructor_decl);
8903 if (VECTOR_TYPE_P (type)
8904 && TYPE_VECTOR_OPAQUE (type))
8905 error ("opaque vector types cannot be initialized");
8907 p->type = constructor_type;
8908 p->fields = constructor_fields;
8909 p->index = constructor_index;
8910 p->max_index = constructor_max_index;
8911 p->unfilled_index = constructor_unfilled_index;
8912 p->unfilled_fields = constructor_unfilled_fields;
8913 p->bit_index = constructor_bit_index;
8914 p->elements = constructor_elements;
8915 p->constant = constructor_constant;
8916 p->simple = constructor_simple;
8917 p->nonconst = constructor_nonconst;
8918 p->erroneous = constructor_erroneous;
8919 p->pending_elts = constructor_pending_elts;
8920 p->depth = constructor_depth;
8921 p->replacement_value.value = 0;
8922 p->replacement_value.original_code = ERROR_MARK;
8923 p->replacement_value.original_type = NULL;
8924 p->implicit = 0;
8925 p->range_stack = 0;
8926 p->outer = 0;
8927 p->incremental = constructor_incremental;
8928 p->designated = constructor_designated;
8929 p->designator_depth = designator_depth;
8930 p->next = 0;
8931 constructor_stack = p;
8933 constructor_constant = 1;
8934 constructor_simple = 1;
8935 constructor_nonconst = 0;
8936 constructor_depth = SPELLING_DEPTH ();
8937 constructor_elements = NULL;
8938 constructor_pending_elts = 0;
8939 constructor_type = type;
8940 constructor_incremental = 1;
8941 constructor_designated = 0;
8942 constructor_zeroinit = 1;
8943 designator_depth = 0;
8944 designator_erroneous = 0;
8946 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8948 constructor_fields = TYPE_FIELDS (constructor_type);
8949 /* Skip any nameless bit fields at the beginning. */
8950 while (constructor_fields != NULL_TREE
8951 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8952 constructor_fields = DECL_CHAIN (constructor_fields);
8954 constructor_unfilled_fields = constructor_fields;
8955 constructor_bit_index = bitsize_zero_node;
8957 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8959 if (TYPE_DOMAIN (constructor_type))
8961 constructor_max_index
8962 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8964 /* Detect non-empty initializations of zero-length arrays. */
8965 if (constructor_max_index == NULL_TREE
8966 && TYPE_SIZE (constructor_type))
8967 constructor_max_index = integer_minus_one_node;
8969 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8970 to initialize VLAs with a nonempty initializer will cause a
8971 proper error; avoid tree checking errors as well by setting a
8972 safe value. */
8973 if (constructor_max_index
8974 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8975 constructor_max_index = integer_minus_one_node;
8977 constructor_index
8978 = convert (bitsizetype,
8979 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8981 else
8983 constructor_index = bitsize_zero_node;
8984 constructor_max_index = NULL_TREE;
8987 constructor_unfilled_index = constructor_index;
8989 else if (gnu_vector_type_p (constructor_type))
8991 /* Vectors are like simple fixed-size arrays. */
8992 constructor_max_index =
8993 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8994 constructor_index = bitsize_zero_node;
8995 constructor_unfilled_index = constructor_index;
8997 else
8999 /* Handle the case of int x = {5}; */
9000 constructor_fields = constructor_type;
9001 constructor_unfilled_fields = constructor_type;
9005 extern location_t last_init_list_comma;
9007 /* Called when we see an open brace for a nested initializer. Finish
9008 off any pending levels with implicit braces. */
9009 void
9010 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
9012 while (constructor_stack->implicit)
9014 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9015 && constructor_fields == NULL_TREE)
9016 process_init_element (input_location,
9017 pop_init_level (loc, 1, braced_init_obstack,
9018 last_init_list_comma),
9019 true, braced_init_obstack);
9020 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
9021 && constructor_max_index
9022 && tree_int_cst_lt (constructor_max_index,
9023 constructor_index))
9024 process_init_element (input_location,
9025 pop_init_level (loc, 1, braced_init_obstack,
9026 last_init_list_comma),
9027 true, braced_init_obstack);
9028 else
9029 break;
9033 /* Push down into a subobject, for initialization.
9034 If this is for an explicit set of braces, IMPLICIT is 0.
9035 If it is because the next element belongs at a lower level,
9036 IMPLICIT is 1 (or 2 if the push is because of designator list). */
9038 void
9039 push_init_level (location_t loc, int implicit,
9040 struct obstack *braced_init_obstack)
9042 struct constructor_stack *p;
9043 tree value = NULL_TREE;
9045 /* Unless this is an explicit brace, we need to preserve previous
9046 content if any. */
9047 if (implicit)
9049 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
9050 value = find_init_member (constructor_fields, braced_init_obstack);
9051 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9052 value = find_init_member (constructor_index, braced_init_obstack);
9055 p = XNEW (struct constructor_stack);
9056 p->type = constructor_type;
9057 p->fields = constructor_fields;
9058 p->index = constructor_index;
9059 p->max_index = constructor_max_index;
9060 p->unfilled_index = constructor_unfilled_index;
9061 p->unfilled_fields = constructor_unfilled_fields;
9062 p->bit_index = constructor_bit_index;
9063 p->elements = constructor_elements;
9064 p->constant = constructor_constant;
9065 p->simple = constructor_simple;
9066 p->nonconst = constructor_nonconst;
9067 p->erroneous = constructor_erroneous;
9068 p->pending_elts = constructor_pending_elts;
9069 p->depth = constructor_depth;
9070 p->replacement_value.value = NULL_TREE;
9071 p->replacement_value.original_code = ERROR_MARK;
9072 p->replacement_value.original_type = NULL;
9073 p->implicit = implicit;
9074 p->outer = 0;
9075 p->incremental = constructor_incremental;
9076 p->designated = constructor_designated;
9077 p->designator_depth = designator_depth;
9078 p->next = constructor_stack;
9079 p->range_stack = 0;
9080 constructor_stack = p;
9082 constructor_constant = 1;
9083 constructor_simple = 1;
9084 constructor_nonconst = 0;
9085 constructor_depth = SPELLING_DEPTH ();
9086 constructor_elements = NULL;
9087 constructor_incremental = 1;
9088 /* If the upper initializer is designated, then mark this as
9089 designated too to prevent bogus warnings. */
9090 constructor_designated = p->designated;
9091 constructor_pending_elts = 0;
9092 if (!implicit)
9094 p->range_stack = constructor_range_stack;
9095 constructor_range_stack = 0;
9096 designator_depth = 0;
9097 designator_erroneous = 0;
9100 /* Don't die if an entire brace-pair level is superfluous
9101 in the containing level. */
9102 if (constructor_type == NULL_TREE)
9104 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9106 /* Don't die if there are extra init elts at the end. */
9107 if (constructor_fields == NULL_TREE)
9108 constructor_type = NULL_TREE;
9109 else
9111 constructor_type = TREE_TYPE (constructor_fields);
9112 push_member_name (constructor_fields);
9113 constructor_depth++;
9116 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9118 constructor_type = TREE_TYPE (constructor_type);
9119 push_array_bounds (tree_to_uhwi (constructor_index));
9120 constructor_depth++;
9123 if (constructor_type == NULL_TREE)
9125 error_init (loc, "extra brace group at end of initializer");
9126 constructor_fields = NULL_TREE;
9127 constructor_unfilled_fields = NULL_TREE;
9128 return;
9131 if (value && TREE_CODE (value) == CONSTRUCTOR)
9133 constructor_constant = TREE_CONSTANT (value);
9134 constructor_simple = TREE_STATIC (value);
9135 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
9136 constructor_elements = CONSTRUCTOR_ELTS (value);
9137 if (!vec_safe_is_empty (constructor_elements)
9138 && (TREE_CODE (constructor_type) == RECORD_TYPE
9139 || TREE_CODE (constructor_type) == ARRAY_TYPE))
9140 set_nonincremental_init (braced_init_obstack);
9143 if (implicit == 1)
9145 found_missing_braces = 1;
9146 if (initializer_stack->missing_brace_richloc)
9147 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9148 (loc, "{");
9151 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9153 constructor_fields = TYPE_FIELDS (constructor_type);
9154 /* Skip any nameless bit fields at the beginning. */
9155 while (constructor_fields != NULL_TREE
9156 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9157 constructor_fields = DECL_CHAIN (constructor_fields);
9159 constructor_unfilled_fields = constructor_fields;
9160 constructor_bit_index = bitsize_zero_node;
9162 else if (gnu_vector_type_p (constructor_type))
9164 /* Vectors are like simple fixed-size arrays. */
9165 constructor_max_index =
9166 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
9167 constructor_index = bitsize_int (0);
9168 constructor_unfilled_index = constructor_index;
9170 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9172 if (TYPE_DOMAIN (constructor_type))
9174 constructor_max_index
9175 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
9177 /* Detect non-empty initializations of zero-length arrays. */
9178 if (constructor_max_index == NULL_TREE
9179 && TYPE_SIZE (constructor_type))
9180 constructor_max_index = integer_minus_one_node;
9182 /* constructor_max_index needs to be an INTEGER_CST. Attempts
9183 to initialize VLAs will cause a proper error; avoid tree
9184 checking errors as well by setting a safe value. */
9185 if (constructor_max_index
9186 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9187 constructor_max_index = integer_minus_one_node;
9189 constructor_index
9190 = convert (bitsizetype,
9191 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9193 else
9194 constructor_index = bitsize_zero_node;
9196 constructor_unfilled_index = constructor_index;
9197 if (value && TREE_CODE (value) == STRING_CST)
9199 /* We need to split the char/wchar array into individual
9200 characters, so that we don't have to special case it
9201 everywhere. */
9202 set_nonincremental_init_from_string (value, braced_init_obstack);
9205 else
9207 if (constructor_type != error_mark_node)
9208 warning_init (input_location, 0, "braces around scalar initializer");
9209 constructor_fields = constructor_type;
9210 constructor_unfilled_fields = constructor_type;
9214 /* At the end of an implicit or explicit brace level,
9215 finish up that level of constructor. If a single expression
9216 with redundant braces initialized that level, return the
9217 c_expr structure for that expression. Otherwise, the original_code
9218 element is set to ERROR_MARK.
9219 If we were outputting the elements as they are read, return 0 as the value
9220 from inner levels (process_init_element ignores that),
9221 but return error_mark_node as the value from the outermost level
9222 (that's what we want to put in DECL_INITIAL).
9223 Otherwise, return a CONSTRUCTOR expression as the value. */
9225 struct c_expr
9226 pop_init_level (location_t loc, int implicit,
9227 struct obstack *braced_init_obstack,
9228 location_t insert_before)
9230 struct constructor_stack *p;
9231 struct c_expr ret;
9232 ret.value = NULL_TREE;
9233 ret.original_code = ERROR_MARK;
9234 ret.original_type = NULL;
9235 ret.m_decimal = 0;
9237 if (implicit == 0)
9239 /* When we come to an explicit close brace,
9240 pop any inner levels that didn't have explicit braces. */
9241 while (constructor_stack->implicit)
9242 process_init_element (input_location,
9243 pop_init_level (loc, 1, braced_init_obstack,
9244 insert_before),
9245 true, braced_init_obstack);
9246 gcc_assert (!constructor_range_stack);
9248 else
9249 if (initializer_stack->missing_brace_richloc)
9250 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9251 (insert_before, "}");
9253 /* Now output all pending elements. */
9254 constructor_incremental = 1;
9255 output_pending_init_elements (1, braced_init_obstack);
9257 p = constructor_stack;
9259 /* Error for initializing a flexible array member, or a zero-length
9260 array member in an inappropriate context. */
9261 if (constructor_type && constructor_fields
9262 && TREE_CODE (constructor_type) == ARRAY_TYPE
9263 && TYPE_DOMAIN (constructor_type)
9264 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
9266 /* Silently discard empty initializations. The parser will
9267 already have pedwarned for empty brackets. */
9268 if (integer_zerop (constructor_unfilled_index))
9269 constructor_type = NULL_TREE;
9270 else
9272 gcc_assert (!TYPE_SIZE (constructor_type));
9274 if (constructor_depth > 2)
9275 error_init (loc, "initialization of flexible array member in a nested context");
9276 else
9277 pedwarn_init (loc, OPT_Wpedantic,
9278 "initialization of a flexible array member");
9280 /* We have already issued an error message for the existence
9281 of a flexible array member not at the end of the structure.
9282 Discard the initializer so that we do not die later. */
9283 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
9284 constructor_type = NULL_TREE;
9288 switch (vec_safe_length (constructor_elements))
9290 case 0:
9291 /* Initialization with { } counts as zeroinit. */
9292 constructor_zeroinit = 1;
9293 break;
9294 case 1:
9295 /* This might be zeroinit as well. */
9296 if (integer_zerop ((*constructor_elements)[0].value))
9297 constructor_zeroinit = 1;
9298 break;
9299 default:
9300 /* If the constructor has more than one element, it can't be { 0 }. */
9301 constructor_zeroinit = 0;
9302 break;
9305 /* Warn when some structs are initialized with direct aggregation. */
9306 if (!implicit && found_missing_braces && warn_missing_braces
9307 && !constructor_zeroinit)
9309 gcc_assert (initializer_stack->missing_brace_richloc);
9310 warning_at (initializer_stack->missing_brace_richloc,
9311 OPT_Wmissing_braces,
9312 "missing braces around initializer");
9315 /* Warn when some struct elements are implicitly initialized to zero. */
9316 if (warn_missing_field_initializers
9317 && constructor_type
9318 && TREE_CODE (constructor_type) == RECORD_TYPE
9319 && constructor_unfilled_fields)
9321 /* Do not warn for flexible array members or zero-length arrays. */
9322 while (constructor_unfilled_fields
9323 && (!DECL_SIZE (constructor_unfilled_fields)
9324 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
9325 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
9327 if (constructor_unfilled_fields
9328 /* Do not warn if this level of the initializer uses member
9329 designators; it is likely to be deliberate. */
9330 && !constructor_designated
9331 /* Do not warn about initializing with { 0 } or with { }. */
9332 && !constructor_zeroinit)
9334 if (warning_at (input_location, OPT_Wmissing_field_initializers,
9335 "missing initializer for field %qD of %qT",
9336 constructor_unfilled_fields,
9337 constructor_type))
9338 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9339 "%qD declared here", constructor_unfilled_fields);
9343 /* Pad out the end of the structure. */
9344 if (p->replacement_value.value)
9345 /* If this closes a superfluous brace pair,
9346 just pass out the element between them. */
9347 ret = p->replacement_value;
9348 else if (constructor_type == NULL_TREE)
9350 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9351 && TREE_CODE (constructor_type) != ARRAY_TYPE
9352 && !gnu_vector_type_p (constructor_type))
9354 /* A nonincremental scalar initializer--just return
9355 the element, after verifying there is just one.
9356 Empty scalar initializers are supported in C2X. */
9357 if (vec_safe_is_empty (constructor_elements))
9359 if (constructor_erroneous || constructor_type == error_mark_node)
9360 ret.value = error_mark_node;
9361 else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
9363 error_init (loc, "invalid initializer");
9364 ret.value = error_mark_node;
9366 else if (TREE_CODE (constructor_type) == POINTER_TYPE)
9367 /* Ensure this is a null pointer constant in the case of a
9368 'constexpr' object initialized with {}. */
9369 ret.value = build_zero_cst (ptr_type_node);
9370 else
9371 ret.value = build_zero_cst (constructor_type);
9373 else if (vec_safe_length (constructor_elements) != 1)
9375 error_init (loc, "extra elements in scalar initializer");
9376 ret.value = (*constructor_elements)[0].value;
9378 else
9379 ret.value = (*constructor_elements)[0].value;
9381 else
9383 if (constructor_erroneous)
9384 ret.value = error_mark_node;
9385 else
9387 ret.value = build_constructor (constructor_type,
9388 constructor_elements);
9389 if (constructor_constant)
9390 TREE_CONSTANT (ret.value) = 1;
9391 if (constructor_constant && constructor_simple)
9392 TREE_STATIC (ret.value) = 1;
9393 if (constructor_nonconst)
9394 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9398 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9400 if (constructor_nonconst)
9401 ret.original_code = C_MAYBE_CONST_EXPR;
9402 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9403 ret.original_code = ERROR_MARK;
9406 constructor_type = p->type;
9407 constructor_fields = p->fields;
9408 constructor_index = p->index;
9409 constructor_max_index = p->max_index;
9410 constructor_unfilled_index = p->unfilled_index;
9411 constructor_unfilled_fields = p->unfilled_fields;
9412 constructor_bit_index = p->bit_index;
9413 constructor_elements = p->elements;
9414 constructor_constant = p->constant;
9415 constructor_simple = p->simple;
9416 constructor_nonconst = p->nonconst;
9417 constructor_erroneous = p->erroneous;
9418 constructor_incremental = p->incremental;
9419 constructor_designated = p->designated;
9420 designator_depth = p->designator_depth;
9421 constructor_pending_elts = p->pending_elts;
9422 constructor_depth = p->depth;
9423 if (!p->implicit)
9424 constructor_range_stack = p->range_stack;
9425 RESTORE_SPELLING_DEPTH (constructor_depth);
9427 constructor_stack = p->next;
9428 XDELETE (p);
9430 if (ret.value == NULL_TREE && constructor_stack == 0)
9431 ret.value = error_mark_node;
9432 return ret;
9435 /* Common handling for both array range and field name designators.
9436 ARRAY argument is nonzero for array ranges. Returns false for success. */
9438 static bool
9439 set_designator (location_t loc, bool array,
9440 struct obstack *braced_init_obstack)
9442 tree subtype;
9443 enum tree_code subcode;
9445 /* Don't die if an entire brace-pair level is superfluous
9446 in the containing level, or for an erroneous type. */
9447 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9448 return true;
9450 /* If there were errors in this designator list already, bail out
9451 silently. */
9452 if (designator_erroneous)
9453 return true;
9455 /* Likewise for an initializer for a variable-size type. Those are
9456 diagnosed in the parser, except for empty initializer braces. */
9457 if (COMPLETE_TYPE_P (constructor_type)
9458 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9459 return true;
9461 if (!designator_depth)
9463 gcc_assert (!constructor_range_stack);
9465 /* Designator list starts at the level of closest explicit
9466 braces. */
9467 while (constructor_stack->implicit)
9468 process_init_element (input_location,
9469 pop_init_level (loc, 1, braced_init_obstack,
9470 last_init_list_comma),
9471 true, braced_init_obstack);
9472 constructor_designated = 1;
9473 return false;
9476 switch (TREE_CODE (constructor_type))
9478 case RECORD_TYPE:
9479 case UNION_TYPE:
9480 subtype = TREE_TYPE (constructor_fields);
9481 if (subtype != error_mark_node)
9482 subtype = TYPE_MAIN_VARIANT (subtype);
9483 break;
9484 case ARRAY_TYPE:
9485 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9486 break;
9487 default:
9488 gcc_unreachable ();
9491 subcode = TREE_CODE (subtype);
9492 if (array && subcode != ARRAY_TYPE)
9494 error_init (loc, "array index in non-array initializer");
9495 return true;
9497 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9499 error_init (loc, "field name not in record or union initializer");
9500 return true;
9503 constructor_designated = 1;
9504 finish_implicit_inits (loc, braced_init_obstack);
9505 push_init_level (loc, 2, braced_init_obstack);
9506 return false;
9509 /* If there are range designators in designator list, push a new designator
9510 to constructor_range_stack. RANGE_END is end of such stack range or
9511 NULL_TREE if there is no range designator at this level. */
9513 static void
9514 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9516 struct constructor_range_stack *p;
9518 p = (struct constructor_range_stack *)
9519 obstack_alloc (braced_init_obstack,
9520 sizeof (struct constructor_range_stack));
9521 p->prev = constructor_range_stack;
9522 p->next = 0;
9523 p->fields = constructor_fields;
9524 p->range_start = constructor_index;
9525 p->index = constructor_index;
9526 p->stack = constructor_stack;
9527 p->range_end = range_end;
9528 if (constructor_range_stack)
9529 constructor_range_stack->next = p;
9530 constructor_range_stack = p;
9533 /* Within an array initializer, specify the next index to be initialized.
9534 FIRST is that index. If LAST is nonzero, then initialize a range
9535 of indices, running from FIRST through LAST. */
9537 void
9538 set_init_index (location_t loc, tree first, tree last,
9539 struct obstack *braced_init_obstack)
9541 if (set_designator (loc, true, braced_init_obstack))
9542 return;
9544 designator_erroneous = 1;
9546 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9547 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9549 error_init (loc, "array index in initializer not of integer type");
9550 return;
9553 if (TREE_CODE (first) != INTEGER_CST)
9555 first = c_fully_fold (first, false, NULL);
9556 if (TREE_CODE (first) == INTEGER_CST)
9557 pedwarn_init (loc, OPT_Wpedantic,
9558 "array index in initializer is not "
9559 "an integer constant expression");
9562 if (last && TREE_CODE (last) != INTEGER_CST)
9564 last = c_fully_fold (last, false, NULL);
9565 if (TREE_CODE (last) == INTEGER_CST)
9566 pedwarn_init (loc, OPT_Wpedantic,
9567 "array index in initializer is not "
9568 "an integer constant expression");
9571 if (TREE_CODE (first) != INTEGER_CST)
9572 error_init (loc, "nonconstant array index in initializer");
9573 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9574 error_init (loc, "nonconstant array index in initializer");
9575 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9576 error_init (loc, "array index in non-array initializer");
9577 else if (tree_int_cst_sgn (first) == -1)
9578 error_init (loc, "array index in initializer exceeds array bounds");
9579 else if (constructor_max_index
9580 && tree_int_cst_lt (constructor_max_index, first))
9581 error_init (loc, "array index in initializer exceeds array bounds");
9582 else
9584 constant_expression_warning (first);
9585 if (last)
9586 constant_expression_warning (last);
9587 constructor_index = convert (bitsizetype, first);
9588 if (tree_int_cst_lt (constructor_index, first))
9590 constructor_index = copy_node (constructor_index);
9591 TREE_OVERFLOW (constructor_index) = 1;
9594 if (last)
9596 if (tree_int_cst_equal (first, last))
9597 last = NULL_TREE;
9598 else if (tree_int_cst_lt (last, first))
9600 error_init (loc, "empty index range in initializer");
9601 last = NULL_TREE;
9603 else
9605 last = convert (bitsizetype, last);
9606 if (constructor_max_index != NULL_TREE
9607 && tree_int_cst_lt (constructor_max_index, last))
9609 error_init (loc, "array index range in initializer exceeds "
9610 "array bounds");
9611 last = NULL_TREE;
9616 designator_depth++;
9617 designator_erroneous = 0;
9618 if (constructor_range_stack || last)
9619 push_range_stack (last, braced_init_obstack);
9623 /* Within a struct initializer, specify the next field to be initialized. */
9625 void
9626 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9627 struct obstack *braced_init_obstack)
9629 tree field;
9631 if (set_designator (loc, false, braced_init_obstack))
9632 return;
9634 designator_erroneous = 1;
9636 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9638 error_init (loc, "field name not in record or union initializer");
9639 return;
9642 field = lookup_field (constructor_type, fieldname);
9644 if (field == NULL_TREE)
9646 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9647 if (guessed_id)
9649 gcc_rich_location rich_loc (fieldname_loc);
9650 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9651 error_at (&rich_loc,
9652 "%qT has no member named %qE; did you mean %qE?",
9653 constructor_type, fieldname, guessed_id);
9655 else
9656 error_at (fieldname_loc, "%qT has no member named %qE",
9657 constructor_type, fieldname);
9659 else
9662 constructor_fields = TREE_VALUE (field);
9663 designator_depth++;
9664 designator_erroneous = 0;
9665 if (constructor_range_stack)
9666 push_range_stack (NULL_TREE, braced_init_obstack);
9667 field = TREE_CHAIN (field);
9668 if (field)
9670 if (set_designator (loc, false, braced_init_obstack))
9671 return;
9674 while (field != NULL_TREE);
9677 /* Add a new initializer to the tree of pending initializers. PURPOSE
9678 identifies the initializer, either array index or field in a structure.
9679 VALUE is the value of that index or field. If ORIGTYPE is not
9680 NULL_TREE, it is the original type of VALUE.
9682 IMPLICIT is true if value comes from pop_init_level (1),
9683 the new initializer has been merged with the existing one
9684 and thus no warnings should be emitted about overriding an
9685 existing initializer. */
9687 static void
9688 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9689 bool implicit, struct obstack *braced_init_obstack)
9691 struct init_node *p, **q, *r;
9693 q = &constructor_pending_elts;
9694 p = 0;
9696 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9698 while (*q != 0)
9700 p = *q;
9701 if (tree_int_cst_lt (purpose, p->purpose))
9702 q = &p->left;
9703 else if (tree_int_cst_lt (p->purpose, purpose))
9704 q = &p->right;
9705 else
9707 if (!implicit)
9709 if (TREE_SIDE_EFFECTS (p->value))
9710 warning_init (loc, OPT_Woverride_init_side_effects,
9711 "initialized field with side-effects "
9712 "overwritten");
9713 else if (warn_override_init)
9714 warning_init (loc, OPT_Woverride_init,
9715 "initialized field overwritten");
9717 p->value = value;
9718 p->origtype = origtype;
9719 return;
9723 else
9725 tree bitpos;
9727 bitpos = bit_position (purpose);
9728 while (*q != NULL)
9730 p = *q;
9731 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9732 q = &p->left;
9733 else if (p->purpose != purpose)
9734 q = &p->right;
9735 else
9737 if (!implicit)
9739 if (TREE_SIDE_EFFECTS (p->value))
9740 warning_init (loc, OPT_Woverride_init_side_effects,
9741 "initialized field with side-effects "
9742 "overwritten");
9743 else if (warn_override_init)
9744 warning_init (loc, OPT_Woverride_init,
9745 "initialized field overwritten");
9747 p->value = value;
9748 p->origtype = origtype;
9749 return;
9754 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9755 sizeof (struct init_node));
9756 r->purpose = purpose;
9757 r->value = value;
9758 r->origtype = origtype;
9760 *q = r;
9761 r->parent = p;
9762 r->left = 0;
9763 r->right = 0;
9764 r->balance = 0;
9766 while (p)
9768 struct init_node *s;
9770 if (r == p->left)
9772 if (p->balance == 0)
9773 p->balance = -1;
9774 else if (p->balance < 0)
9776 if (r->balance < 0)
9778 /* L rotation. */
9779 p->left = r->right;
9780 if (p->left)
9781 p->left->parent = p;
9782 r->right = p;
9784 p->balance = 0;
9785 r->balance = 0;
9787 s = p->parent;
9788 p->parent = r;
9789 r->parent = s;
9790 if (s)
9792 if (s->left == p)
9793 s->left = r;
9794 else
9795 s->right = r;
9797 else
9798 constructor_pending_elts = r;
9800 else
9802 /* LR rotation. */
9803 struct init_node *t = r->right;
9805 r->right = t->left;
9806 if (r->right)
9807 r->right->parent = r;
9808 t->left = r;
9810 p->left = t->right;
9811 if (p->left)
9812 p->left->parent = p;
9813 t->right = p;
9815 p->balance = t->balance < 0;
9816 r->balance = -(t->balance > 0);
9817 t->balance = 0;
9819 s = p->parent;
9820 p->parent = t;
9821 r->parent = t;
9822 t->parent = s;
9823 if (s)
9825 if (s->left == p)
9826 s->left = t;
9827 else
9828 s->right = t;
9830 else
9831 constructor_pending_elts = t;
9833 break;
9835 else
9837 /* p->balance == +1; growth of left side balances the node. */
9838 p->balance = 0;
9839 break;
9842 else /* r == p->right */
9844 if (p->balance == 0)
9845 /* Growth propagation from right side. */
9846 p->balance++;
9847 else if (p->balance > 0)
9849 if (r->balance > 0)
9851 /* R rotation. */
9852 p->right = r->left;
9853 if (p->right)
9854 p->right->parent = p;
9855 r->left = p;
9857 p->balance = 0;
9858 r->balance = 0;
9860 s = p->parent;
9861 p->parent = r;
9862 r->parent = s;
9863 if (s)
9865 if (s->left == p)
9866 s->left = r;
9867 else
9868 s->right = r;
9870 else
9871 constructor_pending_elts = r;
9873 else /* r->balance == -1 */
9875 /* RL rotation */
9876 struct init_node *t = r->left;
9878 r->left = t->right;
9879 if (r->left)
9880 r->left->parent = r;
9881 t->right = r;
9883 p->right = t->left;
9884 if (p->right)
9885 p->right->parent = p;
9886 t->left = p;
9888 r->balance = (t->balance < 0);
9889 p->balance = -(t->balance > 0);
9890 t->balance = 0;
9892 s = p->parent;
9893 p->parent = t;
9894 r->parent = t;
9895 t->parent = s;
9896 if (s)
9898 if (s->left == p)
9899 s->left = t;
9900 else
9901 s->right = t;
9903 else
9904 constructor_pending_elts = t;
9906 break;
9908 else
9910 /* p->balance == -1; growth of right side balances the node. */
9911 p->balance = 0;
9912 break;
9916 r = p;
9917 p = p->parent;
9921 /* Build AVL tree from a sorted chain. */
9923 static void
9924 set_nonincremental_init (struct obstack * braced_init_obstack)
9926 unsigned HOST_WIDE_INT ix;
9927 tree index, value;
9929 if (TREE_CODE (constructor_type) != RECORD_TYPE
9930 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9931 return;
9933 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9934 add_pending_init (input_location, index, value, NULL_TREE, true,
9935 braced_init_obstack);
9936 constructor_elements = NULL;
9937 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9939 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9940 /* Skip any nameless bit fields at the beginning. */
9941 while (constructor_unfilled_fields != NULL_TREE
9942 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9943 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9946 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9948 if (TYPE_DOMAIN (constructor_type))
9949 constructor_unfilled_index
9950 = convert (bitsizetype,
9951 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9952 else
9953 constructor_unfilled_index = bitsize_zero_node;
9955 constructor_incremental = 0;
9958 /* Build AVL tree from a string constant. */
9960 static void
9961 set_nonincremental_init_from_string (tree str,
9962 struct obstack * braced_init_obstack)
9964 tree value, purpose, type;
9965 HOST_WIDE_INT val[2];
9966 const char *p, *end;
9967 int byte, wchar_bytes, charwidth, bitpos;
9969 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9971 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9972 charwidth = TYPE_PRECISION (char_type_node);
9973 gcc_assert ((size_t) wchar_bytes * charwidth
9974 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9975 type = TREE_TYPE (constructor_type);
9976 p = TREE_STRING_POINTER (str);
9977 end = p + TREE_STRING_LENGTH (str);
9979 for (purpose = bitsize_zero_node;
9980 p < end
9981 && !(constructor_max_index
9982 && tree_int_cst_lt (constructor_max_index, purpose));
9983 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9985 if (wchar_bytes == 1)
9987 val[0] = (unsigned char) *p++;
9988 val[1] = 0;
9990 else
9992 val[1] = 0;
9993 val[0] = 0;
9994 for (byte = 0; byte < wchar_bytes; byte++)
9996 if (BYTES_BIG_ENDIAN)
9997 bitpos = (wchar_bytes - byte - 1) * charwidth;
9998 else
9999 bitpos = byte * charwidth;
10000 val[bitpos / HOST_BITS_PER_WIDE_INT]
10001 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
10002 << (bitpos % HOST_BITS_PER_WIDE_INT);
10006 if (!TYPE_UNSIGNED (type))
10008 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
10009 if (bitpos < HOST_BITS_PER_WIDE_INT)
10011 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
10013 val[0] |= HOST_WIDE_INT_M1U << bitpos;
10014 val[1] = -1;
10017 else if (bitpos == HOST_BITS_PER_WIDE_INT)
10019 if (val[0] < 0)
10020 val[1] = -1;
10022 else if (val[1] & (HOST_WIDE_INT_1
10023 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
10024 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
10027 value = wide_int_to_tree (type,
10028 wide_int::from_array (val, 2,
10029 HOST_BITS_PER_WIDE_INT * 2));
10030 add_pending_init (input_location, purpose, value, NULL_TREE, true,
10031 braced_init_obstack);
10034 constructor_incremental = 0;
10037 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
10038 not initialized yet. */
10040 static tree
10041 find_init_member (tree field, struct obstack * braced_init_obstack)
10043 struct init_node *p;
10045 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10047 if (constructor_incremental
10048 && tree_int_cst_lt (field, constructor_unfilled_index))
10049 set_nonincremental_init (braced_init_obstack);
10051 p = constructor_pending_elts;
10052 while (p)
10054 if (tree_int_cst_lt (field, p->purpose))
10055 p = p->left;
10056 else if (tree_int_cst_lt (p->purpose, field))
10057 p = p->right;
10058 else
10059 return p->value;
10062 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10064 tree bitpos = bit_position (field);
10066 if (constructor_incremental
10067 && (!constructor_unfilled_fields
10068 || tree_int_cst_lt (bitpos,
10069 bit_position (constructor_unfilled_fields))))
10070 set_nonincremental_init (braced_init_obstack);
10072 p = constructor_pending_elts;
10073 while (p)
10075 if (field == p->purpose)
10076 return p->value;
10077 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
10078 p = p->left;
10079 else
10080 p = p->right;
10083 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10085 if (!vec_safe_is_empty (constructor_elements)
10086 && (constructor_elements->last ().index == field))
10087 return constructor_elements->last ().value;
10089 return NULL_TREE;
10092 /* "Output" the next constructor element.
10093 At top level, really output it to assembler code now.
10094 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
10095 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
10096 TYPE is the data type that the containing data type wants here.
10097 FIELD is the field (a FIELD_DECL) or the index that this element fills.
10098 If VALUE is a string constant, STRICT_STRING is true if it is
10099 unparenthesized or we should not warn here for it being parenthesized.
10100 For other types of VALUE, STRICT_STRING is not used.
10102 PENDING if true means output pending elements that belong
10103 right after this element. (PENDING is normally true;
10104 it is false while outputting pending elements, to avoid recursion.)
10106 IMPLICIT is true if value comes from pop_init_level (1),
10107 the new initializer has been merged with the existing one
10108 and thus no warnings should be emitted about overriding an
10109 existing initializer. */
10111 static void
10112 output_init_element (location_t loc, tree value, tree origtype,
10113 bool strict_string, tree type, tree field, bool pending,
10114 bool implicit, struct obstack * braced_init_obstack)
10116 tree semantic_type = NULL_TREE;
10117 bool maybe_const = true;
10118 bool npc, int_const_expr, arith_const_expr;
10120 if (type == error_mark_node || value == error_mark_node)
10122 constructor_erroneous = 1;
10123 return;
10125 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
10126 && (TREE_CODE (value) == STRING_CST
10127 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
10128 && !(TREE_CODE (value) == STRING_CST
10129 && TREE_CODE (type) == ARRAY_TYPE
10130 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
10131 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
10132 TYPE_MAIN_VARIANT (type)))
10133 value = array_to_pointer_conversion (input_location, value);
10135 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
10136 && require_constant_value && pending)
10138 /* As an extension, allow initializing objects with static storage
10139 duration with compound literals (which are then treated just as
10140 the brace enclosed list they contain). */
10141 if (flag_isoc99)
10142 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
10143 "constant");
10144 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
10145 value = DECL_INITIAL (decl);
10148 npc = null_pointer_constant_p (value);
10149 int_const_expr = (TREE_CODE (value) == INTEGER_CST
10150 && !TREE_OVERFLOW (value)
10151 && INTEGRAL_TYPE_P (TREE_TYPE (value)));
10152 /* Not fully determined before folding. */
10153 arith_const_expr = true;
10154 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
10156 semantic_type = TREE_TYPE (value);
10157 value = TREE_OPERAND (value, 0);
10159 value = c_fully_fold (value, require_constant_value, &maybe_const);
10160 /* TODO: this may not detect all cases of expressions folding to
10161 constants that are not arithmetic constant expressions. */
10162 if (!maybe_const)
10163 arith_const_expr = false;
10164 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
10165 && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
10166 && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
10167 arith_const_expr = false;
10168 else if (TREE_CODE (value) != INTEGER_CST
10169 && TREE_CODE (value) != REAL_CST
10170 && TREE_CODE (value) != COMPLEX_CST)
10171 arith_const_expr = false;
10172 else if (TREE_OVERFLOW (value))
10173 arith_const_expr = false;
10175 if (value == error_mark_node)
10176 constructor_erroneous = 1;
10177 else if (!TREE_CONSTANT (value))
10178 constructor_constant = 0;
10179 else if (!initializer_constant_valid_p (value,
10180 TREE_TYPE (value),
10181 AGGREGATE_TYPE_P (constructor_type)
10182 && TYPE_REVERSE_STORAGE_ORDER
10183 (constructor_type))
10184 || (RECORD_OR_UNION_TYPE_P (constructor_type)
10185 && DECL_C_BIT_FIELD (field)
10186 && TREE_CODE (value) != INTEGER_CST))
10187 constructor_simple = 0;
10188 if (!maybe_const)
10189 constructor_nonconst = 1;
10191 /* Digest the initializer and issue any errors about incompatible
10192 types before issuing errors about non-constant initializers. */
10193 tree new_value = value;
10194 if (semantic_type)
10195 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
10196 /* In the case of braces around a scalar initializer, the result of
10197 this initializer processing goes through digest_init again at the
10198 outer level. In the case of a constexpr initializer for a
10199 pointer, avoid converting a null pointer constant to something
10200 that is not a null pointer constant to avoid a spurious error
10201 from that second processing. */
10202 if (!require_constexpr_value
10203 || !npc
10204 || TREE_CODE (constructor_type) != POINTER_TYPE)
10205 new_value = digest_init (loc, type, new_value, origtype, npc,
10206 int_const_expr, arith_const_expr, strict_string,
10207 require_constant_value, require_constexpr_value);
10208 if (new_value == error_mark_node)
10210 constructor_erroneous = 1;
10211 return;
10213 if (require_constant_value || require_constant_elements)
10214 constant_expression_warning (new_value);
10216 /* Proceed to check the constness of the original initializer. */
10217 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
10219 if (require_constant_value)
10221 error_init (loc, "initializer element is not constant");
10222 value = error_mark_node;
10224 else if (require_constant_elements)
10225 pedwarn (loc, OPT_Wpedantic,
10226 "initializer element is not computable at load time");
10228 else if (!maybe_const
10229 && (require_constant_value || require_constant_elements))
10230 pedwarn_init (loc, OPT_Wpedantic,
10231 "initializer element is not a constant expression");
10232 /* digest_init has already carried out the additional checks
10233 required for 'constexpr' initializers (using the information
10234 passed to it about whether the original initializer was certain
10235 kinds of constant expression), so that check does not need to be
10236 repeated here. */
10238 /* Issue -Wc++-compat warnings about initializing a bitfield with
10239 enum type. */
10240 if (warn_cxx_compat
10241 && field != NULL_TREE
10242 && TREE_CODE (field) == FIELD_DECL
10243 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
10244 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
10245 != TYPE_MAIN_VARIANT (type))
10246 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
10248 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
10249 if (checktype != error_mark_node
10250 && (TYPE_MAIN_VARIANT (checktype)
10251 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
10252 warning_init (loc, OPT_Wc___compat,
10253 "enum conversion in initialization is invalid in C++");
10256 /* If this field is empty and does not have side effects (and is not at
10257 the end of structure), don't do anything other than checking the
10258 initializer. */
10259 if (field
10260 && (TREE_TYPE (field) == error_mark_node
10261 || (COMPLETE_TYPE_P (TREE_TYPE (field))
10262 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
10263 && !TREE_SIDE_EFFECTS (new_value)
10264 && (TREE_CODE (constructor_type) == ARRAY_TYPE
10265 || DECL_CHAIN (field)))))
10266 return;
10268 /* Finally, set VALUE to the initializer value digested above. */
10269 value = new_value;
10271 /* If this element doesn't come next in sequence,
10272 put it on constructor_pending_elts. */
10273 if (TREE_CODE (constructor_type) == ARRAY_TYPE
10274 && (!constructor_incremental
10275 || !tree_int_cst_equal (field, constructor_unfilled_index)))
10277 if (constructor_incremental
10278 && tree_int_cst_lt (field, constructor_unfilled_index))
10279 set_nonincremental_init (braced_init_obstack);
10281 add_pending_init (loc, field, value, origtype, implicit,
10282 braced_init_obstack);
10283 return;
10285 else if (TREE_CODE (constructor_type) == RECORD_TYPE
10286 && (!constructor_incremental
10287 || field != constructor_unfilled_fields))
10289 /* We do this for records but not for unions. In a union,
10290 no matter which field is specified, it can be initialized
10291 right away since it starts at the beginning of the union. */
10292 if (constructor_incremental)
10294 if (!constructor_unfilled_fields)
10295 set_nonincremental_init (braced_init_obstack);
10296 else
10298 tree bitpos, unfillpos;
10300 bitpos = bit_position (field);
10301 unfillpos = bit_position (constructor_unfilled_fields);
10303 if (tree_int_cst_lt (bitpos, unfillpos))
10304 set_nonincremental_init (braced_init_obstack);
10308 add_pending_init (loc, field, value, origtype, implicit,
10309 braced_init_obstack);
10310 return;
10312 else if (TREE_CODE (constructor_type) == UNION_TYPE
10313 && !vec_safe_is_empty (constructor_elements))
10315 if (!implicit)
10317 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
10318 warning_init (loc, OPT_Woverride_init_side_effects,
10319 "initialized field with side-effects overwritten");
10320 else if (warn_override_init)
10321 warning_init (loc, OPT_Woverride_init,
10322 "initialized field overwritten");
10325 /* We can have just one union field set. */
10326 constructor_elements = NULL;
10329 /* Otherwise, output this element either to
10330 constructor_elements or to the assembler file. */
10332 constructor_elt celt = {field, value};
10333 vec_safe_push (constructor_elements, celt);
10335 /* Advance the variable that indicates sequential elements output. */
10336 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10337 constructor_unfilled_index
10338 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
10339 bitsize_one_node);
10340 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10342 constructor_unfilled_fields
10343 = DECL_CHAIN (constructor_unfilled_fields);
10345 /* Skip any nameless bit fields. */
10346 while (constructor_unfilled_fields != NULL_TREE
10347 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
10348 constructor_unfilled_fields =
10349 DECL_CHAIN (constructor_unfilled_fields);
10351 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10352 constructor_unfilled_fields = NULL_TREE;
10354 /* Now output any pending elements which have become next. */
10355 if (pending)
10356 output_pending_init_elements (0, braced_init_obstack);
10359 /* For two FIELD_DECLs in the same chain, return -1 if field1
10360 comes before field2, 1 if field1 comes after field2 and
10361 0 if field1 == field2. */
10363 static int
10364 init_field_decl_cmp (tree field1, tree field2)
10366 if (field1 == field2)
10367 return 0;
10369 tree bitpos1 = bit_position (field1);
10370 tree bitpos2 = bit_position (field2);
10371 if (tree_int_cst_equal (bitpos1, bitpos2))
10373 /* If one of the fields has non-zero bitsize, then that
10374 field must be the last one in a sequence of zero
10375 sized fields, fields after it will have bigger
10376 bit_position. */
10377 if (TREE_TYPE (field1) != error_mark_node
10378 && COMPLETE_TYPE_P (TREE_TYPE (field1))
10379 && integer_nonzerop (TREE_TYPE (field1)))
10380 return 1;
10381 if (TREE_TYPE (field2) != error_mark_node
10382 && COMPLETE_TYPE_P (TREE_TYPE (field2))
10383 && integer_nonzerop (TREE_TYPE (field2)))
10384 return -1;
10385 /* Otherwise, fallback to DECL_CHAIN walk to find out
10386 which field comes earlier. Walk chains of both
10387 fields, so that if field1 and field2 are close to each
10388 other in either order, it is found soon even for large
10389 sequences of zero sized fields. */
10390 tree f1 = field1, f2 = field2;
10391 while (1)
10393 f1 = DECL_CHAIN (f1);
10394 f2 = DECL_CHAIN (f2);
10395 if (f1 == NULL_TREE)
10397 gcc_assert (f2);
10398 return 1;
10400 if (f2 == NULL_TREE)
10401 return -1;
10402 if (f1 == field2)
10403 return -1;
10404 if (f2 == field1)
10405 return 1;
10406 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10407 return 1;
10408 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10409 return -1;
10412 else if (tree_int_cst_lt (bitpos1, bitpos2))
10413 return -1;
10414 else
10415 return 1;
10418 /* Output any pending elements which have become next.
10419 As we output elements, constructor_unfilled_{fields,index}
10420 advances, which may cause other elements to become next;
10421 if so, they too are output.
10423 If ALL is 0, we return when there are
10424 no more pending elements to output now.
10426 If ALL is 1, we output space as necessary so that
10427 we can output all the pending elements. */
10428 static void
10429 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10431 struct init_node *elt = constructor_pending_elts;
10432 tree next;
10434 retry:
10436 /* Look through the whole pending tree.
10437 If we find an element that should be output now,
10438 output it. Otherwise, set NEXT to the element
10439 that comes first among those still pending. */
10441 next = NULL_TREE;
10442 while (elt)
10444 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10446 if (tree_int_cst_equal (elt->purpose,
10447 constructor_unfilled_index))
10448 output_init_element (input_location, elt->value, elt->origtype,
10449 true, TREE_TYPE (constructor_type),
10450 constructor_unfilled_index, false, false,
10451 braced_init_obstack);
10452 else if (tree_int_cst_lt (constructor_unfilled_index,
10453 elt->purpose))
10455 /* Advance to the next smaller node. */
10456 if (elt->left)
10457 elt = elt->left;
10458 else
10460 /* We have reached the smallest node bigger than the
10461 current unfilled index. Fill the space first. */
10462 next = elt->purpose;
10463 break;
10466 else
10468 /* Advance to the next bigger node. */
10469 if (elt->right)
10470 elt = elt->right;
10471 else
10473 /* We have reached the biggest node in a subtree. Find
10474 the parent of it, which is the next bigger node. */
10475 while (elt->parent && elt->parent->right == elt)
10476 elt = elt->parent;
10477 elt = elt->parent;
10478 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10479 elt->purpose))
10481 next = elt->purpose;
10482 break;
10487 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10489 /* If the current record is complete we are done. */
10490 if (constructor_unfilled_fields == NULL_TREE)
10491 break;
10493 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10494 elt->purpose);
10495 if (cmp == 0)
10496 output_init_element (input_location, elt->value, elt->origtype,
10497 true, TREE_TYPE (elt->purpose),
10498 elt->purpose, false, false,
10499 braced_init_obstack);
10500 else if (cmp < 0)
10502 /* Advance to the next smaller node. */
10503 if (elt->left)
10504 elt = elt->left;
10505 else
10507 /* We have reached the smallest node bigger than the
10508 current unfilled field. Fill the space first. */
10509 next = elt->purpose;
10510 break;
10513 else
10515 /* Advance to the next bigger node. */
10516 if (elt->right)
10517 elt = elt->right;
10518 else
10520 /* We have reached the biggest node in a subtree. Find
10521 the parent of it, which is the next bigger node. */
10522 while (elt->parent && elt->parent->right == elt)
10523 elt = elt->parent;
10524 elt = elt->parent;
10525 if (elt
10526 && init_field_decl_cmp (constructor_unfilled_fields,
10527 elt->purpose) < 0)
10529 next = elt->purpose;
10530 break;
10537 /* Ordinarily return, but not if we want to output all
10538 and there are elements left. */
10539 if (!(all && next != NULL_TREE))
10540 return;
10542 /* If it's not incremental, just skip over the gap, so that after
10543 jumping to retry we will output the next successive element. */
10544 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10545 constructor_unfilled_fields = next;
10546 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10547 constructor_unfilled_index = next;
10549 /* ELT now points to the node in the pending tree with the next
10550 initializer to output. */
10551 goto retry;
10554 /* Expression VALUE coincides with the start of type TYPE in a braced
10555 initializer. Return true if we should treat VALUE as initializing
10556 the first element of TYPE, false if we should treat it as initializing
10557 TYPE as a whole.
10559 If the initializer is clearly invalid, the question becomes:
10560 which choice gives the best error message? */
10562 static bool
10563 initialize_elementwise_p (tree type, tree value)
10565 if (type == error_mark_node || value == error_mark_node)
10566 return false;
10568 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10570 tree value_type = TREE_TYPE (value);
10571 if (value_type == error_mark_node)
10572 return false;
10574 /* GNU vectors can be initialized elementwise. However, treat any
10575 kind of vector value as initializing the vector type as a whole,
10576 regardless of whether the value is a GNU vector. Such initializers
10577 are valid if and only if they would have been valid in a non-braced
10578 initializer like:
10580 TYPE foo = VALUE;
10582 so recursing into the vector type would be at best confusing or at
10583 worst wrong. For example, when -flax-vector-conversions is in effect,
10584 it's possible to initialize a V8HI from a V4SI, even though the vectors
10585 have different element types and different numbers of elements. */
10586 if (gnu_vector_type_p (type))
10587 return !VECTOR_TYPE_P (value_type);
10589 if (AGGREGATE_TYPE_P (type))
10590 return type != TYPE_MAIN_VARIANT (value_type);
10592 return false;
10595 /* Add one non-braced element to the current constructor level.
10596 This adjusts the current position within the constructor's type.
10597 This may also start or terminate implicit levels
10598 to handle a partly-braced initializer.
10600 Once this has found the correct level for the new element,
10601 it calls output_init_element.
10603 IMPLICIT is true if value comes from pop_init_level (1),
10604 the new initializer has been merged with the existing one
10605 and thus no warnings should be emitted about overriding an
10606 existing initializer. */
10608 void
10609 process_init_element (location_t loc, struct c_expr value, bool implicit,
10610 struct obstack * braced_init_obstack)
10612 tree orig_value = value.value;
10613 int string_flag
10614 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10615 bool strict_string = value.original_code == STRING_CST;
10616 bool was_designated = designator_depth != 0;
10618 designator_depth = 0;
10619 designator_erroneous = 0;
10621 if (!implicit && value.value && !integer_zerop (value.value))
10622 constructor_zeroinit = 0;
10624 /* Handle superfluous braces around string cst as in
10625 char x[] = {"foo"}; */
10626 if (constructor_type
10627 && !was_designated
10628 && TREE_CODE (constructor_type) == ARRAY_TYPE
10629 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10630 && integer_zerop (constructor_unfilled_index))
10632 if (constructor_stack->replacement_value.value)
10634 error_init (loc, "excess elements in %qT initializer", constructor_type);
10635 return;
10637 else if (string_flag)
10639 constructor_stack->replacement_value = value;
10640 return;
10644 if (constructor_stack->replacement_value.value != NULL_TREE)
10646 error_init (loc, "excess elements in struct initializer");
10647 return;
10650 /* Ignore elements of a brace group if it is entirely superfluous
10651 and has already been diagnosed, or if the type is erroneous. */
10652 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10653 return;
10655 /* Ignore elements of an initializer for a variable-size type.
10656 Those are diagnosed in the parser (empty initializer braces are OK). */
10657 if (COMPLETE_TYPE_P (constructor_type)
10658 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10659 return;
10661 if (!implicit && warn_designated_init && !was_designated
10662 && TREE_CODE (constructor_type) == RECORD_TYPE
10663 && lookup_attribute ("designated_init",
10664 TYPE_ATTRIBUTES (constructor_type)))
10665 warning_init (loc,
10666 OPT_Wdesignated_init,
10667 "positional initialization of field "
10668 "in %<struct%> declared with %<designated_init%> attribute");
10670 /* If we've exhausted any levels that didn't have braces,
10671 pop them now. */
10672 while (constructor_stack->implicit)
10674 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10675 && constructor_fields == NULL_TREE)
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 if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10681 || gnu_vector_type_p (constructor_type))
10682 && constructor_max_index
10683 && tree_int_cst_lt (constructor_max_index,
10684 constructor_index))
10685 process_init_element (loc,
10686 pop_init_level (loc, 1, braced_init_obstack,
10687 last_init_list_comma),
10688 true, braced_init_obstack);
10689 else
10690 break;
10693 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10694 if (constructor_range_stack)
10696 /* If value is a compound literal and we'll be just using its
10697 content, don't put it into a SAVE_EXPR. */
10698 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10699 || !require_constant_value)
10701 tree semantic_type = NULL_TREE;
10702 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10704 semantic_type = TREE_TYPE (value.value);
10705 value.value = TREE_OPERAND (value.value, 0);
10707 value.value = save_expr (value.value);
10708 if (semantic_type)
10709 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10710 value.value);
10714 while (1)
10716 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10718 tree fieldtype;
10719 enum tree_code fieldcode;
10721 if (constructor_fields == NULL_TREE)
10723 pedwarn_init (loc, 0, "excess elements in struct initializer");
10724 break;
10727 fieldtype = TREE_TYPE (constructor_fields);
10728 if (fieldtype != error_mark_node)
10729 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10730 fieldcode = TREE_CODE (fieldtype);
10732 /* Error for non-static initialization of a flexible array member. */
10733 if (fieldcode == ARRAY_TYPE
10734 && !require_constant_value
10735 && TYPE_SIZE (fieldtype) == NULL_TREE
10736 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10738 error_init (loc, "non-static initialization of a flexible "
10739 "array member");
10740 break;
10743 /* Error for initialization of a flexible array member with
10744 a string constant if the structure is in an array. E.g.:
10745 struct S { int x; char y[]; };
10746 struct S s[] = { { 1, "foo" } };
10747 is invalid. */
10748 if (string_flag
10749 && fieldcode == ARRAY_TYPE
10750 && constructor_depth > 1
10751 && TYPE_SIZE (fieldtype) == NULL_TREE
10752 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10754 bool in_array_p = false;
10755 for (struct constructor_stack *p = constructor_stack;
10756 p && p->type; p = p->next)
10757 if (TREE_CODE (p->type) == ARRAY_TYPE)
10759 in_array_p = true;
10760 break;
10762 if (in_array_p)
10764 error_init (loc, "initialization of flexible array "
10765 "member in a nested context");
10766 break;
10770 /* Accept a string constant to initialize a subarray. */
10771 if (value.value != NULL_TREE
10772 && fieldcode == ARRAY_TYPE
10773 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10774 && string_flag)
10775 value.value = orig_value;
10776 /* Otherwise, if we have come to a subaggregate,
10777 and we don't have an element of its type, push into it. */
10778 else if (value.value != NULL_TREE
10779 && initialize_elementwise_p (fieldtype, value.value))
10781 push_init_level (loc, 1, braced_init_obstack);
10782 continue;
10785 if (value.value)
10787 push_member_name (constructor_fields);
10788 output_init_element (loc, value.value, value.original_type,
10789 strict_string, fieldtype,
10790 constructor_fields, true, implicit,
10791 braced_init_obstack);
10792 RESTORE_SPELLING_DEPTH (constructor_depth);
10794 else
10795 /* Do the bookkeeping for an element that was
10796 directly output as a constructor. */
10798 /* For a record, keep track of end position of last field. */
10799 if (DECL_SIZE (constructor_fields))
10800 constructor_bit_index
10801 = size_binop_loc (input_location, PLUS_EXPR,
10802 bit_position (constructor_fields),
10803 DECL_SIZE (constructor_fields));
10805 /* If the current field was the first one not yet written out,
10806 it isn't now, so update. */
10807 if (constructor_unfilled_fields == constructor_fields)
10809 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10810 /* Skip any nameless bit fields. */
10811 while (constructor_unfilled_fields != 0
10812 && (DECL_UNNAMED_BIT_FIELD
10813 (constructor_unfilled_fields)))
10814 constructor_unfilled_fields =
10815 DECL_CHAIN (constructor_unfilled_fields);
10819 constructor_fields = DECL_CHAIN (constructor_fields);
10820 /* Skip any nameless bit fields at the beginning. */
10821 while (constructor_fields != NULL_TREE
10822 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10823 constructor_fields = DECL_CHAIN (constructor_fields);
10825 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10827 tree fieldtype;
10828 enum tree_code fieldcode;
10830 if (constructor_fields == NULL_TREE)
10832 pedwarn_init (loc, 0,
10833 "excess elements in union initializer");
10834 break;
10837 fieldtype = TREE_TYPE (constructor_fields);
10838 if (fieldtype != error_mark_node)
10839 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10840 fieldcode = TREE_CODE (fieldtype);
10842 /* Warn that traditional C rejects initialization of unions.
10843 We skip the warning if the value is zero. This is done
10844 under the assumption that the zero initializer in user
10845 code appears conditioned on e.g. __STDC__ to avoid
10846 "missing initializer" warnings and relies on default
10847 initialization to zero in the traditional C case.
10848 We also skip the warning if the initializer is designated,
10849 again on the assumption that this must be conditional on
10850 __STDC__ anyway (and we've already complained about the
10851 member-designator already). */
10852 if (!in_system_header_at (input_location) && !constructor_designated
10853 && !(value.value && (integer_zerop (value.value)
10854 || real_zerop (value.value))))
10855 warning (OPT_Wtraditional, "traditional C rejects initialization "
10856 "of unions");
10858 /* Accept a string constant to initialize a subarray. */
10859 if (value.value != NULL_TREE
10860 && fieldcode == ARRAY_TYPE
10861 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10862 && string_flag)
10863 value.value = orig_value;
10864 /* Otherwise, if we have come to a subaggregate,
10865 and we don't have an element of its type, push into it. */
10866 else if (value.value != NULL_TREE
10867 && initialize_elementwise_p (fieldtype, value.value))
10869 push_init_level (loc, 1, braced_init_obstack);
10870 continue;
10873 if (value.value)
10875 push_member_name (constructor_fields);
10876 output_init_element (loc, value.value, value.original_type,
10877 strict_string, fieldtype,
10878 constructor_fields, true, implicit,
10879 braced_init_obstack);
10880 RESTORE_SPELLING_DEPTH (constructor_depth);
10882 else
10883 /* Do the bookkeeping for an element that was
10884 directly output as a constructor. */
10886 constructor_bit_index = DECL_SIZE (constructor_fields);
10887 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10890 constructor_fields = NULL_TREE;
10892 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10894 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10895 enum tree_code eltcode = TREE_CODE (elttype);
10897 /* Accept a string constant to initialize a subarray. */
10898 if (value.value != NULL_TREE
10899 && eltcode == ARRAY_TYPE
10900 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10901 && string_flag)
10902 value.value = orig_value;
10903 /* Otherwise, if we have come to a subaggregate,
10904 and we don't have an element of its type, push into it. */
10905 else if (value.value != NULL_TREE
10906 && initialize_elementwise_p (elttype, value.value))
10908 push_init_level (loc, 1, braced_init_obstack);
10909 continue;
10912 if (constructor_max_index != NULL_TREE
10913 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10914 || integer_all_onesp (constructor_max_index)))
10916 pedwarn_init (loc, 0,
10917 "excess elements in array initializer");
10918 break;
10921 /* Now output the actual element. */
10922 if (value.value)
10924 push_array_bounds (tree_to_uhwi (constructor_index));
10925 output_init_element (loc, value.value, value.original_type,
10926 strict_string, elttype,
10927 constructor_index, true, implicit,
10928 braced_init_obstack);
10929 RESTORE_SPELLING_DEPTH (constructor_depth);
10932 constructor_index
10933 = size_binop_loc (input_location, PLUS_EXPR,
10934 constructor_index, bitsize_one_node);
10936 if (!value.value)
10937 /* If we are doing the bookkeeping for an element that was
10938 directly output as a constructor, we must update
10939 constructor_unfilled_index. */
10940 constructor_unfilled_index = constructor_index;
10942 else if (gnu_vector_type_p (constructor_type))
10944 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10946 /* Do a basic check of initializer size. Note that vectors
10947 always have a fixed size derived from their type. */
10948 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10950 pedwarn_init (loc, 0,
10951 "excess elements in vector initializer");
10952 break;
10955 /* Now output the actual element. */
10956 if (value.value)
10958 if (TREE_CODE (value.value) == VECTOR_CST)
10959 elttype = TYPE_MAIN_VARIANT (constructor_type);
10960 output_init_element (loc, value.value, value.original_type,
10961 strict_string, elttype,
10962 constructor_index, true, implicit,
10963 braced_init_obstack);
10966 constructor_index
10967 = size_binop_loc (input_location,
10968 PLUS_EXPR, constructor_index, bitsize_one_node);
10970 if (!value.value)
10971 /* If we are doing the bookkeeping for an element that was
10972 directly output as a constructor, we must update
10973 constructor_unfilled_index. */
10974 constructor_unfilled_index = constructor_index;
10977 /* Handle the sole element allowed in a braced initializer
10978 for a scalar variable. */
10979 else if (constructor_type != error_mark_node
10980 && constructor_fields == NULL_TREE)
10982 pedwarn_init (loc, 0,
10983 "excess elements in scalar initializer");
10984 break;
10986 else
10988 if (value.value)
10989 output_init_element (loc, value.value, value.original_type,
10990 strict_string, constructor_type,
10991 NULL_TREE, true, implicit,
10992 braced_init_obstack);
10993 constructor_fields = NULL_TREE;
10996 /* Handle range initializers either at this level or anywhere higher
10997 in the designator stack. */
10998 if (constructor_range_stack)
11000 struct constructor_range_stack *p, *range_stack;
11001 int finish = 0;
11003 range_stack = constructor_range_stack;
11004 constructor_range_stack = 0;
11005 while (constructor_stack != range_stack->stack)
11007 gcc_assert (constructor_stack->implicit);
11008 process_init_element (loc,
11009 pop_init_level (loc, 1,
11010 braced_init_obstack,
11011 last_init_list_comma),
11012 true, braced_init_obstack);
11014 for (p = range_stack;
11015 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
11016 p = p->prev)
11018 gcc_assert (constructor_stack->implicit);
11019 process_init_element (loc,
11020 pop_init_level (loc, 1,
11021 braced_init_obstack,
11022 last_init_list_comma),
11023 true, braced_init_obstack);
11026 p->index = size_binop_loc (input_location,
11027 PLUS_EXPR, p->index, bitsize_one_node);
11028 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
11029 finish = 1;
11031 while (1)
11033 constructor_index = p->index;
11034 constructor_fields = p->fields;
11035 if (finish && p->range_end && p->index == p->range_start)
11037 finish = 0;
11038 p->prev = 0;
11040 p = p->next;
11041 if (!p)
11042 break;
11043 finish_implicit_inits (loc, braced_init_obstack);
11044 push_init_level (loc, 2, braced_init_obstack);
11045 p->stack = constructor_stack;
11046 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
11047 p->index = p->range_start;
11050 if (!finish)
11051 constructor_range_stack = range_stack;
11052 continue;
11055 break;
11058 constructor_range_stack = 0;
11061 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
11062 (guaranteed to be 'volatile' or null) and ARGS (represented using
11063 an ASM_EXPR node). */
11064 tree
11065 build_asm_stmt (bool is_volatile, tree args)
11067 if (is_volatile)
11068 ASM_VOLATILE_P (args) = 1;
11069 return add_stmt (args);
11072 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
11073 some INPUTS, and some CLOBBERS. The latter three may be NULL.
11074 SIMPLE indicates whether there was anything at all after the
11075 string in the asm expression -- asm("blah") and asm("blah" : )
11076 are subtly different. We use a ASM_EXPR node to represent this.
11077 LOC is the location of the asm, and IS_INLINE says whether this
11078 is asm inline. */
11079 tree
11080 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
11081 tree clobbers, tree labels, bool simple, bool is_inline)
11083 tree tail;
11084 tree args;
11085 int i;
11086 const char *constraint;
11087 const char **oconstraints;
11088 bool allows_mem, allows_reg, is_inout;
11089 int ninputs, noutputs;
11091 ninputs = list_length (inputs);
11092 noutputs = list_length (outputs);
11093 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
11095 string = resolve_asm_operand_names (string, outputs, inputs, labels);
11097 /* Remove output conversions that change the type but not the mode. */
11098 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
11100 tree output = TREE_VALUE (tail);
11102 output = c_fully_fold (output, false, NULL, true);
11104 /* ??? Really, this should not be here. Users should be using a
11105 proper lvalue, dammit. But there's a long history of using casts
11106 in the output operands. In cases like longlong.h, this becomes a
11107 primitive form of typechecking -- if the cast can be removed, then
11108 the output operand had a type of the proper width; otherwise we'll
11109 get an error. Gross, but ... */
11110 STRIP_NOPS (output);
11112 if (!lvalue_or_else (loc, output, lv_asm))
11113 output = error_mark_node;
11115 if (output != error_mark_node
11116 && (TREE_READONLY (output)
11117 || TYPE_READONLY (TREE_TYPE (output))
11118 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
11119 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
11120 readonly_error (loc, output, lv_asm);
11122 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11123 oconstraints[i] = constraint;
11125 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
11126 &allows_mem, &allows_reg, &is_inout))
11128 /* If the operand is going to end up in memory,
11129 mark it addressable. */
11130 if (!allows_reg && !c_mark_addressable (output))
11131 output = error_mark_node;
11132 if (!(!allows_reg && allows_mem)
11133 && output != error_mark_node
11134 && VOID_TYPE_P (TREE_TYPE (output)))
11136 error_at (loc, "invalid use of void expression");
11137 output = error_mark_node;
11140 else
11141 output = error_mark_node;
11143 TREE_VALUE (tail) = output;
11146 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
11148 tree input;
11150 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11151 input = TREE_VALUE (tail);
11153 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
11154 oconstraints, &allows_mem, &allows_reg))
11156 /* If the operand is going to end up in memory,
11157 mark it addressable. */
11158 if (!allows_reg && allows_mem)
11160 input = c_fully_fold (input, false, NULL, true);
11162 /* Strip the nops as we allow this case. FIXME, this really
11163 should be rejected or made deprecated. */
11164 STRIP_NOPS (input);
11165 if (!c_mark_addressable (input))
11166 input = error_mark_node;
11168 else
11170 struct c_expr expr;
11171 memset (&expr, 0, sizeof (expr));
11172 expr.value = input;
11173 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
11174 input = c_fully_fold (expr.value, false, NULL);
11176 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
11178 error_at (loc, "invalid use of void expression");
11179 input = error_mark_node;
11183 else
11184 input = error_mark_node;
11186 TREE_VALUE (tail) = input;
11189 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
11191 /* asm statements without outputs, including simple ones, are treated
11192 as volatile. */
11193 ASM_INPUT_P (args) = simple;
11194 ASM_VOLATILE_P (args) = (noutputs == 0);
11195 ASM_INLINE_P (args) = is_inline;
11197 return args;
11200 /* Generate a goto statement to LABEL. LOC is the location of the
11201 GOTO. */
11203 tree
11204 c_finish_goto_label (location_t loc, tree label)
11206 tree decl = lookup_label_for_goto (loc, label);
11207 if (!decl)
11208 return NULL_TREE;
11209 TREE_USED (decl) = 1;
11211 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
11212 tree t = build1 (GOTO_EXPR, void_type_node, decl);
11213 SET_EXPR_LOCATION (t, loc);
11214 return add_stmt (t);
11218 /* Generate a computed goto statement to EXPR. LOC is the location of
11219 the GOTO. */
11221 tree
11222 c_finish_goto_ptr (location_t loc, c_expr val)
11224 tree expr = val.value;
11225 tree t;
11226 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
11227 if (expr != error_mark_node
11228 && !POINTER_TYPE_P (TREE_TYPE (expr))
11229 && !null_pointer_constant_p (expr))
11231 error_at (val.get_location (),
11232 "computed goto must be pointer type");
11233 expr = build_zero_cst (ptr_type_node);
11235 expr = c_fully_fold (expr, false, NULL);
11236 expr = convert (ptr_type_node, expr);
11237 t = build1 (GOTO_EXPR, void_type_node, expr);
11238 SET_EXPR_LOCATION (t, loc);
11239 return add_stmt (t);
11242 /* Generate a C `return' statement. RETVAL is the expression for what
11243 to return, or a null pointer for `return;' with no value. LOC is
11244 the location of the return statement, or the location of the expression,
11245 if the statement has any. If ORIGTYPE is not NULL_TREE, it
11246 is the original type of RETVAL. */
11248 tree
11249 c_finish_return (location_t loc, tree retval, tree origtype)
11251 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
11252 bool no_warning = false;
11253 bool npc = false;
11255 /* Use the expansion point to handle cases such as returning NULL
11256 in a function returning void. */
11257 location_t xloc = expansion_point_location_if_in_system_header (loc);
11259 if (TREE_THIS_VOLATILE (current_function_decl))
11260 warning_at (xloc, 0,
11261 "function declared %<noreturn%> has a %<return%> statement");
11263 if (retval)
11265 tree semantic_type = NULL_TREE;
11266 npc = null_pointer_constant_p (retval);
11267 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
11269 semantic_type = TREE_TYPE (retval);
11270 retval = TREE_OPERAND (retval, 0);
11272 retval = c_fully_fold (retval, false, NULL);
11273 if (semantic_type
11274 && valtype != NULL_TREE
11275 && TREE_CODE (valtype) != VOID_TYPE)
11276 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
11279 if (!retval)
11281 current_function_returns_null = 1;
11282 if ((warn_return_type >= 0 || flag_isoc99)
11283 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
11285 bool warned_here;
11286 if (flag_isoc99)
11287 warned_here = pedwarn
11288 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
11289 "%<return%> with no value, in function returning non-void");
11290 else
11291 warned_here = warning_at
11292 (loc, OPT_Wreturn_type,
11293 "%<return%> with no value, in function returning non-void");
11294 no_warning = true;
11295 if (warned_here)
11296 inform (DECL_SOURCE_LOCATION (current_function_decl),
11297 "declared here");
11300 else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
11302 current_function_returns_null = 1;
11303 bool warned_here;
11304 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
11305 warned_here = pedwarn
11306 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
11307 "%<return%> with a value, in function returning void");
11308 else
11309 warned_here = pedwarn
11310 (xloc, OPT_Wpedantic, "ISO C forbids "
11311 "%<return%> with expression, in function returning void");
11312 if (warned_here)
11313 inform (DECL_SOURCE_LOCATION (current_function_decl),
11314 "declared here");
11316 else
11318 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
11319 retval, origtype, ic_return,
11320 npc, NULL_TREE, NULL_TREE, 0);
11321 tree res = DECL_RESULT (current_function_decl);
11322 tree inner;
11323 bool save;
11325 current_function_returns_value = 1;
11326 if (t == error_mark_node)
11327 return NULL_TREE;
11329 save = in_late_binary_op;
11330 if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
11331 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
11332 || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
11333 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
11334 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
11335 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
11336 in_late_binary_op = true;
11337 inner = t = convert (TREE_TYPE (res), t);
11338 in_late_binary_op = save;
11340 /* Strip any conversions, additions, and subtractions, and see if
11341 we are returning the address of a local variable. Warn if so. */
11342 while (1)
11344 switch (TREE_CODE (inner))
11346 CASE_CONVERT:
11347 case NON_LVALUE_EXPR:
11348 case PLUS_EXPR:
11349 case POINTER_PLUS_EXPR:
11350 inner = TREE_OPERAND (inner, 0);
11351 continue;
11353 case MINUS_EXPR:
11354 /* If the second operand of the MINUS_EXPR has a pointer
11355 type (or is converted from it), this may be valid, so
11356 don't give a warning. */
11358 tree op1 = TREE_OPERAND (inner, 1);
11360 while (!POINTER_TYPE_P (TREE_TYPE (op1))
11361 && (CONVERT_EXPR_P (op1)
11362 || TREE_CODE (op1) == NON_LVALUE_EXPR))
11363 op1 = TREE_OPERAND (op1, 0);
11365 if (POINTER_TYPE_P (TREE_TYPE (op1)))
11366 break;
11368 inner = TREE_OPERAND (inner, 0);
11369 continue;
11372 case ADDR_EXPR:
11373 inner = TREE_OPERAND (inner, 0);
11375 while (REFERENCE_CLASS_P (inner)
11376 && !INDIRECT_REF_P (inner))
11377 inner = TREE_OPERAND (inner, 0);
11379 if (DECL_P (inner)
11380 && !DECL_EXTERNAL (inner)
11381 && !TREE_STATIC (inner)
11382 && DECL_CONTEXT (inner) == current_function_decl
11383 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11385 if (TREE_CODE (inner) == LABEL_DECL)
11386 warning_at (loc, OPT_Wreturn_local_addr,
11387 "function returns address of label");
11388 else
11390 warning_at (loc, OPT_Wreturn_local_addr,
11391 "function returns address of local variable");
11392 tree zero = build_zero_cst (TREE_TYPE (res));
11393 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11396 break;
11398 default:
11399 break;
11402 break;
11405 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11406 SET_EXPR_LOCATION (retval, loc);
11408 if (warn_sequence_point)
11409 verify_sequence_points (retval);
11412 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11413 if (no_warning)
11414 suppress_warning (ret_stmt, OPT_Wreturn_type);
11415 return add_stmt (ret_stmt);
11418 struct c_switch {
11419 /* The SWITCH_STMT being built. */
11420 tree switch_stmt;
11422 /* The original type of the testing expression, i.e. before the
11423 default conversion is applied. */
11424 tree orig_type;
11426 /* A splay-tree mapping the low element of a case range to the high
11427 element, or NULL_TREE if there is no high element. Used to
11428 determine whether or not a new case label duplicates an old case
11429 label. We need a tree, rather than simply a hash table, because
11430 of the GNU case range extension. */
11431 splay_tree cases;
11433 /* The bindings at the point of the switch. This is used for
11434 warnings crossing decls when branching to a case label. */
11435 struct c_spot_bindings *bindings;
11437 /* Whether the switch includes any break statements. */
11438 bool break_stmt_seen_p;
11440 /* The next node on the stack. */
11441 struct c_switch *next;
11443 /* Remember whether the controlling expression had boolean type
11444 before integer promotions for the sake of -Wswitch-bool. */
11445 bool bool_cond_p;
11448 /* A stack of the currently active switch statements. The innermost
11449 switch statement is on the top of the stack. There is no need to
11450 mark the stack for garbage collection because it is only active
11451 during the processing of the body of a function, and we never
11452 collect at that point. */
11454 struct c_switch *c_switch_stack;
11456 /* Start a C switch statement, testing expression EXP. Return the new
11457 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11458 SWITCH_COND_LOC is the location of the switch's condition.
11459 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11461 tree
11462 c_start_switch (location_t switch_loc,
11463 location_t switch_cond_loc,
11464 tree exp, bool explicit_cast_p)
11466 tree orig_type = error_mark_node;
11467 bool bool_cond_p = false;
11468 struct c_switch *cs;
11470 if (exp != error_mark_node)
11472 orig_type = TREE_TYPE (exp);
11474 if (!INTEGRAL_TYPE_P (orig_type))
11476 if (orig_type != error_mark_node)
11478 error_at (switch_cond_loc, "switch quantity not an integer");
11479 orig_type = error_mark_node;
11481 exp = integer_zero_node;
11483 else
11485 tree type = TYPE_MAIN_VARIANT (orig_type);
11486 tree e = exp;
11488 /* Warn if the condition has boolean value. */
11489 while (TREE_CODE (e) == COMPOUND_EXPR)
11490 e = TREE_OPERAND (e, 1);
11492 if ((C_BOOLEAN_TYPE_P (type)
11493 || truth_value_p (TREE_CODE (e)))
11494 /* Explicit cast to int suppresses this warning. */
11495 && !(TREE_CODE (type) == INTEGER_TYPE
11496 && explicit_cast_p))
11497 bool_cond_p = true;
11499 if (!in_system_header_at (input_location)
11500 && (type == long_integer_type_node
11501 || type == long_unsigned_type_node))
11502 warning_at (switch_cond_loc,
11503 OPT_Wtraditional, "%<long%> switch expression not "
11504 "converted to %<int%> in ISO C");
11506 exp = c_fully_fold (exp, false, NULL);
11507 exp = default_conversion (exp);
11509 if (warn_sequence_point)
11510 verify_sequence_points (exp);
11514 /* Add this new SWITCH_STMT to the stack. */
11515 cs = XNEW (struct c_switch);
11516 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11517 NULL_TREE, orig_type, NULL_TREE);
11518 cs->orig_type = orig_type;
11519 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11520 cs->bindings = c_get_switch_bindings ();
11521 cs->break_stmt_seen_p = false;
11522 cs->bool_cond_p = bool_cond_p;
11523 cs->next = c_switch_stack;
11524 c_switch_stack = cs;
11526 return add_stmt (cs->switch_stmt);
11529 /* Process a case label at location LOC, with attributes ATTRS. */
11531 tree
11532 do_case (location_t loc, tree low_value, tree high_value, tree attrs)
11534 tree label = NULL_TREE;
11536 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11538 low_value = c_fully_fold (low_value, false, NULL);
11539 if (TREE_CODE (low_value) == INTEGER_CST)
11540 pedwarn (loc, OPT_Wpedantic,
11541 "case label is not an integer constant expression");
11544 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11546 high_value = c_fully_fold (high_value, false, NULL);
11547 if (TREE_CODE (high_value) == INTEGER_CST)
11548 pedwarn (input_location, OPT_Wpedantic,
11549 "case label is not an integer constant expression");
11552 if (c_switch_stack == NULL)
11554 if (low_value)
11555 error_at (loc, "case label not within a switch statement");
11556 else
11557 error_at (loc, "%<default%> label not within a switch statement");
11558 return NULL_TREE;
11561 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11562 EXPR_LOCATION (c_switch_stack->switch_stmt),
11563 loc))
11564 return NULL_TREE;
11566 label = c_add_case_label (loc, c_switch_stack->cases,
11567 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11568 low_value, high_value, attrs);
11569 if (label == error_mark_node)
11570 label = NULL_TREE;
11571 return label;
11574 /* Finish the switch statement. TYPE is the original type of the
11575 controlling expression of the switch, or NULL_TREE. */
11577 void
11578 c_finish_switch (tree body, tree type)
11580 struct c_switch *cs = c_switch_stack;
11581 location_t switch_location;
11583 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11585 /* Emit warnings as needed. */
11586 switch_location = EXPR_LOCATION (cs->switch_stmt);
11587 c_do_switch_warnings (cs->cases, switch_location,
11588 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11589 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11590 if (c_switch_covers_all_cases_p (cs->cases,
11591 SWITCH_STMT_TYPE (cs->switch_stmt)))
11592 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11593 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11595 /* Pop the stack. */
11596 c_switch_stack = cs->next;
11597 splay_tree_delete (cs->cases);
11598 c_release_switch_bindings (cs->bindings);
11599 XDELETE (cs);
11602 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11603 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11604 may be null. */
11606 void
11607 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11608 tree else_block)
11610 tree stmt;
11612 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11613 SET_EXPR_LOCATION (stmt, if_locus);
11614 add_stmt (stmt);
11617 tree
11618 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11620 /* In switch statements break is sometimes stylistically used after
11621 a return statement. This can lead to spurious warnings about
11622 control reaching the end of a non-void function when it is
11623 inlined. Note that we are calling block_may_fallthru with
11624 language specific tree nodes; this works because
11625 block_may_fallthru returns true when given something it does not
11626 understand. */
11627 bool skip = !block_may_fallthru (cur_stmt_list);
11629 if (is_break)
11630 switch (in_statement)
11632 case 0:
11633 error_at (loc, "break statement not within loop or switch");
11634 return NULL_TREE;
11635 case IN_OMP_BLOCK:
11636 error_at (loc, "invalid exit from OpenMP structured block");
11637 return NULL_TREE;
11638 case IN_OMP_FOR:
11639 error_at (loc, "break statement used with OpenMP for loop");
11640 return NULL_TREE;
11641 case IN_ITERATION_STMT:
11642 case IN_OBJC_FOREACH:
11643 break;
11644 default:
11645 gcc_assert (in_statement & IN_SWITCH_STMT);
11646 c_switch_stack->break_stmt_seen_p = true;
11647 break;
11649 else
11650 switch (in_statement & ~IN_SWITCH_STMT)
11652 case 0:
11653 error_at (loc, "continue statement not within a loop");
11654 return NULL_TREE;
11655 case IN_OMP_BLOCK:
11656 error_at (loc, "invalid exit from OpenMP structured block");
11657 return NULL_TREE;
11658 case IN_ITERATION_STMT:
11659 case IN_OMP_FOR:
11660 case IN_OBJC_FOREACH:
11661 break;
11662 default:
11663 gcc_unreachable ();
11666 if (skip)
11667 return NULL_TREE;
11668 else if ((in_statement & IN_OBJC_FOREACH)
11669 && !(is_break && (in_statement & IN_SWITCH_STMT)))
11671 /* The foreach expander produces low-level code using gotos instead
11672 of a structured loop construct. */
11673 gcc_assert (label);
11674 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11676 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11679 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11681 static void
11682 emit_side_effect_warnings (location_t loc, tree expr)
11684 maybe_warn_nodiscard (loc, expr);
11685 if (!warn_unused_value)
11686 return;
11687 if (expr == error_mark_node)
11689 else if (!TREE_SIDE_EFFECTS (expr))
11691 if (!VOID_TYPE_P (TREE_TYPE (expr))
11692 && !warning_suppressed_p (expr, OPT_Wunused_value))
11693 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11695 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11697 tree r = expr;
11698 location_t cloc = loc;
11699 while (TREE_CODE (r) == COMPOUND_EXPR)
11701 if (EXPR_HAS_LOCATION (r))
11702 cloc = EXPR_LOCATION (r);
11703 r = TREE_OPERAND (r, 1);
11705 if (!TREE_SIDE_EFFECTS (r)
11706 && !VOID_TYPE_P (TREE_TYPE (r))
11707 && !CONVERT_EXPR_P (r)
11708 && !warning_suppressed_p (r, OPT_Wunused_value)
11709 && !warning_suppressed_p (expr, OPT_Wunused_value))
11710 warning_at (cloc, OPT_Wunused_value,
11711 "right-hand operand of comma expression has no effect");
11713 else
11714 warn_if_unused_value (expr, loc);
11717 /* Process an expression as if it were a complete statement. Emit
11718 diagnostics, but do not call ADD_STMT. LOC is the location of the
11719 statement. */
11721 tree
11722 c_process_expr_stmt (location_t loc, tree expr)
11724 tree exprv;
11726 if (!expr)
11727 return NULL_TREE;
11729 expr = c_fully_fold (expr, false, NULL);
11731 if (warn_sequence_point)
11732 verify_sequence_points (expr);
11734 if (TREE_TYPE (expr) != error_mark_node
11735 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11736 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11737 error_at (loc, "expression statement has incomplete type");
11739 /* If we're not processing a statement expression, warn about unused values.
11740 Warnings for statement expressions will be emitted later, once we figure
11741 out which is the result. */
11742 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11743 && (warn_unused_value || warn_unused_result))
11744 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11746 exprv = expr;
11747 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11748 exprv = TREE_OPERAND (exprv, 1);
11749 while (CONVERT_EXPR_P (exprv))
11750 exprv = TREE_OPERAND (exprv, 0);
11751 if (DECL_P (exprv)
11752 || handled_component_p (exprv)
11753 || TREE_CODE (exprv) == ADDR_EXPR)
11754 mark_exp_read (exprv);
11756 /* If the expression is not of a type to which we cannot assign a line
11757 number, wrap the thing in a no-op NOP_EXPR. */
11758 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11760 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11761 SET_EXPR_LOCATION (expr, loc);
11764 return expr;
11767 /* Emit an expression as a statement. LOC is the location of the
11768 expression. */
11770 tree
11771 c_finish_expr_stmt (location_t loc, tree expr)
11773 if (expr)
11774 return add_stmt (c_process_expr_stmt (loc, expr));
11775 else
11776 return NULL;
11779 /* Do the opposite and emit a statement as an expression. To begin,
11780 create a new binding level and return it. */
11782 tree
11783 c_begin_stmt_expr (void)
11785 tree ret;
11787 /* We must force a BLOCK for this level so that, if it is not expanded
11788 later, there is a way to turn off the entire subtree of blocks that
11789 are contained in it. */
11790 keep_next_level ();
11791 ret = c_begin_compound_stmt (true);
11793 c_bindings_start_stmt_expr (c_switch_stack == NULL
11794 ? NULL
11795 : c_switch_stack->bindings);
11797 /* Mark the current statement list as belonging to a statement list. */
11798 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11800 return ret;
11803 /* LOC is the location of the compound statement to which this body
11804 belongs. */
11806 tree
11807 c_finish_stmt_expr (location_t loc, tree body)
11809 tree last, type, tmp, val;
11810 tree *last_p;
11812 body = c_end_compound_stmt (loc, body, true);
11814 c_bindings_end_stmt_expr (c_switch_stack == NULL
11815 ? NULL
11816 : c_switch_stack->bindings);
11818 /* Locate the last statement in BODY. See c_end_compound_stmt
11819 about always returning a BIND_EXPR. */
11820 last_p = &BIND_EXPR_BODY (body);
11821 last = BIND_EXPR_BODY (body);
11823 continue_searching:
11824 if (TREE_CODE (last) == STATEMENT_LIST)
11826 tree_stmt_iterator l = tsi_last (last);
11828 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11829 tsi_prev (&l);
11831 /* This can happen with degenerate cases like ({ }). No value. */
11832 if (tsi_end_p (l))
11833 return body;
11835 /* If we're supposed to generate side effects warnings, process
11836 all of the statements except the last. */
11837 if (warn_unused_value || warn_unused_result)
11839 for (tree_stmt_iterator i = tsi_start (last);
11840 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11842 location_t tloc;
11843 tree t = tsi_stmt (i);
11845 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11846 emit_side_effect_warnings (tloc, t);
11849 last_p = tsi_stmt_ptr (l);
11850 last = *last_p;
11853 /* If the end of the list is exception related, then the list was split
11854 by a call to push_cleanup. Continue searching. */
11855 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11856 || TREE_CODE (last) == TRY_CATCH_EXPR)
11858 last_p = &TREE_OPERAND (last, 0);
11859 last = *last_p;
11860 goto continue_searching;
11863 if (last == error_mark_node)
11864 return last;
11866 /* In the case that the BIND_EXPR is not necessary, return the
11867 expression out from inside it. */
11868 if ((last == BIND_EXPR_BODY (body)
11869 /* Skip nested debug stmts. */
11870 || last == expr_first (BIND_EXPR_BODY (body)))
11871 && BIND_EXPR_VARS (body) == NULL)
11873 /* Even if this looks constant, do not allow it in a constant
11874 expression. */
11875 last = c_wrap_maybe_const (last, true);
11876 /* Do not warn if the return value of a statement expression is
11877 unused. */
11878 suppress_warning (last, OPT_Wunused);
11879 return last;
11882 /* Extract the type of said expression. */
11883 type = TREE_TYPE (last);
11885 /* If we're not returning a value at all, then the BIND_EXPR that
11886 we already have is a fine expression to return. */
11887 if (!type || VOID_TYPE_P (type))
11888 return body;
11890 /* Now that we've located the expression containing the value, it seems
11891 silly to make voidify_wrapper_expr repeat the process. Create a
11892 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11893 tmp = create_tmp_var_raw (type);
11895 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11896 tree_expr_nonnegative_p giving up immediately. */
11897 val = last;
11898 if (TREE_CODE (val) == NOP_EXPR
11899 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11900 val = TREE_OPERAND (val, 0);
11902 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11903 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11906 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11907 SET_EXPR_LOCATION (t, loc);
11908 return t;
11912 /* Begin and end compound statements. This is as simple as pushing
11913 and popping new statement lists from the tree. */
11915 tree
11916 c_begin_compound_stmt (bool do_scope)
11918 tree stmt = push_stmt_list ();
11919 if (do_scope)
11920 push_scope ();
11921 return stmt;
11924 /* End a compound statement. STMT is the statement. LOC is the
11925 location of the compound statement-- this is usually the location
11926 of the opening brace. */
11928 tree
11929 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11931 tree block = NULL;
11933 if (do_scope)
11935 if (c_dialect_objc ())
11936 objc_clear_super_receiver ();
11937 block = pop_scope ();
11940 stmt = pop_stmt_list (stmt);
11941 stmt = c_build_bind_expr (loc, block, stmt);
11943 /* If this compound statement is nested immediately inside a statement
11944 expression, then force a BIND_EXPR to be created. Otherwise we'll
11945 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11946 STATEMENT_LISTs merge, and thus we can lose track of what statement
11947 was really last. */
11948 if (building_stmt_list_p ()
11949 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11950 && TREE_CODE (stmt) != BIND_EXPR)
11952 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11953 TREE_SIDE_EFFECTS (stmt) = 1;
11954 SET_EXPR_LOCATION (stmt, loc);
11957 return stmt;
11960 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11961 when the current scope is exited. EH_ONLY is true when this is not
11962 meant to apply to normal control flow transfer. */
11964 void
11965 push_cleanup (tree decl, tree cleanup, bool eh_only)
11967 enum tree_code code;
11968 tree stmt, list;
11969 bool stmt_expr;
11971 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11972 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11973 add_stmt (stmt);
11974 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11975 list = push_stmt_list ();
11976 TREE_OPERAND (stmt, 0) = list;
11977 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11980 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11981 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11983 static tree
11984 build_vec_cmp (tree_code code, tree type,
11985 tree arg0, tree arg1)
11987 tree zero_vec = build_zero_cst (type);
11988 tree minus_one_vec = build_minus_one_cst (type);
11989 tree cmp_type = truth_type_for (TREE_TYPE (arg0));
11990 tree cmp = build2 (code, cmp_type, arg0, arg1);
11991 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11994 /* Possibly warn about an address of OP never being NULL in a comparison
11995 operation CODE involving null. */
11997 static void
11998 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
12000 /* Prevent warnings issued for macro expansion. */
12001 if (!warn_address
12002 || warning_suppressed_p (op, OPT_Waddress)
12003 || from_macro_expansion_at (loc))
12004 return;
12006 if (TREE_CODE (op) == NOP_EXPR)
12008 /* Allow casts to intptr_t to suppress the warning. */
12009 tree type = TREE_TYPE (op);
12010 if (TREE_CODE (type) == INTEGER_TYPE)
12011 return;
12012 op = TREE_OPERAND (op, 0);
12015 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
12017 /* Allow a cast to void* to suppress the warning. */
12018 tree type = TREE_TYPE (TREE_TYPE (op));
12019 if (VOID_TYPE_P (type))
12020 return;
12022 /* Adding any value to a null pointer, including zero, is undefined
12023 in C. This includes the expression &p[0] where p is the null
12024 pointer, although &p[0] will have been folded to p by this point
12025 and so not diagnosed. */
12026 if (code == EQ_EXPR)
12027 warning_at (loc, OPT_Waddress,
12028 "the comparison will always evaluate as %<false%> "
12029 "for the pointer operand in %qE must not be NULL",
12030 op);
12031 else
12032 warning_at (loc, OPT_Waddress,
12033 "the comparison will always evaluate as %<true%> "
12034 "for the pointer operand in %qE must not be NULL",
12035 op);
12037 return;
12040 if (TREE_CODE (op) != ADDR_EXPR)
12041 return;
12043 op = TREE_OPERAND (op, 0);
12045 if (TREE_CODE (op) == IMAGPART_EXPR
12046 || TREE_CODE (op) == REALPART_EXPR)
12048 /* The address of either complex part may not be null. */
12049 if (code == EQ_EXPR)
12050 warning_at (loc, OPT_Waddress,
12051 "the comparison will always evaluate as %<false%> "
12052 "for the address of %qE will never be NULL",
12053 op);
12054 else
12055 warning_at (loc, OPT_Waddress,
12056 "the comparison will always evaluate as %<true%> "
12057 "for the address of %qE will never be NULL",
12058 op);
12059 return;
12062 /* Set to true in the loop below if OP dereferences is operand.
12063 In such a case the ultimate target need not be a decl for
12064 the null [in]equality test to be constant. */
12065 bool deref = false;
12067 /* Get the outermost array or object, or member. */
12068 while (handled_component_p (op))
12070 if (TREE_CODE (op) == COMPONENT_REF)
12072 /* Get the member (its address is never null). */
12073 op = TREE_OPERAND (op, 1);
12074 break;
12077 /* Get the outer array/object to refer to in the warning. */
12078 op = TREE_OPERAND (op, 0);
12079 deref = true;
12082 if ((!deref && !decl_with_nonnull_addr_p (op))
12083 || from_macro_expansion_at (loc))
12084 return;
12086 bool w;
12087 if (code == EQ_EXPR)
12088 w = warning_at (loc, OPT_Waddress,
12089 "the comparison will always evaluate as %<false%> "
12090 "for the address of %qE will never be NULL",
12091 op);
12092 else
12093 w = warning_at (loc, OPT_Waddress,
12094 "the comparison will always evaluate as %<true%> "
12095 "for the address of %qE will never be NULL",
12096 op);
12098 if (w && DECL_P (op))
12099 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
12102 /* Build a binary-operation expression without default conversions.
12103 CODE is the kind of expression to build.
12104 LOCATION is the operator's location.
12105 This function differs from `build' in several ways:
12106 the data type of the result is computed and recorded in it,
12107 warnings are generated if arg data types are invalid,
12108 special handling for addition and subtraction of pointers is known,
12109 and some optimization is done (operations on narrow ints
12110 are done in the narrower type when that gives the same result).
12111 Constant folding is also done before the result is returned.
12113 Note that the operands will never have enumeral types, or function
12114 or array types, because either they will have the default conversions
12115 performed or they have both just been converted to some other type in which
12116 the arithmetic is to be done. */
12118 tree
12119 build_binary_op (location_t location, enum tree_code code,
12120 tree orig_op0, tree orig_op1, bool convert_p)
12122 tree type0, type1, orig_type0, orig_type1;
12123 tree eptype;
12124 enum tree_code code0, code1;
12125 tree op0, op1;
12126 tree ret = error_mark_node;
12127 const char *invalid_op_diag;
12128 bool op0_int_operands, op1_int_operands;
12129 bool int_const, int_const_or_overflow, int_operands;
12131 /* Expression code to give to the expression when it is built.
12132 Normally this is CODE, which is what the caller asked for,
12133 but in some special cases we change it. */
12134 enum tree_code resultcode = code;
12136 /* Data type in which the computation is to be performed.
12137 In the simplest cases this is the common type of the arguments. */
12138 tree result_type = NULL;
12140 /* When the computation is in excess precision, the type of the
12141 final EXCESS_PRECISION_EXPR. */
12142 tree semantic_result_type = NULL;
12144 /* Nonzero means operands have already been type-converted
12145 in whatever way is necessary.
12146 Zero means they need to be converted to RESULT_TYPE. */
12147 int converted = 0;
12149 /* Nonzero means create the expression with this type, rather than
12150 RESULT_TYPE. */
12151 tree build_type = NULL_TREE;
12153 /* Nonzero means after finally constructing the expression
12154 convert it to this type. */
12155 tree final_type = NULL_TREE;
12157 /* Nonzero if this is an operation like MIN or MAX which can
12158 safely be computed in short if both args are promoted shorts.
12159 Also implies COMMON.
12160 -1 indicates a bitwise operation; this makes a difference
12161 in the exact conditions for when it is safe to do the operation
12162 in a narrower mode. */
12163 int shorten = 0;
12165 /* Nonzero if this is a comparison operation;
12166 if both args are promoted shorts, compare the original shorts.
12167 Also implies COMMON. */
12168 int short_compare = 0;
12170 /* Nonzero if this is a right-shift operation, which can be computed on the
12171 original short and then promoted if the operand is a promoted short. */
12172 int short_shift = 0;
12174 /* Nonzero means set RESULT_TYPE to the common type of the args. */
12175 int common = 0;
12177 /* True means types are compatible as far as ObjC is concerned. */
12178 bool objc_ok;
12180 /* True means this is an arithmetic operation that may need excess
12181 precision. */
12182 bool may_need_excess_precision;
12184 /* True means this is a boolean operation that converts both its
12185 operands to truth-values. */
12186 bool boolean_op = false;
12188 /* Remember whether we're doing / or %. */
12189 bool doing_div_or_mod = false;
12191 /* Remember whether we're doing << or >>. */
12192 bool doing_shift = false;
12194 /* Tree holding instrumentation expression. */
12195 tree instrument_expr = NULL;
12197 if (location == UNKNOWN_LOCATION)
12198 location = input_location;
12200 op0 = orig_op0;
12201 op1 = orig_op1;
12203 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
12204 if (op0_int_operands)
12205 op0 = remove_c_maybe_const_expr (op0);
12206 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
12207 if (op1_int_operands)
12208 op1 = remove_c_maybe_const_expr (op1);
12209 int_operands = (op0_int_operands && op1_int_operands);
12210 if (int_operands)
12212 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
12213 && TREE_CODE (orig_op1) == INTEGER_CST);
12214 int_const = (int_const_or_overflow
12215 && !TREE_OVERFLOW (orig_op0)
12216 && !TREE_OVERFLOW (orig_op1));
12218 else
12219 int_const = int_const_or_overflow = false;
12221 /* Do not apply default conversion in mixed vector/scalar expression. */
12222 if (convert_p
12223 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
12225 op0 = default_conversion (op0);
12226 op1 = default_conversion (op1);
12229 orig_type0 = type0 = TREE_TYPE (op0);
12231 orig_type1 = type1 = TREE_TYPE (op1);
12233 /* The expression codes of the data types of the arguments tell us
12234 whether the arguments are integers, floating, pointers, etc. */
12235 code0 = TREE_CODE (type0);
12236 code1 = TREE_CODE (type1);
12238 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
12239 STRIP_TYPE_NOPS (op0);
12240 STRIP_TYPE_NOPS (op1);
12242 /* If an error was already reported for one of the arguments,
12243 avoid reporting another error. */
12245 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12246 return error_mark_node;
12248 if (code0 == POINTER_TYPE
12249 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
12250 return error_mark_node;
12252 if (code1 == POINTER_TYPE
12253 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
12254 return error_mark_node;
12256 if ((invalid_op_diag
12257 = targetm.invalid_binary_op (code, type0, type1)))
12259 error_at (location, invalid_op_diag);
12260 return error_mark_node;
12263 switch (code)
12265 case PLUS_EXPR:
12266 case MINUS_EXPR:
12267 case MULT_EXPR:
12268 case TRUNC_DIV_EXPR:
12269 case CEIL_DIV_EXPR:
12270 case FLOOR_DIV_EXPR:
12271 case ROUND_DIV_EXPR:
12272 case EXACT_DIV_EXPR:
12273 may_need_excess_precision = true;
12274 break;
12276 case EQ_EXPR:
12277 case NE_EXPR:
12278 case LE_EXPR:
12279 case GE_EXPR:
12280 case LT_EXPR:
12281 case GT_EXPR:
12282 /* Excess precision for implicit conversions of integers to
12283 floating point in C11 and later. */
12284 may_need_excess_precision = (flag_isoc11
12285 && (ANY_INTEGRAL_TYPE_P (type0)
12286 || ANY_INTEGRAL_TYPE_P (type1)));
12287 break;
12289 default:
12290 may_need_excess_precision = false;
12291 break;
12293 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
12295 op0 = TREE_OPERAND (op0, 0);
12296 type0 = TREE_TYPE (op0);
12298 else if (may_need_excess_precision
12299 && (eptype = excess_precision_type (type0)) != NULL_TREE)
12301 type0 = eptype;
12302 op0 = convert (eptype, op0);
12304 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
12306 op1 = TREE_OPERAND (op1, 0);
12307 type1 = TREE_TYPE (op1);
12309 else if (may_need_excess_precision
12310 && (eptype = excess_precision_type (type1)) != NULL_TREE)
12312 type1 = eptype;
12313 op1 = convert (eptype, op1);
12316 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
12318 /* In case when one of the operands of the binary operation is
12319 a vector and another is a scalar -- convert scalar to vector. */
12320 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
12321 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
12323 enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
12324 orig_op1, true);
12326 switch (convert_flag)
12328 case stv_error:
12329 return error_mark_node;
12330 case stv_firstarg:
12332 bool maybe_const = true;
12333 tree sc;
12334 sc = c_fully_fold (op0, false, &maybe_const);
12335 sc = save_expr (sc);
12336 sc = convert (TREE_TYPE (type1), sc);
12337 op0 = build_vector_from_val (type1, sc);
12338 if (!maybe_const)
12339 op0 = c_wrap_maybe_const (op0, true);
12340 orig_type0 = type0 = TREE_TYPE (op0);
12341 code0 = TREE_CODE (type0);
12342 converted = 1;
12343 break;
12345 case stv_secondarg:
12347 bool maybe_const = true;
12348 tree sc;
12349 sc = c_fully_fold (op1, false, &maybe_const);
12350 sc = save_expr (sc);
12351 sc = convert (TREE_TYPE (type0), sc);
12352 op1 = build_vector_from_val (type0, sc);
12353 if (!maybe_const)
12354 op1 = c_wrap_maybe_const (op1, true);
12355 orig_type1 = type1 = TREE_TYPE (op1);
12356 code1 = TREE_CODE (type1);
12357 converted = 1;
12358 break;
12360 default:
12361 break;
12365 switch (code)
12367 case PLUS_EXPR:
12368 /* Handle the pointer + int case. */
12369 if (code0 == POINTER_TYPE
12370 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12372 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
12373 goto return_build_binary_op;
12375 else if (code1 == POINTER_TYPE
12376 && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
12378 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
12379 goto return_build_binary_op;
12381 else
12382 common = 1;
12383 break;
12385 case MINUS_EXPR:
12386 /* Subtraction of two similar pointers.
12387 We must subtract them as integers, then divide by object size. */
12388 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12389 && comp_target_types (location, type0, type1))
12391 ret = pointer_diff (location, op0, op1, &instrument_expr);
12392 goto return_build_binary_op;
12394 /* Handle pointer minus int. Just like pointer plus int. */
12395 else if (code0 == POINTER_TYPE
12396 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12398 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12399 goto return_build_binary_op;
12401 else
12402 common = 1;
12403 break;
12405 case MULT_EXPR:
12406 common = 1;
12407 break;
12409 case TRUNC_DIV_EXPR:
12410 case CEIL_DIV_EXPR:
12411 case FLOOR_DIV_EXPR:
12412 case ROUND_DIV_EXPR:
12413 case EXACT_DIV_EXPR:
12414 doing_div_or_mod = true;
12415 warn_for_div_by_zero (location, op1);
12417 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12418 || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
12419 || code0 == COMPLEX_TYPE
12420 || gnu_vector_type_p (type0))
12421 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12422 || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
12423 || code1 == COMPLEX_TYPE
12424 || gnu_vector_type_p (type1)))
12426 enum tree_code tcode0 = code0, tcode1 = code1;
12428 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12429 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12430 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12431 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12433 if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE)
12434 && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE))
12435 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12436 resultcode = RDIV_EXPR;
12437 else
12438 /* Although it would be tempting to shorten always here, that
12439 loses on some targets, since the modulo instruction is
12440 undefined if the quotient can't be represented in the
12441 computation mode. We shorten only if unsigned or if
12442 dividing by something we know != -1. */
12443 shorten = may_shorten_divmod (op0, op1);
12444 common = 1;
12446 break;
12448 case BIT_AND_EXPR:
12449 case BIT_IOR_EXPR:
12450 case BIT_XOR_EXPR:
12451 if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
12452 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12453 shorten = -1;
12454 /* Allow vector types which are not floating point types. */
12455 else if (gnu_vector_type_p (type0)
12456 && gnu_vector_type_p (type1)
12457 && !VECTOR_FLOAT_TYPE_P (type0)
12458 && !VECTOR_FLOAT_TYPE_P (type1))
12459 common = 1;
12460 break;
12462 case TRUNC_MOD_EXPR:
12463 case FLOOR_MOD_EXPR:
12464 doing_div_or_mod = true;
12465 warn_for_div_by_zero (location, op1);
12467 if (gnu_vector_type_p (type0)
12468 && gnu_vector_type_p (type1)
12469 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12470 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12471 common = 1;
12472 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
12473 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12475 /* Although it would be tempting to shorten always here, that loses
12476 on some targets, since the modulo instruction is undefined if the
12477 quotient can't be represented in the computation mode. We shorten
12478 only if unsigned or if dividing by something we know != -1. */
12479 shorten = may_shorten_divmod (op0, op1);
12480 common = 1;
12482 break;
12484 case TRUTH_ANDIF_EXPR:
12485 case TRUTH_ORIF_EXPR:
12486 case TRUTH_AND_EXPR:
12487 case TRUTH_OR_EXPR:
12488 case TRUTH_XOR_EXPR:
12489 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12490 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12491 || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
12492 || code0 == BITINT_TYPE)
12493 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12494 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12495 || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
12496 || code1 == BITINT_TYPE))
12498 /* Result of these operations is always an int,
12499 but that does not mean the operands should be
12500 converted to ints! */
12501 result_type = integer_type_node;
12502 if (op0_int_operands)
12504 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12505 op0 = remove_c_maybe_const_expr (op0);
12507 else
12508 op0 = c_objc_common_truthvalue_conversion (location, op0);
12509 if (op1_int_operands)
12511 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12512 op1 = remove_c_maybe_const_expr (op1);
12514 else
12515 op1 = c_objc_common_truthvalue_conversion (location, op1);
12516 converted = 1;
12517 boolean_op = true;
12519 if (code == TRUTH_ANDIF_EXPR)
12521 int_const_or_overflow = (int_operands
12522 && TREE_CODE (orig_op0) == INTEGER_CST
12523 && (op0 == truthvalue_false_node
12524 || TREE_CODE (orig_op1) == INTEGER_CST));
12525 int_const = (int_const_or_overflow
12526 && !TREE_OVERFLOW (orig_op0)
12527 && (op0 == truthvalue_false_node
12528 || !TREE_OVERFLOW (orig_op1)));
12530 else if (code == TRUTH_ORIF_EXPR)
12532 int_const_or_overflow = (int_operands
12533 && TREE_CODE (orig_op0) == INTEGER_CST
12534 && (op0 == truthvalue_true_node
12535 || TREE_CODE (orig_op1) == INTEGER_CST));
12536 int_const = (int_const_or_overflow
12537 && !TREE_OVERFLOW (orig_op0)
12538 && (op0 == truthvalue_true_node
12539 || !TREE_OVERFLOW (orig_op1)));
12541 break;
12543 /* Shift operations: result has same type as first operand;
12544 always convert second operand to int.
12545 Also set SHORT_SHIFT if shifting rightward. */
12547 case RSHIFT_EXPR:
12548 if (gnu_vector_type_p (type0)
12549 && gnu_vector_type_p (type1)
12550 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12551 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12552 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12553 TYPE_VECTOR_SUBPARTS (type1)))
12555 result_type = type0;
12556 converted = 1;
12558 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12559 || code0 == BITINT_TYPE
12560 || (gnu_vector_type_p (type0)
12561 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12562 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12564 doing_shift = true;
12565 if (TREE_CODE (op1) == INTEGER_CST)
12567 if (tree_int_cst_sgn (op1) < 0)
12569 int_const = false;
12570 if (c_inhibit_evaluation_warnings == 0)
12571 warning_at (location, OPT_Wshift_count_negative,
12572 "right shift count is negative");
12574 else if (code0 == VECTOR_TYPE)
12576 if (compare_tree_int (op1,
12577 TYPE_PRECISION (TREE_TYPE (type0)))
12578 >= 0)
12580 int_const = false;
12581 if (c_inhibit_evaluation_warnings == 0)
12582 warning_at (location, OPT_Wshift_count_overflow,
12583 "right shift count >= width of vector element");
12586 else
12588 if (!integer_zerop (op1))
12589 short_shift = 1;
12591 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12593 int_const = false;
12594 if (c_inhibit_evaluation_warnings == 0)
12595 warning_at (location, OPT_Wshift_count_overflow,
12596 "right shift count >= width of type");
12601 /* Use the type of the value to be shifted. */
12602 result_type = type0;
12603 /* Avoid converting op1 to result_type later. */
12604 converted = 1;
12606 break;
12608 case LSHIFT_EXPR:
12609 if (gnu_vector_type_p (type0)
12610 && gnu_vector_type_p (type1)
12611 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12612 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12613 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12614 TYPE_VECTOR_SUBPARTS (type1)))
12616 result_type = type0;
12617 converted = 1;
12619 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12620 || code0 == BITINT_TYPE
12621 || (gnu_vector_type_p (type0)
12622 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12623 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12625 doing_shift = true;
12626 if (TREE_CODE (op0) == INTEGER_CST
12627 && tree_int_cst_sgn (op0) < 0
12628 && !TYPE_OVERFLOW_WRAPS (type0))
12630 /* Don't reject a left shift of a negative value in a context
12631 where a constant expression is needed in C90. */
12632 if (flag_isoc99)
12633 int_const = false;
12634 if (c_inhibit_evaluation_warnings == 0)
12635 warning_at (location, OPT_Wshift_negative_value,
12636 "left shift of negative value");
12638 if (TREE_CODE (op1) == INTEGER_CST)
12640 if (tree_int_cst_sgn (op1) < 0)
12642 int_const = false;
12643 if (c_inhibit_evaluation_warnings == 0)
12644 warning_at (location, OPT_Wshift_count_negative,
12645 "left shift count is negative");
12647 else if (code0 == VECTOR_TYPE)
12649 if (compare_tree_int (op1,
12650 TYPE_PRECISION (TREE_TYPE (type0)))
12651 >= 0)
12653 int_const = false;
12654 if (c_inhibit_evaluation_warnings == 0)
12655 warning_at (location, OPT_Wshift_count_overflow,
12656 "left shift count >= width of vector element");
12659 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12661 int_const = false;
12662 if (c_inhibit_evaluation_warnings == 0)
12663 warning_at (location, OPT_Wshift_count_overflow,
12664 "left shift count >= width of type");
12666 else if (TREE_CODE (op0) == INTEGER_CST
12667 && maybe_warn_shift_overflow (location, op0, op1)
12668 && flag_isoc99)
12669 int_const = false;
12672 /* Use the type of the value to be shifted. */
12673 result_type = type0;
12674 /* Avoid converting op1 to result_type later. */
12675 converted = 1;
12677 break;
12679 case EQ_EXPR:
12680 case NE_EXPR:
12681 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12683 tree intt;
12684 if (!vector_types_compatible_elements_p (type0, type1))
12686 error_at (location, "comparing vectors with different "
12687 "element types");
12688 return error_mark_node;
12691 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12692 TYPE_VECTOR_SUBPARTS (type1)))
12694 error_at (location, "comparing vectors with different "
12695 "number of elements");
12696 return error_mark_node;
12699 /* It's not precisely specified how the usual arithmetic
12700 conversions apply to the vector types. Here, we use
12701 the unsigned type if one of the operands is signed and
12702 the other one is unsigned. */
12703 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12705 if (!TYPE_UNSIGNED (type0))
12706 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12707 else
12708 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12709 warning_at (location, OPT_Wsign_compare, "comparison between "
12710 "types %qT and %qT", type0, type1);
12713 /* Always construct signed integer vector type. */
12714 intt = c_common_type_for_size (GET_MODE_BITSIZE
12715 (SCALAR_TYPE_MODE
12716 (TREE_TYPE (type0))), 0);
12717 if (!intt)
12719 error_at (location, "could not find an integer type "
12720 "of the same size as %qT",
12721 TREE_TYPE (type0));
12722 return error_mark_node;
12724 result_type = build_opaque_vector_type (intt,
12725 TYPE_VECTOR_SUBPARTS (type0));
12726 converted = 1;
12727 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12728 goto return_build_binary_op;
12730 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12731 warning_at (location,
12732 OPT_Wfloat_equal,
12733 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12734 /* Result of comparison is always int,
12735 but don't convert the args to int! */
12736 build_type = integer_type_node;
12737 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
12738 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12739 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12740 || code1 == BITINT_TYPE
12741 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12742 short_compare = 1;
12743 else if (code0 == POINTER_TYPE
12744 && (code1 == NULLPTR_TYPE
12745 || null_pointer_constant_p (orig_op1)))
12747 maybe_warn_for_null_address (location, op0, code);
12748 result_type = type0;
12750 else if (code1 == POINTER_TYPE
12751 && (code0 == NULLPTR_TYPE
12752 || null_pointer_constant_p (orig_op0)))
12754 maybe_warn_for_null_address (location, op1, code);
12755 result_type = type1;
12757 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12759 tree tt0 = TREE_TYPE (type0);
12760 tree tt1 = TREE_TYPE (type1);
12761 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12762 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12763 addr_space_t as_common = ADDR_SPACE_GENERIC;
12765 /* Anything compares with void *. void * compares with anything.
12766 Otherwise, the targets must be compatible
12767 and both must be object or both incomplete. */
12768 if (comp_target_types (location, type0, type1))
12769 result_type = common_pointer_type (type0, type1);
12770 else if (!addr_space_superset (as0, as1, &as_common))
12772 error_at (location, "comparison of pointers to "
12773 "disjoint address spaces");
12774 return error_mark_node;
12776 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12778 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12779 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12780 "comparison of %<void *%> with function pointer");
12782 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12784 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12785 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12786 "comparison of %<void *%> with function pointer");
12788 else
12789 /* Avoid warning about the volatile ObjC EH puts on decls. */
12790 if (!objc_ok)
12791 pedwarn (location, OPT_Wcompare_distinct_pointer_types,
12792 "comparison of distinct pointer types lacks a cast");
12794 if (result_type == NULL_TREE)
12796 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12797 result_type = build_pointer_type
12798 (build_qualified_type (void_type_node, qual));
12801 else if (code0 == POINTER_TYPE
12802 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12804 result_type = type0;
12805 pedwarn (location, 0, "comparison between pointer and integer");
12807 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
12808 && code1 == POINTER_TYPE)
12810 result_type = type1;
12811 pedwarn (location, 0, "comparison between pointer and integer");
12813 /* 6.5.9: One of the following shall hold:
12814 -- both operands have type nullptr_t; */
12815 else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
12817 result_type = nullptr_type_node;
12818 /* No need to convert the operands to result_type later. */
12819 converted = 1;
12821 /* -- one operand has type nullptr_t and the other is a null pointer
12822 constant. We will have to convert the former to the type of the
12823 latter, because during gimplification we can't have mismatching
12824 comparison operand type. We convert from nullptr_t to the other
12825 type, since only nullptr_t can be converted to nullptr_t. Also,
12826 even a constant 0 is a null pointer constant, so we may have to
12827 create a pointer type from its type. */
12828 else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
12829 result_type = (INTEGRAL_TYPE_P (type1)
12830 ? build_pointer_type (type1) : type1);
12831 else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
12832 result_type = (INTEGRAL_TYPE_P (type0)
12833 ? build_pointer_type (type0) : type0);
12834 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
12835 || truth_value_p (TREE_CODE (orig_op0)))
12836 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
12837 || truth_value_p (TREE_CODE (orig_op1))))
12838 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12839 break;
12841 case LE_EXPR:
12842 case GE_EXPR:
12843 case LT_EXPR:
12844 case GT_EXPR:
12845 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12847 tree intt;
12848 if (!vector_types_compatible_elements_p (type0, type1))
12850 error_at (location, "comparing vectors with different "
12851 "element types");
12852 return error_mark_node;
12855 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12856 TYPE_VECTOR_SUBPARTS (type1)))
12858 error_at (location, "comparing vectors with different "
12859 "number of elements");
12860 return error_mark_node;
12863 /* It's not precisely specified how the usual arithmetic
12864 conversions apply to the vector types. Here, we use
12865 the unsigned type if one of the operands is signed and
12866 the other one is unsigned. */
12867 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12869 if (!TYPE_UNSIGNED (type0))
12870 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12871 else
12872 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12873 warning_at (location, OPT_Wsign_compare, "comparison between "
12874 "types %qT and %qT", type0, type1);
12877 /* Always construct signed integer vector type. */
12878 intt = c_common_type_for_size (GET_MODE_BITSIZE
12879 (SCALAR_TYPE_MODE
12880 (TREE_TYPE (type0))), 0);
12881 if (!intt)
12883 error_at (location, "could not find an integer type "
12884 "of the same size as %qT",
12885 TREE_TYPE (type0));
12886 return error_mark_node;
12888 result_type = build_opaque_vector_type (intt,
12889 TYPE_VECTOR_SUBPARTS (type0));
12890 converted = 1;
12891 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12892 goto return_build_binary_op;
12894 build_type = integer_type_node;
12895 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12896 || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
12897 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12898 || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
12899 short_compare = 1;
12900 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12902 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12903 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12904 addr_space_t as_common;
12906 if (comp_target_types (location, type0, type1))
12908 result_type = common_pointer_type (type0, type1);
12909 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12910 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12911 pedwarn_c99 (location, OPT_Wpedantic,
12912 "comparison of complete and incomplete pointers");
12913 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12914 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12915 "ordered comparisons of pointers to functions");
12916 else if (null_pointer_constant_p (orig_op0)
12917 || null_pointer_constant_p (orig_op1))
12918 warning_at (location, OPT_Wextra,
12919 "ordered comparison of pointer with null pointer");
12922 else if (!addr_space_superset (as0, as1, &as_common))
12924 error_at (location, "comparison of pointers to "
12925 "disjoint address spaces");
12926 return error_mark_node;
12928 else
12930 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12931 result_type = build_pointer_type
12932 (build_qualified_type (void_type_node, qual));
12933 pedwarn (location, OPT_Wcompare_distinct_pointer_types,
12934 "comparison of distinct pointer types lacks a cast");
12937 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12939 result_type = type0;
12940 if (pedantic)
12941 pedwarn (location, OPT_Wpedantic,
12942 "ordered comparison of pointer with integer zero");
12943 else if (extra_warnings)
12944 warning_at (location, OPT_Wextra,
12945 "ordered comparison of pointer with integer zero");
12947 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12949 result_type = type1;
12950 if (pedantic)
12951 pedwarn (location, OPT_Wpedantic,
12952 "ordered comparison of pointer with integer zero");
12953 else if (extra_warnings)
12954 warning_at (location, OPT_Wextra,
12955 "ordered comparison of pointer with integer zero");
12957 else if (code0 == POINTER_TYPE
12958 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12960 result_type = type0;
12961 pedwarn (location, 0, "comparison between pointer and integer");
12963 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
12964 && code1 == POINTER_TYPE)
12966 result_type = type1;
12967 pedwarn (location, 0, "comparison between pointer and integer");
12970 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12971 && current_function_decl != NULL_TREE
12972 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12974 op0 = save_expr (op0);
12975 op1 = save_expr (op1);
12977 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12978 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12981 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
12982 || truth_value_p (TREE_CODE (orig_op0)))
12983 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
12984 || truth_value_p (TREE_CODE (orig_op1))))
12985 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12986 break;
12988 case MIN_EXPR:
12989 case MAX_EXPR:
12990 /* Used for OpenMP atomics. */
12991 gcc_assert (flag_openmp);
12992 common = 1;
12993 break;
12995 default:
12996 gcc_unreachable ();
12999 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
13000 return error_mark_node;
13002 if (gnu_vector_type_p (type0)
13003 && gnu_vector_type_p (type1)
13004 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
13005 || !vector_types_compatible_elements_p (type0, type1)))
13007 gcc_rich_location richloc (location);
13008 maybe_range_label_for_tree_type_mismatch
13009 label_for_op0 (orig_op0, orig_op1),
13010 label_for_op1 (orig_op1, orig_op0);
13011 richloc.maybe_add_expr (orig_op0, &label_for_op0);
13012 richloc.maybe_add_expr (orig_op1, &label_for_op1);
13013 binary_op_error (&richloc, code, type0, type1);
13014 return error_mark_node;
13017 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
13018 || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
13019 || gnu_vector_type_p (type0))
13020 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
13021 || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
13022 || gnu_vector_type_p (type1)))
13024 bool first_complex = (code0 == COMPLEX_TYPE);
13025 bool second_complex = (code1 == COMPLEX_TYPE);
13026 int none_complex = (!first_complex && !second_complex);
13028 if (shorten || common || short_compare)
13030 result_type = c_common_type (type0, type1);
13031 do_warn_double_promotion (result_type, type0, type1,
13032 "implicit conversion from %qT to %qT "
13033 "to match other operand of binary "
13034 "expression",
13035 location);
13036 if (result_type == error_mark_node)
13037 return error_mark_node;
13040 if (first_complex != second_complex
13041 && (code == PLUS_EXPR
13042 || code == MINUS_EXPR
13043 || code == MULT_EXPR
13044 || (code == TRUNC_DIV_EXPR && first_complex))
13045 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
13046 && flag_signed_zeros)
13048 /* An operation on mixed real/complex operands must be
13049 handled specially, but the language-independent code can
13050 more easily optimize the plain complex arithmetic if
13051 -fno-signed-zeros. */
13052 tree real_type = TREE_TYPE (result_type);
13053 tree real, imag;
13054 if (type0 != orig_type0 || type1 != orig_type1)
13056 gcc_assert (may_need_excess_precision && common);
13057 semantic_result_type = c_common_type (orig_type0, orig_type1);
13059 if (first_complex)
13061 if (TREE_TYPE (op0) != result_type)
13062 op0 = convert_and_check (location, result_type, op0);
13063 if (TREE_TYPE (op1) != real_type)
13064 op1 = convert_and_check (location, real_type, op1);
13066 else
13068 if (TREE_TYPE (op0) != real_type)
13069 op0 = convert_and_check (location, real_type, op0);
13070 if (TREE_TYPE (op1) != result_type)
13071 op1 = convert_and_check (location, result_type, op1);
13073 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13074 return error_mark_node;
13075 if (first_complex)
13077 op0 = save_expr (op0);
13078 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
13079 op0, true);
13080 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
13081 op0, true);
13082 switch (code)
13084 case MULT_EXPR:
13085 case TRUNC_DIV_EXPR:
13086 op1 = save_expr (op1);
13087 imag = build2 (resultcode, real_type, imag, op1);
13088 /* Fall through. */
13089 case PLUS_EXPR:
13090 case MINUS_EXPR:
13091 real = build2 (resultcode, real_type, real, op1);
13092 break;
13093 default:
13094 gcc_unreachable();
13097 else
13099 op1 = save_expr (op1);
13100 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
13101 op1, true);
13102 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
13103 op1, true);
13104 switch (code)
13106 case MULT_EXPR:
13107 op0 = save_expr (op0);
13108 imag = build2 (resultcode, real_type, op0, imag);
13109 /* Fall through. */
13110 case PLUS_EXPR:
13111 real = build2 (resultcode, real_type, op0, real);
13112 break;
13113 case MINUS_EXPR:
13114 real = build2 (resultcode, real_type, op0, real);
13115 imag = build1 (NEGATE_EXPR, real_type, imag);
13116 break;
13117 default:
13118 gcc_unreachable();
13121 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
13122 goto return_build_binary_op;
13125 /* For certain operations (which identify themselves by shorten != 0)
13126 if both args were extended from the same smaller type,
13127 do the arithmetic in that type and then extend.
13129 shorten !=0 and !=1 indicates a bitwise operation.
13130 For them, this optimization is safe only if
13131 both args are zero-extended or both are sign-extended.
13132 Otherwise, we might change the result.
13133 Eg, (short)-1 | (unsigned short)-1 is (int)-1
13134 but calculated in (unsigned short) it would be (unsigned short)-1. */
13136 if (shorten && none_complex)
13138 final_type = result_type;
13139 result_type = shorten_binary_op (result_type, op0, op1,
13140 shorten == -1);
13143 /* Shifts can be shortened if shifting right. */
13145 if (short_shift)
13147 int unsigned_arg;
13148 tree arg0 = get_narrower (op0, &unsigned_arg);
13150 final_type = result_type;
13152 if (arg0 == op0 && final_type == TREE_TYPE (op0))
13153 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
13155 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
13156 && tree_int_cst_sgn (op1) > 0
13157 /* We can shorten only if the shift count is less than the
13158 number of bits in the smaller type size. */
13159 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
13160 /* We cannot drop an unsigned shift after sign-extension. */
13161 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
13163 /* Do an unsigned shift if the operand was zero-extended. */
13164 result_type
13165 = c_common_signed_or_unsigned_type (unsigned_arg,
13166 TREE_TYPE (arg0));
13167 /* Convert value-to-be-shifted to that type. */
13168 if (TREE_TYPE (op0) != result_type)
13169 op0 = convert (result_type, op0);
13170 converted = 1;
13174 /* Comparison operations are shortened too but differently.
13175 They identify themselves by setting short_compare = 1. */
13177 if (short_compare)
13179 /* Don't write &op0, etc., because that would prevent op0
13180 from being kept in a register.
13181 Instead, make copies of the our local variables and
13182 pass the copies by reference, then copy them back afterward. */
13183 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
13184 enum tree_code xresultcode = resultcode;
13185 tree val
13186 = shorten_compare (location, &xop0, &xop1, &xresult_type,
13187 &xresultcode);
13189 if (val != NULL_TREE)
13191 ret = val;
13192 goto return_build_binary_op;
13195 op0 = xop0, op1 = xop1;
13196 converted = 1;
13197 resultcode = xresultcode;
13199 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
13201 bool op0_maybe_const = true;
13202 bool op1_maybe_const = true;
13203 tree orig_op0_folded, orig_op1_folded;
13205 if (in_late_binary_op)
13207 orig_op0_folded = orig_op0;
13208 orig_op1_folded = orig_op1;
13210 else
13212 /* Fold for the sake of possible warnings, as in
13213 build_conditional_expr. This requires the
13214 "original" values to be folded, not just op0 and
13215 op1. */
13216 c_inhibit_evaluation_warnings++;
13217 op0 = c_fully_fold (op0, require_constant_value,
13218 &op0_maybe_const);
13219 op1 = c_fully_fold (op1, require_constant_value,
13220 &op1_maybe_const);
13221 c_inhibit_evaluation_warnings--;
13222 orig_op0_folded = c_fully_fold (orig_op0,
13223 require_constant_value,
13224 NULL);
13225 orig_op1_folded = c_fully_fold (orig_op1,
13226 require_constant_value,
13227 NULL);
13230 if (warn_sign_compare)
13231 warn_for_sign_compare (location, orig_op0_folded,
13232 orig_op1_folded, op0, op1,
13233 result_type, resultcode);
13234 if (!in_late_binary_op && !int_operands)
13236 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
13237 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
13238 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
13239 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
13245 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
13246 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
13247 Then the expression will be built.
13248 It will be given type FINAL_TYPE if that is nonzero;
13249 otherwise, it will be given type RESULT_TYPE. */
13251 if (!result_type)
13253 /* Favor showing any expression locations that are available. */
13254 op_location_t oploc (location, UNKNOWN_LOCATION);
13255 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
13256 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
13257 return error_mark_node;
13260 if (build_type == NULL_TREE)
13262 build_type = result_type;
13263 if ((type0 != orig_type0 || type1 != orig_type1)
13264 && !boolean_op)
13266 gcc_assert (may_need_excess_precision && common);
13267 semantic_result_type = c_common_type (orig_type0, orig_type1);
13271 if (!converted)
13273 op0 = ep_convert_and_check (location, result_type, op0,
13274 semantic_result_type);
13275 op1 = ep_convert_and_check (location, result_type, op1,
13276 semantic_result_type);
13278 /* This can happen if one operand has a vector type, and the other
13279 has a different type. */
13280 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13281 return error_mark_node;
13284 if (sanitize_flags_p ((SANITIZE_SHIFT
13285 | SANITIZE_DIVIDE
13286 | SANITIZE_FLOAT_DIVIDE
13287 | SANITIZE_SI_OVERFLOW))
13288 && current_function_decl != NULL_TREE
13289 && (doing_div_or_mod || doing_shift)
13290 && !require_constant_value)
13292 /* OP0 and/or OP1 might have side-effects. */
13293 op0 = save_expr (op0);
13294 op1 = save_expr (op1);
13295 op0 = c_fully_fold (op0, false, NULL);
13296 op1 = c_fully_fold (op1, false, NULL);
13297 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
13298 | SANITIZE_FLOAT_DIVIDE
13299 | SANITIZE_SI_OVERFLOW))))
13300 instrument_expr = ubsan_instrument_division (location, op0, op1);
13301 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
13302 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
13305 /* Treat expressions in initializers specially as they can't trap. */
13306 if (int_const_or_overflow)
13307 ret = (require_constant_value
13308 ? fold_build2_initializer_loc (location, resultcode, build_type,
13309 op0, op1)
13310 : fold_build2_loc (location, resultcode, build_type, op0, op1));
13311 else
13312 ret = build2 (resultcode, build_type, op0, op1);
13313 if (final_type != NULL_TREE)
13314 ret = convert (final_type, ret);
13316 return_build_binary_op:
13317 gcc_assert (ret != error_mark_node);
13318 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
13319 ret = (int_operands
13320 ? note_integer_operands (ret)
13321 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
13322 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
13323 && !in_late_binary_op)
13324 ret = note_integer_operands (ret);
13325 protected_set_expr_location (ret, location);
13327 if (instrument_expr != NULL)
13328 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
13329 instrument_expr, ret);
13331 if (semantic_result_type)
13332 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
13333 semantic_result_type, ret);
13335 return ret;
13339 /* Convert EXPR to be a truth-value, validating its type for this
13340 purpose. LOCATION is the source location for the expression. */
13342 tree
13343 c_objc_common_truthvalue_conversion (location_t location, tree expr)
13345 bool int_const, int_operands;
13347 switch (TREE_CODE (TREE_TYPE (expr)))
13349 case ARRAY_TYPE:
13350 error_at (location, "used array that cannot be converted to pointer where scalar is required");
13351 return error_mark_node;
13353 case RECORD_TYPE:
13354 error_at (location, "used struct type value where scalar is required");
13355 return error_mark_node;
13357 case UNION_TYPE:
13358 error_at (location, "used union type value where scalar is required");
13359 return error_mark_node;
13361 case VOID_TYPE:
13362 error_at (location, "void value not ignored as it ought to be");
13363 return error_mark_node;
13365 case POINTER_TYPE:
13366 if (reject_gcc_builtin (expr))
13367 return error_mark_node;
13368 break;
13370 case FUNCTION_TYPE:
13371 gcc_unreachable ();
13373 case VECTOR_TYPE:
13374 error_at (location, "used vector type where scalar is required");
13375 return error_mark_node;
13377 default:
13378 break;
13381 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
13382 int_operands = EXPR_INT_CONST_OPERANDS (expr);
13383 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
13385 expr = remove_c_maybe_const_expr (expr);
13386 expr = build2 (NE_EXPR, integer_type_node, expr,
13387 convert (TREE_TYPE (expr), integer_zero_node));
13388 expr = note_integer_operands (expr);
13390 else
13391 /* ??? Should we also give an error for vectors rather than leaving
13392 those to give errors later? */
13393 expr = c_common_truthvalue_conversion (location, expr);
13395 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
13397 if (TREE_OVERFLOW (expr))
13398 return expr;
13399 else
13400 return note_integer_operands (expr);
13402 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
13403 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13404 return expr;
13408 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
13409 required. */
13411 tree
13412 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
13414 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13416 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13417 /* Executing a compound literal inside a function reinitializes
13418 it. */
13419 if (!TREE_STATIC (decl))
13420 *se = true;
13421 return decl;
13423 else
13424 return expr;
13427 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13428 statement. LOC is the location of the construct. */
13430 tree
13431 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13432 tree clauses)
13434 body = c_end_compound_stmt (loc, body, true);
13436 tree stmt = make_node (code);
13437 TREE_TYPE (stmt) = void_type_node;
13438 OMP_BODY (stmt) = body;
13439 OMP_CLAUSES (stmt) = clauses;
13440 SET_EXPR_LOCATION (stmt, loc);
13442 return add_stmt (stmt);
13445 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13446 statement. LOC is the location of the OACC_DATA. */
13448 tree
13449 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13451 tree stmt;
13453 block = c_end_compound_stmt (loc, block, true);
13455 stmt = make_node (OACC_DATA);
13456 TREE_TYPE (stmt) = void_type_node;
13457 OACC_DATA_CLAUSES (stmt) = clauses;
13458 OACC_DATA_BODY (stmt) = block;
13459 SET_EXPR_LOCATION (stmt, loc);
13461 return add_stmt (stmt);
13464 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13465 statement. LOC is the location of the OACC_HOST_DATA. */
13467 tree
13468 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13470 tree stmt;
13472 block = c_end_compound_stmt (loc, block, true);
13474 stmt = make_node (OACC_HOST_DATA);
13475 TREE_TYPE (stmt) = void_type_node;
13476 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13477 OACC_HOST_DATA_BODY (stmt) = block;
13478 SET_EXPR_LOCATION (stmt, loc);
13480 return add_stmt (stmt);
13483 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13485 tree
13486 c_begin_omp_parallel (void)
13488 tree block;
13490 keep_next_level ();
13491 block = c_begin_compound_stmt (true);
13493 return block;
13496 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13497 statement. LOC is the location of the OMP_PARALLEL. */
13499 tree
13500 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13502 tree stmt;
13504 block = c_end_compound_stmt (loc, block, true);
13506 stmt = make_node (OMP_PARALLEL);
13507 TREE_TYPE (stmt) = void_type_node;
13508 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13509 OMP_PARALLEL_BODY (stmt) = block;
13510 SET_EXPR_LOCATION (stmt, loc);
13512 return add_stmt (stmt);
13515 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13517 tree
13518 c_begin_omp_task (void)
13520 tree block;
13522 keep_next_level ();
13523 block = c_begin_compound_stmt (true);
13525 return block;
13528 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13529 statement. LOC is the location of the #pragma. */
13531 tree
13532 c_finish_omp_task (location_t loc, tree clauses, tree block)
13534 tree stmt;
13536 block = c_end_compound_stmt (loc, block, true);
13538 stmt = make_node (OMP_TASK);
13539 TREE_TYPE (stmt) = void_type_node;
13540 OMP_TASK_CLAUSES (stmt) = clauses;
13541 OMP_TASK_BODY (stmt) = block;
13542 SET_EXPR_LOCATION (stmt, loc);
13544 return add_stmt (stmt);
13547 /* Generate GOMP_cancel call for #pragma omp cancel. */
13549 void
13550 c_finish_omp_cancel (location_t loc, tree clauses)
13552 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13553 int mask = 0;
13554 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13555 mask = 1;
13556 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13557 mask = 2;
13558 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13559 mask = 4;
13560 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13561 mask = 8;
13562 else
13564 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13565 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13566 "clauses");
13567 return;
13569 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13570 if (ifc != NULL_TREE)
13572 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13573 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13574 error_at (OMP_CLAUSE_LOCATION (ifc),
13575 "expected %<cancel%> %<if%> clause modifier");
13576 else
13578 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13579 if (ifc2 != NULL_TREE)
13581 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13582 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13583 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13584 error_at (OMP_CLAUSE_LOCATION (ifc2),
13585 "expected %<cancel%> %<if%> clause modifier");
13589 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13590 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13591 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13592 build_zero_cst (type));
13594 else
13595 ifc = boolean_true_node;
13596 tree stmt = build_call_expr_loc (loc, fn, 2,
13597 build_int_cst (integer_type_node, mask),
13598 ifc);
13599 add_stmt (stmt);
13602 /* Generate GOMP_cancellation_point call for
13603 #pragma omp cancellation point. */
13605 void
13606 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13608 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13609 int mask = 0;
13610 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13611 mask = 1;
13612 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13613 mask = 2;
13614 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13615 mask = 4;
13616 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13617 mask = 8;
13618 else
13620 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13621 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13622 "clauses");
13623 return;
13625 tree stmt = build_call_expr_loc (loc, fn, 1,
13626 build_int_cst (integer_type_node, mask));
13627 add_stmt (stmt);
13630 /* Helper function for handle_omp_array_sections. Called recursively
13631 to handle multiple array-section-subscripts. C is the clause,
13632 T current expression (initially OMP_CLAUSE_DECL), which is either
13633 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13634 expression if specified, TREE_VALUE length expression if specified,
13635 TREE_CHAIN is what it has been specified after, or some decl.
13636 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13637 set to true if any of the array-section-subscript could have length
13638 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13639 first array-section-subscript which is known not to have length
13640 of one. Given say:
13641 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13642 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13643 all are or may have length of 1, array-section-subscript [:2] is the
13644 first one known not to have length 1. For array-section-subscript
13645 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13646 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13647 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13648 case though, as some lengths could be zero. */
13650 static tree
13651 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13652 bool &maybe_zero_len, unsigned int &first_non_one,
13653 enum c_omp_region_type ort)
13655 tree ret, low_bound, length, type;
13656 if (TREE_CODE (t) != TREE_LIST)
13658 if (error_operand_p (t))
13659 return error_mark_node;
13660 ret = t;
13661 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13662 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13663 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13665 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13666 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13667 return error_mark_node;
13669 while (INDIRECT_REF_P (t))
13671 t = TREE_OPERAND (t, 0);
13672 STRIP_NOPS (t);
13673 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13674 t = TREE_OPERAND (t, 0);
13676 while (TREE_CODE (t) == COMPOUND_EXPR)
13678 t = TREE_OPERAND (t, 1);
13679 STRIP_NOPS (t);
13681 if (TREE_CODE (t) == COMPONENT_REF
13682 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13683 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13684 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13686 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13688 error_at (OMP_CLAUSE_LOCATION (c),
13689 "bit-field %qE in %qs clause",
13690 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13691 return error_mark_node;
13693 while (TREE_CODE (t) == COMPONENT_REF)
13695 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13697 error_at (OMP_CLAUSE_LOCATION (c),
13698 "%qE is a member of a union", t);
13699 return error_mark_node;
13701 t = TREE_OPERAND (t, 0);
13702 while (TREE_CODE (t) == MEM_REF
13703 || INDIRECT_REF_P (t)
13704 || TREE_CODE (t) == ARRAY_REF)
13706 t = TREE_OPERAND (t, 0);
13707 STRIP_NOPS (t);
13708 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13709 t = TREE_OPERAND (t, 0);
13711 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13713 if (maybe_ne (mem_ref_offset (t), 0))
13714 error_at (OMP_CLAUSE_LOCATION (c),
13715 "cannot dereference %qE in %qs clause", t,
13716 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13717 else
13718 t = TREE_OPERAND (t, 0);
13722 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13724 if (DECL_P (t))
13725 error_at (OMP_CLAUSE_LOCATION (c),
13726 "%qD is not a variable in %qs clause", t,
13727 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13728 else
13729 error_at (OMP_CLAUSE_LOCATION (c),
13730 "%qE is not a variable in %qs clause", t,
13731 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13732 return error_mark_node;
13734 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13735 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13736 && TYPE_ATOMIC (TREE_TYPE (t)))
13738 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13739 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13740 return error_mark_node;
13742 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13743 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13744 && VAR_P (t)
13745 && DECL_THREAD_LOCAL_P (t))
13747 error_at (OMP_CLAUSE_LOCATION (c),
13748 "%qD is threadprivate variable in %qs clause", t,
13749 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13750 return error_mark_node;
13752 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13753 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13754 && TYPE_ATOMIC (TREE_TYPE (t))
13755 && POINTER_TYPE_P (TREE_TYPE (t)))
13757 /* If the array section is pointer based and the pointer
13758 itself is _Atomic qualified, we need to atomically load
13759 the pointer. */
13760 c_expr expr;
13761 memset (&expr, 0, sizeof (expr));
13762 expr.value = ret;
13763 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13764 expr, false, false);
13765 ret = expr.value;
13767 return ret;
13770 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13771 maybe_zero_len, first_non_one, ort);
13772 if (ret == error_mark_node || ret == NULL_TREE)
13773 return ret;
13775 type = TREE_TYPE (ret);
13776 low_bound = TREE_PURPOSE (t);
13777 length = TREE_VALUE (t);
13779 if (low_bound == error_mark_node || length == error_mark_node)
13780 return error_mark_node;
13782 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13784 error_at (OMP_CLAUSE_LOCATION (c),
13785 "low bound %qE of array section does not have integral type",
13786 low_bound);
13787 return error_mark_node;
13789 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13791 error_at (OMP_CLAUSE_LOCATION (c),
13792 "length %qE of array section does not have integral type",
13793 length);
13794 return error_mark_node;
13796 if (low_bound
13797 && TREE_CODE (low_bound) == INTEGER_CST
13798 && TYPE_PRECISION (TREE_TYPE (low_bound))
13799 > TYPE_PRECISION (sizetype))
13800 low_bound = fold_convert (sizetype, low_bound);
13801 if (length
13802 && TREE_CODE (length) == INTEGER_CST
13803 && TYPE_PRECISION (TREE_TYPE (length))
13804 > TYPE_PRECISION (sizetype))
13805 length = fold_convert (sizetype, length);
13806 if (low_bound == NULL_TREE)
13807 low_bound = integer_zero_node;
13808 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13809 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13810 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13812 if (length != integer_one_node)
13814 error_at (OMP_CLAUSE_LOCATION (c),
13815 "expected single pointer in %qs clause",
13816 user_omp_clause_code_name (c, ort == C_ORT_ACC));
13817 return error_mark_node;
13820 if (length != NULL_TREE)
13822 if (!integer_nonzerop (length))
13824 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13825 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13826 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13827 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13828 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13830 if (integer_zerop (length))
13832 error_at (OMP_CLAUSE_LOCATION (c),
13833 "zero length array section in %qs clause",
13834 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13835 return error_mark_node;
13838 else
13839 maybe_zero_len = true;
13841 if (first_non_one == types.length ()
13842 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13843 first_non_one++;
13845 if (TREE_CODE (type) == ARRAY_TYPE)
13847 if (length == NULL_TREE
13848 && (TYPE_DOMAIN (type) == NULL_TREE
13849 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13851 error_at (OMP_CLAUSE_LOCATION (c),
13852 "for unknown bound array type length expression must "
13853 "be specified");
13854 return error_mark_node;
13856 if (TREE_CODE (low_bound) == INTEGER_CST
13857 && tree_int_cst_sgn (low_bound) == -1)
13859 error_at (OMP_CLAUSE_LOCATION (c),
13860 "negative low bound in array section in %qs clause",
13861 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13862 return error_mark_node;
13864 if (length != NULL_TREE
13865 && TREE_CODE (length) == INTEGER_CST
13866 && tree_int_cst_sgn (length) == -1)
13868 error_at (OMP_CLAUSE_LOCATION (c),
13869 "negative length in array section in %qs clause",
13870 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13871 return error_mark_node;
13873 if (TYPE_DOMAIN (type)
13874 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13875 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13876 == INTEGER_CST)
13878 tree size
13879 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13880 size = size_binop (PLUS_EXPR, size, size_one_node);
13881 if (TREE_CODE (low_bound) == INTEGER_CST)
13883 if (tree_int_cst_lt (size, low_bound))
13885 error_at (OMP_CLAUSE_LOCATION (c),
13886 "low bound %qE above array section size "
13887 "in %qs clause", low_bound,
13888 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13889 return error_mark_node;
13891 if (tree_int_cst_equal (size, low_bound))
13893 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13894 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13895 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13896 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13897 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13899 error_at (OMP_CLAUSE_LOCATION (c),
13900 "zero length array section in %qs clause",
13901 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13902 return error_mark_node;
13904 maybe_zero_len = true;
13906 else if (length == NULL_TREE
13907 && first_non_one == types.length ()
13908 && tree_int_cst_equal
13909 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13910 low_bound))
13911 first_non_one++;
13913 else if (length == NULL_TREE)
13915 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13916 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13917 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13918 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13919 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13920 maybe_zero_len = true;
13921 if (first_non_one == types.length ())
13922 first_non_one++;
13924 if (length && TREE_CODE (length) == INTEGER_CST)
13926 if (tree_int_cst_lt (size, length))
13928 error_at (OMP_CLAUSE_LOCATION (c),
13929 "length %qE above array section size "
13930 "in %qs clause", length,
13931 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13932 return error_mark_node;
13934 if (TREE_CODE (low_bound) == INTEGER_CST)
13936 tree lbpluslen
13937 = size_binop (PLUS_EXPR,
13938 fold_convert (sizetype, low_bound),
13939 fold_convert (sizetype, length));
13940 if (TREE_CODE (lbpluslen) == INTEGER_CST
13941 && tree_int_cst_lt (size, lbpluslen))
13943 error_at (OMP_CLAUSE_LOCATION (c),
13944 "high bound %qE above array section size "
13945 "in %qs clause", lbpluslen,
13946 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13947 return error_mark_node;
13952 else if (length == NULL_TREE)
13954 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13955 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13956 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13957 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13958 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13959 maybe_zero_len = true;
13960 if (first_non_one == types.length ())
13961 first_non_one++;
13964 /* For [lb:] we will need to evaluate lb more than once. */
13965 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13967 tree lb = save_expr (low_bound);
13968 if (lb != low_bound)
13970 TREE_PURPOSE (t) = lb;
13971 low_bound = lb;
13975 else if (TREE_CODE (type) == POINTER_TYPE)
13977 if (length == NULL_TREE)
13979 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13980 error_at (OMP_CLAUSE_LOCATION (c),
13981 "for array function parameter length expression "
13982 "must be specified");
13983 else
13984 error_at (OMP_CLAUSE_LOCATION (c),
13985 "for pointer type length expression must be specified");
13986 return error_mark_node;
13988 if (length != NULL_TREE
13989 && TREE_CODE (length) == INTEGER_CST
13990 && tree_int_cst_sgn (length) == -1)
13992 error_at (OMP_CLAUSE_LOCATION (c),
13993 "negative length in array section in %qs clause",
13994 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13995 return error_mark_node;
13997 /* If there is a pointer type anywhere but in the very first
13998 array-section-subscript, the array section could be non-contiguous. */
13999 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
14000 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
14001 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
14003 /* If any prior dimension has a non-one length, then deem this
14004 array section as non-contiguous. */
14005 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
14006 d = TREE_CHAIN (d))
14008 tree d_length = TREE_VALUE (d);
14009 if (d_length == NULL_TREE || !integer_onep (d_length))
14011 error_at (OMP_CLAUSE_LOCATION (c),
14012 "array section is not contiguous in %qs clause",
14013 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14014 return error_mark_node;
14019 else
14021 error_at (OMP_CLAUSE_LOCATION (c),
14022 "%qE does not have pointer or array type", ret);
14023 return error_mark_node;
14025 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
14026 types.safe_push (TREE_TYPE (ret));
14027 /* We will need to evaluate lb more than once. */
14028 tree lb = save_expr (low_bound);
14029 if (lb != low_bound)
14031 TREE_PURPOSE (t) = lb;
14032 low_bound = lb;
14034 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
14035 return ret;
14038 /* Handle array sections for clause C. */
14040 static bool
14041 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
14043 bool maybe_zero_len = false;
14044 unsigned int first_non_one = 0;
14045 auto_vec<tree, 10> types;
14046 tree *tp = &OMP_CLAUSE_DECL (c);
14047 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14048 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14049 && TREE_CODE (*tp) == TREE_LIST
14050 && TREE_PURPOSE (*tp)
14051 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
14052 tp = &TREE_VALUE (*tp);
14053 tree first = handle_omp_array_sections_1 (c, *tp, types,
14054 maybe_zero_len, first_non_one,
14055 ort);
14056 if (first == error_mark_node)
14057 return true;
14058 if (first == NULL_TREE)
14059 return false;
14060 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14061 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14063 tree t = *tp;
14064 tree tem = NULL_TREE;
14065 /* Need to evaluate side effects in the length expressions
14066 if any. */
14067 while (TREE_CODE (t) == TREE_LIST)
14069 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
14071 if (tem == NULL_TREE)
14072 tem = TREE_VALUE (t);
14073 else
14074 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
14075 TREE_VALUE (t), tem);
14077 t = TREE_CHAIN (t);
14079 if (tem)
14080 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
14081 first = c_fully_fold (first, false, NULL, true);
14082 *tp = first;
14084 else
14086 unsigned int num = types.length (), i;
14087 tree t, side_effects = NULL_TREE, size = NULL_TREE;
14088 tree condition = NULL_TREE;
14090 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
14091 maybe_zero_len = true;
14093 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
14094 t = TREE_CHAIN (t))
14096 tree low_bound = TREE_PURPOSE (t);
14097 tree length = TREE_VALUE (t);
14099 i--;
14100 if (low_bound
14101 && TREE_CODE (low_bound) == INTEGER_CST
14102 && TYPE_PRECISION (TREE_TYPE (low_bound))
14103 > TYPE_PRECISION (sizetype))
14104 low_bound = fold_convert (sizetype, low_bound);
14105 if (length
14106 && TREE_CODE (length) == INTEGER_CST
14107 && TYPE_PRECISION (TREE_TYPE (length))
14108 > TYPE_PRECISION (sizetype))
14109 length = fold_convert (sizetype, length);
14110 if (low_bound == NULL_TREE)
14111 low_bound = integer_zero_node;
14112 if (!maybe_zero_len && i > first_non_one)
14114 if (integer_nonzerop (low_bound))
14115 goto do_warn_noncontiguous;
14116 if (length != NULL_TREE
14117 && TREE_CODE (length) == INTEGER_CST
14118 && TYPE_DOMAIN (types[i])
14119 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
14120 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
14121 == INTEGER_CST)
14123 tree size;
14124 size = size_binop (PLUS_EXPR,
14125 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14126 size_one_node);
14127 if (!tree_int_cst_equal (length, size))
14129 do_warn_noncontiguous:
14130 error_at (OMP_CLAUSE_LOCATION (c),
14131 "array section is not contiguous in %qs "
14132 "clause",
14133 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14134 return true;
14137 if (length != NULL_TREE
14138 && TREE_SIDE_EFFECTS (length))
14140 if (side_effects == NULL_TREE)
14141 side_effects = length;
14142 else
14143 side_effects = build2 (COMPOUND_EXPR,
14144 TREE_TYPE (side_effects),
14145 length, side_effects);
14148 else
14150 tree l;
14152 if (i > first_non_one
14153 && ((length && integer_nonzerop (length))
14154 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14155 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14156 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
14157 continue;
14158 if (length)
14159 l = fold_convert (sizetype, length);
14160 else
14162 l = size_binop (PLUS_EXPR,
14163 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14164 size_one_node);
14165 l = size_binop (MINUS_EXPR, l,
14166 fold_convert (sizetype, low_bound));
14168 if (i > first_non_one)
14170 l = fold_build2 (NE_EXPR, boolean_type_node, l,
14171 size_zero_node);
14172 if (condition == NULL_TREE)
14173 condition = l;
14174 else
14175 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
14176 l, condition);
14178 else if (size == NULL_TREE)
14180 size = size_in_bytes (TREE_TYPE (types[i]));
14181 tree eltype = TREE_TYPE (types[num - 1]);
14182 while (TREE_CODE (eltype) == ARRAY_TYPE)
14183 eltype = TREE_TYPE (eltype);
14184 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14185 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14186 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14188 if (integer_zerop (size)
14189 || integer_zerop (size_in_bytes (eltype)))
14191 error_at (OMP_CLAUSE_LOCATION (c),
14192 "zero length array section in %qs clause",
14193 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14194 return error_mark_node;
14196 size = size_binop (EXACT_DIV_EXPR, size,
14197 size_in_bytes (eltype));
14199 size = size_binop (MULT_EXPR, size, l);
14200 if (condition)
14201 size = fold_build3 (COND_EXPR, sizetype, condition,
14202 size, size_zero_node);
14204 else
14205 size = size_binop (MULT_EXPR, size, l);
14208 if (side_effects)
14209 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
14210 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14211 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14212 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14214 size = size_binop (MINUS_EXPR, size, size_one_node);
14215 size = c_fully_fold (size, false, NULL);
14216 size = save_expr (size);
14217 tree index_type = build_index_type (size);
14218 tree eltype = TREE_TYPE (first);
14219 while (TREE_CODE (eltype) == ARRAY_TYPE)
14220 eltype = TREE_TYPE (eltype);
14221 tree type = build_array_type (eltype, index_type);
14222 tree ptype = build_pointer_type (eltype);
14223 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14224 t = build_fold_addr_expr (t);
14225 tree t2 = build_fold_addr_expr (first);
14226 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14227 ptrdiff_type_node, t2);
14228 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14229 ptrdiff_type_node, t2,
14230 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14231 ptrdiff_type_node, t));
14232 t2 = c_fully_fold (t2, false, NULL);
14233 if (tree_fits_shwi_p (t2))
14234 t = build2 (MEM_REF, type, t,
14235 build_int_cst (ptype, tree_to_shwi (t2)));
14236 else
14238 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
14239 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
14240 TREE_TYPE (t), t, t2);
14241 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
14243 OMP_CLAUSE_DECL (c) = t;
14244 return false;
14246 first = c_fully_fold (first, false, NULL);
14247 OMP_CLAUSE_DECL (c) = first;
14248 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
14249 return false;
14250 if (size)
14251 size = c_fully_fold (size, false, NULL);
14252 OMP_CLAUSE_SIZE (c) = size;
14253 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14254 || (TREE_CODE (t) == COMPONENT_REF
14255 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
14256 return false;
14257 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
14258 switch (OMP_CLAUSE_MAP_KIND (c))
14260 case GOMP_MAP_ALLOC:
14261 case GOMP_MAP_IF_PRESENT:
14262 case GOMP_MAP_TO:
14263 case GOMP_MAP_FROM:
14264 case GOMP_MAP_TOFROM:
14265 case GOMP_MAP_ALWAYS_TO:
14266 case GOMP_MAP_ALWAYS_FROM:
14267 case GOMP_MAP_ALWAYS_TOFROM:
14268 case GOMP_MAP_RELEASE:
14269 case GOMP_MAP_DELETE:
14270 case GOMP_MAP_FORCE_TO:
14271 case GOMP_MAP_FORCE_FROM:
14272 case GOMP_MAP_FORCE_TOFROM:
14273 case GOMP_MAP_FORCE_PRESENT:
14274 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
14275 break;
14276 default:
14277 break;
14279 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
14280 if (TREE_CODE (t) == COMPONENT_REF)
14281 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
14282 else
14283 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
14284 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
14285 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
14286 && !c_mark_addressable (t))
14287 return false;
14288 OMP_CLAUSE_DECL (c2) = t;
14289 t = build_fold_addr_expr (first);
14290 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
14291 tree ptr = OMP_CLAUSE_DECL (c2);
14292 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
14293 ptr = build_fold_addr_expr (ptr);
14294 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14295 ptrdiff_type_node, t,
14296 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14297 ptrdiff_type_node, ptr));
14298 t = c_fully_fold (t, false, NULL);
14299 OMP_CLAUSE_SIZE (c2) = t;
14300 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
14301 OMP_CLAUSE_CHAIN (c) = c2;
14303 return false;
14306 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
14307 an inline call. But, remap
14308 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
14309 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
14311 static tree
14312 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
14313 tree decl, tree placeholder)
14315 copy_body_data id;
14316 hash_map<tree, tree> decl_map;
14318 decl_map.put (omp_decl1, placeholder);
14319 decl_map.put (omp_decl2, decl);
14320 memset (&id, 0, sizeof (id));
14321 id.src_fn = DECL_CONTEXT (omp_decl1);
14322 id.dst_fn = current_function_decl;
14323 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
14324 id.decl_map = &decl_map;
14326 id.copy_decl = copy_decl_no_change;
14327 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
14328 id.transform_new_cfg = true;
14329 id.transform_return_to_modify = false;
14330 id.eh_lp_nr = 0;
14331 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
14332 return stmt;
14335 /* Helper function of c_finish_omp_clauses, called via walk_tree.
14336 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
14338 static tree
14339 c_find_omp_placeholder_r (tree *tp, int *, void *data)
14341 if (*tp == (tree) data)
14342 return *tp;
14343 return NULL_TREE;
14346 /* Similarly, but also walk aggregate fields. */
14348 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
14350 static tree
14351 c_find_omp_var_r (tree *tp, int *, void *data)
14353 if (*tp == ((struct c_find_omp_var_s *) data)->var)
14354 return *tp;
14355 if (RECORD_OR_UNION_TYPE_P (*tp))
14357 tree field;
14358 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
14360 for (field = TYPE_FIELDS (*tp); field;
14361 field = DECL_CHAIN (field))
14362 if (TREE_CODE (field) == FIELD_DECL)
14364 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
14365 c_find_omp_var_r, data, pset);
14366 if (ret)
14367 return ret;
14368 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
14369 if (ret)
14370 return ret;
14371 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
14372 pset);
14373 if (ret)
14374 return ret;
14375 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
14376 if (ret)
14377 return ret;
14380 else if (INTEGRAL_TYPE_P (*tp))
14381 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
14382 ((struct c_find_omp_var_s *) data)->pset);
14383 return NULL_TREE;
14386 /* Finish OpenMP iterators ITER. Return true if they are errorneous
14387 and clauses containing them should be removed. */
14389 static bool
14390 c_omp_finish_iterators (tree iter)
14392 bool ret = false;
14393 for (tree it = iter; it; it = TREE_CHAIN (it))
14395 tree var = TREE_VEC_ELT (it, 0);
14396 tree begin = TREE_VEC_ELT (it, 1);
14397 tree end = TREE_VEC_ELT (it, 2);
14398 tree step = TREE_VEC_ELT (it, 3);
14399 tree orig_step;
14400 tree type = TREE_TYPE (var);
14401 location_t loc = DECL_SOURCE_LOCATION (var);
14402 if (type == error_mark_node)
14404 ret = true;
14405 continue;
14407 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14409 error_at (loc, "iterator %qD has neither integral nor pointer type",
14410 var);
14411 ret = true;
14412 continue;
14414 else if (TYPE_ATOMIC (type))
14416 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14417 ret = true;
14418 continue;
14420 else if (TYPE_READONLY (type))
14422 error_at (loc, "iterator %qD has const qualified type", var);
14423 ret = true;
14424 continue;
14426 else if (step == error_mark_node
14427 || TREE_TYPE (step) == error_mark_node)
14429 ret = true;
14430 continue;
14432 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14434 error_at (EXPR_LOC_OR_LOC (step, loc),
14435 "iterator step with non-integral type");
14436 ret = true;
14437 continue;
14439 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14440 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14441 orig_step = save_expr (c_fully_fold (step, false, NULL));
14442 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14443 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14444 if (POINTER_TYPE_P (type))
14446 begin = save_expr (begin);
14447 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14448 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14449 fold_convert (sizetype, step),
14450 fold_convert (sizetype, begin));
14451 step = fold_convert (ssizetype, step);
14453 if (integer_zerop (step))
14455 error_at (loc, "iterator %qD has zero step", var);
14456 ret = true;
14457 continue;
14460 if (begin == error_mark_node
14461 || end == error_mark_node
14462 || step == error_mark_node
14463 || orig_step == error_mark_node)
14465 ret = true;
14466 continue;
14468 hash_set<tree> pset;
14469 tree it2;
14470 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14472 tree var2 = TREE_VEC_ELT (it2, 0);
14473 tree begin2 = TREE_VEC_ELT (it2, 1);
14474 tree end2 = TREE_VEC_ELT (it2, 2);
14475 tree step2 = TREE_VEC_ELT (it2, 3);
14476 tree type2 = TREE_TYPE (var2);
14477 location_t loc2 = DECL_SOURCE_LOCATION (var2);
14478 struct c_find_omp_var_s data = { var, &pset };
14479 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14481 error_at (loc2,
14482 "type of iterator %qD refers to outer iterator %qD",
14483 var2, var);
14484 break;
14486 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14488 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14489 "begin expression refers to outer iterator %qD", var);
14490 break;
14492 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14494 error_at (EXPR_LOC_OR_LOC (end2, loc2),
14495 "end expression refers to outer iterator %qD", var);
14496 break;
14498 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14500 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14501 "step expression refers to outer iterator %qD", var);
14502 break;
14505 if (it2)
14507 ret = true;
14508 continue;
14510 TREE_VEC_ELT (it, 1) = begin;
14511 TREE_VEC_ELT (it, 2) = end;
14512 TREE_VEC_ELT (it, 3) = step;
14513 TREE_VEC_ELT (it, 4) = orig_step;
14515 return ret;
14518 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14519 Return true if an error has been detected. */
14521 static bool
14522 c_oacc_check_attachments (tree c)
14524 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14525 return false;
14527 /* OpenACC attach / detach clauses must be pointers. */
14528 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14529 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14531 tree t = OMP_CLAUSE_DECL (c);
14533 while (TREE_CODE (t) == TREE_LIST)
14534 t = TREE_CHAIN (t);
14536 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14538 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14539 user_omp_clause_code_name (c, true));
14540 return true;
14544 return false;
14547 /* For all elements of CLAUSES, validate them against their constraints.
14548 Remove any elements from the list that are invalid. */
14550 tree
14551 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14553 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14554 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14555 bitmap_head oacc_reduction_head, is_on_device_head;
14556 tree c, t, type, *pc;
14557 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14558 bool branch_seen = false;
14559 bool copyprivate_seen = false;
14560 bool mergeable_seen = false;
14561 tree *detach_seen = NULL;
14562 bool linear_variable_step_check = false;
14563 tree *nowait_clause = NULL;
14564 tree ordered_clause = NULL_TREE;
14565 tree schedule_clause = NULL_TREE;
14566 bool oacc_async = false;
14567 bool indir_component_ref_p = false;
14568 tree last_iterators = NULL_TREE;
14569 bool last_iterators_remove = false;
14570 tree *nogroup_seen = NULL;
14571 tree *order_clause = NULL;
14572 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14573 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14574 int reduction_seen = 0;
14575 bool allocate_seen = false;
14576 bool implicit_moved = false;
14577 bool target_in_reduction_seen = false;
14579 bitmap_obstack_initialize (NULL);
14580 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14581 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14582 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14583 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14584 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14585 bitmap_initialize (&map_head, &bitmap_default_obstack);
14586 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14587 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14588 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14589 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14590 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14591 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14593 if (ort & C_ORT_ACC)
14594 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14595 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14597 oacc_async = true;
14598 break;
14601 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
14603 for (pc = &clauses, c = clauses; c ; c = *pc)
14605 bool remove = false;
14606 bool need_complete = false;
14607 bool need_implicitly_determined = false;
14609 /* We've reached the end of a list of expanded nodes. Reset the group
14610 start pointer. */
14611 if (c == grp_sentinel)
14612 grp_start_p = NULL;
14614 switch (OMP_CLAUSE_CODE (c))
14616 case OMP_CLAUSE_SHARED:
14617 need_implicitly_determined = true;
14618 goto check_dup_generic;
14620 case OMP_CLAUSE_PRIVATE:
14621 need_complete = true;
14622 need_implicitly_determined = true;
14623 goto check_dup_generic;
14625 case OMP_CLAUSE_REDUCTION:
14626 if (reduction_seen == 0)
14627 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14628 else if (reduction_seen != -2
14629 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14630 ? -1 : 1))
14632 error_at (OMP_CLAUSE_LOCATION (c),
14633 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14634 "on the same construct");
14635 reduction_seen = -2;
14637 /* FALLTHRU */
14638 case OMP_CLAUSE_IN_REDUCTION:
14639 case OMP_CLAUSE_TASK_REDUCTION:
14640 need_implicitly_determined = true;
14641 t = OMP_CLAUSE_DECL (c);
14642 if (TREE_CODE (t) == TREE_LIST)
14644 if (handle_omp_array_sections (c, ort))
14646 remove = true;
14647 break;
14650 t = OMP_CLAUSE_DECL (c);
14651 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14652 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14654 error_at (OMP_CLAUSE_LOCATION (c),
14655 "%<inscan%> %<reduction%> clause with array "
14656 "section");
14657 remove = true;
14658 break;
14661 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14662 if (t == error_mark_node)
14664 remove = true;
14665 break;
14667 if (oacc_async)
14668 c_mark_addressable (t);
14669 type = TREE_TYPE (t);
14670 if (TREE_CODE (t) == MEM_REF)
14671 type = TREE_TYPE (type);
14672 if (TREE_CODE (type) == ARRAY_TYPE)
14674 tree oatype = type;
14675 gcc_assert (TREE_CODE (t) != MEM_REF);
14676 while (TREE_CODE (type) == ARRAY_TYPE)
14677 type = TREE_TYPE (type);
14678 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14680 error_at (OMP_CLAUSE_LOCATION (c),
14681 "%qD in %<reduction%> clause is a zero size array",
14683 remove = true;
14684 break;
14686 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14687 TYPE_SIZE_UNIT (type));
14688 if (integer_zerop (size))
14690 error_at (OMP_CLAUSE_LOCATION (c),
14691 "%qD in %<reduction%> clause is a zero size array",
14693 remove = true;
14694 break;
14696 size = size_binop (MINUS_EXPR, size, size_one_node);
14697 size = save_expr (size);
14698 tree index_type = build_index_type (size);
14699 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14700 index_type);
14701 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14702 tree ptype = build_pointer_type (type);
14703 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14704 t = build_fold_addr_expr (t);
14705 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14706 OMP_CLAUSE_DECL (c) = t;
14708 if (TYPE_ATOMIC (type))
14710 error_at (OMP_CLAUSE_LOCATION (c),
14711 "%<_Atomic%> %qE in %<reduction%> clause", t);
14712 remove = true;
14713 break;
14715 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14716 || OMP_CLAUSE_REDUCTION_TASK (c))
14718 /* Disallow zero sized or potentially zero sized task
14719 reductions. */
14720 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14722 error_at (OMP_CLAUSE_LOCATION (c),
14723 "zero sized type %qT in %qs clause", type,
14724 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14725 remove = true;
14726 break;
14728 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14730 error_at (OMP_CLAUSE_LOCATION (c),
14731 "variable sized type %qT in %qs clause", type,
14732 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14733 remove = true;
14734 break;
14737 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14738 && (FLOAT_TYPE_P (type)
14739 || TREE_CODE (type) == COMPLEX_TYPE))
14741 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14742 const char *r_name = NULL;
14744 switch (r_code)
14746 case PLUS_EXPR:
14747 case MULT_EXPR:
14748 case MINUS_EXPR:
14749 case TRUTH_ANDIF_EXPR:
14750 case TRUTH_ORIF_EXPR:
14751 break;
14752 case MIN_EXPR:
14753 if (TREE_CODE (type) == COMPLEX_TYPE)
14754 r_name = "min";
14755 break;
14756 case MAX_EXPR:
14757 if (TREE_CODE (type) == COMPLEX_TYPE)
14758 r_name = "max";
14759 break;
14760 case BIT_AND_EXPR:
14761 r_name = "&";
14762 break;
14763 case BIT_XOR_EXPR:
14764 r_name = "^";
14765 break;
14766 case BIT_IOR_EXPR:
14767 r_name = "|";
14768 break;
14769 default:
14770 gcc_unreachable ();
14772 if (r_name)
14774 error_at (OMP_CLAUSE_LOCATION (c),
14775 "%qE has invalid type for %<reduction(%s)%>",
14776 t, r_name);
14777 remove = true;
14778 break;
14781 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14783 error_at (OMP_CLAUSE_LOCATION (c),
14784 "user defined reduction not found for %qE", t);
14785 remove = true;
14786 break;
14788 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14790 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14791 type = TYPE_MAIN_VARIANT (type);
14792 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14793 VAR_DECL, NULL_TREE, type);
14794 tree decl_placeholder = NULL_TREE;
14795 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14796 DECL_ARTIFICIAL (placeholder) = 1;
14797 DECL_IGNORED_P (placeholder) = 1;
14798 if (TREE_CODE (t) == MEM_REF)
14800 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14801 VAR_DECL, NULL_TREE, type);
14802 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14803 DECL_ARTIFICIAL (decl_placeholder) = 1;
14804 DECL_IGNORED_P (decl_placeholder) = 1;
14806 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14807 c_mark_addressable (placeholder);
14808 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14809 c_mark_addressable (decl_placeholder ? decl_placeholder
14810 : OMP_CLAUSE_DECL (c));
14811 OMP_CLAUSE_REDUCTION_MERGE (c)
14812 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14813 TREE_VEC_ELT (list, 0),
14814 TREE_VEC_ELT (list, 1),
14815 decl_placeholder ? decl_placeholder
14816 : OMP_CLAUSE_DECL (c), placeholder);
14817 OMP_CLAUSE_REDUCTION_MERGE (c)
14818 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14819 void_type_node, NULL_TREE,
14820 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14821 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14822 if (TREE_VEC_LENGTH (list) == 6)
14824 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14825 c_mark_addressable (decl_placeholder ? decl_placeholder
14826 : OMP_CLAUSE_DECL (c));
14827 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14828 c_mark_addressable (placeholder);
14829 tree init = TREE_VEC_ELT (list, 5);
14830 if (init == error_mark_node)
14831 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14832 OMP_CLAUSE_REDUCTION_INIT (c)
14833 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14834 TREE_VEC_ELT (list, 3),
14835 decl_placeholder ? decl_placeholder
14836 : OMP_CLAUSE_DECL (c), placeholder);
14837 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14839 tree v = decl_placeholder ? decl_placeholder : t;
14840 OMP_CLAUSE_REDUCTION_INIT (c)
14841 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14842 OMP_CLAUSE_REDUCTION_INIT (c));
14844 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14845 c_find_omp_placeholder_r,
14846 placeholder, NULL))
14847 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14849 else
14851 tree init;
14852 tree v = decl_placeholder ? decl_placeholder : t;
14853 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14854 init = build_constructor (TREE_TYPE (v), NULL);
14855 else
14856 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14857 OMP_CLAUSE_REDUCTION_INIT (c)
14858 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14860 OMP_CLAUSE_REDUCTION_INIT (c)
14861 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14862 void_type_node, NULL_TREE,
14863 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14864 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14866 if (TREE_CODE (t) == MEM_REF)
14868 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14869 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14870 != INTEGER_CST)
14872 sorry ("variable length element type in array "
14873 "%<reduction%> clause");
14874 remove = true;
14875 break;
14877 t = TREE_OPERAND (t, 0);
14878 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14879 t = TREE_OPERAND (t, 0);
14880 if (TREE_CODE (t) == ADDR_EXPR)
14881 t = TREE_OPERAND (t, 0);
14883 goto check_dup_generic_t;
14885 case OMP_CLAUSE_COPYPRIVATE:
14886 copyprivate_seen = true;
14887 if (nowait_clause)
14889 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14890 "%<nowait%> clause must not be used together "
14891 "with %<copyprivate%>");
14892 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14893 nowait_clause = NULL;
14895 goto check_dup_generic;
14897 case OMP_CLAUSE_COPYIN:
14898 t = OMP_CLAUSE_DECL (c);
14899 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14901 error_at (OMP_CLAUSE_LOCATION (c),
14902 "%qE must be %<threadprivate%> for %<copyin%>", t);
14903 remove = true;
14904 break;
14906 goto check_dup_generic;
14908 case OMP_CLAUSE_LINEAR:
14909 if (ort != C_ORT_OMP_DECLARE_SIMD)
14910 need_implicitly_determined = true;
14911 t = OMP_CLAUSE_DECL (c);
14912 if (ort != C_ORT_OMP_DECLARE_SIMD
14913 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14914 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14916 error_at (OMP_CLAUSE_LOCATION (c),
14917 "modifier should not be specified in %<linear%> "
14918 "clause on %<simd%> or %<for%> constructs when not "
14919 "using OpenMP 5.2 modifiers");
14920 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14922 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14923 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14925 error_at (OMP_CLAUSE_LOCATION (c),
14926 "linear clause applied to non-integral non-pointer "
14927 "variable with type %qT", TREE_TYPE (t));
14928 remove = true;
14929 break;
14931 if (TYPE_ATOMIC (TREE_TYPE (t)))
14933 error_at (OMP_CLAUSE_LOCATION (c),
14934 "%<_Atomic%> %qD in %<linear%> clause", t);
14935 remove = true;
14936 break;
14938 if (ort == C_ORT_OMP_DECLARE_SIMD)
14940 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14941 if (TREE_CODE (s) == PARM_DECL)
14943 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14944 /* map_head bitmap is used as uniform_head if
14945 declare_simd. */
14946 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14947 linear_variable_step_check = true;
14948 goto check_dup_generic;
14950 if (TREE_CODE (s) != INTEGER_CST)
14952 error_at (OMP_CLAUSE_LOCATION (c),
14953 "%<linear%> clause step %qE is neither constant "
14954 "nor a parameter", s);
14955 remove = true;
14956 break;
14959 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14961 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14962 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14963 OMP_CLAUSE_DECL (c), s);
14964 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14965 sizetype, fold_convert (sizetype, s),
14966 fold_convert
14967 (sizetype, OMP_CLAUSE_DECL (c)));
14968 if (s == error_mark_node)
14969 s = size_one_node;
14970 OMP_CLAUSE_LINEAR_STEP (c) = s;
14972 else
14973 OMP_CLAUSE_LINEAR_STEP (c)
14974 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14975 goto check_dup_generic;
14977 check_dup_generic:
14978 t = OMP_CLAUSE_DECL (c);
14979 check_dup_generic_t:
14980 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14982 error_at (OMP_CLAUSE_LOCATION (c),
14983 "%qE is not a variable in clause %qs", t,
14984 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14985 remove = true;
14987 else if ((ort == C_ORT_ACC
14988 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14989 || (ort == C_ORT_OMP
14990 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14991 || (OMP_CLAUSE_CODE (c)
14992 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14993 || (ort == C_ORT_OMP_TARGET
14994 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14996 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14997 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14998 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
15000 error_at (OMP_CLAUSE_LOCATION (c),
15001 "%qD appears more than once in data-sharing "
15002 "clauses", t);
15003 remove = true;
15004 break;
15006 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
15007 target_in_reduction_seen = true;
15008 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15010 error_at (OMP_CLAUSE_LOCATION (c),
15011 ort == C_ORT_ACC
15012 ? "%qD appears more than once in reduction clauses"
15013 : "%qD appears more than once in data clauses",
15015 remove = true;
15017 else
15018 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
15020 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15021 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15022 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
15023 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15025 error_at (OMP_CLAUSE_LOCATION (c),
15026 "%qE appears more than once in data clauses", t);
15027 remove = true;
15029 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15030 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
15031 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15032 && bitmap_bit_p (&map_head, DECL_UID (t)))
15034 if (ort == C_ORT_ACC)
15035 error_at (OMP_CLAUSE_LOCATION (c),
15036 "%qD appears more than once in data clauses", t);
15037 else
15038 error_at (OMP_CLAUSE_LOCATION (c),
15039 "%qD appears both in data and map clauses", t);
15040 remove = true;
15042 else
15043 bitmap_set_bit (&generic_head, DECL_UID (t));
15044 break;
15046 case OMP_CLAUSE_FIRSTPRIVATE:
15047 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
15049 move_implicit:
15050 implicit_moved = true;
15051 /* Move firstprivate and map clauses with
15052 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
15053 clauses chain. */
15054 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
15055 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
15056 while (*pc1)
15057 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
15058 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
15060 *pc3 = *pc1;
15061 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
15062 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15064 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
15065 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
15067 *pc2 = *pc1;
15068 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
15069 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15071 else
15072 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
15073 *pc3 = NULL;
15074 *pc2 = cl2;
15075 *pc1 = cl1;
15076 continue;
15078 t = OMP_CLAUSE_DECL (c);
15079 need_complete = true;
15080 need_implicitly_determined = true;
15081 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15083 error_at (OMP_CLAUSE_LOCATION (c),
15084 "%qE is not a variable in clause %<firstprivate%>", t);
15085 remove = true;
15087 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15088 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
15089 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15090 remove = true;
15091 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15092 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15093 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15095 error_at (OMP_CLAUSE_LOCATION (c),
15096 "%qE appears more than once in data clauses", t);
15097 remove = true;
15099 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
15101 if (ort == C_ORT_ACC)
15102 error_at (OMP_CLAUSE_LOCATION (c),
15103 "%qD appears more than once in data clauses", t);
15104 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15105 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
15106 /* Silently drop the clause. */;
15107 else
15108 error_at (OMP_CLAUSE_LOCATION (c),
15109 "%qD appears both in data and map clauses", t);
15110 remove = true;
15112 else
15113 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
15114 break;
15116 case OMP_CLAUSE_LASTPRIVATE:
15117 t = OMP_CLAUSE_DECL (c);
15118 need_complete = true;
15119 need_implicitly_determined = true;
15120 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15122 error_at (OMP_CLAUSE_LOCATION (c),
15123 "%qE is not a variable in clause %<lastprivate%>", t);
15124 remove = true;
15126 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15127 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
15129 error_at (OMP_CLAUSE_LOCATION (c),
15130 "%qE appears more than once in data clauses", t);
15131 remove = true;
15133 else
15134 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
15135 break;
15137 case OMP_CLAUSE_ALIGNED:
15138 t = OMP_CLAUSE_DECL (c);
15139 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15141 error_at (OMP_CLAUSE_LOCATION (c),
15142 "%qE is not a variable in %<aligned%> clause", t);
15143 remove = true;
15145 else if (!POINTER_TYPE_P (TREE_TYPE (t))
15146 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15148 error_at (OMP_CLAUSE_LOCATION (c),
15149 "%qE in %<aligned%> clause is neither a pointer nor "
15150 "an array", t);
15151 remove = true;
15153 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15155 error_at (OMP_CLAUSE_LOCATION (c),
15156 "%<_Atomic%> %qD in %<aligned%> clause", t);
15157 remove = true;
15158 break;
15160 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15162 error_at (OMP_CLAUSE_LOCATION (c),
15163 "%qE appears more than once in %<aligned%> clauses",
15165 remove = true;
15167 else
15168 bitmap_set_bit (&aligned_head, DECL_UID (t));
15169 break;
15171 case OMP_CLAUSE_NONTEMPORAL:
15172 t = OMP_CLAUSE_DECL (c);
15173 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15175 error_at (OMP_CLAUSE_LOCATION (c),
15176 "%qE is not a variable in %<nontemporal%> clause", t);
15177 remove = true;
15179 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15181 error_at (OMP_CLAUSE_LOCATION (c),
15182 "%qE appears more than once in %<nontemporal%> "
15183 "clauses", t);
15184 remove = true;
15186 else
15187 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
15188 break;
15190 case OMP_CLAUSE_ALLOCATE:
15191 t = OMP_CLAUSE_DECL (c);
15192 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15194 error_at (OMP_CLAUSE_LOCATION (c),
15195 "%qE is not a variable in %<allocate%> clause", t);
15196 remove = true;
15198 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15200 warning_at (OMP_CLAUSE_LOCATION (c), 0,
15201 "%qE appears more than once in %<allocate%> clauses",
15203 remove = true;
15205 else
15207 bitmap_set_bit (&aligned_head, DECL_UID (t));
15208 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
15209 allocate_seen = true;
15211 break;
15213 case OMP_CLAUSE_DOACROSS:
15214 t = OMP_CLAUSE_DECL (c);
15215 if (t == NULL_TREE)
15216 break;
15217 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
15219 gcc_assert (TREE_CODE (t) == TREE_LIST);
15220 for (; t; t = TREE_CHAIN (t))
15222 tree decl = TREE_VALUE (t);
15223 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15225 tree offset = TREE_PURPOSE (t);
15226 bool neg = wi::neg_p (wi::to_wide (offset));
15227 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
15228 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
15229 neg ? MINUS_EXPR : PLUS_EXPR,
15230 decl, offset);
15231 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
15232 sizetype,
15233 fold_convert (sizetype, t2),
15234 fold_convert (sizetype, decl));
15235 if (t2 == error_mark_node)
15237 remove = true;
15238 break;
15240 TREE_PURPOSE (t) = t2;
15243 break;
15245 gcc_unreachable ();
15246 case OMP_CLAUSE_DEPEND:
15247 case OMP_CLAUSE_AFFINITY:
15248 t = OMP_CLAUSE_DECL (c);
15249 if (TREE_CODE (t) == TREE_LIST
15250 && TREE_PURPOSE (t)
15251 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
15253 if (TREE_PURPOSE (t) != last_iterators)
15254 last_iterators_remove
15255 = c_omp_finish_iterators (TREE_PURPOSE (t));
15256 last_iterators = TREE_PURPOSE (t);
15257 t = TREE_VALUE (t);
15258 if (last_iterators_remove)
15259 t = error_mark_node;
15261 else
15262 last_iterators = NULL_TREE;
15263 if (TREE_CODE (t) == TREE_LIST)
15265 if (handle_omp_array_sections (c, ort))
15266 remove = true;
15267 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15268 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15270 error_at (OMP_CLAUSE_LOCATION (c),
15271 "%<depend%> clause with %<depobj%> dependence "
15272 "type on array section");
15273 remove = true;
15275 break;
15277 if (t == error_mark_node)
15278 remove = true;
15279 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15280 && t == ridpointers[RID_OMP_ALL_MEMORY])
15282 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
15283 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
15285 error_at (OMP_CLAUSE_LOCATION (c),
15286 "%<omp_all_memory%> used with %<depend%> kind "
15287 "other than %<out%> or %<inout%>");
15288 remove = true;
15291 else if (!lvalue_p (t))
15293 error_at (OMP_CLAUSE_LOCATION (c),
15294 "%qE is not lvalue expression nor array section in "
15295 "%qs clause", t,
15296 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15297 remove = true;
15299 else if (TREE_CODE (t) == COMPONENT_REF
15300 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
15302 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15303 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
15304 error_at (OMP_CLAUSE_LOCATION (c),
15305 "bit-field %qE in %qs clause", t,
15306 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15307 remove = true;
15309 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15310 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15312 if (!c_omp_depend_t_p (TREE_TYPE (t)))
15314 error_at (OMP_CLAUSE_LOCATION (c),
15315 "%qE does not have %<omp_depend_t%> type in "
15316 "%<depend%> clause with %<depobj%> dependence "
15317 "type", t);
15318 remove = true;
15321 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15322 && c_omp_depend_t_p (TREE_TYPE (t)))
15324 error_at (OMP_CLAUSE_LOCATION (c),
15325 "%qE should not have %<omp_depend_t%> type in "
15326 "%<depend%> clause with dependence type other than "
15327 "%<depobj%>", t);
15328 remove = true;
15330 if (!remove)
15332 if (t == ridpointers[RID_OMP_ALL_MEMORY])
15333 t = null_pointer_node;
15334 else
15336 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
15337 ADDR_EXPR, t, false);
15338 if (addr == error_mark_node)
15340 remove = true;
15341 break;
15343 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
15344 RO_UNARY_STAR);
15345 if (t == error_mark_node)
15347 remove = true;
15348 break;
15351 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
15352 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
15353 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
15354 == TREE_VEC))
15355 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
15356 else
15357 OMP_CLAUSE_DECL (c) = t;
15359 break;
15361 case OMP_CLAUSE_MAP:
15362 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
15363 goto move_implicit;
15364 /* FALLTHRU */
15365 case OMP_CLAUSE_TO:
15366 case OMP_CLAUSE_FROM:
15367 case OMP_CLAUSE__CACHE_:
15368 t = OMP_CLAUSE_DECL (c);
15369 if (TREE_CODE (t) == TREE_LIST)
15371 grp_start_p = pc;
15372 grp_sentinel = OMP_CLAUSE_CHAIN (c);
15374 if (handle_omp_array_sections (c, ort))
15375 remove = true;
15376 else
15378 t = OMP_CLAUSE_DECL (c);
15379 if (!omp_mappable_type (TREE_TYPE (t)))
15381 error_at (OMP_CLAUSE_LOCATION (c),
15382 "array section does not have mappable type "
15383 "in %qs clause",
15384 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15385 remove = true;
15387 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15389 error_at (OMP_CLAUSE_LOCATION (c),
15390 "%<_Atomic%> %qE in %qs clause", t,
15391 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15392 remove = true;
15394 while (TREE_CODE (t) == ARRAY_REF)
15395 t = TREE_OPERAND (t, 0);
15396 if (TREE_CODE (t) == COMPONENT_REF
15397 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
15401 t = TREE_OPERAND (t, 0);
15402 if (TREE_CODE (t) == MEM_REF
15403 || INDIRECT_REF_P (t))
15405 t = TREE_OPERAND (t, 0);
15406 STRIP_NOPS (t);
15407 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15408 t = TREE_OPERAND (t, 0);
15411 while (TREE_CODE (t) == COMPONENT_REF
15412 || TREE_CODE (t) == ARRAY_REF);
15414 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15415 && OMP_CLAUSE_MAP_IMPLICIT (c)
15416 && (bitmap_bit_p (&map_head, DECL_UID (t))
15417 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15418 || bitmap_bit_p (&map_firstprivate_head,
15419 DECL_UID (t))))
15421 remove = true;
15422 break;
15424 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
15425 break;
15426 if (bitmap_bit_p (&map_head, DECL_UID (t)))
15428 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15429 error_at (OMP_CLAUSE_LOCATION (c),
15430 "%qD appears more than once in motion "
15431 "clauses", t);
15432 else if (ort == C_ORT_ACC)
15433 error_at (OMP_CLAUSE_LOCATION (c),
15434 "%qD appears more than once in data "
15435 "clauses", t);
15436 else
15437 error_at (OMP_CLAUSE_LOCATION (c),
15438 "%qD appears more than once in map "
15439 "clauses", t);
15440 remove = true;
15442 else
15444 bitmap_set_bit (&map_head, DECL_UID (t));
15445 bitmap_set_bit (&map_field_head, DECL_UID (t));
15449 if (c_oacc_check_attachments (c))
15450 remove = true;
15451 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15452 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15453 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15454 /* In this case, we have a single array element which is a
15455 pointer, and we already set OMP_CLAUSE_SIZE in
15456 handle_omp_array_sections above. For attach/detach clauses,
15457 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
15458 here. */
15459 OMP_CLAUSE_SIZE (c) = size_zero_node;
15460 break;
15462 if (t == error_mark_node)
15464 remove = true;
15465 break;
15467 /* OpenACC attach / detach clauses must be pointers. */
15468 if (c_oacc_check_attachments (c))
15470 remove = true;
15471 break;
15473 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15474 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15475 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15476 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15477 bias) to zero here, so it is not set erroneously to the pointer
15478 size later on in gimplify.cc. */
15479 OMP_CLAUSE_SIZE (c) = size_zero_node;
15480 while (INDIRECT_REF_P (t)
15481 || TREE_CODE (t) == ARRAY_REF)
15483 t = TREE_OPERAND (t, 0);
15484 STRIP_NOPS (t);
15485 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15486 t = TREE_OPERAND (t, 0);
15488 while (TREE_CODE (t) == COMPOUND_EXPR)
15490 t = TREE_OPERAND (t, 1);
15491 STRIP_NOPS (t);
15493 indir_component_ref_p = false;
15494 if (TREE_CODE (t) == COMPONENT_REF
15495 && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15496 || INDIRECT_REF_P (TREE_OPERAND (t, 0))
15497 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15499 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15500 indir_component_ref_p = true;
15501 STRIP_NOPS (t);
15502 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15503 t = TREE_OPERAND (t, 0);
15506 if (TREE_CODE (t) == COMPONENT_REF
15507 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15509 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15511 error_at (OMP_CLAUSE_LOCATION (c),
15512 "bit-field %qE in %qs clause",
15513 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15514 remove = true;
15516 else if (!omp_mappable_type (TREE_TYPE (t)))
15518 error_at (OMP_CLAUSE_LOCATION (c),
15519 "%qE does not have a mappable type in %qs clause",
15520 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15521 remove = true;
15523 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15525 error_at (OMP_CLAUSE_LOCATION (c),
15526 "%<_Atomic%> %qE in %qs clause", t,
15527 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15528 remove = true;
15530 while (TREE_CODE (t) == COMPONENT_REF)
15532 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15533 == UNION_TYPE)
15535 error_at (OMP_CLAUSE_LOCATION (c),
15536 "%qE is a member of a union", t);
15537 remove = true;
15538 break;
15540 t = TREE_OPERAND (t, 0);
15541 if (TREE_CODE (t) == MEM_REF)
15543 if (maybe_ne (mem_ref_offset (t), 0))
15544 error_at (OMP_CLAUSE_LOCATION (c),
15545 "cannot dereference %qE in %qs clause", t,
15546 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15547 else
15548 t = TREE_OPERAND (t, 0);
15550 while (TREE_CODE (t) == MEM_REF
15551 || INDIRECT_REF_P (t)
15552 || TREE_CODE (t) == ARRAY_REF)
15554 t = TREE_OPERAND (t, 0);
15555 STRIP_NOPS (t);
15556 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15557 t = TREE_OPERAND (t, 0);
15560 if (remove)
15561 break;
15562 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15564 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15565 || (ort != C_ORT_ACC
15566 && bitmap_bit_p (&map_head, DECL_UID (t))))
15567 break;
15570 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15572 error_at (OMP_CLAUSE_LOCATION (c),
15573 "%qE is not a variable in %qs clause", t,
15574 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15575 remove = true;
15577 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15579 error_at (OMP_CLAUSE_LOCATION (c),
15580 "%qD is threadprivate variable in %qs clause", t,
15581 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15582 remove = true;
15584 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15585 || (OMP_CLAUSE_MAP_KIND (c)
15586 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15587 && !indir_component_ref_p
15588 && !c_mark_addressable (t))
15589 remove = true;
15590 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15591 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15592 || (OMP_CLAUSE_MAP_KIND (c)
15593 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15594 || (OMP_CLAUSE_MAP_KIND (c)
15595 == GOMP_MAP_FORCE_DEVICEPTR)))
15596 && t == OMP_CLAUSE_DECL (c)
15597 && !omp_mappable_type (TREE_TYPE (t)))
15599 error_at (OMP_CLAUSE_LOCATION (c),
15600 "%qD does not have a mappable type in %qs clause", t,
15601 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15602 remove = true;
15604 else if (TREE_TYPE (t) == error_mark_node)
15605 remove = true;
15606 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15608 error_at (OMP_CLAUSE_LOCATION (c),
15609 "%<_Atomic%> %qE in %qs clause", t,
15610 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15611 remove = true;
15613 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15614 && OMP_CLAUSE_MAP_IMPLICIT (c)
15615 && (bitmap_bit_p (&map_head, DECL_UID (t))
15616 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15617 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15618 remove = true;
15619 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15620 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15622 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15623 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15624 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15626 error_at (OMP_CLAUSE_LOCATION (c),
15627 "%qD appears more than once in data clauses", t);
15628 remove = true;
15630 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15631 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15633 if (ort == C_ORT_ACC)
15634 error_at (OMP_CLAUSE_LOCATION (c),
15635 "%qD appears more than once in data clauses", t);
15636 else
15637 error_at (OMP_CLAUSE_LOCATION (c),
15638 "%qD appears both in data and map clauses", t);
15639 remove = true;
15641 else
15642 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15644 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15645 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15647 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15648 error_at (OMP_CLAUSE_LOCATION (c),
15649 "%qD appears more than once in motion clauses", t);
15650 else if (ort == C_ORT_ACC)
15651 error_at (OMP_CLAUSE_LOCATION (c),
15652 "%qD appears more than once in data clauses", t);
15653 else
15654 error_at (OMP_CLAUSE_LOCATION (c),
15655 "%qD appears more than once in map clauses", t);
15656 remove = true;
15658 else if (ort == C_ORT_ACC
15659 && bitmap_bit_p (&generic_head, DECL_UID (t)))
15661 error_at (OMP_CLAUSE_LOCATION (c),
15662 "%qD appears more than once in data clauses", t);
15663 remove = true;
15665 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15666 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15668 if (ort == C_ORT_ACC)
15669 error_at (OMP_CLAUSE_LOCATION (c),
15670 "%qD appears more than once in data clauses", t);
15671 else
15672 error_at (OMP_CLAUSE_LOCATION (c),
15673 "%qD appears both in data and map clauses", t);
15674 remove = true;
15676 else
15678 bitmap_set_bit (&map_head, DECL_UID (t));
15679 if (t != OMP_CLAUSE_DECL (c)
15680 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15681 bitmap_set_bit (&map_field_head, DECL_UID (t));
15683 break;
15685 case OMP_CLAUSE_ENTER:
15686 case OMP_CLAUSE_LINK:
15687 t = OMP_CLAUSE_DECL (c);
15688 const char *cname;
15689 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15690 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15691 && OMP_CLAUSE_ENTER_TO (c))
15692 cname = "to";
15693 if (TREE_CODE (t) == FUNCTION_DECL
15694 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15696 else if (!VAR_P (t))
15698 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15699 error_at (OMP_CLAUSE_LOCATION (c),
15700 "%qE is neither a variable nor a function name in "
15701 "clause %qs", t, cname);
15702 else
15703 error_at (OMP_CLAUSE_LOCATION (c),
15704 "%qE is not a variable in clause %qs", t, cname);
15705 remove = true;
15707 else if (DECL_THREAD_LOCAL_P (t))
15709 error_at (OMP_CLAUSE_LOCATION (c),
15710 "%qD is threadprivate variable in %qs clause", t,
15711 cname);
15712 remove = true;
15714 else if (!omp_mappable_type (TREE_TYPE (t)))
15716 error_at (OMP_CLAUSE_LOCATION (c),
15717 "%qD does not have a mappable type in %qs clause", t,
15718 cname);
15719 remove = true;
15721 if (remove)
15722 break;
15723 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15725 error_at (OMP_CLAUSE_LOCATION (c),
15726 "%qE appears more than once on the same "
15727 "%<declare target%> directive", t);
15728 remove = true;
15730 else
15731 bitmap_set_bit (&generic_head, DECL_UID (t));
15732 break;
15734 case OMP_CLAUSE_UNIFORM:
15735 t = OMP_CLAUSE_DECL (c);
15736 if (TREE_CODE (t) != PARM_DECL)
15738 if (DECL_P (t))
15739 error_at (OMP_CLAUSE_LOCATION (c),
15740 "%qD is not an argument in %<uniform%> clause", t);
15741 else
15742 error_at (OMP_CLAUSE_LOCATION (c),
15743 "%qE is not an argument in %<uniform%> clause", t);
15744 remove = true;
15745 break;
15747 /* map_head bitmap is used as uniform_head if declare_simd. */
15748 bitmap_set_bit (&map_head, DECL_UID (t));
15749 goto check_dup_generic;
15751 case OMP_CLAUSE_IS_DEVICE_PTR:
15752 case OMP_CLAUSE_USE_DEVICE_PTR:
15753 t = OMP_CLAUSE_DECL (c);
15754 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15755 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15756 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15758 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15759 && ort != C_ORT_ACC)
15761 error_at (OMP_CLAUSE_LOCATION (c),
15762 "%qs variable is not a pointer",
15763 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15764 remove = true;
15766 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15768 error_at (OMP_CLAUSE_LOCATION (c),
15769 "%qs variable is neither a pointer nor an array",
15770 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15771 remove = true;
15774 goto check_dup_generic;
15776 case OMP_CLAUSE_HAS_DEVICE_ADDR:
15777 t = OMP_CLAUSE_DECL (c);
15778 if (TREE_CODE (t) == TREE_LIST)
15780 if (handle_omp_array_sections (c, ort))
15781 remove = true;
15782 else
15784 t = OMP_CLAUSE_DECL (c);
15785 while (TREE_CODE (t) == ARRAY_REF)
15786 t = TREE_OPERAND (t, 0);
15789 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15790 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15791 c_mark_addressable (t);
15792 goto check_dup_generic_t;
15794 case OMP_CLAUSE_USE_DEVICE_ADDR:
15795 t = OMP_CLAUSE_DECL (c);
15796 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15797 c_mark_addressable (t);
15798 goto check_dup_generic;
15800 case OMP_CLAUSE_NOWAIT:
15801 if (copyprivate_seen)
15803 error_at (OMP_CLAUSE_LOCATION (c),
15804 "%<nowait%> clause must not be used together "
15805 "with %<copyprivate%>");
15806 remove = true;
15807 break;
15809 nowait_clause = pc;
15810 pc = &OMP_CLAUSE_CHAIN (c);
15811 continue;
15813 case OMP_CLAUSE_ORDER:
15814 if (ordered_clause)
15816 error_at (OMP_CLAUSE_LOCATION (c),
15817 "%<order%> clause must not be used together "
15818 "with %<ordered%>");
15819 remove = true;
15820 break;
15822 else if (order_clause)
15824 /* Silently remove duplicates. */
15825 remove = true;
15826 break;
15828 order_clause = pc;
15829 pc = &OMP_CLAUSE_CHAIN (c);
15830 continue;
15832 case OMP_CLAUSE_DETACH:
15833 t = OMP_CLAUSE_DECL (c);
15834 if (detach_seen)
15836 error_at (OMP_CLAUSE_LOCATION (c),
15837 "too many %qs clauses on a task construct",
15838 "detach");
15839 remove = true;
15840 break;
15842 detach_seen = pc;
15843 pc = &OMP_CLAUSE_CHAIN (c);
15844 c_mark_addressable (t);
15845 continue;
15847 case OMP_CLAUSE_IF:
15848 case OMP_CLAUSE_SELF:
15849 case OMP_CLAUSE_NUM_THREADS:
15850 case OMP_CLAUSE_NUM_TEAMS:
15851 case OMP_CLAUSE_THREAD_LIMIT:
15852 case OMP_CLAUSE_DEFAULT:
15853 case OMP_CLAUSE_UNTIED:
15854 case OMP_CLAUSE_COLLAPSE:
15855 case OMP_CLAUSE_FINAL:
15856 case OMP_CLAUSE_DEVICE:
15857 case OMP_CLAUSE_DIST_SCHEDULE:
15858 case OMP_CLAUSE_PARALLEL:
15859 case OMP_CLAUSE_FOR:
15860 case OMP_CLAUSE_SECTIONS:
15861 case OMP_CLAUSE_TASKGROUP:
15862 case OMP_CLAUSE_PROC_BIND:
15863 case OMP_CLAUSE_DEVICE_TYPE:
15864 case OMP_CLAUSE_PRIORITY:
15865 case OMP_CLAUSE_GRAINSIZE:
15866 case OMP_CLAUSE_NUM_TASKS:
15867 case OMP_CLAUSE_THREADS:
15868 case OMP_CLAUSE_SIMD:
15869 case OMP_CLAUSE_HINT:
15870 case OMP_CLAUSE_FILTER:
15871 case OMP_CLAUSE_DEFAULTMAP:
15872 case OMP_CLAUSE_BIND:
15873 case OMP_CLAUSE_NUM_GANGS:
15874 case OMP_CLAUSE_NUM_WORKERS:
15875 case OMP_CLAUSE_VECTOR_LENGTH:
15876 case OMP_CLAUSE_ASYNC:
15877 case OMP_CLAUSE_WAIT:
15878 case OMP_CLAUSE_AUTO:
15879 case OMP_CLAUSE_INDEPENDENT:
15880 case OMP_CLAUSE_SEQ:
15881 case OMP_CLAUSE_GANG:
15882 case OMP_CLAUSE_WORKER:
15883 case OMP_CLAUSE_VECTOR:
15884 case OMP_CLAUSE_TILE:
15885 case OMP_CLAUSE_IF_PRESENT:
15886 case OMP_CLAUSE_FINALIZE:
15887 case OMP_CLAUSE_NOHOST:
15888 pc = &OMP_CLAUSE_CHAIN (c);
15889 continue;
15891 case OMP_CLAUSE_MERGEABLE:
15892 mergeable_seen = true;
15893 pc = &OMP_CLAUSE_CHAIN (c);
15894 continue;
15896 case OMP_CLAUSE_NOGROUP:
15897 nogroup_seen = pc;
15898 pc = &OMP_CLAUSE_CHAIN (c);
15899 continue;
15901 case OMP_CLAUSE_SCHEDULE:
15902 schedule_clause = c;
15903 pc = &OMP_CLAUSE_CHAIN (c);
15904 continue;
15906 case OMP_CLAUSE_ORDERED:
15907 ordered_clause = c;
15908 if (order_clause)
15910 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15911 "%<order%> clause must not be used together "
15912 "with %<ordered%>");
15913 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15914 order_clause = NULL;
15916 pc = &OMP_CLAUSE_CHAIN (c);
15917 continue;
15919 case OMP_CLAUSE_SAFELEN:
15920 safelen = c;
15921 pc = &OMP_CLAUSE_CHAIN (c);
15922 continue;
15923 case OMP_CLAUSE_SIMDLEN:
15924 simdlen = c;
15925 pc = &OMP_CLAUSE_CHAIN (c);
15926 continue;
15928 case OMP_CLAUSE_INBRANCH:
15929 case OMP_CLAUSE_NOTINBRANCH:
15930 if (branch_seen)
15932 error_at (OMP_CLAUSE_LOCATION (c),
15933 "%<inbranch%> clause is incompatible with "
15934 "%<notinbranch%>");
15935 remove = true;
15936 break;
15938 branch_seen = true;
15939 pc = &OMP_CLAUSE_CHAIN (c);
15940 continue;
15942 case OMP_CLAUSE_INCLUSIVE:
15943 case OMP_CLAUSE_EXCLUSIVE:
15944 need_complete = true;
15945 need_implicitly_determined = true;
15946 t = OMP_CLAUSE_DECL (c);
15947 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15949 error_at (OMP_CLAUSE_LOCATION (c),
15950 "%qE is not a variable in clause %qs", t,
15951 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15952 remove = true;
15954 break;
15956 default:
15957 gcc_unreachable ();
15960 if (!remove)
15962 t = OMP_CLAUSE_DECL (c);
15964 if (need_complete)
15966 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15967 if (t == error_mark_node)
15968 remove = true;
15971 if (need_implicitly_determined)
15973 const char *share_name = NULL;
15975 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15976 share_name = "threadprivate";
15977 else switch (c_omp_predetermined_sharing (t))
15979 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15980 break;
15981 case OMP_CLAUSE_DEFAULT_SHARED:
15982 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15983 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15984 && c_omp_predefined_variable (t))
15985 /* The __func__ variable and similar function-local
15986 predefined variables may be listed in a shared or
15987 firstprivate clause. */
15988 break;
15989 share_name = "shared";
15990 break;
15991 case OMP_CLAUSE_DEFAULT_PRIVATE:
15992 share_name = "private";
15993 break;
15994 default:
15995 gcc_unreachable ();
15997 if (share_name)
15999 error_at (OMP_CLAUSE_LOCATION (c),
16000 "%qE is predetermined %qs for %qs",
16001 t, share_name,
16002 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16003 remove = true;
16005 else if (TREE_READONLY (t)
16006 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
16007 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
16009 error_at (OMP_CLAUSE_LOCATION (c),
16010 "%<const%> qualified %qE may appear only in "
16011 "%<shared%> or %<firstprivate%> clauses", t);
16012 remove = true;
16017 if (remove)
16019 if (grp_start_p)
16021 /* If we found a clause to remove, we want to remove the whole
16022 expanded group, otherwise gimplify
16023 (omp_resolve_clause_dependencies) can get confused. */
16024 *grp_start_p = grp_sentinel;
16025 pc = grp_start_p;
16026 grp_start_p = NULL;
16028 else
16029 *pc = OMP_CLAUSE_CHAIN (c);
16031 else
16032 pc = &OMP_CLAUSE_CHAIN (c);
16035 if (simdlen
16036 && safelen
16037 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
16038 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
16040 error_at (OMP_CLAUSE_LOCATION (simdlen),
16041 "%<simdlen%> clause value is bigger than "
16042 "%<safelen%> clause value");
16043 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
16044 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
16047 if (ordered_clause
16048 && schedule_clause
16049 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16050 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
16052 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16053 "%<nonmonotonic%> schedule modifier specified together "
16054 "with %<ordered%> clause");
16055 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16056 = (enum omp_clause_schedule_kind)
16057 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16058 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
16061 if (reduction_seen < 0 && ordered_clause)
16063 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
16064 "%qs clause specified together with %<inscan%> "
16065 "%<reduction%> clause", "ordered");
16066 reduction_seen = -2;
16069 if (reduction_seen < 0 && schedule_clause)
16071 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16072 "%qs clause specified together with %<inscan%> "
16073 "%<reduction%> clause", "schedule");
16074 reduction_seen = -2;
16077 if (linear_variable_step_check
16078 || reduction_seen == -2
16079 || allocate_seen
16080 || target_in_reduction_seen)
16081 for (pc = &clauses, c = clauses; c ; c = *pc)
16083 bool remove = false;
16084 if (allocate_seen)
16085 switch (OMP_CLAUSE_CODE (c))
16087 case OMP_CLAUSE_REDUCTION:
16088 case OMP_CLAUSE_IN_REDUCTION:
16089 case OMP_CLAUSE_TASK_REDUCTION:
16090 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
16092 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
16093 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16094 t = TREE_OPERAND (t, 0);
16095 if (TREE_CODE (t) == ADDR_EXPR
16096 || INDIRECT_REF_P (t))
16097 t = TREE_OPERAND (t, 0);
16098 if (DECL_P (t))
16099 bitmap_clear_bit (&aligned_head, DECL_UID (t));
16100 break;
16102 /* FALLTHRU */
16103 case OMP_CLAUSE_PRIVATE:
16104 case OMP_CLAUSE_FIRSTPRIVATE:
16105 case OMP_CLAUSE_LASTPRIVATE:
16106 case OMP_CLAUSE_LINEAR:
16107 if (DECL_P (OMP_CLAUSE_DECL (c)))
16108 bitmap_clear_bit (&aligned_head,
16109 DECL_UID (OMP_CLAUSE_DECL (c)));
16110 break;
16111 default:
16112 break;
16114 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16115 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
16116 && !bitmap_bit_p (&map_head,
16117 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
16119 error_at (OMP_CLAUSE_LOCATION (c),
16120 "%<linear%> clause step is a parameter %qD not "
16121 "specified in %<uniform%> clause",
16122 OMP_CLAUSE_LINEAR_STEP (c));
16123 remove = true;
16125 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16126 && reduction_seen == -2)
16127 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
16128 if (target_in_reduction_seen
16129 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
16131 tree t = OMP_CLAUSE_DECL (c);
16132 while (handled_component_p (t)
16133 || INDIRECT_REF_P (t)
16134 || TREE_CODE (t) == ADDR_EXPR
16135 || TREE_CODE (t) == MEM_REF
16136 || TREE_CODE (t) == NON_LVALUE_EXPR)
16137 t = TREE_OPERAND (t, 0);
16138 if (DECL_P (t)
16139 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16140 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
16143 if (remove)
16144 *pc = OMP_CLAUSE_CHAIN (c);
16145 else
16146 pc = &OMP_CLAUSE_CHAIN (c);
16149 if (allocate_seen)
16150 for (pc = &clauses, c = clauses; c ; c = *pc)
16152 bool remove = false;
16153 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
16154 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
16155 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
16157 error_at (OMP_CLAUSE_LOCATION (c),
16158 "%qD specified in %<allocate%> clause but not in "
16159 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
16160 remove = true;
16162 if (remove)
16163 *pc = OMP_CLAUSE_CHAIN (c);
16164 else
16165 pc = &OMP_CLAUSE_CHAIN (c);
16168 if (nogroup_seen && reduction_seen)
16170 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
16171 "%<nogroup%> clause must not be used together with "
16172 "%<reduction%> clause");
16173 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
16176 if (detach_seen)
16178 if (mergeable_seen)
16180 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
16181 "%<detach%> clause must not be used together with "
16182 "%<mergeable%> clause");
16183 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
16185 else
16187 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
16189 for (pc = &clauses, c = clauses; c ; c = *pc)
16191 bool remove = false;
16192 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
16193 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
16194 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
16195 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
16196 && OMP_CLAUSE_DECL (c) == detach_decl)
16198 error_at (OMP_CLAUSE_LOCATION (c),
16199 "the event handle of a %<detach%> clause "
16200 "should not be in a data-sharing clause");
16201 remove = true;
16203 if (remove)
16204 *pc = OMP_CLAUSE_CHAIN (c);
16205 else
16206 pc = &OMP_CLAUSE_CHAIN (c);
16211 bitmap_obstack_release (NULL);
16212 return clauses;
16215 /* Return code to initialize DST with a copy constructor from SRC.
16216 C doesn't have copy constructors nor assignment operators, only for
16217 _Atomic vars we need to perform __atomic_load from src into a temporary
16218 followed by __atomic_store of the temporary to dst. */
16220 tree
16221 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
16223 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
16224 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
16226 location_t loc = OMP_CLAUSE_LOCATION (clause);
16227 tree type = TREE_TYPE (dst);
16228 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
16229 tree tmp = create_tmp_var (nonatomic_type);
16230 tree tmp_addr = build_fold_addr_expr (tmp);
16231 TREE_ADDRESSABLE (tmp) = 1;
16232 suppress_warning (tmp);
16233 tree src_addr = build_fold_addr_expr (src);
16234 tree dst_addr = build_fold_addr_expr (dst);
16235 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
16236 vec<tree, va_gc> *params;
16237 /* Expansion of a generic atomic load may require an addition
16238 element, so allocate enough to prevent a resize. */
16239 vec_alloc (params, 4);
16241 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
16242 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
16243 params->quick_push (src_addr);
16244 params->quick_push (tmp_addr);
16245 params->quick_push (seq_cst);
16246 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16248 vec_alloc (params, 4);
16250 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
16251 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
16252 params->quick_push (dst_addr);
16253 params->quick_push (tmp_addr);
16254 params->quick_push (seq_cst);
16255 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16256 return build2 (COMPOUND_EXPR, void_type_node, load, store);
16259 /* Create a transaction node. */
16261 tree
16262 c_finish_transaction (location_t loc, tree block, int flags)
16264 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
16265 if (flags & TM_STMT_ATTR_OUTER)
16266 TRANSACTION_EXPR_OUTER (stmt) = 1;
16267 if (flags & TM_STMT_ATTR_RELAXED)
16268 TRANSACTION_EXPR_RELAXED (stmt) = 1;
16269 return add_stmt (stmt);
16272 /* Make a variant type in the proper way for C/C++, propagating qualifiers
16273 down to the element type of an array. If ORIG_QUAL_TYPE is not
16274 NULL, then it should be used as the qualified type
16275 ORIG_QUAL_INDIRECT levels down in array type derivation (to
16276 preserve information about the typedef name from which an array
16277 type was derived). */
16279 tree
16280 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
16281 size_t orig_qual_indirect)
16283 if (type == error_mark_node)
16284 return type;
16286 if (TREE_CODE (type) == ARRAY_TYPE)
16288 tree t;
16289 tree element_type = c_build_qualified_type (TREE_TYPE (type),
16290 type_quals, orig_qual_type,
16291 orig_qual_indirect - 1);
16293 /* See if we already have an identically qualified type. */
16294 if (orig_qual_type && orig_qual_indirect == 0)
16295 t = orig_qual_type;
16296 else
16297 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
16299 if (TYPE_QUALS (strip_array_types (t)) == type_quals
16300 && TYPE_NAME (t) == TYPE_NAME (type)
16301 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
16302 && attribute_list_equal (TYPE_ATTRIBUTES (t),
16303 TYPE_ATTRIBUTES (type)))
16304 break;
16306 if (!t)
16308 tree domain = TYPE_DOMAIN (type);
16310 t = build_variant_type_copy (type);
16311 TREE_TYPE (t) = element_type;
16313 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
16314 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
16315 SET_TYPE_STRUCTURAL_EQUALITY (t);
16316 else if (TYPE_CANONICAL (element_type) != element_type
16317 || (domain && TYPE_CANONICAL (domain) != domain))
16319 tree unqualified_canon
16320 = build_array_type (TYPE_CANONICAL (element_type),
16321 domain? TYPE_CANONICAL (domain)
16322 : NULL_TREE);
16323 if (TYPE_REVERSE_STORAGE_ORDER (type))
16325 unqualified_canon
16326 = build_distinct_type_copy (unqualified_canon);
16327 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
16329 TYPE_CANONICAL (t)
16330 = c_build_qualified_type (unqualified_canon, type_quals);
16332 else
16333 TYPE_CANONICAL (t) = t;
16335 return t;
16338 /* A restrict-qualified pointer type must be a pointer to object or
16339 incomplete type. Note that the use of POINTER_TYPE_P also allows
16340 REFERENCE_TYPEs, which is appropriate for C++. */
16341 if ((type_quals & TYPE_QUAL_RESTRICT)
16342 && (!POINTER_TYPE_P (type)
16343 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
16345 error ("invalid use of %<restrict%>");
16346 type_quals &= ~TYPE_QUAL_RESTRICT;
16349 tree var_type = (orig_qual_type && orig_qual_indirect == 0
16350 ? orig_qual_type
16351 : build_qualified_type (type, type_quals));
16352 /* A variant type does not inherit the list of incomplete vars from the
16353 type main variant. */
16354 if ((RECORD_OR_UNION_TYPE_P (var_type)
16355 || TREE_CODE (var_type) == ENUMERAL_TYPE)
16356 && TYPE_MAIN_VARIANT (var_type) != var_type)
16357 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
16358 return var_type;
16361 /* Build a VA_ARG_EXPR for the C parser. */
16363 tree
16364 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
16366 if (error_operand_p (type))
16367 return error_mark_node;
16368 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
16369 order because it takes the address of the expression. */
16370 else if (handled_component_p (expr)
16371 && reverse_storage_order_for_component_p (expr))
16373 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
16374 return error_mark_node;
16376 else if (!COMPLETE_TYPE_P (type))
16378 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
16379 "type %qT", type);
16380 return error_mark_node;
16382 else if (TREE_CODE (type) == FUNCTION_TYPE)
16384 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
16385 type);
16386 return error_mark_node;
16388 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
16389 warning_at (loc2, OPT_Wc___compat,
16390 "C++ requires promoted type, not enum type, in %<va_arg%>");
16391 return build_va_arg (loc2, expr, type);
16394 /* Return truthvalue of whether T1 is the same tree structure as T2.
16395 Return 1 if they are the same. Return false if they are different. */
16397 bool
16398 c_tree_equal (tree t1, tree t2)
16400 enum tree_code code1, code2;
16402 if (t1 == t2)
16403 return true;
16404 if (!t1 || !t2)
16405 return false;
16407 for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
16408 code1 = TREE_CODE (t1))
16409 t1 = TREE_OPERAND (t1, 0);
16410 for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
16411 code2 = TREE_CODE (t2))
16412 t2 = TREE_OPERAND (t2, 0);
16414 /* They might have become equal now. */
16415 if (t1 == t2)
16416 return true;
16418 if (code1 != code2)
16419 return false;
16421 if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16422 return false;
16424 switch (code1)
16426 case INTEGER_CST:
16427 return wi::to_wide (t1) == wi::to_wide (t2);
16429 case REAL_CST:
16430 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
16432 case STRING_CST:
16433 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16434 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16435 TREE_STRING_LENGTH (t1));
16437 case FIXED_CST:
16438 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16439 TREE_FIXED_CST (t2));
16441 case COMPLEX_CST:
16442 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16443 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16445 case VECTOR_CST:
16446 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16448 case CONSTRUCTOR:
16449 /* We need to do this when determining whether or not two
16450 non-type pointer to member function template arguments
16451 are the same. */
16452 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16453 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16454 return false;
16456 tree field, value;
16457 unsigned int i;
16458 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16460 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16461 if (!c_tree_equal (field, elt2->index)
16462 || !c_tree_equal (value, elt2->value))
16463 return false;
16466 return true;
16468 case TREE_LIST:
16469 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16470 return false;
16471 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16472 return false;
16473 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16475 case SAVE_EXPR:
16476 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16478 case CALL_EXPR:
16480 tree arg1, arg2;
16481 call_expr_arg_iterator iter1, iter2;
16482 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16483 return false;
16484 for (arg1 = first_call_expr_arg (t1, &iter1),
16485 arg2 = first_call_expr_arg (t2, &iter2);
16486 arg1 && arg2;
16487 arg1 = next_call_expr_arg (&iter1),
16488 arg2 = next_call_expr_arg (&iter2))
16489 if (!c_tree_equal (arg1, arg2))
16490 return false;
16491 if (arg1 || arg2)
16492 return false;
16493 return true;
16496 case TARGET_EXPR:
16498 tree o1 = TREE_OPERAND (t1, 0);
16499 tree o2 = TREE_OPERAND (t2, 0);
16501 /* Special case: if either target is an unallocated VAR_DECL,
16502 it means that it's going to be unified with whatever the
16503 TARGET_EXPR is really supposed to initialize, so treat it
16504 as being equivalent to anything. */
16505 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16506 && !DECL_RTL_SET_P (o1))
16507 /*Nop*/;
16508 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16509 && !DECL_RTL_SET_P (o2))
16510 /*Nop*/;
16511 else if (!c_tree_equal (o1, o2))
16512 return false;
16514 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16517 case COMPONENT_REF:
16518 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16519 return false;
16520 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16522 case PARM_DECL:
16523 case VAR_DECL:
16524 case CONST_DECL:
16525 case FIELD_DECL:
16526 case FUNCTION_DECL:
16527 case IDENTIFIER_NODE:
16528 case SSA_NAME:
16529 return false;
16531 case TREE_VEC:
16533 unsigned ix;
16534 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16535 return false;
16536 for (ix = TREE_VEC_LENGTH (t1); ix--;)
16537 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16538 TREE_VEC_ELT (t2, ix)))
16539 return false;
16540 return true;
16543 CASE_CONVERT:
16544 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16545 return false;
16546 break;
16548 default:
16549 break;
16552 switch (TREE_CODE_CLASS (code1))
16554 case tcc_unary:
16555 case tcc_binary:
16556 case tcc_comparison:
16557 case tcc_expression:
16558 case tcc_vl_exp:
16559 case tcc_reference:
16560 case tcc_statement:
16562 int i, n = TREE_OPERAND_LENGTH (t1);
16564 switch (code1)
16566 case PREINCREMENT_EXPR:
16567 case PREDECREMENT_EXPR:
16568 case POSTINCREMENT_EXPR:
16569 case POSTDECREMENT_EXPR:
16570 n = 1;
16571 break;
16572 case ARRAY_REF:
16573 n = 2;
16574 break;
16575 default:
16576 break;
16579 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16580 && n != TREE_OPERAND_LENGTH (t2))
16581 return false;
16583 for (i = 0; i < n; ++i)
16584 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16585 return false;
16587 return true;
16590 case tcc_type:
16591 return comptypes (t1, t2);
16592 default:
16593 gcc_unreachable ();
16597 /* Returns true when the function declaration FNDECL is implicit,
16598 introduced as a result of a call to an otherwise undeclared
16599 function, and false otherwise. */
16601 bool
16602 c_decl_implicit (const_tree fndecl)
16604 return C_DECL_IMPLICIT (fndecl);