Fix thinko in new warning on type punning for storage order purposes
[official-gcc.git] / gcc / c / c-typeck.c
blobdaa2e12a25ff5ebb2375ae471c662d0468f13a98
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
56 /* Possible cases of implicit bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
58 enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_return
65 /* The level of nesting inside "__alignof__". */
66 int in_alignof;
68 /* The level of nesting inside "sizeof". */
69 int in_sizeof;
71 /* The level of nesting inside "typeof". */
72 int in_typeof;
74 /* True when parsing OpenMP loop expressions. */
75 bool c_in_omp_for;
77 /* The argument of last parsed sizeof expression, only to be tested
78 if expr.original_code == SIZEOF_EXPR. */
79 tree c_last_sizeof_arg;
80 location_t c_last_sizeof_loc;
82 /* Nonzero if we might need to print a "missing braces around
83 initializer" message within this initializer. */
84 static int found_missing_braces;
86 static int require_constant_value;
87 static int require_constant_elements;
89 static bool null_pointer_constant_p (const_tree);
90 static tree qualify_type (tree, tree);
91 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93 static int comp_target_types (location_t, tree, tree);
94 static int function_types_compatible_p (const_tree, const_tree, bool *,
95 bool *);
96 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
97 static tree lookup_field (tree, tree);
98 static int convert_arguments (location_t, vec<location_t>, tree,
99 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
100 tree);
101 static tree pointer_diff (location_t, tree, tree, tree *);
102 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
103 enum impl_conv, bool, tree, tree, int,
104 int = 0);
105 static tree valid_compound_expr_initializer (tree, tree);
106 static void push_string (const char *);
107 static void push_member_name (tree);
108 static int spelling_length (void);
109 static char *print_spelling (char *);
110 static void warning_init (location_t, int, const char *);
111 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
112 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
113 bool, struct obstack *);
114 static void output_pending_init_elements (int, struct obstack *);
115 static bool set_designator (location_t, bool, struct obstack *);
116 static void push_range_stack (tree, struct obstack *);
117 static void add_pending_init (location_t, tree, tree, tree, bool,
118 struct obstack *);
119 static void set_nonincremental_init (struct obstack *);
120 static void set_nonincremental_init_from_string (tree, struct obstack *);
121 static tree find_init_member (tree, struct obstack *);
122 static void readonly_warning (tree, enum lvalue_use);
123 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
124 static void record_maybe_used_decl (tree);
125 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
127 /* Return true if EXP is a null pointer constant, false otherwise. */
129 static bool
130 null_pointer_constant_p (const_tree expr)
132 /* This should really operate on c_expr structures, but they aren't
133 yet available everywhere required. */
134 tree type = TREE_TYPE (expr);
135 return (TREE_CODE (expr) == INTEGER_CST
136 && !TREE_OVERFLOW (expr)
137 && integer_zerop (expr)
138 && (INTEGRAL_TYPE_P (type)
139 || (TREE_CODE (type) == POINTER_TYPE
140 && VOID_TYPE_P (TREE_TYPE (type))
141 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
144 /* EXPR may appear in an unevaluated part of an integer constant
145 expression, but not in an evaluated part. Wrap it in a
146 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
147 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
149 static tree
150 note_integer_operands (tree expr)
152 tree ret;
153 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
155 ret = copy_node (expr);
156 TREE_OVERFLOW (ret) = 1;
158 else
160 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
161 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
163 return ret;
166 /* Having checked whether EXPR may appear in an unevaluated part of an
167 integer constant expression and found that it may, remove any
168 C_MAYBE_CONST_EXPR noting this fact and return the resulting
169 expression. */
171 static inline tree
172 remove_c_maybe_const_expr (tree expr)
174 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
175 return C_MAYBE_CONST_EXPR_EXPR (expr);
176 else
177 return expr;
180 \f/* This is a cache to hold if two types are compatible or not. */
182 struct tagged_tu_seen_cache {
183 const struct tagged_tu_seen_cache * next;
184 const_tree t1;
185 const_tree t2;
186 /* The return value of tagged_types_tu_compatible_p if we had seen
187 these two types already. */
188 int val;
191 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
192 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
194 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
195 does not have an incomplete type. (That includes void types.)
196 LOC is the location of the use. */
198 tree
199 require_complete_type (location_t loc, tree value)
201 tree type = TREE_TYPE (value);
203 if (error_operand_p (value))
204 return error_mark_node;
206 /* First, detect a valid value with a complete type. */
207 if (COMPLETE_TYPE_P (type))
208 return value;
210 c_incomplete_type_error (loc, value, type);
211 return error_mark_node;
214 /* Print an error message for invalid use of an incomplete type.
215 VALUE is the expression that was used (or 0 if that isn't known)
216 and TYPE is the type that was invalid. LOC is the location for
217 the error. */
219 void
220 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
222 /* Avoid duplicate error message. */
223 if (TREE_CODE (type) == ERROR_MARK)
224 return;
226 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
227 error_at (loc, "%qD has an incomplete type %qT", value, type);
228 else
230 retry:
231 /* We must print an error message. Be clever about what it says. */
233 switch (TREE_CODE (type))
235 case RECORD_TYPE:
236 case UNION_TYPE:
237 case ENUMERAL_TYPE:
238 break;
240 case VOID_TYPE:
241 error_at (loc, "invalid use of void expression");
242 return;
244 case ARRAY_TYPE:
245 if (TYPE_DOMAIN (type))
247 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
249 error_at (loc, "invalid use of flexible array member");
250 return;
252 type = TREE_TYPE (type);
253 goto retry;
255 error_at (loc, "invalid use of array with unspecified bounds");
256 return;
258 default:
259 gcc_unreachable ();
262 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
263 error_at (loc, "invalid use of undefined type %qT", type);
264 else
265 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
266 error_at (loc, "invalid use of incomplete typedef %qT", type);
270 /* Given a type, apply default promotions wrt unnamed function
271 arguments and return the new type. */
273 tree
274 c_type_promotes_to (tree type)
276 tree ret = NULL_TREE;
278 if (TYPE_MAIN_VARIANT (type) == float_type_node)
279 ret = double_type_node;
280 else if (c_promoting_integer_type_p (type))
282 /* Preserve unsignedness if not really getting any wider. */
283 if (TYPE_UNSIGNED (type)
284 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
285 ret = unsigned_type_node;
286 else
287 ret = integer_type_node;
290 if (ret != NULL_TREE)
291 return (TYPE_ATOMIC (type)
292 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
293 : ret);
295 return type;
298 /* Return true if between two named address spaces, whether there is a superset
299 named address space that encompasses both address spaces. If there is a
300 superset, return which address space is the superset. */
302 static bool
303 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
305 if (as1 == as2)
307 *common = as1;
308 return true;
310 else if (targetm.addr_space.subset_p (as1, as2))
312 *common = as2;
313 return true;
315 else if (targetm.addr_space.subset_p (as2, as1))
317 *common = as1;
318 return true;
320 else
321 return false;
324 /* Return a variant of TYPE which has all the type qualifiers of LIKE
325 as well as those of TYPE. */
327 static tree
328 qualify_type (tree type, tree like)
330 addr_space_t as_type = TYPE_ADDR_SPACE (type);
331 addr_space_t as_like = TYPE_ADDR_SPACE (like);
332 addr_space_t as_common;
334 /* If the two named address spaces are different, determine the common
335 superset address space. If there isn't one, raise an error. */
336 if (!addr_space_superset (as_type, as_like, &as_common))
338 as_common = as_type;
339 error ("%qT and %qT are in disjoint named address spaces",
340 type, like);
343 return c_build_qualified_type (type,
344 TYPE_QUALS_NO_ADDR_SPACE (type)
345 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
346 | ENCODE_QUAL_ADDR_SPACE (as_common));
349 /* Return true iff the given tree T is a variable length array. */
351 bool
352 c_vla_type_p (const_tree t)
354 if (TREE_CODE (t) == ARRAY_TYPE
355 && C_TYPE_VARIABLE_SIZE (t))
356 return true;
357 return false;
360 /* If NTYPE is a type of a non-variadic function with a prototype
361 and OTYPE is a type of a function without a prototype and ATTRS
362 contains attribute format, diagnosess and removes it from ATTRS.
363 Returns the result of build_type_attribute_variant of NTYPE and
364 the (possibly) modified ATTRS. */
366 static tree
367 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
369 if (!prototype_p (otype)
370 && prototype_p (ntype)
371 && lookup_attribute ("format", attrs))
373 warning_at (input_location, OPT_Wattributes,
374 "%qs attribute cannot be applied to a function that "
375 "does not take variable arguments", "format");
376 attrs = remove_attribute ("format", attrs);
378 return build_type_attribute_variant (ntype, attrs);
381 /* Return the composite type of two compatible types.
383 We assume that comptypes has already been done and returned
384 nonzero; if that isn't so, this may crash. In particular, we
385 assume that qualifiers match. */
387 tree
388 composite_type (tree t1, tree t2)
390 enum tree_code code1;
391 enum tree_code code2;
392 tree attributes;
394 /* Save time if the two types are the same. */
396 if (t1 == t2) return t1;
398 /* If one type is nonsense, use the other. */
399 if (t1 == error_mark_node)
400 return t2;
401 if (t2 == error_mark_node)
402 return t1;
404 code1 = TREE_CODE (t1);
405 code2 = TREE_CODE (t2);
407 /* Merge the attributes. */
408 attributes = targetm.merge_type_attributes (t1, t2);
410 /* If one is an enumerated type and the other is the compatible
411 integer type, the composite type might be either of the two
412 (DR#013 question 3). For consistency, use the enumerated type as
413 the composite type. */
415 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
416 return t1;
417 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
418 return t2;
420 gcc_assert (code1 == code2);
422 switch (code1)
424 case POINTER_TYPE:
425 /* For two pointers, do this recursively on the target type. */
427 tree pointed_to_1 = TREE_TYPE (t1);
428 tree pointed_to_2 = TREE_TYPE (t2);
429 tree target = composite_type (pointed_to_1, pointed_to_2);
430 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
431 t1 = build_type_attribute_variant (t1, attributes);
432 return qualify_type (t1, t2);
435 case ARRAY_TYPE:
437 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
438 int quals;
439 tree unqual_elt;
440 tree d1 = TYPE_DOMAIN (t1);
441 tree d2 = TYPE_DOMAIN (t2);
442 bool d1_variable, d2_variable;
443 bool d1_zero, d2_zero;
444 bool t1_complete, t2_complete;
446 /* We should not have any type quals on arrays at all. */
447 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
448 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
450 t1_complete = COMPLETE_TYPE_P (t1);
451 t2_complete = COMPLETE_TYPE_P (t2);
453 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
454 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
456 d1_variable = (!d1_zero
457 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
458 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
459 d2_variable = (!d2_zero
460 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
461 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
462 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
463 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
465 /* Save space: see if the result is identical to one of the args. */
466 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
467 && (d2_variable || d2_zero || !d1_variable))
468 return build_type_attribute_variant (t1, attributes);
469 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
470 && (d1_variable || d1_zero || !d2_variable))
471 return build_type_attribute_variant (t2, attributes);
473 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
474 return build_type_attribute_variant (t1, attributes);
475 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
476 return build_type_attribute_variant (t2, attributes);
478 /* Merge the element types, and have a size if either arg has
479 one. We may have qualifiers on the element types. To set
480 up TYPE_MAIN_VARIANT correctly, we need to form the
481 composite of the unqualified types and add the qualifiers
482 back at the end. */
483 quals = TYPE_QUALS (strip_array_types (elt));
484 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
485 t1 = build_array_type (unqual_elt,
486 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
487 && (d2_variable
488 || d2_zero
489 || !d1_variable))
490 ? t1
491 : t2));
492 /* Ensure a composite type involving a zero-length array type
493 is a zero-length type not an incomplete type. */
494 if (d1_zero && d2_zero
495 && (t1_complete || t2_complete)
496 && !COMPLETE_TYPE_P (t1))
498 TYPE_SIZE (t1) = bitsize_zero_node;
499 TYPE_SIZE_UNIT (t1) = size_zero_node;
501 t1 = c_build_qualified_type (t1, quals);
502 return build_type_attribute_variant (t1, attributes);
505 case ENUMERAL_TYPE:
506 case RECORD_TYPE:
507 case UNION_TYPE:
508 if (attributes != NULL)
510 /* Try harder not to create a new aggregate type. */
511 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
512 return t1;
513 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
514 return t2;
516 return build_type_attribute_variant (t1, attributes);
518 case FUNCTION_TYPE:
519 /* Function types: prefer the one that specified arg types.
520 If both do, merge the arg types. Also merge the return types. */
522 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
523 tree p1 = TYPE_ARG_TYPES (t1);
524 tree p2 = TYPE_ARG_TYPES (t2);
525 int len;
526 tree newargs, n;
527 int i;
529 /* Save space: see if the result is identical to one of the args. */
530 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
531 return build_functype_attribute_variant (t1, t2, attributes);
532 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
533 return build_functype_attribute_variant (t2, t1, attributes);
535 /* Simple way if one arg fails to specify argument types. */
536 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
538 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
539 t1 = build_type_attribute_variant (t1, attributes);
540 return qualify_type (t1, t2);
542 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
544 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
545 t1 = build_type_attribute_variant (t1, attributes);
546 return qualify_type (t1, t2);
549 /* If both args specify argument types, we must merge the two
550 lists, argument by argument. */
552 for (len = 0, newargs = p1;
553 newargs && newargs != void_list_node;
554 len++, newargs = TREE_CHAIN (newargs))
557 for (i = 0; i < len; i++)
558 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
560 n = newargs;
562 for (; p1 && p1 != void_list_node;
563 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
565 /* A null type means arg type is not specified.
566 Take whatever the other function type has. */
567 if (TREE_VALUE (p1) == NULL_TREE)
569 TREE_VALUE (n) = TREE_VALUE (p2);
570 goto parm_done;
572 if (TREE_VALUE (p2) == NULL_TREE)
574 TREE_VALUE (n) = TREE_VALUE (p1);
575 goto parm_done;
578 /* Given wait (union {union wait *u; int *i} *)
579 and wait (union wait *),
580 prefer union wait * as type of parm. */
581 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
582 && TREE_VALUE (p1) != TREE_VALUE (p2))
584 tree memb;
585 tree mv2 = TREE_VALUE (p2);
586 if (mv2 && mv2 != error_mark_node
587 && TREE_CODE (mv2) != ARRAY_TYPE)
588 mv2 = TYPE_MAIN_VARIANT (mv2);
589 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
590 memb; memb = DECL_CHAIN (memb))
592 tree mv3 = TREE_TYPE (memb);
593 if (mv3 && mv3 != error_mark_node
594 && TREE_CODE (mv3) != ARRAY_TYPE)
595 mv3 = TYPE_MAIN_VARIANT (mv3);
596 if (comptypes (mv3, mv2))
598 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
599 TREE_VALUE (p2));
600 pedwarn (input_location, OPT_Wpedantic,
601 "function types not truly compatible in ISO C");
602 goto parm_done;
606 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
607 && TREE_VALUE (p2) != TREE_VALUE (p1))
609 tree memb;
610 tree mv1 = TREE_VALUE (p1);
611 if (mv1 && mv1 != error_mark_node
612 && TREE_CODE (mv1) != ARRAY_TYPE)
613 mv1 = TYPE_MAIN_VARIANT (mv1);
614 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
615 memb; memb = DECL_CHAIN (memb))
617 tree mv3 = TREE_TYPE (memb);
618 if (mv3 && mv3 != error_mark_node
619 && TREE_CODE (mv3) != ARRAY_TYPE)
620 mv3 = TYPE_MAIN_VARIANT (mv3);
621 if (comptypes (mv3, mv1))
623 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
624 TREE_VALUE (p1));
625 pedwarn (input_location, OPT_Wpedantic,
626 "function types not truly compatible in ISO C");
627 goto parm_done;
631 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
632 parm_done: ;
635 t1 = build_function_type (valtype, newargs);
636 t1 = qualify_type (t1, t2);
638 /* FALLTHRU */
640 default:
641 return build_type_attribute_variant (t1, attributes);
646 /* Return the type of a conditional expression between pointers to
647 possibly differently qualified versions of compatible types.
649 We assume that comp_target_types has already been done and returned
650 nonzero; if that isn't so, this may crash. */
652 static tree
653 common_pointer_type (tree t1, tree t2)
655 tree attributes;
656 tree pointed_to_1, mv1;
657 tree pointed_to_2, mv2;
658 tree target;
659 unsigned target_quals;
660 addr_space_t as1, as2, as_common;
661 int quals1, quals2;
663 /* Save time if the two types are the same. */
665 if (t1 == t2) return t1;
667 /* If one type is nonsense, use the other. */
668 if (t1 == error_mark_node)
669 return t2;
670 if (t2 == error_mark_node)
671 return t1;
673 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
674 && TREE_CODE (t2) == POINTER_TYPE);
676 /* Merge the attributes. */
677 attributes = targetm.merge_type_attributes (t1, t2);
679 /* Find the composite type of the target types, and combine the
680 qualifiers of the two types' targets. Do not lose qualifiers on
681 array element types by taking the TYPE_MAIN_VARIANT. */
682 mv1 = pointed_to_1 = TREE_TYPE (t1);
683 mv2 = pointed_to_2 = TREE_TYPE (t2);
684 if (TREE_CODE (mv1) != ARRAY_TYPE)
685 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
686 if (TREE_CODE (mv2) != ARRAY_TYPE)
687 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
688 target = composite_type (mv1, mv2);
690 /* Strip array types to get correct qualifier for pointers to arrays */
691 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
692 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
694 /* For function types do not merge const qualifiers, but drop them
695 if used inconsistently. The middle-end uses these to mark const
696 and noreturn functions. */
697 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
698 target_quals = (quals1 & quals2);
699 else
700 target_quals = (quals1 | quals2);
702 /* If the two named address spaces are different, determine the common
703 superset address space. This is guaranteed to exist due to the
704 assumption that comp_target_type returned non-zero. */
705 as1 = TYPE_ADDR_SPACE (pointed_to_1);
706 as2 = TYPE_ADDR_SPACE (pointed_to_2);
707 if (!addr_space_superset (as1, as2, &as_common))
708 gcc_unreachable ();
710 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
712 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
713 return build_type_attribute_variant (t1, attributes);
716 /* Return the common type for two arithmetic types under the usual
717 arithmetic conversions. The default conversions have already been
718 applied, and enumerated types converted to their compatible integer
719 types. The resulting type is unqualified and has no attributes.
721 This is the type for the result of most arithmetic operations
722 if the operands have the given two types. */
724 static tree
725 c_common_type (tree t1, tree t2)
727 enum tree_code code1;
728 enum tree_code code2;
730 /* If one type is nonsense, use the other. */
731 if (t1 == error_mark_node)
732 return t2;
733 if (t2 == error_mark_node)
734 return t1;
736 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
737 t1 = TYPE_MAIN_VARIANT (t1);
739 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
740 t2 = TYPE_MAIN_VARIANT (t2);
742 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
744 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
745 t1 = build_type_attribute_variant (t1, attrs);
748 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
750 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
751 t2 = build_type_attribute_variant (t2, attrs);
754 /* Save time if the two types are the same. */
756 if (t1 == t2) return t1;
758 code1 = TREE_CODE (t1);
759 code2 = TREE_CODE (t2);
761 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
762 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
763 || code1 == INTEGER_TYPE);
764 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
765 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
766 || code2 == INTEGER_TYPE);
768 /* When one operand is a decimal float type, the other operand cannot be
769 a generic float type or a complex type. We also disallow vector types
770 here. */
771 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
772 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
774 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
776 error ("cannot mix operands of decimal floating and vector types");
777 return error_mark_node;
779 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
781 error ("cannot mix operands of decimal floating and complex types");
782 return error_mark_node;
784 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
786 error ("cannot mix operands of decimal floating "
787 "and other floating types");
788 return error_mark_node;
792 /* If one type is a vector type, return that type. (How the usual
793 arithmetic conversions apply to the vector types extension is not
794 precisely specified.) */
795 if (code1 == VECTOR_TYPE)
796 return t1;
798 if (code2 == VECTOR_TYPE)
799 return t2;
801 /* If one type is complex, form the common type of the non-complex
802 components, then make that complex. Use T1 or T2 if it is the
803 required type. */
804 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
806 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
807 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
808 tree subtype = c_common_type (subtype1, subtype2);
810 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
811 return t1;
812 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
813 return t2;
814 else
815 return build_complex_type (subtype);
818 /* If only one is real, use it as the result. */
820 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
821 return t1;
823 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
824 return t2;
826 /* If both are real and either are decimal floating point types, use
827 the decimal floating point type with the greater precision. */
829 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
831 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
832 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
833 return dfloat128_type_node;
834 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
835 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
836 return dfloat64_type_node;
837 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
838 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
839 return dfloat32_type_node;
842 /* Deal with fixed-point types. */
843 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
845 unsigned int unsignedp = 0, satp = 0;
846 scalar_mode m1, m2;
847 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
849 m1 = SCALAR_TYPE_MODE (t1);
850 m2 = SCALAR_TYPE_MODE (t2);
852 /* If one input type is saturating, the result type is saturating. */
853 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
854 satp = 1;
856 /* If both fixed-point types are unsigned, the result type is unsigned.
857 When mixing fixed-point and integer types, follow the sign of the
858 fixed-point type.
859 Otherwise, the result type is signed. */
860 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
861 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
862 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
863 && TYPE_UNSIGNED (t1))
864 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
865 && TYPE_UNSIGNED (t2)))
866 unsignedp = 1;
868 /* The result type is signed. */
869 if (unsignedp == 0)
871 /* If the input type is unsigned, we need to convert to the
872 signed type. */
873 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
875 enum mode_class mclass = (enum mode_class) 0;
876 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
877 mclass = MODE_FRACT;
878 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
879 mclass = MODE_ACCUM;
880 else
881 gcc_unreachable ();
882 m1 = as_a <scalar_mode>
883 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
885 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
887 enum mode_class mclass = (enum mode_class) 0;
888 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
889 mclass = MODE_FRACT;
890 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
891 mclass = MODE_ACCUM;
892 else
893 gcc_unreachable ();
894 m2 = as_a <scalar_mode>
895 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
899 if (code1 == FIXED_POINT_TYPE)
901 fbit1 = GET_MODE_FBIT (m1);
902 ibit1 = GET_MODE_IBIT (m1);
904 else
906 fbit1 = 0;
907 /* Signed integers need to subtract one sign bit. */
908 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
911 if (code2 == FIXED_POINT_TYPE)
913 fbit2 = GET_MODE_FBIT (m2);
914 ibit2 = GET_MODE_IBIT (m2);
916 else
918 fbit2 = 0;
919 /* Signed integers need to subtract one sign bit. */
920 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
923 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
924 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
925 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
926 satp);
929 /* Both real or both integers; use the one with greater precision. */
931 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
932 return t1;
933 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
934 return t2;
936 /* Same precision. Prefer long longs to longs to ints when the
937 same precision, following the C99 rules on integer type rank
938 (which are equivalent to the C90 rules for C90 types). */
940 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
941 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
942 return long_long_unsigned_type_node;
944 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
945 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
947 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
948 return long_long_unsigned_type_node;
949 else
950 return long_long_integer_type_node;
953 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
954 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
955 return long_unsigned_type_node;
957 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
958 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
960 /* But preserve unsignedness from the other type,
961 since long cannot hold all the values of an unsigned int. */
962 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
963 return long_unsigned_type_node;
964 else
965 return long_integer_type_node;
968 /* For floating types of the same TYPE_PRECISION (which we here
969 assume means either the same set of values, or sets of values
970 neither a subset of the other, with behavior being undefined in
971 the latter case), follow the rules from TS 18661-3: prefer
972 interchange types _FloatN, then standard types long double,
973 double, float, then extended types _FloatNx. For extended types,
974 check them starting with _Float128x as that seems most consistent
975 in spirit with preferring long double to double; for interchange
976 types, also check in that order for consistency although it's not
977 possible for more than one of them to have the same
978 precision. */
979 tree mv1 = TYPE_MAIN_VARIANT (t1);
980 tree mv2 = TYPE_MAIN_VARIANT (t2);
982 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
983 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
984 return FLOATN_TYPE_NODE (i);
986 /* Likewise, prefer long double to double even if same size. */
987 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
988 return long_double_type_node;
990 /* Likewise, prefer double to float even if same size.
991 We got a couple of embedded targets with 32 bit doubles, and the
992 pdp11 might have 64 bit floats. */
993 if (mv1 == double_type_node || mv2 == double_type_node)
994 return double_type_node;
996 if (mv1 == float_type_node || mv2 == float_type_node)
997 return float_type_node;
999 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1000 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1001 return FLOATNX_TYPE_NODE (i);
1003 /* Otherwise prefer the unsigned one. */
1005 if (TYPE_UNSIGNED (t1))
1006 return t1;
1007 else
1008 return t2;
1011 /* Wrapper around c_common_type that is used by c-common.c and other
1012 front end optimizations that remove promotions. ENUMERAL_TYPEs
1013 are allowed here and are converted to their compatible integer types.
1014 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1015 preferably a non-Boolean type as the common type. */
1016 tree
1017 common_type (tree t1, tree t2)
1019 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1020 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1021 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1022 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1024 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1025 if (TREE_CODE (t1) == BOOLEAN_TYPE
1026 && TREE_CODE (t2) == BOOLEAN_TYPE)
1027 return boolean_type_node;
1029 /* If either type is BOOLEAN_TYPE, then return the other. */
1030 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1031 return t2;
1032 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1033 return t1;
1035 return c_common_type (t1, t2);
1038 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1039 or various other operations. Return 2 if they are compatible
1040 but a warning may be needed if you use them together. */
1043 comptypes (tree type1, tree type2)
1045 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1046 int val;
1048 val = comptypes_internal (type1, type2, NULL, NULL);
1049 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1051 return val;
1054 /* Like comptypes, but if it returns non-zero because enum and int are
1055 compatible, it sets *ENUM_AND_INT_P to true. */
1057 static int
1058 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1060 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1061 int val;
1063 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1064 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1066 return val;
1069 /* Like comptypes, but if it returns nonzero for different types, it
1070 sets *DIFFERENT_TYPES_P to true. */
1073 comptypes_check_different_types (tree type1, tree type2,
1074 bool *different_types_p)
1076 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1077 int val;
1079 val = comptypes_internal (type1, type2, NULL, different_types_p);
1080 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1082 return val;
1085 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1086 or various other operations. Return 2 if they are compatible
1087 but a warning may be needed if you use them together. If
1088 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1089 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1090 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1091 NULL, and the types are compatible but different enough not to be
1092 permitted in C11 typedef redeclarations, then this sets
1093 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1094 false, but may or may not be set if the types are incompatible.
1095 This differs from comptypes, in that we don't free the seen
1096 types. */
1098 static int
1099 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1100 bool *different_types_p)
1102 const_tree t1 = type1;
1103 const_tree t2 = type2;
1104 int attrval, val;
1106 /* Suppress errors caused by previously reported errors. */
1108 if (t1 == t2 || !t1 || !t2
1109 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1110 return 1;
1112 /* Enumerated types are compatible with integer types, but this is
1113 not transitive: two enumerated types in the same translation unit
1114 are compatible with each other only if they are the same type. */
1116 if (TREE_CODE (t1) == ENUMERAL_TYPE
1117 && COMPLETE_TYPE_P (t1)
1118 && TREE_CODE (t2) != ENUMERAL_TYPE)
1120 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1121 if (TREE_CODE (t2) != VOID_TYPE)
1123 if (enum_and_int_p != NULL)
1124 *enum_and_int_p = true;
1125 if (different_types_p != NULL)
1126 *different_types_p = true;
1129 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1130 && COMPLETE_TYPE_P (t2)
1131 && TREE_CODE (t1) != ENUMERAL_TYPE)
1133 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1134 if (TREE_CODE (t1) != VOID_TYPE)
1136 if (enum_and_int_p != NULL)
1137 *enum_and_int_p = true;
1138 if (different_types_p != NULL)
1139 *different_types_p = true;
1143 if (t1 == t2)
1144 return 1;
1146 /* Different classes of types can't be compatible. */
1148 if (TREE_CODE (t1) != TREE_CODE (t2))
1149 return 0;
1151 /* Qualifiers must match. C99 6.7.3p9 */
1153 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1154 return 0;
1156 /* Allow for two different type nodes which have essentially the same
1157 definition. Note that we already checked for equality of the type
1158 qualifiers (just above). */
1160 if (TREE_CODE (t1) != ARRAY_TYPE
1161 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1162 return 1;
1164 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1165 if (!(attrval = comp_type_attributes (t1, t2)))
1166 return 0;
1168 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1169 val = 0;
1171 switch (TREE_CODE (t1))
1173 case INTEGER_TYPE:
1174 case FIXED_POINT_TYPE:
1175 case REAL_TYPE:
1176 /* With these nodes, we can't determine type equivalence by
1177 looking at what is stored in the nodes themselves, because
1178 two nodes might have different TYPE_MAIN_VARIANTs but still
1179 represent the same type. For example, wchar_t and int could
1180 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1181 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1182 and are distinct types. On the other hand, int and the
1183 following typedef
1185 typedef int INT __attribute((may_alias));
1187 have identical properties, different TYPE_MAIN_VARIANTs, but
1188 represent the same type. The canonical type system keeps
1189 track of equivalence in this case, so we fall back on it. */
1190 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1192 case POINTER_TYPE:
1193 /* Do not remove mode information. */
1194 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1195 break;
1196 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1197 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1198 enum_and_int_p, different_types_p));
1199 break;
1201 case FUNCTION_TYPE:
1202 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1203 different_types_p);
1204 break;
1206 case ARRAY_TYPE:
1208 tree d1 = TYPE_DOMAIN (t1);
1209 tree d2 = TYPE_DOMAIN (t2);
1210 bool d1_variable, d2_variable;
1211 bool d1_zero, d2_zero;
1212 val = 1;
1214 /* Target types must match incl. qualifiers. */
1215 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1216 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1217 enum_and_int_p,
1218 different_types_p)) == 0)
1219 return 0;
1221 if (different_types_p != NULL
1222 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1223 *different_types_p = true;
1224 /* Sizes must match unless one is missing or variable. */
1225 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1226 break;
1228 d1_zero = !TYPE_MAX_VALUE (d1);
1229 d2_zero = !TYPE_MAX_VALUE (d2);
1231 d1_variable = (!d1_zero
1232 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1233 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1234 d2_variable = (!d2_zero
1235 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1236 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1237 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1238 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1240 if (different_types_p != NULL
1241 && d1_variable != d2_variable)
1242 *different_types_p = true;
1243 if (d1_variable || d2_variable)
1244 break;
1245 if (d1_zero && d2_zero)
1246 break;
1247 if (d1_zero || d2_zero
1248 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1249 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1250 val = 0;
1252 break;
1255 case ENUMERAL_TYPE:
1256 case RECORD_TYPE:
1257 case UNION_TYPE:
1258 if (val != 1 && !same_translation_unit_p (t1, t2))
1260 tree a1 = TYPE_ATTRIBUTES (t1);
1261 tree a2 = TYPE_ATTRIBUTES (t2);
1263 if (! attribute_list_contained (a1, a2)
1264 && ! attribute_list_contained (a2, a1))
1265 break;
1267 if (attrval != 2)
1268 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1269 different_types_p);
1270 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1271 different_types_p);
1273 break;
1275 case VECTOR_TYPE:
1276 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1277 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1278 enum_and_int_p, different_types_p));
1279 break;
1281 default:
1282 break;
1284 return attrval == 2 && val == 1 ? 2 : val;
1287 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1288 their qualifiers, except for named address spaces. If the pointers point to
1289 different named addresses, then we must determine if one address space is a
1290 subset of the other. */
1292 static int
1293 comp_target_types (location_t location, tree ttl, tree ttr)
1295 int val;
1296 int val_ped;
1297 tree mvl = TREE_TYPE (ttl);
1298 tree mvr = TREE_TYPE (ttr);
1299 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1300 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1301 addr_space_t as_common;
1302 bool enum_and_int_p;
1304 /* Fail if pointers point to incompatible address spaces. */
1305 if (!addr_space_superset (asl, asr, &as_common))
1306 return 0;
1308 /* For pedantic record result of comptypes on arrays before losing
1309 qualifiers on the element type below. */
1310 val_ped = 1;
1312 if (TREE_CODE (mvl) == ARRAY_TYPE
1313 && TREE_CODE (mvr) == ARRAY_TYPE)
1314 val_ped = comptypes (mvl, mvr);
1316 /* Qualifiers on element types of array types that are
1317 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1319 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1320 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1321 : TYPE_MAIN_VARIANT (mvl));
1323 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1324 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1325 : TYPE_MAIN_VARIANT (mvr));
1327 enum_and_int_p = false;
1328 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1330 if (val == 1 && val_ped != 1)
1331 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1332 "are incompatible in ISO C");
1334 if (val == 2)
1335 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1337 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1338 warning_at (location, OPT_Wc___compat,
1339 "pointer target types incompatible in C++");
1341 return val;
1344 /* Subroutines of `comptypes'. */
1346 /* Determine whether two trees derive from the same translation unit.
1347 If the CONTEXT chain ends in a null, that tree's context is still
1348 being parsed, so if two trees have context chains ending in null,
1349 they're in the same translation unit. */
1351 bool
1352 same_translation_unit_p (const_tree t1, const_tree t2)
1354 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1355 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1357 case tcc_declaration:
1358 t1 = DECL_CONTEXT (t1); break;
1359 case tcc_type:
1360 t1 = TYPE_CONTEXT (t1); break;
1361 case tcc_exceptional:
1362 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1363 default: gcc_unreachable ();
1366 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1367 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1369 case tcc_declaration:
1370 t2 = DECL_CONTEXT (t2); break;
1371 case tcc_type:
1372 t2 = TYPE_CONTEXT (t2); break;
1373 case tcc_exceptional:
1374 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1375 default: gcc_unreachable ();
1378 return t1 == t2;
1381 /* Allocate the seen two types, assuming that they are compatible. */
1383 static struct tagged_tu_seen_cache *
1384 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1386 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1387 tu->next = tagged_tu_seen_base;
1388 tu->t1 = t1;
1389 tu->t2 = t2;
1391 tagged_tu_seen_base = tu;
1393 /* The C standard says that two structures in different translation
1394 units are compatible with each other only if the types of their
1395 fields are compatible (among other things). We assume that they
1396 are compatible until proven otherwise when building the cache.
1397 An example where this can occur is:
1398 struct a
1400 struct a *next;
1402 If we are comparing this against a similar struct in another TU,
1403 and did not assume they were compatible, we end up with an infinite
1404 loop. */
1405 tu->val = 1;
1406 return tu;
1409 /* Free the seen types until we get to TU_TIL. */
1411 static void
1412 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1414 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1415 while (tu != tu_til)
1417 const struct tagged_tu_seen_cache *const tu1
1418 = (const struct tagged_tu_seen_cache *) tu;
1419 tu = tu1->next;
1420 XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1422 tagged_tu_seen_base = tu_til;
1425 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1426 compatible. If the two types are not the same (which has been
1427 checked earlier), this can only happen when multiple translation
1428 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1429 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1430 comptypes_internal. */
1432 static int
1433 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1434 bool *enum_and_int_p, bool *different_types_p)
1436 tree s1, s2;
1437 bool needs_warning = false;
1439 /* We have to verify that the tags of the types are the same. This
1440 is harder than it looks because this may be a typedef, so we have
1441 to go look at the original type. It may even be a typedef of a
1442 typedef...
1443 In the case of compiler-created builtin structs the TYPE_DECL
1444 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1445 while (TYPE_NAME (t1)
1446 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1447 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1448 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1450 while (TYPE_NAME (t2)
1451 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1452 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1453 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1455 /* C90 didn't have the requirement that the two tags be the same. */
1456 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1457 return 0;
1459 /* C90 didn't say what happened if one or both of the types were
1460 incomplete; we choose to follow C99 rules here, which is that they
1461 are compatible. */
1462 if (TYPE_SIZE (t1) == NULL
1463 || TYPE_SIZE (t2) == NULL)
1464 return 1;
1467 const struct tagged_tu_seen_cache * tts_i;
1468 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1469 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1470 return tts_i->val;
1473 switch (TREE_CODE (t1))
1475 case ENUMERAL_TYPE:
1477 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1478 /* Speed up the case where the type values are in the same order. */
1479 tree tv1 = TYPE_VALUES (t1);
1480 tree tv2 = TYPE_VALUES (t2);
1482 if (tv1 == tv2)
1484 return 1;
1487 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1489 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1490 break;
1491 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1493 tu->val = 0;
1494 return 0;
1498 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1500 return 1;
1502 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1504 tu->val = 0;
1505 return 0;
1508 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1510 tu->val = 0;
1511 return 0;
1514 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1516 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1517 if (s2 == NULL
1518 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1520 tu->val = 0;
1521 return 0;
1524 return 1;
1527 case UNION_TYPE:
1529 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1530 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1532 tu->val = 0;
1533 return 0;
1536 /* Speed up the common case where the fields are in the same order. */
1537 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1538 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1540 int result;
1542 if (DECL_NAME (s1) != DECL_NAME (s2))
1543 break;
1544 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1545 enum_and_int_p, different_types_p);
1547 if (result != 1 && !DECL_NAME (s1))
1548 break;
1549 if (result == 0)
1551 tu->val = 0;
1552 return 0;
1554 if (result == 2)
1555 needs_warning = true;
1557 if (TREE_CODE (s1) == FIELD_DECL
1558 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1559 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1561 tu->val = 0;
1562 return 0;
1565 if (!s1 && !s2)
1567 tu->val = needs_warning ? 2 : 1;
1568 return tu->val;
1571 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1573 bool ok = false;
1575 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1576 if (DECL_NAME (s1) == DECL_NAME (s2))
1578 int result;
1580 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1581 enum_and_int_p,
1582 different_types_p);
1584 if (result != 1 && !DECL_NAME (s1))
1585 continue;
1586 if (result == 0)
1588 tu->val = 0;
1589 return 0;
1591 if (result == 2)
1592 needs_warning = true;
1594 if (TREE_CODE (s1) == FIELD_DECL
1595 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1596 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1597 break;
1599 ok = true;
1600 break;
1602 if (!ok)
1604 tu->val = 0;
1605 return 0;
1608 tu->val = needs_warning ? 2 : 10;
1609 return tu->val;
1612 case RECORD_TYPE:
1614 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1616 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1617 s1 && s2;
1618 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1620 int result;
1621 if (TREE_CODE (s1) != TREE_CODE (s2)
1622 || DECL_NAME (s1) != DECL_NAME (s2))
1623 break;
1624 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1625 enum_and_int_p, different_types_p);
1626 if (result == 0)
1627 break;
1628 if (result == 2)
1629 needs_warning = true;
1631 if (TREE_CODE (s1) == FIELD_DECL
1632 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1633 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1634 break;
1636 if (s1 && s2)
1637 tu->val = 0;
1638 else
1639 tu->val = needs_warning ? 2 : 1;
1640 return tu->val;
1643 default:
1644 gcc_unreachable ();
1648 /* Return 1 if two function types F1 and F2 are compatible.
1649 If either type specifies no argument types,
1650 the other must specify a fixed number of self-promoting arg types.
1651 Otherwise, if one type specifies only the number of arguments,
1652 the other must specify that number of self-promoting arg types.
1653 Otherwise, the argument types must match.
1654 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1656 static int
1657 function_types_compatible_p (const_tree f1, const_tree f2,
1658 bool *enum_and_int_p, bool *different_types_p)
1660 tree args1, args2;
1661 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1662 int val = 1;
1663 int val1;
1664 tree ret1, ret2;
1666 ret1 = TREE_TYPE (f1);
1667 ret2 = TREE_TYPE (f2);
1669 /* 'volatile' qualifiers on a function's return type used to mean
1670 the function is noreturn. */
1671 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1672 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1673 if (TYPE_VOLATILE (ret1))
1674 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1675 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1676 if (TYPE_VOLATILE (ret2))
1677 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1678 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1679 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1680 if (val == 0)
1681 return 0;
1683 args1 = TYPE_ARG_TYPES (f1);
1684 args2 = TYPE_ARG_TYPES (f2);
1686 if (different_types_p != NULL
1687 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1688 *different_types_p = true;
1690 /* An unspecified parmlist matches any specified parmlist
1691 whose argument types don't need default promotions. */
1693 if (args1 == NULL_TREE)
1695 if (flag_isoc2x ? stdarg_p (f2) : !self_promoting_args_p (args2))
1696 return 0;
1697 /* If one of these types comes from a non-prototype fn definition,
1698 compare that with the other type's arglist.
1699 If they don't match, ask for a warning (but no error). */
1700 if (TYPE_ACTUAL_ARG_TYPES (f1)
1701 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1702 enum_and_int_p, different_types_p) != 1)
1703 val = 2;
1704 return val;
1706 if (args2 == NULL_TREE)
1708 if (flag_isoc2x ? stdarg_p (f1) : !self_promoting_args_p (args1))
1709 return 0;
1710 if (TYPE_ACTUAL_ARG_TYPES (f2)
1711 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1712 enum_and_int_p, different_types_p) != 1)
1713 val = 2;
1714 return val;
1717 /* Both types have argument lists: compare them and propagate results. */
1718 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1719 different_types_p);
1720 return val1 != 1 ? val1 : val;
1723 /* Check two lists of types for compatibility, returning 0 for
1724 incompatible, 1 for compatible, or 2 for compatible with
1725 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1726 comptypes_internal. */
1728 static int
1729 type_lists_compatible_p (const_tree args1, const_tree args2,
1730 bool *enum_and_int_p, bool *different_types_p)
1732 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1733 int val = 1;
1734 int newval = 0;
1736 while (1)
1738 tree a1, mv1, a2, mv2;
1739 if (args1 == NULL_TREE && args2 == NULL_TREE)
1740 return val;
1741 /* If one list is shorter than the other,
1742 they fail to match. */
1743 if (args1 == NULL_TREE || args2 == NULL_TREE)
1744 return 0;
1745 mv1 = a1 = TREE_VALUE (args1);
1746 mv2 = a2 = TREE_VALUE (args2);
1747 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1748 mv1 = (TYPE_ATOMIC (mv1)
1749 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1750 TYPE_QUAL_ATOMIC)
1751 : TYPE_MAIN_VARIANT (mv1));
1752 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1753 mv2 = (TYPE_ATOMIC (mv2)
1754 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1755 TYPE_QUAL_ATOMIC)
1756 : TYPE_MAIN_VARIANT (mv2));
1757 /* A null pointer instead of a type
1758 means there is supposed to be an argument
1759 but nothing is specified about what type it has.
1760 So match anything that self-promotes. */
1761 if (different_types_p != NULL
1762 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1763 *different_types_p = true;
1764 if (a1 == NULL_TREE)
1766 if (c_type_promotes_to (a2) != a2)
1767 return 0;
1769 else if (a2 == NULL_TREE)
1771 if (c_type_promotes_to (a1) != a1)
1772 return 0;
1774 /* If one of the lists has an error marker, ignore this arg. */
1775 else if (TREE_CODE (a1) == ERROR_MARK
1776 || TREE_CODE (a2) == ERROR_MARK)
1778 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1779 different_types_p)))
1781 if (different_types_p != NULL)
1782 *different_types_p = true;
1783 /* Allow wait (union {union wait *u; int *i} *)
1784 and wait (union wait *) to be compatible. */
1785 if (TREE_CODE (a1) == UNION_TYPE
1786 && (TYPE_NAME (a1) == NULL_TREE
1787 || TYPE_TRANSPARENT_AGGR (a1))
1788 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1789 && tree_int_cst_equal (TYPE_SIZE (a1),
1790 TYPE_SIZE (a2)))
1792 tree memb;
1793 for (memb = TYPE_FIELDS (a1);
1794 memb; memb = DECL_CHAIN (memb))
1796 tree mv3 = TREE_TYPE (memb);
1797 if (mv3 && mv3 != error_mark_node
1798 && TREE_CODE (mv3) != ARRAY_TYPE)
1799 mv3 = (TYPE_ATOMIC (mv3)
1800 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1801 TYPE_QUAL_ATOMIC)
1802 : TYPE_MAIN_VARIANT (mv3));
1803 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1804 different_types_p))
1805 break;
1807 if (memb == NULL_TREE)
1808 return 0;
1810 else if (TREE_CODE (a2) == UNION_TYPE
1811 && (TYPE_NAME (a2) == NULL_TREE
1812 || TYPE_TRANSPARENT_AGGR (a2))
1813 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1814 && tree_int_cst_equal (TYPE_SIZE (a2),
1815 TYPE_SIZE (a1)))
1817 tree memb;
1818 for (memb = TYPE_FIELDS (a2);
1819 memb; memb = DECL_CHAIN (memb))
1821 tree mv3 = TREE_TYPE (memb);
1822 if (mv3 && mv3 != error_mark_node
1823 && TREE_CODE (mv3) != ARRAY_TYPE)
1824 mv3 = (TYPE_ATOMIC (mv3)
1825 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1826 TYPE_QUAL_ATOMIC)
1827 : TYPE_MAIN_VARIANT (mv3));
1828 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1829 different_types_p))
1830 break;
1832 if (memb == NULL_TREE)
1833 return 0;
1835 else
1836 return 0;
1839 /* comptypes said ok, but record if it said to warn. */
1840 if (newval > val)
1841 val = newval;
1843 args1 = TREE_CHAIN (args1);
1844 args2 = TREE_CHAIN (args2);
1848 /* Compute the size to increment a pointer by. When a function type or void
1849 type or incomplete type is passed, size_one_node is returned.
1850 This function does not emit any diagnostics; the caller is responsible
1851 for that. */
1853 static tree
1854 c_size_in_bytes (const_tree type)
1856 enum tree_code code = TREE_CODE (type);
1858 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1859 || !COMPLETE_TYPE_P (type))
1860 return size_one_node;
1862 /* Convert in case a char is more than one unit. */
1863 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1864 size_int (TYPE_PRECISION (char_type_node)
1865 / BITS_PER_UNIT));
1868 /* Return either DECL or its known constant value (if it has one). */
1870 tree
1871 decl_constant_value_1 (tree decl, bool in_init)
1873 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1874 TREE_CODE (decl) != PARM_DECL
1875 && !TREE_THIS_VOLATILE (decl)
1876 && TREE_READONLY (decl)
1877 && DECL_INITIAL (decl) != NULL_TREE
1878 && !error_operand_p (DECL_INITIAL (decl))
1879 /* This is invalid if initial value is not constant.
1880 If it has either a function call, a memory reference,
1881 or a variable, then re-evaluating it could give different results. */
1882 && TREE_CONSTANT (DECL_INITIAL (decl))
1883 /* Check for cases where this is sub-optimal, even though valid. */
1884 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1885 return DECL_INITIAL (decl);
1886 return decl;
1889 /* Return either DECL or its known constant value (if it has one).
1890 Like the above, but always return decl outside of functions. */
1892 tree
1893 decl_constant_value (tree decl)
1895 /* Don't change a variable array bound or initial value to a constant
1896 in a place where a variable is invalid. */
1897 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1900 /* Convert the array expression EXP to a pointer. */
1901 static tree
1902 array_to_pointer_conversion (location_t loc, tree exp)
1904 tree orig_exp = exp;
1905 tree type = TREE_TYPE (exp);
1906 tree adr;
1907 tree restype = TREE_TYPE (type);
1908 tree ptrtype;
1910 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1912 STRIP_TYPE_NOPS (exp);
1914 if (TREE_NO_WARNING (orig_exp))
1915 TREE_NO_WARNING (exp) = 1;
1917 ptrtype = build_pointer_type (restype);
1919 if (INDIRECT_REF_P (exp))
1920 return convert (ptrtype, TREE_OPERAND (exp, 0));
1922 /* In C++ array compound literals are temporary objects unless they are
1923 const or appear in namespace scope, so they are destroyed too soon
1924 to use them for much of anything (c++/53220). */
1925 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1927 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1928 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1929 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1930 "converting an array compound literal to a pointer "
1931 "is ill-formed in C++");
1934 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1935 return convert (ptrtype, adr);
1938 /* Convert the function expression EXP to a pointer. */
1939 static tree
1940 function_to_pointer_conversion (location_t loc, tree exp)
1942 tree orig_exp = exp;
1944 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1946 STRIP_TYPE_NOPS (exp);
1948 if (TREE_NO_WARNING (orig_exp))
1949 TREE_NO_WARNING (exp) = 1;
1951 return build_unary_op (loc, ADDR_EXPR, exp, false);
1954 /* Mark EXP as read, not just set, for set but not used -Wunused
1955 warning purposes. */
1957 void
1958 mark_exp_read (tree exp)
1960 switch (TREE_CODE (exp))
1962 case VAR_DECL:
1963 case PARM_DECL:
1964 DECL_READ_P (exp) = 1;
1965 break;
1966 case ARRAY_REF:
1967 case COMPONENT_REF:
1968 case MODIFY_EXPR:
1969 case REALPART_EXPR:
1970 case IMAGPART_EXPR:
1971 CASE_CONVERT:
1972 case ADDR_EXPR:
1973 case VIEW_CONVERT_EXPR:
1974 mark_exp_read (TREE_OPERAND (exp, 0));
1975 break;
1976 case COMPOUND_EXPR:
1977 /* Pattern match what build_atomic_assign produces with modifycode
1978 NOP_EXPR. */
1979 if (VAR_P (TREE_OPERAND (exp, 1))
1980 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1981 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1983 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1984 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1985 if (TREE_CODE (t1) == TARGET_EXPR
1986 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1987 && TREE_CODE (t2) == CALL_EXPR)
1989 tree fndecl = get_callee_fndecl (t2);
1990 tree arg = NULL_TREE;
1991 if (fndecl
1992 && TREE_CODE (fndecl) == FUNCTION_DECL
1993 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1994 && call_expr_nargs (t2) >= 2)
1995 switch (DECL_FUNCTION_CODE (fndecl))
1997 case BUILT_IN_ATOMIC_STORE:
1998 arg = CALL_EXPR_ARG (t2, 1);
1999 break;
2000 case BUILT_IN_ATOMIC_STORE_1:
2001 case BUILT_IN_ATOMIC_STORE_2:
2002 case BUILT_IN_ATOMIC_STORE_4:
2003 case BUILT_IN_ATOMIC_STORE_8:
2004 case BUILT_IN_ATOMIC_STORE_16:
2005 arg = CALL_EXPR_ARG (t2, 0);
2006 break;
2007 default:
2008 break;
2010 if (arg)
2012 STRIP_NOPS (arg);
2013 if (TREE_CODE (arg) == ADDR_EXPR
2014 && DECL_P (TREE_OPERAND (arg, 0))
2015 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2016 mark_exp_read (TREE_OPERAND (arg, 0));
2020 /* FALLTHRU */
2021 case C_MAYBE_CONST_EXPR:
2022 mark_exp_read (TREE_OPERAND (exp, 1));
2023 break;
2024 default:
2025 break;
2029 /* Perform the default conversion of arrays and functions to pointers.
2030 Return the result of converting EXP. For any other expression, just
2031 return EXP.
2033 LOC is the location of the expression. */
2035 struct c_expr
2036 default_function_array_conversion (location_t loc, struct c_expr exp)
2038 tree orig_exp = exp.value;
2039 tree type = TREE_TYPE (exp.value);
2040 enum tree_code code = TREE_CODE (type);
2042 switch (code)
2044 case ARRAY_TYPE:
2046 bool not_lvalue = false;
2047 bool lvalue_array_p;
2049 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2050 || CONVERT_EXPR_P (exp.value))
2051 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2053 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2054 not_lvalue = true;
2055 exp.value = TREE_OPERAND (exp.value, 0);
2058 if (TREE_NO_WARNING (orig_exp))
2059 TREE_NO_WARNING (exp.value) = 1;
2061 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2062 if (!flag_isoc99 && !lvalue_array_p)
2064 /* Before C99, non-lvalue arrays do not decay to pointers.
2065 Normally, using such an array would be invalid; but it can
2066 be used correctly inside sizeof or as a statement expression.
2067 Thus, do not give an error here; an error will result later. */
2068 return exp;
2071 exp.value = array_to_pointer_conversion (loc, exp.value);
2073 break;
2074 case FUNCTION_TYPE:
2075 exp.value = function_to_pointer_conversion (loc, exp.value);
2076 break;
2077 default:
2078 break;
2081 return exp;
2084 struct c_expr
2085 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2087 mark_exp_read (exp.value);
2088 return default_function_array_conversion (loc, exp);
2091 /* Return whether EXPR should be treated as an atomic lvalue for the
2092 purposes of load and store handling. */
2094 static bool
2095 really_atomic_lvalue (tree expr)
2097 if (error_operand_p (expr))
2098 return false;
2099 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2100 return false;
2101 if (!lvalue_p (expr))
2102 return false;
2104 /* Ignore _Atomic on register variables, since their addresses can't
2105 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2106 sequences wouldn't work. Ignore _Atomic on structures containing
2107 bit-fields, since accessing elements of atomic structures or
2108 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2109 it's undefined at translation time or execution time, and the
2110 normal atomic sequences again wouldn't work. */
2111 while (handled_component_p (expr))
2113 if (TREE_CODE (expr) == COMPONENT_REF
2114 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2115 return false;
2116 expr = TREE_OPERAND (expr, 0);
2118 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2119 return false;
2120 return true;
2123 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2124 including converting functions and arrays to pointers if CONVERT_P.
2125 If READ_P, also mark the expression as having been read. */
2127 struct c_expr
2128 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2129 bool convert_p, bool read_p)
2131 if (read_p)
2132 mark_exp_read (exp.value);
2133 if (convert_p)
2134 exp = default_function_array_conversion (loc, exp);
2135 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2136 exp.value = require_complete_type (loc, exp.value);
2137 if (really_atomic_lvalue (exp.value))
2139 vec<tree, va_gc> *params;
2140 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2141 tree expr_type = TREE_TYPE (exp.value);
2142 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2143 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2145 gcc_assert (TYPE_ATOMIC (expr_type));
2147 /* Expansion of a generic atomic load may require an addition
2148 element, so allocate enough to prevent a resize. */
2149 vec_alloc (params, 4);
2151 /* Remove the qualifiers for the rest of the expressions and
2152 create the VAL temp variable to hold the RHS. */
2153 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2154 tmp = create_tmp_var_raw (nonatomic_type);
2155 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2156 TREE_ADDRESSABLE (tmp) = 1;
2157 TREE_NO_WARNING (tmp) = 1;
2159 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2160 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2161 params->quick_push (expr_addr);
2162 params->quick_push (tmp_addr);
2163 params->quick_push (seq_cst);
2164 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2166 /* EXPR is always read. */
2167 mark_exp_read (exp.value);
2169 /* Return tmp which contains the value loaded. */
2170 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2171 NULL_TREE, NULL_TREE);
2173 if (convert_p && !error_operand_p (exp.value)
2174 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2175 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2176 return exp;
2179 /* EXP is an expression of integer type. Apply the integer promotions
2180 to it and return the promoted value. */
2182 tree
2183 perform_integral_promotions (tree exp)
2185 tree type = TREE_TYPE (exp);
2186 enum tree_code code = TREE_CODE (type);
2188 gcc_assert (INTEGRAL_TYPE_P (type));
2190 /* Normally convert enums to int,
2191 but convert wide enums to something wider. */
2192 if (code == ENUMERAL_TYPE)
2194 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2195 TYPE_PRECISION (integer_type_node)),
2196 ((TYPE_PRECISION (type)
2197 >= TYPE_PRECISION (integer_type_node))
2198 && TYPE_UNSIGNED (type)));
2200 return convert (type, exp);
2203 /* ??? This should no longer be needed now bit-fields have their
2204 proper types. */
2205 if (TREE_CODE (exp) == COMPONENT_REF
2206 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2207 /* If it's thinner than an int, promote it like a
2208 c_promoting_integer_type_p, otherwise leave it alone. */
2209 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2210 TYPE_PRECISION (integer_type_node)) < 0)
2211 return convert (integer_type_node, exp);
2213 if (c_promoting_integer_type_p (type))
2215 /* Preserve unsignedness if not really getting any wider. */
2216 if (TYPE_UNSIGNED (type)
2217 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2218 return convert (unsigned_type_node, exp);
2220 return convert (integer_type_node, exp);
2223 return exp;
2227 /* Perform default promotions for C data used in expressions.
2228 Enumeral types or short or char are converted to int.
2229 In addition, manifest constants symbols are replaced by their values. */
2231 tree
2232 default_conversion (tree exp)
2234 tree orig_exp;
2235 tree type = TREE_TYPE (exp);
2236 enum tree_code code = TREE_CODE (type);
2237 tree promoted_type;
2239 mark_exp_read (exp);
2241 /* Functions and arrays have been converted during parsing. */
2242 gcc_assert (code != FUNCTION_TYPE);
2243 if (code == ARRAY_TYPE)
2244 return exp;
2246 /* Constants can be used directly unless they're not loadable. */
2247 if (TREE_CODE (exp) == CONST_DECL)
2248 exp = DECL_INITIAL (exp);
2250 /* Strip no-op conversions. */
2251 orig_exp = exp;
2252 STRIP_TYPE_NOPS (exp);
2254 if (TREE_NO_WARNING (orig_exp))
2255 TREE_NO_WARNING (exp) = 1;
2257 if (code == VOID_TYPE)
2259 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2260 "void value not ignored as it ought to be");
2261 return error_mark_node;
2264 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2265 if (exp == error_mark_node)
2266 return error_mark_node;
2268 promoted_type = targetm.promoted_type (type);
2269 if (promoted_type)
2270 return convert (promoted_type, exp);
2272 if (INTEGRAL_TYPE_P (type))
2273 return perform_integral_promotions (exp);
2275 return exp;
2278 /* Look up COMPONENT in a structure or union TYPE.
2280 If the component name is not found, returns NULL_TREE. Otherwise,
2281 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2282 stepping down the chain to the component, which is in the last
2283 TREE_VALUE of the list. Normally the list is of length one, but if
2284 the component is embedded within (nested) anonymous structures or
2285 unions, the list steps down the chain to the component. */
2287 static tree
2288 lookup_field (tree type, tree component)
2290 tree field;
2292 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2293 to the field elements. Use a binary search on this array to quickly
2294 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2295 will always be set for structures which have many elements.
2297 Duplicate field checking replaces duplicates with NULL_TREE so
2298 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2299 case just iterate using DECL_CHAIN. */
2301 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2302 && !seen_error ())
2304 int bot, top, half;
2305 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2307 field = TYPE_FIELDS (type);
2308 bot = 0;
2309 top = TYPE_LANG_SPECIFIC (type)->s->len;
2310 while (top - bot > 1)
2312 half = (top - bot + 1) >> 1;
2313 field = field_array[bot+half];
2315 if (DECL_NAME (field) == NULL_TREE)
2317 /* Step through all anon unions in linear fashion. */
2318 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2320 field = field_array[bot++];
2321 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2323 tree anon = lookup_field (TREE_TYPE (field), component);
2325 if (anon)
2326 return tree_cons (NULL_TREE, field, anon);
2328 /* The Plan 9 compiler permits referring
2329 directly to an anonymous struct/union field
2330 using a typedef name. */
2331 if (flag_plan9_extensions
2332 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2333 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2334 == TYPE_DECL)
2335 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2336 == component))
2337 break;
2341 /* Entire record is only anon unions. */
2342 if (bot > top)
2343 return NULL_TREE;
2345 /* Restart the binary search, with new lower bound. */
2346 continue;
2349 if (DECL_NAME (field) == component)
2350 break;
2351 if (DECL_NAME (field) < component)
2352 bot += half;
2353 else
2354 top = bot + half;
2357 if (DECL_NAME (field_array[bot]) == component)
2358 field = field_array[bot];
2359 else if (DECL_NAME (field) != component)
2360 return NULL_TREE;
2362 else
2364 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2366 if (DECL_NAME (field) == NULL_TREE
2367 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2369 tree anon = lookup_field (TREE_TYPE (field), component);
2371 if (anon)
2372 return tree_cons (NULL_TREE, field, anon);
2374 /* The Plan 9 compiler permits referring directly to an
2375 anonymous struct/union field using a typedef
2376 name. */
2377 if (flag_plan9_extensions
2378 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2379 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2380 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2381 == component))
2382 break;
2385 if (DECL_NAME (field) == component)
2386 break;
2389 if (field == NULL_TREE)
2390 return NULL_TREE;
2393 return tree_cons (NULL_TREE, field, NULL_TREE);
2396 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2398 static void
2399 lookup_field_fuzzy_find_candidates (tree type, tree component,
2400 vec<tree> *candidates)
2402 tree field;
2403 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2405 if (DECL_NAME (field) == NULL_TREE
2406 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2407 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2408 candidates);
2410 if (DECL_NAME (field))
2411 candidates->safe_push (DECL_NAME (field));
2415 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2416 rather than returning a TREE_LIST for an exact match. */
2418 static tree
2419 lookup_field_fuzzy (tree type, tree component)
2421 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2423 /* First, gather a list of candidates. */
2424 auto_vec <tree> candidates;
2426 lookup_field_fuzzy_find_candidates (type, component,
2427 &candidates);
2429 return find_closest_identifier (component, &candidates);
2432 /* Support function for build_component_ref's error-handling.
2434 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2435 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2437 static bool
2438 should_suggest_deref_p (tree datum_type)
2440 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2441 allows "." for ptrs; we could be handling a failed attempt
2442 to access a property. */
2443 if (c_dialect_objc ())
2444 return false;
2446 /* Only suggest it for pointers... */
2447 if (TREE_CODE (datum_type) != POINTER_TYPE)
2448 return false;
2450 /* ...to structs/unions. */
2451 tree underlying_type = TREE_TYPE (datum_type);
2452 enum tree_code code = TREE_CODE (underlying_type);
2453 if (code == RECORD_TYPE || code == UNION_TYPE)
2454 return true;
2455 else
2456 return false;
2459 /* Make an expression to refer to the COMPONENT field of structure or
2460 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2461 location of the COMPONENT_REF. COMPONENT_LOC is the location
2462 of COMPONENT. */
2464 tree
2465 build_component_ref (location_t loc, tree datum, tree component,
2466 location_t component_loc)
2468 tree type = TREE_TYPE (datum);
2469 enum tree_code code = TREE_CODE (type);
2470 tree field = NULL;
2471 tree ref;
2472 bool datum_lvalue = lvalue_p (datum);
2474 if (!objc_is_public (datum, component))
2475 return error_mark_node;
2477 /* Detect Objective-C property syntax object.property. */
2478 if (c_dialect_objc ()
2479 && (ref = objc_maybe_build_component_ref (datum, component)))
2480 return ref;
2482 /* See if there is a field or component with name COMPONENT. */
2484 if (code == RECORD_TYPE || code == UNION_TYPE)
2486 if (!COMPLETE_TYPE_P (type))
2488 c_incomplete_type_error (loc, NULL_TREE, type);
2489 return error_mark_node;
2492 field = lookup_field (type, component);
2494 if (!field)
2496 tree guessed_id = lookup_field_fuzzy (type, component);
2497 if (guessed_id)
2499 /* Attempt to provide a fixit replacement hint, if
2500 we have a valid range for the component. */
2501 location_t reported_loc
2502 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2503 gcc_rich_location rich_loc (reported_loc);
2504 if (component_loc != UNKNOWN_LOCATION)
2505 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2506 error_at (&rich_loc,
2507 "%qT has no member named %qE; did you mean %qE?",
2508 type, component, guessed_id);
2510 else
2511 error_at (loc, "%qT has no member named %qE", type, component);
2512 return error_mark_node;
2515 /* Accessing elements of atomic structures or unions is undefined
2516 behavior (C11 6.5.2.3#5). */
2517 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2519 if (code == RECORD_TYPE)
2520 warning_at (loc, 0, "accessing a member %qE of an atomic "
2521 "structure %qE", component, datum);
2522 else
2523 warning_at (loc, 0, "accessing a member %qE of an atomic "
2524 "union %qE", component, datum);
2527 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2528 This might be better solved in future the way the C++ front
2529 end does it - by giving the anonymous entities each a
2530 separate name and type, and then have build_component_ref
2531 recursively call itself. We can't do that here. */
2534 tree subdatum = TREE_VALUE (field);
2535 int quals;
2536 tree subtype;
2537 bool use_datum_quals;
2539 if (TREE_TYPE (subdatum) == error_mark_node)
2540 return error_mark_node;
2542 /* If this is an rvalue, it does not have qualifiers in C
2543 standard terms and we must avoid propagating such
2544 qualifiers down to a non-lvalue array that is then
2545 converted to a pointer. */
2546 use_datum_quals = (datum_lvalue
2547 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2549 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2550 if (use_datum_quals)
2551 quals |= TYPE_QUALS (TREE_TYPE (datum));
2552 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2554 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2555 NULL_TREE);
2556 SET_EXPR_LOCATION (ref, loc);
2557 if (TREE_READONLY (subdatum)
2558 || (use_datum_quals && TREE_READONLY (datum)))
2559 TREE_READONLY (ref) = 1;
2560 if (TREE_THIS_VOLATILE (subdatum)
2561 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2562 TREE_THIS_VOLATILE (ref) = 1;
2564 if (TREE_DEPRECATED (subdatum))
2565 warn_deprecated_use (subdatum, NULL_TREE);
2567 datum = ref;
2569 field = TREE_CHAIN (field);
2571 while (field);
2573 return ref;
2575 else if (should_suggest_deref_p (type))
2577 /* Special-case the error message for "ptr.field" for the case
2578 where the user has confused "." vs "->". */
2579 rich_location richloc (line_table, loc);
2580 /* "loc" should be the "." token. */
2581 richloc.add_fixit_replace ("->");
2582 error_at (&richloc,
2583 "%qE is a pointer; did you mean to use %<->%>?",
2584 datum);
2585 return error_mark_node;
2587 else if (code != ERROR_MARK)
2588 error_at (loc,
2589 "request for member %qE in something not a structure or union",
2590 component);
2592 return error_mark_node;
2595 /* Given an expression PTR for a pointer, return an expression
2596 for the value pointed to.
2597 ERRORSTRING is the name of the operator to appear in error messages.
2599 LOC is the location to use for the generated tree. */
2601 tree
2602 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2604 tree pointer = default_conversion (ptr);
2605 tree type = TREE_TYPE (pointer);
2606 tree ref;
2608 if (TREE_CODE (type) == POINTER_TYPE)
2610 if (CONVERT_EXPR_P (pointer)
2611 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2613 /* If a warning is issued, mark it to avoid duplicates from
2614 the backend. This only needs to be done at
2615 warn_strict_aliasing > 2. */
2616 if (warn_strict_aliasing > 2)
2617 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2618 type, TREE_OPERAND (pointer, 0)))
2619 TREE_NO_WARNING (pointer) = 1;
2622 if (TREE_CODE (pointer) == ADDR_EXPR
2623 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2624 == TREE_TYPE (type)))
2626 ref = TREE_OPERAND (pointer, 0);
2627 protected_set_expr_location (ref, loc);
2628 return ref;
2630 else
2632 tree t = TREE_TYPE (type);
2634 ref = build1 (INDIRECT_REF, t, pointer);
2636 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2637 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2639 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2640 so that we get the proper error message if the result is used
2641 to assign to. Also, &* is supposed to be a no-op.
2642 And ANSI C seems to specify that the type of the result
2643 should be the const type. */
2644 /* A de-reference of a pointer to const is not a const. It is valid
2645 to change it via some other pointer. */
2646 TREE_READONLY (ref) = TYPE_READONLY (t);
2647 TREE_SIDE_EFFECTS (ref)
2648 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2649 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2650 protected_set_expr_location (ref, loc);
2651 return ref;
2654 else if (TREE_CODE (pointer) != ERROR_MARK)
2655 invalid_indirection_error (loc, type, errstring);
2657 return error_mark_node;
2660 /* This handles expressions of the form "a[i]", which denotes
2661 an array reference.
2663 This is logically equivalent in C to *(a+i), but we may do it differently.
2664 If A is a variable or a member, we generate a primitive ARRAY_REF.
2665 This avoids forcing the array out of registers, and can work on
2666 arrays that are not lvalues (for example, members of structures returned
2667 by functions).
2669 For vector types, allow vector[i] but not i[vector], and create
2670 *(((type*)&vectortype) + i) for the expression.
2672 LOC is the location to use for the returned expression. */
2674 tree
2675 build_array_ref (location_t loc, tree array, tree index)
2677 tree ret;
2678 bool swapped = false;
2679 if (TREE_TYPE (array) == error_mark_node
2680 || TREE_TYPE (index) == error_mark_node)
2681 return error_mark_node;
2683 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2684 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2685 /* Allow vector[index] but not index[vector]. */
2686 && !gnu_vector_type_p (TREE_TYPE (array)))
2688 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2689 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2691 error_at (loc,
2692 "subscripted value is neither array nor pointer nor vector");
2694 return error_mark_node;
2696 std::swap (array, index);
2697 swapped = true;
2700 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2702 error_at (loc, "array subscript is not an integer");
2703 return error_mark_node;
2706 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2708 error_at (loc, "subscripted value is pointer to function");
2709 return error_mark_node;
2712 /* ??? Existing practice has been to warn only when the char
2713 index is syntactically the index, not for char[array]. */
2714 if (!swapped)
2715 warn_array_subscript_with_type_char (loc, index);
2717 /* Apply default promotions *after* noticing character types. */
2718 index = default_conversion (index);
2719 if (index == error_mark_node)
2720 return error_mark_node;
2722 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2724 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2725 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2727 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2729 tree rval, type;
2731 /* An array that is indexed by a non-constant
2732 cannot be stored in a register; we must be able to do
2733 address arithmetic on its address.
2734 Likewise an array of elements of variable size. */
2735 if (TREE_CODE (index) != INTEGER_CST
2736 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2737 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2739 if (!c_mark_addressable (array, true))
2740 return error_mark_node;
2742 /* An array that is indexed by a constant value which is not within
2743 the array bounds cannot be stored in a register either; because we
2744 would get a crash in store_bit_field/extract_bit_field when trying
2745 to access a non-existent part of the register. */
2746 if (TREE_CODE (index) == INTEGER_CST
2747 && TYPE_DOMAIN (TREE_TYPE (array))
2748 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2750 if (!c_mark_addressable (array))
2751 return error_mark_node;
2754 if ((pedantic || warn_c90_c99_compat)
2755 && ! was_vector)
2757 tree foo = array;
2758 while (TREE_CODE (foo) == COMPONENT_REF)
2759 foo = TREE_OPERAND (foo, 0);
2760 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2761 pedwarn (loc, OPT_Wpedantic,
2762 "ISO C forbids subscripting %<register%> array");
2763 else if (!lvalue_p (foo))
2764 pedwarn_c90 (loc, OPT_Wpedantic,
2765 "ISO C90 forbids subscripting non-lvalue "
2766 "array");
2769 type = TREE_TYPE (TREE_TYPE (array));
2770 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2771 /* Array ref is const/volatile if the array elements are
2772 or if the array is. */
2773 TREE_READONLY (rval)
2774 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2775 | TREE_READONLY (array));
2776 TREE_SIDE_EFFECTS (rval)
2777 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2778 | TREE_SIDE_EFFECTS (array));
2779 TREE_THIS_VOLATILE (rval)
2780 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2781 /* This was added by rms on 16 Nov 91.
2782 It fixes vol struct foo *a; a->elts[1]
2783 in an inline function.
2784 Hope it doesn't break something else. */
2785 | TREE_THIS_VOLATILE (array));
2786 ret = require_complete_type (loc, rval);
2787 protected_set_expr_location (ret, loc);
2788 if (non_lvalue)
2789 ret = non_lvalue_loc (loc, ret);
2790 return ret;
2792 else
2794 tree ar = default_conversion (array);
2796 if (ar == error_mark_node)
2797 return ar;
2799 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2800 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2802 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2803 index, false),
2804 RO_ARRAY_INDEXING);
2805 if (non_lvalue)
2806 ret = non_lvalue_loc (loc, ret);
2807 return ret;
2811 /* Build an external reference to identifier ID. FUN indicates
2812 whether this will be used for a function call. LOC is the source
2813 location of the identifier. This sets *TYPE to the type of the
2814 identifier, which is not the same as the type of the returned value
2815 for CONST_DECLs defined as enum constants. If the type of the
2816 identifier is not available, *TYPE is set to NULL. */
2817 tree
2818 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2820 tree ref;
2821 tree decl = lookup_name (id);
2823 /* In Objective-C, an instance variable (ivar) may be preferred to
2824 whatever lookup_name() found. */
2825 decl = objc_lookup_ivar (decl, id);
2827 *type = NULL;
2828 if (decl && decl != error_mark_node)
2830 ref = decl;
2831 *type = TREE_TYPE (ref);
2833 else if (fun)
2834 /* Implicit function declaration. */
2835 ref = implicitly_declare (loc, id);
2836 else if (decl == error_mark_node)
2837 /* Don't complain about something that's already been
2838 complained about. */
2839 return error_mark_node;
2840 else
2842 undeclared_variable (loc, id);
2843 return error_mark_node;
2846 if (TREE_TYPE (ref) == error_mark_node)
2847 return error_mark_node;
2849 if (TREE_DEPRECATED (ref))
2850 warn_deprecated_use (ref, NULL_TREE);
2852 /* Recursive call does not count as usage. */
2853 if (ref != current_function_decl)
2855 TREE_USED (ref) = 1;
2858 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2860 if (!in_sizeof && !in_typeof)
2861 C_DECL_USED (ref) = 1;
2862 else if (DECL_INITIAL (ref) == NULL_TREE
2863 && DECL_EXTERNAL (ref)
2864 && !TREE_PUBLIC (ref))
2865 record_maybe_used_decl (ref);
2868 if (TREE_CODE (ref) == CONST_DECL)
2870 used_types_insert (TREE_TYPE (ref));
2872 if (warn_cxx_compat
2873 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2874 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2876 warning_at (loc, OPT_Wc___compat,
2877 ("enum constant defined in struct or union "
2878 "is not visible in C++"));
2879 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2882 ref = DECL_INITIAL (ref);
2883 TREE_CONSTANT (ref) = 1;
2885 else if (current_function_decl != NULL_TREE
2886 && !DECL_FILE_SCOPE_P (current_function_decl)
2887 && (VAR_OR_FUNCTION_DECL_P (ref)
2888 || TREE_CODE (ref) == PARM_DECL))
2890 tree context = decl_function_context (ref);
2892 if (context != NULL_TREE && context != current_function_decl)
2893 DECL_NONLOCAL (ref) = 1;
2895 /* C99 6.7.4p3: An inline definition of a function with external
2896 linkage ... shall not contain a reference to an identifier with
2897 internal linkage. */
2898 else if (current_function_decl != NULL_TREE
2899 && DECL_DECLARED_INLINE_P (current_function_decl)
2900 && DECL_EXTERNAL (current_function_decl)
2901 && VAR_OR_FUNCTION_DECL_P (ref)
2902 && (!VAR_P (ref) || TREE_STATIC (ref))
2903 && ! TREE_PUBLIC (ref)
2904 && DECL_CONTEXT (ref) != current_function_decl)
2905 record_inline_static (loc, current_function_decl, ref,
2906 csi_internal);
2908 return ref;
2911 /* Record details of decls possibly used inside sizeof or typeof. */
2912 struct maybe_used_decl
2914 /* The decl. */
2915 tree decl;
2916 /* The level seen at (in_sizeof + in_typeof). */
2917 int level;
2918 /* The next one at this level or above, or NULL. */
2919 struct maybe_used_decl *next;
2922 static struct maybe_used_decl *maybe_used_decls;
2924 /* Record that DECL, an undefined static function reference seen
2925 inside sizeof or typeof, might be used if the operand of sizeof is
2926 a VLA type or the operand of typeof is a variably modified
2927 type. */
2929 static void
2930 record_maybe_used_decl (tree decl)
2932 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2933 t->decl = decl;
2934 t->level = in_sizeof + in_typeof;
2935 t->next = maybe_used_decls;
2936 maybe_used_decls = t;
2939 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2940 USED is false, just discard them. If it is true, mark them used
2941 (if no longer inside sizeof or typeof) or move them to the next
2942 level up (if still inside sizeof or typeof). */
2944 void
2945 pop_maybe_used (bool used)
2947 struct maybe_used_decl *p = maybe_used_decls;
2948 int cur_level = in_sizeof + in_typeof;
2949 while (p && p->level > cur_level)
2951 if (used)
2953 if (cur_level == 0)
2954 C_DECL_USED (p->decl) = 1;
2955 else
2956 p->level = cur_level;
2958 p = p->next;
2960 if (!used || cur_level == 0)
2961 maybe_used_decls = p;
2964 /* Return the result of sizeof applied to EXPR. */
2966 struct c_expr
2967 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2969 struct c_expr ret;
2970 if (expr.value == error_mark_node)
2972 ret.value = error_mark_node;
2973 ret.original_code = ERROR_MARK;
2974 ret.original_type = NULL;
2975 pop_maybe_used (false);
2977 else
2979 bool expr_const_operands = true;
2981 if (TREE_CODE (expr.value) == PARM_DECL
2982 && C_ARRAY_PARAMETER (expr.value))
2984 auto_diagnostic_group d;
2985 if (warning_at (loc, OPT_Wsizeof_array_argument,
2986 "%<sizeof%> on array function parameter %qE will "
2987 "return size of %qT", expr.value,
2988 TREE_TYPE (expr.value)))
2989 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2991 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2992 &expr_const_operands);
2993 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2994 c_last_sizeof_arg = expr.value;
2995 c_last_sizeof_loc = loc;
2996 ret.original_code = SIZEOF_EXPR;
2997 ret.original_type = NULL;
2998 if (c_vla_type_p (TREE_TYPE (folded_expr)))
3000 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3001 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3002 folded_expr, ret.value);
3003 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3004 SET_EXPR_LOCATION (ret.value, loc);
3006 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3008 return ret;
3011 /* Return the result of sizeof applied to T, a structure for the type
3012 name passed to sizeof (rather than the type itself). LOC is the
3013 location of the original expression. */
3015 struct c_expr
3016 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3018 tree type;
3019 struct c_expr ret;
3020 tree type_expr = NULL_TREE;
3021 bool type_expr_const = true;
3022 type = groktypename (t, &type_expr, &type_expr_const);
3023 ret.value = c_sizeof (loc, type);
3024 c_last_sizeof_arg = type;
3025 c_last_sizeof_loc = loc;
3026 ret.original_code = SIZEOF_EXPR;
3027 ret.original_type = NULL;
3028 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3029 && c_vla_type_p (type))
3031 /* If the type is a [*] array, it is a VLA but is represented as
3032 having a size of zero. In such a case we must ensure that
3033 the result of sizeof does not get folded to a constant by
3034 c_fully_fold, because if the size is evaluated the result is
3035 not constant and so constraints on zero or negative size
3036 arrays must not be applied when this sizeof call is inside
3037 another array declarator. */
3038 if (!type_expr)
3039 type_expr = integer_zero_node;
3040 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3041 type_expr, ret.value);
3042 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3044 pop_maybe_used (type != error_mark_node
3045 ? C_TYPE_VARIABLE_SIZE (type) : false);
3046 return ret;
3049 /* Build a function call to function FUNCTION with parameters PARAMS.
3050 The function call is at LOC.
3051 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3052 TREE_VALUE of each node is a parameter-expression.
3053 FUNCTION's data type may be a function type or a pointer-to-function. */
3055 tree
3056 build_function_call (location_t loc, tree function, tree params)
3058 vec<tree, va_gc> *v;
3059 tree ret;
3061 vec_alloc (v, list_length (params));
3062 for (; params; params = TREE_CHAIN (params))
3063 v->quick_push (TREE_VALUE (params));
3064 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3065 vec_free (v);
3066 return ret;
3069 /* Give a note about the location of the declaration of DECL. */
3071 static void
3072 inform_declaration (tree decl)
3074 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3075 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3076 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3079 /* Build a function call to function FUNCTION with parameters PARAMS.
3080 If FUNCTION is the result of resolving an overloaded target built-in,
3081 ORIG_FUNDECL is the original function decl, otherwise it is null.
3082 ORIGTYPES, if not NULL, is a vector of types; each element is
3083 either NULL or the original type of the corresponding element in
3084 PARAMS. The original type may differ from TREE_TYPE of the
3085 parameter for enums. FUNCTION's data type may be a function type
3086 or pointer-to-function. This function changes the elements of
3087 PARAMS. */
3089 tree
3090 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3091 tree function, vec<tree, va_gc> *params,
3092 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3094 tree fntype, fundecl = NULL_TREE;
3095 tree name = NULL_TREE, result;
3096 tree tem;
3097 int nargs;
3098 tree *argarray;
3101 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3102 STRIP_TYPE_NOPS (function);
3104 /* Convert anything with function type to a pointer-to-function. */
3105 if (TREE_CODE (function) == FUNCTION_DECL)
3107 name = DECL_NAME (function);
3109 if (flag_tm)
3110 tm_malloc_replacement (function);
3111 fundecl = function;
3112 if (!orig_fundecl)
3113 orig_fundecl = fundecl;
3114 /* Atomic functions have type checking/casting already done. They are
3115 often rewritten and don't match the original parameter list. */
3116 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3117 origtypes = NULL;
3119 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3120 function = function_to_pointer_conversion (loc, function);
3122 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3123 expressions, like those used for ObjC messenger dispatches. */
3124 if (params && !params->is_empty ())
3125 function = objc_rewrite_function_call (function, (*params)[0]);
3127 function = c_fully_fold (function, false, NULL);
3129 fntype = TREE_TYPE (function);
3131 if (TREE_CODE (fntype) == ERROR_MARK)
3132 return error_mark_node;
3134 if (!(TREE_CODE (fntype) == POINTER_TYPE
3135 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3137 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3138 error_at (loc,
3139 "called object %qE is not a function or function pointer",
3140 function);
3141 else if (DECL_P (function))
3143 error_at (loc,
3144 "called object %qD is not a function or function pointer",
3145 function);
3146 inform_declaration (function);
3148 else
3149 error_at (loc,
3150 "called object is not a function or function pointer");
3151 return error_mark_node;
3154 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3155 current_function_returns_abnormally = 1;
3157 /* fntype now gets the type of function pointed to. */
3158 fntype = TREE_TYPE (fntype);
3160 /* Convert the parameters to the types declared in the
3161 function prototype, or apply default promotions. */
3163 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3164 origtypes, function, fundecl);
3165 if (nargs < 0)
3166 return error_mark_node;
3168 /* Check that the function is called through a compatible prototype.
3169 If it is not, warn. */
3170 if (CONVERT_EXPR_P (function)
3171 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3172 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3173 && !comptypes (fntype, TREE_TYPE (tem)))
3175 tree return_type = TREE_TYPE (fntype);
3177 /* This situation leads to run-time undefined behavior. We can't,
3178 therefore, simply error unless we can prove that all possible
3179 executions of the program must execute the code. */
3180 warning_at (loc, 0, "function called through a non-compatible type");
3182 if (VOID_TYPE_P (return_type)
3183 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3184 pedwarn (loc, 0,
3185 "function with qualified void return type called");
3188 argarray = vec_safe_address (params);
3190 /* Check that arguments to builtin functions match the expectations. */
3191 if (fundecl
3192 && fndecl_built_in_p (fundecl)
3193 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3194 orig_fundecl, nargs, argarray))
3195 return error_mark_node;
3197 /* Check that the arguments to the function are valid. */
3198 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3199 nargs, argarray, &arg_loc);
3201 if (name != NULL_TREE
3202 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3204 if (require_constant_value)
3205 result
3206 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3207 function, nargs, argarray);
3208 else
3209 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3210 function, nargs, argarray);
3211 if (TREE_CODE (result) == NOP_EXPR
3212 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3213 STRIP_TYPE_NOPS (result);
3215 else
3216 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3217 function, nargs, argarray);
3218 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3219 later. */
3220 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3221 TREE_NO_WARNING (result) = 1;
3223 /* In this improbable scenario, a nested function returns a VM type.
3224 Create a TARGET_EXPR so that the call always has a LHS, much as
3225 what the C++ FE does for functions returning non-PODs. */
3226 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3228 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3229 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3230 NULL_TREE, NULL_TREE);
3233 if (VOID_TYPE_P (TREE_TYPE (result)))
3235 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3236 pedwarn (loc, 0,
3237 "function with qualified void return type called");
3238 return result;
3240 return require_complete_type (loc, result);
3243 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3245 tree
3246 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3247 tree function, vec<tree, va_gc> *params,
3248 vec<tree, va_gc> *origtypes)
3250 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3251 STRIP_TYPE_NOPS (function);
3253 /* Convert anything with function type to a pointer-to-function. */
3254 if (TREE_CODE (function) == FUNCTION_DECL)
3256 /* Implement type-directed function overloading for builtins.
3257 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3258 handle all the type checking. The result is a complete expression
3259 that implements this function call. */
3260 tree tem = resolve_overloaded_builtin (loc, function, params);
3261 if (tem)
3262 return tem;
3264 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3267 /* Helper for convert_arguments called to convert the VALue of argument
3268 number ARGNUM from ORIGTYPE to the corresponding parameter number
3269 PARMNUM and TYPE.
3270 PLOC is the location where the conversion is being performed.
3271 FUNCTION and FUNDECL are the same as in convert_arguments.
3272 VALTYPE is the original type of VAL before the conversion and,
3273 for EXCESS_PRECISION_EXPR, the operand of the expression.
3274 NPC is true if VAL represents the null pointer constant (VAL itself
3275 will have been folded to an integer constant).
3276 RNAME is the same as FUNCTION except in Objective C when it's
3277 the function selector.
3278 EXCESS_PRECISION is true when VAL was originally represented
3279 as EXCESS_PRECISION_EXPR.
3280 WARNOPT is the same as in convert_for_assignment. */
3282 static tree
3283 convert_argument (location_t ploc, tree function, tree fundecl,
3284 tree type, tree origtype, tree val, tree valtype,
3285 bool npc, tree rname, int parmnum, int argnum,
3286 bool excess_precision, int warnopt)
3288 /* Formal parm type is specified by a function prototype. */
3290 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3292 error_at (ploc, "type of formal parameter %d is incomplete",
3293 parmnum + 1);
3294 return val;
3297 /* Optionally warn about conversions that differ from the default
3298 conversions. */
3299 if (warn_traditional_conversion || warn_traditional)
3301 unsigned int formal_prec = TYPE_PRECISION (type);
3303 if (INTEGRAL_TYPE_P (type)
3304 && TREE_CODE (valtype) == REAL_TYPE)
3305 warning_at (ploc, OPT_Wtraditional_conversion,
3306 "passing argument %d of %qE as integer rather "
3307 "than floating due to prototype",
3308 argnum, rname);
3309 if (INTEGRAL_TYPE_P (type)
3310 && TREE_CODE (valtype) == COMPLEX_TYPE)
3311 warning_at (ploc, OPT_Wtraditional_conversion,
3312 "passing argument %d of %qE as integer rather "
3313 "than complex due to prototype",
3314 argnum, rname);
3315 else if (TREE_CODE (type) == COMPLEX_TYPE
3316 && TREE_CODE (valtype) == REAL_TYPE)
3317 warning_at (ploc, OPT_Wtraditional_conversion,
3318 "passing argument %d of %qE as complex rather "
3319 "than floating due to prototype",
3320 argnum, rname);
3321 else if (TREE_CODE (type) == REAL_TYPE
3322 && INTEGRAL_TYPE_P (valtype))
3323 warning_at (ploc, OPT_Wtraditional_conversion,
3324 "passing argument %d of %qE as floating rather "
3325 "than integer due to prototype",
3326 argnum, rname);
3327 else if (TREE_CODE (type) == COMPLEX_TYPE
3328 && INTEGRAL_TYPE_P (valtype))
3329 warning_at (ploc, OPT_Wtraditional_conversion,
3330 "passing argument %d of %qE as complex rather "
3331 "than integer due to prototype",
3332 argnum, rname);
3333 else if (TREE_CODE (type) == REAL_TYPE
3334 && TREE_CODE (valtype) == COMPLEX_TYPE)
3335 warning_at (ploc, OPT_Wtraditional_conversion,
3336 "passing argument %d of %qE as floating rather "
3337 "than complex due to prototype",
3338 argnum, rname);
3339 /* ??? At some point, messages should be written about
3340 conversions between complex types, but that's too messy
3341 to do now. */
3342 else if (TREE_CODE (type) == REAL_TYPE
3343 && TREE_CODE (valtype) == REAL_TYPE)
3345 /* Warn if any argument is passed as `float',
3346 since without a prototype it would be `double'. */
3347 if (formal_prec == TYPE_PRECISION (float_type_node)
3348 && type != dfloat32_type_node)
3349 warning_at (ploc, 0,
3350 "passing argument %d of %qE as %<float%> "
3351 "rather than %<double%> due to prototype",
3352 argnum, rname);
3354 /* Warn if mismatch between argument and prototype
3355 for decimal float types. Warn of conversions with
3356 binary float types and of precision narrowing due to
3357 prototype. */
3358 else if (type != valtype
3359 && (type == dfloat32_type_node
3360 || type == dfloat64_type_node
3361 || type == dfloat128_type_node
3362 || valtype == dfloat32_type_node
3363 || valtype == dfloat64_type_node
3364 || valtype == dfloat128_type_node)
3365 && (formal_prec
3366 <= TYPE_PRECISION (valtype)
3367 || (type == dfloat128_type_node
3368 && (valtype
3369 != dfloat64_type_node
3370 && (valtype
3371 != dfloat32_type_node)))
3372 || (type == dfloat64_type_node
3373 && (valtype
3374 != dfloat32_type_node))))
3375 warning_at (ploc, 0,
3376 "passing argument %d of %qE as %qT "
3377 "rather than %qT due to prototype",
3378 argnum, rname, type, valtype);
3381 /* Detect integer changing in width or signedness.
3382 These warnings are only activated with
3383 -Wtraditional-conversion, not with -Wtraditional. */
3384 else if (warn_traditional_conversion
3385 && INTEGRAL_TYPE_P (type)
3386 && INTEGRAL_TYPE_P (valtype))
3388 tree would_have_been = default_conversion (val);
3389 tree type1 = TREE_TYPE (would_have_been);
3391 if (val == error_mark_node)
3392 /* VAL could have been of incomplete type. */;
3393 else if (TREE_CODE (type) == ENUMERAL_TYPE
3394 && (TYPE_MAIN_VARIANT (type)
3395 == TYPE_MAIN_VARIANT (valtype)))
3396 /* No warning if function asks for enum
3397 and the actual arg is that enum type. */
3399 else if (formal_prec != TYPE_PRECISION (type1))
3400 warning_at (ploc, OPT_Wtraditional_conversion,
3401 "passing argument %d of %qE "
3402 "with different width due to prototype",
3403 argnum, rname);
3404 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3406 /* Don't complain if the formal parameter type
3407 is an enum, because we can't tell now whether
3408 the value was an enum--even the same enum. */
3409 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3411 else if (TREE_CODE (val) == INTEGER_CST
3412 && int_fits_type_p (val, type))
3413 /* Change in signedness doesn't matter
3414 if a constant value is unaffected. */
3416 /* If the value is extended from a narrower
3417 unsigned type, it doesn't matter whether we
3418 pass it as signed or unsigned; the value
3419 certainly is the same either way. */
3420 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3421 && TYPE_UNSIGNED (valtype))
3423 else if (TYPE_UNSIGNED (type))
3424 warning_at (ploc, OPT_Wtraditional_conversion,
3425 "passing argument %d of %qE "
3426 "as unsigned due to prototype",
3427 argnum, rname);
3428 else
3429 warning_at (ploc, OPT_Wtraditional_conversion,
3430 "passing argument %d of %qE "
3431 "as signed due to prototype",
3432 argnum, rname);
3436 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3437 sake of better warnings from convert_and_check. */
3438 if (excess_precision)
3439 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3441 tree parmval = convert_for_assignment (ploc, ploc, type,
3442 val, origtype, ic_argpass,
3443 npc, fundecl, function,
3444 parmnum + 1, warnopt);
3446 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3447 && INTEGRAL_TYPE_P (type)
3448 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3449 parmval = default_conversion (parmval);
3451 return parmval;
3454 /* Convert the argument expressions in the vector VALUES
3455 to the types in the list TYPELIST.
3457 If TYPELIST is exhausted, or when an element has NULL as its type,
3458 perform the default conversions.
3460 ORIGTYPES is the original types of the expressions in VALUES. This
3461 holds the type of enum values which have been converted to integral
3462 types. It may be NULL.
3464 FUNCTION is a tree for the called function. It is used only for
3465 error messages, where it is formatted with %qE.
3467 This is also where warnings about wrong number of args are generated.
3469 ARG_LOC are locations of function arguments (if any).
3471 Returns the actual number of arguments processed (which may be less
3472 than the length of VALUES in some error situations), or -1 on
3473 failure. */
3475 static int
3476 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3477 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3478 tree function, tree fundecl)
3480 unsigned int parmnum;
3481 bool error_args = false;
3482 const bool type_generic = fundecl
3483 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3484 bool type_generic_remove_excess_precision = false;
3485 bool type_generic_overflow_p = false;
3486 tree selector;
3488 /* Change pointer to function to the function itself for
3489 diagnostics. */
3490 if (TREE_CODE (function) == ADDR_EXPR
3491 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3492 function = TREE_OPERAND (function, 0);
3494 /* Handle an ObjC selector specially for diagnostics. */
3495 selector = objc_message_selector ();
3497 /* For a call to a built-in function declared without a prototype,
3498 set to the built-in function's argument list. */
3499 tree builtin_typelist = NULL_TREE;
3501 /* For type-generic built-in functions, determine whether excess
3502 precision should be removed (classification) or not
3503 (comparison). */
3504 if (fundecl
3505 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3507 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3508 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3510 /* For a call to a built-in function declared without a prototype
3511 use the types of the parameters of the internal built-in to
3512 match those of the arguments to. */
3513 if (tree bdecl = builtin_decl_explicit (code))
3514 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3517 /* For type-generic built-in functions, determine whether excess
3518 precision should be removed (classification) or not
3519 (comparison). */
3520 if (type_generic)
3521 switch (code)
3523 case BUILT_IN_ISFINITE:
3524 case BUILT_IN_ISINF:
3525 case BUILT_IN_ISINF_SIGN:
3526 case BUILT_IN_ISNAN:
3527 case BUILT_IN_ISNORMAL:
3528 case BUILT_IN_FPCLASSIFY:
3529 type_generic_remove_excess_precision = true;
3530 break;
3532 case BUILT_IN_ADD_OVERFLOW_P:
3533 case BUILT_IN_SUB_OVERFLOW_P:
3534 case BUILT_IN_MUL_OVERFLOW_P:
3535 /* The last argument of these type-generic builtins
3536 should not be promoted. */
3537 type_generic_overflow_p = true;
3538 break;
3540 default:
3541 break;
3545 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3546 individual converted arguments. */
3548 tree typetail, builtin_typetail, val;
3549 for (typetail = typelist,
3550 builtin_typetail = builtin_typelist,
3551 parmnum = 0;
3552 values && values->iterate (parmnum, &val);
3553 ++parmnum)
3555 /* The type of the function parameter (if it was declared with one). */
3556 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3557 /* The type of the built-in function parameter (if the function
3558 is a built-in). Used to detect type incompatibilities in
3559 calls to built-ins declared without a prototype. */
3560 tree builtin_type = (builtin_typetail
3561 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3562 /* The original type of the argument being passed to the function. */
3563 tree valtype = TREE_TYPE (val);
3564 /* The called function (or function selector in Objective C). */
3565 tree rname = function;
3566 int argnum = parmnum + 1;
3567 const char *invalid_func_diag;
3568 /* Set for EXCESS_PRECISION_EXPR arguments. */
3569 bool excess_precision = false;
3570 /* The value of the argument after conversion to the type
3571 of the function parameter it is passed to. */
3572 tree parmval;
3573 /* Some __atomic_* builtins have additional hidden argument at
3574 position 0. */
3575 location_t ploc
3576 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3577 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3578 : input_location;
3580 if (type == void_type_node)
3582 if (selector)
3583 error_at (loc, "too many arguments to method %qE", selector);
3584 else
3585 error_at (loc, "too many arguments to function %qE", function);
3586 inform_declaration (fundecl);
3587 return error_args ? -1 : (int) parmnum;
3590 if (builtin_type == void_type_node)
3592 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3593 "too many arguments to built-in function %qE "
3594 "expecting %d", function, parmnum))
3595 inform_declaration (fundecl);
3596 builtin_typetail = NULL_TREE;
3599 if (selector && argnum > 2)
3601 rname = selector;
3602 argnum -= 2;
3605 /* Determine if VAL is a null pointer constant before folding it. */
3606 bool npc = null_pointer_constant_p (val);
3608 /* If there is excess precision and a prototype, convert once to
3609 the required type rather than converting via the semantic
3610 type. Likewise without a prototype a float value represented
3611 as long double should be converted once to double. But for
3612 type-generic classification functions excess precision must
3613 be removed here. */
3614 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3615 && (type || !type_generic || !type_generic_remove_excess_precision))
3617 val = TREE_OPERAND (val, 0);
3618 excess_precision = true;
3620 val = c_fully_fold (val, false, NULL);
3621 STRIP_TYPE_NOPS (val);
3623 val = require_complete_type (ploc, val);
3625 /* Some floating-point arguments must be promoted to double when
3626 no type is specified by a prototype. This applies to
3627 arguments of type float, and to architecture-specific types
3628 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3629 bool promote_float_arg = false;
3630 if (type == NULL_TREE
3631 && TREE_CODE (valtype) == REAL_TYPE
3632 && (TYPE_PRECISION (valtype)
3633 <= TYPE_PRECISION (double_type_node))
3634 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3635 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3636 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3638 /* Promote this argument, unless it has a _FloatN or
3639 _FloatNx type. */
3640 promote_float_arg = true;
3641 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3642 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3644 promote_float_arg = false;
3645 break;
3649 if (type != NULL_TREE)
3651 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3652 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3653 val, valtype, npc, rname, parmnum, argnum,
3654 excess_precision, 0);
3656 else if (promote_float_arg)
3658 if (type_generic)
3659 parmval = val;
3660 else
3662 /* Convert `float' to `double'. */
3663 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3664 warning_at (ploc, OPT_Wdouble_promotion,
3665 "implicit conversion from %qT to %qT when passing "
3666 "argument to function",
3667 valtype, double_type_node);
3668 parmval = convert (double_type_node, val);
3671 else if ((excess_precision && !type_generic)
3672 || (type_generic_overflow_p && parmnum == 2))
3673 /* A "double" argument with excess precision being passed
3674 without a prototype or in variable arguments.
3675 The last argument of __builtin_*_overflow_p should not be
3676 promoted. */
3677 parmval = convert (valtype, val);
3678 else if ((invalid_func_diag =
3679 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3681 error (invalid_func_diag);
3682 return -1;
3684 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3686 return -1;
3688 else
3689 /* Convert `short' and `char' to full-size `int'. */
3690 parmval = default_conversion (val);
3692 (*values)[parmnum] = parmval;
3693 if (parmval == error_mark_node)
3694 error_args = true;
3696 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3698 /* For a call to a built-in function declared without a prototype,
3699 perform the conversions from the argument to the expected type
3700 but issue warnings rather than errors for any mismatches.
3701 Ignore the converted argument and use the PARMVAL obtained
3702 above by applying default conversions instead. */
3703 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3704 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3705 val, valtype, npc, rname, parmnum, argnum,
3706 excess_precision,
3707 OPT_Wbuiltin_declaration_mismatch);
3710 if (typetail)
3711 typetail = TREE_CHAIN (typetail);
3713 if (builtin_typetail)
3714 builtin_typetail = TREE_CHAIN (builtin_typetail);
3717 gcc_assert (parmnum == vec_safe_length (values));
3719 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3721 error_at (loc, "too few arguments to function %qE", function);
3722 inform_declaration (fundecl);
3723 return -1;
3726 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3728 unsigned nargs = parmnum;
3729 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3730 ++nargs;
3732 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3733 "too few arguments to built-in function %qE "
3734 "expecting %u", function, nargs - 1))
3735 inform_declaration (fundecl);
3738 return error_args ? -1 : (int) parmnum;
3741 /* This is the entry point used by the parser to build unary operators
3742 in the input. CODE, a tree_code, specifies the unary operator, and
3743 ARG is the operand. For unary plus, the C parser currently uses
3744 CONVERT_EXPR for code.
3746 LOC is the location to use for the tree generated.
3749 struct c_expr
3750 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3752 struct c_expr result;
3754 result.original_code = code;
3755 result.original_type = NULL;
3757 if (reject_gcc_builtin (arg.value))
3759 result.value = error_mark_node;
3761 else
3763 result.value = build_unary_op (loc, code, arg.value, false);
3765 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3766 overflow_warning (loc, result.value, arg.value);
3769 /* We are typically called when parsing a prefix token at LOC acting on
3770 ARG. Reflect this by updating the source range of the result to
3771 start at LOC and end at the end of ARG. */
3772 set_c_expr_source_range (&result,
3773 loc, arg.get_finish ());
3775 return result;
3778 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3780 bool
3781 char_type_p (tree type)
3783 return (type == char_type_node
3784 || type == unsigned_char_type_node
3785 || type == signed_char_type_node
3786 || type == char16_type_node
3787 || type == char32_type_node);
3790 /* This is the entry point used by the parser to build binary operators
3791 in the input. CODE, a tree_code, specifies the binary operator, and
3792 ARG1 and ARG2 are the operands. In addition to constructing the
3793 expression, we check for operands that were written with other binary
3794 operators in a way that is likely to confuse the user.
3796 LOCATION is the location of the binary operator. */
3798 struct c_expr
3799 parser_build_binary_op (location_t location, enum tree_code code,
3800 struct c_expr arg1, struct c_expr arg2)
3802 struct c_expr result;
3804 enum tree_code code1 = arg1.original_code;
3805 enum tree_code code2 = arg2.original_code;
3806 tree type1 = (arg1.original_type
3807 ? arg1.original_type
3808 : TREE_TYPE (arg1.value));
3809 tree type2 = (arg2.original_type
3810 ? arg2.original_type
3811 : TREE_TYPE (arg2.value));
3813 result.value = build_binary_op (location, code,
3814 arg1.value, arg2.value, true);
3815 result.original_code = code;
3816 result.original_type = NULL;
3818 if (TREE_CODE (result.value) == ERROR_MARK)
3820 set_c_expr_source_range (&result,
3821 arg1.get_start (),
3822 arg2.get_finish ());
3823 return result;
3826 if (location != UNKNOWN_LOCATION)
3827 protected_set_expr_location (result.value, location);
3829 set_c_expr_source_range (&result,
3830 arg1.get_start (),
3831 arg2.get_finish ());
3833 /* Check for cases such as x+y<<z which users are likely
3834 to misinterpret. */
3835 if (warn_parentheses)
3836 warn_about_parentheses (location, code, code1, arg1.value, code2,
3837 arg2.value);
3839 if (warn_logical_op)
3840 warn_logical_operator (location, code, TREE_TYPE (result.value),
3841 code1, arg1.value, code2, arg2.value);
3843 if (warn_tautological_compare)
3845 tree lhs = arg1.value;
3846 tree rhs = arg2.value;
3847 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3849 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3850 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3851 lhs = NULL_TREE;
3852 else
3853 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3855 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3857 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3858 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3859 rhs = NULL_TREE;
3860 else
3861 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3863 if (lhs != NULL_TREE && rhs != NULL_TREE)
3864 warn_tautological_cmp (location, code, lhs, rhs);
3867 if (warn_logical_not_paren
3868 && TREE_CODE_CLASS (code) == tcc_comparison
3869 && code1 == TRUTH_NOT_EXPR
3870 && code2 != TRUTH_NOT_EXPR
3871 /* Avoid warning for !!x == y. */
3872 && (TREE_CODE (arg1.value) != NE_EXPR
3873 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3875 /* Avoid warning for !b == y where b has _Bool type. */
3876 tree t = integer_zero_node;
3877 if (TREE_CODE (arg1.value) == EQ_EXPR
3878 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3879 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3881 t = TREE_OPERAND (arg1.value, 0);
3884 if (TREE_TYPE (t) != integer_type_node)
3885 break;
3886 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3887 t = C_MAYBE_CONST_EXPR_EXPR (t);
3888 else if (CONVERT_EXPR_P (t))
3889 t = TREE_OPERAND (t, 0);
3890 else
3891 break;
3893 while (1);
3895 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3896 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3899 /* Warn about comparisons against string literals, with the exception
3900 of testing for equality or inequality of a string literal with NULL. */
3901 if (code == EQ_EXPR || code == NE_EXPR)
3903 if ((code1 == STRING_CST
3904 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3905 || (code2 == STRING_CST
3906 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3907 warning_at (location, OPT_Waddress,
3908 "comparison with string literal results in unspecified behavior");
3909 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3910 if (POINTER_TYPE_P (type1)
3911 && null_pointer_constant_p (arg2.value)
3912 && char_type_p (type2))
3914 auto_diagnostic_group d;
3915 if (warning_at (location, OPT_Wpointer_compare,
3916 "comparison between pointer and zero character "
3917 "constant"))
3918 inform (arg1.get_start (),
3919 "did you mean to dereference the pointer?");
3921 else if (POINTER_TYPE_P (type2)
3922 && null_pointer_constant_p (arg1.value)
3923 && char_type_p (type1))
3925 auto_diagnostic_group d;
3926 if (warning_at (location, OPT_Wpointer_compare,
3927 "comparison between pointer and zero character "
3928 "constant"))
3929 inform (arg2.get_start (),
3930 "did you mean to dereference the pointer?");
3933 else if (TREE_CODE_CLASS (code) == tcc_comparison
3934 && (code1 == STRING_CST || code2 == STRING_CST))
3935 warning_at (location, OPT_Waddress,
3936 "comparison with string literal results in unspecified behavior");
3938 if (TREE_OVERFLOW_P (result.value)
3939 && !TREE_OVERFLOW_P (arg1.value)
3940 && !TREE_OVERFLOW_P (arg2.value))
3941 overflow_warning (location, result.value);
3943 /* Warn about comparisons of different enum types. */
3944 if (warn_enum_compare
3945 && TREE_CODE_CLASS (code) == tcc_comparison
3946 && TREE_CODE (type1) == ENUMERAL_TYPE
3947 && TREE_CODE (type2) == ENUMERAL_TYPE
3948 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3949 warning_at (location, OPT_Wenum_compare,
3950 "comparison between %qT and %qT",
3951 type1, type2);
3953 return result;
3956 /* Return a tree for the difference of pointers OP0 and OP1.
3957 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3958 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3960 static tree
3961 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3963 tree restype = ptrdiff_type_node;
3964 tree result, inttype;
3966 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3967 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3968 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3969 tree orig_op0 = op0;
3970 tree orig_op1 = op1;
3972 /* If the operands point into different address spaces, we need to
3973 explicitly convert them to pointers into the common address space
3974 before we can subtract the numerical address values. */
3975 if (as0 != as1)
3977 addr_space_t as_common;
3978 tree common_type;
3980 /* Determine the common superset address space. This is guaranteed
3981 to exist because the caller verified that comp_target_types
3982 returned non-zero. */
3983 if (!addr_space_superset (as0, as1, &as_common))
3984 gcc_unreachable ();
3986 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3987 op0 = convert (common_type, op0);
3988 op1 = convert (common_type, op1);
3991 /* Determine integer type result of the subtraction. This will usually
3992 be the same as the result type (ptrdiff_t), but may need to be a wider
3993 type if pointers for the address space are wider than ptrdiff_t. */
3994 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3995 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3996 else
3997 inttype = restype;
3999 if (TREE_CODE (target_type) == VOID_TYPE)
4000 pedwarn (loc, OPT_Wpointer_arith,
4001 "pointer of type %<void *%> used in subtraction");
4002 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4003 pedwarn (loc, OPT_Wpointer_arith,
4004 "pointer to a function used in subtraction");
4006 if (current_function_decl != NULL_TREE
4007 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4009 op0 = save_expr (op0);
4010 op1 = save_expr (op1);
4012 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4013 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4016 /* First do the subtraction, then build the divide operator
4017 and only convert at the very end.
4018 Do not do default conversions in case restype is a short type. */
4020 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4021 pointers. If some platform cannot provide that, or has a larger
4022 ptrdiff_type to support differences larger than half the address
4023 space, cast the pointers to some larger integer type and do the
4024 computations in that type. */
4025 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4026 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4027 convert (inttype, op1), false);
4028 else
4030 /* Cast away qualifiers. */
4031 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4032 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4033 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4036 /* This generates an error if op1 is pointer to incomplete type. */
4037 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4038 error_at (loc, "arithmetic on pointer to an incomplete type");
4039 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4040 TREE_TYPE (TREE_TYPE (orig_op0))))
4041 verify_type_context (loc, TCTX_POINTER_ARITH,
4042 TREE_TYPE (TREE_TYPE (orig_op1)));
4044 op1 = c_size_in_bytes (target_type);
4046 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4047 error_at (loc, "arithmetic on pointer to an empty aggregate");
4049 /* Divide by the size, in easiest possible way. */
4050 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4051 op0, convert (inttype, op1));
4053 /* Convert to final result type if necessary. */
4054 return convert (restype, result);
4057 /* Expand atomic compound assignments into an appropriate sequence as
4058 specified by the C11 standard section 6.5.16.2.
4060 _Atomic T1 E1
4061 T2 E2
4062 E1 op= E2
4064 This sequence is used for all types for which these operations are
4065 supported.
4067 In addition, built-in versions of the 'fe' prefixed routines may
4068 need to be invoked for floating point (real, complex or vector) when
4069 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4071 T1 newval;
4072 T1 old;
4073 T1 *addr
4074 T2 val
4075 fenv_t fenv
4077 addr = &E1;
4078 val = (E2);
4079 __atomic_load (addr, &old, SEQ_CST);
4080 feholdexcept (&fenv);
4081 loop:
4082 newval = old op val;
4083 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4084 SEQ_CST))
4085 goto done;
4086 feclearexcept (FE_ALL_EXCEPT);
4087 goto loop:
4088 done:
4089 feupdateenv (&fenv);
4091 The compiler will issue the __atomic_fetch_* built-in when possible,
4092 otherwise it will generate the generic form of the atomic operations.
4093 This requires temp(s) and has their address taken. The atomic processing
4094 is smart enough to figure out when the size of an object can utilize
4095 a lock-free version, and convert the built-in call to the appropriate
4096 lock-free routine. The optimizers will then dispose of any temps that
4097 are no longer required, and lock-free implementations are utilized as
4098 long as there is target support for the required size.
4100 If the operator is NOP_EXPR, then this is a simple assignment, and
4101 an __atomic_store is issued to perform the assignment rather than
4102 the above loop. */
4104 /* Build an atomic assignment at LOC, expanding into the proper
4105 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4106 the result of the operation, unless RETURN_OLD_P, in which case
4107 return the old value of LHS (this is only for postincrement and
4108 postdecrement). */
4110 static tree
4111 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4112 tree rhs, bool return_old_p)
4114 tree fndecl, func_call;
4115 vec<tree, va_gc> *params;
4116 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4117 tree old, old_addr;
4118 tree compound_stmt = NULL_TREE;
4119 tree stmt, goto_stmt;
4120 tree loop_label, loop_decl, done_label, done_decl;
4122 tree lhs_type = TREE_TYPE (lhs);
4123 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4124 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4125 tree rhs_semantic_type = TREE_TYPE (rhs);
4126 tree nonatomic_rhs_semantic_type;
4127 tree rhs_type;
4129 gcc_assert (TYPE_ATOMIC (lhs_type));
4131 if (return_old_p)
4132 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4134 /* Allocate enough vector items for a compare_exchange. */
4135 vec_alloc (params, 6);
4137 /* Create a compound statement to hold the sequence of statements
4138 with a loop. */
4139 if (modifycode != NOP_EXPR)
4141 compound_stmt = c_begin_compound_stmt (false);
4143 /* For consistency with build_modify_expr on non-_Atomic,
4144 mark the lhs as read. Also, it would be very hard to match
4145 such expressions in mark_exp_read. */
4146 mark_exp_read (lhs);
4149 /* Remove any excess precision (which is only present here in the
4150 case of compound assignments). */
4151 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4153 gcc_assert (modifycode != NOP_EXPR);
4154 rhs = TREE_OPERAND (rhs, 0);
4156 rhs_type = TREE_TYPE (rhs);
4158 /* Fold the RHS if it hasn't already been folded. */
4159 if (modifycode != NOP_EXPR)
4160 rhs = c_fully_fold (rhs, false, NULL);
4162 /* Remove the qualifiers for the rest of the expressions and create
4163 the VAL temp variable to hold the RHS. */
4164 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4165 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4166 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4167 TYPE_UNQUALIFIED);
4168 val = create_tmp_var_raw (nonatomic_rhs_type);
4169 TREE_ADDRESSABLE (val) = 1;
4170 TREE_NO_WARNING (val) = 1;
4171 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4172 NULL_TREE);
4173 TREE_SIDE_EFFECTS (rhs) = 1;
4174 SET_EXPR_LOCATION (rhs, loc);
4175 if (modifycode != NOP_EXPR)
4176 add_stmt (rhs);
4178 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4179 an atomic_store. */
4180 if (modifycode == NOP_EXPR)
4182 compound_stmt = rhs;
4183 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4184 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4185 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4186 params->quick_push (lhs_addr);
4187 params->quick_push (rhs);
4188 params->quick_push (seq_cst);
4189 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4191 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4192 compound_stmt, func_call);
4194 /* VAL is the value which was stored, return a COMPOUND_STMT of
4195 the statement and that value. */
4196 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4199 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4200 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4201 isn't applicable for such builtins. ??? Do we want to handle enums? */
4202 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4203 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4205 built_in_function fncode;
4206 switch (modifycode)
4208 case PLUS_EXPR:
4209 case POINTER_PLUS_EXPR:
4210 fncode = (return_old_p
4211 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4212 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4213 break;
4214 case MINUS_EXPR:
4215 fncode = (return_old_p
4216 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4217 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4218 break;
4219 case BIT_AND_EXPR:
4220 fncode = (return_old_p
4221 ? BUILT_IN_ATOMIC_FETCH_AND_N
4222 : BUILT_IN_ATOMIC_AND_FETCH_N);
4223 break;
4224 case BIT_IOR_EXPR:
4225 fncode = (return_old_p
4226 ? BUILT_IN_ATOMIC_FETCH_OR_N
4227 : BUILT_IN_ATOMIC_OR_FETCH_N);
4228 break;
4229 case BIT_XOR_EXPR:
4230 fncode = (return_old_p
4231 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4232 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4233 break;
4234 default:
4235 goto cas_loop;
4238 /* We can only use "_1" through "_16" variants of the atomic fetch
4239 built-ins. */
4240 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4241 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4242 goto cas_loop;
4244 /* If this is a pointer type, we need to multiply by the size of
4245 the pointer target type. */
4246 if (POINTER_TYPE_P (lhs_type))
4248 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4249 /* ??? This would introduce -Wdiscarded-qualifiers
4250 warning: __atomic_fetch_* expect volatile void *
4251 type as the first argument. (Assignments between
4252 atomic and non-atomic objects are OK.) */
4253 || TYPE_RESTRICT (lhs_type))
4254 goto cas_loop;
4255 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4256 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4257 convert (ptrdiff_type_node, rhs),
4258 convert (ptrdiff_type_node, sz));
4261 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4262 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4263 fndecl = builtin_decl_explicit (fncode);
4264 params->quick_push (lhs_addr);
4265 params->quick_push (rhs);
4266 params->quick_push (seq_cst);
4267 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4269 newval = create_tmp_var_raw (nonatomic_lhs_type);
4270 TREE_ADDRESSABLE (newval) = 1;
4271 TREE_NO_WARNING (newval) = 1;
4272 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4273 NULL_TREE, NULL_TREE);
4274 SET_EXPR_LOCATION (rhs, loc);
4275 add_stmt (rhs);
4277 /* Finish the compound statement. */
4278 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4280 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4281 the statement and that value. */
4282 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4285 cas_loop:
4286 /* Create the variables and labels required for the op= form. */
4287 old = create_tmp_var_raw (nonatomic_lhs_type);
4288 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4289 TREE_ADDRESSABLE (old) = 1;
4290 TREE_NO_WARNING (old) = 1;
4292 newval = create_tmp_var_raw (nonatomic_lhs_type);
4293 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4294 TREE_ADDRESSABLE (newval) = 1;
4295 TREE_NO_WARNING (newval) = 1;
4297 loop_decl = create_artificial_label (loc);
4298 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4300 done_decl = create_artificial_label (loc);
4301 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4303 /* __atomic_load (addr, &old, SEQ_CST). */
4304 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4305 params->quick_push (lhs_addr);
4306 params->quick_push (old_addr);
4307 params->quick_push (seq_cst);
4308 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4309 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4310 NULL_TREE);
4311 add_stmt (old);
4312 params->truncate (0);
4314 /* Create the expressions for floating-point environment
4315 manipulation, if required. */
4316 bool need_fenv = (flag_trapping_math
4317 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4318 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4319 if (need_fenv)
4320 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4322 if (hold_call)
4323 add_stmt (hold_call);
4325 /* loop: */
4326 add_stmt (loop_label);
4328 /* newval = old + val; */
4329 if (rhs_type != rhs_semantic_type)
4330 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4331 rhs = build_binary_op (loc, modifycode, old, val, true);
4332 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4334 tree eptype = TREE_TYPE (rhs);
4335 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4336 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4338 else
4339 rhs = c_fully_fold (rhs, false, NULL);
4340 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4341 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4342 NULL_TREE, 0);
4343 if (rhs != error_mark_node)
4345 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4346 NULL_TREE);
4347 SET_EXPR_LOCATION (rhs, loc);
4348 add_stmt (rhs);
4351 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4352 goto done; */
4353 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4354 params->quick_push (lhs_addr);
4355 params->quick_push (old_addr);
4356 params->quick_push (newval_addr);
4357 params->quick_push (integer_zero_node);
4358 params->quick_push (seq_cst);
4359 params->quick_push (seq_cst);
4360 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4362 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4363 SET_EXPR_LOCATION (goto_stmt, loc);
4365 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4366 SET_EXPR_LOCATION (stmt, loc);
4367 add_stmt (stmt);
4369 if (clear_call)
4370 add_stmt (clear_call);
4372 /* goto loop; */
4373 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4374 SET_EXPR_LOCATION (goto_stmt, loc);
4375 add_stmt (goto_stmt);
4377 /* done: */
4378 add_stmt (done_label);
4380 if (update_call)
4381 add_stmt (update_call);
4383 /* Finish the compound statement. */
4384 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4386 /* NEWVAL is the value that was successfully stored, return a
4387 COMPOUND_EXPR of the statement and the appropriate value. */
4388 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4389 return_old_p ? old : newval);
4392 /* Construct and perhaps optimize a tree representation
4393 for a unary operation. CODE, a tree_code, specifies the operation
4394 and XARG is the operand.
4395 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4396 promotions (such as from short to int).
4397 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4398 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4399 to pointers in C99.
4401 LOCATION is the location of the operator. */
4403 tree
4404 build_unary_op (location_t location, enum tree_code code, tree xarg,
4405 bool noconvert)
4407 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4408 tree arg = xarg;
4409 tree argtype = NULL_TREE;
4410 enum tree_code typecode;
4411 tree val;
4412 tree ret = error_mark_node;
4413 tree eptype = NULL_TREE;
4414 const char *invalid_op_diag;
4415 bool int_operands;
4417 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4418 if (int_operands)
4419 arg = remove_c_maybe_const_expr (arg);
4421 if (code != ADDR_EXPR)
4422 arg = require_complete_type (location, arg);
4424 typecode = TREE_CODE (TREE_TYPE (arg));
4425 if (typecode == ERROR_MARK)
4426 return error_mark_node;
4427 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4428 typecode = INTEGER_TYPE;
4430 if ((invalid_op_diag
4431 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4433 error_at (location, invalid_op_diag);
4434 return error_mark_node;
4437 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4439 eptype = TREE_TYPE (arg);
4440 arg = TREE_OPERAND (arg, 0);
4443 switch (code)
4445 case CONVERT_EXPR:
4446 /* This is used for unary plus, because a CONVERT_EXPR
4447 is enough to prevent anybody from looking inside for
4448 associativity, but won't generate any code. */
4449 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4450 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4451 || gnu_vector_type_p (TREE_TYPE (arg))))
4453 error_at (location, "wrong type argument to unary plus");
4454 return error_mark_node;
4456 else if (!noconvert)
4457 arg = default_conversion (arg);
4458 arg = non_lvalue_loc (location, arg);
4459 break;
4461 case NEGATE_EXPR:
4462 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4463 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4464 || gnu_vector_type_p (TREE_TYPE (arg))))
4466 error_at (location, "wrong type argument to unary minus");
4467 return error_mark_node;
4469 else if (!noconvert)
4470 arg = default_conversion (arg);
4471 break;
4473 case BIT_NOT_EXPR:
4474 /* ~ works on integer types and non float vectors. */
4475 if (typecode == INTEGER_TYPE
4476 || (gnu_vector_type_p (TREE_TYPE (arg))
4477 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4479 tree e = arg;
4481 /* Warn if the expression has boolean value. */
4482 while (TREE_CODE (e) == COMPOUND_EXPR)
4483 e = TREE_OPERAND (e, 1);
4485 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4486 || truth_value_p (TREE_CODE (e))))
4488 auto_diagnostic_group d;
4489 if (warning_at (location, OPT_Wbool_operation,
4490 "%<~%> on a boolean expression"))
4492 gcc_rich_location richloc (location);
4493 richloc.add_fixit_insert_before (location, "!");
4494 inform (&richloc, "did you mean to use logical not?");
4497 if (!noconvert)
4498 arg = default_conversion (arg);
4500 else if (typecode == COMPLEX_TYPE)
4502 code = CONJ_EXPR;
4503 pedwarn (location, OPT_Wpedantic,
4504 "ISO C does not support %<~%> for complex conjugation");
4505 if (!noconvert)
4506 arg = default_conversion (arg);
4508 else
4510 error_at (location, "wrong type argument to bit-complement");
4511 return error_mark_node;
4513 break;
4515 case ABS_EXPR:
4516 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4518 error_at (location, "wrong type argument to abs");
4519 return error_mark_node;
4521 else if (!noconvert)
4522 arg = default_conversion (arg);
4523 break;
4525 case ABSU_EXPR:
4526 if (!(typecode == INTEGER_TYPE))
4528 error_at (location, "wrong type argument to absu");
4529 return error_mark_node;
4531 else if (!noconvert)
4532 arg = default_conversion (arg);
4533 break;
4535 case CONJ_EXPR:
4536 /* Conjugating a real value is a no-op, but allow it anyway. */
4537 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4538 || typecode == COMPLEX_TYPE))
4540 error_at (location, "wrong type argument to conjugation");
4541 return error_mark_node;
4543 else if (!noconvert)
4544 arg = default_conversion (arg);
4545 break;
4547 case TRUTH_NOT_EXPR:
4548 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4549 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4550 && typecode != COMPLEX_TYPE)
4552 error_at (location,
4553 "wrong type argument to unary exclamation mark");
4554 return error_mark_node;
4556 if (int_operands)
4558 arg = c_objc_common_truthvalue_conversion (location, xarg);
4559 arg = remove_c_maybe_const_expr (arg);
4561 else
4562 arg = c_objc_common_truthvalue_conversion (location, arg);
4563 ret = invert_truthvalue_loc (location, arg);
4564 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4565 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4566 location = EXPR_LOCATION (ret);
4567 goto return_build_unary_op;
4569 case REALPART_EXPR:
4570 case IMAGPART_EXPR:
4571 ret = build_real_imag_expr (location, code, arg);
4572 if (ret == error_mark_node)
4573 return error_mark_node;
4574 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4575 eptype = TREE_TYPE (eptype);
4576 goto return_build_unary_op;
4578 case PREINCREMENT_EXPR:
4579 case POSTINCREMENT_EXPR:
4580 case PREDECREMENT_EXPR:
4581 case POSTDECREMENT_EXPR:
4583 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4585 tree inner = build_unary_op (location, code,
4586 C_MAYBE_CONST_EXPR_EXPR (arg),
4587 noconvert);
4588 if (inner == error_mark_node)
4589 return error_mark_node;
4590 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4591 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4592 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4593 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4594 goto return_build_unary_op;
4597 /* Complain about anything that is not a true lvalue. In
4598 Objective-C, skip this check for property_refs. */
4599 if (!objc_is_property_ref (arg)
4600 && !lvalue_or_else (location,
4601 arg, ((code == PREINCREMENT_EXPR
4602 || code == POSTINCREMENT_EXPR)
4603 ? lv_increment
4604 : lv_decrement)))
4605 return error_mark_node;
4607 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4609 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4610 warning_at (location, OPT_Wc___compat,
4611 "increment of enumeration value is invalid in C++");
4612 else
4613 warning_at (location, OPT_Wc___compat,
4614 "decrement of enumeration value is invalid in C++");
4617 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4619 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4620 warning_at (location, OPT_Wbool_operation,
4621 "increment of a boolean expression");
4622 else
4623 warning_at (location, OPT_Wbool_operation,
4624 "decrement of a boolean expression");
4627 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4628 arg = c_fully_fold (arg, false, NULL, true);
4630 bool atomic_op;
4631 atomic_op = really_atomic_lvalue (arg);
4633 /* Increment or decrement the real part of the value,
4634 and don't change the imaginary part. */
4635 if (typecode == COMPLEX_TYPE)
4637 tree real, imag;
4639 pedwarn (location, OPT_Wpedantic,
4640 "ISO C does not support %<++%> and %<--%> on complex types");
4642 if (!atomic_op)
4644 arg = stabilize_reference (arg);
4645 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4646 true);
4647 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4648 true);
4649 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4650 if (real == error_mark_node || imag == error_mark_node)
4651 return error_mark_node;
4652 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4653 real, imag);
4654 goto return_build_unary_op;
4658 /* Report invalid types. */
4660 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4661 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4662 && typecode != COMPLEX_TYPE
4663 && !gnu_vector_type_p (TREE_TYPE (arg)))
4665 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4666 error_at (location, "wrong type argument to increment");
4667 else
4668 error_at (location, "wrong type argument to decrement");
4670 return error_mark_node;
4674 tree inc;
4676 argtype = TREE_TYPE (arg);
4678 /* Compute the increment. */
4680 if (typecode == POINTER_TYPE)
4682 /* If pointer target is an incomplete type,
4683 we just cannot know how to do the arithmetic. */
4684 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4686 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4687 error_at (location,
4688 "increment of pointer to an incomplete type %qT",
4689 TREE_TYPE (argtype));
4690 else
4691 error_at (location,
4692 "decrement of pointer to an incomplete type %qT",
4693 TREE_TYPE (argtype));
4695 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4696 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4698 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4699 pedwarn (location, OPT_Wpointer_arith,
4700 "wrong type argument to increment");
4701 else
4702 pedwarn (location, OPT_Wpointer_arith,
4703 "wrong type argument to decrement");
4705 else
4706 verify_type_context (location, TCTX_POINTER_ARITH,
4707 TREE_TYPE (argtype));
4709 inc = c_size_in_bytes (TREE_TYPE (argtype));
4710 inc = convert_to_ptrofftype_loc (location, inc);
4712 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4714 /* For signed fract types, we invert ++ to -- or
4715 -- to ++, and change inc from 1 to -1, because
4716 it is not possible to represent 1 in signed fract constants.
4717 For unsigned fract types, the result always overflows and
4718 we get an undefined (original) or the maximum value. */
4719 if (code == PREINCREMENT_EXPR)
4720 code = PREDECREMENT_EXPR;
4721 else if (code == PREDECREMENT_EXPR)
4722 code = PREINCREMENT_EXPR;
4723 else if (code == POSTINCREMENT_EXPR)
4724 code = POSTDECREMENT_EXPR;
4725 else /* code == POSTDECREMENT_EXPR */
4726 code = POSTINCREMENT_EXPR;
4728 inc = integer_minus_one_node;
4729 inc = convert (argtype, inc);
4731 else
4733 inc = VECTOR_TYPE_P (argtype)
4734 ? build_one_cst (argtype)
4735 : integer_one_node;
4736 inc = convert (argtype, inc);
4739 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4740 need to ask Objective-C to build the increment or decrement
4741 expression for it. */
4742 if (objc_is_property_ref (arg))
4743 return objc_build_incr_expr_for_property_ref (location, code,
4744 arg, inc);
4746 /* Report a read-only lvalue. */
4747 if (TYPE_READONLY (argtype))
4749 readonly_error (location, arg,
4750 ((code == PREINCREMENT_EXPR
4751 || code == POSTINCREMENT_EXPR)
4752 ? lv_increment : lv_decrement));
4753 return error_mark_node;
4755 else if (TREE_READONLY (arg))
4756 readonly_warning (arg,
4757 ((code == PREINCREMENT_EXPR
4758 || code == POSTINCREMENT_EXPR)
4759 ? lv_increment : lv_decrement));
4761 /* If the argument is atomic, use the special code sequences for
4762 atomic compound assignment. */
4763 if (atomic_op)
4765 arg = stabilize_reference (arg);
4766 ret = build_atomic_assign (location, arg,
4767 ((code == PREINCREMENT_EXPR
4768 || code == POSTINCREMENT_EXPR)
4769 ? PLUS_EXPR
4770 : MINUS_EXPR),
4771 (FRACT_MODE_P (TYPE_MODE (argtype))
4772 ? inc
4773 : integer_one_node),
4774 (code == POSTINCREMENT_EXPR
4775 || code == POSTDECREMENT_EXPR));
4776 goto return_build_unary_op;
4779 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4780 val = boolean_increment (code, arg);
4781 else
4782 val = build2 (code, TREE_TYPE (arg), arg, inc);
4783 TREE_SIDE_EFFECTS (val) = 1;
4784 if (TREE_CODE (val) != code)
4785 TREE_NO_WARNING (val) = 1;
4786 ret = val;
4787 goto return_build_unary_op;
4790 case ADDR_EXPR:
4791 /* Note that this operation never does default_conversion. */
4793 /* The operand of unary '&' must be an lvalue (which excludes
4794 expressions of type void), or, in C99, the result of a [] or
4795 unary '*' operator. */
4796 if (VOID_TYPE_P (TREE_TYPE (arg))
4797 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4798 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4799 pedwarn (location, 0, "taking address of expression of type %<void%>");
4801 /* Let &* cancel out to simplify resulting code. */
4802 if (INDIRECT_REF_P (arg))
4804 /* Don't let this be an lvalue. */
4805 if (lvalue_p (TREE_OPERAND (arg, 0)))
4806 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4807 ret = TREE_OPERAND (arg, 0);
4808 goto return_build_unary_op;
4811 /* Anything not already handled and not a true memory reference
4812 or a non-lvalue array is an error. */
4813 if (typecode != FUNCTION_TYPE && !noconvert
4814 && !lvalue_or_else (location, arg, lv_addressof))
4815 return error_mark_node;
4817 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4818 folding later. */
4819 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4821 tree inner = build_unary_op (location, code,
4822 C_MAYBE_CONST_EXPR_EXPR (arg),
4823 noconvert);
4824 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4825 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4826 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4827 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4828 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4829 goto return_build_unary_op;
4832 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4833 argtype = TREE_TYPE (arg);
4835 /* If the lvalue is const or volatile, merge that into the type
4836 to which the address will point. This is only needed
4837 for function types. */
4838 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4839 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4840 && TREE_CODE (argtype) == FUNCTION_TYPE)
4842 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4843 int quals = orig_quals;
4845 if (TREE_READONLY (arg))
4846 quals |= TYPE_QUAL_CONST;
4847 if (TREE_THIS_VOLATILE (arg))
4848 quals |= TYPE_QUAL_VOLATILE;
4850 argtype = c_build_qualified_type (argtype, quals);
4853 switch (TREE_CODE (arg))
4855 case COMPONENT_REF:
4856 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4858 error_at (location, "cannot take address of bit-field %qD",
4859 TREE_OPERAND (arg, 1));
4860 return error_mark_node;
4863 /* fall through */
4865 case ARRAY_REF:
4866 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4868 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4869 && !POINTER_TYPE_P (TREE_TYPE (arg))
4870 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4872 error_at (location, "cannot take address of scalar with "
4873 "reverse storage order");
4874 return error_mark_node;
4877 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4878 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4879 warning_at (location, OPT_Wscalar_storage_order,
4880 "address of array with reverse scalar storage "
4881 "order requested");
4884 default:
4885 break;
4888 if (!c_mark_addressable (arg))
4889 return error_mark_node;
4891 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4892 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4894 argtype = build_pointer_type (argtype);
4896 /* ??? Cope with user tricks that amount to offsetof. Delete this
4897 when we have proper support for integer constant expressions. */
4898 val = get_base_address (arg);
4899 if (val && INDIRECT_REF_P (val)
4900 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4902 ret = fold_offsetof (arg, argtype);
4903 goto return_build_unary_op;
4906 val = build1 (ADDR_EXPR, argtype, arg);
4908 ret = val;
4909 goto return_build_unary_op;
4911 default:
4912 gcc_unreachable ();
4915 if (argtype == NULL_TREE)
4916 argtype = TREE_TYPE (arg);
4917 if (TREE_CODE (arg) == INTEGER_CST)
4918 ret = (require_constant_value
4919 ? fold_build1_initializer_loc (location, code, argtype, arg)
4920 : fold_build1_loc (location, code, argtype, arg));
4921 else
4922 ret = build1 (code, argtype, arg);
4923 return_build_unary_op:
4924 gcc_assert (ret != error_mark_node);
4925 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4926 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4927 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4928 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4929 ret = note_integer_operands (ret);
4930 if (eptype)
4931 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4932 protected_set_expr_location (ret, location);
4933 return ret;
4936 /* Return nonzero if REF is an lvalue valid for this language.
4937 Lvalues can be assigned, unless their type has TYPE_READONLY.
4938 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4940 bool
4941 lvalue_p (const_tree ref)
4943 const enum tree_code code = TREE_CODE (ref);
4945 switch (code)
4947 case REALPART_EXPR:
4948 case IMAGPART_EXPR:
4949 case COMPONENT_REF:
4950 return lvalue_p (TREE_OPERAND (ref, 0));
4952 case C_MAYBE_CONST_EXPR:
4953 return lvalue_p (TREE_OPERAND (ref, 1));
4955 case COMPOUND_LITERAL_EXPR:
4956 case STRING_CST:
4957 return true;
4959 case INDIRECT_REF:
4960 case ARRAY_REF:
4961 case VAR_DECL:
4962 case PARM_DECL:
4963 case RESULT_DECL:
4964 case ERROR_MARK:
4965 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4966 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4968 case BIND_EXPR:
4969 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4971 default:
4972 return false;
4976 /* Give a warning for storing in something that is read-only in GCC
4977 terms but not const in ISO C terms. */
4979 static void
4980 readonly_warning (tree arg, enum lvalue_use use)
4982 switch (use)
4984 case lv_assign:
4985 warning (0, "assignment of read-only location %qE", arg);
4986 break;
4987 case lv_increment:
4988 warning (0, "increment of read-only location %qE", arg);
4989 break;
4990 case lv_decrement:
4991 warning (0, "decrement of read-only location %qE", arg);
4992 break;
4993 default:
4994 gcc_unreachable ();
4996 return;
5000 /* Return nonzero if REF is an lvalue valid for this language;
5001 otherwise, print an error message and return zero. USE says
5002 how the lvalue is being used and so selects the error message.
5003 LOCATION is the location at which any error should be reported. */
5005 static int
5006 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5008 int win = lvalue_p (ref);
5010 if (!win)
5011 lvalue_error (loc, use);
5013 return win;
5016 /* Mark EXP saying that we need to be able to take the
5017 address of it; it should not be allocated in a register.
5018 Returns true if successful. ARRAY_REF_P is true if this
5019 is for ARRAY_REF construction - in that case we don't want
5020 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5021 it is fine to use ARRAY_REFs for vector subscripts on vector
5022 register variables. */
5024 bool
5025 c_mark_addressable (tree exp, bool array_ref_p)
5027 tree x = exp;
5029 while (1)
5030 switch (TREE_CODE (x))
5032 case VIEW_CONVERT_EXPR:
5033 if (array_ref_p
5034 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5035 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5036 return true;
5037 /* FALLTHRU */
5038 case COMPONENT_REF:
5039 case ADDR_EXPR:
5040 case ARRAY_REF:
5041 case REALPART_EXPR:
5042 case IMAGPART_EXPR:
5043 x = TREE_OPERAND (x, 0);
5044 break;
5046 case COMPOUND_LITERAL_EXPR:
5047 TREE_ADDRESSABLE (x) = 1;
5048 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5049 return true;
5051 case CONSTRUCTOR:
5052 TREE_ADDRESSABLE (x) = 1;
5053 return true;
5055 case VAR_DECL:
5056 case CONST_DECL:
5057 case PARM_DECL:
5058 case RESULT_DECL:
5059 if (C_DECL_REGISTER (x)
5060 && DECL_NONLOCAL (x))
5062 if (TREE_PUBLIC (x) || is_global_var (x))
5064 error
5065 ("global register variable %qD used in nested function", x);
5066 return false;
5068 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5070 else if (C_DECL_REGISTER (x))
5072 if (TREE_PUBLIC (x) || is_global_var (x))
5073 error ("address of global register variable %qD requested", x);
5074 else
5075 error ("address of register variable %qD requested", x);
5076 return false;
5079 /* FALLTHRU */
5080 case FUNCTION_DECL:
5081 TREE_ADDRESSABLE (x) = 1;
5082 /* FALLTHRU */
5083 default:
5084 return true;
5088 /* Convert EXPR to TYPE, warning about conversion problems with
5089 constants. SEMANTIC_TYPE is the type this conversion would use
5090 without excess precision. If SEMANTIC_TYPE is NULL, this function
5091 is equivalent to convert_and_check. This function is a wrapper that
5092 handles conversions that may be different than
5093 the usual ones because of excess precision. */
5095 static tree
5096 ep_convert_and_check (location_t loc, tree type, tree expr,
5097 tree semantic_type)
5099 if (TREE_TYPE (expr) == type)
5100 return expr;
5102 /* For C11, integer conversions may have results with excess
5103 precision. */
5104 if (flag_isoc11 || !semantic_type)
5105 return convert_and_check (loc, type, expr);
5107 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5108 && TREE_TYPE (expr) != semantic_type)
5110 /* For integers, we need to check the real conversion, not
5111 the conversion to the excess precision type. */
5112 expr = convert_and_check (loc, semantic_type, expr);
5114 /* Result type is the excess precision type, which should be
5115 large enough, so do not check. */
5116 return convert (type, expr);
5119 /* If EXPR refers to a built-in declared without a prototype returns
5120 the actual type of the built-in and, if non-null, set *BLTIN to
5121 a pointer to the built-in. Otherwise return the type of EXPR
5122 and clear *BLTIN if non-null. */
5124 static tree
5125 type_or_builtin_type (tree expr, tree *bltin = NULL)
5127 tree dummy;
5128 if (!bltin)
5129 bltin = &dummy;
5131 *bltin = NULL_TREE;
5133 tree type = TREE_TYPE (expr);
5134 if (TREE_CODE (expr) != ADDR_EXPR)
5135 return type;
5137 tree oper = TREE_OPERAND (expr, 0);
5138 if (!DECL_P (oper)
5139 || TREE_CODE (oper) != FUNCTION_DECL
5140 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5141 return type;
5143 built_in_function code = DECL_FUNCTION_CODE (oper);
5144 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5145 return type;
5147 if ((*bltin = builtin_decl_implicit (code)))
5148 type = build_pointer_type (TREE_TYPE (*bltin));
5150 return type;
5153 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5154 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5155 if folded to an integer constant then the unselected half may
5156 contain arbitrary operations not normally permitted in constant
5157 expressions. Set the location of the expression to LOC. */
5159 tree
5160 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5161 tree op1, tree op1_original_type, location_t op1_loc,
5162 tree op2, tree op2_original_type, location_t op2_loc)
5164 tree type1;
5165 tree type2;
5166 enum tree_code code1;
5167 enum tree_code code2;
5168 tree result_type = NULL;
5169 tree semantic_result_type = NULL;
5170 tree orig_op1 = op1, orig_op2 = op2;
5171 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5172 bool ifexp_int_operands;
5173 tree ret;
5175 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5176 if (op1_int_operands)
5177 op1 = remove_c_maybe_const_expr (op1);
5178 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5179 if (op2_int_operands)
5180 op2 = remove_c_maybe_const_expr (op2);
5181 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5182 if (ifexp_int_operands)
5183 ifexp = remove_c_maybe_const_expr (ifexp);
5185 /* Promote both alternatives. */
5187 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5188 op1 = default_conversion (op1);
5189 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5190 op2 = default_conversion (op2);
5192 if (TREE_CODE (ifexp) == ERROR_MARK
5193 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5194 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5195 return error_mark_node;
5197 tree bltin1 = NULL_TREE;
5198 tree bltin2 = NULL_TREE;
5199 type1 = type_or_builtin_type (op1, &bltin1);
5200 code1 = TREE_CODE (type1);
5201 type2 = type_or_builtin_type (op2, &bltin2);
5202 code2 = TREE_CODE (type2);
5204 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5205 return error_mark_node;
5207 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5208 return error_mark_node;
5210 /* C90 does not permit non-lvalue arrays in conditional expressions.
5211 In C99 they will be pointers by now. */
5212 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5214 error_at (colon_loc, "non-lvalue array in conditional expression");
5215 return error_mark_node;
5218 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5219 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5220 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5221 || code1 == COMPLEX_TYPE)
5222 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5223 || code2 == COMPLEX_TYPE))
5225 semantic_result_type = c_common_type (type1, type2);
5226 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5228 op1 = TREE_OPERAND (op1, 0);
5229 type1 = TREE_TYPE (op1);
5230 gcc_assert (TREE_CODE (type1) == code1);
5232 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5234 op2 = TREE_OPERAND (op2, 0);
5235 type2 = TREE_TYPE (op2);
5236 gcc_assert (TREE_CODE (type2) == code2);
5240 if (warn_cxx_compat)
5242 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5243 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5245 if (TREE_CODE (t1) == ENUMERAL_TYPE
5246 && TREE_CODE (t2) == ENUMERAL_TYPE
5247 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5248 warning_at (colon_loc, OPT_Wc___compat,
5249 ("different enum types in conditional is "
5250 "invalid in C++: %qT vs %qT"),
5251 t1, t2);
5254 /* Quickly detect the usual case where op1 and op2 have the same type
5255 after promotion. */
5256 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5258 if (type1 == type2)
5259 result_type = type1;
5260 else
5261 result_type = TYPE_MAIN_VARIANT (type1);
5263 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5264 || code1 == COMPLEX_TYPE)
5265 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5266 || code2 == COMPLEX_TYPE))
5268 /* In C11, a conditional expression between a floating-point
5269 type and an integer type should convert the integer type to
5270 the evaluation format of the floating-point type, with
5271 possible excess precision. */
5272 tree eptype1 = type1;
5273 tree eptype2 = type2;
5274 if (flag_isoc11)
5276 tree eptype;
5277 if (ANY_INTEGRAL_TYPE_P (type1)
5278 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5280 eptype2 = eptype;
5281 if (!semantic_result_type)
5282 semantic_result_type = c_common_type (type1, type2);
5284 else if (ANY_INTEGRAL_TYPE_P (type2)
5285 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5287 eptype1 = eptype;
5288 if (!semantic_result_type)
5289 semantic_result_type = c_common_type (type1, type2);
5292 result_type = c_common_type (eptype1, eptype2);
5293 if (result_type == error_mark_node)
5294 return error_mark_node;
5295 do_warn_double_promotion (result_type, type1, type2,
5296 "implicit conversion from %qT to %qT to "
5297 "match other result of conditional",
5298 colon_loc);
5300 /* If -Wsign-compare, warn here if type1 and type2 have
5301 different signedness. We'll promote the signed to unsigned
5302 and later code won't know it used to be different.
5303 Do this check on the original types, so that explicit casts
5304 will be considered, but default promotions won't. */
5305 if (c_inhibit_evaluation_warnings == 0)
5307 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5308 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5310 if (unsigned_op1 ^ unsigned_op2)
5312 bool ovf;
5314 /* Do not warn if the result type is signed, since the
5315 signed type will only be chosen if it can represent
5316 all the values of the unsigned type. */
5317 if (!TYPE_UNSIGNED (result_type))
5318 /* OK */;
5319 else
5321 bool op1_maybe_const = true;
5322 bool op2_maybe_const = true;
5324 /* Do not warn if the signed quantity is an
5325 unsuffixed integer literal (or some static
5326 constant expression involving such literals) and
5327 it is non-negative. This warning requires the
5328 operands to be folded for best results, so do
5329 that folding in this case even without
5330 warn_sign_compare to avoid warning options
5331 possibly affecting code generation. */
5332 c_inhibit_evaluation_warnings
5333 += (ifexp == truthvalue_false_node);
5334 op1 = c_fully_fold (op1, require_constant_value,
5335 &op1_maybe_const);
5336 c_inhibit_evaluation_warnings
5337 -= (ifexp == truthvalue_false_node);
5339 c_inhibit_evaluation_warnings
5340 += (ifexp == truthvalue_true_node);
5341 op2 = c_fully_fold (op2, require_constant_value,
5342 &op2_maybe_const);
5343 c_inhibit_evaluation_warnings
5344 -= (ifexp == truthvalue_true_node);
5346 if (warn_sign_compare)
5348 if ((unsigned_op2
5349 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5350 || (unsigned_op1
5351 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5352 /* OK */;
5353 else if (unsigned_op2)
5354 warning_at (op1_loc, OPT_Wsign_compare,
5355 "operand of %<?:%> changes signedness from "
5356 "%qT to %qT due to unsignedness of other "
5357 "operand", TREE_TYPE (orig_op1),
5358 TREE_TYPE (orig_op2));
5359 else
5360 warning_at (op2_loc, OPT_Wsign_compare,
5361 "operand of %<?:%> changes signedness from "
5362 "%qT to %qT due to unsignedness of other "
5363 "operand", TREE_TYPE (orig_op2),
5364 TREE_TYPE (orig_op1));
5366 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5367 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5368 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5369 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5374 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5376 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5377 pedwarn (colon_loc, OPT_Wpedantic,
5378 "ISO C forbids conditional expr with only one void side");
5379 result_type = void_type_node;
5381 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5383 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5384 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5385 addr_space_t as_common;
5387 if (comp_target_types (colon_loc, type1, type2))
5388 result_type = common_pointer_type (type1, type2);
5389 else if (null_pointer_constant_p (orig_op1))
5390 result_type = type2;
5391 else if (null_pointer_constant_p (orig_op2))
5392 result_type = type1;
5393 else if (!addr_space_superset (as1, as2, &as_common))
5395 error_at (colon_loc, "pointers to disjoint address spaces "
5396 "used in conditional expression");
5397 return error_mark_node;
5399 else if (VOID_TYPE_P (TREE_TYPE (type1))
5400 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5402 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5403 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5404 & ~TYPE_QUALS (TREE_TYPE (type1))))
5405 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5406 "pointer to array loses qualifier "
5407 "in conditional expression");
5409 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5410 pedwarn (colon_loc, OPT_Wpedantic,
5411 "ISO C forbids conditional expr between "
5412 "%<void *%> and function pointer");
5413 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5414 TREE_TYPE (type2)));
5416 else if (VOID_TYPE_P (TREE_TYPE (type2))
5417 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5419 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5420 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5421 & ~TYPE_QUALS (TREE_TYPE (type2))))
5422 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5423 "pointer to array loses qualifier "
5424 "in conditional expression");
5426 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5427 pedwarn (colon_loc, OPT_Wpedantic,
5428 "ISO C forbids conditional expr between "
5429 "%<void *%> and function pointer");
5430 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5431 TREE_TYPE (type1)));
5433 /* Objective-C pointer comparisons are a bit more lenient. */
5434 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5435 result_type = objc_common_type (type1, type2);
5436 else
5438 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5439 if (bltin1 && bltin2)
5440 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5441 "pointer type mismatch between %qT and %qT "
5442 "of %qD and %qD in conditional expression",
5443 type1, type2, bltin1, bltin2);
5444 else
5445 pedwarn (colon_loc, 0,
5446 "pointer type mismatch in conditional expression");
5447 result_type = build_pointer_type
5448 (build_qualified_type (void_type_node, qual));
5451 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5453 if (!null_pointer_constant_p (orig_op2))
5454 pedwarn (colon_loc, 0,
5455 "pointer/integer type mismatch in conditional expression");
5456 else
5458 op2 = null_pointer_node;
5460 result_type = type1;
5462 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5464 if (!null_pointer_constant_p (orig_op1))
5465 pedwarn (colon_loc, 0,
5466 "pointer/integer type mismatch in conditional expression");
5467 else
5469 op1 = null_pointer_node;
5471 result_type = type2;
5474 if (!result_type)
5476 if (flag_cond_mismatch)
5477 result_type = void_type_node;
5478 else
5480 error_at (colon_loc, "type mismatch in conditional expression");
5481 return error_mark_node;
5485 /* Merge const and volatile flags of the incoming types. */
5486 result_type
5487 = build_type_variant (result_type,
5488 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5489 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5491 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5492 semantic_result_type);
5493 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5494 semantic_result_type);
5496 if (ifexp_bcp && ifexp == truthvalue_true_node)
5498 op2_int_operands = true;
5499 op1 = c_fully_fold (op1, require_constant_value, NULL);
5501 if (ifexp_bcp && ifexp == truthvalue_false_node)
5503 op1_int_operands = true;
5504 op2 = c_fully_fold (op2, require_constant_value, NULL);
5506 int_const = int_operands = (ifexp_int_operands
5507 && op1_int_operands
5508 && op2_int_operands);
5509 if (int_operands)
5511 int_const = ((ifexp == truthvalue_true_node
5512 && TREE_CODE (orig_op1) == INTEGER_CST
5513 && !TREE_OVERFLOW (orig_op1))
5514 || (ifexp == truthvalue_false_node
5515 && TREE_CODE (orig_op2) == INTEGER_CST
5516 && !TREE_OVERFLOW (orig_op2)));
5519 /* Need to convert condition operand into a vector mask. */
5520 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5522 tree vectype = TREE_TYPE (ifexp);
5523 tree elem_type = TREE_TYPE (vectype);
5524 tree zero = build_int_cst (elem_type, 0);
5525 tree zero_vec = build_vector_from_val (vectype, zero);
5526 tree cmp_type = truth_type_for (vectype);
5527 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5530 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5531 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5532 else
5534 if (int_operands)
5536 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5537 nested inside of the expression. */
5538 op1 = c_fully_fold (op1, false, NULL);
5539 op2 = c_fully_fold (op2, false, NULL);
5541 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5542 if (int_operands)
5543 ret = note_integer_operands (ret);
5545 if (semantic_result_type)
5546 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5548 protected_set_expr_location (ret, colon_loc);
5550 /* If the OP1 and OP2 are the same and don't have side-effects,
5551 warn here, because the COND_EXPR will be turned into OP1. */
5552 if (warn_duplicated_branches
5553 && TREE_CODE (ret) == COND_EXPR
5554 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5555 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5556 "this condition has identical branches");
5558 return ret;
5561 /* EXPR is an expression, location LOC, whose result is discarded.
5562 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5563 whose right-hand operand is such a call, possibly recursively). */
5565 static void
5566 maybe_warn_nodiscard (location_t loc, tree expr)
5568 if (VOID_TYPE_P (TREE_TYPE (expr)))
5569 return;
5570 while (TREE_CODE (expr) == COMPOUND_EXPR)
5572 expr = TREE_OPERAND (expr, 1);
5573 if (EXPR_HAS_LOCATION (expr))
5574 loc = EXPR_LOCATION (expr);
5576 if (TREE_CODE (expr) != CALL_EXPR)
5577 return;
5578 tree fn = CALL_EXPR_FN (expr);
5579 if (!fn)
5580 return;
5581 tree attr;
5582 if (TREE_CODE (fn) == ADDR_EXPR
5583 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5584 && (attr = lookup_attribute ("nodiscard",
5585 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5587 fn = TREE_OPERAND (fn, 0);
5588 tree args = TREE_VALUE (attr);
5589 if (args)
5590 args = TREE_VALUE (args);
5591 auto_diagnostic_group d;
5592 int warned;
5593 if (args)
5594 warned = warning_at (loc, OPT_Wunused_result,
5595 "ignoring return value of %qD, declared with "
5596 "attribute %<nodiscard%>: %E", fn, args);
5597 else
5598 warned = warning_at (loc, OPT_Wunused_result,
5599 "ignoring return value of %qD, declared with "
5600 "attribute %<nodiscard%>", fn);
5601 if (warned)
5602 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5604 else
5606 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5607 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5608 if (!attr)
5609 return;
5610 tree args = TREE_VALUE (attr);
5611 if (args)
5612 args = TREE_VALUE (args);
5613 auto_diagnostic_group d;
5614 int warned;
5615 if (args)
5616 warned = warning_at (loc, OPT_Wunused_result,
5617 "ignoring return value of type %qT, declared "
5618 "with attribute %<nodiscard%>: %E",
5619 rettype, args);
5620 else
5621 warned = warning_at (loc, OPT_Wunused_result,
5622 "ignoring return value of type %qT, declared "
5623 "with attribute %<nodiscard%>", rettype);
5624 if (warned)
5626 if (TREE_CODE (fn) == ADDR_EXPR)
5628 fn = TREE_OPERAND (fn, 0);
5629 if (TREE_CODE (fn) == FUNCTION_DECL)
5630 inform (DECL_SOURCE_LOCATION (fn),
5631 "in call to %qD, declared here", fn);
5637 /* Return a compound expression that performs two expressions and
5638 returns the value of the second of them.
5640 LOC is the location of the COMPOUND_EXPR. */
5642 tree
5643 build_compound_expr (location_t loc, tree expr1, tree expr2)
5645 bool expr1_int_operands, expr2_int_operands;
5646 tree eptype = NULL_TREE;
5647 tree ret;
5649 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5650 if (expr1_int_operands)
5651 expr1 = remove_c_maybe_const_expr (expr1);
5652 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5653 if (expr2_int_operands)
5654 expr2 = remove_c_maybe_const_expr (expr2);
5656 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5657 expr1 = TREE_OPERAND (expr1, 0);
5658 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5660 eptype = TREE_TYPE (expr2);
5661 expr2 = TREE_OPERAND (expr2, 0);
5664 if (!TREE_SIDE_EFFECTS (expr1))
5666 /* The left-hand operand of a comma expression is like an expression
5667 statement: with -Wunused, we should warn if it doesn't have
5668 any side-effects, unless it was explicitly cast to (void). */
5669 if (warn_unused_value)
5671 if (VOID_TYPE_P (TREE_TYPE (expr1))
5672 && CONVERT_EXPR_P (expr1))
5673 ; /* (void) a, b */
5674 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5675 && TREE_CODE (expr1) == COMPOUND_EXPR
5676 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5677 ; /* (void) a, (void) b, c */
5678 else
5679 warning_at (loc, OPT_Wunused_value,
5680 "left-hand operand of comma expression has no effect");
5683 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5684 && warn_unused_value)
5686 tree r = expr1;
5687 location_t cloc = loc;
5688 while (TREE_CODE (r) == COMPOUND_EXPR)
5690 if (EXPR_HAS_LOCATION (r))
5691 cloc = EXPR_LOCATION (r);
5692 r = TREE_OPERAND (r, 1);
5694 if (!TREE_SIDE_EFFECTS (r)
5695 && !VOID_TYPE_P (TREE_TYPE (r))
5696 && !CONVERT_EXPR_P (r))
5697 warning_at (cloc, OPT_Wunused_value,
5698 "right-hand operand of comma expression has no effect");
5701 /* With -Wunused, we should also warn if the left-hand operand does have
5702 side-effects, but computes a value which is not used. For example, in
5703 `foo() + bar(), baz()' the result of the `+' operator is not used,
5704 so we should issue a warning. */
5705 else if (warn_unused_value)
5706 warn_if_unused_value (expr1, loc);
5708 maybe_warn_nodiscard (loc, expr1);
5710 if (expr2 == error_mark_node)
5711 return error_mark_node;
5713 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5715 if (flag_isoc99
5716 && expr1_int_operands
5717 && expr2_int_operands)
5718 ret = note_integer_operands (ret);
5720 if (eptype)
5721 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5723 protected_set_expr_location (ret, loc);
5724 return ret;
5727 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5728 which we are casting. OTYPE is the type of the expression being
5729 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5730 of the cast. -Wcast-qual appeared on the command line. Named
5731 address space qualifiers are not handled here, because they result
5732 in different warnings. */
5734 static void
5735 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5737 tree in_type = type;
5738 tree in_otype = otype;
5739 int added = 0;
5740 int discarded = 0;
5741 bool is_const;
5743 /* Check that the qualifiers on IN_TYPE are a superset of the
5744 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5745 nodes is uninteresting and we stop as soon as we hit a
5746 non-POINTER_TYPE node on either type. */
5749 in_otype = TREE_TYPE (in_otype);
5750 in_type = TREE_TYPE (in_type);
5752 /* GNU C allows cv-qualified function types. 'const' means the
5753 function is very pure, 'volatile' means it can't return. We
5754 need to warn when such qualifiers are added, not when they're
5755 taken away. */
5756 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5757 && TREE_CODE (in_type) == FUNCTION_TYPE)
5758 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5759 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5760 else
5761 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5762 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5764 while (TREE_CODE (in_type) == POINTER_TYPE
5765 && TREE_CODE (in_otype) == POINTER_TYPE);
5767 if (added)
5768 warning_at (loc, OPT_Wcast_qual,
5769 "cast adds %q#v qualifier to function type", added);
5771 if (discarded)
5772 /* There are qualifiers present in IN_OTYPE that are not present
5773 in IN_TYPE. */
5774 warning_at (loc, OPT_Wcast_qual,
5775 "cast discards %qv qualifier from pointer target type",
5776 discarded);
5778 if (added || discarded)
5779 return;
5781 /* A cast from **T to const **T is unsafe, because it can cause a
5782 const value to be changed with no additional warning. We only
5783 issue this warning if T is the same on both sides, and we only
5784 issue the warning if there are the same number of pointers on
5785 both sides, as otherwise the cast is clearly unsafe anyhow. A
5786 cast is unsafe when a qualifier is added at one level and const
5787 is not present at all outer levels.
5789 To issue this warning, we check at each level whether the cast
5790 adds new qualifiers not already seen. We don't need to special
5791 case function types, as they won't have the same
5792 TYPE_MAIN_VARIANT. */
5794 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5795 return;
5796 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5797 return;
5799 in_type = type;
5800 in_otype = otype;
5801 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5804 in_type = TREE_TYPE (in_type);
5805 in_otype = TREE_TYPE (in_otype);
5806 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5807 && !is_const)
5809 warning_at (loc, OPT_Wcast_qual,
5810 "to be safe all intermediate pointers in cast from "
5811 "%qT to %qT must be %<const%> qualified",
5812 otype, type);
5813 break;
5815 if (is_const)
5816 is_const = TYPE_READONLY (in_type);
5818 while (TREE_CODE (in_type) == POINTER_TYPE);
5821 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5823 static bool
5824 c_safe_arg_type_equiv_p (tree t1, tree t2)
5826 t1 = TYPE_MAIN_VARIANT (t1);
5827 t2 = TYPE_MAIN_VARIANT (t2);
5829 if (TREE_CODE (t1) == POINTER_TYPE
5830 && TREE_CODE (t2) == POINTER_TYPE)
5831 return true;
5833 /* The signedness of the parameter matters only when an integral
5834 type smaller than int is promoted to int, otherwise only the
5835 precision of the parameter matters.
5836 This check should make sure that the callee does not see
5837 undefined values in argument registers. */
5838 if (INTEGRAL_TYPE_P (t1)
5839 && INTEGRAL_TYPE_P (t2)
5840 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5841 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5842 || !targetm.calls.promote_prototypes (NULL_TREE)
5843 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5844 return true;
5846 return comptypes (t1, t2);
5849 /* Check if a type cast between two function types can be considered safe. */
5851 static bool
5852 c_safe_function_type_cast_p (tree t1, tree t2)
5854 if (TREE_TYPE (t1) == void_type_node &&
5855 TYPE_ARG_TYPES (t1) == void_list_node)
5856 return true;
5858 if (TREE_TYPE (t2) == void_type_node &&
5859 TYPE_ARG_TYPES (t2) == void_list_node)
5860 return true;
5862 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5863 return false;
5865 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5866 t1 && t2;
5867 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5868 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5869 return false;
5871 return true;
5874 /* Build an expression representing a cast to type TYPE of expression EXPR.
5875 LOC is the location of the cast-- typically the open paren of the cast. */
5877 tree
5878 build_c_cast (location_t loc, tree type, tree expr)
5880 tree value;
5882 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5884 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5885 expr = TREE_OPERAND (expr, 0);
5887 value = expr;
5888 if (int_operands)
5889 value = remove_c_maybe_const_expr (value);
5891 if (type == error_mark_node || expr == error_mark_node)
5892 return error_mark_node;
5894 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5895 only in <protocol> qualifications. But when constructing cast expressions,
5896 the protocols do matter and must be kept around. */
5897 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5898 return build1 (NOP_EXPR, type, expr);
5900 type = TYPE_MAIN_VARIANT (type);
5902 if (TREE_CODE (type) == ARRAY_TYPE)
5904 error_at (loc, "cast specifies array type");
5905 return error_mark_node;
5908 if (TREE_CODE (type) == FUNCTION_TYPE)
5910 error_at (loc, "cast specifies function type");
5911 return error_mark_node;
5914 if (!VOID_TYPE_P (type))
5916 value = require_complete_type (loc, value);
5917 if (value == error_mark_node)
5918 return error_mark_node;
5921 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5923 if (RECORD_OR_UNION_TYPE_P (type))
5924 pedwarn (loc, OPT_Wpedantic,
5925 "ISO C forbids casting nonscalar to the same type");
5927 /* Convert to remove any qualifiers from VALUE's type. */
5928 value = convert (type, value);
5930 else if (TREE_CODE (type) == UNION_TYPE)
5932 tree field;
5934 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5935 if (TREE_TYPE (field) != error_mark_node
5936 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5937 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5938 break;
5940 if (field)
5942 tree t;
5943 bool maybe_const = true;
5945 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5946 t = c_fully_fold (value, false, &maybe_const);
5947 t = build_constructor_single (type, field, t);
5948 if (!maybe_const)
5949 t = c_wrap_maybe_const (t, true);
5950 t = digest_init (loc, type, t,
5951 NULL_TREE, false, true, 0);
5952 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5953 return t;
5955 error_at (loc, "cast to union type from type not present in union");
5956 return error_mark_node;
5958 else
5960 tree otype, ovalue;
5962 if (type == void_type_node)
5964 tree t = build1 (CONVERT_EXPR, type, value);
5965 SET_EXPR_LOCATION (t, loc);
5966 return t;
5969 otype = TREE_TYPE (value);
5971 /* Optionally warn about potentially worrisome casts. */
5972 if (warn_cast_qual
5973 && TREE_CODE (type) == POINTER_TYPE
5974 && TREE_CODE (otype) == POINTER_TYPE)
5975 handle_warn_cast_qual (loc, type, otype);
5977 /* Warn about conversions between pointers to disjoint
5978 address spaces. */
5979 if (TREE_CODE (type) == POINTER_TYPE
5980 && TREE_CODE (otype) == POINTER_TYPE
5981 && !null_pointer_constant_p (value))
5983 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5984 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5985 addr_space_t as_common;
5987 if (!addr_space_superset (as_to, as_from, &as_common))
5989 if (ADDR_SPACE_GENERIC_P (as_from))
5990 warning_at (loc, 0, "cast to %s address space pointer "
5991 "from disjoint generic address space pointer",
5992 c_addr_space_name (as_to));
5994 else if (ADDR_SPACE_GENERIC_P (as_to))
5995 warning_at (loc, 0, "cast to generic address space pointer "
5996 "from disjoint %s address space pointer",
5997 c_addr_space_name (as_from));
5999 else
6000 warning_at (loc, 0, "cast to %s address space pointer "
6001 "from disjoint %s address space pointer",
6002 c_addr_space_name (as_to),
6003 c_addr_space_name (as_from));
6007 /* Warn about possible alignment problems. */
6008 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6009 && TREE_CODE (type) == POINTER_TYPE
6010 && TREE_CODE (otype) == POINTER_TYPE
6011 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6012 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6013 /* Don't warn about opaque types, where the actual alignment
6014 restriction is unknown. */
6015 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6016 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6017 && min_align_of_type (TREE_TYPE (type))
6018 > min_align_of_type (TREE_TYPE (otype)))
6019 warning_at (loc, OPT_Wcast_align,
6020 "cast increases required alignment of target type");
6022 if (TREE_CODE (type) == INTEGER_TYPE
6023 && TREE_CODE (otype) == POINTER_TYPE
6024 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6025 /* Unlike conversion of integers to pointers, where the
6026 warning is disabled for converting constants because
6027 of cases such as SIG_*, warn about converting constant
6028 pointers to integers. In some cases it may cause unwanted
6029 sign extension, and a warning is appropriate. */
6030 warning_at (loc, OPT_Wpointer_to_int_cast,
6031 "cast from pointer to integer of different size");
6033 if (TREE_CODE (value) == CALL_EXPR
6034 && TREE_CODE (type) != TREE_CODE (otype))
6035 warning_at (loc, OPT_Wbad_function_cast,
6036 "cast from function call of type %qT "
6037 "to non-matching type %qT", otype, type);
6039 if (TREE_CODE (type) == POINTER_TYPE
6040 && TREE_CODE (otype) == INTEGER_TYPE
6041 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6042 /* Don't warn about converting any constant. */
6043 && !TREE_CONSTANT (value))
6044 warning_at (loc,
6045 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6046 "of different size");
6048 if (warn_strict_aliasing <= 2)
6049 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6051 /* If pedantic, warn for conversions between function and object
6052 pointer types, except for converting a null pointer constant
6053 to function pointer type. */
6054 if (pedantic
6055 && TREE_CODE (type) == POINTER_TYPE
6056 && TREE_CODE (otype) == POINTER_TYPE
6057 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6058 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6059 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6060 "conversion of function pointer to object pointer type");
6062 if (pedantic
6063 && TREE_CODE (type) == POINTER_TYPE
6064 && TREE_CODE (otype) == POINTER_TYPE
6065 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6066 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6067 && !null_pointer_constant_p (value))
6068 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6069 "conversion of object pointer to function pointer type");
6071 if (TREE_CODE (type) == POINTER_TYPE
6072 && TREE_CODE (otype) == POINTER_TYPE
6073 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6074 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6075 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6076 TREE_TYPE (otype)))
6077 warning_at (loc, OPT_Wcast_function_type,
6078 "cast between incompatible function types"
6079 " from %qT to %qT", otype, type);
6081 ovalue = value;
6082 value = convert (type, value);
6084 /* Ignore any integer overflow caused by the cast. */
6085 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6087 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6089 if (!TREE_OVERFLOW (value))
6091 /* Avoid clobbering a shared constant. */
6092 value = copy_node (value);
6093 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6096 else if (TREE_OVERFLOW (value))
6097 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6098 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6102 /* Don't let a cast be an lvalue. */
6103 if (lvalue_p (value))
6104 value = non_lvalue_loc (loc, value);
6106 /* Don't allow the results of casting to floating-point or complex
6107 types be confused with actual constants, or casts involving
6108 integer and pointer types other than direct integer-to-integer
6109 and integer-to-pointer be confused with integer constant
6110 expressions and null pointer constants. */
6111 if (TREE_CODE (value) == REAL_CST
6112 || TREE_CODE (value) == COMPLEX_CST
6113 || (TREE_CODE (value) == INTEGER_CST
6114 && !((TREE_CODE (expr) == INTEGER_CST
6115 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6116 || TREE_CODE (expr) == REAL_CST
6117 || TREE_CODE (expr) == COMPLEX_CST)))
6118 value = build1 (NOP_EXPR, type, value);
6120 /* If the expression has integer operands and so can occur in an
6121 unevaluated part of an integer constant expression, ensure the
6122 return value reflects this. */
6123 if (int_operands
6124 && INTEGRAL_TYPE_P (type)
6125 && !EXPR_INT_CONST_OPERANDS (value))
6126 value = note_integer_operands (value);
6128 protected_set_expr_location (value, loc);
6129 return value;
6132 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6133 location of the open paren of the cast, or the position of the cast
6134 expr. */
6135 tree
6136 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6138 tree type;
6139 tree type_expr = NULL_TREE;
6140 bool type_expr_const = true;
6141 tree ret;
6142 int saved_wsp = warn_strict_prototypes;
6144 /* This avoids warnings about unprototyped casts on
6145 integers. E.g. "#define SIG_DFL (void(*)())0". */
6146 if (TREE_CODE (expr) == INTEGER_CST)
6147 warn_strict_prototypes = 0;
6148 type = groktypename (type_name, &type_expr, &type_expr_const);
6149 warn_strict_prototypes = saved_wsp;
6151 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6152 && reject_gcc_builtin (expr))
6153 return error_mark_node;
6155 ret = build_c_cast (loc, type, expr);
6156 if (type_expr)
6158 bool inner_expr_const = true;
6159 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6160 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6161 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6162 && inner_expr_const);
6163 SET_EXPR_LOCATION (ret, loc);
6166 if (!EXPR_HAS_LOCATION (ret))
6167 protected_set_expr_location (ret, loc);
6169 /* C++ does not permits types to be defined in a cast, but it
6170 allows references to incomplete types. */
6171 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6172 warning_at (loc, OPT_Wc___compat,
6173 "defining a type in a cast is invalid in C++");
6175 return ret;
6178 /* Build an assignment expression of lvalue LHS from value RHS.
6179 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6180 may differ from TREE_TYPE (LHS) for an enum bitfield.
6181 MODIFYCODE is the code for a binary operator that we use
6182 to combine the old value of LHS with RHS to get the new value.
6183 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6184 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6185 which may differ from TREE_TYPE (RHS) for an enum value.
6187 LOCATION is the location of the MODIFYCODE operator.
6188 RHS_LOC is the location of the RHS. */
6190 tree
6191 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6192 enum tree_code modifycode,
6193 location_t rhs_loc, tree rhs, tree rhs_origtype)
6195 tree result;
6196 tree newrhs;
6197 tree rhseval = NULL_TREE;
6198 tree lhstype = TREE_TYPE (lhs);
6199 tree olhstype = lhstype;
6200 bool npc;
6201 bool is_atomic_op;
6203 /* Types that aren't fully specified cannot be used in assignments. */
6204 lhs = require_complete_type (location, lhs);
6206 /* Avoid duplicate error messages from operands that had errors. */
6207 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6208 return error_mark_node;
6210 /* Ensure an error for assigning a non-lvalue array to an array in
6211 C90. */
6212 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6214 error_at (location, "assignment to expression with array type");
6215 return error_mark_node;
6218 /* For ObjC properties, defer this check. */
6219 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6220 return error_mark_node;
6222 is_atomic_op = really_atomic_lvalue (lhs);
6224 newrhs = rhs;
6226 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6228 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6229 lhs_origtype, modifycode, rhs_loc, rhs,
6230 rhs_origtype);
6231 if (inner == error_mark_node)
6232 return error_mark_node;
6233 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6234 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6235 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6236 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6237 protected_set_expr_location (result, location);
6238 return result;
6241 /* If a binary op has been requested, combine the old LHS value with the RHS
6242 producing the value we should actually store into the LHS. */
6244 if (modifycode != NOP_EXPR)
6246 lhs = c_fully_fold (lhs, false, NULL, true);
6247 lhs = stabilize_reference (lhs);
6249 /* Construct the RHS for any non-atomic compound assignemnt. */
6250 if (!is_atomic_op)
6252 /* If in LHS op= RHS the RHS has side-effects, ensure they
6253 are preevaluated before the rest of the assignment expression's
6254 side-effects, because RHS could contain e.g. function calls
6255 that modify LHS. */
6256 if (TREE_SIDE_EFFECTS (rhs))
6258 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6259 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6260 else
6261 newrhs = save_expr (rhs);
6262 rhseval = newrhs;
6263 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6264 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6265 newrhs);
6267 newrhs = build_binary_op (location,
6268 modifycode, lhs, newrhs, true);
6270 /* The original type of the right hand side is no longer
6271 meaningful. */
6272 rhs_origtype = NULL_TREE;
6276 if (c_dialect_objc ())
6278 /* Check if we are modifying an Objective-C property reference;
6279 if so, we need to generate setter calls. */
6280 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6281 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6282 else
6283 result = objc_maybe_build_modify_expr (lhs, newrhs);
6284 if (result)
6285 goto return_result;
6287 /* Else, do the check that we postponed for Objective-C. */
6288 if (!lvalue_or_else (location, lhs, lv_assign))
6289 return error_mark_node;
6292 /* Give an error for storing in something that is 'const'. */
6294 if (TYPE_READONLY (lhstype)
6295 || (RECORD_OR_UNION_TYPE_P (lhstype)
6296 && C_TYPE_FIELDS_READONLY (lhstype)))
6298 readonly_error (location, lhs, lv_assign);
6299 return error_mark_node;
6301 else if (TREE_READONLY (lhs))
6302 readonly_warning (lhs, lv_assign);
6304 /* If storing into a structure or union member,
6305 it has probably been given type `int'.
6306 Compute the type that would go with
6307 the actual amount of storage the member occupies. */
6309 if (TREE_CODE (lhs) == COMPONENT_REF
6310 && (TREE_CODE (lhstype) == INTEGER_TYPE
6311 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6312 || TREE_CODE (lhstype) == REAL_TYPE
6313 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6314 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6316 /* If storing in a field that is in actuality a short or narrower than one,
6317 we must store in the field in its actual type. */
6319 if (lhstype != TREE_TYPE (lhs))
6321 lhs = copy_node (lhs);
6322 TREE_TYPE (lhs) = lhstype;
6325 /* Issue -Wc++-compat warnings about an assignment to an enum type
6326 when LHS does not have its original type. This happens for,
6327 e.g., an enum bitfield in a struct. */
6328 if (warn_cxx_compat
6329 && lhs_origtype != NULL_TREE
6330 && lhs_origtype != lhstype
6331 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6333 tree checktype = (rhs_origtype != NULL_TREE
6334 ? rhs_origtype
6335 : TREE_TYPE (rhs));
6336 if (checktype != error_mark_node
6337 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6338 || (is_atomic_op && modifycode != NOP_EXPR)))
6339 warning_at (location, OPT_Wc___compat,
6340 "enum conversion in assignment is invalid in C++");
6343 /* Remove qualifiers. */
6344 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6345 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6347 /* Convert new value to destination type. Fold it first, then
6348 restore any excess precision information, for the sake of
6349 conversion warnings. */
6351 if (!(is_atomic_op && modifycode != NOP_EXPR))
6353 tree rhs_semantic_type = NULL_TREE;
6354 if (!c_in_omp_for)
6356 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6358 rhs_semantic_type = TREE_TYPE (newrhs);
6359 newrhs = TREE_OPERAND (newrhs, 0);
6361 npc = null_pointer_constant_p (newrhs);
6362 newrhs = c_fully_fold (newrhs, false, NULL);
6363 if (rhs_semantic_type)
6364 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6366 else
6367 npc = null_pointer_constant_p (newrhs);
6368 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6369 rhs_origtype, ic_assign, npc,
6370 NULL_TREE, NULL_TREE, 0);
6371 if (TREE_CODE (newrhs) == ERROR_MARK)
6372 return error_mark_node;
6375 /* Emit ObjC write barrier, if necessary. */
6376 if (c_dialect_objc () && flag_objc_gc)
6378 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6379 if (result)
6381 protected_set_expr_location (result, location);
6382 goto return_result;
6386 /* Scan operands. */
6388 if (is_atomic_op)
6389 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6390 else
6392 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6393 TREE_SIDE_EFFECTS (result) = 1;
6394 protected_set_expr_location (result, location);
6397 /* If we got the LHS in a different type for storing in,
6398 convert the result back to the nominal type of LHS
6399 so that the value we return always has the same type
6400 as the LHS argument. */
6402 if (olhstype == TREE_TYPE (result))
6403 goto return_result;
6405 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6406 rhs_origtype, ic_assign, false, NULL_TREE,
6407 NULL_TREE, 0);
6408 protected_set_expr_location (result, location);
6410 return_result:
6411 if (rhseval)
6412 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6413 return result;
6416 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6417 This is used to implement -fplan9-extensions. */
6419 static bool
6420 find_anonymous_field_with_type (tree struct_type, tree type)
6422 tree field;
6423 bool found;
6425 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6426 found = false;
6427 for (field = TYPE_FIELDS (struct_type);
6428 field != NULL_TREE;
6429 field = TREE_CHAIN (field))
6431 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6432 ? c_build_qualified_type (TREE_TYPE (field),
6433 TYPE_QUAL_ATOMIC)
6434 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6435 if (DECL_NAME (field) == NULL
6436 && comptypes (type, fieldtype))
6438 if (found)
6439 return false;
6440 found = true;
6442 else if (DECL_NAME (field) == NULL
6443 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6444 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6446 if (found)
6447 return false;
6448 found = true;
6451 return found;
6454 /* RHS is an expression whose type is pointer to struct. If there is
6455 an anonymous field in RHS with type TYPE, then return a pointer to
6456 that field in RHS. This is used with -fplan9-extensions. This
6457 returns NULL if no conversion could be found. */
6459 static tree
6460 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6462 tree rhs_struct_type, lhs_main_type;
6463 tree field, found_field;
6464 bool found_sub_field;
6465 tree ret;
6467 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6468 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6469 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6471 gcc_assert (POINTER_TYPE_P (type));
6472 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6473 ? c_build_qualified_type (TREE_TYPE (type),
6474 TYPE_QUAL_ATOMIC)
6475 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6477 found_field = NULL_TREE;
6478 found_sub_field = false;
6479 for (field = TYPE_FIELDS (rhs_struct_type);
6480 field != NULL_TREE;
6481 field = TREE_CHAIN (field))
6483 if (DECL_NAME (field) != NULL_TREE
6484 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6485 continue;
6486 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6487 ? c_build_qualified_type (TREE_TYPE (field),
6488 TYPE_QUAL_ATOMIC)
6489 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6490 if (comptypes (lhs_main_type, fieldtype))
6492 if (found_field != NULL_TREE)
6493 return NULL_TREE;
6494 found_field = field;
6496 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6497 lhs_main_type))
6499 if (found_field != NULL_TREE)
6500 return NULL_TREE;
6501 found_field = field;
6502 found_sub_field = true;
6506 if (found_field == NULL_TREE)
6507 return NULL_TREE;
6509 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6510 build_fold_indirect_ref (rhs), found_field,
6511 NULL_TREE);
6512 ret = build_fold_addr_expr_loc (location, ret);
6514 if (found_sub_field)
6516 ret = convert_to_anonymous_field (location, type, ret);
6517 gcc_assert (ret != NULL_TREE);
6520 return ret;
6523 /* Issue an error message for a bad initializer component.
6524 GMSGID identifies the message.
6525 The component name is taken from the spelling stack. */
6527 static void ATTRIBUTE_GCC_DIAG (2,0)
6528 error_init (location_t loc, const char *gmsgid, ...)
6530 char *ofwhat;
6532 auto_diagnostic_group d;
6534 /* The gmsgid may be a format string with %< and %>. */
6535 va_list ap;
6536 va_start (ap, gmsgid);
6537 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6538 va_end (ap);
6540 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6541 if (*ofwhat && warned)
6542 inform (loc, "(near initialization for %qs)", ofwhat);
6545 /* Issue a pedantic warning for a bad initializer component. OPT is
6546 the option OPT_* (from options.h) controlling this warning or 0 if
6547 it is unconditionally given. GMSGID identifies the message. The
6548 component name is taken from the spelling stack. */
6550 static void ATTRIBUTE_GCC_DIAG (3,0)
6551 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6553 /* Use the location where a macro was expanded rather than where
6554 it was defined to make sure macros defined in system headers
6555 but used incorrectly elsewhere are diagnosed. */
6556 location_t exploc = expansion_point_location_if_in_system_header (loc);
6557 auto_diagnostic_group d;
6558 va_list ap;
6559 va_start (ap, gmsgid);
6560 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6561 va_end (ap);
6562 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6563 if (*ofwhat && warned)
6564 inform (exploc, "(near initialization for %qs)", ofwhat);
6567 /* Issue a warning for a bad initializer component.
6569 OPT is the OPT_W* value corresponding to the warning option that
6570 controls this warning. GMSGID identifies the message. The
6571 component name is taken from the spelling stack. */
6573 static void
6574 warning_init (location_t loc, int opt, const char *gmsgid)
6576 char *ofwhat;
6577 bool warned;
6579 auto_diagnostic_group d;
6581 /* Use the location where a macro was expanded rather than where
6582 it was defined to make sure macros defined in system headers
6583 but used incorrectly elsewhere are diagnosed. */
6584 location_t exploc = expansion_point_location_if_in_system_header (loc);
6586 /* The gmsgid may be a format string with %< and %>. */
6587 warned = warning_at (exploc, opt, gmsgid);
6588 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6589 if (*ofwhat && warned)
6590 inform (exploc, "(near initialization for %qs)", ofwhat);
6593 /* If TYPE is an array type and EXPR is a parenthesized string
6594 constant, warn if pedantic that EXPR is being used to initialize an
6595 object of type TYPE. */
6597 void
6598 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6600 if (pedantic
6601 && TREE_CODE (type) == ARRAY_TYPE
6602 && TREE_CODE (expr.value) == STRING_CST
6603 && expr.original_code != STRING_CST)
6604 pedwarn_init (loc, OPT_Wpedantic,
6605 "array initialized from parenthesized string constant");
6608 /* Attempt to locate the parameter with the given index within FNDECL,
6609 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6611 static location_t
6612 get_fndecl_argument_location (tree fndecl, int argnum)
6614 int i;
6615 tree param;
6617 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6618 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6619 i < argnum && param;
6620 i++, param = TREE_CHAIN (param))
6623 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6624 return DECL_SOURCE_LOCATION (FNDECL). */
6625 if (param == NULL)
6626 return DECL_SOURCE_LOCATION (fndecl);
6628 return DECL_SOURCE_LOCATION (param);
6631 /* Issue a note about a mismatching argument for parameter PARMNUM
6632 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6633 Attempt to issue the note at the pertinent parameter of the decl;
6634 failing that issue it at the location of FUNDECL; failing that
6635 issue it at PLOC. */
6637 static void
6638 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6639 tree expected_type, tree actual_type)
6641 location_t loc;
6642 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6643 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6644 else
6645 loc = ploc;
6647 inform (loc,
6648 "expected %qT but argument is of type %qT",
6649 expected_type, actual_type);
6652 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6653 function FUNDECL declared without prototype to parameter PARMNUM of
6654 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6656 static void
6657 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6658 tree parmtype, tree argtype)
6660 tree_code parmcode = TREE_CODE (parmtype);
6661 tree_code argcode = TREE_CODE (argtype);
6662 tree promoted = c_type_promotes_to (argtype);
6664 /* Avoid warning for enum arguments that promote to an integer type
6665 of the same size/mode. */
6666 if (parmcode == INTEGER_TYPE
6667 && argcode == ENUMERAL_TYPE
6668 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6669 return;
6671 if ((parmcode == argcode
6672 || (parmcode == INTEGER_TYPE
6673 && argcode == ENUMERAL_TYPE))
6674 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6675 return;
6677 /* This diagnoses even signed/unsigned mismatches. Those might be
6678 safe in many cases but GCC may emit suboptimal code for them so
6679 warning on those cases drives efficiency improvements. */
6680 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6681 TYPE_MAIN_VARIANT (promoted) == argtype
6682 ? G_("%qD argument %d type is %qT where %qT is expected "
6683 "in a call to built-in function declared without "
6684 "prototype")
6685 : G_("%qD argument %d promotes to %qT where %qT is expected "
6686 "in a call to built-in function declared without "
6687 "prototype"),
6688 fundecl, parmnum, promoted, parmtype))
6689 inform (DECL_SOURCE_LOCATION (fundecl),
6690 "built-in %qD declared here",
6691 fundecl);
6694 /* Convert value RHS to type TYPE as preparation for an assignment to
6695 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6696 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6697 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6698 constant before any folding.
6699 The real work of conversion is done by `convert'.
6700 The purpose of this function is to generate error messages
6701 for assignments that are not allowed in C.
6702 ERRTYPE says whether it is argument passing, assignment,
6703 initialization or return.
6705 In the following example, '~' denotes where EXPR_LOC and '^' where
6706 LOCATION point to:
6708 f (var); [ic_argpass]
6709 ^ ~~~
6710 x = var; [ic_assign]
6711 ^ ~~~;
6712 int x = var; [ic_init]
6714 return x; [ic_return]
6717 FUNCTION is a tree for the function being called.
6718 PARMNUM is the number of the argument, for printing in error messages.
6719 WARNOPT may be set to a warning option to issue the corresponding warning
6720 rather than an error for invalid conversions. Used for calls to built-in
6721 functions declared without a prototype. */
6723 static tree
6724 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6725 tree rhs, tree origtype, enum impl_conv errtype,
6726 bool null_pointer_constant, tree fundecl,
6727 tree function, int parmnum, int warnopt /* = 0 */)
6729 enum tree_code codel = TREE_CODE (type);
6730 tree orig_rhs = rhs;
6731 tree rhstype;
6732 enum tree_code coder;
6733 tree rname = NULL_TREE;
6734 bool objc_ok = false;
6736 /* Use the expansion point location to handle cases such as user's
6737 function returning a wrong-type macro defined in a system header. */
6738 location = expansion_point_location_if_in_system_header (location);
6740 if (errtype == ic_argpass)
6742 tree selector;
6743 /* Change pointer to function to the function itself for
6744 diagnostics. */
6745 if (TREE_CODE (function) == ADDR_EXPR
6746 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6747 function = TREE_OPERAND (function, 0);
6749 /* Handle an ObjC selector specially for diagnostics. */
6750 selector = objc_message_selector ();
6751 rname = function;
6752 if (selector && parmnum > 2)
6754 rname = selector;
6755 parmnum -= 2;
6759 /* This macro is used to emit diagnostics to ensure that all format
6760 strings are complete sentences, visible to gettext and checked at
6761 compile time. */
6762 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6763 do { \
6764 switch (errtype) \
6766 case ic_argpass: \
6768 auto_diagnostic_group d; \
6769 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6770 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6772 break; \
6773 case ic_assign: \
6774 pedwarn (LOCATION, OPT, AS); \
6775 break; \
6776 case ic_init: \
6777 pedwarn_init (LOCATION, OPT, IN); \
6778 break; \
6779 case ic_return: \
6780 pedwarn (LOCATION, OPT, RE); \
6781 break; \
6782 default: \
6783 gcc_unreachable (); \
6785 } while (0)
6787 /* This macro is used to emit diagnostics to ensure that all format
6788 strings are complete sentences, visible to gettext and checked at
6789 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6790 extra parameter to enumerate qualifiers. */
6791 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6792 do { \
6793 switch (errtype) \
6795 case ic_argpass: \
6797 auto_diagnostic_group d; \
6798 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6799 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6801 break; \
6802 case ic_assign: \
6803 pedwarn (LOCATION, OPT, AS, QUALS); \
6804 break; \
6805 case ic_init: \
6806 pedwarn (LOCATION, OPT, IN, QUALS); \
6807 break; \
6808 case ic_return: \
6809 pedwarn (LOCATION, OPT, RE, QUALS); \
6810 break; \
6811 default: \
6812 gcc_unreachable (); \
6814 } while (0)
6816 /* This macro is used to emit diagnostics to ensure that all format
6817 strings are complete sentences, visible to gettext and checked at
6818 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6819 warning_at instead of pedwarn. */
6820 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6821 do { \
6822 switch (errtype) \
6824 case ic_argpass: \
6826 auto_diagnostic_group d; \
6827 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6828 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6830 break; \
6831 case ic_assign: \
6832 warning_at (LOCATION, OPT, AS, QUALS); \
6833 break; \
6834 case ic_init: \
6835 warning_at (LOCATION, OPT, IN, QUALS); \
6836 break; \
6837 case ic_return: \
6838 warning_at (LOCATION, OPT, RE, QUALS); \
6839 break; \
6840 default: \
6841 gcc_unreachable (); \
6843 } while (0)
6845 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6846 rhs = TREE_OPERAND (rhs, 0);
6848 rhstype = TREE_TYPE (rhs);
6849 coder = TREE_CODE (rhstype);
6851 if (coder == ERROR_MARK)
6852 return error_mark_node;
6854 if (c_dialect_objc ())
6856 int parmno;
6858 switch (errtype)
6860 case ic_return:
6861 parmno = 0;
6862 break;
6864 case ic_assign:
6865 parmno = -1;
6866 break;
6868 case ic_init:
6869 parmno = -2;
6870 break;
6872 default:
6873 parmno = parmnum;
6874 break;
6877 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6880 if (warn_cxx_compat)
6882 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6883 if (checktype != error_mark_node
6884 && TREE_CODE (type) == ENUMERAL_TYPE
6885 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6886 switch (errtype)
6888 case ic_argpass:
6889 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6890 "passing argument %d of %qE is invalid in C++",
6891 parmnum, rname))
6892 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6893 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6894 "expected %qT but argument is of type %qT",
6895 type, rhstype);
6896 break;
6897 case ic_assign:
6898 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6899 "%qT in assignment is invalid in C++", rhstype, type);
6900 break;
6901 case ic_init:
6902 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6903 "%qT to %qT in initialization is invalid in C++",
6904 rhstype, type);
6905 break;
6906 case ic_return:
6907 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6908 "%qT in return is invalid in C++", rhstype, type);
6909 break;
6910 default:
6911 gcc_unreachable ();
6915 if (warn_enum_conversion)
6917 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6918 if (checktype != error_mark_node
6919 && TREE_CODE (checktype) == ENUMERAL_TYPE
6920 && TREE_CODE (type) == ENUMERAL_TYPE
6921 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6923 gcc_rich_location loc (location);
6924 warning_at (&loc, OPT_Wenum_conversion,
6925 "implicit conversion from %qT to %qT",
6926 checktype, type);
6930 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6932 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6933 return rhs;
6936 if (coder == VOID_TYPE)
6938 /* Except for passing an argument to an unprototyped function,
6939 this is a constraint violation. When passing an argument to
6940 an unprototyped function, it is compile-time undefined;
6941 making it a constraint in that case was rejected in
6942 DR#252. */
6943 const char msg[] = "void value not ignored as it ought to be";
6944 if (warnopt)
6945 warning_at (location, warnopt, msg);
6946 else
6947 error_at (location, msg);
6948 return error_mark_node;
6950 rhs = require_complete_type (location, rhs);
6951 if (rhs == error_mark_node)
6952 return error_mark_node;
6954 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6955 return error_mark_node;
6957 /* A non-reference type can convert to a reference. This handles
6958 va_start, va_copy and possibly port built-ins. */
6959 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6961 if (!lvalue_p (rhs))
6963 const char msg[] = "cannot pass rvalue to reference parameter";
6964 if (warnopt)
6965 warning_at (location, warnopt, msg);
6966 else
6967 error_at (location, msg);
6968 return error_mark_node;
6970 if (!c_mark_addressable (rhs))
6971 return error_mark_node;
6972 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6973 SET_EXPR_LOCATION (rhs, location);
6975 rhs = convert_for_assignment (location, expr_loc,
6976 build_pointer_type (TREE_TYPE (type)),
6977 rhs, origtype, errtype,
6978 null_pointer_constant, fundecl, function,
6979 parmnum, warnopt);
6980 if (rhs == error_mark_node)
6981 return error_mark_node;
6983 rhs = build1 (NOP_EXPR, type, rhs);
6984 SET_EXPR_LOCATION (rhs, location);
6985 return rhs;
6987 /* Some types can interconvert without explicit casts. */
6988 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6989 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6990 return convert (type, rhs);
6991 /* Arithmetic types all interconvert, and enum is treated like int. */
6992 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6993 || codel == FIXED_POINT_TYPE
6994 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6995 || codel == BOOLEAN_TYPE)
6996 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6997 || coder == FIXED_POINT_TYPE
6998 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6999 || coder == BOOLEAN_TYPE))
7001 if (warnopt && errtype == ic_argpass)
7002 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7003 rhstype);
7005 bool save = in_late_binary_op;
7006 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
7007 || (coder == REAL_TYPE
7008 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7009 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7010 in_late_binary_op = true;
7011 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7012 ? expr_loc : location, type, orig_rhs);
7013 in_late_binary_op = save;
7014 return ret;
7017 /* Aggregates in different TUs might need conversion. */
7018 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7019 && codel == coder
7020 && comptypes (type, rhstype))
7021 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7022 ? expr_loc : location, type, rhs);
7024 /* Conversion to a transparent union or record from its member types.
7025 This applies only to function arguments. */
7026 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7027 && TYPE_TRANSPARENT_AGGR (type))
7028 && errtype == ic_argpass)
7030 tree memb, marginal_memb = NULL_TREE;
7032 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7034 tree memb_type = TREE_TYPE (memb);
7036 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7037 TYPE_MAIN_VARIANT (rhstype)))
7038 break;
7040 if (TREE_CODE (memb_type) != POINTER_TYPE)
7041 continue;
7043 if (coder == POINTER_TYPE)
7045 tree ttl = TREE_TYPE (memb_type);
7046 tree ttr = TREE_TYPE (rhstype);
7048 /* Any non-function converts to a [const][volatile] void *
7049 and vice versa; otherwise, targets must be the same.
7050 Meanwhile, the lhs target must have all the qualifiers of
7051 the rhs. */
7052 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7053 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7054 || comp_target_types (location, memb_type, rhstype))
7056 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7057 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7058 /* If this type won't generate any warnings, use it. */
7059 if (lquals == rquals
7060 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7061 && TREE_CODE (ttl) == FUNCTION_TYPE)
7062 ? ((lquals | rquals) == rquals)
7063 : ((lquals | rquals) == lquals)))
7064 break;
7066 /* Keep looking for a better type, but remember this one. */
7067 if (!marginal_memb)
7068 marginal_memb = memb;
7072 /* Can convert integer zero to any pointer type. */
7073 if (null_pointer_constant)
7075 rhs = null_pointer_node;
7076 break;
7080 if (memb || marginal_memb)
7082 if (!memb)
7084 /* We have only a marginally acceptable member type;
7085 it needs a warning. */
7086 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7087 tree ttr = TREE_TYPE (rhstype);
7089 /* Const and volatile mean something different for function
7090 types, so the usual warnings are not appropriate. */
7091 if (TREE_CODE (ttr) == FUNCTION_TYPE
7092 && TREE_CODE (ttl) == FUNCTION_TYPE)
7094 /* Because const and volatile on functions are
7095 restrictions that say the function will not do
7096 certain things, it is okay to use a const or volatile
7097 function where an ordinary one is wanted, but not
7098 vice-versa. */
7099 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7100 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7101 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7102 OPT_Wdiscarded_qualifiers,
7103 G_("passing argument %d of %qE "
7104 "makes %q#v qualified function "
7105 "pointer from unqualified"),
7106 G_("assignment makes %q#v qualified "
7107 "function pointer from "
7108 "unqualified"),
7109 G_("initialization makes %q#v qualified "
7110 "function pointer from "
7111 "unqualified"),
7112 G_("return makes %q#v qualified function "
7113 "pointer from unqualified"),
7114 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7116 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7117 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7118 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7119 OPT_Wdiscarded_qualifiers,
7120 G_("passing argument %d of %qE discards "
7121 "%qv qualifier from pointer target type"),
7122 G_("assignment discards %qv qualifier "
7123 "from pointer target type"),
7124 G_("initialization discards %qv qualifier "
7125 "from pointer target type"),
7126 G_("return discards %qv qualifier from "
7127 "pointer target type"),
7128 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7130 memb = marginal_memb;
7133 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7134 pedwarn (location, OPT_Wpedantic,
7135 "ISO C prohibits argument conversion to union type");
7137 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7138 return build_constructor_single (type, memb, rhs);
7142 /* Conversions among pointers */
7143 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7144 && (coder == codel))
7146 /* If RHS refers to a built-in declared without a prototype
7147 BLTIN is the declaration of the built-in with a prototype
7148 and RHSTYPE is set to the actual type of the built-in. */
7149 tree bltin;
7150 rhstype = type_or_builtin_type (rhs, &bltin);
7152 tree ttl = TREE_TYPE (type);
7153 tree ttr = TREE_TYPE (rhstype);
7154 tree mvl = ttl;
7155 tree mvr = ttr;
7156 bool is_opaque_pointer;
7157 int target_cmp = 0; /* Cache comp_target_types () result. */
7158 addr_space_t asl;
7159 addr_space_t asr;
7161 if (TREE_CODE (mvl) != ARRAY_TYPE)
7162 mvl = (TYPE_ATOMIC (mvl)
7163 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7164 TYPE_QUAL_ATOMIC)
7165 : TYPE_MAIN_VARIANT (mvl));
7166 if (TREE_CODE (mvr) != ARRAY_TYPE)
7167 mvr = (TYPE_ATOMIC (mvr)
7168 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7169 TYPE_QUAL_ATOMIC)
7170 : TYPE_MAIN_VARIANT (mvr));
7171 /* Opaque pointers are treated like void pointers. */
7172 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7174 /* The Plan 9 compiler permits a pointer to a struct to be
7175 automatically converted into a pointer to an anonymous field
7176 within the struct. */
7177 if (flag_plan9_extensions
7178 && RECORD_OR_UNION_TYPE_P (mvl)
7179 && RECORD_OR_UNION_TYPE_P (mvr)
7180 && mvl != mvr)
7182 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7183 if (new_rhs != NULL_TREE)
7185 rhs = new_rhs;
7186 rhstype = TREE_TYPE (rhs);
7187 coder = TREE_CODE (rhstype);
7188 ttr = TREE_TYPE (rhstype);
7189 mvr = TYPE_MAIN_VARIANT (ttr);
7193 /* C++ does not allow the implicit conversion void* -> T*. However,
7194 for the purpose of reducing the number of false positives, we
7195 tolerate the special case of
7197 int *p = NULL;
7199 where NULL is typically defined in C to be '(void *) 0'. */
7200 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7201 warning_at (errtype == ic_argpass ? expr_loc : location,
7202 OPT_Wc___compat,
7203 "request for implicit conversion "
7204 "from %qT to %qT not permitted in C++", rhstype, type);
7206 /* See if the pointers point to incompatible address spaces. */
7207 asl = TYPE_ADDR_SPACE (ttl);
7208 asr = TYPE_ADDR_SPACE (ttr);
7209 if (!null_pointer_constant_p (rhs)
7210 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7212 switch (errtype)
7214 case ic_argpass:
7216 const char msg[] = G_("passing argument %d of %qE from "
7217 "pointer to non-enclosed address space");
7218 if (warnopt)
7219 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7220 else
7221 error_at (expr_loc, msg, parmnum, rname);
7222 break;
7224 case ic_assign:
7226 const char msg[] = G_("assignment from pointer to "
7227 "non-enclosed address space");
7228 if (warnopt)
7229 warning_at (location, warnopt, msg);
7230 else
7231 error_at (location, msg);
7232 break;
7234 case ic_init:
7236 const char msg[] = G_("initialization from pointer to "
7237 "non-enclosed address space");
7238 if (warnopt)
7239 warning_at (location, warnopt, msg);
7240 else
7241 error_at (location, msg);
7242 break;
7244 case ic_return:
7246 const char msg[] = G_("return from pointer to "
7247 "non-enclosed address space");
7248 if (warnopt)
7249 warning_at (location, warnopt, msg);
7250 else
7251 error_at (location, msg);
7252 break;
7254 default:
7255 gcc_unreachable ();
7257 return error_mark_node;
7260 /* Check if the right-hand side has a format attribute but the
7261 left-hand side doesn't. */
7262 if (warn_suggest_attribute_format
7263 && check_missing_format_attribute (type, rhstype))
7265 switch (errtype)
7267 case ic_argpass:
7268 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7269 "argument %d of %qE might be "
7270 "a candidate for a format attribute",
7271 parmnum, rname);
7272 break;
7273 case ic_assign:
7274 warning_at (location, OPT_Wsuggest_attribute_format,
7275 "assignment left-hand side might be "
7276 "a candidate for a format attribute");
7277 break;
7278 case ic_init:
7279 warning_at (location, OPT_Wsuggest_attribute_format,
7280 "initialization left-hand side might be "
7281 "a candidate for a format attribute");
7282 break;
7283 case ic_return:
7284 warning_at (location, OPT_Wsuggest_attribute_format,
7285 "return type might be "
7286 "a candidate for a format attribute");
7287 break;
7288 default:
7289 gcc_unreachable ();
7293 /* See if the pointers point to incompatible scalar storage orders. */
7294 if (warn_scalar_storage_order
7295 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7296 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7298 tree t;
7300 switch (errtype)
7302 case ic_argpass:
7303 /* Do not warn for built-in functions, for example memcpy, since we
7304 control how they behave and they can be useful in this area. */
7305 if (TREE_CODE (rname) != FUNCTION_DECL
7306 || !DECL_IS_UNDECLARED_BUILTIN (rname))
7307 warning_at (location, OPT_Wscalar_storage_order,
7308 "passing argument %d of %qE from incompatible "
7309 "scalar storage order", parmnum, rname);
7310 break;
7311 case ic_assign:
7312 /* Do not warn if the RHS is a call to a function that returns a
7313 pointer that is not an alias. */
7314 if (TREE_CODE (rhs) != CALL_EXPR
7315 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7316 || !DECL_IS_MALLOC (t))
7317 warning_at (location, OPT_Wscalar_storage_order,
7318 "assignment to %qT from pointer type %qT with "
7319 "incompatible scalar storage order", type, rhstype);
7320 break;
7321 case ic_init:
7322 /* Likewise. */
7323 if (TREE_CODE (rhs) != CALL_EXPR
7324 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7325 || !DECL_IS_MALLOC (t))
7326 warning_at (location, OPT_Wscalar_storage_order,
7327 "initialization of %qT from pointer type %qT with "
7328 "incompatible scalar storage order", type, rhstype);
7329 break;
7330 case ic_return:
7331 warning_at (location, OPT_Wscalar_storage_order,
7332 "returning %qT from pointer type with incompatible "
7333 "scalar storage order %qT", rhstype, type);
7334 break;
7335 default:
7336 gcc_unreachable ();
7340 /* Any non-function converts to a [const][volatile] void *
7341 and vice versa; otherwise, targets must be the same.
7342 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7343 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7344 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7345 || (target_cmp = comp_target_types (location, type, rhstype))
7346 || is_opaque_pointer
7347 || ((c_common_unsigned_type (mvl)
7348 == c_common_unsigned_type (mvr))
7349 && (c_common_signed_type (mvl)
7350 == c_common_signed_type (mvr))
7351 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7353 /* Warn about loss of qualifers from pointers to arrays with
7354 qualifiers on the element type. */
7355 if (TREE_CODE (ttr) == ARRAY_TYPE)
7357 ttr = strip_array_types (ttr);
7358 ttl = strip_array_types (ttl);
7360 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7361 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7362 WARNING_FOR_QUALIFIERS (location, expr_loc,
7363 OPT_Wdiscarded_array_qualifiers,
7364 G_("passing argument %d of %qE discards "
7365 "%qv qualifier from pointer target type"),
7366 G_("assignment discards %qv qualifier "
7367 "from pointer target type"),
7368 G_("initialization discards %qv qualifier "
7369 "from pointer target type"),
7370 G_("return discards %qv qualifier from "
7371 "pointer target type"),
7372 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7374 else if (pedantic
7375 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7377 (VOID_TYPE_P (ttr)
7378 && !null_pointer_constant
7379 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7380 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7381 G_("ISO C forbids passing argument %d of "
7382 "%qE between function pointer "
7383 "and %<void *%>"),
7384 G_("ISO C forbids assignment between "
7385 "function pointer and %<void *%>"),
7386 G_("ISO C forbids initialization between "
7387 "function pointer and %<void *%>"),
7388 G_("ISO C forbids return between function "
7389 "pointer and %<void *%>"));
7390 /* Const and volatile mean something different for function types,
7391 so the usual warnings are not appropriate. */
7392 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7393 && TREE_CODE (ttl) != FUNCTION_TYPE)
7395 /* Don't warn about loss of qualifier for conversions from
7396 qualified void* to pointers to arrays with corresponding
7397 qualifier on the element type. */
7398 if (!pedantic)
7399 ttl = strip_array_types (ttl);
7401 /* Assignments between atomic and non-atomic objects are OK. */
7402 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7403 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7405 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7406 OPT_Wdiscarded_qualifiers,
7407 G_("passing argument %d of %qE discards "
7408 "%qv qualifier from pointer target type"),
7409 G_("assignment discards %qv qualifier "
7410 "from pointer target type"),
7411 G_("initialization discards %qv qualifier "
7412 "from pointer target type"),
7413 G_("return discards %qv qualifier from "
7414 "pointer target type"),
7415 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7417 /* If this is not a case of ignoring a mismatch in signedness,
7418 no warning. */
7419 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7420 || target_cmp)
7422 /* If there is a mismatch, do warn. */
7423 else if (warn_pointer_sign)
7424 switch (errtype)
7426 case ic_argpass:
7428 auto_diagnostic_group d;
7429 range_label_for_type_mismatch rhs_label (rhstype, type);
7430 gcc_rich_location richloc (expr_loc, &rhs_label);
7431 if (pedwarn (&richloc, OPT_Wpointer_sign,
7432 "pointer targets in passing argument %d of "
7433 "%qE differ in signedness", parmnum, rname))
7434 inform_for_arg (fundecl, expr_loc, parmnum, type,
7435 rhstype);
7437 break;
7438 case ic_assign:
7439 pedwarn (location, OPT_Wpointer_sign,
7440 "pointer targets in assignment from %qT to %qT "
7441 "differ in signedness", rhstype, type);
7442 break;
7443 case ic_init:
7444 pedwarn_init (location, OPT_Wpointer_sign,
7445 "pointer targets in initialization of %qT "
7446 "from %qT differ in signedness", type,
7447 rhstype);
7448 break;
7449 case ic_return:
7450 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7451 "returning %qT from a function with return type "
7452 "%qT differ in signedness", rhstype, type);
7453 break;
7454 default:
7455 gcc_unreachable ();
7458 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7459 && TREE_CODE (ttr) == FUNCTION_TYPE)
7461 /* Because const and volatile on functions are restrictions
7462 that say the function will not do certain things,
7463 it is okay to use a const or volatile function
7464 where an ordinary one is wanted, but not vice-versa. */
7465 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7466 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7467 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7468 OPT_Wdiscarded_qualifiers,
7469 G_("passing argument %d of %qE makes "
7470 "%q#v qualified function pointer "
7471 "from unqualified"),
7472 G_("assignment makes %q#v qualified function "
7473 "pointer from unqualified"),
7474 G_("initialization makes %q#v qualified "
7475 "function pointer from unqualified"),
7476 G_("return makes %q#v qualified function "
7477 "pointer from unqualified"),
7478 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7481 /* Avoid warning about the volatile ObjC EH puts on decls. */
7482 else if (!objc_ok)
7484 switch (errtype)
7486 case ic_argpass:
7488 auto_diagnostic_group d;
7489 range_label_for_type_mismatch rhs_label (rhstype, type);
7490 gcc_rich_location richloc (expr_loc, &rhs_label);
7491 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7492 "passing argument %d of %qE from incompatible "
7493 "pointer type", parmnum, rname))
7494 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7496 break;
7497 case ic_assign:
7498 if (bltin)
7499 pedwarn (location, OPT_Wincompatible_pointer_types,
7500 "assignment to %qT from pointer to "
7501 "%qD with incompatible type %qT",
7502 type, bltin, rhstype);
7503 else
7504 pedwarn (location, OPT_Wincompatible_pointer_types,
7505 "assignment to %qT from incompatible pointer type %qT",
7506 type, rhstype);
7507 break;
7508 case ic_init:
7509 if (bltin)
7510 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7511 "initialization of %qT from pointer to "
7512 "%qD with incompatible type %qT",
7513 type, bltin, rhstype);
7514 else
7515 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7516 "initialization of %qT from incompatible "
7517 "pointer type %qT",
7518 type, rhstype);
7519 break;
7520 case ic_return:
7521 if (bltin)
7522 pedwarn (location, OPT_Wincompatible_pointer_types,
7523 "returning pointer to %qD of type %qT from "
7524 "a function with incompatible type %qT",
7525 bltin, rhstype, type);
7526 else
7527 pedwarn (location, OPT_Wincompatible_pointer_types,
7528 "returning %qT from a function with incompatible "
7529 "return type %qT", rhstype, type);
7530 break;
7531 default:
7532 gcc_unreachable ();
7536 /* If RHS isn't an address, check pointer or array of packed
7537 struct or union. */
7538 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7540 return convert (type, rhs);
7542 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7544 /* ??? This should not be an error when inlining calls to
7545 unprototyped functions. */
7546 const char msg[] = "invalid use of non-lvalue array";
7547 if (warnopt)
7548 warning_at (location, warnopt, msg);
7549 else
7550 error_at (location, msg);
7551 return error_mark_node;
7553 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7555 /* An explicit constant 0 can convert to a pointer,
7556 or one that results from arithmetic, even including
7557 a cast to integer type. */
7558 if (!null_pointer_constant)
7559 switch (errtype)
7561 case ic_argpass:
7563 auto_diagnostic_group d;
7564 range_label_for_type_mismatch rhs_label (rhstype, type);
7565 gcc_rich_location richloc (expr_loc, &rhs_label);
7566 if (pedwarn (&richloc, OPT_Wint_conversion,
7567 "passing argument %d of %qE makes pointer from "
7568 "integer without a cast", parmnum, rname))
7569 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7571 break;
7572 case ic_assign:
7573 pedwarn (location, OPT_Wint_conversion,
7574 "assignment to %qT from %qT makes pointer from integer "
7575 "without a cast", type, rhstype);
7576 break;
7577 case ic_init:
7578 pedwarn_init (location, OPT_Wint_conversion,
7579 "initialization of %qT from %qT makes pointer from "
7580 "integer without a cast", type, rhstype);
7581 break;
7582 case ic_return:
7583 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7584 "function with return type %qT makes pointer from "
7585 "integer without a cast", rhstype, type);
7586 break;
7587 default:
7588 gcc_unreachable ();
7591 return convert (type, rhs);
7593 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7595 switch (errtype)
7597 case ic_argpass:
7599 auto_diagnostic_group d;
7600 range_label_for_type_mismatch rhs_label (rhstype, type);
7601 gcc_rich_location richloc (expr_loc, &rhs_label);
7602 if (pedwarn (&richloc, OPT_Wint_conversion,
7603 "passing argument %d of %qE makes integer from "
7604 "pointer without a cast", parmnum, rname))
7605 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7607 break;
7608 case ic_assign:
7609 pedwarn (location, OPT_Wint_conversion,
7610 "assignment to %qT from %qT makes integer from pointer "
7611 "without a cast", type, rhstype);
7612 break;
7613 case ic_init:
7614 pedwarn_init (location, OPT_Wint_conversion,
7615 "initialization of %qT from %qT makes integer from "
7616 "pointer without a cast", type, rhstype);
7617 break;
7618 case ic_return:
7619 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7620 "function with return type %qT makes integer from "
7621 "pointer without a cast", rhstype, type);
7622 break;
7623 default:
7624 gcc_unreachable ();
7627 return convert (type, rhs);
7629 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7631 tree ret;
7632 bool save = in_late_binary_op;
7633 in_late_binary_op = true;
7634 ret = convert (type, rhs);
7635 in_late_binary_op = save;
7636 return ret;
7639 switch (errtype)
7641 case ic_argpass:
7643 auto_diagnostic_group d;
7644 range_label_for_type_mismatch rhs_label (rhstype, type);
7645 gcc_rich_location richloc (expr_loc, &rhs_label);
7646 const char msg[] = G_("incompatible type for argument %d of %qE");
7647 if (warnopt)
7648 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7649 else
7650 error_at (&richloc, msg, parmnum, rname);
7651 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7653 break;
7654 case ic_assign:
7656 const char msg[]
7657 = G_("incompatible types when assigning to type %qT from type %qT");
7658 if (warnopt)
7659 warning_at (expr_loc, 0, msg, type, rhstype);
7660 else
7661 error_at (expr_loc, msg, type, rhstype);
7662 break;
7664 case ic_init:
7666 const char msg[]
7667 = G_("incompatible types when initializing type %qT using type %qT");
7668 if (warnopt)
7669 warning_at (location, 0, msg, type, rhstype);
7670 else
7671 error_at (location, msg, type, rhstype);
7672 break;
7674 case ic_return:
7676 const char msg[]
7677 = G_("incompatible types when returning type %qT but %qT was expected");
7678 if (warnopt)
7679 warning_at (location, 0, msg, rhstype, type);
7680 else
7681 error_at (location, msg, rhstype, type);
7682 break;
7684 default:
7685 gcc_unreachable ();
7688 return error_mark_node;
7691 /* If VALUE is a compound expr all of whose expressions are constant, then
7692 return its value. Otherwise, return error_mark_node.
7694 This is for handling COMPOUND_EXPRs as initializer elements
7695 which is allowed with a warning when -pedantic is specified. */
7697 static tree
7698 valid_compound_expr_initializer (tree value, tree endtype)
7700 if (TREE_CODE (value) == COMPOUND_EXPR)
7702 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7703 == error_mark_node)
7704 return error_mark_node;
7705 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7706 endtype);
7708 else if (!initializer_constant_valid_p (value, endtype))
7709 return error_mark_node;
7710 else
7711 return value;
7714 /* Perform appropriate conversions on the initial value of a variable,
7715 store it in the declaration DECL,
7716 and print any error messages that are appropriate.
7717 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7718 If the init is invalid, store an ERROR_MARK.
7720 INIT_LOC is the location of the initial value. */
7722 void
7723 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7725 tree value, type;
7726 bool npc = false;
7728 /* If variable's type was invalidly declared, just ignore it. */
7730 type = TREE_TYPE (decl);
7731 if (TREE_CODE (type) == ERROR_MARK)
7732 return;
7734 /* Digest the specified initializer into an expression. */
7736 if (init)
7737 npc = null_pointer_constant_p (init);
7738 value = digest_init (init_loc, type, init, origtype, npc,
7739 true, TREE_STATIC (decl));
7741 /* Store the expression if valid; else report error. */
7743 if (!in_system_header_at (input_location)
7744 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7745 warning (OPT_Wtraditional, "traditional C rejects automatic "
7746 "aggregate initialization");
7748 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7749 DECL_INITIAL (decl) = value;
7751 /* ANSI wants warnings about out-of-range constant initializers. */
7752 STRIP_TYPE_NOPS (value);
7753 if (TREE_STATIC (decl))
7754 constant_expression_warning (value);
7756 /* Check if we need to set array size from compound literal size. */
7757 if (TREE_CODE (type) == ARRAY_TYPE
7758 && TYPE_DOMAIN (type) == NULL_TREE
7759 && value != error_mark_node)
7761 tree inside_init = init;
7763 STRIP_TYPE_NOPS (inside_init);
7764 inside_init = fold (inside_init);
7766 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7768 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7770 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7772 /* For int foo[] = (int [3]){1}; we need to set array size
7773 now since later on array initializer will be just the
7774 brace enclosed list of the compound literal. */
7775 tree etype = strip_array_types (TREE_TYPE (decl));
7776 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7777 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7778 layout_type (type);
7779 layout_decl (cldecl, 0);
7780 TREE_TYPE (decl)
7781 = c_build_qualified_type (type, TYPE_QUALS (etype));
7787 /* Methods for storing and printing names for error messages. */
7789 /* Implement a spelling stack that allows components of a name to be pushed
7790 and popped. Each element on the stack is this structure. */
7792 struct spelling
7794 int kind;
7795 union
7797 unsigned HOST_WIDE_INT i;
7798 const char *s;
7799 } u;
7802 #define SPELLING_STRING 1
7803 #define SPELLING_MEMBER 2
7804 #define SPELLING_BOUNDS 3
7806 static struct spelling *spelling; /* Next stack element (unused). */
7807 static struct spelling *spelling_base; /* Spelling stack base. */
7808 static int spelling_size; /* Size of the spelling stack. */
7810 /* Macros to save and restore the spelling stack around push_... functions.
7811 Alternative to SAVE_SPELLING_STACK. */
7813 #define SPELLING_DEPTH() (spelling - spelling_base)
7814 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7816 /* Push an element on the spelling stack with type KIND and assign VALUE
7817 to MEMBER. */
7819 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7821 int depth = SPELLING_DEPTH (); \
7823 if (depth >= spelling_size) \
7825 spelling_size += 10; \
7826 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7827 spelling_size); \
7828 RESTORE_SPELLING_DEPTH (depth); \
7831 spelling->kind = (KIND); \
7832 spelling->MEMBER = (VALUE); \
7833 spelling++; \
7836 /* Push STRING on the stack. Printed literally. */
7838 static void
7839 push_string (const char *string)
7841 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7844 /* Push a member name on the stack. Printed as '.' STRING. */
7846 static void
7847 push_member_name (tree decl)
7849 const char *const string
7850 = (DECL_NAME (decl)
7851 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7852 : _("<anonymous>"));
7853 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7856 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7858 static void
7859 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7861 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7864 /* Compute the maximum size in bytes of the printed spelling. */
7866 static int
7867 spelling_length (void)
7869 int size = 0;
7870 struct spelling *p;
7872 for (p = spelling_base; p < spelling; p++)
7874 if (p->kind == SPELLING_BOUNDS)
7875 size += 25;
7876 else
7877 size += strlen (p->u.s) + 1;
7880 return size;
7883 /* Print the spelling to BUFFER and return it. */
7885 static char *
7886 print_spelling (char *buffer)
7888 char *d = buffer;
7889 struct spelling *p;
7891 for (p = spelling_base; p < spelling; p++)
7892 if (p->kind == SPELLING_BOUNDS)
7894 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7895 d += strlen (d);
7897 else
7899 const char *s;
7900 if (p->kind == SPELLING_MEMBER)
7901 *d++ = '.';
7902 for (s = p->u.s; (*d = *s++); d++)
7905 *d++ = '\0';
7906 return buffer;
7909 /* Digest the parser output INIT as an initializer for type TYPE.
7910 Return a C expression of type TYPE to represent the initial value.
7912 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7914 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7916 If INIT is a string constant, STRICT_STRING is true if it is
7917 unparenthesized or we should not warn here for it being parenthesized.
7918 For other types of INIT, STRICT_STRING is not used.
7920 INIT_LOC is the location of the INIT.
7922 REQUIRE_CONSTANT requests an error if non-constant initializers or
7923 elements are seen. */
7925 static tree
7926 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7927 bool null_pointer_constant, bool strict_string,
7928 int require_constant)
7930 enum tree_code code = TREE_CODE (type);
7931 tree inside_init = init;
7932 tree semantic_type = NULL_TREE;
7933 bool maybe_const = true;
7935 if (type == error_mark_node
7936 || !init
7937 || error_operand_p (init))
7938 return error_mark_node;
7940 STRIP_TYPE_NOPS (inside_init);
7942 if (!c_in_omp_for)
7944 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7946 semantic_type = TREE_TYPE (inside_init);
7947 inside_init = TREE_OPERAND (inside_init, 0);
7949 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7952 /* Initialization of an array of chars from a string constant
7953 optionally enclosed in braces. */
7955 if (code == ARRAY_TYPE && inside_init
7956 && TREE_CODE (inside_init) == STRING_CST)
7958 tree typ1
7959 = (TYPE_ATOMIC (TREE_TYPE (type))
7960 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7961 TYPE_QUAL_ATOMIC)
7962 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7963 /* Note that an array could be both an array of character type
7964 and an array of wchar_t if wchar_t is signed char or unsigned
7965 char. */
7966 bool char_array = (typ1 == char_type_node
7967 || typ1 == signed_char_type_node
7968 || typ1 == unsigned_char_type_node);
7969 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7970 bool char16_array = !!comptypes (typ1, char16_type_node);
7971 bool char32_array = !!comptypes (typ1, char32_type_node);
7973 if (char_array || wchar_array || char16_array || char32_array)
7975 struct c_expr expr;
7976 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7977 bool incompat_string_cst = false;
7978 expr.value = inside_init;
7979 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7980 expr.original_type = NULL;
7981 maybe_warn_string_init (init_loc, type, expr);
7983 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7984 pedwarn_init (init_loc, OPT_Wpedantic,
7985 "initialization of a flexible array member");
7987 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7988 TYPE_MAIN_VARIANT (type)))
7989 return inside_init;
7991 if (char_array)
7993 if (typ2 != char_type_node)
7994 incompat_string_cst = true;
7996 else if (!comptypes (typ1, typ2))
7997 incompat_string_cst = true;
7999 if (incompat_string_cst)
8001 error_init (init_loc, "cannot initialize array of %qT from "
8002 "a string literal with type array of %qT",
8003 typ1, typ2);
8004 return error_mark_node;
8007 if (TYPE_DOMAIN (type) != NULL_TREE
8008 && TYPE_SIZE (type) != NULL_TREE
8009 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8011 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8012 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8014 /* Subtract the size of a single (possibly wide) character
8015 because it's ok to ignore the terminating null char
8016 that is counted in the length of the constant. */
8017 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8018 pedwarn_init (init_loc, 0,
8019 ("initializer-string for array of %qT "
8020 "is too long"), typ1);
8021 else if (warn_cxx_compat
8022 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8023 warning_at (init_loc, OPT_Wc___compat,
8024 ("initializer-string for array of %qT "
8025 "is too long for C++"), typ1);
8026 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8028 unsigned HOST_WIDE_INT size
8029 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8030 const char *p = TREE_STRING_POINTER (inside_init);
8032 inside_init = build_string (size, p);
8036 TREE_TYPE (inside_init) = type;
8037 return inside_init;
8039 else if (INTEGRAL_TYPE_P (typ1))
8041 error_init (init_loc, "array of inappropriate type initialized "
8042 "from string constant");
8043 return error_mark_node;
8047 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8048 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8049 below and handle as a constructor. */
8050 if (code == VECTOR_TYPE
8051 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8052 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8053 && TREE_CONSTANT (inside_init))
8055 if (TREE_CODE (inside_init) == VECTOR_CST
8056 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8057 TYPE_MAIN_VARIANT (type)))
8058 return inside_init;
8060 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8062 unsigned HOST_WIDE_INT ix;
8063 tree value;
8064 bool constant_p = true;
8066 /* Iterate through elements and check if all constructor
8067 elements are *_CSTs. */
8068 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8069 if (!CONSTANT_CLASS_P (value))
8071 constant_p = false;
8072 break;
8075 if (constant_p)
8076 return build_vector_from_ctor (type,
8077 CONSTRUCTOR_ELTS (inside_init));
8081 if (warn_sequence_point)
8082 verify_sequence_points (inside_init);
8084 /* Any type can be initialized
8085 from an expression of the same type, optionally with braces. */
8087 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8088 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8089 TYPE_MAIN_VARIANT (type))
8090 || (code == ARRAY_TYPE
8091 && comptypes (TREE_TYPE (inside_init), type))
8092 || (gnu_vector_type_p (type)
8093 && comptypes (TREE_TYPE (inside_init), type))
8094 || (code == POINTER_TYPE
8095 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8096 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8097 TREE_TYPE (type)))))
8099 if (code == POINTER_TYPE)
8101 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8103 if (TREE_CODE (inside_init) == STRING_CST
8104 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8105 inside_init = array_to_pointer_conversion
8106 (init_loc, inside_init);
8107 else
8109 error_init (init_loc, "invalid use of non-lvalue array");
8110 return error_mark_node;
8115 if (code == VECTOR_TYPE)
8116 /* Although the types are compatible, we may require a
8117 conversion. */
8118 inside_init = convert (type, inside_init);
8120 if (require_constant
8121 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8123 /* As an extension, allow initializing objects with static storage
8124 duration with compound literals (which are then treated just as
8125 the brace enclosed list they contain). Also allow this for
8126 vectors, as we can only assign them with compound literals. */
8127 if (flag_isoc99 && code != VECTOR_TYPE)
8128 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8129 "is not constant");
8130 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8131 inside_init = DECL_INITIAL (decl);
8134 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8135 && TREE_CODE (inside_init) != CONSTRUCTOR)
8137 error_init (init_loc, "array initialized from non-constant array "
8138 "expression");
8139 return error_mark_node;
8142 /* Compound expressions can only occur here if -Wpedantic or
8143 -pedantic-errors is specified. In the later case, we always want
8144 an error. In the former case, we simply want a warning. */
8145 if (require_constant && pedantic
8146 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8148 inside_init
8149 = valid_compound_expr_initializer (inside_init,
8150 TREE_TYPE (inside_init));
8151 if (inside_init == error_mark_node)
8152 error_init (init_loc, "initializer element is not constant");
8153 else
8154 pedwarn_init (init_loc, OPT_Wpedantic,
8155 "initializer element is not constant");
8156 if (flag_pedantic_errors)
8157 inside_init = error_mark_node;
8159 else if (require_constant
8160 && !initializer_constant_valid_p (inside_init,
8161 TREE_TYPE (inside_init)))
8163 error_init (init_loc, "initializer element is not constant");
8164 inside_init = error_mark_node;
8166 else if (require_constant && !maybe_const)
8167 pedwarn_init (init_loc, OPT_Wpedantic,
8168 "initializer element is not a constant expression");
8170 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8171 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8172 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8173 type, inside_init, origtype,
8174 ic_init, null_pointer_constant,
8175 NULL_TREE, NULL_TREE, 0);
8176 return inside_init;
8179 /* Handle scalar types, including conversions. */
8181 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8182 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8183 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8185 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8186 && (TREE_CODE (init) == STRING_CST
8187 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8188 inside_init = init = array_to_pointer_conversion (init_loc, init);
8189 if (semantic_type)
8190 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8191 inside_init);
8192 inside_init
8193 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8194 inside_init, origtype, ic_init,
8195 null_pointer_constant, NULL_TREE, NULL_TREE,
8198 /* Check to see if we have already given an error message. */
8199 if (inside_init == error_mark_node)
8201 else if (require_constant && !TREE_CONSTANT (inside_init))
8203 error_init (init_loc, "initializer element is not constant");
8204 inside_init = error_mark_node;
8206 else if (require_constant
8207 && !initializer_constant_valid_p (inside_init,
8208 TREE_TYPE (inside_init)))
8210 error_init (init_loc, "initializer element is not computable at "
8211 "load time");
8212 inside_init = error_mark_node;
8214 else if (require_constant && !maybe_const)
8215 pedwarn_init (init_loc, OPT_Wpedantic,
8216 "initializer element is not a constant expression");
8218 return inside_init;
8221 /* Come here only for records and arrays. */
8223 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8225 error_init (init_loc, "variable-sized object may not be initialized");
8226 return error_mark_node;
8229 error_init (init_loc, "invalid initializer");
8230 return error_mark_node;
8233 /* Handle initializers that use braces. */
8235 /* Type of object we are accumulating a constructor for.
8236 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8237 static tree constructor_type;
8239 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8240 left to fill. */
8241 static tree constructor_fields;
8243 /* For an ARRAY_TYPE, this is the specified index
8244 at which to store the next element we get. */
8245 static tree constructor_index;
8247 /* For an ARRAY_TYPE, this is the maximum index. */
8248 static tree constructor_max_index;
8250 /* For a RECORD_TYPE, this is the first field not yet written out. */
8251 static tree constructor_unfilled_fields;
8253 /* For an ARRAY_TYPE, this is the index of the first element
8254 not yet written out. */
8255 static tree constructor_unfilled_index;
8257 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8258 This is so we can generate gaps between fields, when appropriate. */
8259 static tree constructor_bit_index;
8261 /* If we are saving up the elements rather than allocating them,
8262 this is the list of elements so far (in reverse order,
8263 most recent first). */
8264 static vec<constructor_elt, va_gc> *constructor_elements;
8266 /* 1 if constructor should be incrementally stored into a constructor chain,
8267 0 if all the elements should be kept in AVL tree. */
8268 static int constructor_incremental;
8270 /* 1 if so far this constructor's elements are all compile-time constants. */
8271 static int constructor_constant;
8273 /* 1 if so far this constructor's elements are all valid address constants. */
8274 static int constructor_simple;
8276 /* 1 if this constructor has an element that cannot be part of a
8277 constant expression. */
8278 static int constructor_nonconst;
8280 /* 1 if this constructor is erroneous so far. */
8281 static int constructor_erroneous;
8283 /* 1 if this constructor is the universal zero initializer { 0 }. */
8284 static int constructor_zeroinit;
8286 /* Structure for managing pending initializer elements, organized as an
8287 AVL tree. */
8289 struct init_node
8291 struct init_node *left, *right;
8292 struct init_node *parent;
8293 int balance;
8294 tree purpose;
8295 tree value;
8296 tree origtype;
8299 /* Tree of pending elements at this constructor level.
8300 These are elements encountered out of order
8301 which belong at places we haven't reached yet in actually
8302 writing the output.
8303 Will never hold tree nodes across GC runs. */
8304 static struct init_node *constructor_pending_elts;
8306 /* The SPELLING_DEPTH of this constructor. */
8307 static int constructor_depth;
8309 /* DECL node for which an initializer is being read.
8310 0 means we are reading a constructor expression
8311 such as (struct foo) {...}. */
8312 static tree constructor_decl;
8314 /* Nonzero if this is an initializer for a top-level decl. */
8315 static int constructor_top_level;
8317 /* Nonzero if there were any member designators in this initializer. */
8318 static int constructor_designated;
8320 /* Nesting depth of designator list. */
8321 static int designator_depth;
8323 /* Nonzero if there were diagnosed errors in this designator list. */
8324 static int designator_erroneous;
8327 /* This stack has a level for each implicit or explicit level of
8328 structuring in the initializer, including the outermost one. It
8329 saves the values of most of the variables above. */
8331 struct constructor_range_stack;
8333 struct constructor_stack
8335 struct constructor_stack *next;
8336 tree type;
8337 tree fields;
8338 tree index;
8339 tree max_index;
8340 tree unfilled_index;
8341 tree unfilled_fields;
8342 tree bit_index;
8343 vec<constructor_elt, va_gc> *elements;
8344 struct init_node *pending_elts;
8345 int offset;
8346 int depth;
8347 /* If value nonzero, this value should replace the entire
8348 constructor at this level. */
8349 struct c_expr replacement_value;
8350 struct constructor_range_stack *range_stack;
8351 char constant;
8352 char simple;
8353 char nonconst;
8354 char implicit;
8355 char erroneous;
8356 char outer;
8357 char incremental;
8358 char designated;
8359 int designator_depth;
8362 static struct constructor_stack *constructor_stack;
8364 /* This stack represents designators from some range designator up to
8365 the last designator in the list. */
8367 struct constructor_range_stack
8369 struct constructor_range_stack *next, *prev;
8370 struct constructor_stack *stack;
8371 tree range_start;
8372 tree index;
8373 tree range_end;
8374 tree fields;
8377 static struct constructor_range_stack *constructor_range_stack;
8379 /* This stack records separate initializers that are nested.
8380 Nested initializers can't happen in ANSI C, but GNU C allows them
8381 in cases like { ... (struct foo) { ... } ... }. */
8383 struct initializer_stack
8385 struct initializer_stack *next;
8386 tree decl;
8387 struct constructor_stack *constructor_stack;
8388 struct constructor_range_stack *constructor_range_stack;
8389 vec<constructor_elt, va_gc> *elements;
8390 struct spelling *spelling;
8391 struct spelling *spelling_base;
8392 int spelling_size;
8393 char top_level;
8394 char require_constant_value;
8395 char require_constant_elements;
8396 rich_location *missing_brace_richloc;
8399 static struct initializer_stack *initializer_stack;
8401 /* Prepare to parse and output the initializer for variable DECL. */
8403 void
8404 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8405 rich_location *richloc)
8407 const char *locus;
8408 struct initializer_stack *p = XNEW (struct initializer_stack);
8410 p->decl = constructor_decl;
8411 p->require_constant_value = require_constant_value;
8412 p->require_constant_elements = require_constant_elements;
8413 p->constructor_stack = constructor_stack;
8414 p->constructor_range_stack = constructor_range_stack;
8415 p->elements = constructor_elements;
8416 p->spelling = spelling;
8417 p->spelling_base = spelling_base;
8418 p->spelling_size = spelling_size;
8419 p->top_level = constructor_top_level;
8420 p->next = initializer_stack;
8421 p->missing_brace_richloc = richloc;
8422 initializer_stack = p;
8424 constructor_decl = decl;
8425 constructor_designated = 0;
8426 constructor_top_level = top_level;
8428 if (decl != NULL_TREE && decl != error_mark_node)
8430 require_constant_value = TREE_STATIC (decl);
8431 require_constant_elements
8432 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8433 /* For a scalar, you can always use any value to initialize,
8434 even within braces. */
8435 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8436 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8438 else
8440 require_constant_value = 0;
8441 require_constant_elements = 0;
8442 locus = _("(anonymous)");
8445 constructor_stack = 0;
8446 constructor_range_stack = 0;
8448 found_missing_braces = 0;
8450 spelling_base = 0;
8451 spelling_size = 0;
8452 RESTORE_SPELLING_DEPTH (0);
8454 if (locus)
8455 push_string (locus);
8458 void
8459 finish_init (void)
8461 struct initializer_stack *p = initializer_stack;
8463 /* Free the whole constructor stack of this initializer. */
8464 while (constructor_stack)
8466 struct constructor_stack *q = constructor_stack;
8467 constructor_stack = q->next;
8468 XDELETE (q);
8471 gcc_assert (!constructor_range_stack);
8473 /* Pop back to the data of the outer initializer (if any). */
8474 XDELETE (spelling_base);
8476 constructor_decl = p->decl;
8477 require_constant_value = p->require_constant_value;
8478 require_constant_elements = p->require_constant_elements;
8479 constructor_stack = p->constructor_stack;
8480 constructor_range_stack = p->constructor_range_stack;
8481 constructor_elements = p->elements;
8482 spelling = p->spelling;
8483 spelling_base = p->spelling_base;
8484 spelling_size = p->spelling_size;
8485 constructor_top_level = p->top_level;
8486 initializer_stack = p->next;
8487 XDELETE (p);
8490 /* Call here when we see the initializer is surrounded by braces.
8491 This is instead of a call to push_init_level;
8492 it is matched by a call to pop_init_level.
8494 TYPE is the type to initialize, for a constructor expression.
8495 For an initializer for a decl, TYPE is zero. */
8497 void
8498 really_start_incremental_init (tree type)
8500 struct constructor_stack *p = XNEW (struct constructor_stack);
8502 if (type == NULL_TREE)
8503 type = TREE_TYPE (constructor_decl);
8505 if (VECTOR_TYPE_P (type)
8506 && TYPE_VECTOR_OPAQUE (type))
8507 error ("opaque vector types cannot be initialized");
8509 p->type = constructor_type;
8510 p->fields = constructor_fields;
8511 p->index = constructor_index;
8512 p->max_index = constructor_max_index;
8513 p->unfilled_index = constructor_unfilled_index;
8514 p->unfilled_fields = constructor_unfilled_fields;
8515 p->bit_index = constructor_bit_index;
8516 p->elements = constructor_elements;
8517 p->constant = constructor_constant;
8518 p->simple = constructor_simple;
8519 p->nonconst = constructor_nonconst;
8520 p->erroneous = constructor_erroneous;
8521 p->pending_elts = constructor_pending_elts;
8522 p->depth = constructor_depth;
8523 p->replacement_value.value = 0;
8524 p->replacement_value.original_code = ERROR_MARK;
8525 p->replacement_value.original_type = NULL;
8526 p->implicit = 0;
8527 p->range_stack = 0;
8528 p->outer = 0;
8529 p->incremental = constructor_incremental;
8530 p->designated = constructor_designated;
8531 p->designator_depth = designator_depth;
8532 p->next = 0;
8533 constructor_stack = p;
8535 constructor_constant = 1;
8536 constructor_simple = 1;
8537 constructor_nonconst = 0;
8538 constructor_depth = SPELLING_DEPTH ();
8539 constructor_elements = NULL;
8540 constructor_pending_elts = 0;
8541 constructor_type = type;
8542 constructor_incremental = 1;
8543 constructor_designated = 0;
8544 constructor_zeroinit = 1;
8545 designator_depth = 0;
8546 designator_erroneous = 0;
8548 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8550 constructor_fields = TYPE_FIELDS (constructor_type);
8551 /* Skip any nameless bit fields at the beginning. */
8552 while (constructor_fields != NULL_TREE
8553 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8554 constructor_fields = DECL_CHAIN (constructor_fields);
8556 constructor_unfilled_fields = constructor_fields;
8557 constructor_bit_index = bitsize_zero_node;
8559 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8561 if (TYPE_DOMAIN (constructor_type))
8563 constructor_max_index
8564 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8566 /* Detect non-empty initializations of zero-length arrays. */
8567 if (constructor_max_index == NULL_TREE
8568 && TYPE_SIZE (constructor_type))
8569 constructor_max_index = integer_minus_one_node;
8571 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8572 to initialize VLAs will cause a proper error; avoid tree
8573 checking errors as well by setting a safe value. */
8574 if (constructor_max_index
8575 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8576 constructor_max_index = integer_minus_one_node;
8578 constructor_index
8579 = convert (bitsizetype,
8580 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8582 else
8584 constructor_index = bitsize_zero_node;
8585 constructor_max_index = NULL_TREE;
8588 constructor_unfilled_index = constructor_index;
8590 else if (gnu_vector_type_p (constructor_type))
8592 /* Vectors are like simple fixed-size arrays. */
8593 constructor_max_index =
8594 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8595 constructor_index = bitsize_zero_node;
8596 constructor_unfilled_index = constructor_index;
8598 else
8600 /* Handle the case of int x = {5}; */
8601 constructor_fields = constructor_type;
8602 constructor_unfilled_fields = constructor_type;
8606 extern location_t last_init_list_comma;
8608 /* Called when we see an open brace for a nested initializer. Finish
8609 off any pending levels with implicit braces. */
8610 void
8611 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8613 while (constructor_stack->implicit)
8615 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8616 && constructor_fields == NULL_TREE)
8617 process_init_element (input_location,
8618 pop_init_level (loc, 1, braced_init_obstack,
8619 last_init_list_comma),
8620 true, braced_init_obstack);
8621 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8622 && constructor_max_index
8623 && tree_int_cst_lt (constructor_max_index,
8624 constructor_index))
8625 process_init_element (input_location,
8626 pop_init_level (loc, 1, braced_init_obstack,
8627 last_init_list_comma),
8628 true, braced_init_obstack);
8629 else
8630 break;
8634 /* Push down into a subobject, for initialization.
8635 If this is for an explicit set of braces, IMPLICIT is 0.
8636 If it is because the next element belongs at a lower level,
8637 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8639 void
8640 push_init_level (location_t loc, int implicit,
8641 struct obstack *braced_init_obstack)
8643 struct constructor_stack *p;
8644 tree value = NULL_TREE;
8646 /* Unless this is an explicit brace, we need to preserve previous
8647 content if any. */
8648 if (implicit)
8650 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8651 value = find_init_member (constructor_fields, braced_init_obstack);
8652 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8653 value = find_init_member (constructor_index, braced_init_obstack);
8656 p = XNEW (struct constructor_stack);
8657 p->type = constructor_type;
8658 p->fields = constructor_fields;
8659 p->index = constructor_index;
8660 p->max_index = constructor_max_index;
8661 p->unfilled_index = constructor_unfilled_index;
8662 p->unfilled_fields = constructor_unfilled_fields;
8663 p->bit_index = constructor_bit_index;
8664 p->elements = constructor_elements;
8665 p->constant = constructor_constant;
8666 p->simple = constructor_simple;
8667 p->nonconst = constructor_nonconst;
8668 p->erroneous = constructor_erroneous;
8669 p->pending_elts = constructor_pending_elts;
8670 p->depth = constructor_depth;
8671 p->replacement_value.value = NULL_TREE;
8672 p->replacement_value.original_code = ERROR_MARK;
8673 p->replacement_value.original_type = NULL;
8674 p->implicit = implicit;
8675 p->outer = 0;
8676 p->incremental = constructor_incremental;
8677 p->designated = constructor_designated;
8678 p->designator_depth = designator_depth;
8679 p->next = constructor_stack;
8680 p->range_stack = 0;
8681 constructor_stack = p;
8683 constructor_constant = 1;
8684 constructor_simple = 1;
8685 constructor_nonconst = 0;
8686 constructor_depth = SPELLING_DEPTH ();
8687 constructor_elements = NULL;
8688 constructor_incremental = 1;
8689 constructor_designated = 0;
8690 constructor_pending_elts = 0;
8691 if (!implicit)
8693 p->range_stack = constructor_range_stack;
8694 constructor_range_stack = 0;
8695 designator_depth = 0;
8696 designator_erroneous = 0;
8699 /* Don't die if an entire brace-pair level is superfluous
8700 in the containing level. */
8701 if (constructor_type == NULL_TREE)
8703 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8705 /* Don't die if there are extra init elts at the end. */
8706 if (constructor_fields == NULL_TREE)
8707 constructor_type = NULL_TREE;
8708 else
8710 constructor_type = TREE_TYPE (constructor_fields);
8711 push_member_name (constructor_fields);
8712 constructor_depth++;
8714 /* If upper initializer is designated, then mark this as
8715 designated too to prevent bogus warnings. */
8716 constructor_designated = p->designated;
8718 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8720 constructor_type = TREE_TYPE (constructor_type);
8721 push_array_bounds (tree_to_uhwi (constructor_index));
8722 constructor_depth++;
8725 if (constructor_type == NULL_TREE)
8727 error_init (loc, "extra brace group at end of initializer");
8728 constructor_fields = NULL_TREE;
8729 constructor_unfilled_fields = NULL_TREE;
8730 return;
8733 if (value && TREE_CODE (value) == CONSTRUCTOR)
8735 constructor_constant = TREE_CONSTANT (value);
8736 constructor_simple = TREE_STATIC (value);
8737 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8738 constructor_elements = CONSTRUCTOR_ELTS (value);
8739 if (!vec_safe_is_empty (constructor_elements)
8740 && (TREE_CODE (constructor_type) == RECORD_TYPE
8741 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8742 set_nonincremental_init (braced_init_obstack);
8745 if (implicit == 1)
8747 found_missing_braces = 1;
8748 if (initializer_stack->missing_brace_richloc)
8749 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8750 (loc, "{");
8753 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8755 constructor_fields = TYPE_FIELDS (constructor_type);
8756 /* Skip any nameless bit fields at the beginning. */
8757 while (constructor_fields != NULL_TREE
8758 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8759 constructor_fields = DECL_CHAIN (constructor_fields);
8761 constructor_unfilled_fields = constructor_fields;
8762 constructor_bit_index = bitsize_zero_node;
8764 else if (gnu_vector_type_p (constructor_type))
8766 /* Vectors are like simple fixed-size arrays. */
8767 constructor_max_index =
8768 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8769 constructor_index = bitsize_int (0);
8770 constructor_unfilled_index = constructor_index;
8772 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8774 if (TYPE_DOMAIN (constructor_type))
8776 constructor_max_index
8777 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8779 /* Detect non-empty initializations of zero-length arrays. */
8780 if (constructor_max_index == NULL_TREE
8781 && TYPE_SIZE (constructor_type))
8782 constructor_max_index = integer_minus_one_node;
8784 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8785 to initialize VLAs will cause a proper error; avoid tree
8786 checking errors as well by setting a safe value. */
8787 if (constructor_max_index
8788 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8789 constructor_max_index = integer_minus_one_node;
8791 constructor_index
8792 = convert (bitsizetype,
8793 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8795 else
8796 constructor_index = bitsize_zero_node;
8798 constructor_unfilled_index = constructor_index;
8799 if (value && TREE_CODE (value) == STRING_CST)
8801 /* We need to split the char/wchar array into individual
8802 characters, so that we don't have to special case it
8803 everywhere. */
8804 set_nonincremental_init_from_string (value, braced_init_obstack);
8807 else
8809 if (constructor_type != error_mark_node)
8810 warning_init (input_location, 0, "braces around scalar initializer");
8811 constructor_fields = constructor_type;
8812 constructor_unfilled_fields = constructor_type;
8816 /* At the end of an implicit or explicit brace level,
8817 finish up that level of constructor. If a single expression
8818 with redundant braces initialized that level, return the
8819 c_expr structure for that expression. Otherwise, the original_code
8820 element is set to ERROR_MARK.
8821 If we were outputting the elements as they are read, return 0 as the value
8822 from inner levels (process_init_element ignores that),
8823 but return error_mark_node as the value from the outermost level
8824 (that's what we want to put in DECL_INITIAL).
8825 Otherwise, return a CONSTRUCTOR expression as the value. */
8827 struct c_expr
8828 pop_init_level (location_t loc, int implicit,
8829 struct obstack *braced_init_obstack,
8830 location_t insert_before)
8832 struct constructor_stack *p;
8833 struct c_expr ret;
8834 ret.value = NULL_TREE;
8835 ret.original_code = ERROR_MARK;
8836 ret.original_type = NULL;
8838 if (implicit == 0)
8840 /* When we come to an explicit close brace,
8841 pop any inner levels that didn't have explicit braces. */
8842 while (constructor_stack->implicit)
8843 process_init_element (input_location,
8844 pop_init_level (loc, 1, braced_init_obstack,
8845 insert_before),
8846 true, braced_init_obstack);
8847 gcc_assert (!constructor_range_stack);
8849 else
8850 if (initializer_stack->missing_brace_richloc)
8851 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8852 (insert_before, "}");
8854 /* Now output all pending elements. */
8855 constructor_incremental = 1;
8856 output_pending_init_elements (1, braced_init_obstack);
8858 p = constructor_stack;
8860 /* Error for initializing a flexible array member, or a zero-length
8861 array member in an inappropriate context. */
8862 if (constructor_type && constructor_fields
8863 && TREE_CODE (constructor_type) == ARRAY_TYPE
8864 && TYPE_DOMAIN (constructor_type)
8865 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8867 /* Silently discard empty initializations. The parser will
8868 already have pedwarned for empty brackets. */
8869 if (integer_zerop (constructor_unfilled_index))
8870 constructor_type = NULL_TREE;
8871 else
8873 gcc_assert (!TYPE_SIZE (constructor_type));
8875 if (constructor_depth > 2)
8876 error_init (loc, "initialization of flexible array member in a nested context");
8877 else
8878 pedwarn_init (loc, OPT_Wpedantic,
8879 "initialization of a flexible array member");
8881 /* We have already issued an error message for the existence
8882 of a flexible array member not at the end of the structure.
8883 Discard the initializer so that we do not die later. */
8884 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8885 constructor_type = NULL_TREE;
8889 switch (vec_safe_length (constructor_elements))
8891 case 0:
8892 /* Initialization with { } counts as zeroinit. */
8893 constructor_zeroinit = 1;
8894 break;
8895 case 1:
8896 /* This might be zeroinit as well. */
8897 if (integer_zerop ((*constructor_elements)[0].value))
8898 constructor_zeroinit = 1;
8899 break;
8900 default:
8901 /* If the constructor has more than one element, it can't be { 0 }. */
8902 constructor_zeroinit = 0;
8903 break;
8906 /* Warn when some structs are initialized with direct aggregation. */
8907 if (!implicit && found_missing_braces && warn_missing_braces
8908 && !constructor_zeroinit)
8910 gcc_assert (initializer_stack->missing_brace_richloc);
8911 warning_at (initializer_stack->missing_brace_richloc,
8912 OPT_Wmissing_braces,
8913 "missing braces around initializer");
8916 /* Warn when some struct elements are implicitly initialized to zero. */
8917 if (warn_missing_field_initializers
8918 && constructor_type
8919 && TREE_CODE (constructor_type) == RECORD_TYPE
8920 && constructor_unfilled_fields)
8922 /* Do not warn for flexible array members or zero-length arrays. */
8923 while (constructor_unfilled_fields
8924 && (!DECL_SIZE (constructor_unfilled_fields)
8925 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8926 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8928 if (constructor_unfilled_fields
8929 /* Do not warn if this level of the initializer uses member
8930 designators; it is likely to be deliberate. */
8931 && !constructor_designated
8932 /* Do not warn about initializing with { 0 } or with { }. */
8933 && !constructor_zeroinit)
8935 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8936 "missing initializer for field %qD of %qT",
8937 constructor_unfilled_fields,
8938 constructor_type))
8939 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8940 "%qD declared here", constructor_unfilled_fields);
8944 /* Pad out the end of the structure. */
8945 if (p->replacement_value.value)
8946 /* If this closes a superfluous brace pair,
8947 just pass out the element between them. */
8948 ret = p->replacement_value;
8949 else if (constructor_type == NULL_TREE)
8951 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8952 && TREE_CODE (constructor_type) != ARRAY_TYPE
8953 && !gnu_vector_type_p (constructor_type))
8955 /* A nonincremental scalar initializer--just return
8956 the element, after verifying there is just one. */
8957 if (vec_safe_is_empty (constructor_elements))
8959 if (!constructor_erroneous && constructor_type != error_mark_node)
8960 error_init (loc, "empty scalar initializer");
8961 ret.value = error_mark_node;
8963 else if (vec_safe_length (constructor_elements) != 1)
8965 error_init (loc, "extra elements in scalar initializer");
8966 ret.value = (*constructor_elements)[0].value;
8968 else
8969 ret.value = (*constructor_elements)[0].value;
8971 else
8973 if (constructor_erroneous)
8974 ret.value = error_mark_node;
8975 else
8977 ret.value = build_constructor (constructor_type,
8978 constructor_elements);
8979 if (constructor_constant)
8980 TREE_CONSTANT (ret.value) = 1;
8981 if (constructor_constant && constructor_simple)
8982 TREE_STATIC (ret.value) = 1;
8983 if (constructor_nonconst)
8984 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8988 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8990 if (constructor_nonconst)
8991 ret.original_code = C_MAYBE_CONST_EXPR;
8992 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8993 ret.original_code = ERROR_MARK;
8996 constructor_type = p->type;
8997 constructor_fields = p->fields;
8998 constructor_index = p->index;
8999 constructor_max_index = p->max_index;
9000 constructor_unfilled_index = p->unfilled_index;
9001 constructor_unfilled_fields = p->unfilled_fields;
9002 constructor_bit_index = p->bit_index;
9003 constructor_elements = p->elements;
9004 constructor_constant = p->constant;
9005 constructor_simple = p->simple;
9006 constructor_nonconst = p->nonconst;
9007 constructor_erroneous = p->erroneous;
9008 constructor_incremental = p->incremental;
9009 constructor_designated = p->designated;
9010 designator_depth = p->designator_depth;
9011 constructor_pending_elts = p->pending_elts;
9012 constructor_depth = p->depth;
9013 if (!p->implicit)
9014 constructor_range_stack = p->range_stack;
9015 RESTORE_SPELLING_DEPTH (constructor_depth);
9017 constructor_stack = p->next;
9018 XDELETE (p);
9020 if (ret.value == NULL_TREE && constructor_stack == 0)
9021 ret.value = error_mark_node;
9022 return ret;
9025 /* Common handling for both array range and field name designators.
9026 ARRAY argument is nonzero for array ranges. Returns false for success. */
9028 static bool
9029 set_designator (location_t loc, bool array,
9030 struct obstack *braced_init_obstack)
9032 tree subtype;
9033 enum tree_code subcode;
9035 /* Don't die if an entire brace-pair level is superfluous
9036 in the containing level, or for an erroneous type. */
9037 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9038 return true;
9040 /* If there were errors in this designator list already, bail out
9041 silently. */
9042 if (designator_erroneous)
9043 return true;
9045 /* Likewise for an initializer for a variable-size type. Those are
9046 diagnosed in digest_init. */
9047 if (COMPLETE_TYPE_P (constructor_type)
9048 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9049 return true;
9051 if (!designator_depth)
9053 gcc_assert (!constructor_range_stack);
9055 /* Designator list starts at the level of closest explicit
9056 braces. */
9057 while (constructor_stack->implicit)
9058 process_init_element (input_location,
9059 pop_init_level (loc, 1, braced_init_obstack,
9060 last_init_list_comma),
9061 true, braced_init_obstack);
9062 constructor_designated = 1;
9063 return false;
9066 switch (TREE_CODE (constructor_type))
9068 case RECORD_TYPE:
9069 case UNION_TYPE:
9070 subtype = TREE_TYPE (constructor_fields);
9071 if (subtype != error_mark_node)
9072 subtype = TYPE_MAIN_VARIANT (subtype);
9073 break;
9074 case ARRAY_TYPE:
9075 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9076 break;
9077 default:
9078 gcc_unreachable ();
9081 subcode = TREE_CODE (subtype);
9082 if (array && subcode != ARRAY_TYPE)
9084 error_init (loc, "array index in non-array initializer");
9085 return true;
9087 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9089 error_init (loc, "field name not in record or union initializer");
9090 return true;
9093 constructor_designated = 1;
9094 finish_implicit_inits (loc, braced_init_obstack);
9095 push_init_level (loc, 2, braced_init_obstack);
9096 return false;
9099 /* If there are range designators in designator list, push a new designator
9100 to constructor_range_stack. RANGE_END is end of such stack range or
9101 NULL_TREE if there is no range designator at this level. */
9103 static void
9104 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9106 struct constructor_range_stack *p;
9108 p = (struct constructor_range_stack *)
9109 obstack_alloc (braced_init_obstack,
9110 sizeof (struct constructor_range_stack));
9111 p->prev = constructor_range_stack;
9112 p->next = 0;
9113 p->fields = constructor_fields;
9114 p->range_start = constructor_index;
9115 p->index = constructor_index;
9116 p->stack = constructor_stack;
9117 p->range_end = range_end;
9118 if (constructor_range_stack)
9119 constructor_range_stack->next = p;
9120 constructor_range_stack = p;
9123 /* Within an array initializer, specify the next index to be initialized.
9124 FIRST is that index. If LAST is nonzero, then initialize a range
9125 of indices, running from FIRST through LAST. */
9127 void
9128 set_init_index (location_t loc, tree first, tree last,
9129 struct obstack *braced_init_obstack)
9131 if (set_designator (loc, true, braced_init_obstack))
9132 return;
9134 designator_erroneous = 1;
9136 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9137 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9139 error_init (loc, "array index in initializer not of integer type");
9140 return;
9143 if (TREE_CODE (first) != INTEGER_CST)
9145 first = c_fully_fold (first, false, NULL);
9146 if (TREE_CODE (first) == INTEGER_CST)
9147 pedwarn_init (loc, OPT_Wpedantic,
9148 "array index in initializer is not "
9149 "an integer constant expression");
9152 if (last && TREE_CODE (last) != INTEGER_CST)
9154 last = c_fully_fold (last, false, NULL);
9155 if (TREE_CODE (last) == INTEGER_CST)
9156 pedwarn_init (loc, OPT_Wpedantic,
9157 "array index in initializer is not "
9158 "an integer constant expression");
9161 if (TREE_CODE (first) != INTEGER_CST)
9162 error_init (loc, "nonconstant array index in initializer");
9163 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9164 error_init (loc, "nonconstant array index in initializer");
9165 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9166 error_init (loc, "array index in non-array initializer");
9167 else if (tree_int_cst_sgn (first) == -1)
9168 error_init (loc, "array index in initializer exceeds array bounds");
9169 else if (constructor_max_index
9170 && tree_int_cst_lt (constructor_max_index, first))
9171 error_init (loc, "array index in initializer exceeds array bounds");
9172 else
9174 constant_expression_warning (first);
9175 if (last)
9176 constant_expression_warning (last);
9177 constructor_index = convert (bitsizetype, first);
9178 if (tree_int_cst_lt (constructor_index, first))
9180 constructor_index = copy_node (constructor_index);
9181 TREE_OVERFLOW (constructor_index) = 1;
9184 if (last)
9186 if (tree_int_cst_equal (first, last))
9187 last = NULL_TREE;
9188 else if (tree_int_cst_lt (last, first))
9190 error_init (loc, "empty index range in initializer");
9191 last = NULL_TREE;
9193 else
9195 last = convert (bitsizetype, last);
9196 if (constructor_max_index != NULL_TREE
9197 && tree_int_cst_lt (constructor_max_index, last))
9199 error_init (loc, "array index range in initializer exceeds "
9200 "array bounds");
9201 last = NULL_TREE;
9206 designator_depth++;
9207 designator_erroneous = 0;
9208 if (constructor_range_stack || last)
9209 push_range_stack (last, braced_init_obstack);
9213 /* Within a struct initializer, specify the next field to be initialized. */
9215 void
9216 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9217 struct obstack *braced_init_obstack)
9219 tree field;
9221 if (set_designator (loc, false, braced_init_obstack))
9222 return;
9224 designator_erroneous = 1;
9226 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9228 error_init (loc, "field name not in record or union initializer");
9229 return;
9232 field = lookup_field (constructor_type, fieldname);
9234 if (field == NULL_TREE)
9236 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9237 if (guessed_id)
9239 gcc_rich_location rich_loc (fieldname_loc);
9240 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9241 error_at (&rich_loc,
9242 "%qT has no member named %qE; did you mean %qE?",
9243 constructor_type, fieldname, guessed_id);
9245 else
9246 error_at (fieldname_loc, "%qT has no member named %qE",
9247 constructor_type, fieldname);
9249 else
9252 constructor_fields = TREE_VALUE (field);
9253 designator_depth++;
9254 designator_erroneous = 0;
9255 if (constructor_range_stack)
9256 push_range_stack (NULL_TREE, braced_init_obstack);
9257 field = TREE_CHAIN (field);
9258 if (field)
9260 if (set_designator (loc, false, braced_init_obstack))
9261 return;
9264 while (field != NULL_TREE);
9267 /* Add a new initializer to the tree of pending initializers. PURPOSE
9268 identifies the initializer, either array index or field in a structure.
9269 VALUE is the value of that index or field. If ORIGTYPE is not
9270 NULL_TREE, it is the original type of VALUE.
9272 IMPLICIT is true if value comes from pop_init_level (1),
9273 the new initializer has been merged with the existing one
9274 and thus no warnings should be emitted about overriding an
9275 existing initializer. */
9277 static void
9278 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9279 bool implicit, struct obstack *braced_init_obstack)
9281 struct init_node *p, **q, *r;
9283 q = &constructor_pending_elts;
9284 p = 0;
9286 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9288 while (*q != 0)
9290 p = *q;
9291 if (tree_int_cst_lt (purpose, p->purpose))
9292 q = &p->left;
9293 else if (tree_int_cst_lt (p->purpose, purpose))
9294 q = &p->right;
9295 else
9297 if (!implicit)
9299 if (TREE_SIDE_EFFECTS (p->value))
9300 warning_init (loc, OPT_Woverride_init_side_effects,
9301 "initialized field with side-effects "
9302 "overwritten");
9303 else if (warn_override_init)
9304 warning_init (loc, OPT_Woverride_init,
9305 "initialized field overwritten");
9307 p->value = value;
9308 p->origtype = origtype;
9309 return;
9313 else
9315 tree bitpos;
9317 bitpos = bit_position (purpose);
9318 while (*q != NULL)
9320 p = *q;
9321 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9322 q = &p->left;
9323 else if (p->purpose != purpose)
9324 q = &p->right;
9325 else
9327 if (!implicit)
9329 if (TREE_SIDE_EFFECTS (p->value))
9330 warning_init (loc, OPT_Woverride_init_side_effects,
9331 "initialized field with side-effects "
9332 "overwritten");
9333 else if (warn_override_init)
9334 warning_init (loc, OPT_Woverride_init,
9335 "initialized field overwritten");
9337 p->value = value;
9338 p->origtype = origtype;
9339 return;
9344 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9345 sizeof (struct init_node));
9346 r->purpose = purpose;
9347 r->value = value;
9348 r->origtype = origtype;
9350 *q = r;
9351 r->parent = p;
9352 r->left = 0;
9353 r->right = 0;
9354 r->balance = 0;
9356 while (p)
9358 struct init_node *s;
9360 if (r == p->left)
9362 if (p->balance == 0)
9363 p->balance = -1;
9364 else if (p->balance < 0)
9366 if (r->balance < 0)
9368 /* L rotation. */
9369 p->left = r->right;
9370 if (p->left)
9371 p->left->parent = p;
9372 r->right = p;
9374 p->balance = 0;
9375 r->balance = 0;
9377 s = p->parent;
9378 p->parent = r;
9379 r->parent = s;
9380 if (s)
9382 if (s->left == p)
9383 s->left = r;
9384 else
9385 s->right = r;
9387 else
9388 constructor_pending_elts = r;
9390 else
9392 /* LR rotation. */
9393 struct init_node *t = r->right;
9395 r->right = t->left;
9396 if (r->right)
9397 r->right->parent = r;
9398 t->left = r;
9400 p->left = t->right;
9401 if (p->left)
9402 p->left->parent = p;
9403 t->right = p;
9405 p->balance = t->balance < 0;
9406 r->balance = -(t->balance > 0);
9407 t->balance = 0;
9409 s = p->parent;
9410 p->parent = t;
9411 r->parent = t;
9412 t->parent = s;
9413 if (s)
9415 if (s->left == p)
9416 s->left = t;
9417 else
9418 s->right = t;
9420 else
9421 constructor_pending_elts = t;
9423 break;
9425 else
9427 /* p->balance == +1; growth of left side balances the node. */
9428 p->balance = 0;
9429 break;
9432 else /* r == p->right */
9434 if (p->balance == 0)
9435 /* Growth propagation from right side. */
9436 p->balance++;
9437 else if (p->balance > 0)
9439 if (r->balance > 0)
9441 /* R rotation. */
9442 p->right = r->left;
9443 if (p->right)
9444 p->right->parent = p;
9445 r->left = p;
9447 p->balance = 0;
9448 r->balance = 0;
9450 s = p->parent;
9451 p->parent = r;
9452 r->parent = s;
9453 if (s)
9455 if (s->left == p)
9456 s->left = r;
9457 else
9458 s->right = r;
9460 else
9461 constructor_pending_elts = r;
9463 else /* r->balance == -1 */
9465 /* RL rotation */
9466 struct init_node *t = r->left;
9468 r->left = t->right;
9469 if (r->left)
9470 r->left->parent = r;
9471 t->right = r;
9473 p->right = t->left;
9474 if (p->right)
9475 p->right->parent = p;
9476 t->left = p;
9478 r->balance = (t->balance < 0);
9479 p->balance = -(t->balance > 0);
9480 t->balance = 0;
9482 s = p->parent;
9483 p->parent = t;
9484 r->parent = t;
9485 t->parent = s;
9486 if (s)
9488 if (s->left == p)
9489 s->left = t;
9490 else
9491 s->right = t;
9493 else
9494 constructor_pending_elts = t;
9496 break;
9498 else
9500 /* p->balance == -1; growth of right side balances the node. */
9501 p->balance = 0;
9502 break;
9506 r = p;
9507 p = p->parent;
9511 /* Build AVL tree from a sorted chain. */
9513 static void
9514 set_nonincremental_init (struct obstack * braced_init_obstack)
9516 unsigned HOST_WIDE_INT ix;
9517 tree index, value;
9519 if (TREE_CODE (constructor_type) != RECORD_TYPE
9520 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9521 return;
9523 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9524 add_pending_init (input_location, index, value, NULL_TREE, true,
9525 braced_init_obstack);
9526 constructor_elements = NULL;
9527 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9529 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9530 /* Skip any nameless bit fields at the beginning. */
9531 while (constructor_unfilled_fields != NULL_TREE
9532 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9533 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9536 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9538 if (TYPE_DOMAIN (constructor_type))
9539 constructor_unfilled_index
9540 = convert (bitsizetype,
9541 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9542 else
9543 constructor_unfilled_index = bitsize_zero_node;
9545 constructor_incremental = 0;
9548 /* Build AVL tree from a string constant. */
9550 static void
9551 set_nonincremental_init_from_string (tree str,
9552 struct obstack * braced_init_obstack)
9554 tree value, purpose, type;
9555 HOST_WIDE_INT val[2];
9556 const char *p, *end;
9557 int byte, wchar_bytes, charwidth, bitpos;
9559 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9561 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9562 charwidth = TYPE_PRECISION (char_type_node);
9563 gcc_assert ((size_t) wchar_bytes * charwidth
9564 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9565 type = TREE_TYPE (constructor_type);
9566 p = TREE_STRING_POINTER (str);
9567 end = p + TREE_STRING_LENGTH (str);
9569 for (purpose = bitsize_zero_node;
9570 p < end
9571 && !(constructor_max_index
9572 && tree_int_cst_lt (constructor_max_index, purpose));
9573 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9575 if (wchar_bytes == 1)
9577 val[0] = (unsigned char) *p++;
9578 val[1] = 0;
9580 else
9582 val[1] = 0;
9583 val[0] = 0;
9584 for (byte = 0; byte < wchar_bytes; byte++)
9586 if (BYTES_BIG_ENDIAN)
9587 bitpos = (wchar_bytes - byte - 1) * charwidth;
9588 else
9589 bitpos = byte * charwidth;
9590 val[bitpos / HOST_BITS_PER_WIDE_INT]
9591 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9592 << (bitpos % HOST_BITS_PER_WIDE_INT);
9596 if (!TYPE_UNSIGNED (type))
9598 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9599 if (bitpos < HOST_BITS_PER_WIDE_INT)
9601 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9603 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9604 val[1] = -1;
9607 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9609 if (val[0] < 0)
9610 val[1] = -1;
9612 else if (val[1] & (HOST_WIDE_INT_1
9613 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9614 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9617 value = wide_int_to_tree (type,
9618 wide_int::from_array (val, 2,
9619 HOST_BITS_PER_WIDE_INT * 2));
9620 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9621 braced_init_obstack);
9624 constructor_incremental = 0;
9627 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9628 not initialized yet. */
9630 static tree
9631 find_init_member (tree field, struct obstack * braced_init_obstack)
9633 struct init_node *p;
9635 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9637 if (constructor_incremental
9638 && tree_int_cst_lt (field, constructor_unfilled_index))
9639 set_nonincremental_init (braced_init_obstack);
9641 p = constructor_pending_elts;
9642 while (p)
9644 if (tree_int_cst_lt (field, p->purpose))
9645 p = p->left;
9646 else if (tree_int_cst_lt (p->purpose, field))
9647 p = p->right;
9648 else
9649 return p->value;
9652 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9654 tree bitpos = bit_position (field);
9656 if (constructor_incremental
9657 && (!constructor_unfilled_fields
9658 || tree_int_cst_lt (bitpos,
9659 bit_position (constructor_unfilled_fields))))
9660 set_nonincremental_init (braced_init_obstack);
9662 p = constructor_pending_elts;
9663 while (p)
9665 if (field == p->purpose)
9666 return p->value;
9667 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9668 p = p->left;
9669 else
9670 p = p->right;
9673 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9675 if (!vec_safe_is_empty (constructor_elements)
9676 && (constructor_elements->last ().index == field))
9677 return constructor_elements->last ().value;
9679 return NULL_TREE;
9682 /* "Output" the next constructor element.
9683 At top level, really output it to assembler code now.
9684 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9685 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9686 TYPE is the data type that the containing data type wants here.
9687 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9688 If VALUE is a string constant, STRICT_STRING is true if it is
9689 unparenthesized or we should not warn here for it being parenthesized.
9690 For other types of VALUE, STRICT_STRING is not used.
9692 PENDING if true means output pending elements that belong
9693 right after this element. (PENDING is normally true;
9694 it is false while outputting pending elements, to avoid recursion.)
9696 IMPLICIT is true if value comes from pop_init_level (1),
9697 the new initializer has been merged with the existing one
9698 and thus no warnings should be emitted about overriding an
9699 existing initializer. */
9701 static void
9702 output_init_element (location_t loc, tree value, tree origtype,
9703 bool strict_string, tree type, tree field, bool pending,
9704 bool implicit, struct obstack * braced_init_obstack)
9706 tree semantic_type = NULL_TREE;
9707 bool maybe_const = true;
9708 bool npc;
9710 if (type == error_mark_node || value == error_mark_node)
9712 constructor_erroneous = 1;
9713 return;
9715 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9716 && (TREE_CODE (value) == STRING_CST
9717 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9718 && !(TREE_CODE (value) == STRING_CST
9719 && TREE_CODE (type) == ARRAY_TYPE
9720 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9721 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9722 TYPE_MAIN_VARIANT (type)))
9723 value = array_to_pointer_conversion (input_location, value);
9725 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9726 && require_constant_value && pending)
9728 /* As an extension, allow initializing objects with static storage
9729 duration with compound literals (which are then treated just as
9730 the brace enclosed list they contain). */
9731 if (flag_isoc99)
9732 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9733 "constant");
9734 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9735 value = DECL_INITIAL (decl);
9738 npc = null_pointer_constant_p (value);
9739 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9741 semantic_type = TREE_TYPE (value);
9742 value = TREE_OPERAND (value, 0);
9744 value = c_fully_fold (value, require_constant_value, &maybe_const);
9746 if (value == error_mark_node)
9747 constructor_erroneous = 1;
9748 else if (!TREE_CONSTANT (value))
9749 constructor_constant = 0;
9750 else if (!initializer_constant_valid_p (value,
9751 TREE_TYPE (value),
9752 AGGREGATE_TYPE_P (constructor_type)
9753 && TYPE_REVERSE_STORAGE_ORDER
9754 (constructor_type))
9755 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9756 && DECL_C_BIT_FIELD (field)
9757 && TREE_CODE (value) != INTEGER_CST))
9758 constructor_simple = 0;
9759 if (!maybe_const)
9760 constructor_nonconst = 1;
9762 /* Digest the initializer and issue any errors about incompatible
9763 types before issuing errors about non-constant initializers. */
9764 tree new_value = value;
9765 if (semantic_type)
9766 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9767 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9768 require_constant_value);
9769 if (new_value == error_mark_node)
9771 constructor_erroneous = 1;
9772 return;
9774 if (require_constant_value || require_constant_elements)
9775 constant_expression_warning (new_value);
9777 /* Proceed to check the constness of the original initializer. */
9778 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9780 if (require_constant_value)
9782 error_init (loc, "initializer element is not constant");
9783 value = error_mark_node;
9785 else if (require_constant_elements)
9786 pedwarn (loc, OPT_Wpedantic,
9787 "initializer element is not computable at load time");
9789 else if (!maybe_const
9790 && (require_constant_value || require_constant_elements))
9791 pedwarn_init (loc, OPT_Wpedantic,
9792 "initializer element is not a constant expression");
9794 /* Issue -Wc++-compat warnings about initializing a bitfield with
9795 enum type. */
9796 if (warn_cxx_compat
9797 && field != NULL_TREE
9798 && TREE_CODE (field) == FIELD_DECL
9799 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9800 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9801 != TYPE_MAIN_VARIANT (type))
9802 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9804 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9805 if (checktype != error_mark_node
9806 && (TYPE_MAIN_VARIANT (checktype)
9807 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9808 warning_init (loc, OPT_Wc___compat,
9809 "enum conversion in initialization is invalid in C++");
9812 /* If this field is empty and does not have side effects (and is not at
9813 the end of structure), don't do anything other than checking the
9814 initializer. */
9815 if (field
9816 && (TREE_TYPE (field) == error_mark_node
9817 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9818 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9819 && !TREE_SIDE_EFFECTS (new_value)
9820 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9821 || DECL_CHAIN (field)))))
9822 return;
9824 /* Finally, set VALUE to the initializer value digested above. */
9825 value = new_value;
9827 /* If this element doesn't come next in sequence,
9828 put it on constructor_pending_elts. */
9829 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9830 && (!constructor_incremental
9831 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9833 if (constructor_incremental
9834 && tree_int_cst_lt (field, constructor_unfilled_index))
9835 set_nonincremental_init (braced_init_obstack);
9837 add_pending_init (loc, field, value, origtype, implicit,
9838 braced_init_obstack);
9839 return;
9841 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9842 && (!constructor_incremental
9843 || field != constructor_unfilled_fields))
9845 /* We do this for records but not for unions. In a union,
9846 no matter which field is specified, it can be initialized
9847 right away since it starts at the beginning of the union. */
9848 if (constructor_incremental)
9850 if (!constructor_unfilled_fields)
9851 set_nonincremental_init (braced_init_obstack);
9852 else
9854 tree bitpos, unfillpos;
9856 bitpos = bit_position (field);
9857 unfillpos = bit_position (constructor_unfilled_fields);
9859 if (tree_int_cst_lt (bitpos, unfillpos))
9860 set_nonincremental_init (braced_init_obstack);
9864 add_pending_init (loc, field, value, origtype, implicit,
9865 braced_init_obstack);
9866 return;
9868 else if (TREE_CODE (constructor_type) == UNION_TYPE
9869 && !vec_safe_is_empty (constructor_elements))
9871 if (!implicit)
9873 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9874 warning_init (loc, OPT_Woverride_init_side_effects,
9875 "initialized field with side-effects overwritten");
9876 else if (warn_override_init)
9877 warning_init (loc, OPT_Woverride_init,
9878 "initialized field overwritten");
9881 /* We can have just one union field set. */
9882 constructor_elements = NULL;
9885 /* Otherwise, output this element either to
9886 constructor_elements or to the assembler file. */
9888 constructor_elt celt = {field, value};
9889 vec_safe_push (constructor_elements, celt);
9891 /* Advance the variable that indicates sequential elements output. */
9892 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9893 constructor_unfilled_index
9894 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9895 bitsize_one_node);
9896 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9898 constructor_unfilled_fields
9899 = DECL_CHAIN (constructor_unfilled_fields);
9901 /* Skip any nameless bit fields. */
9902 while (constructor_unfilled_fields != NULL_TREE
9903 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9904 constructor_unfilled_fields =
9905 DECL_CHAIN (constructor_unfilled_fields);
9907 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9908 constructor_unfilled_fields = NULL_TREE;
9910 /* Now output any pending elements which have become next. */
9911 if (pending)
9912 output_pending_init_elements (0, braced_init_obstack);
9915 /* For two FIELD_DECLs in the same chain, return -1 if field1
9916 comes before field2, 1 if field1 comes after field2 and
9917 0 if field1 == field2. */
9919 static int
9920 init_field_decl_cmp (tree field1, tree field2)
9922 if (field1 == field2)
9923 return 0;
9925 tree bitpos1 = bit_position (field1);
9926 tree bitpos2 = bit_position (field2);
9927 if (tree_int_cst_equal (bitpos1, bitpos2))
9929 /* If one of the fields has non-zero bitsize, then that
9930 field must be the last one in a sequence of zero
9931 sized fields, fields after it will have bigger
9932 bit_position. */
9933 if (TREE_TYPE (field1) != error_mark_node
9934 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9935 && integer_nonzerop (TREE_TYPE (field1)))
9936 return 1;
9937 if (TREE_TYPE (field2) != error_mark_node
9938 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9939 && integer_nonzerop (TREE_TYPE (field2)))
9940 return -1;
9941 /* Otherwise, fallback to DECL_CHAIN walk to find out
9942 which field comes earlier. Walk chains of both
9943 fields, so that if field1 and field2 are close to each
9944 other in either order, it is found soon even for large
9945 sequences of zero sized fields. */
9946 tree f1 = field1, f2 = field2;
9947 while (1)
9949 f1 = DECL_CHAIN (f1);
9950 f2 = DECL_CHAIN (f2);
9951 if (f1 == NULL_TREE)
9953 gcc_assert (f2);
9954 return 1;
9956 if (f2 == NULL_TREE)
9957 return -1;
9958 if (f1 == field2)
9959 return -1;
9960 if (f2 == field1)
9961 return 1;
9962 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9963 return 1;
9964 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9965 return -1;
9968 else if (tree_int_cst_lt (bitpos1, bitpos2))
9969 return -1;
9970 else
9971 return 1;
9974 /* Output any pending elements which have become next.
9975 As we output elements, constructor_unfilled_{fields,index}
9976 advances, which may cause other elements to become next;
9977 if so, they too are output.
9979 If ALL is 0, we return when there are
9980 no more pending elements to output now.
9982 If ALL is 1, we output space as necessary so that
9983 we can output all the pending elements. */
9984 static void
9985 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9987 struct init_node *elt = constructor_pending_elts;
9988 tree next;
9990 retry:
9992 /* Look through the whole pending tree.
9993 If we find an element that should be output now,
9994 output it. Otherwise, set NEXT to the element
9995 that comes first among those still pending. */
9997 next = NULL_TREE;
9998 while (elt)
10000 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10002 if (tree_int_cst_equal (elt->purpose,
10003 constructor_unfilled_index))
10004 output_init_element (input_location, elt->value, elt->origtype,
10005 true, TREE_TYPE (constructor_type),
10006 constructor_unfilled_index, false, false,
10007 braced_init_obstack);
10008 else if (tree_int_cst_lt (constructor_unfilled_index,
10009 elt->purpose))
10011 /* Advance to the next smaller node. */
10012 if (elt->left)
10013 elt = elt->left;
10014 else
10016 /* We have reached the smallest node bigger than the
10017 current unfilled index. Fill the space first. */
10018 next = elt->purpose;
10019 break;
10022 else
10024 /* Advance to the next bigger node. */
10025 if (elt->right)
10026 elt = elt->right;
10027 else
10029 /* We have reached the biggest node in a subtree. Find
10030 the parent of it, which is the next bigger node. */
10031 while (elt->parent && elt->parent->right == elt)
10032 elt = elt->parent;
10033 elt = elt->parent;
10034 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10035 elt->purpose))
10037 next = elt->purpose;
10038 break;
10043 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10045 /* If the current record is complete we are done. */
10046 if (constructor_unfilled_fields == NULL_TREE)
10047 break;
10049 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10050 elt->purpose);
10051 if (cmp == 0)
10052 output_init_element (input_location, elt->value, elt->origtype,
10053 true, TREE_TYPE (elt->purpose),
10054 elt->purpose, false, false,
10055 braced_init_obstack);
10056 else if (cmp < 0)
10058 /* Advance to the next smaller node. */
10059 if (elt->left)
10060 elt = elt->left;
10061 else
10063 /* We have reached the smallest node bigger than the
10064 current unfilled field. Fill the space first. */
10065 next = elt->purpose;
10066 break;
10069 else
10071 /* Advance to the next bigger node. */
10072 if (elt->right)
10073 elt = elt->right;
10074 else
10076 /* We have reached the biggest node in a subtree. Find
10077 the parent of it, which is the next bigger node. */
10078 while (elt->parent && elt->parent->right == elt)
10079 elt = elt->parent;
10080 elt = elt->parent;
10081 if (elt
10082 && init_field_decl_cmp (constructor_unfilled_fields,
10083 elt->purpose) < 0)
10085 next = elt->purpose;
10086 break;
10093 /* Ordinarily return, but not if we want to output all
10094 and there are elements left. */
10095 if (!(all && next != NULL_TREE))
10096 return;
10098 /* If it's not incremental, just skip over the gap, so that after
10099 jumping to retry we will output the next successive element. */
10100 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10101 constructor_unfilled_fields = next;
10102 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10103 constructor_unfilled_index = next;
10105 /* ELT now points to the node in the pending tree with the next
10106 initializer to output. */
10107 goto retry;
10110 /* Expression VALUE coincides with the start of type TYPE in a braced
10111 initializer. Return true if we should treat VALUE as initializing
10112 the first element of TYPE, false if we should treat it as initializing
10113 TYPE as a whole.
10115 If the initializer is clearly invalid, the question becomes:
10116 which choice gives the best error message? */
10118 static bool
10119 initialize_elementwise_p (tree type, tree value)
10121 if (type == error_mark_node || value == error_mark_node)
10122 return false;
10124 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10126 tree value_type = TREE_TYPE (value);
10127 if (value_type == error_mark_node)
10128 return false;
10130 /* GNU vectors can be initialized elementwise. However, treat any
10131 kind of vector value as initializing the vector type as a whole,
10132 regardless of whether the value is a GNU vector. Such initializers
10133 are valid if and only if they would have been valid in a non-braced
10134 initializer like:
10136 TYPE foo = VALUE;
10138 so recursing into the vector type would be at best confusing or at
10139 worst wrong. For example, when -flax-vector-conversions is in effect,
10140 it's possible to initialize a V8HI from a V4SI, even though the vectors
10141 have different element types and different numbers of elements. */
10142 if (gnu_vector_type_p (type))
10143 return !VECTOR_TYPE_P (value_type);
10145 if (AGGREGATE_TYPE_P (type))
10146 return type != TYPE_MAIN_VARIANT (value_type);
10148 return false;
10151 /* Add one non-braced element to the current constructor level.
10152 This adjusts the current position within the constructor's type.
10153 This may also start or terminate implicit levels
10154 to handle a partly-braced initializer.
10156 Once this has found the correct level for the new element,
10157 it calls output_init_element.
10159 IMPLICIT is true if value comes from pop_init_level (1),
10160 the new initializer has been merged with the existing one
10161 and thus no warnings should be emitted about overriding an
10162 existing initializer. */
10164 void
10165 process_init_element (location_t loc, struct c_expr value, bool implicit,
10166 struct obstack * braced_init_obstack)
10168 tree orig_value = value.value;
10169 int string_flag
10170 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10171 bool strict_string = value.original_code == STRING_CST;
10172 bool was_designated = designator_depth != 0;
10174 designator_depth = 0;
10175 designator_erroneous = 0;
10177 if (!implicit && value.value && !integer_zerop (value.value))
10178 constructor_zeroinit = 0;
10180 /* Handle superfluous braces around string cst as in
10181 char x[] = {"foo"}; */
10182 if (string_flag
10183 && constructor_type
10184 && !was_designated
10185 && TREE_CODE (constructor_type) == ARRAY_TYPE
10186 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10187 && integer_zerop (constructor_unfilled_index))
10189 if (constructor_stack->replacement_value.value)
10190 error_init (loc, "excess elements in %<char%> array initializer");
10191 constructor_stack->replacement_value = value;
10192 return;
10195 if (constructor_stack->replacement_value.value != NULL_TREE)
10197 error_init (loc, "excess elements in struct initializer");
10198 return;
10201 /* Ignore elements of a brace group if it is entirely superfluous
10202 and has already been diagnosed, or if the type is erroneous. */
10203 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10204 return;
10206 /* Ignore elements of an initializer for a variable-size type.
10207 Those are diagnosed in digest_init. */
10208 if (COMPLETE_TYPE_P (constructor_type)
10209 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10210 return;
10212 if (!implicit && warn_designated_init && !was_designated
10213 && TREE_CODE (constructor_type) == RECORD_TYPE
10214 && lookup_attribute ("designated_init",
10215 TYPE_ATTRIBUTES (constructor_type)))
10216 warning_init (loc,
10217 OPT_Wdesignated_init,
10218 "positional initialization of field "
10219 "in %<struct%> declared with %<designated_init%> attribute");
10221 /* If we've exhausted any levels that didn't have braces,
10222 pop them now. */
10223 while (constructor_stack->implicit)
10225 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10226 && constructor_fields == NULL_TREE)
10227 process_init_element (loc,
10228 pop_init_level (loc, 1, braced_init_obstack,
10229 last_init_list_comma),
10230 true, braced_init_obstack);
10231 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10232 || gnu_vector_type_p (constructor_type))
10233 && constructor_max_index
10234 && tree_int_cst_lt (constructor_max_index,
10235 constructor_index))
10236 process_init_element (loc,
10237 pop_init_level (loc, 1, braced_init_obstack,
10238 last_init_list_comma),
10239 true, braced_init_obstack);
10240 else
10241 break;
10244 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10245 if (constructor_range_stack)
10247 /* If value is a compound literal and we'll be just using its
10248 content, don't put it into a SAVE_EXPR. */
10249 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10250 || !require_constant_value)
10252 tree semantic_type = NULL_TREE;
10253 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10255 semantic_type = TREE_TYPE (value.value);
10256 value.value = TREE_OPERAND (value.value, 0);
10258 value.value = save_expr (value.value);
10259 if (semantic_type)
10260 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10261 value.value);
10265 while (1)
10267 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10269 tree fieldtype;
10270 enum tree_code fieldcode;
10272 if (constructor_fields == NULL_TREE)
10274 pedwarn_init (loc, 0, "excess elements in struct initializer");
10275 break;
10278 fieldtype = TREE_TYPE (constructor_fields);
10279 if (fieldtype != error_mark_node)
10280 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10281 fieldcode = TREE_CODE (fieldtype);
10283 /* Error for non-static initialization of a flexible array member. */
10284 if (fieldcode == ARRAY_TYPE
10285 && !require_constant_value
10286 && TYPE_SIZE (fieldtype) == NULL_TREE
10287 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10289 error_init (loc, "non-static initialization of a flexible "
10290 "array member");
10291 break;
10294 /* Error for initialization of a flexible array member with
10295 a string constant if the structure is in an array. E.g.:
10296 struct S { int x; char y[]; };
10297 struct S s[] = { { 1, "foo" } };
10298 is invalid. */
10299 if (string_flag
10300 && fieldcode == ARRAY_TYPE
10301 && constructor_depth > 1
10302 && TYPE_SIZE (fieldtype) == NULL_TREE
10303 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10305 bool in_array_p = false;
10306 for (struct constructor_stack *p = constructor_stack;
10307 p && p->type; p = p->next)
10308 if (TREE_CODE (p->type) == ARRAY_TYPE)
10310 in_array_p = true;
10311 break;
10313 if (in_array_p)
10315 error_init (loc, "initialization of flexible array "
10316 "member in a nested context");
10317 break;
10321 /* Accept a string constant to initialize a subarray. */
10322 if (value.value != NULL_TREE
10323 && fieldcode == ARRAY_TYPE
10324 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10325 && string_flag)
10326 value.value = orig_value;
10327 /* Otherwise, if we have come to a subaggregate,
10328 and we don't have an element of its type, push into it. */
10329 else if (value.value != NULL_TREE
10330 && initialize_elementwise_p (fieldtype, value.value))
10332 push_init_level (loc, 1, braced_init_obstack);
10333 continue;
10336 if (value.value)
10338 push_member_name (constructor_fields);
10339 output_init_element (loc, value.value, value.original_type,
10340 strict_string, fieldtype,
10341 constructor_fields, true, implicit,
10342 braced_init_obstack);
10343 RESTORE_SPELLING_DEPTH (constructor_depth);
10345 else
10346 /* Do the bookkeeping for an element that was
10347 directly output as a constructor. */
10349 /* For a record, keep track of end position of last field. */
10350 if (DECL_SIZE (constructor_fields))
10351 constructor_bit_index
10352 = size_binop_loc (input_location, PLUS_EXPR,
10353 bit_position (constructor_fields),
10354 DECL_SIZE (constructor_fields));
10356 /* If the current field was the first one not yet written out,
10357 it isn't now, so update. */
10358 if (constructor_unfilled_fields == constructor_fields)
10360 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10361 /* Skip any nameless bit fields. */
10362 while (constructor_unfilled_fields != 0
10363 && (DECL_UNNAMED_BIT_FIELD
10364 (constructor_unfilled_fields)))
10365 constructor_unfilled_fields =
10366 DECL_CHAIN (constructor_unfilled_fields);
10370 constructor_fields = DECL_CHAIN (constructor_fields);
10371 /* Skip any nameless bit fields at the beginning. */
10372 while (constructor_fields != NULL_TREE
10373 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10374 constructor_fields = DECL_CHAIN (constructor_fields);
10376 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10378 tree fieldtype;
10379 enum tree_code fieldcode;
10381 if (constructor_fields == NULL_TREE)
10383 pedwarn_init (loc, 0,
10384 "excess elements in union initializer");
10385 break;
10388 fieldtype = TREE_TYPE (constructor_fields);
10389 if (fieldtype != error_mark_node)
10390 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10391 fieldcode = TREE_CODE (fieldtype);
10393 /* Warn that traditional C rejects initialization of unions.
10394 We skip the warning if the value is zero. This is done
10395 under the assumption that the zero initializer in user
10396 code appears conditioned on e.g. __STDC__ to avoid
10397 "missing initializer" warnings and relies on default
10398 initialization to zero in the traditional C case.
10399 We also skip the warning if the initializer is designated,
10400 again on the assumption that this must be conditional on
10401 __STDC__ anyway (and we've already complained about the
10402 member-designator already). */
10403 if (!in_system_header_at (input_location) && !constructor_designated
10404 && !(value.value && (integer_zerop (value.value)
10405 || real_zerop (value.value))))
10406 warning (OPT_Wtraditional, "traditional C rejects initialization "
10407 "of unions");
10409 /* Accept a string constant to initialize a subarray. */
10410 if (value.value != NULL_TREE
10411 && fieldcode == ARRAY_TYPE
10412 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10413 && string_flag)
10414 value.value = orig_value;
10415 /* Otherwise, if we have come to a subaggregate,
10416 and we don't have an element of its type, push into it. */
10417 else if (value.value != NULL_TREE
10418 && initialize_elementwise_p (fieldtype, value.value))
10420 push_init_level (loc, 1, braced_init_obstack);
10421 continue;
10424 if (value.value)
10426 push_member_name (constructor_fields);
10427 output_init_element (loc, value.value, value.original_type,
10428 strict_string, fieldtype,
10429 constructor_fields, true, implicit,
10430 braced_init_obstack);
10431 RESTORE_SPELLING_DEPTH (constructor_depth);
10433 else
10434 /* Do the bookkeeping for an element that was
10435 directly output as a constructor. */
10437 constructor_bit_index = DECL_SIZE (constructor_fields);
10438 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10441 constructor_fields = NULL_TREE;
10443 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10445 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10446 enum tree_code eltcode = TREE_CODE (elttype);
10448 /* Accept a string constant to initialize a subarray. */
10449 if (value.value != NULL_TREE
10450 && eltcode == ARRAY_TYPE
10451 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10452 && string_flag)
10453 value.value = orig_value;
10454 /* Otherwise, if we have come to a subaggregate,
10455 and we don't have an element of its type, push into it. */
10456 else if (value.value != NULL_TREE
10457 && initialize_elementwise_p (elttype, value.value))
10459 push_init_level (loc, 1, braced_init_obstack);
10460 continue;
10463 if (constructor_max_index != NULL_TREE
10464 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10465 || integer_all_onesp (constructor_max_index)))
10467 pedwarn_init (loc, 0,
10468 "excess elements in array initializer");
10469 break;
10472 /* Now output the actual element. */
10473 if (value.value)
10475 push_array_bounds (tree_to_uhwi (constructor_index));
10476 output_init_element (loc, value.value, value.original_type,
10477 strict_string, elttype,
10478 constructor_index, true, implicit,
10479 braced_init_obstack);
10480 RESTORE_SPELLING_DEPTH (constructor_depth);
10483 constructor_index
10484 = size_binop_loc (input_location, PLUS_EXPR,
10485 constructor_index, bitsize_one_node);
10487 if (!value.value)
10488 /* If we are doing the bookkeeping for an element that was
10489 directly output as a constructor, we must update
10490 constructor_unfilled_index. */
10491 constructor_unfilled_index = constructor_index;
10493 else if (gnu_vector_type_p (constructor_type))
10495 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10497 /* Do a basic check of initializer size. Note that vectors
10498 always have a fixed size derived from their type. */
10499 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10501 pedwarn_init (loc, 0,
10502 "excess elements in vector initializer");
10503 break;
10506 /* Now output the actual element. */
10507 if (value.value)
10509 if (TREE_CODE (value.value) == VECTOR_CST)
10510 elttype = TYPE_MAIN_VARIANT (constructor_type);
10511 output_init_element (loc, value.value, value.original_type,
10512 strict_string, elttype,
10513 constructor_index, true, implicit,
10514 braced_init_obstack);
10517 constructor_index
10518 = size_binop_loc (input_location,
10519 PLUS_EXPR, constructor_index, bitsize_one_node);
10521 if (!value.value)
10522 /* If we are doing the bookkeeping for an element that was
10523 directly output as a constructor, we must update
10524 constructor_unfilled_index. */
10525 constructor_unfilled_index = constructor_index;
10528 /* Handle the sole element allowed in a braced initializer
10529 for a scalar variable. */
10530 else if (constructor_type != error_mark_node
10531 && constructor_fields == NULL_TREE)
10533 pedwarn_init (loc, 0,
10534 "excess elements in scalar initializer");
10535 break;
10537 else
10539 if (value.value)
10540 output_init_element (loc, value.value, value.original_type,
10541 strict_string, constructor_type,
10542 NULL_TREE, true, implicit,
10543 braced_init_obstack);
10544 constructor_fields = NULL_TREE;
10547 /* Handle range initializers either at this level or anywhere higher
10548 in the designator stack. */
10549 if (constructor_range_stack)
10551 struct constructor_range_stack *p, *range_stack;
10552 int finish = 0;
10554 range_stack = constructor_range_stack;
10555 constructor_range_stack = 0;
10556 while (constructor_stack != range_stack->stack)
10558 gcc_assert (constructor_stack->implicit);
10559 process_init_element (loc,
10560 pop_init_level (loc, 1,
10561 braced_init_obstack,
10562 last_init_list_comma),
10563 true, braced_init_obstack);
10565 for (p = range_stack;
10566 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10567 p = p->prev)
10569 gcc_assert (constructor_stack->implicit);
10570 process_init_element (loc,
10571 pop_init_level (loc, 1,
10572 braced_init_obstack,
10573 last_init_list_comma),
10574 true, braced_init_obstack);
10577 p->index = size_binop_loc (input_location,
10578 PLUS_EXPR, p->index, bitsize_one_node);
10579 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10580 finish = 1;
10582 while (1)
10584 constructor_index = p->index;
10585 constructor_fields = p->fields;
10586 if (finish && p->range_end && p->index == p->range_start)
10588 finish = 0;
10589 p->prev = 0;
10591 p = p->next;
10592 if (!p)
10593 break;
10594 finish_implicit_inits (loc, braced_init_obstack);
10595 push_init_level (loc, 2, braced_init_obstack);
10596 p->stack = constructor_stack;
10597 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10598 p->index = p->range_start;
10601 if (!finish)
10602 constructor_range_stack = range_stack;
10603 continue;
10606 break;
10609 constructor_range_stack = 0;
10612 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10613 (guaranteed to be 'volatile' or null) and ARGS (represented using
10614 an ASM_EXPR node). */
10615 tree
10616 build_asm_stmt (bool is_volatile, tree args)
10618 if (is_volatile)
10619 ASM_VOLATILE_P (args) = 1;
10620 return add_stmt (args);
10623 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10624 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10625 SIMPLE indicates whether there was anything at all after the
10626 string in the asm expression -- asm("blah") and asm("blah" : )
10627 are subtly different. We use a ASM_EXPR node to represent this.
10628 LOC is the location of the asm, and IS_INLINE says whether this
10629 is asm inline. */
10630 tree
10631 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10632 tree clobbers, tree labels, bool simple, bool is_inline)
10634 tree tail;
10635 tree args;
10636 int i;
10637 const char *constraint;
10638 const char **oconstraints;
10639 bool allows_mem, allows_reg, is_inout;
10640 int ninputs, noutputs;
10642 ninputs = list_length (inputs);
10643 noutputs = list_length (outputs);
10644 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10646 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10648 /* Remove output conversions that change the type but not the mode. */
10649 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10651 tree output = TREE_VALUE (tail);
10653 output = c_fully_fold (output, false, NULL, true);
10655 /* ??? Really, this should not be here. Users should be using a
10656 proper lvalue, dammit. But there's a long history of using casts
10657 in the output operands. In cases like longlong.h, this becomes a
10658 primitive form of typechecking -- if the cast can be removed, then
10659 the output operand had a type of the proper width; otherwise we'll
10660 get an error. Gross, but ... */
10661 STRIP_NOPS (output);
10663 if (!lvalue_or_else (loc, output, lv_asm))
10664 output = error_mark_node;
10666 if (output != error_mark_node
10667 && (TREE_READONLY (output)
10668 || TYPE_READONLY (TREE_TYPE (output))
10669 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10670 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10671 readonly_error (loc, output, lv_asm);
10673 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10674 oconstraints[i] = constraint;
10676 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10677 &allows_mem, &allows_reg, &is_inout))
10679 /* If the operand is going to end up in memory,
10680 mark it addressable. */
10681 if (!allows_reg && !c_mark_addressable (output))
10682 output = error_mark_node;
10683 if (!(!allows_reg && allows_mem)
10684 && output != error_mark_node
10685 && VOID_TYPE_P (TREE_TYPE (output)))
10687 error_at (loc, "invalid use of void expression");
10688 output = error_mark_node;
10691 else
10692 output = error_mark_node;
10694 TREE_VALUE (tail) = output;
10697 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10699 tree input;
10701 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10702 input = TREE_VALUE (tail);
10704 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10705 oconstraints, &allows_mem, &allows_reg))
10707 /* If the operand is going to end up in memory,
10708 mark it addressable. */
10709 if (!allows_reg && allows_mem)
10711 input = c_fully_fold (input, false, NULL, true);
10713 /* Strip the nops as we allow this case. FIXME, this really
10714 should be rejected or made deprecated. */
10715 STRIP_NOPS (input);
10716 if (!c_mark_addressable (input))
10717 input = error_mark_node;
10719 else
10721 struct c_expr expr;
10722 memset (&expr, 0, sizeof (expr));
10723 expr.value = input;
10724 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10725 input = c_fully_fold (expr.value, false, NULL);
10727 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10729 error_at (loc, "invalid use of void expression");
10730 input = error_mark_node;
10734 else
10735 input = error_mark_node;
10737 TREE_VALUE (tail) = input;
10740 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10742 /* asm statements without outputs, including simple ones, are treated
10743 as volatile. */
10744 ASM_INPUT_P (args) = simple;
10745 ASM_VOLATILE_P (args) = (noutputs == 0);
10746 ASM_INLINE_P (args) = is_inline;
10748 return args;
10751 /* Generate a goto statement to LABEL. LOC is the location of the
10752 GOTO. */
10754 tree
10755 c_finish_goto_label (location_t loc, tree label)
10757 tree decl = lookup_label_for_goto (loc, label);
10758 if (!decl)
10759 return NULL_TREE;
10760 TREE_USED (decl) = 1;
10762 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10763 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10764 SET_EXPR_LOCATION (t, loc);
10765 return add_stmt (t);
10769 /* Generate a computed goto statement to EXPR. LOC is the location of
10770 the GOTO. */
10772 tree
10773 c_finish_goto_ptr (location_t loc, tree expr)
10775 tree t;
10776 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10777 expr = c_fully_fold (expr, false, NULL);
10778 expr = convert (ptr_type_node, expr);
10779 t = build1 (GOTO_EXPR, void_type_node, expr);
10780 SET_EXPR_LOCATION (t, loc);
10781 return add_stmt (t);
10784 /* Generate a C `return' statement. RETVAL is the expression for what
10785 to return, or a null pointer for `return;' with no value. LOC is
10786 the location of the return statement, or the location of the expression,
10787 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10788 is the original type of RETVAL. */
10790 tree
10791 c_finish_return (location_t loc, tree retval, tree origtype)
10793 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10794 bool no_warning = false;
10795 bool npc = false;
10797 /* Use the expansion point to handle cases such as returning NULL
10798 in a function returning void. */
10799 location_t xloc = expansion_point_location_if_in_system_header (loc);
10801 if (TREE_THIS_VOLATILE (current_function_decl))
10802 warning_at (xloc, 0,
10803 "function declared %<noreturn%> has a %<return%> statement");
10805 if (retval)
10807 tree semantic_type = NULL_TREE;
10808 npc = null_pointer_constant_p (retval);
10809 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10811 semantic_type = TREE_TYPE (retval);
10812 retval = TREE_OPERAND (retval, 0);
10814 retval = c_fully_fold (retval, false, NULL);
10815 if (semantic_type
10816 && valtype != NULL_TREE
10817 && TREE_CODE (valtype) != VOID_TYPE)
10818 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10821 if (!retval)
10823 current_function_returns_null = 1;
10824 if ((warn_return_type >= 0 || flag_isoc99)
10825 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10827 bool warned_here;
10828 if (flag_isoc99)
10829 warned_here = pedwarn
10830 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10831 "%<return%> with no value, in function returning non-void");
10832 else
10833 warned_here = warning_at
10834 (loc, OPT_Wreturn_type,
10835 "%<return%> with no value, in function returning non-void");
10836 no_warning = true;
10837 if (warned_here)
10838 inform (DECL_SOURCE_LOCATION (current_function_decl),
10839 "declared here");
10842 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10844 current_function_returns_null = 1;
10845 bool warned_here;
10846 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10847 warned_here = pedwarn
10848 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10849 "%<return%> with a value, in function returning void");
10850 else
10851 warned_here = pedwarn
10852 (xloc, OPT_Wpedantic, "ISO C forbids "
10853 "%<return%> with expression, in function returning void");
10854 if (warned_here)
10855 inform (DECL_SOURCE_LOCATION (current_function_decl),
10856 "declared here");
10858 else
10860 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10861 retval, origtype, ic_return,
10862 npc, NULL_TREE, NULL_TREE, 0);
10863 tree res = DECL_RESULT (current_function_decl);
10864 tree inner;
10865 bool save;
10867 current_function_returns_value = 1;
10868 if (t == error_mark_node)
10869 return NULL_TREE;
10871 save = in_late_binary_op;
10872 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10873 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10874 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10875 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10876 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10877 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10878 in_late_binary_op = true;
10879 inner = t = convert (TREE_TYPE (res), t);
10880 in_late_binary_op = save;
10882 /* Strip any conversions, additions, and subtractions, and see if
10883 we are returning the address of a local variable. Warn if so. */
10884 while (1)
10886 switch (TREE_CODE (inner))
10888 CASE_CONVERT:
10889 case NON_LVALUE_EXPR:
10890 case PLUS_EXPR:
10891 case POINTER_PLUS_EXPR:
10892 inner = TREE_OPERAND (inner, 0);
10893 continue;
10895 case MINUS_EXPR:
10896 /* If the second operand of the MINUS_EXPR has a pointer
10897 type (or is converted from it), this may be valid, so
10898 don't give a warning. */
10900 tree op1 = TREE_OPERAND (inner, 1);
10902 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10903 && (CONVERT_EXPR_P (op1)
10904 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10905 op1 = TREE_OPERAND (op1, 0);
10907 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10908 break;
10910 inner = TREE_OPERAND (inner, 0);
10911 continue;
10914 case ADDR_EXPR:
10915 inner = TREE_OPERAND (inner, 0);
10917 while (REFERENCE_CLASS_P (inner)
10918 && !INDIRECT_REF_P (inner))
10919 inner = TREE_OPERAND (inner, 0);
10921 if (DECL_P (inner)
10922 && !DECL_EXTERNAL (inner)
10923 && !TREE_STATIC (inner)
10924 && DECL_CONTEXT (inner) == current_function_decl
10925 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10927 if (TREE_CODE (inner) == LABEL_DECL)
10928 warning_at (loc, OPT_Wreturn_local_addr,
10929 "function returns address of label");
10930 else
10932 warning_at (loc, OPT_Wreturn_local_addr,
10933 "function returns address of local variable");
10934 tree zero = build_zero_cst (TREE_TYPE (res));
10935 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10938 break;
10940 default:
10941 break;
10944 break;
10947 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10948 SET_EXPR_LOCATION (retval, loc);
10950 if (warn_sequence_point)
10951 verify_sequence_points (retval);
10954 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10955 TREE_NO_WARNING (ret_stmt) |= no_warning;
10956 return add_stmt (ret_stmt);
10959 struct c_switch {
10960 /* The SWITCH_STMT being built. */
10961 tree switch_stmt;
10963 /* The original type of the testing expression, i.e. before the
10964 default conversion is applied. */
10965 tree orig_type;
10967 /* A splay-tree mapping the low element of a case range to the high
10968 element, or NULL_TREE if there is no high element. Used to
10969 determine whether or not a new case label duplicates an old case
10970 label. We need a tree, rather than simply a hash table, because
10971 of the GNU case range extension. */
10972 splay_tree cases;
10974 /* The bindings at the point of the switch. This is used for
10975 warnings crossing decls when branching to a case label. */
10976 struct c_spot_bindings *bindings;
10978 /* Whether the switch includes any break statements. */
10979 bool break_stmt_seen_p;
10981 /* The next node on the stack. */
10982 struct c_switch *next;
10984 /* Remember whether the controlling expression had boolean type
10985 before integer promotions for the sake of -Wswitch-bool. */
10986 bool bool_cond_p;
10989 /* A stack of the currently active switch statements. The innermost
10990 switch statement is on the top of the stack. There is no need to
10991 mark the stack for garbage collection because it is only active
10992 during the processing of the body of a function, and we never
10993 collect at that point. */
10995 struct c_switch *c_switch_stack;
10997 /* Start a C switch statement, testing expression EXP. Return the new
10998 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
10999 SWITCH_COND_LOC is the location of the switch's condition.
11000 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11002 tree
11003 c_start_switch (location_t switch_loc,
11004 location_t switch_cond_loc,
11005 tree exp, bool explicit_cast_p)
11007 tree orig_type = error_mark_node;
11008 bool bool_cond_p = false;
11009 struct c_switch *cs;
11011 if (exp != error_mark_node)
11013 orig_type = TREE_TYPE (exp);
11015 if (!INTEGRAL_TYPE_P (orig_type))
11017 if (orig_type != error_mark_node)
11019 error_at (switch_cond_loc, "switch quantity not an integer");
11020 orig_type = error_mark_node;
11022 exp = integer_zero_node;
11024 else
11026 tree type = TYPE_MAIN_VARIANT (orig_type);
11027 tree e = exp;
11029 /* Warn if the condition has boolean value. */
11030 while (TREE_CODE (e) == COMPOUND_EXPR)
11031 e = TREE_OPERAND (e, 1);
11033 if ((TREE_CODE (type) == BOOLEAN_TYPE
11034 || truth_value_p (TREE_CODE (e)))
11035 /* Explicit cast to int suppresses this warning. */
11036 && !(TREE_CODE (type) == INTEGER_TYPE
11037 && explicit_cast_p))
11038 bool_cond_p = true;
11040 if (!in_system_header_at (input_location)
11041 && (type == long_integer_type_node
11042 || type == long_unsigned_type_node))
11043 warning_at (switch_cond_loc,
11044 OPT_Wtraditional, "%<long%> switch expression not "
11045 "converted to %<int%> in ISO C");
11047 exp = c_fully_fold (exp, false, NULL);
11048 exp = default_conversion (exp);
11050 if (warn_sequence_point)
11051 verify_sequence_points (exp);
11055 /* Add this new SWITCH_STMT to the stack. */
11056 cs = XNEW (struct c_switch);
11057 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11058 NULL_TREE, orig_type, NULL_TREE);
11059 cs->orig_type = orig_type;
11060 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11061 cs->bindings = c_get_switch_bindings ();
11062 cs->break_stmt_seen_p = false;
11063 cs->bool_cond_p = bool_cond_p;
11064 cs->next = c_switch_stack;
11065 c_switch_stack = cs;
11067 return add_stmt (cs->switch_stmt);
11070 /* Process a case label at location LOC. */
11072 tree
11073 do_case (location_t loc, tree low_value, tree high_value)
11075 tree label = NULL_TREE;
11077 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11079 low_value = c_fully_fold (low_value, false, NULL);
11080 if (TREE_CODE (low_value) == INTEGER_CST)
11081 pedwarn (loc, OPT_Wpedantic,
11082 "case label is not an integer constant expression");
11085 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11087 high_value = c_fully_fold (high_value, false, NULL);
11088 if (TREE_CODE (high_value) == INTEGER_CST)
11089 pedwarn (input_location, OPT_Wpedantic,
11090 "case label is not an integer constant expression");
11093 if (c_switch_stack == NULL)
11095 if (low_value)
11096 error_at (loc, "case label not within a switch statement");
11097 else
11098 error_at (loc, "%<default%> label not within a switch statement");
11099 return NULL_TREE;
11102 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11103 EXPR_LOCATION (c_switch_stack->switch_stmt),
11104 loc))
11105 return NULL_TREE;
11107 label = c_add_case_label (loc, c_switch_stack->cases,
11108 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11109 low_value, high_value);
11110 if (label == error_mark_node)
11111 label = NULL_TREE;
11112 return label;
11115 /* Finish the switch statement. TYPE is the original type of the
11116 controlling expression of the switch, or NULL_TREE. */
11118 void
11119 c_finish_switch (tree body, tree type)
11121 struct c_switch *cs = c_switch_stack;
11122 location_t switch_location;
11124 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11126 /* Emit warnings as needed. */
11127 switch_location = EXPR_LOCATION (cs->switch_stmt);
11128 c_do_switch_warnings (cs->cases, switch_location,
11129 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11130 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11131 if (c_switch_covers_all_cases_p (cs->cases,
11132 SWITCH_STMT_TYPE (cs->switch_stmt)))
11133 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11134 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11136 /* Pop the stack. */
11137 c_switch_stack = cs->next;
11138 splay_tree_delete (cs->cases);
11139 c_release_switch_bindings (cs->bindings);
11140 XDELETE (cs);
11143 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11144 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11145 may be null. */
11147 void
11148 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11149 tree else_block)
11151 tree stmt;
11153 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11154 SET_EXPR_LOCATION (stmt, if_locus);
11155 add_stmt (stmt);
11158 tree
11159 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11161 /* In switch statements break is sometimes stylistically used after
11162 a return statement. This can lead to spurious warnings about
11163 control reaching the end of a non-void function when it is
11164 inlined. Note that we are calling block_may_fallthru with
11165 language specific tree nodes; this works because
11166 block_may_fallthru returns true when given something it does not
11167 understand. */
11168 bool skip = !block_may_fallthru (cur_stmt_list);
11170 if (is_break)
11171 switch (in_statement)
11173 case 0:
11174 error_at (loc, "break statement not within loop or switch");
11175 return NULL_TREE;
11176 case IN_OMP_BLOCK:
11177 error_at (loc, "invalid exit from OpenMP structured block");
11178 return NULL_TREE;
11179 case IN_OMP_FOR:
11180 error_at (loc, "break statement used with OpenMP for loop");
11181 return NULL_TREE;
11182 case IN_ITERATION_STMT:
11183 case IN_OBJC_FOREACH:
11184 break;
11185 default:
11186 gcc_assert (in_statement & IN_SWITCH_STMT);
11187 c_switch_stack->break_stmt_seen_p = true;
11188 break;
11190 else
11191 switch (in_statement & ~IN_SWITCH_STMT)
11193 case 0:
11194 error_at (loc, "continue statement not within a loop");
11195 return NULL_TREE;
11196 case IN_OMP_BLOCK:
11197 error_at (loc, "invalid exit from OpenMP structured block");
11198 return NULL_TREE;
11199 case IN_ITERATION_STMT:
11200 case IN_OMP_FOR:
11201 case IN_OBJC_FOREACH:
11202 break;
11203 default:
11204 gcc_unreachable ();
11207 if (skip)
11208 return NULL_TREE;
11209 else if (in_statement & IN_OBJC_FOREACH)
11211 /* The foreach expander produces low-level code using gotos instead
11212 of a structured loop construct. */
11213 gcc_assert (label);
11214 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11216 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11219 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11221 static void
11222 emit_side_effect_warnings (location_t loc, tree expr)
11224 maybe_warn_nodiscard (loc, expr);
11225 if (!warn_unused_value)
11226 return;
11227 if (expr == error_mark_node)
11229 else if (!TREE_SIDE_EFFECTS (expr))
11231 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
11232 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11234 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11236 tree r = expr;
11237 location_t cloc = loc;
11238 while (TREE_CODE (r) == COMPOUND_EXPR)
11240 if (EXPR_HAS_LOCATION (r))
11241 cloc = EXPR_LOCATION (r);
11242 r = TREE_OPERAND (r, 1);
11244 if (!TREE_SIDE_EFFECTS (r)
11245 && !VOID_TYPE_P (TREE_TYPE (r))
11246 && !CONVERT_EXPR_P (r)
11247 && !TREE_NO_WARNING (r)
11248 && !TREE_NO_WARNING (expr))
11249 warning_at (cloc, OPT_Wunused_value,
11250 "right-hand operand of comma expression has no effect");
11252 else
11253 warn_if_unused_value (expr, loc);
11256 /* Process an expression as if it were a complete statement. Emit
11257 diagnostics, but do not call ADD_STMT. LOC is the location of the
11258 statement. */
11260 tree
11261 c_process_expr_stmt (location_t loc, tree expr)
11263 tree exprv;
11265 if (!expr)
11266 return NULL_TREE;
11268 expr = c_fully_fold (expr, false, NULL);
11270 if (warn_sequence_point)
11271 verify_sequence_points (expr);
11273 if (TREE_TYPE (expr) != error_mark_node
11274 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11275 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11276 error_at (loc, "expression statement has incomplete type");
11278 /* If we're not processing a statement expression, warn about unused values.
11279 Warnings for statement expressions will be emitted later, once we figure
11280 out which is the result. */
11281 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11282 && (warn_unused_value || warn_unused_result))
11283 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11285 exprv = expr;
11286 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11287 exprv = TREE_OPERAND (exprv, 1);
11288 while (CONVERT_EXPR_P (exprv))
11289 exprv = TREE_OPERAND (exprv, 0);
11290 if (DECL_P (exprv)
11291 || handled_component_p (exprv)
11292 || TREE_CODE (exprv) == ADDR_EXPR)
11293 mark_exp_read (exprv);
11295 /* If the expression is not of a type to which we cannot assign a line
11296 number, wrap the thing in a no-op NOP_EXPR. */
11297 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11299 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11300 SET_EXPR_LOCATION (expr, loc);
11303 return expr;
11306 /* Emit an expression as a statement. LOC is the location of the
11307 expression. */
11309 tree
11310 c_finish_expr_stmt (location_t loc, tree expr)
11312 if (expr)
11313 return add_stmt (c_process_expr_stmt (loc, expr));
11314 else
11315 return NULL;
11318 /* Do the opposite and emit a statement as an expression. To begin,
11319 create a new binding level and return it. */
11321 tree
11322 c_begin_stmt_expr (void)
11324 tree ret;
11326 /* We must force a BLOCK for this level so that, if it is not expanded
11327 later, there is a way to turn off the entire subtree of blocks that
11328 are contained in it. */
11329 keep_next_level ();
11330 ret = c_begin_compound_stmt (true);
11332 c_bindings_start_stmt_expr (c_switch_stack == NULL
11333 ? NULL
11334 : c_switch_stack->bindings);
11336 /* Mark the current statement list as belonging to a statement list. */
11337 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11339 return ret;
11342 /* LOC is the location of the compound statement to which this body
11343 belongs. */
11345 tree
11346 c_finish_stmt_expr (location_t loc, tree body)
11348 tree last, type, tmp, val;
11349 tree *last_p;
11351 body = c_end_compound_stmt (loc, body, true);
11353 c_bindings_end_stmt_expr (c_switch_stack == NULL
11354 ? NULL
11355 : c_switch_stack->bindings);
11357 /* Locate the last statement in BODY. See c_end_compound_stmt
11358 about always returning a BIND_EXPR. */
11359 last_p = &BIND_EXPR_BODY (body);
11360 last = BIND_EXPR_BODY (body);
11362 continue_searching:
11363 if (TREE_CODE (last) == STATEMENT_LIST)
11365 tree_stmt_iterator l = tsi_last (last);
11367 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11368 tsi_prev (&l);
11370 /* This can happen with degenerate cases like ({ }). No value. */
11371 if (tsi_end_p (l))
11372 return body;
11374 /* If we're supposed to generate side effects warnings, process
11375 all of the statements except the last. */
11376 if (warn_unused_value || warn_unused_result)
11378 for (tree_stmt_iterator i = tsi_start (last);
11379 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11381 location_t tloc;
11382 tree t = tsi_stmt (i);
11384 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11385 emit_side_effect_warnings (tloc, t);
11388 last_p = tsi_stmt_ptr (l);
11389 last = *last_p;
11392 /* If the end of the list is exception related, then the list was split
11393 by a call to push_cleanup. Continue searching. */
11394 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11395 || TREE_CODE (last) == TRY_CATCH_EXPR)
11397 last_p = &TREE_OPERAND (last, 0);
11398 last = *last_p;
11399 goto continue_searching;
11402 if (last == error_mark_node)
11403 return last;
11405 /* In the case that the BIND_EXPR is not necessary, return the
11406 expression out from inside it. */
11407 if ((last == BIND_EXPR_BODY (body)
11408 /* Skip nested debug stmts. */
11409 || last == expr_first (BIND_EXPR_BODY (body)))
11410 && BIND_EXPR_VARS (body) == NULL)
11412 /* Even if this looks constant, do not allow it in a constant
11413 expression. */
11414 last = c_wrap_maybe_const (last, true);
11415 /* Do not warn if the return value of a statement expression is
11416 unused. */
11417 TREE_NO_WARNING (last) = 1;
11418 return last;
11421 /* Extract the type of said expression. */
11422 type = TREE_TYPE (last);
11424 /* If we're not returning a value at all, then the BIND_EXPR that
11425 we already have is a fine expression to return. */
11426 if (!type || VOID_TYPE_P (type))
11427 return body;
11429 /* Now that we've located the expression containing the value, it seems
11430 silly to make voidify_wrapper_expr repeat the process. Create a
11431 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11432 tmp = create_tmp_var_raw (type);
11434 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11435 tree_expr_nonnegative_p giving up immediately. */
11436 val = last;
11437 if (TREE_CODE (val) == NOP_EXPR
11438 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11439 val = TREE_OPERAND (val, 0);
11441 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11442 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11445 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11446 SET_EXPR_LOCATION (t, loc);
11447 return t;
11451 /* Begin and end compound statements. This is as simple as pushing
11452 and popping new statement lists from the tree. */
11454 tree
11455 c_begin_compound_stmt (bool do_scope)
11457 tree stmt = push_stmt_list ();
11458 if (do_scope)
11459 push_scope ();
11460 return stmt;
11463 /* End a compound statement. STMT is the statement. LOC is the
11464 location of the compound statement-- this is usually the location
11465 of the opening brace. */
11467 tree
11468 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11470 tree block = NULL;
11472 if (do_scope)
11474 if (c_dialect_objc ())
11475 objc_clear_super_receiver ();
11476 block = pop_scope ();
11479 stmt = pop_stmt_list (stmt);
11480 stmt = c_build_bind_expr (loc, block, stmt);
11482 /* If this compound statement is nested immediately inside a statement
11483 expression, then force a BIND_EXPR to be created. Otherwise we'll
11484 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11485 STATEMENT_LISTs merge, and thus we can lose track of what statement
11486 was really last. */
11487 if (building_stmt_list_p ()
11488 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11489 && TREE_CODE (stmt) != BIND_EXPR)
11491 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11492 TREE_SIDE_EFFECTS (stmt) = 1;
11493 SET_EXPR_LOCATION (stmt, loc);
11496 return stmt;
11499 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11500 when the current scope is exited. EH_ONLY is true when this is not
11501 meant to apply to normal control flow transfer. */
11503 void
11504 push_cleanup (tree decl, tree cleanup, bool eh_only)
11506 enum tree_code code;
11507 tree stmt, list;
11508 bool stmt_expr;
11510 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11511 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11512 add_stmt (stmt);
11513 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11514 list = push_stmt_list ();
11515 TREE_OPERAND (stmt, 0) = list;
11516 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11519 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11520 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11522 static tree
11523 build_vec_cmp (tree_code code, tree type,
11524 tree arg0, tree arg1)
11526 tree zero_vec = build_zero_cst (type);
11527 tree minus_one_vec = build_minus_one_cst (type);
11528 tree cmp_type = truth_type_for (type);
11529 tree cmp = build2 (code, cmp_type, arg0, arg1);
11530 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11533 /* Build a binary-operation expression without default conversions.
11534 CODE is the kind of expression to build.
11535 LOCATION is the operator's location.
11536 This function differs from `build' in several ways:
11537 the data type of the result is computed and recorded in it,
11538 warnings are generated if arg data types are invalid,
11539 special handling for addition and subtraction of pointers is known,
11540 and some optimization is done (operations on narrow ints
11541 are done in the narrower type when that gives the same result).
11542 Constant folding is also done before the result is returned.
11544 Note that the operands will never have enumeral types, or function
11545 or array types, because either they will have the default conversions
11546 performed or they have both just been converted to some other type in which
11547 the arithmetic is to be done. */
11549 tree
11550 build_binary_op (location_t location, enum tree_code code,
11551 tree orig_op0, tree orig_op1, bool convert_p)
11553 tree type0, type1, orig_type0, orig_type1;
11554 tree eptype;
11555 enum tree_code code0, code1;
11556 tree op0, op1;
11557 tree ret = error_mark_node;
11558 const char *invalid_op_diag;
11559 bool op0_int_operands, op1_int_operands;
11560 bool int_const, int_const_or_overflow, int_operands;
11562 /* Expression code to give to the expression when it is built.
11563 Normally this is CODE, which is what the caller asked for,
11564 but in some special cases we change it. */
11565 enum tree_code resultcode = code;
11567 /* Data type in which the computation is to be performed.
11568 In the simplest cases this is the common type of the arguments. */
11569 tree result_type = NULL;
11571 /* When the computation is in excess precision, the type of the
11572 final EXCESS_PRECISION_EXPR. */
11573 tree semantic_result_type = NULL;
11575 /* Nonzero means operands have already been type-converted
11576 in whatever way is necessary.
11577 Zero means they need to be converted to RESULT_TYPE. */
11578 int converted = 0;
11580 /* Nonzero means create the expression with this type, rather than
11581 RESULT_TYPE. */
11582 tree build_type = NULL_TREE;
11584 /* Nonzero means after finally constructing the expression
11585 convert it to this type. */
11586 tree final_type = NULL_TREE;
11588 /* Nonzero if this is an operation like MIN or MAX which can
11589 safely be computed in short if both args are promoted shorts.
11590 Also implies COMMON.
11591 -1 indicates a bitwise operation; this makes a difference
11592 in the exact conditions for when it is safe to do the operation
11593 in a narrower mode. */
11594 int shorten = 0;
11596 /* Nonzero if this is a comparison operation;
11597 if both args are promoted shorts, compare the original shorts.
11598 Also implies COMMON. */
11599 int short_compare = 0;
11601 /* Nonzero if this is a right-shift operation, which can be computed on the
11602 original short and then promoted if the operand is a promoted short. */
11603 int short_shift = 0;
11605 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11606 int common = 0;
11608 /* True means types are compatible as far as ObjC is concerned. */
11609 bool objc_ok;
11611 /* True means this is an arithmetic operation that may need excess
11612 precision. */
11613 bool may_need_excess_precision;
11615 /* True means this is a boolean operation that converts both its
11616 operands to truth-values. */
11617 bool boolean_op = false;
11619 /* Remember whether we're doing / or %. */
11620 bool doing_div_or_mod = false;
11622 /* Remember whether we're doing << or >>. */
11623 bool doing_shift = false;
11625 /* Tree holding instrumentation expression. */
11626 tree instrument_expr = NULL;
11628 if (location == UNKNOWN_LOCATION)
11629 location = input_location;
11631 op0 = orig_op0;
11632 op1 = orig_op1;
11634 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11635 if (op0_int_operands)
11636 op0 = remove_c_maybe_const_expr (op0);
11637 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11638 if (op1_int_operands)
11639 op1 = remove_c_maybe_const_expr (op1);
11640 int_operands = (op0_int_operands && op1_int_operands);
11641 if (int_operands)
11643 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11644 && TREE_CODE (orig_op1) == INTEGER_CST);
11645 int_const = (int_const_or_overflow
11646 && !TREE_OVERFLOW (orig_op0)
11647 && !TREE_OVERFLOW (orig_op1));
11649 else
11650 int_const = int_const_or_overflow = false;
11652 /* Do not apply default conversion in mixed vector/scalar expression. */
11653 if (convert_p
11654 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11656 op0 = default_conversion (op0);
11657 op1 = default_conversion (op1);
11660 orig_type0 = type0 = TREE_TYPE (op0);
11662 orig_type1 = type1 = TREE_TYPE (op1);
11664 /* The expression codes of the data types of the arguments tell us
11665 whether the arguments are integers, floating, pointers, etc. */
11666 code0 = TREE_CODE (type0);
11667 code1 = TREE_CODE (type1);
11669 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11670 STRIP_TYPE_NOPS (op0);
11671 STRIP_TYPE_NOPS (op1);
11673 /* If an error was already reported for one of the arguments,
11674 avoid reporting another error. */
11676 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11677 return error_mark_node;
11679 if (code0 == POINTER_TYPE
11680 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11681 return error_mark_node;
11683 if (code1 == POINTER_TYPE
11684 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11685 return error_mark_node;
11687 if ((invalid_op_diag
11688 = targetm.invalid_binary_op (code, type0, type1)))
11690 error_at (location, invalid_op_diag);
11691 return error_mark_node;
11694 switch (code)
11696 case PLUS_EXPR:
11697 case MINUS_EXPR:
11698 case MULT_EXPR:
11699 case TRUNC_DIV_EXPR:
11700 case CEIL_DIV_EXPR:
11701 case FLOOR_DIV_EXPR:
11702 case ROUND_DIV_EXPR:
11703 case EXACT_DIV_EXPR:
11704 may_need_excess_precision = true;
11705 break;
11707 case EQ_EXPR:
11708 case NE_EXPR:
11709 case LE_EXPR:
11710 case GE_EXPR:
11711 case LT_EXPR:
11712 case GT_EXPR:
11713 /* Excess precision for implicit conversions of integers to
11714 floating point in C11 and later. */
11715 may_need_excess_precision = (flag_isoc11
11716 && (ANY_INTEGRAL_TYPE_P (type0)
11717 || ANY_INTEGRAL_TYPE_P (type1)));
11718 break;
11720 default:
11721 may_need_excess_precision = false;
11722 break;
11724 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11726 op0 = TREE_OPERAND (op0, 0);
11727 type0 = TREE_TYPE (op0);
11729 else if (may_need_excess_precision
11730 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11732 type0 = eptype;
11733 op0 = convert (eptype, op0);
11735 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11737 op1 = TREE_OPERAND (op1, 0);
11738 type1 = TREE_TYPE (op1);
11740 else if (may_need_excess_precision
11741 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11743 type1 = eptype;
11744 op1 = convert (eptype, op1);
11747 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11749 /* In case when one of the operands of the binary operation is
11750 a vector and another is a scalar -- convert scalar to vector. */
11751 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11752 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11754 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11755 true);
11757 switch (convert_flag)
11759 case stv_error:
11760 return error_mark_node;
11761 case stv_firstarg:
11763 bool maybe_const = true;
11764 tree sc;
11765 sc = c_fully_fold (op0, false, &maybe_const);
11766 sc = save_expr (sc);
11767 sc = convert (TREE_TYPE (type1), sc);
11768 op0 = build_vector_from_val (type1, sc);
11769 if (!maybe_const)
11770 op0 = c_wrap_maybe_const (op0, true);
11771 orig_type0 = type0 = TREE_TYPE (op0);
11772 code0 = TREE_CODE (type0);
11773 converted = 1;
11774 break;
11776 case stv_secondarg:
11778 bool maybe_const = true;
11779 tree sc;
11780 sc = c_fully_fold (op1, false, &maybe_const);
11781 sc = save_expr (sc);
11782 sc = convert (TREE_TYPE (type0), sc);
11783 op1 = build_vector_from_val (type0, sc);
11784 if (!maybe_const)
11785 op1 = c_wrap_maybe_const (op1, true);
11786 orig_type1 = type1 = TREE_TYPE (op1);
11787 code1 = TREE_CODE (type1);
11788 converted = 1;
11789 break;
11791 default:
11792 break;
11796 switch (code)
11798 case PLUS_EXPR:
11799 /* Handle the pointer + int case. */
11800 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11802 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11803 goto return_build_binary_op;
11805 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11807 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11808 goto return_build_binary_op;
11810 else
11811 common = 1;
11812 break;
11814 case MINUS_EXPR:
11815 /* Subtraction of two similar pointers.
11816 We must subtract them as integers, then divide by object size. */
11817 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11818 && comp_target_types (location, type0, type1))
11820 ret = pointer_diff (location, op0, op1, &instrument_expr);
11821 goto return_build_binary_op;
11823 /* Handle pointer minus int. Just like pointer plus int. */
11824 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11826 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11827 goto return_build_binary_op;
11829 else
11830 common = 1;
11831 break;
11833 case MULT_EXPR:
11834 common = 1;
11835 break;
11837 case TRUNC_DIV_EXPR:
11838 case CEIL_DIV_EXPR:
11839 case FLOOR_DIV_EXPR:
11840 case ROUND_DIV_EXPR:
11841 case EXACT_DIV_EXPR:
11842 doing_div_or_mod = true;
11843 warn_for_div_by_zero (location, op1);
11845 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11846 || code0 == FIXED_POINT_TYPE
11847 || code0 == COMPLEX_TYPE
11848 || gnu_vector_type_p (type0))
11849 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11850 || code1 == FIXED_POINT_TYPE
11851 || code1 == COMPLEX_TYPE
11852 || gnu_vector_type_p (type1)))
11854 enum tree_code tcode0 = code0, tcode1 = code1;
11856 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11857 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11858 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11859 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11861 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11862 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11863 resultcode = RDIV_EXPR;
11864 else
11865 /* Although it would be tempting to shorten always here, that
11866 loses on some targets, since the modulo instruction is
11867 undefined if the quotient can't be represented in the
11868 computation mode. We shorten only if unsigned or if
11869 dividing by something we know != -1. */
11870 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11871 || (TREE_CODE (op1) == INTEGER_CST
11872 && !integer_all_onesp (op1)));
11873 common = 1;
11875 break;
11877 case BIT_AND_EXPR:
11878 case BIT_IOR_EXPR:
11879 case BIT_XOR_EXPR:
11880 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11881 shorten = -1;
11882 /* Allow vector types which are not floating point types. */
11883 else if (gnu_vector_type_p (type0)
11884 && gnu_vector_type_p (type1)
11885 && !VECTOR_FLOAT_TYPE_P (type0)
11886 && !VECTOR_FLOAT_TYPE_P (type1))
11887 common = 1;
11888 break;
11890 case TRUNC_MOD_EXPR:
11891 case FLOOR_MOD_EXPR:
11892 doing_div_or_mod = true;
11893 warn_for_div_by_zero (location, op1);
11895 if (gnu_vector_type_p (type0)
11896 && gnu_vector_type_p (type1)
11897 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11898 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11899 common = 1;
11900 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11902 /* Although it would be tempting to shorten always here, that loses
11903 on some targets, since the modulo instruction is undefined if the
11904 quotient can't be represented in the computation mode. We shorten
11905 only if unsigned or if dividing by something we know != -1. */
11906 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11907 || (TREE_CODE (op1) == INTEGER_CST
11908 && !integer_all_onesp (op1)));
11909 common = 1;
11911 break;
11913 case TRUTH_ANDIF_EXPR:
11914 case TRUTH_ORIF_EXPR:
11915 case TRUTH_AND_EXPR:
11916 case TRUTH_OR_EXPR:
11917 case TRUTH_XOR_EXPR:
11918 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11919 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11920 || code0 == FIXED_POINT_TYPE)
11921 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11922 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11923 || code1 == FIXED_POINT_TYPE))
11925 /* Result of these operations is always an int,
11926 but that does not mean the operands should be
11927 converted to ints! */
11928 result_type = integer_type_node;
11929 if (op0_int_operands)
11931 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11932 op0 = remove_c_maybe_const_expr (op0);
11934 else
11935 op0 = c_objc_common_truthvalue_conversion (location, op0);
11936 if (op1_int_operands)
11938 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11939 op1 = remove_c_maybe_const_expr (op1);
11941 else
11942 op1 = c_objc_common_truthvalue_conversion (location, op1);
11943 converted = 1;
11944 boolean_op = true;
11946 if (code == TRUTH_ANDIF_EXPR)
11948 int_const_or_overflow = (int_operands
11949 && TREE_CODE (orig_op0) == INTEGER_CST
11950 && (op0 == truthvalue_false_node
11951 || TREE_CODE (orig_op1) == INTEGER_CST));
11952 int_const = (int_const_or_overflow
11953 && !TREE_OVERFLOW (orig_op0)
11954 && (op0 == truthvalue_false_node
11955 || !TREE_OVERFLOW (orig_op1)));
11957 else if (code == TRUTH_ORIF_EXPR)
11959 int_const_or_overflow = (int_operands
11960 && TREE_CODE (orig_op0) == INTEGER_CST
11961 && (op0 == truthvalue_true_node
11962 || TREE_CODE (orig_op1) == INTEGER_CST));
11963 int_const = (int_const_or_overflow
11964 && !TREE_OVERFLOW (orig_op0)
11965 && (op0 == truthvalue_true_node
11966 || !TREE_OVERFLOW (orig_op1)));
11968 break;
11970 /* Shift operations: result has same type as first operand;
11971 always convert second operand to int.
11972 Also set SHORT_SHIFT if shifting rightward. */
11974 case RSHIFT_EXPR:
11975 if (gnu_vector_type_p (type0)
11976 && gnu_vector_type_p (type1)
11977 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11978 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11979 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11980 TYPE_VECTOR_SUBPARTS (type1)))
11982 result_type = type0;
11983 converted = 1;
11985 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11986 || (gnu_vector_type_p (type0)
11987 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11988 && code1 == INTEGER_TYPE)
11990 doing_shift = true;
11991 if (TREE_CODE (op1) == INTEGER_CST)
11993 if (tree_int_cst_sgn (op1) < 0)
11995 int_const = false;
11996 if (c_inhibit_evaluation_warnings == 0)
11997 warning_at (location, OPT_Wshift_count_negative,
11998 "right shift count is negative");
12000 else if (code0 == VECTOR_TYPE)
12002 if (compare_tree_int (op1,
12003 TYPE_PRECISION (TREE_TYPE (type0)))
12004 >= 0)
12006 int_const = false;
12007 if (c_inhibit_evaluation_warnings == 0)
12008 warning_at (location, OPT_Wshift_count_overflow,
12009 "right shift count >= width of vector element");
12012 else
12014 if (!integer_zerop (op1))
12015 short_shift = 1;
12017 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12019 int_const = false;
12020 if (c_inhibit_evaluation_warnings == 0)
12021 warning_at (location, OPT_Wshift_count_overflow,
12022 "right shift count >= width of type");
12027 /* Use the type of the value to be shifted. */
12028 result_type = type0;
12029 /* Avoid converting op1 to result_type later. */
12030 converted = 1;
12032 break;
12034 case LSHIFT_EXPR:
12035 if (gnu_vector_type_p (type0)
12036 && gnu_vector_type_p (type1)
12037 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12038 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12039 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12040 TYPE_VECTOR_SUBPARTS (type1)))
12042 result_type = type0;
12043 converted = 1;
12045 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12046 || (gnu_vector_type_p (type0)
12047 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12048 && code1 == INTEGER_TYPE)
12050 doing_shift = true;
12051 if (TREE_CODE (op0) == INTEGER_CST
12052 && tree_int_cst_sgn (op0) < 0)
12054 /* Don't reject a left shift of a negative value in a context
12055 where a constant expression is needed in C90. */
12056 if (flag_isoc99)
12057 int_const = false;
12058 if (c_inhibit_evaluation_warnings == 0)
12059 warning_at (location, OPT_Wshift_negative_value,
12060 "left shift of negative value");
12062 if (TREE_CODE (op1) == INTEGER_CST)
12064 if (tree_int_cst_sgn (op1) < 0)
12066 int_const = false;
12067 if (c_inhibit_evaluation_warnings == 0)
12068 warning_at (location, OPT_Wshift_count_negative,
12069 "left shift count is negative");
12071 else if (code0 == VECTOR_TYPE)
12073 if (compare_tree_int (op1,
12074 TYPE_PRECISION (TREE_TYPE (type0)))
12075 >= 0)
12077 int_const = false;
12078 if (c_inhibit_evaluation_warnings == 0)
12079 warning_at (location, OPT_Wshift_count_overflow,
12080 "left shift count >= width of vector element");
12083 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12085 int_const = false;
12086 if (c_inhibit_evaluation_warnings == 0)
12087 warning_at (location, OPT_Wshift_count_overflow,
12088 "left shift count >= width of type");
12090 else if (TREE_CODE (op0) == INTEGER_CST
12091 && maybe_warn_shift_overflow (location, op0, op1)
12092 && flag_isoc99)
12093 int_const = false;
12096 /* Use the type of the value to be shifted. */
12097 result_type = type0;
12098 /* Avoid converting op1 to result_type later. */
12099 converted = 1;
12101 break;
12103 case EQ_EXPR:
12104 case NE_EXPR:
12105 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12107 tree intt;
12108 if (!vector_types_compatible_elements_p (type0, type1))
12110 error_at (location, "comparing vectors with different "
12111 "element types");
12112 return error_mark_node;
12115 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12116 TYPE_VECTOR_SUBPARTS (type1)))
12118 error_at (location, "comparing vectors with different "
12119 "number of elements");
12120 return error_mark_node;
12123 /* It's not precisely specified how the usual arithmetic
12124 conversions apply to the vector types. Here, we use
12125 the unsigned type if one of the operands is signed and
12126 the other one is unsigned. */
12127 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12129 if (!TYPE_UNSIGNED (type0))
12130 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12131 else
12132 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12133 warning_at (location, OPT_Wsign_compare, "comparison between "
12134 "types %qT and %qT", type0, type1);
12137 /* Always construct signed integer vector type. */
12138 intt = c_common_type_for_size (GET_MODE_BITSIZE
12139 (SCALAR_TYPE_MODE
12140 (TREE_TYPE (type0))), 0);
12141 if (!intt)
12143 error_at (location, "could not find an integer type "
12144 "of the same size as %qT",
12145 TREE_TYPE (type0));
12146 return error_mark_node;
12148 result_type = build_opaque_vector_type (intt,
12149 TYPE_VECTOR_SUBPARTS (type0));
12150 converted = 1;
12151 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12152 goto return_build_binary_op;
12154 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12155 warning_at (location,
12156 OPT_Wfloat_equal,
12157 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12158 /* Result of comparison is always int,
12159 but don't convert the args to int! */
12160 build_type = integer_type_node;
12161 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12162 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12163 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12164 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12165 short_compare = 1;
12166 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12168 if (TREE_CODE (op0) == ADDR_EXPR
12169 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
12170 && !from_macro_expansion_at (location))
12172 if (code == EQ_EXPR)
12173 warning_at (location,
12174 OPT_Waddress,
12175 "the comparison will always evaluate as %<false%> "
12176 "for the address of %qD will never be NULL",
12177 TREE_OPERAND (op0, 0));
12178 else
12179 warning_at (location,
12180 OPT_Waddress,
12181 "the comparison will always evaluate as %<true%> "
12182 "for the address of %qD will never be NULL",
12183 TREE_OPERAND (op0, 0));
12185 result_type = type0;
12187 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12189 if (TREE_CODE (op1) == ADDR_EXPR
12190 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
12191 && !from_macro_expansion_at (location))
12193 if (code == EQ_EXPR)
12194 warning_at (location,
12195 OPT_Waddress,
12196 "the comparison will always evaluate as %<false%> "
12197 "for the address of %qD will never be NULL",
12198 TREE_OPERAND (op1, 0));
12199 else
12200 warning_at (location,
12201 OPT_Waddress,
12202 "the comparison will always evaluate as %<true%> "
12203 "for the address of %qD will never be NULL",
12204 TREE_OPERAND (op1, 0));
12206 result_type = type1;
12208 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12210 tree tt0 = TREE_TYPE (type0);
12211 tree tt1 = TREE_TYPE (type1);
12212 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12213 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12214 addr_space_t as_common = ADDR_SPACE_GENERIC;
12216 /* Anything compares with void *. void * compares with anything.
12217 Otherwise, the targets must be compatible
12218 and both must be object or both incomplete. */
12219 if (comp_target_types (location, type0, type1))
12220 result_type = common_pointer_type (type0, type1);
12221 else if (!addr_space_superset (as0, as1, &as_common))
12223 error_at (location, "comparison of pointers to "
12224 "disjoint address spaces");
12225 return error_mark_node;
12227 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12229 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12230 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12231 "comparison of %<void *%> with function pointer");
12233 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12235 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12236 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12237 "comparison of %<void *%> with function pointer");
12239 else
12240 /* Avoid warning about the volatile ObjC EH puts on decls. */
12241 if (!objc_ok)
12242 pedwarn (location, 0,
12243 "comparison of distinct pointer types lacks a cast");
12245 if (result_type == NULL_TREE)
12247 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12248 result_type = build_pointer_type
12249 (build_qualified_type (void_type_node, qual));
12252 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12254 result_type = type0;
12255 pedwarn (location, 0, "comparison between pointer and integer");
12257 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12259 result_type = type1;
12260 pedwarn (location, 0, "comparison between pointer and integer");
12262 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12263 || truth_value_p (TREE_CODE (orig_op0)))
12264 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12265 || truth_value_p (TREE_CODE (orig_op1))))
12266 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12267 break;
12269 case LE_EXPR:
12270 case GE_EXPR:
12271 case LT_EXPR:
12272 case GT_EXPR:
12273 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12275 tree intt;
12276 if (!vector_types_compatible_elements_p (type0, type1))
12278 error_at (location, "comparing vectors with different "
12279 "element types");
12280 return error_mark_node;
12283 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12284 TYPE_VECTOR_SUBPARTS (type1)))
12286 error_at (location, "comparing vectors with different "
12287 "number of elements");
12288 return error_mark_node;
12291 /* It's not precisely specified how the usual arithmetic
12292 conversions apply to the vector types. Here, we use
12293 the unsigned type if one of the operands is signed and
12294 the other one is unsigned. */
12295 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12297 if (!TYPE_UNSIGNED (type0))
12298 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12299 else
12300 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12301 warning_at (location, OPT_Wsign_compare, "comparison between "
12302 "types %qT and %qT", type0, type1);
12305 /* Always construct signed integer vector type. */
12306 intt = c_common_type_for_size (GET_MODE_BITSIZE
12307 (SCALAR_TYPE_MODE
12308 (TREE_TYPE (type0))), 0);
12309 if (!intt)
12311 error_at (location, "could not find an integer type "
12312 "of the same size as %qT",
12313 TREE_TYPE (type0));
12314 return error_mark_node;
12316 result_type = build_opaque_vector_type (intt,
12317 TYPE_VECTOR_SUBPARTS (type0));
12318 converted = 1;
12319 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12320 goto return_build_binary_op;
12322 build_type = integer_type_node;
12323 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12324 || code0 == FIXED_POINT_TYPE)
12325 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12326 || code1 == FIXED_POINT_TYPE))
12327 short_compare = 1;
12328 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12330 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12331 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12332 addr_space_t as_common;
12334 if (comp_target_types (location, type0, type1))
12336 result_type = common_pointer_type (type0, type1);
12337 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12338 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12339 pedwarn_c99 (location, OPT_Wpedantic,
12340 "comparison of complete and incomplete pointers");
12341 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12342 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12343 "ordered comparisons of pointers to functions");
12344 else if (null_pointer_constant_p (orig_op0)
12345 || null_pointer_constant_p (orig_op1))
12346 warning_at (location, OPT_Wextra,
12347 "ordered comparison of pointer with null pointer");
12350 else if (!addr_space_superset (as0, as1, &as_common))
12352 error_at (location, "comparison of pointers to "
12353 "disjoint address spaces");
12354 return error_mark_node;
12356 else
12358 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12359 result_type = build_pointer_type
12360 (build_qualified_type (void_type_node, qual));
12361 pedwarn (location, 0,
12362 "comparison of distinct pointer types lacks a cast");
12365 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12367 result_type = type0;
12368 if (pedantic)
12369 pedwarn (location, OPT_Wpedantic,
12370 "ordered comparison of pointer with integer zero");
12371 else if (extra_warnings)
12372 warning_at (location, OPT_Wextra,
12373 "ordered comparison of pointer with integer zero");
12375 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12377 result_type = type1;
12378 if (pedantic)
12379 pedwarn (location, OPT_Wpedantic,
12380 "ordered comparison of pointer with integer zero");
12381 else if (extra_warnings)
12382 warning_at (location, OPT_Wextra,
12383 "ordered comparison of pointer with integer zero");
12385 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12387 result_type = type0;
12388 pedwarn (location, 0, "comparison between pointer and integer");
12390 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12392 result_type = type1;
12393 pedwarn (location, 0, "comparison between pointer and integer");
12396 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12397 && current_function_decl != NULL_TREE
12398 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12400 op0 = save_expr (op0);
12401 op1 = save_expr (op1);
12403 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12404 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12407 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12408 || truth_value_p (TREE_CODE (orig_op0)))
12409 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12410 || truth_value_p (TREE_CODE (orig_op1))))
12411 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12412 break;
12414 default:
12415 gcc_unreachable ();
12418 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12419 return error_mark_node;
12421 if (gnu_vector_type_p (type0)
12422 && gnu_vector_type_p (type1)
12423 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12424 || !vector_types_compatible_elements_p (type0, type1)))
12426 gcc_rich_location richloc (location);
12427 maybe_range_label_for_tree_type_mismatch
12428 label_for_op0 (orig_op0, orig_op1),
12429 label_for_op1 (orig_op1, orig_op0);
12430 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12431 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12432 binary_op_error (&richloc, code, type0, type1);
12433 return error_mark_node;
12436 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12437 || code0 == FIXED_POINT_TYPE
12438 || gnu_vector_type_p (type0))
12440 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12441 || code1 == FIXED_POINT_TYPE
12442 || gnu_vector_type_p (type1)))
12444 bool first_complex = (code0 == COMPLEX_TYPE);
12445 bool second_complex = (code1 == COMPLEX_TYPE);
12446 int none_complex = (!first_complex && !second_complex);
12448 if (shorten || common || short_compare)
12450 result_type = c_common_type (type0, type1);
12451 do_warn_double_promotion (result_type, type0, type1,
12452 "implicit conversion from %qT to %qT "
12453 "to match other operand of binary "
12454 "expression",
12455 location);
12456 if (result_type == error_mark_node)
12457 return error_mark_node;
12460 if (first_complex != second_complex
12461 && (code == PLUS_EXPR
12462 || code == MINUS_EXPR
12463 || code == MULT_EXPR
12464 || (code == TRUNC_DIV_EXPR && first_complex))
12465 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12466 && flag_signed_zeros)
12468 /* An operation on mixed real/complex operands must be
12469 handled specially, but the language-independent code can
12470 more easily optimize the plain complex arithmetic if
12471 -fno-signed-zeros. */
12472 tree real_type = TREE_TYPE (result_type);
12473 tree real, imag;
12474 if (type0 != orig_type0 || type1 != orig_type1)
12476 gcc_assert (may_need_excess_precision && common);
12477 semantic_result_type = c_common_type (orig_type0, orig_type1);
12479 if (first_complex)
12481 if (TREE_TYPE (op0) != result_type)
12482 op0 = convert_and_check (location, result_type, op0);
12483 if (TREE_TYPE (op1) != real_type)
12484 op1 = convert_and_check (location, real_type, op1);
12486 else
12488 if (TREE_TYPE (op0) != real_type)
12489 op0 = convert_and_check (location, real_type, op0);
12490 if (TREE_TYPE (op1) != result_type)
12491 op1 = convert_and_check (location, result_type, op1);
12493 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12494 return error_mark_node;
12495 if (first_complex)
12497 op0 = save_expr (op0);
12498 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12499 op0, true);
12500 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12501 op0, true);
12502 switch (code)
12504 case MULT_EXPR:
12505 case TRUNC_DIV_EXPR:
12506 op1 = save_expr (op1);
12507 imag = build2 (resultcode, real_type, imag, op1);
12508 /* Fall through. */
12509 case PLUS_EXPR:
12510 case MINUS_EXPR:
12511 real = build2 (resultcode, real_type, real, op1);
12512 break;
12513 default:
12514 gcc_unreachable();
12517 else
12519 op1 = save_expr (op1);
12520 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12521 op1, true);
12522 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12523 op1, true);
12524 switch (code)
12526 case MULT_EXPR:
12527 op0 = save_expr (op0);
12528 imag = build2 (resultcode, real_type, op0, imag);
12529 /* Fall through. */
12530 case PLUS_EXPR:
12531 real = build2 (resultcode, real_type, op0, real);
12532 break;
12533 case MINUS_EXPR:
12534 real = build2 (resultcode, real_type, op0, real);
12535 imag = build1 (NEGATE_EXPR, real_type, imag);
12536 break;
12537 default:
12538 gcc_unreachable();
12541 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12542 goto return_build_binary_op;
12545 /* For certain operations (which identify themselves by shorten != 0)
12546 if both args were extended from the same smaller type,
12547 do the arithmetic in that type and then extend.
12549 shorten !=0 and !=1 indicates a bitwise operation.
12550 For them, this optimization is safe only if
12551 both args are zero-extended or both are sign-extended.
12552 Otherwise, we might change the result.
12553 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12554 but calculated in (unsigned short) it would be (unsigned short)-1. */
12556 if (shorten && none_complex)
12558 final_type = result_type;
12559 result_type = shorten_binary_op (result_type, op0, op1,
12560 shorten == -1);
12563 /* Shifts can be shortened if shifting right. */
12565 if (short_shift)
12567 int unsigned_arg;
12568 tree arg0 = get_narrower (op0, &unsigned_arg);
12570 final_type = result_type;
12572 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12573 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12575 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12576 && tree_int_cst_sgn (op1) > 0
12577 /* We can shorten only if the shift count is less than the
12578 number of bits in the smaller type size. */
12579 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12580 /* We cannot drop an unsigned shift after sign-extension. */
12581 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12583 /* Do an unsigned shift if the operand was zero-extended. */
12584 result_type
12585 = c_common_signed_or_unsigned_type (unsigned_arg,
12586 TREE_TYPE (arg0));
12587 /* Convert value-to-be-shifted to that type. */
12588 if (TREE_TYPE (op0) != result_type)
12589 op0 = convert (result_type, op0);
12590 converted = 1;
12594 /* Comparison operations are shortened too but differently.
12595 They identify themselves by setting short_compare = 1. */
12597 if (short_compare)
12599 /* Don't write &op0, etc., because that would prevent op0
12600 from being kept in a register.
12601 Instead, make copies of the our local variables and
12602 pass the copies by reference, then copy them back afterward. */
12603 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12604 enum tree_code xresultcode = resultcode;
12605 tree val
12606 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12607 &xresultcode);
12609 if (val != NULL_TREE)
12611 ret = val;
12612 goto return_build_binary_op;
12615 op0 = xop0, op1 = xop1;
12616 converted = 1;
12617 resultcode = xresultcode;
12619 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12621 bool op0_maybe_const = true;
12622 bool op1_maybe_const = true;
12623 tree orig_op0_folded, orig_op1_folded;
12625 if (in_late_binary_op)
12627 orig_op0_folded = orig_op0;
12628 orig_op1_folded = orig_op1;
12630 else
12632 /* Fold for the sake of possible warnings, as in
12633 build_conditional_expr. This requires the
12634 "original" values to be folded, not just op0 and
12635 op1. */
12636 c_inhibit_evaluation_warnings++;
12637 op0 = c_fully_fold (op0, require_constant_value,
12638 &op0_maybe_const);
12639 op1 = c_fully_fold (op1, require_constant_value,
12640 &op1_maybe_const);
12641 c_inhibit_evaluation_warnings--;
12642 orig_op0_folded = c_fully_fold (orig_op0,
12643 require_constant_value,
12644 NULL);
12645 orig_op1_folded = c_fully_fold (orig_op1,
12646 require_constant_value,
12647 NULL);
12650 if (warn_sign_compare)
12651 warn_for_sign_compare (location, orig_op0_folded,
12652 orig_op1_folded, op0, op1,
12653 result_type, resultcode);
12654 if (!in_late_binary_op && !int_operands)
12656 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12657 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12658 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12659 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12665 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12666 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12667 Then the expression will be built.
12668 It will be given type FINAL_TYPE if that is nonzero;
12669 otherwise, it will be given type RESULT_TYPE. */
12671 if (!result_type)
12673 /* Favor showing any expression locations that are available. */
12674 op_location_t oploc (location, UNKNOWN_LOCATION);
12675 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12676 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12677 return error_mark_node;
12680 if (build_type == NULL_TREE)
12682 build_type = result_type;
12683 if ((type0 != orig_type0 || type1 != orig_type1)
12684 && !boolean_op)
12686 gcc_assert (may_need_excess_precision && common);
12687 semantic_result_type = c_common_type (orig_type0, orig_type1);
12691 if (!converted)
12693 op0 = ep_convert_and_check (location, result_type, op0,
12694 semantic_result_type);
12695 op1 = ep_convert_and_check (location, result_type, op1,
12696 semantic_result_type);
12698 /* This can happen if one operand has a vector type, and the other
12699 has a different type. */
12700 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12701 return error_mark_node;
12704 if (sanitize_flags_p ((SANITIZE_SHIFT
12705 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12706 && current_function_decl != NULL_TREE
12707 && (doing_div_or_mod || doing_shift)
12708 && !require_constant_value)
12710 /* OP0 and/or OP1 might have side-effects. */
12711 op0 = save_expr (op0);
12712 op1 = save_expr (op1);
12713 op0 = c_fully_fold (op0, false, NULL);
12714 op1 = c_fully_fold (op1, false, NULL);
12715 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12716 | SANITIZE_FLOAT_DIVIDE))))
12717 instrument_expr = ubsan_instrument_division (location, op0, op1);
12718 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12719 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12722 /* Treat expressions in initializers specially as they can't trap. */
12723 if (int_const_or_overflow)
12724 ret = (require_constant_value
12725 ? fold_build2_initializer_loc (location, resultcode, build_type,
12726 op0, op1)
12727 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12728 else
12729 ret = build2 (resultcode, build_type, op0, op1);
12730 if (final_type != NULL_TREE)
12731 ret = convert (final_type, ret);
12733 return_build_binary_op:
12734 gcc_assert (ret != error_mark_node);
12735 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12736 ret = (int_operands
12737 ? note_integer_operands (ret)
12738 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12739 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12740 && !in_late_binary_op)
12741 ret = note_integer_operands (ret);
12742 protected_set_expr_location (ret, location);
12744 if (instrument_expr != NULL)
12745 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12746 instrument_expr, ret);
12748 if (semantic_result_type)
12749 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12750 semantic_result_type, ret);
12752 return ret;
12756 /* Convert EXPR to be a truth-value, validating its type for this
12757 purpose. LOCATION is the source location for the expression. */
12759 tree
12760 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12762 bool int_const, int_operands;
12764 switch (TREE_CODE (TREE_TYPE (expr)))
12766 case ARRAY_TYPE:
12767 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12768 return error_mark_node;
12770 case RECORD_TYPE:
12771 error_at (location, "used struct type value where scalar is required");
12772 return error_mark_node;
12774 case UNION_TYPE:
12775 error_at (location, "used union type value where scalar is required");
12776 return error_mark_node;
12778 case VOID_TYPE:
12779 error_at (location, "void value not ignored as it ought to be");
12780 return error_mark_node;
12782 case POINTER_TYPE:
12783 if (reject_gcc_builtin (expr))
12784 return error_mark_node;
12785 break;
12787 case FUNCTION_TYPE:
12788 gcc_unreachable ();
12790 case VECTOR_TYPE:
12791 error_at (location, "used vector type where scalar is required");
12792 return error_mark_node;
12794 default:
12795 break;
12798 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12799 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12800 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12802 expr = remove_c_maybe_const_expr (expr);
12803 expr = build2 (NE_EXPR, integer_type_node, expr,
12804 convert (TREE_TYPE (expr), integer_zero_node));
12805 expr = note_integer_operands (expr);
12807 else
12808 /* ??? Should we also give an error for vectors rather than leaving
12809 those to give errors later? */
12810 expr = c_common_truthvalue_conversion (location, expr);
12812 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12814 if (TREE_OVERFLOW (expr))
12815 return expr;
12816 else
12817 return note_integer_operands (expr);
12819 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12820 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12821 return expr;
12825 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12826 required. */
12828 tree
12829 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12831 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12833 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12834 /* Executing a compound literal inside a function reinitializes
12835 it. */
12836 if (!TREE_STATIC (decl))
12837 *se = true;
12838 return decl;
12840 else
12841 return expr;
12844 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12845 statement. LOC is the location of the construct. */
12847 tree
12848 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12849 tree clauses)
12851 body = c_end_compound_stmt (loc, body, true);
12853 tree stmt = make_node (code);
12854 TREE_TYPE (stmt) = void_type_node;
12855 OMP_BODY (stmt) = body;
12856 OMP_CLAUSES (stmt) = clauses;
12857 SET_EXPR_LOCATION (stmt, loc);
12859 return add_stmt (stmt);
12862 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12863 statement. LOC is the location of the OACC_DATA. */
12865 tree
12866 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12868 tree stmt;
12870 block = c_end_compound_stmt (loc, block, true);
12872 stmt = make_node (OACC_DATA);
12873 TREE_TYPE (stmt) = void_type_node;
12874 OACC_DATA_CLAUSES (stmt) = clauses;
12875 OACC_DATA_BODY (stmt) = block;
12876 SET_EXPR_LOCATION (stmt, loc);
12878 return add_stmt (stmt);
12881 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12882 statement. LOC is the location of the OACC_HOST_DATA. */
12884 tree
12885 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12887 tree stmt;
12889 block = c_end_compound_stmt (loc, block, true);
12891 stmt = make_node (OACC_HOST_DATA);
12892 TREE_TYPE (stmt) = void_type_node;
12893 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12894 OACC_HOST_DATA_BODY (stmt) = block;
12895 SET_EXPR_LOCATION (stmt, loc);
12897 return add_stmt (stmt);
12900 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12902 tree
12903 c_begin_omp_parallel (void)
12905 tree block;
12907 keep_next_level ();
12908 block = c_begin_compound_stmt (true);
12910 return block;
12913 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12914 statement. LOC is the location of the OMP_PARALLEL. */
12916 tree
12917 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12919 tree stmt;
12921 block = c_end_compound_stmt (loc, block, true);
12923 stmt = make_node (OMP_PARALLEL);
12924 TREE_TYPE (stmt) = void_type_node;
12925 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12926 OMP_PARALLEL_BODY (stmt) = block;
12927 SET_EXPR_LOCATION (stmt, loc);
12929 return add_stmt (stmt);
12932 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12934 tree
12935 c_begin_omp_task (void)
12937 tree block;
12939 keep_next_level ();
12940 block = c_begin_compound_stmt (true);
12942 return block;
12945 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12946 statement. LOC is the location of the #pragma. */
12948 tree
12949 c_finish_omp_task (location_t loc, tree clauses, tree block)
12951 tree stmt;
12953 block = c_end_compound_stmt (loc, block, true);
12955 stmt = make_node (OMP_TASK);
12956 TREE_TYPE (stmt) = void_type_node;
12957 OMP_TASK_CLAUSES (stmt) = clauses;
12958 OMP_TASK_BODY (stmt) = block;
12959 SET_EXPR_LOCATION (stmt, loc);
12961 return add_stmt (stmt);
12964 /* Generate GOMP_cancel call for #pragma omp cancel. */
12966 void
12967 c_finish_omp_cancel (location_t loc, tree clauses)
12969 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12970 int mask = 0;
12971 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12972 mask = 1;
12973 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12974 mask = 2;
12975 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12976 mask = 4;
12977 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12978 mask = 8;
12979 else
12981 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12982 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12983 "clauses");
12984 return;
12986 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12987 if (ifc != NULL_TREE)
12989 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12990 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12991 error_at (OMP_CLAUSE_LOCATION (ifc),
12992 "expected %<cancel%> %<if%> clause modifier");
12993 else
12995 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
12996 if (ifc2 != NULL_TREE)
12998 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
12999 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13000 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13001 error_at (OMP_CLAUSE_LOCATION (ifc2),
13002 "expected %<cancel%> %<if%> clause modifier");
13006 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13007 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13008 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13009 build_zero_cst (type));
13011 else
13012 ifc = boolean_true_node;
13013 tree stmt = build_call_expr_loc (loc, fn, 2,
13014 build_int_cst (integer_type_node, mask),
13015 ifc);
13016 add_stmt (stmt);
13019 /* Generate GOMP_cancellation_point call for
13020 #pragma omp cancellation point. */
13022 void
13023 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13025 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13026 int mask = 0;
13027 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13028 mask = 1;
13029 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13030 mask = 2;
13031 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13032 mask = 4;
13033 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13034 mask = 8;
13035 else
13037 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13038 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13039 "clauses");
13040 return;
13042 tree stmt = build_call_expr_loc (loc, fn, 1,
13043 build_int_cst (integer_type_node, mask));
13044 add_stmt (stmt);
13047 /* Helper function for handle_omp_array_sections. Called recursively
13048 to handle multiple array-section-subscripts. C is the clause,
13049 T current expression (initially OMP_CLAUSE_DECL), which is either
13050 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13051 expression if specified, TREE_VALUE length expression if specified,
13052 TREE_CHAIN is what it has been specified after, or some decl.
13053 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13054 set to true if any of the array-section-subscript could have length
13055 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13056 first array-section-subscript which is known not to have length
13057 of one. Given say:
13058 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13059 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13060 all are or may have length of 1, array-section-subscript [:2] is the
13061 first one known not to have length 1. For array-section-subscript
13062 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13063 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13064 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13065 case though, as some lengths could be zero. */
13067 static tree
13068 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13069 bool &maybe_zero_len, unsigned int &first_non_one,
13070 enum c_omp_region_type ort)
13072 tree ret, low_bound, length, type;
13073 if (TREE_CODE (t) != TREE_LIST)
13075 if (error_operand_p (t))
13076 return error_mark_node;
13077 ret = t;
13078 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13079 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13080 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13082 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13083 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13084 return error_mark_node;
13086 if (TREE_CODE (t) == COMPONENT_REF
13087 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13088 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13089 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13091 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13093 error_at (OMP_CLAUSE_LOCATION (c),
13094 "bit-field %qE in %qs clause",
13095 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13096 return error_mark_node;
13098 while (TREE_CODE (t) == COMPONENT_REF)
13100 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13102 error_at (OMP_CLAUSE_LOCATION (c),
13103 "%qE is a member of a union", t);
13104 return error_mark_node;
13106 t = TREE_OPERAND (t, 0);
13107 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13109 if (maybe_ne (mem_ref_offset (t), 0))
13110 error_at (OMP_CLAUSE_LOCATION (c),
13111 "cannot dereference %qE in %qs clause", t,
13112 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13113 else
13114 t = TREE_OPERAND (t, 0);
13118 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13120 if (DECL_P (t))
13121 error_at (OMP_CLAUSE_LOCATION (c),
13122 "%qD is not a variable in %qs clause", t,
13123 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13124 else
13125 error_at (OMP_CLAUSE_LOCATION (c),
13126 "%qE is not a variable in %qs clause", t,
13127 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13128 return error_mark_node;
13130 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13131 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13132 && TYPE_ATOMIC (TREE_TYPE (t)))
13134 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13135 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13136 return error_mark_node;
13138 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13139 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13140 && VAR_P (t)
13141 && DECL_THREAD_LOCAL_P (t))
13143 error_at (OMP_CLAUSE_LOCATION (c),
13144 "%qD is threadprivate variable in %qs clause", t,
13145 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13146 return error_mark_node;
13148 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13149 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13150 && TYPE_ATOMIC (TREE_TYPE (t))
13151 && POINTER_TYPE_P (TREE_TYPE (t)))
13153 /* If the array section is pointer based and the pointer
13154 itself is _Atomic qualified, we need to atomically load
13155 the pointer. */
13156 c_expr expr;
13157 memset (&expr, 0, sizeof (expr));
13158 expr.value = ret;
13159 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13160 expr, false, false);
13161 ret = expr.value;
13163 return ret;
13166 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13167 maybe_zero_len, first_non_one, ort);
13168 if (ret == error_mark_node || ret == NULL_TREE)
13169 return ret;
13171 type = TREE_TYPE (ret);
13172 low_bound = TREE_PURPOSE (t);
13173 length = TREE_VALUE (t);
13175 if (low_bound == error_mark_node || length == error_mark_node)
13176 return error_mark_node;
13178 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13180 error_at (OMP_CLAUSE_LOCATION (c),
13181 "low bound %qE of array section does not have integral type",
13182 low_bound);
13183 return error_mark_node;
13185 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13187 error_at (OMP_CLAUSE_LOCATION (c),
13188 "length %qE of array section does not have integral type",
13189 length);
13190 return error_mark_node;
13192 if (low_bound
13193 && TREE_CODE (low_bound) == INTEGER_CST
13194 && TYPE_PRECISION (TREE_TYPE (low_bound))
13195 > TYPE_PRECISION (sizetype))
13196 low_bound = fold_convert (sizetype, low_bound);
13197 if (length
13198 && TREE_CODE (length) == INTEGER_CST
13199 && TYPE_PRECISION (TREE_TYPE (length))
13200 > TYPE_PRECISION (sizetype))
13201 length = fold_convert (sizetype, length);
13202 if (low_bound == NULL_TREE)
13203 low_bound = integer_zero_node;
13204 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13205 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13206 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13208 if (length != integer_one_node)
13210 error_at (OMP_CLAUSE_LOCATION (c),
13211 "expected single pointer in %qs clause",
13212 c_omp_map_clause_name (c, ort == C_ORT_ACC));
13213 return error_mark_node;
13216 if (length != NULL_TREE)
13218 if (!integer_nonzerop (length))
13220 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13221 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13222 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13223 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13224 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13226 if (integer_zerop (length))
13228 error_at (OMP_CLAUSE_LOCATION (c),
13229 "zero length array section in %qs clause",
13230 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13231 return error_mark_node;
13234 else
13235 maybe_zero_len = true;
13237 if (first_non_one == types.length ()
13238 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13239 first_non_one++;
13241 if (TREE_CODE (type) == ARRAY_TYPE)
13243 if (length == NULL_TREE
13244 && (TYPE_DOMAIN (type) == NULL_TREE
13245 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13247 error_at (OMP_CLAUSE_LOCATION (c),
13248 "for unknown bound array type length expression must "
13249 "be specified");
13250 return error_mark_node;
13252 if (TREE_CODE (low_bound) == INTEGER_CST
13253 && tree_int_cst_sgn (low_bound) == -1)
13255 error_at (OMP_CLAUSE_LOCATION (c),
13256 "negative low bound in array section in %qs clause",
13257 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13258 return error_mark_node;
13260 if (length != NULL_TREE
13261 && TREE_CODE (length) == INTEGER_CST
13262 && tree_int_cst_sgn (length) == -1)
13264 error_at (OMP_CLAUSE_LOCATION (c),
13265 "negative length in array section in %qs clause",
13266 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13267 return error_mark_node;
13269 if (TYPE_DOMAIN (type)
13270 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13271 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13272 == INTEGER_CST)
13274 tree size
13275 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13276 size = size_binop (PLUS_EXPR, size, size_one_node);
13277 if (TREE_CODE (low_bound) == INTEGER_CST)
13279 if (tree_int_cst_lt (size, low_bound))
13281 error_at (OMP_CLAUSE_LOCATION (c),
13282 "low bound %qE above array section size "
13283 "in %qs clause", low_bound,
13284 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13285 return error_mark_node;
13287 if (tree_int_cst_equal (size, low_bound))
13289 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13290 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13291 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13292 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13293 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13295 error_at (OMP_CLAUSE_LOCATION (c),
13296 "zero length array section in %qs clause",
13297 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13298 return error_mark_node;
13300 maybe_zero_len = true;
13302 else if (length == NULL_TREE
13303 && first_non_one == types.length ()
13304 && tree_int_cst_equal
13305 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13306 low_bound))
13307 first_non_one++;
13309 else if (length == NULL_TREE)
13311 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13312 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13313 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13314 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13315 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13316 maybe_zero_len = true;
13317 if (first_non_one == types.length ())
13318 first_non_one++;
13320 if (length && TREE_CODE (length) == INTEGER_CST)
13322 if (tree_int_cst_lt (size, length))
13324 error_at (OMP_CLAUSE_LOCATION (c),
13325 "length %qE above array section size "
13326 "in %qs clause", length,
13327 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13328 return error_mark_node;
13330 if (TREE_CODE (low_bound) == INTEGER_CST)
13332 tree lbpluslen
13333 = size_binop (PLUS_EXPR,
13334 fold_convert (sizetype, low_bound),
13335 fold_convert (sizetype, length));
13336 if (TREE_CODE (lbpluslen) == INTEGER_CST
13337 && tree_int_cst_lt (size, lbpluslen))
13339 error_at (OMP_CLAUSE_LOCATION (c),
13340 "high bound %qE above array section size "
13341 "in %qs clause", lbpluslen,
13342 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13343 return error_mark_node;
13348 else if (length == NULL_TREE)
13350 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13351 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13352 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13353 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13354 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13355 maybe_zero_len = true;
13356 if (first_non_one == types.length ())
13357 first_non_one++;
13360 /* For [lb:] we will need to evaluate lb more than once. */
13361 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13363 tree lb = save_expr (low_bound);
13364 if (lb != low_bound)
13366 TREE_PURPOSE (t) = lb;
13367 low_bound = lb;
13371 else if (TREE_CODE (type) == POINTER_TYPE)
13373 if (length == NULL_TREE)
13375 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13376 error_at (OMP_CLAUSE_LOCATION (c),
13377 "for array function parameter length expression "
13378 "must be specified");
13379 else
13380 error_at (OMP_CLAUSE_LOCATION (c),
13381 "for pointer type length expression must be specified");
13382 return error_mark_node;
13384 if (length != NULL_TREE
13385 && TREE_CODE (length) == INTEGER_CST
13386 && tree_int_cst_sgn (length) == -1)
13388 error_at (OMP_CLAUSE_LOCATION (c),
13389 "negative length in array section in %qs clause",
13390 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13391 return error_mark_node;
13393 /* If there is a pointer type anywhere but in the very first
13394 array-section-subscript, the array section can't be contiguous. */
13395 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13396 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13397 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13399 error_at (OMP_CLAUSE_LOCATION (c),
13400 "array section is not contiguous in %qs clause",
13401 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13402 return error_mark_node;
13405 else
13407 error_at (OMP_CLAUSE_LOCATION (c),
13408 "%qE does not have pointer or array type", ret);
13409 return error_mark_node;
13411 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13412 types.safe_push (TREE_TYPE (ret));
13413 /* We will need to evaluate lb more than once. */
13414 tree lb = save_expr (low_bound);
13415 if (lb != low_bound)
13417 TREE_PURPOSE (t) = lb;
13418 low_bound = lb;
13420 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13421 return ret;
13424 /* Handle array sections for clause C. */
13426 static bool
13427 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13429 bool maybe_zero_len = false;
13430 unsigned int first_non_one = 0;
13431 auto_vec<tree, 10> types;
13432 tree *tp = &OMP_CLAUSE_DECL (c);
13433 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13434 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13435 && TREE_CODE (*tp) == TREE_LIST
13436 && TREE_PURPOSE (*tp)
13437 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13438 tp = &TREE_VALUE (*tp);
13439 tree first = handle_omp_array_sections_1 (c, *tp, types,
13440 maybe_zero_len, first_non_one,
13441 ort);
13442 if (first == error_mark_node)
13443 return true;
13444 if (first == NULL_TREE)
13445 return false;
13446 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13447 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13449 tree t = *tp;
13450 tree tem = NULL_TREE;
13451 /* Need to evaluate side effects in the length expressions
13452 if any. */
13453 while (TREE_CODE (t) == TREE_LIST)
13455 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13457 if (tem == NULL_TREE)
13458 tem = TREE_VALUE (t);
13459 else
13460 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13461 TREE_VALUE (t), tem);
13463 t = TREE_CHAIN (t);
13465 if (tem)
13466 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13467 first = c_fully_fold (first, false, NULL, true);
13468 *tp = first;
13470 else
13472 unsigned int num = types.length (), i;
13473 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13474 tree condition = NULL_TREE;
13476 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13477 maybe_zero_len = true;
13479 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13480 t = TREE_CHAIN (t))
13482 tree low_bound = TREE_PURPOSE (t);
13483 tree length = TREE_VALUE (t);
13485 i--;
13486 if (low_bound
13487 && TREE_CODE (low_bound) == INTEGER_CST
13488 && TYPE_PRECISION (TREE_TYPE (low_bound))
13489 > TYPE_PRECISION (sizetype))
13490 low_bound = fold_convert (sizetype, low_bound);
13491 if (length
13492 && TREE_CODE (length) == INTEGER_CST
13493 && TYPE_PRECISION (TREE_TYPE (length))
13494 > TYPE_PRECISION (sizetype))
13495 length = fold_convert (sizetype, length);
13496 if (low_bound == NULL_TREE)
13497 low_bound = integer_zero_node;
13498 if (!maybe_zero_len && i > first_non_one)
13500 if (integer_nonzerop (low_bound))
13501 goto do_warn_noncontiguous;
13502 if (length != NULL_TREE
13503 && TREE_CODE (length) == INTEGER_CST
13504 && TYPE_DOMAIN (types[i])
13505 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13506 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13507 == INTEGER_CST)
13509 tree size;
13510 size = size_binop (PLUS_EXPR,
13511 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13512 size_one_node);
13513 if (!tree_int_cst_equal (length, size))
13515 do_warn_noncontiguous:
13516 error_at (OMP_CLAUSE_LOCATION (c),
13517 "array section is not contiguous in %qs "
13518 "clause",
13519 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13520 return true;
13523 if (length != NULL_TREE
13524 && TREE_SIDE_EFFECTS (length))
13526 if (side_effects == NULL_TREE)
13527 side_effects = length;
13528 else
13529 side_effects = build2 (COMPOUND_EXPR,
13530 TREE_TYPE (side_effects),
13531 length, side_effects);
13534 else
13536 tree l;
13538 if (i > first_non_one
13539 && ((length && integer_nonzerop (length))
13540 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13541 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13542 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13543 continue;
13544 if (length)
13545 l = fold_convert (sizetype, length);
13546 else
13548 l = size_binop (PLUS_EXPR,
13549 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13550 size_one_node);
13551 l = size_binop (MINUS_EXPR, l,
13552 fold_convert (sizetype, low_bound));
13554 if (i > first_non_one)
13556 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13557 size_zero_node);
13558 if (condition == NULL_TREE)
13559 condition = l;
13560 else
13561 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13562 l, condition);
13564 else if (size == NULL_TREE)
13566 size = size_in_bytes (TREE_TYPE (types[i]));
13567 tree eltype = TREE_TYPE (types[num - 1]);
13568 while (TREE_CODE (eltype) == ARRAY_TYPE)
13569 eltype = TREE_TYPE (eltype);
13570 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13571 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13572 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13574 if (integer_zerop (size)
13575 || integer_zerop (size_in_bytes (eltype)))
13577 error_at (OMP_CLAUSE_LOCATION (c),
13578 "zero length array section in %qs clause",
13579 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13580 return error_mark_node;
13582 size = size_binop (EXACT_DIV_EXPR, size,
13583 size_in_bytes (eltype));
13585 size = size_binop (MULT_EXPR, size, l);
13586 if (condition)
13587 size = fold_build3 (COND_EXPR, sizetype, condition,
13588 size, size_zero_node);
13590 else
13591 size = size_binop (MULT_EXPR, size, l);
13594 if (side_effects)
13595 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13596 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13597 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13598 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13600 size = size_binop (MINUS_EXPR, size, size_one_node);
13601 size = c_fully_fold (size, false, NULL);
13602 size = save_expr (size);
13603 tree index_type = build_index_type (size);
13604 tree eltype = TREE_TYPE (first);
13605 while (TREE_CODE (eltype) == ARRAY_TYPE)
13606 eltype = TREE_TYPE (eltype);
13607 tree type = build_array_type (eltype, index_type);
13608 tree ptype = build_pointer_type (eltype);
13609 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13610 t = build_fold_addr_expr (t);
13611 tree t2 = build_fold_addr_expr (first);
13612 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13613 ptrdiff_type_node, t2);
13614 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13615 ptrdiff_type_node, t2,
13616 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13617 ptrdiff_type_node, t));
13618 t2 = c_fully_fold (t2, false, NULL);
13619 if (tree_fits_shwi_p (t2))
13620 t = build2 (MEM_REF, type, t,
13621 build_int_cst (ptype, tree_to_shwi (t2)));
13622 else
13624 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13625 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13626 TREE_TYPE (t), t, t2);
13627 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13629 OMP_CLAUSE_DECL (c) = t;
13630 return false;
13632 first = c_fully_fold (first, false, NULL);
13633 OMP_CLAUSE_DECL (c) = first;
13634 if (size)
13635 size = c_fully_fold (size, false, NULL);
13636 OMP_CLAUSE_SIZE (c) = size;
13637 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13638 || (TREE_CODE (t) == COMPONENT_REF
13639 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13640 return false;
13641 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13642 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13643 switch (OMP_CLAUSE_MAP_KIND (c))
13645 case GOMP_MAP_ALLOC:
13646 case GOMP_MAP_IF_PRESENT:
13647 case GOMP_MAP_TO:
13648 case GOMP_MAP_FROM:
13649 case GOMP_MAP_TOFROM:
13650 case GOMP_MAP_ALWAYS_TO:
13651 case GOMP_MAP_ALWAYS_FROM:
13652 case GOMP_MAP_ALWAYS_TOFROM:
13653 case GOMP_MAP_RELEASE:
13654 case GOMP_MAP_DELETE:
13655 case GOMP_MAP_FORCE_TO:
13656 case GOMP_MAP_FORCE_FROM:
13657 case GOMP_MAP_FORCE_TOFROM:
13658 case GOMP_MAP_FORCE_PRESENT:
13659 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13660 break;
13661 default:
13662 break;
13664 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13665 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13666 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13667 else if (TREE_CODE (t) == COMPONENT_REF)
13668 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
13669 else
13670 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13671 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
13672 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13673 && !c_mark_addressable (t))
13674 return false;
13675 OMP_CLAUSE_DECL (c2) = t;
13676 t = build_fold_addr_expr (first);
13677 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13678 tree ptr = OMP_CLAUSE_DECL (c2);
13679 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13680 ptr = build_fold_addr_expr (ptr);
13681 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13682 ptrdiff_type_node, t,
13683 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13684 ptrdiff_type_node, ptr));
13685 t = c_fully_fold (t, false, NULL);
13686 OMP_CLAUSE_SIZE (c2) = t;
13687 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13688 OMP_CLAUSE_CHAIN (c) = c2;
13690 return false;
13693 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13694 an inline call. But, remap
13695 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13696 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13698 static tree
13699 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13700 tree decl, tree placeholder)
13702 copy_body_data id;
13703 hash_map<tree, tree> decl_map;
13705 decl_map.put (omp_decl1, placeholder);
13706 decl_map.put (omp_decl2, decl);
13707 memset (&id, 0, sizeof (id));
13708 id.src_fn = DECL_CONTEXT (omp_decl1);
13709 id.dst_fn = current_function_decl;
13710 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13711 id.decl_map = &decl_map;
13713 id.copy_decl = copy_decl_no_change;
13714 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13715 id.transform_new_cfg = true;
13716 id.transform_return_to_modify = false;
13717 id.transform_lang_insert_block = NULL;
13718 id.eh_lp_nr = 0;
13719 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13720 return stmt;
13723 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13724 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13726 static tree
13727 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13729 if (*tp == (tree) data)
13730 return *tp;
13731 return NULL_TREE;
13734 /* Similarly, but also walk aggregate fields. */
13736 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13738 static tree
13739 c_find_omp_var_r (tree *tp, int *, void *data)
13741 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13742 return *tp;
13743 if (RECORD_OR_UNION_TYPE_P (*tp))
13745 tree field;
13746 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13748 for (field = TYPE_FIELDS (*tp); field;
13749 field = DECL_CHAIN (field))
13750 if (TREE_CODE (field) == FIELD_DECL)
13752 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13753 c_find_omp_var_r, data, pset);
13754 if (ret)
13755 return ret;
13756 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13757 if (ret)
13758 return ret;
13759 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13760 pset);
13761 if (ret)
13762 return ret;
13763 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13764 if (ret)
13765 return ret;
13768 else if (INTEGRAL_TYPE_P (*tp))
13769 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13770 ((struct c_find_omp_var_s *) data)->pset);
13771 return NULL_TREE;
13774 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13775 and clauses containing them should be removed. */
13777 static bool
13778 c_omp_finish_iterators (tree iter)
13780 bool ret = false;
13781 for (tree it = iter; it; it = TREE_CHAIN (it))
13783 tree var = TREE_VEC_ELT (it, 0);
13784 tree begin = TREE_VEC_ELT (it, 1);
13785 tree end = TREE_VEC_ELT (it, 2);
13786 tree step = TREE_VEC_ELT (it, 3);
13787 tree orig_step;
13788 tree type = TREE_TYPE (var);
13789 location_t loc = DECL_SOURCE_LOCATION (var);
13790 if (type == error_mark_node)
13792 ret = true;
13793 continue;
13795 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13797 error_at (loc, "iterator %qD has neither integral nor pointer type",
13798 var);
13799 ret = true;
13800 continue;
13802 else if (TYPE_ATOMIC (type))
13804 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13805 ret = true;
13806 continue;
13808 else if (TYPE_READONLY (type))
13810 error_at (loc, "iterator %qD has const qualified type", var);
13811 ret = true;
13812 continue;
13814 else if (step == error_mark_node
13815 || TREE_TYPE (step) == error_mark_node)
13817 ret = true;
13818 continue;
13820 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13822 error_at (EXPR_LOC_OR_LOC (step, loc),
13823 "iterator step with non-integral type");
13824 ret = true;
13825 continue;
13827 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13828 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13829 orig_step = save_expr (c_fully_fold (step, false, NULL));
13830 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13831 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13832 if (POINTER_TYPE_P (type))
13834 begin = save_expr (begin);
13835 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13836 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13837 fold_convert (sizetype, step),
13838 fold_convert (sizetype, begin));
13839 step = fold_convert (ssizetype, step);
13841 if (integer_zerop (step))
13843 error_at (loc, "iterator %qD has zero step", var);
13844 ret = true;
13845 continue;
13848 if (begin == error_mark_node
13849 || end == error_mark_node
13850 || step == error_mark_node
13851 || orig_step == error_mark_node)
13853 ret = true;
13854 continue;
13856 hash_set<tree> pset;
13857 tree it2;
13858 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13860 tree var2 = TREE_VEC_ELT (it2, 0);
13861 tree begin2 = TREE_VEC_ELT (it2, 1);
13862 tree end2 = TREE_VEC_ELT (it2, 2);
13863 tree step2 = TREE_VEC_ELT (it2, 3);
13864 tree type2 = TREE_TYPE (var2);
13865 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13866 struct c_find_omp_var_s data = { var, &pset };
13867 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13869 error_at (loc2,
13870 "type of iterator %qD refers to outer iterator %qD",
13871 var2, var);
13872 break;
13874 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13876 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13877 "begin expression refers to outer iterator %qD", var);
13878 break;
13880 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13882 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13883 "end expression refers to outer iterator %qD", var);
13884 break;
13886 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13888 error_at (EXPR_LOC_OR_LOC (step2, loc2),
13889 "step expression refers to outer iterator %qD", var);
13890 break;
13893 if (it2)
13895 ret = true;
13896 continue;
13898 TREE_VEC_ELT (it, 1) = begin;
13899 TREE_VEC_ELT (it, 2) = end;
13900 TREE_VEC_ELT (it, 3) = step;
13901 TREE_VEC_ELT (it, 4) = orig_step;
13903 return ret;
13906 /* Ensure that pointers are used in OpenACC attach and detach clauses.
13907 Return true if an error has been detected. */
13909 static bool
13910 c_oacc_check_attachments (tree c)
13912 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13913 return false;
13915 /* OpenACC attach / detach clauses must be pointers. */
13916 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13917 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
13919 tree t = OMP_CLAUSE_DECL (c);
13921 while (TREE_CODE (t) == TREE_LIST)
13922 t = TREE_CHAIN (t);
13924 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13926 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
13927 c_omp_map_clause_name (c, true));
13928 return true;
13932 return false;
13935 /* For all elements of CLAUSES, validate them against their constraints.
13936 Remove any elements from the list that are invalid. */
13938 tree
13939 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13941 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13942 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
13943 bitmap_head oacc_reduction_head;
13944 tree c, t, type, *pc;
13945 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13946 bool branch_seen = false;
13947 bool copyprivate_seen = false;
13948 bool mergeable_seen = false;
13949 tree *detach_seen = NULL;
13950 bool linear_variable_step_check = false;
13951 tree *nowait_clause = NULL;
13952 tree ordered_clause = NULL_TREE;
13953 tree schedule_clause = NULL_TREE;
13954 bool oacc_async = false;
13955 tree last_iterators = NULL_TREE;
13956 bool last_iterators_remove = false;
13957 tree *nogroup_seen = NULL;
13958 tree *order_clause = NULL;
13959 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
13960 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
13961 int reduction_seen = 0;
13962 bool allocate_seen = false;
13963 bool implicit_moved = false;
13965 bitmap_obstack_initialize (NULL);
13966 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13967 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13968 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13969 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13970 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
13971 bitmap_initialize (&map_head, &bitmap_default_obstack);
13972 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13973 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
13974 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
13975 instead. */
13976 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13978 if (ort & C_ORT_ACC)
13979 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13980 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13982 oacc_async = true;
13983 break;
13986 for (pc = &clauses, c = clauses; c ; c = *pc)
13988 bool remove = false;
13989 bool need_complete = false;
13990 bool need_implicitly_determined = false;
13992 switch (OMP_CLAUSE_CODE (c))
13994 case OMP_CLAUSE_SHARED:
13995 need_implicitly_determined = true;
13996 goto check_dup_generic;
13998 case OMP_CLAUSE_PRIVATE:
13999 need_complete = true;
14000 need_implicitly_determined = true;
14001 goto check_dup_generic;
14003 case OMP_CLAUSE_REDUCTION:
14004 if (reduction_seen == 0)
14005 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14006 else if (reduction_seen != -2
14007 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14008 ? -1 : 1))
14010 error_at (OMP_CLAUSE_LOCATION (c),
14011 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14012 "on the same construct");
14013 reduction_seen = -2;
14015 /* FALLTHRU */
14016 case OMP_CLAUSE_IN_REDUCTION:
14017 case OMP_CLAUSE_TASK_REDUCTION:
14018 need_implicitly_determined = true;
14019 t = OMP_CLAUSE_DECL (c);
14020 if (TREE_CODE (t) == TREE_LIST)
14022 if (handle_omp_array_sections (c, ort))
14024 remove = true;
14025 break;
14028 t = OMP_CLAUSE_DECL (c);
14029 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14030 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14032 error_at (OMP_CLAUSE_LOCATION (c),
14033 "%<inscan%> %<reduction%> clause with array "
14034 "section");
14035 remove = true;
14036 break;
14039 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14040 if (t == error_mark_node)
14042 remove = true;
14043 break;
14045 if (oacc_async)
14046 c_mark_addressable (t);
14047 type = TREE_TYPE (t);
14048 if (TREE_CODE (t) == MEM_REF)
14049 type = TREE_TYPE (type);
14050 if (TREE_CODE (type) == ARRAY_TYPE)
14052 tree oatype = type;
14053 gcc_assert (TREE_CODE (t) != MEM_REF);
14054 while (TREE_CODE (type) == ARRAY_TYPE)
14055 type = TREE_TYPE (type);
14056 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14058 error_at (OMP_CLAUSE_LOCATION (c),
14059 "%qD in %<reduction%> clause is a zero size array",
14061 remove = true;
14062 break;
14064 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14065 TYPE_SIZE_UNIT (type));
14066 if (integer_zerop (size))
14068 error_at (OMP_CLAUSE_LOCATION (c),
14069 "%qD in %<reduction%> clause is a zero size array",
14071 remove = true;
14072 break;
14074 size = size_binop (MINUS_EXPR, size, size_one_node);
14075 size = save_expr (size);
14076 tree index_type = build_index_type (size);
14077 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14078 index_type);
14079 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14080 tree ptype = build_pointer_type (type);
14081 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14082 t = build_fold_addr_expr (t);
14083 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14084 OMP_CLAUSE_DECL (c) = t;
14086 if (TYPE_ATOMIC (type))
14088 error_at (OMP_CLAUSE_LOCATION (c),
14089 "%<_Atomic%> %qE in %<reduction%> clause", t);
14090 remove = true;
14091 break;
14093 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14094 || OMP_CLAUSE_REDUCTION_TASK (c))
14096 /* Disallow zero sized or potentially zero sized task
14097 reductions. */
14098 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14100 error_at (OMP_CLAUSE_LOCATION (c),
14101 "zero sized type %qT in %qs clause", type,
14102 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14103 remove = true;
14104 break;
14106 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14108 error_at (OMP_CLAUSE_LOCATION (c),
14109 "variable sized type %qT in %qs clause", type,
14110 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14111 remove = true;
14112 break;
14115 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14116 && (FLOAT_TYPE_P (type)
14117 || TREE_CODE (type) == COMPLEX_TYPE))
14119 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14120 const char *r_name = NULL;
14122 switch (r_code)
14124 case PLUS_EXPR:
14125 case MULT_EXPR:
14126 case MINUS_EXPR:
14127 case TRUTH_ANDIF_EXPR:
14128 case TRUTH_ORIF_EXPR:
14129 break;
14130 case MIN_EXPR:
14131 if (TREE_CODE (type) == COMPLEX_TYPE)
14132 r_name = "min";
14133 break;
14134 case MAX_EXPR:
14135 if (TREE_CODE (type) == COMPLEX_TYPE)
14136 r_name = "max";
14137 break;
14138 case BIT_AND_EXPR:
14139 r_name = "&";
14140 break;
14141 case BIT_XOR_EXPR:
14142 r_name = "^";
14143 break;
14144 case BIT_IOR_EXPR:
14145 r_name = "|";
14146 break;
14147 default:
14148 gcc_unreachable ();
14150 if (r_name)
14152 error_at (OMP_CLAUSE_LOCATION (c),
14153 "%qE has invalid type for %<reduction(%s)%>",
14154 t, r_name);
14155 remove = true;
14156 break;
14159 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14161 error_at (OMP_CLAUSE_LOCATION (c),
14162 "user defined reduction not found for %qE", t);
14163 remove = true;
14164 break;
14166 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14168 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14169 type = TYPE_MAIN_VARIANT (type);
14170 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14171 VAR_DECL, NULL_TREE, type);
14172 tree decl_placeholder = NULL_TREE;
14173 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14174 DECL_ARTIFICIAL (placeholder) = 1;
14175 DECL_IGNORED_P (placeholder) = 1;
14176 if (TREE_CODE (t) == MEM_REF)
14178 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14179 VAR_DECL, NULL_TREE, type);
14180 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14181 DECL_ARTIFICIAL (decl_placeholder) = 1;
14182 DECL_IGNORED_P (decl_placeholder) = 1;
14184 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14185 c_mark_addressable (placeholder);
14186 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14187 c_mark_addressable (decl_placeholder ? decl_placeholder
14188 : OMP_CLAUSE_DECL (c));
14189 OMP_CLAUSE_REDUCTION_MERGE (c)
14190 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14191 TREE_VEC_ELT (list, 0),
14192 TREE_VEC_ELT (list, 1),
14193 decl_placeholder ? decl_placeholder
14194 : OMP_CLAUSE_DECL (c), placeholder);
14195 OMP_CLAUSE_REDUCTION_MERGE (c)
14196 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14197 void_type_node, NULL_TREE,
14198 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14199 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14200 if (TREE_VEC_LENGTH (list) == 6)
14202 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14203 c_mark_addressable (decl_placeholder ? decl_placeholder
14204 : OMP_CLAUSE_DECL (c));
14205 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14206 c_mark_addressable (placeholder);
14207 tree init = TREE_VEC_ELT (list, 5);
14208 if (init == error_mark_node)
14209 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14210 OMP_CLAUSE_REDUCTION_INIT (c)
14211 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14212 TREE_VEC_ELT (list, 3),
14213 decl_placeholder ? decl_placeholder
14214 : OMP_CLAUSE_DECL (c), placeholder);
14215 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14217 tree v = decl_placeholder ? decl_placeholder : t;
14218 OMP_CLAUSE_REDUCTION_INIT (c)
14219 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14220 OMP_CLAUSE_REDUCTION_INIT (c));
14222 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14223 c_find_omp_placeholder_r,
14224 placeholder, NULL))
14225 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14227 else
14229 tree init;
14230 tree v = decl_placeholder ? decl_placeholder : t;
14231 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14232 init = build_constructor (TREE_TYPE (v), NULL);
14233 else
14234 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14235 OMP_CLAUSE_REDUCTION_INIT (c)
14236 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14238 OMP_CLAUSE_REDUCTION_INIT (c)
14239 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14240 void_type_node, NULL_TREE,
14241 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14242 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14244 if (TREE_CODE (t) == MEM_REF)
14246 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14247 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14248 != INTEGER_CST)
14250 sorry ("variable length element type in array "
14251 "%<reduction%> clause");
14252 remove = true;
14253 break;
14255 t = TREE_OPERAND (t, 0);
14256 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14257 t = TREE_OPERAND (t, 0);
14258 if (TREE_CODE (t) == ADDR_EXPR)
14259 t = TREE_OPERAND (t, 0);
14261 goto check_dup_generic_t;
14263 case OMP_CLAUSE_COPYPRIVATE:
14264 copyprivate_seen = true;
14265 if (nowait_clause)
14267 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14268 "%<nowait%> clause must not be used together "
14269 "with %<copyprivate%>");
14270 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14271 nowait_clause = NULL;
14273 goto check_dup_generic;
14275 case OMP_CLAUSE_COPYIN:
14276 t = OMP_CLAUSE_DECL (c);
14277 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14279 error_at (OMP_CLAUSE_LOCATION (c),
14280 "%qE must be %<threadprivate%> for %<copyin%>", t);
14281 remove = true;
14282 break;
14284 goto check_dup_generic;
14286 case OMP_CLAUSE_LINEAR:
14287 if (ort != C_ORT_OMP_DECLARE_SIMD)
14288 need_implicitly_determined = true;
14289 t = OMP_CLAUSE_DECL (c);
14290 if (ort != C_ORT_OMP_DECLARE_SIMD
14291 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
14293 error_at (OMP_CLAUSE_LOCATION (c),
14294 "modifier should not be specified in %<linear%> "
14295 "clause on %<simd%> or %<for%> constructs");
14296 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14298 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14299 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14301 error_at (OMP_CLAUSE_LOCATION (c),
14302 "linear clause applied to non-integral non-pointer "
14303 "variable with type %qT", TREE_TYPE (t));
14304 remove = true;
14305 break;
14307 if (TYPE_ATOMIC (TREE_TYPE (t)))
14309 error_at (OMP_CLAUSE_LOCATION (c),
14310 "%<_Atomic%> %qD in %<linear%> clause", t);
14311 remove = true;
14312 break;
14314 if (ort == C_ORT_OMP_DECLARE_SIMD)
14316 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14317 if (TREE_CODE (s) == PARM_DECL)
14319 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14320 /* map_head bitmap is used as uniform_head if
14321 declare_simd. */
14322 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14323 linear_variable_step_check = true;
14324 goto check_dup_generic;
14326 if (TREE_CODE (s) != INTEGER_CST)
14328 error_at (OMP_CLAUSE_LOCATION (c),
14329 "%<linear%> clause step %qE is neither constant "
14330 "nor a parameter", s);
14331 remove = true;
14332 break;
14335 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14337 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14338 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14339 OMP_CLAUSE_DECL (c), s);
14340 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14341 sizetype, fold_convert (sizetype, s),
14342 fold_convert
14343 (sizetype, OMP_CLAUSE_DECL (c)));
14344 if (s == error_mark_node)
14345 s = size_one_node;
14346 OMP_CLAUSE_LINEAR_STEP (c) = s;
14348 else
14349 OMP_CLAUSE_LINEAR_STEP (c)
14350 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14351 goto check_dup_generic;
14353 check_dup_generic:
14354 t = OMP_CLAUSE_DECL (c);
14355 check_dup_generic_t:
14356 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14358 error_at (OMP_CLAUSE_LOCATION (c),
14359 "%qE is not a variable in clause %qs", t,
14360 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14361 remove = true;
14363 else if ((ort == C_ORT_ACC
14364 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14365 || (ort == C_ORT_OMP
14366 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14367 || (OMP_CLAUSE_CODE (c)
14368 == OMP_CLAUSE_USE_DEVICE_ADDR))))
14370 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14372 error_at (OMP_CLAUSE_LOCATION (c),
14373 ort == C_ORT_ACC
14374 ? "%qD appears more than once in reduction clauses"
14375 : "%qD appears more than once in data clauses",
14377 remove = true;
14379 else
14380 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14382 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14383 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14384 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14386 error_at (OMP_CLAUSE_LOCATION (c),
14387 "%qE appears more than once in data clauses", t);
14388 remove = true;
14390 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14391 && bitmap_bit_p (&map_head, DECL_UID (t)))
14393 if (ort == C_ORT_ACC)
14394 error_at (OMP_CLAUSE_LOCATION (c),
14395 "%qD appears more than once in data clauses", t);
14396 else
14397 error_at (OMP_CLAUSE_LOCATION (c),
14398 "%qD appears both in data and map clauses", t);
14399 remove = true;
14401 else
14402 bitmap_set_bit (&generic_head, DECL_UID (t));
14403 break;
14405 case OMP_CLAUSE_FIRSTPRIVATE:
14406 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
14408 move_implicit:
14409 implicit_moved = true;
14410 /* Move firstprivate and map clauses with
14411 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
14412 clauses chain. */
14413 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
14414 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
14415 while (*pc1)
14416 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
14417 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
14419 *pc3 = *pc1;
14420 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
14421 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14423 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
14424 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
14426 *pc2 = *pc1;
14427 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
14428 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14430 else
14431 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
14432 *pc3 = NULL;
14433 *pc2 = cl2;
14434 *pc1 = cl1;
14435 continue;
14437 t = OMP_CLAUSE_DECL (c);
14438 need_complete = true;
14439 need_implicitly_determined = true;
14440 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14442 error_at (OMP_CLAUSE_LOCATION (c),
14443 "%qE is not a variable in clause %<firstprivate%>", t);
14444 remove = true;
14446 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14447 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
14448 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14449 remove = true;
14450 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14451 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14453 error_at (OMP_CLAUSE_LOCATION (c),
14454 "%qE appears more than once in data clauses", t);
14455 remove = true;
14457 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14459 if (ort == C_ORT_ACC)
14460 error_at (OMP_CLAUSE_LOCATION (c),
14461 "%qD appears more than once in data clauses", t);
14462 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14463 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
14464 /* Silently drop the clause. */;
14465 else
14466 error_at (OMP_CLAUSE_LOCATION (c),
14467 "%qD appears both in data and map clauses", t);
14468 remove = true;
14470 else
14471 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14472 break;
14474 case OMP_CLAUSE_LASTPRIVATE:
14475 t = OMP_CLAUSE_DECL (c);
14476 need_complete = true;
14477 need_implicitly_determined = true;
14478 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14480 error_at (OMP_CLAUSE_LOCATION (c),
14481 "%qE is not a variable in clause %<lastprivate%>", t);
14482 remove = true;
14484 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14485 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14487 error_at (OMP_CLAUSE_LOCATION (c),
14488 "%qE appears more than once in data clauses", t);
14489 remove = true;
14491 else
14492 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14493 break;
14495 case OMP_CLAUSE_ALIGNED:
14496 t = OMP_CLAUSE_DECL (c);
14497 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14499 error_at (OMP_CLAUSE_LOCATION (c),
14500 "%qE is not a variable in %<aligned%> clause", t);
14501 remove = true;
14503 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14504 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14506 error_at (OMP_CLAUSE_LOCATION (c),
14507 "%qE in %<aligned%> clause is neither a pointer nor "
14508 "an array", t);
14509 remove = true;
14511 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14513 error_at (OMP_CLAUSE_LOCATION (c),
14514 "%<_Atomic%> %qD in %<aligned%> clause", t);
14515 remove = true;
14516 break;
14518 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14520 error_at (OMP_CLAUSE_LOCATION (c),
14521 "%qE appears more than once in %<aligned%> clauses",
14523 remove = true;
14525 else
14526 bitmap_set_bit (&aligned_head, DECL_UID (t));
14527 break;
14529 case OMP_CLAUSE_NONTEMPORAL:
14530 t = OMP_CLAUSE_DECL (c);
14531 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14533 error_at (OMP_CLAUSE_LOCATION (c),
14534 "%qE is not a variable in %<nontemporal%> clause", t);
14535 remove = true;
14537 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14539 error_at (OMP_CLAUSE_LOCATION (c),
14540 "%qE appears more than once in %<nontemporal%> "
14541 "clauses", t);
14542 remove = true;
14544 else
14545 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14546 break;
14548 case OMP_CLAUSE_ALLOCATE:
14549 t = OMP_CLAUSE_DECL (c);
14550 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14552 error_at (OMP_CLAUSE_LOCATION (c),
14553 "%qE is not a variable in %<allocate%> clause", t);
14554 remove = true;
14556 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14558 warning_at (OMP_CLAUSE_LOCATION (c), 0,
14559 "%qE appears more than once in %<allocate%> clauses",
14561 remove = true;
14563 else
14565 bitmap_set_bit (&aligned_head, DECL_UID (t));
14566 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
14567 allocate_seen = true;
14569 break;
14571 case OMP_CLAUSE_DEPEND:
14572 t = OMP_CLAUSE_DECL (c);
14573 if (t == NULL_TREE)
14575 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14576 == OMP_CLAUSE_DEPEND_SOURCE);
14577 break;
14579 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14581 gcc_assert (TREE_CODE (t) == TREE_LIST);
14582 for (; t; t = TREE_CHAIN (t))
14584 tree decl = TREE_VALUE (t);
14585 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14587 tree offset = TREE_PURPOSE (t);
14588 bool neg = wi::neg_p (wi::to_wide (offset));
14589 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14590 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14591 neg ? MINUS_EXPR : PLUS_EXPR,
14592 decl, offset);
14593 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14594 sizetype,
14595 fold_convert (sizetype, t2),
14596 fold_convert (sizetype, decl));
14597 if (t2 == error_mark_node)
14599 remove = true;
14600 break;
14602 TREE_PURPOSE (t) = t2;
14605 break;
14607 /* FALLTHRU */
14608 case OMP_CLAUSE_AFFINITY:
14609 t = OMP_CLAUSE_DECL (c);
14610 if (TREE_CODE (t) == TREE_LIST
14611 && TREE_PURPOSE (t)
14612 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14614 if (TREE_PURPOSE (t) != last_iterators)
14615 last_iterators_remove
14616 = c_omp_finish_iterators (TREE_PURPOSE (t));
14617 last_iterators = TREE_PURPOSE (t);
14618 t = TREE_VALUE (t);
14619 if (last_iterators_remove)
14620 t = error_mark_node;
14622 else
14623 last_iterators = NULL_TREE;
14624 if (TREE_CODE (t) == TREE_LIST)
14626 if (handle_omp_array_sections (c, ort))
14627 remove = true;
14628 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14629 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14631 error_at (OMP_CLAUSE_LOCATION (c),
14632 "%<depend%> clause with %<depobj%> dependence "
14633 "type on array section");
14634 remove = true;
14636 break;
14638 if (t == error_mark_node)
14639 remove = true;
14640 else if (!lvalue_p (t))
14642 error_at (OMP_CLAUSE_LOCATION (c),
14643 "%qE is not lvalue expression nor array section in "
14644 "%qs clause", t,
14645 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14646 remove = true;
14648 else if (TREE_CODE (t) == COMPONENT_REF
14649 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14651 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14652 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
14653 error_at (OMP_CLAUSE_LOCATION (c),
14654 "bit-field %qE in %qs clause", t,
14655 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14656 remove = true;
14658 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14659 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14661 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14663 error_at (OMP_CLAUSE_LOCATION (c),
14664 "%qE does not have %<omp_depend_t%> type in "
14665 "%<depend%> clause with %<depobj%> dependence "
14666 "type", t);
14667 remove = true;
14670 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14671 && c_omp_depend_t_p (TREE_TYPE (t)))
14673 error_at (OMP_CLAUSE_LOCATION (c),
14674 "%qE should not have %<omp_depend_t%> type in "
14675 "%<depend%> clause with dependence type other than "
14676 "%<depobj%>", t);
14677 remove = true;
14679 if (!remove)
14681 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14682 t, false);
14683 if (addr == error_mark_node)
14684 remove = true;
14685 else
14687 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14688 RO_UNARY_STAR);
14689 if (t == error_mark_node)
14690 remove = true;
14691 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14692 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14693 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14694 == TREE_VEC))
14695 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14696 else
14697 OMP_CLAUSE_DECL (c) = t;
14700 break;
14702 case OMP_CLAUSE_MAP:
14703 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
14704 goto move_implicit;
14705 /* FALLTHRU */
14706 case OMP_CLAUSE_TO:
14707 case OMP_CLAUSE_FROM:
14708 case OMP_CLAUSE__CACHE_:
14709 t = OMP_CLAUSE_DECL (c);
14710 if (TREE_CODE (t) == TREE_LIST)
14712 if (handle_omp_array_sections (c, ort))
14713 remove = true;
14714 else
14716 t = OMP_CLAUSE_DECL (c);
14717 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14719 error_at (OMP_CLAUSE_LOCATION (c),
14720 "array section does not have mappable type "
14721 "in %qs clause",
14722 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14723 remove = true;
14725 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14727 error_at (OMP_CLAUSE_LOCATION (c),
14728 "%<_Atomic%> %qE in %qs clause", t,
14729 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14730 remove = true;
14732 while (TREE_CODE (t) == ARRAY_REF)
14733 t = TREE_OPERAND (t, 0);
14734 if (TREE_CODE (t) == COMPONENT_REF
14735 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14737 while (TREE_CODE (t) == COMPONENT_REF)
14738 t = TREE_OPERAND (t, 0);
14739 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14740 && OMP_CLAUSE_MAP_IMPLICIT (c)
14741 && (bitmap_bit_p (&map_head, DECL_UID (t))
14742 || bitmap_bit_p (&map_field_head, DECL_UID (t))
14743 || bitmap_bit_p (&map_firstprivate_head,
14744 DECL_UID (t))))
14746 remove = true;
14747 break;
14749 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14750 break;
14751 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14753 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14754 error_at (OMP_CLAUSE_LOCATION (c),
14755 "%qD appears more than once in motion "
14756 "clauses", t);
14757 else if (ort == C_ORT_ACC)
14758 error_at (OMP_CLAUSE_LOCATION (c),
14759 "%qD appears more than once in data "
14760 "clauses", t);
14761 else
14762 error_at (OMP_CLAUSE_LOCATION (c),
14763 "%qD appears more than once in map "
14764 "clauses", t);
14765 remove = true;
14767 else
14769 bitmap_set_bit (&map_head, DECL_UID (t));
14770 bitmap_set_bit (&map_field_head, DECL_UID (t));
14774 if (c_oacc_check_attachments (c))
14775 remove = true;
14776 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14777 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14778 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14779 /* In this case, we have a single array element which is a
14780 pointer, and we already set OMP_CLAUSE_SIZE in
14781 handle_omp_array_sections above. For attach/detach clauses,
14782 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
14783 here. */
14784 OMP_CLAUSE_SIZE (c) = size_zero_node;
14785 break;
14787 if (t == error_mark_node)
14789 remove = true;
14790 break;
14792 /* OpenACC attach / detach clauses must be pointers. */
14793 if (c_oacc_check_attachments (c))
14795 remove = true;
14796 break;
14798 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14799 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14800 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14801 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
14802 bias) to zero here, so it is not set erroneously to the pointer
14803 size later on in gimplify.c. */
14804 OMP_CLAUSE_SIZE (c) = size_zero_node;
14805 if (TREE_CODE (t) == COMPONENT_REF
14806 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14808 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14810 error_at (OMP_CLAUSE_LOCATION (c),
14811 "bit-field %qE in %qs clause",
14812 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14813 remove = true;
14815 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14817 error_at (OMP_CLAUSE_LOCATION (c),
14818 "%qE does not have a mappable type in %qs clause",
14819 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14820 remove = true;
14822 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14824 error_at (OMP_CLAUSE_LOCATION (c),
14825 "%<_Atomic%> %qE in %qs clause", t,
14826 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14827 remove = true;
14829 while (TREE_CODE (t) == COMPONENT_REF)
14831 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14832 == UNION_TYPE)
14834 error_at (OMP_CLAUSE_LOCATION (c),
14835 "%qE is a member of a union", t);
14836 remove = true;
14837 break;
14839 t = TREE_OPERAND (t, 0);
14840 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
14842 if (maybe_ne (mem_ref_offset (t), 0))
14843 error_at (OMP_CLAUSE_LOCATION (c),
14844 "cannot dereference %qE in %qs clause", t,
14845 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14846 else
14847 t = TREE_OPERAND (t, 0);
14850 if (remove)
14851 break;
14852 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14854 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
14855 || (ort == C_ORT_OMP
14856 && bitmap_bit_p (&map_head, DECL_UID (t))))
14857 break;
14860 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14862 error_at (OMP_CLAUSE_LOCATION (c),
14863 "%qE is not a variable in %qs clause", t,
14864 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14865 remove = true;
14867 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14869 error_at (OMP_CLAUSE_LOCATION (c),
14870 "%qD is threadprivate variable in %qs clause", t,
14871 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14872 remove = true;
14874 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14875 || (OMP_CLAUSE_MAP_KIND (c)
14876 != GOMP_MAP_FIRSTPRIVATE_POINTER))
14877 && !c_mark_addressable (t))
14878 remove = true;
14879 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14880 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14881 || (OMP_CLAUSE_MAP_KIND (c)
14882 == GOMP_MAP_FIRSTPRIVATE_POINTER)
14883 || (OMP_CLAUSE_MAP_KIND (c)
14884 == GOMP_MAP_FORCE_DEVICEPTR)))
14885 && t == OMP_CLAUSE_DECL (c)
14886 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14888 error_at (OMP_CLAUSE_LOCATION (c),
14889 "%qD does not have a mappable type in %qs clause", t,
14890 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14891 remove = true;
14893 else if (TREE_TYPE (t) == error_mark_node)
14894 remove = true;
14895 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14897 error_at (OMP_CLAUSE_LOCATION (c),
14898 "%<_Atomic%> %qE in %qs clause", t,
14899 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14900 remove = true;
14902 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14903 && OMP_CLAUSE_MAP_IMPLICIT (c)
14904 && (bitmap_bit_p (&map_head, DECL_UID (t))
14905 || bitmap_bit_p (&map_field_head, DECL_UID (t))
14906 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
14907 remove = true;
14908 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14909 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14911 if (bitmap_bit_p (&generic_head, DECL_UID (t))
14912 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14914 error_at (OMP_CLAUSE_LOCATION (c),
14915 "%qD appears more than once in data clauses", t);
14916 remove = true;
14918 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14920 if (ort == C_ORT_ACC)
14921 error_at (OMP_CLAUSE_LOCATION (c),
14922 "%qD appears more than once in data clauses", t);
14923 else
14924 error_at (OMP_CLAUSE_LOCATION (c),
14925 "%qD appears both in data and map clauses", t);
14926 remove = true;
14928 else
14930 bitmap_set_bit (&generic_head, DECL_UID (t));
14931 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
14934 else if (bitmap_bit_p (&map_head, DECL_UID (t))
14935 && (ort != C_ORT_OMP
14936 || !bitmap_bit_p (&map_field_head, DECL_UID (t))))
14938 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14939 error_at (OMP_CLAUSE_LOCATION (c),
14940 "%qD appears more than once in motion clauses", t);
14941 else if (ort == C_ORT_ACC)
14942 error_at (OMP_CLAUSE_LOCATION (c),
14943 "%qD appears more than once in data clauses", t);
14944 else
14945 error_at (OMP_CLAUSE_LOCATION (c),
14946 "%qD appears more than once in map clauses", t);
14947 remove = true;
14949 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14950 && ort == C_ORT_ACC)
14952 error_at (OMP_CLAUSE_LOCATION (c),
14953 "%qD appears more than once in data clauses", t);
14954 remove = true;
14956 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14958 if (ort == C_ORT_ACC)
14959 error_at (OMP_CLAUSE_LOCATION (c),
14960 "%qD appears more than once in data clauses", t);
14961 else
14962 error_at (OMP_CLAUSE_LOCATION (c),
14963 "%qD appears both in data and map clauses", t);
14964 remove = true;
14966 else
14968 bitmap_set_bit (&map_head, DECL_UID (t));
14969 if (t != OMP_CLAUSE_DECL (c)
14970 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14971 bitmap_set_bit (&map_field_head, DECL_UID (t));
14973 break;
14975 case OMP_CLAUSE_TO_DECLARE:
14976 case OMP_CLAUSE_LINK:
14977 t = OMP_CLAUSE_DECL (c);
14978 if (TREE_CODE (t) == FUNCTION_DECL
14979 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14981 else if (!VAR_P (t))
14983 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14984 error_at (OMP_CLAUSE_LOCATION (c),
14985 "%qE is neither a variable nor a function name in "
14986 "clause %qs", t,
14987 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14988 else
14989 error_at (OMP_CLAUSE_LOCATION (c),
14990 "%qE is not a variable in clause %qs", t,
14991 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14992 remove = true;
14994 else if (DECL_THREAD_LOCAL_P (t))
14996 error_at (OMP_CLAUSE_LOCATION (c),
14997 "%qD is threadprivate variable in %qs clause", t,
14998 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14999 remove = true;
15001 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
15003 error_at (OMP_CLAUSE_LOCATION (c),
15004 "%qD does not have a mappable type in %qs clause", t,
15005 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15006 remove = true;
15008 if (remove)
15009 break;
15010 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15012 error_at (OMP_CLAUSE_LOCATION (c),
15013 "%qE appears more than once on the same "
15014 "%<declare target%> directive", t);
15015 remove = true;
15017 else
15018 bitmap_set_bit (&generic_head, DECL_UID (t));
15019 break;
15021 case OMP_CLAUSE_UNIFORM:
15022 t = OMP_CLAUSE_DECL (c);
15023 if (TREE_CODE (t) != PARM_DECL)
15025 if (DECL_P (t))
15026 error_at (OMP_CLAUSE_LOCATION (c),
15027 "%qD is not an argument in %<uniform%> clause", t);
15028 else
15029 error_at (OMP_CLAUSE_LOCATION (c),
15030 "%qE is not an argument in %<uniform%> clause", t);
15031 remove = true;
15032 break;
15034 /* map_head bitmap is used as uniform_head if declare_simd. */
15035 bitmap_set_bit (&map_head, DECL_UID (t));
15036 goto check_dup_generic;
15038 case OMP_CLAUSE_IS_DEVICE_PTR:
15039 case OMP_CLAUSE_USE_DEVICE_PTR:
15040 t = OMP_CLAUSE_DECL (c);
15041 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15043 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15044 && ort == C_ORT_OMP)
15046 error_at (OMP_CLAUSE_LOCATION (c),
15047 "%qs variable is not a pointer",
15048 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15049 remove = true;
15051 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15053 error_at (OMP_CLAUSE_LOCATION (c),
15054 "%qs variable is neither a pointer nor an array",
15055 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15056 remove = true;
15059 goto check_dup_generic;
15061 case OMP_CLAUSE_USE_DEVICE_ADDR:
15062 t = OMP_CLAUSE_DECL (c);
15063 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15064 c_mark_addressable (t);
15065 goto check_dup_generic;
15067 case OMP_CLAUSE_NOWAIT:
15068 if (copyprivate_seen)
15070 error_at (OMP_CLAUSE_LOCATION (c),
15071 "%<nowait%> clause must not be used together "
15072 "with %<copyprivate%>");
15073 remove = true;
15074 break;
15076 nowait_clause = pc;
15077 pc = &OMP_CLAUSE_CHAIN (c);
15078 continue;
15080 case OMP_CLAUSE_ORDER:
15081 if (ordered_clause)
15083 error_at (OMP_CLAUSE_LOCATION (c),
15084 "%<order%> clause must not be used together "
15085 "with %<ordered%>");
15086 remove = true;
15087 break;
15089 else if (order_clause)
15091 /* Silently remove duplicates. */
15092 remove = true;
15093 break;
15095 order_clause = pc;
15096 pc = &OMP_CLAUSE_CHAIN (c);
15097 continue;
15099 case OMP_CLAUSE_DETACH:
15100 t = OMP_CLAUSE_DECL (c);
15101 if (detach_seen)
15103 error_at (OMP_CLAUSE_LOCATION (c),
15104 "too many %qs clauses on a task construct",
15105 "detach");
15106 remove = true;
15107 break;
15109 detach_seen = pc;
15110 pc = &OMP_CLAUSE_CHAIN (c);
15111 c_mark_addressable (t);
15112 continue;
15114 case OMP_CLAUSE_IF:
15115 case OMP_CLAUSE_NUM_THREADS:
15116 case OMP_CLAUSE_NUM_TEAMS:
15117 case OMP_CLAUSE_THREAD_LIMIT:
15118 case OMP_CLAUSE_DEFAULT:
15119 case OMP_CLAUSE_UNTIED:
15120 case OMP_CLAUSE_COLLAPSE:
15121 case OMP_CLAUSE_FINAL:
15122 case OMP_CLAUSE_DEVICE:
15123 case OMP_CLAUSE_DIST_SCHEDULE:
15124 case OMP_CLAUSE_PARALLEL:
15125 case OMP_CLAUSE_FOR:
15126 case OMP_CLAUSE_SECTIONS:
15127 case OMP_CLAUSE_TASKGROUP:
15128 case OMP_CLAUSE_PROC_BIND:
15129 case OMP_CLAUSE_DEVICE_TYPE:
15130 case OMP_CLAUSE_PRIORITY:
15131 case OMP_CLAUSE_GRAINSIZE:
15132 case OMP_CLAUSE_NUM_TASKS:
15133 case OMP_CLAUSE_THREADS:
15134 case OMP_CLAUSE_SIMD:
15135 case OMP_CLAUSE_HINT:
15136 case OMP_CLAUSE_DEFAULTMAP:
15137 case OMP_CLAUSE_BIND:
15138 case OMP_CLAUSE_NUM_GANGS:
15139 case OMP_CLAUSE_NUM_WORKERS:
15140 case OMP_CLAUSE_VECTOR_LENGTH:
15141 case OMP_CLAUSE_ASYNC:
15142 case OMP_CLAUSE_WAIT:
15143 case OMP_CLAUSE_AUTO:
15144 case OMP_CLAUSE_INDEPENDENT:
15145 case OMP_CLAUSE_SEQ:
15146 case OMP_CLAUSE_GANG:
15147 case OMP_CLAUSE_WORKER:
15148 case OMP_CLAUSE_VECTOR:
15149 case OMP_CLAUSE_TILE:
15150 case OMP_CLAUSE_IF_PRESENT:
15151 case OMP_CLAUSE_FINALIZE:
15152 pc = &OMP_CLAUSE_CHAIN (c);
15153 continue;
15155 case OMP_CLAUSE_MERGEABLE:
15156 mergeable_seen = true;
15157 pc = &OMP_CLAUSE_CHAIN (c);
15158 continue;
15160 case OMP_CLAUSE_NOGROUP:
15161 nogroup_seen = pc;
15162 pc = &OMP_CLAUSE_CHAIN (c);
15163 continue;
15165 case OMP_CLAUSE_SCHEDULE:
15166 schedule_clause = c;
15167 pc = &OMP_CLAUSE_CHAIN (c);
15168 continue;
15170 case OMP_CLAUSE_ORDERED:
15171 ordered_clause = c;
15172 if (order_clause)
15174 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15175 "%<order%> clause must not be used together "
15176 "with %<ordered%>");
15177 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15178 order_clause = NULL;
15180 pc = &OMP_CLAUSE_CHAIN (c);
15181 continue;
15183 case OMP_CLAUSE_SAFELEN:
15184 safelen = c;
15185 pc = &OMP_CLAUSE_CHAIN (c);
15186 continue;
15187 case OMP_CLAUSE_SIMDLEN:
15188 simdlen = c;
15189 pc = &OMP_CLAUSE_CHAIN (c);
15190 continue;
15192 case OMP_CLAUSE_INBRANCH:
15193 case OMP_CLAUSE_NOTINBRANCH:
15194 if (branch_seen)
15196 error_at (OMP_CLAUSE_LOCATION (c),
15197 "%<inbranch%> clause is incompatible with "
15198 "%<notinbranch%>");
15199 remove = true;
15200 break;
15202 branch_seen = true;
15203 pc = &OMP_CLAUSE_CHAIN (c);
15204 continue;
15206 case OMP_CLAUSE_INCLUSIVE:
15207 case OMP_CLAUSE_EXCLUSIVE:
15208 need_complete = true;
15209 need_implicitly_determined = true;
15210 t = OMP_CLAUSE_DECL (c);
15211 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15213 error_at (OMP_CLAUSE_LOCATION (c),
15214 "%qE is not a variable in clause %qs", t,
15215 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15216 remove = true;
15218 break;
15220 default:
15221 gcc_unreachable ();
15224 if (!remove)
15226 t = OMP_CLAUSE_DECL (c);
15228 if (need_complete)
15230 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15231 if (t == error_mark_node)
15232 remove = true;
15235 if (need_implicitly_determined)
15237 const char *share_name = NULL;
15239 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15240 share_name = "threadprivate";
15241 else switch (c_omp_predetermined_sharing (t))
15243 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15244 break;
15245 case OMP_CLAUSE_DEFAULT_SHARED:
15246 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15247 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15248 && c_omp_predefined_variable (t))
15249 /* The __func__ variable and similar function-local
15250 predefined variables may be listed in a shared or
15251 firstprivate clause. */
15252 break;
15253 share_name = "shared";
15254 break;
15255 case OMP_CLAUSE_DEFAULT_PRIVATE:
15256 share_name = "private";
15257 break;
15258 default:
15259 gcc_unreachable ();
15261 if (share_name)
15263 error_at (OMP_CLAUSE_LOCATION (c),
15264 "%qE is predetermined %qs for %qs",
15265 t, share_name,
15266 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15267 remove = true;
15269 else if (TREE_READONLY (t)
15270 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15271 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15273 error_at (OMP_CLAUSE_LOCATION (c),
15274 "%<const%> qualified %qE may appear only in "
15275 "%<shared%> or %<firstprivate%> clauses", t);
15276 remove = true;
15281 if (remove)
15282 *pc = OMP_CLAUSE_CHAIN (c);
15283 else
15284 pc = &OMP_CLAUSE_CHAIN (c);
15287 if (simdlen
15288 && safelen
15289 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15290 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15292 error_at (OMP_CLAUSE_LOCATION (simdlen),
15293 "%<simdlen%> clause value is bigger than "
15294 "%<safelen%> clause value");
15295 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15296 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15299 if (ordered_clause
15300 && schedule_clause
15301 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15302 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15304 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15305 "%<nonmonotonic%> schedule modifier specified together "
15306 "with %<ordered%> clause");
15307 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15308 = (enum omp_clause_schedule_kind)
15309 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15310 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15313 if (reduction_seen < 0 && ordered_clause)
15315 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15316 "%qs clause specified together with %<inscan%> "
15317 "%<reduction%> clause", "ordered");
15318 reduction_seen = -2;
15321 if (reduction_seen < 0 && schedule_clause)
15323 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15324 "%qs clause specified together with %<inscan%> "
15325 "%<reduction%> clause", "schedule");
15326 reduction_seen = -2;
15329 if (linear_variable_step_check || reduction_seen == -2 || allocate_seen)
15330 for (pc = &clauses, c = clauses; c ; c = *pc)
15332 bool remove = false;
15333 if (allocate_seen)
15334 switch (OMP_CLAUSE_CODE (c))
15336 case OMP_CLAUSE_REDUCTION:
15337 case OMP_CLAUSE_IN_REDUCTION:
15338 case OMP_CLAUSE_TASK_REDUCTION:
15339 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
15341 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
15342 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15343 t = TREE_OPERAND (t, 0);
15344 if (TREE_CODE (t) == ADDR_EXPR
15345 || TREE_CODE (t) == INDIRECT_REF)
15346 t = TREE_OPERAND (t, 0);
15347 if (DECL_P (t))
15348 bitmap_clear_bit (&aligned_head, DECL_UID (t));
15349 break;
15351 /* FALLTHRU */
15352 case OMP_CLAUSE_PRIVATE:
15353 case OMP_CLAUSE_FIRSTPRIVATE:
15354 case OMP_CLAUSE_LASTPRIVATE:
15355 case OMP_CLAUSE_LINEAR:
15356 if (DECL_P (OMP_CLAUSE_DECL (c)))
15357 bitmap_clear_bit (&aligned_head,
15358 DECL_UID (OMP_CLAUSE_DECL (c)));
15359 break;
15360 default:
15361 break;
15363 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15364 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15365 && !bitmap_bit_p (&map_head,
15366 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15368 error_at (OMP_CLAUSE_LOCATION (c),
15369 "%<linear%> clause step is a parameter %qD not "
15370 "specified in %<uniform%> clause",
15371 OMP_CLAUSE_LINEAR_STEP (c));
15372 remove = true;
15374 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15375 && reduction_seen == -2)
15376 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15378 if (remove)
15379 *pc = OMP_CLAUSE_CHAIN (c);
15380 else
15381 pc = &OMP_CLAUSE_CHAIN (c);
15384 if (allocate_seen)
15385 for (pc = &clauses, c = clauses; c ; c = *pc)
15387 bool remove = false;
15388 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
15389 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
15390 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
15392 error_at (OMP_CLAUSE_LOCATION (c),
15393 "%qD specified in %<allocate%> clause but not in "
15394 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
15395 remove = true;
15397 if (remove)
15398 *pc = OMP_CLAUSE_CHAIN (c);
15399 else
15400 pc = &OMP_CLAUSE_CHAIN (c);
15403 if (nogroup_seen && reduction_seen)
15405 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15406 "%<nogroup%> clause must not be used together with "
15407 "%<reduction%> clause");
15408 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15411 if (detach_seen)
15413 if (mergeable_seen)
15415 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
15416 "%<detach%> clause must not be used together with "
15417 "%<mergeable%> clause");
15418 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
15420 else
15422 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
15424 for (pc = &clauses, c = clauses; c ; c = *pc)
15426 bool remove = false;
15427 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15428 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15429 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
15430 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
15431 && OMP_CLAUSE_DECL (c) == detach_decl)
15433 error_at (OMP_CLAUSE_LOCATION (c),
15434 "the event handle of a %<detach%> clause "
15435 "should not be in a data-sharing clause");
15436 remove = true;
15438 if (remove)
15439 *pc = OMP_CLAUSE_CHAIN (c);
15440 else
15441 pc = &OMP_CLAUSE_CHAIN (c);
15446 bitmap_obstack_release (NULL);
15447 return clauses;
15450 /* Return code to initialize DST with a copy constructor from SRC.
15451 C doesn't have copy constructors nor assignment operators, only for
15452 _Atomic vars we need to perform __atomic_load from src into a temporary
15453 followed by __atomic_store of the temporary to dst. */
15455 tree
15456 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15458 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15459 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15461 location_t loc = OMP_CLAUSE_LOCATION (clause);
15462 tree type = TREE_TYPE (dst);
15463 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15464 tree tmp = create_tmp_var (nonatomic_type);
15465 tree tmp_addr = build_fold_addr_expr (tmp);
15466 TREE_ADDRESSABLE (tmp) = 1;
15467 TREE_NO_WARNING (tmp) = 1;
15468 tree src_addr = build_fold_addr_expr (src);
15469 tree dst_addr = build_fold_addr_expr (dst);
15470 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15471 vec<tree, va_gc> *params;
15472 /* Expansion of a generic atomic load may require an addition
15473 element, so allocate enough to prevent a resize. */
15474 vec_alloc (params, 4);
15476 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15477 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15478 params->quick_push (src_addr);
15479 params->quick_push (tmp_addr);
15480 params->quick_push (seq_cst);
15481 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15483 vec_alloc (params, 4);
15485 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15486 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15487 params->quick_push (dst_addr);
15488 params->quick_push (tmp_addr);
15489 params->quick_push (seq_cst);
15490 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15491 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15494 /* Create a transaction node. */
15496 tree
15497 c_finish_transaction (location_t loc, tree block, int flags)
15499 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15500 if (flags & TM_STMT_ATTR_OUTER)
15501 TRANSACTION_EXPR_OUTER (stmt) = 1;
15502 if (flags & TM_STMT_ATTR_RELAXED)
15503 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15504 return add_stmt (stmt);
15507 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15508 down to the element type of an array. If ORIG_QUAL_TYPE is not
15509 NULL, then it should be used as the qualified type
15510 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15511 preserve information about the typedef name from which an array
15512 type was derived). */
15514 tree
15515 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15516 size_t orig_qual_indirect)
15518 if (type == error_mark_node)
15519 return type;
15521 if (TREE_CODE (type) == ARRAY_TYPE)
15523 tree t;
15524 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15525 type_quals, orig_qual_type,
15526 orig_qual_indirect - 1);
15528 /* See if we already have an identically qualified type. */
15529 if (orig_qual_type && orig_qual_indirect == 0)
15530 t = orig_qual_type;
15531 else
15532 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15534 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15535 && TYPE_NAME (t) == TYPE_NAME (type)
15536 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15537 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15538 TYPE_ATTRIBUTES (type)))
15539 break;
15541 if (!t)
15543 tree domain = TYPE_DOMAIN (type);
15545 t = build_variant_type_copy (type);
15546 TREE_TYPE (t) = element_type;
15548 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15549 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15550 SET_TYPE_STRUCTURAL_EQUALITY (t);
15551 else if (TYPE_CANONICAL (element_type) != element_type
15552 || (domain && TYPE_CANONICAL (domain) != domain))
15554 tree unqualified_canon
15555 = build_array_type (TYPE_CANONICAL (element_type),
15556 domain? TYPE_CANONICAL (domain)
15557 : NULL_TREE);
15558 if (TYPE_REVERSE_STORAGE_ORDER (type))
15560 unqualified_canon
15561 = build_distinct_type_copy (unqualified_canon);
15562 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15564 TYPE_CANONICAL (t)
15565 = c_build_qualified_type (unqualified_canon, type_quals);
15567 else
15568 TYPE_CANONICAL (t) = t;
15570 return t;
15573 /* A restrict-qualified pointer type must be a pointer to object or
15574 incomplete type. Note that the use of POINTER_TYPE_P also allows
15575 REFERENCE_TYPEs, which is appropriate for C++. */
15576 if ((type_quals & TYPE_QUAL_RESTRICT)
15577 && (!POINTER_TYPE_P (type)
15578 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15580 error ("invalid use of %<restrict%>");
15581 type_quals &= ~TYPE_QUAL_RESTRICT;
15584 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15585 ? orig_qual_type
15586 : build_qualified_type (type, type_quals));
15587 /* A variant type does not inherit the list of incomplete vars from the
15588 type main variant. */
15589 if ((RECORD_OR_UNION_TYPE_P (var_type)
15590 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15591 && TYPE_MAIN_VARIANT (var_type) != var_type)
15592 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15593 return var_type;
15596 /* Build a VA_ARG_EXPR for the C parser. */
15598 tree
15599 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15601 if (error_operand_p (type))
15602 return error_mark_node;
15603 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15604 order because it takes the address of the expression. */
15605 else if (handled_component_p (expr)
15606 && reverse_storage_order_for_component_p (expr))
15608 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15609 return error_mark_node;
15611 else if (!COMPLETE_TYPE_P (type))
15613 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15614 "type %qT", type);
15615 return error_mark_node;
15617 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15618 warning_at (loc2, OPT_Wc___compat,
15619 "C++ requires promoted type, not enum type, in %<va_arg%>");
15620 return build_va_arg (loc2, expr, type);
15623 /* Return truthvalue of whether T1 is the same tree structure as T2.
15624 Return 1 if they are the same. Return false if they are different. */
15626 bool
15627 c_tree_equal (tree t1, tree t2)
15629 enum tree_code code1, code2;
15631 if (t1 == t2)
15632 return true;
15633 if (!t1 || !t2)
15634 return false;
15636 for (code1 = TREE_CODE (t1);
15637 CONVERT_EXPR_CODE_P (code1)
15638 || code1 == NON_LVALUE_EXPR;
15639 code1 = TREE_CODE (t1))
15640 t1 = TREE_OPERAND (t1, 0);
15641 for (code2 = TREE_CODE (t2);
15642 CONVERT_EXPR_CODE_P (code2)
15643 || code2 == NON_LVALUE_EXPR;
15644 code2 = TREE_CODE (t2))
15645 t2 = TREE_OPERAND (t2, 0);
15647 /* They might have become equal now. */
15648 if (t1 == t2)
15649 return true;
15651 if (code1 != code2)
15652 return false;
15654 switch (code1)
15656 case INTEGER_CST:
15657 return wi::to_wide (t1) == wi::to_wide (t2);
15659 case REAL_CST:
15660 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15662 case STRING_CST:
15663 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15664 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15665 TREE_STRING_LENGTH (t1));
15667 case FIXED_CST:
15668 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15669 TREE_FIXED_CST (t2));
15671 case COMPLEX_CST:
15672 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15673 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15675 case VECTOR_CST:
15676 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15678 case CONSTRUCTOR:
15679 /* We need to do this when determining whether or not two
15680 non-type pointer to member function template arguments
15681 are the same. */
15682 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15683 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15684 return false;
15686 tree field, value;
15687 unsigned int i;
15688 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15690 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15691 if (!c_tree_equal (field, elt2->index)
15692 || !c_tree_equal (value, elt2->value))
15693 return false;
15696 return true;
15698 case TREE_LIST:
15699 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15700 return false;
15701 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15702 return false;
15703 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15705 case SAVE_EXPR:
15706 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15708 case CALL_EXPR:
15710 tree arg1, arg2;
15711 call_expr_arg_iterator iter1, iter2;
15712 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15713 return false;
15714 for (arg1 = first_call_expr_arg (t1, &iter1),
15715 arg2 = first_call_expr_arg (t2, &iter2);
15716 arg1 && arg2;
15717 arg1 = next_call_expr_arg (&iter1),
15718 arg2 = next_call_expr_arg (&iter2))
15719 if (!c_tree_equal (arg1, arg2))
15720 return false;
15721 if (arg1 || arg2)
15722 return false;
15723 return true;
15726 case TARGET_EXPR:
15728 tree o1 = TREE_OPERAND (t1, 0);
15729 tree o2 = TREE_OPERAND (t2, 0);
15731 /* Special case: if either target is an unallocated VAR_DECL,
15732 it means that it's going to be unified with whatever the
15733 TARGET_EXPR is really supposed to initialize, so treat it
15734 as being equivalent to anything. */
15735 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15736 && !DECL_RTL_SET_P (o1))
15737 /*Nop*/;
15738 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15739 && !DECL_RTL_SET_P (o2))
15740 /*Nop*/;
15741 else if (!c_tree_equal (o1, o2))
15742 return false;
15744 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15747 case COMPONENT_REF:
15748 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15749 return false;
15750 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15752 case PARM_DECL:
15753 case VAR_DECL:
15754 case CONST_DECL:
15755 case FIELD_DECL:
15756 case FUNCTION_DECL:
15757 case IDENTIFIER_NODE:
15758 case SSA_NAME:
15759 return false;
15761 case TREE_VEC:
15763 unsigned ix;
15764 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15765 return false;
15766 for (ix = TREE_VEC_LENGTH (t1); ix--;)
15767 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15768 TREE_VEC_ELT (t2, ix)))
15769 return false;
15770 return true;
15773 default:
15774 break;
15777 switch (TREE_CODE_CLASS (code1))
15779 case tcc_unary:
15780 case tcc_binary:
15781 case tcc_comparison:
15782 case tcc_expression:
15783 case tcc_vl_exp:
15784 case tcc_reference:
15785 case tcc_statement:
15787 int i, n = TREE_OPERAND_LENGTH (t1);
15789 switch (code1)
15791 case PREINCREMENT_EXPR:
15792 case PREDECREMENT_EXPR:
15793 case POSTINCREMENT_EXPR:
15794 case POSTDECREMENT_EXPR:
15795 n = 1;
15796 break;
15797 case ARRAY_REF:
15798 n = 2;
15799 break;
15800 default:
15801 break;
15804 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15805 && n != TREE_OPERAND_LENGTH (t2))
15806 return false;
15808 for (i = 0; i < n; ++i)
15809 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15810 return false;
15812 return true;
15815 case tcc_type:
15816 return comptypes (t1, t2);
15817 default:
15818 gcc_unreachable ();
15820 /* We can get here with --disable-checking. */
15821 return false;
15824 /* Returns true when the function declaration FNDECL is implicit,
15825 introduced as a result of a call to an otherwise undeclared
15826 function, and false otherwise. */
15828 bool
15829 c_decl_implicit (const_tree fndecl)
15831 return C_DECL_IMPLICIT (fndecl);