Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / c / c-typeck.c
blob0c07af61f24f015f17d055ef398445333c9d8f85
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
56 /* Possible cases of implicit bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
58 enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_return
65 /* The level of nesting inside "__alignof__". */
66 int in_alignof;
68 /* The level of nesting inside "sizeof". */
69 int in_sizeof;
71 /* The level of nesting inside "typeof". */
72 int in_typeof;
74 /* True when parsing OpenMP loop expressions. */
75 bool c_in_omp_for;
77 /* The argument of last parsed sizeof expression, only to be tested
78 if expr.original_code == SIZEOF_EXPR. */
79 tree c_last_sizeof_arg;
80 location_t c_last_sizeof_loc;
82 /* Nonzero if we might need to print a "missing braces around
83 initializer" message within this initializer. */
84 static int found_missing_braces;
86 static int require_constant_value;
87 static int require_constant_elements;
89 static bool null_pointer_constant_p (const_tree);
90 static tree qualify_type (tree, tree);
91 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93 static int comp_target_types (location_t, tree, tree);
94 static int function_types_compatible_p (const_tree, const_tree, bool *,
95 bool *);
96 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
97 static tree lookup_field (tree, tree);
98 static int convert_arguments (location_t, vec<location_t>, tree,
99 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
100 tree);
101 static tree pointer_diff (location_t, tree, tree, tree *);
102 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
103 enum impl_conv, bool, tree, tree, int,
104 int = 0);
105 static tree valid_compound_expr_initializer (tree, tree);
106 static void push_string (const char *);
107 static void push_member_name (tree);
108 static int spelling_length (void);
109 static char *print_spelling (char *);
110 static void warning_init (location_t, int, const char *);
111 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
112 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
113 bool, struct obstack *);
114 static void output_pending_init_elements (int, struct obstack *);
115 static bool set_designator (location_t, bool, struct obstack *);
116 static void push_range_stack (tree, struct obstack *);
117 static void add_pending_init (location_t, tree, tree, tree, bool,
118 struct obstack *);
119 static void set_nonincremental_init (struct obstack *);
120 static void set_nonincremental_init_from_string (tree, struct obstack *);
121 static tree find_init_member (tree, struct obstack *);
122 static void readonly_warning (tree, enum lvalue_use);
123 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
124 static void record_maybe_used_decl (tree);
125 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
127 /* Return true if EXP is a null pointer constant, false otherwise. */
129 static bool
130 null_pointer_constant_p (const_tree expr)
132 /* This should really operate on c_expr structures, but they aren't
133 yet available everywhere required. */
134 tree type = TREE_TYPE (expr);
135 return (TREE_CODE (expr) == INTEGER_CST
136 && !TREE_OVERFLOW (expr)
137 && integer_zerop (expr)
138 && (INTEGRAL_TYPE_P (type)
139 || (TREE_CODE (type) == POINTER_TYPE
140 && VOID_TYPE_P (TREE_TYPE (type))
141 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
144 /* EXPR may appear in an unevaluated part of an integer constant
145 expression, but not in an evaluated part. Wrap it in a
146 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
147 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
149 static tree
150 note_integer_operands (tree expr)
152 tree ret;
153 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
155 ret = copy_node (expr);
156 TREE_OVERFLOW (ret) = 1;
158 else
160 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
161 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
163 return ret;
166 /* Having checked whether EXPR may appear in an unevaluated part of an
167 integer constant expression and found that it may, remove any
168 C_MAYBE_CONST_EXPR noting this fact and return the resulting
169 expression. */
171 static inline tree
172 remove_c_maybe_const_expr (tree expr)
174 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
175 return C_MAYBE_CONST_EXPR_EXPR (expr);
176 else
177 return expr;
180 \f/* This is a cache to hold if two types are compatible or not. */
182 struct tagged_tu_seen_cache {
183 const struct tagged_tu_seen_cache * next;
184 const_tree t1;
185 const_tree t2;
186 /* The return value of tagged_types_tu_compatible_p if we had seen
187 these two types already. */
188 int val;
191 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
192 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
194 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
195 does not have an incomplete type. (That includes void types.)
196 LOC is the location of the use. */
198 tree
199 require_complete_type (location_t loc, tree value)
201 tree type = TREE_TYPE (value);
203 if (error_operand_p (value))
204 return error_mark_node;
206 /* First, detect a valid value with a complete type. */
207 if (COMPLETE_TYPE_P (type))
208 return value;
210 c_incomplete_type_error (loc, value, type);
211 return error_mark_node;
214 /* Print an error message for invalid use of an incomplete type.
215 VALUE is the expression that was used (or 0 if that isn't known)
216 and TYPE is the type that was invalid. LOC is the location for
217 the error. */
219 void
220 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
222 /* Avoid duplicate error message. */
223 if (TREE_CODE (type) == ERROR_MARK)
224 return;
226 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
227 error_at (loc, "%qD has an incomplete type %qT", value, type);
228 else
230 retry:
231 /* We must print an error message. Be clever about what it says. */
233 switch (TREE_CODE (type))
235 case RECORD_TYPE:
236 case UNION_TYPE:
237 case ENUMERAL_TYPE:
238 break;
240 case VOID_TYPE:
241 error_at (loc, "invalid use of void expression");
242 return;
244 case ARRAY_TYPE:
245 if (TYPE_DOMAIN (type))
247 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
249 error_at (loc, "invalid use of flexible array member");
250 return;
252 type = TREE_TYPE (type);
253 goto retry;
255 error_at (loc, "invalid use of array with unspecified bounds");
256 return;
258 default:
259 gcc_unreachable ();
262 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
263 error_at (loc, "invalid use of undefined type %qT", type);
264 else
265 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
266 error_at (loc, "invalid use of incomplete typedef %qT", type);
270 /* Given a type, apply default promotions wrt unnamed function
271 arguments and return the new type. */
273 tree
274 c_type_promotes_to (tree type)
276 tree ret = NULL_TREE;
278 if (TYPE_MAIN_VARIANT (type) == float_type_node)
279 ret = double_type_node;
280 else if (c_promoting_integer_type_p (type))
282 /* Preserve unsignedness if not really getting any wider. */
283 if (TYPE_UNSIGNED (type)
284 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
285 ret = unsigned_type_node;
286 else
287 ret = integer_type_node;
290 if (ret != NULL_TREE)
291 return (TYPE_ATOMIC (type)
292 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
293 : ret);
295 return type;
298 /* Return true if between two named address spaces, whether there is a superset
299 named address space that encompasses both address spaces. If there is a
300 superset, return which address space is the superset. */
302 static bool
303 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
305 if (as1 == as2)
307 *common = as1;
308 return true;
310 else if (targetm.addr_space.subset_p (as1, as2))
312 *common = as2;
313 return true;
315 else if (targetm.addr_space.subset_p (as2, as1))
317 *common = as1;
318 return true;
320 else
321 return false;
324 /* Return a variant of TYPE which has all the type qualifiers of LIKE
325 as well as those of TYPE. */
327 static tree
328 qualify_type (tree type, tree like)
330 addr_space_t as_type = TYPE_ADDR_SPACE (type);
331 addr_space_t as_like = TYPE_ADDR_SPACE (like);
332 addr_space_t as_common;
334 /* If the two named address spaces are different, determine the common
335 superset address space. If there isn't one, raise an error. */
336 if (!addr_space_superset (as_type, as_like, &as_common))
338 as_common = as_type;
339 error ("%qT and %qT are in disjoint named address spaces",
340 type, like);
343 return c_build_qualified_type (type,
344 TYPE_QUALS_NO_ADDR_SPACE (type)
345 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
346 | ENCODE_QUAL_ADDR_SPACE (as_common));
349 /* Return true iff the given tree T is a variable length array. */
351 bool
352 c_vla_type_p (const_tree t)
354 if (TREE_CODE (t) == ARRAY_TYPE
355 && C_TYPE_VARIABLE_SIZE (t))
356 return true;
357 return false;
360 /* If NTYPE is a type of a non-variadic function with a prototype
361 and OTYPE is a type of a function without a prototype and ATTRS
362 contains attribute format, diagnosess and removes it from ATTRS.
363 Returns the result of build_type_attribute_variant of NTYPE and
364 the (possibly) modified ATTRS. */
366 static tree
367 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
369 if (!prototype_p (otype)
370 && prototype_p (ntype)
371 && lookup_attribute ("format", attrs))
373 warning_at (input_location, OPT_Wattributes,
374 "%qs attribute cannot be applied to a function that "
375 "does not take variable arguments", "format");
376 attrs = remove_attribute ("format", attrs);
378 return build_type_attribute_variant (ntype, attrs);
381 /* Return the composite type of two compatible types.
383 We assume that comptypes has already been done and returned
384 nonzero; if that isn't so, this may crash. In particular, we
385 assume that qualifiers match. */
387 tree
388 composite_type (tree t1, tree t2)
390 enum tree_code code1;
391 enum tree_code code2;
392 tree attributes;
394 /* Save time if the two types are the same. */
396 if (t1 == t2) return t1;
398 /* If one type is nonsense, use the other. */
399 if (t1 == error_mark_node)
400 return t2;
401 if (t2 == error_mark_node)
402 return t1;
404 code1 = TREE_CODE (t1);
405 code2 = TREE_CODE (t2);
407 /* Merge the attributes. */
408 attributes = targetm.merge_type_attributes (t1, t2);
410 /* If one is an enumerated type and the other is the compatible
411 integer type, the composite type might be either of the two
412 (DR#013 question 3). For consistency, use the enumerated type as
413 the composite type. */
415 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
416 return t1;
417 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
418 return t2;
420 gcc_assert (code1 == code2);
422 switch (code1)
424 case POINTER_TYPE:
425 /* For two pointers, do this recursively on the target type. */
427 tree pointed_to_1 = TREE_TYPE (t1);
428 tree pointed_to_2 = TREE_TYPE (t2);
429 tree target = composite_type (pointed_to_1, pointed_to_2);
430 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
431 t1 = build_type_attribute_variant (t1, attributes);
432 return qualify_type (t1, t2);
435 case ARRAY_TYPE:
437 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
438 int quals;
439 tree unqual_elt;
440 tree d1 = TYPE_DOMAIN (t1);
441 tree d2 = TYPE_DOMAIN (t2);
442 bool d1_variable, d2_variable;
443 bool d1_zero, d2_zero;
444 bool t1_complete, t2_complete;
446 /* We should not have any type quals on arrays at all. */
447 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
448 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
450 t1_complete = COMPLETE_TYPE_P (t1);
451 t2_complete = COMPLETE_TYPE_P (t2);
453 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
454 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
456 d1_variable = (!d1_zero
457 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
458 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
459 d2_variable = (!d2_zero
460 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
461 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
462 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
463 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
465 /* Save space: see if the result is identical to one of the args. */
466 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
467 && (d2_variable || d2_zero || !d1_variable))
468 return build_type_attribute_variant (t1, attributes);
469 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
470 && (d1_variable || d1_zero || !d2_variable))
471 return build_type_attribute_variant (t2, attributes);
473 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
474 return build_type_attribute_variant (t1, attributes);
475 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
476 return build_type_attribute_variant (t2, attributes);
478 /* Merge the element types, and have a size if either arg has
479 one. We may have qualifiers on the element types. To set
480 up TYPE_MAIN_VARIANT correctly, we need to form the
481 composite of the unqualified types and add the qualifiers
482 back at the end. */
483 quals = TYPE_QUALS (strip_array_types (elt));
484 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
485 t1 = build_array_type (unqual_elt,
486 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
487 && (d2_variable
488 || d2_zero
489 || !d1_variable))
490 ? t1
491 : t2));
492 /* Ensure a composite type involving a zero-length array type
493 is a zero-length type not an incomplete type. */
494 if (d1_zero && d2_zero
495 && (t1_complete || t2_complete)
496 && !COMPLETE_TYPE_P (t1))
498 TYPE_SIZE (t1) = bitsize_zero_node;
499 TYPE_SIZE_UNIT (t1) = size_zero_node;
501 t1 = c_build_qualified_type (t1, quals);
502 return build_type_attribute_variant (t1, attributes);
505 case ENUMERAL_TYPE:
506 case RECORD_TYPE:
507 case UNION_TYPE:
508 if (attributes != NULL)
510 /* Try harder not to create a new aggregate type. */
511 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
512 return t1;
513 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
514 return t2;
516 return build_type_attribute_variant (t1, attributes);
518 case FUNCTION_TYPE:
519 /* Function types: prefer the one that specified arg types.
520 If both do, merge the arg types. Also merge the return types. */
522 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
523 tree p1 = TYPE_ARG_TYPES (t1);
524 tree p2 = TYPE_ARG_TYPES (t2);
525 int len;
526 tree newargs, n;
527 int i;
529 /* Save space: see if the result is identical to one of the args. */
530 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
531 return build_functype_attribute_variant (t1, t2, attributes);
532 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
533 return build_functype_attribute_variant (t2, t1, attributes);
535 /* Simple way if one arg fails to specify argument types. */
536 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
538 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
539 t1 = build_type_attribute_variant (t1, attributes);
540 return qualify_type (t1, t2);
542 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
544 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
545 t1 = build_type_attribute_variant (t1, attributes);
546 return qualify_type (t1, t2);
549 /* If both args specify argument types, we must merge the two
550 lists, argument by argument. */
552 for (len = 0, newargs = p1;
553 newargs && newargs != void_list_node;
554 len++, newargs = TREE_CHAIN (newargs))
557 for (i = 0; i < len; i++)
558 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
560 n = newargs;
562 for (; p1 && p1 != void_list_node;
563 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
565 /* A null type means arg type is not specified.
566 Take whatever the other function type has. */
567 if (TREE_VALUE (p1) == NULL_TREE)
569 TREE_VALUE (n) = TREE_VALUE (p2);
570 goto parm_done;
572 if (TREE_VALUE (p2) == NULL_TREE)
574 TREE_VALUE (n) = TREE_VALUE (p1);
575 goto parm_done;
578 /* Given wait (union {union wait *u; int *i} *)
579 and wait (union wait *),
580 prefer union wait * as type of parm. */
581 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
582 && TREE_VALUE (p1) != TREE_VALUE (p2))
584 tree memb;
585 tree mv2 = TREE_VALUE (p2);
586 if (mv2 && mv2 != error_mark_node
587 && TREE_CODE (mv2) != ARRAY_TYPE)
588 mv2 = TYPE_MAIN_VARIANT (mv2);
589 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
590 memb; memb = DECL_CHAIN (memb))
592 tree mv3 = TREE_TYPE (memb);
593 if (mv3 && mv3 != error_mark_node
594 && TREE_CODE (mv3) != ARRAY_TYPE)
595 mv3 = TYPE_MAIN_VARIANT (mv3);
596 if (comptypes (mv3, mv2))
598 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
599 TREE_VALUE (p2));
600 pedwarn (input_location, OPT_Wpedantic,
601 "function types not truly compatible in ISO C");
602 goto parm_done;
606 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
607 && TREE_VALUE (p2) != TREE_VALUE (p1))
609 tree memb;
610 tree mv1 = TREE_VALUE (p1);
611 if (mv1 && mv1 != error_mark_node
612 && TREE_CODE (mv1) != ARRAY_TYPE)
613 mv1 = TYPE_MAIN_VARIANT (mv1);
614 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
615 memb; memb = DECL_CHAIN (memb))
617 tree mv3 = TREE_TYPE (memb);
618 if (mv3 && mv3 != error_mark_node
619 && TREE_CODE (mv3) != ARRAY_TYPE)
620 mv3 = TYPE_MAIN_VARIANT (mv3);
621 if (comptypes (mv3, mv1))
623 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
624 TREE_VALUE (p1));
625 pedwarn (input_location, OPT_Wpedantic,
626 "function types not truly compatible in ISO C");
627 goto parm_done;
631 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
632 parm_done: ;
635 t1 = build_function_type (valtype, newargs);
636 t1 = qualify_type (t1, t2);
638 /* FALLTHRU */
640 default:
641 return build_type_attribute_variant (t1, attributes);
646 /* Return the type of a conditional expression between pointers to
647 possibly differently qualified versions of compatible types.
649 We assume that comp_target_types has already been done and returned
650 nonzero; if that isn't so, this may crash. */
652 static tree
653 common_pointer_type (tree t1, tree t2)
655 tree attributes;
656 tree pointed_to_1, mv1;
657 tree pointed_to_2, mv2;
658 tree target;
659 unsigned target_quals;
660 addr_space_t as1, as2, as_common;
661 int quals1, quals2;
663 /* Save time if the two types are the same. */
665 if (t1 == t2) return t1;
667 /* If one type is nonsense, use the other. */
668 if (t1 == error_mark_node)
669 return t2;
670 if (t2 == error_mark_node)
671 return t1;
673 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
674 && TREE_CODE (t2) == POINTER_TYPE);
676 /* Merge the attributes. */
677 attributes = targetm.merge_type_attributes (t1, t2);
679 /* Find the composite type of the target types, and combine the
680 qualifiers of the two types' targets. Do not lose qualifiers on
681 array element types by taking the TYPE_MAIN_VARIANT. */
682 mv1 = pointed_to_1 = TREE_TYPE (t1);
683 mv2 = pointed_to_2 = TREE_TYPE (t2);
684 if (TREE_CODE (mv1) != ARRAY_TYPE)
685 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
686 if (TREE_CODE (mv2) != ARRAY_TYPE)
687 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
688 target = composite_type (mv1, mv2);
690 /* Strip array types to get correct qualifier for pointers to arrays */
691 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
692 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
694 /* For function types do not merge const qualifiers, but drop them
695 if used inconsistently. The middle-end uses these to mark const
696 and noreturn functions. */
697 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
698 target_quals = (quals1 & quals2);
699 else
700 target_quals = (quals1 | quals2);
702 /* If the two named address spaces are different, determine the common
703 superset address space. This is guaranteed to exist due to the
704 assumption that comp_target_type returned non-zero. */
705 as1 = TYPE_ADDR_SPACE (pointed_to_1);
706 as2 = TYPE_ADDR_SPACE (pointed_to_2);
707 if (!addr_space_superset (as1, as2, &as_common))
708 gcc_unreachable ();
710 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
712 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
713 return build_type_attribute_variant (t1, attributes);
716 /* Return the common type for two arithmetic types under the usual
717 arithmetic conversions. The default conversions have already been
718 applied, and enumerated types converted to their compatible integer
719 types. The resulting type is unqualified and has no attributes.
721 This is the type for the result of most arithmetic operations
722 if the operands have the given two types. */
724 static tree
725 c_common_type (tree t1, tree t2)
727 enum tree_code code1;
728 enum tree_code code2;
730 /* If one type is nonsense, use the other. */
731 if (t1 == error_mark_node)
732 return t2;
733 if (t2 == error_mark_node)
734 return t1;
736 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
737 t1 = TYPE_MAIN_VARIANT (t1);
739 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
740 t2 = TYPE_MAIN_VARIANT (t2);
742 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
744 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
745 t1 = build_type_attribute_variant (t1, attrs);
748 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
750 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
751 t2 = build_type_attribute_variant (t2, attrs);
754 /* Save time if the two types are the same. */
756 if (t1 == t2) return t1;
758 code1 = TREE_CODE (t1);
759 code2 = TREE_CODE (t2);
761 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
762 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
763 || code1 == INTEGER_TYPE);
764 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
765 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
766 || code2 == INTEGER_TYPE);
768 /* When one operand is a decimal float type, the other operand cannot be
769 a generic float type or a complex type. We also disallow vector types
770 here. */
771 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
772 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
774 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
776 error ("cannot mix operands of decimal floating and vector types");
777 return error_mark_node;
779 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
781 error ("cannot mix operands of decimal floating and complex types");
782 return error_mark_node;
784 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
786 error ("cannot mix operands of decimal floating "
787 "and other floating types");
788 return error_mark_node;
792 /* If one type is a vector type, return that type. (How the usual
793 arithmetic conversions apply to the vector types extension is not
794 precisely specified.) */
795 if (code1 == VECTOR_TYPE)
796 return t1;
798 if (code2 == VECTOR_TYPE)
799 return t2;
801 /* If one type is complex, form the common type of the non-complex
802 components, then make that complex. Use T1 or T2 if it is the
803 required type. */
804 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
806 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
807 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
808 tree subtype = c_common_type (subtype1, subtype2);
810 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
811 return t1;
812 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
813 return t2;
814 else
815 return build_complex_type (subtype);
818 /* If only one is real, use it as the result. */
820 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
821 return t1;
823 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
824 return t2;
826 /* If both are real and either are decimal floating point types, use
827 the decimal floating point type with the greater precision. */
829 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
831 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
832 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
833 return dfloat128_type_node;
834 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
835 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
836 return dfloat64_type_node;
837 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
838 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
839 return dfloat32_type_node;
842 /* Deal with fixed-point types. */
843 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
845 unsigned int unsignedp = 0, satp = 0;
846 scalar_mode m1, m2;
847 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
849 m1 = SCALAR_TYPE_MODE (t1);
850 m2 = SCALAR_TYPE_MODE (t2);
852 /* If one input type is saturating, the result type is saturating. */
853 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
854 satp = 1;
856 /* If both fixed-point types are unsigned, the result type is unsigned.
857 When mixing fixed-point and integer types, follow the sign of the
858 fixed-point type.
859 Otherwise, the result type is signed. */
860 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
861 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
862 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
863 && TYPE_UNSIGNED (t1))
864 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
865 && TYPE_UNSIGNED (t2)))
866 unsignedp = 1;
868 /* The result type is signed. */
869 if (unsignedp == 0)
871 /* If the input type is unsigned, we need to convert to the
872 signed type. */
873 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
875 enum mode_class mclass = (enum mode_class) 0;
876 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
877 mclass = MODE_FRACT;
878 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
879 mclass = MODE_ACCUM;
880 else
881 gcc_unreachable ();
882 m1 = as_a <scalar_mode>
883 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
885 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
887 enum mode_class mclass = (enum mode_class) 0;
888 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
889 mclass = MODE_FRACT;
890 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
891 mclass = MODE_ACCUM;
892 else
893 gcc_unreachable ();
894 m2 = as_a <scalar_mode>
895 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
899 if (code1 == FIXED_POINT_TYPE)
901 fbit1 = GET_MODE_FBIT (m1);
902 ibit1 = GET_MODE_IBIT (m1);
904 else
906 fbit1 = 0;
907 /* Signed integers need to subtract one sign bit. */
908 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
911 if (code2 == FIXED_POINT_TYPE)
913 fbit2 = GET_MODE_FBIT (m2);
914 ibit2 = GET_MODE_IBIT (m2);
916 else
918 fbit2 = 0;
919 /* Signed integers need to subtract one sign bit. */
920 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
923 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
924 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
925 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
926 satp);
929 /* Both real or both integers; use the one with greater precision. */
931 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
932 return t1;
933 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
934 return t2;
936 /* Same precision. Prefer long longs to longs to ints when the
937 same precision, following the C99 rules on integer type rank
938 (which are equivalent to the C90 rules for C90 types). */
940 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
941 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
942 return long_long_unsigned_type_node;
944 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
945 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
947 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
948 return long_long_unsigned_type_node;
949 else
950 return long_long_integer_type_node;
953 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
954 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
955 return long_unsigned_type_node;
957 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
958 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
960 /* But preserve unsignedness from the other type,
961 since long cannot hold all the values of an unsigned int. */
962 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
963 return long_unsigned_type_node;
964 else
965 return long_integer_type_node;
968 /* For floating types of the same TYPE_PRECISION (which we here
969 assume means either the same set of values, or sets of values
970 neither a subset of the other, with behavior being undefined in
971 the latter case), follow the rules from TS 18661-3: prefer
972 interchange types _FloatN, then standard types long double,
973 double, float, then extended types _FloatNx. For extended types,
974 check them starting with _Float128x as that seems most consistent
975 in spirit with preferring long double to double; for interchange
976 types, also check in that order for consistency although it's not
977 possible for more than one of them to have the same
978 precision. */
979 tree mv1 = TYPE_MAIN_VARIANT (t1);
980 tree mv2 = TYPE_MAIN_VARIANT (t2);
982 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
983 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
984 return FLOATN_TYPE_NODE (i);
986 /* Likewise, prefer long double to double even if same size. */
987 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
988 return long_double_type_node;
990 /* Likewise, prefer double to float even if same size.
991 We got a couple of embedded targets with 32 bit doubles, and the
992 pdp11 might have 64 bit floats. */
993 if (mv1 == double_type_node || mv2 == double_type_node)
994 return double_type_node;
996 if (mv1 == float_type_node || mv2 == float_type_node)
997 return float_type_node;
999 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1000 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1001 return FLOATNX_TYPE_NODE (i);
1003 /* Otherwise prefer the unsigned one. */
1005 if (TYPE_UNSIGNED (t1))
1006 return t1;
1007 else
1008 return t2;
1011 /* Wrapper around c_common_type that is used by c-common.c and other
1012 front end optimizations that remove promotions. ENUMERAL_TYPEs
1013 are allowed here and are converted to their compatible integer types.
1014 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1015 preferably a non-Boolean type as the common type. */
1016 tree
1017 common_type (tree t1, tree t2)
1019 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1020 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1021 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1022 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1024 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1025 if (TREE_CODE (t1) == BOOLEAN_TYPE
1026 && TREE_CODE (t2) == BOOLEAN_TYPE)
1027 return boolean_type_node;
1029 /* If either type is BOOLEAN_TYPE, then return the other. */
1030 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1031 return t2;
1032 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1033 return t1;
1035 return c_common_type (t1, t2);
1038 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1039 or various other operations. Return 2 if they are compatible
1040 but a warning may be needed if you use them together. */
1043 comptypes (tree type1, tree type2)
1045 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1046 int val;
1048 val = comptypes_internal (type1, type2, NULL, NULL);
1049 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1051 return val;
1054 /* Like comptypes, but if it returns non-zero because enum and int are
1055 compatible, it sets *ENUM_AND_INT_P to true. */
1057 static int
1058 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1060 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1061 int val;
1063 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1064 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1066 return val;
1069 /* Like comptypes, but if it returns nonzero for different types, it
1070 sets *DIFFERENT_TYPES_P to true. */
1073 comptypes_check_different_types (tree type1, tree type2,
1074 bool *different_types_p)
1076 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1077 int val;
1079 val = comptypes_internal (type1, type2, NULL, different_types_p);
1080 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1082 return val;
1085 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1086 or various other operations. Return 2 if they are compatible
1087 but a warning may be needed if you use them together. If
1088 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1089 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1090 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1091 NULL, and the types are compatible but different enough not to be
1092 permitted in C11 typedef redeclarations, then this sets
1093 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1094 false, but may or may not be set if the types are incompatible.
1095 This differs from comptypes, in that we don't free the seen
1096 types. */
1098 static int
1099 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1100 bool *different_types_p)
1102 const_tree t1 = type1;
1103 const_tree t2 = type2;
1104 int attrval, val;
1106 /* Suppress errors caused by previously reported errors. */
1108 if (t1 == t2 || !t1 || !t2
1109 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1110 return 1;
1112 /* Enumerated types are compatible with integer types, but this is
1113 not transitive: two enumerated types in the same translation unit
1114 are compatible with each other only if they are the same type. */
1116 if (TREE_CODE (t1) == ENUMERAL_TYPE
1117 && COMPLETE_TYPE_P (t1)
1118 && TREE_CODE (t2) != ENUMERAL_TYPE)
1120 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1121 if (TREE_CODE (t2) != VOID_TYPE)
1123 if (enum_and_int_p != NULL)
1124 *enum_and_int_p = true;
1125 if (different_types_p != NULL)
1126 *different_types_p = true;
1129 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1130 && COMPLETE_TYPE_P (t2)
1131 && TREE_CODE (t1) != ENUMERAL_TYPE)
1133 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1134 if (TREE_CODE (t1) != VOID_TYPE)
1136 if (enum_and_int_p != NULL)
1137 *enum_and_int_p = true;
1138 if (different_types_p != NULL)
1139 *different_types_p = true;
1143 if (t1 == t2)
1144 return 1;
1146 /* Different classes of types can't be compatible. */
1148 if (TREE_CODE (t1) != TREE_CODE (t2))
1149 return 0;
1151 /* Qualifiers must match. C99 6.7.3p9 */
1153 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1154 return 0;
1156 /* Allow for two different type nodes which have essentially the same
1157 definition. Note that we already checked for equality of the type
1158 qualifiers (just above). */
1160 if (TREE_CODE (t1) != ARRAY_TYPE
1161 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1162 return 1;
1164 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1165 if (!(attrval = comp_type_attributes (t1, t2)))
1166 return 0;
1168 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1169 val = 0;
1171 switch (TREE_CODE (t1))
1173 case INTEGER_TYPE:
1174 case FIXED_POINT_TYPE:
1175 case REAL_TYPE:
1176 /* With these nodes, we can't determine type equivalence by
1177 looking at what is stored in the nodes themselves, because
1178 two nodes might have different TYPE_MAIN_VARIANTs but still
1179 represent the same type. For example, wchar_t and int could
1180 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1181 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1182 and are distinct types. On the other hand, int and the
1183 following typedef
1185 typedef int INT __attribute((may_alias));
1187 have identical properties, different TYPE_MAIN_VARIANTs, but
1188 represent the same type. The canonical type system keeps
1189 track of equivalence in this case, so we fall back on it. */
1190 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1192 case POINTER_TYPE:
1193 /* Do not remove mode information. */
1194 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1195 break;
1196 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1197 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1198 enum_and_int_p, different_types_p));
1199 break;
1201 case FUNCTION_TYPE:
1202 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1203 different_types_p);
1204 break;
1206 case ARRAY_TYPE:
1208 tree d1 = TYPE_DOMAIN (t1);
1209 tree d2 = TYPE_DOMAIN (t2);
1210 bool d1_variable, d2_variable;
1211 bool d1_zero, d2_zero;
1212 val = 1;
1214 /* Target types must match incl. qualifiers. */
1215 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1216 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1217 enum_and_int_p,
1218 different_types_p)) == 0)
1219 return 0;
1221 if (different_types_p != NULL
1222 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1223 *different_types_p = true;
1224 /* Sizes must match unless one is missing or variable. */
1225 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1226 break;
1228 d1_zero = !TYPE_MAX_VALUE (d1);
1229 d2_zero = !TYPE_MAX_VALUE (d2);
1231 d1_variable = (!d1_zero
1232 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1233 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1234 d2_variable = (!d2_zero
1235 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1236 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1237 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1238 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1240 if (different_types_p != NULL
1241 && d1_variable != d2_variable)
1242 *different_types_p = true;
1243 if (d1_variable || d2_variable)
1244 break;
1245 if (d1_zero && d2_zero)
1246 break;
1247 if (d1_zero || d2_zero
1248 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1249 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1250 val = 0;
1252 break;
1255 case ENUMERAL_TYPE:
1256 case RECORD_TYPE:
1257 case UNION_TYPE:
1258 if (val != 1 && !same_translation_unit_p (t1, t2))
1260 tree a1 = TYPE_ATTRIBUTES (t1);
1261 tree a2 = TYPE_ATTRIBUTES (t2);
1263 if (! attribute_list_contained (a1, a2)
1264 && ! attribute_list_contained (a2, a1))
1265 break;
1267 if (attrval != 2)
1268 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1269 different_types_p);
1270 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1271 different_types_p);
1273 break;
1275 case VECTOR_TYPE:
1276 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1277 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1278 enum_and_int_p, different_types_p));
1279 break;
1281 default:
1282 break;
1284 return attrval == 2 && val == 1 ? 2 : val;
1287 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1288 their qualifiers, except for named address spaces. If the pointers point to
1289 different named addresses, then we must determine if one address space is a
1290 subset of the other. */
1292 static int
1293 comp_target_types (location_t location, tree ttl, tree ttr)
1295 int val;
1296 int val_ped;
1297 tree mvl = TREE_TYPE (ttl);
1298 tree mvr = TREE_TYPE (ttr);
1299 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1300 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1301 addr_space_t as_common;
1302 bool enum_and_int_p;
1304 /* Fail if pointers point to incompatible address spaces. */
1305 if (!addr_space_superset (asl, asr, &as_common))
1306 return 0;
1308 /* For pedantic record result of comptypes on arrays before losing
1309 qualifiers on the element type below. */
1310 val_ped = 1;
1312 if (TREE_CODE (mvl) == ARRAY_TYPE
1313 && TREE_CODE (mvr) == ARRAY_TYPE)
1314 val_ped = comptypes (mvl, mvr);
1316 /* Qualifiers on element types of array types that are
1317 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1319 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1320 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1321 : TYPE_MAIN_VARIANT (mvl));
1323 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1324 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1325 : TYPE_MAIN_VARIANT (mvr));
1327 enum_and_int_p = false;
1328 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1330 if (val == 1 && val_ped != 1)
1331 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1332 "are incompatible in ISO C");
1334 if (val == 2)
1335 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1337 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1338 warning_at (location, OPT_Wc___compat,
1339 "pointer target types incompatible in C++");
1341 return val;
1344 /* Subroutines of `comptypes'. */
1346 /* Determine whether two trees derive from the same translation unit.
1347 If the CONTEXT chain ends in a null, that tree's context is still
1348 being parsed, so if two trees have context chains ending in null,
1349 they're in the same translation unit. */
1351 bool
1352 same_translation_unit_p (const_tree t1, const_tree t2)
1354 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1355 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1357 case tcc_declaration:
1358 t1 = DECL_CONTEXT (t1); break;
1359 case tcc_type:
1360 t1 = TYPE_CONTEXT (t1); break;
1361 case tcc_exceptional:
1362 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1363 default: gcc_unreachable ();
1366 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1367 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1369 case tcc_declaration:
1370 t2 = DECL_CONTEXT (t2); break;
1371 case tcc_type:
1372 t2 = TYPE_CONTEXT (t2); break;
1373 case tcc_exceptional:
1374 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1375 default: gcc_unreachable ();
1378 return t1 == t2;
1381 /* Allocate the seen two types, assuming that they are compatible. */
1383 static struct tagged_tu_seen_cache *
1384 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1386 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1387 tu->next = tagged_tu_seen_base;
1388 tu->t1 = t1;
1389 tu->t2 = t2;
1391 tagged_tu_seen_base = tu;
1393 /* The C standard says that two structures in different translation
1394 units are compatible with each other only if the types of their
1395 fields are compatible (among other things). We assume that they
1396 are compatible until proven otherwise when building the cache.
1397 An example where this can occur is:
1398 struct a
1400 struct a *next;
1402 If we are comparing this against a similar struct in another TU,
1403 and did not assume they were compatible, we end up with an infinite
1404 loop. */
1405 tu->val = 1;
1406 return tu;
1409 /* Free the seen types until we get to TU_TIL. */
1411 static void
1412 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1414 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1415 while (tu != tu_til)
1417 const struct tagged_tu_seen_cache *const tu1
1418 = (const struct tagged_tu_seen_cache *) tu;
1419 tu = tu1->next;
1420 XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1422 tagged_tu_seen_base = tu_til;
1425 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1426 compatible. If the two types are not the same (which has been
1427 checked earlier), this can only happen when multiple translation
1428 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1429 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1430 comptypes_internal. */
1432 static int
1433 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1434 bool *enum_and_int_p, bool *different_types_p)
1436 tree s1, s2;
1437 bool needs_warning = false;
1439 /* We have to verify that the tags of the types are the same. This
1440 is harder than it looks because this may be a typedef, so we have
1441 to go look at the original type. It may even be a typedef of a
1442 typedef...
1443 In the case of compiler-created builtin structs the TYPE_DECL
1444 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1445 while (TYPE_NAME (t1)
1446 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1447 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1448 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1450 while (TYPE_NAME (t2)
1451 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1452 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1453 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1455 /* C90 didn't have the requirement that the two tags be the same. */
1456 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1457 return 0;
1459 /* C90 didn't say what happened if one or both of the types were
1460 incomplete; we choose to follow C99 rules here, which is that they
1461 are compatible. */
1462 if (TYPE_SIZE (t1) == NULL
1463 || TYPE_SIZE (t2) == NULL)
1464 return 1;
1467 const struct tagged_tu_seen_cache * tts_i;
1468 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1469 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1470 return tts_i->val;
1473 switch (TREE_CODE (t1))
1475 case ENUMERAL_TYPE:
1477 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1478 /* Speed up the case where the type values are in the same order. */
1479 tree tv1 = TYPE_VALUES (t1);
1480 tree tv2 = TYPE_VALUES (t2);
1482 if (tv1 == tv2)
1484 return 1;
1487 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1489 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1490 break;
1491 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1493 tu->val = 0;
1494 return 0;
1498 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1500 return 1;
1502 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1504 tu->val = 0;
1505 return 0;
1508 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1510 tu->val = 0;
1511 return 0;
1514 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1516 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1517 if (s2 == NULL
1518 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1520 tu->val = 0;
1521 return 0;
1524 return 1;
1527 case UNION_TYPE:
1529 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1530 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1532 tu->val = 0;
1533 return 0;
1536 /* Speed up the common case where the fields are in the same order. */
1537 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1538 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1540 int result;
1542 if (DECL_NAME (s1) != DECL_NAME (s2))
1543 break;
1544 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1545 enum_and_int_p, different_types_p);
1547 if (result != 1 && !DECL_NAME (s1))
1548 break;
1549 if (result == 0)
1551 tu->val = 0;
1552 return 0;
1554 if (result == 2)
1555 needs_warning = true;
1557 if (TREE_CODE (s1) == FIELD_DECL
1558 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1559 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1561 tu->val = 0;
1562 return 0;
1565 if (!s1 && !s2)
1567 tu->val = needs_warning ? 2 : 1;
1568 return tu->val;
1571 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1573 bool ok = false;
1575 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1576 if (DECL_NAME (s1) == DECL_NAME (s2))
1578 int result;
1580 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1581 enum_and_int_p,
1582 different_types_p);
1584 if (result != 1 && !DECL_NAME (s1))
1585 continue;
1586 if (result == 0)
1588 tu->val = 0;
1589 return 0;
1591 if (result == 2)
1592 needs_warning = true;
1594 if (TREE_CODE (s1) == FIELD_DECL
1595 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1596 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1597 break;
1599 ok = true;
1600 break;
1602 if (!ok)
1604 tu->val = 0;
1605 return 0;
1608 tu->val = needs_warning ? 2 : 10;
1609 return tu->val;
1612 case RECORD_TYPE:
1614 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1616 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1617 s1 && s2;
1618 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1620 int result;
1621 if (TREE_CODE (s1) != TREE_CODE (s2)
1622 || DECL_NAME (s1) != DECL_NAME (s2))
1623 break;
1624 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1625 enum_and_int_p, different_types_p);
1626 if (result == 0)
1627 break;
1628 if (result == 2)
1629 needs_warning = true;
1631 if (TREE_CODE (s1) == FIELD_DECL
1632 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1633 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1634 break;
1636 if (s1 && s2)
1637 tu->val = 0;
1638 else
1639 tu->val = needs_warning ? 2 : 1;
1640 return tu->val;
1643 default:
1644 gcc_unreachable ();
1648 /* Return 1 if two function types F1 and F2 are compatible.
1649 If either type specifies no argument types,
1650 the other must specify a fixed number of self-promoting arg types.
1651 Otherwise, if one type specifies only the number of arguments,
1652 the other must specify that number of self-promoting arg types.
1653 Otherwise, the argument types must match.
1654 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1656 static int
1657 function_types_compatible_p (const_tree f1, const_tree f2,
1658 bool *enum_and_int_p, bool *different_types_p)
1660 tree args1, args2;
1661 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1662 int val = 1;
1663 int val1;
1664 tree ret1, ret2;
1666 ret1 = TREE_TYPE (f1);
1667 ret2 = TREE_TYPE (f2);
1669 /* 'volatile' qualifiers on a function's return type used to mean
1670 the function is noreturn. */
1671 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1672 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1673 if (TYPE_VOLATILE (ret1))
1674 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1675 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1676 if (TYPE_VOLATILE (ret2))
1677 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1678 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1679 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1680 if (val == 0)
1681 return 0;
1683 args1 = TYPE_ARG_TYPES (f1);
1684 args2 = TYPE_ARG_TYPES (f2);
1686 if (different_types_p != NULL
1687 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1688 *different_types_p = true;
1690 /* An unspecified parmlist matches any specified parmlist
1691 whose argument types don't need default promotions. */
1693 if (args1 == NULL_TREE)
1695 if (flag_isoc2x ? stdarg_p (f2) : !self_promoting_args_p (args2))
1696 return 0;
1697 /* If one of these types comes from a non-prototype fn definition,
1698 compare that with the other type's arglist.
1699 If they don't match, ask for a warning (but no error). */
1700 if (TYPE_ACTUAL_ARG_TYPES (f1)
1701 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1702 enum_and_int_p, different_types_p) != 1)
1703 val = 2;
1704 return val;
1706 if (args2 == NULL_TREE)
1708 if (flag_isoc2x ? stdarg_p (f1) : !self_promoting_args_p (args1))
1709 return 0;
1710 if (TYPE_ACTUAL_ARG_TYPES (f2)
1711 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1712 enum_and_int_p, different_types_p) != 1)
1713 val = 2;
1714 return val;
1717 /* Both types have argument lists: compare them and propagate results. */
1718 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1719 different_types_p);
1720 return val1 != 1 ? val1 : val;
1723 /* Check two lists of types for compatibility, returning 0 for
1724 incompatible, 1 for compatible, or 2 for compatible with
1725 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1726 comptypes_internal. */
1728 static int
1729 type_lists_compatible_p (const_tree args1, const_tree args2,
1730 bool *enum_and_int_p, bool *different_types_p)
1732 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1733 int val = 1;
1734 int newval = 0;
1736 while (1)
1738 tree a1, mv1, a2, mv2;
1739 if (args1 == NULL_TREE && args2 == NULL_TREE)
1740 return val;
1741 /* If one list is shorter than the other,
1742 they fail to match. */
1743 if (args1 == NULL_TREE || args2 == NULL_TREE)
1744 return 0;
1745 mv1 = a1 = TREE_VALUE (args1);
1746 mv2 = a2 = TREE_VALUE (args2);
1747 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1748 mv1 = (TYPE_ATOMIC (mv1)
1749 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1750 TYPE_QUAL_ATOMIC)
1751 : TYPE_MAIN_VARIANT (mv1));
1752 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1753 mv2 = (TYPE_ATOMIC (mv2)
1754 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1755 TYPE_QUAL_ATOMIC)
1756 : TYPE_MAIN_VARIANT (mv2));
1757 /* A null pointer instead of a type
1758 means there is supposed to be an argument
1759 but nothing is specified about what type it has.
1760 So match anything that self-promotes. */
1761 if (different_types_p != NULL
1762 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1763 *different_types_p = true;
1764 if (a1 == NULL_TREE)
1766 if (c_type_promotes_to (a2) != a2)
1767 return 0;
1769 else if (a2 == NULL_TREE)
1771 if (c_type_promotes_to (a1) != a1)
1772 return 0;
1774 /* If one of the lists has an error marker, ignore this arg. */
1775 else if (TREE_CODE (a1) == ERROR_MARK
1776 || TREE_CODE (a2) == ERROR_MARK)
1778 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1779 different_types_p)))
1781 if (different_types_p != NULL)
1782 *different_types_p = true;
1783 /* Allow wait (union {union wait *u; int *i} *)
1784 and wait (union wait *) to be compatible. */
1785 if (TREE_CODE (a1) == UNION_TYPE
1786 && (TYPE_NAME (a1) == NULL_TREE
1787 || TYPE_TRANSPARENT_AGGR (a1))
1788 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1789 && tree_int_cst_equal (TYPE_SIZE (a1),
1790 TYPE_SIZE (a2)))
1792 tree memb;
1793 for (memb = TYPE_FIELDS (a1);
1794 memb; memb = DECL_CHAIN (memb))
1796 tree mv3 = TREE_TYPE (memb);
1797 if (mv3 && mv3 != error_mark_node
1798 && TREE_CODE (mv3) != ARRAY_TYPE)
1799 mv3 = (TYPE_ATOMIC (mv3)
1800 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1801 TYPE_QUAL_ATOMIC)
1802 : TYPE_MAIN_VARIANT (mv3));
1803 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1804 different_types_p))
1805 break;
1807 if (memb == NULL_TREE)
1808 return 0;
1810 else if (TREE_CODE (a2) == UNION_TYPE
1811 && (TYPE_NAME (a2) == NULL_TREE
1812 || TYPE_TRANSPARENT_AGGR (a2))
1813 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1814 && tree_int_cst_equal (TYPE_SIZE (a2),
1815 TYPE_SIZE (a1)))
1817 tree memb;
1818 for (memb = TYPE_FIELDS (a2);
1819 memb; memb = DECL_CHAIN (memb))
1821 tree mv3 = TREE_TYPE (memb);
1822 if (mv3 && mv3 != error_mark_node
1823 && TREE_CODE (mv3) != ARRAY_TYPE)
1824 mv3 = (TYPE_ATOMIC (mv3)
1825 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1826 TYPE_QUAL_ATOMIC)
1827 : TYPE_MAIN_VARIANT (mv3));
1828 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1829 different_types_p))
1830 break;
1832 if (memb == NULL_TREE)
1833 return 0;
1835 else
1836 return 0;
1839 /* comptypes said ok, but record if it said to warn. */
1840 if (newval > val)
1841 val = newval;
1843 args1 = TREE_CHAIN (args1);
1844 args2 = TREE_CHAIN (args2);
1848 /* Compute the size to increment a pointer by. When a function type or void
1849 type or incomplete type is passed, size_one_node is returned.
1850 This function does not emit any diagnostics; the caller is responsible
1851 for that. */
1853 static tree
1854 c_size_in_bytes (const_tree type)
1856 enum tree_code code = TREE_CODE (type);
1858 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1859 || !COMPLETE_TYPE_P (type))
1860 return size_one_node;
1862 /* Convert in case a char is more than one unit. */
1863 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1864 size_int (TYPE_PRECISION (char_type_node)
1865 / BITS_PER_UNIT));
1868 /* Return either DECL or its known constant value (if it has one). */
1870 tree
1871 decl_constant_value_1 (tree decl, bool in_init)
1873 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1874 TREE_CODE (decl) != PARM_DECL
1875 && !TREE_THIS_VOLATILE (decl)
1876 && TREE_READONLY (decl)
1877 && DECL_INITIAL (decl) != NULL_TREE
1878 && !error_operand_p (DECL_INITIAL (decl))
1879 /* This is invalid if initial value is not constant.
1880 If it has either a function call, a memory reference,
1881 or a variable, then re-evaluating it could give different results. */
1882 && TREE_CONSTANT (DECL_INITIAL (decl))
1883 /* Check for cases where this is sub-optimal, even though valid. */
1884 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1885 return DECL_INITIAL (decl);
1886 return decl;
1889 /* Return either DECL or its known constant value (if it has one).
1890 Like the above, but always return decl outside of functions. */
1892 tree
1893 decl_constant_value (tree decl)
1895 /* Don't change a variable array bound or initial value to a constant
1896 in a place where a variable is invalid. */
1897 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1900 /* Convert the array expression EXP to a pointer. */
1901 static tree
1902 array_to_pointer_conversion (location_t loc, tree exp)
1904 tree orig_exp = exp;
1905 tree type = TREE_TYPE (exp);
1906 tree adr;
1907 tree restype = TREE_TYPE (type);
1908 tree ptrtype;
1910 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1912 STRIP_TYPE_NOPS (exp);
1914 copy_warning (exp, orig_exp);
1916 ptrtype = build_pointer_type (restype);
1918 if (INDIRECT_REF_P (exp))
1919 return convert (ptrtype, TREE_OPERAND (exp, 0));
1921 /* In C++ array compound literals are temporary objects unless they are
1922 const or appear in namespace scope, so they are destroyed too soon
1923 to use them for much of anything (c++/53220). */
1924 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1926 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1927 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1928 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1929 "converting an array compound literal to a pointer "
1930 "is ill-formed in C++");
1933 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1934 return convert (ptrtype, adr);
1937 /* Convert the function expression EXP to a pointer. */
1938 static tree
1939 function_to_pointer_conversion (location_t loc, tree exp)
1941 tree orig_exp = exp;
1943 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1945 STRIP_TYPE_NOPS (exp);
1947 copy_warning (exp, orig_exp);
1949 return build_unary_op (loc, ADDR_EXPR, exp, false);
1952 /* Mark EXP as read, not just set, for set but not used -Wunused
1953 warning purposes. */
1955 void
1956 mark_exp_read (tree exp)
1958 switch (TREE_CODE (exp))
1960 case VAR_DECL:
1961 case PARM_DECL:
1962 DECL_READ_P (exp) = 1;
1963 break;
1964 case ARRAY_REF:
1965 case COMPONENT_REF:
1966 case MODIFY_EXPR:
1967 case REALPART_EXPR:
1968 case IMAGPART_EXPR:
1969 CASE_CONVERT:
1970 case ADDR_EXPR:
1971 case VIEW_CONVERT_EXPR:
1972 mark_exp_read (TREE_OPERAND (exp, 0));
1973 break;
1974 case COMPOUND_EXPR:
1975 /* Pattern match what build_atomic_assign produces with modifycode
1976 NOP_EXPR. */
1977 if (VAR_P (TREE_OPERAND (exp, 1))
1978 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1979 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1981 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1982 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1983 if (TREE_CODE (t1) == TARGET_EXPR
1984 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1985 && TREE_CODE (t2) == CALL_EXPR)
1987 tree fndecl = get_callee_fndecl (t2);
1988 tree arg = NULL_TREE;
1989 if (fndecl
1990 && TREE_CODE (fndecl) == FUNCTION_DECL
1991 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1992 && call_expr_nargs (t2) >= 2)
1993 switch (DECL_FUNCTION_CODE (fndecl))
1995 case BUILT_IN_ATOMIC_STORE:
1996 arg = CALL_EXPR_ARG (t2, 1);
1997 break;
1998 case BUILT_IN_ATOMIC_STORE_1:
1999 case BUILT_IN_ATOMIC_STORE_2:
2000 case BUILT_IN_ATOMIC_STORE_4:
2001 case BUILT_IN_ATOMIC_STORE_8:
2002 case BUILT_IN_ATOMIC_STORE_16:
2003 arg = CALL_EXPR_ARG (t2, 0);
2004 break;
2005 default:
2006 break;
2008 if (arg)
2010 STRIP_NOPS (arg);
2011 if (TREE_CODE (arg) == ADDR_EXPR
2012 && DECL_P (TREE_OPERAND (arg, 0))
2013 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2014 mark_exp_read (TREE_OPERAND (arg, 0));
2018 /* FALLTHRU */
2019 case C_MAYBE_CONST_EXPR:
2020 mark_exp_read (TREE_OPERAND (exp, 1));
2021 break;
2022 default:
2023 break;
2027 /* Perform the default conversion of arrays and functions to pointers.
2028 Return the result of converting EXP. For any other expression, just
2029 return EXP.
2031 LOC is the location of the expression. */
2033 struct c_expr
2034 default_function_array_conversion (location_t loc, struct c_expr exp)
2036 tree orig_exp = exp.value;
2037 tree type = TREE_TYPE (exp.value);
2038 enum tree_code code = TREE_CODE (type);
2040 switch (code)
2042 case ARRAY_TYPE:
2044 bool not_lvalue = false;
2045 bool lvalue_array_p;
2047 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2048 || CONVERT_EXPR_P (exp.value))
2049 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2051 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2052 not_lvalue = true;
2053 exp.value = TREE_OPERAND (exp.value, 0);
2056 copy_warning (exp.value, orig_exp);
2058 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2059 if (!flag_isoc99 && !lvalue_array_p)
2061 /* Before C99, non-lvalue arrays do not decay to pointers.
2062 Normally, using such an array would be invalid; but it can
2063 be used correctly inside sizeof or as a statement expression.
2064 Thus, do not give an error here; an error will result later. */
2065 return exp;
2068 exp.value = array_to_pointer_conversion (loc, exp.value);
2070 break;
2071 case FUNCTION_TYPE:
2072 exp.value = function_to_pointer_conversion (loc, exp.value);
2073 break;
2074 default:
2075 break;
2078 return exp;
2081 struct c_expr
2082 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2084 mark_exp_read (exp.value);
2085 return default_function_array_conversion (loc, exp);
2088 /* Return whether EXPR should be treated as an atomic lvalue for the
2089 purposes of load and store handling. */
2091 static bool
2092 really_atomic_lvalue (tree expr)
2094 if (error_operand_p (expr))
2095 return false;
2096 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2097 return false;
2098 if (!lvalue_p (expr))
2099 return false;
2101 /* Ignore _Atomic on register variables, since their addresses can't
2102 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2103 sequences wouldn't work. Ignore _Atomic on structures containing
2104 bit-fields, since accessing elements of atomic structures or
2105 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2106 it's undefined at translation time or execution time, and the
2107 normal atomic sequences again wouldn't work. */
2108 while (handled_component_p (expr))
2110 if (TREE_CODE (expr) == COMPONENT_REF
2111 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2112 return false;
2113 expr = TREE_OPERAND (expr, 0);
2115 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2116 return false;
2117 return true;
2120 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2121 including converting functions and arrays to pointers if CONVERT_P.
2122 If READ_P, also mark the expression as having been read. */
2124 struct c_expr
2125 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2126 bool convert_p, bool read_p)
2128 if (read_p)
2129 mark_exp_read (exp.value);
2130 if (convert_p)
2131 exp = default_function_array_conversion (loc, exp);
2132 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2133 exp.value = require_complete_type (loc, exp.value);
2134 if (really_atomic_lvalue (exp.value))
2136 vec<tree, va_gc> *params;
2137 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2138 tree expr_type = TREE_TYPE (exp.value);
2139 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2140 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2142 gcc_assert (TYPE_ATOMIC (expr_type));
2144 /* Expansion of a generic atomic load may require an addition
2145 element, so allocate enough to prevent a resize. */
2146 vec_alloc (params, 4);
2148 /* Remove the qualifiers for the rest of the expressions and
2149 create the VAL temp variable to hold the RHS. */
2150 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2151 tmp = create_tmp_var_raw (nonatomic_type);
2152 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2153 TREE_ADDRESSABLE (tmp) = 1;
2154 /* Do not disable warnings for TMP even though it's artificial.
2155 -Winvalid-memory-model depends on it. */
2157 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2158 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2159 params->quick_push (expr_addr);
2160 params->quick_push (tmp_addr);
2161 params->quick_push (seq_cst);
2162 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2164 /* EXPR is always read. */
2165 mark_exp_read (exp.value);
2167 /* Return tmp which contains the value loaded. */
2168 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2169 NULL_TREE, NULL_TREE);
2171 if (convert_p && !error_operand_p (exp.value)
2172 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2173 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2174 return exp;
2177 /* EXP is an expression of integer type. Apply the integer promotions
2178 to it and return the promoted value. */
2180 tree
2181 perform_integral_promotions (tree exp)
2183 tree type = TREE_TYPE (exp);
2184 enum tree_code code = TREE_CODE (type);
2186 gcc_assert (INTEGRAL_TYPE_P (type));
2188 /* Normally convert enums to int,
2189 but convert wide enums to something wider. */
2190 if (code == ENUMERAL_TYPE)
2192 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2193 TYPE_PRECISION (integer_type_node)),
2194 ((TYPE_PRECISION (type)
2195 >= TYPE_PRECISION (integer_type_node))
2196 && TYPE_UNSIGNED (type)));
2198 return convert (type, exp);
2201 /* ??? This should no longer be needed now bit-fields have their
2202 proper types. */
2203 if (TREE_CODE (exp) == COMPONENT_REF
2204 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2205 /* If it's thinner than an int, promote it like a
2206 c_promoting_integer_type_p, otherwise leave it alone. */
2207 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2208 TYPE_PRECISION (integer_type_node)) < 0)
2209 return convert (integer_type_node, exp);
2211 if (c_promoting_integer_type_p (type))
2213 /* Preserve unsignedness if not really getting any wider. */
2214 if (TYPE_UNSIGNED (type)
2215 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2216 return convert (unsigned_type_node, exp);
2218 return convert (integer_type_node, exp);
2221 return exp;
2225 /* Perform default promotions for C data used in expressions.
2226 Enumeral types or short or char are converted to int.
2227 In addition, manifest constants symbols are replaced by their values. */
2229 tree
2230 default_conversion (tree exp)
2232 tree orig_exp;
2233 tree type = TREE_TYPE (exp);
2234 enum tree_code code = TREE_CODE (type);
2235 tree promoted_type;
2237 mark_exp_read (exp);
2239 /* Functions and arrays have been converted during parsing. */
2240 gcc_assert (code != FUNCTION_TYPE);
2241 if (code == ARRAY_TYPE)
2242 return exp;
2244 /* Constants can be used directly unless they're not loadable. */
2245 if (TREE_CODE (exp) == CONST_DECL)
2246 exp = DECL_INITIAL (exp);
2248 /* Strip no-op conversions. */
2249 orig_exp = exp;
2250 STRIP_TYPE_NOPS (exp);
2252 copy_warning (exp, orig_exp);
2254 if (code == VOID_TYPE)
2256 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2257 "void value not ignored as it ought to be");
2258 return error_mark_node;
2261 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2262 if (exp == error_mark_node)
2263 return error_mark_node;
2265 promoted_type = targetm.promoted_type (type);
2266 if (promoted_type)
2267 return convert (promoted_type, exp);
2269 if (INTEGRAL_TYPE_P (type))
2270 return perform_integral_promotions (exp);
2272 return exp;
2275 /* Look up COMPONENT in a structure or union TYPE.
2277 If the component name is not found, returns NULL_TREE. Otherwise,
2278 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2279 stepping down the chain to the component, which is in the last
2280 TREE_VALUE of the list. Normally the list is of length one, but if
2281 the component is embedded within (nested) anonymous structures or
2282 unions, the list steps down the chain to the component. */
2284 static tree
2285 lookup_field (tree type, tree component)
2287 tree field;
2289 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2290 to the field elements. Use a binary search on this array to quickly
2291 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2292 will always be set for structures which have many elements.
2294 Duplicate field checking replaces duplicates with NULL_TREE so
2295 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2296 case just iterate using DECL_CHAIN. */
2298 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2299 && !seen_error ())
2301 int bot, top, half;
2302 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2304 field = TYPE_FIELDS (type);
2305 bot = 0;
2306 top = TYPE_LANG_SPECIFIC (type)->s->len;
2307 while (top - bot > 1)
2309 half = (top - bot + 1) >> 1;
2310 field = field_array[bot+half];
2312 if (DECL_NAME (field) == NULL_TREE)
2314 /* Step through all anon unions in linear fashion. */
2315 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2317 field = field_array[bot++];
2318 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2320 tree anon = lookup_field (TREE_TYPE (field), component);
2322 if (anon)
2323 return tree_cons (NULL_TREE, field, anon);
2325 /* The Plan 9 compiler permits referring
2326 directly to an anonymous struct/union field
2327 using a typedef name. */
2328 if (flag_plan9_extensions
2329 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2330 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2331 == TYPE_DECL)
2332 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2333 == component))
2334 break;
2338 /* Entire record is only anon unions. */
2339 if (bot > top)
2340 return NULL_TREE;
2342 /* Restart the binary search, with new lower bound. */
2343 continue;
2346 if (DECL_NAME (field) == component)
2347 break;
2348 if (DECL_NAME (field) < component)
2349 bot += half;
2350 else
2351 top = bot + half;
2354 if (DECL_NAME (field_array[bot]) == component)
2355 field = field_array[bot];
2356 else if (DECL_NAME (field) != component)
2357 return NULL_TREE;
2359 else
2361 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2363 if (DECL_NAME (field) == NULL_TREE
2364 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2366 tree anon = lookup_field (TREE_TYPE (field), component);
2368 if (anon)
2369 return tree_cons (NULL_TREE, field, anon);
2371 /* The Plan 9 compiler permits referring directly to an
2372 anonymous struct/union field using a typedef
2373 name. */
2374 if (flag_plan9_extensions
2375 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2376 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2377 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2378 == component))
2379 break;
2382 if (DECL_NAME (field) == component)
2383 break;
2386 if (field == NULL_TREE)
2387 return NULL_TREE;
2390 return tree_cons (NULL_TREE, field, NULL_TREE);
2393 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2395 static void
2396 lookup_field_fuzzy_find_candidates (tree type, tree component,
2397 vec<tree> *candidates)
2399 tree field;
2400 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2402 if (DECL_NAME (field) == NULL_TREE
2403 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2404 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2405 candidates);
2407 if (DECL_NAME (field))
2408 candidates->safe_push (DECL_NAME (field));
2412 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2413 rather than returning a TREE_LIST for an exact match. */
2415 static tree
2416 lookup_field_fuzzy (tree type, tree component)
2418 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2420 /* First, gather a list of candidates. */
2421 auto_vec <tree> candidates;
2423 lookup_field_fuzzy_find_candidates (type, component,
2424 &candidates);
2426 return find_closest_identifier (component, &candidates);
2429 /* Support function for build_component_ref's error-handling.
2431 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2432 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2434 static bool
2435 should_suggest_deref_p (tree datum_type)
2437 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2438 allows "." for ptrs; we could be handling a failed attempt
2439 to access a property. */
2440 if (c_dialect_objc ())
2441 return false;
2443 /* Only suggest it for pointers... */
2444 if (TREE_CODE (datum_type) != POINTER_TYPE)
2445 return false;
2447 /* ...to structs/unions. */
2448 tree underlying_type = TREE_TYPE (datum_type);
2449 enum tree_code code = TREE_CODE (underlying_type);
2450 if (code == RECORD_TYPE || code == UNION_TYPE)
2451 return true;
2452 else
2453 return false;
2456 /* Make an expression to refer to the COMPONENT field of structure or
2457 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2458 location of the COMPONENT_REF. COMPONENT_LOC is the location
2459 of COMPONENT. */
2461 tree
2462 build_component_ref (location_t loc, tree datum, tree component,
2463 location_t component_loc)
2465 tree type = TREE_TYPE (datum);
2466 enum tree_code code = TREE_CODE (type);
2467 tree field = NULL;
2468 tree ref;
2469 bool datum_lvalue = lvalue_p (datum);
2471 if (!objc_is_public (datum, component))
2472 return error_mark_node;
2474 /* Detect Objective-C property syntax object.property. */
2475 if (c_dialect_objc ()
2476 && (ref = objc_maybe_build_component_ref (datum, component)))
2477 return ref;
2479 /* See if there is a field or component with name COMPONENT. */
2481 if (code == RECORD_TYPE || code == UNION_TYPE)
2483 if (!COMPLETE_TYPE_P (type))
2485 c_incomplete_type_error (loc, NULL_TREE, type);
2486 return error_mark_node;
2489 field = lookup_field (type, component);
2491 if (!field)
2493 tree guessed_id = lookup_field_fuzzy (type, component);
2494 if (guessed_id)
2496 /* Attempt to provide a fixit replacement hint, if
2497 we have a valid range for the component. */
2498 location_t reported_loc
2499 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2500 gcc_rich_location rich_loc (reported_loc);
2501 if (component_loc != UNKNOWN_LOCATION)
2502 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2503 error_at (&rich_loc,
2504 "%qT has no member named %qE; did you mean %qE?",
2505 type, component, guessed_id);
2507 else
2508 error_at (loc, "%qT has no member named %qE", type, component);
2509 return error_mark_node;
2512 /* Accessing elements of atomic structures or unions is undefined
2513 behavior (C11 6.5.2.3#5). */
2514 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2516 if (code == RECORD_TYPE)
2517 warning_at (loc, 0, "accessing a member %qE of an atomic "
2518 "structure %qE", component, datum);
2519 else
2520 warning_at (loc, 0, "accessing a member %qE of an atomic "
2521 "union %qE", component, datum);
2524 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2525 This might be better solved in future the way the C++ front
2526 end does it - by giving the anonymous entities each a
2527 separate name and type, and then have build_component_ref
2528 recursively call itself. We can't do that here. */
2531 tree subdatum = TREE_VALUE (field);
2532 int quals;
2533 tree subtype;
2534 bool use_datum_quals;
2536 if (TREE_TYPE (subdatum) == error_mark_node)
2537 return error_mark_node;
2539 /* If this is an rvalue, it does not have qualifiers in C
2540 standard terms and we must avoid propagating such
2541 qualifiers down to a non-lvalue array that is then
2542 converted to a pointer. */
2543 use_datum_quals = (datum_lvalue
2544 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2546 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2547 if (use_datum_quals)
2548 quals |= TYPE_QUALS (TREE_TYPE (datum));
2549 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2551 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2552 NULL_TREE);
2553 SET_EXPR_LOCATION (ref, loc);
2554 if (TREE_READONLY (subdatum)
2555 || (use_datum_quals && TREE_READONLY (datum)))
2556 TREE_READONLY (ref) = 1;
2557 if (TREE_THIS_VOLATILE (subdatum)
2558 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2559 TREE_THIS_VOLATILE (ref) = 1;
2561 if (TREE_DEPRECATED (subdatum))
2562 warn_deprecated_use (subdatum, NULL_TREE);
2564 datum = ref;
2566 field = TREE_CHAIN (field);
2568 while (field);
2570 return ref;
2572 else if (should_suggest_deref_p (type))
2574 /* Special-case the error message for "ptr.field" for the case
2575 where the user has confused "." vs "->". */
2576 rich_location richloc (line_table, loc);
2577 /* "loc" should be the "." token. */
2578 richloc.add_fixit_replace ("->");
2579 error_at (&richloc,
2580 "%qE is a pointer; did you mean to use %<->%>?",
2581 datum);
2582 return error_mark_node;
2584 else if (code != ERROR_MARK)
2585 error_at (loc,
2586 "request for member %qE in something not a structure or union",
2587 component);
2589 return error_mark_node;
2592 /* Given an expression PTR for a pointer, return an expression
2593 for the value pointed to.
2594 ERRORSTRING is the name of the operator to appear in error messages.
2596 LOC is the location to use for the generated tree. */
2598 tree
2599 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2601 tree pointer = default_conversion (ptr);
2602 tree type = TREE_TYPE (pointer);
2603 tree ref;
2605 if (TREE_CODE (type) == POINTER_TYPE)
2607 if (CONVERT_EXPR_P (pointer)
2608 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2610 /* If a warning is issued, mark it to avoid duplicates from
2611 the backend. This only needs to be done at
2612 warn_strict_aliasing > 2. */
2613 if (warn_strict_aliasing > 2)
2614 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2615 type, TREE_OPERAND (pointer, 0)))
2616 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2619 if (TREE_CODE (pointer) == ADDR_EXPR
2620 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2621 == TREE_TYPE (type)))
2623 ref = TREE_OPERAND (pointer, 0);
2624 protected_set_expr_location (ref, loc);
2625 return ref;
2627 else
2629 tree t = TREE_TYPE (type);
2631 ref = build1 (INDIRECT_REF, t, pointer);
2633 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2634 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2636 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2637 so that we get the proper error message if the result is used
2638 to assign to. Also, &* is supposed to be a no-op.
2639 And ANSI C seems to specify that the type of the result
2640 should be the const type. */
2641 /* A de-reference of a pointer to const is not a const. It is valid
2642 to change it via some other pointer. */
2643 TREE_READONLY (ref) = TYPE_READONLY (t);
2644 TREE_SIDE_EFFECTS (ref)
2645 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2646 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2647 protected_set_expr_location (ref, loc);
2648 return ref;
2651 else if (TREE_CODE (pointer) != ERROR_MARK)
2652 invalid_indirection_error (loc, type, errstring);
2654 return error_mark_node;
2657 /* This handles expressions of the form "a[i]", which denotes
2658 an array reference.
2660 This is logically equivalent in C to *(a+i), but we may do it differently.
2661 If A is a variable or a member, we generate a primitive ARRAY_REF.
2662 This avoids forcing the array out of registers, and can work on
2663 arrays that are not lvalues (for example, members of structures returned
2664 by functions).
2666 For vector types, allow vector[i] but not i[vector], and create
2667 *(((type*)&vectortype) + i) for the expression.
2669 LOC is the location to use for the returned expression. */
2671 tree
2672 build_array_ref (location_t loc, tree array, tree index)
2674 tree ret;
2675 bool swapped = false;
2676 if (TREE_TYPE (array) == error_mark_node
2677 || TREE_TYPE (index) == error_mark_node)
2678 return error_mark_node;
2680 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2681 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2682 /* Allow vector[index] but not index[vector]. */
2683 && !gnu_vector_type_p (TREE_TYPE (array)))
2685 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2686 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2688 error_at (loc,
2689 "subscripted value is neither array nor pointer nor vector");
2691 return error_mark_node;
2693 std::swap (array, index);
2694 swapped = true;
2697 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2699 error_at (loc, "array subscript is not an integer");
2700 return error_mark_node;
2703 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2705 error_at (loc, "subscripted value is pointer to function");
2706 return error_mark_node;
2709 /* ??? Existing practice has been to warn only when the char
2710 index is syntactically the index, not for char[array]. */
2711 if (!swapped)
2712 warn_array_subscript_with_type_char (loc, index);
2714 /* Apply default promotions *after* noticing character types. */
2715 index = default_conversion (index);
2716 if (index == error_mark_node)
2717 return error_mark_node;
2719 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2721 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2722 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2724 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2726 tree rval, type;
2728 /* An array that is indexed by a non-constant
2729 cannot be stored in a register; we must be able to do
2730 address arithmetic on its address.
2731 Likewise an array of elements of variable size. */
2732 if (TREE_CODE (index) != INTEGER_CST
2733 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2734 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2736 if (!c_mark_addressable (array, true))
2737 return error_mark_node;
2739 /* An array that is indexed by a constant value which is not within
2740 the array bounds cannot be stored in a register either; because we
2741 would get a crash in store_bit_field/extract_bit_field when trying
2742 to access a non-existent part of the register. */
2743 if (TREE_CODE (index) == INTEGER_CST
2744 && TYPE_DOMAIN (TREE_TYPE (array))
2745 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2747 if (!c_mark_addressable (array))
2748 return error_mark_node;
2751 if ((pedantic || warn_c90_c99_compat)
2752 && ! was_vector)
2754 tree foo = array;
2755 while (TREE_CODE (foo) == COMPONENT_REF)
2756 foo = TREE_OPERAND (foo, 0);
2757 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2758 pedwarn (loc, OPT_Wpedantic,
2759 "ISO C forbids subscripting %<register%> array");
2760 else if (!lvalue_p (foo))
2761 pedwarn_c90 (loc, OPT_Wpedantic,
2762 "ISO C90 forbids subscripting non-lvalue "
2763 "array");
2766 type = TREE_TYPE (TREE_TYPE (array));
2767 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2768 /* Array ref is const/volatile if the array elements are
2769 or if the array is. */
2770 TREE_READONLY (rval)
2771 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2772 | TREE_READONLY (array));
2773 TREE_SIDE_EFFECTS (rval)
2774 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2775 | TREE_SIDE_EFFECTS (array));
2776 TREE_THIS_VOLATILE (rval)
2777 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2778 /* This was added by rms on 16 Nov 91.
2779 It fixes vol struct foo *a; a->elts[1]
2780 in an inline function.
2781 Hope it doesn't break something else. */
2782 | TREE_THIS_VOLATILE (array));
2783 ret = require_complete_type (loc, rval);
2784 protected_set_expr_location (ret, loc);
2785 if (non_lvalue)
2786 ret = non_lvalue_loc (loc, ret);
2787 return ret;
2789 else
2791 tree ar = default_conversion (array);
2793 if (ar == error_mark_node)
2794 return ar;
2796 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2797 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2799 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2800 index, false),
2801 RO_ARRAY_INDEXING);
2802 if (non_lvalue)
2803 ret = non_lvalue_loc (loc, ret);
2804 return ret;
2808 /* Build an external reference to identifier ID. FUN indicates
2809 whether this will be used for a function call. LOC is the source
2810 location of the identifier. This sets *TYPE to the type of the
2811 identifier, which is not the same as the type of the returned value
2812 for CONST_DECLs defined as enum constants. If the type of the
2813 identifier is not available, *TYPE is set to NULL. */
2814 tree
2815 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2817 tree ref;
2818 tree decl = lookup_name (id);
2820 /* In Objective-C, an instance variable (ivar) may be preferred to
2821 whatever lookup_name() found. */
2822 decl = objc_lookup_ivar (decl, id);
2824 *type = NULL;
2825 if (decl && decl != error_mark_node)
2827 ref = decl;
2828 *type = TREE_TYPE (ref);
2830 else if (fun)
2831 /* Implicit function declaration. */
2832 ref = implicitly_declare (loc, id);
2833 else if (decl == error_mark_node)
2834 /* Don't complain about something that's already been
2835 complained about. */
2836 return error_mark_node;
2837 else
2839 undeclared_variable (loc, id);
2840 return error_mark_node;
2843 if (TREE_TYPE (ref) == error_mark_node)
2844 return error_mark_node;
2846 if (TREE_DEPRECATED (ref))
2847 warn_deprecated_use (ref, NULL_TREE);
2849 /* Recursive call does not count as usage. */
2850 if (ref != current_function_decl)
2852 TREE_USED (ref) = 1;
2855 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2857 if (!in_sizeof && !in_typeof)
2858 C_DECL_USED (ref) = 1;
2859 else if (DECL_INITIAL (ref) == NULL_TREE
2860 && DECL_EXTERNAL (ref)
2861 && !TREE_PUBLIC (ref))
2862 record_maybe_used_decl (ref);
2865 if (TREE_CODE (ref) == CONST_DECL)
2867 used_types_insert (TREE_TYPE (ref));
2869 if (warn_cxx_compat
2870 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2871 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2873 warning_at (loc, OPT_Wc___compat,
2874 ("enum constant defined in struct or union "
2875 "is not visible in C++"));
2876 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2879 ref = DECL_INITIAL (ref);
2880 TREE_CONSTANT (ref) = 1;
2882 else if (current_function_decl != NULL_TREE
2883 && !DECL_FILE_SCOPE_P (current_function_decl)
2884 && (VAR_OR_FUNCTION_DECL_P (ref)
2885 || TREE_CODE (ref) == PARM_DECL))
2887 tree context = decl_function_context (ref);
2889 if (context != NULL_TREE && context != current_function_decl)
2890 DECL_NONLOCAL (ref) = 1;
2892 /* C99 6.7.4p3: An inline definition of a function with external
2893 linkage ... shall not contain a reference to an identifier with
2894 internal linkage. */
2895 else if (current_function_decl != NULL_TREE
2896 && DECL_DECLARED_INLINE_P (current_function_decl)
2897 && DECL_EXTERNAL (current_function_decl)
2898 && VAR_OR_FUNCTION_DECL_P (ref)
2899 && (!VAR_P (ref) || TREE_STATIC (ref))
2900 && ! TREE_PUBLIC (ref)
2901 && DECL_CONTEXT (ref) != current_function_decl)
2902 record_inline_static (loc, current_function_decl, ref,
2903 csi_internal);
2905 return ref;
2908 /* Record details of decls possibly used inside sizeof or typeof. */
2909 struct maybe_used_decl
2911 /* The decl. */
2912 tree decl;
2913 /* The level seen at (in_sizeof + in_typeof). */
2914 int level;
2915 /* The next one at this level or above, or NULL. */
2916 struct maybe_used_decl *next;
2919 static struct maybe_used_decl *maybe_used_decls;
2921 /* Record that DECL, an undefined static function reference seen
2922 inside sizeof or typeof, might be used if the operand of sizeof is
2923 a VLA type or the operand of typeof is a variably modified
2924 type. */
2926 static void
2927 record_maybe_used_decl (tree decl)
2929 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2930 t->decl = decl;
2931 t->level = in_sizeof + in_typeof;
2932 t->next = maybe_used_decls;
2933 maybe_used_decls = t;
2936 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2937 USED is false, just discard them. If it is true, mark them used
2938 (if no longer inside sizeof or typeof) or move them to the next
2939 level up (if still inside sizeof or typeof). */
2941 void
2942 pop_maybe_used (bool used)
2944 struct maybe_used_decl *p = maybe_used_decls;
2945 int cur_level = in_sizeof + in_typeof;
2946 while (p && p->level > cur_level)
2948 if (used)
2950 if (cur_level == 0)
2951 C_DECL_USED (p->decl) = 1;
2952 else
2953 p->level = cur_level;
2955 p = p->next;
2957 if (!used || cur_level == 0)
2958 maybe_used_decls = p;
2961 /* Return the result of sizeof applied to EXPR. */
2963 struct c_expr
2964 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2966 struct c_expr ret;
2967 if (expr.value == error_mark_node)
2969 ret.value = error_mark_node;
2970 ret.original_code = ERROR_MARK;
2971 ret.original_type = NULL;
2972 pop_maybe_used (false);
2974 else
2976 bool expr_const_operands = true;
2978 if (TREE_CODE (expr.value) == PARM_DECL
2979 && C_ARRAY_PARAMETER (expr.value))
2981 auto_diagnostic_group d;
2982 if (warning_at (loc, OPT_Wsizeof_array_argument,
2983 "%<sizeof%> on array function parameter %qE will "
2984 "return size of %qT", expr.value,
2985 TREE_TYPE (expr.value)))
2986 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2988 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2989 &expr_const_operands);
2990 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2991 c_last_sizeof_arg = expr.value;
2992 c_last_sizeof_loc = loc;
2993 ret.original_code = SIZEOF_EXPR;
2994 ret.original_type = NULL;
2995 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
2997 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2998 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2999 folded_expr, ret.value);
3000 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3001 SET_EXPR_LOCATION (ret.value, loc);
3003 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3005 return ret;
3008 /* Return the result of sizeof applied to T, a structure for the type
3009 name passed to sizeof (rather than the type itself). LOC is the
3010 location of the original expression. */
3012 struct c_expr
3013 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3015 tree type;
3016 struct c_expr ret;
3017 tree type_expr = NULL_TREE;
3018 bool type_expr_const = true;
3019 type = groktypename (t, &type_expr, &type_expr_const);
3020 ret.value = c_sizeof (loc, type);
3021 c_last_sizeof_arg = type;
3022 c_last_sizeof_loc = loc;
3023 ret.original_code = SIZEOF_EXPR;
3024 ret.original_type = NULL;
3025 if (type == error_mark_node)
3027 ret.value = error_mark_node;
3028 ret.original_code = ERROR_MARK;
3030 else
3031 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3032 && C_TYPE_VARIABLE_SIZE (type))
3034 /* If the type is a [*] array, it is a VLA but is represented as
3035 having a size of zero. In such a case we must ensure that
3036 the result of sizeof does not get folded to a constant by
3037 c_fully_fold, because if the size is evaluated the result is
3038 not constant and so constraints on zero or negative size
3039 arrays must not be applied when this sizeof call is inside
3040 another array declarator. */
3041 if (!type_expr)
3042 type_expr = integer_zero_node;
3043 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3044 type_expr, ret.value);
3045 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3047 pop_maybe_used (type != error_mark_node
3048 ? C_TYPE_VARIABLE_SIZE (type) : false);
3049 return ret;
3052 /* Build a function call to function FUNCTION with parameters PARAMS.
3053 The function call is at LOC.
3054 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3055 TREE_VALUE of each node is a parameter-expression.
3056 FUNCTION's data type may be a function type or a pointer-to-function. */
3058 tree
3059 build_function_call (location_t loc, tree function, tree params)
3061 vec<tree, va_gc> *v;
3062 tree ret;
3064 vec_alloc (v, list_length (params));
3065 for (; params; params = TREE_CHAIN (params))
3066 v->quick_push (TREE_VALUE (params));
3067 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3068 vec_free (v);
3069 return ret;
3072 /* Give a note about the location of the declaration of DECL. */
3074 static void
3075 inform_declaration (tree decl)
3077 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3078 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3079 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3082 /* Build a function call to function FUNCTION with parameters PARAMS.
3083 If FUNCTION is the result of resolving an overloaded target built-in,
3084 ORIG_FUNDECL is the original function decl, otherwise it is null.
3085 ORIGTYPES, if not NULL, is a vector of types; each element is
3086 either NULL or the original type of the corresponding element in
3087 PARAMS. The original type may differ from TREE_TYPE of the
3088 parameter for enums. FUNCTION's data type may be a function type
3089 or pointer-to-function. This function changes the elements of
3090 PARAMS. */
3092 tree
3093 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3094 tree function, vec<tree, va_gc> *params,
3095 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3097 tree fntype, fundecl = NULL_TREE;
3098 tree name = NULL_TREE, result;
3099 tree tem;
3100 int nargs;
3101 tree *argarray;
3104 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3105 STRIP_TYPE_NOPS (function);
3107 /* Convert anything with function type to a pointer-to-function. */
3108 if (TREE_CODE (function) == FUNCTION_DECL)
3110 name = DECL_NAME (function);
3112 if (flag_tm)
3113 tm_malloc_replacement (function);
3114 fundecl = function;
3115 if (!orig_fundecl)
3116 orig_fundecl = fundecl;
3117 /* Atomic functions have type checking/casting already done. They are
3118 often rewritten and don't match the original parameter list. */
3119 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3120 origtypes = NULL;
3122 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3123 function = function_to_pointer_conversion (loc, function);
3125 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3126 expressions, like those used for ObjC messenger dispatches. */
3127 if (params && !params->is_empty ())
3128 function = objc_rewrite_function_call (function, (*params)[0]);
3130 function = c_fully_fold (function, false, NULL);
3132 fntype = TREE_TYPE (function);
3134 if (TREE_CODE (fntype) == ERROR_MARK)
3135 return error_mark_node;
3137 if (!(TREE_CODE (fntype) == POINTER_TYPE
3138 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3140 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3141 error_at (loc,
3142 "called object %qE is not a function or function pointer",
3143 function);
3144 else if (DECL_P (function))
3146 error_at (loc,
3147 "called object %qD is not a function or function pointer",
3148 function);
3149 inform_declaration (function);
3151 else
3152 error_at (loc,
3153 "called object is not a function or function pointer");
3154 return error_mark_node;
3157 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3158 current_function_returns_abnormally = 1;
3160 /* fntype now gets the type of function pointed to. */
3161 fntype = TREE_TYPE (fntype);
3163 /* Convert the parameters to the types declared in the
3164 function prototype, or apply default promotions. */
3166 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3167 origtypes, function, fundecl);
3168 if (nargs < 0)
3169 return error_mark_node;
3171 /* Check that the function is called through a compatible prototype.
3172 If it is not, warn. */
3173 if (CONVERT_EXPR_P (function)
3174 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3175 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3176 && !comptypes (fntype, TREE_TYPE (tem)))
3178 tree return_type = TREE_TYPE (fntype);
3180 /* This situation leads to run-time undefined behavior. We can't,
3181 therefore, simply error unless we can prove that all possible
3182 executions of the program must execute the code. */
3183 warning_at (loc, 0, "function called through a non-compatible type");
3185 if (VOID_TYPE_P (return_type)
3186 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3187 pedwarn (loc, 0,
3188 "function with qualified void return type called");
3191 argarray = vec_safe_address (params);
3193 /* Check that arguments to builtin functions match the expectations. */
3194 if (fundecl
3195 && fndecl_built_in_p (fundecl)
3196 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3197 orig_fundecl, nargs, argarray))
3198 return error_mark_node;
3200 /* Check that the arguments to the function are valid. */
3201 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3202 nargs, argarray, &arg_loc);
3204 if (name != NULL_TREE
3205 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3207 if (require_constant_value)
3208 result
3209 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3210 function, nargs, argarray);
3211 else
3212 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3213 function, nargs, argarray);
3214 if (TREE_CODE (result) == NOP_EXPR
3215 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3216 STRIP_TYPE_NOPS (result);
3218 else
3219 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3220 function, nargs, argarray);
3221 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3222 later. */
3223 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3224 suppress_warning (result, OPT_Wnonnull);
3226 /* In this improbable scenario, a nested function returns a VM type.
3227 Create a TARGET_EXPR so that the call always has a LHS, much as
3228 what the C++ FE does for functions returning non-PODs. */
3229 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3231 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3232 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3233 NULL_TREE, NULL_TREE);
3236 if (VOID_TYPE_P (TREE_TYPE (result)))
3238 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3239 pedwarn (loc, 0,
3240 "function with qualified void return type called");
3241 return result;
3243 return require_complete_type (loc, result);
3246 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3248 tree
3249 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3250 tree function, vec<tree, va_gc> *params,
3251 vec<tree, va_gc> *origtypes)
3253 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3254 STRIP_TYPE_NOPS (function);
3256 /* Convert anything with function type to a pointer-to-function. */
3257 if (TREE_CODE (function) == FUNCTION_DECL)
3259 /* Implement type-directed function overloading for builtins.
3260 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3261 handle all the type checking. The result is a complete expression
3262 that implements this function call. */
3263 tree tem = resolve_overloaded_builtin (loc, function, params);
3264 if (tem)
3265 return tem;
3267 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3270 /* Helper for convert_arguments called to convert the VALue of argument
3271 number ARGNUM from ORIGTYPE to the corresponding parameter number
3272 PARMNUM and TYPE.
3273 PLOC is the location where the conversion is being performed.
3274 FUNCTION and FUNDECL are the same as in convert_arguments.
3275 VALTYPE is the original type of VAL before the conversion and,
3276 for EXCESS_PRECISION_EXPR, the operand of the expression.
3277 NPC is true if VAL represents the null pointer constant (VAL itself
3278 will have been folded to an integer constant).
3279 RNAME is the same as FUNCTION except in Objective C when it's
3280 the function selector.
3281 EXCESS_PRECISION is true when VAL was originally represented
3282 as EXCESS_PRECISION_EXPR.
3283 WARNOPT is the same as in convert_for_assignment. */
3285 static tree
3286 convert_argument (location_t ploc, tree function, tree fundecl,
3287 tree type, tree origtype, tree val, tree valtype,
3288 bool npc, tree rname, int parmnum, int argnum,
3289 bool excess_precision, int warnopt)
3291 /* Formal parm type is specified by a function prototype. */
3293 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3295 error_at (ploc, "type of formal parameter %d is incomplete",
3296 parmnum + 1);
3297 return val;
3300 /* Optionally warn about conversions that differ from the default
3301 conversions. */
3302 if (warn_traditional_conversion || warn_traditional)
3304 unsigned int formal_prec = TYPE_PRECISION (type);
3306 if (INTEGRAL_TYPE_P (type)
3307 && TREE_CODE (valtype) == REAL_TYPE)
3308 warning_at (ploc, OPT_Wtraditional_conversion,
3309 "passing argument %d of %qE as integer rather "
3310 "than floating due to prototype",
3311 argnum, rname);
3312 if (INTEGRAL_TYPE_P (type)
3313 && TREE_CODE (valtype) == COMPLEX_TYPE)
3314 warning_at (ploc, OPT_Wtraditional_conversion,
3315 "passing argument %d of %qE as integer rather "
3316 "than complex due to prototype",
3317 argnum, rname);
3318 else if (TREE_CODE (type) == COMPLEX_TYPE
3319 && TREE_CODE (valtype) == REAL_TYPE)
3320 warning_at (ploc, OPT_Wtraditional_conversion,
3321 "passing argument %d of %qE as complex rather "
3322 "than floating due to prototype",
3323 argnum, rname);
3324 else if (TREE_CODE (type) == REAL_TYPE
3325 && INTEGRAL_TYPE_P (valtype))
3326 warning_at (ploc, OPT_Wtraditional_conversion,
3327 "passing argument %d of %qE as floating rather "
3328 "than integer due to prototype",
3329 argnum, rname);
3330 else if (TREE_CODE (type) == COMPLEX_TYPE
3331 && INTEGRAL_TYPE_P (valtype))
3332 warning_at (ploc, OPT_Wtraditional_conversion,
3333 "passing argument %d of %qE as complex rather "
3334 "than integer due to prototype",
3335 argnum, rname);
3336 else if (TREE_CODE (type) == REAL_TYPE
3337 && TREE_CODE (valtype) == COMPLEX_TYPE)
3338 warning_at (ploc, OPT_Wtraditional_conversion,
3339 "passing argument %d of %qE as floating rather "
3340 "than complex due to prototype",
3341 argnum, rname);
3342 /* ??? At some point, messages should be written about
3343 conversions between complex types, but that's too messy
3344 to do now. */
3345 else if (TREE_CODE (type) == REAL_TYPE
3346 && TREE_CODE (valtype) == REAL_TYPE)
3348 /* Warn if any argument is passed as `float',
3349 since without a prototype it would be `double'. */
3350 if (formal_prec == TYPE_PRECISION (float_type_node)
3351 && type != dfloat32_type_node)
3352 warning_at (ploc, 0,
3353 "passing argument %d of %qE as %<float%> "
3354 "rather than %<double%> due to prototype",
3355 argnum, rname);
3357 /* Warn if mismatch between argument and prototype
3358 for decimal float types. Warn of conversions with
3359 binary float types and of precision narrowing due to
3360 prototype. */
3361 else if (type != valtype
3362 && (type == dfloat32_type_node
3363 || type == dfloat64_type_node
3364 || type == dfloat128_type_node
3365 || valtype == dfloat32_type_node
3366 || valtype == dfloat64_type_node
3367 || valtype == dfloat128_type_node)
3368 && (formal_prec
3369 <= TYPE_PRECISION (valtype)
3370 || (type == dfloat128_type_node
3371 && (valtype
3372 != dfloat64_type_node
3373 && (valtype
3374 != dfloat32_type_node)))
3375 || (type == dfloat64_type_node
3376 && (valtype
3377 != dfloat32_type_node))))
3378 warning_at (ploc, 0,
3379 "passing argument %d of %qE as %qT "
3380 "rather than %qT due to prototype",
3381 argnum, rname, type, valtype);
3384 /* Detect integer changing in width or signedness.
3385 These warnings are only activated with
3386 -Wtraditional-conversion, not with -Wtraditional. */
3387 else if (warn_traditional_conversion
3388 && INTEGRAL_TYPE_P (type)
3389 && INTEGRAL_TYPE_P (valtype))
3391 tree would_have_been = default_conversion (val);
3392 tree type1 = TREE_TYPE (would_have_been);
3394 if (val == error_mark_node)
3395 /* VAL could have been of incomplete type. */;
3396 else if (TREE_CODE (type) == ENUMERAL_TYPE
3397 && (TYPE_MAIN_VARIANT (type)
3398 == TYPE_MAIN_VARIANT (valtype)))
3399 /* No warning if function asks for enum
3400 and the actual arg is that enum type. */
3402 else if (formal_prec != TYPE_PRECISION (type1))
3403 warning_at (ploc, OPT_Wtraditional_conversion,
3404 "passing argument %d of %qE "
3405 "with different width due to prototype",
3406 argnum, rname);
3407 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3409 /* Don't complain if the formal parameter type
3410 is an enum, because we can't tell now whether
3411 the value was an enum--even the same enum. */
3412 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3414 else if (TREE_CODE (val) == INTEGER_CST
3415 && int_fits_type_p (val, type))
3416 /* Change in signedness doesn't matter
3417 if a constant value is unaffected. */
3419 /* If the value is extended from a narrower
3420 unsigned type, it doesn't matter whether we
3421 pass it as signed or unsigned; the value
3422 certainly is the same either way. */
3423 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3424 && TYPE_UNSIGNED (valtype))
3426 else if (TYPE_UNSIGNED (type))
3427 warning_at (ploc, OPT_Wtraditional_conversion,
3428 "passing argument %d of %qE "
3429 "as unsigned due to prototype",
3430 argnum, rname);
3431 else
3432 warning_at (ploc, OPT_Wtraditional_conversion,
3433 "passing argument %d of %qE "
3434 "as signed due to prototype",
3435 argnum, rname);
3439 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3440 sake of better warnings from convert_and_check. */
3441 if (excess_precision)
3442 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3444 tree parmval = convert_for_assignment (ploc, ploc, type,
3445 val, origtype, ic_argpass,
3446 npc, fundecl, function,
3447 parmnum + 1, warnopt);
3449 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3450 && INTEGRAL_TYPE_P (type)
3451 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3452 parmval = default_conversion (parmval);
3454 return parmval;
3457 /* Convert the argument expressions in the vector VALUES
3458 to the types in the list TYPELIST.
3460 If TYPELIST is exhausted, or when an element has NULL as its type,
3461 perform the default conversions.
3463 ORIGTYPES is the original types of the expressions in VALUES. This
3464 holds the type of enum values which have been converted to integral
3465 types. It may be NULL.
3467 FUNCTION is a tree for the called function. It is used only for
3468 error messages, where it is formatted with %qE.
3470 This is also where warnings about wrong number of args are generated.
3472 ARG_LOC are locations of function arguments (if any).
3474 Returns the actual number of arguments processed (which may be less
3475 than the length of VALUES in some error situations), or -1 on
3476 failure. */
3478 static int
3479 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3480 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3481 tree function, tree fundecl)
3483 unsigned int parmnum;
3484 bool error_args = false;
3485 const bool type_generic = fundecl
3486 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3487 bool type_generic_remove_excess_precision = false;
3488 bool type_generic_overflow_p = false;
3489 tree selector;
3491 /* Change pointer to function to the function itself for
3492 diagnostics. */
3493 if (TREE_CODE (function) == ADDR_EXPR
3494 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3495 function = TREE_OPERAND (function, 0);
3497 /* Handle an ObjC selector specially for diagnostics. */
3498 selector = objc_message_selector ();
3500 /* For a call to a built-in function declared without a prototype,
3501 set to the built-in function's argument list. */
3502 tree builtin_typelist = NULL_TREE;
3504 /* For type-generic built-in functions, determine whether excess
3505 precision should be removed (classification) or not
3506 (comparison). */
3507 if (fundecl
3508 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3510 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3511 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3513 /* For a call to a built-in function declared without a prototype
3514 use the types of the parameters of the internal built-in to
3515 match those of the arguments to. */
3516 if (tree bdecl = builtin_decl_explicit (code))
3517 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3520 /* For type-generic built-in functions, determine whether excess
3521 precision should be removed (classification) or not
3522 (comparison). */
3523 if (type_generic)
3524 switch (code)
3526 case BUILT_IN_ISFINITE:
3527 case BUILT_IN_ISINF:
3528 case BUILT_IN_ISINF_SIGN:
3529 case BUILT_IN_ISNAN:
3530 case BUILT_IN_ISNORMAL:
3531 case BUILT_IN_FPCLASSIFY:
3532 type_generic_remove_excess_precision = true;
3533 break;
3535 case BUILT_IN_ADD_OVERFLOW_P:
3536 case BUILT_IN_SUB_OVERFLOW_P:
3537 case BUILT_IN_MUL_OVERFLOW_P:
3538 /* The last argument of these type-generic builtins
3539 should not be promoted. */
3540 type_generic_overflow_p = true;
3541 break;
3543 default:
3544 break;
3548 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3549 individual converted arguments. */
3551 tree typetail, builtin_typetail, val;
3552 for (typetail = typelist,
3553 builtin_typetail = builtin_typelist,
3554 parmnum = 0;
3555 values && values->iterate (parmnum, &val);
3556 ++parmnum)
3558 /* The type of the function parameter (if it was declared with one). */
3559 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3560 /* The type of the built-in function parameter (if the function
3561 is a built-in). Used to detect type incompatibilities in
3562 calls to built-ins declared without a prototype. */
3563 tree builtin_type = (builtin_typetail
3564 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3565 /* The original type of the argument being passed to the function. */
3566 tree valtype = TREE_TYPE (val);
3567 /* The called function (or function selector in Objective C). */
3568 tree rname = function;
3569 int argnum = parmnum + 1;
3570 const char *invalid_func_diag;
3571 /* Set for EXCESS_PRECISION_EXPR arguments. */
3572 bool excess_precision = false;
3573 /* The value of the argument after conversion to the type
3574 of the function parameter it is passed to. */
3575 tree parmval;
3576 /* Some __atomic_* builtins have additional hidden argument at
3577 position 0. */
3578 location_t ploc
3579 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3580 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3581 : input_location;
3583 if (type == void_type_node)
3585 if (selector)
3586 error_at (loc, "too many arguments to method %qE", selector);
3587 else
3588 error_at (loc, "too many arguments to function %qE", function);
3589 inform_declaration (fundecl);
3590 return error_args ? -1 : (int) parmnum;
3593 if (builtin_type == void_type_node)
3595 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3596 "too many arguments to built-in function %qE "
3597 "expecting %d", function, parmnum))
3598 inform_declaration (fundecl);
3599 builtin_typetail = NULL_TREE;
3602 if (selector && argnum > 2)
3604 rname = selector;
3605 argnum -= 2;
3608 /* Determine if VAL is a null pointer constant before folding it. */
3609 bool npc = null_pointer_constant_p (val);
3611 /* If there is excess precision and a prototype, convert once to
3612 the required type rather than converting via the semantic
3613 type. Likewise without a prototype a float value represented
3614 as long double should be converted once to double. But for
3615 type-generic classification functions excess precision must
3616 be removed here. */
3617 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3618 && (type || !type_generic || !type_generic_remove_excess_precision))
3620 val = TREE_OPERAND (val, 0);
3621 excess_precision = true;
3623 val = c_fully_fold (val, false, NULL);
3624 STRIP_TYPE_NOPS (val);
3626 val = require_complete_type (ploc, val);
3628 /* Some floating-point arguments must be promoted to double when
3629 no type is specified by a prototype. This applies to
3630 arguments of type float, and to architecture-specific types
3631 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3632 bool promote_float_arg = false;
3633 if (type == NULL_TREE
3634 && TREE_CODE (valtype) == REAL_TYPE
3635 && (TYPE_PRECISION (valtype)
3636 <= TYPE_PRECISION (double_type_node))
3637 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3638 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3639 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3641 /* Promote this argument, unless it has a _FloatN or
3642 _FloatNx type. */
3643 promote_float_arg = true;
3644 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3645 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3647 promote_float_arg = false;
3648 break;
3652 if (type != NULL_TREE)
3654 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3655 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3656 val, valtype, npc, rname, parmnum, argnum,
3657 excess_precision, 0);
3659 else if (promote_float_arg)
3661 if (type_generic)
3662 parmval = val;
3663 else
3665 /* Convert `float' to `double'. */
3666 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3667 warning_at (ploc, OPT_Wdouble_promotion,
3668 "implicit conversion from %qT to %qT when passing "
3669 "argument to function",
3670 valtype, double_type_node);
3671 parmval = convert (double_type_node, val);
3674 else if ((excess_precision && !type_generic)
3675 || (type_generic_overflow_p && parmnum == 2))
3676 /* A "double" argument with excess precision being passed
3677 without a prototype or in variable arguments.
3678 The last argument of __builtin_*_overflow_p should not be
3679 promoted. */
3680 parmval = convert (valtype, val);
3681 else if ((invalid_func_diag =
3682 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3684 error (invalid_func_diag);
3685 return -1;
3687 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3689 return -1;
3691 else
3692 /* Convert `short' and `char' to full-size `int'. */
3693 parmval = default_conversion (val);
3695 (*values)[parmnum] = parmval;
3696 if (parmval == error_mark_node)
3697 error_args = true;
3699 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3701 /* For a call to a built-in function declared without a prototype,
3702 perform the conversions from the argument to the expected type
3703 but issue warnings rather than errors for any mismatches.
3704 Ignore the converted argument and use the PARMVAL obtained
3705 above by applying default conversions instead. */
3706 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3707 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3708 val, valtype, npc, rname, parmnum, argnum,
3709 excess_precision,
3710 OPT_Wbuiltin_declaration_mismatch);
3713 if (typetail)
3714 typetail = TREE_CHAIN (typetail);
3716 if (builtin_typetail)
3717 builtin_typetail = TREE_CHAIN (builtin_typetail);
3720 gcc_assert (parmnum == vec_safe_length (values));
3722 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3724 error_at (loc, "too few arguments to function %qE", function);
3725 inform_declaration (fundecl);
3726 return -1;
3729 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3731 unsigned nargs = parmnum;
3732 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3733 ++nargs;
3735 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3736 "too few arguments to built-in function %qE "
3737 "expecting %u", function, nargs - 1))
3738 inform_declaration (fundecl);
3741 return error_args ? -1 : (int) parmnum;
3744 /* This is the entry point used by the parser to build unary operators
3745 in the input. CODE, a tree_code, specifies the unary operator, and
3746 ARG is the operand. For unary plus, the C parser currently uses
3747 CONVERT_EXPR for code.
3749 LOC is the location to use for the tree generated.
3752 struct c_expr
3753 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3755 struct c_expr result;
3757 result.original_code = code;
3758 result.original_type = NULL;
3760 if (reject_gcc_builtin (arg.value))
3762 result.value = error_mark_node;
3764 else
3766 result.value = build_unary_op (loc, code, arg.value, false);
3768 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3769 overflow_warning (loc, result.value, arg.value);
3772 /* We are typically called when parsing a prefix token at LOC acting on
3773 ARG. Reflect this by updating the source range of the result to
3774 start at LOC and end at the end of ARG. */
3775 set_c_expr_source_range (&result,
3776 loc, arg.get_finish ());
3778 return result;
3781 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3783 bool
3784 char_type_p (tree type)
3786 return (type == char_type_node
3787 || type == unsigned_char_type_node
3788 || type == signed_char_type_node
3789 || type == char16_type_node
3790 || type == char32_type_node);
3793 /* This is the entry point used by the parser to build binary operators
3794 in the input. CODE, a tree_code, specifies the binary operator, and
3795 ARG1 and ARG2 are the operands. In addition to constructing the
3796 expression, we check for operands that were written with other binary
3797 operators in a way that is likely to confuse the user.
3799 LOCATION is the location of the binary operator. */
3801 struct c_expr
3802 parser_build_binary_op (location_t location, enum tree_code code,
3803 struct c_expr arg1, struct c_expr arg2)
3805 struct c_expr result;
3807 enum tree_code code1 = arg1.original_code;
3808 enum tree_code code2 = arg2.original_code;
3809 tree type1 = (arg1.original_type
3810 ? arg1.original_type
3811 : TREE_TYPE (arg1.value));
3812 tree type2 = (arg2.original_type
3813 ? arg2.original_type
3814 : TREE_TYPE (arg2.value));
3816 result.value = build_binary_op (location, code,
3817 arg1.value, arg2.value, true);
3818 result.original_code = code;
3819 result.original_type = NULL;
3821 if (TREE_CODE (result.value) == ERROR_MARK)
3823 set_c_expr_source_range (&result,
3824 arg1.get_start (),
3825 arg2.get_finish ());
3826 return result;
3829 if (location != UNKNOWN_LOCATION)
3830 protected_set_expr_location (result.value, location);
3832 set_c_expr_source_range (&result,
3833 arg1.get_start (),
3834 arg2.get_finish ());
3836 /* Check for cases such as x+y<<z which users are likely
3837 to misinterpret. */
3838 if (warn_parentheses)
3839 warn_about_parentheses (location, code, code1, arg1.value, code2,
3840 arg2.value);
3842 if (warn_logical_op)
3843 warn_logical_operator (location, code, TREE_TYPE (result.value),
3844 code1, arg1.value, code2, arg2.value);
3846 if (warn_tautological_compare)
3848 tree lhs = arg1.value;
3849 tree rhs = arg2.value;
3850 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3852 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3853 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3854 lhs = NULL_TREE;
3855 else
3856 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3858 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3860 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3861 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3862 rhs = NULL_TREE;
3863 else
3864 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3866 if (lhs != NULL_TREE && rhs != NULL_TREE)
3867 warn_tautological_cmp (location, code, lhs, rhs);
3870 if (warn_logical_not_paren
3871 && TREE_CODE_CLASS (code) == tcc_comparison
3872 && code1 == TRUTH_NOT_EXPR
3873 && code2 != TRUTH_NOT_EXPR
3874 /* Avoid warning for !!x == y. */
3875 && (TREE_CODE (arg1.value) != NE_EXPR
3876 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3878 /* Avoid warning for !b == y where b has _Bool type. */
3879 tree t = integer_zero_node;
3880 if (TREE_CODE (arg1.value) == EQ_EXPR
3881 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3882 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3884 t = TREE_OPERAND (arg1.value, 0);
3887 if (TREE_TYPE (t) != integer_type_node)
3888 break;
3889 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3890 t = C_MAYBE_CONST_EXPR_EXPR (t);
3891 else if (CONVERT_EXPR_P (t))
3892 t = TREE_OPERAND (t, 0);
3893 else
3894 break;
3896 while (1);
3898 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3899 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3902 /* Warn about comparisons against string literals, with the exception
3903 of testing for equality or inequality of a string literal with NULL. */
3904 if (code == EQ_EXPR || code == NE_EXPR)
3906 if ((code1 == STRING_CST
3907 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3908 || (code2 == STRING_CST
3909 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3910 warning_at (location, OPT_Waddress,
3911 "comparison with string literal results in unspecified behavior");
3912 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3913 if (POINTER_TYPE_P (type1)
3914 && null_pointer_constant_p (arg2.value)
3915 && char_type_p (type2))
3917 auto_diagnostic_group d;
3918 if (warning_at (location, OPT_Wpointer_compare,
3919 "comparison between pointer and zero character "
3920 "constant"))
3921 inform (arg1.get_start (),
3922 "did you mean to dereference the pointer?");
3924 else if (POINTER_TYPE_P (type2)
3925 && null_pointer_constant_p (arg1.value)
3926 && char_type_p (type1))
3928 auto_diagnostic_group d;
3929 if (warning_at (location, OPT_Wpointer_compare,
3930 "comparison between pointer and zero character "
3931 "constant"))
3932 inform (arg2.get_start (),
3933 "did you mean to dereference the pointer?");
3936 else if (TREE_CODE_CLASS (code) == tcc_comparison
3937 && (code1 == STRING_CST || code2 == STRING_CST))
3938 warning_at (location, OPT_Waddress,
3939 "comparison with string literal results in unspecified behavior");
3941 if (TREE_OVERFLOW_P (result.value)
3942 && !TREE_OVERFLOW_P (arg1.value)
3943 && !TREE_OVERFLOW_P (arg2.value))
3944 overflow_warning (location, result.value);
3946 /* Warn about comparisons of different enum types. */
3947 if (warn_enum_compare
3948 && TREE_CODE_CLASS (code) == tcc_comparison
3949 && TREE_CODE (type1) == ENUMERAL_TYPE
3950 && TREE_CODE (type2) == ENUMERAL_TYPE
3951 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3952 warning_at (location, OPT_Wenum_compare,
3953 "comparison between %qT and %qT",
3954 type1, type2);
3956 return result;
3959 /* Return a tree for the difference of pointers OP0 and OP1.
3960 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3961 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3963 static tree
3964 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3966 tree restype = ptrdiff_type_node;
3967 tree result, inttype;
3969 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3970 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3971 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3972 tree orig_op0 = op0;
3973 tree orig_op1 = op1;
3975 /* If the operands point into different address spaces, we need to
3976 explicitly convert them to pointers into the common address space
3977 before we can subtract the numerical address values. */
3978 if (as0 != as1)
3980 addr_space_t as_common;
3981 tree common_type;
3983 /* Determine the common superset address space. This is guaranteed
3984 to exist because the caller verified that comp_target_types
3985 returned non-zero. */
3986 if (!addr_space_superset (as0, as1, &as_common))
3987 gcc_unreachable ();
3989 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3990 op0 = convert (common_type, op0);
3991 op1 = convert (common_type, op1);
3994 /* Determine integer type result of the subtraction. This will usually
3995 be the same as the result type (ptrdiff_t), but may need to be a wider
3996 type if pointers for the address space are wider than ptrdiff_t. */
3997 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3998 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3999 else
4000 inttype = restype;
4002 if (TREE_CODE (target_type) == VOID_TYPE)
4003 pedwarn (loc, OPT_Wpointer_arith,
4004 "pointer of type %<void *%> used in subtraction");
4005 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4006 pedwarn (loc, OPT_Wpointer_arith,
4007 "pointer to a function used in subtraction");
4009 if (current_function_decl != NULL_TREE
4010 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4012 op0 = save_expr (op0);
4013 op1 = save_expr (op1);
4015 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4016 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4019 /* First do the subtraction, then build the divide operator
4020 and only convert at the very end.
4021 Do not do default conversions in case restype is a short type. */
4023 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4024 pointers. If some platform cannot provide that, or has a larger
4025 ptrdiff_type to support differences larger than half the address
4026 space, cast the pointers to some larger integer type and do the
4027 computations in that type. */
4028 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4029 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4030 convert (inttype, op1), false);
4031 else
4033 /* Cast away qualifiers. */
4034 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4035 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4036 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4039 /* This generates an error if op1 is pointer to incomplete type. */
4040 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4041 error_at (loc, "arithmetic on pointer to an incomplete type");
4042 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4043 TREE_TYPE (TREE_TYPE (orig_op0))))
4044 verify_type_context (loc, TCTX_POINTER_ARITH,
4045 TREE_TYPE (TREE_TYPE (orig_op1)));
4047 op1 = c_size_in_bytes (target_type);
4049 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4050 error_at (loc, "arithmetic on pointer to an empty aggregate");
4052 /* Divide by the size, in easiest possible way. */
4053 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4054 op0, convert (inttype, op1));
4056 /* Convert to final result type if necessary. */
4057 return convert (restype, result);
4060 /* Expand atomic compound assignments into an appropriate sequence as
4061 specified by the C11 standard section 6.5.16.2.
4063 _Atomic T1 E1
4064 T2 E2
4065 E1 op= E2
4067 This sequence is used for all types for which these operations are
4068 supported.
4070 In addition, built-in versions of the 'fe' prefixed routines may
4071 need to be invoked for floating point (real, complex or vector) when
4072 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4074 T1 newval;
4075 T1 old;
4076 T1 *addr
4077 T2 val
4078 fenv_t fenv
4080 addr = &E1;
4081 val = (E2);
4082 __atomic_load (addr, &old, SEQ_CST);
4083 feholdexcept (&fenv);
4084 loop:
4085 newval = old op val;
4086 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4087 SEQ_CST))
4088 goto done;
4089 feclearexcept (FE_ALL_EXCEPT);
4090 goto loop:
4091 done:
4092 feupdateenv (&fenv);
4094 The compiler will issue the __atomic_fetch_* built-in when possible,
4095 otherwise it will generate the generic form of the atomic operations.
4096 This requires temp(s) and has their address taken. The atomic processing
4097 is smart enough to figure out when the size of an object can utilize
4098 a lock-free version, and convert the built-in call to the appropriate
4099 lock-free routine. The optimizers will then dispose of any temps that
4100 are no longer required, and lock-free implementations are utilized as
4101 long as there is target support for the required size.
4103 If the operator is NOP_EXPR, then this is a simple assignment, and
4104 an __atomic_store is issued to perform the assignment rather than
4105 the above loop. */
4107 /* Build an atomic assignment at LOC, expanding into the proper
4108 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4109 the result of the operation, unless RETURN_OLD_P, in which case
4110 return the old value of LHS (this is only for postincrement and
4111 postdecrement). */
4113 static tree
4114 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4115 tree rhs, bool return_old_p)
4117 tree fndecl, func_call;
4118 vec<tree, va_gc> *params;
4119 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4120 tree old, old_addr;
4121 tree compound_stmt = NULL_TREE;
4122 tree stmt, goto_stmt;
4123 tree loop_label, loop_decl, done_label, done_decl;
4125 tree lhs_type = TREE_TYPE (lhs);
4126 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4127 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4128 tree rhs_semantic_type = TREE_TYPE (rhs);
4129 tree nonatomic_rhs_semantic_type;
4130 tree rhs_type;
4132 gcc_assert (TYPE_ATOMIC (lhs_type));
4134 if (return_old_p)
4135 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4137 /* Allocate enough vector items for a compare_exchange. */
4138 vec_alloc (params, 6);
4140 /* Create a compound statement to hold the sequence of statements
4141 with a loop. */
4142 if (modifycode != NOP_EXPR)
4144 compound_stmt = c_begin_compound_stmt (false);
4146 /* For consistency with build_modify_expr on non-_Atomic,
4147 mark the lhs as read. Also, it would be very hard to match
4148 such expressions in mark_exp_read. */
4149 mark_exp_read (lhs);
4152 /* Remove any excess precision (which is only present here in the
4153 case of compound assignments). */
4154 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4156 gcc_assert (modifycode != NOP_EXPR);
4157 rhs = TREE_OPERAND (rhs, 0);
4159 rhs_type = TREE_TYPE (rhs);
4161 /* Fold the RHS if it hasn't already been folded. */
4162 if (modifycode != NOP_EXPR)
4163 rhs = c_fully_fold (rhs, false, NULL);
4165 /* Remove the qualifiers for the rest of the expressions and create
4166 the VAL temp variable to hold the RHS. */
4167 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4168 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4169 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4170 TYPE_UNQUALIFIED);
4171 val = create_tmp_var_raw (nonatomic_rhs_type);
4172 TREE_ADDRESSABLE (val) = 1;
4173 suppress_warning (val);
4174 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4175 NULL_TREE);
4176 TREE_SIDE_EFFECTS (rhs) = 1;
4177 SET_EXPR_LOCATION (rhs, loc);
4178 if (modifycode != NOP_EXPR)
4179 add_stmt (rhs);
4181 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4182 an atomic_store. */
4183 if (modifycode == NOP_EXPR)
4185 compound_stmt = rhs;
4186 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4187 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4188 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4189 params->quick_push (lhs_addr);
4190 params->quick_push (rhs);
4191 params->quick_push (seq_cst);
4192 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4194 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4195 compound_stmt, func_call);
4197 /* VAL is the value which was stored, return a COMPOUND_STMT of
4198 the statement and that value. */
4199 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4202 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4203 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4204 isn't applicable for such builtins. ??? Do we want to handle enums? */
4205 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4206 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4208 built_in_function fncode;
4209 switch (modifycode)
4211 case PLUS_EXPR:
4212 case POINTER_PLUS_EXPR:
4213 fncode = (return_old_p
4214 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4215 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4216 break;
4217 case MINUS_EXPR:
4218 fncode = (return_old_p
4219 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4220 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4221 break;
4222 case BIT_AND_EXPR:
4223 fncode = (return_old_p
4224 ? BUILT_IN_ATOMIC_FETCH_AND_N
4225 : BUILT_IN_ATOMIC_AND_FETCH_N);
4226 break;
4227 case BIT_IOR_EXPR:
4228 fncode = (return_old_p
4229 ? BUILT_IN_ATOMIC_FETCH_OR_N
4230 : BUILT_IN_ATOMIC_OR_FETCH_N);
4231 break;
4232 case BIT_XOR_EXPR:
4233 fncode = (return_old_p
4234 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4235 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4236 break;
4237 default:
4238 goto cas_loop;
4241 /* We can only use "_1" through "_16" variants of the atomic fetch
4242 built-ins. */
4243 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4244 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4245 goto cas_loop;
4247 /* If this is a pointer type, we need to multiply by the size of
4248 the pointer target type. */
4249 if (POINTER_TYPE_P (lhs_type))
4251 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4252 /* ??? This would introduce -Wdiscarded-qualifiers
4253 warning: __atomic_fetch_* expect volatile void *
4254 type as the first argument. (Assignments between
4255 atomic and non-atomic objects are OK.) */
4256 || TYPE_RESTRICT (lhs_type))
4257 goto cas_loop;
4258 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4259 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4260 convert (ptrdiff_type_node, rhs),
4261 convert (ptrdiff_type_node, sz));
4264 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4265 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4266 fndecl = builtin_decl_explicit (fncode);
4267 params->quick_push (lhs_addr);
4268 params->quick_push (rhs);
4269 params->quick_push (seq_cst);
4270 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4272 newval = create_tmp_var_raw (nonatomic_lhs_type);
4273 TREE_ADDRESSABLE (newval) = 1;
4274 suppress_warning (newval);
4275 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4276 NULL_TREE, NULL_TREE);
4277 SET_EXPR_LOCATION (rhs, loc);
4278 add_stmt (rhs);
4280 /* Finish the compound statement. */
4281 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4283 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4284 the statement and that value. */
4285 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4288 cas_loop:
4289 /* Create the variables and labels required for the op= form. */
4290 old = create_tmp_var_raw (nonatomic_lhs_type);
4291 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4292 TREE_ADDRESSABLE (old) = 1;
4293 suppress_warning (old);
4295 newval = create_tmp_var_raw (nonatomic_lhs_type);
4296 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4297 TREE_ADDRESSABLE (newval) = 1;
4298 suppress_warning (newval);
4300 loop_decl = create_artificial_label (loc);
4301 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4303 done_decl = create_artificial_label (loc);
4304 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4306 /* __atomic_load (addr, &old, SEQ_CST). */
4307 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4308 params->quick_push (lhs_addr);
4309 params->quick_push (old_addr);
4310 params->quick_push (seq_cst);
4311 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4312 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4313 NULL_TREE);
4314 add_stmt (old);
4315 params->truncate (0);
4317 /* Create the expressions for floating-point environment
4318 manipulation, if required. */
4319 bool need_fenv = (flag_trapping_math
4320 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4321 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4322 if (need_fenv)
4323 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4325 if (hold_call)
4326 add_stmt (hold_call);
4328 /* loop: */
4329 add_stmt (loop_label);
4331 /* newval = old + val; */
4332 if (rhs_type != rhs_semantic_type)
4333 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4334 rhs = build_binary_op (loc, modifycode, old, val, true);
4335 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4337 tree eptype = TREE_TYPE (rhs);
4338 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4339 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4341 else
4342 rhs = c_fully_fold (rhs, false, NULL);
4343 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4344 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4345 NULL_TREE, 0);
4346 if (rhs != error_mark_node)
4348 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4349 NULL_TREE);
4350 SET_EXPR_LOCATION (rhs, loc);
4351 add_stmt (rhs);
4354 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4355 goto done; */
4356 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4357 params->quick_push (lhs_addr);
4358 params->quick_push (old_addr);
4359 params->quick_push (newval_addr);
4360 params->quick_push (integer_zero_node);
4361 params->quick_push (seq_cst);
4362 params->quick_push (seq_cst);
4363 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4365 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4366 SET_EXPR_LOCATION (goto_stmt, loc);
4368 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4369 SET_EXPR_LOCATION (stmt, loc);
4370 add_stmt (stmt);
4372 if (clear_call)
4373 add_stmt (clear_call);
4375 /* goto loop; */
4376 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4377 SET_EXPR_LOCATION (goto_stmt, loc);
4378 add_stmt (goto_stmt);
4380 /* done: */
4381 add_stmt (done_label);
4383 if (update_call)
4384 add_stmt (update_call);
4386 /* Finish the compound statement. */
4387 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4389 /* NEWVAL is the value that was successfully stored, return a
4390 COMPOUND_EXPR of the statement and the appropriate value. */
4391 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4392 return_old_p ? old : newval);
4395 /* Construct and perhaps optimize a tree representation
4396 for a unary operation. CODE, a tree_code, specifies the operation
4397 and XARG is the operand.
4398 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4399 promotions (such as from short to int).
4400 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4401 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4402 to pointers in C99.
4404 LOCATION is the location of the operator. */
4406 tree
4407 build_unary_op (location_t location, enum tree_code code, tree xarg,
4408 bool noconvert)
4410 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4411 tree arg = xarg;
4412 tree argtype = NULL_TREE;
4413 enum tree_code typecode;
4414 tree val;
4415 tree ret = error_mark_node;
4416 tree eptype = NULL_TREE;
4417 const char *invalid_op_diag;
4418 bool int_operands;
4420 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4421 if (int_operands)
4422 arg = remove_c_maybe_const_expr (arg);
4424 if (code != ADDR_EXPR)
4425 arg = require_complete_type (location, arg);
4427 typecode = TREE_CODE (TREE_TYPE (arg));
4428 if (typecode == ERROR_MARK)
4429 return error_mark_node;
4430 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4431 typecode = INTEGER_TYPE;
4433 if ((invalid_op_diag
4434 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4436 error_at (location, invalid_op_diag);
4437 return error_mark_node;
4440 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4442 eptype = TREE_TYPE (arg);
4443 arg = TREE_OPERAND (arg, 0);
4446 switch (code)
4448 case CONVERT_EXPR:
4449 /* This is used for unary plus, because a CONVERT_EXPR
4450 is enough to prevent anybody from looking inside for
4451 associativity, but won't generate any code. */
4452 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4453 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4454 || gnu_vector_type_p (TREE_TYPE (arg))))
4456 error_at (location, "wrong type argument to unary plus");
4457 return error_mark_node;
4459 else if (!noconvert)
4460 arg = default_conversion (arg);
4461 arg = non_lvalue_loc (location, arg);
4462 break;
4464 case NEGATE_EXPR:
4465 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4466 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4467 || gnu_vector_type_p (TREE_TYPE (arg))))
4469 error_at (location, "wrong type argument to unary minus");
4470 return error_mark_node;
4472 else if (!noconvert)
4473 arg = default_conversion (arg);
4474 break;
4476 case BIT_NOT_EXPR:
4477 /* ~ works on integer types and non float vectors. */
4478 if (typecode == INTEGER_TYPE
4479 || (gnu_vector_type_p (TREE_TYPE (arg))
4480 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4482 tree e = arg;
4484 /* Warn if the expression has boolean value. */
4485 while (TREE_CODE (e) == COMPOUND_EXPR)
4486 e = TREE_OPERAND (e, 1);
4488 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4489 || truth_value_p (TREE_CODE (e))))
4491 auto_diagnostic_group d;
4492 if (warning_at (location, OPT_Wbool_operation,
4493 "%<~%> on a boolean expression"))
4495 gcc_rich_location richloc (location);
4496 richloc.add_fixit_insert_before (location, "!");
4497 inform (&richloc, "did you mean to use logical not?");
4500 if (!noconvert)
4501 arg = default_conversion (arg);
4503 else if (typecode == COMPLEX_TYPE)
4505 code = CONJ_EXPR;
4506 pedwarn (location, OPT_Wpedantic,
4507 "ISO C does not support %<~%> for complex conjugation");
4508 if (!noconvert)
4509 arg = default_conversion (arg);
4511 else
4513 error_at (location, "wrong type argument to bit-complement");
4514 return error_mark_node;
4516 break;
4518 case ABS_EXPR:
4519 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4521 error_at (location, "wrong type argument to abs");
4522 return error_mark_node;
4524 else if (!noconvert)
4525 arg = default_conversion (arg);
4526 break;
4528 case ABSU_EXPR:
4529 if (!(typecode == INTEGER_TYPE))
4531 error_at (location, "wrong type argument to absu");
4532 return error_mark_node;
4534 else if (!noconvert)
4535 arg = default_conversion (arg);
4536 break;
4538 case CONJ_EXPR:
4539 /* Conjugating a real value is a no-op, but allow it anyway. */
4540 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4541 || typecode == COMPLEX_TYPE))
4543 error_at (location, "wrong type argument to conjugation");
4544 return error_mark_node;
4546 else if (!noconvert)
4547 arg = default_conversion (arg);
4548 break;
4550 case TRUTH_NOT_EXPR:
4551 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4552 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4553 && typecode != COMPLEX_TYPE)
4555 error_at (location,
4556 "wrong type argument to unary exclamation mark");
4557 return error_mark_node;
4559 if (int_operands)
4561 arg = c_objc_common_truthvalue_conversion (location, xarg);
4562 arg = remove_c_maybe_const_expr (arg);
4564 else
4565 arg = c_objc_common_truthvalue_conversion (location, arg);
4566 ret = invert_truthvalue_loc (location, arg);
4567 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4568 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4569 location = EXPR_LOCATION (ret);
4570 goto return_build_unary_op;
4572 case REALPART_EXPR:
4573 case IMAGPART_EXPR:
4574 ret = build_real_imag_expr (location, code, arg);
4575 if (ret == error_mark_node)
4576 return error_mark_node;
4577 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4578 eptype = TREE_TYPE (eptype);
4579 goto return_build_unary_op;
4581 case PREINCREMENT_EXPR:
4582 case POSTINCREMENT_EXPR:
4583 case PREDECREMENT_EXPR:
4584 case POSTDECREMENT_EXPR:
4586 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4588 tree inner = build_unary_op (location, code,
4589 C_MAYBE_CONST_EXPR_EXPR (arg),
4590 noconvert);
4591 if (inner == error_mark_node)
4592 return error_mark_node;
4593 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4594 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4595 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4596 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4597 goto return_build_unary_op;
4600 /* Complain about anything that is not a true lvalue. In
4601 Objective-C, skip this check for property_refs. */
4602 if (!objc_is_property_ref (arg)
4603 && !lvalue_or_else (location,
4604 arg, ((code == PREINCREMENT_EXPR
4605 || code == POSTINCREMENT_EXPR)
4606 ? lv_increment
4607 : lv_decrement)))
4608 return error_mark_node;
4610 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4612 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4613 warning_at (location, OPT_Wc___compat,
4614 "increment of enumeration value is invalid in C++");
4615 else
4616 warning_at (location, OPT_Wc___compat,
4617 "decrement of enumeration value is invalid in C++");
4620 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4622 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4623 warning_at (location, OPT_Wbool_operation,
4624 "increment of a boolean expression");
4625 else
4626 warning_at (location, OPT_Wbool_operation,
4627 "decrement of a boolean expression");
4630 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4631 arg = c_fully_fold (arg, false, NULL, true);
4633 bool atomic_op;
4634 atomic_op = really_atomic_lvalue (arg);
4636 /* Increment or decrement the real part of the value,
4637 and don't change the imaginary part. */
4638 if (typecode == COMPLEX_TYPE)
4640 tree real, imag;
4642 pedwarn (location, OPT_Wpedantic,
4643 "ISO C does not support %<++%> and %<--%> on complex types");
4645 if (!atomic_op)
4647 arg = stabilize_reference (arg);
4648 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4649 true);
4650 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4651 true);
4652 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4653 if (real == error_mark_node || imag == error_mark_node)
4654 return error_mark_node;
4655 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4656 real, imag);
4657 goto return_build_unary_op;
4661 /* Report invalid types. */
4663 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4664 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4665 && typecode != COMPLEX_TYPE
4666 && !gnu_vector_type_p (TREE_TYPE (arg)))
4668 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4669 error_at (location, "wrong type argument to increment");
4670 else
4671 error_at (location, "wrong type argument to decrement");
4673 return error_mark_node;
4677 tree inc;
4679 argtype = TREE_TYPE (arg);
4681 /* Compute the increment. */
4683 if (typecode == POINTER_TYPE)
4685 /* If pointer target is an incomplete type,
4686 we just cannot know how to do the arithmetic. */
4687 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4689 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4690 error_at (location,
4691 "increment of pointer to an incomplete type %qT",
4692 TREE_TYPE (argtype));
4693 else
4694 error_at (location,
4695 "decrement of pointer to an incomplete type %qT",
4696 TREE_TYPE (argtype));
4698 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4699 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4701 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4702 pedwarn (location, OPT_Wpointer_arith,
4703 "wrong type argument to increment");
4704 else
4705 pedwarn (location, OPT_Wpointer_arith,
4706 "wrong type argument to decrement");
4708 else
4709 verify_type_context (location, TCTX_POINTER_ARITH,
4710 TREE_TYPE (argtype));
4712 inc = c_size_in_bytes (TREE_TYPE (argtype));
4713 inc = convert_to_ptrofftype_loc (location, inc);
4715 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4717 /* For signed fract types, we invert ++ to -- or
4718 -- to ++, and change inc from 1 to -1, because
4719 it is not possible to represent 1 in signed fract constants.
4720 For unsigned fract types, the result always overflows and
4721 we get an undefined (original) or the maximum value. */
4722 if (code == PREINCREMENT_EXPR)
4723 code = PREDECREMENT_EXPR;
4724 else if (code == PREDECREMENT_EXPR)
4725 code = PREINCREMENT_EXPR;
4726 else if (code == POSTINCREMENT_EXPR)
4727 code = POSTDECREMENT_EXPR;
4728 else /* code == POSTDECREMENT_EXPR */
4729 code = POSTINCREMENT_EXPR;
4731 inc = integer_minus_one_node;
4732 inc = convert (argtype, inc);
4734 else
4736 inc = VECTOR_TYPE_P (argtype)
4737 ? build_one_cst (argtype)
4738 : integer_one_node;
4739 inc = convert (argtype, inc);
4742 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4743 need to ask Objective-C to build the increment or decrement
4744 expression for it. */
4745 if (objc_is_property_ref (arg))
4746 return objc_build_incr_expr_for_property_ref (location, code,
4747 arg, inc);
4749 /* Report a read-only lvalue. */
4750 if (TYPE_READONLY (argtype))
4752 readonly_error (location, arg,
4753 ((code == PREINCREMENT_EXPR
4754 || code == POSTINCREMENT_EXPR)
4755 ? lv_increment : lv_decrement));
4756 return error_mark_node;
4758 else if (TREE_READONLY (arg))
4759 readonly_warning (arg,
4760 ((code == PREINCREMENT_EXPR
4761 || code == POSTINCREMENT_EXPR)
4762 ? lv_increment : lv_decrement));
4764 /* If the argument is atomic, use the special code sequences for
4765 atomic compound assignment. */
4766 if (atomic_op)
4768 arg = stabilize_reference (arg);
4769 ret = build_atomic_assign (location, arg,
4770 ((code == PREINCREMENT_EXPR
4771 || code == POSTINCREMENT_EXPR)
4772 ? PLUS_EXPR
4773 : MINUS_EXPR),
4774 (FRACT_MODE_P (TYPE_MODE (argtype))
4775 ? inc
4776 : integer_one_node),
4777 (code == POSTINCREMENT_EXPR
4778 || code == POSTDECREMENT_EXPR));
4779 goto return_build_unary_op;
4782 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4783 val = boolean_increment (code, arg);
4784 else
4785 val = build2 (code, TREE_TYPE (arg), arg, inc);
4786 TREE_SIDE_EFFECTS (val) = 1;
4787 ret = val;
4788 goto return_build_unary_op;
4791 case ADDR_EXPR:
4792 /* Note that this operation never does default_conversion. */
4794 /* The operand of unary '&' must be an lvalue (which excludes
4795 expressions of type void), or, in C99, the result of a [] or
4796 unary '*' operator. */
4797 if (VOID_TYPE_P (TREE_TYPE (arg))
4798 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4799 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4800 pedwarn (location, 0, "taking address of expression of type %<void%>");
4802 /* Let &* cancel out to simplify resulting code. */
4803 if (INDIRECT_REF_P (arg))
4805 /* Don't let this be an lvalue. */
4806 if (lvalue_p (TREE_OPERAND (arg, 0)))
4807 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4808 ret = TREE_OPERAND (arg, 0);
4809 goto return_build_unary_op;
4812 /* Anything not already handled and not a true memory reference
4813 or a non-lvalue array is an error. */
4814 if (typecode != FUNCTION_TYPE && !noconvert
4815 && !lvalue_or_else (location, arg, lv_addressof))
4816 return error_mark_node;
4818 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4819 folding later. */
4820 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4822 tree inner = build_unary_op (location, code,
4823 C_MAYBE_CONST_EXPR_EXPR (arg),
4824 noconvert);
4825 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4826 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4827 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4828 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4829 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4830 goto return_build_unary_op;
4833 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4834 argtype = TREE_TYPE (arg);
4836 /* If the lvalue is const or volatile, merge that into the type
4837 to which the address will point. This is only needed
4838 for function types. */
4839 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4840 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4841 && TREE_CODE (argtype) == FUNCTION_TYPE)
4843 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4844 int quals = orig_quals;
4846 if (TREE_READONLY (arg))
4847 quals |= TYPE_QUAL_CONST;
4848 if (TREE_THIS_VOLATILE (arg))
4849 quals |= TYPE_QUAL_VOLATILE;
4851 argtype = c_build_qualified_type (argtype, quals);
4854 switch (TREE_CODE (arg))
4856 case COMPONENT_REF:
4857 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4859 error_at (location, "cannot take address of bit-field %qD",
4860 TREE_OPERAND (arg, 1));
4861 return error_mark_node;
4864 /* fall through */
4866 case ARRAY_REF:
4867 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4869 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4870 && !POINTER_TYPE_P (TREE_TYPE (arg))
4871 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4873 error_at (location, "cannot take address of scalar with "
4874 "reverse storage order");
4875 return error_mark_node;
4878 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4879 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4880 warning_at (location, OPT_Wscalar_storage_order,
4881 "address of array with reverse scalar storage "
4882 "order requested");
4885 default:
4886 break;
4889 if (!c_mark_addressable (arg))
4890 return error_mark_node;
4892 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4893 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4895 argtype = build_pointer_type (argtype);
4897 /* ??? Cope with user tricks that amount to offsetof. Delete this
4898 when we have proper support for integer constant expressions. */
4899 val = get_base_address (arg);
4900 if (val && INDIRECT_REF_P (val)
4901 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4903 ret = fold_offsetof (arg, argtype);
4904 goto return_build_unary_op;
4907 val = build1 (ADDR_EXPR, argtype, arg);
4909 ret = val;
4910 goto return_build_unary_op;
4912 default:
4913 gcc_unreachable ();
4916 if (argtype == NULL_TREE)
4917 argtype = TREE_TYPE (arg);
4918 if (TREE_CODE (arg) == INTEGER_CST)
4919 ret = (require_constant_value
4920 ? fold_build1_initializer_loc (location, code, argtype, arg)
4921 : fold_build1_loc (location, code, argtype, arg));
4922 else
4923 ret = build1 (code, argtype, arg);
4924 return_build_unary_op:
4925 gcc_assert (ret != error_mark_node);
4926 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4927 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4928 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4929 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4930 ret = note_integer_operands (ret);
4931 if (eptype)
4932 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4933 protected_set_expr_location (ret, location);
4934 return ret;
4937 /* Return nonzero if REF is an lvalue valid for this language.
4938 Lvalues can be assigned, unless their type has TYPE_READONLY.
4939 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4941 bool
4942 lvalue_p (const_tree ref)
4944 const enum tree_code code = TREE_CODE (ref);
4946 switch (code)
4948 case REALPART_EXPR:
4949 case IMAGPART_EXPR:
4950 case COMPONENT_REF:
4951 return lvalue_p (TREE_OPERAND (ref, 0));
4953 case C_MAYBE_CONST_EXPR:
4954 return lvalue_p (TREE_OPERAND (ref, 1));
4956 case COMPOUND_LITERAL_EXPR:
4957 case STRING_CST:
4958 return true;
4960 case INDIRECT_REF:
4961 case ARRAY_REF:
4962 case VAR_DECL:
4963 case PARM_DECL:
4964 case RESULT_DECL:
4965 case ERROR_MARK:
4966 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4967 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4969 case BIND_EXPR:
4970 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4972 default:
4973 return false;
4977 /* Give a warning for storing in something that is read-only in GCC
4978 terms but not const in ISO C terms. */
4980 static void
4981 readonly_warning (tree arg, enum lvalue_use use)
4983 switch (use)
4985 case lv_assign:
4986 warning (0, "assignment of read-only location %qE", arg);
4987 break;
4988 case lv_increment:
4989 warning (0, "increment of read-only location %qE", arg);
4990 break;
4991 case lv_decrement:
4992 warning (0, "decrement of read-only location %qE", arg);
4993 break;
4994 default:
4995 gcc_unreachable ();
4997 return;
5001 /* Return nonzero if REF is an lvalue valid for this language;
5002 otherwise, print an error message and return zero. USE says
5003 how the lvalue is being used and so selects the error message.
5004 LOCATION is the location at which any error should be reported. */
5006 static int
5007 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5009 int win = lvalue_p (ref);
5011 if (!win)
5012 lvalue_error (loc, use);
5014 return win;
5017 /* Mark EXP saying that we need to be able to take the
5018 address of it; it should not be allocated in a register.
5019 Returns true if successful. ARRAY_REF_P is true if this
5020 is for ARRAY_REF construction - in that case we don't want
5021 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5022 it is fine to use ARRAY_REFs for vector subscripts on vector
5023 register variables. */
5025 bool
5026 c_mark_addressable (tree exp, bool array_ref_p)
5028 tree x = exp;
5030 while (1)
5031 switch (TREE_CODE (x))
5033 case VIEW_CONVERT_EXPR:
5034 if (array_ref_p
5035 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5036 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5037 return true;
5038 x = TREE_OPERAND (x, 0);
5039 break;
5041 case COMPONENT_REF:
5042 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5044 error ("cannot take address of bit-field %qD",
5045 TREE_OPERAND (x, 1));
5046 return false;
5048 /* FALLTHRU */
5049 case ADDR_EXPR:
5050 case ARRAY_REF:
5051 case REALPART_EXPR:
5052 case IMAGPART_EXPR:
5053 x = TREE_OPERAND (x, 0);
5054 break;
5056 case COMPOUND_LITERAL_EXPR:
5057 TREE_ADDRESSABLE (x) = 1;
5058 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5059 return true;
5061 case CONSTRUCTOR:
5062 TREE_ADDRESSABLE (x) = 1;
5063 return true;
5065 case VAR_DECL:
5066 case CONST_DECL:
5067 case PARM_DECL:
5068 case RESULT_DECL:
5069 if (C_DECL_REGISTER (x)
5070 && DECL_NONLOCAL (x))
5072 if (TREE_PUBLIC (x) || is_global_var (x))
5074 error
5075 ("global register variable %qD used in nested function", x);
5076 return false;
5078 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5080 else if (C_DECL_REGISTER (x))
5082 if (TREE_PUBLIC (x) || is_global_var (x))
5083 error ("address of global register variable %qD requested", x);
5084 else
5085 error ("address of register variable %qD requested", x);
5086 return false;
5089 /* FALLTHRU */
5090 case FUNCTION_DECL:
5091 TREE_ADDRESSABLE (x) = 1;
5092 /* FALLTHRU */
5093 default:
5094 return true;
5098 /* Convert EXPR to TYPE, warning about conversion problems with
5099 constants. SEMANTIC_TYPE is the type this conversion would use
5100 without excess precision. If SEMANTIC_TYPE is NULL, this function
5101 is equivalent to convert_and_check. This function is a wrapper that
5102 handles conversions that may be different than
5103 the usual ones because of excess precision. */
5105 static tree
5106 ep_convert_and_check (location_t loc, tree type, tree expr,
5107 tree semantic_type)
5109 if (TREE_TYPE (expr) == type)
5110 return expr;
5112 /* For C11, integer conversions may have results with excess
5113 precision. */
5114 if (flag_isoc11 || !semantic_type)
5115 return convert_and_check (loc, type, expr);
5117 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5118 && TREE_TYPE (expr) != semantic_type)
5120 /* For integers, we need to check the real conversion, not
5121 the conversion to the excess precision type. */
5122 expr = convert_and_check (loc, semantic_type, expr);
5124 /* Result type is the excess precision type, which should be
5125 large enough, so do not check. */
5126 return convert (type, expr);
5129 /* If EXPR refers to a built-in declared without a prototype returns
5130 the actual type of the built-in and, if non-null, set *BLTIN to
5131 a pointer to the built-in. Otherwise return the type of EXPR
5132 and clear *BLTIN if non-null. */
5134 static tree
5135 type_or_builtin_type (tree expr, tree *bltin = NULL)
5137 tree dummy;
5138 if (!bltin)
5139 bltin = &dummy;
5141 *bltin = NULL_TREE;
5143 tree type = TREE_TYPE (expr);
5144 if (TREE_CODE (expr) != ADDR_EXPR)
5145 return type;
5147 tree oper = TREE_OPERAND (expr, 0);
5148 if (!DECL_P (oper)
5149 || TREE_CODE (oper) != FUNCTION_DECL
5150 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5151 return type;
5153 built_in_function code = DECL_FUNCTION_CODE (oper);
5154 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5155 return type;
5157 if ((*bltin = builtin_decl_implicit (code)))
5158 type = build_pointer_type (TREE_TYPE (*bltin));
5160 return type;
5163 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5164 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5165 if folded to an integer constant then the unselected half may
5166 contain arbitrary operations not normally permitted in constant
5167 expressions. Set the location of the expression to LOC. */
5169 tree
5170 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5171 tree op1, tree op1_original_type, location_t op1_loc,
5172 tree op2, tree op2_original_type, location_t op2_loc)
5174 tree type1;
5175 tree type2;
5176 enum tree_code code1;
5177 enum tree_code code2;
5178 tree result_type = NULL;
5179 tree semantic_result_type = NULL;
5180 tree orig_op1 = op1, orig_op2 = op2;
5181 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5182 bool ifexp_int_operands;
5183 tree ret;
5185 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5186 if (op1_int_operands)
5187 op1 = remove_c_maybe_const_expr (op1);
5188 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5189 if (op2_int_operands)
5190 op2 = remove_c_maybe_const_expr (op2);
5191 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5192 if (ifexp_int_operands)
5193 ifexp = remove_c_maybe_const_expr (ifexp);
5195 /* Promote both alternatives. */
5197 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5198 op1 = default_conversion (op1);
5199 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5200 op2 = default_conversion (op2);
5202 if (TREE_CODE (ifexp) == ERROR_MARK
5203 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5204 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5205 return error_mark_node;
5207 tree bltin1 = NULL_TREE;
5208 tree bltin2 = NULL_TREE;
5209 type1 = type_or_builtin_type (op1, &bltin1);
5210 code1 = TREE_CODE (type1);
5211 type2 = type_or_builtin_type (op2, &bltin2);
5212 code2 = TREE_CODE (type2);
5214 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5215 return error_mark_node;
5217 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5218 return error_mark_node;
5220 /* C90 does not permit non-lvalue arrays in conditional expressions.
5221 In C99 they will be pointers by now. */
5222 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5224 error_at (colon_loc, "non-lvalue array in conditional expression");
5225 return error_mark_node;
5228 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5229 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5230 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5231 || code1 == COMPLEX_TYPE)
5232 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5233 || code2 == COMPLEX_TYPE))
5235 semantic_result_type = c_common_type (type1, type2);
5236 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5238 op1 = TREE_OPERAND (op1, 0);
5239 type1 = TREE_TYPE (op1);
5240 gcc_assert (TREE_CODE (type1) == code1);
5242 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5244 op2 = TREE_OPERAND (op2, 0);
5245 type2 = TREE_TYPE (op2);
5246 gcc_assert (TREE_CODE (type2) == code2);
5250 if (warn_cxx_compat)
5252 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5253 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5255 if (TREE_CODE (t1) == ENUMERAL_TYPE
5256 && TREE_CODE (t2) == ENUMERAL_TYPE
5257 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5258 warning_at (colon_loc, OPT_Wc___compat,
5259 ("different enum types in conditional is "
5260 "invalid in C++: %qT vs %qT"),
5261 t1, t2);
5264 /* Quickly detect the usual case where op1 and op2 have the same type
5265 after promotion. */
5266 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5268 if (type1 == type2)
5269 result_type = type1;
5270 else
5271 result_type = TYPE_MAIN_VARIANT (type1);
5273 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5274 || code1 == COMPLEX_TYPE)
5275 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5276 || code2 == COMPLEX_TYPE))
5278 /* In C11, a conditional expression between a floating-point
5279 type and an integer type should convert the integer type to
5280 the evaluation format of the floating-point type, with
5281 possible excess precision. */
5282 tree eptype1 = type1;
5283 tree eptype2 = type2;
5284 if (flag_isoc11)
5286 tree eptype;
5287 if (ANY_INTEGRAL_TYPE_P (type1)
5288 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5290 eptype2 = eptype;
5291 if (!semantic_result_type)
5292 semantic_result_type = c_common_type (type1, type2);
5294 else if (ANY_INTEGRAL_TYPE_P (type2)
5295 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5297 eptype1 = eptype;
5298 if (!semantic_result_type)
5299 semantic_result_type = c_common_type (type1, type2);
5302 result_type = c_common_type (eptype1, eptype2);
5303 if (result_type == error_mark_node)
5304 return error_mark_node;
5305 do_warn_double_promotion (result_type, type1, type2,
5306 "implicit conversion from %qT to %qT to "
5307 "match other result of conditional",
5308 colon_loc);
5310 /* If -Wsign-compare, warn here if type1 and type2 have
5311 different signedness. We'll promote the signed to unsigned
5312 and later code won't know it used to be different.
5313 Do this check on the original types, so that explicit casts
5314 will be considered, but default promotions won't. */
5315 if (c_inhibit_evaluation_warnings == 0)
5317 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5318 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5320 if (unsigned_op1 ^ unsigned_op2)
5322 bool ovf;
5324 /* Do not warn if the result type is signed, since the
5325 signed type will only be chosen if it can represent
5326 all the values of the unsigned type. */
5327 if (!TYPE_UNSIGNED (result_type))
5328 /* OK */;
5329 else
5331 bool op1_maybe_const = true;
5332 bool op2_maybe_const = true;
5334 /* Do not warn if the signed quantity is an
5335 unsuffixed integer literal (or some static
5336 constant expression involving such literals) and
5337 it is non-negative. This warning requires the
5338 operands to be folded for best results, so do
5339 that folding in this case even without
5340 warn_sign_compare to avoid warning options
5341 possibly affecting code generation. */
5342 c_inhibit_evaluation_warnings
5343 += (ifexp == truthvalue_false_node);
5344 op1 = c_fully_fold (op1, require_constant_value,
5345 &op1_maybe_const);
5346 c_inhibit_evaluation_warnings
5347 -= (ifexp == truthvalue_false_node);
5349 c_inhibit_evaluation_warnings
5350 += (ifexp == truthvalue_true_node);
5351 op2 = c_fully_fold (op2, require_constant_value,
5352 &op2_maybe_const);
5353 c_inhibit_evaluation_warnings
5354 -= (ifexp == truthvalue_true_node);
5356 if (warn_sign_compare)
5358 if ((unsigned_op2
5359 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5360 || (unsigned_op1
5361 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5362 /* OK */;
5363 else if (unsigned_op2)
5364 warning_at (op1_loc, OPT_Wsign_compare,
5365 "operand of %<?:%> changes signedness from "
5366 "%qT to %qT due to unsignedness of other "
5367 "operand", TREE_TYPE (orig_op1),
5368 TREE_TYPE (orig_op2));
5369 else
5370 warning_at (op2_loc, OPT_Wsign_compare,
5371 "operand of %<?:%> changes signedness from "
5372 "%qT to %qT due to unsignedness of other "
5373 "operand", TREE_TYPE (orig_op2),
5374 TREE_TYPE (orig_op1));
5376 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5377 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5378 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5379 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5384 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5386 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5387 pedwarn (colon_loc, OPT_Wpedantic,
5388 "ISO C forbids conditional expr with only one void side");
5389 result_type = void_type_node;
5391 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5393 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5394 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5395 addr_space_t as_common;
5397 if (comp_target_types (colon_loc, type1, type2))
5398 result_type = common_pointer_type (type1, type2);
5399 else if (null_pointer_constant_p (orig_op1))
5400 result_type = type2;
5401 else if (null_pointer_constant_p (orig_op2))
5402 result_type = type1;
5403 else if (!addr_space_superset (as1, as2, &as_common))
5405 error_at (colon_loc, "pointers to disjoint address spaces "
5406 "used in conditional expression");
5407 return error_mark_node;
5409 else if (VOID_TYPE_P (TREE_TYPE (type1))
5410 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5412 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5413 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5414 & ~TYPE_QUALS (TREE_TYPE (type1))))
5415 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5416 "pointer to array loses qualifier "
5417 "in conditional expression");
5419 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5420 pedwarn (colon_loc, OPT_Wpedantic,
5421 "ISO C forbids conditional expr between "
5422 "%<void *%> and function pointer");
5423 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5424 TREE_TYPE (type2)));
5426 else if (VOID_TYPE_P (TREE_TYPE (type2))
5427 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5429 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5430 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5431 & ~TYPE_QUALS (TREE_TYPE (type2))))
5432 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5433 "pointer to array loses qualifier "
5434 "in conditional expression");
5436 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5437 pedwarn (colon_loc, OPT_Wpedantic,
5438 "ISO C forbids conditional expr between "
5439 "%<void *%> and function pointer");
5440 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5441 TREE_TYPE (type1)));
5443 /* Objective-C pointer comparisons are a bit more lenient. */
5444 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5445 result_type = objc_common_type (type1, type2);
5446 else
5448 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5449 if (bltin1 && bltin2)
5450 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5451 "pointer type mismatch between %qT and %qT "
5452 "of %qD and %qD in conditional expression",
5453 type1, type2, bltin1, bltin2);
5454 else
5455 pedwarn (colon_loc, 0,
5456 "pointer type mismatch in conditional expression");
5457 result_type = build_pointer_type
5458 (build_qualified_type (void_type_node, qual));
5461 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5463 if (!null_pointer_constant_p (orig_op2))
5464 pedwarn (colon_loc, 0,
5465 "pointer/integer type mismatch in conditional expression");
5466 else
5468 op2 = null_pointer_node;
5470 result_type = type1;
5472 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5474 if (!null_pointer_constant_p (orig_op1))
5475 pedwarn (colon_loc, 0,
5476 "pointer/integer type mismatch in conditional expression");
5477 else
5479 op1 = null_pointer_node;
5481 result_type = type2;
5484 if (!result_type)
5486 if (flag_cond_mismatch)
5487 result_type = void_type_node;
5488 else
5490 error_at (colon_loc, "type mismatch in conditional expression");
5491 return error_mark_node;
5495 /* Merge const and volatile flags of the incoming types. */
5496 result_type
5497 = build_type_variant (result_type,
5498 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5499 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5501 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5502 semantic_result_type);
5503 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5504 semantic_result_type);
5506 if (ifexp_bcp && ifexp == truthvalue_true_node)
5508 op2_int_operands = true;
5509 op1 = c_fully_fold (op1, require_constant_value, NULL);
5511 if (ifexp_bcp && ifexp == truthvalue_false_node)
5513 op1_int_operands = true;
5514 op2 = c_fully_fold (op2, require_constant_value, NULL);
5516 int_const = int_operands = (ifexp_int_operands
5517 && op1_int_operands
5518 && op2_int_operands);
5519 if (int_operands)
5521 int_const = ((ifexp == truthvalue_true_node
5522 && TREE_CODE (orig_op1) == INTEGER_CST
5523 && !TREE_OVERFLOW (orig_op1))
5524 || (ifexp == truthvalue_false_node
5525 && TREE_CODE (orig_op2) == INTEGER_CST
5526 && !TREE_OVERFLOW (orig_op2)));
5529 /* Need to convert condition operand into a vector mask. */
5530 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5532 tree vectype = TREE_TYPE (ifexp);
5533 tree elem_type = TREE_TYPE (vectype);
5534 tree zero = build_int_cst (elem_type, 0);
5535 tree zero_vec = build_vector_from_val (vectype, zero);
5536 tree cmp_type = truth_type_for (vectype);
5537 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5540 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5541 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5542 else
5544 if (int_operands)
5546 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5547 nested inside of the expression. */
5548 op1 = c_fully_fold (op1, false, NULL);
5549 op2 = c_fully_fold (op2, false, NULL);
5551 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5552 if (int_operands)
5553 ret = note_integer_operands (ret);
5555 if (semantic_result_type)
5556 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5558 protected_set_expr_location (ret, colon_loc);
5560 /* If the OP1 and OP2 are the same and don't have side-effects,
5561 warn here, because the COND_EXPR will be turned into OP1. */
5562 if (warn_duplicated_branches
5563 && TREE_CODE (ret) == COND_EXPR
5564 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5565 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5566 "this condition has identical branches");
5568 return ret;
5571 /* EXPR is an expression, location LOC, whose result is discarded.
5572 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5573 whose right-hand operand is such a call, possibly recursively). */
5575 static void
5576 maybe_warn_nodiscard (location_t loc, tree expr)
5578 if (VOID_TYPE_P (TREE_TYPE (expr)))
5579 return;
5580 while (TREE_CODE (expr) == COMPOUND_EXPR)
5582 expr = TREE_OPERAND (expr, 1);
5583 if (EXPR_HAS_LOCATION (expr))
5584 loc = EXPR_LOCATION (expr);
5586 if (TREE_CODE (expr) != CALL_EXPR)
5587 return;
5588 tree fn = CALL_EXPR_FN (expr);
5589 if (!fn)
5590 return;
5591 tree attr;
5592 if (TREE_CODE (fn) == ADDR_EXPR
5593 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5594 && (attr = lookup_attribute ("nodiscard",
5595 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5597 fn = TREE_OPERAND (fn, 0);
5598 tree args = TREE_VALUE (attr);
5599 if (args)
5600 args = TREE_VALUE (args);
5601 auto_diagnostic_group d;
5602 int warned;
5603 if (args)
5604 warned = warning_at (loc, OPT_Wunused_result,
5605 "ignoring return value of %qD, declared with "
5606 "attribute %<nodiscard%>: %E", fn, args);
5607 else
5608 warned = warning_at (loc, OPT_Wunused_result,
5609 "ignoring return value of %qD, declared with "
5610 "attribute %<nodiscard%>", fn);
5611 if (warned)
5612 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5614 else
5616 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5617 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5618 if (!attr)
5619 return;
5620 tree args = TREE_VALUE (attr);
5621 if (args)
5622 args = TREE_VALUE (args);
5623 auto_diagnostic_group d;
5624 int warned;
5625 if (args)
5626 warned = warning_at (loc, OPT_Wunused_result,
5627 "ignoring return value of type %qT, declared "
5628 "with attribute %<nodiscard%>: %E",
5629 rettype, args);
5630 else
5631 warned = warning_at (loc, OPT_Wunused_result,
5632 "ignoring return value of type %qT, declared "
5633 "with attribute %<nodiscard%>", rettype);
5634 if (warned)
5636 if (TREE_CODE (fn) == ADDR_EXPR)
5638 fn = TREE_OPERAND (fn, 0);
5639 if (TREE_CODE (fn) == FUNCTION_DECL)
5640 inform (DECL_SOURCE_LOCATION (fn),
5641 "in call to %qD, declared here", fn);
5647 /* Return a compound expression that performs two expressions and
5648 returns the value of the second of them.
5650 LOC is the location of the COMPOUND_EXPR. */
5652 tree
5653 build_compound_expr (location_t loc, tree expr1, tree expr2)
5655 bool expr1_int_operands, expr2_int_operands;
5656 tree eptype = NULL_TREE;
5657 tree ret;
5659 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5660 if (expr1_int_operands)
5661 expr1 = remove_c_maybe_const_expr (expr1);
5662 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5663 if (expr2_int_operands)
5664 expr2 = remove_c_maybe_const_expr (expr2);
5666 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5667 expr1 = TREE_OPERAND (expr1, 0);
5668 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5670 eptype = TREE_TYPE (expr2);
5671 expr2 = TREE_OPERAND (expr2, 0);
5674 if (!TREE_SIDE_EFFECTS (expr1))
5676 /* The left-hand operand of a comma expression is like an expression
5677 statement: with -Wunused, we should warn if it doesn't have
5678 any side-effects, unless it was explicitly cast to (void). */
5679 if (warn_unused_value)
5681 if (VOID_TYPE_P (TREE_TYPE (expr1))
5682 && CONVERT_EXPR_P (expr1))
5683 ; /* (void) a, b */
5684 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5685 && TREE_CODE (expr1) == COMPOUND_EXPR
5686 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5687 ; /* (void) a, (void) b, c */
5688 else
5689 warning_at (loc, OPT_Wunused_value,
5690 "left-hand operand of comma expression has no effect");
5693 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5694 && warn_unused_value)
5696 tree r = expr1;
5697 location_t cloc = loc;
5698 while (TREE_CODE (r) == COMPOUND_EXPR)
5700 if (EXPR_HAS_LOCATION (r))
5701 cloc = EXPR_LOCATION (r);
5702 r = TREE_OPERAND (r, 1);
5704 if (!TREE_SIDE_EFFECTS (r)
5705 && !VOID_TYPE_P (TREE_TYPE (r))
5706 && !CONVERT_EXPR_P (r))
5707 warning_at (cloc, OPT_Wunused_value,
5708 "right-hand operand of comma expression has no effect");
5711 /* With -Wunused, we should also warn if the left-hand operand does have
5712 side-effects, but computes a value which is not used. For example, in
5713 `foo() + bar(), baz()' the result of the `+' operator is not used,
5714 so we should issue a warning. */
5715 else if (warn_unused_value)
5716 warn_if_unused_value (expr1, loc);
5718 maybe_warn_nodiscard (loc, expr1);
5720 if (expr2 == error_mark_node)
5721 return error_mark_node;
5723 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5725 if (flag_isoc99
5726 && expr1_int_operands
5727 && expr2_int_operands)
5728 ret = note_integer_operands (ret);
5730 if (eptype)
5731 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5733 protected_set_expr_location (ret, loc);
5734 return ret;
5737 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5738 which we are casting. OTYPE is the type of the expression being
5739 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5740 of the cast. -Wcast-qual appeared on the command line. Named
5741 address space qualifiers are not handled here, because they result
5742 in different warnings. */
5744 static void
5745 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5747 tree in_type = type;
5748 tree in_otype = otype;
5749 int added = 0;
5750 int discarded = 0;
5751 bool is_const;
5753 /* Check that the qualifiers on IN_TYPE are a superset of the
5754 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5755 nodes is uninteresting and we stop as soon as we hit a
5756 non-POINTER_TYPE node on either type. */
5759 in_otype = TREE_TYPE (in_otype);
5760 in_type = TREE_TYPE (in_type);
5762 /* GNU C allows cv-qualified function types. 'const' means the
5763 function is very pure, 'volatile' means it can't return. We
5764 need to warn when such qualifiers are added, not when they're
5765 taken away. */
5766 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5767 && TREE_CODE (in_type) == FUNCTION_TYPE)
5768 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5769 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5770 else
5771 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5772 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5774 while (TREE_CODE (in_type) == POINTER_TYPE
5775 && TREE_CODE (in_otype) == POINTER_TYPE);
5777 if (added)
5778 warning_at (loc, OPT_Wcast_qual,
5779 "cast adds %q#v qualifier to function type", added);
5781 if (discarded)
5782 /* There are qualifiers present in IN_OTYPE that are not present
5783 in IN_TYPE. */
5784 warning_at (loc, OPT_Wcast_qual,
5785 "cast discards %qv qualifier from pointer target type",
5786 discarded);
5788 if (added || discarded)
5789 return;
5791 /* A cast from **T to const **T is unsafe, because it can cause a
5792 const value to be changed with no additional warning. We only
5793 issue this warning if T is the same on both sides, and we only
5794 issue the warning if there are the same number of pointers on
5795 both sides, as otherwise the cast is clearly unsafe anyhow. A
5796 cast is unsafe when a qualifier is added at one level and const
5797 is not present at all outer levels.
5799 To issue this warning, we check at each level whether the cast
5800 adds new qualifiers not already seen. We don't need to special
5801 case function types, as they won't have the same
5802 TYPE_MAIN_VARIANT. */
5804 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5805 return;
5806 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5807 return;
5809 in_type = type;
5810 in_otype = otype;
5811 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5814 in_type = TREE_TYPE (in_type);
5815 in_otype = TREE_TYPE (in_otype);
5816 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5817 && !is_const)
5819 warning_at (loc, OPT_Wcast_qual,
5820 "to be safe all intermediate pointers in cast from "
5821 "%qT to %qT must be %<const%> qualified",
5822 otype, type);
5823 break;
5825 if (is_const)
5826 is_const = TYPE_READONLY (in_type);
5828 while (TREE_CODE (in_type) == POINTER_TYPE);
5831 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5833 static bool
5834 c_safe_arg_type_equiv_p (tree t1, tree t2)
5836 t1 = TYPE_MAIN_VARIANT (t1);
5837 t2 = TYPE_MAIN_VARIANT (t2);
5839 if (TREE_CODE (t1) == POINTER_TYPE
5840 && TREE_CODE (t2) == POINTER_TYPE)
5841 return true;
5843 /* The signedness of the parameter matters only when an integral
5844 type smaller than int is promoted to int, otherwise only the
5845 precision of the parameter matters.
5846 This check should make sure that the callee does not see
5847 undefined values in argument registers. */
5848 if (INTEGRAL_TYPE_P (t1)
5849 && INTEGRAL_TYPE_P (t2)
5850 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5851 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5852 || !targetm.calls.promote_prototypes (NULL_TREE)
5853 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5854 return true;
5856 return comptypes (t1, t2);
5859 /* Check if a type cast between two function types can be considered safe. */
5861 static bool
5862 c_safe_function_type_cast_p (tree t1, tree t2)
5864 if (TREE_TYPE (t1) == void_type_node &&
5865 TYPE_ARG_TYPES (t1) == void_list_node)
5866 return true;
5868 if (TREE_TYPE (t2) == void_type_node &&
5869 TYPE_ARG_TYPES (t2) == void_list_node)
5870 return true;
5872 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5873 return false;
5875 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5876 t1 && t2;
5877 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5878 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5879 return false;
5881 return true;
5884 /* Build an expression representing a cast to type TYPE of expression EXPR.
5885 LOC is the location of the cast-- typically the open paren of the cast. */
5887 tree
5888 build_c_cast (location_t loc, tree type, tree expr)
5890 tree value;
5892 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5894 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5895 expr = TREE_OPERAND (expr, 0);
5897 value = expr;
5898 if (int_operands)
5899 value = remove_c_maybe_const_expr (value);
5901 if (type == error_mark_node || expr == error_mark_node)
5902 return error_mark_node;
5904 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5905 only in <protocol> qualifications. But when constructing cast expressions,
5906 the protocols do matter and must be kept around. */
5907 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5908 return build1 (NOP_EXPR, type, expr);
5910 type = TYPE_MAIN_VARIANT (type);
5912 if (TREE_CODE (type) == ARRAY_TYPE)
5914 error_at (loc, "cast specifies array type");
5915 return error_mark_node;
5918 if (TREE_CODE (type) == FUNCTION_TYPE)
5920 error_at (loc, "cast specifies function type");
5921 return error_mark_node;
5924 if (!VOID_TYPE_P (type))
5926 value = require_complete_type (loc, value);
5927 if (value == error_mark_node)
5928 return error_mark_node;
5931 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5933 if (RECORD_OR_UNION_TYPE_P (type))
5934 pedwarn (loc, OPT_Wpedantic,
5935 "ISO C forbids casting nonscalar to the same type");
5937 /* Convert to remove any qualifiers from VALUE's type. */
5938 value = convert (type, value);
5940 else if (TREE_CODE (type) == UNION_TYPE)
5942 tree field;
5944 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5945 if (TREE_TYPE (field) != error_mark_node
5946 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5947 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5948 break;
5950 if (field)
5952 tree t;
5953 bool maybe_const = true;
5955 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5956 t = c_fully_fold (value, false, &maybe_const);
5957 t = build_constructor_single (type, field, t);
5958 if (!maybe_const)
5959 t = c_wrap_maybe_const (t, true);
5960 t = digest_init (loc, type, t,
5961 NULL_TREE, false, true, 0);
5962 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5963 return t;
5965 error_at (loc, "cast to union type from type not present in union");
5966 return error_mark_node;
5968 else
5970 tree otype, ovalue;
5972 if (type == void_type_node)
5974 tree t = build1 (CONVERT_EXPR, type, value);
5975 SET_EXPR_LOCATION (t, loc);
5976 return t;
5979 otype = TREE_TYPE (value);
5981 /* Optionally warn about potentially worrisome casts. */
5982 if (warn_cast_qual
5983 && TREE_CODE (type) == POINTER_TYPE
5984 && TREE_CODE (otype) == POINTER_TYPE)
5985 handle_warn_cast_qual (loc, type, otype);
5987 /* Warn about conversions between pointers to disjoint
5988 address spaces. */
5989 if (TREE_CODE (type) == POINTER_TYPE
5990 && TREE_CODE (otype) == POINTER_TYPE
5991 && !null_pointer_constant_p (value))
5993 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5994 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5995 addr_space_t as_common;
5997 if (!addr_space_superset (as_to, as_from, &as_common))
5999 if (ADDR_SPACE_GENERIC_P (as_from))
6000 warning_at (loc, 0, "cast to %s address space pointer "
6001 "from disjoint generic address space pointer",
6002 c_addr_space_name (as_to));
6004 else if (ADDR_SPACE_GENERIC_P (as_to))
6005 warning_at (loc, 0, "cast to generic address space pointer "
6006 "from disjoint %s address space pointer",
6007 c_addr_space_name (as_from));
6009 else
6010 warning_at (loc, 0, "cast to %s address space pointer "
6011 "from disjoint %s address space pointer",
6012 c_addr_space_name (as_to),
6013 c_addr_space_name (as_from));
6017 /* Warn about possible alignment problems. */
6018 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6019 && TREE_CODE (type) == POINTER_TYPE
6020 && TREE_CODE (otype) == POINTER_TYPE
6021 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6022 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6023 /* Don't warn about opaque types, where the actual alignment
6024 restriction is unknown. */
6025 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6026 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6027 && min_align_of_type (TREE_TYPE (type))
6028 > min_align_of_type (TREE_TYPE (otype)))
6029 warning_at (loc, OPT_Wcast_align,
6030 "cast increases required alignment of target type");
6032 if (TREE_CODE (type) == INTEGER_TYPE
6033 && TREE_CODE (otype) == POINTER_TYPE
6034 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6035 /* Unlike conversion of integers to pointers, where the
6036 warning is disabled for converting constants because
6037 of cases such as SIG_*, warn about converting constant
6038 pointers to integers. In some cases it may cause unwanted
6039 sign extension, and a warning is appropriate. */
6040 warning_at (loc, OPT_Wpointer_to_int_cast,
6041 "cast from pointer to integer of different size");
6043 if (TREE_CODE (value) == CALL_EXPR
6044 && TREE_CODE (type) != TREE_CODE (otype))
6045 warning_at (loc, OPT_Wbad_function_cast,
6046 "cast from function call of type %qT "
6047 "to non-matching type %qT", otype, type);
6049 if (TREE_CODE (type) == POINTER_TYPE
6050 && TREE_CODE (otype) == INTEGER_TYPE
6051 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6052 /* Don't warn about converting any constant. */
6053 && !TREE_CONSTANT (value))
6054 warning_at (loc,
6055 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6056 "of different size");
6058 if (warn_strict_aliasing <= 2)
6059 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6061 /* If pedantic, warn for conversions between function and object
6062 pointer types, except for converting a null pointer constant
6063 to function pointer type. */
6064 if (pedantic
6065 && TREE_CODE (type) == POINTER_TYPE
6066 && TREE_CODE (otype) == POINTER_TYPE
6067 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6068 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6069 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6070 "conversion of function pointer to object pointer type");
6072 if (pedantic
6073 && TREE_CODE (type) == POINTER_TYPE
6074 && TREE_CODE (otype) == POINTER_TYPE
6075 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6076 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6077 && !null_pointer_constant_p (value))
6078 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6079 "conversion of object pointer to function pointer type");
6081 if (TREE_CODE (type) == POINTER_TYPE
6082 && TREE_CODE (otype) == POINTER_TYPE
6083 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6084 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6085 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6086 TREE_TYPE (otype)))
6087 warning_at (loc, OPT_Wcast_function_type,
6088 "cast between incompatible function types"
6089 " from %qT to %qT", otype, type);
6091 ovalue = value;
6092 value = convert (type, value);
6094 /* Ignore any integer overflow caused by the cast. */
6095 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6097 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6099 if (!TREE_OVERFLOW (value))
6101 /* Avoid clobbering a shared constant. */
6102 value = copy_node (value);
6103 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6106 else if (TREE_OVERFLOW (value))
6107 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6108 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6112 /* Don't let a cast be an lvalue. */
6113 if (lvalue_p (value))
6114 value = non_lvalue_loc (loc, value);
6116 /* Don't allow the results of casting to floating-point or complex
6117 types be confused with actual constants, or casts involving
6118 integer and pointer types other than direct integer-to-integer
6119 and integer-to-pointer be confused with integer constant
6120 expressions and null pointer constants. */
6121 if (TREE_CODE (value) == REAL_CST
6122 || TREE_CODE (value) == COMPLEX_CST
6123 || (TREE_CODE (value) == INTEGER_CST
6124 && !((TREE_CODE (expr) == INTEGER_CST
6125 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6126 || TREE_CODE (expr) == REAL_CST
6127 || TREE_CODE (expr) == COMPLEX_CST)))
6128 value = build1 (NOP_EXPR, type, value);
6130 /* If the expression has integer operands and so can occur in an
6131 unevaluated part of an integer constant expression, ensure the
6132 return value reflects this. */
6133 if (int_operands
6134 && INTEGRAL_TYPE_P (type)
6135 && value != error_mark_node
6136 && !EXPR_INT_CONST_OPERANDS (value))
6137 value = note_integer_operands (value);
6139 protected_set_expr_location (value, loc);
6140 return value;
6143 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6144 location of the open paren of the cast, or the position of the cast
6145 expr. */
6146 tree
6147 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6149 tree type;
6150 tree type_expr = NULL_TREE;
6151 bool type_expr_const = true;
6152 tree ret;
6153 int saved_wsp = warn_strict_prototypes;
6155 /* This avoids warnings about unprototyped casts on
6156 integers. E.g. "#define SIG_DFL (void(*)())0". */
6157 if (TREE_CODE (expr) == INTEGER_CST)
6158 warn_strict_prototypes = 0;
6159 type = groktypename (type_name, &type_expr, &type_expr_const);
6160 warn_strict_prototypes = saved_wsp;
6162 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6163 && reject_gcc_builtin (expr))
6164 return error_mark_node;
6166 ret = build_c_cast (loc, type, expr);
6167 if (type_expr)
6169 bool inner_expr_const = true;
6170 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6171 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6172 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6173 && inner_expr_const);
6174 SET_EXPR_LOCATION (ret, loc);
6177 if (!EXPR_HAS_LOCATION (ret))
6178 protected_set_expr_location (ret, loc);
6180 /* C++ does not permits types to be defined in a cast, but it
6181 allows references to incomplete types. */
6182 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6183 warning_at (loc, OPT_Wc___compat,
6184 "defining a type in a cast is invalid in C++");
6186 return ret;
6189 /* Build an assignment expression of lvalue LHS from value RHS.
6190 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6191 may differ from TREE_TYPE (LHS) for an enum bitfield.
6192 MODIFYCODE is the code for a binary operator that we use
6193 to combine the old value of LHS with RHS to get the new value.
6194 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6195 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6196 which may differ from TREE_TYPE (RHS) for an enum value.
6198 LOCATION is the location of the MODIFYCODE operator.
6199 RHS_LOC is the location of the RHS. */
6201 tree
6202 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6203 enum tree_code modifycode,
6204 location_t rhs_loc, tree rhs, tree rhs_origtype)
6206 tree result;
6207 tree newrhs;
6208 tree rhseval = NULL_TREE;
6209 tree lhstype = TREE_TYPE (lhs);
6210 tree olhstype = lhstype;
6211 bool npc;
6212 bool is_atomic_op;
6214 /* Types that aren't fully specified cannot be used in assignments. */
6215 lhs = require_complete_type (location, lhs);
6217 /* Avoid duplicate error messages from operands that had errors. */
6218 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6219 return error_mark_node;
6221 /* Ensure an error for assigning a non-lvalue array to an array in
6222 C90. */
6223 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6225 error_at (location, "assignment to expression with array type");
6226 return error_mark_node;
6229 /* For ObjC properties, defer this check. */
6230 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6231 return error_mark_node;
6233 is_atomic_op = really_atomic_lvalue (lhs);
6235 newrhs = rhs;
6237 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6239 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6240 lhs_origtype, modifycode, rhs_loc, rhs,
6241 rhs_origtype);
6242 if (inner == error_mark_node)
6243 return error_mark_node;
6244 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6245 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6246 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6247 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6248 protected_set_expr_location (result, location);
6249 return result;
6252 /* If a binary op has been requested, combine the old LHS value with the RHS
6253 producing the value we should actually store into the LHS. */
6255 if (modifycode != NOP_EXPR)
6257 lhs = c_fully_fold (lhs, false, NULL, true);
6258 lhs = stabilize_reference (lhs);
6260 /* Construct the RHS for any non-atomic compound assignemnt. */
6261 if (!is_atomic_op)
6263 /* If in LHS op= RHS the RHS has side-effects, ensure they
6264 are preevaluated before the rest of the assignment expression's
6265 side-effects, because RHS could contain e.g. function calls
6266 that modify LHS. */
6267 if (TREE_SIDE_EFFECTS (rhs))
6269 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6270 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6271 else
6272 newrhs = save_expr (rhs);
6273 rhseval = newrhs;
6274 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6275 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6276 newrhs);
6278 newrhs = build_binary_op (location,
6279 modifycode, lhs, newrhs, true);
6281 /* The original type of the right hand side is no longer
6282 meaningful. */
6283 rhs_origtype = NULL_TREE;
6287 if (c_dialect_objc ())
6289 /* Check if we are modifying an Objective-C property reference;
6290 if so, we need to generate setter calls. */
6291 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6292 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6293 else
6294 result = objc_maybe_build_modify_expr (lhs, newrhs);
6295 if (result)
6296 goto return_result;
6298 /* Else, do the check that we postponed for Objective-C. */
6299 if (!lvalue_or_else (location, lhs, lv_assign))
6300 return error_mark_node;
6303 /* Give an error for storing in something that is 'const'. */
6305 if (TYPE_READONLY (lhstype)
6306 || (RECORD_OR_UNION_TYPE_P (lhstype)
6307 && C_TYPE_FIELDS_READONLY (lhstype)))
6309 readonly_error (location, lhs, lv_assign);
6310 return error_mark_node;
6312 else if (TREE_READONLY (lhs))
6313 readonly_warning (lhs, lv_assign);
6315 /* If storing into a structure or union member,
6316 it has probably been given type `int'.
6317 Compute the type that would go with
6318 the actual amount of storage the member occupies. */
6320 if (TREE_CODE (lhs) == COMPONENT_REF
6321 && (TREE_CODE (lhstype) == INTEGER_TYPE
6322 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6323 || TREE_CODE (lhstype) == REAL_TYPE
6324 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6325 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6327 /* If storing in a field that is in actuality a short or narrower than one,
6328 we must store in the field in its actual type. */
6330 if (lhstype != TREE_TYPE (lhs))
6332 lhs = copy_node (lhs);
6333 TREE_TYPE (lhs) = lhstype;
6336 /* Issue -Wc++-compat warnings about an assignment to an enum type
6337 when LHS does not have its original type. This happens for,
6338 e.g., an enum bitfield in a struct. */
6339 if (warn_cxx_compat
6340 && lhs_origtype != NULL_TREE
6341 && lhs_origtype != lhstype
6342 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6344 tree checktype = (rhs_origtype != NULL_TREE
6345 ? rhs_origtype
6346 : TREE_TYPE (rhs));
6347 if (checktype != error_mark_node
6348 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6349 || (is_atomic_op && modifycode != NOP_EXPR)))
6350 warning_at (location, OPT_Wc___compat,
6351 "enum conversion in assignment is invalid in C++");
6354 /* Remove qualifiers. */
6355 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6356 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6358 /* Convert new value to destination type. Fold it first, then
6359 restore any excess precision information, for the sake of
6360 conversion warnings. */
6362 if (!(is_atomic_op && modifycode != NOP_EXPR))
6364 tree rhs_semantic_type = NULL_TREE;
6365 if (!c_in_omp_for)
6367 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6369 rhs_semantic_type = TREE_TYPE (newrhs);
6370 newrhs = TREE_OPERAND (newrhs, 0);
6372 npc = null_pointer_constant_p (newrhs);
6373 newrhs = c_fully_fold (newrhs, false, NULL);
6374 if (rhs_semantic_type)
6375 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6377 else
6378 npc = null_pointer_constant_p (newrhs);
6379 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6380 rhs_origtype, ic_assign, npc,
6381 NULL_TREE, NULL_TREE, 0);
6382 if (TREE_CODE (newrhs) == ERROR_MARK)
6383 return error_mark_node;
6386 /* Emit ObjC write barrier, if necessary. */
6387 if (c_dialect_objc () && flag_objc_gc)
6389 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6390 if (result)
6392 protected_set_expr_location (result, location);
6393 goto return_result;
6397 /* Scan operands. */
6399 if (is_atomic_op)
6400 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6401 else
6403 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6404 TREE_SIDE_EFFECTS (result) = 1;
6405 protected_set_expr_location (result, location);
6408 /* If we got the LHS in a different type for storing in,
6409 convert the result back to the nominal type of LHS
6410 so that the value we return always has the same type
6411 as the LHS argument. */
6413 if (olhstype == TREE_TYPE (result))
6414 goto return_result;
6416 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6417 rhs_origtype, ic_assign, false, NULL_TREE,
6418 NULL_TREE, 0);
6419 protected_set_expr_location (result, location);
6421 return_result:
6422 if (rhseval)
6423 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6424 return result;
6427 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6428 This is used to implement -fplan9-extensions. */
6430 static bool
6431 find_anonymous_field_with_type (tree struct_type, tree type)
6433 tree field;
6434 bool found;
6436 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6437 found = false;
6438 for (field = TYPE_FIELDS (struct_type);
6439 field != NULL_TREE;
6440 field = TREE_CHAIN (field))
6442 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6443 ? c_build_qualified_type (TREE_TYPE (field),
6444 TYPE_QUAL_ATOMIC)
6445 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6446 if (DECL_NAME (field) == NULL
6447 && comptypes (type, fieldtype))
6449 if (found)
6450 return false;
6451 found = true;
6453 else if (DECL_NAME (field) == NULL
6454 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6455 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6457 if (found)
6458 return false;
6459 found = true;
6462 return found;
6465 /* RHS is an expression whose type is pointer to struct. If there is
6466 an anonymous field in RHS with type TYPE, then return a pointer to
6467 that field in RHS. This is used with -fplan9-extensions. This
6468 returns NULL if no conversion could be found. */
6470 static tree
6471 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6473 tree rhs_struct_type, lhs_main_type;
6474 tree field, found_field;
6475 bool found_sub_field;
6476 tree ret;
6478 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6479 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6480 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6482 gcc_assert (POINTER_TYPE_P (type));
6483 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6484 ? c_build_qualified_type (TREE_TYPE (type),
6485 TYPE_QUAL_ATOMIC)
6486 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6488 found_field = NULL_TREE;
6489 found_sub_field = false;
6490 for (field = TYPE_FIELDS (rhs_struct_type);
6491 field != NULL_TREE;
6492 field = TREE_CHAIN (field))
6494 if (DECL_NAME (field) != NULL_TREE
6495 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6496 continue;
6497 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6498 ? c_build_qualified_type (TREE_TYPE (field),
6499 TYPE_QUAL_ATOMIC)
6500 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6501 if (comptypes (lhs_main_type, fieldtype))
6503 if (found_field != NULL_TREE)
6504 return NULL_TREE;
6505 found_field = field;
6507 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6508 lhs_main_type))
6510 if (found_field != NULL_TREE)
6511 return NULL_TREE;
6512 found_field = field;
6513 found_sub_field = true;
6517 if (found_field == NULL_TREE)
6518 return NULL_TREE;
6520 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6521 build_fold_indirect_ref (rhs), found_field,
6522 NULL_TREE);
6523 ret = build_fold_addr_expr_loc (location, ret);
6525 if (found_sub_field)
6527 ret = convert_to_anonymous_field (location, type, ret);
6528 gcc_assert (ret != NULL_TREE);
6531 return ret;
6534 /* Issue an error message for a bad initializer component.
6535 GMSGID identifies the message.
6536 The component name is taken from the spelling stack. */
6538 static void ATTRIBUTE_GCC_DIAG (2,0)
6539 error_init (location_t loc, const char *gmsgid, ...)
6541 char *ofwhat;
6543 auto_diagnostic_group d;
6545 /* The gmsgid may be a format string with %< and %>. */
6546 va_list ap;
6547 va_start (ap, gmsgid);
6548 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6549 va_end (ap);
6551 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6552 if (*ofwhat && warned)
6553 inform (loc, "(near initialization for %qs)", ofwhat);
6556 /* Issue a pedantic warning for a bad initializer component. OPT is
6557 the option OPT_* (from options.h) controlling this warning or 0 if
6558 it is unconditionally given. GMSGID identifies the message. The
6559 component name is taken from the spelling stack. */
6561 static void ATTRIBUTE_GCC_DIAG (3,0)
6562 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6564 /* Use the location where a macro was expanded rather than where
6565 it was defined to make sure macros defined in system headers
6566 but used incorrectly elsewhere are diagnosed. */
6567 location_t exploc = expansion_point_location_if_in_system_header (loc);
6568 auto_diagnostic_group d;
6569 va_list ap;
6570 va_start (ap, gmsgid);
6571 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6572 va_end (ap);
6573 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6574 if (*ofwhat && warned)
6575 inform (exploc, "(near initialization for %qs)", ofwhat);
6578 /* Issue a warning for a bad initializer component.
6580 OPT is the OPT_W* value corresponding to the warning option that
6581 controls this warning. GMSGID identifies the message. The
6582 component name is taken from the spelling stack. */
6584 static void
6585 warning_init (location_t loc, int opt, const char *gmsgid)
6587 char *ofwhat;
6588 bool warned;
6590 auto_diagnostic_group d;
6592 /* Use the location where a macro was expanded rather than where
6593 it was defined to make sure macros defined in system headers
6594 but used incorrectly elsewhere are diagnosed. */
6595 location_t exploc = expansion_point_location_if_in_system_header (loc);
6597 /* The gmsgid may be a format string with %< and %>. */
6598 warned = warning_at (exploc, opt, gmsgid);
6599 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6600 if (*ofwhat && warned)
6601 inform (exploc, "(near initialization for %qs)", ofwhat);
6604 /* If TYPE is an array type and EXPR is a parenthesized string
6605 constant, warn if pedantic that EXPR is being used to initialize an
6606 object of type TYPE. */
6608 void
6609 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6611 if (pedantic
6612 && TREE_CODE (type) == ARRAY_TYPE
6613 && TREE_CODE (expr.value) == STRING_CST
6614 && expr.original_code != STRING_CST)
6615 pedwarn_init (loc, OPT_Wpedantic,
6616 "array initialized from parenthesized string constant");
6619 /* Attempt to locate the parameter with the given index within FNDECL,
6620 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6622 static location_t
6623 get_fndecl_argument_location (tree fndecl, int argnum)
6625 int i;
6626 tree param;
6628 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6629 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6630 i < argnum && param;
6631 i++, param = TREE_CHAIN (param))
6634 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6635 return DECL_SOURCE_LOCATION (FNDECL). */
6636 if (param == NULL)
6637 return DECL_SOURCE_LOCATION (fndecl);
6639 return DECL_SOURCE_LOCATION (param);
6642 /* Issue a note about a mismatching argument for parameter PARMNUM
6643 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6644 Attempt to issue the note at the pertinent parameter of the decl;
6645 failing that issue it at the location of FUNDECL; failing that
6646 issue it at PLOC. */
6648 static void
6649 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6650 tree expected_type, tree actual_type)
6652 location_t loc;
6653 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6654 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6655 else
6656 loc = ploc;
6658 inform (loc,
6659 "expected %qT but argument is of type %qT",
6660 expected_type, actual_type);
6663 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6664 function FUNDECL declared without prototype to parameter PARMNUM of
6665 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6667 static void
6668 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6669 tree parmtype, tree argtype)
6671 tree_code parmcode = TREE_CODE (parmtype);
6672 tree_code argcode = TREE_CODE (argtype);
6673 tree promoted = c_type_promotes_to (argtype);
6675 /* Avoid warning for enum arguments that promote to an integer type
6676 of the same size/mode. */
6677 if (parmcode == INTEGER_TYPE
6678 && argcode == ENUMERAL_TYPE
6679 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6680 return;
6682 if ((parmcode == argcode
6683 || (parmcode == INTEGER_TYPE
6684 && argcode == ENUMERAL_TYPE))
6685 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6686 return;
6688 /* This diagnoses even signed/unsigned mismatches. Those might be
6689 safe in many cases but GCC may emit suboptimal code for them so
6690 warning on those cases drives efficiency improvements. */
6691 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6692 TYPE_MAIN_VARIANT (promoted) == argtype
6693 ? G_("%qD argument %d type is %qT where %qT is expected "
6694 "in a call to built-in function declared without "
6695 "prototype")
6696 : G_("%qD argument %d promotes to %qT where %qT is expected "
6697 "in a call to built-in function declared without "
6698 "prototype"),
6699 fundecl, parmnum, promoted, parmtype))
6700 inform (DECL_SOURCE_LOCATION (fundecl),
6701 "built-in %qD declared here",
6702 fundecl);
6705 /* Convert value RHS to type TYPE as preparation for an assignment to
6706 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6707 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6708 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6709 constant before any folding.
6710 The real work of conversion is done by `convert'.
6711 The purpose of this function is to generate error messages
6712 for assignments that are not allowed in C.
6713 ERRTYPE says whether it is argument passing, assignment,
6714 initialization or return.
6716 In the following example, '~' denotes where EXPR_LOC and '^' where
6717 LOCATION point to:
6719 f (var); [ic_argpass]
6720 ^ ~~~
6721 x = var; [ic_assign]
6722 ^ ~~~;
6723 int x = var; [ic_init]
6725 return x; [ic_return]
6728 FUNCTION is a tree for the function being called.
6729 PARMNUM is the number of the argument, for printing in error messages.
6730 WARNOPT may be set to a warning option to issue the corresponding warning
6731 rather than an error for invalid conversions. Used for calls to built-in
6732 functions declared without a prototype. */
6734 static tree
6735 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6736 tree rhs, tree origtype, enum impl_conv errtype,
6737 bool null_pointer_constant, tree fundecl,
6738 tree function, int parmnum, int warnopt /* = 0 */)
6740 enum tree_code codel = TREE_CODE (type);
6741 tree orig_rhs = rhs;
6742 tree rhstype;
6743 enum tree_code coder;
6744 tree rname = NULL_TREE;
6745 bool objc_ok = false;
6747 /* Use the expansion point location to handle cases such as user's
6748 function returning a wrong-type macro defined in a system header. */
6749 location = expansion_point_location_if_in_system_header (location);
6751 if (errtype == ic_argpass)
6753 tree selector;
6754 /* Change pointer to function to the function itself for
6755 diagnostics. */
6756 if (TREE_CODE (function) == ADDR_EXPR
6757 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6758 function = TREE_OPERAND (function, 0);
6760 /* Handle an ObjC selector specially for diagnostics. */
6761 selector = objc_message_selector ();
6762 rname = function;
6763 if (selector && parmnum > 2)
6765 rname = selector;
6766 parmnum -= 2;
6770 /* This macro is used to emit diagnostics to ensure that all format
6771 strings are complete sentences, visible to gettext and checked at
6772 compile time. */
6773 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6774 do { \
6775 switch (errtype) \
6777 case ic_argpass: \
6779 auto_diagnostic_group d; \
6780 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6781 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6783 break; \
6784 case ic_assign: \
6785 pedwarn (LOCATION, OPT, AS); \
6786 break; \
6787 case ic_init: \
6788 pedwarn_init (LOCATION, OPT, IN); \
6789 break; \
6790 case ic_return: \
6791 pedwarn (LOCATION, OPT, RE); \
6792 break; \
6793 default: \
6794 gcc_unreachable (); \
6796 } while (0)
6798 /* This macro is used to emit diagnostics to ensure that all format
6799 strings are complete sentences, visible to gettext and checked at
6800 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6801 extra parameter to enumerate qualifiers. */
6802 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6803 do { \
6804 switch (errtype) \
6806 case ic_argpass: \
6808 auto_diagnostic_group d; \
6809 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6810 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6812 break; \
6813 case ic_assign: \
6814 pedwarn (LOCATION, OPT, AS, QUALS); \
6815 break; \
6816 case ic_init: \
6817 pedwarn (LOCATION, OPT, IN, QUALS); \
6818 break; \
6819 case ic_return: \
6820 pedwarn (LOCATION, OPT, RE, QUALS); \
6821 break; \
6822 default: \
6823 gcc_unreachable (); \
6825 } while (0)
6827 /* This macro is used to emit diagnostics to ensure that all format
6828 strings are complete sentences, visible to gettext and checked at
6829 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6830 warning_at instead of pedwarn. */
6831 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6832 do { \
6833 switch (errtype) \
6835 case ic_argpass: \
6837 auto_diagnostic_group d; \
6838 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6839 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6841 break; \
6842 case ic_assign: \
6843 warning_at (LOCATION, OPT, AS, QUALS); \
6844 break; \
6845 case ic_init: \
6846 warning_at (LOCATION, OPT, IN, QUALS); \
6847 break; \
6848 case ic_return: \
6849 warning_at (LOCATION, OPT, RE, QUALS); \
6850 break; \
6851 default: \
6852 gcc_unreachable (); \
6854 } while (0)
6856 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6857 rhs = TREE_OPERAND (rhs, 0);
6859 rhstype = TREE_TYPE (rhs);
6860 coder = TREE_CODE (rhstype);
6862 if (coder == ERROR_MARK)
6863 return error_mark_node;
6865 if (c_dialect_objc ())
6867 int parmno;
6869 switch (errtype)
6871 case ic_return:
6872 parmno = 0;
6873 break;
6875 case ic_assign:
6876 parmno = -1;
6877 break;
6879 case ic_init:
6880 parmno = -2;
6881 break;
6883 default:
6884 parmno = parmnum;
6885 break;
6888 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6891 if (warn_cxx_compat)
6893 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6894 if (checktype != error_mark_node
6895 && TREE_CODE (type) == ENUMERAL_TYPE
6896 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6897 switch (errtype)
6899 case ic_argpass:
6900 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6901 "passing argument %d of %qE is invalid in C++",
6902 parmnum, rname))
6903 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6904 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6905 "expected %qT but argument is of type %qT",
6906 type, rhstype);
6907 break;
6908 case ic_assign:
6909 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6910 "%qT in assignment is invalid in C++", rhstype, type);
6911 break;
6912 case ic_init:
6913 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6914 "%qT to %qT in initialization is invalid in C++",
6915 rhstype, type);
6916 break;
6917 case ic_return:
6918 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6919 "%qT in return is invalid in C++", rhstype, type);
6920 break;
6921 default:
6922 gcc_unreachable ();
6926 if (warn_enum_conversion)
6928 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6929 if (checktype != error_mark_node
6930 && TREE_CODE (checktype) == ENUMERAL_TYPE
6931 && TREE_CODE (type) == ENUMERAL_TYPE
6932 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6934 gcc_rich_location loc (location);
6935 warning_at (&loc, OPT_Wenum_conversion,
6936 "implicit conversion from %qT to %qT",
6937 checktype, type);
6941 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6943 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6944 return rhs;
6947 if (coder == VOID_TYPE)
6949 /* Except for passing an argument to an unprototyped function,
6950 this is a constraint violation. When passing an argument to
6951 an unprototyped function, it is compile-time undefined;
6952 making it a constraint in that case was rejected in
6953 DR#252. */
6954 const char msg[] = "void value not ignored as it ought to be";
6955 if (warnopt)
6956 warning_at (location, warnopt, msg);
6957 else
6958 error_at (location, msg);
6959 return error_mark_node;
6961 rhs = require_complete_type (location, rhs);
6962 if (rhs == error_mark_node)
6963 return error_mark_node;
6965 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6966 return error_mark_node;
6968 /* A non-reference type can convert to a reference. This handles
6969 va_start, va_copy and possibly port built-ins. */
6970 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6972 if (!lvalue_p (rhs))
6974 const char msg[] = "cannot pass rvalue to reference parameter";
6975 if (warnopt)
6976 warning_at (location, warnopt, msg);
6977 else
6978 error_at (location, msg);
6979 return error_mark_node;
6981 if (!c_mark_addressable (rhs))
6982 return error_mark_node;
6983 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6984 SET_EXPR_LOCATION (rhs, location);
6986 rhs = convert_for_assignment (location, expr_loc,
6987 build_pointer_type (TREE_TYPE (type)),
6988 rhs, origtype, errtype,
6989 null_pointer_constant, fundecl, function,
6990 parmnum, warnopt);
6991 if (rhs == error_mark_node)
6992 return error_mark_node;
6994 rhs = build1 (NOP_EXPR, type, rhs);
6995 SET_EXPR_LOCATION (rhs, location);
6996 return rhs;
6998 /* Some types can interconvert without explicit casts. */
6999 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7000 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7001 return convert (type, rhs);
7002 /* Arithmetic types all interconvert, and enum is treated like int. */
7003 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7004 || codel == FIXED_POINT_TYPE
7005 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7006 || codel == BOOLEAN_TYPE)
7007 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7008 || coder == FIXED_POINT_TYPE
7009 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7010 || coder == BOOLEAN_TYPE))
7012 if (warnopt && errtype == ic_argpass)
7013 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7014 rhstype);
7016 bool save = in_late_binary_op;
7017 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
7018 || (coder == REAL_TYPE
7019 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7020 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7021 in_late_binary_op = true;
7022 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7023 ? expr_loc : location, type, orig_rhs);
7024 in_late_binary_op = save;
7025 return ret;
7028 /* Aggregates in different TUs might need conversion. */
7029 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7030 && codel == coder
7031 && comptypes (type, rhstype))
7032 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7033 ? expr_loc : location, type, rhs);
7035 /* Conversion to a transparent union or record from its member types.
7036 This applies only to function arguments. */
7037 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7038 && TYPE_TRANSPARENT_AGGR (type))
7039 && errtype == ic_argpass)
7041 tree memb, marginal_memb = NULL_TREE;
7043 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7045 tree memb_type = TREE_TYPE (memb);
7047 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7048 TYPE_MAIN_VARIANT (rhstype)))
7049 break;
7051 if (TREE_CODE (memb_type) != POINTER_TYPE)
7052 continue;
7054 if (coder == POINTER_TYPE)
7056 tree ttl = TREE_TYPE (memb_type);
7057 tree ttr = TREE_TYPE (rhstype);
7059 /* Any non-function converts to a [const][volatile] void *
7060 and vice versa; otherwise, targets must be the same.
7061 Meanwhile, the lhs target must have all the qualifiers of
7062 the rhs. */
7063 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7064 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7065 || comp_target_types (location, memb_type, rhstype))
7067 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7068 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7069 /* If this type won't generate any warnings, use it. */
7070 if (lquals == rquals
7071 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7072 && TREE_CODE (ttl) == FUNCTION_TYPE)
7073 ? ((lquals | rquals) == rquals)
7074 : ((lquals | rquals) == lquals)))
7075 break;
7077 /* Keep looking for a better type, but remember this one. */
7078 if (!marginal_memb)
7079 marginal_memb = memb;
7083 /* Can convert integer zero to any pointer type. */
7084 if (null_pointer_constant)
7086 rhs = null_pointer_node;
7087 break;
7091 if (memb || marginal_memb)
7093 if (!memb)
7095 /* We have only a marginally acceptable member type;
7096 it needs a warning. */
7097 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7098 tree ttr = TREE_TYPE (rhstype);
7100 /* Const and volatile mean something different for function
7101 types, so the usual warnings are not appropriate. */
7102 if (TREE_CODE (ttr) == FUNCTION_TYPE
7103 && TREE_CODE (ttl) == FUNCTION_TYPE)
7105 /* Because const and volatile on functions are
7106 restrictions that say the function will not do
7107 certain things, it is okay to use a const or volatile
7108 function where an ordinary one is wanted, but not
7109 vice-versa. */
7110 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7111 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7112 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7113 OPT_Wdiscarded_qualifiers,
7114 G_("passing argument %d of %qE "
7115 "makes %q#v qualified function "
7116 "pointer from unqualified"),
7117 G_("assignment makes %q#v qualified "
7118 "function pointer from "
7119 "unqualified"),
7120 G_("initialization makes %q#v qualified "
7121 "function pointer from "
7122 "unqualified"),
7123 G_("return makes %q#v qualified function "
7124 "pointer from unqualified"),
7125 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7127 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7128 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7129 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7130 OPT_Wdiscarded_qualifiers,
7131 G_("passing argument %d of %qE discards "
7132 "%qv qualifier from pointer target type"),
7133 G_("assignment discards %qv qualifier "
7134 "from pointer target type"),
7135 G_("initialization discards %qv qualifier "
7136 "from pointer target type"),
7137 G_("return discards %qv qualifier from "
7138 "pointer target type"),
7139 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7141 memb = marginal_memb;
7144 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7145 pedwarn (location, OPT_Wpedantic,
7146 "ISO C prohibits argument conversion to union type");
7148 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7149 return build_constructor_single (type, memb, rhs);
7153 /* Conversions among pointers */
7154 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7155 && (coder == codel))
7157 /* If RHS refers to a built-in declared without a prototype
7158 BLTIN is the declaration of the built-in with a prototype
7159 and RHSTYPE is set to the actual type of the built-in. */
7160 tree bltin;
7161 rhstype = type_or_builtin_type (rhs, &bltin);
7163 tree ttl = TREE_TYPE (type);
7164 tree ttr = TREE_TYPE (rhstype);
7165 tree mvl = ttl;
7166 tree mvr = ttr;
7167 bool is_opaque_pointer;
7168 int target_cmp = 0; /* Cache comp_target_types () result. */
7169 addr_space_t asl;
7170 addr_space_t asr;
7172 if (TREE_CODE (mvl) != ARRAY_TYPE)
7173 mvl = (TYPE_ATOMIC (mvl)
7174 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7175 TYPE_QUAL_ATOMIC)
7176 : TYPE_MAIN_VARIANT (mvl));
7177 if (TREE_CODE (mvr) != ARRAY_TYPE)
7178 mvr = (TYPE_ATOMIC (mvr)
7179 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7180 TYPE_QUAL_ATOMIC)
7181 : TYPE_MAIN_VARIANT (mvr));
7182 /* Opaque pointers are treated like void pointers. */
7183 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7185 /* The Plan 9 compiler permits a pointer to a struct to be
7186 automatically converted into a pointer to an anonymous field
7187 within the struct. */
7188 if (flag_plan9_extensions
7189 && RECORD_OR_UNION_TYPE_P (mvl)
7190 && RECORD_OR_UNION_TYPE_P (mvr)
7191 && mvl != mvr)
7193 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7194 if (new_rhs != NULL_TREE)
7196 rhs = new_rhs;
7197 rhstype = TREE_TYPE (rhs);
7198 coder = TREE_CODE (rhstype);
7199 ttr = TREE_TYPE (rhstype);
7200 mvr = TYPE_MAIN_VARIANT (ttr);
7204 /* C++ does not allow the implicit conversion void* -> T*. However,
7205 for the purpose of reducing the number of false positives, we
7206 tolerate the special case of
7208 int *p = NULL;
7210 where NULL is typically defined in C to be '(void *) 0'. */
7211 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7212 warning_at (errtype == ic_argpass ? expr_loc : location,
7213 OPT_Wc___compat,
7214 "request for implicit conversion "
7215 "from %qT to %qT not permitted in C++", rhstype, type);
7217 /* See if the pointers point to incompatible address spaces. */
7218 asl = TYPE_ADDR_SPACE (ttl);
7219 asr = TYPE_ADDR_SPACE (ttr);
7220 if (!null_pointer_constant_p (rhs)
7221 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7223 switch (errtype)
7225 case ic_argpass:
7227 const char msg[] = G_("passing argument %d of %qE from "
7228 "pointer to non-enclosed address space");
7229 if (warnopt)
7230 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7231 else
7232 error_at (expr_loc, msg, parmnum, rname);
7233 break;
7235 case ic_assign:
7237 const char msg[] = G_("assignment from pointer to "
7238 "non-enclosed address space");
7239 if (warnopt)
7240 warning_at (location, warnopt, msg);
7241 else
7242 error_at (location, msg);
7243 break;
7245 case ic_init:
7247 const char msg[] = G_("initialization from pointer to "
7248 "non-enclosed address space");
7249 if (warnopt)
7250 warning_at (location, warnopt, msg);
7251 else
7252 error_at (location, msg);
7253 break;
7255 case ic_return:
7257 const char msg[] = G_("return from pointer to "
7258 "non-enclosed address space");
7259 if (warnopt)
7260 warning_at (location, warnopt, msg);
7261 else
7262 error_at (location, msg);
7263 break;
7265 default:
7266 gcc_unreachable ();
7268 return error_mark_node;
7271 /* Check if the right-hand side has a format attribute but the
7272 left-hand side doesn't. */
7273 if (warn_suggest_attribute_format
7274 && check_missing_format_attribute (type, rhstype))
7276 switch (errtype)
7278 case ic_argpass:
7279 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7280 "argument %d of %qE might be "
7281 "a candidate for a format attribute",
7282 parmnum, rname);
7283 break;
7284 case ic_assign:
7285 warning_at (location, OPT_Wsuggest_attribute_format,
7286 "assignment left-hand side might be "
7287 "a candidate for a format attribute");
7288 break;
7289 case ic_init:
7290 warning_at (location, OPT_Wsuggest_attribute_format,
7291 "initialization left-hand side might be "
7292 "a candidate for a format attribute");
7293 break;
7294 case ic_return:
7295 warning_at (location, OPT_Wsuggest_attribute_format,
7296 "return type might be "
7297 "a candidate for a format attribute");
7298 break;
7299 default:
7300 gcc_unreachable ();
7304 /* See if the pointers point to incompatible scalar storage orders. */
7305 if (warn_scalar_storage_order
7306 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7307 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7309 tree t;
7311 switch (errtype)
7313 case ic_argpass:
7314 /* Do not warn for built-in functions, for example memcpy, since we
7315 control how they behave and they can be useful in this area. */
7316 if (TREE_CODE (rname) != FUNCTION_DECL
7317 || !fndecl_built_in_p (rname))
7318 warning_at (location, OPT_Wscalar_storage_order,
7319 "passing argument %d of %qE from incompatible "
7320 "scalar storage order", parmnum, rname);
7321 break;
7322 case ic_assign:
7323 /* Do not warn if the RHS is a call to a function that returns a
7324 pointer that is not an alias. */
7325 if (TREE_CODE (rhs) != CALL_EXPR
7326 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7327 || !DECL_IS_MALLOC (t))
7328 warning_at (location, OPT_Wscalar_storage_order,
7329 "assignment to %qT from pointer type %qT with "
7330 "incompatible scalar storage order", type, rhstype);
7331 break;
7332 case ic_init:
7333 /* Likewise. */
7334 if (TREE_CODE (rhs) != CALL_EXPR
7335 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7336 || !DECL_IS_MALLOC (t))
7337 warning_at (location, OPT_Wscalar_storage_order,
7338 "initialization of %qT from pointer type %qT with "
7339 "incompatible scalar storage order", type, rhstype);
7340 break;
7341 case ic_return:
7342 warning_at (location, OPT_Wscalar_storage_order,
7343 "returning %qT from pointer type with incompatible "
7344 "scalar storage order %qT", rhstype, type);
7345 break;
7346 default:
7347 gcc_unreachable ();
7351 /* Any non-function converts to a [const][volatile] void *
7352 and vice versa; otherwise, targets must be the same.
7353 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7354 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7355 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7356 || (target_cmp = comp_target_types (location, type, rhstype))
7357 || is_opaque_pointer
7358 || ((c_common_unsigned_type (mvl)
7359 == c_common_unsigned_type (mvr))
7360 && (c_common_signed_type (mvl)
7361 == c_common_signed_type (mvr))
7362 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7364 /* Warn about loss of qualifers from pointers to arrays with
7365 qualifiers on the element type. */
7366 if (TREE_CODE (ttr) == ARRAY_TYPE)
7368 ttr = strip_array_types (ttr);
7369 ttl = strip_array_types (ttl);
7371 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7372 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7373 WARNING_FOR_QUALIFIERS (location, expr_loc,
7374 OPT_Wdiscarded_array_qualifiers,
7375 G_("passing argument %d of %qE discards "
7376 "%qv qualifier from pointer target type"),
7377 G_("assignment discards %qv qualifier "
7378 "from pointer target type"),
7379 G_("initialization discards %qv qualifier "
7380 "from pointer target type"),
7381 G_("return discards %qv qualifier from "
7382 "pointer target type"),
7383 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7385 else if (pedantic
7386 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7388 (VOID_TYPE_P (ttr)
7389 && !null_pointer_constant
7390 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7391 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7392 G_("ISO C forbids passing argument %d of "
7393 "%qE between function pointer "
7394 "and %<void *%>"),
7395 G_("ISO C forbids assignment between "
7396 "function pointer and %<void *%>"),
7397 G_("ISO C forbids initialization between "
7398 "function pointer and %<void *%>"),
7399 G_("ISO C forbids return between function "
7400 "pointer and %<void *%>"));
7401 /* Const and volatile mean something different for function types,
7402 so the usual warnings are not appropriate. */
7403 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7404 && TREE_CODE (ttl) != FUNCTION_TYPE)
7406 /* Don't warn about loss of qualifier for conversions from
7407 qualified void* to pointers to arrays with corresponding
7408 qualifier on the element type. */
7409 if (!pedantic)
7410 ttl = strip_array_types (ttl);
7412 /* Assignments between atomic and non-atomic objects are OK. */
7413 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7414 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7416 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7417 OPT_Wdiscarded_qualifiers,
7418 G_("passing argument %d of %qE discards "
7419 "%qv qualifier from pointer target type"),
7420 G_("assignment discards %qv qualifier "
7421 "from pointer target type"),
7422 G_("initialization discards %qv qualifier "
7423 "from pointer target type"),
7424 G_("return discards %qv qualifier from "
7425 "pointer target type"),
7426 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7428 /* If this is not a case of ignoring a mismatch in signedness,
7429 no warning. */
7430 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7431 || target_cmp)
7433 /* If there is a mismatch, do warn. */
7434 else if (warn_pointer_sign)
7435 switch (errtype)
7437 case ic_argpass:
7439 auto_diagnostic_group d;
7440 range_label_for_type_mismatch rhs_label (rhstype, type);
7441 gcc_rich_location richloc (expr_loc, &rhs_label);
7442 if (pedwarn (&richloc, OPT_Wpointer_sign,
7443 "pointer targets in passing argument %d of "
7444 "%qE differ in signedness", parmnum, rname))
7445 inform_for_arg (fundecl, expr_loc, parmnum, type,
7446 rhstype);
7448 break;
7449 case ic_assign:
7450 pedwarn (location, OPT_Wpointer_sign,
7451 "pointer targets in assignment from %qT to %qT "
7452 "differ in signedness", rhstype, type);
7453 break;
7454 case ic_init:
7455 pedwarn_init (location, OPT_Wpointer_sign,
7456 "pointer targets in initialization of %qT "
7457 "from %qT differ in signedness", type,
7458 rhstype);
7459 break;
7460 case ic_return:
7461 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7462 "returning %qT from a function with return type "
7463 "%qT differ in signedness", rhstype, type);
7464 break;
7465 default:
7466 gcc_unreachable ();
7469 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7470 && TREE_CODE (ttr) == FUNCTION_TYPE)
7472 /* Because const and volatile on functions are restrictions
7473 that say the function will not do certain things,
7474 it is okay to use a const or volatile function
7475 where an ordinary one is wanted, but not vice-versa. */
7476 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7477 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7478 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7479 OPT_Wdiscarded_qualifiers,
7480 G_("passing argument %d of %qE makes "
7481 "%q#v qualified function pointer "
7482 "from unqualified"),
7483 G_("assignment makes %q#v qualified function "
7484 "pointer from unqualified"),
7485 G_("initialization makes %q#v qualified "
7486 "function pointer from unqualified"),
7487 G_("return makes %q#v qualified function "
7488 "pointer from unqualified"),
7489 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7492 /* Avoid warning about the volatile ObjC EH puts on decls. */
7493 else if (!objc_ok)
7495 switch (errtype)
7497 case ic_argpass:
7499 auto_diagnostic_group d;
7500 range_label_for_type_mismatch rhs_label (rhstype, type);
7501 gcc_rich_location richloc (expr_loc, &rhs_label);
7502 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7503 "passing argument %d of %qE from incompatible "
7504 "pointer type", parmnum, rname))
7505 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7507 break;
7508 case ic_assign:
7509 if (bltin)
7510 pedwarn (location, OPT_Wincompatible_pointer_types,
7511 "assignment to %qT from pointer to "
7512 "%qD with incompatible type %qT",
7513 type, bltin, rhstype);
7514 else
7515 pedwarn (location, OPT_Wincompatible_pointer_types,
7516 "assignment to %qT from incompatible pointer type %qT",
7517 type, rhstype);
7518 break;
7519 case ic_init:
7520 if (bltin)
7521 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7522 "initialization of %qT from pointer to "
7523 "%qD with incompatible type %qT",
7524 type, bltin, rhstype);
7525 else
7526 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7527 "initialization of %qT from incompatible "
7528 "pointer type %qT",
7529 type, rhstype);
7530 break;
7531 case ic_return:
7532 if (bltin)
7533 pedwarn (location, OPT_Wincompatible_pointer_types,
7534 "returning pointer to %qD of type %qT from "
7535 "a function with incompatible type %qT",
7536 bltin, rhstype, type);
7537 else
7538 pedwarn (location, OPT_Wincompatible_pointer_types,
7539 "returning %qT from a function with incompatible "
7540 "return type %qT", rhstype, type);
7541 break;
7542 default:
7543 gcc_unreachable ();
7547 /* If RHS isn't an address, check pointer or array of packed
7548 struct or union. */
7549 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7551 return convert (type, rhs);
7553 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7555 /* ??? This should not be an error when inlining calls to
7556 unprototyped functions. */
7557 const char msg[] = "invalid use of non-lvalue array";
7558 if (warnopt)
7559 warning_at (location, warnopt, msg);
7560 else
7561 error_at (location, msg);
7562 return error_mark_node;
7564 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7566 /* An explicit constant 0 can convert to a pointer,
7567 or one that results from arithmetic, even including
7568 a cast to integer type. */
7569 if (!null_pointer_constant)
7570 switch (errtype)
7572 case ic_argpass:
7574 auto_diagnostic_group d;
7575 range_label_for_type_mismatch rhs_label (rhstype, type);
7576 gcc_rich_location richloc (expr_loc, &rhs_label);
7577 if (pedwarn (&richloc, OPT_Wint_conversion,
7578 "passing argument %d of %qE makes pointer from "
7579 "integer without a cast", parmnum, rname))
7580 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7582 break;
7583 case ic_assign:
7584 pedwarn (location, OPT_Wint_conversion,
7585 "assignment to %qT from %qT makes pointer from integer "
7586 "without a cast", type, rhstype);
7587 break;
7588 case ic_init:
7589 pedwarn_init (location, OPT_Wint_conversion,
7590 "initialization of %qT from %qT makes pointer from "
7591 "integer without a cast", type, rhstype);
7592 break;
7593 case ic_return:
7594 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7595 "function with return type %qT makes pointer from "
7596 "integer without a cast", rhstype, type);
7597 break;
7598 default:
7599 gcc_unreachable ();
7602 return convert (type, rhs);
7604 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7606 switch (errtype)
7608 case ic_argpass:
7610 auto_diagnostic_group d;
7611 range_label_for_type_mismatch rhs_label (rhstype, type);
7612 gcc_rich_location richloc (expr_loc, &rhs_label);
7613 if (pedwarn (&richloc, OPT_Wint_conversion,
7614 "passing argument %d of %qE makes integer from "
7615 "pointer without a cast", parmnum, rname))
7616 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7618 break;
7619 case ic_assign:
7620 pedwarn (location, OPT_Wint_conversion,
7621 "assignment to %qT from %qT makes integer from pointer "
7622 "without a cast", type, rhstype);
7623 break;
7624 case ic_init:
7625 pedwarn_init (location, OPT_Wint_conversion,
7626 "initialization of %qT from %qT makes integer from "
7627 "pointer without a cast", type, rhstype);
7628 break;
7629 case ic_return:
7630 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7631 "function with return type %qT makes integer from "
7632 "pointer without a cast", rhstype, type);
7633 break;
7634 default:
7635 gcc_unreachable ();
7638 return convert (type, rhs);
7640 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7642 tree ret;
7643 bool save = in_late_binary_op;
7644 in_late_binary_op = true;
7645 ret = convert (type, rhs);
7646 in_late_binary_op = save;
7647 return ret;
7650 switch (errtype)
7652 case ic_argpass:
7654 auto_diagnostic_group d;
7655 range_label_for_type_mismatch rhs_label (rhstype, type);
7656 gcc_rich_location richloc (expr_loc, &rhs_label);
7657 const char msg[] = G_("incompatible type for argument %d of %qE");
7658 if (warnopt)
7659 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7660 else
7661 error_at (&richloc, msg, parmnum, rname);
7662 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7664 break;
7665 case ic_assign:
7667 const char msg[]
7668 = G_("incompatible types when assigning to type %qT from type %qT");
7669 if (warnopt)
7670 warning_at (expr_loc, 0, msg, type, rhstype);
7671 else
7672 error_at (expr_loc, msg, type, rhstype);
7673 break;
7675 case ic_init:
7677 const char msg[]
7678 = G_("incompatible types when initializing type %qT using type %qT");
7679 if (warnopt)
7680 warning_at (location, 0, msg, type, rhstype);
7681 else
7682 error_at (location, msg, type, rhstype);
7683 break;
7685 case ic_return:
7687 const char msg[]
7688 = G_("incompatible types when returning type %qT but %qT was expected");
7689 if (warnopt)
7690 warning_at (location, 0, msg, rhstype, type);
7691 else
7692 error_at (location, msg, rhstype, type);
7693 break;
7695 default:
7696 gcc_unreachable ();
7699 return error_mark_node;
7702 /* If VALUE is a compound expr all of whose expressions are constant, then
7703 return its value. Otherwise, return error_mark_node.
7705 This is for handling COMPOUND_EXPRs as initializer elements
7706 which is allowed with a warning when -pedantic is specified. */
7708 static tree
7709 valid_compound_expr_initializer (tree value, tree endtype)
7711 if (TREE_CODE (value) == COMPOUND_EXPR)
7713 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7714 == error_mark_node)
7715 return error_mark_node;
7716 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7717 endtype);
7719 else if (!initializer_constant_valid_p (value, endtype))
7720 return error_mark_node;
7721 else
7722 return value;
7725 /* Perform appropriate conversions on the initial value of a variable,
7726 store it in the declaration DECL,
7727 and print any error messages that are appropriate.
7728 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7729 If the init is invalid, store an ERROR_MARK.
7731 INIT_LOC is the location of the initial value. */
7733 void
7734 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7736 tree value, type;
7737 bool npc = false;
7739 /* If variable's type was invalidly declared, just ignore it. */
7741 type = TREE_TYPE (decl);
7742 if (TREE_CODE (type) == ERROR_MARK)
7743 return;
7745 /* Digest the specified initializer into an expression. */
7747 if (init)
7748 npc = null_pointer_constant_p (init);
7749 value = digest_init (init_loc, type, init, origtype, npc,
7750 true, TREE_STATIC (decl));
7752 /* Store the expression if valid; else report error. */
7754 if (!in_system_header_at (input_location)
7755 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7756 warning (OPT_Wtraditional, "traditional C rejects automatic "
7757 "aggregate initialization");
7759 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7760 DECL_INITIAL (decl) = value;
7762 /* ANSI wants warnings about out-of-range constant initializers. */
7763 STRIP_TYPE_NOPS (value);
7764 if (TREE_STATIC (decl))
7765 constant_expression_warning (value);
7767 /* Check if we need to set array size from compound literal size. */
7768 if (TREE_CODE (type) == ARRAY_TYPE
7769 && TYPE_DOMAIN (type) == NULL_TREE
7770 && value != error_mark_node)
7772 tree inside_init = init;
7774 STRIP_TYPE_NOPS (inside_init);
7775 inside_init = fold (inside_init);
7777 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7779 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7781 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7783 /* For int foo[] = (int [3]){1}; we need to set array size
7784 now since later on array initializer will be just the
7785 brace enclosed list of the compound literal. */
7786 tree etype = strip_array_types (TREE_TYPE (decl));
7787 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7788 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7789 layout_type (type);
7790 layout_decl (cldecl, 0);
7791 TREE_TYPE (decl)
7792 = c_build_qualified_type (type, TYPE_QUALS (etype));
7798 /* Methods for storing and printing names for error messages. */
7800 /* Implement a spelling stack that allows components of a name to be pushed
7801 and popped. Each element on the stack is this structure. */
7803 struct spelling
7805 int kind;
7806 union
7808 unsigned HOST_WIDE_INT i;
7809 const char *s;
7810 } u;
7813 #define SPELLING_STRING 1
7814 #define SPELLING_MEMBER 2
7815 #define SPELLING_BOUNDS 3
7817 static struct spelling *spelling; /* Next stack element (unused). */
7818 static struct spelling *spelling_base; /* Spelling stack base. */
7819 static int spelling_size; /* Size of the spelling stack. */
7821 /* Macros to save and restore the spelling stack around push_... functions.
7822 Alternative to SAVE_SPELLING_STACK. */
7824 #define SPELLING_DEPTH() (spelling - spelling_base)
7825 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7827 /* Push an element on the spelling stack with type KIND and assign VALUE
7828 to MEMBER. */
7830 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7832 int depth = SPELLING_DEPTH (); \
7834 if (depth >= spelling_size) \
7836 spelling_size += 10; \
7837 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7838 spelling_size); \
7839 RESTORE_SPELLING_DEPTH (depth); \
7842 spelling->kind = (KIND); \
7843 spelling->MEMBER = (VALUE); \
7844 spelling++; \
7847 /* Push STRING on the stack. Printed literally. */
7849 static void
7850 push_string (const char *string)
7852 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7855 /* Push a member name on the stack. Printed as '.' STRING. */
7857 static void
7858 push_member_name (tree decl)
7860 const char *const string
7861 = (DECL_NAME (decl)
7862 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7863 : _("<anonymous>"));
7864 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7867 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7869 static void
7870 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7872 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7875 /* Compute the maximum size in bytes of the printed spelling. */
7877 static int
7878 spelling_length (void)
7880 int size = 0;
7881 struct spelling *p;
7883 for (p = spelling_base; p < spelling; p++)
7885 if (p->kind == SPELLING_BOUNDS)
7886 size += 25;
7887 else
7888 size += strlen (p->u.s) + 1;
7891 return size;
7894 /* Print the spelling to BUFFER and return it. */
7896 static char *
7897 print_spelling (char *buffer)
7899 char *d = buffer;
7900 struct spelling *p;
7902 for (p = spelling_base; p < spelling; p++)
7903 if (p->kind == SPELLING_BOUNDS)
7905 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7906 d += strlen (d);
7908 else
7910 const char *s;
7911 if (p->kind == SPELLING_MEMBER)
7912 *d++ = '.';
7913 for (s = p->u.s; (*d = *s++); d++)
7916 *d++ = '\0';
7917 return buffer;
7920 /* Digest the parser output INIT as an initializer for type TYPE.
7921 Return a C expression of type TYPE to represent the initial value.
7923 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7925 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7927 If INIT is a string constant, STRICT_STRING is true if it is
7928 unparenthesized or we should not warn here for it being parenthesized.
7929 For other types of INIT, STRICT_STRING is not used.
7931 INIT_LOC is the location of the INIT.
7933 REQUIRE_CONSTANT requests an error if non-constant initializers or
7934 elements are seen. */
7936 static tree
7937 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7938 bool null_pointer_constant, bool strict_string,
7939 int require_constant)
7941 enum tree_code code = TREE_CODE (type);
7942 tree inside_init = init;
7943 tree semantic_type = NULL_TREE;
7944 bool maybe_const = true;
7946 if (type == error_mark_node
7947 || !init
7948 || error_operand_p (init))
7949 return error_mark_node;
7951 STRIP_TYPE_NOPS (inside_init);
7953 if (!c_in_omp_for)
7955 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7957 semantic_type = TREE_TYPE (inside_init);
7958 inside_init = TREE_OPERAND (inside_init, 0);
7960 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7963 /* Initialization of an array of chars from a string constant
7964 optionally enclosed in braces. */
7966 if (code == ARRAY_TYPE && inside_init
7967 && TREE_CODE (inside_init) == STRING_CST)
7969 tree typ1
7970 = (TYPE_ATOMIC (TREE_TYPE (type))
7971 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7972 TYPE_QUAL_ATOMIC)
7973 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7974 /* Note that an array could be both an array of character type
7975 and an array of wchar_t if wchar_t is signed char or unsigned
7976 char. */
7977 bool char_array = (typ1 == char_type_node
7978 || typ1 == signed_char_type_node
7979 || typ1 == unsigned_char_type_node);
7980 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7981 bool char16_array = !!comptypes (typ1, char16_type_node);
7982 bool char32_array = !!comptypes (typ1, char32_type_node);
7984 if (char_array || wchar_array || char16_array || char32_array)
7986 struct c_expr expr;
7987 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7988 bool incompat_string_cst = false;
7989 expr.value = inside_init;
7990 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7991 expr.original_type = NULL;
7992 maybe_warn_string_init (init_loc, type, expr);
7994 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7995 pedwarn_init (init_loc, OPT_Wpedantic,
7996 "initialization of a flexible array member");
7998 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7999 TYPE_MAIN_VARIANT (type)))
8000 return inside_init;
8002 if (char_array)
8004 if (typ2 != char_type_node)
8005 incompat_string_cst = true;
8007 else if (!comptypes (typ1, typ2))
8008 incompat_string_cst = true;
8010 if (incompat_string_cst)
8012 error_init (init_loc, "cannot initialize array of %qT from "
8013 "a string literal with type array of %qT",
8014 typ1, typ2);
8015 return error_mark_node;
8018 if (TYPE_DOMAIN (type) != NULL_TREE
8019 && TYPE_SIZE (type) != NULL_TREE
8020 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8022 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8023 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8025 /* Subtract the size of a single (possibly wide) character
8026 because it's ok to ignore the terminating null char
8027 that is counted in the length of the constant. */
8028 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8029 pedwarn_init (init_loc, 0,
8030 ("initializer-string for array of %qT "
8031 "is too long"), typ1);
8032 else if (warn_cxx_compat
8033 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8034 warning_at (init_loc, OPT_Wc___compat,
8035 ("initializer-string for array of %qT "
8036 "is too long for C++"), typ1);
8037 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8039 unsigned HOST_WIDE_INT size
8040 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8041 const char *p = TREE_STRING_POINTER (inside_init);
8043 inside_init = build_string (size, p);
8047 TREE_TYPE (inside_init) = type;
8048 return inside_init;
8050 else if (INTEGRAL_TYPE_P (typ1))
8052 error_init (init_loc, "array of inappropriate type initialized "
8053 "from string constant");
8054 return error_mark_node;
8058 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8059 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8060 below and handle as a constructor. */
8061 if (code == VECTOR_TYPE
8062 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8063 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8064 && TREE_CONSTANT (inside_init))
8066 if (TREE_CODE (inside_init) == VECTOR_CST
8067 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8068 TYPE_MAIN_VARIANT (type)))
8069 return inside_init;
8071 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8073 unsigned HOST_WIDE_INT ix;
8074 tree value;
8075 bool constant_p = true;
8077 /* Iterate through elements and check if all constructor
8078 elements are *_CSTs. */
8079 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8080 if (!CONSTANT_CLASS_P (value))
8082 constant_p = false;
8083 break;
8086 if (constant_p)
8087 return build_vector_from_ctor (type,
8088 CONSTRUCTOR_ELTS (inside_init));
8092 if (warn_sequence_point)
8093 verify_sequence_points (inside_init);
8095 /* Any type can be initialized
8096 from an expression of the same type, optionally with braces. */
8098 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8099 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8100 TYPE_MAIN_VARIANT (type))
8101 || (code == ARRAY_TYPE
8102 && comptypes (TREE_TYPE (inside_init), type))
8103 || (gnu_vector_type_p (type)
8104 && comptypes (TREE_TYPE (inside_init), type))
8105 || (code == POINTER_TYPE
8106 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8107 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8108 TREE_TYPE (type)))))
8110 if (code == POINTER_TYPE)
8112 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8114 if (TREE_CODE (inside_init) == STRING_CST
8115 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8116 inside_init = array_to_pointer_conversion
8117 (init_loc, inside_init);
8118 else
8120 error_init (init_loc, "invalid use of non-lvalue array");
8121 return error_mark_node;
8126 if (code == VECTOR_TYPE)
8127 /* Although the types are compatible, we may require a
8128 conversion. */
8129 inside_init = convert (type, inside_init);
8131 if (require_constant
8132 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8134 /* As an extension, allow initializing objects with static storage
8135 duration with compound literals (which are then treated just as
8136 the brace enclosed list they contain). Also allow this for
8137 vectors, as we can only assign them with compound literals. */
8138 if (flag_isoc99 && code != VECTOR_TYPE)
8139 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8140 "is not constant");
8141 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8142 inside_init = DECL_INITIAL (decl);
8145 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8146 && TREE_CODE (inside_init) != CONSTRUCTOR)
8148 error_init (init_loc, "array initialized from non-constant array "
8149 "expression");
8150 return error_mark_node;
8153 /* Compound expressions can only occur here if -Wpedantic or
8154 -pedantic-errors is specified. In the later case, we always want
8155 an error. In the former case, we simply want a warning. */
8156 if (require_constant && pedantic
8157 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8159 inside_init
8160 = valid_compound_expr_initializer (inside_init,
8161 TREE_TYPE (inside_init));
8162 if (inside_init == error_mark_node)
8163 error_init (init_loc, "initializer element is not constant");
8164 else
8165 pedwarn_init (init_loc, OPT_Wpedantic,
8166 "initializer element is not constant");
8167 if (flag_pedantic_errors)
8168 inside_init = error_mark_node;
8170 else if (require_constant
8171 && !initializer_constant_valid_p (inside_init,
8172 TREE_TYPE (inside_init)))
8174 error_init (init_loc, "initializer element is not constant");
8175 inside_init = error_mark_node;
8177 else if (require_constant && !maybe_const)
8178 pedwarn_init (init_loc, OPT_Wpedantic,
8179 "initializer element is not a constant expression");
8181 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8182 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8183 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8184 type, inside_init, origtype,
8185 ic_init, null_pointer_constant,
8186 NULL_TREE, NULL_TREE, 0);
8187 return inside_init;
8190 /* Handle scalar types, including conversions. */
8192 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8193 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8194 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8196 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8197 && (TREE_CODE (init) == STRING_CST
8198 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8199 inside_init = init = array_to_pointer_conversion (init_loc, init);
8200 if (semantic_type)
8201 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8202 inside_init);
8203 inside_init
8204 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8205 inside_init, origtype, ic_init,
8206 null_pointer_constant, NULL_TREE, NULL_TREE,
8209 /* Check to see if we have already given an error message. */
8210 if (inside_init == error_mark_node)
8212 else if (require_constant && !TREE_CONSTANT (inside_init))
8214 error_init (init_loc, "initializer element is not constant");
8215 inside_init = error_mark_node;
8217 else if (require_constant
8218 && !initializer_constant_valid_p (inside_init,
8219 TREE_TYPE (inside_init)))
8221 error_init (init_loc, "initializer element is not computable at "
8222 "load time");
8223 inside_init = error_mark_node;
8225 else if (require_constant && !maybe_const)
8226 pedwarn_init (init_loc, OPT_Wpedantic,
8227 "initializer element is not a constant expression");
8229 return inside_init;
8232 /* Come here only for records and arrays. */
8234 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8236 error_init (init_loc, "variable-sized object may not be initialized");
8237 return error_mark_node;
8240 error_init (init_loc, "invalid initializer");
8241 return error_mark_node;
8244 /* Handle initializers that use braces. */
8246 /* Type of object we are accumulating a constructor for.
8247 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8248 static tree constructor_type;
8250 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8251 left to fill. */
8252 static tree constructor_fields;
8254 /* For an ARRAY_TYPE, this is the specified index
8255 at which to store the next element we get. */
8256 static tree constructor_index;
8258 /* For an ARRAY_TYPE, this is the maximum index. */
8259 static tree constructor_max_index;
8261 /* For a RECORD_TYPE, this is the first field not yet written out. */
8262 static tree constructor_unfilled_fields;
8264 /* For an ARRAY_TYPE, this is the index of the first element
8265 not yet written out. */
8266 static tree constructor_unfilled_index;
8268 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8269 This is so we can generate gaps between fields, when appropriate. */
8270 static tree constructor_bit_index;
8272 /* If we are saving up the elements rather than allocating them,
8273 this is the list of elements so far (in reverse order,
8274 most recent first). */
8275 static vec<constructor_elt, va_gc> *constructor_elements;
8277 /* 1 if constructor should be incrementally stored into a constructor chain,
8278 0 if all the elements should be kept in AVL tree. */
8279 static int constructor_incremental;
8281 /* 1 if so far this constructor's elements are all compile-time constants. */
8282 static int constructor_constant;
8284 /* 1 if so far this constructor's elements are all valid address constants. */
8285 static int constructor_simple;
8287 /* 1 if this constructor has an element that cannot be part of a
8288 constant expression. */
8289 static int constructor_nonconst;
8291 /* 1 if this constructor is erroneous so far. */
8292 static int constructor_erroneous;
8294 /* 1 if this constructor is the universal zero initializer { 0 }. */
8295 static int constructor_zeroinit;
8297 /* Structure for managing pending initializer elements, organized as an
8298 AVL tree. */
8300 struct init_node
8302 struct init_node *left, *right;
8303 struct init_node *parent;
8304 int balance;
8305 tree purpose;
8306 tree value;
8307 tree origtype;
8310 /* Tree of pending elements at this constructor level.
8311 These are elements encountered out of order
8312 which belong at places we haven't reached yet in actually
8313 writing the output.
8314 Will never hold tree nodes across GC runs. */
8315 static struct init_node *constructor_pending_elts;
8317 /* The SPELLING_DEPTH of this constructor. */
8318 static int constructor_depth;
8320 /* DECL node for which an initializer is being read.
8321 0 means we are reading a constructor expression
8322 such as (struct foo) {...}. */
8323 static tree constructor_decl;
8325 /* Nonzero if this is an initializer for a top-level decl. */
8326 static int constructor_top_level;
8328 /* Nonzero if there were any member designators in this initializer. */
8329 static int constructor_designated;
8331 /* Nesting depth of designator list. */
8332 static int designator_depth;
8334 /* Nonzero if there were diagnosed errors in this designator list. */
8335 static int designator_erroneous;
8338 /* This stack has a level for each implicit or explicit level of
8339 structuring in the initializer, including the outermost one. It
8340 saves the values of most of the variables above. */
8342 struct constructor_range_stack;
8344 struct constructor_stack
8346 struct constructor_stack *next;
8347 tree type;
8348 tree fields;
8349 tree index;
8350 tree max_index;
8351 tree unfilled_index;
8352 tree unfilled_fields;
8353 tree bit_index;
8354 vec<constructor_elt, va_gc> *elements;
8355 struct init_node *pending_elts;
8356 int offset;
8357 int depth;
8358 /* If value nonzero, this value should replace the entire
8359 constructor at this level. */
8360 struct c_expr replacement_value;
8361 struct constructor_range_stack *range_stack;
8362 char constant;
8363 char simple;
8364 char nonconst;
8365 char implicit;
8366 char erroneous;
8367 char outer;
8368 char incremental;
8369 char designated;
8370 int designator_depth;
8373 static struct constructor_stack *constructor_stack;
8375 /* This stack represents designators from some range designator up to
8376 the last designator in the list. */
8378 struct constructor_range_stack
8380 struct constructor_range_stack *next, *prev;
8381 struct constructor_stack *stack;
8382 tree range_start;
8383 tree index;
8384 tree range_end;
8385 tree fields;
8388 static struct constructor_range_stack *constructor_range_stack;
8390 /* This stack records separate initializers that are nested.
8391 Nested initializers can't happen in ANSI C, but GNU C allows them
8392 in cases like { ... (struct foo) { ... } ... }. */
8394 struct initializer_stack
8396 struct initializer_stack *next;
8397 tree decl;
8398 struct constructor_stack *constructor_stack;
8399 struct constructor_range_stack *constructor_range_stack;
8400 vec<constructor_elt, va_gc> *elements;
8401 struct spelling *spelling;
8402 struct spelling *spelling_base;
8403 int spelling_size;
8404 char top_level;
8405 char require_constant_value;
8406 char require_constant_elements;
8407 rich_location *missing_brace_richloc;
8410 static struct initializer_stack *initializer_stack;
8412 /* Prepare to parse and output the initializer for variable DECL. */
8414 void
8415 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8416 rich_location *richloc)
8418 const char *locus;
8419 struct initializer_stack *p = XNEW (struct initializer_stack);
8421 p->decl = constructor_decl;
8422 p->require_constant_value = require_constant_value;
8423 p->require_constant_elements = require_constant_elements;
8424 p->constructor_stack = constructor_stack;
8425 p->constructor_range_stack = constructor_range_stack;
8426 p->elements = constructor_elements;
8427 p->spelling = spelling;
8428 p->spelling_base = spelling_base;
8429 p->spelling_size = spelling_size;
8430 p->top_level = constructor_top_level;
8431 p->next = initializer_stack;
8432 p->missing_brace_richloc = richloc;
8433 initializer_stack = p;
8435 constructor_decl = decl;
8436 constructor_designated = 0;
8437 constructor_top_level = top_level;
8439 if (decl != NULL_TREE && decl != error_mark_node)
8441 require_constant_value = TREE_STATIC (decl);
8442 require_constant_elements
8443 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8444 /* For a scalar, you can always use any value to initialize,
8445 even within braces. */
8446 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8447 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8449 else
8451 require_constant_value = 0;
8452 require_constant_elements = 0;
8453 locus = _("(anonymous)");
8456 constructor_stack = 0;
8457 constructor_range_stack = 0;
8459 found_missing_braces = 0;
8461 spelling_base = 0;
8462 spelling_size = 0;
8463 RESTORE_SPELLING_DEPTH (0);
8465 if (locus)
8466 push_string (locus);
8469 void
8470 finish_init (void)
8472 struct initializer_stack *p = initializer_stack;
8474 /* Free the whole constructor stack of this initializer. */
8475 while (constructor_stack)
8477 struct constructor_stack *q = constructor_stack;
8478 constructor_stack = q->next;
8479 XDELETE (q);
8482 gcc_assert (!constructor_range_stack);
8484 /* Pop back to the data of the outer initializer (if any). */
8485 XDELETE (spelling_base);
8487 constructor_decl = p->decl;
8488 require_constant_value = p->require_constant_value;
8489 require_constant_elements = p->require_constant_elements;
8490 constructor_stack = p->constructor_stack;
8491 constructor_range_stack = p->constructor_range_stack;
8492 constructor_elements = p->elements;
8493 spelling = p->spelling;
8494 spelling_base = p->spelling_base;
8495 spelling_size = p->spelling_size;
8496 constructor_top_level = p->top_level;
8497 initializer_stack = p->next;
8498 XDELETE (p);
8501 /* Call here when we see the initializer is surrounded by braces.
8502 This is instead of a call to push_init_level;
8503 it is matched by a call to pop_init_level.
8505 TYPE is the type to initialize, for a constructor expression.
8506 For an initializer for a decl, TYPE is zero. */
8508 void
8509 really_start_incremental_init (tree type)
8511 struct constructor_stack *p = XNEW (struct constructor_stack);
8513 if (type == NULL_TREE)
8514 type = TREE_TYPE (constructor_decl);
8516 if (VECTOR_TYPE_P (type)
8517 && TYPE_VECTOR_OPAQUE (type))
8518 error ("opaque vector types cannot be initialized");
8520 p->type = constructor_type;
8521 p->fields = constructor_fields;
8522 p->index = constructor_index;
8523 p->max_index = constructor_max_index;
8524 p->unfilled_index = constructor_unfilled_index;
8525 p->unfilled_fields = constructor_unfilled_fields;
8526 p->bit_index = constructor_bit_index;
8527 p->elements = constructor_elements;
8528 p->constant = constructor_constant;
8529 p->simple = constructor_simple;
8530 p->nonconst = constructor_nonconst;
8531 p->erroneous = constructor_erroneous;
8532 p->pending_elts = constructor_pending_elts;
8533 p->depth = constructor_depth;
8534 p->replacement_value.value = 0;
8535 p->replacement_value.original_code = ERROR_MARK;
8536 p->replacement_value.original_type = NULL;
8537 p->implicit = 0;
8538 p->range_stack = 0;
8539 p->outer = 0;
8540 p->incremental = constructor_incremental;
8541 p->designated = constructor_designated;
8542 p->designator_depth = designator_depth;
8543 p->next = 0;
8544 constructor_stack = p;
8546 constructor_constant = 1;
8547 constructor_simple = 1;
8548 constructor_nonconst = 0;
8549 constructor_depth = SPELLING_DEPTH ();
8550 constructor_elements = NULL;
8551 constructor_pending_elts = 0;
8552 constructor_type = type;
8553 constructor_incremental = 1;
8554 constructor_designated = 0;
8555 constructor_zeroinit = 1;
8556 designator_depth = 0;
8557 designator_erroneous = 0;
8559 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8561 constructor_fields = TYPE_FIELDS (constructor_type);
8562 /* Skip any nameless bit fields at the beginning. */
8563 while (constructor_fields != NULL_TREE
8564 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8565 constructor_fields = DECL_CHAIN (constructor_fields);
8567 constructor_unfilled_fields = constructor_fields;
8568 constructor_bit_index = bitsize_zero_node;
8570 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8572 if (TYPE_DOMAIN (constructor_type))
8574 constructor_max_index
8575 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8577 /* Detect non-empty initializations of zero-length arrays. */
8578 if (constructor_max_index == NULL_TREE
8579 && TYPE_SIZE (constructor_type))
8580 constructor_max_index = integer_minus_one_node;
8582 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8583 to initialize VLAs will cause a proper error; avoid tree
8584 checking errors as well by setting a safe value. */
8585 if (constructor_max_index
8586 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8587 constructor_max_index = integer_minus_one_node;
8589 constructor_index
8590 = convert (bitsizetype,
8591 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8593 else
8595 constructor_index = bitsize_zero_node;
8596 constructor_max_index = NULL_TREE;
8599 constructor_unfilled_index = constructor_index;
8601 else if (gnu_vector_type_p (constructor_type))
8603 /* Vectors are like simple fixed-size arrays. */
8604 constructor_max_index =
8605 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8606 constructor_index = bitsize_zero_node;
8607 constructor_unfilled_index = constructor_index;
8609 else
8611 /* Handle the case of int x = {5}; */
8612 constructor_fields = constructor_type;
8613 constructor_unfilled_fields = constructor_type;
8617 extern location_t last_init_list_comma;
8619 /* Called when we see an open brace for a nested initializer. Finish
8620 off any pending levels with implicit braces. */
8621 void
8622 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8624 while (constructor_stack->implicit)
8626 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8627 && constructor_fields == NULL_TREE)
8628 process_init_element (input_location,
8629 pop_init_level (loc, 1, braced_init_obstack,
8630 last_init_list_comma),
8631 true, braced_init_obstack);
8632 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8633 && constructor_max_index
8634 && tree_int_cst_lt (constructor_max_index,
8635 constructor_index))
8636 process_init_element (input_location,
8637 pop_init_level (loc, 1, braced_init_obstack,
8638 last_init_list_comma),
8639 true, braced_init_obstack);
8640 else
8641 break;
8645 /* Push down into a subobject, for initialization.
8646 If this is for an explicit set of braces, IMPLICIT is 0.
8647 If it is because the next element belongs at a lower level,
8648 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8650 void
8651 push_init_level (location_t loc, int implicit,
8652 struct obstack *braced_init_obstack)
8654 struct constructor_stack *p;
8655 tree value = NULL_TREE;
8657 /* Unless this is an explicit brace, we need to preserve previous
8658 content if any. */
8659 if (implicit)
8661 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8662 value = find_init_member (constructor_fields, braced_init_obstack);
8663 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8664 value = find_init_member (constructor_index, braced_init_obstack);
8667 p = XNEW (struct constructor_stack);
8668 p->type = constructor_type;
8669 p->fields = constructor_fields;
8670 p->index = constructor_index;
8671 p->max_index = constructor_max_index;
8672 p->unfilled_index = constructor_unfilled_index;
8673 p->unfilled_fields = constructor_unfilled_fields;
8674 p->bit_index = constructor_bit_index;
8675 p->elements = constructor_elements;
8676 p->constant = constructor_constant;
8677 p->simple = constructor_simple;
8678 p->nonconst = constructor_nonconst;
8679 p->erroneous = constructor_erroneous;
8680 p->pending_elts = constructor_pending_elts;
8681 p->depth = constructor_depth;
8682 p->replacement_value.value = NULL_TREE;
8683 p->replacement_value.original_code = ERROR_MARK;
8684 p->replacement_value.original_type = NULL;
8685 p->implicit = implicit;
8686 p->outer = 0;
8687 p->incremental = constructor_incremental;
8688 p->designated = constructor_designated;
8689 p->designator_depth = designator_depth;
8690 p->next = constructor_stack;
8691 p->range_stack = 0;
8692 constructor_stack = p;
8694 constructor_constant = 1;
8695 constructor_simple = 1;
8696 constructor_nonconst = 0;
8697 constructor_depth = SPELLING_DEPTH ();
8698 constructor_elements = NULL;
8699 constructor_incremental = 1;
8700 constructor_designated = 0;
8701 constructor_pending_elts = 0;
8702 if (!implicit)
8704 p->range_stack = constructor_range_stack;
8705 constructor_range_stack = 0;
8706 designator_depth = 0;
8707 designator_erroneous = 0;
8710 /* Don't die if an entire brace-pair level is superfluous
8711 in the containing level. */
8712 if (constructor_type == NULL_TREE)
8714 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8716 /* Don't die if there are extra init elts at the end. */
8717 if (constructor_fields == NULL_TREE)
8718 constructor_type = NULL_TREE;
8719 else
8721 constructor_type = TREE_TYPE (constructor_fields);
8722 push_member_name (constructor_fields);
8723 constructor_depth++;
8725 /* If upper initializer is designated, then mark this as
8726 designated too to prevent bogus warnings. */
8727 constructor_designated = p->designated;
8729 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8731 constructor_type = TREE_TYPE (constructor_type);
8732 push_array_bounds (tree_to_uhwi (constructor_index));
8733 constructor_depth++;
8736 if (constructor_type == NULL_TREE)
8738 error_init (loc, "extra brace group at end of initializer");
8739 constructor_fields = NULL_TREE;
8740 constructor_unfilled_fields = NULL_TREE;
8741 return;
8744 if (value && TREE_CODE (value) == CONSTRUCTOR)
8746 constructor_constant = TREE_CONSTANT (value);
8747 constructor_simple = TREE_STATIC (value);
8748 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8749 constructor_elements = CONSTRUCTOR_ELTS (value);
8750 if (!vec_safe_is_empty (constructor_elements)
8751 && (TREE_CODE (constructor_type) == RECORD_TYPE
8752 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8753 set_nonincremental_init (braced_init_obstack);
8756 if (implicit == 1)
8758 found_missing_braces = 1;
8759 if (initializer_stack->missing_brace_richloc)
8760 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8761 (loc, "{");
8764 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8766 constructor_fields = TYPE_FIELDS (constructor_type);
8767 /* Skip any nameless bit fields at the beginning. */
8768 while (constructor_fields != NULL_TREE
8769 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8770 constructor_fields = DECL_CHAIN (constructor_fields);
8772 constructor_unfilled_fields = constructor_fields;
8773 constructor_bit_index = bitsize_zero_node;
8775 else if (gnu_vector_type_p (constructor_type))
8777 /* Vectors are like simple fixed-size arrays. */
8778 constructor_max_index =
8779 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8780 constructor_index = bitsize_int (0);
8781 constructor_unfilled_index = constructor_index;
8783 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8785 if (TYPE_DOMAIN (constructor_type))
8787 constructor_max_index
8788 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8790 /* Detect non-empty initializations of zero-length arrays. */
8791 if (constructor_max_index == NULL_TREE
8792 && TYPE_SIZE (constructor_type))
8793 constructor_max_index = integer_minus_one_node;
8795 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8796 to initialize VLAs will cause a proper error; avoid tree
8797 checking errors as well by setting a safe value. */
8798 if (constructor_max_index
8799 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8800 constructor_max_index = integer_minus_one_node;
8802 constructor_index
8803 = convert (bitsizetype,
8804 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8806 else
8807 constructor_index = bitsize_zero_node;
8809 constructor_unfilled_index = constructor_index;
8810 if (value && TREE_CODE (value) == STRING_CST)
8812 /* We need to split the char/wchar array into individual
8813 characters, so that we don't have to special case it
8814 everywhere. */
8815 set_nonincremental_init_from_string (value, braced_init_obstack);
8818 else
8820 if (constructor_type != error_mark_node)
8821 warning_init (input_location, 0, "braces around scalar initializer");
8822 constructor_fields = constructor_type;
8823 constructor_unfilled_fields = constructor_type;
8827 /* At the end of an implicit or explicit brace level,
8828 finish up that level of constructor. If a single expression
8829 with redundant braces initialized that level, return the
8830 c_expr structure for that expression. Otherwise, the original_code
8831 element is set to ERROR_MARK.
8832 If we were outputting the elements as they are read, return 0 as the value
8833 from inner levels (process_init_element ignores that),
8834 but return error_mark_node as the value from the outermost level
8835 (that's what we want to put in DECL_INITIAL).
8836 Otherwise, return a CONSTRUCTOR expression as the value. */
8838 struct c_expr
8839 pop_init_level (location_t loc, int implicit,
8840 struct obstack *braced_init_obstack,
8841 location_t insert_before)
8843 struct constructor_stack *p;
8844 struct c_expr ret;
8845 ret.value = NULL_TREE;
8846 ret.original_code = ERROR_MARK;
8847 ret.original_type = NULL;
8849 if (implicit == 0)
8851 /* When we come to an explicit close brace,
8852 pop any inner levels that didn't have explicit braces. */
8853 while (constructor_stack->implicit)
8854 process_init_element (input_location,
8855 pop_init_level (loc, 1, braced_init_obstack,
8856 insert_before),
8857 true, braced_init_obstack);
8858 gcc_assert (!constructor_range_stack);
8860 else
8861 if (initializer_stack->missing_brace_richloc)
8862 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8863 (insert_before, "}");
8865 /* Now output all pending elements. */
8866 constructor_incremental = 1;
8867 output_pending_init_elements (1, braced_init_obstack);
8869 p = constructor_stack;
8871 /* Error for initializing a flexible array member, or a zero-length
8872 array member in an inappropriate context. */
8873 if (constructor_type && constructor_fields
8874 && TREE_CODE (constructor_type) == ARRAY_TYPE
8875 && TYPE_DOMAIN (constructor_type)
8876 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8878 /* Silently discard empty initializations. The parser will
8879 already have pedwarned for empty brackets. */
8880 if (integer_zerop (constructor_unfilled_index))
8881 constructor_type = NULL_TREE;
8882 else
8884 gcc_assert (!TYPE_SIZE (constructor_type));
8886 if (constructor_depth > 2)
8887 error_init (loc, "initialization of flexible array member in a nested context");
8888 else
8889 pedwarn_init (loc, OPT_Wpedantic,
8890 "initialization of a flexible array member");
8892 /* We have already issued an error message for the existence
8893 of a flexible array member not at the end of the structure.
8894 Discard the initializer so that we do not die later. */
8895 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8896 constructor_type = NULL_TREE;
8900 switch (vec_safe_length (constructor_elements))
8902 case 0:
8903 /* Initialization with { } counts as zeroinit. */
8904 constructor_zeroinit = 1;
8905 break;
8906 case 1:
8907 /* This might be zeroinit as well. */
8908 if (integer_zerop ((*constructor_elements)[0].value))
8909 constructor_zeroinit = 1;
8910 break;
8911 default:
8912 /* If the constructor has more than one element, it can't be { 0 }. */
8913 constructor_zeroinit = 0;
8914 break;
8917 /* Warn when some structs are initialized with direct aggregation. */
8918 if (!implicit && found_missing_braces && warn_missing_braces
8919 && !constructor_zeroinit)
8921 gcc_assert (initializer_stack->missing_brace_richloc);
8922 warning_at (initializer_stack->missing_brace_richloc,
8923 OPT_Wmissing_braces,
8924 "missing braces around initializer");
8927 /* Warn when some struct elements are implicitly initialized to zero. */
8928 if (warn_missing_field_initializers
8929 && constructor_type
8930 && TREE_CODE (constructor_type) == RECORD_TYPE
8931 && constructor_unfilled_fields)
8933 /* Do not warn for flexible array members or zero-length arrays. */
8934 while (constructor_unfilled_fields
8935 && (!DECL_SIZE (constructor_unfilled_fields)
8936 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8937 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8939 if (constructor_unfilled_fields
8940 /* Do not warn if this level of the initializer uses member
8941 designators; it is likely to be deliberate. */
8942 && !constructor_designated
8943 /* Do not warn about initializing with { 0 } or with { }. */
8944 && !constructor_zeroinit)
8946 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8947 "missing initializer for field %qD of %qT",
8948 constructor_unfilled_fields,
8949 constructor_type))
8950 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8951 "%qD declared here", constructor_unfilled_fields);
8955 /* Pad out the end of the structure. */
8956 if (p->replacement_value.value)
8957 /* If this closes a superfluous brace pair,
8958 just pass out the element between them. */
8959 ret = p->replacement_value;
8960 else if (constructor_type == NULL_TREE)
8962 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8963 && TREE_CODE (constructor_type) != ARRAY_TYPE
8964 && !gnu_vector_type_p (constructor_type))
8966 /* A nonincremental scalar initializer--just return
8967 the element, after verifying there is just one. */
8968 if (vec_safe_is_empty (constructor_elements))
8970 if (!constructor_erroneous && constructor_type != error_mark_node)
8971 error_init (loc, "empty scalar initializer");
8972 ret.value = error_mark_node;
8974 else if (vec_safe_length (constructor_elements) != 1)
8976 error_init (loc, "extra elements in scalar initializer");
8977 ret.value = (*constructor_elements)[0].value;
8979 else
8980 ret.value = (*constructor_elements)[0].value;
8982 else
8984 if (constructor_erroneous)
8985 ret.value = error_mark_node;
8986 else
8988 ret.value = build_constructor (constructor_type,
8989 constructor_elements);
8990 if (constructor_constant)
8991 TREE_CONSTANT (ret.value) = 1;
8992 if (constructor_constant && constructor_simple)
8993 TREE_STATIC (ret.value) = 1;
8994 if (constructor_nonconst)
8995 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8999 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9001 if (constructor_nonconst)
9002 ret.original_code = C_MAYBE_CONST_EXPR;
9003 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9004 ret.original_code = ERROR_MARK;
9007 constructor_type = p->type;
9008 constructor_fields = p->fields;
9009 constructor_index = p->index;
9010 constructor_max_index = p->max_index;
9011 constructor_unfilled_index = p->unfilled_index;
9012 constructor_unfilled_fields = p->unfilled_fields;
9013 constructor_bit_index = p->bit_index;
9014 constructor_elements = p->elements;
9015 constructor_constant = p->constant;
9016 constructor_simple = p->simple;
9017 constructor_nonconst = p->nonconst;
9018 constructor_erroneous = p->erroneous;
9019 constructor_incremental = p->incremental;
9020 constructor_designated = p->designated;
9021 designator_depth = p->designator_depth;
9022 constructor_pending_elts = p->pending_elts;
9023 constructor_depth = p->depth;
9024 if (!p->implicit)
9025 constructor_range_stack = p->range_stack;
9026 RESTORE_SPELLING_DEPTH (constructor_depth);
9028 constructor_stack = p->next;
9029 XDELETE (p);
9031 if (ret.value == NULL_TREE && constructor_stack == 0)
9032 ret.value = error_mark_node;
9033 return ret;
9036 /* Common handling for both array range and field name designators.
9037 ARRAY argument is nonzero for array ranges. Returns false for success. */
9039 static bool
9040 set_designator (location_t loc, bool array,
9041 struct obstack *braced_init_obstack)
9043 tree subtype;
9044 enum tree_code subcode;
9046 /* Don't die if an entire brace-pair level is superfluous
9047 in the containing level, or for an erroneous type. */
9048 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9049 return true;
9051 /* If there were errors in this designator list already, bail out
9052 silently. */
9053 if (designator_erroneous)
9054 return true;
9056 /* Likewise for an initializer for a variable-size type. Those are
9057 diagnosed in digest_init. */
9058 if (COMPLETE_TYPE_P (constructor_type)
9059 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9060 return true;
9062 if (!designator_depth)
9064 gcc_assert (!constructor_range_stack);
9066 /* Designator list starts at the level of closest explicit
9067 braces. */
9068 while (constructor_stack->implicit)
9069 process_init_element (input_location,
9070 pop_init_level (loc, 1, braced_init_obstack,
9071 last_init_list_comma),
9072 true, braced_init_obstack);
9073 constructor_designated = 1;
9074 return false;
9077 switch (TREE_CODE (constructor_type))
9079 case RECORD_TYPE:
9080 case UNION_TYPE:
9081 subtype = TREE_TYPE (constructor_fields);
9082 if (subtype != error_mark_node)
9083 subtype = TYPE_MAIN_VARIANT (subtype);
9084 break;
9085 case ARRAY_TYPE:
9086 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9087 break;
9088 default:
9089 gcc_unreachable ();
9092 subcode = TREE_CODE (subtype);
9093 if (array && subcode != ARRAY_TYPE)
9095 error_init (loc, "array index in non-array initializer");
9096 return true;
9098 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9100 error_init (loc, "field name not in record or union initializer");
9101 return true;
9104 constructor_designated = 1;
9105 finish_implicit_inits (loc, braced_init_obstack);
9106 push_init_level (loc, 2, braced_init_obstack);
9107 return false;
9110 /* If there are range designators in designator list, push a new designator
9111 to constructor_range_stack. RANGE_END is end of such stack range or
9112 NULL_TREE if there is no range designator at this level. */
9114 static void
9115 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9117 struct constructor_range_stack *p;
9119 p = (struct constructor_range_stack *)
9120 obstack_alloc (braced_init_obstack,
9121 sizeof (struct constructor_range_stack));
9122 p->prev = constructor_range_stack;
9123 p->next = 0;
9124 p->fields = constructor_fields;
9125 p->range_start = constructor_index;
9126 p->index = constructor_index;
9127 p->stack = constructor_stack;
9128 p->range_end = range_end;
9129 if (constructor_range_stack)
9130 constructor_range_stack->next = p;
9131 constructor_range_stack = p;
9134 /* Within an array initializer, specify the next index to be initialized.
9135 FIRST is that index. If LAST is nonzero, then initialize a range
9136 of indices, running from FIRST through LAST. */
9138 void
9139 set_init_index (location_t loc, tree first, tree last,
9140 struct obstack *braced_init_obstack)
9142 if (set_designator (loc, true, braced_init_obstack))
9143 return;
9145 designator_erroneous = 1;
9147 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9148 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9150 error_init (loc, "array index in initializer not of integer type");
9151 return;
9154 if (TREE_CODE (first) != INTEGER_CST)
9156 first = c_fully_fold (first, false, NULL);
9157 if (TREE_CODE (first) == INTEGER_CST)
9158 pedwarn_init (loc, OPT_Wpedantic,
9159 "array index in initializer is not "
9160 "an integer constant expression");
9163 if (last && TREE_CODE (last) != INTEGER_CST)
9165 last = c_fully_fold (last, false, NULL);
9166 if (TREE_CODE (last) == INTEGER_CST)
9167 pedwarn_init (loc, OPT_Wpedantic,
9168 "array index in initializer is not "
9169 "an integer constant expression");
9172 if (TREE_CODE (first) != INTEGER_CST)
9173 error_init (loc, "nonconstant array index in initializer");
9174 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9175 error_init (loc, "nonconstant array index in initializer");
9176 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9177 error_init (loc, "array index in non-array initializer");
9178 else if (tree_int_cst_sgn (first) == -1)
9179 error_init (loc, "array index in initializer exceeds array bounds");
9180 else if (constructor_max_index
9181 && tree_int_cst_lt (constructor_max_index, first))
9182 error_init (loc, "array index in initializer exceeds array bounds");
9183 else
9185 constant_expression_warning (first);
9186 if (last)
9187 constant_expression_warning (last);
9188 constructor_index = convert (bitsizetype, first);
9189 if (tree_int_cst_lt (constructor_index, first))
9191 constructor_index = copy_node (constructor_index);
9192 TREE_OVERFLOW (constructor_index) = 1;
9195 if (last)
9197 if (tree_int_cst_equal (first, last))
9198 last = NULL_TREE;
9199 else if (tree_int_cst_lt (last, first))
9201 error_init (loc, "empty index range in initializer");
9202 last = NULL_TREE;
9204 else
9206 last = convert (bitsizetype, last);
9207 if (constructor_max_index != NULL_TREE
9208 && tree_int_cst_lt (constructor_max_index, last))
9210 error_init (loc, "array index range in initializer exceeds "
9211 "array bounds");
9212 last = NULL_TREE;
9217 designator_depth++;
9218 designator_erroneous = 0;
9219 if (constructor_range_stack || last)
9220 push_range_stack (last, braced_init_obstack);
9224 /* Within a struct initializer, specify the next field to be initialized. */
9226 void
9227 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9228 struct obstack *braced_init_obstack)
9230 tree field;
9232 if (set_designator (loc, false, braced_init_obstack))
9233 return;
9235 designator_erroneous = 1;
9237 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9239 error_init (loc, "field name not in record or union initializer");
9240 return;
9243 field = lookup_field (constructor_type, fieldname);
9245 if (field == NULL_TREE)
9247 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9248 if (guessed_id)
9250 gcc_rich_location rich_loc (fieldname_loc);
9251 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9252 error_at (&rich_loc,
9253 "%qT has no member named %qE; did you mean %qE?",
9254 constructor_type, fieldname, guessed_id);
9256 else
9257 error_at (fieldname_loc, "%qT has no member named %qE",
9258 constructor_type, fieldname);
9260 else
9263 constructor_fields = TREE_VALUE (field);
9264 designator_depth++;
9265 designator_erroneous = 0;
9266 if (constructor_range_stack)
9267 push_range_stack (NULL_TREE, braced_init_obstack);
9268 field = TREE_CHAIN (field);
9269 if (field)
9271 if (set_designator (loc, false, braced_init_obstack))
9272 return;
9275 while (field != NULL_TREE);
9278 /* Add a new initializer to the tree of pending initializers. PURPOSE
9279 identifies the initializer, either array index or field in a structure.
9280 VALUE is the value of that index or field. If ORIGTYPE is not
9281 NULL_TREE, it is the original type of VALUE.
9283 IMPLICIT is true if value comes from pop_init_level (1),
9284 the new initializer has been merged with the existing one
9285 and thus no warnings should be emitted about overriding an
9286 existing initializer. */
9288 static void
9289 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9290 bool implicit, struct obstack *braced_init_obstack)
9292 struct init_node *p, **q, *r;
9294 q = &constructor_pending_elts;
9295 p = 0;
9297 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9299 while (*q != 0)
9301 p = *q;
9302 if (tree_int_cst_lt (purpose, p->purpose))
9303 q = &p->left;
9304 else if (tree_int_cst_lt (p->purpose, purpose))
9305 q = &p->right;
9306 else
9308 if (!implicit)
9310 if (TREE_SIDE_EFFECTS (p->value))
9311 warning_init (loc, OPT_Woverride_init_side_effects,
9312 "initialized field with side-effects "
9313 "overwritten");
9314 else if (warn_override_init)
9315 warning_init (loc, OPT_Woverride_init,
9316 "initialized field overwritten");
9318 p->value = value;
9319 p->origtype = origtype;
9320 return;
9324 else
9326 tree bitpos;
9328 bitpos = bit_position (purpose);
9329 while (*q != NULL)
9331 p = *q;
9332 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9333 q = &p->left;
9334 else if (p->purpose != purpose)
9335 q = &p->right;
9336 else
9338 if (!implicit)
9340 if (TREE_SIDE_EFFECTS (p->value))
9341 warning_init (loc, OPT_Woverride_init_side_effects,
9342 "initialized field with side-effects "
9343 "overwritten");
9344 else if (warn_override_init)
9345 warning_init (loc, OPT_Woverride_init,
9346 "initialized field overwritten");
9348 p->value = value;
9349 p->origtype = origtype;
9350 return;
9355 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9356 sizeof (struct init_node));
9357 r->purpose = purpose;
9358 r->value = value;
9359 r->origtype = origtype;
9361 *q = r;
9362 r->parent = p;
9363 r->left = 0;
9364 r->right = 0;
9365 r->balance = 0;
9367 while (p)
9369 struct init_node *s;
9371 if (r == p->left)
9373 if (p->balance == 0)
9374 p->balance = -1;
9375 else if (p->balance < 0)
9377 if (r->balance < 0)
9379 /* L rotation. */
9380 p->left = r->right;
9381 if (p->left)
9382 p->left->parent = p;
9383 r->right = p;
9385 p->balance = 0;
9386 r->balance = 0;
9388 s = p->parent;
9389 p->parent = r;
9390 r->parent = s;
9391 if (s)
9393 if (s->left == p)
9394 s->left = r;
9395 else
9396 s->right = r;
9398 else
9399 constructor_pending_elts = r;
9401 else
9403 /* LR rotation. */
9404 struct init_node *t = r->right;
9406 r->right = t->left;
9407 if (r->right)
9408 r->right->parent = r;
9409 t->left = r;
9411 p->left = t->right;
9412 if (p->left)
9413 p->left->parent = p;
9414 t->right = p;
9416 p->balance = t->balance < 0;
9417 r->balance = -(t->balance > 0);
9418 t->balance = 0;
9420 s = p->parent;
9421 p->parent = t;
9422 r->parent = t;
9423 t->parent = s;
9424 if (s)
9426 if (s->left == p)
9427 s->left = t;
9428 else
9429 s->right = t;
9431 else
9432 constructor_pending_elts = t;
9434 break;
9436 else
9438 /* p->balance == +1; growth of left side balances the node. */
9439 p->balance = 0;
9440 break;
9443 else /* r == p->right */
9445 if (p->balance == 0)
9446 /* Growth propagation from right side. */
9447 p->balance++;
9448 else if (p->balance > 0)
9450 if (r->balance > 0)
9452 /* R rotation. */
9453 p->right = r->left;
9454 if (p->right)
9455 p->right->parent = p;
9456 r->left = p;
9458 p->balance = 0;
9459 r->balance = 0;
9461 s = p->parent;
9462 p->parent = r;
9463 r->parent = s;
9464 if (s)
9466 if (s->left == p)
9467 s->left = r;
9468 else
9469 s->right = r;
9471 else
9472 constructor_pending_elts = r;
9474 else /* r->balance == -1 */
9476 /* RL rotation */
9477 struct init_node *t = r->left;
9479 r->left = t->right;
9480 if (r->left)
9481 r->left->parent = r;
9482 t->right = r;
9484 p->right = t->left;
9485 if (p->right)
9486 p->right->parent = p;
9487 t->left = p;
9489 r->balance = (t->balance < 0);
9490 p->balance = -(t->balance > 0);
9491 t->balance = 0;
9493 s = p->parent;
9494 p->parent = t;
9495 r->parent = t;
9496 t->parent = s;
9497 if (s)
9499 if (s->left == p)
9500 s->left = t;
9501 else
9502 s->right = t;
9504 else
9505 constructor_pending_elts = t;
9507 break;
9509 else
9511 /* p->balance == -1; growth of right side balances the node. */
9512 p->balance = 0;
9513 break;
9517 r = p;
9518 p = p->parent;
9522 /* Build AVL tree from a sorted chain. */
9524 static void
9525 set_nonincremental_init (struct obstack * braced_init_obstack)
9527 unsigned HOST_WIDE_INT ix;
9528 tree index, value;
9530 if (TREE_CODE (constructor_type) != RECORD_TYPE
9531 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9532 return;
9534 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9535 add_pending_init (input_location, index, value, NULL_TREE, true,
9536 braced_init_obstack);
9537 constructor_elements = NULL;
9538 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9540 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9541 /* Skip any nameless bit fields at the beginning. */
9542 while (constructor_unfilled_fields != NULL_TREE
9543 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9544 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9547 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9549 if (TYPE_DOMAIN (constructor_type))
9550 constructor_unfilled_index
9551 = convert (bitsizetype,
9552 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9553 else
9554 constructor_unfilled_index = bitsize_zero_node;
9556 constructor_incremental = 0;
9559 /* Build AVL tree from a string constant. */
9561 static void
9562 set_nonincremental_init_from_string (tree str,
9563 struct obstack * braced_init_obstack)
9565 tree value, purpose, type;
9566 HOST_WIDE_INT val[2];
9567 const char *p, *end;
9568 int byte, wchar_bytes, charwidth, bitpos;
9570 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9572 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9573 charwidth = TYPE_PRECISION (char_type_node);
9574 gcc_assert ((size_t) wchar_bytes * charwidth
9575 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9576 type = TREE_TYPE (constructor_type);
9577 p = TREE_STRING_POINTER (str);
9578 end = p + TREE_STRING_LENGTH (str);
9580 for (purpose = bitsize_zero_node;
9581 p < end
9582 && !(constructor_max_index
9583 && tree_int_cst_lt (constructor_max_index, purpose));
9584 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9586 if (wchar_bytes == 1)
9588 val[0] = (unsigned char) *p++;
9589 val[1] = 0;
9591 else
9593 val[1] = 0;
9594 val[0] = 0;
9595 for (byte = 0; byte < wchar_bytes; byte++)
9597 if (BYTES_BIG_ENDIAN)
9598 bitpos = (wchar_bytes - byte - 1) * charwidth;
9599 else
9600 bitpos = byte * charwidth;
9601 val[bitpos / HOST_BITS_PER_WIDE_INT]
9602 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9603 << (bitpos % HOST_BITS_PER_WIDE_INT);
9607 if (!TYPE_UNSIGNED (type))
9609 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9610 if (bitpos < HOST_BITS_PER_WIDE_INT)
9612 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9614 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9615 val[1] = -1;
9618 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9620 if (val[0] < 0)
9621 val[1] = -1;
9623 else if (val[1] & (HOST_WIDE_INT_1
9624 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9625 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9628 value = wide_int_to_tree (type,
9629 wide_int::from_array (val, 2,
9630 HOST_BITS_PER_WIDE_INT * 2));
9631 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9632 braced_init_obstack);
9635 constructor_incremental = 0;
9638 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9639 not initialized yet. */
9641 static tree
9642 find_init_member (tree field, struct obstack * braced_init_obstack)
9644 struct init_node *p;
9646 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9648 if (constructor_incremental
9649 && tree_int_cst_lt (field, constructor_unfilled_index))
9650 set_nonincremental_init (braced_init_obstack);
9652 p = constructor_pending_elts;
9653 while (p)
9655 if (tree_int_cst_lt (field, p->purpose))
9656 p = p->left;
9657 else if (tree_int_cst_lt (p->purpose, field))
9658 p = p->right;
9659 else
9660 return p->value;
9663 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9665 tree bitpos = bit_position (field);
9667 if (constructor_incremental
9668 && (!constructor_unfilled_fields
9669 || tree_int_cst_lt (bitpos,
9670 bit_position (constructor_unfilled_fields))))
9671 set_nonincremental_init (braced_init_obstack);
9673 p = constructor_pending_elts;
9674 while (p)
9676 if (field == p->purpose)
9677 return p->value;
9678 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9679 p = p->left;
9680 else
9681 p = p->right;
9684 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9686 if (!vec_safe_is_empty (constructor_elements)
9687 && (constructor_elements->last ().index == field))
9688 return constructor_elements->last ().value;
9690 return NULL_TREE;
9693 /* "Output" the next constructor element.
9694 At top level, really output it to assembler code now.
9695 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9696 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9697 TYPE is the data type that the containing data type wants here.
9698 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9699 If VALUE is a string constant, STRICT_STRING is true if it is
9700 unparenthesized or we should not warn here for it being parenthesized.
9701 For other types of VALUE, STRICT_STRING is not used.
9703 PENDING if true means output pending elements that belong
9704 right after this element. (PENDING is normally true;
9705 it is false while outputting pending elements, to avoid recursion.)
9707 IMPLICIT is true if value comes from pop_init_level (1),
9708 the new initializer has been merged with the existing one
9709 and thus no warnings should be emitted about overriding an
9710 existing initializer. */
9712 static void
9713 output_init_element (location_t loc, tree value, tree origtype,
9714 bool strict_string, tree type, tree field, bool pending,
9715 bool implicit, struct obstack * braced_init_obstack)
9717 tree semantic_type = NULL_TREE;
9718 bool maybe_const = true;
9719 bool npc;
9721 if (type == error_mark_node || value == error_mark_node)
9723 constructor_erroneous = 1;
9724 return;
9726 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9727 && (TREE_CODE (value) == STRING_CST
9728 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9729 && !(TREE_CODE (value) == STRING_CST
9730 && TREE_CODE (type) == ARRAY_TYPE
9731 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9732 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9733 TYPE_MAIN_VARIANT (type)))
9734 value = array_to_pointer_conversion (input_location, value);
9736 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9737 && require_constant_value && pending)
9739 /* As an extension, allow initializing objects with static storage
9740 duration with compound literals (which are then treated just as
9741 the brace enclosed list they contain). */
9742 if (flag_isoc99)
9743 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9744 "constant");
9745 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9746 value = DECL_INITIAL (decl);
9749 npc = null_pointer_constant_p (value);
9750 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9752 semantic_type = TREE_TYPE (value);
9753 value = TREE_OPERAND (value, 0);
9755 value = c_fully_fold (value, require_constant_value, &maybe_const);
9757 if (value == error_mark_node)
9758 constructor_erroneous = 1;
9759 else if (!TREE_CONSTANT (value))
9760 constructor_constant = 0;
9761 else if (!initializer_constant_valid_p (value,
9762 TREE_TYPE (value),
9763 AGGREGATE_TYPE_P (constructor_type)
9764 && TYPE_REVERSE_STORAGE_ORDER
9765 (constructor_type))
9766 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9767 && DECL_C_BIT_FIELD (field)
9768 && TREE_CODE (value) != INTEGER_CST))
9769 constructor_simple = 0;
9770 if (!maybe_const)
9771 constructor_nonconst = 1;
9773 /* Digest the initializer and issue any errors about incompatible
9774 types before issuing errors about non-constant initializers. */
9775 tree new_value = value;
9776 if (semantic_type)
9777 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9778 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9779 require_constant_value);
9780 if (new_value == error_mark_node)
9782 constructor_erroneous = 1;
9783 return;
9785 if (require_constant_value || require_constant_elements)
9786 constant_expression_warning (new_value);
9788 /* Proceed to check the constness of the original initializer. */
9789 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9791 if (require_constant_value)
9793 error_init (loc, "initializer element is not constant");
9794 value = error_mark_node;
9796 else if (require_constant_elements)
9797 pedwarn (loc, OPT_Wpedantic,
9798 "initializer element is not computable at load time");
9800 else if (!maybe_const
9801 && (require_constant_value || require_constant_elements))
9802 pedwarn_init (loc, OPT_Wpedantic,
9803 "initializer element is not a constant expression");
9805 /* Issue -Wc++-compat warnings about initializing a bitfield with
9806 enum type. */
9807 if (warn_cxx_compat
9808 && field != NULL_TREE
9809 && TREE_CODE (field) == FIELD_DECL
9810 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9811 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9812 != TYPE_MAIN_VARIANT (type))
9813 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9815 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9816 if (checktype != error_mark_node
9817 && (TYPE_MAIN_VARIANT (checktype)
9818 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9819 warning_init (loc, OPT_Wc___compat,
9820 "enum conversion in initialization is invalid in C++");
9823 /* If this field is empty and does not have side effects (and is not at
9824 the end of structure), don't do anything other than checking the
9825 initializer. */
9826 if (field
9827 && (TREE_TYPE (field) == error_mark_node
9828 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9829 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9830 && !TREE_SIDE_EFFECTS (new_value)
9831 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9832 || DECL_CHAIN (field)))))
9833 return;
9835 /* Finally, set VALUE to the initializer value digested above. */
9836 value = new_value;
9838 /* If this element doesn't come next in sequence,
9839 put it on constructor_pending_elts. */
9840 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9841 && (!constructor_incremental
9842 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9844 if (constructor_incremental
9845 && tree_int_cst_lt (field, constructor_unfilled_index))
9846 set_nonincremental_init (braced_init_obstack);
9848 add_pending_init (loc, field, value, origtype, implicit,
9849 braced_init_obstack);
9850 return;
9852 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9853 && (!constructor_incremental
9854 || field != constructor_unfilled_fields))
9856 /* We do this for records but not for unions. In a union,
9857 no matter which field is specified, it can be initialized
9858 right away since it starts at the beginning of the union. */
9859 if (constructor_incremental)
9861 if (!constructor_unfilled_fields)
9862 set_nonincremental_init (braced_init_obstack);
9863 else
9865 tree bitpos, unfillpos;
9867 bitpos = bit_position (field);
9868 unfillpos = bit_position (constructor_unfilled_fields);
9870 if (tree_int_cst_lt (bitpos, unfillpos))
9871 set_nonincremental_init (braced_init_obstack);
9875 add_pending_init (loc, field, value, origtype, implicit,
9876 braced_init_obstack);
9877 return;
9879 else if (TREE_CODE (constructor_type) == UNION_TYPE
9880 && !vec_safe_is_empty (constructor_elements))
9882 if (!implicit)
9884 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9885 warning_init (loc, OPT_Woverride_init_side_effects,
9886 "initialized field with side-effects overwritten");
9887 else if (warn_override_init)
9888 warning_init (loc, OPT_Woverride_init,
9889 "initialized field overwritten");
9892 /* We can have just one union field set. */
9893 constructor_elements = NULL;
9896 /* Otherwise, output this element either to
9897 constructor_elements or to the assembler file. */
9899 constructor_elt celt = {field, value};
9900 vec_safe_push (constructor_elements, celt);
9902 /* Advance the variable that indicates sequential elements output. */
9903 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9904 constructor_unfilled_index
9905 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9906 bitsize_one_node);
9907 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9909 constructor_unfilled_fields
9910 = DECL_CHAIN (constructor_unfilled_fields);
9912 /* Skip any nameless bit fields. */
9913 while (constructor_unfilled_fields != NULL_TREE
9914 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9915 constructor_unfilled_fields =
9916 DECL_CHAIN (constructor_unfilled_fields);
9918 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9919 constructor_unfilled_fields = NULL_TREE;
9921 /* Now output any pending elements which have become next. */
9922 if (pending)
9923 output_pending_init_elements (0, braced_init_obstack);
9926 /* For two FIELD_DECLs in the same chain, return -1 if field1
9927 comes before field2, 1 if field1 comes after field2 and
9928 0 if field1 == field2. */
9930 static int
9931 init_field_decl_cmp (tree field1, tree field2)
9933 if (field1 == field2)
9934 return 0;
9936 tree bitpos1 = bit_position (field1);
9937 tree bitpos2 = bit_position (field2);
9938 if (tree_int_cst_equal (bitpos1, bitpos2))
9940 /* If one of the fields has non-zero bitsize, then that
9941 field must be the last one in a sequence of zero
9942 sized fields, fields after it will have bigger
9943 bit_position. */
9944 if (TREE_TYPE (field1) != error_mark_node
9945 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9946 && integer_nonzerop (TREE_TYPE (field1)))
9947 return 1;
9948 if (TREE_TYPE (field2) != error_mark_node
9949 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9950 && integer_nonzerop (TREE_TYPE (field2)))
9951 return -1;
9952 /* Otherwise, fallback to DECL_CHAIN walk to find out
9953 which field comes earlier. Walk chains of both
9954 fields, so that if field1 and field2 are close to each
9955 other in either order, it is found soon even for large
9956 sequences of zero sized fields. */
9957 tree f1 = field1, f2 = field2;
9958 while (1)
9960 f1 = DECL_CHAIN (f1);
9961 f2 = DECL_CHAIN (f2);
9962 if (f1 == NULL_TREE)
9964 gcc_assert (f2);
9965 return 1;
9967 if (f2 == NULL_TREE)
9968 return -1;
9969 if (f1 == field2)
9970 return -1;
9971 if (f2 == field1)
9972 return 1;
9973 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9974 return 1;
9975 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9976 return -1;
9979 else if (tree_int_cst_lt (bitpos1, bitpos2))
9980 return -1;
9981 else
9982 return 1;
9985 /* Output any pending elements which have become next.
9986 As we output elements, constructor_unfilled_{fields,index}
9987 advances, which may cause other elements to become next;
9988 if so, they too are output.
9990 If ALL is 0, we return when there are
9991 no more pending elements to output now.
9993 If ALL is 1, we output space as necessary so that
9994 we can output all the pending elements. */
9995 static void
9996 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9998 struct init_node *elt = constructor_pending_elts;
9999 tree next;
10001 retry:
10003 /* Look through the whole pending tree.
10004 If we find an element that should be output now,
10005 output it. Otherwise, set NEXT to the element
10006 that comes first among those still pending. */
10008 next = NULL_TREE;
10009 while (elt)
10011 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10013 if (tree_int_cst_equal (elt->purpose,
10014 constructor_unfilled_index))
10015 output_init_element (input_location, elt->value, elt->origtype,
10016 true, TREE_TYPE (constructor_type),
10017 constructor_unfilled_index, false, false,
10018 braced_init_obstack);
10019 else if (tree_int_cst_lt (constructor_unfilled_index,
10020 elt->purpose))
10022 /* Advance to the next smaller node. */
10023 if (elt->left)
10024 elt = elt->left;
10025 else
10027 /* We have reached the smallest node bigger than the
10028 current unfilled index. Fill the space first. */
10029 next = elt->purpose;
10030 break;
10033 else
10035 /* Advance to the next bigger node. */
10036 if (elt->right)
10037 elt = elt->right;
10038 else
10040 /* We have reached the biggest node in a subtree. Find
10041 the parent of it, which is the next bigger node. */
10042 while (elt->parent && elt->parent->right == elt)
10043 elt = elt->parent;
10044 elt = elt->parent;
10045 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10046 elt->purpose))
10048 next = elt->purpose;
10049 break;
10054 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10056 /* If the current record is complete we are done. */
10057 if (constructor_unfilled_fields == NULL_TREE)
10058 break;
10060 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10061 elt->purpose);
10062 if (cmp == 0)
10063 output_init_element (input_location, elt->value, elt->origtype,
10064 true, TREE_TYPE (elt->purpose),
10065 elt->purpose, false, false,
10066 braced_init_obstack);
10067 else if (cmp < 0)
10069 /* Advance to the next smaller node. */
10070 if (elt->left)
10071 elt = elt->left;
10072 else
10074 /* We have reached the smallest node bigger than the
10075 current unfilled field. Fill the space first. */
10076 next = elt->purpose;
10077 break;
10080 else
10082 /* Advance to the next bigger node. */
10083 if (elt->right)
10084 elt = elt->right;
10085 else
10087 /* We have reached the biggest node in a subtree. Find
10088 the parent of it, which is the next bigger node. */
10089 while (elt->parent && elt->parent->right == elt)
10090 elt = elt->parent;
10091 elt = elt->parent;
10092 if (elt
10093 && init_field_decl_cmp (constructor_unfilled_fields,
10094 elt->purpose) < 0)
10096 next = elt->purpose;
10097 break;
10104 /* Ordinarily return, but not if we want to output all
10105 and there are elements left. */
10106 if (!(all && next != NULL_TREE))
10107 return;
10109 /* If it's not incremental, just skip over the gap, so that after
10110 jumping to retry we will output the next successive element. */
10111 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10112 constructor_unfilled_fields = next;
10113 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10114 constructor_unfilled_index = next;
10116 /* ELT now points to the node in the pending tree with the next
10117 initializer to output. */
10118 goto retry;
10121 /* Expression VALUE coincides with the start of type TYPE in a braced
10122 initializer. Return true if we should treat VALUE as initializing
10123 the first element of TYPE, false if we should treat it as initializing
10124 TYPE as a whole.
10126 If the initializer is clearly invalid, the question becomes:
10127 which choice gives the best error message? */
10129 static bool
10130 initialize_elementwise_p (tree type, tree value)
10132 if (type == error_mark_node || value == error_mark_node)
10133 return false;
10135 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10137 tree value_type = TREE_TYPE (value);
10138 if (value_type == error_mark_node)
10139 return false;
10141 /* GNU vectors can be initialized elementwise. However, treat any
10142 kind of vector value as initializing the vector type as a whole,
10143 regardless of whether the value is a GNU vector. Such initializers
10144 are valid if and only if they would have been valid in a non-braced
10145 initializer like:
10147 TYPE foo = VALUE;
10149 so recursing into the vector type would be at best confusing or at
10150 worst wrong. For example, when -flax-vector-conversions is in effect,
10151 it's possible to initialize a V8HI from a V4SI, even though the vectors
10152 have different element types and different numbers of elements. */
10153 if (gnu_vector_type_p (type))
10154 return !VECTOR_TYPE_P (value_type);
10156 if (AGGREGATE_TYPE_P (type))
10157 return type != TYPE_MAIN_VARIANT (value_type);
10159 return false;
10162 /* Add one non-braced element to the current constructor level.
10163 This adjusts the current position within the constructor's type.
10164 This may also start or terminate implicit levels
10165 to handle a partly-braced initializer.
10167 Once this has found the correct level for the new element,
10168 it calls output_init_element.
10170 IMPLICIT is true if value comes from pop_init_level (1),
10171 the new initializer has been merged with the existing one
10172 and thus no warnings should be emitted about overriding an
10173 existing initializer. */
10175 void
10176 process_init_element (location_t loc, struct c_expr value, bool implicit,
10177 struct obstack * braced_init_obstack)
10179 tree orig_value = value.value;
10180 int string_flag
10181 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10182 bool strict_string = value.original_code == STRING_CST;
10183 bool was_designated = designator_depth != 0;
10185 designator_depth = 0;
10186 designator_erroneous = 0;
10188 if (!implicit && value.value && !integer_zerop (value.value))
10189 constructor_zeroinit = 0;
10191 /* Handle superfluous braces around string cst as in
10192 char x[] = {"foo"}; */
10193 if (string_flag
10194 && constructor_type
10195 && !was_designated
10196 && TREE_CODE (constructor_type) == ARRAY_TYPE
10197 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10198 && integer_zerop (constructor_unfilled_index))
10200 if (constructor_stack->replacement_value.value)
10201 error_init (loc, "excess elements in %<char%> array initializer");
10202 constructor_stack->replacement_value = value;
10203 return;
10206 if (constructor_stack->replacement_value.value != NULL_TREE)
10208 error_init (loc, "excess elements in struct initializer");
10209 return;
10212 /* Ignore elements of a brace group if it is entirely superfluous
10213 and has already been diagnosed, or if the type is erroneous. */
10214 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10215 return;
10217 /* Ignore elements of an initializer for a variable-size type.
10218 Those are diagnosed in digest_init. */
10219 if (COMPLETE_TYPE_P (constructor_type)
10220 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10221 return;
10223 if (!implicit && warn_designated_init && !was_designated
10224 && TREE_CODE (constructor_type) == RECORD_TYPE
10225 && lookup_attribute ("designated_init",
10226 TYPE_ATTRIBUTES (constructor_type)))
10227 warning_init (loc,
10228 OPT_Wdesignated_init,
10229 "positional initialization of field "
10230 "in %<struct%> declared with %<designated_init%> attribute");
10232 /* If we've exhausted any levels that didn't have braces,
10233 pop them now. */
10234 while (constructor_stack->implicit)
10236 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10237 && constructor_fields == NULL_TREE)
10238 process_init_element (loc,
10239 pop_init_level (loc, 1, braced_init_obstack,
10240 last_init_list_comma),
10241 true, braced_init_obstack);
10242 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10243 || gnu_vector_type_p (constructor_type))
10244 && constructor_max_index
10245 && tree_int_cst_lt (constructor_max_index,
10246 constructor_index))
10247 process_init_element (loc,
10248 pop_init_level (loc, 1, braced_init_obstack,
10249 last_init_list_comma),
10250 true, braced_init_obstack);
10251 else
10252 break;
10255 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10256 if (constructor_range_stack)
10258 /* If value is a compound literal and we'll be just using its
10259 content, don't put it into a SAVE_EXPR. */
10260 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10261 || !require_constant_value)
10263 tree semantic_type = NULL_TREE;
10264 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10266 semantic_type = TREE_TYPE (value.value);
10267 value.value = TREE_OPERAND (value.value, 0);
10269 value.value = save_expr (value.value);
10270 if (semantic_type)
10271 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10272 value.value);
10276 while (1)
10278 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10280 tree fieldtype;
10281 enum tree_code fieldcode;
10283 if (constructor_fields == NULL_TREE)
10285 pedwarn_init (loc, 0, "excess elements in struct initializer");
10286 break;
10289 fieldtype = TREE_TYPE (constructor_fields);
10290 if (fieldtype != error_mark_node)
10291 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10292 fieldcode = TREE_CODE (fieldtype);
10294 /* Error for non-static initialization of a flexible array member. */
10295 if (fieldcode == ARRAY_TYPE
10296 && !require_constant_value
10297 && TYPE_SIZE (fieldtype) == NULL_TREE
10298 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10300 error_init (loc, "non-static initialization of a flexible "
10301 "array member");
10302 break;
10305 /* Error for initialization of a flexible array member with
10306 a string constant if the structure is in an array. E.g.:
10307 struct S { int x; char y[]; };
10308 struct S s[] = { { 1, "foo" } };
10309 is invalid. */
10310 if (string_flag
10311 && fieldcode == ARRAY_TYPE
10312 && constructor_depth > 1
10313 && TYPE_SIZE (fieldtype) == NULL_TREE
10314 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10316 bool in_array_p = false;
10317 for (struct constructor_stack *p = constructor_stack;
10318 p && p->type; p = p->next)
10319 if (TREE_CODE (p->type) == ARRAY_TYPE)
10321 in_array_p = true;
10322 break;
10324 if (in_array_p)
10326 error_init (loc, "initialization of flexible array "
10327 "member in a nested context");
10328 break;
10332 /* Accept a string constant to initialize a subarray. */
10333 if (value.value != NULL_TREE
10334 && fieldcode == ARRAY_TYPE
10335 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10336 && string_flag)
10337 value.value = orig_value;
10338 /* Otherwise, if we have come to a subaggregate,
10339 and we don't have an element of its type, push into it. */
10340 else if (value.value != NULL_TREE
10341 && initialize_elementwise_p (fieldtype, value.value))
10343 push_init_level (loc, 1, braced_init_obstack);
10344 continue;
10347 if (value.value)
10349 push_member_name (constructor_fields);
10350 output_init_element (loc, value.value, value.original_type,
10351 strict_string, fieldtype,
10352 constructor_fields, true, implicit,
10353 braced_init_obstack);
10354 RESTORE_SPELLING_DEPTH (constructor_depth);
10356 else
10357 /* Do the bookkeeping for an element that was
10358 directly output as a constructor. */
10360 /* For a record, keep track of end position of last field. */
10361 if (DECL_SIZE (constructor_fields))
10362 constructor_bit_index
10363 = size_binop_loc (input_location, PLUS_EXPR,
10364 bit_position (constructor_fields),
10365 DECL_SIZE (constructor_fields));
10367 /* If the current field was the first one not yet written out,
10368 it isn't now, so update. */
10369 if (constructor_unfilled_fields == constructor_fields)
10371 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10372 /* Skip any nameless bit fields. */
10373 while (constructor_unfilled_fields != 0
10374 && (DECL_UNNAMED_BIT_FIELD
10375 (constructor_unfilled_fields)))
10376 constructor_unfilled_fields =
10377 DECL_CHAIN (constructor_unfilled_fields);
10381 constructor_fields = DECL_CHAIN (constructor_fields);
10382 /* Skip any nameless bit fields at the beginning. */
10383 while (constructor_fields != NULL_TREE
10384 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10385 constructor_fields = DECL_CHAIN (constructor_fields);
10387 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10389 tree fieldtype;
10390 enum tree_code fieldcode;
10392 if (constructor_fields == NULL_TREE)
10394 pedwarn_init (loc, 0,
10395 "excess elements in union initializer");
10396 break;
10399 fieldtype = TREE_TYPE (constructor_fields);
10400 if (fieldtype != error_mark_node)
10401 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10402 fieldcode = TREE_CODE (fieldtype);
10404 /* Warn that traditional C rejects initialization of unions.
10405 We skip the warning if the value is zero. This is done
10406 under the assumption that the zero initializer in user
10407 code appears conditioned on e.g. __STDC__ to avoid
10408 "missing initializer" warnings and relies on default
10409 initialization to zero in the traditional C case.
10410 We also skip the warning if the initializer is designated,
10411 again on the assumption that this must be conditional on
10412 __STDC__ anyway (and we've already complained about the
10413 member-designator already). */
10414 if (!in_system_header_at (input_location) && !constructor_designated
10415 && !(value.value && (integer_zerop (value.value)
10416 || real_zerop (value.value))))
10417 warning (OPT_Wtraditional, "traditional C rejects initialization "
10418 "of unions");
10420 /* Accept a string constant to initialize a subarray. */
10421 if (value.value != NULL_TREE
10422 && fieldcode == ARRAY_TYPE
10423 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10424 && string_flag)
10425 value.value = orig_value;
10426 /* Otherwise, if we have come to a subaggregate,
10427 and we don't have an element of its type, push into it. */
10428 else if (value.value != NULL_TREE
10429 && initialize_elementwise_p (fieldtype, value.value))
10431 push_init_level (loc, 1, braced_init_obstack);
10432 continue;
10435 if (value.value)
10437 push_member_name (constructor_fields);
10438 output_init_element (loc, value.value, value.original_type,
10439 strict_string, fieldtype,
10440 constructor_fields, true, implicit,
10441 braced_init_obstack);
10442 RESTORE_SPELLING_DEPTH (constructor_depth);
10444 else
10445 /* Do the bookkeeping for an element that was
10446 directly output as a constructor. */
10448 constructor_bit_index = DECL_SIZE (constructor_fields);
10449 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10452 constructor_fields = NULL_TREE;
10454 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10456 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10457 enum tree_code eltcode = TREE_CODE (elttype);
10459 /* Accept a string constant to initialize a subarray. */
10460 if (value.value != NULL_TREE
10461 && eltcode == ARRAY_TYPE
10462 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10463 && string_flag)
10464 value.value = orig_value;
10465 /* Otherwise, if we have come to a subaggregate,
10466 and we don't have an element of its type, push into it. */
10467 else if (value.value != NULL_TREE
10468 && initialize_elementwise_p (elttype, value.value))
10470 push_init_level (loc, 1, braced_init_obstack);
10471 continue;
10474 if (constructor_max_index != NULL_TREE
10475 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10476 || integer_all_onesp (constructor_max_index)))
10478 pedwarn_init (loc, 0,
10479 "excess elements in array initializer");
10480 break;
10483 /* Now output the actual element. */
10484 if (value.value)
10486 push_array_bounds (tree_to_uhwi (constructor_index));
10487 output_init_element (loc, value.value, value.original_type,
10488 strict_string, elttype,
10489 constructor_index, true, implicit,
10490 braced_init_obstack);
10491 RESTORE_SPELLING_DEPTH (constructor_depth);
10494 constructor_index
10495 = size_binop_loc (input_location, PLUS_EXPR,
10496 constructor_index, bitsize_one_node);
10498 if (!value.value)
10499 /* If we are doing the bookkeeping for an element that was
10500 directly output as a constructor, we must update
10501 constructor_unfilled_index. */
10502 constructor_unfilled_index = constructor_index;
10504 else if (gnu_vector_type_p (constructor_type))
10506 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10508 /* Do a basic check of initializer size. Note that vectors
10509 always have a fixed size derived from their type. */
10510 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10512 pedwarn_init (loc, 0,
10513 "excess elements in vector initializer");
10514 break;
10517 /* Now output the actual element. */
10518 if (value.value)
10520 if (TREE_CODE (value.value) == VECTOR_CST)
10521 elttype = TYPE_MAIN_VARIANT (constructor_type);
10522 output_init_element (loc, value.value, value.original_type,
10523 strict_string, elttype,
10524 constructor_index, true, implicit,
10525 braced_init_obstack);
10528 constructor_index
10529 = size_binop_loc (input_location,
10530 PLUS_EXPR, constructor_index, bitsize_one_node);
10532 if (!value.value)
10533 /* If we are doing the bookkeeping for an element that was
10534 directly output as a constructor, we must update
10535 constructor_unfilled_index. */
10536 constructor_unfilled_index = constructor_index;
10539 /* Handle the sole element allowed in a braced initializer
10540 for a scalar variable. */
10541 else if (constructor_type != error_mark_node
10542 && constructor_fields == NULL_TREE)
10544 pedwarn_init (loc, 0,
10545 "excess elements in scalar initializer");
10546 break;
10548 else
10550 if (value.value)
10551 output_init_element (loc, value.value, value.original_type,
10552 strict_string, constructor_type,
10553 NULL_TREE, true, implicit,
10554 braced_init_obstack);
10555 constructor_fields = NULL_TREE;
10558 /* Handle range initializers either at this level or anywhere higher
10559 in the designator stack. */
10560 if (constructor_range_stack)
10562 struct constructor_range_stack *p, *range_stack;
10563 int finish = 0;
10565 range_stack = constructor_range_stack;
10566 constructor_range_stack = 0;
10567 while (constructor_stack != range_stack->stack)
10569 gcc_assert (constructor_stack->implicit);
10570 process_init_element (loc,
10571 pop_init_level (loc, 1,
10572 braced_init_obstack,
10573 last_init_list_comma),
10574 true, braced_init_obstack);
10576 for (p = range_stack;
10577 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10578 p = p->prev)
10580 gcc_assert (constructor_stack->implicit);
10581 process_init_element (loc,
10582 pop_init_level (loc, 1,
10583 braced_init_obstack,
10584 last_init_list_comma),
10585 true, braced_init_obstack);
10588 p->index = size_binop_loc (input_location,
10589 PLUS_EXPR, p->index, bitsize_one_node);
10590 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10591 finish = 1;
10593 while (1)
10595 constructor_index = p->index;
10596 constructor_fields = p->fields;
10597 if (finish && p->range_end && p->index == p->range_start)
10599 finish = 0;
10600 p->prev = 0;
10602 p = p->next;
10603 if (!p)
10604 break;
10605 finish_implicit_inits (loc, braced_init_obstack);
10606 push_init_level (loc, 2, braced_init_obstack);
10607 p->stack = constructor_stack;
10608 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10609 p->index = p->range_start;
10612 if (!finish)
10613 constructor_range_stack = range_stack;
10614 continue;
10617 break;
10620 constructor_range_stack = 0;
10623 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10624 (guaranteed to be 'volatile' or null) and ARGS (represented using
10625 an ASM_EXPR node). */
10626 tree
10627 build_asm_stmt (bool is_volatile, tree args)
10629 if (is_volatile)
10630 ASM_VOLATILE_P (args) = 1;
10631 return add_stmt (args);
10634 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10635 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10636 SIMPLE indicates whether there was anything at all after the
10637 string in the asm expression -- asm("blah") and asm("blah" : )
10638 are subtly different. We use a ASM_EXPR node to represent this.
10639 LOC is the location of the asm, and IS_INLINE says whether this
10640 is asm inline. */
10641 tree
10642 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10643 tree clobbers, tree labels, bool simple, bool is_inline)
10645 tree tail;
10646 tree args;
10647 int i;
10648 const char *constraint;
10649 const char **oconstraints;
10650 bool allows_mem, allows_reg, is_inout;
10651 int ninputs, noutputs;
10653 ninputs = list_length (inputs);
10654 noutputs = list_length (outputs);
10655 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10657 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10659 /* Remove output conversions that change the type but not the mode. */
10660 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10662 tree output = TREE_VALUE (tail);
10664 output = c_fully_fold (output, false, NULL, true);
10666 /* ??? Really, this should not be here. Users should be using a
10667 proper lvalue, dammit. But there's a long history of using casts
10668 in the output operands. In cases like longlong.h, this becomes a
10669 primitive form of typechecking -- if the cast can be removed, then
10670 the output operand had a type of the proper width; otherwise we'll
10671 get an error. Gross, but ... */
10672 STRIP_NOPS (output);
10674 if (!lvalue_or_else (loc, output, lv_asm))
10675 output = error_mark_node;
10677 if (output != error_mark_node
10678 && (TREE_READONLY (output)
10679 || TYPE_READONLY (TREE_TYPE (output))
10680 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10681 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10682 readonly_error (loc, output, lv_asm);
10684 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10685 oconstraints[i] = constraint;
10687 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10688 &allows_mem, &allows_reg, &is_inout))
10690 /* If the operand is going to end up in memory,
10691 mark it addressable. */
10692 if (!allows_reg && !c_mark_addressable (output))
10693 output = error_mark_node;
10694 if (!(!allows_reg && allows_mem)
10695 && output != error_mark_node
10696 && VOID_TYPE_P (TREE_TYPE (output)))
10698 error_at (loc, "invalid use of void expression");
10699 output = error_mark_node;
10702 else
10703 output = error_mark_node;
10705 TREE_VALUE (tail) = output;
10708 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10710 tree input;
10712 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10713 input = TREE_VALUE (tail);
10715 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10716 oconstraints, &allows_mem, &allows_reg))
10718 /* If the operand is going to end up in memory,
10719 mark it addressable. */
10720 if (!allows_reg && allows_mem)
10722 input = c_fully_fold (input, false, NULL, true);
10724 /* Strip the nops as we allow this case. FIXME, this really
10725 should be rejected or made deprecated. */
10726 STRIP_NOPS (input);
10727 if (!c_mark_addressable (input))
10728 input = error_mark_node;
10730 else
10732 struct c_expr expr;
10733 memset (&expr, 0, sizeof (expr));
10734 expr.value = input;
10735 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10736 input = c_fully_fold (expr.value, false, NULL);
10738 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10740 error_at (loc, "invalid use of void expression");
10741 input = error_mark_node;
10745 else
10746 input = error_mark_node;
10748 TREE_VALUE (tail) = input;
10751 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10753 /* asm statements without outputs, including simple ones, are treated
10754 as volatile. */
10755 ASM_INPUT_P (args) = simple;
10756 ASM_VOLATILE_P (args) = (noutputs == 0);
10757 ASM_INLINE_P (args) = is_inline;
10759 return args;
10762 /* Generate a goto statement to LABEL. LOC is the location of the
10763 GOTO. */
10765 tree
10766 c_finish_goto_label (location_t loc, tree label)
10768 tree decl = lookup_label_for_goto (loc, label);
10769 if (!decl)
10770 return NULL_TREE;
10771 TREE_USED (decl) = 1;
10773 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10774 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10775 SET_EXPR_LOCATION (t, loc);
10776 return add_stmt (t);
10780 /* Generate a computed goto statement to EXPR. LOC is the location of
10781 the GOTO. */
10783 tree
10784 c_finish_goto_ptr (location_t loc, tree expr)
10786 tree t;
10787 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10788 expr = c_fully_fold (expr, false, NULL);
10789 expr = convert (ptr_type_node, expr);
10790 t = build1 (GOTO_EXPR, void_type_node, expr);
10791 SET_EXPR_LOCATION (t, loc);
10792 return add_stmt (t);
10795 /* Generate a C `return' statement. RETVAL is the expression for what
10796 to return, or a null pointer for `return;' with no value. LOC is
10797 the location of the return statement, or the location of the expression,
10798 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10799 is the original type of RETVAL. */
10801 tree
10802 c_finish_return (location_t loc, tree retval, tree origtype)
10804 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10805 bool no_warning = false;
10806 bool npc = false;
10808 /* Use the expansion point to handle cases such as returning NULL
10809 in a function returning void. */
10810 location_t xloc = expansion_point_location_if_in_system_header (loc);
10812 if (TREE_THIS_VOLATILE (current_function_decl))
10813 warning_at (xloc, 0,
10814 "function declared %<noreturn%> has a %<return%> statement");
10816 if (retval)
10818 tree semantic_type = NULL_TREE;
10819 npc = null_pointer_constant_p (retval);
10820 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10822 semantic_type = TREE_TYPE (retval);
10823 retval = TREE_OPERAND (retval, 0);
10825 retval = c_fully_fold (retval, false, NULL);
10826 if (semantic_type
10827 && valtype != NULL_TREE
10828 && TREE_CODE (valtype) != VOID_TYPE)
10829 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10832 if (!retval)
10834 current_function_returns_null = 1;
10835 if ((warn_return_type >= 0 || flag_isoc99)
10836 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10838 bool warned_here;
10839 if (flag_isoc99)
10840 warned_here = pedwarn
10841 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10842 "%<return%> with no value, in function returning non-void");
10843 else
10844 warned_here = warning_at
10845 (loc, OPT_Wreturn_type,
10846 "%<return%> with no value, in function returning non-void");
10847 no_warning = true;
10848 if (warned_here)
10849 inform (DECL_SOURCE_LOCATION (current_function_decl),
10850 "declared here");
10853 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10855 current_function_returns_null = 1;
10856 bool warned_here;
10857 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10858 warned_here = pedwarn
10859 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10860 "%<return%> with a value, in function returning void");
10861 else
10862 warned_here = pedwarn
10863 (xloc, OPT_Wpedantic, "ISO C forbids "
10864 "%<return%> with expression, in function returning void");
10865 if (warned_here)
10866 inform (DECL_SOURCE_LOCATION (current_function_decl),
10867 "declared here");
10869 else
10871 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10872 retval, origtype, ic_return,
10873 npc, NULL_TREE, NULL_TREE, 0);
10874 tree res = DECL_RESULT (current_function_decl);
10875 tree inner;
10876 bool save;
10878 current_function_returns_value = 1;
10879 if (t == error_mark_node)
10880 return NULL_TREE;
10882 save = in_late_binary_op;
10883 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10884 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10885 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10886 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10887 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10888 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10889 in_late_binary_op = true;
10890 inner = t = convert (TREE_TYPE (res), t);
10891 in_late_binary_op = save;
10893 /* Strip any conversions, additions, and subtractions, and see if
10894 we are returning the address of a local variable. Warn if so. */
10895 while (1)
10897 switch (TREE_CODE (inner))
10899 CASE_CONVERT:
10900 case NON_LVALUE_EXPR:
10901 case PLUS_EXPR:
10902 case POINTER_PLUS_EXPR:
10903 inner = TREE_OPERAND (inner, 0);
10904 continue;
10906 case MINUS_EXPR:
10907 /* If the second operand of the MINUS_EXPR has a pointer
10908 type (or is converted from it), this may be valid, so
10909 don't give a warning. */
10911 tree op1 = TREE_OPERAND (inner, 1);
10913 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10914 && (CONVERT_EXPR_P (op1)
10915 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10916 op1 = TREE_OPERAND (op1, 0);
10918 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10919 break;
10921 inner = TREE_OPERAND (inner, 0);
10922 continue;
10925 case ADDR_EXPR:
10926 inner = TREE_OPERAND (inner, 0);
10928 while (REFERENCE_CLASS_P (inner)
10929 && !INDIRECT_REF_P (inner))
10930 inner = TREE_OPERAND (inner, 0);
10932 if (DECL_P (inner)
10933 && !DECL_EXTERNAL (inner)
10934 && !TREE_STATIC (inner)
10935 && DECL_CONTEXT (inner) == current_function_decl
10936 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10938 if (TREE_CODE (inner) == LABEL_DECL)
10939 warning_at (loc, OPT_Wreturn_local_addr,
10940 "function returns address of label");
10941 else
10943 warning_at (loc, OPT_Wreturn_local_addr,
10944 "function returns address of local variable");
10945 tree zero = build_zero_cst (TREE_TYPE (res));
10946 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10949 break;
10951 default:
10952 break;
10955 break;
10958 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10959 SET_EXPR_LOCATION (retval, loc);
10961 if (warn_sequence_point)
10962 verify_sequence_points (retval);
10965 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10966 if (no_warning)
10967 suppress_warning (ret_stmt, OPT_Wreturn_type);
10968 return add_stmt (ret_stmt);
10971 struct c_switch {
10972 /* The SWITCH_STMT being built. */
10973 tree switch_stmt;
10975 /* The original type of the testing expression, i.e. before the
10976 default conversion is applied. */
10977 tree orig_type;
10979 /* A splay-tree mapping the low element of a case range to the high
10980 element, or NULL_TREE if there is no high element. Used to
10981 determine whether or not a new case label duplicates an old case
10982 label. We need a tree, rather than simply a hash table, because
10983 of the GNU case range extension. */
10984 splay_tree cases;
10986 /* The bindings at the point of the switch. This is used for
10987 warnings crossing decls when branching to a case label. */
10988 struct c_spot_bindings *bindings;
10990 /* Whether the switch includes any break statements. */
10991 bool break_stmt_seen_p;
10993 /* The next node on the stack. */
10994 struct c_switch *next;
10996 /* Remember whether the controlling expression had boolean type
10997 before integer promotions for the sake of -Wswitch-bool. */
10998 bool bool_cond_p;
11001 /* A stack of the currently active switch statements. The innermost
11002 switch statement is on the top of the stack. There is no need to
11003 mark the stack for garbage collection because it is only active
11004 during the processing of the body of a function, and we never
11005 collect at that point. */
11007 struct c_switch *c_switch_stack;
11009 /* Start a C switch statement, testing expression EXP. Return the new
11010 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11011 SWITCH_COND_LOC is the location of the switch's condition.
11012 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11014 tree
11015 c_start_switch (location_t switch_loc,
11016 location_t switch_cond_loc,
11017 tree exp, bool explicit_cast_p)
11019 tree orig_type = error_mark_node;
11020 bool bool_cond_p = false;
11021 struct c_switch *cs;
11023 if (exp != error_mark_node)
11025 orig_type = TREE_TYPE (exp);
11027 if (!INTEGRAL_TYPE_P (orig_type))
11029 if (orig_type != error_mark_node)
11031 error_at (switch_cond_loc, "switch quantity not an integer");
11032 orig_type = error_mark_node;
11034 exp = integer_zero_node;
11036 else
11038 tree type = TYPE_MAIN_VARIANT (orig_type);
11039 tree e = exp;
11041 /* Warn if the condition has boolean value. */
11042 while (TREE_CODE (e) == COMPOUND_EXPR)
11043 e = TREE_OPERAND (e, 1);
11045 if ((TREE_CODE (type) == BOOLEAN_TYPE
11046 || truth_value_p (TREE_CODE (e)))
11047 /* Explicit cast to int suppresses this warning. */
11048 && !(TREE_CODE (type) == INTEGER_TYPE
11049 && explicit_cast_p))
11050 bool_cond_p = true;
11052 if (!in_system_header_at (input_location)
11053 && (type == long_integer_type_node
11054 || type == long_unsigned_type_node))
11055 warning_at (switch_cond_loc,
11056 OPT_Wtraditional, "%<long%> switch expression not "
11057 "converted to %<int%> in ISO C");
11059 exp = c_fully_fold (exp, false, NULL);
11060 exp = default_conversion (exp);
11062 if (warn_sequence_point)
11063 verify_sequence_points (exp);
11067 /* Add this new SWITCH_STMT to the stack. */
11068 cs = XNEW (struct c_switch);
11069 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11070 NULL_TREE, orig_type, NULL_TREE);
11071 cs->orig_type = orig_type;
11072 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11073 cs->bindings = c_get_switch_bindings ();
11074 cs->break_stmt_seen_p = false;
11075 cs->bool_cond_p = bool_cond_p;
11076 cs->next = c_switch_stack;
11077 c_switch_stack = cs;
11079 return add_stmt (cs->switch_stmt);
11082 /* Process a case label at location LOC. */
11084 tree
11085 do_case (location_t loc, tree low_value, tree high_value)
11087 tree label = NULL_TREE;
11089 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11091 low_value = c_fully_fold (low_value, false, NULL);
11092 if (TREE_CODE (low_value) == INTEGER_CST)
11093 pedwarn (loc, OPT_Wpedantic,
11094 "case label is not an integer constant expression");
11097 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11099 high_value = c_fully_fold (high_value, false, NULL);
11100 if (TREE_CODE (high_value) == INTEGER_CST)
11101 pedwarn (input_location, OPT_Wpedantic,
11102 "case label is not an integer constant expression");
11105 if (c_switch_stack == NULL)
11107 if (low_value)
11108 error_at (loc, "case label not within a switch statement");
11109 else
11110 error_at (loc, "%<default%> label not within a switch statement");
11111 return NULL_TREE;
11114 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11115 EXPR_LOCATION (c_switch_stack->switch_stmt),
11116 loc))
11117 return NULL_TREE;
11119 label = c_add_case_label (loc, c_switch_stack->cases,
11120 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11121 low_value, high_value);
11122 if (label == error_mark_node)
11123 label = NULL_TREE;
11124 return label;
11127 /* Finish the switch statement. TYPE is the original type of the
11128 controlling expression of the switch, or NULL_TREE. */
11130 void
11131 c_finish_switch (tree body, tree type)
11133 struct c_switch *cs = c_switch_stack;
11134 location_t switch_location;
11136 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11138 /* Emit warnings as needed. */
11139 switch_location = EXPR_LOCATION (cs->switch_stmt);
11140 c_do_switch_warnings (cs->cases, switch_location,
11141 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11142 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11143 if (c_switch_covers_all_cases_p (cs->cases,
11144 SWITCH_STMT_TYPE (cs->switch_stmt)))
11145 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11146 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11148 /* Pop the stack. */
11149 c_switch_stack = cs->next;
11150 splay_tree_delete (cs->cases);
11151 c_release_switch_bindings (cs->bindings);
11152 XDELETE (cs);
11155 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11156 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11157 may be null. */
11159 void
11160 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11161 tree else_block)
11163 tree stmt;
11165 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11166 SET_EXPR_LOCATION (stmt, if_locus);
11167 add_stmt (stmt);
11170 tree
11171 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11173 /* In switch statements break is sometimes stylistically used after
11174 a return statement. This can lead to spurious warnings about
11175 control reaching the end of a non-void function when it is
11176 inlined. Note that we are calling block_may_fallthru with
11177 language specific tree nodes; this works because
11178 block_may_fallthru returns true when given something it does not
11179 understand. */
11180 bool skip = !block_may_fallthru (cur_stmt_list);
11182 if (is_break)
11183 switch (in_statement)
11185 case 0:
11186 error_at (loc, "break statement not within loop or switch");
11187 return NULL_TREE;
11188 case IN_OMP_BLOCK:
11189 error_at (loc, "invalid exit from OpenMP structured block");
11190 return NULL_TREE;
11191 case IN_OMP_FOR:
11192 error_at (loc, "break statement used with OpenMP for loop");
11193 return NULL_TREE;
11194 case IN_ITERATION_STMT:
11195 case IN_OBJC_FOREACH:
11196 break;
11197 default:
11198 gcc_assert (in_statement & IN_SWITCH_STMT);
11199 c_switch_stack->break_stmt_seen_p = true;
11200 break;
11202 else
11203 switch (in_statement & ~IN_SWITCH_STMT)
11205 case 0:
11206 error_at (loc, "continue statement not within a loop");
11207 return NULL_TREE;
11208 case IN_OMP_BLOCK:
11209 error_at (loc, "invalid exit from OpenMP structured block");
11210 return NULL_TREE;
11211 case IN_ITERATION_STMT:
11212 case IN_OMP_FOR:
11213 case IN_OBJC_FOREACH:
11214 break;
11215 default:
11216 gcc_unreachable ();
11219 if (skip)
11220 return NULL_TREE;
11221 else if (in_statement & IN_OBJC_FOREACH)
11223 /* The foreach expander produces low-level code using gotos instead
11224 of a structured loop construct. */
11225 gcc_assert (label);
11226 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11228 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11231 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11233 static void
11234 emit_side_effect_warnings (location_t loc, tree expr)
11236 maybe_warn_nodiscard (loc, expr);
11237 if (!warn_unused_value)
11238 return;
11239 if (expr == error_mark_node)
11241 else if (!TREE_SIDE_EFFECTS (expr))
11243 if (!VOID_TYPE_P (TREE_TYPE (expr))
11244 && !warning_suppressed_p (expr, OPT_Wunused_value))
11245 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11247 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11249 tree r = expr;
11250 location_t cloc = loc;
11251 while (TREE_CODE (r) == COMPOUND_EXPR)
11253 if (EXPR_HAS_LOCATION (r))
11254 cloc = EXPR_LOCATION (r);
11255 r = TREE_OPERAND (r, 1);
11257 if (!TREE_SIDE_EFFECTS (r)
11258 && !VOID_TYPE_P (TREE_TYPE (r))
11259 && !CONVERT_EXPR_P (r)
11260 && !warning_suppressed_p (r, OPT_Wunused_value)
11261 && !warning_suppressed_p (expr, OPT_Wunused_value))
11262 warning_at (cloc, OPT_Wunused_value,
11263 "right-hand operand of comma expression has no effect");
11265 else
11266 warn_if_unused_value (expr, loc);
11269 /* Process an expression as if it were a complete statement. Emit
11270 diagnostics, but do not call ADD_STMT. LOC is the location of the
11271 statement. */
11273 tree
11274 c_process_expr_stmt (location_t loc, tree expr)
11276 tree exprv;
11278 if (!expr)
11279 return NULL_TREE;
11281 expr = c_fully_fold (expr, false, NULL);
11283 if (warn_sequence_point)
11284 verify_sequence_points (expr);
11286 if (TREE_TYPE (expr) != error_mark_node
11287 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11288 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11289 error_at (loc, "expression statement has incomplete type");
11291 /* If we're not processing a statement expression, warn about unused values.
11292 Warnings for statement expressions will be emitted later, once we figure
11293 out which is the result. */
11294 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11295 && (warn_unused_value || warn_unused_result))
11296 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11298 exprv = expr;
11299 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11300 exprv = TREE_OPERAND (exprv, 1);
11301 while (CONVERT_EXPR_P (exprv))
11302 exprv = TREE_OPERAND (exprv, 0);
11303 if (DECL_P (exprv)
11304 || handled_component_p (exprv)
11305 || TREE_CODE (exprv) == ADDR_EXPR)
11306 mark_exp_read (exprv);
11308 /* If the expression is not of a type to which we cannot assign a line
11309 number, wrap the thing in a no-op NOP_EXPR. */
11310 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11312 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11313 SET_EXPR_LOCATION (expr, loc);
11316 return expr;
11319 /* Emit an expression as a statement. LOC is the location of the
11320 expression. */
11322 tree
11323 c_finish_expr_stmt (location_t loc, tree expr)
11325 if (expr)
11326 return add_stmt (c_process_expr_stmt (loc, expr));
11327 else
11328 return NULL;
11331 /* Do the opposite and emit a statement as an expression. To begin,
11332 create a new binding level and return it. */
11334 tree
11335 c_begin_stmt_expr (void)
11337 tree ret;
11339 /* We must force a BLOCK for this level so that, if it is not expanded
11340 later, there is a way to turn off the entire subtree of blocks that
11341 are contained in it. */
11342 keep_next_level ();
11343 ret = c_begin_compound_stmt (true);
11345 c_bindings_start_stmt_expr (c_switch_stack == NULL
11346 ? NULL
11347 : c_switch_stack->bindings);
11349 /* Mark the current statement list as belonging to a statement list. */
11350 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11352 return ret;
11355 /* LOC is the location of the compound statement to which this body
11356 belongs. */
11358 tree
11359 c_finish_stmt_expr (location_t loc, tree body)
11361 tree last, type, tmp, val;
11362 tree *last_p;
11364 body = c_end_compound_stmt (loc, body, true);
11366 c_bindings_end_stmt_expr (c_switch_stack == NULL
11367 ? NULL
11368 : c_switch_stack->bindings);
11370 /* Locate the last statement in BODY. See c_end_compound_stmt
11371 about always returning a BIND_EXPR. */
11372 last_p = &BIND_EXPR_BODY (body);
11373 last = BIND_EXPR_BODY (body);
11375 continue_searching:
11376 if (TREE_CODE (last) == STATEMENT_LIST)
11378 tree_stmt_iterator l = tsi_last (last);
11380 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11381 tsi_prev (&l);
11383 /* This can happen with degenerate cases like ({ }). No value. */
11384 if (tsi_end_p (l))
11385 return body;
11387 /* If we're supposed to generate side effects warnings, process
11388 all of the statements except the last. */
11389 if (warn_unused_value || warn_unused_result)
11391 for (tree_stmt_iterator i = tsi_start (last);
11392 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11394 location_t tloc;
11395 tree t = tsi_stmt (i);
11397 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11398 emit_side_effect_warnings (tloc, t);
11401 last_p = tsi_stmt_ptr (l);
11402 last = *last_p;
11405 /* If the end of the list is exception related, then the list was split
11406 by a call to push_cleanup. Continue searching. */
11407 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11408 || TREE_CODE (last) == TRY_CATCH_EXPR)
11410 last_p = &TREE_OPERAND (last, 0);
11411 last = *last_p;
11412 goto continue_searching;
11415 if (last == error_mark_node)
11416 return last;
11418 /* In the case that the BIND_EXPR is not necessary, return the
11419 expression out from inside it. */
11420 if ((last == BIND_EXPR_BODY (body)
11421 /* Skip nested debug stmts. */
11422 || last == expr_first (BIND_EXPR_BODY (body)))
11423 && BIND_EXPR_VARS (body) == NULL)
11425 /* Even if this looks constant, do not allow it in a constant
11426 expression. */
11427 last = c_wrap_maybe_const (last, true);
11428 /* Do not warn if the return value of a statement expression is
11429 unused. */
11430 suppress_warning (last, OPT_Wunused);
11431 return last;
11434 /* Extract the type of said expression. */
11435 type = TREE_TYPE (last);
11437 /* If we're not returning a value at all, then the BIND_EXPR that
11438 we already have is a fine expression to return. */
11439 if (!type || VOID_TYPE_P (type))
11440 return body;
11442 /* Now that we've located the expression containing the value, it seems
11443 silly to make voidify_wrapper_expr repeat the process. Create a
11444 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11445 tmp = create_tmp_var_raw (type);
11447 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11448 tree_expr_nonnegative_p giving up immediately. */
11449 val = last;
11450 if (TREE_CODE (val) == NOP_EXPR
11451 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11452 val = TREE_OPERAND (val, 0);
11454 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11455 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11458 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11459 SET_EXPR_LOCATION (t, loc);
11460 return t;
11464 /* Begin and end compound statements. This is as simple as pushing
11465 and popping new statement lists from the tree. */
11467 tree
11468 c_begin_compound_stmt (bool do_scope)
11470 tree stmt = push_stmt_list ();
11471 if (do_scope)
11472 push_scope ();
11473 return stmt;
11476 /* End a compound statement. STMT is the statement. LOC is the
11477 location of the compound statement-- this is usually the location
11478 of the opening brace. */
11480 tree
11481 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11483 tree block = NULL;
11485 if (do_scope)
11487 if (c_dialect_objc ())
11488 objc_clear_super_receiver ();
11489 block = pop_scope ();
11492 stmt = pop_stmt_list (stmt);
11493 stmt = c_build_bind_expr (loc, block, stmt);
11495 /* If this compound statement is nested immediately inside a statement
11496 expression, then force a BIND_EXPR to be created. Otherwise we'll
11497 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11498 STATEMENT_LISTs merge, and thus we can lose track of what statement
11499 was really last. */
11500 if (building_stmt_list_p ()
11501 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11502 && TREE_CODE (stmt) != BIND_EXPR)
11504 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11505 TREE_SIDE_EFFECTS (stmt) = 1;
11506 SET_EXPR_LOCATION (stmt, loc);
11509 return stmt;
11512 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11513 when the current scope is exited. EH_ONLY is true when this is not
11514 meant to apply to normal control flow transfer. */
11516 void
11517 push_cleanup (tree decl, tree cleanup, bool eh_only)
11519 enum tree_code code;
11520 tree stmt, list;
11521 bool stmt_expr;
11523 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11524 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11525 add_stmt (stmt);
11526 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11527 list = push_stmt_list ();
11528 TREE_OPERAND (stmt, 0) = list;
11529 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11532 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11533 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11535 static tree
11536 build_vec_cmp (tree_code code, tree type,
11537 tree arg0, tree arg1)
11539 tree zero_vec = build_zero_cst (type);
11540 tree minus_one_vec = build_minus_one_cst (type);
11541 tree cmp_type = truth_type_for (type);
11542 tree cmp = build2 (code, cmp_type, arg0, arg1);
11543 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11546 /* Build a binary-operation expression without default conversions.
11547 CODE is the kind of expression to build.
11548 LOCATION is the operator's location.
11549 This function differs from `build' in several ways:
11550 the data type of the result is computed and recorded in it,
11551 warnings are generated if arg data types are invalid,
11552 special handling for addition and subtraction of pointers is known,
11553 and some optimization is done (operations on narrow ints
11554 are done in the narrower type when that gives the same result).
11555 Constant folding is also done before the result is returned.
11557 Note that the operands will never have enumeral types, or function
11558 or array types, because either they will have the default conversions
11559 performed or they have both just been converted to some other type in which
11560 the arithmetic is to be done. */
11562 tree
11563 build_binary_op (location_t location, enum tree_code code,
11564 tree orig_op0, tree orig_op1, bool convert_p)
11566 tree type0, type1, orig_type0, orig_type1;
11567 tree eptype;
11568 enum tree_code code0, code1;
11569 tree op0, op1;
11570 tree ret = error_mark_node;
11571 const char *invalid_op_diag;
11572 bool op0_int_operands, op1_int_operands;
11573 bool int_const, int_const_or_overflow, int_operands;
11575 /* Expression code to give to the expression when it is built.
11576 Normally this is CODE, which is what the caller asked for,
11577 but in some special cases we change it. */
11578 enum tree_code resultcode = code;
11580 /* Data type in which the computation is to be performed.
11581 In the simplest cases this is the common type of the arguments. */
11582 tree result_type = NULL;
11584 /* When the computation is in excess precision, the type of the
11585 final EXCESS_PRECISION_EXPR. */
11586 tree semantic_result_type = NULL;
11588 /* Nonzero means operands have already been type-converted
11589 in whatever way is necessary.
11590 Zero means they need to be converted to RESULT_TYPE. */
11591 int converted = 0;
11593 /* Nonzero means create the expression with this type, rather than
11594 RESULT_TYPE. */
11595 tree build_type = NULL_TREE;
11597 /* Nonzero means after finally constructing the expression
11598 convert it to this type. */
11599 tree final_type = NULL_TREE;
11601 /* Nonzero if this is an operation like MIN or MAX which can
11602 safely be computed in short if both args are promoted shorts.
11603 Also implies COMMON.
11604 -1 indicates a bitwise operation; this makes a difference
11605 in the exact conditions for when it is safe to do the operation
11606 in a narrower mode. */
11607 int shorten = 0;
11609 /* Nonzero if this is a comparison operation;
11610 if both args are promoted shorts, compare the original shorts.
11611 Also implies COMMON. */
11612 int short_compare = 0;
11614 /* Nonzero if this is a right-shift operation, which can be computed on the
11615 original short and then promoted if the operand is a promoted short. */
11616 int short_shift = 0;
11618 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11619 int common = 0;
11621 /* True means types are compatible as far as ObjC is concerned. */
11622 bool objc_ok;
11624 /* True means this is an arithmetic operation that may need excess
11625 precision. */
11626 bool may_need_excess_precision;
11628 /* True means this is a boolean operation that converts both its
11629 operands to truth-values. */
11630 bool boolean_op = false;
11632 /* Remember whether we're doing / or %. */
11633 bool doing_div_or_mod = false;
11635 /* Remember whether we're doing << or >>. */
11636 bool doing_shift = false;
11638 /* Tree holding instrumentation expression. */
11639 tree instrument_expr = NULL;
11641 if (location == UNKNOWN_LOCATION)
11642 location = input_location;
11644 op0 = orig_op0;
11645 op1 = orig_op1;
11647 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11648 if (op0_int_operands)
11649 op0 = remove_c_maybe_const_expr (op0);
11650 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11651 if (op1_int_operands)
11652 op1 = remove_c_maybe_const_expr (op1);
11653 int_operands = (op0_int_operands && op1_int_operands);
11654 if (int_operands)
11656 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11657 && TREE_CODE (orig_op1) == INTEGER_CST);
11658 int_const = (int_const_or_overflow
11659 && !TREE_OVERFLOW (orig_op0)
11660 && !TREE_OVERFLOW (orig_op1));
11662 else
11663 int_const = int_const_or_overflow = false;
11665 /* Do not apply default conversion in mixed vector/scalar expression. */
11666 if (convert_p
11667 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11669 op0 = default_conversion (op0);
11670 op1 = default_conversion (op1);
11673 orig_type0 = type0 = TREE_TYPE (op0);
11675 orig_type1 = type1 = TREE_TYPE (op1);
11677 /* The expression codes of the data types of the arguments tell us
11678 whether the arguments are integers, floating, pointers, etc. */
11679 code0 = TREE_CODE (type0);
11680 code1 = TREE_CODE (type1);
11682 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11683 STRIP_TYPE_NOPS (op0);
11684 STRIP_TYPE_NOPS (op1);
11686 /* If an error was already reported for one of the arguments,
11687 avoid reporting another error. */
11689 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11690 return error_mark_node;
11692 if (code0 == POINTER_TYPE
11693 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11694 return error_mark_node;
11696 if (code1 == POINTER_TYPE
11697 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11698 return error_mark_node;
11700 if ((invalid_op_diag
11701 = targetm.invalid_binary_op (code, type0, type1)))
11703 error_at (location, invalid_op_diag);
11704 return error_mark_node;
11707 switch (code)
11709 case PLUS_EXPR:
11710 case MINUS_EXPR:
11711 case MULT_EXPR:
11712 case TRUNC_DIV_EXPR:
11713 case CEIL_DIV_EXPR:
11714 case FLOOR_DIV_EXPR:
11715 case ROUND_DIV_EXPR:
11716 case EXACT_DIV_EXPR:
11717 may_need_excess_precision = true;
11718 break;
11720 case EQ_EXPR:
11721 case NE_EXPR:
11722 case LE_EXPR:
11723 case GE_EXPR:
11724 case LT_EXPR:
11725 case GT_EXPR:
11726 /* Excess precision for implicit conversions of integers to
11727 floating point in C11 and later. */
11728 may_need_excess_precision = (flag_isoc11
11729 && (ANY_INTEGRAL_TYPE_P (type0)
11730 || ANY_INTEGRAL_TYPE_P (type1)));
11731 break;
11733 default:
11734 may_need_excess_precision = false;
11735 break;
11737 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11739 op0 = TREE_OPERAND (op0, 0);
11740 type0 = TREE_TYPE (op0);
11742 else if (may_need_excess_precision
11743 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11745 type0 = eptype;
11746 op0 = convert (eptype, op0);
11748 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11750 op1 = TREE_OPERAND (op1, 0);
11751 type1 = TREE_TYPE (op1);
11753 else if (may_need_excess_precision
11754 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11756 type1 = eptype;
11757 op1 = convert (eptype, op1);
11760 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11762 /* In case when one of the operands of the binary operation is
11763 a vector and another is a scalar -- convert scalar to vector. */
11764 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11765 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11767 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11768 true);
11770 switch (convert_flag)
11772 case stv_error:
11773 return error_mark_node;
11774 case stv_firstarg:
11776 bool maybe_const = true;
11777 tree sc;
11778 sc = c_fully_fold (op0, false, &maybe_const);
11779 sc = save_expr (sc);
11780 sc = convert (TREE_TYPE (type1), sc);
11781 op0 = build_vector_from_val (type1, sc);
11782 if (!maybe_const)
11783 op0 = c_wrap_maybe_const (op0, true);
11784 orig_type0 = type0 = TREE_TYPE (op0);
11785 code0 = TREE_CODE (type0);
11786 converted = 1;
11787 break;
11789 case stv_secondarg:
11791 bool maybe_const = true;
11792 tree sc;
11793 sc = c_fully_fold (op1, false, &maybe_const);
11794 sc = save_expr (sc);
11795 sc = convert (TREE_TYPE (type0), sc);
11796 op1 = build_vector_from_val (type0, sc);
11797 if (!maybe_const)
11798 op1 = c_wrap_maybe_const (op1, true);
11799 orig_type1 = type1 = TREE_TYPE (op1);
11800 code1 = TREE_CODE (type1);
11801 converted = 1;
11802 break;
11804 default:
11805 break;
11809 switch (code)
11811 case PLUS_EXPR:
11812 /* Handle the pointer + int case. */
11813 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11815 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11816 goto return_build_binary_op;
11818 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11820 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11821 goto return_build_binary_op;
11823 else
11824 common = 1;
11825 break;
11827 case MINUS_EXPR:
11828 /* Subtraction of two similar pointers.
11829 We must subtract them as integers, then divide by object size. */
11830 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11831 && comp_target_types (location, type0, type1))
11833 ret = pointer_diff (location, op0, op1, &instrument_expr);
11834 goto return_build_binary_op;
11836 /* Handle pointer minus int. Just like pointer plus int. */
11837 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11839 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11840 goto return_build_binary_op;
11842 else
11843 common = 1;
11844 break;
11846 case MULT_EXPR:
11847 common = 1;
11848 break;
11850 case TRUNC_DIV_EXPR:
11851 case CEIL_DIV_EXPR:
11852 case FLOOR_DIV_EXPR:
11853 case ROUND_DIV_EXPR:
11854 case EXACT_DIV_EXPR:
11855 doing_div_or_mod = true;
11856 warn_for_div_by_zero (location, op1);
11858 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11859 || code0 == FIXED_POINT_TYPE
11860 || code0 == COMPLEX_TYPE
11861 || gnu_vector_type_p (type0))
11862 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11863 || code1 == FIXED_POINT_TYPE
11864 || code1 == COMPLEX_TYPE
11865 || gnu_vector_type_p (type1)))
11867 enum tree_code tcode0 = code0, tcode1 = code1;
11869 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11870 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11871 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11872 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11874 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11875 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11876 resultcode = RDIV_EXPR;
11877 else
11878 /* Although it would be tempting to shorten always here, that
11879 loses on some targets, since the modulo instruction is
11880 undefined if the quotient can't be represented in the
11881 computation mode. We shorten only if unsigned or if
11882 dividing by something we know != -1. */
11883 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11884 || (TREE_CODE (op1) == INTEGER_CST
11885 && !integer_all_onesp (op1)));
11886 common = 1;
11888 break;
11890 case BIT_AND_EXPR:
11891 case BIT_IOR_EXPR:
11892 case BIT_XOR_EXPR:
11893 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11894 shorten = -1;
11895 /* Allow vector types which are not floating point types. */
11896 else if (gnu_vector_type_p (type0)
11897 && gnu_vector_type_p (type1)
11898 && !VECTOR_FLOAT_TYPE_P (type0)
11899 && !VECTOR_FLOAT_TYPE_P (type1))
11900 common = 1;
11901 break;
11903 case TRUNC_MOD_EXPR:
11904 case FLOOR_MOD_EXPR:
11905 doing_div_or_mod = true;
11906 warn_for_div_by_zero (location, op1);
11908 if (gnu_vector_type_p (type0)
11909 && gnu_vector_type_p (type1)
11910 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11911 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11912 common = 1;
11913 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11915 /* Although it would be tempting to shorten always here, that loses
11916 on some targets, since the modulo instruction is undefined if the
11917 quotient can't be represented in the computation mode. We shorten
11918 only if unsigned or if dividing by something we know != -1. */
11919 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11920 || (TREE_CODE (op1) == INTEGER_CST
11921 && !integer_all_onesp (op1)));
11922 common = 1;
11924 break;
11926 case TRUTH_ANDIF_EXPR:
11927 case TRUTH_ORIF_EXPR:
11928 case TRUTH_AND_EXPR:
11929 case TRUTH_OR_EXPR:
11930 case TRUTH_XOR_EXPR:
11931 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11932 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11933 || code0 == FIXED_POINT_TYPE)
11934 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11935 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11936 || code1 == FIXED_POINT_TYPE))
11938 /* Result of these operations is always an int,
11939 but that does not mean the operands should be
11940 converted to ints! */
11941 result_type = integer_type_node;
11942 if (op0_int_operands)
11944 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11945 op0 = remove_c_maybe_const_expr (op0);
11947 else
11948 op0 = c_objc_common_truthvalue_conversion (location, op0);
11949 if (op1_int_operands)
11951 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11952 op1 = remove_c_maybe_const_expr (op1);
11954 else
11955 op1 = c_objc_common_truthvalue_conversion (location, op1);
11956 converted = 1;
11957 boolean_op = true;
11959 if (code == TRUTH_ANDIF_EXPR)
11961 int_const_or_overflow = (int_operands
11962 && TREE_CODE (orig_op0) == INTEGER_CST
11963 && (op0 == truthvalue_false_node
11964 || TREE_CODE (orig_op1) == INTEGER_CST));
11965 int_const = (int_const_or_overflow
11966 && !TREE_OVERFLOW (orig_op0)
11967 && (op0 == truthvalue_false_node
11968 || !TREE_OVERFLOW (orig_op1)));
11970 else if (code == TRUTH_ORIF_EXPR)
11972 int_const_or_overflow = (int_operands
11973 && TREE_CODE (orig_op0) == INTEGER_CST
11974 && (op0 == truthvalue_true_node
11975 || TREE_CODE (orig_op1) == INTEGER_CST));
11976 int_const = (int_const_or_overflow
11977 && !TREE_OVERFLOW (orig_op0)
11978 && (op0 == truthvalue_true_node
11979 || !TREE_OVERFLOW (orig_op1)));
11981 break;
11983 /* Shift operations: result has same type as first operand;
11984 always convert second operand to int.
11985 Also set SHORT_SHIFT if shifting rightward. */
11987 case RSHIFT_EXPR:
11988 if (gnu_vector_type_p (type0)
11989 && gnu_vector_type_p (type1)
11990 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11991 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11992 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11993 TYPE_VECTOR_SUBPARTS (type1)))
11995 result_type = type0;
11996 converted = 1;
11998 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11999 || (gnu_vector_type_p (type0)
12000 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12001 && code1 == INTEGER_TYPE)
12003 doing_shift = true;
12004 if (TREE_CODE (op1) == INTEGER_CST)
12006 if (tree_int_cst_sgn (op1) < 0)
12008 int_const = false;
12009 if (c_inhibit_evaluation_warnings == 0)
12010 warning_at (location, OPT_Wshift_count_negative,
12011 "right shift count is negative");
12013 else if (code0 == VECTOR_TYPE)
12015 if (compare_tree_int (op1,
12016 TYPE_PRECISION (TREE_TYPE (type0)))
12017 >= 0)
12019 int_const = false;
12020 if (c_inhibit_evaluation_warnings == 0)
12021 warning_at (location, OPT_Wshift_count_overflow,
12022 "right shift count >= width of vector element");
12025 else
12027 if (!integer_zerop (op1))
12028 short_shift = 1;
12030 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12032 int_const = false;
12033 if (c_inhibit_evaluation_warnings == 0)
12034 warning_at (location, OPT_Wshift_count_overflow,
12035 "right shift count >= width of type");
12040 /* Use the type of the value to be shifted. */
12041 result_type = type0;
12042 /* Avoid converting op1 to result_type later. */
12043 converted = 1;
12045 break;
12047 case LSHIFT_EXPR:
12048 if (gnu_vector_type_p (type0)
12049 && gnu_vector_type_p (type1)
12050 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12051 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12052 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12053 TYPE_VECTOR_SUBPARTS (type1)))
12055 result_type = type0;
12056 converted = 1;
12058 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12059 || (gnu_vector_type_p (type0)
12060 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12061 && code1 == INTEGER_TYPE)
12063 doing_shift = true;
12064 if (TREE_CODE (op0) == INTEGER_CST
12065 && tree_int_cst_sgn (op0) < 0)
12067 /* Don't reject a left shift of a negative value in a context
12068 where a constant expression is needed in C90. */
12069 if (flag_isoc99)
12070 int_const = false;
12071 if (c_inhibit_evaluation_warnings == 0)
12072 warning_at (location, OPT_Wshift_negative_value,
12073 "left shift of negative value");
12075 if (TREE_CODE (op1) == INTEGER_CST)
12077 if (tree_int_cst_sgn (op1) < 0)
12079 int_const = false;
12080 if (c_inhibit_evaluation_warnings == 0)
12081 warning_at (location, OPT_Wshift_count_negative,
12082 "left shift count is negative");
12084 else if (code0 == VECTOR_TYPE)
12086 if (compare_tree_int (op1,
12087 TYPE_PRECISION (TREE_TYPE (type0)))
12088 >= 0)
12090 int_const = false;
12091 if (c_inhibit_evaluation_warnings == 0)
12092 warning_at (location, OPT_Wshift_count_overflow,
12093 "left shift count >= width of vector element");
12096 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12098 int_const = false;
12099 if (c_inhibit_evaluation_warnings == 0)
12100 warning_at (location, OPT_Wshift_count_overflow,
12101 "left shift count >= width of type");
12103 else if (TREE_CODE (op0) == INTEGER_CST
12104 && maybe_warn_shift_overflow (location, op0, op1)
12105 && flag_isoc99)
12106 int_const = false;
12109 /* Use the type of the value to be shifted. */
12110 result_type = type0;
12111 /* Avoid converting op1 to result_type later. */
12112 converted = 1;
12114 break;
12116 case EQ_EXPR:
12117 case NE_EXPR:
12118 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12120 tree intt;
12121 if (!vector_types_compatible_elements_p (type0, type1))
12123 error_at (location, "comparing vectors with different "
12124 "element types");
12125 return error_mark_node;
12128 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12129 TYPE_VECTOR_SUBPARTS (type1)))
12131 error_at (location, "comparing vectors with different "
12132 "number of elements");
12133 return error_mark_node;
12136 /* It's not precisely specified how the usual arithmetic
12137 conversions apply to the vector types. Here, we use
12138 the unsigned type if one of the operands is signed and
12139 the other one is unsigned. */
12140 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12142 if (!TYPE_UNSIGNED (type0))
12143 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12144 else
12145 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12146 warning_at (location, OPT_Wsign_compare, "comparison between "
12147 "types %qT and %qT", type0, type1);
12150 /* Always construct signed integer vector type. */
12151 intt = c_common_type_for_size (GET_MODE_BITSIZE
12152 (SCALAR_TYPE_MODE
12153 (TREE_TYPE (type0))), 0);
12154 if (!intt)
12156 error_at (location, "could not find an integer type "
12157 "of the same size as %qT",
12158 TREE_TYPE (type0));
12159 return error_mark_node;
12161 result_type = build_opaque_vector_type (intt,
12162 TYPE_VECTOR_SUBPARTS (type0));
12163 converted = 1;
12164 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12165 goto return_build_binary_op;
12167 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12168 warning_at (location,
12169 OPT_Wfloat_equal,
12170 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12171 /* Result of comparison is always int,
12172 but don't convert the args to int! */
12173 build_type = integer_type_node;
12174 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12175 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12176 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12177 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12178 short_compare = 1;
12179 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12181 if (TREE_CODE (op0) == ADDR_EXPR
12182 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
12183 && !from_macro_expansion_at (location))
12185 if (code == EQ_EXPR)
12186 warning_at (location,
12187 OPT_Waddress,
12188 "the comparison will always evaluate as %<false%> "
12189 "for the address of %qD will never be NULL",
12190 TREE_OPERAND (op0, 0));
12191 else
12192 warning_at (location,
12193 OPT_Waddress,
12194 "the comparison will always evaluate as %<true%> "
12195 "for the address of %qD will never be NULL",
12196 TREE_OPERAND (op0, 0));
12198 result_type = type0;
12200 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12202 if (TREE_CODE (op1) == ADDR_EXPR
12203 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
12204 && !from_macro_expansion_at (location))
12206 if (code == EQ_EXPR)
12207 warning_at (location,
12208 OPT_Waddress,
12209 "the comparison will always evaluate as %<false%> "
12210 "for the address of %qD will never be NULL",
12211 TREE_OPERAND (op1, 0));
12212 else
12213 warning_at (location,
12214 OPT_Waddress,
12215 "the comparison will always evaluate as %<true%> "
12216 "for the address of %qD will never be NULL",
12217 TREE_OPERAND (op1, 0));
12219 result_type = type1;
12221 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12223 tree tt0 = TREE_TYPE (type0);
12224 tree tt1 = TREE_TYPE (type1);
12225 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12226 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12227 addr_space_t as_common = ADDR_SPACE_GENERIC;
12229 /* Anything compares with void *. void * compares with anything.
12230 Otherwise, the targets must be compatible
12231 and both must be object or both incomplete. */
12232 if (comp_target_types (location, type0, type1))
12233 result_type = common_pointer_type (type0, type1);
12234 else if (!addr_space_superset (as0, as1, &as_common))
12236 error_at (location, "comparison of pointers to "
12237 "disjoint address spaces");
12238 return error_mark_node;
12240 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12242 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12243 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12244 "comparison of %<void *%> with function pointer");
12246 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12248 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12249 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12250 "comparison of %<void *%> with function pointer");
12252 else
12253 /* Avoid warning about the volatile ObjC EH puts on decls. */
12254 if (!objc_ok)
12255 pedwarn (location, 0,
12256 "comparison of distinct pointer types lacks a cast");
12258 if (result_type == NULL_TREE)
12260 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12261 result_type = build_pointer_type
12262 (build_qualified_type (void_type_node, qual));
12265 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12267 result_type = type0;
12268 pedwarn (location, 0, "comparison between pointer and integer");
12270 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12272 result_type = type1;
12273 pedwarn (location, 0, "comparison between pointer and integer");
12275 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12276 || truth_value_p (TREE_CODE (orig_op0)))
12277 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12278 || truth_value_p (TREE_CODE (orig_op1))))
12279 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12280 break;
12282 case LE_EXPR:
12283 case GE_EXPR:
12284 case LT_EXPR:
12285 case GT_EXPR:
12286 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12288 tree intt;
12289 if (!vector_types_compatible_elements_p (type0, type1))
12291 error_at (location, "comparing vectors with different "
12292 "element types");
12293 return error_mark_node;
12296 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12297 TYPE_VECTOR_SUBPARTS (type1)))
12299 error_at (location, "comparing vectors with different "
12300 "number of elements");
12301 return error_mark_node;
12304 /* It's not precisely specified how the usual arithmetic
12305 conversions apply to the vector types. Here, we use
12306 the unsigned type if one of the operands is signed and
12307 the other one is unsigned. */
12308 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12310 if (!TYPE_UNSIGNED (type0))
12311 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12312 else
12313 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12314 warning_at (location, OPT_Wsign_compare, "comparison between "
12315 "types %qT and %qT", type0, type1);
12318 /* Always construct signed integer vector type. */
12319 intt = c_common_type_for_size (GET_MODE_BITSIZE
12320 (SCALAR_TYPE_MODE
12321 (TREE_TYPE (type0))), 0);
12322 if (!intt)
12324 error_at (location, "could not find an integer type "
12325 "of the same size as %qT",
12326 TREE_TYPE (type0));
12327 return error_mark_node;
12329 result_type = build_opaque_vector_type (intt,
12330 TYPE_VECTOR_SUBPARTS (type0));
12331 converted = 1;
12332 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12333 goto return_build_binary_op;
12335 build_type = integer_type_node;
12336 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12337 || code0 == FIXED_POINT_TYPE)
12338 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12339 || code1 == FIXED_POINT_TYPE))
12340 short_compare = 1;
12341 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12343 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12344 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12345 addr_space_t as_common;
12347 if (comp_target_types (location, type0, type1))
12349 result_type = common_pointer_type (type0, type1);
12350 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12351 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12352 pedwarn_c99 (location, OPT_Wpedantic,
12353 "comparison of complete and incomplete pointers");
12354 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12355 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12356 "ordered comparisons of pointers to functions");
12357 else if (null_pointer_constant_p (orig_op0)
12358 || null_pointer_constant_p (orig_op1))
12359 warning_at (location, OPT_Wextra,
12360 "ordered comparison of pointer with null pointer");
12363 else if (!addr_space_superset (as0, as1, &as_common))
12365 error_at (location, "comparison of pointers to "
12366 "disjoint address spaces");
12367 return error_mark_node;
12369 else
12371 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12372 result_type = build_pointer_type
12373 (build_qualified_type (void_type_node, qual));
12374 pedwarn (location, 0,
12375 "comparison of distinct pointer types lacks a cast");
12378 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12380 result_type = type0;
12381 if (pedantic)
12382 pedwarn (location, OPT_Wpedantic,
12383 "ordered comparison of pointer with integer zero");
12384 else if (extra_warnings)
12385 warning_at (location, OPT_Wextra,
12386 "ordered comparison of pointer with integer zero");
12388 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12390 result_type = type1;
12391 if (pedantic)
12392 pedwarn (location, OPT_Wpedantic,
12393 "ordered comparison of pointer with integer zero");
12394 else if (extra_warnings)
12395 warning_at (location, OPT_Wextra,
12396 "ordered comparison of pointer with integer zero");
12398 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12400 result_type = type0;
12401 pedwarn (location, 0, "comparison between pointer and integer");
12403 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12405 result_type = type1;
12406 pedwarn (location, 0, "comparison between pointer and integer");
12409 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12410 && current_function_decl != NULL_TREE
12411 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12413 op0 = save_expr (op0);
12414 op1 = save_expr (op1);
12416 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12417 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12420 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12421 || truth_value_p (TREE_CODE (orig_op0)))
12422 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12423 || truth_value_p (TREE_CODE (orig_op1))))
12424 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12425 break;
12427 default:
12428 gcc_unreachable ();
12431 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12432 return error_mark_node;
12434 if (gnu_vector_type_p (type0)
12435 && gnu_vector_type_p (type1)
12436 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12437 || !vector_types_compatible_elements_p (type0, type1)))
12439 gcc_rich_location richloc (location);
12440 maybe_range_label_for_tree_type_mismatch
12441 label_for_op0 (orig_op0, orig_op1),
12442 label_for_op1 (orig_op1, orig_op0);
12443 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12444 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12445 binary_op_error (&richloc, code, type0, type1);
12446 return error_mark_node;
12449 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12450 || code0 == FIXED_POINT_TYPE
12451 || gnu_vector_type_p (type0))
12453 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12454 || code1 == FIXED_POINT_TYPE
12455 || gnu_vector_type_p (type1)))
12457 bool first_complex = (code0 == COMPLEX_TYPE);
12458 bool second_complex = (code1 == COMPLEX_TYPE);
12459 int none_complex = (!first_complex && !second_complex);
12461 if (shorten || common || short_compare)
12463 result_type = c_common_type (type0, type1);
12464 do_warn_double_promotion (result_type, type0, type1,
12465 "implicit conversion from %qT to %qT "
12466 "to match other operand of binary "
12467 "expression",
12468 location);
12469 if (result_type == error_mark_node)
12470 return error_mark_node;
12473 if (first_complex != second_complex
12474 && (code == PLUS_EXPR
12475 || code == MINUS_EXPR
12476 || code == MULT_EXPR
12477 || (code == TRUNC_DIV_EXPR && first_complex))
12478 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12479 && flag_signed_zeros)
12481 /* An operation on mixed real/complex operands must be
12482 handled specially, but the language-independent code can
12483 more easily optimize the plain complex arithmetic if
12484 -fno-signed-zeros. */
12485 tree real_type = TREE_TYPE (result_type);
12486 tree real, imag;
12487 if (type0 != orig_type0 || type1 != orig_type1)
12489 gcc_assert (may_need_excess_precision && common);
12490 semantic_result_type = c_common_type (orig_type0, orig_type1);
12492 if (first_complex)
12494 if (TREE_TYPE (op0) != result_type)
12495 op0 = convert_and_check (location, result_type, op0);
12496 if (TREE_TYPE (op1) != real_type)
12497 op1 = convert_and_check (location, real_type, op1);
12499 else
12501 if (TREE_TYPE (op0) != real_type)
12502 op0 = convert_and_check (location, real_type, op0);
12503 if (TREE_TYPE (op1) != result_type)
12504 op1 = convert_and_check (location, result_type, op1);
12506 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12507 return error_mark_node;
12508 if (first_complex)
12510 op0 = save_expr (op0);
12511 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12512 op0, true);
12513 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12514 op0, true);
12515 switch (code)
12517 case MULT_EXPR:
12518 case TRUNC_DIV_EXPR:
12519 op1 = save_expr (op1);
12520 imag = build2 (resultcode, real_type, imag, op1);
12521 /* Fall through. */
12522 case PLUS_EXPR:
12523 case MINUS_EXPR:
12524 real = build2 (resultcode, real_type, real, op1);
12525 break;
12526 default:
12527 gcc_unreachable();
12530 else
12532 op1 = save_expr (op1);
12533 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12534 op1, true);
12535 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12536 op1, true);
12537 switch (code)
12539 case MULT_EXPR:
12540 op0 = save_expr (op0);
12541 imag = build2 (resultcode, real_type, op0, imag);
12542 /* Fall through. */
12543 case PLUS_EXPR:
12544 real = build2 (resultcode, real_type, op0, real);
12545 break;
12546 case MINUS_EXPR:
12547 real = build2 (resultcode, real_type, op0, real);
12548 imag = build1 (NEGATE_EXPR, real_type, imag);
12549 break;
12550 default:
12551 gcc_unreachable();
12554 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12555 goto return_build_binary_op;
12558 /* For certain operations (which identify themselves by shorten != 0)
12559 if both args were extended from the same smaller type,
12560 do the arithmetic in that type and then extend.
12562 shorten !=0 and !=1 indicates a bitwise operation.
12563 For them, this optimization is safe only if
12564 both args are zero-extended or both are sign-extended.
12565 Otherwise, we might change the result.
12566 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12567 but calculated in (unsigned short) it would be (unsigned short)-1. */
12569 if (shorten && none_complex)
12571 final_type = result_type;
12572 result_type = shorten_binary_op (result_type, op0, op1,
12573 shorten == -1);
12576 /* Shifts can be shortened if shifting right. */
12578 if (short_shift)
12580 int unsigned_arg;
12581 tree arg0 = get_narrower (op0, &unsigned_arg);
12583 final_type = result_type;
12585 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12586 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12588 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12589 && tree_int_cst_sgn (op1) > 0
12590 /* We can shorten only if the shift count is less than the
12591 number of bits in the smaller type size. */
12592 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12593 /* We cannot drop an unsigned shift after sign-extension. */
12594 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12596 /* Do an unsigned shift if the operand was zero-extended. */
12597 result_type
12598 = c_common_signed_or_unsigned_type (unsigned_arg,
12599 TREE_TYPE (arg0));
12600 /* Convert value-to-be-shifted to that type. */
12601 if (TREE_TYPE (op0) != result_type)
12602 op0 = convert (result_type, op0);
12603 converted = 1;
12607 /* Comparison operations are shortened too but differently.
12608 They identify themselves by setting short_compare = 1. */
12610 if (short_compare)
12612 /* Don't write &op0, etc., because that would prevent op0
12613 from being kept in a register.
12614 Instead, make copies of the our local variables and
12615 pass the copies by reference, then copy them back afterward. */
12616 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12617 enum tree_code xresultcode = resultcode;
12618 tree val
12619 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12620 &xresultcode);
12622 if (val != NULL_TREE)
12624 ret = val;
12625 goto return_build_binary_op;
12628 op0 = xop0, op1 = xop1;
12629 converted = 1;
12630 resultcode = xresultcode;
12632 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12634 bool op0_maybe_const = true;
12635 bool op1_maybe_const = true;
12636 tree orig_op0_folded, orig_op1_folded;
12638 if (in_late_binary_op)
12640 orig_op0_folded = orig_op0;
12641 orig_op1_folded = orig_op1;
12643 else
12645 /* Fold for the sake of possible warnings, as in
12646 build_conditional_expr. This requires the
12647 "original" values to be folded, not just op0 and
12648 op1. */
12649 c_inhibit_evaluation_warnings++;
12650 op0 = c_fully_fold (op0, require_constant_value,
12651 &op0_maybe_const);
12652 op1 = c_fully_fold (op1, require_constant_value,
12653 &op1_maybe_const);
12654 c_inhibit_evaluation_warnings--;
12655 orig_op0_folded = c_fully_fold (orig_op0,
12656 require_constant_value,
12657 NULL);
12658 orig_op1_folded = c_fully_fold (orig_op1,
12659 require_constant_value,
12660 NULL);
12663 if (warn_sign_compare)
12664 warn_for_sign_compare (location, orig_op0_folded,
12665 orig_op1_folded, op0, op1,
12666 result_type, resultcode);
12667 if (!in_late_binary_op && !int_operands)
12669 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12670 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12671 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12672 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12678 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12679 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12680 Then the expression will be built.
12681 It will be given type FINAL_TYPE if that is nonzero;
12682 otherwise, it will be given type RESULT_TYPE. */
12684 if (!result_type)
12686 /* Favor showing any expression locations that are available. */
12687 op_location_t oploc (location, UNKNOWN_LOCATION);
12688 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12689 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12690 return error_mark_node;
12693 if (build_type == NULL_TREE)
12695 build_type = result_type;
12696 if ((type0 != orig_type0 || type1 != orig_type1)
12697 && !boolean_op)
12699 gcc_assert (may_need_excess_precision && common);
12700 semantic_result_type = c_common_type (orig_type0, orig_type1);
12704 if (!converted)
12706 op0 = ep_convert_and_check (location, result_type, op0,
12707 semantic_result_type);
12708 op1 = ep_convert_and_check (location, result_type, op1,
12709 semantic_result_type);
12711 /* This can happen if one operand has a vector type, and the other
12712 has a different type. */
12713 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12714 return error_mark_node;
12717 if (sanitize_flags_p ((SANITIZE_SHIFT
12718 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12719 && current_function_decl != NULL_TREE
12720 && (doing_div_or_mod || doing_shift)
12721 && !require_constant_value)
12723 /* OP0 and/or OP1 might have side-effects. */
12724 op0 = save_expr (op0);
12725 op1 = save_expr (op1);
12726 op0 = c_fully_fold (op0, false, NULL);
12727 op1 = c_fully_fold (op1, false, NULL);
12728 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12729 | SANITIZE_FLOAT_DIVIDE))))
12730 instrument_expr = ubsan_instrument_division (location, op0, op1);
12731 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12732 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12735 /* Treat expressions in initializers specially as they can't trap. */
12736 if (int_const_or_overflow)
12737 ret = (require_constant_value
12738 ? fold_build2_initializer_loc (location, resultcode, build_type,
12739 op0, op1)
12740 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12741 else
12742 ret = build2 (resultcode, build_type, op0, op1);
12743 if (final_type != NULL_TREE)
12744 ret = convert (final_type, ret);
12746 return_build_binary_op:
12747 gcc_assert (ret != error_mark_node);
12748 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12749 ret = (int_operands
12750 ? note_integer_operands (ret)
12751 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12752 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12753 && !in_late_binary_op)
12754 ret = note_integer_operands (ret);
12755 protected_set_expr_location (ret, location);
12757 if (instrument_expr != NULL)
12758 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12759 instrument_expr, ret);
12761 if (semantic_result_type)
12762 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12763 semantic_result_type, ret);
12765 return ret;
12769 /* Convert EXPR to be a truth-value, validating its type for this
12770 purpose. LOCATION is the source location for the expression. */
12772 tree
12773 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12775 bool int_const, int_operands;
12777 switch (TREE_CODE (TREE_TYPE (expr)))
12779 case ARRAY_TYPE:
12780 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12781 return error_mark_node;
12783 case RECORD_TYPE:
12784 error_at (location, "used struct type value where scalar is required");
12785 return error_mark_node;
12787 case UNION_TYPE:
12788 error_at (location, "used union type value where scalar is required");
12789 return error_mark_node;
12791 case VOID_TYPE:
12792 error_at (location, "void value not ignored as it ought to be");
12793 return error_mark_node;
12795 case POINTER_TYPE:
12796 if (reject_gcc_builtin (expr))
12797 return error_mark_node;
12798 break;
12800 case FUNCTION_TYPE:
12801 gcc_unreachable ();
12803 case VECTOR_TYPE:
12804 error_at (location, "used vector type where scalar is required");
12805 return error_mark_node;
12807 default:
12808 break;
12811 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12812 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12813 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12815 expr = remove_c_maybe_const_expr (expr);
12816 expr = build2 (NE_EXPR, integer_type_node, expr,
12817 convert (TREE_TYPE (expr), integer_zero_node));
12818 expr = note_integer_operands (expr);
12820 else
12821 /* ??? Should we also give an error for vectors rather than leaving
12822 those to give errors later? */
12823 expr = c_common_truthvalue_conversion (location, expr);
12825 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12827 if (TREE_OVERFLOW (expr))
12828 return expr;
12829 else
12830 return note_integer_operands (expr);
12832 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12833 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12834 return expr;
12838 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12839 required. */
12841 tree
12842 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12844 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12846 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12847 /* Executing a compound literal inside a function reinitializes
12848 it. */
12849 if (!TREE_STATIC (decl))
12850 *se = true;
12851 return decl;
12853 else
12854 return expr;
12857 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12858 statement. LOC is the location of the construct. */
12860 tree
12861 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12862 tree clauses)
12864 body = c_end_compound_stmt (loc, body, true);
12866 tree stmt = make_node (code);
12867 TREE_TYPE (stmt) = void_type_node;
12868 OMP_BODY (stmt) = body;
12869 OMP_CLAUSES (stmt) = clauses;
12870 SET_EXPR_LOCATION (stmt, loc);
12872 return add_stmt (stmt);
12875 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12876 statement. LOC is the location of the OACC_DATA. */
12878 tree
12879 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12881 tree stmt;
12883 block = c_end_compound_stmt (loc, block, true);
12885 stmt = make_node (OACC_DATA);
12886 TREE_TYPE (stmt) = void_type_node;
12887 OACC_DATA_CLAUSES (stmt) = clauses;
12888 OACC_DATA_BODY (stmt) = block;
12889 SET_EXPR_LOCATION (stmt, loc);
12891 return add_stmt (stmt);
12894 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12895 statement. LOC is the location of the OACC_HOST_DATA. */
12897 tree
12898 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12900 tree stmt;
12902 block = c_end_compound_stmt (loc, block, true);
12904 stmt = make_node (OACC_HOST_DATA);
12905 TREE_TYPE (stmt) = void_type_node;
12906 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12907 OACC_HOST_DATA_BODY (stmt) = block;
12908 SET_EXPR_LOCATION (stmt, loc);
12910 return add_stmt (stmt);
12913 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12915 tree
12916 c_begin_omp_parallel (void)
12918 tree block;
12920 keep_next_level ();
12921 block = c_begin_compound_stmt (true);
12923 return block;
12926 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12927 statement. LOC is the location of the OMP_PARALLEL. */
12929 tree
12930 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12932 tree stmt;
12934 block = c_end_compound_stmt (loc, block, true);
12936 stmt = make_node (OMP_PARALLEL);
12937 TREE_TYPE (stmt) = void_type_node;
12938 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12939 OMP_PARALLEL_BODY (stmt) = block;
12940 SET_EXPR_LOCATION (stmt, loc);
12942 return add_stmt (stmt);
12945 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12947 tree
12948 c_begin_omp_task (void)
12950 tree block;
12952 keep_next_level ();
12953 block = c_begin_compound_stmt (true);
12955 return block;
12958 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12959 statement. LOC is the location of the #pragma. */
12961 tree
12962 c_finish_omp_task (location_t loc, tree clauses, tree block)
12964 tree stmt;
12966 block = c_end_compound_stmt (loc, block, true);
12968 stmt = make_node (OMP_TASK);
12969 TREE_TYPE (stmt) = void_type_node;
12970 OMP_TASK_CLAUSES (stmt) = clauses;
12971 OMP_TASK_BODY (stmt) = block;
12972 SET_EXPR_LOCATION (stmt, loc);
12974 return add_stmt (stmt);
12977 /* Generate GOMP_cancel call for #pragma omp cancel. */
12979 void
12980 c_finish_omp_cancel (location_t loc, tree clauses)
12982 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12983 int mask = 0;
12984 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12985 mask = 1;
12986 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12987 mask = 2;
12988 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12989 mask = 4;
12990 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12991 mask = 8;
12992 else
12994 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12995 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12996 "clauses");
12997 return;
12999 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13000 if (ifc != NULL_TREE)
13002 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13003 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13004 error_at (OMP_CLAUSE_LOCATION (ifc),
13005 "expected %<cancel%> %<if%> clause modifier");
13006 else
13008 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13009 if (ifc2 != NULL_TREE)
13011 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13012 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13013 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13014 error_at (OMP_CLAUSE_LOCATION (ifc2),
13015 "expected %<cancel%> %<if%> clause modifier");
13019 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13020 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13021 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13022 build_zero_cst (type));
13024 else
13025 ifc = boolean_true_node;
13026 tree stmt = build_call_expr_loc (loc, fn, 2,
13027 build_int_cst (integer_type_node, mask),
13028 ifc);
13029 add_stmt (stmt);
13032 /* Generate GOMP_cancellation_point call for
13033 #pragma omp cancellation point. */
13035 void
13036 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13038 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13039 int mask = 0;
13040 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13041 mask = 1;
13042 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13043 mask = 2;
13044 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13045 mask = 4;
13046 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13047 mask = 8;
13048 else
13050 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13051 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13052 "clauses");
13053 return;
13055 tree stmt = build_call_expr_loc (loc, fn, 1,
13056 build_int_cst (integer_type_node, mask));
13057 add_stmt (stmt);
13060 /* Helper function for handle_omp_array_sections. Called recursively
13061 to handle multiple array-section-subscripts. C is the clause,
13062 T current expression (initially OMP_CLAUSE_DECL), which is either
13063 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13064 expression if specified, TREE_VALUE length expression if specified,
13065 TREE_CHAIN is what it has been specified after, or some decl.
13066 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13067 set to true if any of the array-section-subscript could have length
13068 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13069 first array-section-subscript which is known not to have length
13070 of one. Given say:
13071 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13072 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13073 all are or may have length of 1, array-section-subscript [:2] is the
13074 first one known not to have length 1. For array-section-subscript
13075 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13076 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13077 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13078 case though, as some lengths could be zero. */
13080 static tree
13081 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13082 bool &maybe_zero_len, unsigned int &first_non_one,
13083 enum c_omp_region_type ort)
13085 tree ret, low_bound, length, type;
13086 if (TREE_CODE (t) != TREE_LIST)
13088 if (error_operand_p (t))
13089 return error_mark_node;
13090 ret = t;
13091 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13092 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13093 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13095 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13096 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13097 return error_mark_node;
13099 if (TREE_CODE (t) == COMPONENT_REF
13100 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13101 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13102 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13104 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13106 error_at (OMP_CLAUSE_LOCATION (c),
13107 "bit-field %qE in %qs clause",
13108 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13109 return error_mark_node;
13111 while (TREE_CODE (t) == COMPONENT_REF)
13113 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13115 error_at (OMP_CLAUSE_LOCATION (c),
13116 "%qE is a member of a union", t);
13117 return error_mark_node;
13119 t = TREE_OPERAND (t, 0);
13120 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13122 if (maybe_ne (mem_ref_offset (t), 0))
13123 error_at (OMP_CLAUSE_LOCATION (c),
13124 "cannot dereference %qE in %qs clause", t,
13125 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13126 else
13127 t = TREE_OPERAND (t, 0);
13131 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13133 if (DECL_P (t))
13134 error_at (OMP_CLAUSE_LOCATION (c),
13135 "%qD is not a variable in %qs clause", t,
13136 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13137 else
13138 error_at (OMP_CLAUSE_LOCATION (c),
13139 "%qE is not a variable in %qs clause", t,
13140 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13141 return error_mark_node;
13143 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13144 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13145 && TYPE_ATOMIC (TREE_TYPE (t)))
13147 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13148 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13149 return error_mark_node;
13151 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13152 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13153 && VAR_P (t)
13154 && DECL_THREAD_LOCAL_P (t))
13156 error_at (OMP_CLAUSE_LOCATION (c),
13157 "%qD is threadprivate variable in %qs clause", t,
13158 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13159 return error_mark_node;
13161 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13162 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13163 && TYPE_ATOMIC (TREE_TYPE (t))
13164 && POINTER_TYPE_P (TREE_TYPE (t)))
13166 /* If the array section is pointer based and the pointer
13167 itself is _Atomic qualified, we need to atomically load
13168 the pointer. */
13169 c_expr expr;
13170 memset (&expr, 0, sizeof (expr));
13171 expr.value = ret;
13172 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13173 expr, false, false);
13174 ret = expr.value;
13176 return ret;
13179 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13180 maybe_zero_len, first_non_one, ort);
13181 if (ret == error_mark_node || ret == NULL_TREE)
13182 return ret;
13184 type = TREE_TYPE (ret);
13185 low_bound = TREE_PURPOSE (t);
13186 length = TREE_VALUE (t);
13188 if (low_bound == error_mark_node || length == error_mark_node)
13189 return error_mark_node;
13191 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13193 error_at (OMP_CLAUSE_LOCATION (c),
13194 "low bound %qE of array section does not have integral type",
13195 low_bound);
13196 return error_mark_node;
13198 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13200 error_at (OMP_CLAUSE_LOCATION (c),
13201 "length %qE of array section does not have integral type",
13202 length);
13203 return error_mark_node;
13205 if (low_bound
13206 && TREE_CODE (low_bound) == INTEGER_CST
13207 && TYPE_PRECISION (TREE_TYPE (low_bound))
13208 > TYPE_PRECISION (sizetype))
13209 low_bound = fold_convert (sizetype, low_bound);
13210 if (length
13211 && TREE_CODE (length) == INTEGER_CST
13212 && TYPE_PRECISION (TREE_TYPE (length))
13213 > TYPE_PRECISION (sizetype))
13214 length = fold_convert (sizetype, length);
13215 if (low_bound == NULL_TREE)
13216 low_bound = integer_zero_node;
13217 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13218 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13219 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13221 if (length != integer_one_node)
13223 error_at (OMP_CLAUSE_LOCATION (c),
13224 "expected single pointer in %qs clause",
13225 c_omp_map_clause_name (c, ort == C_ORT_ACC));
13226 return error_mark_node;
13229 if (length != NULL_TREE)
13231 if (!integer_nonzerop (length))
13233 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13234 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13235 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13236 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13237 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13239 if (integer_zerop (length))
13241 error_at (OMP_CLAUSE_LOCATION (c),
13242 "zero length array section in %qs clause",
13243 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13244 return error_mark_node;
13247 else
13248 maybe_zero_len = true;
13250 if (first_non_one == types.length ()
13251 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13252 first_non_one++;
13254 if (TREE_CODE (type) == ARRAY_TYPE)
13256 if (length == NULL_TREE
13257 && (TYPE_DOMAIN (type) == NULL_TREE
13258 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13260 error_at (OMP_CLAUSE_LOCATION (c),
13261 "for unknown bound array type length expression must "
13262 "be specified");
13263 return error_mark_node;
13265 if (TREE_CODE (low_bound) == INTEGER_CST
13266 && tree_int_cst_sgn (low_bound) == -1)
13268 error_at (OMP_CLAUSE_LOCATION (c),
13269 "negative low bound in array section in %qs clause",
13270 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13271 return error_mark_node;
13273 if (length != NULL_TREE
13274 && TREE_CODE (length) == INTEGER_CST
13275 && tree_int_cst_sgn (length) == -1)
13277 error_at (OMP_CLAUSE_LOCATION (c),
13278 "negative length in array section in %qs clause",
13279 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13280 return error_mark_node;
13282 if (TYPE_DOMAIN (type)
13283 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13284 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13285 == INTEGER_CST)
13287 tree size
13288 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13289 size = size_binop (PLUS_EXPR, size, size_one_node);
13290 if (TREE_CODE (low_bound) == INTEGER_CST)
13292 if (tree_int_cst_lt (size, low_bound))
13294 error_at (OMP_CLAUSE_LOCATION (c),
13295 "low bound %qE above array section size "
13296 "in %qs clause", low_bound,
13297 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13298 return error_mark_node;
13300 if (tree_int_cst_equal (size, low_bound))
13302 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13303 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13304 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13305 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13306 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13308 error_at (OMP_CLAUSE_LOCATION (c),
13309 "zero length array section in %qs clause",
13310 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13311 return error_mark_node;
13313 maybe_zero_len = true;
13315 else if (length == NULL_TREE
13316 && first_non_one == types.length ()
13317 && tree_int_cst_equal
13318 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13319 low_bound))
13320 first_non_one++;
13322 else if (length == NULL_TREE)
13324 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13325 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13326 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13327 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13328 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13329 maybe_zero_len = true;
13330 if (first_non_one == types.length ())
13331 first_non_one++;
13333 if (length && TREE_CODE (length) == INTEGER_CST)
13335 if (tree_int_cst_lt (size, length))
13337 error_at (OMP_CLAUSE_LOCATION (c),
13338 "length %qE above array section size "
13339 "in %qs clause", length,
13340 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13341 return error_mark_node;
13343 if (TREE_CODE (low_bound) == INTEGER_CST)
13345 tree lbpluslen
13346 = size_binop (PLUS_EXPR,
13347 fold_convert (sizetype, low_bound),
13348 fold_convert (sizetype, length));
13349 if (TREE_CODE (lbpluslen) == INTEGER_CST
13350 && tree_int_cst_lt (size, lbpluslen))
13352 error_at (OMP_CLAUSE_LOCATION (c),
13353 "high bound %qE above array section size "
13354 "in %qs clause", lbpluslen,
13355 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13356 return error_mark_node;
13361 else if (length == NULL_TREE)
13363 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13364 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13365 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13366 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13367 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13368 maybe_zero_len = true;
13369 if (first_non_one == types.length ())
13370 first_non_one++;
13373 /* For [lb:] we will need to evaluate lb more than once. */
13374 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13376 tree lb = save_expr (low_bound);
13377 if (lb != low_bound)
13379 TREE_PURPOSE (t) = lb;
13380 low_bound = lb;
13384 else if (TREE_CODE (type) == POINTER_TYPE)
13386 if (length == NULL_TREE)
13388 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13389 error_at (OMP_CLAUSE_LOCATION (c),
13390 "for array function parameter length expression "
13391 "must be specified");
13392 else
13393 error_at (OMP_CLAUSE_LOCATION (c),
13394 "for pointer type length expression must be specified");
13395 return error_mark_node;
13397 if (length != NULL_TREE
13398 && TREE_CODE (length) == INTEGER_CST
13399 && tree_int_cst_sgn (length) == -1)
13401 error_at (OMP_CLAUSE_LOCATION (c),
13402 "negative length in array section in %qs clause",
13403 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13404 return error_mark_node;
13406 /* If there is a pointer type anywhere but in the very first
13407 array-section-subscript, the array section can't be contiguous. */
13408 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13409 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13410 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13412 error_at (OMP_CLAUSE_LOCATION (c),
13413 "array section is not contiguous in %qs clause",
13414 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13415 return error_mark_node;
13418 else
13420 error_at (OMP_CLAUSE_LOCATION (c),
13421 "%qE does not have pointer or array type", ret);
13422 return error_mark_node;
13424 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13425 types.safe_push (TREE_TYPE (ret));
13426 /* We will need to evaluate lb more than once. */
13427 tree lb = save_expr (low_bound);
13428 if (lb != low_bound)
13430 TREE_PURPOSE (t) = lb;
13431 low_bound = lb;
13433 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13434 return ret;
13437 /* Handle array sections for clause C. */
13439 static bool
13440 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13442 bool maybe_zero_len = false;
13443 unsigned int first_non_one = 0;
13444 auto_vec<tree, 10> types;
13445 tree *tp = &OMP_CLAUSE_DECL (c);
13446 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13447 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13448 && TREE_CODE (*tp) == TREE_LIST
13449 && TREE_PURPOSE (*tp)
13450 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13451 tp = &TREE_VALUE (*tp);
13452 tree first = handle_omp_array_sections_1 (c, *tp, types,
13453 maybe_zero_len, first_non_one,
13454 ort);
13455 if (first == error_mark_node)
13456 return true;
13457 if (first == NULL_TREE)
13458 return false;
13459 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13460 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13462 tree t = *tp;
13463 tree tem = NULL_TREE;
13464 /* Need to evaluate side effects in the length expressions
13465 if any. */
13466 while (TREE_CODE (t) == TREE_LIST)
13468 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13470 if (tem == NULL_TREE)
13471 tem = TREE_VALUE (t);
13472 else
13473 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13474 TREE_VALUE (t), tem);
13476 t = TREE_CHAIN (t);
13478 if (tem)
13479 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13480 first = c_fully_fold (first, false, NULL, true);
13481 *tp = first;
13483 else
13485 unsigned int num = types.length (), i;
13486 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13487 tree condition = NULL_TREE;
13489 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13490 maybe_zero_len = true;
13492 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13493 t = TREE_CHAIN (t))
13495 tree low_bound = TREE_PURPOSE (t);
13496 tree length = TREE_VALUE (t);
13498 i--;
13499 if (low_bound
13500 && TREE_CODE (low_bound) == INTEGER_CST
13501 && TYPE_PRECISION (TREE_TYPE (low_bound))
13502 > TYPE_PRECISION (sizetype))
13503 low_bound = fold_convert (sizetype, low_bound);
13504 if (length
13505 && TREE_CODE (length) == INTEGER_CST
13506 && TYPE_PRECISION (TREE_TYPE (length))
13507 > TYPE_PRECISION (sizetype))
13508 length = fold_convert (sizetype, length);
13509 if (low_bound == NULL_TREE)
13510 low_bound = integer_zero_node;
13511 if (!maybe_zero_len && i > first_non_one)
13513 if (integer_nonzerop (low_bound))
13514 goto do_warn_noncontiguous;
13515 if (length != NULL_TREE
13516 && TREE_CODE (length) == INTEGER_CST
13517 && TYPE_DOMAIN (types[i])
13518 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13519 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13520 == INTEGER_CST)
13522 tree size;
13523 size = size_binop (PLUS_EXPR,
13524 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13525 size_one_node);
13526 if (!tree_int_cst_equal (length, size))
13528 do_warn_noncontiguous:
13529 error_at (OMP_CLAUSE_LOCATION (c),
13530 "array section is not contiguous in %qs "
13531 "clause",
13532 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13533 return true;
13536 if (length != NULL_TREE
13537 && TREE_SIDE_EFFECTS (length))
13539 if (side_effects == NULL_TREE)
13540 side_effects = length;
13541 else
13542 side_effects = build2 (COMPOUND_EXPR,
13543 TREE_TYPE (side_effects),
13544 length, side_effects);
13547 else
13549 tree l;
13551 if (i > first_non_one
13552 && ((length && integer_nonzerop (length))
13553 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13554 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13555 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13556 continue;
13557 if (length)
13558 l = fold_convert (sizetype, length);
13559 else
13561 l = size_binop (PLUS_EXPR,
13562 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13563 size_one_node);
13564 l = size_binop (MINUS_EXPR, l,
13565 fold_convert (sizetype, low_bound));
13567 if (i > first_non_one)
13569 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13570 size_zero_node);
13571 if (condition == NULL_TREE)
13572 condition = l;
13573 else
13574 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13575 l, condition);
13577 else if (size == NULL_TREE)
13579 size = size_in_bytes (TREE_TYPE (types[i]));
13580 tree eltype = TREE_TYPE (types[num - 1]);
13581 while (TREE_CODE (eltype) == ARRAY_TYPE)
13582 eltype = TREE_TYPE (eltype);
13583 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13584 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13585 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13587 if (integer_zerop (size)
13588 || integer_zerop (size_in_bytes (eltype)))
13590 error_at (OMP_CLAUSE_LOCATION (c),
13591 "zero length array section in %qs clause",
13592 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13593 return error_mark_node;
13595 size = size_binop (EXACT_DIV_EXPR, size,
13596 size_in_bytes (eltype));
13598 size = size_binop (MULT_EXPR, size, l);
13599 if (condition)
13600 size = fold_build3 (COND_EXPR, sizetype, condition,
13601 size, size_zero_node);
13603 else
13604 size = size_binop (MULT_EXPR, size, l);
13607 if (side_effects)
13608 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13609 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13610 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13611 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13613 size = size_binop (MINUS_EXPR, size, size_one_node);
13614 size = c_fully_fold (size, false, NULL);
13615 size = save_expr (size);
13616 tree index_type = build_index_type (size);
13617 tree eltype = TREE_TYPE (first);
13618 while (TREE_CODE (eltype) == ARRAY_TYPE)
13619 eltype = TREE_TYPE (eltype);
13620 tree type = build_array_type (eltype, index_type);
13621 tree ptype = build_pointer_type (eltype);
13622 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13623 t = build_fold_addr_expr (t);
13624 tree t2 = build_fold_addr_expr (first);
13625 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13626 ptrdiff_type_node, t2);
13627 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13628 ptrdiff_type_node, t2,
13629 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13630 ptrdiff_type_node, t));
13631 t2 = c_fully_fold (t2, false, NULL);
13632 if (tree_fits_shwi_p (t2))
13633 t = build2 (MEM_REF, type, t,
13634 build_int_cst (ptype, tree_to_shwi (t2)));
13635 else
13637 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13638 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13639 TREE_TYPE (t), t, t2);
13640 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13642 OMP_CLAUSE_DECL (c) = t;
13643 return false;
13645 first = c_fully_fold (first, false, NULL);
13646 OMP_CLAUSE_DECL (c) = first;
13647 if (size)
13648 size = c_fully_fold (size, false, NULL);
13649 OMP_CLAUSE_SIZE (c) = size;
13650 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13651 || (TREE_CODE (t) == COMPONENT_REF
13652 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13653 return false;
13654 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13655 switch (OMP_CLAUSE_MAP_KIND (c))
13657 case GOMP_MAP_ALLOC:
13658 case GOMP_MAP_IF_PRESENT:
13659 case GOMP_MAP_TO:
13660 case GOMP_MAP_FROM:
13661 case GOMP_MAP_TOFROM:
13662 case GOMP_MAP_ALWAYS_TO:
13663 case GOMP_MAP_ALWAYS_FROM:
13664 case GOMP_MAP_ALWAYS_TOFROM:
13665 case GOMP_MAP_RELEASE:
13666 case GOMP_MAP_DELETE:
13667 case GOMP_MAP_FORCE_TO:
13668 case GOMP_MAP_FORCE_FROM:
13669 case GOMP_MAP_FORCE_TOFROM:
13670 case GOMP_MAP_FORCE_PRESENT:
13671 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13672 break;
13673 default:
13674 break;
13676 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13677 if (TREE_CODE (t) == COMPONENT_REF)
13678 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
13679 else
13680 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13681 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
13682 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13683 && !c_mark_addressable (t))
13684 return false;
13685 OMP_CLAUSE_DECL (c2) = t;
13686 t = build_fold_addr_expr (first);
13687 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13688 tree ptr = OMP_CLAUSE_DECL (c2);
13689 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13690 ptr = build_fold_addr_expr (ptr);
13691 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13692 ptrdiff_type_node, t,
13693 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13694 ptrdiff_type_node, ptr));
13695 t = c_fully_fold (t, false, NULL);
13696 OMP_CLAUSE_SIZE (c2) = t;
13697 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13698 OMP_CLAUSE_CHAIN (c) = c2;
13700 return false;
13703 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13704 an inline call. But, remap
13705 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13706 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13708 static tree
13709 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13710 tree decl, tree placeholder)
13712 copy_body_data id;
13713 hash_map<tree, tree> decl_map;
13715 decl_map.put (omp_decl1, placeholder);
13716 decl_map.put (omp_decl2, decl);
13717 memset (&id, 0, sizeof (id));
13718 id.src_fn = DECL_CONTEXT (omp_decl1);
13719 id.dst_fn = current_function_decl;
13720 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13721 id.decl_map = &decl_map;
13723 id.copy_decl = copy_decl_no_change;
13724 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13725 id.transform_new_cfg = true;
13726 id.transform_return_to_modify = false;
13727 id.transform_lang_insert_block = NULL;
13728 id.eh_lp_nr = 0;
13729 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13730 return stmt;
13733 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13734 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13736 static tree
13737 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13739 if (*tp == (tree) data)
13740 return *tp;
13741 return NULL_TREE;
13744 /* Similarly, but also walk aggregate fields. */
13746 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13748 static tree
13749 c_find_omp_var_r (tree *tp, int *, void *data)
13751 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13752 return *tp;
13753 if (RECORD_OR_UNION_TYPE_P (*tp))
13755 tree field;
13756 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13758 for (field = TYPE_FIELDS (*tp); field;
13759 field = DECL_CHAIN (field))
13760 if (TREE_CODE (field) == FIELD_DECL)
13762 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13763 c_find_omp_var_r, data, pset);
13764 if (ret)
13765 return ret;
13766 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13767 if (ret)
13768 return ret;
13769 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13770 pset);
13771 if (ret)
13772 return ret;
13773 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13774 if (ret)
13775 return ret;
13778 else if (INTEGRAL_TYPE_P (*tp))
13779 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13780 ((struct c_find_omp_var_s *) data)->pset);
13781 return NULL_TREE;
13784 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13785 and clauses containing them should be removed. */
13787 static bool
13788 c_omp_finish_iterators (tree iter)
13790 bool ret = false;
13791 for (tree it = iter; it; it = TREE_CHAIN (it))
13793 tree var = TREE_VEC_ELT (it, 0);
13794 tree begin = TREE_VEC_ELT (it, 1);
13795 tree end = TREE_VEC_ELT (it, 2);
13796 tree step = TREE_VEC_ELT (it, 3);
13797 tree orig_step;
13798 tree type = TREE_TYPE (var);
13799 location_t loc = DECL_SOURCE_LOCATION (var);
13800 if (type == error_mark_node)
13802 ret = true;
13803 continue;
13805 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13807 error_at (loc, "iterator %qD has neither integral nor pointer type",
13808 var);
13809 ret = true;
13810 continue;
13812 else if (TYPE_ATOMIC (type))
13814 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13815 ret = true;
13816 continue;
13818 else if (TYPE_READONLY (type))
13820 error_at (loc, "iterator %qD has const qualified type", var);
13821 ret = true;
13822 continue;
13824 else if (step == error_mark_node
13825 || TREE_TYPE (step) == error_mark_node)
13827 ret = true;
13828 continue;
13830 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13832 error_at (EXPR_LOC_OR_LOC (step, loc),
13833 "iterator step with non-integral type");
13834 ret = true;
13835 continue;
13837 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13838 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13839 orig_step = save_expr (c_fully_fold (step, false, NULL));
13840 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13841 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13842 if (POINTER_TYPE_P (type))
13844 begin = save_expr (begin);
13845 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13846 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13847 fold_convert (sizetype, step),
13848 fold_convert (sizetype, begin));
13849 step = fold_convert (ssizetype, step);
13851 if (integer_zerop (step))
13853 error_at (loc, "iterator %qD has zero step", var);
13854 ret = true;
13855 continue;
13858 if (begin == error_mark_node
13859 || end == error_mark_node
13860 || step == error_mark_node
13861 || orig_step == error_mark_node)
13863 ret = true;
13864 continue;
13866 hash_set<tree> pset;
13867 tree it2;
13868 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13870 tree var2 = TREE_VEC_ELT (it2, 0);
13871 tree begin2 = TREE_VEC_ELT (it2, 1);
13872 tree end2 = TREE_VEC_ELT (it2, 2);
13873 tree step2 = TREE_VEC_ELT (it2, 3);
13874 tree type2 = TREE_TYPE (var2);
13875 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13876 struct c_find_omp_var_s data = { var, &pset };
13877 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13879 error_at (loc2,
13880 "type of iterator %qD refers to outer iterator %qD",
13881 var2, var);
13882 break;
13884 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13886 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13887 "begin expression refers to outer iterator %qD", var);
13888 break;
13890 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13892 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13893 "end expression refers to outer iterator %qD", var);
13894 break;
13896 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13898 error_at (EXPR_LOC_OR_LOC (step2, loc2),
13899 "step expression refers to outer iterator %qD", var);
13900 break;
13903 if (it2)
13905 ret = true;
13906 continue;
13908 TREE_VEC_ELT (it, 1) = begin;
13909 TREE_VEC_ELT (it, 2) = end;
13910 TREE_VEC_ELT (it, 3) = step;
13911 TREE_VEC_ELT (it, 4) = orig_step;
13913 return ret;
13916 /* Ensure that pointers are used in OpenACC attach and detach clauses.
13917 Return true if an error has been detected. */
13919 static bool
13920 c_oacc_check_attachments (tree c)
13922 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13923 return false;
13925 /* OpenACC attach / detach clauses must be pointers. */
13926 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13927 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
13929 tree t = OMP_CLAUSE_DECL (c);
13931 while (TREE_CODE (t) == TREE_LIST)
13932 t = TREE_CHAIN (t);
13934 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13936 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
13937 c_omp_map_clause_name (c, true));
13938 return true;
13942 return false;
13945 /* For all elements of CLAUSES, validate them against their constraints.
13946 Remove any elements from the list that are invalid. */
13948 tree
13949 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13951 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13952 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
13953 bitmap_head oacc_reduction_head;
13954 tree c, t, type, *pc;
13955 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13956 bool branch_seen = false;
13957 bool copyprivate_seen = false;
13958 bool mergeable_seen = false;
13959 tree *detach_seen = NULL;
13960 bool linear_variable_step_check = false;
13961 tree *nowait_clause = NULL;
13962 tree ordered_clause = NULL_TREE;
13963 tree schedule_clause = NULL_TREE;
13964 bool oacc_async = false;
13965 tree last_iterators = NULL_TREE;
13966 bool last_iterators_remove = false;
13967 tree *nogroup_seen = NULL;
13968 tree *order_clause = NULL;
13969 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
13970 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
13971 int reduction_seen = 0;
13972 bool allocate_seen = false;
13973 bool implicit_moved = false;
13974 bool target_in_reduction_seen = false;
13976 bitmap_obstack_initialize (NULL);
13977 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13978 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13979 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13980 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13981 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
13982 bitmap_initialize (&map_head, &bitmap_default_obstack);
13983 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13984 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
13985 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
13986 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
13987 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13989 if (ort & C_ORT_ACC)
13990 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13991 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13993 oacc_async = true;
13994 break;
13997 for (pc = &clauses, c = clauses; c ; c = *pc)
13999 bool remove = false;
14000 bool need_complete = false;
14001 bool need_implicitly_determined = false;
14003 switch (OMP_CLAUSE_CODE (c))
14005 case OMP_CLAUSE_SHARED:
14006 need_implicitly_determined = true;
14007 goto check_dup_generic;
14009 case OMP_CLAUSE_PRIVATE:
14010 need_complete = true;
14011 need_implicitly_determined = true;
14012 goto check_dup_generic;
14014 case OMP_CLAUSE_REDUCTION:
14015 if (reduction_seen == 0)
14016 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14017 else if (reduction_seen != -2
14018 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14019 ? -1 : 1))
14021 error_at (OMP_CLAUSE_LOCATION (c),
14022 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14023 "on the same construct");
14024 reduction_seen = -2;
14026 /* FALLTHRU */
14027 case OMP_CLAUSE_IN_REDUCTION:
14028 case OMP_CLAUSE_TASK_REDUCTION:
14029 need_implicitly_determined = true;
14030 t = OMP_CLAUSE_DECL (c);
14031 if (TREE_CODE (t) == TREE_LIST)
14033 if (handle_omp_array_sections (c, ort))
14035 remove = true;
14036 break;
14039 t = OMP_CLAUSE_DECL (c);
14040 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14041 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14043 error_at (OMP_CLAUSE_LOCATION (c),
14044 "%<inscan%> %<reduction%> clause with array "
14045 "section");
14046 remove = true;
14047 break;
14050 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14051 if (t == error_mark_node)
14053 remove = true;
14054 break;
14056 if (oacc_async)
14057 c_mark_addressable (t);
14058 type = TREE_TYPE (t);
14059 if (TREE_CODE (t) == MEM_REF)
14060 type = TREE_TYPE (type);
14061 if (TREE_CODE (type) == ARRAY_TYPE)
14063 tree oatype = type;
14064 gcc_assert (TREE_CODE (t) != MEM_REF);
14065 while (TREE_CODE (type) == ARRAY_TYPE)
14066 type = TREE_TYPE (type);
14067 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14069 error_at (OMP_CLAUSE_LOCATION (c),
14070 "%qD in %<reduction%> clause is a zero size array",
14072 remove = true;
14073 break;
14075 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14076 TYPE_SIZE_UNIT (type));
14077 if (integer_zerop (size))
14079 error_at (OMP_CLAUSE_LOCATION (c),
14080 "%qD in %<reduction%> clause is a zero size array",
14082 remove = true;
14083 break;
14085 size = size_binop (MINUS_EXPR, size, size_one_node);
14086 size = save_expr (size);
14087 tree index_type = build_index_type (size);
14088 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14089 index_type);
14090 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14091 tree ptype = build_pointer_type (type);
14092 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14093 t = build_fold_addr_expr (t);
14094 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14095 OMP_CLAUSE_DECL (c) = t;
14097 if (TYPE_ATOMIC (type))
14099 error_at (OMP_CLAUSE_LOCATION (c),
14100 "%<_Atomic%> %qE in %<reduction%> clause", t);
14101 remove = true;
14102 break;
14104 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14105 || OMP_CLAUSE_REDUCTION_TASK (c))
14107 /* Disallow zero sized or potentially zero sized task
14108 reductions. */
14109 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14111 error_at (OMP_CLAUSE_LOCATION (c),
14112 "zero sized type %qT in %qs clause", type,
14113 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14114 remove = true;
14115 break;
14117 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14119 error_at (OMP_CLAUSE_LOCATION (c),
14120 "variable sized type %qT in %qs clause", type,
14121 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14122 remove = true;
14123 break;
14126 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14127 && (FLOAT_TYPE_P (type)
14128 || TREE_CODE (type) == COMPLEX_TYPE))
14130 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14131 const char *r_name = NULL;
14133 switch (r_code)
14135 case PLUS_EXPR:
14136 case MULT_EXPR:
14137 case MINUS_EXPR:
14138 case TRUTH_ANDIF_EXPR:
14139 case TRUTH_ORIF_EXPR:
14140 break;
14141 case MIN_EXPR:
14142 if (TREE_CODE (type) == COMPLEX_TYPE)
14143 r_name = "min";
14144 break;
14145 case MAX_EXPR:
14146 if (TREE_CODE (type) == COMPLEX_TYPE)
14147 r_name = "max";
14148 break;
14149 case BIT_AND_EXPR:
14150 r_name = "&";
14151 break;
14152 case BIT_XOR_EXPR:
14153 r_name = "^";
14154 break;
14155 case BIT_IOR_EXPR:
14156 r_name = "|";
14157 break;
14158 default:
14159 gcc_unreachable ();
14161 if (r_name)
14163 error_at (OMP_CLAUSE_LOCATION (c),
14164 "%qE has invalid type for %<reduction(%s)%>",
14165 t, r_name);
14166 remove = true;
14167 break;
14170 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14172 error_at (OMP_CLAUSE_LOCATION (c),
14173 "user defined reduction not found for %qE", t);
14174 remove = true;
14175 break;
14177 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14179 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14180 type = TYPE_MAIN_VARIANT (type);
14181 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14182 VAR_DECL, NULL_TREE, type);
14183 tree decl_placeholder = NULL_TREE;
14184 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14185 DECL_ARTIFICIAL (placeholder) = 1;
14186 DECL_IGNORED_P (placeholder) = 1;
14187 if (TREE_CODE (t) == MEM_REF)
14189 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14190 VAR_DECL, NULL_TREE, type);
14191 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14192 DECL_ARTIFICIAL (decl_placeholder) = 1;
14193 DECL_IGNORED_P (decl_placeholder) = 1;
14195 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14196 c_mark_addressable (placeholder);
14197 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14198 c_mark_addressable (decl_placeholder ? decl_placeholder
14199 : OMP_CLAUSE_DECL (c));
14200 OMP_CLAUSE_REDUCTION_MERGE (c)
14201 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14202 TREE_VEC_ELT (list, 0),
14203 TREE_VEC_ELT (list, 1),
14204 decl_placeholder ? decl_placeholder
14205 : OMP_CLAUSE_DECL (c), placeholder);
14206 OMP_CLAUSE_REDUCTION_MERGE (c)
14207 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14208 void_type_node, NULL_TREE,
14209 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14210 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14211 if (TREE_VEC_LENGTH (list) == 6)
14213 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14214 c_mark_addressable (decl_placeholder ? decl_placeholder
14215 : OMP_CLAUSE_DECL (c));
14216 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14217 c_mark_addressable (placeholder);
14218 tree init = TREE_VEC_ELT (list, 5);
14219 if (init == error_mark_node)
14220 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14221 OMP_CLAUSE_REDUCTION_INIT (c)
14222 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14223 TREE_VEC_ELT (list, 3),
14224 decl_placeholder ? decl_placeholder
14225 : OMP_CLAUSE_DECL (c), placeholder);
14226 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14228 tree v = decl_placeholder ? decl_placeholder : t;
14229 OMP_CLAUSE_REDUCTION_INIT (c)
14230 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14231 OMP_CLAUSE_REDUCTION_INIT (c));
14233 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14234 c_find_omp_placeholder_r,
14235 placeholder, NULL))
14236 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14238 else
14240 tree init;
14241 tree v = decl_placeholder ? decl_placeholder : t;
14242 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14243 init = build_constructor (TREE_TYPE (v), NULL);
14244 else
14245 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14246 OMP_CLAUSE_REDUCTION_INIT (c)
14247 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14249 OMP_CLAUSE_REDUCTION_INIT (c)
14250 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14251 void_type_node, NULL_TREE,
14252 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14253 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14255 if (TREE_CODE (t) == MEM_REF)
14257 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14258 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14259 != INTEGER_CST)
14261 sorry ("variable length element type in array "
14262 "%<reduction%> clause");
14263 remove = true;
14264 break;
14266 t = TREE_OPERAND (t, 0);
14267 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14268 t = TREE_OPERAND (t, 0);
14269 if (TREE_CODE (t) == ADDR_EXPR)
14270 t = TREE_OPERAND (t, 0);
14272 goto check_dup_generic_t;
14274 case OMP_CLAUSE_COPYPRIVATE:
14275 copyprivate_seen = true;
14276 if (nowait_clause)
14278 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14279 "%<nowait%> clause must not be used together "
14280 "with %<copyprivate%>");
14281 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14282 nowait_clause = NULL;
14284 goto check_dup_generic;
14286 case OMP_CLAUSE_COPYIN:
14287 t = OMP_CLAUSE_DECL (c);
14288 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14290 error_at (OMP_CLAUSE_LOCATION (c),
14291 "%qE must be %<threadprivate%> for %<copyin%>", t);
14292 remove = true;
14293 break;
14295 goto check_dup_generic;
14297 case OMP_CLAUSE_LINEAR:
14298 if (ort != C_ORT_OMP_DECLARE_SIMD)
14299 need_implicitly_determined = true;
14300 t = OMP_CLAUSE_DECL (c);
14301 if (ort != C_ORT_OMP_DECLARE_SIMD
14302 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
14304 error_at (OMP_CLAUSE_LOCATION (c),
14305 "modifier should not be specified in %<linear%> "
14306 "clause on %<simd%> or %<for%> constructs");
14307 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14309 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14310 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14312 error_at (OMP_CLAUSE_LOCATION (c),
14313 "linear clause applied to non-integral non-pointer "
14314 "variable with type %qT", TREE_TYPE (t));
14315 remove = true;
14316 break;
14318 if (TYPE_ATOMIC (TREE_TYPE (t)))
14320 error_at (OMP_CLAUSE_LOCATION (c),
14321 "%<_Atomic%> %qD in %<linear%> clause", t);
14322 remove = true;
14323 break;
14325 if (ort == C_ORT_OMP_DECLARE_SIMD)
14327 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14328 if (TREE_CODE (s) == PARM_DECL)
14330 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14331 /* map_head bitmap is used as uniform_head if
14332 declare_simd. */
14333 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14334 linear_variable_step_check = true;
14335 goto check_dup_generic;
14337 if (TREE_CODE (s) != INTEGER_CST)
14339 error_at (OMP_CLAUSE_LOCATION (c),
14340 "%<linear%> clause step %qE is neither constant "
14341 "nor a parameter", s);
14342 remove = true;
14343 break;
14346 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14348 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14349 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14350 OMP_CLAUSE_DECL (c), s);
14351 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14352 sizetype, fold_convert (sizetype, s),
14353 fold_convert
14354 (sizetype, OMP_CLAUSE_DECL (c)));
14355 if (s == error_mark_node)
14356 s = size_one_node;
14357 OMP_CLAUSE_LINEAR_STEP (c) = s;
14359 else
14360 OMP_CLAUSE_LINEAR_STEP (c)
14361 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14362 goto check_dup_generic;
14364 check_dup_generic:
14365 t = OMP_CLAUSE_DECL (c);
14366 check_dup_generic_t:
14367 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14369 error_at (OMP_CLAUSE_LOCATION (c),
14370 "%qE is not a variable in clause %qs", t,
14371 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14372 remove = true;
14374 else if ((ort == C_ORT_ACC
14375 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14376 || (ort == C_ORT_OMP
14377 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14378 || (OMP_CLAUSE_CODE (c)
14379 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14380 || (ort == C_ORT_OMP_TARGET
14381 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14383 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14384 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14385 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14387 error_at (OMP_CLAUSE_LOCATION (c),
14388 "%qD appears more than once in data-sharing "
14389 "clauses", t);
14390 remove = true;
14391 break;
14393 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14394 target_in_reduction_seen = true;
14395 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14397 error_at (OMP_CLAUSE_LOCATION (c),
14398 ort == C_ORT_ACC
14399 ? "%qD appears more than once in reduction clauses"
14400 : "%qD appears more than once in data clauses",
14402 remove = true;
14404 else
14405 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14407 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14408 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14409 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
14410 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14412 error_at (OMP_CLAUSE_LOCATION (c),
14413 "%qE appears more than once in data clauses", t);
14414 remove = true;
14416 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14417 && bitmap_bit_p (&map_head, DECL_UID (t)))
14419 if (ort == C_ORT_ACC)
14420 error_at (OMP_CLAUSE_LOCATION (c),
14421 "%qD appears more than once in data clauses", t);
14422 else
14423 error_at (OMP_CLAUSE_LOCATION (c),
14424 "%qD appears both in data and map clauses", t);
14425 remove = true;
14427 else
14428 bitmap_set_bit (&generic_head, DECL_UID (t));
14429 break;
14431 case OMP_CLAUSE_FIRSTPRIVATE:
14432 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
14434 move_implicit:
14435 implicit_moved = true;
14436 /* Move firstprivate and map clauses with
14437 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
14438 clauses chain. */
14439 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
14440 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
14441 while (*pc1)
14442 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
14443 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
14445 *pc3 = *pc1;
14446 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
14447 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14449 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
14450 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
14452 *pc2 = *pc1;
14453 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
14454 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14456 else
14457 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
14458 *pc3 = NULL;
14459 *pc2 = cl2;
14460 *pc1 = cl1;
14461 continue;
14463 t = OMP_CLAUSE_DECL (c);
14464 need_complete = true;
14465 need_implicitly_determined = true;
14466 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14468 error_at (OMP_CLAUSE_LOCATION (c),
14469 "%qE is not a variable in clause %<firstprivate%>", t);
14470 remove = true;
14472 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14473 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
14474 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14475 remove = true;
14476 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14477 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14478 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14480 error_at (OMP_CLAUSE_LOCATION (c),
14481 "%qE appears more than once in data clauses", t);
14482 remove = true;
14484 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14486 if (ort == C_ORT_ACC)
14487 error_at (OMP_CLAUSE_LOCATION (c),
14488 "%qD appears more than once in data clauses", t);
14489 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14490 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
14491 /* Silently drop the clause. */;
14492 else
14493 error_at (OMP_CLAUSE_LOCATION (c),
14494 "%qD appears both in data and map clauses", t);
14495 remove = true;
14497 else
14498 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14499 break;
14501 case OMP_CLAUSE_LASTPRIVATE:
14502 t = OMP_CLAUSE_DECL (c);
14503 need_complete = true;
14504 need_implicitly_determined = true;
14505 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14507 error_at (OMP_CLAUSE_LOCATION (c),
14508 "%qE is not a variable in clause %<lastprivate%>", t);
14509 remove = true;
14511 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14512 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14514 error_at (OMP_CLAUSE_LOCATION (c),
14515 "%qE appears more than once in data clauses", t);
14516 remove = true;
14518 else
14519 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14520 break;
14522 case OMP_CLAUSE_ALIGNED:
14523 t = OMP_CLAUSE_DECL (c);
14524 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14526 error_at (OMP_CLAUSE_LOCATION (c),
14527 "%qE is not a variable in %<aligned%> clause", t);
14528 remove = true;
14530 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14531 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14533 error_at (OMP_CLAUSE_LOCATION (c),
14534 "%qE in %<aligned%> clause is neither a pointer nor "
14535 "an array", t);
14536 remove = true;
14538 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14540 error_at (OMP_CLAUSE_LOCATION (c),
14541 "%<_Atomic%> %qD in %<aligned%> clause", t);
14542 remove = true;
14543 break;
14545 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14547 error_at (OMP_CLAUSE_LOCATION (c),
14548 "%qE appears more than once in %<aligned%> clauses",
14550 remove = true;
14552 else
14553 bitmap_set_bit (&aligned_head, DECL_UID (t));
14554 break;
14556 case OMP_CLAUSE_NONTEMPORAL:
14557 t = OMP_CLAUSE_DECL (c);
14558 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14560 error_at (OMP_CLAUSE_LOCATION (c),
14561 "%qE is not a variable in %<nontemporal%> clause", t);
14562 remove = true;
14564 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14566 error_at (OMP_CLAUSE_LOCATION (c),
14567 "%qE appears more than once in %<nontemporal%> "
14568 "clauses", t);
14569 remove = true;
14571 else
14572 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14573 break;
14575 case OMP_CLAUSE_ALLOCATE:
14576 t = OMP_CLAUSE_DECL (c);
14577 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14579 error_at (OMP_CLAUSE_LOCATION (c),
14580 "%qE is not a variable in %<allocate%> clause", t);
14581 remove = true;
14583 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14585 warning_at (OMP_CLAUSE_LOCATION (c), 0,
14586 "%qE appears more than once in %<allocate%> clauses",
14588 remove = true;
14590 else
14592 bitmap_set_bit (&aligned_head, DECL_UID (t));
14593 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
14594 allocate_seen = true;
14596 break;
14598 case OMP_CLAUSE_DEPEND:
14599 t = OMP_CLAUSE_DECL (c);
14600 if (t == NULL_TREE)
14602 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14603 == OMP_CLAUSE_DEPEND_SOURCE);
14604 break;
14606 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14608 gcc_assert (TREE_CODE (t) == TREE_LIST);
14609 for (; t; t = TREE_CHAIN (t))
14611 tree decl = TREE_VALUE (t);
14612 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14614 tree offset = TREE_PURPOSE (t);
14615 bool neg = wi::neg_p (wi::to_wide (offset));
14616 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14617 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14618 neg ? MINUS_EXPR : PLUS_EXPR,
14619 decl, offset);
14620 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14621 sizetype,
14622 fold_convert (sizetype, t2),
14623 fold_convert (sizetype, decl));
14624 if (t2 == error_mark_node)
14626 remove = true;
14627 break;
14629 TREE_PURPOSE (t) = t2;
14632 break;
14634 /* FALLTHRU */
14635 case OMP_CLAUSE_AFFINITY:
14636 t = OMP_CLAUSE_DECL (c);
14637 if (TREE_CODE (t) == TREE_LIST
14638 && TREE_PURPOSE (t)
14639 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14641 if (TREE_PURPOSE (t) != last_iterators)
14642 last_iterators_remove
14643 = c_omp_finish_iterators (TREE_PURPOSE (t));
14644 last_iterators = TREE_PURPOSE (t);
14645 t = TREE_VALUE (t);
14646 if (last_iterators_remove)
14647 t = error_mark_node;
14649 else
14650 last_iterators = NULL_TREE;
14651 if (TREE_CODE (t) == TREE_LIST)
14653 if (handle_omp_array_sections (c, ort))
14654 remove = true;
14655 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14656 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14658 error_at (OMP_CLAUSE_LOCATION (c),
14659 "%<depend%> clause with %<depobj%> dependence "
14660 "type on array section");
14661 remove = true;
14663 break;
14665 if (t == error_mark_node)
14666 remove = true;
14667 else if (!lvalue_p (t))
14669 error_at (OMP_CLAUSE_LOCATION (c),
14670 "%qE is not lvalue expression nor array section in "
14671 "%qs clause", t,
14672 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14673 remove = true;
14675 else if (TREE_CODE (t) == COMPONENT_REF
14676 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14678 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14679 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
14680 error_at (OMP_CLAUSE_LOCATION (c),
14681 "bit-field %qE in %qs clause", t,
14682 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14683 remove = true;
14685 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14686 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14688 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14690 error_at (OMP_CLAUSE_LOCATION (c),
14691 "%qE does not have %<omp_depend_t%> type in "
14692 "%<depend%> clause with %<depobj%> dependence "
14693 "type", t);
14694 remove = true;
14697 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14698 && c_omp_depend_t_p (TREE_TYPE (t)))
14700 error_at (OMP_CLAUSE_LOCATION (c),
14701 "%qE should not have %<omp_depend_t%> type in "
14702 "%<depend%> clause with dependence type other than "
14703 "%<depobj%>", t);
14704 remove = true;
14706 if (!remove)
14708 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14709 t, false);
14710 if (addr == error_mark_node)
14711 remove = true;
14712 else
14714 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14715 RO_UNARY_STAR);
14716 if (t == error_mark_node)
14717 remove = true;
14718 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14719 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14720 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14721 == TREE_VEC))
14722 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14723 else
14724 OMP_CLAUSE_DECL (c) = t;
14727 break;
14729 case OMP_CLAUSE_MAP:
14730 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
14731 goto move_implicit;
14732 /* FALLTHRU */
14733 case OMP_CLAUSE_TO:
14734 case OMP_CLAUSE_FROM:
14735 case OMP_CLAUSE__CACHE_:
14736 t = OMP_CLAUSE_DECL (c);
14737 if (TREE_CODE (t) == TREE_LIST)
14739 if (handle_omp_array_sections (c, ort))
14740 remove = true;
14741 else
14743 t = OMP_CLAUSE_DECL (c);
14744 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14746 error_at (OMP_CLAUSE_LOCATION (c),
14747 "array section does not have mappable type "
14748 "in %qs clause",
14749 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14750 remove = true;
14752 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14754 error_at (OMP_CLAUSE_LOCATION (c),
14755 "%<_Atomic%> %qE in %qs clause", t,
14756 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14757 remove = true;
14759 while (TREE_CODE (t) == ARRAY_REF)
14760 t = TREE_OPERAND (t, 0);
14761 if (TREE_CODE (t) == COMPONENT_REF
14762 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14764 while (TREE_CODE (t) == COMPONENT_REF)
14765 t = TREE_OPERAND (t, 0);
14766 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14767 && OMP_CLAUSE_MAP_IMPLICIT (c)
14768 && (bitmap_bit_p (&map_head, DECL_UID (t))
14769 || bitmap_bit_p (&map_field_head, DECL_UID (t))
14770 || bitmap_bit_p (&map_firstprivate_head,
14771 DECL_UID (t))))
14773 remove = true;
14774 break;
14776 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14777 break;
14778 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14780 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14781 error_at (OMP_CLAUSE_LOCATION (c),
14782 "%qD appears more than once in motion "
14783 "clauses", t);
14784 else if (ort == C_ORT_ACC)
14785 error_at (OMP_CLAUSE_LOCATION (c),
14786 "%qD appears more than once in data "
14787 "clauses", t);
14788 else
14789 error_at (OMP_CLAUSE_LOCATION (c),
14790 "%qD appears more than once in map "
14791 "clauses", t);
14792 remove = true;
14794 else
14796 bitmap_set_bit (&map_head, DECL_UID (t));
14797 bitmap_set_bit (&map_field_head, DECL_UID (t));
14801 if (c_oacc_check_attachments (c))
14802 remove = true;
14803 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14804 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14805 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14806 /* In this case, we have a single array element which is a
14807 pointer, and we already set OMP_CLAUSE_SIZE in
14808 handle_omp_array_sections above. For attach/detach clauses,
14809 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
14810 here. */
14811 OMP_CLAUSE_SIZE (c) = size_zero_node;
14812 break;
14814 if (t == error_mark_node)
14816 remove = true;
14817 break;
14819 /* OpenACC attach / detach clauses must be pointers. */
14820 if (c_oacc_check_attachments (c))
14822 remove = true;
14823 break;
14825 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14826 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14827 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14828 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
14829 bias) to zero here, so it is not set erroneously to the pointer
14830 size later on in gimplify.c. */
14831 OMP_CLAUSE_SIZE (c) = size_zero_node;
14832 if (TREE_CODE (t) == COMPONENT_REF
14833 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14835 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14837 error_at (OMP_CLAUSE_LOCATION (c),
14838 "bit-field %qE in %qs clause",
14839 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14840 remove = true;
14842 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14844 error_at (OMP_CLAUSE_LOCATION (c),
14845 "%qE does not have a mappable type in %qs clause",
14846 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14847 remove = true;
14849 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14851 error_at (OMP_CLAUSE_LOCATION (c),
14852 "%<_Atomic%> %qE in %qs clause", t,
14853 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14854 remove = true;
14856 while (TREE_CODE (t) == COMPONENT_REF)
14858 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14859 == UNION_TYPE)
14861 error_at (OMP_CLAUSE_LOCATION (c),
14862 "%qE is a member of a union", t);
14863 remove = true;
14864 break;
14866 t = TREE_OPERAND (t, 0);
14867 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
14869 if (maybe_ne (mem_ref_offset (t), 0))
14870 error_at (OMP_CLAUSE_LOCATION (c),
14871 "cannot dereference %qE in %qs clause", t,
14872 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14873 else
14874 t = TREE_OPERAND (t, 0);
14877 if (remove)
14878 break;
14879 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14881 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
14882 || (ort != C_ORT_ACC
14883 && bitmap_bit_p (&map_head, DECL_UID (t))))
14884 break;
14887 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14889 error_at (OMP_CLAUSE_LOCATION (c),
14890 "%qE is not a variable in %qs clause", t,
14891 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14892 remove = true;
14894 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14896 error_at (OMP_CLAUSE_LOCATION (c),
14897 "%qD is threadprivate variable in %qs clause", t,
14898 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14899 remove = true;
14901 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14902 || (OMP_CLAUSE_MAP_KIND (c)
14903 != GOMP_MAP_FIRSTPRIVATE_POINTER))
14904 && !c_mark_addressable (t))
14905 remove = true;
14906 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14907 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14908 || (OMP_CLAUSE_MAP_KIND (c)
14909 == GOMP_MAP_FIRSTPRIVATE_POINTER)
14910 || (OMP_CLAUSE_MAP_KIND (c)
14911 == GOMP_MAP_FORCE_DEVICEPTR)))
14912 && t == OMP_CLAUSE_DECL (c)
14913 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14915 error_at (OMP_CLAUSE_LOCATION (c),
14916 "%qD does not have a mappable type in %qs clause", t,
14917 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14918 remove = true;
14920 else if (TREE_TYPE (t) == error_mark_node)
14921 remove = true;
14922 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14924 error_at (OMP_CLAUSE_LOCATION (c),
14925 "%<_Atomic%> %qE in %qs clause", t,
14926 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14927 remove = true;
14929 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14930 && OMP_CLAUSE_MAP_IMPLICIT (c)
14931 && (bitmap_bit_p (&map_head, DECL_UID (t))
14932 || bitmap_bit_p (&map_field_head, DECL_UID (t))
14933 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
14934 remove = true;
14935 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14936 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14938 if (bitmap_bit_p (&generic_head, DECL_UID (t))
14939 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14940 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14942 error_at (OMP_CLAUSE_LOCATION (c),
14943 "%qD appears more than once in data clauses", t);
14944 remove = true;
14946 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14948 if (ort == C_ORT_ACC)
14949 error_at (OMP_CLAUSE_LOCATION (c),
14950 "%qD appears more than once in data clauses", t);
14951 else
14952 error_at (OMP_CLAUSE_LOCATION (c),
14953 "%qD appears both in data and map clauses", t);
14954 remove = true;
14956 else
14957 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
14959 else if (bitmap_bit_p (&map_head, DECL_UID (t))
14960 && (ort == C_ORT_ACC
14961 || !bitmap_bit_p (&map_field_head, DECL_UID (t))))
14963 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14964 error_at (OMP_CLAUSE_LOCATION (c),
14965 "%qD appears more than once in motion clauses", t);
14966 else if (ort == C_ORT_ACC)
14967 error_at (OMP_CLAUSE_LOCATION (c),
14968 "%qD appears more than once in data clauses", t);
14969 else
14970 error_at (OMP_CLAUSE_LOCATION (c),
14971 "%qD appears more than once in map clauses", t);
14972 remove = true;
14974 else if (ort == C_ORT_ACC
14975 && bitmap_bit_p (&generic_head, DECL_UID (t)))
14977 error_at (OMP_CLAUSE_LOCATION (c),
14978 "%qD appears more than once in data clauses", t);
14979 remove = true;
14981 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14983 if (ort == C_ORT_ACC)
14984 error_at (OMP_CLAUSE_LOCATION (c),
14985 "%qD appears more than once in data clauses", t);
14986 else
14987 error_at (OMP_CLAUSE_LOCATION (c),
14988 "%qD appears both in data and map clauses", t);
14989 remove = true;
14991 else
14993 bitmap_set_bit (&map_head, DECL_UID (t));
14994 if (t != OMP_CLAUSE_DECL (c)
14995 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14996 bitmap_set_bit (&map_field_head, DECL_UID (t));
14998 break;
15000 case OMP_CLAUSE_TO_DECLARE:
15001 case OMP_CLAUSE_LINK:
15002 t = OMP_CLAUSE_DECL (c);
15003 if (TREE_CODE (t) == FUNCTION_DECL
15004 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
15006 else if (!VAR_P (t))
15008 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
15009 error_at (OMP_CLAUSE_LOCATION (c),
15010 "%qE is neither a variable nor a function name in "
15011 "clause %qs", t,
15012 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15013 else
15014 error_at (OMP_CLAUSE_LOCATION (c),
15015 "%qE is not a variable in clause %qs", t,
15016 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15017 remove = true;
15019 else if (DECL_THREAD_LOCAL_P (t))
15021 error_at (OMP_CLAUSE_LOCATION (c),
15022 "%qD is threadprivate variable in %qs clause", t,
15023 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15024 remove = true;
15026 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
15028 error_at (OMP_CLAUSE_LOCATION (c),
15029 "%qD does not have a mappable type in %qs clause", t,
15030 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15031 remove = true;
15033 if (remove)
15034 break;
15035 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15037 error_at (OMP_CLAUSE_LOCATION (c),
15038 "%qE appears more than once on the same "
15039 "%<declare target%> directive", t);
15040 remove = true;
15042 else
15043 bitmap_set_bit (&generic_head, DECL_UID (t));
15044 break;
15046 case OMP_CLAUSE_UNIFORM:
15047 t = OMP_CLAUSE_DECL (c);
15048 if (TREE_CODE (t) != PARM_DECL)
15050 if (DECL_P (t))
15051 error_at (OMP_CLAUSE_LOCATION (c),
15052 "%qD is not an argument in %<uniform%> clause", t);
15053 else
15054 error_at (OMP_CLAUSE_LOCATION (c),
15055 "%qE is not an argument in %<uniform%> clause", t);
15056 remove = true;
15057 break;
15059 /* map_head bitmap is used as uniform_head if declare_simd. */
15060 bitmap_set_bit (&map_head, DECL_UID (t));
15061 goto check_dup_generic;
15063 case OMP_CLAUSE_IS_DEVICE_PTR:
15064 case OMP_CLAUSE_USE_DEVICE_PTR:
15065 t = OMP_CLAUSE_DECL (c);
15066 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15068 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15069 && ort != C_ORT_ACC)
15071 error_at (OMP_CLAUSE_LOCATION (c),
15072 "%qs variable is not a pointer",
15073 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15074 remove = true;
15076 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15078 error_at (OMP_CLAUSE_LOCATION (c),
15079 "%qs variable is neither a pointer nor an array",
15080 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15081 remove = true;
15084 goto check_dup_generic;
15086 case OMP_CLAUSE_USE_DEVICE_ADDR:
15087 t = OMP_CLAUSE_DECL (c);
15088 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15089 c_mark_addressable (t);
15090 goto check_dup_generic;
15092 case OMP_CLAUSE_NOWAIT:
15093 if (copyprivate_seen)
15095 error_at (OMP_CLAUSE_LOCATION (c),
15096 "%<nowait%> clause must not be used together "
15097 "with %<copyprivate%>");
15098 remove = true;
15099 break;
15101 nowait_clause = pc;
15102 pc = &OMP_CLAUSE_CHAIN (c);
15103 continue;
15105 case OMP_CLAUSE_ORDER:
15106 if (ordered_clause)
15108 error_at (OMP_CLAUSE_LOCATION (c),
15109 "%<order%> clause must not be used together "
15110 "with %<ordered%>");
15111 remove = true;
15112 break;
15114 else if (order_clause)
15116 /* Silently remove duplicates. */
15117 remove = true;
15118 break;
15120 order_clause = pc;
15121 pc = &OMP_CLAUSE_CHAIN (c);
15122 continue;
15124 case OMP_CLAUSE_DETACH:
15125 t = OMP_CLAUSE_DECL (c);
15126 if (detach_seen)
15128 error_at (OMP_CLAUSE_LOCATION (c),
15129 "too many %qs clauses on a task construct",
15130 "detach");
15131 remove = true;
15132 break;
15134 detach_seen = pc;
15135 pc = &OMP_CLAUSE_CHAIN (c);
15136 c_mark_addressable (t);
15137 continue;
15139 case OMP_CLAUSE_IF:
15140 case OMP_CLAUSE_NUM_THREADS:
15141 case OMP_CLAUSE_NUM_TEAMS:
15142 case OMP_CLAUSE_THREAD_LIMIT:
15143 case OMP_CLAUSE_DEFAULT:
15144 case OMP_CLAUSE_UNTIED:
15145 case OMP_CLAUSE_COLLAPSE:
15146 case OMP_CLAUSE_FINAL:
15147 case OMP_CLAUSE_DEVICE:
15148 case OMP_CLAUSE_DIST_SCHEDULE:
15149 case OMP_CLAUSE_PARALLEL:
15150 case OMP_CLAUSE_FOR:
15151 case OMP_CLAUSE_SECTIONS:
15152 case OMP_CLAUSE_TASKGROUP:
15153 case OMP_CLAUSE_PROC_BIND:
15154 case OMP_CLAUSE_DEVICE_TYPE:
15155 case OMP_CLAUSE_PRIORITY:
15156 case OMP_CLAUSE_GRAINSIZE:
15157 case OMP_CLAUSE_NUM_TASKS:
15158 case OMP_CLAUSE_THREADS:
15159 case OMP_CLAUSE_SIMD:
15160 case OMP_CLAUSE_HINT:
15161 case OMP_CLAUSE_FILTER:
15162 case OMP_CLAUSE_DEFAULTMAP:
15163 case OMP_CLAUSE_BIND:
15164 case OMP_CLAUSE_NUM_GANGS:
15165 case OMP_CLAUSE_NUM_WORKERS:
15166 case OMP_CLAUSE_VECTOR_LENGTH:
15167 case OMP_CLAUSE_ASYNC:
15168 case OMP_CLAUSE_WAIT:
15169 case OMP_CLAUSE_AUTO:
15170 case OMP_CLAUSE_INDEPENDENT:
15171 case OMP_CLAUSE_SEQ:
15172 case OMP_CLAUSE_GANG:
15173 case OMP_CLAUSE_WORKER:
15174 case OMP_CLAUSE_VECTOR:
15175 case OMP_CLAUSE_TILE:
15176 case OMP_CLAUSE_IF_PRESENT:
15177 case OMP_CLAUSE_FINALIZE:
15178 case OMP_CLAUSE_NOHOST:
15179 pc = &OMP_CLAUSE_CHAIN (c);
15180 continue;
15182 case OMP_CLAUSE_MERGEABLE:
15183 mergeable_seen = true;
15184 pc = &OMP_CLAUSE_CHAIN (c);
15185 continue;
15187 case OMP_CLAUSE_NOGROUP:
15188 nogroup_seen = pc;
15189 pc = &OMP_CLAUSE_CHAIN (c);
15190 continue;
15192 case OMP_CLAUSE_SCHEDULE:
15193 schedule_clause = c;
15194 pc = &OMP_CLAUSE_CHAIN (c);
15195 continue;
15197 case OMP_CLAUSE_ORDERED:
15198 ordered_clause = c;
15199 if (order_clause)
15201 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15202 "%<order%> clause must not be used together "
15203 "with %<ordered%>");
15204 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15205 order_clause = NULL;
15207 pc = &OMP_CLAUSE_CHAIN (c);
15208 continue;
15210 case OMP_CLAUSE_SAFELEN:
15211 safelen = c;
15212 pc = &OMP_CLAUSE_CHAIN (c);
15213 continue;
15214 case OMP_CLAUSE_SIMDLEN:
15215 simdlen = c;
15216 pc = &OMP_CLAUSE_CHAIN (c);
15217 continue;
15219 case OMP_CLAUSE_INBRANCH:
15220 case OMP_CLAUSE_NOTINBRANCH:
15221 if (branch_seen)
15223 error_at (OMP_CLAUSE_LOCATION (c),
15224 "%<inbranch%> clause is incompatible with "
15225 "%<notinbranch%>");
15226 remove = true;
15227 break;
15229 branch_seen = true;
15230 pc = &OMP_CLAUSE_CHAIN (c);
15231 continue;
15233 case OMP_CLAUSE_INCLUSIVE:
15234 case OMP_CLAUSE_EXCLUSIVE:
15235 need_complete = true;
15236 need_implicitly_determined = true;
15237 t = OMP_CLAUSE_DECL (c);
15238 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15240 error_at (OMP_CLAUSE_LOCATION (c),
15241 "%qE is not a variable in clause %qs", t,
15242 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15243 remove = true;
15245 break;
15247 default:
15248 gcc_unreachable ();
15251 if (!remove)
15253 t = OMP_CLAUSE_DECL (c);
15255 if (need_complete)
15257 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15258 if (t == error_mark_node)
15259 remove = true;
15262 if (need_implicitly_determined)
15264 const char *share_name = NULL;
15266 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15267 share_name = "threadprivate";
15268 else switch (c_omp_predetermined_sharing (t))
15270 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15271 break;
15272 case OMP_CLAUSE_DEFAULT_SHARED:
15273 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15274 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15275 && c_omp_predefined_variable (t))
15276 /* The __func__ variable and similar function-local
15277 predefined variables may be listed in a shared or
15278 firstprivate clause. */
15279 break;
15280 share_name = "shared";
15281 break;
15282 case OMP_CLAUSE_DEFAULT_PRIVATE:
15283 share_name = "private";
15284 break;
15285 default:
15286 gcc_unreachable ();
15288 if (share_name)
15290 error_at (OMP_CLAUSE_LOCATION (c),
15291 "%qE is predetermined %qs for %qs",
15292 t, share_name,
15293 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15294 remove = true;
15296 else if (TREE_READONLY (t)
15297 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15298 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15300 error_at (OMP_CLAUSE_LOCATION (c),
15301 "%<const%> qualified %qE may appear only in "
15302 "%<shared%> or %<firstprivate%> clauses", t);
15303 remove = true;
15308 if (remove)
15309 *pc = OMP_CLAUSE_CHAIN (c);
15310 else
15311 pc = &OMP_CLAUSE_CHAIN (c);
15314 if (simdlen
15315 && safelen
15316 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15317 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15319 error_at (OMP_CLAUSE_LOCATION (simdlen),
15320 "%<simdlen%> clause value is bigger than "
15321 "%<safelen%> clause value");
15322 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15323 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15326 if (ordered_clause
15327 && schedule_clause
15328 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15329 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15331 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15332 "%<nonmonotonic%> schedule modifier specified together "
15333 "with %<ordered%> clause");
15334 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15335 = (enum omp_clause_schedule_kind)
15336 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15337 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15340 if (reduction_seen < 0 && ordered_clause)
15342 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15343 "%qs clause specified together with %<inscan%> "
15344 "%<reduction%> clause", "ordered");
15345 reduction_seen = -2;
15348 if (reduction_seen < 0 && schedule_clause)
15350 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15351 "%qs clause specified together with %<inscan%> "
15352 "%<reduction%> clause", "schedule");
15353 reduction_seen = -2;
15356 if (linear_variable_step_check
15357 || reduction_seen == -2
15358 || allocate_seen
15359 || target_in_reduction_seen)
15360 for (pc = &clauses, c = clauses; c ; c = *pc)
15362 bool remove = false;
15363 if (allocate_seen)
15364 switch (OMP_CLAUSE_CODE (c))
15366 case OMP_CLAUSE_REDUCTION:
15367 case OMP_CLAUSE_IN_REDUCTION:
15368 case OMP_CLAUSE_TASK_REDUCTION:
15369 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
15371 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
15372 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15373 t = TREE_OPERAND (t, 0);
15374 if (TREE_CODE (t) == ADDR_EXPR
15375 || TREE_CODE (t) == INDIRECT_REF)
15376 t = TREE_OPERAND (t, 0);
15377 if (DECL_P (t))
15378 bitmap_clear_bit (&aligned_head, DECL_UID (t));
15379 break;
15381 /* FALLTHRU */
15382 case OMP_CLAUSE_PRIVATE:
15383 case OMP_CLAUSE_FIRSTPRIVATE:
15384 case OMP_CLAUSE_LASTPRIVATE:
15385 case OMP_CLAUSE_LINEAR:
15386 if (DECL_P (OMP_CLAUSE_DECL (c)))
15387 bitmap_clear_bit (&aligned_head,
15388 DECL_UID (OMP_CLAUSE_DECL (c)));
15389 break;
15390 default:
15391 break;
15393 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15394 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15395 && !bitmap_bit_p (&map_head,
15396 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15398 error_at (OMP_CLAUSE_LOCATION (c),
15399 "%<linear%> clause step is a parameter %qD not "
15400 "specified in %<uniform%> clause",
15401 OMP_CLAUSE_LINEAR_STEP (c));
15402 remove = true;
15404 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15405 && reduction_seen == -2)
15406 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15407 if (target_in_reduction_seen
15408 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
15410 tree t = OMP_CLAUSE_DECL (c);
15411 while (handled_component_p (t)
15412 || TREE_CODE (t) == INDIRECT_REF
15413 || TREE_CODE (t) == ADDR_EXPR
15414 || TREE_CODE (t) == MEM_REF
15415 || TREE_CODE (t) == NON_LVALUE_EXPR)
15416 t = TREE_OPERAND (t, 0);
15417 if (DECL_P (t)
15418 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15419 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15422 if (remove)
15423 *pc = OMP_CLAUSE_CHAIN (c);
15424 else
15425 pc = &OMP_CLAUSE_CHAIN (c);
15428 if (allocate_seen)
15429 for (pc = &clauses, c = clauses; c ; c = *pc)
15431 bool remove = false;
15432 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
15433 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
15434 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
15436 error_at (OMP_CLAUSE_LOCATION (c),
15437 "%qD specified in %<allocate%> clause but not in "
15438 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
15439 remove = true;
15441 if (remove)
15442 *pc = OMP_CLAUSE_CHAIN (c);
15443 else
15444 pc = &OMP_CLAUSE_CHAIN (c);
15447 if (nogroup_seen && reduction_seen)
15449 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15450 "%<nogroup%> clause must not be used together with "
15451 "%<reduction%> clause");
15452 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15455 if (detach_seen)
15457 if (mergeable_seen)
15459 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
15460 "%<detach%> clause must not be used together with "
15461 "%<mergeable%> clause");
15462 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
15464 else
15466 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
15468 for (pc = &clauses, c = clauses; c ; c = *pc)
15470 bool remove = false;
15471 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15472 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15473 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
15474 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
15475 && OMP_CLAUSE_DECL (c) == detach_decl)
15477 error_at (OMP_CLAUSE_LOCATION (c),
15478 "the event handle of a %<detach%> clause "
15479 "should not be in a data-sharing clause");
15480 remove = true;
15482 if (remove)
15483 *pc = OMP_CLAUSE_CHAIN (c);
15484 else
15485 pc = &OMP_CLAUSE_CHAIN (c);
15490 bitmap_obstack_release (NULL);
15491 return clauses;
15494 /* Return code to initialize DST with a copy constructor from SRC.
15495 C doesn't have copy constructors nor assignment operators, only for
15496 _Atomic vars we need to perform __atomic_load from src into a temporary
15497 followed by __atomic_store of the temporary to dst. */
15499 tree
15500 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15502 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15503 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15505 location_t loc = OMP_CLAUSE_LOCATION (clause);
15506 tree type = TREE_TYPE (dst);
15507 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15508 tree tmp = create_tmp_var (nonatomic_type);
15509 tree tmp_addr = build_fold_addr_expr (tmp);
15510 TREE_ADDRESSABLE (tmp) = 1;
15511 suppress_warning (tmp);
15512 tree src_addr = build_fold_addr_expr (src);
15513 tree dst_addr = build_fold_addr_expr (dst);
15514 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15515 vec<tree, va_gc> *params;
15516 /* Expansion of a generic atomic load may require an addition
15517 element, so allocate enough to prevent a resize. */
15518 vec_alloc (params, 4);
15520 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15521 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15522 params->quick_push (src_addr);
15523 params->quick_push (tmp_addr);
15524 params->quick_push (seq_cst);
15525 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15527 vec_alloc (params, 4);
15529 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15530 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15531 params->quick_push (dst_addr);
15532 params->quick_push (tmp_addr);
15533 params->quick_push (seq_cst);
15534 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15535 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15538 /* Create a transaction node. */
15540 tree
15541 c_finish_transaction (location_t loc, tree block, int flags)
15543 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15544 if (flags & TM_STMT_ATTR_OUTER)
15545 TRANSACTION_EXPR_OUTER (stmt) = 1;
15546 if (flags & TM_STMT_ATTR_RELAXED)
15547 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15548 return add_stmt (stmt);
15551 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15552 down to the element type of an array. If ORIG_QUAL_TYPE is not
15553 NULL, then it should be used as the qualified type
15554 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15555 preserve information about the typedef name from which an array
15556 type was derived). */
15558 tree
15559 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15560 size_t orig_qual_indirect)
15562 if (type == error_mark_node)
15563 return type;
15565 if (TREE_CODE (type) == ARRAY_TYPE)
15567 tree t;
15568 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15569 type_quals, orig_qual_type,
15570 orig_qual_indirect - 1);
15572 /* See if we already have an identically qualified type. */
15573 if (orig_qual_type && orig_qual_indirect == 0)
15574 t = orig_qual_type;
15575 else
15576 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15578 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15579 && TYPE_NAME (t) == TYPE_NAME (type)
15580 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15581 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15582 TYPE_ATTRIBUTES (type)))
15583 break;
15585 if (!t)
15587 tree domain = TYPE_DOMAIN (type);
15589 t = build_variant_type_copy (type);
15590 TREE_TYPE (t) = element_type;
15592 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15593 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15594 SET_TYPE_STRUCTURAL_EQUALITY (t);
15595 else if (TYPE_CANONICAL (element_type) != element_type
15596 || (domain && TYPE_CANONICAL (domain) != domain))
15598 tree unqualified_canon
15599 = build_array_type (TYPE_CANONICAL (element_type),
15600 domain? TYPE_CANONICAL (domain)
15601 : NULL_TREE);
15602 if (TYPE_REVERSE_STORAGE_ORDER (type))
15604 unqualified_canon
15605 = build_distinct_type_copy (unqualified_canon);
15606 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15608 TYPE_CANONICAL (t)
15609 = c_build_qualified_type (unqualified_canon, type_quals);
15611 else
15612 TYPE_CANONICAL (t) = t;
15614 return t;
15617 /* A restrict-qualified pointer type must be a pointer to object or
15618 incomplete type. Note that the use of POINTER_TYPE_P also allows
15619 REFERENCE_TYPEs, which is appropriate for C++. */
15620 if ((type_quals & TYPE_QUAL_RESTRICT)
15621 && (!POINTER_TYPE_P (type)
15622 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15624 error ("invalid use of %<restrict%>");
15625 type_quals &= ~TYPE_QUAL_RESTRICT;
15628 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15629 ? orig_qual_type
15630 : build_qualified_type (type, type_quals));
15631 /* A variant type does not inherit the list of incomplete vars from the
15632 type main variant. */
15633 if ((RECORD_OR_UNION_TYPE_P (var_type)
15634 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15635 && TYPE_MAIN_VARIANT (var_type) != var_type)
15636 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15637 return var_type;
15640 /* Build a VA_ARG_EXPR for the C parser. */
15642 tree
15643 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15645 if (error_operand_p (type))
15646 return error_mark_node;
15647 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15648 order because it takes the address of the expression. */
15649 else if (handled_component_p (expr)
15650 && reverse_storage_order_for_component_p (expr))
15652 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15653 return error_mark_node;
15655 else if (!COMPLETE_TYPE_P (type))
15657 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15658 "type %qT", type);
15659 return error_mark_node;
15661 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15662 warning_at (loc2, OPT_Wc___compat,
15663 "C++ requires promoted type, not enum type, in %<va_arg%>");
15664 return build_va_arg (loc2, expr, type);
15667 /* Return truthvalue of whether T1 is the same tree structure as T2.
15668 Return 1 if they are the same. Return false if they are different. */
15670 bool
15671 c_tree_equal (tree t1, tree t2)
15673 enum tree_code code1, code2;
15675 if (t1 == t2)
15676 return true;
15677 if (!t1 || !t2)
15678 return false;
15680 for (code1 = TREE_CODE (t1);
15681 CONVERT_EXPR_CODE_P (code1)
15682 || code1 == NON_LVALUE_EXPR;
15683 code1 = TREE_CODE (t1))
15684 t1 = TREE_OPERAND (t1, 0);
15685 for (code2 = TREE_CODE (t2);
15686 CONVERT_EXPR_CODE_P (code2)
15687 || code2 == NON_LVALUE_EXPR;
15688 code2 = TREE_CODE (t2))
15689 t2 = TREE_OPERAND (t2, 0);
15691 /* They might have become equal now. */
15692 if (t1 == t2)
15693 return true;
15695 if (code1 != code2)
15696 return false;
15698 switch (code1)
15700 case INTEGER_CST:
15701 return wi::to_wide (t1) == wi::to_wide (t2);
15703 case REAL_CST:
15704 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15706 case STRING_CST:
15707 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15708 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15709 TREE_STRING_LENGTH (t1));
15711 case FIXED_CST:
15712 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15713 TREE_FIXED_CST (t2));
15715 case COMPLEX_CST:
15716 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15717 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15719 case VECTOR_CST:
15720 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15722 case CONSTRUCTOR:
15723 /* We need to do this when determining whether or not two
15724 non-type pointer to member function template arguments
15725 are the same. */
15726 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15727 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15728 return false;
15730 tree field, value;
15731 unsigned int i;
15732 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15734 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15735 if (!c_tree_equal (field, elt2->index)
15736 || !c_tree_equal (value, elt2->value))
15737 return false;
15740 return true;
15742 case TREE_LIST:
15743 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15744 return false;
15745 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15746 return false;
15747 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15749 case SAVE_EXPR:
15750 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15752 case CALL_EXPR:
15754 tree arg1, arg2;
15755 call_expr_arg_iterator iter1, iter2;
15756 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15757 return false;
15758 for (arg1 = first_call_expr_arg (t1, &iter1),
15759 arg2 = first_call_expr_arg (t2, &iter2);
15760 arg1 && arg2;
15761 arg1 = next_call_expr_arg (&iter1),
15762 arg2 = next_call_expr_arg (&iter2))
15763 if (!c_tree_equal (arg1, arg2))
15764 return false;
15765 if (arg1 || arg2)
15766 return false;
15767 return true;
15770 case TARGET_EXPR:
15772 tree o1 = TREE_OPERAND (t1, 0);
15773 tree o2 = TREE_OPERAND (t2, 0);
15775 /* Special case: if either target is an unallocated VAR_DECL,
15776 it means that it's going to be unified with whatever the
15777 TARGET_EXPR is really supposed to initialize, so treat it
15778 as being equivalent to anything. */
15779 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15780 && !DECL_RTL_SET_P (o1))
15781 /*Nop*/;
15782 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15783 && !DECL_RTL_SET_P (o2))
15784 /*Nop*/;
15785 else if (!c_tree_equal (o1, o2))
15786 return false;
15788 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15791 case COMPONENT_REF:
15792 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15793 return false;
15794 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15796 case PARM_DECL:
15797 case VAR_DECL:
15798 case CONST_DECL:
15799 case FIELD_DECL:
15800 case FUNCTION_DECL:
15801 case IDENTIFIER_NODE:
15802 case SSA_NAME:
15803 return false;
15805 case TREE_VEC:
15807 unsigned ix;
15808 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15809 return false;
15810 for (ix = TREE_VEC_LENGTH (t1); ix--;)
15811 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15812 TREE_VEC_ELT (t2, ix)))
15813 return false;
15814 return true;
15817 default:
15818 break;
15821 switch (TREE_CODE_CLASS (code1))
15823 case tcc_unary:
15824 case tcc_binary:
15825 case tcc_comparison:
15826 case tcc_expression:
15827 case tcc_vl_exp:
15828 case tcc_reference:
15829 case tcc_statement:
15831 int i, n = TREE_OPERAND_LENGTH (t1);
15833 switch (code1)
15835 case PREINCREMENT_EXPR:
15836 case PREDECREMENT_EXPR:
15837 case POSTINCREMENT_EXPR:
15838 case POSTDECREMENT_EXPR:
15839 n = 1;
15840 break;
15841 case ARRAY_REF:
15842 n = 2;
15843 break;
15844 default:
15845 break;
15848 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15849 && n != TREE_OPERAND_LENGTH (t2))
15850 return false;
15852 for (i = 0; i < n; ++i)
15853 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15854 return false;
15856 return true;
15859 case tcc_type:
15860 return comptypes (t1, t2);
15861 default:
15862 gcc_unreachable ();
15864 /* We can get here with --disable-checking. */
15865 return false;
15868 /* Returns true when the function declaration FNDECL is implicit,
15869 introduced as a result of a call to an otherwise undeclared
15870 function, and false otherwise. */
15872 bool
15873 c_decl_implicit (const_tree fndecl)
15875 return C_DECL_IMPLICIT (fndecl);