Daily bump.
[official-gcc.git] / gcc / c / c-typeck.c
blobfb5c288b54988d7038cc46dc14e2dfb6a177446a
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2020 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)
743 t1 = build_type_attribute_variant (t1, NULL_TREE);
745 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
746 t2 = build_type_attribute_variant (t2, NULL_TREE);
748 /* Save time if the two types are the same. */
750 if (t1 == t2) return t1;
752 code1 = TREE_CODE (t1);
753 code2 = TREE_CODE (t2);
755 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
756 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
757 || code1 == INTEGER_TYPE);
758 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
759 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
760 || code2 == INTEGER_TYPE);
762 /* When one operand is a decimal float type, the other operand cannot be
763 a generic float type or a complex type. We also disallow vector types
764 here. */
765 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
766 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
768 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
770 error ("cannot mix operands of decimal floating and vector types");
771 return error_mark_node;
773 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
775 error ("cannot mix operands of decimal floating and complex types");
776 return error_mark_node;
778 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
780 error ("cannot mix operands of decimal floating "
781 "and other floating types");
782 return error_mark_node;
786 /* If one type is a vector type, return that type. (How the usual
787 arithmetic conversions apply to the vector types extension is not
788 precisely specified.) */
789 if (code1 == VECTOR_TYPE)
790 return t1;
792 if (code2 == VECTOR_TYPE)
793 return t2;
795 /* If one type is complex, form the common type of the non-complex
796 components, then make that complex. Use T1 or T2 if it is the
797 required type. */
798 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
800 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
801 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
802 tree subtype = c_common_type (subtype1, subtype2);
804 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
805 return t1;
806 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
807 return t2;
808 else
809 return build_complex_type (subtype);
812 /* If only one is real, use it as the result. */
814 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
815 return t1;
817 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
818 return t2;
820 /* If both are real and either are decimal floating point types, use
821 the decimal floating point type with the greater precision. */
823 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
825 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
826 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
827 return dfloat128_type_node;
828 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
829 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
830 return dfloat64_type_node;
831 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
832 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
833 return dfloat32_type_node;
836 /* Deal with fixed-point types. */
837 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
839 unsigned int unsignedp = 0, satp = 0;
840 scalar_mode m1, m2;
841 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
843 m1 = SCALAR_TYPE_MODE (t1);
844 m2 = SCALAR_TYPE_MODE (t2);
846 /* If one input type is saturating, the result type is saturating. */
847 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
848 satp = 1;
850 /* If both fixed-point types are unsigned, the result type is unsigned.
851 When mixing fixed-point and integer types, follow the sign of the
852 fixed-point type.
853 Otherwise, the result type is signed. */
854 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
855 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
856 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
857 && TYPE_UNSIGNED (t1))
858 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
859 && TYPE_UNSIGNED (t2)))
860 unsignedp = 1;
862 /* The result type is signed. */
863 if (unsignedp == 0)
865 /* If the input type is unsigned, we need to convert to the
866 signed type. */
867 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
869 enum mode_class mclass = (enum mode_class) 0;
870 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
871 mclass = MODE_FRACT;
872 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
873 mclass = MODE_ACCUM;
874 else
875 gcc_unreachable ();
876 m1 = as_a <scalar_mode>
877 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
879 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
881 enum mode_class mclass = (enum mode_class) 0;
882 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
883 mclass = MODE_FRACT;
884 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
885 mclass = MODE_ACCUM;
886 else
887 gcc_unreachable ();
888 m2 = as_a <scalar_mode>
889 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
893 if (code1 == FIXED_POINT_TYPE)
895 fbit1 = GET_MODE_FBIT (m1);
896 ibit1 = GET_MODE_IBIT (m1);
898 else
900 fbit1 = 0;
901 /* Signed integers need to subtract one sign bit. */
902 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
905 if (code2 == FIXED_POINT_TYPE)
907 fbit2 = GET_MODE_FBIT (m2);
908 ibit2 = GET_MODE_IBIT (m2);
910 else
912 fbit2 = 0;
913 /* Signed integers need to subtract one sign bit. */
914 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
917 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
918 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
919 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
920 satp);
923 /* Both real or both integers; use the one with greater precision. */
925 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
926 return t1;
927 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
928 return t2;
930 /* Same precision. Prefer long longs to longs to ints when the
931 same precision, following the C99 rules on integer type rank
932 (which are equivalent to the C90 rules for C90 types). */
934 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
935 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
936 return long_long_unsigned_type_node;
938 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
939 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
941 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
942 return long_long_unsigned_type_node;
943 else
944 return long_long_integer_type_node;
947 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
948 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
949 return long_unsigned_type_node;
951 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
952 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
954 /* But preserve unsignedness from the other type,
955 since long cannot hold all the values of an unsigned int. */
956 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
957 return long_unsigned_type_node;
958 else
959 return long_integer_type_node;
962 /* For floating types of the same TYPE_PRECISION (which we here
963 assume means either the same set of values, or sets of values
964 neither a subset of the other, with behavior being undefined in
965 the latter case), follow the rules from TS 18661-3: prefer
966 interchange types _FloatN, then standard types long double,
967 double, float, then extended types _FloatNx. For extended types,
968 check them starting with _Float128x as that seems most consistent
969 in spirit with preferring long double to double; for interchange
970 types, also check in that order for consistency although it's not
971 possible for more than one of them to have the same
972 precision. */
973 tree mv1 = TYPE_MAIN_VARIANT (t1);
974 tree mv2 = TYPE_MAIN_VARIANT (t2);
976 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
977 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
978 return FLOATN_TYPE_NODE (i);
980 /* Likewise, prefer long double to double even if same size. */
981 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
982 return long_double_type_node;
984 /* Likewise, prefer double to float even if same size.
985 We got a couple of embedded targets with 32 bit doubles, and the
986 pdp11 might have 64 bit floats. */
987 if (mv1 == double_type_node || mv2 == double_type_node)
988 return double_type_node;
990 if (mv1 == float_type_node || mv2 == float_type_node)
991 return float_type_node;
993 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
994 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
995 return FLOATNX_TYPE_NODE (i);
997 /* Otherwise prefer the unsigned one. */
999 if (TYPE_UNSIGNED (t1))
1000 return t1;
1001 else
1002 return t2;
1005 /* Wrapper around c_common_type that is used by c-common.c and other
1006 front end optimizations that remove promotions. ENUMERAL_TYPEs
1007 are allowed here and are converted to their compatible integer types.
1008 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1009 preferably a non-Boolean type as the common type. */
1010 tree
1011 common_type (tree t1, tree t2)
1013 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1014 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1015 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1016 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1018 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1019 if (TREE_CODE (t1) == BOOLEAN_TYPE
1020 && TREE_CODE (t2) == BOOLEAN_TYPE)
1021 return boolean_type_node;
1023 /* If either type is BOOLEAN_TYPE, then return the other. */
1024 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1025 return t2;
1026 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1027 return t1;
1029 return c_common_type (t1, t2);
1032 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1033 or various other operations. Return 2 if they are compatible
1034 but a warning may be needed if you use them together. */
1037 comptypes (tree type1, tree type2)
1039 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1040 int val;
1042 val = comptypes_internal (type1, type2, NULL, NULL);
1043 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1045 return val;
1048 /* Like comptypes, but if it returns non-zero because enum and int are
1049 compatible, it sets *ENUM_AND_INT_P to true. */
1051 static int
1052 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1054 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1055 int val;
1057 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1058 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1060 return val;
1063 /* Like comptypes, but if it returns nonzero for different types, it
1064 sets *DIFFERENT_TYPES_P to true. */
1067 comptypes_check_different_types (tree type1, tree type2,
1068 bool *different_types_p)
1070 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1071 int val;
1073 val = comptypes_internal (type1, type2, NULL, different_types_p);
1074 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1076 return val;
1079 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1080 or various other operations. Return 2 if they are compatible
1081 but a warning may be needed if you use them together. If
1082 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1083 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1084 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1085 NULL, and the types are compatible but different enough not to be
1086 permitted in C11 typedef redeclarations, then this sets
1087 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1088 false, but may or may not be set if the types are incompatible.
1089 This differs from comptypes, in that we don't free the seen
1090 types. */
1092 static int
1093 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1094 bool *different_types_p)
1096 const_tree t1 = type1;
1097 const_tree t2 = type2;
1098 int attrval, val;
1100 /* Suppress errors caused by previously reported errors. */
1102 if (t1 == t2 || !t1 || !t2
1103 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1104 return 1;
1106 /* Enumerated types are compatible with integer types, but this is
1107 not transitive: two enumerated types in the same translation unit
1108 are compatible with each other only if they are the same type. */
1110 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1112 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1113 if (TREE_CODE (t2) != VOID_TYPE)
1115 if (enum_and_int_p != NULL)
1116 *enum_and_int_p = true;
1117 if (different_types_p != NULL)
1118 *different_types_p = true;
1121 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1123 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1124 if (TREE_CODE (t1) != VOID_TYPE)
1126 if (enum_and_int_p != NULL)
1127 *enum_and_int_p = true;
1128 if (different_types_p != NULL)
1129 *different_types_p = true;
1133 if (t1 == t2)
1134 return 1;
1136 /* Different classes of types can't be compatible. */
1138 if (TREE_CODE (t1) != TREE_CODE (t2))
1139 return 0;
1141 /* Qualifiers must match. C99 6.7.3p9 */
1143 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1144 return 0;
1146 /* Allow for two different type nodes which have essentially the same
1147 definition. Note that we already checked for equality of the type
1148 qualifiers (just above). */
1150 if (TREE_CODE (t1) != ARRAY_TYPE
1151 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1152 return 1;
1154 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1155 if (!(attrval = comp_type_attributes (t1, t2)))
1156 return 0;
1158 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1159 val = 0;
1161 switch (TREE_CODE (t1))
1163 case INTEGER_TYPE:
1164 case FIXED_POINT_TYPE:
1165 case REAL_TYPE:
1166 /* With these nodes, we can't determine type equivalence by
1167 looking at what is stored in the nodes themselves, because
1168 two nodes might have different TYPE_MAIN_VARIANTs but still
1169 represent the same type. For example, wchar_t and int could
1170 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1171 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1172 and are distinct types. On the other hand, int and the
1173 following typedef
1175 typedef int INT __attribute((may_alias));
1177 have identical properties, different TYPE_MAIN_VARIANTs, but
1178 represent the same type. The canonical type system keeps
1179 track of equivalence in this case, so we fall back on it. */
1180 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1182 case POINTER_TYPE:
1183 /* Do not remove mode information. */
1184 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1185 break;
1186 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1187 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1188 enum_and_int_p, different_types_p));
1189 break;
1191 case FUNCTION_TYPE:
1192 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1193 different_types_p);
1194 break;
1196 case ARRAY_TYPE:
1198 tree d1 = TYPE_DOMAIN (t1);
1199 tree d2 = TYPE_DOMAIN (t2);
1200 bool d1_variable, d2_variable;
1201 bool d1_zero, d2_zero;
1202 val = 1;
1204 /* Target types must match incl. qualifiers. */
1205 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1206 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1207 enum_and_int_p,
1208 different_types_p)) == 0)
1209 return 0;
1211 if (different_types_p != NULL
1212 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1213 *different_types_p = true;
1214 /* Sizes must match unless one is missing or variable. */
1215 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1216 break;
1218 d1_zero = !TYPE_MAX_VALUE (d1);
1219 d2_zero = !TYPE_MAX_VALUE (d2);
1221 d1_variable = (!d1_zero
1222 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1223 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1224 d2_variable = (!d2_zero
1225 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1226 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1227 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1228 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1230 if (different_types_p != NULL
1231 && d1_variable != d2_variable)
1232 *different_types_p = true;
1233 if (d1_variable || d2_variable)
1234 break;
1235 if (d1_zero && d2_zero)
1236 break;
1237 if (d1_zero || d2_zero
1238 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1239 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1240 val = 0;
1242 break;
1245 case ENUMERAL_TYPE:
1246 case RECORD_TYPE:
1247 case UNION_TYPE:
1248 if (val != 1 && !same_translation_unit_p (t1, t2))
1250 tree a1 = TYPE_ATTRIBUTES (t1);
1251 tree a2 = TYPE_ATTRIBUTES (t2);
1253 if (! attribute_list_contained (a1, a2)
1254 && ! attribute_list_contained (a2, a1))
1255 break;
1257 if (attrval != 2)
1258 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1259 different_types_p);
1260 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1261 different_types_p);
1263 break;
1265 case VECTOR_TYPE:
1266 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1267 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1268 enum_and_int_p, different_types_p));
1269 break;
1271 default:
1272 break;
1274 return attrval == 2 && val == 1 ? 2 : val;
1277 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1278 their qualifiers, except for named address spaces. If the pointers point to
1279 different named addresses, then we must determine if one address space is a
1280 subset of the other. */
1282 static int
1283 comp_target_types (location_t location, tree ttl, tree ttr)
1285 int val;
1286 int val_ped;
1287 tree mvl = TREE_TYPE (ttl);
1288 tree mvr = TREE_TYPE (ttr);
1289 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1290 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1291 addr_space_t as_common;
1292 bool enum_and_int_p;
1294 /* Fail if pointers point to incompatible address spaces. */
1295 if (!addr_space_superset (asl, asr, &as_common))
1296 return 0;
1298 /* For pedantic record result of comptypes on arrays before losing
1299 qualifiers on the element type below. */
1300 val_ped = 1;
1302 if (TREE_CODE (mvl) == ARRAY_TYPE
1303 && TREE_CODE (mvr) == ARRAY_TYPE)
1304 val_ped = comptypes (mvl, mvr);
1306 /* Qualifiers on element types of array types that are
1307 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1309 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1310 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1311 : TYPE_MAIN_VARIANT (mvl));
1313 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1314 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1315 : TYPE_MAIN_VARIANT (mvr));
1317 enum_and_int_p = false;
1318 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1320 if (val == 1 && val_ped != 1)
1321 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1322 "are incompatible in ISO C");
1324 if (val == 2)
1325 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1327 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1328 warning_at (location, OPT_Wc___compat,
1329 "pointer target types incompatible in C++");
1331 return val;
1334 /* Subroutines of `comptypes'. */
1336 /* Determine whether two trees derive from the same translation unit.
1337 If the CONTEXT chain ends in a null, that tree's context is still
1338 being parsed, so if two trees have context chains ending in null,
1339 they're in the same translation unit. */
1341 bool
1342 same_translation_unit_p (const_tree t1, const_tree t2)
1344 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1345 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1347 case tcc_declaration:
1348 t1 = DECL_CONTEXT (t1); break;
1349 case tcc_type:
1350 t1 = TYPE_CONTEXT (t1); break;
1351 case tcc_exceptional:
1352 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1353 default: gcc_unreachable ();
1356 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1357 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1359 case tcc_declaration:
1360 t2 = DECL_CONTEXT (t2); break;
1361 case tcc_type:
1362 t2 = TYPE_CONTEXT (t2); break;
1363 case tcc_exceptional:
1364 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1365 default: gcc_unreachable ();
1368 return t1 == t2;
1371 /* Allocate the seen two types, assuming that they are compatible. */
1373 static struct tagged_tu_seen_cache *
1374 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1376 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1377 tu->next = tagged_tu_seen_base;
1378 tu->t1 = t1;
1379 tu->t2 = t2;
1381 tagged_tu_seen_base = tu;
1383 /* The C standard says that two structures in different translation
1384 units are compatible with each other only if the types of their
1385 fields are compatible (among other things). We assume that they
1386 are compatible until proven otherwise when building the cache.
1387 An example where this can occur is:
1388 struct a
1390 struct a *next;
1392 If we are comparing this against a similar struct in another TU,
1393 and did not assume they were compatible, we end up with an infinite
1394 loop. */
1395 tu->val = 1;
1396 return tu;
1399 /* Free the seen types until we get to TU_TIL. */
1401 static void
1402 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1404 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1405 while (tu != tu_til)
1407 const struct tagged_tu_seen_cache *const tu1
1408 = (const struct tagged_tu_seen_cache *) tu;
1409 tu = tu1->next;
1410 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1412 tagged_tu_seen_base = tu_til;
1415 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1416 compatible. If the two types are not the same (which has been
1417 checked earlier), this can only happen when multiple translation
1418 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1419 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1420 comptypes_internal. */
1422 static int
1423 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1424 bool *enum_and_int_p, bool *different_types_p)
1426 tree s1, s2;
1427 bool needs_warning = false;
1429 /* We have to verify that the tags of the types are the same. This
1430 is harder than it looks because this may be a typedef, so we have
1431 to go look at the original type. It may even be a typedef of a
1432 typedef...
1433 In the case of compiler-created builtin structs the TYPE_DECL
1434 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1435 while (TYPE_NAME (t1)
1436 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1437 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1438 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1440 while (TYPE_NAME (t2)
1441 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1442 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1443 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1445 /* C90 didn't have the requirement that the two tags be the same. */
1446 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1447 return 0;
1449 /* C90 didn't say what happened if one or both of the types were
1450 incomplete; we choose to follow C99 rules here, which is that they
1451 are compatible. */
1452 if (TYPE_SIZE (t1) == NULL
1453 || TYPE_SIZE (t2) == NULL)
1454 return 1;
1457 const struct tagged_tu_seen_cache * tts_i;
1458 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1459 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1460 return tts_i->val;
1463 switch (TREE_CODE (t1))
1465 case ENUMERAL_TYPE:
1467 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1468 /* Speed up the case where the type values are in the same order. */
1469 tree tv1 = TYPE_VALUES (t1);
1470 tree tv2 = TYPE_VALUES (t2);
1472 if (tv1 == tv2)
1474 return 1;
1477 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1479 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1480 break;
1481 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1483 tu->val = 0;
1484 return 0;
1488 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1490 return 1;
1492 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1494 tu->val = 0;
1495 return 0;
1498 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1500 tu->val = 0;
1501 return 0;
1504 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1506 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1507 if (s2 == NULL
1508 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1510 tu->val = 0;
1511 return 0;
1514 return 1;
1517 case UNION_TYPE:
1519 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1520 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1522 tu->val = 0;
1523 return 0;
1526 /* Speed up the common case where the fields are in the same order. */
1527 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1528 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1530 int result;
1532 if (DECL_NAME (s1) != DECL_NAME (s2))
1533 break;
1534 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1535 enum_and_int_p, different_types_p);
1537 if (result != 1 && !DECL_NAME (s1))
1538 break;
1539 if (result == 0)
1541 tu->val = 0;
1542 return 0;
1544 if (result == 2)
1545 needs_warning = true;
1547 if (TREE_CODE (s1) == FIELD_DECL
1548 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1549 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1551 tu->val = 0;
1552 return 0;
1555 if (!s1 && !s2)
1557 tu->val = needs_warning ? 2 : 1;
1558 return tu->val;
1561 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1563 bool ok = false;
1565 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1566 if (DECL_NAME (s1) == DECL_NAME (s2))
1568 int result;
1570 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1571 enum_and_int_p,
1572 different_types_p);
1574 if (result != 1 && !DECL_NAME (s1))
1575 continue;
1576 if (result == 0)
1578 tu->val = 0;
1579 return 0;
1581 if (result == 2)
1582 needs_warning = true;
1584 if (TREE_CODE (s1) == FIELD_DECL
1585 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1586 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1587 break;
1589 ok = true;
1590 break;
1592 if (!ok)
1594 tu->val = 0;
1595 return 0;
1598 tu->val = needs_warning ? 2 : 10;
1599 return tu->val;
1602 case RECORD_TYPE:
1604 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1606 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1607 s1 && s2;
1608 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1610 int result;
1611 if (TREE_CODE (s1) != TREE_CODE (s2)
1612 || DECL_NAME (s1) != DECL_NAME (s2))
1613 break;
1614 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1615 enum_and_int_p, different_types_p);
1616 if (result == 0)
1617 break;
1618 if (result == 2)
1619 needs_warning = true;
1621 if (TREE_CODE (s1) == FIELD_DECL
1622 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1623 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1624 break;
1626 if (s1 && s2)
1627 tu->val = 0;
1628 else
1629 tu->val = needs_warning ? 2 : 1;
1630 return tu->val;
1633 default:
1634 gcc_unreachable ();
1638 /* Return 1 if two function types F1 and F2 are compatible.
1639 If either type specifies no argument types,
1640 the other must specify a fixed number of self-promoting arg types.
1641 Otherwise, if one type specifies only the number of arguments,
1642 the other must specify that number of self-promoting arg types.
1643 Otherwise, the argument types must match.
1644 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1646 static int
1647 function_types_compatible_p (const_tree f1, const_tree f2,
1648 bool *enum_and_int_p, bool *different_types_p)
1650 tree args1, args2;
1651 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1652 int val = 1;
1653 int val1;
1654 tree ret1, ret2;
1656 ret1 = TREE_TYPE (f1);
1657 ret2 = TREE_TYPE (f2);
1659 /* 'volatile' qualifiers on a function's return type used to mean
1660 the function is noreturn. */
1661 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1662 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1663 if (TYPE_VOLATILE (ret1))
1664 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1665 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1666 if (TYPE_VOLATILE (ret2))
1667 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1668 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1669 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1670 if (val == 0)
1671 return 0;
1673 args1 = TYPE_ARG_TYPES (f1);
1674 args2 = TYPE_ARG_TYPES (f2);
1676 if (different_types_p != NULL
1677 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1678 *different_types_p = true;
1680 /* An unspecified parmlist matches any specified parmlist
1681 whose argument types don't need default promotions. */
1683 if (args1 == NULL_TREE)
1685 if (!self_promoting_args_p (args2))
1686 return 0;
1687 /* If one of these types comes from a non-prototype fn definition,
1688 compare that with the other type's arglist.
1689 If they don't match, ask for a warning (but no error). */
1690 if (TYPE_ACTUAL_ARG_TYPES (f1)
1691 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1692 enum_and_int_p, different_types_p) != 1)
1693 val = 2;
1694 return val;
1696 if (args2 == NULL_TREE)
1698 if (!self_promoting_args_p (args1))
1699 return 0;
1700 if (TYPE_ACTUAL_ARG_TYPES (f2)
1701 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1702 enum_and_int_p, different_types_p) != 1)
1703 val = 2;
1704 return val;
1707 /* Both types have argument lists: compare them and propagate results. */
1708 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1709 different_types_p);
1710 return val1 != 1 ? val1 : val;
1713 /* Check two lists of types for compatibility, returning 0 for
1714 incompatible, 1 for compatible, or 2 for compatible with
1715 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1716 comptypes_internal. */
1718 static int
1719 type_lists_compatible_p (const_tree args1, const_tree args2,
1720 bool *enum_and_int_p, bool *different_types_p)
1722 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1723 int val = 1;
1724 int newval = 0;
1726 while (1)
1728 tree a1, mv1, a2, mv2;
1729 if (args1 == NULL_TREE && args2 == NULL_TREE)
1730 return val;
1731 /* If one list is shorter than the other,
1732 they fail to match. */
1733 if (args1 == NULL_TREE || args2 == NULL_TREE)
1734 return 0;
1735 mv1 = a1 = TREE_VALUE (args1);
1736 mv2 = a2 = TREE_VALUE (args2);
1737 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1738 mv1 = (TYPE_ATOMIC (mv1)
1739 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1740 TYPE_QUAL_ATOMIC)
1741 : TYPE_MAIN_VARIANT (mv1));
1742 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1743 mv2 = (TYPE_ATOMIC (mv2)
1744 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1745 TYPE_QUAL_ATOMIC)
1746 : TYPE_MAIN_VARIANT (mv2));
1747 /* A null pointer instead of a type
1748 means there is supposed to be an argument
1749 but nothing is specified about what type it has.
1750 So match anything that self-promotes. */
1751 if (different_types_p != NULL
1752 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1753 *different_types_p = true;
1754 if (a1 == NULL_TREE)
1756 if (c_type_promotes_to (a2) != a2)
1757 return 0;
1759 else if (a2 == NULL_TREE)
1761 if (c_type_promotes_to (a1) != a1)
1762 return 0;
1764 /* If one of the lists has an error marker, ignore this arg. */
1765 else if (TREE_CODE (a1) == ERROR_MARK
1766 || TREE_CODE (a2) == ERROR_MARK)
1768 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1769 different_types_p)))
1771 if (different_types_p != NULL)
1772 *different_types_p = true;
1773 /* Allow wait (union {union wait *u; int *i} *)
1774 and wait (union wait *) to be compatible. */
1775 if (TREE_CODE (a1) == UNION_TYPE
1776 && (TYPE_NAME (a1) == NULL_TREE
1777 || TYPE_TRANSPARENT_AGGR (a1))
1778 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1779 && tree_int_cst_equal (TYPE_SIZE (a1),
1780 TYPE_SIZE (a2)))
1782 tree memb;
1783 for (memb = TYPE_FIELDS (a1);
1784 memb; memb = DECL_CHAIN (memb))
1786 tree mv3 = TREE_TYPE (memb);
1787 if (mv3 && mv3 != error_mark_node
1788 && TREE_CODE (mv3) != ARRAY_TYPE)
1789 mv3 = (TYPE_ATOMIC (mv3)
1790 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1791 TYPE_QUAL_ATOMIC)
1792 : TYPE_MAIN_VARIANT (mv3));
1793 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1794 different_types_p))
1795 break;
1797 if (memb == NULL_TREE)
1798 return 0;
1800 else if (TREE_CODE (a2) == UNION_TYPE
1801 && (TYPE_NAME (a2) == NULL_TREE
1802 || TYPE_TRANSPARENT_AGGR (a2))
1803 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1804 && tree_int_cst_equal (TYPE_SIZE (a2),
1805 TYPE_SIZE (a1)))
1807 tree memb;
1808 for (memb = TYPE_FIELDS (a2);
1809 memb; memb = DECL_CHAIN (memb))
1811 tree mv3 = TREE_TYPE (memb);
1812 if (mv3 && mv3 != error_mark_node
1813 && TREE_CODE (mv3) != ARRAY_TYPE)
1814 mv3 = (TYPE_ATOMIC (mv3)
1815 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1816 TYPE_QUAL_ATOMIC)
1817 : TYPE_MAIN_VARIANT (mv3));
1818 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1819 different_types_p))
1820 break;
1822 if (memb == NULL_TREE)
1823 return 0;
1825 else
1826 return 0;
1829 /* comptypes said ok, but record if it said to warn. */
1830 if (newval > val)
1831 val = newval;
1833 args1 = TREE_CHAIN (args1);
1834 args2 = TREE_CHAIN (args2);
1838 /* Compute the size to increment a pointer by. When a function type or void
1839 type or incomplete type is passed, size_one_node is returned.
1840 This function does not emit any diagnostics; the caller is responsible
1841 for that. */
1843 static tree
1844 c_size_in_bytes (const_tree type)
1846 enum tree_code code = TREE_CODE (type);
1848 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1849 || !COMPLETE_TYPE_P (type))
1850 return size_one_node;
1852 /* Convert in case a char is more than one unit. */
1853 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1854 size_int (TYPE_PRECISION (char_type_node)
1855 / BITS_PER_UNIT));
1858 /* Return either DECL or its known constant value (if it has one). */
1860 tree
1861 decl_constant_value_1 (tree decl, bool in_init)
1863 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1864 TREE_CODE (decl) != PARM_DECL
1865 && !TREE_THIS_VOLATILE (decl)
1866 && TREE_READONLY (decl)
1867 && DECL_INITIAL (decl) != NULL_TREE
1868 && !error_operand_p (DECL_INITIAL (decl))
1869 /* This is invalid if initial value is not constant.
1870 If it has either a function call, a memory reference,
1871 or a variable, then re-evaluating it could give different results. */
1872 && TREE_CONSTANT (DECL_INITIAL (decl))
1873 /* Check for cases where this is sub-optimal, even though valid. */
1874 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1875 return DECL_INITIAL (decl);
1876 return decl;
1879 /* Return either DECL or its known constant value (if it has one).
1880 Like the above, but always return decl outside of functions. */
1882 tree
1883 decl_constant_value (tree decl)
1885 /* Don't change a variable array bound or initial value to a constant
1886 in a place where a variable is invalid. */
1887 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1890 /* Convert the array expression EXP to a pointer. */
1891 static tree
1892 array_to_pointer_conversion (location_t loc, tree exp)
1894 tree orig_exp = exp;
1895 tree type = TREE_TYPE (exp);
1896 tree adr;
1897 tree restype = TREE_TYPE (type);
1898 tree ptrtype;
1900 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1902 STRIP_TYPE_NOPS (exp);
1904 if (TREE_NO_WARNING (orig_exp))
1905 TREE_NO_WARNING (exp) = 1;
1907 ptrtype = build_pointer_type (restype);
1909 if (INDIRECT_REF_P (exp))
1910 return convert (ptrtype, TREE_OPERAND (exp, 0));
1912 /* In C++ array compound literals are temporary objects unless they are
1913 const or appear in namespace scope, so they are destroyed too soon
1914 to use them for much of anything (c++/53220). */
1915 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1917 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1918 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1919 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1920 "converting an array compound literal to a pointer "
1921 "is ill-formed in C++");
1924 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1925 return convert (ptrtype, adr);
1928 /* Convert the function expression EXP to a pointer. */
1929 static tree
1930 function_to_pointer_conversion (location_t loc, tree exp)
1932 tree orig_exp = exp;
1934 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1936 STRIP_TYPE_NOPS (exp);
1938 if (TREE_NO_WARNING (orig_exp))
1939 TREE_NO_WARNING (exp) = 1;
1941 return build_unary_op (loc, ADDR_EXPR, exp, false);
1944 /* Mark EXP as read, not just set, for set but not used -Wunused
1945 warning purposes. */
1947 void
1948 mark_exp_read (tree exp)
1950 switch (TREE_CODE (exp))
1952 case VAR_DECL:
1953 case PARM_DECL:
1954 DECL_READ_P (exp) = 1;
1955 break;
1956 case ARRAY_REF:
1957 case COMPONENT_REF:
1958 case MODIFY_EXPR:
1959 case REALPART_EXPR:
1960 case IMAGPART_EXPR:
1961 CASE_CONVERT:
1962 case ADDR_EXPR:
1963 case VIEW_CONVERT_EXPR:
1964 mark_exp_read (TREE_OPERAND (exp, 0));
1965 break;
1966 case COMPOUND_EXPR:
1967 case C_MAYBE_CONST_EXPR:
1968 mark_exp_read (TREE_OPERAND (exp, 1));
1969 break;
1970 default:
1971 break;
1975 /* Perform the default conversion of arrays and functions to pointers.
1976 Return the result of converting EXP. For any other expression, just
1977 return EXP.
1979 LOC is the location of the expression. */
1981 struct c_expr
1982 default_function_array_conversion (location_t loc, struct c_expr exp)
1984 tree orig_exp = exp.value;
1985 tree type = TREE_TYPE (exp.value);
1986 enum tree_code code = TREE_CODE (type);
1988 switch (code)
1990 case ARRAY_TYPE:
1992 bool not_lvalue = false;
1993 bool lvalue_array_p;
1995 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1996 || CONVERT_EXPR_P (exp.value))
1997 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1999 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2000 not_lvalue = true;
2001 exp.value = TREE_OPERAND (exp.value, 0);
2004 if (TREE_NO_WARNING (orig_exp))
2005 TREE_NO_WARNING (exp.value) = 1;
2007 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2008 if (!flag_isoc99 && !lvalue_array_p)
2010 /* Before C99, non-lvalue arrays do not decay to pointers.
2011 Normally, using such an array would be invalid; but it can
2012 be used correctly inside sizeof or as a statement expression.
2013 Thus, do not give an error here; an error will result later. */
2014 return exp;
2017 exp.value = array_to_pointer_conversion (loc, exp.value);
2019 break;
2020 case FUNCTION_TYPE:
2021 exp.value = function_to_pointer_conversion (loc, exp.value);
2022 break;
2023 default:
2024 break;
2027 return exp;
2030 struct c_expr
2031 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2033 mark_exp_read (exp.value);
2034 return default_function_array_conversion (loc, exp);
2037 /* Return whether EXPR should be treated as an atomic lvalue for the
2038 purposes of load and store handling. */
2040 static bool
2041 really_atomic_lvalue (tree expr)
2043 if (error_operand_p (expr))
2044 return false;
2045 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2046 return false;
2047 if (!lvalue_p (expr))
2048 return false;
2050 /* Ignore _Atomic on register variables, since their addresses can't
2051 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2052 sequences wouldn't work. Ignore _Atomic on structures containing
2053 bit-fields, since accessing elements of atomic structures or
2054 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2055 it's undefined at translation time or execution time, and the
2056 normal atomic sequences again wouldn't work. */
2057 while (handled_component_p (expr))
2059 if (TREE_CODE (expr) == COMPONENT_REF
2060 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2061 return false;
2062 expr = TREE_OPERAND (expr, 0);
2064 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2065 return false;
2066 return true;
2069 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2070 including converting functions and arrays to pointers if CONVERT_P.
2071 If READ_P, also mark the expression as having been read. */
2073 struct c_expr
2074 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2075 bool convert_p, bool read_p)
2077 if (read_p)
2078 mark_exp_read (exp.value);
2079 if (convert_p)
2080 exp = default_function_array_conversion (loc, exp);
2081 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2082 exp.value = require_complete_type (loc, exp.value);
2083 if (really_atomic_lvalue (exp.value))
2085 vec<tree, va_gc> *params;
2086 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2087 tree expr_type = TREE_TYPE (exp.value);
2088 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2089 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2091 gcc_assert (TYPE_ATOMIC (expr_type));
2093 /* Expansion of a generic atomic load may require an addition
2094 element, so allocate enough to prevent a resize. */
2095 vec_alloc (params, 4);
2097 /* Remove the qualifiers for the rest of the expressions and
2098 create the VAL temp variable to hold the RHS. */
2099 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2100 tmp = create_tmp_var_raw (nonatomic_type);
2101 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2102 TREE_ADDRESSABLE (tmp) = 1;
2103 TREE_NO_WARNING (tmp) = 1;
2105 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2106 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2107 params->quick_push (expr_addr);
2108 params->quick_push (tmp_addr);
2109 params->quick_push (seq_cst);
2110 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2112 /* EXPR is always read. */
2113 mark_exp_read (exp.value);
2115 /* Return tmp which contains the value loaded. */
2116 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2117 NULL_TREE, NULL_TREE);
2119 return exp;
2122 /* EXP is an expression of integer type. Apply the integer promotions
2123 to it and return the promoted value. */
2125 tree
2126 perform_integral_promotions (tree exp)
2128 tree type = TREE_TYPE (exp);
2129 enum tree_code code = TREE_CODE (type);
2131 gcc_assert (INTEGRAL_TYPE_P (type));
2133 /* Normally convert enums to int,
2134 but convert wide enums to something wider. */
2135 if (code == ENUMERAL_TYPE)
2137 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2138 TYPE_PRECISION (integer_type_node)),
2139 ((TYPE_PRECISION (type)
2140 >= TYPE_PRECISION (integer_type_node))
2141 && TYPE_UNSIGNED (type)));
2143 return convert (type, exp);
2146 /* ??? This should no longer be needed now bit-fields have their
2147 proper types. */
2148 if (TREE_CODE (exp) == COMPONENT_REF
2149 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2150 /* If it's thinner than an int, promote it like a
2151 c_promoting_integer_type_p, otherwise leave it alone. */
2152 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2153 TYPE_PRECISION (integer_type_node)) < 0)
2154 return convert (integer_type_node, exp);
2156 if (c_promoting_integer_type_p (type))
2158 /* Preserve unsignedness if not really getting any wider. */
2159 if (TYPE_UNSIGNED (type)
2160 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2161 return convert (unsigned_type_node, exp);
2163 return convert (integer_type_node, exp);
2166 return exp;
2170 /* Perform default promotions for C data used in expressions.
2171 Enumeral types or short or char are converted to int.
2172 In addition, manifest constants symbols are replaced by their values. */
2174 tree
2175 default_conversion (tree exp)
2177 tree orig_exp;
2178 tree type = TREE_TYPE (exp);
2179 enum tree_code code = TREE_CODE (type);
2180 tree promoted_type;
2182 mark_exp_read (exp);
2184 /* Functions and arrays have been converted during parsing. */
2185 gcc_assert (code != FUNCTION_TYPE);
2186 if (code == ARRAY_TYPE)
2187 return exp;
2189 /* Constants can be used directly unless they're not loadable. */
2190 if (TREE_CODE (exp) == CONST_DECL)
2191 exp = DECL_INITIAL (exp);
2193 /* Strip no-op conversions. */
2194 orig_exp = exp;
2195 STRIP_TYPE_NOPS (exp);
2197 if (TREE_NO_WARNING (orig_exp))
2198 TREE_NO_WARNING (exp) = 1;
2200 if (code == VOID_TYPE)
2202 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2203 "void value not ignored as it ought to be");
2204 return error_mark_node;
2207 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2208 if (exp == error_mark_node)
2209 return error_mark_node;
2211 promoted_type = targetm.promoted_type (type);
2212 if (promoted_type)
2213 return convert (promoted_type, exp);
2215 if (INTEGRAL_TYPE_P (type))
2216 return perform_integral_promotions (exp);
2218 return exp;
2221 /* Look up COMPONENT in a structure or union TYPE.
2223 If the component name is not found, returns NULL_TREE. Otherwise,
2224 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2225 stepping down the chain to the component, which is in the last
2226 TREE_VALUE of the list. Normally the list is of length one, but if
2227 the component is embedded within (nested) anonymous structures or
2228 unions, the list steps down the chain to the component. */
2230 static tree
2231 lookup_field (tree type, tree component)
2233 tree field;
2235 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2236 to the field elements. Use a binary search on this array to quickly
2237 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2238 will always be set for structures which have many elements.
2240 Duplicate field checking replaces duplicates with NULL_TREE so
2241 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2242 case just iterate using DECL_CHAIN. */
2244 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2245 && !seen_error ())
2247 int bot, top, half;
2248 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2250 field = TYPE_FIELDS (type);
2251 bot = 0;
2252 top = TYPE_LANG_SPECIFIC (type)->s->len;
2253 while (top - bot > 1)
2255 half = (top - bot + 1) >> 1;
2256 field = field_array[bot+half];
2258 if (DECL_NAME (field) == NULL_TREE)
2260 /* Step through all anon unions in linear fashion. */
2261 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2263 field = field_array[bot++];
2264 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2266 tree anon = lookup_field (TREE_TYPE (field), component);
2268 if (anon)
2269 return tree_cons (NULL_TREE, field, anon);
2271 /* The Plan 9 compiler permits referring
2272 directly to an anonymous struct/union field
2273 using a typedef name. */
2274 if (flag_plan9_extensions
2275 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2276 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2277 == TYPE_DECL)
2278 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2279 == component))
2280 break;
2284 /* Entire record is only anon unions. */
2285 if (bot > top)
2286 return NULL_TREE;
2288 /* Restart the binary search, with new lower bound. */
2289 continue;
2292 if (DECL_NAME (field) == component)
2293 break;
2294 if (DECL_NAME (field) < component)
2295 bot += half;
2296 else
2297 top = bot + half;
2300 if (DECL_NAME (field_array[bot]) == component)
2301 field = field_array[bot];
2302 else if (DECL_NAME (field) != component)
2303 return NULL_TREE;
2305 else
2307 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2309 if (DECL_NAME (field) == NULL_TREE
2310 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2312 tree anon = lookup_field (TREE_TYPE (field), component);
2314 if (anon)
2315 return tree_cons (NULL_TREE, field, anon);
2317 /* The Plan 9 compiler permits referring directly to an
2318 anonymous struct/union field using a typedef
2319 name. */
2320 if (flag_plan9_extensions
2321 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2322 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2323 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2324 == component))
2325 break;
2328 if (DECL_NAME (field) == component)
2329 break;
2332 if (field == NULL_TREE)
2333 return NULL_TREE;
2336 return tree_cons (NULL_TREE, field, NULL_TREE);
2339 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2341 static void
2342 lookup_field_fuzzy_find_candidates (tree type, tree component,
2343 vec<tree> *candidates)
2345 tree field;
2346 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2348 if (DECL_NAME (field) == NULL_TREE
2349 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2350 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2351 candidates);
2353 if (DECL_NAME (field))
2354 candidates->safe_push (DECL_NAME (field));
2358 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2359 rather than returning a TREE_LIST for an exact match. */
2361 static tree
2362 lookup_field_fuzzy (tree type, tree component)
2364 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2366 /* First, gather a list of candidates. */
2367 auto_vec <tree> candidates;
2369 lookup_field_fuzzy_find_candidates (type, component,
2370 &candidates);
2372 return find_closest_identifier (component, &candidates);
2375 /* Support function for build_component_ref's error-handling.
2377 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2378 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2380 static bool
2381 should_suggest_deref_p (tree datum_type)
2383 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2384 allows "." for ptrs; we could be handling a failed attempt
2385 to access a property. */
2386 if (c_dialect_objc ())
2387 return false;
2389 /* Only suggest it for pointers... */
2390 if (TREE_CODE (datum_type) != POINTER_TYPE)
2391 return false;
2393 /* ...to structs/unions. */
2394 tree underlying_type = TREE_TYPE (datum_type);
2395 enum tree_code code = TREE_CODE (underlying_type);
2396 if (code == RECORD_TYPE || code == UNION_TYPE)
2397 return true;
2398 else
2399 return false;
2402 /* Make an expression to refer to the COMPONENT field of structure or
2403 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2404 location of the COMPONENT_REF. COMPONENT_LOC is the location
2405 of COMPONENT. */
2407 tree
2408 build_component_ref (location_t loc, tree datum, tree component,
2409 location_t component_loc)
2411 tree type = TREE_TYPE (datum);
2412 enum tree_code code = TREE_CODE (type);
2413 tree field = NULL;
2414 tree ref;
2415 bool datum_lvalue = lvalue_p (datum);
2417 if (!objc_is_public (datum, component))
2418 return error_mark_node;
2420 /* Detect Objective-C property syntax object.property. */
2421 if (c_dialect_objc ()
2422 && (ref = objc_maybe_build_component_ref (datum, component)))
2423 return ref;
2425 /* See if there is a field or component with name COMPONENT. */
2427 if (code == RECORD_TYPE || code == UNION_TYPE)
2429 if (!COMPLETE_TYPE_P (type))
2431 c_incomplete_type_error (loc, NULL_TREE, type);
2432 return error_mark_node;
2435 field = lookup_field (type, component);
2437 if (!field)
2439 tree guessed_id = lookup_field_fuzzy (type, component);
2440 if (guessed_id)
2442 /* Attempt to provide a fixit replacement hint, if
2443 we have a valid range for the component. */
2444 location_t reported_loc
2445 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2446 gcc_rich_location rich_loc (reported_loc);
2447 if (component_loc != UNKNOWN_LOCATION)
2448 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2449 error_at (&rich_loc,
2450 "%qT has no member named %qE; did you mean %qE?",
2451 type, component, guessed_id);
2453 else
2454 error_at (loc, "%qT has no member named %qE", type, component);
2455 return error_mark_node;
2458 /* Accessing elements of atomic structures or unions is undefined
2459 behavior (C11 6.5.2.3#5). */
2460 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2462 if (code == RECORD_TYPE)
2463 warning_at (loc, 0, "accessing a member %qE of an atomic "
2464 "structure %qE", component, datum);
2465 else
2466 warning_at (loc, 0, "accessing a member %qE of an atomic "
2467 "union %qE", component, datum);
2470 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2471 This might be better solved in future the way the C++ front
2472 end does it - by giving the anonymous entities each a
2473 separate name and type, and then have build_component_ref
2474 recursively call itself. We can't do that here. */
2477 tree subdatum = TREE_VALUE (field);
2478 int quals;
2479 tree subtype;
2480 bool use_datum_quals;
2482 if (TREE_TYPE (subdatum) == error_mark_node)
2483 return error_mark_node;
2485 /* If this is an rvalue, it does not have qualifiers in C
2486 standard terms and we must avoid propagating such
2487 qualifiers down to a non-lvalue array that is then
2488 converted to a pointer. */
2489 use_datum_quals = (datum_lvalue
2490 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2492 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2493 if (use_datum_quals)
2494 quals |= TYPE_QUALS (TREE_TYPE (datum));
2495 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2497 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2498 NULL_TREE);
2499 SET_EXPR_LOCATION (ref, loc);
2500 if (TREE_READONLY (subdatum)
2501 || (use_datum_quals && TREE_READONLY (datum)))
2502 TREE_READONLY (ref) = 1;
2503 if (TREE_THIS_VOLATILE (subdatum)
2504 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2505 TREE_THIS_VOLATILE (ref) = 1;
2507 if (TREE_DEPRECATED (subdatum))
2508 warn_deprecated_use (subdatum, NULL_TREE);
2510 datum = ref;
2512 field = TREE_CHAIN (field);
2514 while (field);
2516 return ref;
2518 else if (should_suggest_deref_p (type))
2520 /* Special-case the error message for "ptr.field" for the case
2521 where the user has confused "." vs "->". */
2522 rich_location richloc (line_table, loc);
2523 /* "loc" should be the "." token. */
2524 richloc.add_fixit_replace ("->");
2525 error_at (&richloc,
2526 "%qE is a pointer; did you mean to use %<->%>?",
2527 datum);
2528 return error_mark_node;
2530 else if (code != ERROR_MARK)
2531 error_at (loc,
2532 "request for member %qE in something not a structure or union",
2533 component);
2535 return error_mark_node;
2538 /* Given an expression PTR for a pointer, return an expression
2539 for the value pointed to.
2540 ERRORSTRING is the name of the operator to appear in error messages.
2542 LOC is the location to use for the generated tree. */
2544 tree
2545 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2547 tree pointer = default_conversion (ptr);
2548 tree type = TREE_TYPE (pointer);
2549 tree ref;
2551 if (TREE_CODE (type) == POINTER_TYPE)
2553 if (CONVERT_EXPR_P (pointer)
2554 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2556 /* If a warning is issued, mark it to avoid duplicates from
2557 the backend. This only needs to be done at
2558 warn_strict_aliasing > 2. */
2559 if (warn_strict_aliasing > 2)
2560 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2561 type, TREE_OPERAND (pointer, 0)))
2562 TREE_NO_WARNING (pointer) = 1;
2565 if (TREE_CODE (pointer) == ADDR_EXPR
2566 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2567 == TREE_TYPE (type)))
2569 ref = TREE_OPERAND (pointer, 0);
2570 protected_set_expr_location (ref, loc);
2571 return ref;
2573 else
2575 tree t = TREE_TYPE (type);
2577 ref = build1 (INDIRECT_REF, t, pointer);
2579 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2580 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2582 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2583 so that we get the proper error message if the result is used
2584 to assign to. Also, &* is supposed to be a no-op.
2585 And ANSI C seems to specify that the type of the result
2586 should be the const type. */
2587 /* A de-reference of a pointer to const is not a const. It is valid
2588 to change it via some other pointer. */
2589 TREE_READONLY (ref) = TYPE_READONLY (t);
2590 TREE_SIDE_EFFECTS (ref)
2591 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2592 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2593 protected_set_expr_location (ref, loc);
2594 return ref;
2597 else if (TREE_CODE (pointer) != ERROR_MARK)
2598 invalid_indirection_error (loc, type, errstring);
2600 return error_mark_node;
2603 /* This handles expressions of the form "a[i]", which denotes
2604 an array reference.
2606 This is logically equivalent in C to *(a+i), but we may do it differently.
2607 If A is a variable or a member, we generate a primitive ARRAY_REF.
2608 This avoids forcing the array out of registers, and can work on
2609 arrays that are not lvalues (for example, members of structures returned
2610 by functions).
2612 For vector types, allow vector[i] but not i[vector], and create
2613 *(((type*)&vectortype) + i) for the expression.
2615 LOC is the location to use for the returned expression. */
2617 tree
2618 build_array_ref (location_t loc, tree array, tree index)
2620 tree ret;
2621 bool swapped = false;
2622 if (TREE_TYPE (array) == error_mark_node
2623 || TREE_TYPE (index) == error_mark_node)
2624 return error_mark_node;
2626 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2627 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2628 /* Allow vector[index] but not index[vector]. */
2629 && !gnu_vector_type_p (TREE_TYPE (array)))
2631 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2632 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2634 error_at (loc,
2635 "subscripted value is neither array nor pointer nor vector");
2637 return error_mark_node;
2639 std::swap (array, index);
2640 swapped = true;
2643 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2645 error_at (loc, "array subscript is not an integer");
2646 return error_mark_node;
2649 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2651 error_at (loc, "subscripted value is pointer to function");
2652 return error_mark_node;
2655 /* ??? Existing practice has been to warn only when the char
2656 index is syntactically the index, not for char[array]. */
2657 if (!swapped)
2658 warn_array_subscript_with_type_char (loc, index);
2660 /* Apply default promotions *after* noticing character types. */
2661 index = default_conversion (index);
2662 if (index == error_mark_node)
2663 return error_mark_node;
2665 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2667 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2668 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2670 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2672 tree rval, type;
2674 /* An array that is indexed by a non-constant
2675 cannot be stored in a register; we must be able to do
2676 address arithmetic on its address.
2677 Likewise an array of elements of variable size. */
2678 if (TREE_CODE (index) != INTEGER_CST
2679 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2680 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2682 if (!c_mark_addressable (array, true))
2683 return error_mark_node;
2685 /* An array that is indexed by a constant value which is not within
2686 the array bounds cannot be stored in a register either; because we
2687 would get a crash in store_bit_field/extract_bit_field when trying
2688 to access a non-existent part of the register. */
2689 if (TREE_CODE (index) == INTEGER_CST
2690 && TYPE_DOMAIN (TREE_TYPE (array))
2691 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2693 if (!c_mark_addressable (array))
2694 return error_mark_node;
2697 if ((pedantic || warn_c90_c99_compat)
2698 && ! was_vector)
2700 tree foo = array;
2701 while (TREE_CODE (foo) == COMPONENT_REF)
2702 foo = TREE_OPERAND (foo, 0);
2703 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2704 pedwarn (loc, OPT_Wpedantic,
2705 "ISO C forbids subscripting %<register%> array");
2706 else if (!lvalue_p (foo))
2707 pedwarn_c90 (loc, OPT_Wpedantic,
2708 "ISO C90 forbids subscripting non-lvalue "
2709 "array");
2712 type = TREE_TYPE (TREE_TYPE (array));
2713 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2714 /* Array ref is const/volatile if the array elements are
2715 or if the array is. */
2716 TREE_READONLY (rval)
2717 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2718 | TREE_READONLY (array));
2719 TREE_SIDE_EFFECTS (rval)
2720 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2721 | TREE_SIDE_EFFECTS (array));
2722 TREE_THIS_VOLATILE (rval)
2723 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2724 /* This was added by rms on 16 Nov 91.
2725 It fixes vol struct foo *a; a->elts[1]
2726 in an inline function.
2727 Hope it doesn't break something else. */
2728 | TREE_THIS_VOLATILE (array));
2729 ret = require_complete_type (loc, rval);
2730 protected_set_expr_location (ret, loc);
2731 if (non_lvalue)
2732 ret = non_lvalue_loc (loc, ret);
2733 return ret;
2735 else
2737 tree ar = default_conversion (array);
2739 if (ar == error_mark_node)
2740 return ar;
2742 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2743 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2745 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2746 index, false),
2747 RO_ARRAY_INDEXING);
2748 if (non_lvalue)
2749 ret = non_lvalue_loc (loc, ret);
2750 return ret;
2754 /* Build an external reference to identifier ID. FUN indicates
2755 whether this will be used for a function call. LOC is the source
2756 location of the identifier. This sets *TYPE to the type of the
2757 identifier, which is not the same as the type of the returned value
2758 for CONST_DECLs defined as enum constants. If the type of the
2759 identifier is not available, *TYPE is set to NULL. */
2760 tree
2761 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2763 tree ref;
2764 tree decl = lookup_name (id);
2766 /* In Objective-C, an instance variable (ivar) may be preferred to
2767 whatever lookup_name() found. */
2768 decl = objc_lookup_ivar (decl, id);
2770 *type = NULL;
2771 if (decl && decl != error_mark_node)
2773 ref = decl;
2774 *type = TREE_TYPE (ref);
2776 else if (fun)
2777 /* Implicit function declaration. */
2778 ref = implicitly_declare (loc, id);
2779 else if (decl == error_mark_node)
2780 /* Don't complain about something that's already been
2781 complained about. */
2782 return error_mark_node;
2783 else
2785 undeclared_variable (loc, id);
2786 return error_mark_node;
2789 if (TREE_TYPE (ref) == error_mark_node)
2790 return error_mark_node;
2792 if (TREE_DEPRECATED (ref))
2793 warn_deprecated_use (ref, NULL_TREE);
2795 /* Recursive call does not count as usage. */
2796 if (ref != current_function_decl)
2798 TREE_USED (ref) = 1;
2801 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2803 if (!in_sizeof && !in_typeof)
2804 C_DECL_USED (ref) = 1;
2805 else if (DECL_INITIAL (ref) == NULL_TREE
2806 && DECL_EXTERNAL (ref)
2807 && !TREE_PUBLIC (ref))
2808 record_maybe_used_decl (ref);
2811 if (TREE_CODE (ref) == CONST_DECL)
2813 used_types_insert (TREE_TYPE (ref));
2815 if (warn_cxx_compat
2816 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2817 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2819 warning_at (loc, OPT_Wc___compat,
2820 ("enum constant defined in struct or union "
2821 "is not visible in C++"));
2822 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2825 ref = DECL_INITIAL (ref);
2826 TREE_CONSTANT (ref) = 1;
2828 else if (current_function_decl != NULL_TREE
2829 && !DECL_FILE_SCOPE_P (current_function_decl)
2830 && (VAR_OR_FUNCTION_DECL_P (ref)
2831 || TREE_CODE (ref) == PARM_DECL))
2833 tree context = decl_function_context (ref);
2835 if (context != NULL_TREE && context != current_function_decl)
2836 DECL_NONLOCAL (ref) = 1;
2838 /* C99 6.7.4p3: An inline definition of a function with external
2839 linkage ... shall not contain a reference to an identifier with
2840 internal linkage. */
2841 else if (current_function_decl != NULL_TREE
2842 && DECL_DECLARED_INLINE_P (current_function_decl)
2843 && DECL_EXTERNAL (current_function_decl)
2844 && VAR_OR_FUNCTION_DECL_P (ref)
2845 && (!VAR_P (ref) || TREE_STATIC (ref))
2846 && ! TREE_PUBLIC (ref)
2847 && DECL_CONTEXT (ref) != current_function_decl)
2848 record_inline_static (loc, current_function_decl, ref,
2849 csi_internal);
2851 return ref;
2854 /* Record details of decls possibly used inside sizeof or typeof. */
2855 struct maybe_used_decl
2857 /* The decl. */
2858 tree decl;
2859 /* The level seen at (in_sizeof + in_typeof). */
2860 int level;
2861 /* The next one at this level or above, or NULL. */
2862 struct maybe_used_decl *next;
2865 static struct maybe_used_decl *maybe_used_decls;
2867 /* Record that DECL, an undefined static function reference seen
2868 inside sizeof or typeof, might be used if the operand of sizeof is
2869 a VLA type or the operand of typeof is a variably modified
2870 type. */
2872 static void
2873 record_maybe_used_decl (tree decl)
2875 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2876 t->decl = decl;
2877 t->level = in_sizeof + in_typeof;
2878 t->next = maybe_used_decls;
2879 maybe_used_decls = t;
2882 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2883 USED is false, just discard them. If it is true, mark them used
2884 (if no longer inside sizeof or typeof) or move them to the next
2885 level up (if still inside sizeof or typeof). */
2887 void
2888 pop_maybe_used (bool used)
2890 struct maybe_used_decl *p = maybe_used_decls;
2891 int cur_level = in_sizeof + in_typeof;
2892 while (p && p->level > cur_level)
2894 if (used)
2896 if (cur_level == 0)
2897 C_DECL_USED (p->decl) = 1;
2898 else
2899 p->level = cur_level;
2901 p = p->next;
2903 if (!used || cur_level == 0)
2904 maybe_used_decls = p;
2907 /* Return the result of sizeof applied to EXPR. */
2909 struct c_expr
2910 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2912 struct c_expr ret;
2913 if (expr.value == error_mark_node)
2915 ret.value = error_mark_node;
2916 ret.original_code = ERROR_MARK;
2917 ret.original_type = NULL;
2918 pop_maybe_used (false);
2920 else
2922 bool expr_const_operands = true;
2924 if (TREE_CODE (expr.value) == PARM_DECL
2925 && C_ARRAY_PARAMETER (expr.value))
2927 auto_diagnostic_group d;
2928 if (warning_at (loc, OPT_Wsizeof_array_argument,
2929 "%<sizeof%> on array function parameter %qE will "
2930 "return size of %qT", expr.value,
2931 TREE_TYPE (expr.value)))
2932 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2934 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2935 &expr_const_operands);
2936 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2937 c_last_sizeof_arg = expr.value;
2938 c_last_sizeof_loc = loc;
2939 ret.original_code = SIZEOF_EXPR;
2940 ret.original_type = NULL;
2941 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2943 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2944 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2945 folded_expr, ret.value);
2946 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2947 SET_EXPR_LOCATION (ret.value, loc);
2949 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2951 return ret;
2954 /* Return the result of sizeof applied to T, a structure for the type
2955 name passed to sizeof (rather than the type itself). LOC is the
2956 location of the original expression. */
2958 struct c_expr
2959 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2961 tree type;
2962 struct c_expr ret;
2963 tree type_expr = NULL_TREE;
2964 bool type_expr_const = true;
2965 type = groktypename (t, &type_expr, &type_expr_const);
2966 ret.value = c_sizeof (loc, type);
2967 c_last_sizeof_arg = type;
2968 c_last_sizeof_loc = loc;
2969 ret.original_code = SIZEOF_EXPR;
2970 ret.original_type = NULL;
2971 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2972 && c_vla_type_p (type))
2974 /* If the type is a [*] array, it is a VLA but is represented as
2975 having a size of zero. In such a case we must ensure that
2976 the result of sizeof does not get folded to a constant by
2977 c_fully_fold, because if the size is evaluated the result is
2978 not constant and so constraints on zero or negative size
2979 arrays must not be applied when this sizeof call is inside
2980 another array declarator. */
2981 if (!type_expr)
2982 type_expr = integer_zero_node;
2983 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2984 type_expr, ret.value);
2985 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2987 pop_maybe_used (type != error_mark_node
2988 ? C_TYPE_VARIABLE_SIZE (type) : false);
2989 return ret;
2992 /* Build a function call to function FUNCTION with parameters PARAMS.
2993 The function call is at LOC.
2994 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2995 TREE_VALUE of each node is a parameter-expression.
2996 FUNCTION's data type may be a function type or a pointer-to-function. */
2998 tree
2999 build_function_call (location_t loc, tree function, tree params)
3001 vec<tree, va_gc> *v;
3002 tree ret;
3004 vec_alloc (v, list_length (params));
3005 for (; params; params = TREE_CHAIN (params))
3006 v->quick_push (TREE_VALUE (params));
3007 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3008 vec_free (v);
3009 return ret;
3012 /* Give a note about the location of the declaration of DECL. */
3014 static void
3015 inform_declaration (tree decl)
3017 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
3018 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3021 /* Build a function call to function FUNCTION with parameters PARAMS.
3022 If FUNCTION is the result of resolving an overloaded target built-in,
3023 ORIG_FUNDECL is the original function decl, otherwise it is null.
3024 ORIGTYPES, if not NULL, is a vector of types; each element is
3025 either NULL or the original type of the corresponding element in
3026 PARAMS. The original type may differ from TREE_TYPE of the
3027 parameter for enums. FUNCTION's data type may be a function type
3028 or pointer-to-function. This function changes the elements of
3029 PARAMS. */
3031 tree
3032 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3033 tree function, vec<tree, va_gc> *params,
3034 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3036 tree fntype, fundecl = NULL_TREE;
3037 tree name = NULL_TREE, result;
3038 tree tem;
3039 int nargs;
3040 tree *argarray;
3043 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3044 STRIP_TYPE_NOPS (function);
3046 /* Convert anything with function type to a pointer-to-function. */
3047 if (TREE_CODE (function) == FUNCTION_DECL)
3049 name = DECL_NAME (function);
3051 if (flag_tm)
3052 tm_malloc_replacement (function);
3053 fundecl = function;
3054 if (!orig_fundecl)
3055 orig_fundecl = fundecl;
3056 /* Atomic functions have type checking/casting already done. They are
3057 often rewritten and don't match the original parameter list. */
3058 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3059 origtypes = NULL;
3061 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3062 function = function_to_pointer_conversion (loc, function);
3064 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3065 expressions, like those used for ObjC messenger dispatches. */
3066 if (params && !params->is_empty ())
3067 function = objc_rewrite_function_call (function, (*params)[0]);
3069 function = c_fully_fold (function, false, NULL);
3071 fntype = TREE_TYPE (function);
3073 if (TREE_CODE (fntype) == ERROR_MARK)
3074 return error_mark_node;
3076 if (!(TREE_CODE (fntype) == POINTER_TYPE
3077 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3079 if (!flag_diagnostics_show_caret)
3080 error_at (loc,
3081 "called object %qE is not a function or function pointer",
3082 function);
3083 else if (DECL_P (function))
3085 error_at (loc,
3086 "called object %qD is not a function or function pointer",
3087 function);
3088 inform_declaration (function);
3090 else
3091 error_at (loc,
3092 "called object is not a function or function pointer");
3093 return error_mark_node;
3096 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3097 current_function_returns_abnormally = 1;
3099 /* fntype now gets the type of function pointed to. */
3100 fntype = TREE_TYPE (fntype);
3102 /* Convert the parameters to the types declared in the
3103 function prototype, or apply default promotions. */
3105 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3106 origtypes, function, fundecl);
3107 if (nargs < 0)
3108 return error_mark_node;
3110 /* Check that the function is called through a compatible prototype.
3111 If it is not, warn. */
3112 if (CONVERT_EXPR_P (function)
3113 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3114 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3115 && !comptypes (fntype, TREE_TYPE (tem)))
3117 tree return_type = TREE_TYPE (fntype);
3119 /* This situation leads to run-time undefined behavior. We can't,
3120 therefore, simply error unless we can prove that all possible
3121 executions of the program must execute the code. */
3122 warning_at (loc, 0, "function called through a non-compatible type");
3124 if (VOID_TYPE_P (return_type)
3125 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3126 pedwarn (loc, 0,
3127 "function with qualified void return type called");
3130 argarray = vec_safe_address (params);
3132 /* Check that arguments to builtin functions match the expectations. */
3133 if (fundecl
3134 && fndecl_built_in_p (fundecl)
3135 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3136 orig_fundecl, nargs, argarray))
3137 return error_mark_node;
3139 /* Check that the arguments to the function are valid. */
3140 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3141 nargs, argarray, &arg_loc);
3143 if (name != NULL_TREE
3144 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3146 if (require_constant_value)
3147 result
3148 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3149 function, nargs, argarray);
3150 else
3151 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3152 function, nargs, argarray);
3153 if (TREE_CODE (result) == NOP_EXPR
3154 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3155 STRIP_TYPE_NOPS (result);
3157 else
3158 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3159 function, nargs, argarray);
3160 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3161 later. */
3162 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3163 TREE_NO_WARNING (result) = 1;
3165 /* In this improbable scenario, a nested function returns a VM type.
3166 Create a TARGET_EXPR so that the call always has a LHS, much as
3167 what the C++ FE does for functions returning non-PODs. */
3168 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3170 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3171 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3172 NULL_TREE, NULL_TREE);
3175 if (VOID_TYPE_P (TREE_TYPE (result)))
3177 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3178 pedwarn (loc, 0,
3179 "function with qualified void return type called");
3180 return result;
3182 return require_complete_type (loc, result);
3185 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3187 tree
3188 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3189 tree function, vec<tree, va_gc> *params,
3190 vec<tree, va_gc> *origtypes)
3192 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3193 STRIP_TYPE_NOPS (function);
3195 /* Convert anything with function type to a pointer-to-function. */
3196 if (TREE_CODE (function) == FUNCTION_DECL)
3198 /* Implement type-directed function overloading for builtins.
3199 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3200 handle all the type checking. The result is a complete expression
3201 that implements this function call. */
3202 tree tem = resolve_overloaded_builtin (loc, function, params);
3203 if (tem)
3204 return tem;
3206 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3209 /* Helper for convert_arguments called to convert the VALue of argument
3210 number ARGNUM from ORIGTYPE to the corresponding parameter number
3211 PARMNUM and TYPE.
3212 PLOC is the location where the conversion is being performed.
3213 FUNCTION and FUNDECL are the same as in convert_arguments.
3214 VALTYPE is the original type of VAL before the conversion and,
3215 for EXCESS_PRECISION_EXPR, the operand of the expression.
3216 NPC is true if VAL represents the null pointer constant (VAL itself
3217 will have been folded to an integer constant).
3218 RNAME is the same as FUNCTION except in Objective C when it's
3219 the function selector.
3220 EXCESS_PRECISION is true when VAL was originally represented
3221 as EXCESS_PRECISION_EXPR.
3222 WARNOPT is the same as in convert_for_assignment. */
3224 static tree
3225 convert_argument (location_t ploc, tree function, tree fundecl,
3226 tree type, tree origtype, tree val, tree valtype,
3227 bool npc, tree rname, int parmnum, int argnum,
3228 bool excess_precision, int warnopt)
3230 /* Formal parm type is specified by a function prototype. */
3232 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3234 error_at (ploc, "type of formal parameter %d is incomplete",
3235 parmnum + 1);
3236 return val;
3239 /* Optionally warn about conversions that differ from the default
3240 conversions. */
3241 if (warn_traditional_conversion || warn_traditional)
3243 unsigned int formal_prec = TYPE_PRECISION (type);
3245 if (INTEGRAL_TYPE_P (type)
3246 && TREE_CODE (valtype) == REAL_TYPE)
3247 warning_at (ploc, OPT_Wtraditional_conversion,
3248 "passing argument %d of %qE as integer rather "
3249 "than floating due to prototype",
3250 argnum, rname);
3251 if (INTEGRAL_TYPE_P (type)
3252 && TREE_CODE (valtype) == COMPLEX_TYPE)
3253 warning_at (ploc, OPT_Wtraditional_conversion,
3254 "passing argument %d of %qE as integer rather "
3255 "than complex due to prototype",
3256 argnum, rname);
3257 else if (TREE_CODE (type) == COMPLEX_TYPE
3258 && TREE_CODE (valtype) == REAL_TYPE)
3259 warning_at (ploc, OPT_Wtraditional_conversion,
3260 "passing argument %d of %qE as complex rather "
3261 "than floating due to prototype",
3262 argnum, rname);
3263 else if (TREE_CODE (type) == REAL_TYPE
3264 && INTEGRAL_TYPE_P (valtype))
3265 warning_at (ploc, OPT_Wtraditional_conversion,
3266 "passing argument %d of %qE as floating rather "
3267 "than integer due to prototype",
3268 argnum, rname);
3269 else if (TREE_CODE (type) == COMPLEX_TYPE
3270 && INTEGRAL_TYPE_P (valtype))
3271 warning_at (ploc, OPT_Wtraditional_conversion,
3272 "passing argument %d of %qE as complex rather "
3273 "than integer due to prototype",
3274 argnum, rname);
3275 else if (TREE_CODE (type) == REAL_TYPE
3276 && TREE_CODE (valtype) == COMPLEX_TYPE)
3277 warning_at (ploc, OPT_Wtraditional_conversion,
3278 "passing argument %d of %qE as floating rather "
3279 "than complex due to prototype",
3280 argnum, rname);
3281 /* ??? At some point, messages should be written about
3282 conversions between complex types, but that's too messy
3283 to do now. */
3284 else if (TREE_CODE (type) == REAL_TYPE
3285 && TREE_CODE (valtype) == REAL_TYPE)
3287 /* Warn if any argument is passed as `float',
3288 since without a prototype it would be `double'. */
3289 if (formal_prec == TYPE_PRECISION (float_type_node)
3290 && type != dfloat32_type_node)
3291 warning_at (ploc, 0,
3292 "passing argument %d of %qE as %<float%> "
3293 "rather than %<double%> due to prototype",
3294 argnum, rname);
3296 /* Warn if mismatch between argument and prototype
3297 for decimal float types. Warn of conversions with
3298 binary float types and of precision narrowing due to
3299 prototype. */
3300 else if (type != valtype
3301 && (type == dfloat32_type_node
3302 || type == dfloat64_type_node
3303 || type == dfloat128_type_node
3304 || valtype == dfloat32_type_node
3305 || valtype == dfloat64_type_node
3306 || valtype == dfloat128_type_node)
3307 && (formal_prec
3308 <= TYPE_PRECISION (valtype)
3309 || (type == dfloat128_type_node
3310 && (valtype
3311 != dfloat64_type_node
3312 && (valtype
3313 != dfloat32_type_node)))
3314 || (type == dfloat64_type_node
3315 && (valtype
3316 != dfloat32_type_node))))
3317 warning_at (ploc, 0,
3318 "passing argument %d of %qE as %qT "
3319 "rather than %qT due to prototype",
3320 argnum, rname, type, valtype);
3323 /* Detect integer changing in width or signedness.
3324 These warnings are only activated with
3325 -Wtraditional-conversion, not with -Wtraditional. */
3326 else if (warn_traditional_conversion
3327 && INTEGRAL_TYPE_P (type)
3328 && INTEGRAL_TYPE_P (valtype))
3330 tree would_have_been = default_conversion (val);
3331 tree type1 = TREE_TYPE (would_have_been);
3333 if (val == error_mark_node)
3334 /* VAL could have been of incomplete type. */;
3335 else if (TREE_CODE (type) == ENUMERAL_TYPE
3336 && (TYPE_MAIN_VARIANT (type)
3337 == TYPE_MAIN_VARIANT (valtype)))
3338 /* No warning if function asks for enum
3339 and the actual arg is that enum type. */
3341 else if (formal_prec != TYPE_PRECISION (type1))
3342 warning_at (ploc, OPT_Wtraditional_conversion,
3343 "passing argument %d of %qE "
3344 "with different width due to prototype",
3345 argnum, rname);
3346 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3348 /* Don't complain if the formal parameter type
3349 is an enum, because we can't tell now whether
3350 the value was an enum--even the same enum. */
3351 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3353 else if (TREE_CODE (val) == INTEGER_CST
3354 && int_fits_type_p (val, type))
3355 /* Change in signedness doesn't matter
3356 if a constant value is unaffected. */
3358 /* If the value is extended from a narrower
3359 unsigned type, it doesn't matter whether we
3360 pass it as signed or unsigned; the value
3361 certainly is the same either way. */
3362 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3363 && TYPE_UNSIGNED (valtype))
3365 else if (TYPE_UNSIGNED (type))
3366 warning_at (ploc, OPT_Wtraditional_conversion,
3367 "passing argument %d of %qE "
3368 "as unsigned due to prototype",
3369 argnum, rname);
3370 else
3371 warning_at (ploc, OPT_Wtraditional_conversion,
3372 "passing argument %d of %qE "
3373 "as signed due to prototype",
3374 argnum, rname);
3378 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3379 sake of better warnings from convert_and_check. */
3380 if (excess_precision)
3381 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3383 tree parmval = convert_for_assignment (ploc, ploc, type,
3384 val, origtype, ic_argpass,
3385 npc, fundecl, function,
3386 parmnum + 1, warnopt);
3388 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3389 && INTEGRAL_TYPE_P (type)
3390 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3391 parmval = default_conversion (parmval);
3393 return parmval;
3396 /* Convert the argument expressions in the vector VALUES
3397 to the types in the list TYPELIST.
3399 If TYPELIST is exhausted, or when an element has NULL as its type,
3400 perform the default conversions.
3402 ORIGTYPES is the original types of the expressions in VALUES. This
3403 holds the type of enum values which have been converted to integral
3404 types. It may be NULL.
3406 FUNCTION is a tree for the called function. It is used only for
3407 error messages, where it is formatted with %qE.
3409 This is also where warnings about wrong number of args are generated.
3411 ARG_LOC are locations of function arguments (if any).
3413 Returns the actual number of arguments processed (which may be less
3414 than the length of VALUES in some error situations), or -1 on
3415 failure. */
3417 static int
3418 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3419 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3420 tree function, tree fundecl)
3422 unsigned int parmnum;
3423 bool error_args = false;
3424 const bool type_generic = fundecl
3425 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3426 bool type_generic_remove_excess_precision = false;
3427 bool type_generic_overflow_p = false;
3428 tree selector;
3430 /* Change pointer to function to the function itself for
3431 diagnostics. */
3432 if (TREE_CODE (function) == ADDR_EXPR
3433 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3434 function = TREE_OPERAND (function, 0);
3436 /* Handle an ObjC selector specially for diagnostics. */
3437 selector = objc_message_selector ();
3439 /* For a call to a built-in function declared without a prototype,
3440 set to the built-in function's argument list. */
3441 tree builtin_typelist = NULL_TREE;
3443 /* For type-generic built-in functions, determine whether excess
3444 precision should be removed (classification) or not
3445 (comparison). */
3446 if (fundecl
3447 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3449 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3450 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3452 /* For a call to a built-in function declared without a prototype
3453 use the types of the parameters of the internal built-in to
3454 match those of the arguments to. */
3455 if (tree bdecl = builtin_decl_explicit (code))
3456 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3459 /* For type-generic built-in functions, determine whether excess
3460 precision should be removed (classification) or not
3461 (comparison). */
3462 if (type_generic)
3463 switch (code)
3465 case BUILT_IN_ISFINITE:
3466 case BUILT_IN_ISINF:
3467 case BUILT_IN_ISINF_SIGN:
3468 case BUILT_IN_ISNAN:
3469 case BUILT_IN_ISNORMAL:
3470 case BUILT_IN_FPCLASSIFY:
3471 type_generic_remove_excess_precision = true;
3472 break;
3474 case BUILT_IN_ADD_OVERFLOW_P:
3475 case BUILT_IN_SUB_OVERFLOW_P:
3476 case BUILT_IN_MUL_OVERFLOW_P:
3477 /* The last argument of these type-generic builtins
3478 should not be promoted. */
3479 type_generic_overflow_p = true;
3480 break;
3482 default:
3483 break;
3487 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3488 individual converted arguments. */
3490 tree typetail, builtin_typetail, val;
3491 for (typetail = typelist,
3492 builtin_typetail = builtin_typelist,
3493 parmnum = 0;
3494 values && values->iterate (parmnum, &val);
3495 ++parmnum)
3497 /* The type of the function parameter (if it was declared with one). */
3498 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3499 /* The type of the built-in function parameter (if the function
3500 is a built-in). Used to detect type incompatibilities in
3501 calls to built-ins declared without a prototype. */
3502 tree builtin_type = (builtin_typetail
3503 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3504 /* The original type of the argument being passed to the function. */
3505 tree valtype = TREE_TYPE (val);
3506 /* The called function (or function selector in Objective C). */
3507 tree rname = function;
3508 int argnum = parmnum + 1;
3509 const char *invalid_func_diag;
3510 /* Set for EXCESS_PRECISION_EXPR arguments. */
3511 bool excess_precision = false;
3512 /* The value of the argument after conversion to the type
3513 of the function parameter it is passed to. */
3514 tree parmval;
3515 /* Some __atomic_* builtins have additional hidden argument at
3516 position 0. */
3517 location_t ploc
3518 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3519 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3520 : input_location;
3522 if (type == void_type_node)
3524 if (selector)
3525 error_at (loc, "too many arguments to method %qE", selector);
3526 else
3527 error_at (loc, "too many arguments to function %qE", function);
3528 inform_declaration (fundecl);
3529 return error_args ? -1 : (int) parmnum;
3532 if (builtin_type == void_type_node)
3534 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3535 "too many arguments to built-in function %qE "
3536 "expecting %d", function, parmnum))
3537 inform_declaration (fundecl);
3538 builtin_typetail = NULL_TREE;
3541 if (selector && argnum > 2)
3543 rname = selector;
3544 argnum -= 2;
3547 /* Determine if VAL is a null pointer constant before folding it. */
3548 bool npc = null_pointer_constant_p (val);
3550 /* If there is excess precision and a prototype, convert once to
3551 the required type rather than converting via the semantic
3552 type. Likewise without a prototype a float value represented
3553 as long double should be converted once to double. But for
3554 type-generic classification functions excess precision must
3555 be removed here. */
3556 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3557 && (type || !type_generic || !type_generic_remove_excess_precision))
3559 val = TREE_OPERAND (val, 0);
3560 excess_precision = true;
3562 val = c_fully_fold (val, false, NULL);
3563 STRIP_TYPE_NOPS (val);
3565 val = require_complete_type (ploc, val);
3567 /* Some floating-point arguments must be promoted to double when
3568 no type is specified by a prototype. This applies to
3569 arguments of type float, and to architecture-specific types
3570 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3571 bool promote_float_arg = false;
3572 if (type == NULL_TREE
3573 && TREE_CODE (valtype) == REAL_TYPE
3574 && (TYPE_PRECISION (valtype)
3575 <= TYPE_PRECISION (double_type_node))
3576 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3577 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3578 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3580 /* Promote this argument, unless it has a _FloatN or
3581 _FloatNx type. */
3582 promote_float_arg = true;
3583 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3584 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3586 promote_float_arg = false;
3587 break;
3591 if (type != NULL_TREE)
3593 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3594 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3595 val, valtype, npc, rname, parmnum, argnum,
3596 excess_precision, 0);
3598 else if (promote_float_arg)
3600 if (type_generic)
3601 parmval = val;
3602 else
3604 /* Convert `float' to `double'. */
3605 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3606 warning_at (ploc, OPT_Wdouble_promotion,
3607 "implicit conversion from %qT to %qT when passing "
3608 "argument to function",
3609 valtype, double_type_node);
3610 parmval = convert (double_type_node, val);
3613 else if ((excess_precision && !type_generic)
3614 || (type_generic_overflow_p && parmnum == 2))
3615 /* A "double" argument with excess precision being passed
3616 without a prototype or in variable arguments.
3617 The last argument of __builtin_*_overflow_p should not be
3618 promoted. */
3619 parmval = convert (valtype, val);
3620 else if ((invalid_func_diag =
3621 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3623 error (invalid_func_diag);
3624 return -1;
3626 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3628 return -1;
3630 else
3631 /* Convert `short' and `char' to full-size `int'. */
3632 parmval = default_conversion (val);
3634 (*values)[parmnum] = parmval;
3635 if (parmval == error_mark_node)
3636 error_args = true;
3638 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3640 /* For a call to a built-in function declared without a prototype,
3641 perform the conversions from the argument to the expected type
3642 but issue warnings rather than errors for any mismatches.
3643 Ignore the converted argument and use the PARMVAL obtained
3644 above by applying default conversions instead. */
3645 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3646 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3647 val, valtype, npc, rname, parmnum, argnum,
3648 excess_precision,
3649 OPT_Wbuiltin_declaration_mismatch);
3652 if (typetail)
3653 typetail = TREE_CHAIN (typetail);
3655 if (builtin_typetail)
3656 builtin_typetail = TREE_CHAIN (builtin_typetail);
3659 gcc_assert (parmnum == vec_safe_length (values));
3661 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3663 error_at (loc, "too few arguments to function %qE", function);
3664 inform_declaration (fundecl);
3665 return -1;
3668 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3670 unsigned nargs = parmnum;
3671 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3672 ++nargs;
3674 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3675 "too few arguments to built-in function %qE "
3676 "expecting %u", function, nargs - 1))
3677 inform_declaration (fundecl);
3680 return error_args ? -1 : (int) parmnum;
3683 /* This is the entry point used by the parser to build unary operators
3684 in the input. CODE, a tree_code, specifies the unary operator, and
3685 ARG is the operand. For unary plus, the C parser currently uses
3686 CONVERT_EXPR for code.
3688 LOC is the location to use for the tree generated.
3691 struct c_expr
3692 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3694 struct c_expr result;
3696 result.original_code = code;
3697 result.original_type = NULL;
3699 if (reject_gcc_builtin (arg.value))
3701 result.value = error_mark_node;
3703 else
3705 result.value = build_unary_op (loc, code, arg.value, false);
3707 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3708 overflow_warning (loc, result.value, arg.value);
3711 /* We are typically called when parsing a prefix token at LOC acting on
3712 ARG. Reflect this by updating the source range of the result to
3713 start at LOC and end at the end of ARG. */
3714 set_c_expr_source_range (&result,
3715 loc, arg.get_finish ());
3717 return result;
3720 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3722 static bool
3723 char_type_p (tree type)
3725 return (type == char_type_node
3726 || type == unsigned_char_type_node
3727 || type == signed_char_type_node
3728 || type == char16_type_node
3729 || type == char32_type_node);
3732 /* This is the entry point used by the parser to build binary operators
3733 in the input. CODE, a tree_code, specifies the binary operator, and
3734 ARG1 and ARG2 are the operands. In addition to constructing the
3735 expression, we check for operands that were written with other binary
3736 operators in a way that is likely to confuse the user.
3738 LOCATION is the location of the binary operator. */
3740 struct c_expr
3741 parser_build_binary_op (location_t location, enum tree_code code,
3742 struct c_expr arg1, struct c_expr arg2)
3744 struct c_expr result;
3746 enum tree_code code1 = arg1.original_code;
3747 enum tree_code code2 = arg2.original_code;
3748 tree type1 = (arg1.original_type
3749 ? arg1.original_type
3750 : TREE_TYPE (arg1.value));
3751 tree type2 = (arg2.original_type
3752 ? arg2.original_type
3753 : TREE_TYPE (arg2.value));
3755 result.value = build_binary_op (location, code,
3756 arg1.value, arg2.value, true);
3757 result.original_code = code;
3758 result.original_type = NULL;
3760 if (TREE_CODE (result.value) == ERROR_MARK)
3762 set_c_expr_source_range (&result,
3763 arg1.get_start (),
3764 arg2.get_finish ());
3765 return result;
3768 if (location != UNKNOWN_LOCATION)
3769 protected_set_expr_location (result.value, location);
3771 set_c_expr_source_range (&result,
3772 arg1.get_start (),
3773 arg2.get_finish ());
3775 /* Check for cases such as x+y<<z which users are likely
3776 to misinterpret. */
3777 if (warn_parentheses)
3778 warn_about_parentheses (location, code, code1, arg1.value, code2,
3779 arg2.value);
3781 if (warn_logical_op)
3782 warn_logical_operator (location, code, TREE_TYPE (result.value),
3783 code1, arg1.value, code2, arg2.value);
3785 if (warn_tautological_compare)
3787 tree lhs = arg1.value;
3788 tree rhs = arg2.value;
3789 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3791 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3792 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3793 lhs = NULL_TREE;
3794 else
3795 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3797 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3799 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3800 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3801 rhs = NULL_TREE;
3802 else
3803 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3805 if (lhs != NULL_TREE && rhs != NULL_TREE)
3806 warn_tautological_cmp (location, code, lhs, rhs);
3809 if (warn_logical_not_paren
3810 && TREE_CODE_CLASS (code) == tcc_comparison
3811 && code1 == TRUTH_NOT_EXPR
3812 && code2 != TRUTH_NOT_EXPR
3813 /* Avoid warning for !!x == y. */
3814 && (TREE_CODE (arg1.value) != NE_EXPR
3815 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3817 /* Avoid warning for !b == y where b has _Bool type. */
3818 tree t = integer_zero_node;
3819 if (TREE_CODE (arg1.value) == EQ_EXPR
3820 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3821 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3823 t = TREE_OPERAND (arg1.value, 0);
3826 if (TREE_TYPE (t) != integer_type_node)
3827 break;
3828 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3829 t = C_MAYBE_CONST_EXPR_EXPR (t);
3830 else if (CONVERT_EXPR_P (t))
3831 t = TREE_OPERAND (t, 0);
3832 else
3833 break;
3835 while (1);
3837 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3838 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3841 /* Warn about comparisons against string literals, with the exception
3842 of testing for equality or inequality of a string literal with NULL. */
3843 if (code == EQ_EXPR || code == NE_EXPR)
3845 if ((code1 == STRING_CST
3846 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3847 || (code2 == STRING_CST
3848 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3849 warning_at (location, OPT_Waddress,
3850 "comparison with string literal results in unspecified behavior");
3851 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3852 if (POINTER_TYPE_P (type1)
3853 && null_pointer_constant_p (arg2.value)
3854 && char_type_p (type2))
3856 auto_diagnostic_group d;
3857 if (warning_at (location, OPT_Wpointer_compare,
3858 "comparison between pointer and zero character "
3859 "constant"))
3860 inform (arg1.get_start (),
3861 "did you mean to dereference the pointer?");
3863 else if (POINTER_TYPE_P (type2)
3864 && null_pointer_constant_p (arg1.value)
3865 && char_type_p (type1))
3867 auto_diagnostic_group d;
3868 if (warning_at (location, OPT_Wpointer_compare,
3869 "comparison between pointer and zero character "
3870 "constant"))
3871 inform (arg2.get_start (),
3872 "did you mean to dereference the pointer?");
3875 else if (TREE_CODE_CLASS (code) == tcc_comparison
3876 && (code1 == STRING_CST || code2 == STRING_CST))
3877 warning_at (location, OPT_Waddress,
3878 "comparison with string literal results in unspecified behavior");
3880 if (TREE_OVERFLOW_P (result.value)
3881 && !TREE_OVERFLOW_P (arg1.value)
3882 && !TREE_OVERFLOW_P (arg2.value))
3883 overflow_warning (location, result.value);
3885 /* Warn about comparisons of different enum types. */
3886 if (warn_enum_compare
3887 && TREE_CODE_CLASS (code) == tcc_comparison
3888 && TREE_CODE (type1) == ENUMERAL_TYPE
3889 && TREE_CODE (type2) == ENUMERAL_TYPE
3890 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3891 warning_at (location, OPT_Wenum_compare,
3892 "comparison between %qT and %qT",
3893 type1, type2);
3895 return result;
3898 /* Return a tree for the difference of pointers OP0 and OP1.
3899 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3900 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3902 static tree
3903 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3905 tree restype = ptrdiff_type_node;
3906 tree result, inttype;
3908 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3909 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3910 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3911 tree orig_op0 = op0;
3912 tree orig_op1 = op1;
3914 /* If the operands point into different address spaces, we need to
3915 explicitly convert them to pointers into the common address space
3916 before we can subtract the numerical address values. */
3917 if (as0 != as1)
3919 addr_space_t as_common;
3920 tree common_type;
3922 /* Determine the common superset address space. This is guaranteed
3923 to exist because the caller verified that comp_target_types
3924 returned non-zero. */
3925 if (!addr_space_superset (as0, as1, &as_common))
3926 gcc_unreachable ();
3928 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3929 op0 = convert (common_type, op0);
3930 op1 = convert (common_type, op1);
3933 /* Determine integer type result of the subtraction. This will usually
3934 be the same as the result type (ptrdiff_t), but may need to be a wider
3935 type if pointers for the address space are wider than ptrdiff_t. */
3936 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3937 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3938 else
3939 inttype = restype;
3941 if (TREE_CODE (target_type) == VOID_TYPE)
3942 pedwarn (loc, OPT_Wpointer_arith,
3943 "pointer of type %<void *%> used in subtraction");
3944 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3945 pedwarn (loc, OPT_Wpointer_arith,
3946 "pointer to a function used in subtraction");
3948 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3950 gcc_assert (current_function_decl != NULL_TREE);
3952 op0 = save_expr (op0);
3953 op1 = save_expr (op1);
3955 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3956 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3959 /* First do the subtraction, then build the divide operator
3960 and only convert at the very end.
3961 Do not do default conversions in case restype is a short type. */
3963 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3964 pointers. If some platform cannot provide that, or has a larger
3965 ptrdiff_type to support differences larger than half the address
3966 space, cast the pointers to some larger integer type and do the
3967 computations in that type. */
3968 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3969 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3970 convert (inttype, op1), false);
3971 else
3973 /* Cast away qualifiers. */
3974 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3975 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3976 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3979 /* This generates an error if op1 is pointer to incomplete type. */
3980 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3981 error_at (loc, "arithmetic on pointer to an incomplete type");
3982 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
3983 TREE_TYPE (TREE_TYPE (orig_op0))))
3984 verify_type_context (loc, TCTX_POINTER_ARITH,
3985 TREE_TYPE (TREE_TYPE (orig_op1)));
3987 op1 = c_size_in_bytes (target_type);
3989 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3990 error_at (loc, "arithmetic on pointer to an empty aggregate");
3992 /* Divide by the size, in easiest possible way. */
3993 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3994 op0, convert (inttype, op1));
3996 /* Convert to final result type if necessary. */
3997 return convert (restype, result);
4000 /* Expand atomic compound assignments into an appropriate sequence as
4001 specified by the C11 standard section 6.5.16.2.
4003 _Atomic T1 E1
4004 T2 E2
4005 E1 op= E2
4007 This sequence is used for all types for which these operations are
4008 supported.
4010 In addition, built-in versions of the 'fe' prefixed routines may
4011 need to be invoked for floating point (real, complex or vector) when
4012 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4014 T1 newval;
4015 T1 old;
4016 T1 *addr
4017 T2 val
4018 fenv_t fenv
4020 addr = &E1;
4021 val = (E2);
4022 __atomic_load (addr, &old, SEQ_CST);
4023 feholdexcept (&fenv);
4024 loop:
4025 newval = old op val;
4026 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4027 SEQ_CST))
4028 goto done;
4029 feclearexcept (FE_ALL_EXCEPT);
4030 goto loop:
4031 done:
4032 feupdateenv (&fenv);
4034 The compiler will issue the __atomic_fetch_* built-in when possible,
4035 otherwise it will generate the generic form of the atomic operations.
4036 This requires temp(s) and has their address taken. The atomic processing
4037 is smart enough to figure out when the size of an object can utilize
4038 a lock-free version, and convert the built-in call to the appropriate
4039 lock-free routine. The optimizers will then dispose of any temps that
4040 are no longer required, and lock-free implementations are utilized as
4041 long as there is target support for the required size.
4043 If the operator is NOP_EXPR, then this is a simple assignment, and
4044 an __atomic_store is issued to perform the assignment rather than
4045 the above loop. */
4047 /* Build an atomic assignment at LOC, expanding into the proper
4048 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4049 the result of the operation, unless RETURN_OLD_P, in which case
4050 return the old value of LHS (this is only for postincrement and
4051 postdecrement). */
4053 static tree
4054 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4055 tree rhs, bool return_old_p)
4057 tree fndecl, func_call;
4058 vec<tree, va_gc> *params;
4059 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4060 tree old, old_addr;
4061 tree compound_stmt;
4062 tree stmt, goto_stmt;
4063 tree loop_label, loop_decl, done_label, done_decl;
4065 tree lhs_type = TREE_TYPE (lhs);
4066 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4067 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4068 tree rhs_semantic_type = TREE_TYPE (rhs);
4069 tree nonatomic_rhs_semantic_type;
4070 tree rhs_type;
4072 gcc_assert (TYPE_ATOMIC (lhs_type));
4074 if (return_old_p)
4075 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4077 /* Allocate enough vector items for a compare_exchange. */
4078 vec_alloc (params, 6);
4080 /* Create a compound statement to hold the sequence of statements
4081 with a loop. */
4082 compound_stmt = c_begin_compound_stmt (false);
4084 /* Remove any excess precision (which is only present here in the
4085 case of compound assignments). */
4086 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4088 gcc_assert (modifycode != NOP_EXPR);
4089 rhs = TREE_OPERAND (rhs, 0);
4091 rhs_type = TREE_TYPE (rhs);
4093 /* Fold the RHS if it hasn't already been folded. */
4094 if (modifycode != NOP_EXPR)
4095 rhs = c_fully_fold (rhs, false, NULL);
4097 /* Remove the qualifiers for the rest of the expressions and create
4098 the VAL temp variable to hold the RHS. */
4099 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4100 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4101 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4102 TYPE_UNQUALIFIED);
4103 val = create_tmp_var_raw (nonatomic_rhs_type);
4104 TREE_ADDRESSABLE (val) = 1;
4105 TREE_NO_WARNING (val) = 1;
4106 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4107 NULL_TREE);
4108 SET_EXPR_LOCATION (rhs, loc);
4109 add_stmt (rhs);
4111 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4112 an atomic_store. */
4113 if (modifycode == NOP_EXPR)
4115 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4116 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4117 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4118 params->quick_push (lhs_addr);
4119 params->quick_push (rhs);
4120 params->quick_push (seq_cst);
4121 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4122 add_stmt (func_call);
4124 /* Finish the compound statement. */
4125 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4127 /* VAL is the value which was stored, return a COMPOUND_STMT of
4128 the statement and that value. */
4129 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4132 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4133 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4134 isn't applicable for such builtins. ??? Do we want to handle enums? */
4135 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4136 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4138 built_in_function fncode;
4139 switch (modifycode)
4141 case PLUS_EXPR:
4142 case POINTER_PLUS_EXPR:
4143 fncode = (return_old_p
4144 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4145 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4146 break;
4147 case MINUS_EXPR:
4148 fncode = (return_old_p
4149 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4150 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4151 break;
4152 case BIT_AND_EXPR:
4153 fncode = (return_old_p
4154 ? BUILT_IN_ATOMIC_FETCH_AND_N
4155 : BUILT_IN_ATOMIC_AND_FETCH_N);
4156 break;
4157 case BIT_IOR_EXPR:
4158 fncode = (return_old_p
4159 ? BUILT_IN_ATOMIC_FETCH_OR_N
4160 : BUILT_IN_ATOMIC_OR_FETCH_N);
4161 break;
4162 case BIT_XOR_EXPR:
4163 fncode = (return_old_p
4164 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4165 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4166 break;
4167 default:
4168 goto cas_loop;
4171 /* We can only use "_1" through "_16" variants of the atomic fetch
4172 built-ins. */
4173 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4174 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4175 goto cas_loop;
4177 /* If this is a pointer type, we need to multiply by the size of
4178 the pointer target type. */
4179 if (POINTER_TYPE_P (lhs_type))
4181 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4182 /* ??? This would introduce -Wdiscarded-qualifiers
4183 warning: __atomic_fetch_* expect volatile void *
4184 type as the first argument. (Assignments between
4185 atomic and non-atomic objects are OK.) */
4186 || TYPE_RESTRICT (lhs_type))
4187 goto cas_loop;
4188 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4189 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4190 convert (ptrdiff_type_node, rhs),
4191 convert (ptrdiff_type_node, sz));
4194 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4195 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4196 fndecl = builtin_decl_explicit (fncode);
4197 params->quick_push (lhs_addr);
4198 params->quick_push (rhs);
4199 params->quick_push (seq_cst);
4200 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4202 newval = create_tmp_var_raw (nonatomic_lhs_type);
4203 TREE_ADDRESSABLE (newval) = 1;
4204 TREE_NO_WARNING (newval) = 1;
4205 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4206 NULL_TREE, NULL_TREE);
4207 SET_EXPR_LOCATION (rhs, loc);
4208 add_stmt (rhs);
4210 /* Finish the compound statement. */
4211 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4213 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4214 the statement and that value. */
4215 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4218 cas_loop:
4219 /* Create the variables and labels required for the op= form. */
4220 old = create_tmp_var_raw (nonatomic_lhs_type);
4221 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4222 TREE_ADDRESSABLE (old) = 1;
4223 TREE_NO_WARNING (old) = 1;
4225 newval = create_tmp_var_raw (nonatomic_lhs_type);
4226 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4227 TREE_ADDRESSABLE (newval) = 1;
4228 TREE_NO_WARNING (newval) = 1;
4230 loop_decl = create_artificial_label (loc);
4231 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4233 done_decl = create_artificial_label (loc);
4234 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4236 /* __atomic_load (addr, &old, SEQ_CST). */
4237 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4238 params->quick_push (lhs_addr);
4239 params->quick_push (old_addr);
4240 params->quick_push (seq_cst);
4241 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4242 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4243 NULL_TREE);
4244 add_stmt (old);
4245 params->truncate (0);
4247 /* Create the expressions for floating-point environment
4248 manipulation, if required. */
4249 bool need_fenv = (flag_trapping_math
4250 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4251 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4252 if (need_fenv)
4253 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4255 if (hold_call)
4256 add_stmt (hold_call);
4258 /* loop: */
4259 add_stmt (loop_label);
4261 /* newval = old + val; */
4262 if (rhs_type != rhs_semantic_type)
4263 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4264 rhs = build_binary_op (loc, modifycode, old, val, true);
4265 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4267 tree eptype = TREE_TYPE (rhs);
4268 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4269 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4271 else
4272 rhs = c_fully_fold (rhs, false, NULL);
4273 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4274 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4275 NULL_TREE, 0);
4276 if (rhs != error_mark_node)
4278 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4279 NULL_TREE);
4280 SET_EXPR_LOCATION (rhs, loc);
4281 add_stmt (rhs);
4284 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4285 goto done; */
4286 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4287 params->quick_push (lhs_addr);
4288 params->quick_push (old_addr);
4289 params->quick_push (newval_addr);
4290 params->quick_push (integer_zero_node);
4291 params->quick_push (seq_cst);
4292 params->quick_push (seq_cst);
4293 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4295 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4296 SET_EXPR_LOCATION (goto_stmt, loc);
4298 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4299 SET_EXPR_LOCATION (stmt, loc);
4300 add_stmt (stmt);
4302 if (clear_call)
4303 add_stmt (clear_call);
4305 /* goto loop; */
4306 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4307 SET_EXPR_LOCATION (goto_stmt, loc);
4308 add_stmt (goto_stmt);
4310 /* done: */
4311 add_stmt (done_label);
4313 if (update_call)
4314 add_stmt (update_call);
4316 /* Finish the compound statement. */
4317 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4319 /* NEWVAL is the value that was successfully stored, return a
4320 COMPOUND_EXPR of the statement and the appropriate value. */
4321 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4322 return_old_p ? old : newval);
4325 /* Construct and perhaps optimize a tree representation
4326 for a unary operation. CODE, a tree_code, specifies the operation
4327 and XARG is the operand.
4328 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4329 promotions (such as from short to int).
4330 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4331 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4332 to pointers in C99.
4334 LOCATION is the location of the operator. */
4336 tree
4337 build_unary_op (location_t location, enum tree_code code, tree xarg,
4338 bool noconvert)
4340 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4341 tree arg = xarg;
4342 tree argtype = NULL_TREE;
4343 enum tree_code typecode;
4344 tree val;
4345 tree ret = error_mark_node;
4346 tree eptype = NULL_TREE;
4347 const char *invalid_op_diag;
4348 bool int_operands;
4350 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4351 if (int_operands)
4352 arg = remove_c_maybe_const_expr (arg);
4354 if (code != ADDR_EXPR)
4355 arg = require_complete_type (location, arg);
4357 typecode = TREE_CODE (TREE_TYPE (arg));
4358 if (typecode == ERROR_MARK)
4359 return error_mark_node;
4360 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4361 typecode = INTEGER_TYPE;
4363 if ((invalid_op_diag
4364 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4366 error_at (location, invalid_op_diag);
4367 return error_mark_node;
4370 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4372 eptype = TREE_TYPE (arg);
4373 arg = TREE_OPERAND (arg, 0);
4376 switch (code)
4378 case CONVERT_EXPR:
4379 /* This is used for unary plus, because a CONVERT_EXPR
4380 is enough to prevent anybody from looking inside for
4381 associativity, but won't generate any code. */
4382 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4383 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4384 || gnu_vector_type_p (TREE_TYPE (arg))))
4386 error_at (location, "wrong type argument to unary plus");
4387 return error_mark_node;
4389 else if (!noconvert)
4390 arg = default_conversion (arg);
4391 arg = non_lvalue_loc (location, arg);
4392 break;
4394 case NEGATE_EXPR:
4395 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4396 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4397 || gnu_vector_type_p (TREE_TYPE (arg))))
4399 error_at (location, "wrong type argument to unary minus");
4400 return error_mark_node;
4402 else if (!noconvert)
4403 arg = default_conversion (arg);
4404 break;
4406 case BIT_NOT_EXPR:
4407 /* ~ works on integer types and non float vectors. */
4408 if (typecode == INTEGER_TYPE
4409 || (gnu_vector_type_p (TREE_TYPE (arg))
4410 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4412 tree e = arg;
4414 /* Warn if the expression has boolean value. */
4415 while (TREE_CODE (e) == COMPOUND_EXPR)
4416 e = TREE_OPERAND (e, 1);
4418 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4419 || truth_value_p (TREE_CODE (e))))
4421 auto_diagnostic_group d;
4422 if (warning_at (location, OPT_Wbool_operation,
4423 "%<~%> on a boolean expression"))
4425 gcc_rich_location richloc (location);
4426 richloc.add_fixit_insert_before (location, "!");
4427 inform (&richloc, "did you mean to use logical not?");
4430 if (!noconvert)
4431 arg = default_conversion (arg);
4433 else if (typecode == COMPLEX_TYPE)
4435 code = CONJ_EXPR;
4436 pedwarn (location, OPT_Wpedantic,
4437 "ISO C does not support %<~%> for complex conjugation");
4438 if (!noconvert)
4439 arg = default_conversion (arg);
4441 else
4443 error_at (location, "wrong type argument to bit-complement");
4444 return error_mark_node;
4446 break;
4448 case ABS_EXPR:
4449 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4451 error_at (location, "wrong type argument to abs");
4452 return error_mark_node;
4454 else if (!noconvert)
4455 arg = default_conversion (arg);
4456 break;
4458 case ABSU_EXPR:
4459 if (!(typecode == INTEGER_TYPE))
4461 error_at (location, "wrong type argument to absu");
4462 return error_mark_node;
4464 else if (!noconvert)
4465 arg = default_conversion (arg);
4466 break;
4468 case CONJ_EXPR:
4469 /* Conjugating a real value is a no-op, but allow it anyway. */
4470 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4471 || typecode == COMPLEX_TYPE))
4473 error_at (location, "wrong type argument to conjugation");
4474 return error_mark_node;
4476 else if (!noconvert)
4477 arg = default_conversion (arg);
4478 break;
4480 case TRUTH_NOT_EXPR:
4481 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4482 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4483 && typecode != COMPLEX_TYPE)
4485 error_at (location,
4486 "wrong type argument to unary exclamation mark");
4487 return error_mark_node;
4489 if (int_operands)
4491 arg = c_objc_common_truthvalue_conversion (location, xarg);
4492 arg = remove_c_maybe_const_expr (arg);
4494 else
4495 arg = c_objc_common_truthvalue_conversion (location, arg);
4496 ret = invert_truthvalue_loc (location, arg);
4497 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4498 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4499 location = EXPR_LOCATION (ret);
4500 goto return_build_unary_op;
4502 case REALPART_EXPR:
4503 case IMAGPART_EXPR:
4504 ret = build_real_imag_expr (location, code, arg);
4505 if (ret == error_mark_node)
4506 return error_mark_node;
4507 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4508 eptype = TREE_TYPE (eptype);
4509 goto return_build_unary_op;
4511 case PREINCREMENT_EXPR:
4512 case POSTINCREMENT_EXPR:
4513 case PREDECREMENT_EXPR:
4514 case POSTDECREMENT_EXPR:
4516 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4518 tree inner = build_unary_op (location, code,
4519 C_MAYBE_CONST_EXPR_EXPR (arg),
4520 noconvert);
4521 if (inner == error_mark_node)
4522 return error_mark_node;
4523 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4524 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4525 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4526 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4527 goto return_build_unary_op;
4530 /* Complain about anything that is not a true lvalue. In
4531 Objective-C, skip this check for property_refs. */
4532 if (!objc_is_property_ref (arg)
4533 && !lvalue_or_else (location,
4534 arg, ((code == PREINCREMENT_EXPR
4535 || code == POSTINCREMENT_EXPR)
4536 ? lv_increment
4537 : lv_decrement)))
4538 return error_mark_node;
4540 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4542 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4543 warning_at (location, OPT_Wc___compat,
4544 "increment of enumeration value is invalid in C++");
4545 else
4546 warning_at (location, OPT_Wc___compat,
4547 "decrement of enumeration value is invalid in C++");
4550 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4552 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4553 warning_at (location, OPT_Wbool_operation,
4554 "increment of a boolean expression");
4555 else
4556 warning_at (location, OPT_Wbool_operation,
4557 "decrement of a boolean expression");
4560 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4561 arg = c_fully_fold (arg, false, NULL, true);
4563 bool atomic_op;
4564 atomic_op = really_atomic_lvalue (arg);
4566 /* Increment or decrement the real part of the value,
4567 and don't change the imaginary part. */
4568 if (typecode == COMPLEX_TYPE)
4570 tree real, imag;
4572 pedwarn (location, OPT_Wpedantic,
4573 "ISO C does not support %<++%> and %<--%> on complex types");
4575 if (!atomic_op)
4577 arg = stabilize_reference (arg);
4578 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4579 true);
4580 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4581 true);
4582 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4583 if (real == error_mark_node || imag == error_mark_node)
4584 return error_mark_node;
4585 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4586 real, imag);
4587 goto return_build_unary_op;
4591 /* Report invalid types. */
4593 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4594 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4595 && typecode != COMPLEX_TYPE
4596 && !gnu_vector_type_p (TREE_TYPE (arg)))
4598 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4599 error_at (location, "wrong type argument to increment");
4600 else
4601 error_at (location, "wrong type argument to decrement");
4603 return error_mark_node;
4607 tree inc;
4609 argtype = TREE_TYPE (arg);
4611 /* Compute the increment. */
4613 if (typecode == POINTER_TYPE)
4615 /* If pointer target is an incomplete type,
4616 we just cannot know how to do the arithmetic. */
4617 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4619 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4620 error_at (location,
4621 "increment of pointer to an incomplete type %qT",
4622 TREE_TYPE (argtype));
4623 else
4624 error_at (location,
4625 "decrement of pointer to an incomplete type %qT",
4626 TREE_TYPE (argtype));
4628 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4629 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4631 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4632 pedwarn (location, OPT_Wpointer_arith,
4633 "wrong type argument to increment");
4634 else
4635 pedwarn (location, OPT_Wpointer_arith,
4636 "wrong type argument to decrement");
4638 else
4639 verify_type_context (location, TCTX_POINTER_ARITH,
4640 TREE_TYPE (argtype));
4642 inc = c_size_in_bytes (TREE_TYPE (argtype));
4643 inc = convert_to_ptrofftype_loc (location, inc);
4645 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4647 /* For signed fract types, we invert ++ to -- or
4648 -- to ++, and change inc from 1 to -1, because
4649 it is not possible to represent 1 in signed fract constants.
4650 For unsigned fract types, the result always overflows and
4651 we get an undefined (original) or the maximum value. */
4652 if (code == PREINCREMENT_EXPR)
4653 code = PREDECREMENT_EXPR;
4654 else if (code == PREDECREMENT_EXPR)
4655 code = PREINCREMENT_EXPR;
4656 else if (code == POSTINCREMENT_EXPR)
4657 code = POSTDECREMENT_EXPR;
4658 else /* code == POSTDECREMENT_EXPR */
4659 code = POSTINCREMENT_EXPR;
4661 inc = integer_minus_one_node;
4662 inc = convert (argtype, inc);
4664 else
4666 inc = VECTOR_TYPE_P (argtype)
4667 ? build_one_cst (argtype)
4668 : integer_one_node;
4669 inc = convert (argtype, inc);
4672 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4673 need to ask Objective-C to build the increment or decrement
4674 expression for it. */
4675 if (objc_is_property_ref (arg))
4676 return objc_build_incr_expr_for_property_ref (location, code,
4677 arg, inc);
4679 /* Report a read-only lvalue. */
4680 if (TYPE_READONLY (argtype))
4682 readonly_error (location, arg,
4683 ((code == PREINCREMENT_EXPR
4684 || code == POSTINCREMENT_EXPR)
4685 ? lv_increment : lv_decrement));
4686 return error_mark_node;
4688 else if (TREE_READONLY (arg))
4689 readonly_warning (arg,
4690 ((code == PREINCREMENT_EXPR
4691 || code == POSTINCREMENT_EXPR)
4692 ? lv_increment : lv_decrement));
4694 /* If the argument is atomic, use the special code sequences for
4695 atomic compound assignment. */
4696 if (atomic_op)
4698 arg = stabilize_reference (arg);
4699 ret = build_atomic_assign (location, arg,
4700 ((code == PREINCREMENT_EXPR
4701 || code == POSTINCREMENT_EXPR)
4702 ? PLUS_EXPR
4703 : MINUS_EXPR),
4704 (FRACT_MODE_P (TYPE_MODE (argtype))
4705 ? inc
4706 : integer_one_node),
4707 (code == POSTINCREMENT_EXPR
4708 || code == POSTDECREMENT_EXPR));
4709 goto return_build_unary_op;
4712 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4713 val = boolean_increment (code, arg);
4714 else
4715 val = build2 (code, TREE_TYPE (arg), arg, inc);
4716 TREE_SIDE_EFFECTS (val) = 1;
4717 if (TREE_CODE (val) != code)
4718 TREE_NO_WARNING (val) = 1;
4719 ret = val;
4720 goto return_build_unary_op;
4723 case ADDR_EXPR:
4724 /* Note that this operation never does default_conversion. */
4726 /* The operand of unary '&' must be an lvalue (which excludes
4727 expressions of type void), or, in C99, the result of a [] or
4728 unary '*' operator. */
4729 if (VOID_TYPE_P (TREE_TYPE (arg))
4730 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4731 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4732 pedwarn (location, 0, "taking address of expression of type %<void%>");
4734 /* Let &* cancel out to simplify resulting code. */
4735 if (INDIRECT_REF_P (arg))
4737 /* Don't let this be an lvalue. */
4738 if (lvalue_p (TREE_OPERAND (arg, 0)))
4739 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4740 ret = TREE_OPERAND (arg, 0);
4741 goto return_build_unary_op;
4744 /* Anything not already handled and not a true memory reference
4745 or a non-lvalue array is an error. */
4746 if (typecode != FUNCTION_TYPE && !noconvert
4747 && !lvalue_or_else (location, arg, lv_addressof))
4748 return error_mark_node;
4750 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4751 folding later. */
4752 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4754 tree inner = build_unary_op (location, code,
4755 C_MAYBE_CONST_EXPR_EXPR (arg),
4756 noconvert);
4757 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4758 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4759 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4760 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4761 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4762 goto return_build_unary_op;
4765 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4766 argtype = TREE_TYPE (arg);
4768 /* If the lvalue is const or volatile, merge that into the type
4769 to which the address will point. This is only needed
4770 for function types. */
4771 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4772 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4773 && TREE_CODE (argtype) == FUNCTION_TYPE)
4775 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4776 int quals = orig_quals;
4778 if (TREE_READONLY (arg))
4779 quals |= TYPE_QUAL_CONST;
4780 if (TREE_THIS_VOLATILE (arg))
4781 quals |= TYPE_QUAL_VOLATILE;
4783 argtype = c_build_qualified_type (argtype, quals);
4786 switch (TREE_CODE (arg))
4788 case COMPONENT_REF:
4789 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4791 error_at (location, "cannot take address of bit-field %qD",
4792 TREE_OPERAND (arg, 1));
4793 return error_mark_node;
4796 /* fall through */
4798 case ARRAY_REF:
4799 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4801 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4802 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4804 error_at (location, "cannot take address of scalar with "
4805 "reverse storage order");
4806 return error_mark_node;
4809 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4810 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4811 warning_at (location, OPT_Wscalar_storage_order,
4812 "address of array with reverse scalar storage "
4813 "order requested");
4816 default:
4817 break;
4820 if (!c_mark_addressable (arg))
4821 return error_mark_node;
4823 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4824 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4826 argtype = build_pointer_type (argtype);
4828 /* ??? Cope with user tricks that amount to offsetof. Delete this
4829 when we have proper support for integer constant expressions. */
4830 val = get_base_address (arg);
4831 if (val && INDIRECT_REF_P (val)
4832 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4834 ret = fold_offsetof (arg, argtype);
4835 goto return_build_unary_op;
4838 val = build1 (ADDR_EXPR, argtype, arg);
4840 ret = val;
4841 goto return_build_unary_op;
4843 default:
4844 gcc_unreachable ();
4847 if (argtype == NULL_TREE)
4848 argtype = TREE_TYPE (arg);
4849 if (TREE_CODE (arg) == INTEGER_CST)
4850 ret = (require_constant_value
4851 ? fold_build1_initializer_loc (location, code, argtype, arg)
4852 : fold_build1_loc (location, code, argtype, arg));
4853 else
4854 ret = build1 (code, argtype, arg);
4855 return_build_unary_op:
4856 gcc_assert (ret != error_mark_node);
4857 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4858 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4859 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4860 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4861 ret = note_integer_operands (ret);
4862 if (eptype)
4863 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4864 protected_set_expr_location (ret, location);
4865 return ret;
4868 /* Return nonzero if REF is an lvalue valid for this language.
4869 Lvalues can be assigned, unless their type has TYPE_READONLY.
4870 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4872 bool
4873 lvalue_p (const_tree ref)
4875 const enum tree_code code = TREE_CODE (ref);
4877 switch (code)
4879 case REALPART_EXPR:
4880 case IMAGPART_EXPR:
4881 case COMPONENT_REF:
4882 return lvalue_p (TREE_OPERAND (ref, 0));
4884 case C_MAYBE_CONST_EXPR:
4885 return lvalue_p (TREE_OPERAND (ref, 1));
4887 case COMPOUND_LITERAL_EXPR:
4888 case STRING_CST:
4889 return true;
4891 case INDIRECT_REF:
4892 case ARRAY_REF:
4893 case VAR_DECL:
4894 case PARM_DECL:
4895 case RESULT_DECL:
4896 case ERROR_MARK:
4897 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4898 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4900 case BIND_EXPR:
4901 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4903 default:
4904 return false;
4908 /* Give a warning for storing in something that is read-only in GCC
4909 terms but not const in ISO C terms. */
4911 static void
4912 readonly_warning (tree arg, enum lvalue_use use)
4914 switch (use)
4916 case lv_assign:
4917 warning (0, "assignment of read-only location %qE", arg);
4918 break;
4919 case lv_increment:
4920 warning (0, "increment of read-only location %qE", arg);
4921 break;
4922 case lv_decrement:
4923 warning (0, "decrement of read-only location %qE", arg);
4924 break;
4925 default:
4926 gcc_unreachable ();
4928 return;
4932 /* Return nonzero if REF is an lvalue valid for this language;
4933 otherwise, print an error message and return zero. USE says
4934 how the lvalue is being used and so selects the error message.
4935 LOCATION is the location at which any error should be reported. */
4937 static int
4938 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4940 int win = lvalue_p (ref);
4942 if (!win)
4943 lvalue_error (loc, use);
4945 return win;
4948 /* Mark EXP saying that we need to be able to take the
4949 address of it; it should not be allocated in a register.
4950 Returns true if successful. ARRAY_REF_P is true if this
4951 is for ARRAY_REF construction - in that case we don't want
4952 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4953 it is fine to use ARRAY_REFs for vector subscripts on vector
4954 register variables. */
4956 bool
4957 c_mark_addressable (tree exp, bool array_ref_p)
4959 tree x = exp;
4961 while (1)
4962 switch (TREE_CODE (x))
4964 case VIEW_CONVERT_EXPR:
4965 if (array_ref_p
4966 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4967 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4968 return true;
4969 /* FALLTHRU */
4970 case COMPONENT_REF:
4971 case ADDR_EXPR:
4972 case ARRAY_REF:
4973 case REALPART_EXPR:
4974 case IMAGPART_EXPR:
4975 x = TREE_OPERAND (x, 0);
4976 break;
4978 case COMPOUND_LITERAL_EXPR:
4979 TREE_ADDRESSABLE (x) = 1;
4980 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
4981 return true;
4983 case CONSTRUCTOR:
4984 TREE_ADDRESSABLE (x) = 1;
4985 return true;
4987 case VAR_DECL:
4988 case CONST_DECL:
4989 case PARM_DECL:
4990 case RESULT_DECL:
4991 if (C_DECL_REGISTER (x)
4992 && DECL_NONLOCAL (x))
4994 if (TREE_PUBLIC (x) || is_global_var (x))
4996 error
4997 ("global register variable %qD used in nested function", x);
4998 return false;
5000 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5002 else if (C_DECL_REGISTER (x))
5004 if (TREE_PUBLIC (x) || is_global_var (x))
5005 error ("address of global register variable %qD requested", x);
5006 else
5007 error ("address of register variable %qD requested", x);
5008 return false;
5011 /* FALLTHRU */
5012 case FUNCTION_DECL:
5013 TREE_ADDRESSABLE (x) = 1;
5014 /* FALLTHRU */
5015 default:
5016 return true;
5020 /* Convert EXPR to TYPE, warning about conversion problems with
5021 constants. SEMANTIC_TYPE is the type this conversion would use
5022 without excess precision. If SEMANTIC_TYPE is NULL, this function
5023 is equivalent to convert_and_check. This function is a wrapper that
5024 handles conversions that may be different than
5025 the usual ones because of excess precision. */
5027 static tree
5028 ep_convert_and_check (location_t loc, tree type, tree expr,
5029 tree semantic_type)
5031 if (TREE_TYPE (expr) == type)
5032 return expr;
5034 /* For C11, integer conversions may have results with excess
5035 precision. */
5036 if (flag_isoc11 || !semantic_type)
5037 return convert_and_check (loc, type, expr);
5039 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5040 && TREE_TYPE (expr) != semantic_type)
5042 /* For integers, we need to check the real conversion, not
5043 the conversion to the excess precision type. */
5044 expr = convert_and_check (loc, semantic_type, expr);
5046 /* Result type is the excess precision type, which should be
5047 large enough, so do not check. */
5048 return convert (type, expr);
5051 /* If EXPR refers to a built-in declared without a prototype returns
5052 the actual type of the built-in and, if non-null, set *BLTIN to
5053 a pointer to the built-in. Otherwise return the type of EXPR
5054 and clear *BLTIN if non-null. */
5056 static tree
5057 type_or_builtin_type (tree expr, tree *bltin = NULL)
5059 tree dummy;
5060 if (!bltin)
5061 bltin = &dummy;
5063 *bltin = NULL_TREE;
5065 tree type = TREE_TYPE (expr);
5066 if (TREE_CODE (expr) != ADDR_EXPR)
5067 return type;
5069 tree oper = TREE_OPERAND (expr, 0);
5070 if (!DECL_P (oper)
5071 || TREE_CODE (oper) != FUNCTION_DECL
5072 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5073 return type;
5075 built_in_function code = DECL_FUNCTION_CODE (oper);
5076 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5077 return type;
5079 if ((*bltin = builtin_decl_implicit (code)))
5080 type = build_pointer_type (TREE_TYPE (*bltin));
5082 return type;
5085 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5086 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5087 if folded to an integer constant then the unselected half may
5088 contain arbitrary operations not normally permitted in constant
5089 expressions. Set the location of the expression to LOC. */
5091 tree
5092 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5093 tree op1, tree op1_original_type, location_t op1_loc,
5094 tree op2, tree op2_original_type, location_t op2_loc)
5096 tree type1;
5097 tree type2;
5098 enum tree_code code1;
5099 enum tree_code code2;
5100 tree result_type = NULL;
5101 tree semantic_result_type = NULL;
5102 tree orig_op1 = op1, orig_op2 = op2;
5103 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5104 bool ifexp_int_operands;
5105 tree ret;
5107 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5108 if (op1_int_operands)
5109 op1 = remove_c_maybe_const_expr (op1);
5110 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5111 if (op2_int_operands)
5112 op2 = remove_c_maybe_const_expr (op2);
5113 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5114 if (ifexp_int_operands)
5115 ifexp = remove_c_maybe_const_expr (ifexp);
5117 /* Promote both alternatives. */
5119 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5120 op1 = default_conversion (op1);
5121 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5122 op2 = default_conversion (op2);
5124 if (TREE_CODE (ifexp) == ERROR_MARK
5125 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5126 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5127 return error_mark_node;
5129 tree bltin1 = NULL_TREE;
5130 tree bltin2 = NULL_TREE;
5131 type1 = type_or_builtin_type (op1, &bltin1);
5132 code1 = TREE_CODE (type1);
5133 type2 = type_or_builtin_type (op2, &bltin2);
5134 code2 = TREE_CODE (type2);
5136 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5137 return error_mark_node;
5139 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5140 return error_mark_node;
5142 /* C90 does not permit non-lvalue arrays in conditional expressions.
5143 In C99 they will be pointers by now. */
5144 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5146 error_at (colon_loc, "non-lvalue array in conditional expression");
5147 return error_mark_node;
5150 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5151 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5152 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5153 || code1 == COMPLEX_TYPE)
5154 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5155 || code2 == COMPLEX_TYPE))
5157 semantic_result_type = c_common_type (type1, type2);
5158 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5160 op1 = TREE_OPERAND (op1, 0);
5161 type1 = TREE_TYPE (op1);
5162 gcc_assert (TREE_CODE (type1) == code1);
5164 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5166 op2 = TREE_OPERAND (op2, 0);
5167 type2 = TREE_TYPE (op2);
5168 gcc_assert (TREE_CODE (type2) == code2);
5172 if (warn_cxx_compat)
5174 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5175 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5177 if (TREE_CODE (t1) == ENUMERAL_TYPE
5178 && TREE_CODE (t2) == ENUMERAL_TYPE
5179 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5180 warning_at (colon_loc, OPT_Wc___compat,
5181 ("different enum types in conditional is "
5182 "invalid in C++: %qT vs %qT"),
5183 t1, t2);
5186 /* Quickly detect the usual case where op1 and op2 have the same type
5187 after promotion. */
5188 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5190 if (type1 == type2)
5191 result_type = type1;
5192 else
5193 result_type = TYPE_MAIN_VARIANT (type1);
5195 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5196 || code1 == COMPLEX_TYPE)
5197 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5198 || code2 == COMPLEX_TYPE))
5200 /* In C11, a conditional expression between a floating-point
5201 type and an integer type should convert the integer type to
5202 the evaluation format of the floating-point type, with
5203 possible excess precision. */
5204 tree eptype1 = type1;
5205 tree eptype2 = type2;
5206 if (flag_isoc11)
5208 tree eptype;
5209 if (ANY_INTEGRAL_TYPE_P (type1)
5210 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5212 eptype2 = eptype;
5213 if (!semantic_result_type)
5214 semantic_result_type = c_common_type (type1, type2);
5216 else if (ANY_INTEGRAL_TYPE_P (type2)
5217 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5219 eptype1 = eptype;
5220 if (!semantic_result_type)
5221 semantic_result_type = c_common_type (type1, type2);
5224 result_type = c_common_type (eptype1, eptype2);
5225 if (result_type == error_mark_node)
5226 return error_mark_node;
5227 do_warn_double_promotion (result_type, type1, type2,
5228 "implicit conversion from %qT to %qT to "
5229 "match other result of conditional",
5230 colon_loc);
5232 /* If -Wsign-compare, warn here if type1 and type2 have
5233 different signedness. We'll promote the signed to unsigned
5234 and later code won't know it used to be different.
5235 Do this check on the original types, so that explicit casts
5236 will be considered, but default promotions won't. */
5237 if (c_inhibit_evaluation_warnings == 0)
5239 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5240 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5242 if (unsigned_op1 ^ unsigned_op2)
5244 bool ovf;
5246 /* Do not warn if the result type is signed, since the
5247 signed type will only be chosen if it can represent
5248 all the values of the unsigned type. */
5249 if (!TYPE_UNSIGNED (result_type))
5250 /* OK */;
5251 else
5253 bool op1_maybe_const = true;
5254 bool op2_maybe_const = true;
5256 /* Do not warn if the signed quantity is an
5257 unsuffixed integer literal (or some static
5258 constant expression involving such literals) and
5259 it is non-negative. This warning requires the
5260 operands to be folded for best results, so do
5261 that folding in this case even without
5262 warn_sign_compare to avoid warning options
5263 possibly affecting code generation. */
5264 c_inhibit_evaluation_warnings
5265 += (ifexp == truthvalue_false_node);
5266 op1 = c_fully_fold (op1, require_constant_value,
5267 &op1_maybe_const);
5268 c_inhibit_evaluation_warnings
5269 -= (ifexp == truthvalue_false_node);
5271 c_inhibit_evaluation_warnings
5272 += (ifexp == truthvalue_true_node);
5273 op2 = c_fully_fold (op2, require_constant_value,
5274 &op2_maybe_const);
5275 c_inhibit_evaluation_warnings
5276 -= (ifexp == truthvalue_true_node);
5278 if (warn_sign_compare)
5280 if ((unsigned_op2
5281 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5282 || (unsigned_op1
5283 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5284 /* OK */;
5285 else if (unsigned_op2)
5286 warning_at (op1_loc, OPT_Wsign_compare,
5287 "operand of %<?:%> changes signedness from "
5288 "%qT to %qT due to unsignedness of other "
5289 "operand", TREE_TYPE (orig_op1),
5290 TREE_TYPE (orig_op2));
5291 else
5292 warning_at (op2_loc, OPT_Wsign_compare,
5293 "operand of %<?:%> changes signedness from "
5294 "%qT to %qT due to unsignedness of other "
5295 "operand", TREE_TYPE (orig_op2),
5296 TREE_TYPE (orig_op1));
5298 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5299 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5300 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5301 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5306 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5308 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5309 pedwarn (colon_loc, OPT_Wpedantic,
5310 "ISO C forbids conditional expr with only one void side");
5311 result_type = void_type_node;
5313 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5315 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5316 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5317 addr_space_t as_common;
5319 if (comp_target_types (colon_loc, type1, type2))
5320 result_type = common_pointer_type (type1, type2);
5321 else if (null_pointer_constant_p (orig_op1))
5322 result_type = type2;
5323 else if (null_pointer_constant_p (orig_op2))
5324 result_type = type1;
5325 else if (!addr_space_superset (as1, as2, &as_common))
5327 error_at (colon_loc, "pointers to disjoint address spaces "
5328 "used in conditional expression");
5329 return error_mark_node;
5331 else if (VOID_TYPE_P (TREE_TYPE (type1))
5332 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5334 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5335 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5336 & ~TYPE_QUALS (TREE_TYPE (type1))))
5337 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5338 "pointer to array loses qualifier "
5339 "in conditional expression");
5341 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5342 pedwarn (colon_loc, OPT_Wpedantic,
5343 "ISO C forbids conditional expr between "
5344 "%<void *%> and function pointer");
5345 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5346 TREE_TYPE (type2)));
5348 else if (VOID_TYPE_P (TREE_TYPE (type2))
5349 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5351 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5352 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5353 & ~TYPE_QUALS (TREE_TYPE (type2))))
5354 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5355 "pointer to array loses qualifier "
5356 "in conditional expression");
5358 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5359 pedwarn (colon_loc, OPT_Wpedantic,
5360 "ISO C forbids conditional expr between "
5361 "%<void *%> and function pointer");
5362 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5363 TREE_TYPE (type1)));
5365 /* Objective-C pointer comparisons are a bit more lenient. */
5366 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5367 result_type = objc_common_type (type1, type2);
5368 else
5370 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5371 if (bltin1 && bltin2)
5372 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5373 "pointer type mismatch between %qT and %qT "
5374 "of %qD and %qD in conditional expression",
5375 type1, type2, bltin1, bltin2);
5376 else
5377 pedwarn (colon_loc, 0,
5378 "pointer type mismatch in conditional expression");
5379 result_type = build_pointer_type
5380 (build_qualified_type (void_type_node, qual));
5383 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5385 if (!null_pointer_constant_p (orig_op2))
5386 pedwarn (colon_loc, 0,
5387 "pointer/integer type mismatch in conditional expression");
5388 else
5390 op2 = null_pointer_node;
5392 result_type = type1;
5394 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5396 if (!null_pointer_constant_p (orig_op1))
5397 pedwarn (colon_loc, 0,
5398 "pointer/integer type mismatch in conditional expression");
5399 else
5401 op1 = null_pointer_node;
5403 result_type = type2;
5406 if (!result_type)
5408 if (flag_cond_mismatch)
5409 result_type = void_type_node;
5410 else
5412 error_at (colon_loc, "type mismatch in conditional expression");
5413 return error_mark_node;
5417 /* Merge const and volatile flags of the incoming types. */
5418 result_type
5419 = build_type_variant (result_type,
5420 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5421 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5423 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5424 semantic_result_type);
5425 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5426 semantic_result_type);
5428 if (ifexp_bcp && ifexp == truthvalue_true_node)
5430 op2_int_operands = true;
5431 op1 = c_fully_fold (op1, require_constant_value, NULL);
5433 if (ifexp_bcp && ifexp == truthvalue_false_node)
5435 op1_int_operands = true;
5436 op2 = c_fully_fold (op2, require_constant_value, NULL);
5438 int_const = int_operands = (ifexp_int_operands
5439 && op1_int_operands
5440 && op2_int_operands);
5441 if (int_operands)
5443 int_const = ((ifexp == truthvalue_true_node
5444 && TREE_CODE (orig_op1) == INTEGER_CST
5445 && !TREE_OVERFLOW (orig_op1))
5446 || (ifexp == truthvalue_false_node
5447 && TREE_CODE (orig_op2) == INTEGER_CST
5448 && !TREE_OVERFLOW (orig_op2)));
5451 /* Need to convert condition operand into a vector mask. */
5452 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5454 tree vectype = TREE_TYPE (ifexp);
5455 tree elem_type = TREE_TYPE (vectype);
5456 tree zero = build_int_cst (elem_type, 0);
5457 tree zero_vec = build_vector_from_val (vectype, zero);
5458 tree cmp_type = truth_type_for (vectype);
5459 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5462 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5463 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5464 else
5466 if (int_operands)
5468 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5469 nested inside of the expression. */
5470 op1 = c_fully_fold (op1, false, NULL);
5471 op2 = c_fully_fold (op2, false, NULL);
5473 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5474 if (int_operands)
5475 ret = note_integer_operands (ret);
5477 if (semantic_result_type)
5478 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5480 protected_set_expr_location (ret, colon_loc);
5482 /* If the OP1 and OP2 are the same and don't have side-effects,
5483 warn here, because the COND_EXPR will be turned into OP1. */
5484 if (warn_duplicated_branches
5485 && TREE_CODE (ret) == COND_EXPR
5486 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5487 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5488 "this condition has identical branches");
5490 return ret;
5493 /* Return a compound expression that performs two expressions and
5494 returns the value of the second of them.
5496 LOC is the location of the COMPOUND_EXPR. */
5498 tree
5499 build_compound_expr (location_t loc, tree expr1, tree expr2)
5501 bool expr1_int_operands, expr2_int_operands;
5502 tree eptype = NULL_TREE;
5503 tree ret;
5505 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5506 if (expr1_int_operands)
5507 expr1 = remove_c_maybe_const_expr (expr1);
5508 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5509 if (expr2_int_operands)
5510 expr2 = remove_c_maybe_const_expr (expr2);
5512 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5513 expr1 = TREE_OPERAND (expr1, 0);
5514 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5516 eptype = TREE_TYPE (expr2);
5517 expr2 = TREE_OPERAND (expr2, 0);
5520 if (!TREE_SIDE_EFFECTS (expr1))
5522 /* The left-hand operand of a comma expression is like an expression
5523 statement: with -Wunused, we should warn if it doesn't have
5524 any side-effects, unless it was explicitly cast to (void). */
5525 if (warn_unused_value)
5527 if (VOID_TYPE_P (TREE_TYPE (expr1))
5528 && CONVERT_EXPR_P (expr1))
5529 ; /* (void) a, b */
5530 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5531 && TREE_CODE (expr1) == COMPOUND_EXPR
5532 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5533 ; /* (void) a, (void) b, c */
5534 else
5535 warning_at (loc, OPT_Wunused_value,
5536 "left-hand operand of comma expression has no effect");
5539 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5540 && warn_unused_value)
5542 tree r = expr1;
5543 location_t cloc = loc;
5544 while (TREE_CODE (r) == COMPOUND_EXPR)
5546 if (EXPR_HAS_LOCATION (r))
5547 cloc = EXPR_LOCATION (r);
5548 r = TREE_OPERAND (r, 1);
5550 if (!TREE_SIDE_EFFECTS (r)
5551 && !VOID_TYPE_P (TREE_TYPE (r))
5552 && !CONVERT_EXPR_P (r))
5553 warning_at (cloc, OPT_Wunused_value,
5554 "right-hand operand of comma expression has no effect");
5557 /* With -Wunused, we should also warn if the left-hand operand does have
5558 side-effects, but computes a value which is not used. For example, in
5559 `foo() + bar(), baz()' the result of the `+' operator is not used,
5560 so we should issue a warning. */
5561 else if (warn_unused_value)
5562 warn_if_unused_value (expr1, loc);
5564 if (expr2 == error_mark_node)
5565 return error_mark_node;
5567 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5569 if (flag_isoc99
5570 && expr1_int_operands
5571 && expr2_int_operands)
5572 ret = note_integer_operands (ret);
5574 if (eptype)
5575 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5577 protected_set_expr_location (ret, loc);
5578 return ret;
5581 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5582 which we are casting. OTYPE is the type of the expression being
5583 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5584 of the cast. -Wcast-qual appeared on the command line. Named
5585 address space qualifiers are not handled here, because they result
5586 in different warnings. */
5588 static void
5589 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5591 tree in_type = type;
5592 tree in_otype = otype;
5593 int added = 0;
5594 int discarded = 0;
5595 bool is_const;
5597 /* Check that the qualifiers on IN_TYPE are a superset of the
5598 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5599 nodes is uninteresting and we stop as soon as we hit a
5600 non-POINTER_TYPE node on either type. */
5603 in_otype = TREE_TYPE (in_otype);
5604 in_type = TREE_TYPE (in_type);
5606 /* GNU C allows cv-qualified function types. 'const' means the
5607 function is very pure, 'volatile' means it can't return. We
5608 need to warn when such qualifiers are added, not when they're
5609 taken away. */
5610 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5611 && TREE_CODE (in_type) == FUNCTION_TYPE)
5612 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5613 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5614 else
5615 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5616 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5618 while (TREE_CODE (in_type) == POINTER_TYPE
5619 && TREE_CODE (in_otype) == POINTER_TYPE);
5621 if (added)
5622 warning_at (loc, OPT_Wcast_qual,
5623 "cast adds %q#v qualifier to function type", added);
5625 if (discarded)
5626 /* There are qualifiers present in IN_OTYPE that are not present
5627 in IN_TYPE. */
5628 warning_at (loc, OPT_Wcast_qual,
5629 "cast discards %qv qualifier from pointer target type",
5630 discarded);
5632 if (added || discarded)
5633 return;
5635 /* A cast from **T to const **T is unsafe, because it can cause a
5636 const value to be changed with no additional warning. We only
5637 issue this warning if T is the same on both sides, and we only
5638 issue the warning if there are the same number of pointers on
5639 both sides, as otherwise the cast is clearly unsafe anyhow. A
5640 cast is unsafe when a qualifier is added at one level and const
5641 is not present at all outer levels.
5643 To issue this warning, we check at each level whether the cast
5644 adds new qualifiers not already seen. We don't need to special
5645 case function types, as they won't have the same
5646 TYPE_MAIN_VARIANT. */
5648 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5649 return;
5650 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5651 return;
5653 in_type = type;
5654 in_otype = otype;
5655 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5658 in_type = TREE_TYPE (in_type);
5659 in_otype = TREE_TYPE (in_otype);
5660 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5661 && !is_const)
5663 warning_at (loc, OPT_Wcast_qual,
5664 "to be safe all intermediate pointers in cast from "
5665 "%qT to %qT must be %<const%> qualified",
5666 otype, type);
5667 break;
5669 if (is_const)
5670 is_const = TYPE_READONLY (in_type);
5672 while (TREE_CODE (in_type) == POINTER_TYPE);
5675 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5677 static bool
5678 c_safe_arg_type_equiv_p (tree t1, tree t2)
5680 t1 = TYPE_MAIN_VARIANT (t1);
5681 t2 = TYPE_MAIN_VARIANT (t2);
5683 if (TREE_CODE (t1) == POINTER_TYPE
5684 && TREE_CODE (t2) == POINTER_TYPE)
5685 return true;
5687 /* The signedness of the parameter matters only when an integral
5688 type smaller than int is promoted to int, otherwise only the
5689 precision of the parameter matters.
5690 This check should make sure that the callee does not see
5691 undefined values in argument registers. */
5692 if (INTEGRAL_TYPE_P (t1)
5693 && INTEGRAL_TYPE_P (t2)
5694 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5695 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5696 || !targetm.calls.promote_prototypes (NULL_TREE)
5697 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5698 return true;
5700 return comptypes (t1, t2);
5703 /* Check if a type cast between two function types can be considered safe. */
5705 static bool
5706 c_safe_function_type_cast_p (tree t1, tree t2)
5708 if (TREE_TYPE (t1) == void_type_node &&
5709 TYPE_ARG_TYPES (t1) == void_list_node)
5710 return true;
5712 if (TREE_TYPE (t2) == void_type_node &&
5713 TYPE_ARG_TYPES (t2) == void_list_node)
5714 return true;
5716 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5717 return false;
5719 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5720 t1 && t2;
5721 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5722 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5723 return false;
5725 return true;
5728 /* Build an expression representing a cast to type TYPE of expression EXPR.
5729 LOC is the location of the cast-- typically the open paren of the cast. */
5731 tree
5732 build_c_cast (location_t loc, tree type, tree expr)
5734 tree value;
5736 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5738 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5739 expr = TREE_OPERAND (expr, 0);
5741 value = expr;
5742 if (int_operands)
5743 value = remove_c_maybe_const_expr (value);
5745 if (type == error_mark_node || expr == error_mark_node)
5746 return error_mark_node;
5748 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5749 only in <protocol> qualifications. But when constructing cast expressions,
5750 the protocols do matter and must be kept around. */
5751 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5752 return build1 (NOP_EXPR, type, expr);
5754 type = TYPE_MAIN_VARIANT (type);
5756 if (TREE_CODE (type) == ARRAY_TYPE)
5758 error_at (loc, "cast specifies array type");
5759 return error_mark_node;
5762 if (TREE_CODE (type) == FUNCTION_TYPE)
5764 error_at (loc, "cast specifies function type");
5765 return error_mark_node;
5768 if (!VOID_TYPE_P (type))
5770 value = require_complete_type (loc, value);
5771 if (value == error_mark_node)
5772 return error_mark_node;
5775 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5777 if (RECORD_OR_UNION_TYPE_P (type))
5778 pedwarn (loc, OPT_Wpedantic,
5779 "ISO C forbids casting nonscalar to the same type");
5781 /* Convert to remove any qualifiers from VALUE's type. */
5782 value = convert (type, value);
5784 else if (TREE_CODE (type) == UNION_TYPE)
5786 tree field;
5788 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5789 if (TREE_TYPE (field) != error_mark_node
5790 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5791 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5792 break;
5794 if (field)
5796 tree t;
5797 bool maybe_const = true;
5799 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5800 t = c_fully_fold (value, false, &maybe_const);
5801 t = build_constructor_single (type, field, t);
5802 if (!maybe_const)
5803 t = c_wrap_maybe_const (t, true);
5804 t = digest_init (loc, type, t,
5805 NULL_TREE, false, true, 0);
5806 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5807 return t;
5809 error_at (loc, "cast to union type from type not present in union");
5810 return error_mark_node;
5812 else
5814 tree otype, ovalue;
5816 if (type == void_type_node)
5818 tree t = build1 (CONVERT_EXPR, type, value);
5819 SET_EXPR_LOCATION (t, loc);
5820 return t;
5823 otype = TREE_TYPE (value);
5825 /* Optionally warn about potentially worrisome casts. */
5826 if (warn_cast_qual
5827 && TREE_CODE (type) == POINTER_TYPE
5828 && TREE_CODE (otype) == POINTER_TYPE)
5829 handle_warn_cast_qual (loc, type, otype);
5831 /* Warn about conversions between pointers to disjoint
5832 address spaces. */
5833 if (TREE_CODE (type) == POINTER_TYPE
5834 && TREE_CODE (otype) == POINTER_TYPE
5835 && !null_pointer_constant_p (value))
5837 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5838 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5839 addr_space_t as_common;
5841 if (!addr_space_superset (as_to, as_from, &as_common))
5843 if (ADDR_SPACE_GENERIC_P (as_from))
5844 warning_at (loc, 0, "cast to %s address space pointer "
5845 "from disjoint generic address space pointer",
5846 c_addr_space_name (as_to));
5848 else if (ADDR_SPACE_GENERIC_P (as_to))
5849 warning_at (loc, 0, "cast to generic address space pointer "
5850 "from disjoint %s address space pointer",
5851 c_addr_space_name (as_from));
5853 else
5854 warning_at (loc, 0, "cast to %s address space pointer "
5855 "from disjoint %s address space pointer",
5856 c_addr_space_name (as_to),
5857 c_addr_space_name (as_from));
5861 /* Warn about possible alignment problems. */
5862 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5863 && TREE_CODE (type) == POINTER_TYPE
5864 && TREE_CODE (otype) == POINTER_TYPE
5865 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5866 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5867 /* Don't warn about opaque types, where the actual alignment
5868 restriction is unknown. */
5869 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5870 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5871 && min_align_of_type (TREE_TYPE (type))
5872 > min_align_of_type (TREE_TYPE (otype)))
5873 warning_at (loc, OPT_Wcast_align,
5874 "cast increases required alignment of target type");
5876 if (TREE_CODE (type) == INTEGER_TYPE
5877 && TREE_CODE (otype) == POINTER_TYPE
5878 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5879 /* Unlike conversion of integers to pointers, where the
5880 warning is disabled for converting constants because
5881 of cases such as SIG_*, warn about converting constant
5882 pointers to integers. In some cases it may cause unwanted
5883 sign extension, and a warning is appropriate. */
5884 warning_at (loc, OPT_Wpointer_to_int_cast,
5885 "cast from pointer to integer of different size");
5887 if (TREE_CODE (value) == CALL_EXPR
5888 && TREE_CODE (type) != TREE_CODE (otype))
5889 warning_at (loc, OPT_Wbad_function_cast,
5890 "cast from function call of type %qT "
5891 "to non-matching type %qT", otype, type);
5893 if (TREE_CODE (type) == POINTER_TYPE
5894 && TREE_CODE (otype) == INTEGER_TYPE
5895 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5896 /* Don't warn about converting any constant. */
5897 && !TREE_CONSTANT (value))
5898 warning_at (loc,
5899 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5900 "of different size");
5902 if (warn_strict_aliasing <= 2)
5903 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5905 /* If pedantic, warn for conversions between function and object
5906 pointer types, except for converting a null pointer constant
5907 to function pointer type. */
5908 if (pedantic
5909 && TREE_CODE (type) == POINTER_TYPE
5910 && TREE_CODE (otype) == POINTER_TYPE
5911 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5912 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5913 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5914 "conversion of function pointer to object pointer type");
5916 if (pedantic
5917 && TREE_CODE (type) == POINTER_TYPE
5918 && TREE_CODE (otype) == POINTER_TYPE
5919 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5920 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5921 && !null_pointer_constant_p (value))
5922 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5923 "conversion of object pointer to function pointer type");
5925 if (TREE_CODE (type) == POINTER_TYPE
5926 && TREE_CODE (otype) == POINTER_TYPE
5927 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5928 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5929 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5930 TREE_TYPE (otype)))
5931 warning_at (loc, OPT_Wcast_function_type,
5932 "cast between incompatible function types"
5933 " from %qT to %qT", otype, type);
5935 ovalue = value;
5936 value = convert (type, value);
5938 /* Ignore any integer overflow caused by the cast. */
5939 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5941 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5943 if (!TREE_OVERFLOW (value))
5945 /* Avoid clobbering a shared constant. */
5946 value = copy_node (value);
5947 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5950 else if (TREE_OVERFLOW (value))
5951 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5952 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5956 /* Don't let a cast be an lvalue. */
5957 if (lvalue_p (value))
5958 value = non_lvalue_loc (loc, value);
5960 /* Don't allow the results of casting to floating-point or complex
5961 types be confused with actual constants, or casts involving
5962 integer and pointer types other than direct integer-to-integer
5963 and integer-to-pointer be confused with integer constant
5964 expressions and null pointer constants. */
5965 if (TREE_CODE (value) == REAL_CST
5966 || TREE_CODE (value) == COMPLEX_CST
5967 || (TREE_CODE (value) == INTEGER_CST
5968 && !((TREE_CODE (expr) == INTEGER_CST
5969 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5970 || TREE_CODE (expr) == REAL_CST
5971 || TREE_CODE (expr) == COMPLEX_CST)))
5972 value = build1 (NOP_EXPR, type, value);
5974 /* If the expression has integer operands and so can occur in an
5975 unevaluated part of an integer constant expression, ensure the
5976 return value reflects this. */
5977 if (int_operands
5978 && INTEGRAL_TYPE_P (type)
5979 && !EXPR_INT_CONST_OPERANDS (value))
5980 value = note_integer_operands (value);
5982 protected_set_expr_location (value, loc);
5983 return value;
5986 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5987 location of the open paren of the cast, or the position of the cast
5988 expr. */
5989 tree
5990 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5992 tree type;
5993 tree type_expr = NULL_TREE;
5994 bool type_expr_const = true;
5995 tree ret;
5996 int saved_wsp = warn_strict_prototypes;
5998 /* This avoids warnings about unprototyped casts on
5999 integers. E.g. "#define SIG_DFL (void(*)())0". */
6000 if (TREE_CODE (expr) == INTEGER_CST)
6001 warn_strict_prototypes = 0;
6002 type = groktypename (type_name, &type_expr, &type_expr_const);
6003 warn_strict_prototypes = saved_wsp;
6005 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6006 && reject_gcc_builtin (expr))
6007 return error_mark_node;
6009 ret = build_c_cast (loc, type, expr);
6010 if (type_expr)
6012 bool inner_expr_const = true;
6013 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6014 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6015 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6016 && inner_expr_const);
6017 SET_EXPR_LOCATION (ret, loc);
6020 if (!EXPR_HAS_LOCATION (ret))
6021 protected_set_expr_location (ret, loc);
6023 /* C++ does not permits types to be defined in a cast, but it
6024 allows references to incomplete types. */
6025 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6026 warning_at (loc, OPT_Wc___compat,
6027 "defining a type in a cast is invalid in C++");
6029 return ret;
6032 /* Build an assignment expression of lvalue LHS from value RHS.
6033 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6034 may differ from TREE_TYPE (LHS) for an enum bitfield.
6035 MODIFYCODE is the code for a binary operator that we use
6036 to combine the old value of LHS with RHS to get the new value.
6037 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6038 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6039 which may differ from TREE_TYPE (RHS) for an enum value.
6041 LOCATION is the location of the MODIFYCODE operator.
6042 RHS_LOC is the location of the RHS. */
6044 tree
6045 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6046 enum tree_code modifycode,
6047 location_t rhs_loc, tree rhs, tree rhs_origtype)
6049 tree result;
6050 tree newrhs;
6051 tree rhseval = NULL_TREE;
6052 tree lhstype = TREE_TYPE (lhs);
6053 tree olhstype = lhstype;
6054 bool npc;
6055 bool is_atomic_op;
6057 /* Types that aren't fully specified cannot be used in assignments. */
6058 lhs = require_complete_type (location, lhs);
6060 /* Avoid duplicate error messages from operands that had errors. */
6061 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6062 return error_mark_node;
6064 /* Ensure an error for assigning a non-lvalue array to an array in
6065 C90. */
6066 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6068 error_at (location, "assignment to expression with array type");
6069 return error_mark_node;
6072 /* For ObjC properties, defer this check. */
6073 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6074 return error_mark_node;
6076 is_atomic_op = really_atomic_lvalue (lhs);
6078 newrhs = rhs;
6080 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6082 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6083 lhs_origtype, modifycode, rhs_loc, rhs,
6084 rhs_origtype);
6085 if (inner == error_mark_node)
6086 return error_mark_node;
6087 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6088 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6089 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6090 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6091 protected_set_expr_location (result, location);
6092 return result;
6095 /* If a binary op has been requested, combine the old LHS value with the RHS
6096 producing the value we should actually store into the LHS. */
6098 if (modifycode != NOP_EXPR)
6100 lhs = c_fully_fold (lhs, false, NULL, true);
6101 lhs = stabilize_reference (lhs);
6103 /* Construct the RHS for any non-atomic compound assignemnt. */
6104 if (!is_atomic_op)
6106 /* If in LHS op= RHS the RHS has side-effects, ensure they
6107 are preevaluated before the rest of the assignment expression's
6108 side-effects, because RHS could contain e.g. function calls
6109 that modify LHS. */
6110 if (TREE_SIDE_EFFECTS (rhs))
6112 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6113 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6114 else
6115 newrhs = save_expr (rhs);
6116 rhseval = newrhs;
6117 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6118 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6119 newrhs);
6121 newrhs = build_binary_op (location,
6122 modifycode, lhs, newrhs, true);
6124 /* The original type of the right hand side is no longer
6125 meaningful. */
6126 rhs_origtype = NULL_TREE;
6130 if (c_dialect_objc ())
6132 /* Check if we are modifying an Objective-C property reference;
6133 if so, we need to generate setter calls. */
6134 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6135 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6136 else
6137 result = objc_maybe_build_modify_expr (lhs, newrhs);
6138 if (result)
6139 goto return_result;
6141 /* Else, do the check that we postponed for Objective-C. */
6142 if (!lvalue_or_else (location, lhs, lv_assign))
6143 return error_mark_node;
6146 /* Give an error for storing in something that is 'const'. */
6148 if (TYPE_READONLY (lhstype)
6149 || (RECORD_OR_UNION_TYPE_P (lhstype)
6150 && C_TYPE_FIELDS_READONLY (lhstype)))
6152 readonly_error (location, lhs, lv_assign);
6153 return error_mark_node;
6155 else if (TREE_READONLY (lhs))
6156 readonly_warning (lhs, lv_assign);
6158 /* If storing into a structure or union member,
6159 it has probably been given type `int'.
6160 Compute the type that would go with
6161 the actual amount of storage the member occupies. */
6163 if (TREE_CODE (lhs) == COMPONENT_REF
6164 && (TREE_CODE (lhstype) == INTEGER_TYPE
6165 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6166 || TREE_CODE (lhstype) == REAL_TYPE
6167 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6168 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6170 /* If storing in a field that is in actuality a short or narrower than one,
6171 we must store in the field in its actual type. */
6173 if (lhstype != TREE_TYPE (lhs))
6175 lhs = copy_node (lhs);
6176 TREE_TYPE (lhs) = lhstype;
6179 /* Issue -Wc++-compat warnings about an assignment to an enum type
6180 when LHS does not have its original type. This happens for,
6181 e.g., an enum bitfield in a struct. */
6182 if (warn_cxx_compat
6183 && lhs_origtype != NULL_TREE
6184 && lhs_origtype != lhstype
6185 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6187 tree checktype = (rhs_origtype != NULL_TREE
6188 ? rhs_origtype
6189 : TREE_TYPE (rhs));
6190 if (checktype != error_mark_node
6191 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6192 || (is_atomic_op && modifycode != NOP_EXPR)))
6193 warning_at (location, OPT_Wc___compat,
6194 "enum conversion in assignment is invalid in C++");
6197 /* If the lhs is atomic, remove that qualifier. */
6198 if (is_atomic_op)
6200 lhstype = build_qualified_type (lhstype,
6201 (TYPE_QUALS (lhstype)
6202 & ~TYPE_QUAL_ATOMIC));
6203 olhstype = build_qualified_type (olhstype,
6204 (TYPE_QUALS (lhstype)
6205 & ~TYPE_QUAL_ATOMIC));
6208 /* Convert new value to destination type. Fold it first, then
6209 restore any excess precision information, for the sake of
6210 conversion warnings. */
6212 if (!(is_atomic_op && modifycode != NOP_EXPR))
6214 tree rhs_semantic_type = NULL_TREE;
6215 if (!c_in_omp_for)
6217 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6219 rhs_semantic_type = TREE_TYPE (newrhs);
6220 newrhs = TREE_OPERAND (newrhs, 0);
6222 npc = null_pointer_constant_p (newrhs);
6223 newrhs = c_fully_fold (newrhs, false, NULL);
6224 if (rhs_semantic_type)
6225 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6227 else
6228 npc = null_pointer_constant_p (newrhs);
6229 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6230 rhs_origtype, ic_assign, npc,
6231 NULL_TREE, NULL_TREE, 0);
6232 if (TREE_CODE (newrhs) == ERROR_MARK)
6233 return error_mark_node;
6236 /* Emit ObjC write barrier, if necessary. */
6237 if (c_dialect_objc () && flag_objc_gc)
6239 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6240 if (result)
6242 protected_set_expr_location (result, location);
6243 goto return_result;
6247 /* Scan operands. */
6249 if (is_atomic_op)
6250 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6251 else
6253 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6254 TREE_SIDE_EFFECTS (result) = 1;
6255 protected_set_expr_location (result, location);
6258 /* If we got the LHS in a different type for storing in,
6259 convert the result back to the nominal type of LHS
6260 so that the value we return always has the same type
6261 as the LHS argument. */
6263 if (olhstype == TREE_TYPE (result))
6264 goto return_result;
6266 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6267 rhs_origtype, ic_assign, false, NULL_TREE,
6268 NULL_TREE, 0);
6269 protected_set_expr_location (result, location);
6271 return_result:
6272 if (rhseval)
6273 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6274 return result;
6277 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6278 This is used to implement -fplan9-extensions. */
6280 static bool
6281 find_anonymous_field_with_type (tree struct_type, tree type)
6283 tree field;
6284 bool found;
6286 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6287 found = false;
6288 for (field = TYPE_FIELDS (struct_type);
6289 field != NULL_TREE;
6290 field = TREE_CHAIN (field))
6292 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6293 ? c_build_qualified_type (TREE_TYPE (field),
6294 TYPE_QUAL_ATOMIC)
6295 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6296 if (DECL_NAME (field) == NULL
6297 && comptypes (type, fieldtype))
6299 if (found)
6300 return false;
6301 found = true;
6303 else if (DECL_NAME (field) == NULL
6304 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6305 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6307 if (found)
6308 return false;
6309 found = true;
6312 return found;
6315 /* RHS is an expression whose type is pointer to struct. If there is
6316 an anonymous field in RHS with type TYPE, then return a pointer to
6317 that field in RHS. This is used with -fplan9-extensions. This
6318 returns NULL if no conversion could be found. */
6320 static tree
6321 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6323 tree rhs_struct_type, lhs_main_type;
6324 tree field, found_field;
6325 bool found_sub_field;
6326 tree ret;
6328 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6329 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6330 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6332 gcc_assert (POINTER_TYPE_P (type));
6333 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6334 ? c_build_qualified_type (TREE_TYPE (type),
6335 TYPE_QUAL_ATOMIC)
6336 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6338 found_field = NULL_TREE;
6339 found_sub_field = false;
6340 for (field = TYPE_FIELDS (rhs_struct_type);
6341 field != NULL_TREE;
6342 field = TREE_CHAIN (field))
6344 if (DECL_NAME (field) != NULL_TREE
6345 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6346 continue;
6347 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6348 ? c_build_qualified_type (TREE_TYPE (field),
6349 TYPE_QUAL_ATOMIC)
6350 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6351 if (comptypes (lhs_main_type, fieldtype))
6353 if (found_field != NULL_TREE)
6354 return NULL_TREE;
6355 found_field = field;
6357 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6358 lhs_main_type))
6360 if (found_field != NULL_TREE)
6361 return NULL_TREE;
6362 found_field = field;
6363 found_sub_field = true;
6367 if (found_field == NULL_TREE)
6368 return NULL_TREE;
6370 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6371 build_fold_indirect_ref (rhs), found_field,
6372 NULL_TREE);
6373 ret = build_fold_addr_expr_loc (location, ret);
6375 if (found_sub_field)
6377 ret = convert_to_anonymous_field (location, type, ret);
6378 gcc_assert (ret != NULL_TREE);
6381 return ret;
6384 /* Issue an error message for a bad initializer component.
6385 GMSGID identifies the message.
6386 The component name is taken from the spelling stack. */
6388 static void ATTRIBUTE_GCC_DIAG (2,0)
6389 error_init (location_t loc, const char *gmsgid, ...)
6391 char *ofwhat;
6393 auto_diagnostic_group d;
6395 /* The gmsgid may be a format string with %< and %>. */
6396 va_list ap;
6397 va_start (ap, gmsgid);
6398 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6399 va_end (ap);
6401 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6402 if (*ofwhat && warned)
6403 inform (loc, "(near initialization for %qs)", ofwhat);
6406 /* Issue a pedantic warning for a bad initializer component. OPT is
6407 the option OPT_* (from options.h) controlling this warning or 0 if
6408 it is unconditionally given. GMSGID identifies the message. The
6409 component name is taken from the spelling stack. */
6411 static void ATTRIBUTE_GCC_DIAG (3,0)
6412 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6414 /* Use the location where a macro was expanded rather than where
6415 it was defined to make sure macros defined in system headers
6416 but used incorrectly elsewhere are diagnosed. */
6417 location_t exploc = expansion_point_location_if_in_system_header (loc);
6418 auto_diagnostic_group d;
6419 va_list ap;
6420 va_start (ap, gmsgid);
6421 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6422 va_end (ap);
6423 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6424 if (*ofwhat && warned)
6425 inform (exploc, "(near initialization for %qs)", ofwhat);
6428 /* Issue a warning for a bad initializer component.
6430 OPT is the OPT_W* value corresponding to the warning option that
6431 controls this warning. GMSGID identifies the message. The
6432 component name is taken from the spelling stack. */
6434 static void
6435 warning_init (location_t loc, int opt, const char *gmsgid)
6437 char *ofwhat;
6438 bool warned;
6440 auto_diagnostic_group d;
6442 /* Use the location where a macro was expanded rather than where
6443 it was defined to make sure macros defined in system headers
6444 but used incorrectly elsewhere are diagnosed. */
6445 location_t exploc = expansion_point_location_if_in_system_header (loc);
6447 /* The gmsgid may be a format string with %< and %>. */
6448 warned = warning_at (exploc, opt, gmsgid);
6449 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6450 if (*ofwhat && warned)
6451 inform (exploc, "(near initialization for %qs)", ofwhat);
6454 /* If TYPE is an array type and EXPR is a parenthesized string
6455 constant, warn if pedantic that EXPR is being used to initialize an
6456 object of type TYPE. */
6458 void
6459 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6461 if (pedantic
6462 && TREE_CODE (type) == ARRAY_TYPE
6463 && TREE_CODE (expr.value) == STRING_CST
6464 && expr.original_code != STRING_CST)
6465 pedwarn_init (loc, OPT_Wpedantic,
6466 "array initialized from parenthesized string constant");
6469 /* Attempt to locate the parameter with the given index within FNDECL,
6470 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6472 static location_t
6473 get_fndecl_argument_location (tree fndecl, int argnum)
6475 int i;
6476 tree param;
6478 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6479 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6480 i < argnum && param;
6481 i++, param = TREE_CHAIN (param))
6484 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6485 return DECL_SOURCE_LOCATION (FNDECL). */
6486 if (param == NULL)
6487 return DECL_SOURCE_LOCATION (fndecl);
6489 return DECL_SOURCE_LOCATION (param);
6492 /* Issue a note about a mismatching argument for parameter PARMNUM
6493 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6494 Attempt to issue the note at the pertinent parameter of the decl;
6495 failing that issue it at the location of FUNDECL; failing that
6496 issue it at PLOC. */
6498 static void
6499 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6500 tree expected_type, tree actual_type)
6502 location_t loc;
6503 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6504 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6505 else
6506 loc = ploc;
6508 inform (loc,
6509 "expected %qT but argument is of type %qT",
6510 expected_type, actual_type);
6513 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6514 function FUNDECL declared without prototype to parameter PARMNUM of
6515 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6517 static void
6518 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6519 tree parmtype, tree argtype)
6521 tree_code parmcode = TREE_CODE (parmtype);
6522 tree_code argcode = TREE_CODE (argtype);
6523 tree promoted = c_type_promotes_to (argtype);
6525 /* Avoid warning for enum arguments that promote to an integer type
6526 of the same size/mode. */
6527 if (parmcode == INTEGER_TYPE
6528 && argcode == ENUMERAL_TYPE
6529 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6530 return;
6532 if ((parmcode == argcode
6533 || (parmcode == INTEGER_TYPE
6534 && argcode == ENUMERAL_TYPE))
6535 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6536 return;
6538 /* This diagnoses even signed/unsigned mismatches. Those might be
6539 safe in many cases but GCC may emit suboptimal code for them so
6540 warning on those cases drives efficiency improvements. */
6541 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6542 TYPE_MAIN_VARIANT (promoted) == argtype
6543 ? G_("%qD argument %d type is %qT where %qT is expected "
6544 "in a call to built-in function declared without "
6545 "prototype")
6546 : G_("%qD argument %d promotes to %qT where %qT is expected "
6547 "in a call to built-in function declared without "
6548 "prototype"),
6549 fundecl, parmnum, promoted, parmtype))
6550 inform (DECL_SOURCE_LOCATION (fundecl),
6551 "built-in %qD declared here",
6552 fundecl);
6555 /* Convert value RHS to type TYPE as preparation for an assignment to
6556 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6557 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6558 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6559 constant before any folding.
6560 The real work of conversion is done by `convert'.
6561 The purpose of this function is to generate error messages
6562 for assignments that are not allowed in C.
6563 ERRTYPE says whether it is argument passing, assignment,
6564 initialization or return.
6566 In the following example, '~' denotes where EXPR_LOC and '^' where
6567 LOCATION point to:
6569 f (var); [ic_argpass]
6570 ^ ~~~
6571 x = var; [ic_assign]
6572 ^ ~~~;
6573 int x = var; [ic_init]
6575 return x; [ic_return]
6578 FUNCTION is a tree for the function being called.
6579 PARMNUM is the number of the argument, for printing in error messages.
6580 WARNOPT may be set to a warning option to issue the corresponding warning
6581 rather than an error for invalid conversions. Used for calls to built-in
6582 functions declared without a prototype. */
6584 static tree
6585 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6586 tree rhs, tree origtype, enum impl_conv errtype,
6587 bool null_pointer_constant, tree fundecl,
6588 tree function, int parmnum, int warnopt /* = 0 */)
6590 enum tree_code codel = TREE_CODE (type);
6591 tree orig_rhs = rhs;
6592 tree rhstype;
6593 enum tree_code coder;
6594 tree rname = NULL_TREE;
6595 bool objc_ok = false;
6597 /* Use the expansion point location to handle cases such as user's
6598 function returning a wrong-type macro defined in a system header. */
6599 location = expansion_point_location_if_in_system_header (location);
6601 if (errtype == ic_argpass)
6603 tree selector;
6604 /* Change pointer to function to the function itself for
6605 diagnostics. */
6606 if (TREE_CODE (function) == ADDR_EXPR
6607 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6608 function = TREE_OPERAND (function, 0);
6610 /* Handle an ObjC selector specially for diagnostics. */
6611 selector = objc_message_selector ();
6612 rname = function;
6613 if (selector && parmnum > 2)
6615 rname = selector;
6616 parmnum -= 2;
6620 /* This macro is used to emit diagnostics to ensure that all format
6621 strings are complete sentences, visible to gettext and checked at
6622 compile time. */
6623 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6624 do { \
6625 switch (errtype) \
6627 case ic_argpass: \
6629 auto_diagnostic_group d; \
6630 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6631 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6633 break; \
6634 case ic_assign: \
6635 pedwarn (LOCATION, OPT, AS); \
6636 break; \
6637 case ic_init: \
6638 pedwarn_init (LOCATION, OPT, IN); \
6639 break; \
6640 case ic_return: \
6641 pedwarn (LOCATION, OPT, RE); \
6642 break; \
6643 default: \
6644 gcc_unreachable (); \
6646 } while (0)
6648 /* This macro is used to emit diagnostics to ensure that all format
6649 strings are complete sentences, visible to gettext and checked at
6650 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6651 extra parameter to enumerate qualifiers. */
6652 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6653 do { \
6654 switch (errtype) \
6656 case ic_argpass: \
6658 auto_diagnostic_group d; \
6659 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6660 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6662 break; \
6663 case ic_assign: \
6664 pedwarn (LOCATION, OPT, AS, QUALS); \
6665 break; \
6666 case ic_init: \
6667 pedwarn (LOCATION, OPT, IN, QUALS); \
6668 break; \
6669 case ic_return: \
6670 pedwarn (LOCATION, OPT, RE, QUALS); \
6671 break; \
6672 default: \
6673 gcc_unreachable (); \
6675 } while (0)
6677 /* This macro is used to emit diagnostics to ensure that all format
6678 strings are complete sentences, visible to gettext and checked at
6679 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6680 warning_at instead of pedwarn. */
6681 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6682 do { \
6683 switch (errtype) \
6685 case ic_argpass: \
6687 auto_diagnostic_group d; \
6688 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6689 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6691 break; \
6692 case ic_assign: \
6693 warning_at (LOCATION, OPT, AS, QUALS); \
6694 break; \
6695 case ic_init: \
6696 warning_at (LOCATION, OPT, IN, QUALS); \
6697 break; \
6698 case ic_return: \
6699 warning_at (LOCATION, OPT, RE, QUALS); \
6700 break; \
6701 default: \
6702 gcc_unreachable (); \
6704 } while (0)
6706 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6707 rhs = TREE_OPERAND (rhs, 0);
6709 rhstype = TREE_TYPE (rhs);
6710 coder = TREE_CODE (rhstype);
6712 if (coder == ERROR_MARK)
6713 return error_mark_node;
6715 if (c_dialect_objc ())
6717 int parmno;
6719 switch (errtype)
6721 case ic_return:
6722 parmno = 0;
6723 break;
6725 case ic_assign:
6726 parmno = -1;
6727 break;
6729 case ic_init:
6730 parmno = -2;
6731 break;
6733 default:
6734 parmno = parmnum;
6735 break;
6738 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6741 if (warn_cxx_compat)
6743 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6744 if (checktype != error_mark_node
6745 && TREE_CODE (type) == ENUMERAL_TYPE
6746 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6747 switch (errtype)
6749 case ic_argpass:
6750 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6751 "passing argument %d of %qE is invalid in C++",
6752 parmnum, rname))
6753 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6754 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6755 "expected %qT but argument is of type %qT",
6756 type, rhstype);
6757 break;
6758 case ic_assign:
6759 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6760 "%qT in assignment is invalid in C++", rhstype, type);
6761 break;
6762 case ic_init:
6763 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6764 "%qT to %qT in initialization is invalid in C++",
6765 rhstype, type);
6766 break;
6767 case ic_return:
6768 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6769 "%qT in return is invalid in C++", rhstype, type);
6770 break;
6771 default:
6772 gcc_unreachable ();
6776 if (warn_enum_conversion)
6778 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6779 if (checktype != error_mark_node
6780 && TREE_CODE (checktype) == ENUMERAL_TYPE
6781 && TREE_CODE (type) == ENUMERAL_TYPE
6782 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6784 gcc_rich_location loc (location);
6785 warning_at (&loc, OPT_Wenum_conversion,
6786 "implicit conversion from %qT to %qT",
6787 checktype, type);
6791 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6793 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6794 return rhs;
6797 if (coder == VOID_TYPE)
6799 /* Except for passing an argument to an unprototyped function,
6800 this is a constraint violation. When passing an argument to
6801 an unprototyped function, it is compile-time undefined;
6802 making it a constraint in that case was rejected in
6803 DR#252. */
6804 const char msg[] = "void value not ignored as it ought to be";
6805 if (warnopt)
6806 warning_at (location, warnopt, msg);
6807 else
6808 error_at (location, msg);
6809 return error_mark_node;
6811 rhs = require_complete_type (location, rhs);
6812 if (rhs == error_mark_node)
6813 return error_mark_node;
6815 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6816 return error_mark_node;
6818 /* A non-reference type can convert to a reference. This handles
6819 va_start, va_copy and possibly port built-ins. */
6820 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6822 if (!lvalue_p (rhs))
6824 const char msg[] = "cannot pass rvalue to reference parameter";
6825 if (warnopt)
6826 warning_at (location, warnopt, msg);
6827 else
6828 error_at (location, msg);
6829 return error_mark_node;
6831 if (!c_mark_addressable (rhs))
6832 return error_mark_node;
6833 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6834 SET_EXPR_LOCATION (rhs, location);
6836 rhs = convert_for_assignment (location, expr_loc,
6837 build_pointer_type (TREE_TYPE (type)),
6838 rhs, origtype, errtype,
6839 null_pointer_constant, fundecl, function,
6840 parmnum, warnopt);
6841 if (rhs == error_mark_node)
6842 return error_mark_node;
6844 rhs = build1 (NOP_EXPR, type, rhs);
6845 SET_EXPR_LOCATION (rhs, location);
6846 return rhs;
6848 /* Some types can interconvert without explicit casts. */
6849 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6850 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6851 return convert (type, rhs);
6852 /* Arithmetic types all interconvert, and enum is treated like int. */
6853 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6854 || codel == FIXED_POINT_TYPE
6855 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6856 || codel == BOOLEAN_TYPE)
6857 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6858 || coder == FIXED_POINT_TYPE
6859 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6860 || coder == BOOLEAN_TYPE))
6862 if (warnopt && errtype == ic_argpass)
6863 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
6864 rhstype);
6866 bool save = in_late_binary_op;
6867 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6868 || (coder == REAL_TYPE
6869 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6870 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6871 in_late_binary_op = true;
6872 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6873 ? expr_loc : location, type, orig_rhs);
6874 in_late_binary_op = save;
6875 return ret;
6878 /* Aggregates in different TUs might need conversion. */
6879 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6880 && codel == coder
6881 && comptypes (type, rhstype))
6882 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6883 ? expr_loc : location, type, rhs);
6885 /* Conversion to a transparent union or record from its member types.
6886 This applies only to function arguments. */
6887 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6888 && TYPE_TRANSPARENT_AGGR (type))
6889 && errtype == ic_argpass)
6891 tree memb, marginal_memb = NULL_TREE;
6893 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6895 tree memb_type = TREE_TYPE (memb);
6897 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6898 TYPE_MAIN_VARIANT (rhstype)))
6899 break;
6901 if (TREE_CODE (memb_type) != POINTER_TYPE)
6902 continue;
6904 if (coder == POINTER_TYPE)
6906 tree ttl = TREE_TYPE (memb_type);
6907 tree ttr = TREE_TYPE (rhstype);
6909 /* Any non-function converts to a [const][volatile] void *
6910 and vice versa; otherwise, targets must be the same.
6911 Meanwhile, the lhs target must have all the qualifiers of
6912 the rhs. */
6913 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6914 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6915 || comp_target_types (location, memb_type, rhstype))
6917 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6918 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6919 /* If this type won't generate any warnings, use it. */
6920 if (lquals == rquals
6921 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6922 && TREE_CODE (ttl) == FUNCTION_TYPE)
6923 ? ((lquals | rquals) == rquals)
6924 : ((lquals | rquals) == lquals)))
6925 break;
6927 /* Keep looking for a better type, but remember this one. */
6928 if (!marginal_memb)
6929 marginal_memb = memb;
6933 /* Can convert integer zero to any pointer type. */
6934 if (null_pointer_constant)
6936 rhs = null_pointer_node;
6937 break;
6941 if (memb || marginal_memb)
6943 if (!memb)
6945 /* We have only a marginally acceptable member type;
6946 it needs a warning. */
6947 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6948 tree ttr = TREE_TYPE (rhstype);
6950 /* Const and volatile mean something different for function
6951 types, so the usual warnings are not appropriate. */
6952 if (TREE_CODE (ttr) == FUNCTION_TYPE
6953 && TREE_CODE (ttl) == FUNCTION_TYPE)
6955 /* Because const and volatile on functions are
6956 restrictions that say the function will not do
6957 certain things, it is okay to use a const or volatile
6958 function where an ordinary one is wanted, but not
6959 vice-versa. */
6960 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6961 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6962 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6963 OPT_Wdiscarded_qualifiers,
6964 G_("passing argument %d of %qE "
6965 "makes %q#v qualified function "
6966 "pointer from unqualified"),
6967 G_("assignment makes %q#v qualified "
6968 "function pointer from "
6969 "unqualified"),
6970 G_("initialization makes %q#v qualified "
6971 "function pointer from "
6972 "unqualified"),
6973 G_("return makes %q#v qualified function "
6974 "pointer from unqualified"),
6975 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6977 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6978 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6979 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6980 OPT_Wdiscarded_qualifiers,
6981 G_("passing argument %d of %qE discards "
6982 "%qv qualifier from pointer target type"),
6983 G_("assignment discards %qv qualifier "
6984 "from pointer target type"),
6985 G_("initialization discards %qv qualifier "
6986 "from pointer target type"),
6987 G_("return discards %qv qualifier from "
6988 "pointer target type"),
6989 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6991 memb = marginal_memb;
6994 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6995 pedwarn (location, OPT_Wpedantic,
6996 "ISO C prohibits argument conversion to union type");
6998 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6999 return build_constructor_single (type, memb, rhs);
7003 /* Conversions among pointers */
7004 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7005 && (coder == codel))
7007 /* If RHS refers to a built-in declared without a prototype
7008 BLTIN is the declaration of the built-in with a prototype
7009 and RHSTYPE is set to the actual type of the built-in. */
7010 tree bltin;
7011 rhstype = type_or_builtin_type (rhs, &bltin);
7013 tree ttl = TREE_TYPE (type);
7014 tree ttr = TREE_TYPE (rhstype);
7015 tree mvl = ttl;
7016 tree mvr = ttr;
7017 bool is_opaque_pointer;
7018 int target_cmp = 0; /* Cache comp_target_types () result. */
7019 addr_space_t asl;
7020 addr_space_t asr;
7022 if (TREE_CODE (mvl) != ARRAY_TYPE)
7023 mvl = (TYPE_ATOMIC (mvl)
7024 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7025 TYPE_QUAL_ATOMIC)
7026 : TYPE_MAIN_VARIANT (mvl));
7027 if (TREE_CODE (mvr) != ARRAY_TYPE)
7028 mvr = (TYPE_ATOMIC (mvr)
7029 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7030 TYPE_QUAL_ATOMIC)
7031 : TYPE_MAIN_VARIANT (mvr));
7032 /* Opaque pointers are treated like void pointers. */
7033 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7035 /* The Plan 9 compiler permits a pointer to a struct to be
7036 automatically converted into a pointer to an anonymous field
7037 within the struct. */
7038 if (flag_plan9_extensions
7039 && RECORD_OR_UNION_TYPE_P (mvl)
7040 && RECORD_OR_UNION_TYPE_P (mvr)
7041 && mvl != mvr)
7043 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7044 if (new_rhs != NULL_TREE)
7046 rhs = new_rhs;
7047 rhstype = TREE_TYPE (rhs);
7048 coder = TREE_CODE (rhstype);
7049 ttr = TREE_TYPE (rhstype);
7050 mvr = TYPE_MAIN_VARIANT (ttr);
7054 /* C++ does not allow the implicit conversion void* -> T*. However,
7055 for the purpose of reducing the number of false positives, we
7056 tolerate the special case of
7058 int *p = NULL;
7060 where NULL is typically defined in C to be '(void *) 0'. */
7061 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7062 warning_at (errtype == ic_argpass ? expr_loc : location,
7063 OPT_Wc___compat,
7064 "request for implicit conversion "
7065 "from %qT to %qT not permitted in C++", rhstype, type);
7067 /* See if the pointers point to incompatible address spaces. */
7068 asl = TYPE_ADDR_SPACE (ttl);
7069 asr = TYPE_ADDR_SPACE (ttr);
7070 if (!null_pointer_constant_p (rhs)
7071 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7073 switch (errtype)
7075 case ic_argpass:
7077 const char msg[] = G_("passing argument %d of %qE from "
7078 "pointer to non-enclosed address space");
7079 if (warnopt)
7080 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7081 else
7082 error_at (expr_loc, msg, parmnum, rname);
7083 break;
7085 case ic_assign:
7087 const char msg[] = G_("assignment from pointer to "
7088 "non-enclosed address space");
7089 if (warnopt)
7090 warning_at (location, warnopt, msg);
7091 else
7092 error_at (location, msg);
7093 break;
7095 case ic_init:
7097 const char msg[] = G_("initialization from pointer to "
7098 "non-enclosed address space");
7099 if (warnopt)
7100 warning_at (location, warnopt, msg);
7101 else
7102 error_at (location, msg);
7103 break;
7105 case ic_return:
7107 const char msg[] = G_("return from pointer to "
7108 "non-enclosed address space");
7109 if (warnopt)
7110 warning_at (location, warnopt, msg);
7111 else
7112 error_at (location, msg);
7113 break;
7115 default:
7116 gcc_unreachable ();
7118 return error_mark_node;
7121 /* Check if the right-hand side has a format attribute but the
7122 left-hand side doesn't. */
7123 if (warn_suggest_attribute_format
7124 && check_missing_format_attribute (type, rhstype))
7126 switch (errtype)
7128 case ic_argpass:
7129 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7130 "argument %d of %qE might be "
7131 "a candidate for a format attribute",
7132 parmnum, rname);
7133 break;
7134 case ic_assign:
7135 warning_at (location, OPT_Wsuggest_attribute_format,
7136 "assignment left-hand side might be "
7137 "a candidate for a format attribute");
7138 break;
7139 case ic_init:
7140 warning_at (location, OPT_Wsuggest_attribute_format,
7141 "initialization left-hand side might be "
7142 "a candidate for a format attribute");
7143 break;
7144 case ic_return:
7145 warning_at (location, OPT_Wsuggest_attribute_format,
7146 "return type might be "
7147 "a candidate for a format attribute");
7148 break;
7149 default:
7150 gcc_unreachable ();
7154 /* See if the pointers point to incompatible scalar storage orders. */
7155 if (warn_scalar_storage_order
7156 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7157 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7159 switch (errtype)
7161 case ic_argpass:
7162 /* Do not warn for built-in functions, for example memcpy, since we
7163 control how they behave and they can be useful in this area. */
7164 if (TREE_CODE (rname) != FUNCTION_DECL || !DECL_IS_BUILTIN (rname))
7165 warning_at (location, OPT_Wscalar_storage_order,
7166 "passing argument %d of %qE from incompatible "
7167 "scalar storage order", parmnum, rname);
7168 break;
7169 case ic_assign:
7170 warning_at (location, OPT_Wscalar_storage_order,
7171 "assignment to %qT from pointer type %qT with "
7172 "incompatible scalar storage order", type, rhstype);
7173 break;
7174 case ic_init:
7175 warning_at (location, OPT_Wscalar_storage_order,
7176 "initialization of %qT from pointer type %qT with "
7177 "incompatible scalar storage order", type, rhstype);
7178 break;
7179 case ic_return:
7180 warning_at (location, OPT_Wscalar_storage_order,
7181 "returning %qT from pointer type with incompatible "
7182 "scalar storage order %qT", rhstype, type);
7183 break;
7184 default:
7185 gcc_unreachable ();
7189 /* Any non-function converts to a [const][volatile] void *
7190 and vice versa; otherwise, targets must be the same.
7191 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7192 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7193 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7194 || (target_cmp = comp_target_types (location, type, rhstype))
7195 || is_opaque_pointer
7196 || ((c_common_unsigned_type (mvl)
7197 == c_common_unsigned_type (mvr))
7198 && (c_common_signed_type (mvl)
7199 == c_common_signed_type (mvr))
7200 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7202 /* Warn about loss of qualifers from pointers to arrays with
7203 qualifiers on the element type. */
7204 if (TREE_CODE (ttr) == ARRAY_TYPE)
7206 ttr = strip_array_types (ttr);
7207 ttl = strip_array_types (ttl);
7209 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7210 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7211 WARNING_FOR_QUALIFIERS (location, expr_loc,
7212 OPT_Wdiscarded_array_qualifiers,
7213 G_("passing argument %d of %qE discards "
7214 "%qv qualifier from pointer target type"),
7215 G_("assignment discards %qv qualifier "
7216 "from pointer target type"),
7217 G_("initialization discards %qv qualifier "
7218 "from pointer target type"),
7219 G_("return discards %qv qualifier from "
7220 "pointer target type"),
7221 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7223 else if (pedantic
7224 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7226 (VOID_TYPE_P (ttr)
7227 && !null_pointer_constant
7228 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7229 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7230 G_("ISO C forbids passing argument %d of "
7231 "%qE between function pointer "
7232 "and %<void *%>"),
7233 G_("ISO C forbids assignment between "
7234 "function pointer and %<void *%>"),
7235 G_("ISO C forbids initialization between "
7236 "function pointer and %<void *%>"),
7237 G_("ISO C forbids return between function "
7238 "pointer and %<void *%>"));
7239 /* Const and volatile mean something different for function types,
7240 so the usual warnings are not appropriate. */
7241 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7242 && TREE_CODE (ttl) != FUNCTION_TYPE)
7244 /* Don't warn about loss of qualifier for conversions from
7245 qualified void* to pointers to arrays with corresponding
7246 qualifier on the element type. */
7247 if (!pedantic)
7248 ttl = strip_array_types (ttl);
7250 /* Assignments between atomic and non-atomic objects are OK. */
7251 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7252 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7254 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7255 OPT_Wdiscarded_qualifiers,
7256 G_("passing argument %d of %qE discards "
7257 "%qv qualifier from pointer target type"),
7258 G_("assignment discards %qv qualifier "
7259 "from pointer target type"),
7260 G_("initialization discards %qv qualifier "
7261 "from pointer target type"),
7262 G_("return discards %qv qualifier from "
7263 "pointer target type"),
7264 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7266 /* If this is not a case of ignoring a mismatch in signedness,
7267 no warning. */
7268 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7269 || target_cmp)
7271 /* If there is a mismatch, do warn. */
7272 else if (warn_pointer_sign)
7273 switch (errtype)
7275 case ic_argpass:
7277 auto_diagnostic_group d;
7278 range_label_for_type_mismatch rhs_label (rhstype, type);
7279 gcc_rich_location richloc (expr_loc, &rhs_label);
7280 if (pedwarn (&richloc, OPT_Wpointer_sign,
7281 "pointer targets in passing argument %d of "
7282 "%qE differ in signedness", parmnum, rname))
7283 inform_for_arg (fundecl, expr_loc, parmnum, type,
7284 rhstype);
7286 break;
7287 case ic_assign:
7288 pedwarn (location, OPT_Wpointer_sign,
7289 "pointer targets in assignment from %qT to %qT "
7290 "differ in signedness", rhstype, type);
7291 break;
7292 case ic_init:
7293 pedwarn_init (location, OPT_Wpointer_sign,
7294 "pointer targets in initialization of %qT "
7295 "from %qT differ in signedness", type,
7296 rhstype);
7297 break;
7298 case ic_return:
7299 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7300 "returning %qT from a function with return type "
7301 "%qT differ in signedness", rhstype, type);
7302 break;
7303 default:
7304 gcc_unreachable ();
7307 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7308 && TREE_CODE (ttr) == FUNCTION_TYPE)
7310 /* Because const and volatile on functions are restrictions
7311 that say the function will not do certain things,
7312 it is okay to use a const or volatile function
7313 where an ordinary one is wanted, but not vice-versa. */
7314 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7315 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7316 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7317 OPT_Wdiscarded_qualifiers,
7318 G_("passing argument %d of %qE makes "
7319 "%q#v qualified function pointer "
7320 "from unqualified"),
7321 G_("assignment makes %q#v qualified function "
7322 "pointer from unqualified"),
7323 G_("initialization makes %q#v qualified "
7324 "function pointer from unqualified"),
7325 G_("return makes %q#v qualified function "
7326 "pointer from unqualified"),
7327 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7330 /* Avoid warning about the volatile ObjC EH puts on decls. */
7331 else if (!objc_ok)
7333 switch (errtype)
7335 case ic_argpass:
7337 auto_diagnostic_group d;
7338 range_label_for_type_mismatch rhs_label (rhstype, type);
7339 gcc_rich_location richloc (expr_loc, &rhs_label);
7340 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7341 "passing argument %d of %qE from incompatible "
7342 "pointer type", parmnum, rname))
7343 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7345 break;
7346 case ic_assign:
7347 if (bltin)
7348 pedwarn (location, OPT_Wincompatible_pointer_types,
7349 "assignment to %qT from pointer to "
7350 "%qD with incompatible type %qT",
7351 type, bltin, rhstype);
7352 else
7353 pedwarn (location, OPT_Wincompatible_pointer_types,
7354 "assignment to %qT from incompatible pointer type %qT",
7355 type, rhstype);
7356 break;
7357 case ic_init:
7358 if (bltin)
7359 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7360 "initialization of %qT from pointer to "
7361 "%qD with incompatible type %qT",
7362 type, bltin, rhstype);
7363 else
7364 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7365 "initialization of %qT from incompatible "
7366 "pointer type %qT",
7367 type, rhstype);
7368 break;
7369 case ic_return:
7370 if (bltin)
7371 pedwarn (location, OPT_Wincompatible_pointer_types,
7372 "returning pointer to %qD of type %qT from "
7373 "a function with incompatible type %qT",
7374 bltin, rhstype, type);
7375 else
7376 pedwarn (location, OPT_Wincompatible_pointer_types,
7377 "returning %qT from a function with incompatible "
7378 "return type %qT", rhstype, type);
7379 break;
7380 default:
7381 gcc_unreachable ();
7385 /* If RHS isn't an address, check pointer or array of packed
7386 struct or union. */
7387 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7389 return convert (type, rhs);
7391 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7393 /* ??? This should not be an error when inlining calls to
7394 unprototyped functions. */
7395 const char msg[] = "invalid use of non-lvalue array";
7396 if (warnopt)
7397 warning_at (location, warnopt, msg);
7398 else
7399 error_at (location, msg);
7400 return error_mark_node;
7402 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7404 /* An explicit constant 0 can convert to a pointer,
7405 or one that results from arithmetic, even including
7406 a cast to integer type. */
7407 if (!null_pointer_constant)
7408 switch (errtype)
7410 case ic_argpass:
7412 auto_diagnostic_group d;
7413 range_label_for_type_mismatch rhs_label (rhstype, type);
7414 gcc_rich_location richloc (expr_loc, &rhs_label);
7415 if (pedwarn (&richloc, OPT_Wint_conversion,
7416 "passing argument %d of %qE makes pointer from "
7417 "integer without a cast", parmnum, rname))
7418 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7420 break;
7421 case ic_assign:
7422 pedwarn (location, OPT_Wint_conversion,
7423 "assignment to %qT from %qT makes pointer from integer "
7424 "without a cast", type, rhstype);
7425 break;
7426 case ic_init:
7427 pedwarn_init (location, OPT_Wint_conversion,
7428 "initialization of %qT from %qT makes pointer from "
7429 "integer without a cast", type, rhstype);
7430 break;
7431 case ic_return:
7432 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7433 "function with return type %qT makes pointer from "
7434 "integer without a cast", rhstype, type);
7435 break;
7436 default:
7437 gcc_unreachable ();
7440 return convert (type, rhs);
7442 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7444 switch (errtype)
7446 case ic_argpass:
7448 auto_diagnostic_group d;
7449 range_label_for_type_mismatch rhs_label (rhstype, type);
7450 gcc_rich_location richloc (expr_loc, &rhs_label);
7451 if (pedwarn (&richloc, OPT_Wint_conversion,
7452 "passing argument %d of %qE makes integer from "
7453 "pointer without a cast", parmnum, rname))
7454 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7456 break;
7457 case ic_assign:
7458 pedwarn (location, OPT_Wint_conversion,
7459 "assignment to %qT from %qT makes integer from pointer "
7460 "without a cast", type, rhstype);
7461 break;
7462 case ic_init:
7463 pedwarn_init (location, OPT_Wint_conversion,
7464 "initialization of %qT from %qT makes integer from "
7465 "pointer without a cast", type, rhstype);
7466 break;
7467 case ic_return:
7468 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7469 "function with return type %qT makes integer from "
7470 "pointer without a cast", rhstype, type);
7471 break;
7472 default:
7473 gcc_unreachable ();
7476 return convert (type, rhs);
7478 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7480 tree ret;
7481 bool save = in_late_binary_op;
7482 in_late_binary_op = true;
7483 ret = convert (type, rhs);
7484 in_late_binary_op = save;
7485 return ret;
7488 switch (errtype)
7490 case ic_argpass:
7492 auto_diagnostic_group d;
7493 range_label_for_type_mismatch rhs_label (rhstype, type);
7494 gcc_rich_location richloc (expr_loc, &rhs_label);
7495 const char msg[] = G_("incompatible type for argument %d of %qE");
7496 if (warnopt)
7497 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7498 else
7499 error_at (&richloc, msg, parmnum, rname);
7500 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7502 break;
7503 case ic_assign:
7505 const char msg[]
7506 = G_("incompatible types when assigning to type %qT from type %qT");
7507 if (warnopt)
7508 warning_at (expr_loc, 0, msg, type, rhstype);
7509 else
7510 error_at (expr_loc, msg, type, rhstype);
7511 break;
7513 case ic_init:
7515 const char msg[]
7516 = G_("incompatible types when initializing type %qT using type %qT");
7517 if (warnopt)
7518 warning_at (location, 0, msg, type, rhstype);
7519 else
7520 error_at (location, msg, type, rhstype);
7521 break;
7523 case ic_return:
7525 const char msg[]
7526 = G_("incompatible types when returning type %qT but %qT was expected");
7527 if (warnopt)
7528 warning_at (location, 0, msg, rhstype, type);
7529 else
7530 error_at (location, msg, rhstype, type);
7531 break;
7533 default:
7534 gcc_unreachable ();
7537 return error_mark_node;
7540 /* If VALUE is a compound expr all of whose expressions are constant, then
7541 return its value. Otherwise, return error_mark_node.
7543 This is for handling COMPOUND_EXPRs as initializer elements
7544 which is allowed with a warning when -pedantic is specified. */
7546 static tree
7547 valid_compound_expr_initializer (tree value, tree endtype)
7549 if (TREE_CODE (value) == COMPOUND_EXPR)
7551 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7552 == error_mark_node)
7553 return error_mark_node;
7554 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7555 endtype);
7557 else if (!initializer_constant_valid_p (value, endtype))
7558 return error_mark_node;
7559 else
7560 return value;
7563 /* Perform appropriate conversions on the initial value of a variable,
7564 store it in the declaration DECL,
7565 and print any error messages that are appropriate.
7566 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7567 If the init is invalid, store an ERROR_MARK.
7569 INIT_LOC is the location of the initial value. */
7571 void
7572 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7574 tree value, type;
7575 bool npc = false;
7577 /* If variable's type was invalidly declared, just ignore it. */
7579 type = TREE_TYPE (decl);
7580 if (TREE_CODE (type) == ERROR_MARK)
7581 return;
7583 /* Digest the specified initializer into an expression. */
7585 if (init)
7586 npc = null_pointer_constant_p (init);
7587 value = digest_init (init_loc, type, init, origtype, npc,
7588 true, TREE_STATIC (decl));
7590 /* Store the expression if valid; else report error. */
7592 if (!in_system_header_at (input_location)
7593 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7594 warning (OPT_Wtraditional, "traditional C rejects automatic "
7595 "aggregate initialization");
7597 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7598 DECL_INITIAL (decl) = value;
7600 /* ANSI wants warnings about out-of-range constant initializers. */
7601 STRIP_TYPE_NOPS (value);
7602 if (TREE_STATIC (decl))
7603 constant_expression_warning (value);
7605 /* Check if we need to set array size from compound literal size. */
7606 if (TREE_CODE (type) == ARRAY_TYPE
7607 && TYPE_DOMAIN (type) == NULL_TREE
7608 && value != error_mark_node)
7610 tree inside_init = init;
7612 STRIP_TYPE_NOPS (inside_init);
7613 inside_init = fold (inside_init);
7615 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7617 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7619 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7621 /* For int foo[] = (int [3]){1}; we need to set array size
7622 now since later on array initializer will be just the
7623 brace enclosed list of the compound literal. */
7624 tree etype = strip_array_types (TREE_TYPE (decl));
7625 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7626 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7627 layout_type (type);
7628 layout_decl (cldecl, 0);
7629 TREE_TYPE (decl)
7630 = c_build_qualified_type (type, TYPE_QUALS (etype));
7636 /* Methods for storing and printing names for error messages. */
7638 /* Implement a spelling stack that allows components of a name to be pushed
7639 and popped. Each element on the stack is this structure. */
7641 struct spelling
7643 int kind;
7644 union
7646 unsigned HOST_WIDE_INT i;
7647 const char *s;
7648 } u;
7651 #define SPELLING_STRING 1
7652 #define SPELLING_MEMBER 2
7653 #define SPELLING_BOUNDS 3
7655 static struct spelling *spelling; /* Next stack element (unused). */
7656 static struct spelling *spelling_base; /* Spelling stack base. */
7657 static int spelling_size; /* Size of the spelling stack. */
7659 /* Macros to save and restore the spelling stack around push_... functions.
7660 Alternative to SAVE_SPELLING_STACK. */
7662 #define SPELLING_DEPTH() (spelling - spelling_base)
7663 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7665 /* Push an element on the spelling stack with type KIND and assign VALUE
7666 to MEMBER. */
7668 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7670 int depth = SPELLING_DEPTH (); \
7672 if (depth >= spelling_size) \
7674 spelling_size += 10; \
7675 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7676 spelling_size); \
7677 RESTORE_SPELLING_DEPTH (depth); \
7680 spelling->kind = (KIND); \
7681 spelling->MEMBER = (VALUE); \
7682 spelling++; \
7685 /* Push STRING on the stack. Printed literally. */
7687 static void
7688 push_string (const char *string)
7690 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7693 /* Push a member name on the stack. Printed as '.' STRING. */
7695 static void
7696 push_member_name (tree decl)
7698 const char *const string
7699 = (DECL_NAME (decl)
7700 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7701 : _("<anonymous>"));
7702 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7705 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7707 static void
7708 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7710 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7713 /* Compute the maximum size in bytes of the printed spelling. */
7715 static int
7716 spelling_length (void)
7718 int size = 0;
7719 struct spelling *p;
7721 for (p = spelling_base; p < spelling; p++)
7723 if (p->kind == SPELLING_BOUNDS)
7724 size += 25;
7725 else
7726 size += strlen (p->u.s) + 1;
7729 return size;
7732 /* Print the spelling to BUFFER and return it. */
7734 static char *
7735 print_spelling (char *buffer)
7737 char *d = buffer;
7738 struct spelling *p;
7740 for (p = spelling_base; p < spelling; p++)
7741 if (p->kind == SPELLING_BOUNDS)
7743 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7744 d += strlen (d);
7746 else
7748 const char *s;
7749 if (p->kind == SPELLING_MEMBER)
7750 *d++ = '.';
7751 for (s = p->u.s; (*d = *s++); d++)
7754 *d++ = '\0';
7755 return buffer;
7758 /* Digest the parser output INIT as an initializer for type TYPE.
7759 Return a C expression of type TYPE to represent the initial value.
7761 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7763 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7765 If INIT is a string constant, STRICT_STRING is true if it is
7766 unparenthesized or we should not warn here for it being parenthesized.
7767 For other types of INIT, STRICT_STRING is not used.
7769 INIT_LOC is the location of the INIT.
7771 REQUIRE_CONSTANT requests an error if non-constant initializers or
7772 elements are seen. */
7774 static tree
7775 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7776 bool null_pointer_constant, bool strict_string,
7777 int require_constant)
7779 enum tree_code code = TREE_CODE (type);
7780 tree inside_init = init;
7781 tree semantic_type = NULL_TREE;
7782 bool maybe_const = true;
7784 if (type == error_mark_node
7785 || !init
7786 || error_operand_p (init))
7787 return error_mark_node;
7789 STRIP_TYPE_NOPS (inside_init);
7791 if (!c_in_omp_for)
7793 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7795 semantic_type = TREE_TYPE (inside_init);
7796 inside_init = TREE_OPERAND (inside_init, 0);
7798 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7801 /* Initialization of an array of chars from a string constant
7802 optionally enclosed in braces. */
7804 if (code == ARRAY_TYPE && inside_init
7805 && TREE_CODE (inside_init) == STRING_CST)
7807 tree typ1
7808 = (TYPE_ATOMIC (TREE_TYPE (type))
7809 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7810 TYPE_QUAL_ATOMIC)
7811 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7812 /* Note that an array could be both an array of character type
7813 and an array of wchar_t if wchar_t is signed char or unsigned
7814 char. */
7815 bool char_array = (typ1 == char_type_node
7816 || typ1 == signed_char_type_node
7817 || typ1 == unsigned_char_type_node);
7818 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7819 bool char16_array = !!comptypes (typ1, char16_type_node);
7820 bool char32_array = !!comptypes (typ1, char32_type_node);
7822 if (char_array || wchar_array || char16_array || char32_array)
7824 struct c_expr expr;
7825 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7826 bool incompat_string_cst = false;
7827 expr.value = inside_init;
7828 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7829 expr.original_type = NULL;
7830 maybe_warn_string_init (init_loc, type, expr);
7832 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7833 pedwarn_init (init_loc, OPT_Wpedantic,
7834 "initialization of a flexible array member");
7836 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7837 TYPE_MAIN_VARIANT (type)))
7838 return inside_init;
7840 if (char_array)
7842 if (typ2 != char_type_node)
7843 incompat_string_cst = true;
7845 else if (!comptypes (typ1, typ2))
7846 incompat_string_cst = true;
7848 if (incompat_string_cst)
7850 error_init (init_loc, "cannot initialize array of %qT from "
7851 "a string literal with type array of %qT",
7852 typ1, typ2);
7853 return error_mark_node;
7856 if (TYPE_DOMAIN (type) != NULL_TREE
7857 && TYPE_SIZE (type) != NULL_TREE
7858 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7860 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7861 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
7863 /* Subtract the size of a single (possibly wide) character
7864 because it's ok to ignore the terminating null char
7865 that is counted in the length of the constant. */
7866 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
7867 pedwarn_init (init_loc, 0,
7868 ("initializer-string for array of %qT "
7869 "is too long"), typ1);
7870 else if (warn_cxx_compat
7871 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7872 warning_at (init_loc, OPT_Wc___compat,
7873 ("initializer-string for array of %qT "
7874 "is too long for C++"), typ1);
7875 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7877 unsigned HOST_WIDE_INT size
7878 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7879 const char *p = TREE_STRING_POINTER (inside_init);
7881 inside_init = build_string (size, p);
7885 TREE_TYPE (inside_init) = type;
7886 return inside_init;
7888 else if (INTEGRAL_TYPE_P (typ1))
7890 error_init (init_loc, "array of inappropriate type initialized "
7891 "from string constant");
7892 return error_mark_node;
7896 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7897 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7898 below and handle as a constructor. */
7899 if (code == VECTOR_TYPE
7900 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7901 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7902 && TREE_CONSTANT (inside_init))
7904 if (TREE_CODE (inside_init) == VECTOR_CST
7905 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7906 TYPE_MAIN_VARIANT (type)))
7907 return inside_init;
7909 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7911 unsigned HOST_WIDE_INT ix;
7912 tree value;
7913 bool constant_p = true;
7915 /* Iterate through elements and check if all constructor
7916 elements are *_CSTs. */
7917 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7918 if (!CONSTANT_CLASS_P (value))
7920 constant_p = false;
7921 break;
7924 if (constant_p)
7925 return build_vector_from_ctor (type,
7926 CONSTRUCTOR_ELTS (inside_init));
7930 if (warn_sequence_point)
7931 verify_sequence_points (inside_init);
7933 /* Any type can be initialized
7934 from an expression of the same type, optionally with braces. */
7936 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7937 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7938 TYPE_MAIN_VARIANT (type))
7939 || (code == ARRAY_TYPE
7940 && comptypes (TREE_TYPE (inside_init), type))
7941 || (gnu_vector_type_p (type)
7942 && comptypes (TREE_TYPE (inside_init), type))
7943 || (code == POINTER_TYPE
7944 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7945 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7946 TREE_TYPE (type)))))
7948 if (code == POINTER_TYPE)
7950 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7952 if (TREE_CODE (inside_init) == STRING_CST
7953 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7954 inside_init = array_to_pointer_conversion
7955 (init_loc, inside_init);
7956 else
7958 error_init (init_loc, "invalid use of non-lvalue array");
7959 return error_mark_node;
7964 if (code == VECTOR_TYPE)
7965 /* Although the types are compatible, we may require a
7966 conversion. */
7967 inside_init = convert (type, inside_init);
7969 if (require_constant
7970 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7972 /* As an extension, allow initializing objects with static storage
7973 duration with compound literals (which are then treated just as
7974 the brace enclosed list they contain). Also allow this for
7975 vectors, as we can only assign them with compound literals. */
7976 if (flag_isoc99 && code != VECTOR_TYPE)
7977 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7978 "is not constant");
7979 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7980 inside_init = DECL_INITIAL (decl);
7983 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7984 && TREE_CODE (inside_init) != CONSTRUCTOR)
7986 error_init (init_loc, "array initialized from non-constant array "
7987 "expression");
7988 return error_mark_node;
7991 /* Compound expressions can only occur here if -Wpedantic or
7992 -pedantic-errors is specified. In the later case, we always want
7993 an error. In the former case, we simply want a warning. */
7994 if (require_constant && pedantic
7995 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7997 inside_init
7998 = valid_compound_expr_initializer (inside_init,
7999 TREE_TYPE (inside_init));
8000 if (inside_init == error_mark_node)
8001 error_init (init_loc, "initializer element is not constant");
8002 else
8003 pedwarn_init (init_loc, OPT_Wpedantic,
8004 "initializer element is not constant");
8005 if (flag_pedantic_errors)
8006 inside_init = error_mark_node;
8008 else if (require_constant
8009 && !initializer_constant_valid_p (inside_init,
8010 TREE_TYPE (inside_init)))
8012 error_init (init_loc, "initializer element is not constant");
8013 inside_init = error_mark_node;
8015 else if (require_constant && !maybe_const)
8016 pedwarn_init (init_loc, OPT_Wpedantic,
8017 "initializer element is not a constant expression");
8019 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8020 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8021 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8022 type, inside_init, origtype,
8023 ic_init, null_pointer_constant,
8024 NULL_TREE, NULL_TREE, 0);
8025 return inside_init;
8028 /* Handle scalar types, including conversions. */
8030 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8031 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8032 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8034 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8035 && (TREE_CODE (init) == STRING_CST
8036 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8037 inside_init = init = array_to_pointer_conversion (init_loc, init);
8038 if (semantic_type)
8039 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8040 inside_init);
8041 inside_init
8042 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8043 inside_init, origtype, ic_init,
8044 null_pointer_constant, NULL_TREE, NULL_TREE,
8047 /* Check to see if we have already given an error message. */
8048 if (inside_init == error_mark_node)
8050 else if (require_constant && !TREE_CONSTANT (inside_init))
8052 error_init (init_loc, "initializer element is not constant");
8053 inside_init = error_mark_node;
8055 else if (require_constant
8056 && !initializer_constant_valid_p (inside_init,
8057 TREE_TYPE (inside_init)))
8059 error_init (init_loc, "initializer element is not computable at "
8060 "load time");
8061 inside_init = error_mark_node;
8063 else if (require_constant && !maybe_const)
8064 pedwarn_init (init_loc, OPT_Wpedantic,
8065 "initializer element is not a constant expression");
8067 return inside_init;
8070 /* Come here only for records and arrays. */
8072 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8074 error_init (init_loc, "variable-sized object may not be initialized");
8075 return error_mark_node;
8078 error_init (init_loc, "invalid initializer");
8079 return error_mark_node;
8082 /* Handle initializers that use braces. */
8084 /* Type of object we are accumulating a constructor for.
8085 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8086 static tree constructor_type;
8088 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8089 left to fill. */
8090 static tree constructor_fields;
8092 /* For an ARRAY_TYPE, this is the specified index
8093 at which to store the next element we get. */
8094 static tree constructor_index;
8096 /* For an ARRAY_TYPE, this is the maximum index. */
8097 static tree constructor_max_index;
8099 /* For a RECORD_TYPE, this is the first field not yet written out. */
8100 static tree constructor_unfilled_fields;
8102 /* For an ARRAY_TYPE, this is the index of the first element
8103 not yet written out. */
8104 static tree constructor_unfilled_index;
8106 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8107 This is so we can generate gaps between fields, when appropriate. */
8108 static tree constructor_bit_index;
8110 /* If we are saving up the elements rather than allocating them,
8111 this is the list of elements so far (in reverse order,
8112 most recent first). */
8113 static vec<constructor_elt, va_gc> *constructor_elements;
8115 /* 1 if constructor should be incrementally stored into a constructor chain,
8116 0 if all the elements should be kept in AVL tree. */
8117 static int constructor_incremental;
8119 /* 1 if so far this constructor's elements are all compile-time constants. */
8120 static int constructor_constant;
8122 /* 1 if so far this constructor's elements are all valid address constants. */
8123 static int constructor_simple;
8125 /* 1 if this constructor has an element that cannot be part of a
8126 constant expression. */
8127 static int constructor_nonconst;
8129 /* 1 if this constructor is erroneous so far. */
8130 static int constructor_erroneous;
8132 /* 1 if this constructor is the universal zero initializer { 0 }. */
8133 static int constructor_zeroinit;
8135 /* Structure for managing pending initializer elements, organized as an
8136 AVL tree. */
8138 struct init_node
8140 struct init_node *left, *right;
8141 struct init_node *parent;
8142 int balance;
8143 tree purpose;
8144 tree value;
8145 tree origtype;
8148 /* Tree of pending elements at this constructor level.
8149 These are elements encountered out of order
8150 which belong at places we haven't reached yet in actually
8151 writing the output.
8152 Will never hold tree nodes across GC runs. */
8153 static struct init_node *constructor_pending_elts;
8155 /* The SPELLING_DEPTH of this constructor. */
8156 static int constructor_depth;
8158 /* DECL node for which an initializer is being read.
8159 0 means we are reading a constructor expression
8160 such as (struct foo) {...}. */
8161 static tree constructor_decl;
8163 /* Nonzero if this is an initializer for a top-level decl. */
8164 static int constructor_top_level;
8166 /* Nonzero if there were any member designators in this initializer. */
8167 static int constructor_designated;
8169 /* Nesting depth of designator list. */
8170 static int designator_depth;
8172 /* Nonzero if there were diagnosed errors in this designator list. */
8173 static int designator_erroneous;
8176 /* This stack has a level for each implicit or explicit level of
8177 structuring in the initializer, including the outermost one. It
8178 saves the values of most of the variables above. */
8180 struct constructor_range_stack;
8182 struct constructor_stack
8184 struct constructor_stack *next;
8185 tree type;
8186 tree fields;
8187 tree index;
8188 tree max_index;
8189 tree unfilled_index;
8190 tree unfilled_fields;
8191 tree bit_index;
8192 vec<constructor_elt, va_gc> *elements;
8193 struct init_node *pending_elts;
8194 int offset;
8195 int depth;
8196 /* If value nonzero, this value should replace the entire
8197 constructor at this level. */
8198 struct c_expr replacement_value;
8199 struct constructor_range_stack *range_stack;
8200 char constant;
8201 char simple;
8202 char nonconst;
8203 char implicit;
8204 char erroneous;
8205 char outer;
8206 char incremental;
8207 char designated;
8208 int designator_depth;
8211 static struct constructor_stack *constructor_stack;
8213 /* This stack represents designators from some range designator up to
8214 the last designator in the list. */
8216 struct constructor_range_stack
8218 struct constructor_range_stack *next, *prev;
8219 struct constructor_stack *stack;
8220 tree range_start;
8221 tree index;
8222 tree range_end;
8223 tree fields;
8226 static struct constructor_range_stack *constructor_range_stack;
8228 /* This stack records separate initializers that are nested.
8229 Nested initializers can't happen in ANSI C, but GNU C allows them
8230 in cases like { ... (struct foo) { ... } ... }. */
8232 struct initializer_stack
8234 struct initializer_stack *next;
8235 tree decl;
8236 struct constructor_stack *constructor_stack;
8237 struct constructor_range_stack *constructor_range_stack;
8238 vec<constructor_elt, va_gc> *elements;
8239 struct spelling *spelling;
8240 struct spelling *spelling_base;
8241 int spelling_size;
8242 char top_level;
8243 char require_constant_value;
8244 char require_constant_elements;
8245 rich_location *missing_brace_richloc;
8248 static struct initializer_stack *initializer_stack;
8250 /* Prepare to parse and output the initializer for variable DECL. */
8252 void
8253 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8254 rich_location *richloc)
8256 const char *locus;
8257 struct initializer_stack *p = XNEW (struct initializer_stack);
8259 p->decl = constructor_decl;
8260 p->require_constant_value = require_constant_value;
8261 p->require_constant_elements = require_constant_elements;
8262 p->constructor_stack = constructor_stack;
8263 p->constructor_range_stack = constructor_range_stack;
8264 p->elements = constructor_elements;
8265 p->spelling = spelling;
8266 p->spelling_base = spelling_base;
8267 p->spelling_size = spelling_size;
8268 p->top_level = constructor_top_level;
8269 p->next = initializer_stack;
8270 p->missing_brace_richloc = richloc;
8271 initializer_stack = p;
8273 constructor_decl = decl;
8274 constructor_designated = 0;
8275 constructor_top_level = top_level;
8277 if (decl != NULL_TREE && decl != error_mark_node)
8279 require_constant_value = TREE_STATIC (decl);
8280 require_constant_elements
8281 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8282 /* For a scalar, you can always use any value to initialize,
8283 even within braces. */
8284 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8285 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8287 else
8289 require_constant_value = 0;
8290 require_constant_elements = 0;
8291 locus = _("(anonymous)");
8294 constructor_stack = 0;
8295 constructor_range_stack = 0;
8297 found_missing_braces = 0;
8299 spelling_base = 0;
8300 spelling_size = 0;
8301 RESTORE_SPELLING_DEPTH (0);
8303 if (locus)
8304 push_string (locus);
8307 void
8308 finish_init (void)
8310 struct initializer_stack *p = initializer_stack;
8312 /* Free the whole constructor stack of this initializer. */
8313 while (constructor_stack)
8315 struct constructor_stack *q = constructor_stack;
8316 constructor_stack = q->next;
8317 free (q);
8320 gcc_assert (!constructor_range_stack);
8322 /* Pop back to the data of the outer initializer (if any). */
8323 free (spelling_base);
8325 constructor_decl = p->decl;
8326 require_constant_value = p->require_constant_value;
8327 require_constant_elements = p->require_constant_elements;
8328 constructor_stack = p->constructor_stack;
8329 constructor_range_stack = p->constructor_range_stack;
8330 constructor_elements = p->elements;
8331 spelling = p->spelling;
8332 spelling_base = p->spelling_base;
8333 spelling_size = p->spelling_size;
8334 constructor_top_level = p->top_level;
8335 initializer_stack = p->next;
8336 free (p);
8339 /* Call here when we see the initializer is surrounded by braces.
8340 This is instead of a call to push_init_level;
8341 it is matched by a call to pop_init_level.
8343 TYPE is the type to initialize, for a constructor expression.
8344 For an initializer for a decl, TYPE is zero. */
8346 void
8347 really_start_incremental_init (tree type)
8349 struct constructor_stack *p = XNEW (struct constructor_stack);
8351 if (type == NULL_TREE)
8352 type = TREE_TYPE (constructor_decl);
8354 if (VECTOR_TYPE_P (type)
8355 && TYPE_VECTOR_OPAQUE (type))
8356 error ("opaque vector types cannot be initialized");
8358 p->type = constructor_type;
8359 p->fields = constructor_fields;
8360 p->index = constructor_index;
8361 p->max_index = constructor_max_index;
8362 p->unfilled_index = constructor_unfilled_index;
8363 p->unfilled_fields = constructor_unfilled_fields;
8364 p->bit_index = constructor_bit_index;
8365 p->elements = constructor_elements;
8366 p->constant = constructor_constant;
8367 p->simple = constructor_simple;
8368 p->nonconst = constructor_nonconst;
8369 p->erroneous = constructor_erroneous;
8370 p->pending_elts = constructor_pending_elts;
8371 p->depth = constructor_depth;
8372 p->replacement_value.value = 0;
8373 p->replacement_value.original_code = ERROR_MARK;
8374 p->replacement_value.original_type = NULL;
8375 p->implicit = 0;
8376 p->range_stack = 0;
8377 p->outer = 0;
8378 p->incremental = constructor_incremental;
8379 p->designated = constructor_designated;
8380 p->designator_depth = designator_depth;
8381 p->next = 0;
8382 constructor_stack = p;
8384 constructor_constant = 1;
8385 constructor_simple = 1;
8386 constructor_nonconst = 0;
8387 constructor_depth = SPELLING_DEPTH ();
8388 constructor_elements = NULL;
8389 constructor_pending_elts = 0;
8390 constructor_type = type;
8391 constructor_incremental = 1;
8392 constructor_designated = 0;
8393 constructor_zeroinit = 1;
8394 designator_depth = 0;
8395 designator_erroneous = 0;
8397 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8399 constructor_fields = TYPE_FIELDS (constructor_type);
8400 /* Skip any nameless bit fields at the beginning. */
8401 while (constructor_fields != NULL_TREE
8402 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8403 constructor_fields = DECL_CHAIN (constructor_fields);
8405 constructor_unfilled_fields = constructor_fields;
8406 constructor_bit_index = bitsize_zero_node;
8408 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8410 if (TYPE_DOMAIN (constructor_type))
8412 constructor_max_index
8413 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8415 /* Detect non-empty initializations of zero-length arrays. */
8416 if (constructor_max_index == NULL_TREE
8417 && TYPE_SIZE (constructor_type))
8418 constructor_max_index = integer_minus_one_node;
8420 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8421 to initialize VLAs will cause a proper error; avoid tree
8422 checking errors as well by setting a safe value. */
8423 if (constructor_max_index
8424 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8425 constructor_max_index = integer_minus_one_node;
8427 constructor_index
8428 = convert (bitsizetype,
8429 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8431 else
8433 constructor_index = bitsize_zero_node;
8434 constructor_max_index = NULL_TREE;
8437 constructor_unfilled_index = constructor_index;
8439 else if (gnu_vector_type_p (constructor_type))
8441 /* Vectors are like simple fixed-size arrays. */
8442 constructor_max_index =
8443 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8444 constructor_index = bitsize_zero_node;
8445 constructor_unfilled_index = constructor_index;
8447 else
8449 /* Handle the case of int x = {5}; */
8450 constructor_fields = constructor_type;
8451 constructor_unfilled_fields = constructor_type;
8455 extern location_t last_init_list_comma;
8457 /* Called when we see an open brace for a nested initializer. Finish
8458 off any pending levels with implicit braces. */
8459 void
8460 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8462 while (constructor_stack->implicit)
8464 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8465 && constructor_fields == NULL_TREE)
8466 process_init_element (input_location,
8467 pop_init_level (loc, 1, braced_init_obstack,
8468 last_init_list_comma),
8469 true, braced_init_obstack);
8470 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8471 && constructor_max_index
8472 && tree_int_cst_lt (constructor_max_index,
8473 constructor_index))
8474 process_init_element (input_location,
8475 pop_init_level (loc, 1, braced_init_obstack,
8476 last_init_list_comma),
8477 true, braced_init_obstack);
8478 else
8479 break;
8483 /* Push down into a subobject, for initialization.
8484 If this is for an explicit set of braces, IMPLICIT is 0.
8485 If it is because the next element belongs at a lower level,
8486 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8488 void
8489 push_init_level (location_t loc, int implicit,
8490 struct obstack *braced_init_obstack)
8492 struct constructor_stack *p;
8493 tree value = NULL_TREE;
8495 /* Unless this is an explicit brace, we need to preserve previous
8496 content if any. */
8497 if (implicit)
8499 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8500 value = find_init_member (constructor_fields, braced_init_obstack);
8501 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8502 value = find_init_member (constructor_index, braced_init_obstack);
8505 p = XNEW (struct constructor_stack);
8506 p->type = constructor_type;
8507 p->fields = constructor_fields;
8508 p->index = constructor_index;
8509 p->max_index = constructor_max_index;
8510 p->unfilled_index = constructor_unfilled_index;
8511 p->unfilled_fields = constructor_unfilled_fields;
8512 p->bit_index = constructor_bit_index;
8513 p->elements = constructor_elements;
8514 p->constant = constructor_constant;
8515 p->simple = constructor_simple;
8516 p->nonconst = constructor_nonconst;
8517 p->erroneous = constructor_erroneous;
8518 p->pending_elts = constructor_pending_elts;
8519 p->depth = constructor_depth;
8520 p->replacement_value.value = NULL_TREE;
8521 p->replacement_value.original_code = ERROR_MARK;
8522 p->replacement_value.original_type = NULL;
8523 p->implicit = implicit;
8524 p->outer = 0;
8525 p->incremental = constructor_incremental;
8526 p->designated = constructor_designated;
8527 p->designator_depth = designator_depth;
8528 p->next = constructor_stack;
8529 p->range_stack = 0;
8530 constructor_stack = p;
8532 constructor_constant = 1;
8533 constructor_simple = 1;
8534 constructor_nonconst = 0;
8535 constructor_depth = SPELLING_DEPTH ();
8536 constructor_elements = NULL;
8537 constructor_incremental = 1;
8538 constructor_designated = 0;
8539 constructor_pending_elts = 0;
8540 if (!implicit)
8542 p->range_stack = constructor_range_stack;
8543 constructor_range_stack = 0;
8544 designator_depth = 0;
8545 designator_erroneous = 0;
8548 /* Don't die if an entire brace-pair level is superfluous
8549 in the containing level. */
8550 if (constructor_type == NULL_TREE)
8552 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8554 /* Don't die if there are extra init elts at the end. */
8555 if (constructor_fields == NULL_TREE)
8556 constructor_type = NULL_TREE;
8557 else
8559 constructor_type = TREE_TYPE (constructor_fields);
8560 push_member_name (constructor_fields);
8561 constructor_depth++;
8563 /* If upper initializer is designated, then mark this as
8564 designated too to prevent bogus warnings. */
8565 constructor_designated = p->designated;
8567 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8569 constructor_type = TREE_TYPE (constructor_type);
8570 push_array_bounds (tree_to_uhwi (constructor_index));
8571 constructor_depth++;
8574 if (constructor_type == NULL_TREE)
8576 error_init (loc, "extra brace group at end of initializer");
8577 constructor_fields = NULL_TREE;
8578 constructor_unfilled_fields = NULL_TREE;
8579 return;
8582 if (value && TREE_CODE (value) == CONSTRUCTOR)
8584 constructor_constant = TREE_CONSTANT (value);
8585 constructor_simple = TREE_STATIC (value);
8586 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8587 constructor_elements = CONSTRUCTOR_ELTS (value);
8588 if (!vec_safe_is_empty (constructor_elements)
8589 && (TREE_CODE (constructor_type) == RECORD_TYPE
8590 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8591 set_nonincremental_init (braced_init_obstack);
8594 if (implicit == 1)
8596 found_missing_braces = 1;
8597 if (initializer_stack->missing_brace_richloc)
8598 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8599 (loc, "{");
8602 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8604 constructor_fields = TYPE_FIELDS (constructor_type);
8605 /* Skip any nameless bit fields at the beginning. */
8606 while (constructor_fields != NULL_TREE
8607 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8608 constructor_fields = DECL_CHAIN (constructor_fields);
8610 constructor_unfilled_fields = constructor_fields;
8611 constructor_bit_index = bitsize_zero_node;
8613 else if (gnu_vector_type_p (constructor_type))
8615 /* Vectors are like simple fixed-size arrays. */
8616 constructor_max_index =
8617 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8618 constructor_index = bitsize_int (0);
8619 constructor_unfilled_index = constructor_index;
8621 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8623 if (TYPE_DOMAIN (constructor_type))
8625 constructor_max_index
8626 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8628 /* Detect non-empty initializations of zero-length arrays. */
8629 if (constructor_max_index == NULL_TREE
8630 && TYPE_SIZE (constructor_type))
8631 constructor_max_index = integer_minus_one_node;
8633 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8634 to initialize VLAs will cause a proper error; avoid tree
8635 checking errors as well by setting a safe value. */
8636 if (constructor_max_index
8637 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8638 constructor_max_index = integer_minus_one_node;
8640 constructor_index
8641 = convert (bitsizetype,
8642 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8644 else
8645 constructor_index = bitsize_zero_node;
8647 constructor_unfilled_index = constructor_index;
8648 if (value && TREE_CODE (value) == STRING_CST)
8650 /* We need to split the char/wchar array into individual
8651 characters, so that we don't have to special case it
8652 everywhere. */
8653 set_nonincremental_init_from_string (value, braced_init_obstack);
8656 else
8658 if (constructor_type != error_mark_node)
8659 warning_init (input_location, 0, "braces around scalar initializer");
8660 constructor_fields = constructor_type;
8661 constructor_unfilled_fields = constructor_type;
8665 /* At the end of an implicit or explicit brace level,
8666 finish up that level of constructor. If a single expression
8667 with redundant braces initialized that level, return the
8668 c_expr structure for that expression. Otherwise, the original_code
8669 element is set to ERROR_MARK.
8670 If we were outputting the elements as they are read, return 0 as the value
8671 from inner levels (process_init_element ignores that),
8672 but return error_mark_node as the value from the outermost level
8673 (that's what we want to put in DECL_INITIAL).
8674 Otherwise, return a CONSTRUCTOR expression as the value. */
8676 struct c_expr
8677 pop_init_level (location_t loc, int implicit,
8678 struct obstack *braced_init_obstack,
8679 location_t insert_before)
8681 struct constructor_stack *p;
8682 struct c_expr ret;
8683 ret.value = NULL_TREE;
8684 ret.original_code = ERROR_MARK;
8685 ret.original_type = NULL;
8687 if (implicit == 0)
8689 /* When we come to an explicit close brace,
8690 pop any inner levels that didn't have explicit braces. */
8691 while (constructor_stack->implicit)
8692 process_init_element (input_location,
8693 pop_init_level (loc, 1, braced_init_obstack,
8694 insert_before),
8695 true, braced_init_obstack);
8696 gcc_assert (!constructor_range_stack);
8698 else
8699 if (initializer_stack->missing_brace_richloc)
8700 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8701 (insert_before, "}");
8703 /* Now output all pending elements. */
8704 constructor_incremental = 1;
8705 output_pending_init_elements (1, braced_init_obstack);
8707 p = constructor_stack;
8709 /* Error for initializing a flexible array member, or a zero-length
8710 array member in an inappropriate context. */
8711 if (constructor_type && constructor_fields
8712 && TREE_CODE (constructor_type) == ARRAY_TYPE
8713 && TYPE_DOMAIN (constructor_type)
8714 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8716 /* Silently discard empty initializations. The parser will
8717 already have pedwarned for empty brackets. */
8718 if (integer_zerop (constructor_unfilled_index))
8719 constructor_type = NULL_TREE;
8720 else
8722 gcc_assert (!TYPE_SIZE (constructor_type));
8724 if (constructor_depth > 2)
8725 error_init (loc, "initialization of flexible array member in a nested context");
8726 else
8727 pedwarn_init (loc, OPT_Wpedantic,
8728 "initialization of a flexible array member");
8730 /* We have already issued an error message for the existence
8731 of a flexible array member not at the end of the structure.
8732 Discard the initializer so that we do not die later. */
8733 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8734 constructor_type = NULL_TREE;
8738 switch (vec_safe_length (constructor_elements))
8740 case 0:
8741 /* Initialization with { } counts as zeroinit. */
8742 constructor_zeroinit = 1;
8743 break;
8744 case 1:
8745 /* This might be zeroinit as well. */
8746 if (integer_zerop ((*constructor_elements)[0].value))
8747 constructor_zeroinit = 1;
8748 break;
8749 default:
8750 /* If the constructor has more than one element, it can't be { 0 }. */
8751 constructor_zeroinit = 0;
8752 break;
8755 /* Warn when some structs are initialized with direct aggregation. */
8756 if (!implicit && found_missing_braces && warn_missing_braces
8757 && !constructor_zeroinit)
8759 gcc_assert (initializer_stack->missing_brace_richloc);
8760 warning_at (initializer_stack->missing_brace_richloc,
8761 OPT_Wmissing_braces,
8762 "missing braces around initializer");
8765 /* Warn when some struct elements are implicitly initialized to zero. */
8766 if (warn_missing_field_initializers
8767 && constructor_type
8768 && TREE_CODE (constructor_type) == RECORD_TYPE
8769 && constructor_unfilled_fields)
8771 /* Do not warn for flexible array members or zero-length arrays. */
8772 while (constructor_unfilled_fields
8773 && (!DECL_SIZE (constructor_unfilled_fields)
8774 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8775 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8777 if (constructor_unfilled_fields
8778 /* Do not warn if this level of the initializer uses member
8779 designators; it is likely to be deliberate. */
8780 && !constructor_designated
8781 /* Do not warn about initializing with { 0 } or with { }. */
8782 && !constructor_zeroinit)
8784 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8785 "missing initializer for field %qD of %qT",
8786 constructor_unfilled_fields,
8787 constructor_type))
8788 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8789 "%qD declared here", constructor_unfilled_fields);
8793 /* Pad out the end of the structure. */
8794 if (p->replacement_value.value)
8795 /* If this closes a superfluous brace pair,
8796 just pass out the element between them. */
8797 ret = p->replacement_value;
8798 else if (constructor_type == NULL_TREE)
8800 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8801 && TREE_CODE (constructor_type) != ARRAY_TYPE
8802 && !gnu_vector_type_p (constructor_type))
8804 /* A nonincremental scalar initializer--just return
8805 the element, after verifying there is just one. */
8806 if (vec_safe_is_empty (constructor_elements))
8808 if (!constructor_erroneous && constructor_type != error_mark_node)
8809 error_init (loc, "empty scalar initializer");
8810 ret.value = error_mark_node;
8812 else if (vec_safe_length (constructor_elements) != 1)
8814 error_init (loc, "extra elements in scalar initializer");
8815 ret.value = (*constructor_elements)[0].value;
8817 else
8818 ret.value = (*constructor_elements)[0].value;
8820 else
8822 if (constructor_erroneous)
8823 ret.value = error_mark_node;
8824 else
8826 ret.value = build_constructor (constructor_type,
8827 constructor_elements);
8828 if (constructor_constant)
8829 TREE_CONSTANT (ret.value) = 1;
8830 if (constructor_constant && constructor_simple)
8831 TREE_STATIC (ret.value) = 1;
8832 if (constructor_nonconst)
8833 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8837 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8839 if (constructor_nonconst)
8840 ret.original_code = C_MAYBE_CONST_EXPR;
8841 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8842 ret.original_code = ERROR_MARK;
8845 constructor_type = p->type;
8846 constructor_fields = p->fields;
8847 constructor_index = p->index;
8848 constructor_max_index = p->max_index;
8849 constructor_unfilled_index = p->unfilled_index;
8850 constructor_unfilled_fields = p->unfilled_fields;
8851 constructor_bit_index = p->bit_index;
8852 constructor_elements = p->elements;
8853 constructor_constant = p->constant;
8854 constructor_simple = p->simple;
8855 constructor_nonconst = p->nonconst;
8856 constructor_erroneous = p->erroneous;
8857 constructor_incremental = p->incremental;
8858 constructor_designated = p->designated;
8859 designator_depth = p->designator_depth;
8860 constructor_pending_elts = p->pending_elts;
8861 constructor_depth = p->depth;
8862 if (!p->implicit)
8863 constructor_range_stack = p->range_stack;
8864 RESTORE_SPELLING_DEPTH (constructor_depth);
8866 constructor_stack = p->next;
8867 free (p);
8869 if (ret.value == NULL_TREE && constructor_stack == 0)
8870 ret.value = error_mark_node;
8871 return ret;
8874 /* Common handling for both array range and field name designators.
8875 ARRAY argument is nonzero for array ranges. Returns false for success. */
8877 static bool
8878 set_designator (location_t loc, bool array,
8879 struct obstack *braced_init_obstack)
8881 tree subtype;
8882 enum tree_code subcode;
8884 /* Don't die if an entire brace-pair level is superfluous
8885 in the containing level, or for an erroneous type. */
8886 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
8887 return true;
8889 /* If there were errors in this designator list already, bail out
8890 silently. */
8891 if (designator_erroneous)
8892 return true;
8894 /* Likewise for an initializer for a variable-size type. Those are
8895 diagnosed in digest_init. */
8896 if (COMPLETE_TYPE_P (constructor_type)
8897 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
8898 return true;
8900 if (!designator_depth)
8902 gcc_assert (!constructor_range_stack);
8904 /* Designator list starts at the level of closest explicit
8905 braces. */
8906 while (constructor_stack->implicit)
8907 process_init_element (input_location,
8908 pop_init_level (loc, 1, braced_init_obstack,
8909 last_init_list_comma),
8910 true, braced_init_obstack);
8911 constructor_designated = 1;
8912 return false;
8915 switch (TREE_CODE (constructor_type))
8917 case RECORD_TYPE:
8918 case UNION_TYPE:
8919 subtype = TREE_TYPE (constructor_fields);
8920 if (subtype != error_mark_node)
8921 subtype = TYPE_MAIN_VARIANT (subtype);
8922 break;
8923 case ARRAY_TYPE:
8924 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8925 break;
8926 default:
8927 gcc_unreachable ();
8930 subcode = TREE_CODE (subtype);
8931 if (array && subcode != ARRAY_TYPE)
8933 error_init (loc, "array index in non-array initializer");
8934 return true;
8936 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8938 error_init (loc, "field name not in record or union initializer");
8939 return true;
8942 constructor_designated = 1;
8943 finish_implicit_inits (loc, braced_init_obstack);
8944 push_init_level (loc, 2, braced_init_obstack);
8945 return false;
8948 /* If there are range designators in designator list, push a new designator
8949 to constructor_range_stack. RANGE_END is end of such stack range or
8950 NULL_TREE if there is no range designator at this level. */
8952 static void
8953 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8955 struct constructor_range_stack *p;
8957 p = (struct constructor_range_stack *)
8958 obstack_alloc (braced_init_obstack,
8959 sizeof (struct constructor_range_stack));
8960 p->prev = constructor_range_stack;
8961 p->next = 0;
8962 p->fields = constructor_fields;
8963 p->range_start = constructor_index;
8964 p->index = constructor_index;
8965 p->stack = constructor_stack;
8966 p->range_end = range_end;
8967 if (constructor_range_stack)
8968 constructor_range_stack->next = p;
8969 constructor_range_stack = p;
8972 /* Within an array initializer, specify the next index to be initialized.
8973 FIRST is that index. If LAST is nonzero, then initialize a range
8974 of indices, running from FIRST through LAST. */
8976 void
8977 set_init_index (location_t loc, tree first, tree last,
8978 struct obstack *braced_init_obstack)
8980 if (set_designator (loc, true, braced_init_obstack))
8981 return;
8983 designator_erroneous = 1;
8985 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8986 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8988 error_init (loc, "array index in initializer not of integer type");
8989 return;
8992 if (TREE_CODE (first) != INTEGER_CST)
8994 first = c_fully_fold (first, false, NULL);
8995 if (TREE_CODE (first) == INTEGER_CST)
8996 pedwarn_init (loc, OPT_Wpedantic,
8997 "array index in initializer is not "
8998 "an integer constant expression");
9001 if (last && TREE_CODE (last) != INTEGER_CST)
9003 last = c_fully_fold (last, false, NULL);
9004 if (TREE_CODE (last) == INTEGER_CST)
9005 pedwarn_init (loc, OPT_Wpedantic,
9006 "array index in initializer is not "
9007 "an integer constant expression");
9010 if (TREE_CODE (first) != INTEGER_CST)
9011 error_init (loc, "nonconstant array index in initializer");
9012 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9013 error_init (loc, "nonconstant array index in initializer");
9014 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9015 error_init (loc, "array index in non-array initializer");
9016 else if (tree_int_cst_sgn (first) == -1)
9017 error_init (loc, "array index in initializer exceeds array bounds");
9018 else if (constructor_max_index
9019 && tree_int_cst_lt (constructor_max_index, first))
9020 error_init (loc, "array index in initializer exceeds array bounds");
9021 else
9023 constant_expression_warning (first);
9024 if (last)
9025 constant_expression_warning (last);
9026 constructor_index = convert (bitsizetype, first);
9027 if (tree_int_cst_lt (constructor_index, first))
9029 constructor_index = copy_node (constructor_index);
9030 TREE_OVERFLOW (constructor_index) = 1;
9033 if (last)
9035 if (tree_int_cst_equal (first, last))
9036 last = NULL_TREE;
9037 else if (tree_int_cst_lt (last, first))
9039 error_init (loc, "empty index range in initializer");
9040 last = NULL_TREE;
9042 else
9044 last = convert (bitsizetype, last);
9045 if (constructor_max_index != NULL_TREE
9046 && tree_int_cst_lt (constructor_max_index, last))
9048 error_init (loc, "array index range in initializer exceeds "
9049 "array bounds");
9050 last = NULL_TREE;
9055 designator_depth++;
9056 designator_erroneous = 0;
9057 if (constructor_range_stack || last)
9058 push_range_stack (last, braced_init_obstack);
9062 /* Within a struct initializer, specify the next field to be initialized. */
9064 void
9065 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9066 struct obstack *braced_init_obstack)
9068 tree field;
9070 if (set_designator (loc, false, braced_init_obstack))
9071 return;
9073 designator_erroneous = 1;
9075 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9077 error_init (loc, "field name not in record or union initializer");
9078 return;
9081 field = lookup_field (constructor_type, fieldname);
9083 if (field == NULL_TREE)
9085 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9086 if (guessed_id)
9088 gcc_rich_location rich_loc (fieldname_loc);
9089 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9090 error_at (&rich_loc,
9091 "%qT has no member named %qE; did you mean %qE?",
9092 constructor_type, fieldname, guessed_id);
9094 else
9095 error_at (fieldname_loc, "%qT has no member named %qE",
9096 constructor_type, fieldname);
9098 else
9101 constructor_fields = TREE_VALUE (field);
9102 designator_depth++;
9103 designator_erroneous = 0;
9104 if (constructor_range_stack)
9105 push_range_stack (NULL_TREE, braced_init_obstack);
9106 field = TREE_CHAIN (field);
9107 if (field)
9109 if (set_designator (loc, false, braced_init_obstack))
9110 return;
9113 while (field != NULL_TREE);
9116 /* Add a new initializer to the tree of pending initializers. PURPOSE
9117 identifies the initializer, either array index or field in a structure.
9118 VALUE is the value of that index or field. If ORIGTYPE is not
9119 NULL_TREE, it is the original type of VALUE.
9121 IMPLICIT is true if value comes from pop_init_level (1),
9122 the new initializer has been merged with the existing one
9123 and thus no warnings should be emitted about overriding an
9124 existing initializer. */
9126 static void
9127 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9128 bool implicit, struct obstack *braced_init_obstack)
9130 struct init_node *p, **q, *r;
9132 q = &constructor_pending_elts;
9133 p = 0;
9135 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9137 while (*q != 0)
9139 p = *q;
9140 if (tree_int_cst_lt (purpose, p->purpose))
9141 q = &p->left;
9142 else if (tree_int_cst_lt (p->purpose, purpose))
9143 q = &p->right;
9144 else
9146 if (!implicit)
9148 if (TREE_SIDE_EFFECTS (p->value))
9149 warning_init (loc, OPT_Woverride_init_side_effects,
9150 "initialized field with side-effects "
9151 "overwritten");
9152 else if (warn_override_init)
9153 warning_init (loc, OPT_Woverride_init,
9154 "initialized field overwritten");
9156 p->value = value;
9157 p->origtype = origtype;
9158 return;
9162 else
9164 tree bitpos;
9166 bitpos = bit_position (purpose);
9167 while (*q != NULL)
9169 p = *q;
9170 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9171 q = &p->left;
9172 else if (p->purpose != purpose)
9173 q = &p->right;
9174 else
9176 if (!implicit)
9178 if (TREE_SIDE_EFFECTS (p->value))
9179 warning_init (loc, OPT_Woverride_init_side_effects,
9180 "initialized field with side-effects "
9181 "overwritten");
9182 else if (warn_override_init)
9183 warning_init (loc, OPT_Woverride_init,
9184 "initialized field overwritten");
9186 p->value = value;
9187 p->origtype = origtype;
9188 return;
9193 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9194 sizeof (struct init_node));
9195 r->purpose = purpose;
9196 r->value = value;
9197 r->origtype = origtype;
9199 *q = r;
9200 r->parent = p;
9201 r->left = 0;
9202 r->right = 0;
9203 r->balance = 0;
9205 while (p)
9207 struct init_node *s;
9209 if (r == p->left)
9211 if (p->balance == 0)
9212 p->balance = -1;
9213 else if (p->balance < 0)
9215 if (r->balance < 0)
9217 /* L rotation. */
9218 p->left = r->right;
9219 if (p->left)
9220 p->left->parent = p;
9221 r->right = p;
9223 p->balance = 0;
9224 r->balance = 0;
9226 s = p->parent;
9227 p->parent = r;
9228 r->parent = s;
9229 if (s)
9231 if (s->left == p)
9232 s->left = r;
9233 else
9234 s->right = r;
9236 else
9237 constructor_pending_elts = r;
9239 else
9241 /* LR rotation. */
9242 struct init_node *t = r->right;
9244 r->right = t->left;
9245 if (r->right)
9246 r->right->parent = r;
9247 t->left = r;
9249 p->left = t->right;
9250 if (p->left)
9251 p->left->parent = p;
9252 t->right = p;
9254 p->balance = t->balance < 0;
9255 r->balance = -(t->balance > 0);
9256 t->balance = 0;
9258 s = p->parent;
9259 p->parent = t;
9260 r->parent = t;
9261 t->parent = s;
9262 if (s)
9264 if (s->left == p)
9265 s->left = t;
9266 else
9267 s->right = t;
9269 else
9270 constructor_pending_elts = t;
9272 break;
9274 else
9276 /* p->balance == +1; growth of left side balances the node. */
9277 p->balance = 0;
9278 break;
9281 else /* r == p->right */
9283 if (p->balance == 0)
9284 /* Growth propagation from right side. */
9285 p->balance++;
9286 else if (p->balance > 0)
9288 if (r->balance > 0)
9290 /* R rotation. */
9291 p->right = r->left;
9292 if (p->right)
9293 p->right->parent = p;
9294 r->left = p;
9296 p->balance = 0;
9297 r->balance = 0;
9299 s = p->parent;
9300 p->parent = r;
9301 r->parent = s;
9302 if (s)
9304 if (s->left == p)
9305 s->left = r;
9306 else
9307 s->right = r;
9309 else
9310 constructor_pending_elts = r;
9312 else /* r->balance == -1 */
9314 /* RL rotation */
9315 struct init_node *t = r->left;
9317 r->left = t->right;
9318 if (r->left)
9319 r->left->parent = r;
9320 t->right = r;
9322 p->right = t->left;
9323 if (p->right)
9324 p->right->parent = p;
9325 t->left = p;
9327 r->balance = (t->balance < 0);
9328 p->balance = -(t->balance > 0);
9329 t->balance = 0;
9331 s = p->parent;
9332 p->parent = t;
9333 r->parent = t;
9334 t->parent = s;
9335 if (s)
9337 if (s->left == p)
9338 s->left = t;
9339 else
9340 s->right = t;
9342 else
9343 constructor_pending_elts = t;
9345 break;
9347 else
9349 /* p->balance == -1; growth of right side balances the node. */
9350 p->balance = 0;
9351 break;
9355 r = p;
9356 p = p->parent;
9360 /* Build AVL tree from a sorted chain. */
9362 static void
9363 set_nonincremental_init (struct obstack * braced_init_obstack)
9365 unsigned HOST_WIDE_INT ix;
9366 tree index, value;
9368 if (TREE_CODE (constructor_type) != RECORD_TYPE
9369 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9370 return;
9372 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9373 add_pending_init (input_location, index, value, NULL_TREE, true,
9374 braced_init_obstack);
9375 constructor_elements = NULL;
9376 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9378 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9379 /* Skip any nameless bit fields at the beginning. */
9380 while (constructor_unfilled_fields != NULL_TREE
9381 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9382 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9385 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9387 if (TYPE_DOMAIN (constructor_type))
9388 constructor_unfilled_index
9389 = convert (bitsizetype,
9390 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9391 else
9392 constructor_unfilled_index = bitsize_zero_node;
9394 constructor_incremental = 0;
9397 /* Build AVL tree from a string constant. */
9399 static void
9400 set_nonincremental_init_from_string (tree str,
9401 struct obstack * braced_init_obstack)
9403 tree value, purpose, type;
9404 HOST_WIDE_INT val[2];
9405 const char *p, *end;
9406 int byte, wchar_bytes, charwidth, bitpos;
9408 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9410 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9411 charwidth = TYPE_PRECISION (char_type_node);
9412 gcc_assert ((size_t) wchar_bytes * charwidth
9413 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9414 type = TREE_TYPE (constructor_type);
9415 p = TREE_STRING_POINTER (str);
9416 end = p + TREE_STRING_LENGTH (str);
9418 for (purpose = bitsize_zero_node;
9419 p < end
9420 && !(constructor_max_index
9421 && tree_int_cst_lt (constructor_max_index, purpose));
9422 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9424 if (wchar_bytes == 1)
9426 val[0] = (unsigned char) *p++;
9427 val[1] = 0;
9429 else
9431 val[1] = 0;
9432 val[0] = 0;
9433 for (byte = 0; byte < wchar_bytes; byte++)
9435 if (BYTES_BIG_ENDIAN)
9436 bitpos = (wchar_bytes - byte - 1) * charwidth;
9437 else
9438 bitpos = byte * charwidth;
9439 val[bitpos / HOST_BITS_PER_WIDE_INT]
9440 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9441 << (bitpos % HOST_BITS_PER_WIDE_INT);
9445 if (!TYPE_UNSIGNED (type))
9447 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9448 if (bitpos < HOST_BITS_PER_WIDE_INT)
9450 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9452 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9453 val[1] = -1;
9456 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9458 if (val[0] < 0)
9459 val[1] = -1;
9461 else if (val[1] & (HOST_WIDE_INT_1
9462 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9463 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9466 value = wide_int_to_tree (type,
9467 wide_int::from_array (val, 2,
9468 HOST_BITS_PER_WIDE_INT * 2));
9469 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9470 braced_init_obstack);
9473 constructor_incremental = 0;
9476 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9477 not initialized yet. */
9479 static tree
9480 find_init_member (tree field, struct obstack * braced_init_obstack)
9482 struct init_node *p;
9484 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9486 if (constructor_incremental
9487 && tree_int_cst_lt (field, constructor_unfilled_index))
9488 set_nonincremental_init (braced_init_obstack);
9490 p = constructor_pending_elts;
9491 while (p)
9493 if (tree_int_cst_lt (field, p->purpose))
9494 p = p->left;
9495 else if (tree_int_cst_lt (p->purpose, field))
9496 p = p->right;
9497 else
9498 return p->value;
9501 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9503 tree bitpos = bit_position (field);
9505 if (constructor_incremental
9506 && (!constructor_unfilled_fields
9507 || tree_int_cst_lt (bitpos,
9508 bit_position (constructor_unfilled_fields))))
9509 set_nonincremental_init (braced_init_obstack);
9511 p = constructor_pending_elts;
9512 while (p)
9514 if (field == p->purpose)
9515 return p->value;
9516 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9517 p = p->left;
9518 else
9519 p = p->right;
9522 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9524 if (!vec_safe_is_empty (constructor_elements)
9525 && (constructor_elements->last ().index == field))
9526 return constructor_elements->last ().value;
9528 return NULL_TREE;
9531 /* "Output" the next constructor element.
9532 At top level, really output it to assembler code now.
9533 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9534 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9535 TYPE is the data type that the containing data type wants here.
9536 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9537 If VALUE is a string constant, STRICT_STRING is true if it is
9538 unparenthesized or we should not warn here for it being parenthesized.
9539 For other types of VALUE, STRICT_STRING is not used.
9541 PENDING if true means output pending elements that belong
9542 right after this element. (PENDING is normally true;
9543 it is false while outputting pending elements, to avoid recursion.)
9545 IMPLICIT is true if value comes from pop_init_level (1),
9546 the new initializer has been merged with the existing one
9547 and thus no warnings should be emitted about overriding an
9548 existing initializer. */
9550 static void
9551 output_init_element (location_t loc, tree value, tree origtype,
9552 bool strict_string, tree type, tree field, bool pending,
9553 bool implicit, struct obstack * braced_init_obstack)
9555 tree semantic_type = NULL_TREE;
9556 bool maybe_const = true;
9557 bool npc;
9559 if (type == error_mark_node || value == error_mark_node)
9561 constructor_erroneous = 1;
9562 return;
9564 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9565 && (TREE_CODE (value) == STRING_CST
9566 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9567 && !(TREE_CODE (value) == STRING_CST
9568 && TREE_CODE (type) == ARRAY_TYPE
9569 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9570 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9571 TYPE_MAIN_VARIANT (type)))
9572 value = array_to_pointer_conversion (input_location, value);
9574 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9575 && require_constant_value && pending)
9577 /* As an extension, allow initializing objects with static storage
9578 duration with compound literals (which are then treated just as
9579 the brace enclosed list they contain). */
9580 if (flag_isoc99)
9581 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9582 "constant");
9583 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9584 value = DECL_INITIAL (decl);
9587 npc = null_pointer_constant_p (value);
9588 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9590 semantic_type = TREE_TYPE (value);
9591 value = TREE_OPERAND (value, 0);
9593 value = c_fully_fold (value, require_constant_value, &maybe_const);
9595 if (value == error_mark_node)
9596 constructor_erroneous = 1;
9597 else if (!TREE_CONSTANT (value))
9598 constructor_constant = 0;
9599 else if (!initializer_constant_valid_p (value,
9600 TREE_TYPE (value),
9601 AGGREGATE_TYPE_P (constructor_type)
9602 && TYPE_REVERSE_STORAGE_ORDER
9603 (constructor_type))
9604 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9605 && DECL_C_BIT_FIELD (field)
9606 && TREE_CODE (value) != INTEGER_CST))
9607 constructor_simple = 0;
9608 if (!maybe_const)
9609 constructor_nonconst = 1;
9611 /* Digest the initializer and issue any errors about incompatible
9612 types before issuing errors about non-constant initializers. */
9613 tree new_value = value;
9614 if (semantic_type)
9615 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9616 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9617 require_constant_value);
9618 if (new_value == error_mark_node)
9620 constructor_erroneous = 1;
9621 return;
9623 if (require_constant_value || require_constant_elements)
9624 constant_expression_warning (new_value);
9626 /* Proceed to check the constness of the original initializer. */
9627 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9629 if (require_constant_value)
9631 error_init (loc, "initializer element is not constant");
9632 value = error_mark_node;
9634 else if (require_constant_elements)
9635 pedwarn (loc, OPT_Wpedantic,
9636 "initializer element is not computable at load time");
9638 else if (!maybe_const
9639 && (require_constant_value || require_constant_elements))
9640 pedwarn_init (loc, OPT_Wpedantic,
9641 "initializer element is not a constant expression");
9643 /* Issue -Wc++-compat warnings about initializing a bitfield with
9644 enum type. */
9645 if (warn_cxx_compat
9646 && field != NULL_TREE
9647 && TREE_CODE (field) == FIELD_DECL
9648 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9649 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9650 != TYPE_MAIN_VARIANT (type))
9651 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9653 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9654 if (checktype != error_mark_node
9655 && (TYPE_MAIN_VARIANT (checktype)
9656 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9657 warning_init (loc, OPT_Wc___compat,
9658 "enum conversion in initialization is invalid in C++");
9661 /* If this field is empty and does not have side effects (and is not at
9662 the end of structure), don't do anything other than checking the
9663 initializer. */
9664 if (field
9665 && (TREE_TYPE (field) == error_mark_node
9666 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9667 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9668 && !TREE_SIDE_EFFECTS (new_value)
9669 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9670 || DECL_CHAIN (field)))))
9671 return;
9673 /* Finally, set VALUE to the initializer value digested above. */
9674 value = new_value;
9676 /* If this element doesn't come next in sequence,
9677 put it on constructor_pending_elts. */
9678 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9679 && (!constructor_incremental
9680 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9682 if (constructor_incremental
9683 && tree_int_cst_lt (field, constructor_unfilled_index))
9684 set_nonincremental_init (braced_init_obstack);
9686 add_pending_init (loc, field, value, origtype, implicit,
9687 braced_init_obstack);
9688 return;
9690 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9691 && (!constructor_incremental
9692 || field != constructor_unfilled_fields))
9694 /* We do this for records but not for unions. In a union,
9695 no matter which field is specified, it can be initialized
9696 right away since it starts at the beginning of the union. */
9697 if (constructor_incremental)
9699 if (!constructor_unfilled_fields)
9700 set_nonincremental_init (braced_init_obstack);
9701 else
9703 tree bitpos, unfillpos;
9705 bitpos = bit_position (field);
9706 unfillpos = bit_position (constructor_unfilled_fields);
9708 if (tree_int_cst_lt (bitpos, unfillpos))
9709 set_nonincremental_init (braced_init_obstack);
9713 add_pending_init (loc, field, value, origtype, implicit,
9714 braced_init_obstack);
9715 return;
9717 else if (TREE_CODE (constructor_type) == UNION_TYPE
9718 && !vec_safe_is_empty (constructor_elements))
9720 if (!implicit)
9722 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9723 warning_init (loc, OPT_Woverride_init_side_effects,
9724 "initialized field with side-effects overwritten");
9725 else if (warn_override_init)
9726 warning_init (loc, OPT_Woverride_init,
9727 "initialized field overwritten");
9730 /* We can have just one union field set. */
9731 constructor_elements = NULL;
9734 /* Otherwise, output this element either to
9735 constructor_elements or to the assembler file. */
9737 constructor_elt celt = {field, value};
9738 vec_safe_push (constructor_elements, celt);
9740 /* Advance the variable that indicates sequential elements output. */
9741 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9742 constructor_unfilled_index
9743 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9744 bitsize_one_node);
9745 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9747 constructor_unfilled_fields
9748 = DECL_CHAIN (constructor_unfilled_fields);
9750 /* Skip any nameless bit fields. */
9751 while (constructor_unfilled_fields != NULL_TREE
9752 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9753 constructor_unfilled_fields =
9754 DECL_CHAIN (constructor_unfilled_fields);
9756 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9757 constructor_unfilled_fields = NULL_TREE;
9759 /* Now output any pending elements which have become next. */
9760 if (pending)
9761 output_pending_init_elements (0, braced_init_obstack);
9764 /* For two FIELD_DECLs in the same chain, return -1 if field1
9765 comes before field2, 1 if field1 comes after field2 and
9766 0 if field1 == field2. */
9768 static int
9769 init_field_decl_cmp (tree field1, tree field2)
9771 if (field1 == field2)
9772 return 0;
9774 tree bitpos1 = bit_position (field1);
9775 tree bitpos2 = bit_position (field2);
9776 if (tree_int_cst_equal (bitpos1, bitpos2))
9778 /* If one of the fields has non-zero bitsize, then that
9779 field must be the last one in a sequence of zero
9780 sized fields, fields after it will have bigger
9781 bit_position. */
9782 if (TREE_TYPE (field1) != error_mark_node
9783 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9784 && integer_nonzerop (TREE_TYPE (field1)))
9785 return 1;
9786 if (TREE_TYPE (field2) != error_mark_node
9787 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9788 && integer_nonzerop (TREE_TYPE (field2)))
9789 return -1;
9790 /* Otherwise, fallback to DECL_CHAIN walk to find out
9791 which field comes earlier. Walk chains of both
9792 fields, so that if field1 and field2 are close to each
9793 other in either order, it is found soon even for large
9794 sequences of zero sized fields. */
9795 tree f1 = field1, f2 = field2;
9796 while (1)
9798 f1 = DECL_CHAIN (f1);
9799 f2 = DECL_CHAIN (f2);
9800 if (f1 == NULL_TREE)
9802 gcc_assert (f2);
9803 return 1;
9805 if (f2 == NULL_TREE)
9806 return -1;
9807 if (f1 == field2)
9808 return -1;
9809 if (f2 == field1)
9810 return 1;
9811 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9812 return 1;
9813 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9814 return -1;
9817 else if (tree_int_cst_lt (bitpos1, bitpos2))
9818 return -1;
9819 else
9820 return 1;
9823 /* Output any pending elements which have become next.
9824 As we output elements, constructor_unfilled_{fields,index}
9825 advances, which may cause other elements to become next;
9826 if so, they too are output.
9828 If ALL is 0, we return when there are
9829 no more pending elements to output now.
9831 If ALL is 1, we output space as necessary so that
9832 we can output all the pending elements. */
9833 static void
9834 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9836 struct init_node *elt = constructor_pending_elts;
9837 tree next;
9839 retry:
9841 /* Look through the whole pending tree.
9842 If we find an element that should be output now,
9843 output it. Otherwise, set NEXT to the element
9844 that comes first among those still pending. */
9846 next = NULL_TREE;
9847 while (elt)
9849 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9851 if (tree_int_cst_equal (elt->purpose,
9852 constructor_unfilled_index))
9853 output_init_element (input_location, elt->value, elt->origtype,
9854 true, TREE_TYPE (constructor_type),
9855 constructor_unfilled_index, false, false,
9856 braced_init_obstack);
9857 else if (tree_int_cst_lt (constructor_unfilled_index,
9858 elt->purpose))
9860 /* Advance to the next smaller node. */
9861 if (elt->left)
9862 elt = elt->left;
9863 else
9865 /* We have reached the smallest node bigger than the
9866 current unfilled index. Fill the space first. */
9867 next = elt->purpose;
9868 break;
9871 else
9873 /* Advance to the next bigger node. */
9874 if (elt->right)
9875 elt = elt->right;
9876 else
9878 /* We have reached the biggest node in a subtree. Find
9879 the parent of it, which is the next bigger node. */
9880 while (elt->parent && elt->parent->right == elt)
9881 elt = elt->parent;
9882 elt = elt->parent;
9883 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9884 elt->purpose))
9886 next = elt->purpose;
9887 break;
9892 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9894 /* If the current record is complete we are done. */
9895 if (constructor_unfilled_fields == NULL_TREE)
9896 break;
9898 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9899 elt->purpose);
9900 if (cmp == 0)
9901 output_init_element (input_location, elt->value, elt->origtype,
9902 true, TREE_TYPE (elt->purpose),
9903 elt->purpose, false, false,
9904 braced_init_obstack);
9905 else if (cmp < 0)
9907 /* Advance to the next smaller node. */
9908 if (elt->left)
9909 elt = elt->left;
9910 else
9912 /* We have reached the smallest node bigger than the
9913 current unfilled field. Fill the space first. */
9914 next = elt->purpose;
9915 break;
9918 else
9920 /* Advance to the next bigger node. */
9921 if (elt->right)
9922 elt = elt->right;
9923 else
9925 /* We have reached the biggest node in a subtree. Find
9926 the parent of it, which is the next bigger node. */
9927 while (elt->parent && elt->parent->right == elt)
9928 elt = elt->parent;
9929 elt = elt->parent;
9930 if (elt
9931 && init_field_decl_cmp (constructor_unfilled_fields,
9932 elt->purpose) < 0)
9934 next = elt->purpose;
9935 break;
9942 /* Ordinarily return, but not if we want to output all
9943 and there are elements left. */
9944 if (!(all && next != NULL_TREE))
9945 return;
9947 /* If it's not incremental, just skip over the gap, so that after
9948 jumping to retry we will output the next successive element. */
9949 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9950 constructor_unfilled_fields = next;
9951 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9952 constructor_unfilled_index = next;
9954 /* ELT now points to the node in the pending tree with the next
9955 initializer to output. */
9956 goto retry;
9959 /* Add one non-braced element to the current constructor level.
9960 This adjusts the current position within the constructor's type.
9961 This may also start or terminate implicit levels
9962 to handle a partly-braced initializer.
9964 Once this has found the correct level for the new element,
9965 it calls output_init_element.
9967 IMPLICIT is true if value comes from pop_init_level (1),
9968 the new initializer has been merged with the existing one
9969 and thus no warnings should be emitted about overriding an
9970 existing initializer. */
9972 void
9973 process_init_element (location_t loc, struct c_expr value, bool implicit,
9974 struct obstack * braced_init_obstack)
9976 tree orig_value = value.value;
9977 int string_flag
9978 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9979 bool strict_string = value.original_code == STRING_CST;
9980 bool was_designated = designator_depth != 0;
9982 designator_depth = 0;
9983 designator_erroneous = 0;
9985 if (!implicit && value.value && !integer_zerop (value.value))
9986 constructor_zeroinit = 0;
9988 /* Handle superfluous braces around string cst as in
9989 char x[] = {"foo"}; */
9990 if (string_flag
9991 && constructor_type
9992 && !was_designated
9993 && TREE_CODE (constructor_type) == ARRAY_TYPE
9994 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9995 && integer_zerop (constructor_unfilled_index))
9997 if (constructor_stack->replacement_value.value)
9998 error_init (loc, "excess elements in %<char%> array initializer");
9999 constructor_stack->replacement_value = value;
10000 return;
10003 if (constructor_stack->replacement_value.value != NULL_TREE)
10005 error_init (loc, "excess elements in struct initializer");
10006 return;
10009 /* Ignore elements of a brace group if it is entirely superfluous
10010 and has already been diagnosed, or if the type is erroneous. */
10011 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10012 return;
10014 /* Ignore elements of an initializer for a variable-size type.
10015 Those are diagnosed in digest_init. */
10016 if (COMPLETE_TYPE_P (constructor_type)
10017 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10018 return;
10020 if (!implicit && warn_designated_init && !was_designated
10021 && TREE_CODE (constructor_type) == RECORD_TYPE
10022 && lookup_attribute ("designated_init",
10023 TYPE_ATTRIBUTES (constructor_type)))
10024 warning_init (loc,
10025 OPT_Wdesignated_init,
10026 "positional initialization of field "
10027 "in %<struct%> declared with %<designated_init%> attribute");
10029 /* If we've exhausted any levels that didn't have braces,
10030 pop them now. */
10031 while (constructor_stack->implicit)
10033 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10034 && constructor_fields == NULL_TREE)
10035 process_init_element (loc,
10036 pop_init_level (loc, 1, braced_init_obstack,
10037 last_init_list_comma),
10038 true, braced_init_obstack);
10039 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10040 || gnu_vector_type_p (constructor_type))
10041 && constructor_max_index
10042 && tree_int_cst_lt (constructor_max_index,
10043 constructor_index))
10044 process_init_element (loc,
10045 pop_init_level (loc, 1, braced_init_obstack,
10046 last_init_list_comma),
10047 true, braced_init_obstack);
10048 else
10049 break;
10052 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10053 if (constructor_range_stack)
10055 /* If value is a compound literal and we'll be just using its
10056 content, don't put it into a SAVE_EXPR. */
10057 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10058 || !require_constant_value)
10060 tree semantic_type = NULL_TREE;
10061 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10063 semantic_type = TREE_TYPE (value.value);
10064 value.value = TREE_OPERAND (value.value, 0);
10066 value.value = save_expr (value.value);
10067 if (semantic_type)
10068 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10069 value.value);
10073 while (1)
10075 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10077 tree fieldtype;
10078 enum tree_code fieldcode;
10080 if (constructor_fields == NULL_TREE)
10082 pedwarn_init (loc, 0, "excess elements in struct initializer");
10083 break;
10086 fieldtype = TREE_TYPE (constructor_fields);
10087 if (fieldtype != error_mark_node)
10088 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10089 fieldcode = TREE_CODE (fieldtype);
10091 /* Error for non-static initialization of a flexible array member. */
10092 if (fieldcode == ARRAY_TYPE
10093 && !require_constant_value
10094 && TYPE_SIZE (fieldtype) == NULL_TREE
10095 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10097 error_init (loc, "non-static initialization of a flexible "
10098 "array member");
10099 break;
10102 /* Error for initialization of a flexible array member with
10103 a string constant if the structure is in an array. E.g.:
10104 struct S { int x; char y[]; };
10105 struct S s[] = { { 1, "foo" } };
10106 is invalid. */
10107 if (string_flag
10108 && fieldcode == ARRAY_TYPE
10109 && constructor_depth > 1
10110 && TYPE_SIZE (fieldtype) == NULL_TREE
10111 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10113 bool in_array_p = false;
10114 for (struct constructor_stack *p = constructor_stack;
10115 p && p->type; p = p->next)
10116 if (TREE_CODE (p->type) == ARRAY_TYPE)
10118 in_array_p = true;
10119 break;
10121 if (in_array_p)
10123 error_init (loc, "initialization of flexible array "
10124 "member in a nested context");
10125 break;
10129 /* Accept a string constant to initialize a subarray. */
10130 if (value.value != NULL_TREE
10131 && fieldcode == ARRAY_TYPE
10132 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10133 && string_flag)
10134 value.value = orig_value;
10135 /* Otherwise, if we have come to a subaggregate,
10136 and we don't have an element of its type, push into it. */
10137 else if (value.value != NULL_TREE
10138 && value.value != error_mark_node
10139 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
10140 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
10141 || fieldcode == UNION_TYPE
10142 || gnu_vector_type_p (fieldtype)))
10144 push_init_level (loc, 1, braced_init_obstack);
10145 continue;
10148 if (value.value)
10150 push_member_name (constructor_fields);
10151 output_init_element (loc, value.value, value.original_type,
10152 strict_string, fieldtype,
10153 constructor_fields, true, implicit,
10154 braced_init_obstack);
10155 RESTORE_SPELLING_DEPTH (constructor_depth);
10157 else
10158 /* Do the bookkeeping for an element that was
10159 directly output as a constructor. */
10161 /* For a record, keep track of end position of last field. */
10162 if (DECL_SIZE (constructor_fields))
10163 constructor_bit_index
10164 = size_binop_loc (input_location, PLUS_EXPR,
10165 bit_position (constructor_fields),
10166 DECL_SIZE (constructor_fields));
10168 /* If the current field was the first one not yet written out,
10169 it isn't now, so update. */
10170 if (constructor_unfilled_fields == constructor_fields)
10172 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10173 /* Skip any nameless bit fields. */
10174 while (constructor_unfilled_fields != 0
10175 && (DECL_UNNAMED_BIT_FIELD
10176 (constructor_unfilled_fields)))
10177 constructor_unfilled_fields =
10178 DECL_CHAIN (constructor_unfilled_fields);
10182 constructor_fields = DECL_CHAIN (constructor_fields);
10183 /* Skip any nameless bit fields at the beginning. */
10184 while (constructor_fields != NULL_TREE
10185 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10186 constructor_fields = DECL_CHAIN (constructor_fields);
10188 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10190 tree fieldtype;
10191 enum tree_code fieldcode;
10193 if (constructor_fields == NULL_TREE)
10195 pedwarn_init (loc, 0,
10196 "excess elements in union initializer");
10197 break;
10200 fieldtype = TREE_TYPE (constructor_fields);
10201 if (fieldtype != error_mark_node)
10202 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10203 fieldcode = TREE_CODE (fieldtype);
10205 /* Warn that traditional C rejects initialization of unions.
10206 We skip the warning if the value is zero. This is done
10207 under the assumption that the zero initializer in user
10208 code appears conditioned on e.g. __STDC__ to avoid
10209 "missing initializer" warnings and relies on default
10210 initialization to zero in the traditional C case.
10211 We also skip the warning if the initializer is designated,
10212 again on the assumption that this must be conditional on
10213 __STDC__ anyway (and we've already complained about the
10214 member-designator already). */
10215 if (!in_system_header_at (input_location) && !constructor_designated
10216 && !(value.value && (integer_zerop (value.value)
10217 || real_zerop (value.value))))
10218 warning (OPT_Wtraditional, "traditional C rejects initialization "
10219 "of unions");
10221 /* Accept a string constant to initialize a subarray. */
10222 if (value.value != NULL_TREE
10223 && fieldcode == ARRAY_TYPE
10224 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10225 && string_flag)
10226 value.value = orig_value;
10227 /* Otherwise, if we have come to a subaggregate,
10228 and we don't have an element of its type, push into it. */
10229 else if (value.value != NULL_TREE
10230 && value.value != error_mark_node
10231 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
10232 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
10233 || fieldcode == UNION_TYPE
10234 || gnu_vector_type_p (fieldtype)))
10236 push_init_level (loc, 1, braced_init_obstack);
10237 continue;
10240 if (value.value)
10242 push_member_name (constructor_fields);
10243 output_init_element (loc, value.value, value.original_type,
10244 strict_string, fieldtype,
10245 constructor_fields, true, implicit,
10246 braced_init_obstack);
10247 RESTORE_SPELLING_DEPTH (constructor_depth);
10249 else
10250 /* Do the bookkeeping for an element that was
10251 directly output as a constructor. */
10253 constructor_bit_index = DECL_SIZE (constructor_fields);
10254 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10257 constructor_fields = NULL_TREE;
10259 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10261 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10262 enum tree_code eltcode = TREE_CODE (elttype);
10264 /* Accept a string constant to initialize a subarray. */
10265 if (value.value != NULL_TREE
10266 && eltcode == ARRAY_TYPE
10267 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10268 && string_flag)
10269 value.value = orig_value;
10270 /* Otherwise, if we have come to a subaggregate,
10271 and we don't have an element of its type, push into it. */
10272 else if (value.value != NULL_TREE
10273 && value.value != error_mark_node
10274 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
10275 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
10276 || eltcode == UNION_TYPE
10277 || gnu_vector_type_p (elttype)))
10279 push_init_level (loc, 1, braced_init_obstack);
10280 continue;
10283 if (constructor_max_index != NULL_TREE
10284 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10285 || integer_all_onesp (constructor_max_index)))
10287 pedwarn_init (loc, 0,
10288 "excess elements in array initializer");
10289 break;
10292 /* Now output the actual element. */
10293 if (value.value)
10295 push_array_bounds (tree_to_uhwi (constructor_index));
10296 output_init_element (loc, value.value, value.original_type,
10297 strict_string, elttype,
10298 constructor_index, true, implicit,
10299 braced_init_obstack);
10300 RESTORE_SPELLING_DEPTH (constructor_depth);
10303 constructor_index
10304 = size_binop_loc (input_location, PLUS_EXPR,
10305 constructor_index, bitsize_one_node);
10307 if (!value.value)
10308 /* If we are doing the bookkeeping for an element that was
10309 directly output as a constructor, we must update
10310 constructor_unfilled_index. */
10311 constructor_unfilled_index = constructor_index;
10313 else if (gnu_vector_type_p (constructor_type))
10315 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10317 /* Do a basic check of initializer size. Note that vectors
10318 always have a fixed size derived from their type. */
10319 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10321 pedwarn_init (loc, 0,
10322 "excess elements in vector initializer");
10323 break;
10326 /* Now output the actual element. */
10327 if (value.value)
10329 if (TREE_CODE (value.value) == VECTOR_CST)
10330 elttype = TYPE_MAIN_VARIANT (constructor_type);
10331 output_init_element (loc, value.value, value.original_type,
10332 strict_string, elttype,
10333 constructor_index, true, implicit,
10334 braced_init_obstack);
10337 constructor_index
10338 = size_binop_loc (input_location,
10339 PLUS_EXPR, constructor_index, bitsize_one_node);
10341 if (!value.value)
10342 /* If we are doing the bookkeeping for an element that was
10343 directly output as a constructor, we must update
10344 constructor_unfilled_index. */
10345 constructor_unfilled_index = constructor_index;
10348 /* Handle the sole element allowed in a braced initializer
10349 for a scalar variable. */
10350 else if (constructor_type != error_mark_node
10351 && constructor_fields == NULL_TREE)
10353 pedwarn_init (loc, 0,
10354 "excess elements in scalar initializer");
10355 break;
10357 else
10359 if (value.value)
10360 output_init_element (loc, value.value, value.original_type,
10361 strict_string, constructor_type,
10362 NULL_TREE, true, implicit,
10363 braced_init_obstack);
10364 constructor_fields = NULL_TREE;
10367 /* Handle range initializers either at this level or anywhere higher
10368 in the designator stack. */
10369 if (constructor_range_stack)
10371 struct constructor_range_stack *p, *range_stack;
10372 int finish = 0;
10374 range_stack = constructor_range_stack;
10375 constructor_range_stack = 0;
10376 while (constructor_stack != range_stack->stack)
10378 gcc_assert (constructor_stack->implicit);
10379 process_init_element (loc,
10380 pop_init_level (loc, 1,
10381 braced_init_obstack,
10382 last_init_list_comma),
10383 true, braced_init_obstack);
10385 for (p = range_stack;
10386 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10387 p = p->prev)
10389 gcc_assert (constructor_stack->implicit);
10390 process_init_element (loc,
10391 pop_init_level (loc, 1,
10392 braced_init_obstack,
10393 last_init_list_comma),
10394 true, braced_init_obstack);
10397 p->index = size_binop_loc (input_location,
10398 PLUS_EXPR, p->index, bitsize_one_node);
10399 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10400 finish = 1;
10402 while (1)
10404 constructor_index = p->index;
10405 constructor_fields = p->fields;
10406 if (finish && p->range_end && p->index == p->range_start)
10408 finish = 0;
10409 p->prev = 0;
10411 p = p->next;
10412 if (!p)
10413 break;
10414 finish_implicit_inits (loc, braced_init_obstack);
10415 push_init_level (loc, 2, braced_init_obstack);
10416 p->stack = constructor_stack;
10417 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10418 p->index = p->range_start;
10421 if (!finish)
10422 constructor_range_stack = range_stack;
10423 continue;
10426 break;
10429 constructor_range_stack = 0;
10432 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10433 (guaranteed to be 'volatile' or null) and ARGS (represented using
10434 an ASM_EXPR node). */
10435 tree
10436 build_asm_stmt (bool is_volatile, tree args)
10438 if (is_volatile)
10439 ASM_VOLATILE_P (args) = 1;
10440 return add_stmt (args);
10443 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10444 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10445 SIMPLE indicates whether there was anything at all after the
10446 string in the asm expression -- asm("blah") and asm("blah" : )
10447 are subtly different. We use a ASM_EXPR node to represent this.
10448 LOC is the location of the asm, and IS_INLINE says whether this
10449 is asm inline. */
10450 tree
10451 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10452 tree clobbers, tree labels, bool simple, bool is_inline)
10454 tree tail;
10455 tree args;
10456 int i;
10457 const char *constraint;
10458 const char **oconstraints;
10459 bool allows_mem, allows_reg, is_inout;
10460 int ninputs, noutputs;
10462 ninputs = list_length (inputs);
10463 noutputs = list_length (outputs);
10464 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10466 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10468 /* Remove output conversions that change the type but not the mode. */
10469 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10471 tree output = TREE_VALUE (tail);
10473 output = c_fully_fold (output, false, NULL, true);
10475 /* ??? Really, this should not be here. Users should be using a
10476 proper lvalue, dammit. But there's a long history of using casts
10477 in the output operands. In cases like longlong.h, this becomes a
10478 primitive form of typechecking -- if the cast can be removed, then
10479 the output operand had a type of the proper width; otherwise we'll
10480 get an error. Gross, but ... */
10481 STRIP_NOPS (output);
10483 if (!lvalue_or_else (loc, output, lv_asm))
10484 output = error_mark_node;
10486 if (output != error_mark_node
10487 && (TREE_READONLY (output)
10488 || TYPE_READONLY (TREE_TYPE (output))
10489 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10490 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10491 readonly_error (loc, output, lv_asm);
10493 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10494 oconstraints[i] = constraint;
10496 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10497 &allows_mem, &allows_reg, &is_inout))
10499 /* If the operand is going to end up in memory,
10500 mark it addressable. */
10501 if (!allows_reg && !c_mark_addressable (output))
10502 output = error_mark_node;
10503 if (!(!allows_reg && allows_mem)
10504 && output != error_mark_node
10505 && VOID_TYPE_P (TREE_TYPE (output)))
10507 error_at (loc, "invalid use of void expression");
10508 output = error_mark_node;
10511 else
10512 output = error_mark_node;
10514 TREE_VALUE (tail) = output;
10517 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10519 tree input;
10521 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10522 input = TREE_VALUE (tail);
10524 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10525 oconstraints, &allows_mem, &allows_reg))
10527 /* If the operand is going to end up in memory,
10528 mark it addressable. */
10529 if (!allows_reg && allows_mem)
10531 input = c_fully_fold (input, false, NULL, true);
10533 /* Strip the nops as we allow this case. FIXME, this really
10534 should be rejected or made deprecated. */
10535 STRIP_NOPS (input);
10536 if (!c_mark_addressable (input))
10537 input = error_mark_node;
10539 else
10541 struct c_expr expr;
10542 memset (&expr, 0, sizeof (expr));
10543 expr.value = input;
10544 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10545 input = c_fully_fold (expr.value, false, NULL);
10547 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10549 error_at (loc, "invalid use of void expression");
10550 input = error_mark_node;
10554 else
10555 input = error_mark_node;
10557 TREE_VALUE (tail) = input;
10560 /* ASMs with labels cannot have outputs. This should have been
10561 enforced by the parser. */
10562 gcc_assert (outputs == NULL || labels == NULL);
10564 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10566 /* asm statements without outputs, including simple ones, are treated
10567 as volatile. */
10568 ASM_INPUT_P (args) = simple;
10569 ASM_VOLATILE_P (args) = (noutputs == 0);
10570 ASM_INLINE_P (args) = is_inline;
10572 return args;
10575 /* Generate a goto statement to LABEL. LOC is the location of the
10576 GOTO. */
10578 tree
10579 c_finish_goto_label (location_t loc, tree label)
10581 tree decl = lookup_label_for_goto (loc, label);
10582 if (!decl)
10583 return NULL_TREE;
10584 TREE_USED (decl) = 1;
10586 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10587 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10588 SET_EXPR_LOCATION (t, loc);
10589 return add_stmt (t);
10593 /* Generate a computed goto statement to EXPR. LOC is the location of
10594 the GOTO. */
10596 tree
10597 c_finish_goto_ptr (location_t loc, tree expr)
10599 tree t;
10600 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10601 expr = c_fully_fold (expr, false, NULL);
10602 expr = convert (ptr_type_node, expr);
10603 t = build1 (GOTO_EXPR, void_type_node, expr);
10604 SET_EXPR_LOCATION (t, loc);
10605 return add_stmt (t);
10608 /* Generate a C `return' statement. RETVAL is the expression for what
10609 to return, or a null pointer for `return;' with no value. LOC is
10610 the location of the return statement, or the location of the expression,
10611 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10612 is the original type of RETVAL. */
10614 tree
10615 c_finish_return (location_t loc, tree retval, tree origtype)
10617 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10618 bool no_warning = false;
10619 bool npc = false;
10621 /* Use the expansion point to handle cases such as returning NULL
10622 in a function returning void. */
10623 location_t xloc = expansion_point_location_if_in_system_header (loc);
10625 if (TREE_THIS_VOLATILE (current_function_decl))
10626 warning_at (xloc, 0,
10627 "function declared %<noreturn%> has a %<return%> statement");
10629 if (retval)
10631 tree semantic_type = NULL_TREE;
10632 npc = null_pointer_constant_p (retval);
10633 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10635 semantic_type = TREE_TYPE (retval);
10636 retval = TREE_OPERAND (retval, 0);
10638 retval = c_fully_fold (retval, false, NULL);
10639 if (semantic_type)
10640 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10643 if (!retval)
10645 current_function_returns_null = 1;
10646 if ((warn_return_type >= 0 || flag_isoc99)
10647 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10649 bool warned_here;
10650 if (flag_isoc99)
10651 warned_here = pedwarn
10652 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10653 "%<return%> with no value, in function returning non-void");
10654 else
10655 warned_here = warning_at
10656 (loc, OPT_Wreturn_type,
10657 "%<return%> with no value, in function returning non-void");
10658 no_warning = true;
10659 if (warned_here)
10660 inform (DECL_SOURCE_LOCATION (current_function_decl),
10661 "declared here");
10664 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10666 current_function_returns_null = 1;
10667 bool warned_here;
10668 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10669 warned_here = pedwarn
10670 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10671 "%<return%> with a value, in function returning void");
10672 else
10673 warned_here = pedwarn
10674 (xloc, OPT_Wpedantic, "ISO C forbids "
10675 "%<return%> with expression, in function returning void");
10676 if (warned_here)
10677 inform (DECL_SOURCE_LOCATION (current_function_decl),
10678 "declared here");
10680 else
10682 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10683 retval, origtype, ic_return,
10684 npc, NULL_TREE, NULL_TREE, 0);
10685 tree res = DECL_RESULT (current_function_decl);
10686 tree inner;
10687 bool save;
10689 current_function_returns_value = 1;
10690 if (t == error_mark_node)
10691 return NULL_TREE;
10693 save = in_late_binary_op;
10694 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10695 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10696 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10697 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10698 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10699 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10700 in_late_binary_op = true;
10701 inner = t = convert (TREE_TYPE (res), t);
10702 in_late_binary_op = save;
10704 /* Strip any conversions, additions, and subtractions, and see if
10705 we are returning the address of a local variable. Warn if so. */
10706 while (1)
10708 switch (TREE_CODE (inner))
10710 CASE_CONVERT:
10711 case NON_LVALUE_EXPR:
10712 case PLUS_EXPR:
10713 case POINTER_PLUS_EXPR:
10714 inner = TREE_OPERAND (inner, 0);
10715 continue;
10717 case MINUS_EXPR:
10718 /* If the second operand of the MINUS_EXPR has a pointer
10719 type (or is converted from it), this may be valid, so
10720 don't give a warning. */
10722 tree op1 = TREE_OPERAND (inner, 1);
10724 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10725 && (CONVERT_EXPR_P (op1)
10726 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10727 op1 = TREE_OPERAND (op1, 0);
10729 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10730 break;
10732 inner = TREE_OPERAND (inner, 0);
10733 continue;
10736 case ADDR_EXPR:
10737 inner = TREE_OPERAND (inner, 0);
10739 while (REFERENCE_CLASS_P (inner)
10740 && !INDIRECT_REF_P (inner))
10741 inner = TREE_OPERAND (inner, 0);
10743 if (DECL_P (inner)
10744 && !DECL_EXTERNAL (inner)
10745 && !TREE_STATIC (inner)
10746 && DECL_CONTEXT (inner) == current_function_decl
10747 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10749 if (TREE_CODE (inner) == LABEL_DECL)
10750 warning_at (loc, OPT_Wreturn_local_addr,
10751 "function returns address of label");
10752 else
10754 warning_at (loc, OPT_Wreturn_local_addr,
10755 "function returns address of local variable");
10756 tree zero = build_zero_cst (TREE_TYPE (res));
10757 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10760 break;
10762 default:
10763 break;
10766 break;
10769 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10770 SET_EXPR_LOCATION (retval, loc);
10772 if (warn_sequence_point)
10773 verify_sequence_points (retval);
10776 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10777 TREE_NO_WARNING (ret_stmt) |= no_warning;
10778 return add_stmt (ret_stmt);
10781 struct c_switch {
10782 /* The SWITCH_EXPR being built. */
10783 tree switch_expr;
10785 /* The original type of the testing expression, i.e. before the
10786 default conversion is applied. */
10787 tree orig_type;
10789 /* A splay-tree mapping the low element of a case range to the high
10790 element, or NULL_TREE if there is no high element. Used to
10791 determine whether or not a new case label duplicates an old case
10792 label. We need a tree, rather than simply a hash table, because
10793 of the GNU case range extension. */
10794 splay_tree cases;
10796 /* The bindings at the point of the switch. This is used for
10797 warnings crossing decls when branching to a case label. */
10798 struct c_spot_bindings *bindings;
10800 /* The next node on the stack. */
10801 struct c_switch *next;
10803 /* Remember whether the controlling expression had boolean type
10804 before integer promotions for the sake of -Wswitch-bool. */
10805 bool bool_cond_p;
10808 /* A stack of the currently active switch statements. The innermost
10809 switch statement is on the top of the stack. There is no need to
10810 mark the stack for garbage collection because it is only active
10811 during the processing of the body of a function, and we never
10812 collect at that point. */
10814 struct c_switch *c_switch_stack;
10816 /* Start a C switch statement, testing expression EXP. Return the new
10817 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10818 SWITCH_COND_LOC is the location of the switch's condition.
10819 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10821 tree
10822 c_start_case (location_t switch_loc,
10823 location_t switch_cond_loc,
10824 tree exp, bool explicit_cast_p)
10826 tree orig_type = error_mark_node;
10827 bool bool_cond_p = false;
10828 struct c_switch *cs;
10830 if (exp != error_mark_node)
10832 orig_type = TREE_TYPE (exp);
10834 if (!INTEGRAL_TYPE_P (orig_type))
10836 if (orig_type != error_mark_node)
10838 error_at (switch_cond_loc, "switch quantity not an integer");
10839 orig_type = error_mark_node;
10841 exp = integer_zero_node;
10843 else
10845 tree type = TYPE_MAIN_VARIANT (orig_type);
10846 tree e = exp;
10848 /* Warn if the condition has boolean value. */
10849 while (TREE_CODE (e) == COMPOUND_EXPR)
10850 e = TREE_OPERAND (e, 1);
10852 if ((TREE_CODE (type) == BOOLEAN_TYPE
10853 || truth_value_p (TREE_CODE (e)))
10854 /* Explicit cast to int suppresses this warning. */
10855 && !(TREE_CODE (type) == INTEGER_TYPE
10856 && explicit_cast_p))
10857 bool_cond_p = true;
10859 if (!in_system_header_at (input_location)
10860 && (type == long_integer_type_node
10861 || type == long_unsigned_type_node))
10862 warning_at (switch_cond_loc,
10863 OPT_Wtraditional, "%<long%> switch expression not "
10864 "converted to %<int%> in ISO C");
10866 exp = c_fully_fold (exp, false, NULL);
10867 exp = default_conversion (exp);
10869 if (warn_sequence_point)
10870 verify_sequence_points (exp);
10874 /* Add this new SWITCH_EXPR to the stack. */
10875 cs = XNEW (struct c_switch);
10876 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10877 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10878 cs->orig_type = orig_type;
10879 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10880 cs->bindings = c_get_switch_bindings ();
10881 cs->bool_cond_p = bool_cond_p;
10882 cs->next = c_switch_stack;
10883 c_switch_stack = cs;
10885 return add_stmt (cs->switch_expr);
10888 /* Process a case label at location LOC. */
10890 tree
10891 do_case (location_t loc, tree low_value, tree high_value)
10893 tree label = NULL_TREE;
10895 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10897 low_value = c_fully_fold (low_value, false, NULL);
10898 if (TREE_CODE (low_value) == INTEGER_CST)
10899 pedwarn (loc, OPT_Wpedantic,
10900 "case label is not an integer constant expression");
10903 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10905 high_value = c_fully_fold (high_value, false, NULL);
10906 if (TREE_CODE (high_value) == INTEGER_CST)
10907 pedwarn (input_location, OPT_Wpedantic,
10908 "case label is not an integer constant expression");
10911 if (c_switch_stack == NULL)
10913 if (low_value)
10914 error_at (loc, "case label not within a switch statement");
10915 else
10916 error_at (loc, "%<default%> label not within a switch statement");
10917 return NULL_TREE;
10920 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10921 EXPR_LOCATION (c_switch_stack->switch_expr),
10922 loc))
10923 return NULL_TREE;
10925 label = c_add_case_label (loc, c_switch_stack->cases,
10926 SWITCH_COND (c_switch_stack->switch_expr),
10927 low_value, high_value);
10928 if (label == error_mark_node)
10929 label = NULL_TREE;
10930 return label;
10933 /* Finish the switch statement. TYPE is the original type of the
10934 controlling expression of the switch, or NULL_TREE. */
10936 void
10937 c_finish_case (tree body, tree type)
10939 struct c_switch *cs = c_switch_stack;
10940 location_t switch_location;
10942 SWITCH_BODY (cs->switch_expr) = body;
10944 /* Emit warnings as needed. */
10945 switch_location = EXPR_LOCATION (cs->switch_expr);
10946 c_do_switch_warnings (cs->cases, switch_location,
10947 type ? type : TREE_TYPE (cs->switch_expr),
10948 SWITCH_COND (cs->switch_expr), cs->bool_cond_p);
10949 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10950 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10952 /* Pop the stack. */
10953 c_switch_stack = cs->next;
10954 splay_tree_delete (cs->cases);
10955 c_release_switch_bindings (cs->bindings);
10956 XDELETE (cs);
10959 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10960 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10961 may be null. */
10963 void
10964 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10965 tree else_block)
10967 tree stmt;
10969 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10970 SET_EXPR_LOCATION (stmt, if_locus);
10971 add_stmt (stmt);
10974 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10975 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10976 is false for DO loops. INCR is the FOR increment expression. BODY is
10977 the statement controlled by the loop. BLAB is the break label. CLAB is
10978 the continue label. Everything is allowed to be NULL.
10979 COND_LOCUS is the location of the loop condition, INCR_LOCUS is the
10980 location of the FOR increment expression. */
10982 void
10983 c_finish_loop (location_t start_locus, location_t cond_locus, tree cond,
10984 location_t incr_locus, tree incr, tree body, tree blab,
10985 tree clab, bool cond_is_first)
10987 tree entry = NULL, exit = NULL, t;
10989 /* If the condition is zero don't generate a loop construct. */
10990 if (cond && integer_zerop (cond))
10992 if (cond_is_first)
10994 t = build_and_jump (&blab);
10995 SET_EXPR_LOCATION (t, start_locus);
10996 add_stmt (t);
10999 else
11001 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
11003 /* If we have an exit condition, then we build an IF with gotos either
11004 out of the loop, or to the top of it. If there's no exit condition,
11005 then we just build a jump back to the top. */
11006 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
11008 if (cond && !integer_nonzerop (cond))
11010 /* Canonicalize the loop condition to the end. This means
11011 generating a branch to the loop condition. Reuse the
11012 continue label, if possible. */
11013 if (cond_is_first)
11015 if (incr || !clab)
11017 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
11018 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
11020 else
11021 t = build1 (GOTO_EXPR, void_type_node, clab);
11022 SET_EXPR_LOCATION (t, start_locus);
11023 add_stmt (t);
11026 t = build_and_jump (&blab);
11027 exit = fold_build3_loc (cond_is_first ? start_locus : input_location,
11028 COND_EXPR, void_type_node, cond, exit, t);
11030 else
11032 /* For the backward-goto's location of an unconditional loop
11033 use the beginning of the body, or, if there is none, the
11034 top of the loop. */
11035 location_t loc = EXPR_LOCATION (expr_first (body));
11036 if (loc == UNKNOWN_LOCATION)
11037 loc = start_locus;
11038 SET_EXPR_LOCATION (exit, loc);
11041 add_stmt (top);
11044 if (body)
11045 add_stmt (body);
11046 if (clab)
11047 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
11048 if (incr)
11050 if (MAY_HAVE_DEBUG_MARKER_STMTS && incr_locus != UNKNOWN_LOCATION)
11052 t = build0 (DEBUG_BEGIN_STMT, void_type_node);
11053 SET_EXPR_LOCATION (t, incr_locus);
11054 add_stmt (t);
11056 add_stmt (incr);
11058 if (entry)
11059 add_stmt (entry);
11060 if (MAY_HAVE_DEBUG_MARKER_STMTS && cond_locus != UNKNOWN_LOCATION)
11062 t = build0 (DEBUG_BEGIN_STMT, void_type_node);
11063 SET_EXPR_LOCATION (t, cond_locus);
11064 add_stmt (t);
11066 if (exit)
11067 add_stmt (exit);
11068 if (blab)
11069 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
11072 tree
11073 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
11075 bool skip;
11076 tree label = *label_p;
11078 /* In switch statements break is sometimes stylistically used after
11079 a return statement. This can lead to spurious warnings about
11080 control reaching the end of a non-void function when it is
11081 inlined. Note that we are calling block_may_fallthru with
11082 language specific tree nodes; this works because
11083 block_may_fallthru returns true when given something it does not
11084 understand. */
11085 skip = !block_may_fallthru (cur_stmt_list);
11087 if (!label)
11089 if (!skip)
11090 *label_p = label = create_artificial_label (loc);
11092 else if (TREE_CODE (label) == LABEL_DECL)
11094 else switch (TREE_INT_CST_LOW (label))
11096 case 0:
11097 if (is_break)
11098 error_at (loc, "break statement not within loop or switch");
11099 else
11100 error_at (loc, "continue statement not within a loop");
11101 return NULL_TREE;
11103 case 1:
11104 gcc_assert (is_break);
11105 error_at (loc, "break statement used with OpenMP for loop");
11106 return NULL_TREE;
11108 case 2:
11109 if (is_break)
11110 error ("break statement within %<#pragma simd%> loop body");
11111 else
11112 error ("continue statement within %<#pragma simd%> loop body");
11113 return NULL_TREE;
11115 default:
11116 gcc_unreachable ();
11119 if (skip)
11120 return NULL_TREE;
11122 if (!is_break)
11123 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
11125 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
11128 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11130 static void
11131 emit_side_effect_warnings (location_t loc, tree expr)
11133 if (expr == error_mark_node)
11135 else if (!TREE_SIDE_EFFECTS (expr))
11137 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
11138 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11140 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11142 tree r = expr;
11143 location_t cloc = loc;
11144 while (TREE_CODE (r) == COMPOUND_EXPR)
11146 if (EXPR_HAS_LOCATION (r))
11147 cloc = EXPR_LOCATION (r);
11148 r = TREE_OPERAND (r, 1);
11150 if (!TREE_SIDE_EFFECTS (r)
11151 && !VOID_TYPE_P (TREE_TYPE (r))
11152 && !CONVERT_EXPR_P (r)
11153 && !TREE_NO_WARNING (r)
11154 && !TREE_NO_WARNING (expr))
11155 warning_at (cloc, OPT_Wunused_value,
11156 "right-hand operand of comma expression has no effect");
11158 else
11159 warn_if_unused_value (expr, loc);
11162 /* Process an expression as if it were a complete statement. Emit
11163 diagnostics, but do not call ADD_STMT. LOC is the location of the
11164 statement. */
11166 tree
11167 c_process_expr_stmt (location_t loc, tree expr)
11169 tree exprv;
11171 if (!expr)
11172 return NULL_TREE;
11174 expr = c_fully_fold (expr, false, NULL);
11176 if (warn_sequence_point)
11177 verify_sequence_points (expr);
11179 if (TREE_TYPE (expr) != error_mark_node
11180 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11181 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11182 error_at (loc, "expression statement has incomplete type");
11184 /* If we're not processing a statement expression, warn about unused values.
11185 Warnings for statement expressions will be emitted later, once we figure
11186 out which is the result. */
11187 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11188 && warn_unused_value)
11189 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11191 exprv = expr;
11192 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11193 exprv = TREE_OPERAND (exprv, 1);
11194 while (CONVERT_EXPR_P (exprv))
11195 exprv = TREE_OPERAND (exprv, 0);
11196 if (DECL_P (exprv)
11197 || handled_component_p (exprv)
11198 || TREE_CODE (exprv) == ADDR_EXPR)
11199 mark_exp_read (exprv);
11201 /* If the expression is not of a type to which we cannot assign a line
11202 number, wrap the thing in a no-op NOP_EXPR. */
11203 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11205 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11206 SET_EXPR_LOCATION (expr, loc);
11209 return expr;
11212 /* Emit an expression as a statement. LOC is the location of the
11213 expression. */
11215 tree
11216 c_finish_expr_stmt (location_t loc, tree expr)
11218 if (expr)
11219 return add_stmt (c_process_expr_stmt (loc, expr));
11220 else
11221 return NULL;
11224 /* Do the opposite and emit a statement as an expression. To begin,
11225 create a new binding level and return it. */
11227 tree
11228 c_begin_stmt_expr (void)
11230 tree ret;
11232 /* We must force a BLOCK for this level so that, if it is not expanded
11233 later, there is a way to turn off the entire subtree of blocks that
11234 are contained in it. */
11235 keep_next_level ();
11236 ret = c_begin_compound_stmt (true);
11238 c_bindings_start_stmt_expr (c_switch_stack == NULL
11239 ? NULL
11240 : c_switch_stack->bindings);
11242 /* Mark the current statement list as belonging to a statement list. */
11243 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11245 return ret;
11248 /* LOC is the location of the compound statement to which this body
11249 belongs. */
11251 tree
11252 c_finish_stmt_expr (location_t loc, tree body)
11254 tree last, type, tmp, val;
11255 tree *last_p;
11257 body = c_end_compound_stmt (loc, body, true);
11259 c_bindings_end_stmt_expr (c_switch_stack == NULL
11260 ? NULL
11261 : c_switch_stack->bindings);
11263 /* Locate the last statement in BODY. See c_end_compound_stmt
11264 about always returning a BIND_EXPR. */
11265 last_p = &BIND_EXPR_BODY (body);
11266 last = BIND_EXPR_BODY (body);
11268 continue_searching:
11269 if (TREE_CODE (last) == STATEMENT_LIST)
11271 tree_stmt_iterator l = tsi_last (last);
11273 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11274 tsi_prev (&l);
11276 /* This can happen with degenerate cases like ({ }). No value. */
11277 if (tsi_end_p (l))
11278 return body;
11280 /* If we're supposed to generate side effects warnings, process
11281 all of the statements except the last. */
11282 if (warn_unused_value)
11284 for (tree_stmt_iterator i = tsi_start (last);
11285 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11287 location_t tloc;
11288 tree t = tsi_stmt (i);
11290 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11291 emit_side_effect_warnings (tloc, t);
11294 last_p = tsi_stmt_ptr (l);
11295 last = *last_p;
11298 /* If the end of the list is exception related, then the list was split
11299 by a call to push_cleanup. Continue searching. */
11300 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11301 || TREE_CODE (last) == TRY_CATCH_EXPR)
11303 last_p = &TREE_OPERAND (last, 0);
11304 last = *last_p;
11305 goto continue_searching;
11308 if (last == error_mark_node)
11309 return last;
11311 /* In the case that the BIND_EXPR is not necessary, return the
11312 expression out from inside it. */
11313 if ((last == BIND_EXPR_BODY (body)
11314 /* Skip nested debug stmts. */
11315 || last == expr_first (BIND_EXPR_BODY (body)))
11316 && BIND_EXPR_VARS (body) == NULL)
11318 /* Even if this looks constant, do not allow it in a constant
11319 expression. */
11320 last = c_wrap_maybe_const (last, true);
11321 /* Do not warn if the return value of a statement expression is
11322 unused. */
11323 TREE_NO_WARNING (last) = 1;
11324 return last;
11327 /* Extract the type of said expression. */
11328 type = TREE_TYPE (last);
11330 /* If we're not returning a value at all, then the BIND_EXPR that
11331 we already have is a fine expression to return. */
11332 if (!type || VOID_TYPE_P (type))
11333 return body;
11335 /* Now that we've located the expression containing the value, it seems
11336 silly to make voidify_wrapper_expr repeat the process. Create a
11337 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11338 tmp = create_tmp_var_raw (type);
11340 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11341 tree_expr_nonnegative_p giving up immediately. */
11342 val = last;
11343 if (TREE_CODE (val) == NOP_EXPR
11344 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11345 val = TREE_OPERAND (val, 0);
11347 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11348 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11351 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11352 SET_EXPR_LOCATION (t, loc);
11353 return t;
11357 /* Begin and end compound statements. This is as simple as pushing
11358 and popping new statement lists from the tree. */
11360 tree
11361 c_begin_compound_stmt (bool do_scope)
11363 tree stmt = push_stmt_list ();
11364 if (do_scope)
11365 push_scope ();
11366 return stmt;
11369 /* End a compound statement. STMT is the statement. LOC is the
11370 location of the compound statement-- this is usually the location
11371 of the opening brace. */
11373 tree
11374 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11376 tree block = NULL;
11378 if (do_scope)
11380 if (c_dialect_objc ())
11381 objc_clear_super_receiver ();
11382 block = pop_scope ();
11385 stmt = pop_stmt_list (stmt);
11386 stmt = c_build_bind_expr (loc, block, stmt);
11388 /* If this compound statement is nested immediately inside a statement
11389 expression, then force a BIND_EXPR to be created. Otherwise we'll
11390 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11391 STATEMENT_LISTs merge, and thus we can lose track of what statement
11392 was really last. */
11393 if (building_stmt_list_p ()
11394 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11395 && TREE_CODE (stmt) != BIND_EXPR)
11397 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11398 TREE_SIDE_EFFECTS (stmt) = 1;
11399 SET_EXPR_LOCATION (stmt, loc);
11402 return stmt;
11405 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11406 when the current scope is exited. EH_ONLY is true when this is not
11407 meant to apply to normal control flow transfer. */
11409 void
11410 push_cleanup (tree decl, tree cleanup, bool eh_only)
11412 enum tree_code code;
11413 tree stmt, list;
11414 bool stmt_expr;
11416 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11417 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11418 add_stmt (stmt);
11419 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11420 list = push_stmt_list ();
11421 TREE_OPERAND (stmt, 0) = list;
11422 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11425 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11426 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11428 static tree
11429 build_vec_cmp (tree_code code, tree type,
11430 tree arg0, tree arg1)
11432 tree zero_vec = build_zero_cst (type);
11433 tree minus_one_vec = build_minus_one_cst (type);
11434 tree cmp_type = truth_type_for (type);
11435 tree cmp = build2 (code, cmp_type, arg0, arg1);
11436 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11439 /* Build a binary-operation expression without default conversions.
11440 CODE is the kind of expression to build.
11441 LOCATION is the operator's location.
11442 This function differs from `build' in several ways:
11443 the data type of the result is computed and recorded in it,
11444 warnings are generated if arg data types are invalid,
11445 special handling for addition and subtraction of pointers is known,
11446 and some optimization is done (operations on narrow ints
11447 are done in the narrower type when that gives the same result).
11448 Constant folding is also done before the result is returned.
11450 Note that the operands will never have enumeral types, or function
11451 or array types, because either they will have the default conversions
11452 performed or they have both just been converted to some other type in which
11453 the arithmetic is to be done. */
11455 tree
11456 build_binary_op (location_t location, enum tree_code code,
11457 tree orig_op0, tree orig_op1, bool convert_p)
11459 tree type0, type1, orig_type0, orig_type1;
11460 tree eptype;
11461 enum tree_code code0, code1;
11462 tree op0, op1;
11463 tree ret = error_mark_node;
11464 const char *invalid_op_diag;
11465 bool op0_int_operands, op1_int_operands;
11466 bool int_const, int_const_or_overflow, int_operands;
11468 /* Expression code to give to the expression when it is built.
11469 Normally this is CODE, which is what the caller asked for,
11470 but in some special cases we change it. */
11471 enum tree_code resultcode = code;
11473 /* Data type in which the computation is to be performed.
11474 In the simplest cases this is the common type of the arguments. */
11475 tree result_type = NULL;
11477 /* When the computation is in excess precision, the type of the
11478 final EXCESS_PRECISION_EXPR. */
11479 tree semantic_result_type = NULL;
11481 /* Nonzero means operands have already been type-converted
11482 in whatever way is necessary.
11483 Zero means they need to be converted to RESULT_TYPE. */
11484 int converted = 0;
11486 /* Nonzero means create the expression with this type, rather than
11487 RESULT_TYPE. */
11488 tree build_type = NULL_TREE;
11490 /* Nonzero means after finally constructing the expression
11491 convert it to this type. */
11492 tree final_type = NULL_TREE;
11494 /* Nonzero if this is an operation like MIN or MAX which can
11495 safely be computed in short if both args are promoted shorts.
11496 Also implies COMMON.
11497 -1 indicates a bitwise operation; this makes a difference
11498 in the exact conditions for when it is safe to do the operation
11499 in a narrower mode. */
11500 int shorten = 0;
11502 /* Nonzero if this is a comparison operation;
11503 if both args are promoted shorts, compare the original shorts.
11504 Also implies COMMON. */
11505 int short_compare = 0;
11507 /* Nonzero if this is a right-shift operation, which can be computed on the
11508 original short and then promoted if the operand is a promoted short. */
11509 int short_shift = 0;
11511 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11512 int common = 0;
11514 /* True means types are compatible as far as ObjC is concerned. */
11515 bool objc_ok;
11517 /* True means this is an arithmetic operation that may need excess
11518 precision. */
11519 bool may_need_excess_precision;
11521 /* True means this is a boolean operation that converts both its
11522 operands to truth-values. */
11523 bool boolean_op = false;
11525 /* Remember whether we're doing / or %. */
11526 bool doing_div_or_mod = false;
11528 /* Remember whether we're doing << or >>. */
11529 bool doing_shift = false;
11531 /* Tree holding instrumentation expression. */
11532 tree instrument_expr = NULL;
11534 if (location == UNKNOWN_LOCATION)
11535 location = input_location;
11537 op0 = orig_op0;
11538 op1 = orig_op1;
11540 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11541 if (op0_int_operands)
11542 op0 = remove_c_maybe_const_expr (op0);
11543 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11544 if (op1_int_operands)
11545 op1 = remove_c_maybe_const_expr (op1);
11546 int_operands = (op0_int_operands && op1_int_operands);
11547 if (int_operands)
11549 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11550 && TREE_CODE (orig_op1) == INTEGER_CST);
11551 int_const = (int_const_or_overflow
11552 && !TREE_OVERFLOW (orig_op0)
11553 && !TREE_OVERFLOW (orig_op1));
11555 else
11556 int_const = int_const_or_overflow = false;
11558 /* Do not apply default conversion in mixed vector/scalar expression. */
11559 if (convert_p
11560 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11562 op0 = default_conversion (op0);
11563 op1 = default_conversion (op1);
11566 orig_type0 = type0 = TREE_TYPE (op0);
11568 orig_type1 = type1 = TREE_TYPE (op1);
11570 /* The expression codes of the data types of the arguments tell us
11571 whether the arguments are integers, floating, pointers, etc. */
11572 code0 = TREE_CODE (type0);
11573 code1 = TREE_CODE (type1);
11575 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11576 STRIP_TYPE_NOPS (op0);
11577 STRIP_TYPE_NOPS (op1);
11579 /* If an error was already reported for one of the arguments,
11580 avoid reporting another error. */
11582 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11583 return error_mark_node;
11585 if (code0 == POINTER_TYPE
11586 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11587 return error_mark_node;
11589 if (code1 == POINTER_TYPE
11590 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11591 return error_mark_node;
11593 if ((invalid_op_diag
11594 = targetm.invalid_binary_op (code, type0, type1)))
11596 error_at (location, invalid_op_diag);
11597 return error_mark_node;
11600 switch (code)
11602 case PLUS_EXPR:
11603 case MINUS_EXPR:
11604 case MULT_EXPR:
11605 case TRUNC_DIV_EXPR:
11606 case CEIL_DIV_EXPR:
11607 case FLOOR_DIV_EXPR:
11608 case ROUND_DIV_EXPR:
11609 case EXACT_DIV_EXPR:
11610 may_need_excess_precision = true;
11611 break;
11613 case EQ_EXPR:
11614 case NE_EXPR:
11615 case LE_EXPR:
11616 case GE_EXPR:
11617 case LT_EXPR:
11618 case GT_EXPR:
11619 /* Excess precision for implicit conversions of integers to
11620 floating point in C11 and later. */
11621 may_need_excess_precision = (flag_isoc11
11622 && (ANY_INTEGRAL_TYPE_P (type0)
11623 || ANY_INTEGRAL_TYPE_P (type1)));
11624 break;
11626 default:
11627 may_need_excess_precision = false;
11628 break;
11630 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11632 op0 = TREE_OPERAND (op0, 0);
11633 type0 = TREE_TYPE (op0);
11635 else if (may_need_excess_precision
11636 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11638 type0 = eptype;
11639 op0 = convert (eptype, op0);
11641 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11643 op1 = TREE_OPERAND (op1, 0);
11644 type1 = TREE_TYPE (op1);
11646 else if (may_need_excess_precision
11647 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11649 type1 = eptype;
11650 op1 = convert (eptype, op1);
11653 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11655 /* In case when one of the operands of the binary operation is
11656 a vector and another is a scalar -- convert scalar to vector. */
11657 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11658 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11660 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11661 true);
11663 switch (convert_flag)
11665 case stv_error:
11666 return error_mark_node;
11667 case stv_firstarg:
11669 bool maybe_const = true;
11670 tree sc;
11671 sc = c_fully_fold (op0, false, &maybe_const);
11672 sc = save_expr (sc);
11673 sc = convert (TREE_TYPE (type1), sc);
11674 op0 = build_vector_from_val (type1, sc);
11675 if (!maybe_const)
11676 op0 = c_wrap_maybe_const (op0, true);
11677 orig_type0 = type0 = TREE_TYPE (op0);
11678 code0 = TREE_CODE (type0);
11679 converted = 1;
11680 break;
11682 case stv_secondarg:
11684 bool maybe_const = true;
11685 tree sc;
11686 sc = c_fully_fold (op1, false, &maybe_const);
11687 sc = save_expr (sc);
11688 sc = convert (TREE_TYPE (type0), sc);
11689 op1 = build_vector_from_val (type0, sc);
11690 if (!maybe_const)
11691 op1 = c_wrap_maybe_const (op1, true);
11692 orig_type1 = type1 = TREE_TYPE (op1);
11693 code1 = TREE_CODE (type1);
11694 converted = 1;
11695 break;
11697 default:
11698 break;
11702 switch (code)
11704 case PLUS_EXPR:
11705 /* Handle the pointer + int case. */
11706 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11708 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11709 goto return_build_binary_op;
11711 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11713 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11714 goto return_build_binary_op;
11716 else
11717 common = 1;
11718 break;
11720 case MINUS_EXPR:
11721 /* Subtraction of two similar pointers.
11722 We must subtract them as integers, then divide by object size. */
11723 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11724 && comp_target_types (location, type0, type1))
11726 ret = pointer_diff (location, op0, op1, &instrument_expr);
11727 goto return_build_binary_op;
11729 /* Handle pointer minus int. Just like pointer plus int. */
11730 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11732 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11733 goto return_build_binary_op;
11735 else
11736 common = 1;
11737 break;
11739 case MULT_EXPR:
11740 common = 1;
11741 break;
11743 case TRUNC_DIV_EXPR:
11744 case CEIL_DIV_EXPR:
11745 case FLOOR_DIV_EXPR:
11746 case ROUND_DIV_EXPR:
11747 case EXACT_DIV_EXPR:
11748 doing_div_or_mod = true;
11749 warn_for_div_by_zero (location, op1);
11751 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11752 || code0 == FIXED_POINT_TYPE
11753 || code0 == COMPLEX_TYPE
11754 || gnu_vector_type_p (type0))
11755 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11756 || code1 == FIXED_POINT_TYPE
11757 || code1 == COMPLEX_TYPE
11758 || gnu_vector_type_p (type1)))
11760 enum tree_code tcode0 = code0, tcode1 = code1;
11762 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11763 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11764 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11765 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11767 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11768 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11769 resultcode = RDIV_EXPR;
11770 else
11771 /* Although it would be tempting to shorten always here, that
11772 loses on some targets, since the modulo instruction is
11773 undefined if the quotient can't be represented in the
11774 computation mode. We shorten only if unsigned or if
11775 dividing by something we know != -1. */
11776 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11777 || (TREE_CODE (op1) == INTEGER_CST
11778 && !integer_all_onesp (op1)));
11779 common = 1;
11781 break;
11783 case BIT_AND_EXPR:
11784 case BIT_IOR_EXPR:
11785 case BIT_XOR_EXPR:
11786 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11787 shorten = -1;
11788 /* Allow vector types which are not floating point types. */
11789 else if (gnu_vector_type_p (type0)
11790 && gnu_vector_type_p (type1)
11791 && !VECTOR_FLOAT_TYPE_P (type0)
11792 && !VECTOR_FLOAT_TYPE_P (type1))
11793 common = 1;
11794 break;
11796 case TRUNC_MOD_EXPR:
11797 case FLOOR_MOD_EXPR:
11798 doing_div_or_mod = true;
11799 warn_for_div_by_zero (location, op1);
11801 if (gnu_vector_type_p (type0)
11802 && gnu_vector_type_p (type1)
11803 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11804 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11805 common = 1;
11806 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11808 /* Although it would be tempting to shorten always here, that loses
11809 on some targets, since the modulo instruction is undefined if the
11810 quotient can't be represented in the computation mode. We shorten
11811 only if unsigned or if dividing by something we know != -1. */
11812 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11813 || (TREE_CODE (op1) == INTEGER_CST
11814 && !integer_all_onesp (op1)));
11815 common = 1;
11817 break;
11819 case TRUTH_ANDIF_EXPR:
11820 case TRUTH_ORIF_EXPR:
11821 case TRUTH_AND_EXPR:
11822 case TRUTH_OR_EXPR:
11823 case TRUTH_XOR_EXPR:
11824 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11825 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11826 || code0 == FIXED_POINT_TYPE)
11827 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11828 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11829 || code1 == FIXED_POINT_TYPE))
11831 /* Result of these operations is always an int,
11832 but that does not mean the operands should be
11833 converted to ints! */
11834 result_type = integer_type_node;
11835 if (op0_int_operands)
11837 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11838 op0 = remove_c_maybe_const_expr (op0);
11840 else
11841 op0 = c_objc_common_truthvalue_conversion (location, op0);
11842 if (op1_int_operands)
11844 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11845 op1 = remove_c_maybe_const_expr (op1);
11847 else
11848 op1 = c_objc_common_truthvalue_conversion (location, op1);
11849 converted = 1;
11850 boolean_op = true;
11852 if (code == TRUTH_ANDIF_EXPR)
11854 int_const_or_overflow = (int_operands
11855 && TREE_CODE (orig_op0) == INTEGER_CST
11856 && (op0 == truthvalue_false_node
11857 || TREE_CODE (orig_op1) == INTEGER_CST));
11858 int_const = (int_const_or_overflow
11859 && !TREE_OVERFLOW (orig_op0)
11860 && (op0 == truthvalue_false_node
11861 || !TREE_OVERFLOW (orig_op1)));
11863 else if (code == TRUTH_ORIF_EXPR)
11865 int_const_or_overflow = (int_operands
11866 && TREE_CODE (orig_op0) == INTEGER_CST
11867 && (op0 == truthvalue_true_node
11868 || TREE_CODE (orig_op1) == INTEGER_CST));
11869 int_const = (int_const_or_overflow
11870 && !TREE_OVERFLOW (orig_op0)
11871 && (op0 == truthvalue_true_node
11872 || !TREE_OVERFLOW (orig_op1)));
11874 break;
11876 /* Shift operations: result has same type as first operand;
11877 always convert second operand to int.
11878 Also set SHORT_SHIFT if shifting rightward. */
11880 case RSHIFT_EXPR:
11881 if (gnu_vector_type_p (type0)
11882 && gnu_vector_type_p (type1)
11883 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11884 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11885 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11886 TYPE_VECTOR_SUBPARTS (type1)))
11888 result_type = type0;
11889 converted = 1;
11891 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11892 || (gnu_vector_type_p (type0)
11893 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11894 && code1 == INTEGER_TYPE)
11896 doing_shift = true;
11897 if (TREE_CODE (op1) == INTEGER_CST)
11899 if (tree_int_cst_sgn (op1) < 0)
11901 int_const = false;
11902 if (c_inhibit_evaluation_warnings == 0)
11903 warning_at (location, OPT_Wshift_count_negative,
11904 "right shift count is negative");
11906 else if (code0 == VECTOR_TYPE)
11908 if (compare_tree_int (op1,
11909 TYPE_PRECISION (TREE_TYPE (type0)))
11910 >= 0)
11912 int_const = false;
11913 if (c_inhibit_evaluation_warnings == 0)
11914 warning_at (location, OPT_Wshift_count_overflow,
11915 "right shift count >= width of vector element");
11918 else
11920 if (!integer_zerop (op1))
11921 short_shift = 1;
11923 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11925 int_const = false;
11926 if (c_inhibit_evaluation_warnings == 0)
11927 warning_at (location, OPT_Wshift_count_overflow,
11928 "right shift count >= width of type");
11933 /* Use the type of the value to be shifted. */
11934 result_type = type0;
11935 /* Avoid converting op1 to result_type later. */
11936 converted = 1;
11938 break;
11940 case LSHIFT_EXPR:
11941 if (gnu_vector_type_p (type0)
11942 && gnu_vector_type_p (type1)
11943 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11944 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11945 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11946 TYPE_VECTOR_SUBPARTS (type1)))
11948 result_type = type0;
11949 converted = 1;
11951 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11952 || (gnu_vector_type_p (type0)
11953 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11954 && code1 == INTEGER_TYPE)
11956 doing_shift = true;
11957 if (TREE_CODE (op0) == INTEGER_CST
11958 && tree_int_cst_sgn (op0) < 0)
11960 /* Don't reject a left shift of a negative value in a context
11961 where a constant expression is needed in C90. */
11962 if (flag_isoc99)
11963 int_const = false;
11964 if (c_inhibit_evaluation_warnings == 0)
11965 warning_at (location, OPT_Wshift_negative_value,
11966 "left shift of negative value");
11968 if (TREE_CODE (op1) == INTEGER_CST)
11970 if (tree_int_cst_sgn (op1) < 0)
11972 int_const = false;
11973 if (c_inhibit_evaluation_warnings == 0)
11974 warning_at (location, OPT_Wshift_count_negative,
11975 "left shift count is negative");
11977 else if (code0 == VECTOR_TYPE)
11979 if (compare_tree_int (op1,
11980 TYPE_PRECISION (TREE_TYPE (type0)))
11981 >= 0)
11983 int_const = false;
11984 if (c_inhibit_evaluation_warnings == 0)
11985 warning_at (location, OPT_Wshift_count_overflow,
11986 "left shift count >= width of vector element");
11989 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11991 int_const = false;
11992 if (c_inhibit_evaluation_warnings == 0)
11993 warning_at (location, OPT_Wshift_count_overflow,
11994 "left shift count >= width of type");
11996 else if (TREE_CODE (op0) == INTEGER_CST
11997 && maybe_warn_shift_overflow (location, op0, op1)
11998 && flag_isoc99)
11999 int_const = false;
12002 /* Use the type of the value to be shifted. */
12003 result_type = type0;
12004 /* Avoid converting op1 to result_type later. */
12005 converted = 1;
12007 break;
12009 case EQ_EXPR:
12010 case NE_EXPR:
12011 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12013 tree intt;
12014 if (!vector_types_compatible_elements_p (type0, type1))
12016 error_at (location, "comparing vectors with different "
12017 "element types");
12018 return error_mark_node;
12021 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12022 TYPE_VECTOR_SUBPARTS (type1)))
12024 error_at (location, "comparing vectors with different "
12025 "number of elements");
12026 return error_mark_node;
12029 /* It's not precisely specified how the usual arithmetic
12030 conversions apply to the vector types. Here, we use
12031 the unsigned type if one of the operands is signed and
12032 the other one is unsigned. */
12033 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12035 if (!TYPE_UNSIGNED (type0))
12036 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12037 else
12038 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12039 warning_at (location, OPT_Wsign_compare, "comparison between "
12040 "types %qT and %qT", type0, type1);
12043 /* Always construct signed integer vector type. */
12044 intt = c_common_type_for_size (GET_MODE_BITSIZE
12045 (SCALAR_TYPE_MODE
12046 (TREE_TYPE (type0))), 0);
12047 if (!intt)
12049 error_at (location, "could not find an integer type "
12050 "of the same size as %qT",
12051 TREE_TYPE (type0));
12052 return error_mark_node;
12054 result_type = build_opaque_vector_type (intt,
12055 TYPE_VECTOR_SUBPARTS (type0));
12056 converted = 1;
12057 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12058 goto return_build_binary_op;
12060 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12061 warning_at (location,
12062 OPT_Wfloat_equal,
12063 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12064 /* Result of comparison is always int,
12065 but don't convert the args to int! */
12066 build_type = integer_type_node;
12067 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12068 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12069 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12070 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12071 short_compare = 1;
12072 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12074 if (TREE_CODE (op0) == ADDR_EXPR
12075 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
12076 && !from_macro_expansion_at (location))
12078 if (code == EQ_EXPR)
12079 warning_at (location,
12080 OPT_Waddress,
12081 "the comparison will always evaluate as %<false%> "
12082 "for the address of %qD will never be NULL",
12083 TREE_OPERAND (op0, 0));
12084 else
12085 warning_at (location,
12086 OPT_Waddress,
12087 "the comparison will always evaluate as %<true%> "
12088 "for the address of %qD will never be NULL",
12089 TREE_OPERAND (op0, 0));
12091 result_type = type0;
12093 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12095 if (TREE_CODE (op1) == ADDR_EXPR
12096 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
12097 && !from_macro_expansion_at (location))
12099 if (code == EQ_EXPR)
12100 warning_at (location,
12101 OPT_Waddress,
12102 "the comparison will always evaluate as %<false%> "
12103 "for the address of %qD will never be NULL",
12104 TREE_OPERAND (op1, 0));
12105 else
12106 warning_at (location,
12107 OPT_Waddress,
12108 "the comparison will always evaluate as %<true%> "
12109 "for the address of %qD will never be NULL",
12110 TREE_OPERAND (op1, 0));
12112 result_type = type1;
12114 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12116 tree tt0 = TREE_TYPE (type0);
12117 tree tt1 = TREE_TYPE (type1);
12118 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12119 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12120 addr_space_t as_common = ADDR_SPACE_GENERIC;
12122 /* Anything compares with void *. void * compares with anything.
12123 Otherwise, the targets must be compatible
12124 and both must be object or both incomplete. */
12125 if (comp_target_types (location, type0, type1))
12126 result_type = common_pointer_type (type0, type1);
12127 else if (!addr_space_superset (as0, as1, &as_common))
12129 error_at (location, "comparison of pointers to "
12130 "disjoint address spaces");
12131 return error_mark_node;
12133 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12135 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12136 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12137 "comparison of %<void *%> with function pointer");
12139 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12141 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12142 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12143 "comparison of %<void *%> with function pointer");
12145 else
12146 /* Avoid warning about the volatile ObjC EH puts on decls. */
12147 if (!objc_ok)
12148 pedwarn (location, 0,
12149 "comparison of distinct pointer types lacks a cast");
12151 if (result_type == NULL_TREE)
12153 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12154 result_type = build_pointer_type
12155 (build_qualified_type (void_type_node, qual));
12158 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12160 result_type = type0;
12161 pedwarn (location, 0, "comparison between pointer and integer");
12163 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12165 result_type = type1;
12166 pedwarn (location, 0, "comparison between pointer and integer");
12168 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12169 || truth_value_p (TREE_CODE (orig_op0)))
12170 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12171 || truth_value_p (TREE_CODE (orig_op1))))
12172 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12173 break;
12175 case LE_EXPR:
12176 case GE_EXPR:
12177 case LT_EXPR:
12178 case GT_EXPR:
12179 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12181 tree intt;
12182 if (!vector_types_compatible_elements_p (type0, type1))
12184 error_at (location, "comparing vectors with different "
12185 "element types");
12186 return error_mark_node;
12189 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12190 TYPE_VECTOR_SUBPARTS (type1)))
12192 error_at (location, "comparing vectors with different "
12193 "number of elements");
12194 return error_mark_node;
12197 /* It's not precisely specified how the usual arithmetic
12198 conversions apply to the vector types. Here, we use
12199 the unsigned type if one of the operands is signed and
12200 the other one is unsigned. */
12201 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12203 if (!TYPE_UNSIGNED (type0))
12204 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12205 else
12206 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12207 warning_at (location, OPT_Wsign_compare, "comparison between "
12208 "types %qT and %qT", type0, type1);
12211 /* Always construct signed integer vector type. */
12212 intt = c_common_type_for_size (GET_MODE_BITSIZE
12213 (SCALAR_TYPE_MODE
12214 (TREE_TYPE (type0))), 0);
12215 if (!intt)
12217 error_at (location, "could not find an integer type "
12218 "of the same size as %qT",
12219 TREE_TYPE (type0));
12220 return error_mark_node;
12222 result_type = build_opaque_vector_type (intt,
12223 TYPE_VECTOR_SUBPARTS (type0));
12224 converted = 1;
12225 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12226 goto return_build_binary_op;
12228 build_type = integer_type_node;
12229 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12230 || code0 == FIXED_POINT_TYPE)
12231 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12232 || code1 == FIXED_POINT_TYPE))
12233 short_compare = 1;
12234 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12236 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12237 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12238 addr_space_t as_common;
12240 if (comp_target_types (location, type0, type1))
12242 result_type = common_pointer_type (type0, type1);
12243 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12244 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12245 pedwarn (location, 0,
12246 "comparison of complete and incomplete pointers");
12247 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12248 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12249 "ordered comparisons of pointers to functions");
12250 else if (null_pointer_constant_p (orig_op0)
12251 || null_pointer_constant_p (orig_op1))
12252 warning_at (location, OPT_Wextra,
12253 "ordered comparison of pointer with null pointer");
12256 else if (!addr_space_superset (as0, as1, &as_common))
12258 error_at (location, "comparison of pointers to "
12259 "disjoint address spaces");
12260 return error_mark_node;
12262 else
12264 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12265 result_type = build_pointer_type
12266 (build_qualified_type (void_type_node, qual));
12267 pedwarn (location, 0,
12268 "comparison of distinct pointer types lacks a cast");
12271 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12273 result_type = type0;
12274 if (pedantic)
12275 pedwarn (location, OPT_Wpedantic,
12276 "ordered comparison of pointer with integer zero");
12277 else if (extra_warnings)
12278 warning_at (location, OPT_Wextra,
12279 "ordered comparison of pointer with integer zero");
12281 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12283 result_type = type1;
12284 if (pedantic)
12285 pedwarn (location, OPT_Wpedantic,
12286 "ordered comparison of pointer with integer zero");
12287 else if (extra_warnings)
12288 warning_at (location, OPT_Wextra,
12289 "ordered comparison of pointer with integer zero");
12291 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12293 result_type = type0;
12294 pedwarn (location, 0, "comparison between pointer and integer");
12296 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12298 result_type = type1;
12299 pedwarn (location, 0, "comparison between pointer and integer");
12302 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12303 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12305 op0 = save_expr (op0);
12306 op1 = save_expr (op1);
12308 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12309 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12312 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12313 || truth_value_p (TREE_CODE (orig_op0)))
12314 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12315 || truth_value_p (TREE_CODE (orig_op1))))
12316 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12317 break;
12319 default:
12320 gcc_unreachable ();
12323 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12324 return error_mark_node;
12326 if (gnu_vector_type_p (type0)
12327 && gnu_vector_type_p (type1)
12328 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12329 || !vector_types_compatible_elements_p (type0, type1)))
12331 gcc_rich_location richloc (location);
12332 maybe_range_label_for_tree_type_mismatch
12333 label_for_op0 (orig_op0, orig_op1),
12334 label_for_op1 (orig_op1, orig_op0);
12335 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12336 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12337 binary_op_error (&richloc, code, type0, type1);
12338 return error_mark_node;
12341 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12342 || code0 == FIXED_POINT_TYPE
12343 || gnu_vector_type_p (type0))
12345 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12346 || code1 == FIXED_POINT_TYPE
12347 || gnu_vector_type_p (type1)))
12349 bool first_complex = (code0 == COMPLEX_TYPE);
12350 bool second_complex = (code1 == COMPLEX_TYPE);
12351 int none_complex = (!first_complex && !second_complex);
12353 if (shorten || common || short_compare)
12355 result_type = c_common_type (type0, type1);
12356 do_warn_double_promotion (result_type, type0, type1,
12357 "implicit conversion from %qT to %qT "
12358 "to match other operand of binary "
12359 "expression",
12360 location);
12361 if (result_type == error_mark_node)
12362 return error_mark_node;
12365 if (first_complex != second_complex
12366 && (code == PLUS_EXPR
12367 || code == MINUS_EXPR
12368 || code == MULT_EXPR
12369 || (code == TRUNC_DIV_EXPR && first_complex))
12370 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12371 && flag_signed_zeros)
12373 /* An operation on mixed real/complex operands must be
12374 handled specially, but the language-independent code can
12375 more easily optimize the plain complex arithmetic if
12376 -fno-signed-zeros. */
12377 tree real_type = TREE_TYPE (result_type);
12378 tree real, imag;
12379 if (type0 != orig_type0 || type1 != orig_type1)
12381 gcc_assert (may_need_excess_precision && common);
12382 semantic_result_type = c_common_type (orig_type0, orig_type1);
12384 if (first_complex)
12386 if (TREE_TYPE (op0) != result_type)
12387 op0 = convert_and_check (location, result_type, op0);
12388 if (TREE_TYPE (op1) != real_type)
12389 op1 = convert_and_check (location, real_type, op1);
12391 else
12393 if (TREE_TYPE (op0) != real_type)
12394 op0 = convert_and_check (location, real_type, op0);
12395 if (TREE_TYPE (op1) != result_type)
12396 op1 = convert_and_check (location, result_type, op1);
12398 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12399 return error_mark_node;
12400 if (first_complex)
12402 op0 = save_expr (op0);
12403 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12404 op0, true);
12405 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12406 op0, true);
12407 switch (code)
12409 case MULT_EXPR:
12410 case TRUNC_DIV_EXPR:
12411 op1 = save_expr (op1);
12412 imag = build2 (resultcode, real_type, imag, op1);
12413 /* Fall through. */
12414 case PLUS_EXPR:
12415 case MINUS_EXPR:
12416 real = build2 (resultcode, real_type, real, op1);
12417 break;
12418 default:
12419 gcc_unreachable();
12422 else
12424 op1 = save_expr (op1);
12425 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12426 op1, true);
12427 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12428 op1, true);
12429 switch (code)
12431 case MULT_EXPR:
12432 op0 = save_expr (op0);
12433 imag = build2 (resultcode, real_type, op0, imag);
12434 /* Fall through. */
12435 case PLUS_EXPR:
12436 real = build2 (resultcode, real_type, op0, real);
12437 break;
12438 case MINUS_EXPR:
12439 real = build2 (resultcode, real_type, op0, real);
12440 imag = build1 (NEGATE_EXPR, real_type, imag);
12441 break;
12442 default:
12443 gcc_unreachable();
12446 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12447 goto return_build_binary_op;
12450 /* For certain operations (which identify themselves by shorten != 0)
12451 if both args were extended from the same smaller type,
12452 do the arithmetic in that type and then extend.
12454 shorten !=0 and !=1 indicates a bitwise operation.
12455 For them, this optimization is safe only if
12456 both args are zero-extended or both are sign-extended.
12457 Otherwise, we might change the result.
12458 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12459 but calculated in (unsigned short) it would be (unsigned short)-1. */
12461 if (shorten && none_complex)
12463 final_type = result_type;
12464 result_type = shorten_binary_op (result_type, op0, op1,
12465 shorten == -1);
12468 /* Shifts can be shortened if shifting right. */
12470 if (short_shift)
12472 int unsigned_arg;
12473 tree arg0 = get_narrower (op0, &unsigned_arg);
12475 final_type = result_type;
12477 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12478 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12480 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12481 && tree_int_cst_sgn (op1) > 0
12482 /* We can shorten only if the shift count is less than the
12483 number of bits in the smaller type size. */
12484 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12485 /* We cannot drop an unsigned shift after sign-extension. */
12486 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12488 /* Do an unsigned shift if the operand was zero-extended. */
12489 result_type
12490 = c_common_signed_or_unsigned_type (unsigned_arg,
12491 TREE_TYPE (arg0));
12492 /* Convert value-to-be-shifted to that type. */
12493 if (TREE_TYPE (op0) != result_type)
12494 op0 = convert (result_type, op0);
12495 converted = 1;
12499 /* Comparison operations are shortened too but differently.
12500 They identify themselves by setting short_compare = 1. */
12502 if (short_compare)
12504 /* Don't write &op0, etc., because that would prevent op0
12505 from being kept in a register.
12506 Instead, make copies of the our local variables and
12507 pass the copies by reference, then copy them back afterward. */
12508 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12509 enum tree_code xresultcode = resultcode;
12510 tree val
12511 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12512 &xresultcode);
12514 if (val != NULL_TREE)
12516 ret = val;
12517 goto return_build_binary_op;
12520 op0 = xop0, op1 = xop1;
12521 converted = 1;
12522 resultcode = xresultcode;
12524 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12526 bool op0_maybe_const = true;
12527 bool op1_maybe_const = true;
12528 tree orig_op0_folded, orig_op1_folded;
12530 if (in_late_binary_op)
12532 orig_op0_folded = orig_op0;
12533 orig_op1_folded = orig_op1;
12535 else
12537 /* Fold for the sake of possible warnings, as in
12538 build_conditional_expr. This requires the
12539 "original" values to be folded, not just op0 and
12540 op1. */
12541 c_inhibit_evaluation_warnings++;
12542 op0 = c_fully_fold (op0, require_constant_value,
12543 &op0_maybe_const);
12544 op1 = c_fully_fold (op1, require_constant_value,
12545 &op1_maybe_const);
12546 c_inhibit_evaluation_warnings--;
12547 orig_op0_folded = c_fully_fold (orig_op0,
12548 require_constant_value,
12549 NULL);
12550 orig_op1_folded = c_fully_fold (orig_op1,
12551 require_constant_value,
12552 NULL);
12555 if (warn_sign_compare)
12556 warn_for_sign_compare (location, orig_op0_folded,
12557 orig_op1_folded, op0, op1,
12558 result_type, resultcode);
12559 if (!in_late_binary_op && !int_operands)
12561 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12562 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12563 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12564 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12570 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12571 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12572 Then the expression will be built.
12573 It will be given type FINAL_TYPE if that is nonzero;
12574 otherwise, it will be given type RESULT_TYPE. */
12576 if (!result_type)
12578 /* Favor showing any expression locations that are available. */
12579 op_location_t oploc (location, UNKNOWN_LOCATION);
12580 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12581 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12582 return error_mark_node;
12585 if (build_type == NULL_TREE)
12587 build_type = result_type;
12588 if ((type0 != orig_type0 || type1 != orig_type1)
12589 && !boolean_op)
12591 gcc_assert (may_need_excess_precision && common);
12592 semantic_result_type = c_common_type (orig_type0, orig_type1);
12596 if (!converted)
12598 op0 = ep_convert_and_check (location, result_type, op0,
12599 semantic_result_type);
12600 op1 = ep_convert_and_check (location, result_type, op1,
12601 semantic_result_type);
12603 /* This can happen if one operand has a vector type, and the other
12604 has a different type. */
12605 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12606 return error_mark_node;
12609 if (sanitize_flags_p ((SANITIZE_SHIFT
12610 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12611 && current_function_decl != NULL_TREE
12612 && (doing_div_or_mod || doing_shift)
12613 && !require_constant_value)
12615 /* OP0 and/or OP1 might have side-effects. */
12616 op0 = save_expr (op0);
12617 op1 = save_expr (op1);
12618 op0 = c_fully_fold (op0, false, NULL);
12619 op1 = c_fully_fold (op1, false, NULL);
12620 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12621 | SANITIZE_FLOAT_DIVIDE))))
12622 instrument_expr = ubsan_instrument_division (location, op0, op1);
12623 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12624 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12627 /* Treat expressions in initializers specially as they can't trap. */
12628 if (int_const_or_overflow)
12629 ret = (require_constant_value
12630 ? fold_build2_initializer_loc (location, resultcode, build_type,
12631 op0, op1)
12632 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12633 else
12634 ret = build2 (resultcode, build_type, op0, op1);
12635 if (final_type != NULL_TREE)
12636 ret = convert (final_type, ret);
12638 return_build_binary_op:
12639 gcc_assert (ret != error_mark_node);
12640 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12641 ret = (int_operands
12642 ? note_integer_operands (ret)
12643 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12644 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12645 && !in_late_binary_op)
12646 ret = note_integer_operands (ret);
12647 protected_set_expr_location (ret, location);
12649 if (instrument_expr != NULL)
12650 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12651 instrument_expr, ret);
12653 if (semantic_result_type)
12654 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12655 semantic_result_type, ret);
12657 return ret;
12661 /* Convert EXPR to be a truth-value, validating its type for this
12662 purpose. LOCATION is the source location for the expression. */
12664 tree
12665 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12667 bool int_const, int_operands;
12669 switch (TREE_CODE (TREE_TYPE (expr)))
12671 case ARRAY_TYPE:
12672 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12673 return error_mark_node;
12675 case RECORD_TYPE:
12676 error_at (location, "used struct type value where scalar is required");
12677 return error_mark_node;
12679 case UNION_TYPE:
12680 error_at (location, "used union type value where scalar is required");
12681 return error_mark_node;
12683 case VOID_TYPE:
12684 error_at (location, "void value not ignored as it ought to be");
12685 return error_mark_node;
12687 case POINTER_TYPE:
12688 if (reject_gcc_builtin (expr))
12689 return error_mark_node;
12690 break;
12692 case FUNCTION_TYPE:
12693 gcc_unreachable ();
12695 case VECTOR_TYPE:
12696 error_at (location, "used vector type where scalar is required");
12697 return error_mark_node;
12699 default:
12700 break;
12703 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12704 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12705 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12707 expr = remove_c_maybe_const_expr (expr);
12708 expr = build2 (NE_EXPR, integer_type_node, expr,
12709 convert (TREE_TYPE (expr), integer_zero_node));
12710 expr = note_integer_operands (expr);
12712 else
12713 /* ??? Should we also give an error for vectors rather than leaving
12714 those to give errors later? */
12715 expr = c_common_truthvalue_conversion (location, expr);
12717 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12719 if (TREE_OVERFLOW (expr))
12720 return expr;
12721 else
12722 return note_integer_operands (expr);
12724 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12725 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12726 return expr;
12730 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12731 required. */
12733 tree
12734 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12736 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12738 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12739 /* Executing a compound literal inside a function reinitializes
12740 it. */
12741 if (!TREE_STATIC (decl))
12742 *se = true;
12743 return decl;
12745 else
12746 return expr;
12749 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12750 statement. LOC is the location of the construct. */
12752 tree
12753 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12754 tree clauses)
12756 body = c_end_compound_stmt (loc, body, true);
12758 tree stmt = make_node (code);
12759 TREE_TYPE (stmt) = void_type_node;
12760 OMP_BODY (stmt) = body;
12761 OMP_CLAUSES (stmt) = clauses;
12762 SET_EXPR_LOCATION (stmt, loc);
12764 return add_stmt (stmt);
12767 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12768 statement. LOC is the location of the OACC_DATA. */
12770 tree
12771 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12773 tree stmt;
12775 block = c_end_compound_stmt (loc, block, true);
12777 stmt = make_node (OACC_DATA);
12778 TREE_TYPE (stmt) = void_type_node;
12779 OACC_DATA_CLAUSES (stmt) = clauses;
12780 OACC_DATA_BODY (stmt) = block;
12781 SET_EXPR_LOCATION (stmt, loc);
12783 return add_stmt (stmt);
12786 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12787 statement. LOC is the location of the OACC_HOST_DATA. */
12789 tree
12790 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12792 tree stmt;
12794 block = c_end_compound_stmt (loc, block, true);
12796 stmt = make_node (OACC_HOST_DATA);
12797 TREE_TYPE (stmt) = void_type_node;
12798 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12799 OACC_HOST_DATA_BODY (stmt) = block;
12800 SET_EXPR_LOCATION (stmt, loc);
12802 return add_stmt (stmt);
12805 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12807 tree
12808 c_begin_omp_parallel (void)
12810 tree block;
12812 keep_next_level ();
12813 block = c_begin_compound_stmt (true);
12815 return block;
12818 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12819 statement. LOC is the location of the OMP_PARALLEL. */
12821 tree
12822 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12824 tree stmt;
12826 block = c_end_compound_stmt (loc, block, true);
12828 stmt = make_node (OMP_PARALLEL);
12829 TREE_TYPE (stmt) = void_type_node;
12830 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12831 OMP_PARALLEL_BODY (stmt) = block;
12832 SET_EXPR_LOCATION (stmt, loc);
12834 return add_stmt (stmt);
12837 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12839 tree
12840 c_begin_omp_task (void)
12842 tree block;
12844 keep_next_level ();
12845 block = c_begin_compound_stmt (true);
12847 return block;
12850 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12851 statement. LOC is the location of the #pragma. */
12853 tree
12854 c_finish_omp_task (location_t loc, tree clauses, tree block)
12856 tree stmt;
12858 block = c_end_compound_stmt (loc, block, true);
12860 stmt = make_node (OMP_TASK);
12861 TREE_TYPE (stmt) = void_type_node;
12862 OMP_TASK_CLAUSES (stmt) = clauses;
12863 OMP_TASK_BODY (stmt) = block;
12864 SET_EXPR_LOCATION (stmt, loc);
12866 return add_stmt (stmt);
12869 /* Generate GOMP_cancel call for #pragma omp cancel. */
12871 void
12872 c_finish_omp_cancel (location_t loc, tree clauses)
12874 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12875 int mask = 0;
12876 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12877 mask = 1;
12878 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12879 mask = 2;
12880 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12881 mask = 4;
12882 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12883 mask = 8;
12884 else
12886 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12887 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12888 "clauses");
12889 return;
12891 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12892 if (ifc != NULL_TREE)
12894 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12895 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12896 error_at (OMP_CLAUSE_LOCATION (ifc),
12897 "expected %<cancel%> %<if%> clause modifier");
12898 else
12900 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
12901 if (ifc2 != NULL_TREE)
12903 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
12904 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
12905 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
12906 error_at (OMP_CLAUSE_LOCATION (ifc2),
12907 "expected %<cancel%> %<if%> clause modifier");
12911 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12912 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12913 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12914 build_zero_cst (type));
12916 else
12917 ifc = boolean_true_node;
12918 tree stmt = build_call_expr_loc (loc, fn, 2,
12919 build_int_cst (integer_type_node, mask),
12920 ifc);
12921 add_stmt (stmt);
12924 /* Generate GOMP_cancellation_point call for
12925 #pragma omp cancellation point. */
12927 void
12928 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12930 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12931 int mask = 0;
12932 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12933 mask = 1;
12934 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12935 mask = 2;
12936 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12937 mask = 4;
12938 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12939 mask = 8;
12940 else
12942 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12943 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12944 "clauses");
12945 return;
12947 tree stmt = build_call_expr_loc (loc, fn, 1,
12948 build_int_cst (integer_type_node, mask));
12949 add_stmt (stmt);
12952 /* Helper function for handle_omp_array_sections. Called recursively
12953 to handle multiple array-section-subscripts. C is the clause,
12954 T current expression (initially OMP_CLAUSE_DECL), which is either
12955 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12956 expression if specified, TREE_VALUE length expression if specified,
12957 TREE_CHAIN is what it has been specified after, or some decl.
12958 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12959 set to true if any of the array-section-subscript could have length
12960 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12961 first array-section-subscript which is known not to have length
12962 of one. Given say:
12963 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12964 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12965 all are or may have length of 1, array-section-subscript [:2] is the
12966 first one known not to have length 1. For array-section-subscript
12967 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12968 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12969 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12970 case though, as some lengths could be zero. */
12972 static tree
12973 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12974 bool &maybe_zero_len, unsigned int &first_non_one,
12975 enum c_omp_region_type ort)
12977 tree ret, low_bound, length, type;
12978 if (TREE_CODE (t) != TREE_LIST)
12980 if (error_operand_p (t))
12981 return error_mark_node;
12982 ret = t;
12983 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12984 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12986 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12987 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12988 return error_mark_node;
12990 if (TREE_CODE (t) == COMPONENT_REF
12991 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12992 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12993 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12995 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12997 error_at (OMP_CLAUSE_LOCATION (c),
12998 "bit-field %qE in %qs clause",
12999 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13000 return error_mark_node;
13002 while (TREE_CODE (t) == COMPONENT_REF)
13004 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13006 error_at (OMP_CLAUSE_LOCATION (c),
13007 "%qE is a member of a union", t);
13008 return error_mark_node;
13010 t = TREE_OPERAND (t, 0);
13011 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13013 if (maybe_ne (mem_ref_offset (t), 0))
13014 error_at (OMP_CLAUSE_LOCATION (c),
13015 "cannot dereference %qE in %qs clause", t,
13016 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13017 else
13018 t = TREE_OPERAND (t, 0);
13022 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13024 if (DECL_P (t))
13025 error_at (OMP_CLAUSE_LOCATION (c),
13026 "%qD is not a variable in %qs clause", t,
13027 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13028 else
13029 error_at (OMP_CLAUSE_LOCATION (c),
13030 "%qE is not a variable in %qs clause", t,
13031 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13032 return error_mark_node;
13034 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13035 && TYPE_ATOMIC (TREE_TYPE (t)))
13037 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13038 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13039 return error_mark_node;
13041 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13042 && VAR_P (t)
13043 && DECL_THREAD_LOCAL_P (t))
13045 error_at (OMP_CLAUSE_LOCATION (c),
13046 "%qD is threadprivate variable in %qs clause", t,
13047 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13048 return error_mark_node;
13050 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13051 && TYPE_ATOMIC (TREE_TYPE (t))
13052 && POINTER_TYPE_P (TREE_TYPE (t)))
13054 /* If the array section is pointer based and the pointer
13055 itself is _Atomic qualified, we need to atomically load
13056 the pointer. */
13057 c_expr expr;
13058 memset (&expr, 0, sizeof (expr));
13059 expr.value = ret;
13060 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13061 expr, false, false);
13062 ret = expr.value;
13064 return ret;
13067 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13068 maybe_zero_len, first_non_one, ort);
13069 if (ret == error_mark_node || ret == NULL_TREE)
13070 return ret;
13072 type = TREE_TYPE (ret);
13073 low_bound = TREE_PURPOSE (t);
13074 length = TREE_VALUE (t);
13076 if (low_bound == error_mark_node || length == error_mark_node)
13077 return error_mark_node;
13079 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13081 error_at (OMP_CLAUSE_LOCATION (c),
13082 "low bound %qE of array section does not have integral type",
13083 low_bound);
13084 return error_mark_node;
13086 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13088 error_at (OMP_CLAUSE_LOCATION (c),
13089 "length %qE of array section does not have integral type",
13090 length);
13091 return error_mark_node;
13093 if (low_bound
13094 && TREE_CODE (low_bound) == INTEGER_CST
13095 && TYPE_PRECISION (TREE_TYPE (low_bound))
13096 > TYPE_PRECISION (sizetype))
13097 low_bound = fold_convert (sizetype, low_bound);
13098 if (length
13099 && TREE_CODE (length) == INTEGER_CST
13100 && TYPE_PRECISION (TREE_TYPE (length))
13101 > TYPE_PRECISION (sizetype))
13102 length = fold_convert (sizetype, length);
13103 if (low_bound == NULL_TREE)
13104 low_bound = integer_zero_node;
13105 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13106 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13107 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13109 if (length != integer_one_node)
13111 error_at (OMP_CLAUSE_LOCATION (c),
13112 "expected single pointer in %qs clause",
13113 c_omp_map_clause_name (c, ort == C_ORT_ACC));
13114 return error_mark_node;
13117 if (length != NULL_TREE)
13119 if (!integer_nonzerop (length))
13121 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13122 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13123 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13124 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13126 if (integer_zerop (length))
13128 error_at (OMP_CLAUSE_LOCATION (c),
13129 "zero length array section in %qs clause",
13130 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13131 return error_mark_node;
13134 else
13135 maybe_zero_len = true;
13137 if (first_non_one == types.length ()
13138 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13139 first_non_one++;
13141 if (TREE_CODE (type) == ARRAY_TYPE)
13143 if (length == NULL_TREE
13144 && (TYPE_DOMAIN (type) == NULL_TREE
13145 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13147 error_at (OMP_CLAUSE_LOCATION (c),
13148 "for unknown bound array type length expression must "
13149 "be specified");
13150 return error_mark_node;
13152 if (TREE_CODE (low_bound) == INTEGER_CST
13153 && tree_int_cst_sgn (low_bound) == -1)
13155 error_at (OMP_CLAUSE_LOCATION (c),
13156 "negative low bound in array section in %qs clause",
13157 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13158 return error_mark_node;
13160 if (length != NULL_TREE
13161 && TREE_CODE (length) == INTEGER_CST
13162 && tree_int_cst_sgn (length) == -1)
13164 error_at (OMP_CLAUSE_LOCATION (c),
13165 "negative length in array section in %qs clause",
13166 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13167 return error_mark_node;
13169 if (TYPE_DOMAIN (type)
13170 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13171 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13172 == INTEGER_CST)
13174 tree size
13175 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13176 size = size_binop (PLUS_EXPR, size, size_one_node);
13177 if (TREE_CODE (low_bound) == INTEGER_CST)
13179 if (tree_int_cst_lt (size, low_bound))
13181 error_at (OMP_CLAUSE_LOCATION (c),
13182 "low bound %qE above array section size "
13183 "in %qs clause", low_bound,
13184 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13185 return error_mark_node;
13187 if (tree_int_cst_equal (size, low_bound))
13189 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13190 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13191 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13192 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13194 error_at (OMP_CLAUSE_LOCATION (c),
13195 "zero length array section in %qs clause",
13196 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13197 return error_mark_node;
13199 maybe_zero_len = true;
13201 else if (length == NULL_TREE
13202 && first_non_one == types.length ()
13203 && tree_int_cst_equal
13204 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13205 low_bound))
13206 first_non_one++;
13208 else if (length == NULL_TREE)
13210 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13211 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13212 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13213 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13214 maybe_zero_len = true;
13215 if (first_non_one == types.length ())
13216 first_non_one++;
13218 if (length && TREE_CODE (length) == INTEGER_CST)
13220 if (tree_int_cst_lt (size, length))
13222 error_at (OMP_CLAUSE_LOCATION (c),
13223 "length %qE above array section size "
13224 "in %qs clause", length,
13225 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13226 return error_mark_node;
13228 if (TREE_CODE (low_bound) == INTEGER_CST)
13230 tree lbpluslen
13231 = size_binop (PLUS_EXPR,
13232 fold_convert (sizetype, low_bound),
13233 fold_convert (sizetype, length));
13234 if (TREE_CODE (lbpluslen) == INTEGER_CST
13235 && tree_int_cst_lt (size, lbpluslen))
13237 error_at (OMP_CLAUSE_LOCATION (c),
13238 "high bound %qE above array section size "
13239 "in %qs clause", lbpluslen,
13240 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13241 return error_mark_node;
13246 else if (length == NULL_TREE)
13248 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13249 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13250 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13251 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13252 maybe_zero_len = true;
13253 if (first_non_one == types.length ())
13254 first_non_one++;
13257 /* For [lb:] we will need to evaluate lb more than once. */
13258 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13260 tree lb = save_expr (low_bound);
13261 if (lb != low_bound)
13263 TREE_PURPOSE (t) = lb;
13264 low_bound = lb;
13268 else if (TREE_CODE (type) == POINTER_TYPE)
13270 if (length == NULL_TREE)
13272 error_at (OMP_CLAUSE_LOCATION (c),
13273 "for pointer type length expression must be specified");
13274 return error_mark_node;
13276 if (length != NULL_TREE
13277 && TREE_CODE (length) == INTEGER_CST
13278 && tree_int_cst_sgn (length) == -1)
13280 error_at (OMP_CLAUSE_LOCATION (c),
13281 "negative length in array section in %qs clause",
13282 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13283 return error_mark_node;
13285 /* If there is a pointer type anywhere but in the very first
13286 array-section-subscript, the array section can't be contiguous. */
13287 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13288 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13290 error_at (OMP_CLAUSE_LOCATION (c),
13291 "array section is not contiguous in %qs clause",
13292 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13293 return error_mark_node;
13296 else
13298 error_at (OMP_CLAUSE_LOCATION (c),
13299 "%qE does not have pointer or array type", ret);
13300 return error_mark_node;
13302 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13303 types.safe_push (TREE_TYPE (ret));
13304 /* We will need to evaluate lb more than once. */
13305 tree lb = save_expr (low_bound);
13306 if (lb != low_bound)
13308 TREE_PURPOSE (t) = lb;
13309 low_bound = lb;
13311 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13312 return ret;
13315 /* Handle array sections for clause C. */
13317 static bool
13318 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13320 bool maybe_zero_len = false;
13321 unsigned int first_non_one = 0;
13322 auto_vec<tree, 10> types;
13323 tree *tp = &OMP_CLAUSE_DECL (c);
13324 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13325 && TREE_CODE (*tp) == TREE_LIST
13326 && TREE_PURPOSE (*tp)
13327 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13328 tp = &TREE_VALUE (*tp);
13329 tree first = handle_omp_array_sections_1 (c, *tp, types,
13330 maybe_zero_len, first_non_one,
13331 ort);
13332 if (first == error_mark_node)
13333 return true;
13334 if (first == NULL_TREE)
13335 return false;
13336 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13338 tree t = *tp;
13339 tree tem = NULL_TREE;
13340 /* Need to evaluate side effects in the length expressions
13341 if any. */
13342 while (TREE_CODE (t) == TREE_LIST)
13344 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13346 if (tem == NULL_TREE)
13347 tem = TREE_VALUE (t);
13348 else
13349 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13350 TREE_VALUE (t), tem);
13352 t = TREE_CHAIN (t);
13354 if (tem)
13355 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13356 first = c_fully_fold (first, false, NULL, true);
13357 *tp = first;
13359 else
13361 unsigned int num = types.length (), i;
13362 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13363 tree condition = NULL_TREE;
13365 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13366 maybe_zero_len = true;
13368 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13369 t = TREE_CHAIN (t))
13371 tree low_bound = TREE_PURPOSE (t);
13372 tree length = TREE_VALUE (t);
13374 i--;
13375 if (low_bound
13376 && TREE_CODE (low_bound) == INTEGER_CST
13377 && TYPE_PRECISION (TREE_TYPE (low_bound))
13378 > TYPE_PRECISION (sizetype))
13379 low_bound = fold_convert (sizetype, low_bound);
13380 if (length
13381 && TREE_CODE (length) == INTEGER_CST
13382 && TYPE_PRECISION (TREE_TYPE (length))
13383 > TYPE_PRECISION (sizetype))
13384 length = fold_convert (sizetype, length);
13385 if (low_bound == NULL_TREE)
13386 low_bound = integer_zero_node;
13387 if (!maybe_zero_len && i > first_non_one)
13389 if (integer_nonzerop (low_bound))
13390 goto do_warn_noncontiguous;
13391 if (length != NULL_TREE
13392 && TREE_CODE (length) == INTEGER_CST
13393 && TYPE_DOMAIN (types[i])
13394 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13395 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13396 == INTEGER_CST)
13398 tree size;
13399 size = size_binop (PLUS_EXPR,
13400 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13401 size_one_node);
13402 if (!tree_int_cst_equal (length, size))
13404 do_warn_noncontiguous:
13405 error_at (OMP_CLAUSE_LOCATION (c),
13406 "array section is not contiguous in %qs "
13407 "clause",
13408 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13409 return true;
13412 if (length != NULL_TREE
13413 && TREE_SIDE_EFFECTS (length))
13415 if (side_effects == NULL_TREE)
13416 side_effects = length;
13417 else
13418 side_effects = build2 (COMPOUND_EXPR,
13419 TREE_TYPE (side_effects),
13420 length, side_effects);
13423 else
13425 tree l;
13427 if (i > first_non_one
13428 && ((length && integer_nonzerop (length))
13429 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13430 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13431 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13432 continue;
13433 if (length)
13434 l = fold_convert (sizetype, length);
13435 else
13437 l = size_binop (PLUS_EXPR,
13438 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13439 size_one_node);
13440 l = size_binop (MINUS_EXPR, l,
13441 fold_convert (sizetype, low_bound));
13443 if (i > first_non_one)
13445 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13446 size_zero_node);
13447 if (condition == NULL_TREE)
13448 condition = l;
13449 else
13450 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13451 l, condition);
13453 else if (size == NULL_TREE)
13455 size = size_in_bytes (TREE_TYPE (types[i]));
13456 tree eltype = TREE_TYPE (types[num - 1]);
13457 while (TREE_CODE (eltype) == ARRAY_TYPE)
13458 eltype = TREE_TYPE (eltype);
13459 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13460 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13461 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13463 if (integer_zerop (size)
13464 || integer_zerop (size_in_bytes (eltype)))
13466 error_at (OMP_CLAUSE_LOCATION (c),
13467 "zero length array section in %qs clause",
13468 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13469 return error_mark_node;
13471 size = size_binop (EXACT_DIV_EXPR, size,
13472 size_in_bytes (eltype));
13474 size = size_binop (MULT_EXPR, size, l);
13475 if (condition)
13476 size = fold_build3 (COND_EXPR, sizetype, condition,
13477 size, size_zero_node);
13479 else
13480 size = size_binop (MULT_EXPR, size, l);
13483 if (side_effects)
13484 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13485 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13486 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13487 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13489 size = size_binop (MINUS_EXPR, size, size_one_node);
13490 size = c_fully_fold (size, false, NULL);
13491 size = save_expr (size);
13492 tree index_type = build_index_type (size);
13493 tree eltype = TREE_TYPE (first);
13494 while (TREE_CODE (eltype) == ARRAY_TYPE)
13495 eltype = TREE_TYPE (eltype);
13496 tree type = build_array_type (eltype, index_type);
13497 tree ptype = build_pointer_type (eltype);
13498 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13499 t = build_fold_addr_expr (t);
13500 tree t2 = build_fold_addr_expr (first);
13501 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13502 ptrdiff_type_node, t2);
13503 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13504 ptrdiff_type_node, t2,
13505 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13506 ptrdiff_type_node, t));
13507 t2 = c_fully_fold (t2, false, NULL);
13508 if (tree_fits_shwi_p (t2))
13509 t = build2 (MEM_REF, type, t,
13510 build_int_cst (ptype, tree_to_shwi (t2)));
13511 else
13513 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13514 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13515 TREE_TYPE (t), t, t2);
13516 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13518 OMP_CLAUSE_DECL (c) = t;
13519 return false;
13521 first = c_fully_fold (first, false, NULL);
13522 OMP_CLAUSE_DECL (c) = first;
13523 if (size)
13524 size = c_fully_fold (size, false, NULL);
13525 OMP_CLAUSE_SIZE (c) = size;
13526 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13527 || (TREE_CODE (t) == COMPONENT_REF
13528 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13529 return false;
13530 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13531 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13532 switch (OMP_CLAUSE_MAP_KIND (c))
13534 case GOMP_MAP_ALLOC:
13535 case GOMP_MAP_IF_PRESENT:
13536 case GOMP_MAP_TO:
13537 case GOMP_MAP_FROM:
13538 case GOMP_MAP_TOFROM:
13539 case GOMP_MAP_ALWAYS_TO:
13540 case GOMP_MAP_ALWAYS_FROM:
13541 case GOMP_MAP_ALWAYS_TOFROM:
13542 case GOMP_MAP_RELEASE:
13543 case GOMP_MAP_DELETE:
13544 case GOMP_MAP_FORCE_TO:
13545 case GOMP_MAP_FORCE_FROM:
13546 case GOMP_MAP_FORCE_TOFROM:
13547 case GOMP_MAP_FORCE_PRESENT:
13548 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13549 break;
13550 default:
13551 break;
13553 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13554 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13555 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13556 else if (TREE_CODE (t) == COMPONENT_REF)
13558 gomp_map_kind k = (ort == C_ORT_ACC) ? GOMP_MAP_ATTACH_DETACH
13559 : GOMP_MAP_ALWAYS_POINTER;
13560 OMP_CLAUSE_SET_MAP_KIND (c2, k);
13562 else
13563 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13564 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13565 && !c_mark_addressable (t))
13566 return false;
13567 OMP_CLAUSE_DECL (c2) = t;
13568 t = build_fold_addr_expr (first);
13569 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13570 tree ptr = OMP_CLAUSE_DECL (c2);
13571 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13572 ptr = build_fold_addr_expr (ptr);
13573 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13574 ptrdiff_type_node, t,
13575 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13576 ptrdiff_type_node, ptr));
13577 t = c_fully_fold (t, false, NULL);
13578 OMP_CLAUSE_SIZE (c2) = t;
13579 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13580 OMP_CLAUSE_CHAIN (c) = c2;
13582 return false;
13585 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13586 an inline call. But, remap
13587 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13588 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13590 static tree
13591 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13592 tree decl, tree placeholder)
13594 copy_body_data id;
13595 hash_map<tree, tree> decl_map;
13597 decl_map.put (omp_decl1, placeholder);
13598 decl_map.put (omp_decl2, decl);
13599 memset (&id, 0, sizeof (id));
13600 id.src_fn = DECL_CONTEXT (omp_decl1);
13601 id.dst_fn = current_function_decl;
13602 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13603 id.decl_map = &decl_map;
13605 id.copy_decl = copy_decl_no_change;
13606 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13607 id.transform_new_cfg = true;
13608 id.transform_return_to_modify = false;
13609 id.transform_lang_insert_block = NULL;
13610 id.eh_lp_nr = 0;
13611 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13612 return stmt;
13615 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13616 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13618 static tree
13619 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13621 if (*tp == (tree) data)
13622 return *tp;
13623 return NULL_TREE;
13626 /* Similarly, but also walk aggregate fields. */
13628 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13630 static tree
13631 c_find_omp_var_r (tree *tp, int *, void *data)
13633 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13634 return *tp;
13635 if (RECORD_OR_UNION_TYPE_P (*tp))
13637 tree field;
13638 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13640 for (field = TYPE_FIELDS (*tp); field;
13641 field = DECL_CHAIN (field))
13642 if (TREE_CODE (field) == FIELD_DECL)
13644 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13645 c_find_omp_var_r, data, pset);
13646 if (ret)
13647 return ret;
13648 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13649 if (ret)
13650 return ret;
13651 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13652 pset);
13653 if (ret)
13654 return ret;
13655 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13656 if (ret)
13657 return ret;
13660 else if (INTEGRAL_TYPE_P (*tp))
13661 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13662 ((struct c_find_omp_var_s *) data)->pset);
13663 return NULL_TREE;
13666 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13667 and clauses containing them should be removed. */
13669 static bool
13670 c_omp_finish_iterators (tree iter)
13672 bool ret = false;
13673 for (tree it = iter; it; it = TREE_CHAIN (it))
13675 tree var = TREE_VEC_ELT (it, 0);
13676 tree begin = TREE_VEC_ELT (it, 1);
13677 tree end = TREE_VEC_ELT (it, 2);
13678 tree step = TREE_VEC_ELT (it, 3);
13679 tree orig_step;
13680 tree type = TREE_TYPE (var);
13681 location_t loc = DECL_SOURCE_LOCATION (var);
13682 if (type == error_mark_node)
13684 ret = true;
13685 continue;
13687 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13689 error_at (loc, "iterator %qD has neither integral nor pointer type",
13690 var);
13691 ret = true;
13692 continue;
13694 else if (TYPE_ATOMIC (type))
13696 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13697 ret = true;
13698 continue;
13700 else if (TYPE_READONLY (type))
13702 error_at (loc, "iterator %qD has const qualified type", var);
13703 ret = true;
13704 continue;
13706 else if (step == error_mark_node
13707 || TREE_TYPE (step) == error_mark_node)
13709 ret = true;
13710 continue;
13712 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13714 error_at (EXPR_LOC_OR_LOC (step, loc),
13715 "iterator step with non-integral type");
13716 ret = true;
13717 continue;
13719 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13720 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13721 orig_step = save_expr (c_fully_fold (step, false, NULL));
13722 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13723 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13724 if (POINTER_TYPE_P (type))
13726 begin = save_expr (begin);
13727 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13728 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13729 fold_convert (sizetype, step),
13730 fold_convert (sizetype, begin));
13731 step = fold_convert (ssizetype, step);
13733 if (integer_zerop (step))
13735 error_at (loc, "iterator %qD has zero step", var);
13736 ret = true;
13737 continue;
13740 if (begin == error_mark_node
13741 || end == error_mark_node
13742 || step == error_mark_node
13743 || orig_step == error_mark_node)
13745 ret = true;
13746 continue;
13748 hash_set<tree> pset;
13749 tree it2;
13750 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13752 tree var2 = TREE_VEC_ELT (it2, 0);
13753 tree begin2 = TREE_VEC_ELT (it2, 1);
13754 tree end2 = TREE_VEC_ELT (it2, 2);
13755 tree step2 = TREE_VEC_ELT (it2, 3);
13756 tree type2 = TREE_TYPE (var2);
13757 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13758 struct c_find_omp_var_s data = { var, &pset };
13759 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13761 error_at (loc2,
13762 "type of iterator %qD refers to outer iterator %qD",
13763 var2, var);
13764 break;
13766 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13768 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13769 "begin expression refers to outer iterator %qD", var);
13770 break;
13772 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13774 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13775 "end expression refers to outer iterator %qD", var);
13776 break;
13778 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13780 error_at (EXPR_LOC_OR_LOC (step2, loc2),
13781 "step expression refers to outer iterator %qD", var);
13782 break;
13785 if (it2)
13787 ret = true;
13788 continue;
13790 TREE_VEC_ELT (it, 1) = begin;
13791 TREE_VEC_ELT (it, 2) = end;
13792 TREE_VEC_ELT (it, 3) = step;
13793 TREE_VEC_ELT (it, 4) = orig_step;
13795 return ret;
13798 /* Ensure that pointers are used in OpenACC attach and detach clauses.
13799 Return true if an error has been detected. */
13801 static bool
13802 c_oacc_check_attachments (tree c)
13804 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13805 return false;
13807 /* OpenACC attach / detach clauses must be pointers. */
13808 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13809 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
13811 tree t = OMP_CLAUSE_DECL (c);
13813 while (TREE_CODE (t) == TREE_LIST)
13814 t = TREE_CHAIN (t);
13816 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13818 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
13819 c_omp_map_clause_name (c, true));
13820 return true;
13824 return false;
13827 /* For all elements of CLAUSES, validate them against their constraints.
13828 Remove any elements from the list that are invalid. */
13830 tree
13831 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13833 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13834 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13835 tree c, t, type, *pc;
13836 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13837 bool branch_seen = false;
13838 bool copyprivate_seen = false;
13839 bool linear_variable_step_check = false;
13840 tree *nowait_clause = NULL;
13841 tree ordered_clause = NULL_TREE;
13842 tree schedule_clause = NULL_TREE;
13843 bool oacc_async = false;
13844 tree last_iterators = NULL_TREE;
13845 bool last_iterators_remove = false;
13846 tree *nogroup_seen = NULL;
13847 tree *order_clause = NULL;
13848 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
13849 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
13850 int reduction_seen = 0;
13852 bitmap_obstack_initialize (NULL);
13853 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13854 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13855 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13856 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13857 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
13858 bitmap_initialize (&map_head, &bitmap_default_obstack);
13859 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13860 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
13861 instead. */
13862 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13864 if (ort & C_ORT_ACC)
13865 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13866 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13868 oacc_async = true;
13869 break;
13872 for (pc = &clauses, c = clauses; c ; c = *pc)
13874 bool remove = false;
13875 bool need_complete = false;
13876 bool need_implicitly_determined = false;
13878 switch (OMP_CLAUSE_CODE (c))
13880 case OMP_CLAUSE_SHARED:
13881 need_implicitly_determined = true;
13882 goto check_dup_generic;
13884 case OMP_CLAUSE_PRIVATE:
13885 need_complete = true;
13886 need_implicitly_determined = true;
13887 goto check_dup_generic;
13889 case OMP_CLAUSE_REDUCTION:
13890 if (reduction_seen == 0)
13891 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
13892 else if (reduction_seen != -2
13893 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
13894 ? -1 : 1))
13896 error_at (OMP_CLAUSE_LOCATION (c),
13897 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
13898 "on the same construct");
13899 reduction_seen = -2;
13901 /* FALLTHRU */
13902 case OMP_CLAUSE_IN_REDUCTION:
13903 case OMP_CLAUSE_TASK_REDUCTION:
13904 need_implicitly_determined = true;
13905 t = OMP_CLAUSE_DECL (c);
13906 if (TREE_CODE (t) == TREE_LIST)
13908 if (handle_omp_array_sections (c, ort))
13910 remove = true;
13911 break;
13914 t = OMP_CLAUSE_DECL (c);
13915 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13916 && OMP_CLAUSE_REDUCTION_INSCAN (c))
13918 error_at (OMP_CLAUSE_LOCATION (c),
13919 "%<inscan%> %<reduction%> clause with array "
13920 "section");
13921 remove = true;
13922 break;
13925 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13926 if (t == error_mark_node)
13928 remove = true;
13929 break;
13931 if (oacc_async)
13932 c_mark_addressable (t);
13933 type = TREE_TYPE (t);
13934 if (TREE_CODE (t) == MEM_REF)
13935 type = TREE_TYPE (type);
13936 if (TREE_CODE (type) == ARRAY_TYPE)
13938 tree oatype = type;
13939 gcc_assert (TREE_CODE (t) != MEM_REF);
13940 while (TREE_CODE (type) == ARRAY_TYPE)
13941 type = TREE_TYPE (type);
13942 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13944 error_at (OMP_CLAUSE_LOCATION (c),
13945 "%qD in %<reduction%> clause is a zero size array",
13947 remove = true;
13948 break;
13950 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13951 TYPE_SIZE_UNIT (type));
13952 if (integer_zerop (size))
13954 error_at (OMP_CLAUSE_LOCATION (c),
13955 "%qD in %<reduction%> clause is a zero size array",
13957 remove = true;
13958 break;
13960 size = size_binop (MINUS_EXPR, size, size_one_node);
13961 size = save_expr (size);
13962 tree index_type = build_index_type (size);
13963 tree atype = build_array_type (type, index_type);
13964 tree ptype = build_pointer_type (type);
13965 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13966 t = build_fold_addr_expr (t);
13967 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13968 OMP_CLAUSE_DECL (c) = t;
13970 if (TYPE_ATOMIC (type))
13972 error_at (OMP_CLAUSE_LOCATION (c),
13973 "%<_Atomic%> %qE in %<reduction%> clause", t);
13974 remove = true;
13975 break;
13977 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13978 || OMP_CLAUSE_REDUCTION_TASK (c))
13980 /* Disallow zero sized or potentially zero sized task
13981 reductions. */
13982 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13984 error_at (OMP_CLAUSE_LOCATION (c),
13985 "zero sized type %qT in %qs clause", type,
13986 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13987 remove = true;
13988 break;
13990 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
13992 error_at (OMP_CLAUSE_LOCATION (c),
13993 "variable sized type %qT in %qs clause", type,
13994 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13995 remove = true;
13996 break;
13999 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14000 && (FLOAT_TYPE_P (type)
14001 || TREE_CODE (type) == COMPLEX_TYPE))
14003 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14004 const char *r_name = NULL;
14006 switch (r_code)
14008 case PLUS_EXPR:
14009 case MULT_EXPR:
14010 case MINUS_EXPR:
14011 break;
14012 case MIN_EXPR:
14013 if (TREE_CODE (type) == COMPLEX_TYPE)
14014 r_name = "min";
14015 break;
14016 case MAX_EXPR:
14017 if (TREE_CODE (type) == COMPLEX_TYPE)
14018 r_name = "max";
14019 break;
14020 case BIT_AND_EXPR:
14021 r_name = "&";
14022 break;
14023 case BIT_XOR_EXPR:
14024 r_name = "^";
14025 break;
14026 case BIT_IOR_EXPR:
14027 r_name = "|";
14028 break;
14029 case TRUTH_ANDIF_EXPR:
14030 if (FLOAT_TYPE_P (type))
14031 r_name = "&&";
14032 break;
14033 case TRUTH_ORIF_EXPR:
14034 if (FLOAT_TYPE_P (type))
14035 r_name = "||";
14036 break;
14037 default:
14038 gcc_unreachable ();
14040 if (r_name)
14042 error_at (OMP_CLAUSE_LOCATION (c),
14043 "%qE has invalid type for %<reduction(%s)%>",
14044 t, r_name);
14045 remove = true;
14046 break;
14049 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14051 error_at (OMP_CLAUSE_LOCATION (c),
14052 "user defined reduction not found for %qE", t);
14053 remove = true;
14054 break;
14056 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14058 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14059 type = TYPE_MAIN_VARIANT (type);
14060 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14061 VAR_DECL, NULL_TREE, type);
14062 tree decl_placeholder = NULL_TREE;
14063 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14064 DECL_ARTIFICIAL (placeholder) = 1;
14065 DECL_IGNORED_P (placeholder) = 1;
14066 if (TREE_CODE (t) == MEM_REF)
14068 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14069 VAR_DECL, NULL_TREE, type);
14070 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14071 DECL_ARTIFICIAL (decl_placeholder) = 1;
14072 DECL_IGNORED_P (decl_placeholder) = 1;
14074 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14075 c_mark_addressable (placeholder);
14076 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14077 c_mark_addressable (decl_placeholder ? decl_placeholder
14078 : OMP_CLAUSE_DECL (c));
14079 OMP_CLAUSE_REDUCTION_MERGE (c)
14080 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14081 TREE_VEC_ELT (list, 0),
14082 TREE_VEC_ELT (list, 1),
14083 decl_placeholder ? decl_placeholder
14084 : OMP_CLAUSE_DECL (c), placeholder);
14085 OMP_CLAUSE_REDUCTION_MERGE (c)
14086 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14087 void_type_node, NULL_TREE,
14088 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14089 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14090 if (TREE_VEC_LENGTH (list) == 6)
14092 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14093 c_mark_addressable (decl_placeholder ? decl_placeholder
14094 : OMP_CLAUSE_DECL (c));
14095 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14096 c_mark_addressable (placeholder);
14097 tree init = TREE_VEC_ELT (list, 5);
14098 if (init == error_mark_node)
14099 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14100 OMP_CLAUSE_REDUCTION_INIT (c)
14101 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14102 TREE_VEC_ELT (list, 3),
14103 decl_placeholder ? decl_placeholder
14104 : OMP_CLAUSE_DECL (c), placeholder);
14105 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14107 tree v = decl_placeholder ? decl_placeholder : t;
14108 OMP_CLAUSE_REDUCTION_INIT (c)
14109 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14110 OMP_CLAUSE_REDUCTION_INIT (c));
14112 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14113 c_find_omp_placeholder_r,
14114 placeholder, NULL))
14115 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14117 else
14119 tree init;
14120 tree v = decl_placeholder ? decl_placeholder : t;
14121 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14122 init = build_constructor (TREE_TYPE (v), NULL);
14123 else
14124 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14125 OMP_CLAUSE_REDUCTION_INIT (c)
14126 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14128 OMP_CLAUSE_REDUCTION_INIT (c)
14129 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14130 void_type_node, NULL_TREE,
14131 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14132 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14134 if (TREE_CODE (t) == MEM_REF)
14136 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14137 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14138 != INTEGER_CST)
14140 sorry ("variable length element type in array "
14141 "%<reduction%> clause");
14142 remove = true;
14143 break;
14145 t = TREE_OPERAND (t, 0);
14146 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14147 t = TREE_OPERAND (t, 0);
14148 if (TREE_CODE (t) == ADDR_EXPR)
14149 t = TREE_OPERAND (t, 0);
14151 goto check_dup_generic_t;
14153 case OMP_CLAUSE_COPYPRIVATE:
14154 copyprivate_seen = true;
14155 if (nowait_clause)
14157 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14158 "%<nowait%> clause must not be used together "
14159 "with %<copyprivate%>");
14160 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14161 nowait_clause = NULL;
14163 goto check_dup_generic;
14165 case OMP_CLAUSE_COPYIN:
14166 t = OMP_CLAUSE_DECL (c);
14167 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14169 error_at (OMP_CLAUSE_LOCATION (c),
14170 "%qE must be %<threadprivate%> for %<copyin%>", t);
14171 remove = true;
14172 break;
14174 goto check_dup_generic;
14176 case OMP_CLAUSE_LINEAR:
14177 if (ort != C_ORT_OMP_DECLARE_SIMD)
14178 need_implicitly_determined = true;
14179 t = OMP_CLAUSE_DECL (c);
14180 if (ort != C_ORT_OMP_DECLARE_SIMD
14181 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
14183 error_at (OMP_CLAUSE_LOCATION (c),
14184 "modifier should not be specified in %<linear%> "
14185 "clause on %<simd%> or %<for%> constructs");
14186 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14188 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14189 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14191 error_at (OMP_CLAUSE_LOCATION (c),
14192 "linear clause applied to non-integral non-pointer "
14193 "variable with type %qT", TREE_TYPE (t));
14194 remove = true;
14195 break;
14197 if (TYPE_ATOMIC (TREE_TYPE (t)))
14199 error_at (OMP_CLAUSE_LOCATION (c),
14200 "%<_Atomic%> %qD in %<linear%> clause", t);
14201 remove = true;
14202 break;
14204 if (ort == C_ORT_OMP_DECLARE_SIMD)
14206 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14207 if (TREE_CODE (s) == PARM_DECL)
14209 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14210 /* map_head bitmap is used as uniform_head if
14211 declare_simd. */
14212 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14213 linear_variable_step_check = true;
14214 goto check_dup_generic;
14216 if (TREE_CODE (s) != INTEGER_CST)
14218 error_at (OMP_CLAUSE_LOCATION (c),
14219 "%<linear%> clause step %qE is neither constant "
14220 "nor a parameter", s);
14221 remove = true;
14222 break;
14225 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14227 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14228 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14229 OMP_CLAUSE_DECL (c), s);
14230 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14231 sizetype, fold_convert (sizetype, s),
14232 fold_convert
14233 (sizetype, OMP_CLAUSE_DECL (c)));
14234 if (s == error_mark_node)
14235 s = size_one_node;
14236 OMP_CLAUSE_LINEAR_STEP (c) = s;
14238 else
14239 OMP_CLAUSE_LINEAR_STEP (c)
14240 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14241 goto check_dup_generic;
14243 check_dup_generic:
14244 t = OMP_CLAUSE_DECL (c);
14245 check_dup_generic_t:
14246 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14248 error_at (OMP_CLAUSE_LOCATION (c),
14249 "%qE is not a variable in clause %qs", t,
14250 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14251 remove = true;
14253 else if ((ort == C_ORT_ACC
14254 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14255 || (ort == C_ORT_OMP
14256 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14257 || (OMP_CLAUSE_CODE (c)
14258 == OMP_CLAUSE_USE_DEVICE_ADDR))))
14260 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14262 error_at (OMP_CLAUSE_LOCATION (c),
14263 ort == C_ORT_ACC
14264 ? "%qD appears more than once in reduction clauses"
14265 : "%qD appears more than once in data clauses",
14267 remove = true;
14269 else
14270 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14272 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14273 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14274 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14276 error_at (OMP_CLAUSE_LOCATION (c),
14277 "%qE appears more than once in data clauses", t);
14278 remove = true;
14280 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14281 && bitmap_bit_p (&map_head, DECL_UID (t)))
14283 if (ort == C_ORT_ACC)
14284 error_at (OMP_CLAUSE_LOCATION (c),
14285 "%qD appears more than once in data clauses", t);
14286 else
14287 error_at (OMP_CLAUSE_LOCATION (c),
14288 "%qD appears both in data and map clauses", t);
14289 remove = true;
14291 else
14292 bitmap_set_bit (&generic_head, DECL_UID (t));
14293 break;
14295 case OMP_CLAUSE_FIRSTPRIVATE:
14296 t = OMP_CLAUSE_DECL (c);
14297 need_complete = true;
14298 need_implicitly_determined = true;
14299 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14301 error_at (OMP_CLAUSE_LOCATION (c),
14302 "%qE is not a variable in clause %<firstprivate%>", t);
14303 remove = true;
14305 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14306 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14308 error_at (OMP_CLAUSE_LOCATION (c),
14309 "%qE appears more than once in data clauses", t);
14310 remove = true;
14312 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14314 if (ort == C_ORT_ACC)
14315 error_at (OMP_CLAUSE_LOCATION (c),
14316 "%qD appears more than once in data clauses", t);
14317 else
14318 error_at (OMP_CLAUSE_LOCATION (c),
14319 "%qD appears both in data and map clauses", t);
14320 remove = true;
14322 else
14323 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14324 break;
14326 case OMP_CLAUSE_LASTPRIVATE:
14327 t = OMP_CLAUSE_DECL (c);
14328 need_complete = true;
14329 need_implicitly_determined = true;
14330 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14332 error_at (OMP_CLAUSE_LOCATION (c),
14333 "%qE is not a variable in clause %<lastprivate%>", t);
14334 remove = true;
14336 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14337 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14339 error_at (OMP_CLAUSE_LOCATION (c),
14340 "%qE appears more than once in data clauses", t);
14341 remove = true;
14343 else
14344 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14345 break;
14347 case OMP_CLAUSE_ALIGNED:
14348 t = OMP_CLAUSE_DECL (c);
14349 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14351 error_at (OMP_CLAUSE_LOCATION (c),
14352 "%qE is not a variable in %<aligned%> clause", t);
14353 remove = true;
14355 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14356 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14358 error_at (OMP_CLAUSE_LOCATION (c),
14359 "%qE in %<aligned%> clause is neither a pointer nor "
14360 "an array", t);
14361 remove = true;
14363 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14365 error_at (OMP_CLAUSE_LOCATION (c),
14366 "%<_Atomic%> %qD in %<aligned%> clause", t);
14367 remove = true;
14368 break;
14370 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14372 error_at (OMP_CLAUSE_LOCATION (c),
14373 "%qE appears more than once in %<aligned%> clauses",
14375 remove = true;
14377 else
14378 bitmap_set_bit (&aligned_head, DECL_UID (t));
14379 break;
14381 case OMP_CLAUSE_NONTEMPORAL:
14382 t = OMP_CLAUSE_DECL (c);
14383 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14385 error_at (OMP_CLAUSE_LOCATION (c),
14386 "%qE is not a variable in %<nontemporal%> clause", t);
14387 remove = true;
14389 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14391 error_at (OMP_CLAUSE_LOCATION (c),
14392 "%qE appears more than once in %<nontemporal%> "
14393 "clauses", t);
14394 remove = true;
14396 else
14397 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14398 break;
14400 case OMP_CLAUSE_DEPEND:
14401 t = OMP_CLAUSE_DECL (c);
14402 if (t == NULL_TREE)
14404 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14405 == OMP_CLAUSE_DEPEND_SOURCE);
14406 break;
14408 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14410 gcc_assert (TREE_CODE (t) == TREE_LIST);
14411 for (; t; t = TREE_CHAIN (t))
14413 tree decl = TREE_VALUE (t);
14414 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14416 tree offset = TREE_PURPOSE (t);
14417 bool neg = wi::neg_p (wi::to_wide (offset));
14418 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14419 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14420 neg ? MINUS_EXPR : PLUS_EXPR,
14421 decl, offset);
14422 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14423 sizetype,
14424 fold_convert (sizetype, t2),
14425 fold_convert (sizetype, decl));
14426 if (t2 == error_mark_node)
14428 remove = true;
14429 break;
14431 TREE_PURPOSE (t) = t2;
14434 break;
14436 if (TREE_CODE (t) == TREE_LIST
14437 && TREE_PURPOSE (t)
14438 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14440 if (TREE_PURPOSE (t) != last_iterators)
14441 last_iterators_remove
14442 = c_omp_finish_iterators (TREE_PURPOSE (t));
14443 last_iterators = TREE_PURPOSE (t);
14444 t = TREE_VALUE (t);
14445 if (last_iterators_remove)
14446 t = error_mark_node;
14448 else
14449 last_iterators = NULL_TREE;
14450 if (TREE_CODE (t) == TREE_LIST)
14452 if (handle_omp_array_sections (c, ort))
14453 remove = true;
14454 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14456 error_at (OMP_CLAUSE_LOCATION (c),
14457 "%<depend%> clause with %<depobj%> dependence "
14458 "type on array section");
14459 remove = true;
14461 break;
14463 if (t == error_mark_node)
14464 remove = true;
14465 else if (!lvalue_p (t))
14467 error_at (OMP_CLAUSE_LOCATION (c),
14468 "%qE is not lvalue expression nor array section in "
14469 "%<depend%> clause", t);
14470 remove = true;
14472 else if (TREE_CODE (t) == COMPONENT_REF
14473 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14475 error_at (OMP_CLAUSE_LOCATION (c),
14476 "bit-field %qE in %qs clause", t, "depend");
14477 remove = true;
14479 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14481 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14483 error_at (OMP_CLAUSE_LOCATION (c),
14484 "%qE does not have %<omp_depend_t%> type in "
14485 "%<depend%> clause with %<depobj%> dependence "
14486 "type", t);
14487 remove = true;
14490 else if (c_omp_depend_t_p (TREE_TYPE (t)))
14492 error_at (OMP_CLAUSE_LOCATION (c),
14493 "%qE should not have %<omp_depend_t%> type in "
14494 "%<depend%> clause with dependence type other than "
14495 "%<depobj%>", t);
14496 remove = true;
14498 if (!remove)
14500 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14501 t, false);
14502 if (addr == error_mark_node)
14503 remove = true;
14504 else
14506 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14507 RO_UNARY_STAR);
14508 if (t == error_mark_node)
14509 remove = true;
14510 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14511 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14512 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14513 == TREE_VEC))
14514 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14515 else
14516 OMP_CLAUSE_DECL (c) = t;
14519 break;
14521 case OMP_CLAUSE_MAP:
14522 case OMP_CLAUSE_TO:
14523 case OMP_CLAUSE_FROM:
14524 case OMP_CLAUSE__CACHE_:
14525 t = OMP_CLAUSE_DECL (c);
14526 if (TREE_CODE (t) == TREE_LIST)
14528 if (handle_omp_array_sections (c, ort))
14529 remove = true;
14530 else
14532 t = OMP_CLAUSE_DECL (c);
14533 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14535 error_at (OMP_CLAUSE_LOCATION (c),
14536 "array section does not have mappable type "
14537 "in %qs clause",
14538 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14539 remove = true;
14541 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14543 error_at (OMP_CLAUSE_LOCATION (c),
14544 "%<_Atomic%> %qE in %qs clause", t,
14545 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14546 remove = true;
14548 while (TREE_CODE (t) == ARRAY_REF)
14549 t = TREE_OPERAND (t, 0);
14550 if (TREE_CODE (t) == COMPONENT_REF
14551 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14553 while (TREE_CODE (t) == COMPONENT_REF)
14554 t = TREE_OPERAND (t, 0);
14555 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14556 break;
14557 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14559 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14560 error_at (OMP_CLAUSE_LOCATION (c),
14561 "%qD appears more than once in motion "
14562 "clauses", t);
14563 else if (ort == C_ORT_ACC)
14564 error_at (OMP_CLAUSE_LOCATION (c),
14565 "%qD appears more than once in data "
14566 "clauses", t);
14567 else
14568 error_at (OMP_CLAUSE_LOCATION (c),
14569 "%qD appears more than once in map "
14570 "clauses", t);
14571 remove = true;
14573 else
14575 bitmap_set_bit (&map_head, DECL_UID (t));
14576 bitmap_set_bit (&map_field_head, DECL_UID (t));
14580 if (c_oacc_check_attachments (c))
14581 remove = true;
14582 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14583 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14584 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14585 /* In this case, we have a single array element which is a
14586 pointer, and we already set OMP_CLAUSE_SIZE in
14587 handle_omp_array_sections above. For attach/detach clauses,
14588 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
14589 here. */
14590 OMP_CLAUSE_SIZE (c) = size_zero_node;
14591 break;
14593 if (t == error_mark_node)
14595 remove = true;
14596 break;
14598 /* OpenACC attach / detach clauses must be pointers. */
14599 if (c_oacc_check_attachments (c))
14601 remove = true;
14602 break;
14604 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14605 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14606 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14607 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
14608 bias) to zero here, so it is not set erroneously to the pointer
14609 size later on in gimplify.c. */
14610 OMP_CLAUSE_SIZE (c) = size_zero_node;
14611 if (TREE_CODE (t) == COMPONENT_REF
14612 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14614 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14616 error_at (OMP_CLAUSE_LOCATION (c),
14617 "bit-field %qE in %qs clause",
14618 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14619 remove = true;
14621 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14623 error_at (OMP_CLAUSE_LOCATION (c),
14624 "%qE does not have a mappable type in %qs clause",
14625 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14626 remove = true;
14628 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14630 error_at (OMP_CLAUSE_LOCATION (c),
14631 "%<_Atomic%> %qE in %qs clause", t,
14632 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14633 remove = true;
14635 while (TREE_CODE (t) == COMPONENT_REF)
14637 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14638 == UNION_TYPE)
14640 error_at (OMP_CLAUSE_LOCATION (c),
14641 "%qE is a member of a union", t);
14642 remove = true;
14643 break;
14645 t = TREE_OPERAND (t, 0);
14646 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
14648 if (maybe_ne (mem_ref_offset (t), 0))
14649 error_at (OMP_CLAUSE_LOCATION (c),
14650 "cannot dereference %qE in %qs clause", t,
14651 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14652 else
14653 t = TREE_OPERAND (t, 0);
14656 if (remove)
14657 break;
14658 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14660 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14661 break;
14664 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14666 error_at (OMP_CLAUSE_LOCATION (c),
14667 "%qE is not a variable in %qs clause", t,
14668 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14669 remove = true;
14671 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14673 error_at (OMP_CLAUSE_LOCATION (c),
14674 "%qD is threadprivate variable in %qs clause", t,
14675 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14676 remove = true;
14678 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14679 || (OMP_CLAUSE_MAP_KIND (c)
14680 != GOMP_MAP_FIRSTPRIVATE_POINTER))
14681 && !c_mark_addressable (t))
14682 remove = true;
14683 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14684 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14685 || (OMP_CLAUSE_MAP_KIND (c)
14686 == GOMP_MAP_FIRSTPRIVATE_POINTER)
14687 || (OMP_CLAUSE_MAP_KIND (c)
14688 == GOMP_MAP_FORCE_DEVICEPTR)))
14689 && t == OMP_CLAUSE_DECL (c)
14690 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14692 error_at (OMP_CLAUSE_LOCATION (c),
14693 "%qD does not have a mappable type in %qs clause", t,
14694 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14695 remove = true;
14697 else if (TREE_TYPE (t) == error_mark_node)
14698 remove = true;
14699 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14701 error_at (OMP_CLAUSE_LOCATION (c),
14702 "%<_Atomic%> %qE in %qs clause", t,
14703 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14704 remove = true;
14706 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14707 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14709 if (bitmap_bit_p (&generic_head, DECL_UID (t))
14710 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14712 error_at (OMP_CLAUSE_LOCATION (c),
14713 "%qD appears more than once in data clauses", t);
14714 remove = true;
14716 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14718 if (ort == C_ORT_ACC)
14719 error_at (OMP_CLAUSE_LOCATION (c),
14720 "%qD appears more than once in data clauses", t);
14721 else
14722 error_at (OMP_CLAUSE_LOCATION (c),
14723 "%qD appears both in data and map clauses", t);
14724 remove = true;
14726 else
14727 bitmap_set_bit (&generic_head, DECL_UID (t));
14729 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14731 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14732 error_at (OMP_CLAUSE_LOCATION (c),
14733 "%qD appears more than once in motion clauses", t);
14734 else if (ort == C_ORT_ACC)
14735 error_at (OMP_CLAUSE_LOCATION (c),
14736 "%qD appears more than once in data clauses", t);
14737 else
14738 error_at (OMP_CLAUSE_LOCATION (c),
14739 "%qD appears more than once in map clauses", t);
14740 remove = true;
14742 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14743 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14745 if (ort == C_ORT_ACC)
14746 error_at (OMP_CLAUSE_LOCATION (c),
14747 "%qD appears more than once in data clauses", t);
14748 else
14749 error_at (OMP_CLAUSE_LOCATION (c),
14750 "%qD appears both in data and map clauses", t);
14751 remove = true;
14753 else
14755 bitmap_set_bit (&map_head, DECL_UID (t));
14756 if (t != OMP_CLAUSE_DECL (c)
14757 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14758 bitmap_set_bit (&map_field_head, DECL_UID (t));
14760 break;
14762 case OMP_CLAUSE_TO_DECLARE:
14763 case OMP_CLAUSE_LINK:
14764 t = OMP_CLAUSE_DECL (c);
14765 if (TREE_CODE (t) == FUNCTION_DECL
14766 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14768 else if (!VAR_P (t))
14770 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14771 error_at (OMP_CLAUSE_LOCATION (c),
14772 "%qE is neither a variable nor a function name in "
14773 "clause %qs", t,
14774 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14775 else
14776 error_at (OMP_CLAUSE_LOCATION (c),
14777 "%qE is not a variable in clause %qs", t,
14778 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14779 remove = true;
14781 else if (DECL_THREAD_LOCAL_P (t))
14783 error_at (OMP_CLAUSE_LOCATION (c),
14784 "%qD is threadprivate variable in %qs clause", t,
14785 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14786 remove = true;
14788 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14790 error_at (OMP_CLAUSE_LOCATION (c),
14791 "%qD does not have a mappable type in %qs clause", t,
14792 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14793 remove = true;
14795 if (remove)
14796 break;
14797 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
14799 error_at (OMP_CLAUSE_LOCATION (c),
14800 "%qE appears more than once on the same "
14801 "%<declare target%> directive", t);
14802 remove = true;
14804 else
14805 bitmap_set_bit (&generic_head, DECL_UID (t));
14806 break;
14808 case OMP_CLAUSE_UNIFORM:
14809 t = OMP_CLAUSE_DECL (c);
14810 if (TREE_CODE (t) != PARM_DECL)
14812 if (DECL_P (t))
14813 error_at (OMP_CLAUSE_LOCATION (c),
14814 "%qD is not an argument in %<uniform%> clause", t);
14815 else
14816 error_at (OMP_CLAUSE_LOCATION (c),
14817 "%qE is not an argument in %<uniform%> clause", t);
14818 remove = true;
14819 break;
14821 /* map_head bitmap is used as uniform_head if declare_simd. */
14822 bitmap_set_bit (&map_head, DECL_UID (t));
14823 goto check_dup_generic;
14825 case OMP_CLAUSE_IS_DEVICE_PTR:
14826 case OMP_CLAUSE_USE_DEVICE_PTR:
14827 t = OMP_CLAUSE_DECL (c);
14828 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14830 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14831 && ort == C_ORT_OMP)
14833 error_at (OMP_CLAUSE_LOCATION (c),
14834 "%qs variable is not a pointer",
14835 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14836 remove = true;
14838 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14840 error_at (OMP_CLAUSE_LOCATION (c),
14841 "%qs variable is neither a pointer nor an array",
14842 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14843 remove = true;
14846 goto check_dup_generic;
14848 case OMP_CLAUSE_USE_DEVICE_ADDR:
14849 t = OMP_CLAUSE_DECL (c);
14850 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14851 c_mark_addressable (t);
14852 goto check_dup_generic;
14854 case OMP_CLAUSE_NOWAIT:
14855 if (copyprivate_seen)
14857 error_at (OMP_CLAUSE_LOCATION (c),
14858 "%<nowait%> clause must not be used together "
14859 "with %<copyprivate%>");
14860 remove = true;
14861 break;
14863 nowait_clause = pc;
14864 pc = &OMP_CLAUSE_CHAIN (c);
14865 continue;
14867 case OMP_CLAUSE_ORDER:
14868 if (ordered_clause)
14870 error_at (OMP_CLAUSE_LOCATION (c),
14871 "%<order%> clause must not be used together "
14872 "with %<ordered%>");
14873 remove = true;
14874 break;
14876 else if (order_clause)
14878 /* Silently remove duplicates. */
14879 remove = true;
14880 break;
14882 order_clause = pc;
14883 pc = &OMP_CLAUSE_CHAIN (c);
14884 continue;
14886 case OMP_CLAUSE_IF:
14887 case OMP_CLAUSE_NUM_THREADS:
14888 case OMP_CLAUSE_NUM_TEAMS:
14889 case OMP_CLAUSE_THREAD_LIMIT:
14890 case OMP_CLAUSE_DEFAULT:
14891 case OMP_CLAUSE_UNTIED:
14892 case OMP_CLAUSE_COLLAPSE:
14893 case OMP_CLAUSE_FINAL:
14894 case OMP_CLAUSE_MERGEABLE:
14895 case OMP_CLAUSE_DEVICE:
14896 case OMP_CLAUSE_DIST_SCHEDULE:
14897 case OMP_CLAUSE_PARALLEL:
14898 case OMP_CLAUSE_FOR:
14899 case OMP_CLAUSE_SECTIONS:
14900 case OMP_CLAUSE_TASKGROUP:
14901 case OMP_CLAUSE_PROC_BIND:
14902 case OMP_CLAUSE_DEVICE_TYPE:
14903 case OMP_CLAUSE_PRIORITY:
14904 case OMP_CLAUSE_GRAINSIZE:
14905 case OMP_CLAUSE_NUM_TASKS:
14906 case OMP_CLAUSE_THREADS:
14907 case OMP_CLAUSE_SIMD:
14908 case OMP_CLAUSE_HINT:
14909 case OMP_CLAUSE_DEFAULTMAP:
14910 case OMP_CLAUSE_BIND:
14911 case OMP_CLAUSE_NUM_GANGS:
14912 case OMP_CLAUSE_NUM_WORKERS:
14913 case OMP_CLAUSE_VECTOR_LENGTH:
14914 case OMP_CLAUSE_ASYNC:
14915 case OMP_CLAUSE_WAIT:
14916 case OMP_CLAUSE_AUTO:
14917 case OMP_CLAUSE_INDEPENDENT:
14918 case OMP_CLAUSE_SEQ:
14919 case OMP_CLAUSE_GANG:
14920 case OMP_CLAUSE_WORKER:
14921 case OMP_CLAUSE_VECTOR:
14922 case OMP_CLAUSE_TILE:
14923 case OMP_CLAUSE_IF_PRESENT:
14924 case OMP_CLAUSE_FINALIZE:
14925 pc = &OMP_CLAUSE_CHAIN (c);
14926 continue;
14928 case OMP_CLAUSE_NOGROUP:
14929 nogroup_seen = pc;
14930 pc = &OMP_CLAUSE_CHAIN (c);
14931 continue;
14933 case OMP_CLAUSE_SCHEDULE:
14934 schedule_clause = c;
14935 pc = &OMP_CLAUSE_CHAIN (c);
14936 continue;
14938 case OMP_CLAUSE_ORDERED:
14939 ordered_clause = c;
14940 if (order_clause)
14942 error_at (OMP_CLAUSE_LOCATION (*order_clause),
14943 "%<order%> clause must not be used together "
14944 "with %<ordered%>");
14945 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
14946 order_clause = NULL;
14948 pc = &OMP_CLAUSE_CHAIN (c);
14949 continue;
14951 case OMP_CLAUSE_SAFELEN:
14952 safelen = c;
14953 pc = &OMP_CLAUSE_CHAIN (c);
14954 continue;
14955 case OMP_CLAUSE_SIMDLEN:
14956 simdlen = c;
14957 pc = &OMP_CLAUSE_CHAIN (c);
14958 continue;
14960 case OMP_CLAUSE_INBRANCH:
14961 case OMP_CLAUSE_NOTINBRANCH:
14962 if (branch_seen)
14964 error_at (OMP_CLAUSE_LOCATION (c),
14965 "%<inbranch%> clause is incompatible with "
14966 "%<notinbranch%>");
14967 remove = true;
14968 break;
14970 branch_seen = true;
14971 pc = &OMP_CLAUSE_CHAIN (c);
14972 continue;
14974 case OMP_CLAUSE_INCLUSIVE:
14975 case OMP_CLAUSE_EXCLUSIVE:
14976 need_complete = true;
14977 need_implicitly_determined = true;
14978 t = OMP_CLAUSE_DECL (c);
14979 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14981 error_at (OMP_CLAUSE_LOCATION (c),
14982 "%qE is not a variable in clause %qs", t,
14983 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14984 remove = true;
14986 break;
14988 default:
14989 gcc_unreachable ();
14992 if (!remove)
14994 t = OMP_CLAUSE_DECL (c);
14996 if (need_complete)
14998 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14999 if (t == error_mark_node)
15000 remove = true;
15003 if (need_implicitly_determined)
15005 const char *share_name = NULL;
15007 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15008 share_name = "threadprivate";
15009 else switch (c_omp_predetermined_sharing (t))
15011 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15012 break;
15013 case OMP_CLAUSE_DEFAULT_SHARED:
15014 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15015 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15016 && c_omp_predefined_variable (t))
15017 /* The __func__ variable and similar function-local
15018 predefined variables may be listed in a shared or
15019 firstprivate clause. */
15020 break;
15021 share_name = "shared";
15022 break;
15023 case OMP_CLAUSE_DEFAULT_PRIVATE:
15024 share_name = "private";
15025 break;
15026 default:
15027 gcc_unreachable ();
15029 if (share_name)
15031 error_at (OMP_CLAUSE_LOCATION (c),
15032 "%qE is predetermined %qs for %qs",
15033 t, share_name,
15034 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15035 remove = true;
15037 else if (TREE_READONLY (t)
15038 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15039 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15041 error_at (OMP_CLAUSE_LOCATION (c),
15042 "%<const%> qualified %qE may appear only in "
15043 "%<shared%> or %<firstprivate%> clauses", t);
15044 remove = true;
15049 if (remove)
15050 *pc = OMP_CLAUSE_CHAIN (c);
15051 else
15052 pc = &OMP_CLAUSE_CHAIN (c);
15055 if (simdlen
15056 && safelen
15057 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15058 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15060 error_at (OMP_CLAUSE_LOCATION (simdlen),
15061 "%<simdlen%> clause value is bigger than "
15062 "%<safelen%> clause value");
15063 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15064 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15067 if (ordered_clause
15068 && schedule_clause
15069 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15070 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15072 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15073 "%<nonmonotonic%> schedule modifier specified together "
15074 "with %<ordered%> clause");
15075 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15076 = (enum omp_clause_schedule_kind)
15077 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15078 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15081 if (reduction_seen < 0 && ordered_clause)
15083 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15084 "%qs clause specified together with %<inscan%> "
15085 "%<reduction%> clause", "ordered");
15086 reduction_seen = -2;
15089 if (reduction_seen < 0 && schedule_clause)
15091 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15092 "%qs clause specified together with %<inscan%> "
15093 "%<reduction%> clause", "schedule");
15094 reduction_seen = -2;
15097 if (linear_variable_step_check || reduction_seen == -2)
15098 for (pc = &clauses, c = clauses; c ; c = *pc)
15100 bool remove = false;
15101 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15102 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15103 && !bitmap_bit_p (&map_head,
15104 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15106 error_at (OMP_CLAUSE_LOCATION (c),
15107 "%<linear%> clause step is a parameter %qD not "
15108 "specified in %<uniform%> clause",
15109 OMP_CLAUSE_LINEAR_STEP (c));
15110 remove = true;
15112 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
15113 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15115 if (remove)
15116 *pc = OMP_CLAUSE_CHAIN (c);
15117 else
15118 pc = &OMP_CLAUSE_CHAIN (c);
15121 if (nogroup_seen && reduction_seen)
15123 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15124 "%<nogroup%> clause must not be used together with "
15125 "%<reduction%> clause");
15126 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15129 bitmap_obstack_release (NULL);
15130 return clauses;
15133 /* Return code to initialize DST with a copy constructor from SRC.
15134 C doesn't have copy constructors nor assignment operators, only for
15135 _Atomic vars we need to perform __atomic_load from src into a temporary
15136 followed by __atomic_store of the temporary to dst. */
15138 tree
15139 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15141 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15142 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15144 location_t loc = OMP_CLAUSE_LOCATION (clause);
15145 tree type = TREE_TYPE (dst);
15146 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15147 tree tmp = create_tmp_var (nonatomic_type);
15148 tree tmp_addr = build_fold_addr_expr (tmp);
15149 TREE_ADDRESSABLE (tmp) = 1;
15150 TREE_NO_WARNING (tmp) = 1;
15151 tree src_addr = build_fold_addr_expr (src);
15152 tree dst_addr = build_fold_addr_expr (dst);
15153 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15154 vec<tree, va_gc> *params;
15155 /* Expansion of a generic atomic load may require an addition
15156 element, so allocate enough to prevent a resize. */
15157 vec_alloc (params, 4);
15159 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15160 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15161 params->quick_push (src_addr);
15162 params->quick_push (tmp_addr);
15163 params->quick_push (seq_cst);
15164 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15166 vec_alloc (params, 4);
15168 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15169 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15170 params->quick_push (dst_addr);
15171 params->quick_push (tmp_addr);
15172 params->quick_push (seq_cst);
15173 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15174 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15177 /* Create a transaction node. */
15179 tree
15180 c_finish_transaction (location_t loc, tree block, int flags)
15182 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15183 if (flags & TM_STMT_ATTR_OUTER)
15184 TRANSACTION_EXPR_OUTER (stmt) = 1;
15185 if (flags & TM_STMT_ATTR_RELAXED)
15186 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15187 return add_stmt (stmt);
15190 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15191 down to the element type of an array. If ORIG_QUAL_TYPE is not
15192 NULL, then it should be used as the qualified type
15193 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15194 preserve information about the typedef name from which an array
15195 type was derived). */
15197 tree
15198 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15199 size_t orig_qual_indirect)
15201 if (type == error_mark_node)
15202 return type;
15204 if (TREE_CODE (type) == ARRAY_TYPE)
15206 tree t;
15207 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15208 type_quals, orig_qual_type,
15209 orig_qual_indirect - 1);
15211 /* See if we already have an identically qualified type. */
15212 if (orig_qual_type && orig_qual_indirect == 0)
15213 t = orig_qual_type;
15214 else
15215 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15217 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15218 && TYPE_NAME (t) == TYPE_NAME (type)
15219 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15220 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15221 TYPE_ATTRIBUTES (type)))
15222 break;
15224 if (!t)
15226 tree domain = TYPE_DOMAIN (type);
15228 t = build_variant_type_copy (type);
15229 TREE_TYPE (t) = element_type;
15231 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15232 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15233 SET_TYPE_STRUCTURAL_EQUALITY (t);
15234 else if (TYPE_CANONICAL (element_type) != element_type
15235 || (domain && TYPE_CANONICAL (domain) != domain))
15237 tree unqualified_canon
15238 = build_array_type (TYPE_CANONICAL (element_type),
15239 domain? TYPE_CANONICAL (domain)
15240 : NULL_TREE);
15241 if (TYPE_REVERSE_STORAGE_ORDER (type))
15243 unqualified_canon
15244 = build_distinct_type_copy (unqualified_canon);
15245 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15247 TYPE_CANONICAL (t)
15248 = c_build_qualified_type (unqualified_canon, type_quals);
15250 else
15251 TYPE_CANONICAL (t) = t;
15253 return t;
15256 /* A restrict-qualified pointer type must be a pointer to object or
15257 incomplete type. Note that the use of POINTER_TYPE_P also allows
15258 REFERENCE_TYPEs, which is appropriate for C++. */
15259 if ((type_quals & TYPE_QUAL_RESTRICT)
15260 && (!POINTER_TYPE_P (type)
15261 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15263 error ("invalid use of %<restrict%>");
15264 type_quals &= ~TYPE_QUAL_RESTRICT;
15267 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15268 ? orig_qual_type
15269 : build_qualified_type (type, type_quals));
15270 /* A variant type does not inherit the list of incomplete vars from the
15271 type main variant. */
15272 if ((RECORD_OR_UNION_TYPE_P (var_type)
15273 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15274 && TYPE_MAIN_VARIANT (var_type) != var_type)
15275 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15276 return var_type;
15279 /* Build a VA_ARG_EXPR for the C parser. */
15281 tree
15282 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15284 if (error_operand_p (type))
15285 return error_mark_node;
15286 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15287 order because it takes the address of the expression. */
15288 else if (handled_component_p (expr)
15289 && reverse_storage_order_for_component_p (expr))
15291 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15292 return error_mark_node;
15294 else if (!COMPLETE_TYPE_P (type))
15296 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15297 "type %qT", type);
15298 return error_mark_node;
15300 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15301 warning_at (loc2, OPT_Wc___compat,
15302 "C++ requires promoted type, not enum type, in %<va_arg%>");
15303 return build_va_arg (loc2, expr, type);
15306 /* Return truthvalue of whether T1 is the same tree structure as T2.
15307 Return 1 if they are the same. Return false if they are different. */
15309 bool
15310 c_tree_equal (tree t1, tree t2)
15312 enum tree_code code1, code2;
15314 if (t1 == t2)
15315 return true;
15316 if (!t1 || !t2)
15317 return false;
15319 for (code1 = TREE_CODE (t1);
15320 CONVERT_EXPR_CODE_P (code1)
15321 || code1 == NON_LVALUE_EXPR;
15322 code1 = TREE_CODE (t1))
15323 t1 = TREE_OPERAND (t1, 0);
15324 for (code2 = TREE_CODE (t2);
15325 CONVERT_EXPR_CODE_P (code2)
15326 || code2 == NON_LVALUE_EXPR;
15327 code2 = TREE_CODE (t2))
15328 t2 = TREE_OPERAND (t2, 0);
15330 /* They might have become equal now. */
15331 if (t1 == t2)
15332 return true;
15334 if (code1 != code2)
15335 return false;
15337 switch (code1)
15339 case INTEGER_CST:
15340 return wi::to_wide (t1) == wi::to_wide (t2);
15342 case REAL_CST:
15343 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15345 case STRING_CST:
15346 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15347 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15348 TREE_STRING_LENGTH (t1));
15350 case FIXED_CST:
15351 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15352 TREE_FIXED_CST (t2));
15354 case COMPLEX_CST:
15355 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15356 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15358 case VECTOR_CST:
15359 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15361 case CONSTRUCTOR:
15362 /* We need to do this when determining whether or not two
15363 non-type pointer to member function template arguments
15364 are the same. */
15365 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15366 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15367 return false;
15369 tree field, value;
15370 unsigned int i;
15371 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15373 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15374 if (!c_tree_equal (field, elt2->index)
15375 || !c_tree_equal (value, elt2->value))
15376 return false;
15379 return true;
15381 case TREE_LIST:
15382 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15383 return false;
15384 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15385 return false;
15386 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15388 case SAVE_EXPR:
15389 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15391 case CALL_EXPR:
15393 tree arg1, arg2;
15394 call_expr_arg_iterator iter1, iter2;
15395 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15396 return false;
15397 for (arg1 = first_call_expr_arg (t1, &iter1),
15398 arg2 = first_call_expr_arg (t2, &iter2);
15399 arg1 && arg2;
15400 arg1 = next_call_expr_arg (&iter1),
15401 arg2 = next_call_expr_arg (&iter2))
15402 if (!c_tree_equal (arg1, arg2))
15403 return false;
15404 if (arg1 || arg2)
15405 return false;
15406 return true;
15409 case TARGET_EXPR:
15411 tree o1 = TREE_OPERAND (t1, 0);
15412 tree o2 = TREE_OPERAND (t2, 0);
15414 /* Special case: if either target is an unallocated VAR_DECL,
15415 it means that it's going to be unified with whatever the
15416 TARGET_EXPR is really supposed to initialize, so treat it
15417 as being equivalent to anything. */
15418 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15419 && !DECL_RTL_SET_P (o1))
15420 /*Nop*/;
15421 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15422 && !DECL_RTL_SET_P (o2))
15423 /*Nop*/;
15424 else if (!c_tree_equal (o1, o2))
15425 return false;
15427 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15430 case COMPONENT_REF:
15431 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15432 return false;
15433 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15435 case PARM_DECL:
15436 case VAR_DECL:
15437 case CONST_DECL:
15438 case FIELD_DECL:
15439 case FUNCTION_DECL:
15440 case IDENTIFIER_NODE:
15441 case SSA_NAME:
15442 return false;
15444 case TREE_VEC:
15446 unsigned ix;
15447 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15448 return false;
15449 for (ix = TREE_VEC_LENGTH (t1); ix--;)
15450 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15451 TREE_VEC_ELT (t2, ix)))
15452 return false;
15453 return true;
15456 default:
15457 break;
15460 switch (TREE_CODE_CLASS (code1))
15462 case tcc_unary:
15463 case tcc_binary:
15464 case tcc_comparison:
15465 case tcc_expression:
15466 case tcc_vl_exp:
15467 case tcc_reference:
15468 case tcc_statement:
15470 int i, n = TREE_OPERAND_LENGTH (t1);
15472 switch (code1)
15474 case PREINCREMENT_EXPR:
15475 case PREDECREMENT_EXPR:
15476 case POSTINCREMENT_EXPR:
15477 case POSTDECREMENT_EXPR:
15478 n = 1;
15479 break;
15480 case ARRAY_REF:
15481 n = 2;
15482 break;
15483 default:
15484 break;
15487 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15488 && n != TREE_OPERAND_LENGTH (t2))
15489 return false;
15491 for (i = 0; i < n; ++i)
15492 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15493 return false;
15495 return true;
15498 case tcc_type:
15499 return comptypes (t1, t2);
15500 default:
15501 gcc_unreachable ();
15503 /* We can get here with --disable-checking. */
15504 return false;
15507 /* Returns true when the function declaration FNDECL is implicit,
15508 introduced as a result of a call to an otherwise undeclared
15509 function, and false otherwise. */
15511 bool
15512 c_decl_implicit (const_tree fndecl)
15514 return C_DECL_IMPLICIT (fndecl);