Add emergency dump after an ICE
[official-gcc.git] / gcc / c / c-typeck.c
blobbb27099bfe1ccf51ec8b03d73a008e6b22e70809
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
56 /* Possible cases of implicit bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
58 enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_return
65 /* The level of nesting inside "__alignof__". */
66 int in_alignof;
68 /* The level of nesting inside "sizeof". */
69 int in_sizeof;
71 /* The level of nesting inside "typeof". */
72 int in_typeof;
74 /* True when parsing OpenMP loop expressions. */
75 bool c_in_omp_for;
77 /* The argument of last parsed sizeof expression, only to be tested
78 if expr.original_code == SIZEOF_EXPR. */
79 tree c_last_sizeof_arg;
80 location_t c_last_sizeof_loc;
82 /* Nonzero if we might need to print a "missing braces around
83 initializer" message within this initializer. */
84 static int found_missing_braces;
86 static int require_constant_value;
87 static int require_constant_elements;
89 static bool null_pointer_constant_p (const_tree);
90 static tree qualify_type (tree, tree);
91 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93 static int comp_target_types (location_t, tree, tree);
94 static int function_types_compatible_p (const_tree, const_tree, bool *,
95 bool *);
96 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
97 static tree lookup_field (tree, tree);
98 static int convert_arguments (location_t, vec<location_t>, tree,
99 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
100 tree);
101 static tree pointer_diff (location_t, tree, tree, tree *);
102 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
103 enum impl_conv, bool, tree, tree, int,
104 int = 0);
105 static tree valid_compound_expr_initializer (tree, tree);
106 static void push_string (const char *);
107 static void push_member_name (tree);
108 static int spelling_length (void);
109 static char *print_spelling (char *);
110 static void warning_init (location_t, int, const char *);
111 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
112 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
113 bool, struct obstack *);
114 static void output_pending_init_elements (int, struct obstack *);
115 static bool set_designator (location_t, bool, struct obstack *);
116 static void push_range_stack (tree, struct obstack *);
117 static void add_pending_init (location_t, tree, tree, tree, bool,
118 struct obstack *);
119 static void set_nonincremental_init (struct obstack *);
120 static void set_nonincremental_init_from_string (tree, struct obstack *);
121 static tree find_init_member (tree, struct obstack *);
122 static void readonly_warning (tree, enum lvalue_use);
123 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
124 static void record_maybe_used_decl (tree);
125 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
127 /* Return true if EXP is a null pointer constant, false otherwise. */
129 static bool
130 null_pointer_constant_p (const_tree expr)
132 /* This should really operate on c_expr structures, but they aren't
133 yet available everywhere required. */
134 tree type = TREE_TYPE (expr);
135 return (TREE_CODE (expr) == INTEGER_CST
136 && !TREE_OVERFLOW (expr)
137 && integer_zerop (expr)
138 && (INTEGRAL_TYPE_P (type)
139 || (TREE_CODE (type) == POINTER_TYPE
140 && VOID_TYPE_P (TREE_TYPE (type))
141 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
144 /* EXPR may appear in an unevaluated part of an integer constant
145 expression, but not in an evaluated part. Wrap it in a
146 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
147 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
149 static tree
150 note_integer_operands (tree expr)
152 tree ret;
153 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
155 ret = copy_node (expr);
156 TREE_OVERFLOW (ret) = 1;
158 else
160 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
161 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
163 return ret;
166 /* Having checked whether EXPR may appear in an unevaluated part of an
167 integer constant expression and found that it may, remove any
168 C_MAYBE_CONST_EXPR noting this fact and return the resulting
169 expression. */
171 static inline tree
172 remove_c_maybe_const_expr (tree expr)
174 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
175 return C_MAYBE_CONST_EXPR_EXPR (expr);
176 else
177 return expr;
180 \f/* This is a cache to hold if two types are compatible or not. */
182 struct tagged_tu_seen_cache {
183 const struct tagged_tu_seen_cache * next;
184 const_tree t1;
185 const_tree t2;
186 /* The return value of tagged_types_tu_compatible_p if we had seen
187 these two types already. */
188 int val;
191 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
192 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
194 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
195 does not have an incomplete type. (That includes void types.)
196 LOC is the location of the use. */
198 tree
199 require_complete_type (location_t loc, tree value)
201 tree type = TREE_TYPE (value);
203 if (error_operand_p (value))
204 return error_mark_node;
206 /* First, detect a valid value with a complete type. */
207 if (COMPLETE_TYPE_P (type))
208 return value;
210 c_incomplete_type_error (loc, value, type);
211 return error_mark_node;
214 /* Print an error message for invalid use of an incomplete type.
215 VALUE is the expression that was used (or 0 if that isn't known)
216 and TYPE is the type that was invalid. LOC is the location for
217 the error. */
219 void
220 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
222 /* Avoid duplicate error message. */
223 if (TREE_CODE (type) == ERROR_MARK)
224 return;
226 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
227 error_at (loc, "%qD has an incomplete type %qT", value, type);
228 else
230 retry:
231 /* We must print an error message. Be clever about what it says. */
233 switch (TREE_CODE (type))
235 case RECORD_TYPE:
236 case UNION_TYPE:
237 case ENUMERAL_TYPE:
238 break;
240 case VOID_TYPE:
241 error_at (loc, "invalid use of void expression");
242 return;
244 case ARRAY_TYPE:
245 if (TYPE_DOMAIN (type))
247 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
249 error_at (loc, "invalid use of flexible array member");
250 return;
252 type = TREE_TYPE (type);
253 goto retry;
255 error_at (loc, "invalid use of array with unspecified bounds");
256 return;
258 default:
259 gcc_unreachable ();
262 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
263 error_at (loc, "invalid use of undefined type %qT", type);
264 else
265 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
266 error_at (loc, "invalid use of incomplete typedef %qT", type);
270 /* Given a type, apply default promotions wrt unnamed function
271 arguments and return the new type. */
273 tree
274 c_type_promotes_to (tree type)
276 tree ret = NULL_TREE;
278 if (TYPE_MAIN_VARIANT (type) == float_type_node)
279 ret = double_type_node;
280 else if (c_promoting_integer_type_p (type))
282 /* Preserve unsignedness if not really getting any wider. */
283 if (TYPE_UNSIGNED (type)
284 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
285 ret = unsigned_type_node;
286 else
287 ret = integer_type_node;
290 if (ret != NULL_TREE)
291 return (TYPE_ATOMIC (type)
292 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
293 : ret);
295 return type;
298 /* Return true if between two named address spaces, whether there is a superset
299 named address space that encompasses both address spaces. If there is a
300 superset, return which address space is the superset. */
302 static bool
303 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
305 if (as1 == as2)
307 *common = as1;
308 return true;
310 else if (targetm.addr_space.subset_p (as1, as2))
312 *common = as2;
313 return true;
315 else if (targetm.addr_space.subset_p (as2, as1))
317 *common = as1;
318 return true;
320 else
321 return false;
324 /* Return a variant of TYPE which has all the type qualifiers of LIKE
325 as well as those of TYPE. */
327 static tree
328 qualify_type (tree type, tree like)
330 addr_space_t as_type = TYPE_ADDR_SPACE (type);
331 addr_space_t as_like = TYPE_ADDR_SPACE (like);
332 addr_space_t as_common;
334 /* If the two named address spaces are different, determine the common
335 superset address space. If there isn't one, raise an error. */
336 if (!addr_space_superset (as_type, as_like, &as_common))
338 as_common = as_type;
339 error ("%qT and %qT are in disjoint named address spaces",
340 type, like);
343 return c_build_qualified_type (type,
344 TYPE_QUALS_NO_ADDR_SPACE (type)
345 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
346 | ENCODE_QUAL_ADDR_SPACE (as_common));
349 /* Return true iff the given tree T is a variable length array. */
351 bool
352 c_vla_type_p (const_tree t)
354 if (TREE_CODE (t) == ARRAY_TYPE
355 && C_TYPE_VARIABLE_SIZE (t))
356 return true;
357 return false;
360 /* If NTYPE is a type of a non-variadic function with a prototype
361 and OTYPE is a type of a function without a prototype and ATTRS
362 contains attribute format, diagnosess and removes it from ATTRS.
363 Returns the result of build_type_attribute_variant of NTYPE and
364 the (possibly) modified ATTRS. */
366 static tree
367 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
369 if (!prototype_p (otype)
370 && prototype_p (ntype)
371 && lookup_attribute ("format", attrs))
373 warning_at (input_location, OPT_Wattributes,
374 "%qs attribute cannot be applied to a function that "
375 "does not take variable arguments", "format");
376 attrs = remove_attribute ("format", attrs);
378 return build_type_attribute_variant (ntype, attrs);
381 /* Return the composite type of two compatible types.
383 We assume that comptypes has already been done and returned
384 nonzero; if that isn't so, this may crash. In particular, we
385 assume that qualifiers match. */
387 tree
388 composite_type (tree t1, tree t2)
390 enum tree_code code1;
391 enum tree_code code2;
392 tree attributes;
394 /* Save time if the two types are the same. */
396 if (t1 == t2) return t1;
398 /* If one type is nonsense, use the other. */
399 if (t1 == error_mark_node)
400 return t2;
401 if (t2 == error_mark_node)
402 return t1;
404 code1 = TREE_CODE (t1);
405 code2 = TREE_CODE (t2);
407 /* Merge the attributes. */
408 attributes = targetm.merge_type_attributes (t1, t2);
410 /* If one is an enumerated type and the other is the compatible
411 integer type, the composite type might be either of the two
412 (DR#013 question 3). For consistency, use the enumerated type as
413 the composite type. */
415 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
416 return t1;
417 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
418 return t2;
420 gcc_assert (code1 == code2);
422 switch (code1)
424 case POINTER_TYPE:
425 /* For two pointers, do this recursively on the target type. */
427 tree pointed_to_1 = TREE_TYPE (t1);
428 tree pointed_to_2 = TREE_TYPE (t2);
429 tree target = composite_type (pointed_to_1, pointed_to_2);
430 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
431 t1 = build_type_attribute_variant (t1, attributes);
432 return qualify_type (t1, t2);
435 case ARRAY_TYPE:
437 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
438 int quals;
439 tree unqual_elt;
440 tree d1 = TYPE_DOMAIN (t1);
441 tree d2 = TYPE_DOMAIN (t2);
442 bool d1_variable, d2_variable;
443 bool d1_zero, d2_zero;
444 bool t1_complete, t2_complete;
446 /* We should not have any type quals on arrays at all. */
447 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
448 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
450 t1_complete = COMPLETE_TYPE_P (t1);
451 t2_complete = COMPLETE_TYPE_P (t2);
453 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
454 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
456 d1_variable = (!d1_zero
457 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
458 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
459 d2_variable = (!d2_zero
460 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
461 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
462 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
463 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
465 /* Save space: see if the result is identical to one of the args. */
466 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
467 && (d2_variable || d2_zero || !d1_variable))
468 return build_type_attribute_variant (t1, attributes);
469 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
470 && (d1_variable || d1_zero || !d2_variable))
471 return build_type_attribute_variant (t2, attributes);
473 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
474 return build_type_attribute_variant (t1, attributes);
475 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
476 return build_type_attribute_variant (t2, attributes);
478 /* Merge the element types, and have a size if either arg has
479 one. We may have qualifiers on the element types. To set
480 up TYPE_MAIN_VARIANT correctly, we need to form the
481 composite of the unqualified types and add the qualifiers
482 back at the end. */
483 quals = TYPE_QUALS (strip_array_types (elt));
484 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
485 t1 = build_array_type (unqual_elt,
486 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
487 && (d2_variable
488 || d2_zero
489 || !d1_variable))
490 ? t1
491 : t2));
492 /* Ensure a composite type involving a zero-length array type
493 is a zero-length type not an incomplete type. */
494 if (d1_zero && d2_zero
495 && (t1_complete || t2_complete)
496 && !COMPLETE_TYPE_P (t1))
498 TYPE_SIZE (t1) = bitsize_zero_node;
499 TYPE_SIZE_UNIT (t1) = size_zero_node;
501 t1 = c_build_qualified_type (t1, quals);
502 return build_type_attribute_variant (t1, attributes);
505 case ENUMERAL_TYPE:
506 case RECORD_TYPE:
507 case UNION_TYPE:
508 if (attributes != NULL)
510 /* Try harder not to create a new aggregate type. */
511 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
512 return t1;
513 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
514 return t2;
516 return build_type_attribute_variant (t1, attributes);
518 case FUNCTION_TYPE:
519 /* Function types: prefer the one that specified arg types.
520 If both do, merge the arg types. Also merge the return types. */
522 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
523 tree p1 = TYPE_ARG_TYPES (t1);
524 tree p2 = TYPE_ARG_TYPES (t2);
525 int len;
526 tree newargs, n;
527 int i;
529 /* Save space: see if the result is identical to one of the args. */
530 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
531 return build_functype_attribute_variant (t1, t2, attributes);
532 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
533 return build_functype_attribute_variant (t2, t1, attributes);
535 /* Simple way if one arg fails to specify argument types. */
536 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
538 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
539 t1 = build_type_attribute_variant (t1, attributes);
540 return qualify_type (t1, t2);
542 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
544 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
545 t1 = build_type_attribute_variant (t1, attributes);
546 return qualify_type (t1, t2);
549 /* If both args specify argument types, we must merge the two
550 lists, argument by argument. */
552 for (len = 0, newargs = p1;
553 newargs && newargs != void_list_node;
554 len++, newargs = TREE_CHAIN (newargs))
557 for (i = 0; i < len; i++)
558 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
560 n = newargs;
562 for (; p1 && p1 != void_list_node;
563 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
565 /* A null type means arg type is not specified.
566 Take whatever the other function type has. */
567 if (TREE_VALUE (p1) == NULL_TREE)
569 TREE_VALUE (n) = TREE_VALUE (p2);
570 goto parm_done;
572 if (TREE_VALUE (p2) == NULL_TREE)
574 TREE_VALUE (n) = TREE_VALUE (p1);
575 goto parm_done;
578 /* Given wait (union {union wait *u; int *i} *)
579 and wait (union wait *),
580 prefer union wait * as type of parm. */
581 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
582 && TREE_VALUE (p1) != TREE_VALUE (p2))
584 tree memb;
585 tree mv2 = TREE_VALUE (p2);
586 if (mv2 && mv2 != error_mark_node
587 && TREE_CODE (mv2) != ARRAY_TYPE)
588 mv2 = TYPE_MAIN_VARIANT (mv2);
589 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
590 memb; memb = DECL_CHAIN (memb))
592 tree mv3 = TREE_TYPE (memb);
593 if (mv3 && mv3 != error_mark_node
594 && TREE_CODE (mv3) != ARRAY_TYPE)
595 mv3 = TYPE_MAIN_VARIANT (mv3);
596 if (comptypes (mv3, mv2))
598 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
599 TREE_VALUE (p2));
600 pedwarn (input_location, OPT_Wpedantic,
601 "function types not truly compatible in ISO C");
602 goto parm_done;
606 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
607 && TREE_VALUE (p2) != TREE_VALUE (p1))
609 tree memb;
610 tree mv1 = TREE_VALUE (p1);
611 if (mv1 && mv1 != error_mark_node
612 && TREE_CODE (mv1) != ARRAY_TYPE)
613 mv1 = TYPE_MAIN_VARIANT (mv1);
614 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
615 memb; memb = DECL_CHAIN (memb))
617 tree mv3 = TREE_TYPE (memb);
618 if (mv3 && mv3 != error_mark_node
619 && TREE_CODE (mv3) != ARRAY_TYPE)
620 mv3 = TYPE_MAIN_VARIANT (mv3);
621 if (comptypes (mv3, mv1))
623 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
624 TREE_VALUE (p1));
625 pedwarn (input_location, OPT_Wpedantic,
626 "function types not truly compatible in ISO C");
627 goto parm_done;
631 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
632 parm_done: ;
635 t1 = build_function_type (valtype, newargs);
636 t1 = qualify_type (t1, t2);
638 /* FALLTHRU */
640 default:
641 return build_type_attribute_variant (t1, attributes);
646 /* Return the type of a conditional expression between pointers to
647 possibly differently qualified versions of compatible types.
649 We assume that comp_target_types has already been done and returned
650 nonzero; if that isn't so, this may crash. */
652 static tree
653 common_pointer_type (tree t1, tree t2)
655 tree attributes;
656 tree pointed_to_1, mv1;
657 tree pointed_to_2, mv2;
658 tree target;
659 unsigned target_quals;
660 addr_space_t as1, as2, as_common;
661 int quals1, quals2;
663 /* Save time if the two types are the same. */
665 if (t1 == t2) return t1;
667 /* If one type is nonsense, use the other. */
668 if (t1 == error_mark_node)
669 return t2;
670 if (t2 == error_mark_node)
671 return t1;
673 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
674 && TREE_CODE (t2) == POINTER_TYPE);
676 /* Merge the attributes. */
677 attributes = targetm.merge_type_attributes (t1, t2);
679 /* Find the composite type of the target types, and combine the
680 qualifiers of the two types' targets. Do not lose qualifiers on
681 array element types by taking the TYPE_MAIN_VARIANT. */
682 mv1 = pointed_to_1 = TREE_TYPE (t1);
683 mv2 = pointed_to_2 = TREE_TYPE (t2);
684 if (TREE_CODE (mv1) != ARRAY_TYPE)
685 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
686 if (TREE_CODE (mv2) != ARRAY_TYPE)
687 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
688 target = composite_type (mv1, mv2);
690 /* Strip array types to get correct qualifier for pointers to arrays */
691 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
692 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
694 /* For function types do not merge const qualifiers, but drop them
695 if used inconsistently. The middle-end uses these to mark const
696 and noreturn functions. */
697 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
698 target_quals = (quals1 & quals2);
699 else
700 target_quals = (quals1 | quals2);
702 /* If the two named address spaces are different, determine the common
703 superset address space. This is guaranteed to exist due to the
704 assumption that comp_target_type returned non-zero. */
705 as1 = TYPE_ADDR_SPACE (pointed_to_1);
706 as2 = TYPE_ADDR_SPACE (pointed_to_2);
707 if (!addr_space_superset (as1, as2, &as_common))
708 gcc_unreachable ();
710 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
712 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
713 return build_type_attribute_variant (t1, attributes);
716 /* Return the common type for two arithmetic types under the usual
717 arithmetic conversions. The default conversions have already been
718 applied, and enumerated types converted to their compatible integer
719 types. The resulting type is unqualified and has no attributes.
721 This is the type for the result of most arithmetic operations
722 if the operands have the given two types. */
724 static tree
725 c_common_type (tree t1, tree t2)
727 enum tree_code code1;
728 enum tree_code code2;
730 /* If one type is nonsense, use the other. */
731 if (t1 == error_mark_node)
732 return t2;
733 if (t2 == error_mark_node)
734 return t1;
736 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
737 t1 = TYPE_MAIN_VARIANT (t1);
739 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
740 t2 = TYPE_MAIN_VARIANT (t2);
742 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
743 t1 = build_type_attribute_variant (t1, NULL_TREE);
745 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
746 t2 = build_type_attribute_variant (t2, NULL_TREE);
748 /* Save time if the two types are the same. */
750 if (t1 == t2) return t1;
752 code1 = TREE_CODE (t1);
753 code2 = TREE_CODE (t2);
755 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
756 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
757 || code1 == INTEGER_TYPE);
758 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
759 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
760 || code2 == INTEGER_TYPE);
762 /* When one operand is a decimal float type, the other operand cannot be
763 a generic float type or a complex type. We also disallow vector types
764 here. */
765 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
766 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
768 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
770 error ("cannot mix operands of decimal floating and vector types");
771 return error_mark_node;
773 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
775 error ("cannot mix operands of decimal floating and complex types");
776 return error_mark_node;
778 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
780 error ("cannot mix operands of decimal floating "
781 "and other floating types");
782 return error_mark_node;
786 /* If one type is a vector type, return that type. (How the usual
787 arithmetic conversions apply to the vector types extension is not
788 precisely specified.) */
789 if (code1 == VECTOR_TYPE)
790 return t1;
792 if (code2 == VECTOR_TYPE)
793 return t2;
795 /* If one type is complex, form the common type of the non-complex
796 components, then make that complex. Use T1 or T2 if it is the
797 required type. */
798 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
800 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
801 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
802 tree subtype = c_common_type (subtype1, subtype2);
804 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
805 return t1;
806 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
807 return t2;
808 else
809 return build_complex_type (subtype);
812 /* If only one is real, use it as the result. */
814 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
815 return t1;
817 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
818 return t2;
820 /* If both are real and either are decimal floating point types, use
821 the decimal floating point type with the greater precision. */
823 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
825 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
826 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
827 return dfloat128_type_node;
828 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
829 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
830 return dfloat64_type_node;
831 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
832 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
833 return dfloat32_type_node;
836 /* Deal with fixed-point types. */
837 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
839 unsigned int unsignedp = 0, satp = 0;
840 scalar_mode m1, m2;
841 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
843 m1 = SCALAR_TYPE_MODE (t1);
844 m2 = SCALAR_TYPE_MODE (t2);
846 /* If one input type is saturating, the result type is saturating. */
847 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
848 satp = 1;
850 /* If both fixed-point types are unsigned, the result type is unsigned.
851 When mixing fixed-point and integer types, follow the sign of the
852 fixed-point type.
853 Otherwise, the result type is signed. */
854 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
855 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
856 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
857 && TYPE_UNSIGNED (t1))
858 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
859 && TYPE_UNSIGNED (t2)))
860 unsignedp = 1;
862 /* The result type is signed. */
863 if (unsignedp == 0)
865 /* If the input type is unsigned, we need to convert to the
866 signed type. */
867 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
869 enum mode_class mclass = (enum mode_class) 0;
870 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
871 mclass = MODE_FRACT;
872 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
873 mclass = MODE_ACCUM;
874 else
875 gcc_unreachable ();
876 m1 = as_a <scalar_mode>
877 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
879 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
881 enum mode_class mclass = (enum mode_class) 0;
882 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
883 mclass = MODE_FRACT;
884 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
885 mclass = MODE_ACCUM;
886 else
887 gcc_unreachable ();
888 m2 = as_a <scalar_mode>
889 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
893 if (code1 == FIXED_POINT_TYPE)
895 fbit1 = GET_MODE_FBIT (m1);
896 ibit1 = GET_MODE_IBIT (m1);
898 else
900 fbit1 = 0;
901 /* Signed integers need to subtract one sign bit. */
902 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
905 if (code2 == FIXED_POINT_TYPE)
907 fbit2 = GET_MODE_FBIT (m2);
908 ibit2 = GET_MODE_IBIT (m2);
910 else
912 fbit2 = 0;
913 /* Signed integers need to subtract one sign bit. */
914 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
917 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
918 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
919 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
920 satp);
923 /* Both real or both integers; use the one with greater precision. */
925 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
926 return t1;
927 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
928 return t2;
930 /* Same precision. Prefer long longs to longs to ints when the
931 same precision, following the C99 rules on integer type rank
932 (which are equivalent to the C90 rules for C90 types). */
934 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
935 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
936 return long_long_unsigned_type_node;
938 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
939 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
941 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
942 return long_long_unsigned_type_node;
943 else
944 return long_long_integer_type_node;
947 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
948 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
949 return long_unsigned_type_node;
951 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
952 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
954 /* But preserve unsignedness from the other type,
955 since long cannot hold all the values of an unsigned int. */
956 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
957 return long_unsigned_type_node;
958 else
959 return long_integer_type_node;
962 /* For floating types of the same TYPE_PRECISION (which we here
963 assume means either the same set of values, or sets of values
964 neither a subset of the other, with behavior being undefined in
965 the latter case), follow the rules from TS 18661-3: prefer
966 interchange types _FloatN, then standard types long double,
967 double, float, then extended types _FloatNx. For extended types,
968 check them starting with _Float128x as that seems most consistent
969 in spirit with preferring long double to double; for interchange
970 types, also check in that order for consistency although it's not
971 possible for more than one of them to have the same
972 precision. */
973 tree mv1 = TYPE_MAIN_VARIANT (t1);
974 tree mv2 = TYPE_MAIN_VARIANT (t2);
976 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
977 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
978 return FLOATN_TYPE_NODE (i);
980 /* Likewise, prefer long double to double even if same size. */
981 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
982 return long_double_type_node;
984 /* Likewise, prefer double to float even if same size.
985 We got a couple of embedded targets with 32 bit doubles, and the
986 pdp11 might have 64 bit floats. */
987 if (mv1 == double_type_node || mv2 == double_type_node)
988 return double_type_node;
990 if (mv1 == float_type_node || mv2 == float_type_node)
991 return float_type_node;
993 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
994 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
995 return FLOATNX_TYPE_NODE (i);
997 /* Otherwise prefer the unsigned one. */
999 if (TYPE_UNSIGNED (t1))
1000 return t1;
1001 else
1002 return t2;
1005 /* Wrapper around c_common_type that is used by c-common.c and other
1006 front end optimizations that remove promotions. ENUMERAL_TYPEs
1007 are allowed here and are converted to their compatible integer types.
1008 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1009 preferably a non-Boolean type as the common type. */
1010 tree
1011 common_type (tree t1, tree t2)
1013 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1014 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1015 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1016 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1018 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1019 if (TREE_CODE (t1) == BOOLEAN_TYPE
1020 && TREE_CODE (t2) == BOOLEAN_TYPE)
1021 return boolean_type_node;
1023 /* If either type is BOOLEAN_TYPE, then return the other. */
1024 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1025 return t2;
1026 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1027 return t1;
1029 return c_common_type (t1, t2);
1032 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1033 or various other operations. Return 2 if they are compatible
1034 but a warning may be needed if you use them together. */
1037 comptypes (tree type1, tree type2)
1039 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1040 int val;
1042 val = comptypes_internal (type1, type2, NULL, NULL);
1043 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1045 return val;
1048 /* Like comptypes, but if it returns non-zero because enum and int are
1049 compatible, it sets *ENUM_AND_INT_P to true. */
1051 static int
1052 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1054 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1055 int val;
1057 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1058 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1060 return val;
1063 /* Like comptypes, but if it returns nonzero for different types, it
1064 sets *DIFFERENT_TYPES_P to true. */
1067 comptypes_check_different_types (tree type1, tree type2,
1068 bool *different_types_p)
1070 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1071 int val;
1073 val = comptypes_internal (type1, type2, NULL, different_types_p);
1074 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1076 return val;
1079 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1080 or various other operations. Return 2 if they are compatible
1081 but a warning may be needed if you use them together. If
1082 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1083 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1084 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1085 NULL, and the types are compatible but different enough not to be
1086 permitted in C11 typedef redeclarations, then this sets
1087 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1088 false, but may or may not be set if the types are incompatible.
1089 This differs from comptypes, in that we don't free the seen
1090 types. */
1092 static int
1093 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1094 bool *different_types_p)
1096 const_tree t1 = type1;
1097 const_tree t2 = type2;
1098 int attrval, val;
1100 /* Suppress errors caused by previously reported errors. */
1102 if (t1 == t2 || !t1 || !t2
1103 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1104 return 1;
1106 /* Enumerated types are compatible with integer types, but this is
1107 not transitive: two enumerated types in the same translation unit
1108 are compatible with each other only if they are the same type. */
1110 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1112 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1113 if (TREE_CODE (t2) != VOID_TYPE)
1115 if (enum_and_int_p != NULL)
1116 *enum_and_int_p = true;
1117 if (different_types_p != NULL)
1118 *different_types_p = true;
1121 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1123 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1124 if (TREE_CODE (t1) != VOID_TYPE)
1126 if (enum_and_int_p != NULL)
1127 *enum_and_int_p = true;
1128 if (different_types_p != NULL)
1129 *different_types_p = true;
1133 if (t1 == t2)
1134 return 1;
1136 /* Different classes of types can't be compatible. */
1138 if (TREE_CODE (t1) != TREE_CODE (t2))
1139 return 0;
1141 /* Qualifiers must match. C99 6.7.3p9 */
1143 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1144 return 0;
1146 /* Allow for two different type nodes which have essentially the same
1147 definition. Note that we already checked for equality of the type
1148 qualifiers (just above). */
1150 if (TREE_CODE (t1) != ARRAY_TYPE
1151 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1152 return 1;
1154 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1155 if (!(attrval = comp_type_attributes (t1, t2)))
1156 return 0;
1158 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1159 val = 0;
1161 switch (TREE_CODE (t1))
1163 case INTEGER_TYPE:
1164 case FIXED_POINT_TYPE:
1165 case REAL_TYPE:
1166 /* With these nodes, we can't determine type equivalence by
1167 looking at what is stored in the nodes themselves, because
1168 two nodes might have different TYPE_MAIN_VARIANTs but still
1169 represent the same type. For example, wchar_t and int could
1170 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1171 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1172 and are distinct types. On the other hand, int and the
1173 following typedef
1175 typedef int INT __attribute((may_alias));
1177 have identical properties, different TYPE_MAIN_VARIANTs, but
1178 represent the same type. The canonical type system keeps
1179 track of equivalence in this case, so we fall back on it. */
1180 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1182 case POINTER_TYPE:
1183 /* Do not remove mode information. */
1184 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1185 break;
1186 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1187 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1188 enum_and_int_p, different_types_p));
1189 break;
1191 case FUNCTION_TYPE:
1192 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1193 different_types_p);
1194 break;
1196 case ARRAY_TYPE:
1198 tree d1 = TYPE_DOMAIN (t1);
1199 tree d2 = TYPE_DOMAIN (t2);
1200 bool d1_variable, d2_variable;
1201 bool d1_zero, d2_zero;
1202 val = 1;
1204 /* Target types must match incl. qualifiers. */
1205 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1206 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1207 enum_and_int_p,
1208 different_types_p)) == 0)
1209 return 0;
1211 if (different_types_p != NULL
1212 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1213 *different_types_p = true;
1214 /* Sizes must match unless one is missing or variable. */
1215 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1216 break;
1218 d1_zero = !TYPE_MAX_VALUE (d1);
1219 d2_zero = !TYPE_MAX_VALUE (d2);
1221 d1_variable = (!d1_zero
1222 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1223 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1224 d2_variable = (!d2_zero
1225 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1226 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1227 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1228 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1230 if (different_types_p != NULL
1231 && d1_variable != d2_variable)
1232 *different_types_p = true;
1233 if (d1_variable || d2_variable)
1234 break;
1235 if (d1_zero && d2_zero)
1236 break;
1237 if (d1_zero || d2_zero
1238 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1239 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1240 val = 0;
1242 break;
1245 case ENUMERAL_TYPE:
1246 case RECORD_TYPE:
1247 case UNION_TYPE:
1248 if (val != 1 && !same_translation_unit_p (t1, t2))
1250 tree a1 = TYPE_ATTRIBUTES (t1);
1251 tree a2 = TYPE_ATTRIBUTES (t2);
1253 if (! attribute_list_contained (a1, a2)
1254 && ! attribute_list_contained (a2, a1))
1255 break;
1257 if (attrval != 2)
1258 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1259 different_types_p);
1260 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1261 different_types_p);
1263 break;
1265 case VECTOR_TYPE:
1266 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1267 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1268 enum_and_int_p, different_types_p));
1269 break;
1271 default:
1272 break;
1274 return attrval == 2 && val == 1 ? 2 : val;
1277 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1278 their qualifiers, except for named address spaces. If the pointers point to
1279 different named addresses, then we must determine if one address space is a
1280 subset of the other. */
1282 static int
1283 comp_target_types (location_t location, tree ttl, tree ttr)
1285 int val;
1286 int val_ped;
1287 tree mvl = TREE_TYPE (ttl);
1288 tree mvr = TREE_TYPE (ttr);
1289 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1290 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1291 addr_space_t as_common;
1292 bool enum_and_int_p;
1294 /* Fail if pointers point to incompatible address spaces. */
1295 if (!addr_space_superset (asl, asr, &as_common))
1296 return 0;
1298 /* For pedantic record result of comptypes on arrays before losing
1299 qualifiers on the element type below. */
1300 val_ped = 1;
1302 if (TREE_CODE (mvl) == ARRAY_TYPE
1303 && TREE_CODE (mvr) == ARRAY_TYPE)
1304 val_ped = comptypes (mvl, mvr);
1306 /* Qualifiers on element types of array types that are
1307 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1309 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1310 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1311 : TYPE_MAIN_VARIANT (mvl));
1313 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1314 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1315 : TYPE_MAIN_VARIANT (mvr));
1317 enum_and_int_p = false;
1318 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1320 if (val == 1 && val_ped != 1)
1321 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1322 "are incompatible in ISO C");
1324 if (val == 2)
1325 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1327 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1328 warning_at (location, OPT_Wc___compat,
1329 "pointer target types incompatible in C++");
1331 return val;
1334 /* Subroutines of `comptypes'. */
1336 /* Determine whether two trees derive from the same translation unit.
1337 If the CONTEXT chain ends in a null, that tree's context is still
1338 being parsed, so if two trees have context chains ending in null,
1339 they're in the same translation unit. */
1341 bool
1342 same_translation_unit_p (const_tree t1, const_tree t2)
1344 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1345 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1347 case tcc_declaration:
1348 t1 = DECL_CONTEXT (t1); break;
1349 case tcc_type:
1350 t1 = TYPE_CONTEXT (t1); break;
1351 case tcc_exceptional:
1352 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1353 default: gcc_unreachable ();
1356 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1357 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1359 case tcc_declaration:
1360 t2 = DECL_CONTEXT (t2); break;
1361 case tcc_type:
1362 t2 = TYPE_CONTEXT (t2); break;
1363 case tcc_exceptional:
1364 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1365 default: gcc_unreachable ();
1368 return t1 == t2;
1371 /* Allocate the seen two types, assuming that they are compatible. */
1373 static struct tagged_tu_seen_cache *
1374 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1376 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1377 tu->next = tagged_tu_seen_base;
1378 tu->t1 = t1;
1379 tu->t2 = t2;
1381 tagged_tu_seen_base = tu;
1383 /* The C standard says that two structures in different translation
1384 units are compatible with each other only if the types of their
1385 fields are compatible (among other things). We assume that they
1386 are compatible until proven otherwise when building the cache.
1387 An example where this can occur is:
1388 struct a
1390 struct a *next;
1392 If we are comparing this against a similar struct in another TU,
1393 and did not assume they were compatible, we end up with an infinite
1394 loop. */
1395 tu->val = 1;
1396 return tu;
1399 /* Free the seen types until we get to TU_TIL. */
1401 static void
1402 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1404 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1405 while (tu != tu_til)
1407 const struct tagged_tu_seen_cache *const tu1
1408 = (const struct tagged_tu_seen_cache *) tu;
1409 tu = tu1->next;
1410 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1412 tagged_tu_seen_base = tu_til;
1415 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1416 compatible. If the two types are not the same (which has been
1417 checked earlier), this can only happen when multiple translation
1418 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1419 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1420 comptypes_internal. */
1422 static int
1423 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1424 bool *enum_and_int_p, bool *different_types_p)
1426 tree s1, s2;
1427 bool needs_warning = false;
1429 /* We have to verify that the tags of the types are the same. This
1430 is harder than it looks because this may be a typedef, so we have
1431 to go look at the original type. It may even be a typedef of a
1432 typedef...
1433 In the case of compiler-created builtin structs the TYPE_DECL
1434 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1435 while (TYPE_NAME (t1)
1436 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1437 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1438 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1440 while (TYPE_NAME (t2)
1441 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1442 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1443 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1445 /* C90 didn't have the requirement that the two tags be the same. */
1446 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1447 return 0;
1449 /* C90 didn't say what happened if one or both of the types were
1450 incomplete; we choose to follow C99 rules here, which is that they
1451 are compatible. */
1452 if (TYPE_SIZE (t1) == NULL
1453 || TYPE_SIZE (t2) == NULL)
1454 return 1;
1457 const struct tagged_tu_seen_cache * tts_i;
1458 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1459 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1460 return tts_i->val;
1463 switch (TREE_CODE (t1))
1465 case ENUMERAL_TYPE:
1467 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1468 /* Speed up the case where the type values are in the same order. */
1469 tree tv1 = TYPE_VALUES (t1);
1470 tree tv2 = TYPE_VALUES (t2);
1472 if (tv1 == tv2)
1474 return 1;
1477 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1479 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1480 break;
1481 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1483 tu->val = 0;
1484 return 0;
1488 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1490 return 1;
1492 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1494 tu->val = 0;
1495 return 0;
1498 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1500 tu->val = 0;
1501 return 0;
1504 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1506 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1507 if (s2 == NULL
1508 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1510 tu->val = 0;
1511 return 0;
1514 return 1;
1517 case UNION_TYPE:
1519 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1520 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1522 tu->val = 0;
1523 return 0;
1526 /* Speed up the common case where the fields are in the same order. */
1527 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1528 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1530 int result;
1532 if (DECL_NAME (s1) != DECL_NAME (s2))
1533 break;
1534 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1535 enum_and_int_p, different_types_p);
1537 if (result != 1 && !DECL_NAME (s1))
1538 break;
1539 if (result == 0)
1541 tu->val = 0;
1542 return 0;
1544 if (result == 2)
1545 needs_warning = true;
1547 if (TREE_CODE (s1) == FIELD_DECL
1548 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1549 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1551 tu->val = 0;
1552 return 0;
1555 if (!s1 && !s2)
1557 tu->val = needs_warning ? 2 : 1;
1558 return tu->val;
1561 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1563 bool ok = false;
1565 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1566 if (DECL_NAME (s1) == DECL_NAME (s2))
1568 int result;
1570 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1571 enum_and_int_p,
1572 different_types_p);
1574 if (result != 1 && !DECL_NAME (s1))
1575 continue;
1576 if (result == 0)
1578 tu->val = 0;
1579 return 0;
1581 if (result == 2)
1582 needs_warning = true;
1584 if (TREE_CODE (s1) == FIELD_DECL
1585 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1586 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1587 break;
1589 ok = true;
1590 break;
1592 if (!ok)
1594 tu->val = 0;
1595 return 0;
1598 tu->val = needs_warning ? 2 : 10;
1599 return tu->val;
1602 case RECORD_TYPE:
1604 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1606 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1607 s1 && s2;
1608 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1610 int result;
1611 if (TREE_CODE (s1) != TREE_CODE (s2)
1612 || DECL_NAME (s1) != DECL_NAME (s2))
1613 break;
1614 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1615 enum_and_int_p, different_types_p);
1616 if (result == 0)
1617 break;
1618 if (result == 2)
1619 needs_warning = true;
1621 if (TREE_CODE (s1) == FIELD_DECL
1622 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1623 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1624 break;
1626 if (s1 && s2)
1627 tu->val = 0;
1628 else
1629 tu->val = needs_warning ? 2 : 1;
1630 return tu->val;
1633 default:
1634 gcc_unreachable ();
1638 /* Return 1 if two function types F1 and F2 are compatible.
1639 If either type specifies no argument types,
1640 the other must specify a fixed number of self-promoting arg types.
1641 Otherwise, if one type specifies only the number of arguments,
1642 the other must specify that number of self-promoting arg types.
1643 Otherwise, the argument types must match.
1644 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1646 static int
1647 function_types_compatible_p (const_tree f1, const_tree f2,
1648 bool *enum_and_int_p, bool *different_types_p)
1650 tree args1, args2;
1651 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1652 int val = 1;
1653 int val1;
1654 tree ret1, ret2;
1656 ret1 = TREE_TYPE (f1);
1657 ret2 = TREE_TYPE (f2);
1659 /* 'volatile' qualifiers on a function's return type used to mean
1660 the function is noreturn. */
1661 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1662 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1663 if (TYPE_VOLATILE (ret1))
1664 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1665 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1666 if (TYPE_VOLATILE (ret2))
1667 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1668 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1669 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1670 if (val == 0)
1671 return 0;
1673 args1 = TYPE_ARG_TYPES (f1);
1674 args2 = TYPE_ARG_TYPES (f2);
1676 if (different_types_p != NULL
1677 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1678 *different_types_p = true;
1680 /* An unspecified parmlist matches any specified parmlist
1681 whose argument types don't need default promotions. */
1683 if (args1 == NULL_TREE)
1685 if (!self_promoting_args_p (args2))
1686 return 0;
1687 /* If one of these types comes from a non-prototype fn definition,
1688 compare that with the other type's arglist.
1689 If they don't match, ask for a warning (but no error). */
1690 if (TYPE_ACTUAL_ARG_TYPES (f1)
1691 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1692 enum_and_int_p, different_types_p) != 1)
1693 val = 2;
1694 return val;
1696 if (args2 == NULL_TREE)
1698 if (!self_promoting_args_p (args1))
1699 return 0;
1700 if (TYPE_ACTUAL_ARG_TYPES (f2)
1701 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1702 enum_and_int_p, different_types_p) != 1)
1703 val = 2;
1704 return val;
1707 /* Both types have argument lists: compare them and propagate results. */
1708 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1709 different_types_p);
1710 return val1 != 1 ? val1 : val;
1713 /* Check two lists of types for compatibility, returning 0 for
1714 incompatible, 1 for compatible, or 2 for compatible with
1715 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1716 comptypes_internal. */
1718 static int
1719 type_lists_compatible_p (const_tree args1, const_tree args2,
1720 bool *enum_and_int_p, bool *different_types_p)
1722 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1723 int val = 1;
1724 int newval = 0;
1726 while (1)
1728 tree a1, mv1, a2, mv2;
1729 if (args1 == NULL_TREE && args2 == NULL_TREE)
1730 return val;
1731 /* If one list is shorter than the other,
1732 they fail to match. */
1733 if (args1 == NULL_TREE || args2 == NULL_TREE)
1734 return 0;
1735 mv1 = a1 = TREE_VALUE (args1);
1736 mv2 = a2 = TREE_VALUE (args2);
1737 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1738 mv1 = (TYPE_ATOMIC (mv1)
1739 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1740 TYPE_QUAL_ATOMIC)
1741 : TYPE_MAIN_VARIANT (mv1));
1742 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1743 mv2 = (TYPE_ATOMIC (mv2)
1744 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1745 TYPE_QUAL_ATOMIC)
1746 : TYPE_MAIN_VARIANT (mv2));
1747 /* A null pointer instead of a type
1748 means there is supposed to be an argument
1749 but nothing is specified about what type it has.
1750 So match anything that self-promotes. */
1751 if (different_types_p != NULL
1752 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1753 *different_types_p = true;
1754 if (a1 == NULL_TREE)
1756 if (c_type_promotes_to (a2) != a2)
1757 return 0;
1759 else if (a2 == NULL_TREE)
1761 if (c_type_promotes_to (a1) != a1)
1762 return 0;
1764 /* If one of the lists has an error marker, ignore this arg. */
1765 else if (TREE_CODE (a1) == ERROR_MARK
1766 || TREE_CODE (a2) == ERROR_MARK)
1768 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1769 different_types_p)))
1771 if (different_types_p != NULL)
1772 *different_types_p = true;
1773 /* Allow wait (union {union wait *u; int *i} *)
1774 and wait (union wait *) to be compatible. */
1775 if (TREE_CODE (a1) == UNION_TYPE
1776 && (TYPE_NAME (a1) == NULL_TREE
1777 || TYPE_TRANSPARENT_AGGR (a1))
1778 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1779 && tree_int_cst_equal (TYPE_SIZE (a1),
1780 TYPE_SIZE (a2)))
1782 tree memb;
1783 for (memb = TYPE_FIELDS (a1);
1784 memb; memb = DECL_CHAIN (memb))
1786 tree mv3 = TREE_TYPE (memb);
1787 if (mv3 && mv3 != error_mark_node
1788 && TREE_CODE (mv3) != ARRAY_TYPE)
1789 mv3 = (TYPE_ATOMIC (mv3)
1790 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1791 TYPE_QUAL_ATOMIC)
1792 : TYPE_MAIN_VARIANT (mv3));
1793 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1794 different_types_p))
1795 break;
1797 if (memb == NULL_TREE)
1798 return 0;
1800 else if (TREE_CODE (a2) == UNION_TYPE
1801 && (TYPE_NAME (a2) == NULL_TREE
1802 || TYPE_TRANSPARENT_AGGR (a2))
1803 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1804 && tree_int_cst_equal (TYPE_SIZE (a2),
1805 TYPE_SIZE (a1)))
1807 tree memb;
1808 for (memb = TYPE_FIELDS (a2);
1809 memb; memb = DECL_CHAIN (memb))
1811 tree mv3 = TREE_TYPE (memb);
1812 if (mv3 && mv3 != error_mark_node
1813 && TREE_CODE (mv3) != ARRAY_TYPE)
1814 mv3 = (TYPE_ATOMIC (mv3)
1815 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1816 TYPE_QUAL_ATOMIC)
1817 : TYPE_MAIN_VARIANT (mv3));
1818 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1819 different_types_p))
1820 break;
1822 if (memb == NULL_TREE)
1823 return 0;
1825 else
1826 return 0;
1829 /* comptypes said ok, but record if it said to warn. */
1830 if (newval > val)
1831 val = newval;
1833 args1 = TREE_CHAIN (args1);
1834 args2 = TREE_CHAIN (args2);
1838 /* Compute the size to increment a pointer by. When a function type or void
1839 type or incomplete type is passed, size_one_node is returned.
1840 This function does not emit any diagnostics; the caller is responsible
1841 for that. */
1843 static tree
1844 c_size_in_bytes (const_tree type)
1846 enum tree_code code = TREE_CODE (type);
1848 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1849 || !COMPLETE_TYPE_P (type))
1850 return size_one_node;
1852 /* Convert in case a char is more than one unit. */
1853 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1854 size_int (TYPE_PRECISION (char_type_node)
1855 / BITS_PER_UNIT));
1858 /* Return either DECL or its known constant value (if it has one). */
1860 tree
1861 decl_constant_value_1 (tree decl, bool in_init)
1863 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1864 TREE_CODE (decl) != PARM_DECL
1865 && !TREE_THIS_VOLATILE (decl)
1866 && TREE_READONLY (decl)
1867 && DECL_INITIAL (decl) != NULL_TREE
1868 && !error_operand_p (DECL_INITIAL (decl))
1869 /* This is invalid if initial value is not constant.
1870 If it has either a function call, a memory reference,
1871 or a variable, then re-evaluating it could give different results. */
1872 && TREE_CONSTANT (DECL_INITIAL (decl))
1873 /* Check for cases where this is sub-optimal, even though valid. */
1874 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1875 return DECL_INITIAL (decl);
1876 return decl;
1879 /* Return either DECL or its known constant value (if it has one).
1880 Like the above, but always return decl outside of functions. */
1882 tree
1883 decl_constant_value (tree decl)
1885 /* Don't change a variable array bound or initial value to a constant
1886 in a place where a variable is invalid. */
1887 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1890 /* Convert the array expression EXP to a pointer. */
1891 static tree
1892 array_to_pointer_conversion (location_t loc, tree exp)
1894 tree orig_exp = exp;
1895 tree type = TREE_TYPE (exp);
1896 tree adr;
1897 tree restype = TREE_TYPE (type);
1898 tree ptrtype;
1900 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1902 STRIP_TYPE_NOPS (exp);
1904 if (TREE_NO_WARNING (orig_exp))
1905 TREE_NO_WARNING (exp) = 1;
1907 ptrtype = build_pointer_type (restype);
1909 if (INDIRECT_REF_P (exp))
1910 return convert (ptrtype, TREE_OPERAND (exp, 0));
1912 /* In C++ array compound literals are temporary objects unless they are
1913 const or appear in namespace scope, so they are destroyed too soon
1914 to use them for much of anything (c++/53220). */
1915 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1917 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1918 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1919 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1920 "converting an array compound literal to a pointer "
1921 "is ill-formed in C++");
1924 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1925 return convert (ptrtype, adr);
1928 /* Convert the function expression EXP to a pointer. */
1929 static tree
1930 function_to_pointer_conversion (location_t loc, tree exp)
1932 tree orig_exp = exp;
1934 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1936 STRIP_TYPE_NOPS (exp);
1938 if (TREE_NO_WARNING (orig_exp))
1939 TREE_NO_WARNING (exp) = 1;
1941 return build_unary_op (loc, ADDR_EXPR, exp, false);
1944 /* Mark EXP as read, not just set, for set but not used -Wunused
1945 warning purposes. */
1947 void
1948 mark_exp_read (tree exp)
1950 switch (TREE_CODE (exp))
1952 case VAR_DECL:
1953 case PARM_DECL:
1954 DECL_READ_P (exp) = 1;
1955 break;
1956 case ARRAY_REF:
1957 case COMPONENT_REF:
1958 case MODIFY_EXPR:
1959 case REALPART_EXPR:
1960 case IMAGPART_EXPR:
1961 CASE_CONVERT:
1962 case ADDR_EXPR:
1963 case VIEW_CONVERT_EXPR:
1964 mark_exp_read (TREE_OPERAND (exp, 0));
1965 break;
1966 case COMPOUND_EXPR:
1967 case C_MAYBE_CONST_EXPR:
1968 mark_exp_read (TREE_OPERAND (exp, 1));
1969 break;
1970 default:
1971 break;
1975 /* Perform the default conversion of arrays and functions to pointers.
1976 Return the result of converting EXP. For any other expression, just
1977 return EXP.
1979 LOC is the location of the expression. */
1981 struct c_expr
1982 default_function_array_conversion (location_t loc, struct c_expr exp)
1984 tree orig_exp = exp.value;
1985 tree type = TREE_TYPE (exp.value);
1986 enum tree_code code = TREE_CODE (type);
1988 switch (code)
1990 case ARRAY_TYPE:
1992 bool not_lvalue = false;
1993 bool lvalue_array_p;
1995 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1996 || CONVERT_EXPR_P (exp.value))
1997 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1999 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2000 not_lvalue = true;
2001 exp.value = TREE_OPERAND (exp.value, 0);
2004 if (TREE_NO_WARNING (orig_exp))
2005 TREE_NO_WARNING (exp.value) = 1;
2007 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2008 if (!flag_isoc99 && !lvalue_array_p)
2010 /* Before C99, non-lvalue arrays do not decay to pointers.
2011 Normally, using such an array would be invalid; but it can
2012 be used correctly inside sizeof or as a statement expression.
2013 Thus, do not give an error here; an error will result later. */
2014 return exp;
2017 exp.value = array_to_pointer_conversion (loc, exp.value);
2019 break;
2020 case FUNCTION_TYPE:
2021 exp.value = function_to_pointer_conversion (loc, exp.value);
2022 break;
2023 default:
2024 break;
2027 return exp;
2030 struct c_expr
2031 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2033 mark_exp_read (exp.value);
2034 return default_function_array_conversion (loc, exp);
2037 /* Return whether EXPR should be treated as an atomic lvalue for the
2038 purposes of load and store handling. */
2040 static bool
2041 really_atomic_lvalue (tree expr)
2043 if (error_operand_p (expr))
2044 return false;
2045 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2046 return false;
2047 if (!lvalue_p (expr))
2048 return false;
2050 /* Ignore _Atomic on register variables, since their addresses can't
2051 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2052 sequences wouldn't work. Ignore _Atomic on structures containing
2053 bit-fields, since accessing elements of atomic structures or
2054 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2055 it's undefined at translation time or execution time, and the
2056 normal atomic sequences again wouldn't work. */
2057 while (handled_component_p (expr))
2059 if (TREE_CODE (expr) == COMPONENT_REF
2060 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2061 return false;
2062 expr = TREE_OPERAND (expr, 0);
2064 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2065 return false;
2066 return true;
2069 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2070 including converting functions and arrays to pointers if CONVERT_P.
2071 If READ_P, also mark the expression as having been read. */
2073 struct c_expr
2074 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2075 bool convert_p, bool read_p)
2077 if (read_p)
2078 mark_exp_read (exp.value);
2079 if (convert_p)
2080 exp = default_function_array_conversion (loc, exp);
2081 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2082 exp.value = require_complete_type (loc, exp.value);
2083 if (really_atomic_lvalue (exp.value))
2085 vec<tree, va_gc> *params;
2086 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2087 tree expr_type = TREE_TYPE (exp.value);
2088 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2089 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2091 gcc_assert (TYPE_ATOMIC (expr_type));
2093 /* Expansion of a generic atomic load may require an addition
2094 element, so allocate enough to prevent a resize. */
2095 vec_alloc (params, 4);
2097 /* Remove the qualifiers for the rest of the expressions and
2098 create the VAL temp variable to hold the RHS. */
2099 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2100 tmp = create_tmp_var_raw (nonatomic_type);
2101 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2102 TREE_ADDRESSABLE (tmp) = 1;
2103 TREE_NO_WARNING (tmp) = 1;
2105 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2106 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2107 params->quick_push (expr_addr);
2108 params->quick_push (tmp_addr);
2109 params->quick_push (seq_cst);
2110 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2112 /* EXPR is always read. */
2113 mark_exp_read (exp.value);
2115 /* Return tmp which contains the value loaded. */
2116 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2117 NULL_TREE, NULL_TREE);
2119 return exp;
2122 /* EXP is an expression of integer type. Apply the integer promotions
2123 to it and return the promoted value. */
2125 tree
2126 perform_integral_promotions (tree exp)
2128 tree type = TREE_TYPE (exp);
2129 enum tree_code code = TREE_CODE (type);
2131 gcc_assert (INTEGRAL_TYPE_P (type));
2133 /* Normally convert enums to int,
2134 but convert wide enums to something wider. */
2135 if (code == ENUMERAL_TYPE)
2137 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2138 TYPE_PRECISION (integer_type_node)),
2139 ((TYPE_PRECISION (type)
2140 >= TYPE_PRECISION (integer_type_node))
2141 && TYPE_UNSIGNED (type)));
2143 return convert (type, exp);
2146 /* ??? This should no longer be needed now bit-fields have their
2147 proper types. */
2148 if (TREE_CODE (exp) == COMPONENT_REF
2149 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2150 /* If it's thinner than an int, promote it like a
2151 c_promoting_integer_type_p, otherwise leave it alone. */
2152 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2153 TYPE_PRECISION (integer_type_node)) < 0)
2154 return convert (integer_type_node, exp);
2156 if (c_promoting_integer_type_p (type))
2158 /* Preserve unsignedness if not really getting any wider. */
2159 if (TYPE_UNSIGNED (type)
2160 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2161 return convert (unsigned_type_node, exp);
2163 return convert (integer_type_node, exp);
2166 return exp;
2170 /* Perform default promotions for C data used in expressions.
2171 Enumeral types or short or char are converted to int.
2172 In addition, manifest constants symbols are replaced by their values. */
2174 tree
2175 default_conversion (tree exp)
2177 tree orig_exp;
2178 tree type = TREE_TYPE (exp);
2179 enum tree_code code = TREE_CODE (type);
2180 tree promoted_type;
2182 mark_exp_read (exp);
2184 /* Functions and arrays have been converted during parsing. */
2185 gcc_assert (code != FUNCTION_TYPE);
2186 if (code == ARRAY_TYPE)
2187 return exp;
2189 /* Constants can be used directly unless they're not loadable. */
2190 if (TREE_CODE (exp) == CONST_DECL)
2191 exp = DECL_INITIAL (exp);
2193 /* Strip no-op conversions. */
2194 orig_exp = exp;
2195 STRIP_TYPE_NOPS (exp);
2197 if (TREE_NO_WARNING (orig_exp))
2198 TREE_NO_WARNING (exp) = 1;
2200 if (code == VOID_TYPE)
2202 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2203 "void value not ignored as it ought to be");
2204 return error_mark_node;
2207 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2208 if (exp == error_mark_node)
2209 return error_mark_node;
2211 promoted_type = targetm.promoted_type (type);
2212 if (promoted_type)
2213 return convert (promoted_type, exp);
2215 if (INTEGRAL_TYPE_P (type))
2216 return perform_integral_promotions (exp);
2218 return exp;
2221 /* Look up COMPONENT in a structure or union TYPE.
2223 If the component name is not found, returns NULL_TREE. Otherwise,
2224 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2225 stepping down the chain to the component, which is in the last
2226 TREE_VALUE of the list. Normally the list is of length one, but if
2227 the component is embedded within (nested) anonymous structures or
2228 unions, the list steps down the chain to the component. */
2230 static tree
2231 lookup_field (tree type, tree component)
2233 tree field;
2235 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2236 to the field elements. Use a binary search on this array to quickly
2237 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2238 will always be set for structures which have many elements.
2240 Duplicate field checking replaces duplicates with NULL_TREE so
2241 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2242 case just iterate using DECL_CHAIN. */
2244 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2245 && !seen_error ())
2247 int bot, top, half;
2248 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2250 field = TYPE_FIELDS (type);
2251 bot = 0;
2252 top = TYPE_LANG_SPECIFIC (type)->s->len;
2253 while (top - bot > 1)
2255 half = (top - bot + 1) >> 1;
2256 field = field_array[bot+half];
2258 if (DECL_NAME (field) == NULL_TREE)
2260 /* Step through all anon unions in linear fashion. */
2261 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2263 field = field_array[bot++];
2264 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2266 tree anon = lookup_field (TREE_TYPE (field), component);
2268 if (anon)
2269 return tree_cons (NULL_TREE, field, anon);
2271 /* The Plan 9 compiler permits referring
2272 directly to an anonymous struct/union field
2273 using a typedef name. */
2274 if (flag_plan9_extensions
2275 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2276 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2277 == TYPE_DECL)
2278 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2279 == component))
2280 break;
2284 /* Entire record is only anon unions. */
2285 if (bot > top)
2286 return NULL_TREE;
2288 /* Restart the binary search, with new lower bound. */
2289 continue;
2292 if (DECL_NAME (field) == component)
2293 break;
2294 if (DECL_NAME (field) < component)
2295 bot += half;
2296 else
2297 top = bot + half;
2300 if (DECL_NAME (field_array[bot]) == component)
2301 field = field_array[bot];
2302 else if (DECL_NAME (field) != component)
2303 return NULL_TREE;
2305 else
2307 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2309 if (DECL_NAME (field) == NULL_TREE
2310 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2312 tree anon = lookup_field (TREE_TYPE (field), component);
2314 if (anon)
2315 return tree_cons (NULL_TREE, field, anon);
2317 /* The Plan 9 compiler permits referring directly to an
2318 anonymous struct/union field using a typedef
2319 name. */
2320 if (flag_plan9_extensions
2321 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2322 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2323 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2324 == component))
2325 break;
2328 if (DECL_NAME (field) == component)
2329 break;
2332 if (field == NULL_TREE)
2333 return NULL_TREE;
2336 return tree_cons (NULL_TREE, field, NULL_TREE);
2339 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2341 static void
2342 lookup_field_fuzzy_find_candidates (tree type, tree component,
2343 vec<tree> *candidates)
2345 tree field;
2346 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2348 if (DECL_NAME (field) == NULL_TREE
2349 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2350 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2351 candidates);
2353 if (DECL_NAME (field))
2354 candidates->safe_push (DECL_NAME (field));
2358 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2359 rather than returning a TREE_LIST for an exact match. */
2361 static tree
2362 lookup_field_fuzzy (tree type, tree component)
2364 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2366 /* First, gather a list of candidates. */
2367 auto_vec <tree> candidates;
2369 lookup_field_fuzzy_find_candidates (type, component,
2370 &candidates);
2372 return find_closest_identifier (component, &candidates);
2375 /* Support function for build_component_ref's error-handling.
2377 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2378 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2380 static bool
2381 should_suggest_deref_p (tree datum_type)
2383 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2384 allows "." for ptrs; we could be handling a failed attempt
2385 to access a property. */
2386 if (c_dialect_objc ())
2387 return false;
2389 /* Only suggest it for pointers... */
2390 if (TREE_CODE (datum_type) != POINTER_TYPE)
2391 return false;
2393 /* ...to structs/unions. */
2394 tree underlying_type = TREE_TYPE (datum_type);
2395 enum tree_code code = TREE_CODE (underlying_type);
2396 if (code == RECORD_TYPE || code == UNION_TYPE)
2397 return true;
2398 else
2399 return false;
2402 /* Make an expression to refer to the COMPONENT field of structure or
2403 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2404 location of the COMPONENT_REF. COMPONENT_LOC is the location
2405 of COMPONENT. */
2407 tree
2408 build_component_ref (location_t loc, tree datum, tree component,
2409 location_t component_loc)
2411 tree type = TREE_TYPE (datum);
2412 enum tree_code code = TREE_CODE (type);
2413 tree field = NULL;
2414 tree ref;
2415 bool datum_lvalue = lvalue_p (datum);
2417 if (!objc_is_public (datum, component))
2418 return error_mark_node;
2420 /* Detect Objective-C property syntax object.property. */
2421 if (c_dialect_objc ()
2422 && (ref = objc_maybe_build_component_ref (datum, component)))
2423 return ref;
2425 /* See if there is a field or component with name COMPONENT. */
2427 if (code == RECORD_TYPE || code == UNION_TYPE)
2429 if (!COMPLETE_TYPE_P (type))
2431 c_incomplete_type_error (loc, NULL_TREE, type);
2432 return error_mark_node;
2435 field = lookup_field (type, component);
2437 if (!field)
2439 tree guessed_id = lookup_field_fuzzy (type, component);
2440 if (guessed_id)
2442 /* Attempt to provide a fixit replacement hint, if
2443 we have a valid range for the component. */
2444 location_t reported_loc
2445 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2446 gcc_rich_location rich_loc (reported_loc);
2447 if (component_loc != UNKNOWN_LOCATION)
2448 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2449 error_at (&rich_loc,
2450 "%qT has no member named %qE; did you mean %qE?",
2451 type, component, guessed_id);
2453 else
2454 error_at (loc, "%qT has no member named %qE", type, component);
2455 return error_mark_node;
2458 /* Accessing elements of atomic structures or unions is undefined
2459 behavior (C11 6.5.2.3#5). */
2460 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2462 if (code == RECORD_TYPE)
2463 warning_at (loc, 0, "accessing a member %qE of an atomic "
2464 "structure %qE", component, datum);
2465 else
2466 warning_at (loc, 0, "accessing a member %qE of an atomic "
2467 "union %qE", component, datum);
2470 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2471 This might be better solved in future the way the C++ front
2472 end does it - by giving the anonymous entities each a
2473 separate name and type, and then have build_component_ref
2474 recursively call itself. We can't do that here. */
2477 tree subdatum = TREE_VALUE (field);
2478 int quals;
2479 tree subtype;
2480 bool use_datum_quals;
2482 if (TREE_TYPE (subdatum) == error_mark_node)
2483 return error_mark_node;
2485 /* If this is an rvalue, it does not have qualifiers in C
2486 standard terms and we must avoid propagating such
2487 qualifiers down to a non-lvalue array that is then
2488 converted to a pointer. */
2489 use_datum_quals = (datum_lvalue
2490 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2492 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2493 if (use_datum_quals)
2494 quals |= TYPE_QUALS (TREE_TYPE (datum));
2495 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2497 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2498 NULL_TREE);
2499 SET_EXPR_LOCATION (ref, loc);
2500 if (TREE_READONLY (subdatum)
2501 || (use_datum_quals && TREE_READONLY (datum)))
2502 TREE_READONLY (ref) = 1;
2503 if (TREE_THIS_VOLATILE (subdatum)
2504 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2505 TREE_THIS_VOLATILE (ref) = 1;
2507 if (TREE_DEPRECATED (subdatum))
2508 warn_deprecated_use (subdatum, NULL_TREE);
2510 datum = ref;
2512 field = TREE_CHAIN (field);
2514 while (field);
2516 return ref;
2518 else if (should_suggest_deref_p (type))
2520 /* Special-case the error message for "ptr.field" for the case
2521 where the user has confused "." vs "->". */
2522 rich_location richloc (line_table, loc);
2523 /* "loc" should be the "." token. */
2524 richloc.add_fixit_replace ("->");
2525 error_at (&richloc,
2526 "%qE is a pointer; did you mean to use %<->%>?",
2527 datum);
2528 return error_mark_node;
2530 else if (code != ERROR_MARK)
2531 error_at (loc,
2532 "request for member %qE in something not a structure or union",
2533 component);
2535 return error_mark_node;
2538 /* Given an expression PTR for a pointer, return an expression
2539 for the value pointed to.
2540 ERRORSTRING is the name of the operator to appear in error messages.
2542 LOC is the location to use for the generated tree. */
2544 tree
2545 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2547 tree pointer = default_conversion (ptr);
2548 tree type = TREE_TYPE (pointer);
2549 tree ref;
2551 if (TREE_CODE (type) == POINTER_TYPE)
2553 if (CONVERT_EXPR_P (pointer)
2554 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2556 /* If a warning is issued, mark it to avoid duplicates from
2557 the backend. This only needs to be done at
2558 warn_strict_aliasing > 2. */
2559 if (warn_strict_aliasing > 2)
2560 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2561 type, TREE_OPERAND (pointer, 0)))
2562 TREE_NO_WARNING (pointer) = 1;
2565 if (TREE_CODE (pointer) == ADDR_EXPR
2566 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2567 == TREE_TYPE (type)))
2569 ref = TREE_OPERAND (pointer, 0);
2570 protected_set_expr_location (ref, loc);
2571 return ref;
2573 else
2575 tree t = TREE_TYPE (type);
2577 ref = build1 (INDIRECT_REF, t, pointer);
2579 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2580 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2582 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2583 so that we get the proper error message if the result is used
2584 to assign to. Also, &* is supposed to be a no-op.
2585 And ANSI C seems to specify that the type of the result
2586 should be the const type. */
2587 /* A de-reference of a pointer to const is not a const. It is valid
2588 to change it via some other pointer. */
2589 TREE_READONLY (ref) = TYPE_READONLY (t);
2590 TREE_SIDE_EFFECTS (ref)
2591 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2592 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2593 protected_set_expr_location (ref, loc);
2594 return ref;
2597 else if (TREE_CODE (pointer) != ERROR_MARK)
2598 invalid_indirection_error (loc, type, errstring);
2600 return error_mark_node;
2603 /* This handles expressions of the form "a[i]", which denotes
2604 an array reference.
2606 This is logically equivalent in C to *(a+i), but we may do it differently.
2607 If A is a variable or a member, we generate a primitive ARRAY_REF.
2608 This avoids forcing the array out of registers, and can work on
2609 arrays that are not lvalues (for example, members of structures returned
2610 by functions).
2612 For vector types, allow vector[i] but not i[vector], and create
2613 *(((type*)&vectortype) + i) for the expression.
2615 LOC is the location to use for the returned expression. */
2617 tree
2618 build_array_ref (location_t loc, tree array, tree index)
2620 tree ret;
2621 bool swapped = false;
2622 if (TREE_TYPE (array) == error_mark_node
2623 || TREE_TYPE (index) == error_mark_node)
2624 return error_mark_node;
2626 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2627 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2628 /* Allow vector[index] but not index[vector]. */
2629 && !gnu_vector_type_p (TREE_TYPE (array)))
2631 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2632 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2634 error_at (loc,
2635 "subscripted value is neither array nor pointer nor vector");
2637 return error_mark_node;
2639 std::swap (array, index);
2640 swapped = true;
2643 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2645 error_at (loc, "array subscript is not an integer");
2646 return error_mark_node;
2649 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2651 error_at (loc, "subscripted value is pointer to function");
2652 return error_mark_node;
2655 /* ??? Existing practice has been to warn only when the char
2656 index is syntactically the index, not for char[array]. */
2657 if (!swapped)
2658 warn_array_subscript_with_type_char (loc, index);
2660 /* Apply default promotions *after* noticing character types. */
2661 index = default_conversion (index);
2662 if (index == error_mark_node)
2663 return error_mark_node;
2665 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2667 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2668 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2670 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2672 tree rval, type;
2674 /* An array that is indexed by a non-constant
2675 cannot be stored in a register; we must be able to do
2676 address arithmetic on its address.
2677 Likewise an array of elements of variable size. */
2678 if (TREE_CODE (index) != INTEGER_CST
2679 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2680 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2682 if (!c_mark_addressable (array, true))
2683 return error_mark_node;
2685 /* An array that is indexed by a constant value which is not within
2686 the array bounds cannot be stored in a register either; because we
2687 would get a crash in store_bit_field/extract_bit_field when trying
2688 to access a non-existent part of the register. */
2689 if (TREE_CODE (index) == INTEGER_CST
2690 && TYPE_DOMAIN (TREE_TYPE (array))
2691 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2693 if (!c_mark_addressable (array))
2694 return error_mark_node;
2697 if ((pedantic || warn_c90_c99_compat)
2698 && ! was_vector)
2700 tree foo = array;
2701 while (TREE_CODE (foo) == COMPONENT_REF)
2702 foo = TREE_OPERAND (foo, 0);
2703 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2704 pedwarn (loc, OPT_Wpedantic,
2705 "ISO C forbids subscripting %<register%> array");
2706 else if (!lvalue_p (foo))
2707 pedwarn_c90 (loc, OPT_Wpedantic,
2708 "ISO C90 forbids subscripting non-lvalue "
2709 "array");
2712 type = TREE_TYPE (TREE_TYPE (array));
2713 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2714 /* Array ref is const/volatile if the array elements are
2715 or if the array is. */
2716 TREE_READONLY (rval)
2717 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2718 | TREE_READONLY (array));
2719 TREE_SIDE_EFFECTS (rval)
2720 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2721 | TREE_SIDE_EFFECTS (array));
2722 TREE_THIS_VOLATILE (rval)
2723 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2724 /* This was added by rms on 16 Nov 91.
2725 It fixes vol struct foo *a; a->elts[1]
2726 in an inline function.
2727 Hope it doesn't break something else. */
2728 | TREE_THIS_VOLATILE (array));
2729 ret = require_complete_type (loc, rval);
2730 protected_set_expr_location (ret, loc);
2731 if (non_lvalue)
2732 ret = non_lvalue_loc (loc, ret);
2733 return ret;
2735 else
2737 tree ar = default_conversion (array);
2739 if (ar == error_mark_node)
2740 return ar;
2742 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2743 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2745 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2746 index, false),
2747 RO_ARRAY_INDEXING);
2748 if (non_lvalue)
2749 ret = non_lvalue_loc (loc, ret);
2750 return ret;
2754 /* Build an external reference to identifier ID. FUN indicates
2755 whether this will be used for a function call. LOC is the source
2756 location of the identifier. This sets *TYPE to the type of the
2757 identifier, which is not the same as the type of the returned value
2758 for CONST_DECLs defined as enum constants. If the type of the
2759 identifier is not available, *TYPE is set to NULL. */
2760 tree
2761 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2763 tree ref;
2764 tree decl = lookup_name (id);
2766 /* In Objective-C, an instance variable (ivar) may be preferred to
2767 whatever lookup_name() found. */
2768 decl = objc_lookup_ivar (decl, id);
2770 *type = NULL;
2771 if (decl && decl != error_mark_node)
2773 ref = decl;
2774 *type = TREE_TYPE (ref);
2776 else if (fun)
2777 /* Implicit function declaration. */
2778 ref = implicitly_declare (loc, id);
2779 else if (decl == error_mark_node)
2780 /* Don't complain about something that's already been
2781 complained about. */
2782 return error_mark_node;
2783 else
2785 undeclared_variable (loc, id);
2786 return error_mark_node;
2789 if (TREE_TYPE (ref) == error_mark_node)
2790 return error_mark_node;
2792 if (TREE_DEPRECATED (ref))
2793 warn_deprecated_use (ref, NULL_TREE);
2795 /* Recursive call does not count as usage. */
2796 if (ref != current_function_decl)
2798 TREE_USED (ref) = 1;
2801 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2803 if (!in_sizeof && !in_typeof)
2804 C_DECL_USED (ref) = 1;
2805 else if (DECL_INITIAL (ref) == NULL_TREE
2806 && DECL_EXTERNAL (ref)
2807 && !TREE_PUBLIC (ref))
2808 record_maybe_used_decl (ref);
2811 if (TREE_CODE (ref) == CONST_DECL)
2813 used_types_insert (TREE_TYPE (ref));
2815 if (warn_cxx_compat
2816 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2817 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2819 warning_at (loc, OPT_Wc___compat,
2820 ("enum constant defined in struct or union "
2821 "is not visible in C++"));
2822 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2825 ref = DECL_INITIAL (ref);
2826 TREE_CONSTANT (ref) = 1;
2828 else if (current_function_decl != NULL_TREE
2829 && !DECL_FILE_SCOPE_P (current_function_decl)
2830 && (VAR_OR_FUNCTION_DECL_P (ref)
2831 || TREE_CODE (ref) == PARM_DECL))
2833 tree context = decl_function_context (ref);
2835 if (context != NULL_TREE && context != current_function_decl)
2836 DECL_NONLOCAL (ref) = 1;
2838 /* C99 6.7.4p3: An inline definition of a function with external
2839 linkage ... shall not contain a reference to an identifier with
2840 internal linkage. */
2841 else if (current_function_decl != NULL_TREE
2842 && DECL_DECLARED_INLINE_P (current_function_decl)
2843 && DECL_EXTERNAL (current_function_decl)
2844 && VAR_OR_FUNCTION_DECL_P (ref)
2845 && (!VAR_P (ref) || TREE_STATIC (ref))
2846 && ! TREE_PUBLIC (ref)
2847 && DECL_CONTEXT (ref) != current_function_decl)
2848 record_inline_static (loc, current_function_decl, ref,
2849 csi_internal);
2851 return ref;
2854 /* Record details of decls possibly used inside sizeof or typeof. */
2855 struct maybe_used_decl
2857 /* The decl. */
2858 tree decl;
2859 /* The level seen at (in_sizeof + in_typeof). */
2860 int level;
2861 /* The next one at this level or above, or NULL. */
2862 struct maybe_used_decl *next;
2865 static struct maybe_used_decl *maybe_used_decls;
2867 /* Record that DECL, an undefined static function reference seen
2868 inside sizeof or typeof, might be used if the operand of sizeof is
2869 a VLA type or the operand of typeof is a variably modified
2870 type. */
2872 static void
2873 record_maybe_used_decl (tree decl)
2875 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2876 t->decl = decl;
2877 t->level = in_sizeof + in_typeof;
2878 t->next = maybe_used_decls;
2879 maybe_used_decls = t;
2882 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2883 USED is false, just discard them. If it is true, mark them used
2884 (if no longer inside sizeof or typeof) or move them to the next
2885 level up (if still inside sizeof or typeof). */
2887 void
2888 pop_maybe_used (bool used)
2890 struct maybe_used_decl *p = maybe_used_decls;
2891 int cur_level = in_sizeof + in_typeof;
2892 while (p && p->level > cur_level)
2894 if (used)
2896 if (cur_level == 0)
2897 C_DECL_USED (p->decl) = 1;
2898 else
2899 p->level = cur_level;
2901 p = p->next;
2903 if (!used || cur_level == 0)
2904 maybe_used_decls = p;
2907 /* Return the result of sizeof applied to EXPR. */
2909 struct c_expr
2910 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2912 struct c_expr ret;
2913 if (expr.value == error_mark_node)
2915 ret.value = error_mark_node;
2916 ret.original_code = ERROR_MARK;
2917 ret.original_type = NULL;
2918 pop_maybe_used (false);
2920 else
2922 bool expr_const_operands = true;
2924 if (TREE_CODE (expr.value) == PARM_DECL
2925 && C_ARRAY_PARAMETER (expr.value))
2927 auto_diagnostic_group d;
2928 if (warning_at (loc, OPT_Wsizeof_array_argument,
2929 "%<sizeof%> on array function parameter %qE will "
2930 "return size of %qT", expr.value,
2931 TREE_TYPE (expr.value)))
2932 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2934 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2935 &expr_const_operands);
2936 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2937 c_last_sizeof_arg = expr.value;
2938 c_last_sizeof_loc = loc;
2939 ret.original_code = SIZEOF_EXPR;
2940 ret.original_type = NULL;
2941 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2943 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2944 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2945 folded_expr, ret.value);
2946 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2947 SET_EXPR_LOCATION (ret.value, loc);
2949 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2951 return ret;
2954 /* Return the result of sizeof applied to T, a structure for the type
2955 name passed to sizeof (rather than the type itself). LOC is the
2956 location of the original expression. */
2958 struct c_expr
2959 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2961 tree type;
2962 struct c_expr ret;
2963 tree type_expr = NULL_TREE;
2964 bool type_expr_const = true;
2965 type = groktypename (t, &type_expr, &type_expr_const);
2966 ret.value = c_sizeof (loc, type);
2967 c_last_sizeof_arg = type;
2968 c_last_sizeof_loc = loc;
2969 ret.original_code = SIZEOF_EXPR;
2970 ret.original_type = NULL;
2971 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2972 && c_vla_type_p (type))
2974 /* If the type is a [*] array, it is a VLA but is represented as
2975 having a size of zero. In such a case we must ensure that
2976 the result of sizeof does not get folded to a constant by
2977 c_fully_fold, because if the size is evaluated the result is
2978 not constant and so constraints on zero or negative size
2979 arrays must not be applied when this sizeof call is inside
2980 another array declarator. */
2981 if (!type_expr)
2982 type_expr = integer_zero_node;
2983 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2984 type_expr, ret.value);
2985 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2987 pop_maybe_used (type != error_mark_node
2988 ? C_TYPE_VARIABLE_SIZE (type) : false);
2989 return ret;
2992 /* Build a function call to function FUNCTION with parameters PARAMS.
2993 The function call is at LOC.
2994 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2995 TREE_VALUE of each node is a parameter-expression.
2996 FUNCTION's data type may be a function type or a pointer-to-function. */
2998 tree
2999 build_function_call (location_t loc, tree function, tree params)
3001 vec<tree, va_gc> *v;
3002 tree ret;
3004 vec_alloc (v, list_length (params));
3005 for (; params; params = TREE_CHAIN (params))
3006 v->quick_push (TREE_VALUE (params));
3007 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3008 vec_free (v);
3009 return ret;
3012 /* Give a note about the location of the declaration of DECL. */
3014 static void
3015 inform_declaration (tree decl)
3017 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
3018 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3021 /* Build a function call to function FUNCTION with parameters PARAMS.
3022 If FUNCTION is the result of resolving an overloaded target built-in,
3023 ORIG_FUNDECL is the original function decl, otherwise it is null.
3024 ORIGTYPES, if not NULL, is a vector of types; each element is
3025 either NULL or the original type of the corresponding element in
3026 PARAMS. The original type may differ from TREE_TYPE of the
3027 parameter for enums. FUNCTION's data type may be a function type
3028 or pointer-to-function. This function changes the elements of
3029 PARAMS. */
3031 tree
3032 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3033 tree function, vec<tree, va_gc> *params,
3034 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3036 tree fntype, fundecl = NULL_TREE;
3037 tree name = NULL_TREE, result;
3038 tree tem;
3039 int nargs;
3040 tree *argarray;
3043 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3044 STRIP_TYPE_NOPS (function);
3046 /* Convert anything with function type to a pointer-to-function. */
3047 if (TREE_CODE (function) == FUNCTION_DECL)
3049 name = DECL_NAME (function);
3051 if (flag_tm)
3052 tm_malloc_replacement (function);
3053 fundecl = function;
3054 if (!orig_fundecl)
3055 orig_fundecl = fundecl;
3056 /* Atomic functions have type checking/casting already done. They are
3057 often rewritten and don't match the original parameter list. */
3058 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3059 origtypes = NULL;
3061 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3062 function = function_to_pointer_conversion (loc, function);
3064 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3065 expressions, like those used for ObjC messenger dispatches. */
3066 if (params && !params->is_empty ())
3067 function = objc_rewrite_function_call (function, (*params)[0]);
3069 function = c_fully_fold (function, false, NULL);
3071 fntype = TREE_TYPE (function);
3073 if (TREE_CODE (fntype) == ERROR_MARK)
3074 return error_mark_node;
3076 if (!(TREE_CODE (fntype) == POINTER_TYPE
3077 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3079 if (!flag_diagnostics_show_caret)
3080 error_at (loc,
3081 "called object %qE is not a function or function pointer",
3082 function);
3083 else if (DECL_P (function))
3085 error_at (loc,
3086 "called object %qD is not a function or function pointer",
3087 function);
3088 inform_declaration (function);
3090 else
3091 error_at (loc,
3092 "called object is not a function or function pointer");
3093 return error_mark_node;
3096 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3097 current_function_returns_abnormally = 1;
3099 /* fntype now gets the type of function pointed to. */
3100 fntype = TREE_TYPE (fntype);
3102 /* Convert the parameters to the types declared in the
3103 function prototype, or apply default promotions. */
3105 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3106 origtypes, function, fundecl);
3107 if (nargs < 0)
3108 return error_mark_node;
3110 /* Check that the function is called through a compatible prototype.
3111 If it is not, warn. */
3112 if (CONVERT_EXPR_P (function)
3113 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3114 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3115 && !comptypes (fntype, TREE_TYPE (tem)))
3117 tree return_type = TREE_TYPE (fntype);
3119 /* This situation leads to run-time undefined behavior. We can't,
3120 therefore, simply error unless we can prove that all possible
3121 executions of the program must execute the code. */
3122 warning_at (loc, 0, "function called through a non-compatible type");
3124 if (VOID_TYPE_P (return_type)
3125 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3126 pedwarn (loc, 0,
3127 "function with qualified void return type called");
3130 argarray = vec_safe_address (params);
3132 /* Check that arguments to builtin functions match the expectations. */
3133 if (fundecl
3134 && fndecl_built_in_p (fundecl)
3135 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3136 orig_fundecl, nargs, argarray))
3137 return error_mark_node;
3139 /* Check that the arguments to the function are valid. */
3140 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3141 nargs, argarray, &arg_loc);
3143 if (name != NULL_TREE
3144 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3146 if (require_constant_value)
3147 result
3148 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3149 function, nargs, argarray);
3150 else
3151 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3152 function, nargs, argarray);
3153 if (TREE_CODE (result) == NOP_EXPR
3154 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3155 STRIP_TYPE_NOPS (result);
3157 else
3158 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3159 function, nargs, argarray);
3160 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3161 later. */
3162 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3163 TREE_NO_WARNING (result) = 1;
3165 /* In this improbable scenario, a nested function returns a VM type.
3166 Create a TARGET_EXPR so that the call always has a LHS, much as
3167 what the C++ FE does for functions returning non-PODs. */
3168 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3170 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3171 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3172 NULL_TREE, NULL_TREE);
3175 if (VOID_TYPE_P (TREE_TYPE (result)))
3177 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3178 pedwarn (loc, 0,
3179 "function with qualified void return type called");
3180 return result;
3182 return require_complete_type (loc, result);
3185 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3187 tree
3188 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3189 tree function, vec<tree, va_gc> *params,
3190 vec<tree, va_gc> *origtypes)
3192 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3193 STRIP_TYPE_NOPS (function);
3195 /* Convert anything with function type to a pointer-to-function. */
3196 if (TREE_CODE (function) == FUNCTION_DECL)
3198 /* Implement type-directed function overloading for builtins.
3199 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3200 handle all the type checking. The result is a complete expression
3201 that implements this function call. */
3202 tree tem = resolve_overloaded_builtin (loc, function, params);
3203 if (tem)
3204 return tem;
3206 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3209 /* Helper for convert_arguments called to convert the VALue of argument
3210 number ARGNUM from ORIGTYPE to the corresponding parameter number
3211 PARMNUM and TYPE.
3212 PLOC is the location where the conversion is being performed.
3213 FUNCTION and FUNDECL are the same as in convert_arguments.
3214 VALTYPE is the original type of VAL before the conversion and,
3215 for EXCESS_PRECISION_EXPR, the operand of the expression.
3216 NPC is true if VAL represents the null pointer constant (VAL itself
3217 will have been folded to an integer constant).
3218 RNAME is the same as FUNCTION except in Objective C when it's
3219 the function selector.
3220 EXCESS_PRECISION is true when VAL was originally represented
3221 as EXCESS_PRECISION_EXPR.
3222 WARNOPT is the same as in convert_for_assignment. */
3224 static tree
3225 convert_argument (location_t ploc, tree function, tree fundecl,
3226 tree type, tree origtype, tree val, tree valtype,
3227 bool npc, tree rname, int parmnum, int argnum,
3228 bool excess_precision, int warnopt)
3230 /* Formal parm type is specified by a function prototype. */
3232 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3234 error_at (ploc, "type of formal parameter %d is incomplete",
3235 parmnum + 1);
3236 return val;
3239 /* Optionally warn about conversions that differ from the default
3240 conversions. */
3241 if (warn_traditional_conversion || warn_traditional)
3243 unsigned int formal_prec = TYPE_PRECISION (type);
3245 if (INTEGRAL_TYPE_P (type)
3246 && TREE_CODE (valtype) == REAL_TYPE)
3247 warning_at (ploc, OPT_Wtraditional_conversion,
3248 "passing argument %d of %qE as integer rather "
3249 "than floating due to prototype",
3250 argnum, rname);
3251 if (INTEGRAL_TYPE_P (type)
3252 && TREE_CODE (valtype) == COMPLEX_TYPE)
3253 warning_at (ploc, OPT_Wtraditional_conversion,
3254 "passing argument %d of %qE as integer rather "
3255 "than complex due to prototype",
3256 argnum, rname);
3257 else if (TREE_CODE (type) == COMPLEX_TYPE
3258 && TREE_CODE (valtype) == REAL_TYPE)
3259 warning_at (ploc, OPT_Wtraditional_conversion,
3260 "passing argument %d of %qE as complex rather "
3261 "than floating due to prototype",
3262 argnum, rname);
3263 else if (TREE_CODE (type) == REAL_TYPE
3264 && INTEGRAL_TYPE_P (valtype))
3265 warning_at (ploc, OPT_Wtraditional_conversion,
3266 "passing argument %d of %qE as floating rather "
3267 "than integer due to prototype",
3268 argnum, rname);
3269 else if (TREE_CODE (type) == COMPLEX_TYPE
3270 && INTEGRAL_TYPE_P (valtype))
3271 warning_at (ploc, OPT_Wtraditional_conversion,
3272 "passing argument %d of %qE as complex rather "
3273 "than integer due to prototype",
3274 argnum, rname);
3275 else if (TREE_CODE (type) == REAL_TYPE
3276 && TREE_CODE (valtype) == COMPLEX_TYPE)
3277 warning_at (ploc, OPT_Wtraditional_conversion,
3278 "passing argument %d of %qE as floating rather "
3279 "than complex due to prototype",
3280 argnum, rname);
3281 /* ??? At some point, messages should be written about
3282 conversions between complex types, but that's too messy
3283 to do now. */
3284 else if (TREE_CODE (type) == REAL_TYPE
3285 && TREE_CODE (valtype) == REAL_TYPE)
3287 /* Warn if any argument is passed as `float',
3288 since without a prototype it would be `double'. */
3289 if (formal_prec == TYPE_PRECISION (float_type_node)
3290 && type != dfloat32_type_node)
3291 warning_at (ploc, 0,
3292 "passing argument %d of %qE as %<float%> "
3293 "rather than %<double%> due to prototype",
3294 argnum, rname);
3296 /* Warn if mismatch between argument and prototype
3297 for decimal float types. Warn of conversions with
3298 binary float types and of precision narrowing due to
3299 prototype. */
3300 else if (type != valtype
3301 && (type == dfloat32_type_node
3302 || type == dfloat64_type_node
3303 || type == dfloat128_type_node
3304 || valtype == dfloat32_type_node
3305 || valtype == dfloat64_type_node
3306 || valtype == dfloat128_type_node)
3307 && (formal_prec
3308 <= TYPE_PRECISION (valtype)
3309 || (type == dfloat128_type_node
3310 && (valtype
3311 != dfloat64_type_node
3312 && (valtype
3313 != dfloat32_type_node)))
3314 || (type == dfloat64_type_node
3315 && (valtype
3316 != dfloat32_type_node))))
3317 warning_at (ploc, 0,
3318 "passing argument %d of %qE as %qT "
3319 "rather than %qT due to prototype",
3320 argnum, rname, type, valtype);
3323 /* Detect integer changing in width or signedness.
3324 These warnings are only activated with
3325 -Wtraditional-conversion, not with -Wtraditional. */
3326 else if (warn_traditional_conversion
3327 && INTEGRAL_TYPE_P (type)
3328 && INTEGRAL_TYPE_P (valtype))
3330 tree would_have_been = default_conversion (val);
3331 tree type1 = TREE_TYPE (would_have_been);
3333 if (val == error_mark_node)
3334 /* VAL could have been of incomplete type. */;
3335 else if (TREE_CODE (type) == ENUMERAL_TYPE
3336 && (TYPE_MAIN_VARIANT (type)
3337 == TYPE_MAIN_VARIANT (valtype)))
3338 /* No warning if function asks for enum
3339 and the actual arg is that enum type. */
3341 else if (formal_prec != TYPE_PRECISION (type1))
3342 warning_at (ploc, OPT_Wtraditional_conversion,
3343 "passing argument %d of %qE "
3344 "with different width due to prototype",
3345 argnum, rname);
3346 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3348 /* Don't complain if the formal parameter type
3349 is an enum, because we can't tell now whether
3350 the value was an enum--even the same enum. */
3351 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3353 else if (TREE_CODE (val) == INTEGER_CST
3354 && int_fits_type_p (val, type))
3355 /* Change in signedness doesn't matter
3356 if a constant value is unaffected. */
3358 /* If the value is extended from a narrower
3359 unsigned type, it doesn't matter whether we
3360 pass it as signed or unsigned; the value
3361 certainly is the same either way. */
3362 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3363 && TYPE_UNSIGNED (valtype))
3365 else if (TYPE_UNSIGNED (type))
3366 warning_at (ploc, OPT_Wtraditional_conversion,
3367 "passing argument %d of %qE "
3368 "as unsigned due to prototype",
3369 argnum, rname);
3370 else
3371 warning_at (ploc, OPT_Wtraditional_conversion,
3372 "passing argument %d of %qE "
3373 "as signed due to prototype",
3374 argnum, rname);
3378 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3379 sake of better warnings from convert_and_check. */
3380 if (excess_precision)
3381 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3383 tree parmval = convert_for_assignment (ploc, ploc, type,
3384 val, origtype, ic_argpass,
3385 npc, fundecl, function,
3386 parmnum + 1, warnopt);
3388 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3389 && INTEGRAL_TYPE_P (type)
3390 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3391 parmval = default_conversion (parmval);
3393 return parmval;
3396 /* Convert the argument expressions in the vector VALUES
3397 to the types in the list TYPELIST.
3399 If TYPELIST is exhausted, or when an element has NULL as its type,
3400 perform the default conversions.
3402 ORIGTYPES is the original types of the expressions in VALUES. This
3403 holds the type of enum values which have been converted to integral
3404 types. It may be NULL.
3406 FUNCTION is a tree for the called function. It is used only for
3407 error messages, where it is formatted with %qE.
3409 This is also where warnings about wrong number of args are generated.
3411 ARG_LOC are locations of function arguments (if any).
3413 Returns the actual number of arguments processed (which may be less
3414 than the length of VALUES in some error situations), or -1 on
3415 failure. */
3417 static int
3418 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3419 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3420 tree function, tree fundecl)
3422 unsigned int parmnum;
3423 bool error_args = false;
3424 const bool type_generic = fundecl
3425 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3426 bool type_generic_remove_excess_precision = false;
3427 bool type_generic_overflow_p = false;
3428 tree selector;
3430 /* Change pointer to function to the function itself for
3431 diagnostics. */
3432 if (TREE_CODE (function) == ADDR_EXPR
3433 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3434 function = TREE_OPERAND (function, 0);
3436 /* Handle an ObjC selector specially for diagnostics. */
3437 selector = objc_message_selector ();
3439 /* For a call to a built-in function declared without a prototype,
3440 set to the built-in function's argument list. */
3441 tree builtin_typelist = NULL_TREE;
3443 /* For type-generic built-in functions, determine whether excess
3444 precision should be removed (classification) or not
3445 (comparison). */
3446 if (fundecl
3447 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3449 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3450 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3452 /* For a call to a built-in function declared without a prototype
3453 use the types of the parameters of the internal built-in to
3454 match those of the arguments to. */
3455 if (tree bdecl = builtin_decl_explicit (code))
3456 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3459 /* For type-generic built-in functions, determine whether excess
3460 precision should be removed (classification) or not
3461 (comparison). */
3462 if (type_generic)
3463 switch (code)
3465 case BUILT_IN_ISFINITE:
3466 case BUILT_IN_ISINF:
3467 case BUILT_IN_ISINF_SIGN:
3468 case BUILT_IN_ISNAN:
3469 case BUILT_IN_ISNORMAL:
3470 case BUILT_IN_FPCLASSIFY:
3471 type_generic_remove_excess_precision = true;
3472 break;
3474 case BUILT_IN_ADD_OVERFLOW_P:
3475 case BUILT_IN_SUB_OVERFLOW_P:
3476 case BUILT_IN_MUL_OVERFLOW_P:
3477 /* The last argument of these type-generic builtins
3478 should not be promoted. */
3479 type_generic_overflow_p = true;
3480 break;
3482 default:
3483 break;
3487 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3488 individual converted arguments. */
3490 tree typetail, builtin_typetail, val;
3491 for (typetail = typelist,
3492 builtin_typetail = builtin_typelist,
3493 parmnum = 0;
3494 values && values->iterate (parmnum, &val);
3495 ++parmnum)
3497 /* The type of the function parameter (if it was declared with one). */
3498 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3499 /* The type of the built-in function parameter (if the function
3500 is a built-in). Used to detect type incompatibilities in
3501 calls to built-ins declared without a prototype. */
3502 tree builtin_type = (builtin_typetail
3503 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3504 /* The original type of the argument being passed to the function. */
3505 tree valtype = TREE_TYPE (val);
3506 /* The called function (or function selector in Objective C). */
3507 tree rname = function;
3508 int argnum = parmnum + 1;
3509 const char *invalid_func_diag;
3510 /* Set for EXCESS_PRECISION_EXPR arguments. */
3511 bool excess_precision = false;
3512 /* The value of the argument after conversion to the type
3513 of the function parameter it is passed to. */
3514 tree parmval;
3515 /* Some __atomic_* builtins have additional hidden argument at
3516 position 0. */
3517 location_t ploc
3518 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3519 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3520 : input_location;
3522 if (type == void_type_node)
3524 if (selector)
3525 error_at (loc, "too many arguments to method %qE", selector);
3526 else
3527 error_at (loc, "too many arguments to function %qE", function);
3528 inform_declaration (fundecl);
3529 return error_args ? -1 : (int) parmnum;
3532 if (builtin_type == void_type_node)
3534 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3535 "too many arguments to built-in function %qE "
3536 "expecting %d", function, parmnum))
3537 inform_declaration (fundecl);
3538 builtin_typetail = NULL_TREE;
3541 if (selector && argnum > 2)
3543 rname = selector;
3544 argnum -= 2;
3547 /* Determine if VAL is a null pointer constant before folding it. */
3548 bool npc = null_pointer_constant_p (val);
3550 /* If there is excess precision and a prototype, convert once to
3551 the required type rather than converting via the semantic
3552 type. Likewise without a prototype a float value represented
3553 as long double should be converted once to double. But for
3554 type-generic classification functions excess precision must
3555 be removed here. */
3556 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3557 && (type || !type_generic || !type_generic_remove_excess_precision))
3559 val = TREE_OPERAND (val, 0);
3560 excess_precision = true;
3562 val = c_fully_fold (val, false, NULL);
3563 STRIP_TYPE_NOPS (val);
3565 val = require_complete_type (ploc, val);
3567 /* Some floating-point arguments must be promoted to double when
3568 no type is specified by a prototype. This applies to
3569 arguments of type float, and to architecture-specific types
3570 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3571 bool promote_float_arg = false;
3572 if (type == NULL_TREE
3573 && TREE_CODE (valtype) == REAL_TYPE
3574 && (TYPE_PRECISION (valtype)
3575 <= TYPE_PRECISION (double_type_node))
3576 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3577 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3578 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3580 /* Promote this argument, unless it has a _FloatN or
3581 _FloatNx type. */
3582 promote_float_arg = true;
3583 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3584 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3586 promote_float_arg = false;
3587 break;
3591 if (type != NULL_TREE)
3593 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3594 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3595 val, valtype, npc, rname, parmnum, argnum,
3596 excess_precision, 0);
3598 else if (promote_float_arg)
3600 if (type_generic)
3601 parmval = val;
3602 else
3604 /* Convert `float' to `double'. */
3605 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3606 warning_at (ploc, OPT_Wdouble_promotion,
3607 "implicit conversion from %qT to %qT when passing "
3608 "argument to function",
3609 valtype, double_type_node);
3610 parmval = convert (double_type_node, val);
3613 else if ((excess_precision && !type_generic)
3614 || (type_generic_overflow_p && parmnum == 2))
3615 /* A "double" argument with excess precision being passed
3616 without a prototype or in variable arguments.
3617 The last argument of __builtin_*_overflow_p should not be
3618 promoted. */
3619 parmval = convert (valtype, val);
3620 else if ((invalid_func_diag =
3621 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3623 error (invalid_func_diag);
3624 return -1;
3626 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3628 return -1;
3630 else
3631 /* Convert `short' and `char' to full-size `int'. */
3632 parmval = default_conversion (val);
3634 (*values)[parmnum] = parmval;
3635 if (parmval == error_mark_node)
3636 error_args = true;
3638 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3640 /* For a call to a built-in function declared without a prototype,
3641 perform the conversions from the argument to the expected type
3642 but issue warnings rather than errors for any mismatches.
3643 Ignore the converted argument and use the PARMVAL obtained
3644 above by applying default conversions instead. */
3645 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3646 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3647 val, valtype, npc, rname, parmnum, argnum,
3648 excess_precision,
3649 OPT_Wbuiltin_declaration_mismatch);
3652 if (typetail)
3653 typetail = TREE_CHAIN (typetail);
3655 if (builtin_typetail)
3656 builtin_typetail = TREE_CHAIN (builtin_typetail);
3659 gcc_assert (parmnum == vec_safe_length (values));
3661 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3663 error_at (loc, "too few arguments to function %qE", function);
3664 inform_declaration (fundecl);
3665 return -1;
3668 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3670 unsigned nargs = parmnum;
3671 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3672 ++nargs;
3674 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3675 "too few arguments to built-in function %qE "
3676 "expecting %u", function, nargs - 1))
3677 inform_declaration (fundecl);
3680 return error_args ? -1 : (int) parmnum;
3683 /* This is the entry point used by the parser to build unary operators
3684 in the input. CODE, a tree_code, specifies the unary operator, and
3685 ARG is the operand. For unary plus, the C parser currently uses
3686 CONVERT_EXPR for code.
3688 LOC is the location to use for the tree generated.
3691 struct c_expr
3692 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3694 struct c_expr result;
3696 result.original_code = code;
3697 result.original_type = NULL;
3699 if (reject_gcc_builtin (arg.value))
3701 result.value = error_mark_node;
3703 else
3705 result.value = build_unary_op (loc, code, arg.value, false);
3707 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3708 overflow_warning (loc, result.value, arg.value);
3711 /* We are typically called when parsing a prefix token at LOC acting on
3712 ARG. Reflect this by updating the source range of the result to
3713 start at LOC and end at the end of ARG. */
3714 set_c_expr_source_range (&result,
3715 loc, arg.get_finish ());
3717 return result;
3720 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3722 static bool
3723 char_type_p (tree type)
3725 return (type == char_type_node
3726 || type == unsigned_char_type_node
3727 || type == signed_char_type_node
3728 || type == char16_type_node
3729 || type == char32_type_node);
3732 /* This is the entry point used by the parser to build binary operators
3733 in the input. CODE, a tree_code, specifies the binary operator, and
3734 ARG1 and ARG2 are the operands. In addition to constructing the
3735 expression, we check for operands that were written with other binary
3736 operators in a way that is likely to confuse the user.
3738 LOCATION is the location of the binary operator. */
3740 struct c_expr
3741 parser_build_binary_op (location_t location, enum tree_code code,
3742 struct c_expr arg1, struct c_expr arg2)
3744 struct c_expr result;
3746 enum tree_code code1 = arg1.original_code;
3747 enum tree_code code2 = arg2.original_code;
3748 tree type1 = (arg1.original_type
3749 ? arg1.original_type
3750 : TREE_TYPE (arg1.value));
3751 tree type2 = (arg2.original_type
3752 ? arg2.original_type
3753 : TREE_TYPE (arg2.value));
3755 result.value = build_binary_op (location, code,
3756 arg1.value, arg2.value, true);
3757 result.original_code = code;
3758 result.original_type = NULL;
3760 if (TREE_CODE (result.value) == ERROR_MARK)
3762 set_c_expr_source_range (&result,
3763 arg1.get_start (),
3764 arg2.get_finish ());
3765 return result;
3768 if (location != UNKNOWN_LOCATION)
3769 protected_set_expr_location (result.value, location);
3771 set_c_expr_source_range (&result,
3772 arg1.get_start (),
3773 arg2.get_finish ());
3775 /* Check for cases such as x+y<<z which users are likely
3776 to misinterpret. */
3777 if (warn_parentheses)
3778 warn_about_parentheses (location, code, code1, arg1.value, code2,
3779 arg2.value);
3781 if (warn_logical_op)
3782 warn_logical_operator (location, code, TREE_TYPE (result.value),
3783 code1, arg1.value, code2, arg2.value);
3785 if (warn_tautological_compare)
3787 tree lhs = arg1.value;
3788 tree rhs = arg2.value;
3789 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3791 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3792 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3793 lhs = NULL_TREE;
3794 else
3795 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3797 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3799 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3800 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3801 rhs = NULL_TREE;
3802 else
3803 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3805 if (lhs != NULL_TREE && rhs != NULL_TREE)
3806 warn_tautological_cmp (location, code, lhs, rhs);
3809 if (warn_logical_not_paren
3810 && TREE_CODE_CLASS (code) == tcc_comparison
3811 && code1 == TRUTH_NOT_EXPR
3812 && code2 != TRUTH_NOT_EXPR
3813 /* Avoid warning for !!x == y. */
3814 && (TREE_CODE (arg1.value) != NE_EXPR
3815 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3817 /* Avoid warning for !b == y where b has _Bool type. */
3818 tree t = integer_zero_node;
3819 if (TREE_CODE (arg1.value) == EQ_EXPR
3820 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3821 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3823 t = TREE_OPERAND (arg1.value, 0);
3826 if (TREE_TYPE (t) != integer_type_node)
3827 break;
3828 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3829 t = C_MAYBE_CONST_EXPR_EXPR (t);
3830 else if (CONVERT_EXPR_P (t))
3831 t = TREE_OPERAND (t, 0);
3832 else
3833 break;
3835 while (1);
3837 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3838 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3841 /* Warn about comparisons against string literals, with the exception
3842 of testing for equality or inequality of a string literal with NULL. */
3843 if (code == EQ_EXPR || code == NE_EXPR)
3845 if ((code1 == STRING_CST
3846 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3847 || (code2 == STRING_CST
3848 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3849 warning_at (location, OPT_Waddress,
3850 "comparison with string literal results in unspecified behavior");
3851 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3852 if (POINTER_TYPE_P (type1)
3853 && null_pointer_constant_p (arg2.value)
3854 && char_type_p (type2))
3856 auto_diagnostic_group d;
3857 if (warning_at (location, OPT_Wpointer_compare,
3858 "comparison between pointer and zero character "
3859 "constant"))
3860 inform (arg1.get_start (),
3861 "did you mean to dereference the pointer?");
3863 else if (POINTER_TYPE_P (type2)
3864 && null_pointer_constant_p (arg1.value)
3865 && char_type_p (type1))
3867 auto_diagnostic_group d;
3868 if (warning_at (location, OPT_Wpointer_compare,
3869 "comparison between pointer and zero character "
3870 "constant"))
3871 inform (arg2.get_start (),
3872 "did you mean to dereference the pointer?");
3875 else if (TREE_CODE_CLASS (code) == tcc_comparison
3876 && (code1 == STRING_CST || code2 == STRING_CST))
3877 warning_at (location, OPT_Waddress,
3878 "comparison with string literal results in unspecified behavior");
3880 if (TREE_OVERFLOW_P (result.value)
3881 && !TREE_OVERFLOW_P (arg1.value)
3882 && !TREE_OVERFLOW_P (arg2.value))
3883 overflow_warning (location, result.value);
3885 /* Warn about comparisons of different enum types. */
3886 if (warn_enum_compare
3887 && TREE_CODE_CLASS (code) == tcc_comparison
3888 && TREE_CODE (type1) == ENUMERAL_TYPE
3889 && TREE_CODE (type2) == ENUMERAL_TYPE
3890 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3891 warning_at (location, OPT_Wenum_compare,
3892 "comparison between %qT and %qT",
3893 type1, type2);
3895 return result;
3898 /* Return a tree for the difference of pointers OP0 and OP1.
3899 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3900 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3902 static tree
3903 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3905 tree restype = ptrdiff_type_node;
3906 tree result, inttype;
3908 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3909 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3910 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3911 tree orig_op0 = op0;
3912 tree orig_op1 = op1;
3914 /* If the operands point into different address spaces, we need to
3915 explicitly convert them to pointers into the common address space
3916 before we can subtract the numerical address values. */
3917 if (as0 != as1)
3919 addr_space_t as_common;
3920 tree common_type;
3922 /* Determine the common superset address space. This is guaranteed
3923 to exist because the caller verified that comp_target_types
3924 returned non-zero. */
3925 if (!addr_space_superset (as0, as1, &as_common))
3926 gcc_unreachable ();
3928 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3929 op0 = convert (common_type, op0);
3930 op1 = convert (common_type, op1);
3933 /* Determine integer type result of the subtraction. This will usually
3934 be the same as the result type (ptrdiff_t), but may need to be a wider
3935 type if pointers for the address space are wider than ptrdiff_t. */
3936 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3937 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3938 else
3939 inttype = restype;
3941 if (TREE_CODE (target_type) == VOID_TYPE)
3942 pedwarn (loc, OPT_Wpointer_arith,
3943 "pointer of type %<void *%> used in subtraction");
3944 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3945 pedwarn (loc, OPT_Wpointer_arith,
3946 "pointer to a function used in subtraction");
3948 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3950 gcc_assert (current_function_decl != NULL_TREE);
3952 op0 = save_expr (op0);
3953 op1 = save_expr (op1);
3955 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3956 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3959 /* First do the subtraction, then build the divide operator
3960 and only convert at the very end.
3961 Do not do default conversions in case restype is a short type. */
3963 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3964 pointers. If some platform cannot provide that, or has a larger
3965 ptrdiff_type to support differences larger than half the address
3966 space, cast the pointers to some larger integer type and do the
3967 computations in that type. */
3968 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3969 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3970 convert (inttype, op1), false);
3971 else
3973 /* Cast away qualifiers. */
3974 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3975 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3976 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3979 /* This generates an error if op1 is pointer to incomplete type. */
3980 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3981 error_at (loc, "arithmetic on pointer to an incomplete type");
3982 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
3983 TREE_TYPE (TREE_TYPE (orig_op0))))
3984 verify_type_context (loc, TCTX_POINTER_ARITH,
3985 TREE_TYPE (TREE_TYPE (orig_op1)));
3987 op1 = c_size_in_bytes (target_type);
3989 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3990 error_at (loc, "arithmetic on pointer to an empty aggregate");
3992 /* Divide by the size, in easiest possible way. */
3993 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3994 op0, convert (inttype, op1));
3996 /* Convert to final result type if necessary. */
3997 return convert (restype, result);
4000 /* Expand atomic compound assignments into an appropriate sequence as
4001 specified by the C11 standard section 6.5.16.2.
4003 _Atomic T1 E1
4004 T2 E2
4005 E1 op= E2
4007 This sequence is used for all types for which these operations are
4008 supported.
4010 In addition, built-in versions of the 'fe' prefixed routines may
4011 need to be invoked for floating point (real, complex or vector) when
4012 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4014 T1 newval;
4015 T1 old;
4016 T1 *addr
4017 T2 val
4018 fenv_t fenv
4020 addr = &E1;
4021 val = (E2);
4022 __atomic_load (addr, &old, SEQ_CST);
4023 feholdexcept (&fenv);
4024 loop:
4025 newval = old op val;
4026 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4027 SEQ_CST))
4028 goto done;
4029 feclearexcept (FE_ALL_EXCEPT);
4030 goto loop:
4031 done:
4032 feupdateenv (&fenv);
4034 The compiler will issue the __atomic_fetch_* built-in when possible,
4035 otherwise it will generate the generic form of the atomic operations.
4036 This requires temp(s) and has their address taken. The atomic processing
4037 is smart enough to figure out when the size of an object can utilize
4038 a lock-free version, and convert the built-in call to the appropriate
4039 lock-free routine. The optimizers will then dispose of any temps that
4040 are no longer required, and lock-free implementations are utilized as
4041 long as there is target support for the required size.
4043 If the operator is NOP_EXPR, then this is a simple assignment, and
4044 an __atomic_store is issued to perform the assignment rather than
4045 the above loop. */
4047 /* Build an atomic assignment at LOC, expanding into the proper
4048 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4049 the result of the operation, unless RETURN_OLD_P, in which case
4050 return the old value of LHS (this is only for postincrement and
4051 postdecrement). */
4053 static tree
4054 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4055 tree rhs, bool return_old_p)
4057 tree fndecl, func_call;
4058 vec<tree, va_gc> *params;
4059 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4060 tree old, old_addr;
4061 tree compound_stmt;
4062 tree stmt, goto_stmt;
4063 tree loop_label, loop_decl, done_label, done_decl;
4065 tree lhs_type = TREE_TYPE (lhs);
4066 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4067 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4068 tree rhs_semantic_type = TREE_TYPE (rhs);
4069 tree nonatomic_rhs_semantic_type;
4070 tree rhs_type;
4072 gcc_assert (TYPE_ATOMIC (lhs_type));
4074 if (return_old_p)
4075 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4077 /* Allocate enough vector items for a compare_exchange. */
4078 vec_alloc (params, 6);
4080 /* Create a compound statement to hold the sequence of statements
4081 with a loop. */
4082 compound_stmt = c_begin_compound_stmt (false);
4084 /* Remove any excess precision (which is only present here in the
4085 case of compound assignments). */
4086 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4088 gcc_assert (modifycode != NOP_EXPR);
4089 rhs = TREE_OPERAND (rhs, 0);
4091 rhs_type = TREE_TYPE (rhs);
4093 /* Fold the RHS if it hasn't already been folded. */
4094 if (modifycode != NOP_EXPR)
4095 rhs = c_fully_fold (rhs, false, NULL);
4097 /* Remove the qualifiers for the rest of the expressions and create
4098 the VAL temp variable to hold the RHS. */
4099 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4100 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4101 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4102 TYPE_UNQUALIFIED);
4103 val = create_tmp_var_raw (nonatomic_rhs_type);
4104 TREE_ADDRESSABLE (val) = 1;
4105 TREE_NO_WARNING (val) = 1;
4106 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4107 NULL_TREE);
4108 SET_EXPR_LOCATION (rhs, loc);
4109 add_stmt (rhs);
4111 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4112 an atomic_store. */
4113 if (modifycode == NOP_EXPR)
4115 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4116 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4117 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4118 params->quick_push (lhs_addr);
4119 params->quick_push (rhs);
4120 params->quick_push (seq_cst);
4121 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4122 add_stmt (func_call);
4124 /* Finish the compound statement. */
4125 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4127 /* VAL is the value which was stored, return a COMPOUND_STMT of
4128 the statement and that value. */
4129 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4132 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4133 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4134 isn't applicable for such builtins. ??? Do we want to handle enums? */
4135 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4136 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4138 built_in_function fncode;
4139 switch (modifycode)
4141 case PLUS_EXPR:
4142 case POINTER_PLUS_EXPR:
4143 fncode = (return_old_p
4144 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4145 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4146 break;
4147 case MINUS_EXPR:
4148 fncode = (return_old_p
4149 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4150 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4151 break;
4152 case BIT_AND_EXPR:
4153 fncode = (return_old_p
4154 ? BUILT_IN_ATOMIC_FETCH_AND_N
4155 : BUILT_IN_ATOMIC_AND_FETCH_N);
4156 break;
4157 case BIT_IOR_EXPR:
4158 fncode = (return_old_p
4159 ? BUILT_IN_ATOMIC_FETCH_OR_N
4160 : BUILT_IN_ATOMIC_OR_FETCH_N);
4161 break;
4162 case BIT_XOR_EXPR:
4163 fncode = (return_old_p
4164 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4165 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4166 break;
4167 default:
4168 goto cas_loop;
4171 /* We can only use "_1" through "_16" variants of the atomic fetch
4172 built-ins. */
4173 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4174 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4175 goto cas_loop;
4177 /* If this is a pointer type, we need to multiply by the size of
4178 the pointer target type. */
4179 if (POINTER_TYPE_P (lhs_type))
4181 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4182 /* ??? This would introduce -Wdiscarded-qualifiers
4183 warning: __atomic_fetch_* expect volatile void *
4184 type as the first argument. (Assignments between
4185 atomic and non-atomic objects are OK.) */
4186 || TYPE_RESTRICT (lhs_type))
4187 goto cas_loop;
4188 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4189 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4190 convert (ptrdiff_type_node, rhs),
4191 convert (ptrdiff_type_node, sz));
4194 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4195 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4196 fndecl = builtin_decl_explicit (fncode);
4197 params->quick_push (lhs_addr);
4198 params->quick_push (rhs);
4199 params->quick_push (seq_cst);
4200 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4202 newval = create_tmp_var_raw (nonatomic_lhs_type);
4203 TREE_ADDRESSABLE (newval) = 1;
4204 TREE_NO_WARNING (newval) = 1;
4205 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4206 NULL_TREE, NULL_TREE);
4207 SET_EXPR_LOCATION (rhs, loc);
4208 add_stmt (rhs);
4210 /* Finish the compound statement. */
4211 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4213 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4214 the statement and that value. */
4215 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4218 cas_loop:
4219 /* Create the variables and labels required for the op= form. */
4220 old = create_tmp_var_raw (nonatomic_lhs_type);
4221 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4222 TREE_ADDRESSABLE (old) = 1;
4223 TREE_NO_WARNING (old) = 1;
4225 newval = create_tmp_var_raw (nonatomic_lhs_type);
4226 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4227 TREE_ADDRESSABLE (newval) = 1;
4228 TREE_NO_WARNING (newval) = 1;
4230 loop_decl = create_artificial_label (loc);
4231 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4233 done_decl = create_artificial_label (loc);
4234 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4236 /* __atomic_load (addr, &old, SEQ_CST). */
4237 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4238 params->quick_push (lhs_addr);
4239 params->quick_push (old_addr);
4240 params->quick_push (seq_cst);
4241 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4242 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4243 NULL_TREE);
4244 add_stmt (old);
4245 params->truncate (0);
4247 /* Create the expressions for floating-point environment
4248 manipulation, if required. */
4249 bool need_fenv = (flag_trapping_math
4250 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4251 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4252 if (need_fenv)
4253 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4255 if (hold_call)
4256 add_stmt (hold_call);
4258 /* loop: */
4259 add_stmt (loop_label);
4261 /* newval = old + val; */
4262 if (rhs_type != rhs_semantic_type)
4263 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4264 rhs = build_binary_op (loc, modifycode, old, val, true);
4265 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4267 tree eptype = TREE_TYPE (rhs);
4268 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4269 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4271 else
4272 rhs = c_fully_fold (rhs, false, NULL);
4273 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4274 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4275 NULL_TREE, 0);
4276 if (rhs != error_mark_node)
4278 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4279 NULL_TREE);
4280 SET_EXPR_LOCATION (rhs, loc);
4281 add_stmt (rhs);
4284 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4285 goto done; */
4286 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4287 params->quick_push (lhs_addr);
4288 params->quick_push (old_addr);
4289 params->quick_push (newval_addr);
4290 params->quick_push (integer_zero_node);
4291 params->quick_push (seq_cst);
4292 params->quick_push (seq_cst);
4293 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4295 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4296 SET_EXPR_LOCATION (goto_stmt, loc);
4298 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4299 SET_EXPR_LOCATION (stmt, loc);
4300 add_stmt (stmt);
4302 if (clear_call)
4303 add_stmt (clear_call);
4305 /* goto loop; */
4306 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4307 SET_EXPR_LOCATION (goto_stmt, loc);
4308 add_stmt (goto_stmt);
4310 /* done: */
4311 add_stmt (done_label);
4313 if (update_call)
4314 add_stmt (update_call);
4316 /* Finish the compound statement. */
4317 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4319 /* NEWVAL is the value that was successfully stored, return a
4320 COMPOUND_EXPR of the statement and the appropriate value. */
4321 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4322 return_old_p ? old : newval);
4325 /* Construct and perhaps optimize a tree representation
4326 for a unary operation. CODE, a tree_code, specifies the operation
4327 and XARG is the operand.
4328 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4329 promotions (such as from short to int).
4330 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4331 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4332 to pointers in C99.
4334 LOCATION is the location of the operator. */
4336 tree
4337 build_unary_op (location_t location, enum tree_code code, tree xarg,
4338 bool noconvert)
4340 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4341 tree arg = xarg;
4342 tree argtype = NULL_TREE;
4343 enum tree_code typecode;
4344 tree val;
4345 tree ret = error_mark_node;
4346 tree eptype = NULL_TREE;
4347 const char *invalid_op_diag;
4348 bool int_operands;
4350 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4351 if (int_operands)
4352 arg = remove_c_maybe_const_expr (arg);
4354 if (code != ADDR_EXPR)
4355 arg = require_complete_type (location, arg);
4357 typecode = TREE_CODE (TREE_TYPE (arg));
4358 if (typecode == ERROR_MARK)
4359 return error_mark_node;
4360 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4361 typecode = INTEGER_TYPE;
4363 if ((invalid_op_diag
4364 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4366 error_at (location, invalid_op_diag);
4367 return error_mark_node;
4370 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4372 eptype = TREE_TYPE (arg);
4373 arg = TREE_OPERAND (arg, 0);
4376 switch (code)
4378 case CONVERT_EXPR:
4379 /* This is used for unary plus, because a CONVERT_EXPR
4380 is enough to prevent anybody from looking inside for
4381 associativity, but won't generate any code. */
4382 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4383 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4384 || gnu_vector_type_p (TREE_TYPE (arg))))
4386 error_at (location, "wrong type argument to unary plus");
4387 return error_mark_node;
4389 else if (!noconvert)
4390 arg = default_conversion (arg);
4391 arg = non_lvalue_loc (location, arg);
4392 break;
4394 case NEGATE_EXPR:
4395 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4396 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4397 || gnu_vector_type_p (TREE_TYPE (arg))))
4399 error_at (location, "wrong type argument to unary minus");
4400 return error_mark_node;
4402 else if (!noconvert)
4403 arg = default_conversion (arg);
4404 break;
4406 case BIT_NOT_EXPR:
4407 /* ~ works on integer types and non float vectors. */
4408 if (typecode == INTEGER_TYPE
4409 || (gnu_vector_type_p (TREE_TYPE (arg))
4410 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4412 tree e = arg;
4414 /* Warn if the expression has boolean value. */
4415 while (TREE_CODE (e) == COMPOUND_EXPR)
4416 e = TREE_OPERAND (e, 1);
4418 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4419 || truth_value_p (TREE_CODE (e))))
4421 auto_diagnostic_group d;
4422 if (warning_at (location, OPT_Wbool_operation,
4423 "%<~%> on a boolean expression"))
4425 gcc_rich_location richloc (location);
4426 richloc.add_fixit_insert_before (location, "!");
4427 inform (&richloc, "did you mean to use logical not?");
4430 if (!noconvert)
4431 arg = default_conversion (arg);
4433 else if (typecode == COMPLEX_TYPE)
4435 code = CONJ_EXPR;
4436 pedwarn (location, OPT_Wpedantic,
4437 "ISO C does not support %<~%> for complex conjugation");
4438 if (!noconvert)
4439 arg = default_conversion (arg);
4441 else
4443 error_at (location, "wrong type argument to bit-complement");
4444 return error_mark_node;
4446 break;
4448 case ABS_EXPR:
4449 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4451 error_at (location, "wrong type argument to abs");
4452 return error_mark_node;
4454 else if (!noconvert)
4455 arg = default_conversion (arg);
4456 break;
4458 case ABSU_EXPR:
4459 if (!(typecode == INTEGER_TYPE))
4461 error_at (location, "wrong type argument to absu");
4462 return error_mark_node;
4464 else if (!noconvert)
4465 arg = default_conversion (arg);
4466 break;
4468 case CONJ_EXPR:
4469 /* Conjugating a real value is a no-op, but allow it anyway. */
4470 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4471 || typecode == COMPLEX_TYPE))
4473 error_at (location, "wrong type argument to conjugation");
4474 return error_mark_node;
4476 else if (!noconvert)
4477 arg = default_conversion (arg);
4478 break;
4480 case TRUTH_NOT_EXPR:
4481 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4482 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4483 && typecode != COMPLEX_TYPE)
4485 error_at (location,
4486 "wrong type argument to unary exclamation mark");
4487 return error_mark_node;
4489 if (int_operands)
4491 arg = c_objc_common_truthvalue_conversion (location, xarg);
4492 arg = remove_c_maybe_const_expr (arg);
4494 else
4495 arg = c_objc_common_truthvalue_conversion (location, arg);
4496 ret = invert_truthvalue_loc (location, arg);
4497 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4498 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4499 location = EXPR_LOCATION (ret);
4500 goto return_build_unary_op;
4502 case REALPART_EXPR:
4503 case IMAGPART_EXPR:
4504 ret = build_real_imag_expr (location, code, arg);
4505 if (ret == error_mark_node)
4506 return error_mark_node;
4507 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4508 eptype = TREE_TYPE (eptype);
4509 goto return_build_unary_op;
4511 case PREINCREMENT_EXPR:
4512 case POSTINCREMENT_EXPR:
4513 case PREDECREMENT_EXPR:
4514 case POSTDECREMENT_EXPR:
4516 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4518 tree inner = build_unary_op (location, code,
4519 C_MAYBE_CONST_EXPR_EXPR (arg),
4520 noconvert);
4521 if (inner == error_mark_node)
4522 return error_mark_node;
4523 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4524 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4525 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4526 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4527 goto return_build_unary_op;
4530 /* Complain about anything that is not a true lvalue. In
4531 Objective-C, skip this check for property_refs. */
4532 if (!objc_is_property_ref (arg)
4533 && !lvalue_or_else (location,
4534 arg, ((code == PREINCREMENT_EXPR
4535 || code == POSTINCREMENT_EXPR)
4536 ? lv_increment
4537 : lv_decrement)))
4538 return error_mark_node;
4540 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4542 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4543 warning_at (location, OPT_Wc___compat,
4544 "increment of enumeration value is invalid in C++");
4545 else
4546 warning_at (location, OPT_Wc___compat,
4547 "decrement of enumeration value is invalid in C++");
4550 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4552 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4553 warning_at (location, OPT_Wbool_operation,
4554 "increment of a boolean expression");
4555 else
4556 warning_at (location, OPT_Wbool_operation,
4557 "decrement of a boolean expression");
4560 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4561 arg = c_fully_fold (arg, false, NULL, true);
4563 bool atomic_op;
4564 atomic_op = really_atomic_lvalue (arg);
4566 /* Increment or decrement the real part of the value,
4567 and don't change the imaginary part. */
4568 if (typecode == COMPLEX_TYPE)
4570 tree real, imag;
4572 pedwarn (location, OPT_Wpedantic,
4573 "ISO C does not support %<++%> and %<--%> on complex types");
4575 if (!atomic_op)
4577 arg = stabilize_reference (arg);
4578 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4579 true);
4580 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4581 true);
4582 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4583 if (real == error_mark_node || imag == error_mark_node)
4584 return error_mark_node;
4585 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4586 real, imag);
4587 goto return_build_unary_op;
4591 /* Report invalid types. */
4593 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4594 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4595 && typecode != COMPLEX_TYPE
4596 && !gnu_vector_type_p (TREE_TYPE (arg)))
4598 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4599 error_at (location, "wrong type argument to increment");
4600 else
4601 error_at (location, "wrong type argument to decrement");
4603 return error_mark_node;
4607 tree inc;
4609 argtype = TREE_TYPE (arg);
4611 /* Compute the increment. */
4613 if (typecode == POINTER_TYPE)
4615 /* If pointer target is an incomplete type,
4616 we just cannot know how to do the arithmetic. */
4617 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4619 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4620 error_at (location,
4621 "increment of pointer to an incomplete type %qT",
4622 TREE_TYPE (argtype));
4623 else
4624 error_at (location,
4625 "decrement of pointer to an incomplete type %qT",
4626 TREE_TYPE (argtype));
4628 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4629 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4631 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4632 pedwarn (location, OPT_Wpointer_arith,
4633 "wrong type argument to increment");
4634 else
4635 pedwarn (location, OPT_Wpointer_arith,
4636 "wrong type argument to decrement");
4638 else
4639 verify_type_context (location, TCTX_POINTER_ARITH,
4640 TREE_TYPE (argtype));
4642 inc = c_size_in_bytes (TREE_TYPE (argtype));
4643 inc = convert_to_ptrofftype_loc (location, inc);
4645 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4647 /* For signed fract types, we invert ++ to -- or
4648 -- to ++, and change inc from 1 to -1, because
4649 it is not possible to represent 1 in signed fract constants.
4650 For unsigned fract types, the result always overflows and
4651 we get an undefined (original) or the maximum value. */
4652 if (code == PREINCREMENT_EXPR)
4653 code = PREDECREMENT_EXPR;
4654 else if (code == PREDECREMENT_EXPR)
4655 code = PREINCREMENT_EXPR;
4656 else if (code == POSTINCREMENT_EXPR)
4657 code = POSTDECREMENT_EXPR;
4658 else /* code == POSTDECREMENT_EXPR */
4659 code = POSTINCREMENT_EXPR;
4661 inc = integer_minus_one_node;
4662 inc = convert (argtype, inc);
4664 else
4666 inc = VECTOR_TYPE_P (argtype)
4667 ? build_one_cst (argtype)
4668 : integer_one_node;
4669 inc = convert (argtype, inc);
4672 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4673 need to ask Objective-C to build the increment or decrement
4674 expression for it. */
4675 if (objc_is_property_ref (arg))
4676 return objc_build_incr_expr_for_property_ref (location, code,
4677 arg, inc);
4679 /* Report a read-only lvalue. */
4680 if (TYPE_READONLY (argtype))
4682 readonly_error (location, arg,
4683 ((code == PREINCREMENT_EXPR
4684 || code == POSTINCREMENT_EXPR)
4685 ? lv_increment : lv_decrement));
4686 return error_mark_node;
4688 else if (TREE_READONLY (arg))
4689 readonly_warning (arg,
4690 ((code == PREINCREMENT_EXPR
4691 || code == POSTINCREMENT_EXPR)
4692 ? lv_increment : lv_decrement));
4694 /* If the argument is atomic, use the special code sequences for
4695 atomic compound assignment. */
4696 if (atomic_op)
4698 arg = stabilize_reference (arg);
4699 ret = build_atomic_assign (location, arg,
4700 ((code == PREINCREMENT_EXPR
4701 || code == POSTINCREMENT_EXPR)
4702 ? PLUS_EXPR
4703 : MINUS_EXPR),
4704 (FRACT_MODE_P (TYPE_MODE (argtype))
4705 ? inc
4706 : integer_one_node),
4707 (code == POSTINCREMENT_EXPR
4708 || code == POSTDECREMENT_EXPR));
4709 goto return_build_unary_op;
4712 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4713 val = boolean_increment (code, arg);
4714 else
4715 val = build2 (code, TREE_TYPE (arg), arg, inc);
4716 TREE_SIDE_EFFECTS (val) = 1;
4717 if (TREE_CODE (val) != code)
4718 TREE_NO_WARNING (val) = 1;
4719 ret = val;
4720 goto return_build_unary_op;
4723 case ADDR_EXPR:
4724 /* Note that this operation never does default_conversion. */
4726 /* The operand of unary '&' must be an lvalue (which excludes
4727 expressions of type void), or, in C99, the result of a [] or
4728 unary '*' operator. */
4729 if (VOID_TYPE_P (TREE_TYPE (arg))
4730 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4731 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4732 pedwarn (location, 0, "taking address of expression of type %<void%>");
4734 /* Let &* cancel out to simplify resulting code. */
4735 if (INDIRECT_REF_P (arg))
4737 /* Don't let this be an lvalue. */
4738 if (lvalue_p (TREE_OPERAND (arg, 0)))
4739 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4740 ret = TREE_OPERAND (arg, 0);
4741 goto return_build_unary_op;
4744 /* Anything not already handled and not a true memory reference
4745 or a non-lvalue array is an error. */
4746 if (typecode != FUNCTION_TYPE && !noconvert
4747 && !lvalue_or_else (location, arg, lv_addressof))
4748 return error_mark_node;
4750 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4751 folding later. */
4752 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4754 tree inner = build_unary_op (location, code,
4755 C_MAYBE_CONST_EXPR_EXPR (arg),
4756 noconvert);
4757 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4758 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4759 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4760 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4761 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4762 goto return_build_unary_op;
4765 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4766 argtype = TREE_TYPE (arg);
4768 /* If the lvalue is const or volatile, merge that into the type
4769 to which the address will point. This is only needed
4770 for function types. */
4771 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4772 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4773 && TREE_CODE (argtype) == FUNCTION_TYPE)
4775 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4776 int quals = orig_quals;
4778 if (TREE_READONLY (arg))
4779 quals |= TYPE_QUAL_CONST;
4780 if (TREE_THIS_VOLATILE (arg))
4781 quals |= TYPE_QUAL_VOLATILE;
4783 argtype = c_build_qualified_type (argtype, quals);
4786 switch (TREE_CODE (arg))
4788 case COMPONENT_REF:
4789 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4791 error_at (location, "cannot take address of bit-field %qD",
4792 TREE_OPERAND (arg, 1));
4793 return error_mark_node;
4796 /* fall through */
4798 case ARRAY_REF:
4799 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4801 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4802 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4804 error_at (location, "cannot take address of scalar with "
4805 "reverse storage order");
4806 return error_mark_node;
4809 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4810 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4811 warning_at (location, OPT_Wscalar_storage_order,
4812 "address of array with reverse scalar storage "
4813 "order requested");
4816 default:
4817 break;
4820 if (!c_mark_addressable (arg))
4821 return error_mark_node;
4823 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4824 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4826 argtype = build_pointer_type (argtype);
4828 /* ??? Cope with user tricks that amount to offsetof. Delete this
4829 when we have proper support for integer constant expressions. */
4830 val = get_base_address (arg);
4831 if (val && INDIRECT_REF_P (val)
4832 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4834 ret = fold_offsetof (arg, argtype);
4835 goto return_build_unary_op;
4838 val = build1 (ADDR_EXPR, argtype, arg);
4840 ret = val;
4841 goto return_build_unary_op;
4843 default:
4844 gcc_unreachable ();
4847 if (argtype == NULL_TREE)
4848 argtype = TREE_TYPE (arg);
4849 if (TREE_CODE (arg) == INTEGER_CST)
4850 ret = (require_constant_value
4851 ? fold_build1_initializer_loc (location, code, argtype, arg)
4852 : fold_build1_loc (location, code, argtype, arg));
4853 else
4854 ret = build1 (code, argtype, arg);
4855 return_build_unary_op:
4856 gcc_assert (ret != error_mark_node);
4857 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4858 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4859 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4860 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4861 ret = note_integer_operands (ret);
4862 if (eptype)
4863 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4864 protected_set_expr_location (ret, location);
4865 return ret;
4868 /* Return nonzero if REF is an lvalue valid for this language.
4869 Lvalues can be assigned, unless their type has TYPE_READONLY.
4870 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4872 bool
4873 lvalue_p (const_tree ref)
4875 const enum tree_code code = TREE_CODE (ref);
4877 switch (code)
4879 case REALPART_EXPR:
4880 case IMAGPART_EXPR:
4881 case COMPONENT_REF:
4882 return lvalue_p (TREE_OPERAND (ref, 0));
4884 case C_MAYBE_CONST_EXPR:
4885 return lvalue_p (TREE_OPERAND (ref, 1));
4887 case COMPOUND_LITERAL_EXPR:
4888 case STRING_CST:
4889 return true;
4891 case INDIRECT_REF:
4892 case ARRAY_REF:
4893 case VAR_DECL:
4894 case PARM_DECL:
4895 case RESULT_DECL:
4896 case ERROR_MARK:
4897 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4898 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4900 case BIND_EXPR:
4901 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4903 default:
4904 return false;
4908 /* Give a warning for storing in something that is read-only in GCC
4909 terms but not const in ISO C terms. */
4911 static void
4912 readonly_warning (tree arg, enum lvalue_use use)
4914 switch (use)
4916 case lv_assign:
4917 warning (0, "assignment of read-only location %qE", arg);
4918 break;
4919 case lv_increment:
4920 warning (0, "increment of read-only location %qE", arg);
4921 break;
4922 case lv_decrement:
4923 warning (0, "decrement of read-only location %qE", arg);
4924 break;
4925 default:
4926 gcc_unreachable ();
4928 return;
4932 /* Return nonzero if REF is an lvalue valid for this language;
4933 otherwise, print an error message and return zero. USE says
4934 how the lvalue is being used and so selects the error message.
4935 LOCATION is the location at which any error should be reported. */
4937 static int
4938 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4940 int win = lvalue_p (ref);
4942 if (!win)
4943 lvalue_error (loc, use);
4945 return win;
4948 /* Mark EXP saying that we need to be able to take the
4949 address of it; it should not be allocated in a register.
4950 Returns true if successful. ARRAY_REF_P is true if this
4951 is for ARRAY_REF construction - in that case we don't want
4952 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4953 it is fine to use ARRAY_REFs for vector subscripts on vector
4954 register variables. */
4956 bool
4957 c_mark_addressable (tree exp, bool array_ref_p)
4959 tree x = exp;
4961 while (1)
4962 switch (TREE_CODE (x))
4964 case VIEW_CONVERT_EXPR:
4965 if (array_ref_p
4966 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4967 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4968 return true;
4969 /* FALLTHRU */
4970 case COMPONENT_REF:
4971 case ADDR_EXPR:
4972 case ARRAY_REF:
4973 case REALPART_EXPR:
4974 case IMAGPART_EXPR:
4975 x = TREE_OPERAND (x, 0);
4976 break;
4978 case COMPOUND_LITERAL_EXPR:
4979 TREE_ADDRESSABLE (x) = 1;
4980 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
4981 return true;
4983 case CONSTRUCTOR:
4984 TREE_ADDRESSABLE (x) = 1;
4985 return true;
4987 case VAR_DECL:
4988 case CONST_DECL:
4989 case PARM_DECL:
4990 case RESULT_DECL:
4991 if (C_DECL_REGISTER (x)
4992 && DECL_NONLOCAL (x))
4994 if (TREE_PUBLIC (x) || is_global_var (x))
4996 error
4997 ("global register variable %qD used in nested function", x);
4998 return false;
5000 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5002 else if (C_DECL_REGISTER (x))
5004 if (TREE_PUBLIC (x) || is_global_var (x))
5005 error ("address of global register variable %qD requested", x);
5006 else
5007 error ("address of register variable %qD requested", x);
5008 return false;
5011 /* FALLTHRU */
5012 case FUNCTION_DECL:
5013 TREE_ADDRESSABLE (x) = 1;
5014 /* FALLTHRU */
5015 default:
5016 return true;
5020 /* Convert EXPR to TYPE, warning about conversion problems with
5021 constants. SEMANTIC_TYPE is the type this conversion would use
5022 without excess precision. If SEMANTIC_TYPE is NULL, this function
5023 is equivalent to convert_and_check. This function is a wrapper that
5024 handles conversions that may be different than
5025 the usual ones because of excess precision. */
5027 static tree
5028 ep_convert_and_check (location_t loc, tree type, tree expr,
5029 tree semantic_type)
5031 if (TREE_TYPE (expr) == type)
5032 return expr;
5034 /* For C11, integer conversions may have results with excess
5035 precision. */
5036 if (flag_isoc11 || !semantic_type)
5037 return convert_and_check (loc, type, expr);
5039 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5040 && TREE_TYPE (expr) != semantic_type)
5042 /* For integers, we need to check the real conversion, not
5043 the conversion to the excess precision type. */
5044 expr = convert_and_check (loc, semantic_type, expr);
5046 /* Result type is the excess precision type, which should be
5047 large enough, so do not check. */
5048 return convert (type, expr);
5051 /* If EXPR refers to a built-in declared without a prototype returns
5052 the actual type of the built-in and, if non-null, set *BLTIN to
5053 a pointer to the built-in. Otherwise return the type of EXPR
5054 and clear *BLTIN if non-null. */
5056 static tree
5057 type_or_builtin_type (tree expr, tree *bltin = NULL)
5059 tree dummy;
5060 if (!bltin)
5061 bltin = &dummy;
5063 *bltin = NULL_TREE;
5065 tree type = TREE_TYPE (expr);
5066 if (TREE_CODE (expr) != ADDR_EXPR)
5067 return type;
5069 tree oper = TREE_OPERAND (expr, 0);
5070 if (!DECL_P (oper)
5071 || TREE_CODE (oper) != FUNCTION_DECL
5072 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5073 return type;
5075 built_in_function code = DECL_FUNCTION_CODE (oper);
5076 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5077 return type;
5079 if ((*bltin = builtin_decl_implicit (code)))
5080 type = build_pointer_type (TREE_TYPE (*bltin));
5082 return type;
5085 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5086 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5087 if folded to an integer constant then the unselected half may
5088 contain arbitrary operations not normally permitted in constant
5089 expressions. Set the location of the expression to LOC. */
5091 tree
5092 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5093 tree op1, tree op1_original_type, location_t op1_loc,
5094 tree op2, tree op2_original_type, location_t op2_loc)
5096 tree type1;
5097 tree type2;
5098 enum tree_code code1;
5099 enum tree_code code2;
5100 tree result_type = NULL;
5101 tree semantic_result_type = NULL;
5102 tree orig_op1 = op1, orig_op2 = op2;
5103 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5104 bool ifexp_int_operands;
5105 tree ret;
5107 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5108 if (op1_int_operands)
5109 op1 = remove_c_maybe_const_expr (op1);
5110 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5111 if (op2_int_operands)
5112 op2 = remove_c_maybe_const_expr (op2);
5113 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5114 if (ifexp_int_operands)
5115 ifexp = remove_c_maybe_const_expr (ifexp);
5117 /* Promote both alternatives. */
5119 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5120 op1 = default_conversion (op1);
5121 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5122 op2 = default_conversion (op2);
5124 if (TREE_CODE (ifexp) == ERROR_MARK
5125 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5126 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5127 return error_mark_node;
5129 tree bltin1 = NULL_TREE;
5130 tree bltin2 = NULL_TREE;
5131 type1 = type_or_builtin_type (op1, &bltin1);
5132 code1 = TREE_CODE (type1);
5133 type2 = type_or_builtin_type (op2, &bltin2);
5134 code2 = TREE_CODE (type2);
5136 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5137 return error_mark_node;
5139 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5140 return error_mark_node;
5142 /* C90 does not permit non-lvalue arrays in conditional expressions.
5143 In C99 they will be pointers by now. */
5144 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5146 error_at (colon_loc, "non-lvalue array in conditional expression");
5147 return error_mark_node;
5150 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5151 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5152 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5153 || code1 == COMPLEX_TYPE)
5154 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5155 || code2 == COMPLEX_TYPE))
5157 semantic_result_type = c_common_type (type1, type2);
5158 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5160 op1 = TREE_OPERAND (op1, 0);
5161 type1 = TREE_TYPE (op1);
5162 gcc_assert (TREE_CODE (type1) == code1);
5164 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5166 op2 = TREE_OPERAND (op2, 0);
5167 type2 = TREE_TYPE (op2);
5168 gcc_assert (TREE_CODE (type2) == code2);
5172 if (warn_cxx_compat)
5174 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5175 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5177 if (TREE_CODE (t1) == ENUMERAL_TYPE
5178 && TREE_CODE (t2) == ENUMERAL_TYPE
5179 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5180 warning_at (colon_loc, OPT_Wc___compat,
5181 ("different enum types in conditional is "
5182 "invalid in C++: %qT vs %qT"),
5183 t1, t2);
5186 /* Quickly detect the usual case where op1 and op2 have the same type
5187 after promotion. */
5188 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5190 if (type1 == type2)
5191 result_type = type1;
5192 else
5193 result_type = TYPE_MAIN_VARIANT (type1);
5195 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5196 || code1 == COMPLEX_TYPE)
5197 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5198 || code2 == COMPLEX_TYPE))
5200 /* In C11, a conditional expression between a floating-point
5201 type and an integer type should convert the integer type to
5202 the evaluation format of the floating-point type, with
5203 possible excess precision. */
5204 tree eptype1 = type1;
5205 tree eptype2 = type2;
5206 if (flag_isoc11)
5208 tree eptype;
5209 if (ANY_INTEGRAL_TYPE_P (type1)
5210 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5212 eptype2 = eptype;
5213 if (!semantic_result_type)
5214 semantic_result_type = c_common_type (type1, type2);
5216 else if (ANY_INTEGRAL_TYPE_P (type2)
5217 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5219 eptype1 = eptype;
5220 if (!semantic_result_type)
5221 semantic_result_type = c_common_type (type1, type2);
5224 result_type = c_common_type (eptype1, eptype2);
5225 if (result_type == error_mark_node)
5226 return error_mark_node;
5227 do_warn_double_promotion (result_type, type1, type2,
5228 "implicit conversion from %qT to %qT to "
5229 "match other result of conditional",
5230 colon_loc);
5232 /* If -Wsign-compare, warn here if type1 and type2 have
5233 different signedness. We'll promote the signed to unsigned
5234 and later code won't know it used to be different.
5235 Do this check on the original types, so that explicit casts
5236 will be considered, but default promotions won't. */
5237 if (c_inhibit_evaluation_warnings == 0)
5239 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5240 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5242 if (unsigned_op1 ^ unsigned_op2)
5244 bool ovf;
5246 /* Do not warn if the result type is signed, since the
5247 signed type will only be chosen if it can represent
5248 all the values of the unsigned type. */
5249 if (!TYPE_UNSIGNED (result_type))
5250 /* OK */;
5251 else
5253 bool op1_maybe_const = true;
5254 bool op2_maybe_const = true;
5256 /* Do not warn if the signed quantity is an
5257 unsuffixed integer literal (or some static
5258 constant expression involving such literals) and
5259 it is non-negative. This warning requires the
5260 operands to be folded for best results, so do
5261 that folding in this case even without
5262 warn_sign_compare to avoid warning options
5263 possibly affecting code generation. */
5264 c_inhibit_evaluation_warnings
5265 += (ifexp == truthvalue_false_node);
5266 op1 = c_fully_fold (op1, require_constant_value,
5267 &op1_maybe_const);
5268 c_inhibit_evaluation_warnings
5269 -= (ifexp == truthvalue_false_node);
5271 c_inhibit_evaluation_warnings
5272 += (ifexp == truthvalue_true_node);
5273 op2 = c_fully_fold (op2, require_constant_value,
5274 &op2_maybe_const);
5275 c_inhibit_evaluation_warnings
5276 -= (ifexp == truthvalue_true_node);
5278 if (warn_sign_compare)
5280 if ((unsigned_op2
5281 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5282 || (unsigned_op1
5283 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5284 /* OK */;
5285 else if (unsigned_op2)
5286 warning_at (op1_loc, OPT_Wsign_compare,
5287 "operand of %<?:%> changes signedness from "
5288 "%qT to %qT due to unsignedness of other "
5289 "operand", TREE_TYPE (orig_op1),
5290 TREE_TYPE (orig_op2));
5291 else
5292 warning_at (op2_loc, OPT_Wsign_compare,
5293 "operand of %<?:%> changes signedness from "
5294 "%qT to %qT due to unsignedness of other "
5295 "operand", TREE_TYPE (orig_op2),
5296 TREE_TYPE (orig_op1));
5298 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5299 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5300 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5301 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5306 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5308 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5309 pedwarn (colon_loc, OPT_Wpedantic,
5310 "ISO C forbids conditional expr with only one void side");
5311 result_type = void_type_node;
5313 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5315 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5316 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5317 addr_space_t as_common;
5319 if (comp_target_types (colon_loc, type1, type2))
5320 result_type = common_pointer_type (type1, type2);
5321 else if (null_pointer_constant_p (orig_op1))
5322 result_type = type2;
5323 else if (null_pointer_constant_p (orig_op2))
5324 result_type = type1;
5325 else if (!addr_space_superset (as1, as2, &as_common))
5327 error_at (colon_loc, "pointers to disjoint address spaces "
5328 "used in conditional expression");
5329 return error_mark_node;
5331 else if (VOID_TYPE_P (TREE_TYPE (type1))
5332 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5334 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5335 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5336 & ~TYPE_QUALS (TREE_TYPE (type1))))
5337 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5338 "pointer to array loses qualifier "
5339 "in conditional expression");
5341 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5342 pedwarn (colon_loc, OPT_Wpedantic,
5343 "ISO C forbids conditional expr between "
5344 "%<void *%> and function pointer");
5345 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5346 TREE_TYPE (type2)));
5348 else if (VOID_TYPE_P (TREE_TYPE (type2))
5349 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5351 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5352 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5353 & ~TYPE_QUALS (TREE_TYPE (type2))))
5354 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5355 "pointer to array loses qualifier "
5356 "in conditional expression");
5358 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5359 pedwarn (colon_loc, OPT_Wpedantic,
5360 "ISO C forbids conditional expr between "
5361 "%<void *%> and function pointer");
5362 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5363 TREE_TYPE (type1)));
5365 /* Objective-C pointer comparisons are a bit more lenient. */
5366 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5367 result_type = objc_common_type (type1, type2);
5368 else
5370 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5371 if (bltin1 && bltin2)
5372 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5373 "pointer type mismatch between %qT and %qT "
5374 "of %qD and %qD in conditional expression",
5375 type1, type2, bltin1, bltin2);
5376 else
5377 pedwarn (colon_loc, 0,
5378 "pointer type mismatch in conditional expression");
5379 result_type = build_pointer_type
5380 (build_qualified_type (void_type_node, qual));
5383 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5385 if (!null_pointer_constant_p (orig_op2))
5386 pedwarn (colon_loc, 0,
5387 "pointer/integer type mismatch in conditional expression");
5388 else
5390 op2 = null_pointer_node;
5392 result_type = type1;
5394 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5396 if (!null_pointer_constant_p (orig_op1))
5397 pedwarn (colon_loc, 0,
5398 "pointer/integer type mismatch in conditional expression");
5399 else
5401 op1 = null_pointer_node;
5403 result_type = type2;
5406 if (!result_type)
5408 if (flag_cond_mismatch)
5409 result_type = void_type_node;
5410 else
5412 error_at (colon_loc, "type mismatch in conditional expression");
5413 return error_mark_node;
5417 /* Merge const and volatile flags of the incoming types. */
5418 result_type
5419 = build_type_variant (result_type,
5420 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5421 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5423 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5424 semantic_result_type);
5425 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5426 semantic_result_type);
5428 if (ifexp_bcp && ifexp == truthvalue_true_node)
5430 op2_int_operands = true;
5431 op1 = c_fully_fold (op1, require_constant_value, NULL);
5433 if (ifexp_bcp && ifexp == truthvalue_false_node)
5435 op1_int_operands = true;
5436 op2 = c_fully_fold (op2, require_constant_value, NULL);
5438 int_const = int_operands = (ifexp_int_operands
5439 && op1_int_operands
5440 && op2_int_operands);
5441 if (int_operands)
5443 int_const = ((ifexp == truthvalue_true_node
5444 && TREE_CODE (orig_op1) == INTEGER_CST
5445 && !TREE_OVERFLOW (orig_op1))
5446 || (ifexp == truthvalue_false_node
5447 && TREE_CODE (orig_op2) == INTEGER_CST
5448 && !TREE_OVERFLOW (orig_op2)));
5451 /* Need to convert condition operand into a vector mask. */
5452 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5454 tree vectype = TREE_TYPE (ifexp);
5455 tree elem_type = TREE_TYPE (vectype);
5456 tree zero = build_int_cst (elem_type, 0);
5457 tree zero_vec = build_vector_from_val (vectype, zero);
5458 tree cmp_type = truth_type_for (vectype);
5459 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5462 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5463 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5464 else
5466 if (int_operands)
5468 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5469 nested inside of the expression. */
5470 op1 = c_fully_fold (op1, false, NULL);
5471 op2 = c_fully_fold (op2, false, NULL);
5473 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5474 if (int_operands)
5475 ret = note_integer_operands (ret);
5477 if (semantic_result_type)
5478 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5480 protected_set_expr_location (ret, colon_loc);
5482 /* If the OP1 and OP2 are the same and don't have side-effects,
5483 warn here, because the COND_EXPR will be turned into OP1. */
5484 if (warn_duplicated_branches
5485 && TREE_CODE (ret) == COND_EXPR
5486 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5487 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5488 "this condition has identical branches");
5490 return ret;
5493 /* Return a compound expression that performs two expressions and
5494 returns the value of the second of them.
5496 LOC is the location of the COMPOUND_EXPR. */
5498 tree
5499 build_compound_expr (location_t loc, tree expr1, tree expr2)
5501 bool expr1_int_operands, expr2_int_operands;
5502 tree eptype = NULL_TREE;
5503 tree ret;
5505 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5506 if (expr1_int_operands)
5507 expr1 = remove_c_maybe_const_expr (expr1);
5508 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5509 if (expr2_int_operands)
5510 expr2 = remove_c_maybe_const_expr (expr2);
5512 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5513 expr1 = TREE_OPERAND (expr1, 0);
5514 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5516 eptype = TREE_TYPE (expr2);
5517 expr2 = TREE_OPERAND (expr2, 0);
5520 if (!TREE_SIDE_EFFECTS (expr1))
5522 /* The left-hand operand of a comma expression is like an expression
5523 statement: with -Wunused, we should warn if it doesn't have
5524 any side-effects, unless it was explicitly cast to (void). */
5525 if (warn_unused_value)
5527 if (VOID_TYPE_P (TREE_TYPE (expr1))
5528 && CONVERT_EXPR_P (expr1))
5529 ; /* (void) a, b */
5530 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5531 && TREE_CODE (expr1) == COMPOUND_EXPR
5532 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5533 ; /* (void) a, (void) b, c */
5534 else
5535 warning_at (loc, OPT_Wunused_value,
5536 "left-hand operand of comma expression has no effect");
5539 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5540 && warn_unused_value)
5542 tree r = expr1;
5543 location_t cloc = loc;
5544 while (TREE_CODE (r) == COMPOUND_EXPR)
5546 if (EXPR_HAS_LOCATION (r))
5547 cloc = EXPR_LOCATION (r);
5548 r = TREE_OPERAND (r, 1);
5550 if (!TREE_SIDE_EFFECTS (r)
5551 && !VOID_TYPE_P (TREE_TYPE (r))
5552 && !CONVERT_EXPR_P (r))
5553 warning_at (cloc, OPT_Wunused_value,
5554 "right-hand operand of comma expression has no effect");
5557 /* With -Wunused, we should also warn if the left-hand operand does have
5558 side-effects, but computes a value which is not used. For example, in
5559 `foo() + bar(), baz()' the result of the `+' operator is not used,
5560 so we should issue a warning. */
5561 else if (warn_unused_value)
5562 warn_if_unused_value (expr1, loc);
5564 if (expr2 == error_mark_node)
5565 return error_mark_node;
5567 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5569 if (flag_isoc99
5570 && expr1_int_operands
5571 && expr2_int_operands)
5572 ret = note_integer_operands (ret);
5574 if (eptype)
5575 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5577 protected_set_expr_location (ret, loc);
5578 return ret;
5581 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5582 which we are casting. OTYPE is the type of the expression being
5583 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5584 of the cast. -Wcast-qual appeared on the command line. Named
5585 address space qualifiers are not handled here, because they result
5586 in different warnings. */
5588 static void
5589 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5591 tree in_type = type;
5592 tree in_otype = otype;
5593 int added = 0;
5594 int discarded = 0;
5595 bool is_const;
5597 /* Check that the qualifiers on IN_TYPE are a superset of the
5598 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5599 nodes is uninteresting and we stop as soon as we hit a
5600 non-POINTER_TYPE node on either type. */
5603 in_otype = TREE_TYPE (in_otype);
5604 in_type = TREE_TYPE (in_type);
5606 /* GNU C allows cv-qualified function types. 'const' means the
5607 function is very pure, 'volatile' means it can't return. We
5608 need to warn when such qualifiers are added, not when they're
5609 taken away. */
5610 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5611 && TREE_CODE (in_type) == FUNCTION_TYPE)
5612 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5613 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5614 else
5615 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5616 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5618 while (TREE_CODE (in_type) == POINTER_TYPE
5619 && TREE_CODE (in_otype) == POINTER_TYPE);
5621 if (added)
5622 warning_at (loc, OPT_Wcast_qual,
5623 "cast adds %q#v qualifier to function type", added);
5625 if (discarded)
5626 /* There are qualifiers present in IN_OTYPE that are not present
5627 in IN_TYPE. */
5628 warning_at (loc, OPT_Wcast_qual,
5629 "cast discards %qv qualifier from pointer target type",
5630 discarded);
5632 if (added || discarded)
5633 return;
5635 /* A cast from **T to const **T is unsafe, because it can cause a
5636 const value to be changed with no additional warning. We only
5637 issue this warning if T is the same on both sides, and we only
5638 issue the warning if there are the same number of pointers on
5639 both sides, as otherwise the cast is clearly unsafe anyhow. A
5640 cast is unsafe when a qualifier is added at one level and const
5641 is not present at all outer levels.
5643 To issue this warning, we check at each level whether the cast
5644 adds new qualifiers not already seen. We don't need to special
5645 case function types, as they won't have the same
5646 TYPE_MAIN_VARIANT. */
5648 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5649 return;
5650 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5651 return;
5653 in_type = type;
5654 in_otype = otype;
5655 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5658 in_type = TREE_TYPE (in_type);
5659 in_otype = TREE_TYPE (in_otype);
5660 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5661 && !is_const)
5663 warning_at (loc, OPT_Wcast_qual,
5664 "to be safe all intermediate pointers in cast from "
5665 "%qT to %qT must be %<const%> qualified",
5666 otype, type);
5667 break;
5669 if (is_const)
5670 is_const = TYPE_READONLY (in_type);
5672 while (TREE_CODE (in_type) == POINTER_TYPE);
5675 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5677 static bool
5678 c_safe_arg_type_equiv_p (tree t1, tree t2)
5680 t1 = TYPE_MAIN_VARIANT (t1);
5681 t2 = TYPE_MAIN_VARIANT (t2);
5683 if (TREE_CODE (t1) == POINTER_TYPE
5684 && TREE_CODE (t2) == POINTER_TYPE)
5685 return true;
5687 /* The signedness of the parameter matters only when an integral
5688 type smaller than int is promoted to int, otherwise only the
5689 precision of the parameter matters.
5690 This check should make sure that the callee does not see
5691 undefined values in argument registers. */
5692 if (INTEGRAL_TYPE_P (t1)
5693 && INTEGRAL_TYPE_P (t2)
5694 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5695 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5696 || !targetm.calls.promote_prototypes (NULL_TREE)
5697 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5698 return true;
5700 return comptypes (t1, t2);
5703 /* Check if a type cast between two function types can be considered safe. */
5705 static bool
5706 c_safe_function_type_cast_p (tree t1, tree t2)
5708 if (TREE_TYPE (t1) == void_type_node &&
5709 TYPE_ARG_TYPES (t1) == void_list_node)
5710 return true;
5712 if (TREE_TYPE (t2) == void_type_node &&
5713 TYPE_ARG_TYPES (t2) == void_list_node)
5714 return true;
5716 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5717 return false;
5719 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5720 t1 && t2;
5721 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5722 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5723 return false;
5725 return true;
5728 /* Build an expression representing a cast to type TYPE of expression EXPR.
5729 LOC is the location of the cast-- typically the open paren of the cast. */
5731 tree
5732 build_c_cast (location_t loc, tree type, tree expr)
5734 tree value;
5736 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5738 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5739 expr = TREE_OPERAND (expr, 0);
5741 value = expr;
5742 if (int_operands)
5743 value = remove_c_maybe_const_expr (value);
5745 if (type == error_mark_node || expr == error_mark_node)
5746 return error_mark_node;
5748 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5749 only in <protocol> qualifications. But when constructing cast expressions,
5750 the protocols do matter and must be kept around. */
5751 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5752 return build1 (NOP_EXPR, type, expr);
5754 type = TYPE_MAIN_VARIANT (type);
5756 if (TREE_CODE (type) == ARRAY_TYPE)
5758 error_at (loc, "cast specifies array type");
5759 return error_mark_node;
5762 if (TREE_CODE (type) == FUNCTION_TYPE)
5764 error_at (loc, "cast specifies function type");
5765 return error_mark_node;
5768 if (!VOID_TYPE_P (type))
5770 value = require_complete_type (loc, value);
5771 if (value == error_mark_node)
5772 return error_mark_node;
5775 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5777 if (RECORD_OR_UNION_TYPE_P (type))
5778 pedwarn (loc, OPT_Wpedantic,
5779 "ISO C forbids casting nonscalar to the same type");
5781 /* Convert to remove any qualifiers from VALUE's type. */
5782 value = convert (type, value);
5784 else if (TREE_CODE (type) == UNION_TYPE)
5786 tree field;
5788 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5789 if (TREE_TYPE (field) != error_mark_node
5790 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5791 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5792 break;
5794 if (field)
5796 tree t;
5797 bool maybe_const = true;
5799 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5800 t = c_fully_fold (value, false, &maybe_const);
5801 t = build_constructor_single (type, field, t);
5802 if (!maybe_const)
5803 t = c_wrap_maybe_const (t, true);
5804 t = digest_init (loc, type, t,
5805 NULL_TREE, false, true, 0);
5806 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5807 return t;
5809 error_at (loc, "cast to union type from type not present in union");
5810 return error_mark_node;
5812 else
5814 tree otype, ovalue;
5816 if (type == void_type_node)
5818 tree t = build1 (CONVERT_EXPR, type, value);
5819 SET_EXPR_LOCATION (t, loc);
5820 return t;
5823 otype = TREE_TYPE (value);
5825 /* Optionally warn about potentially worrisome casts. */
5826 if (warn_cast_qual
5827 && TREE_CODE (type) == POINTER_TYPE
5828 && TREE_CODE (otype) == POINTER_TYPE)
5829 handle_warn_cast_qual (loc, type, otype);
5831 /* Warn about conversions between pointers to disjoint
5832 address spaces. */
5833 if (TREE_CODE (type) == POINTER_TYPE
5834 && TREE_CODE (otype) == POINTER_TYPE
5835 && !null_pointer_constant_p (value))
5837 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5838 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5839 addr_space_t as_common;
5841 if (!addr_space_superset (as_to, as_from, &as_common))
5843 if (ADDR_SPACE_GENERIC_P (as_from))
5844 warning_at (loc, 0, "cast to %s address space pointer "
5845 "from disjoint generic address space pointer",
5846 c_addr_space_name (as_to));
5848 else if (ADDR_SPACE_GENERIC_P (as_to))
5849 warning_at (loc, 0, "cast to generic address space pointer "
5850 "from disjoint %s address space pointer",
5851 c_addr_space_name (as_from));
5853 else
5854 warning_at (loc, 0, "cast to %s address space pointer "
5855 "from disjoint %s address space pointer",
5856 c_addr_space_name (as_to),
5857 c_addr_space_name (as_from));
5861 /* Warn about possible alignment problems. */
5862 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5863 && TREE_CODE (type) == POINTER_TYPE
5864 && TREE_CODE (otype) == POINTER_TYPE
5865 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5866 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5867 /* Don't warn about opaque types, where the actual alignment
5868 restriction is unknown. */
5869 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5870 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5871 && min_align_of_type (TREE_TYPE (type))
5872 > min_align_of_type (TREE_TYPE (otype)))
5873 warning_at (loc, OPT_Wcast_align,
5874 "cast increases required alignment of target type");
5876 if (TREE_CODE (type) == INTEGER_TYPE
5877 && TREE_CODE (otype) == POINTER_TYPE
5878 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5879 /* Unlike conversion of integers to pointers, where the
5880 warning is disabled for converting constants because
5881 of cases such as SIG_*, warn about converting constant
5882 pointers to integers. In some cases it may cause unwanted
5883 sign extension, and a warning is appropriate. */
5884 warning_at (loc, OPT_Wpointer_to_int_cast,
5885 "cast from pointer to integer of different size");
5887 if (TREE_CODE (value) == CALL_EXPR
5888 && TREE_CODE (type) != TREE_CODE (otype))
5889 warning_at (loc, OPT_Wbad_function_cast,
5890 "cast from function call of type %qT "
5891 "to non-matching type %qT", otype, type);
5893 if (TREE_CODE (type) == POINTER_TYPE
5894 && TREE_CODE (otype) == INTEGER_TYPE
5895 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5896 /* Don't warn about converting any constant. */
5897 && !TREE_CONSTANT (value))
5898 warning_at (loc,
5899 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5900 "of different size");
5902 if (warn_strict_aliasing <= 2)
5903 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5905 /* If pedantic, warn for conversions between function and object
5906 pointer types, except for converting a null pointer constant
5907 to function pointer type. */
5908 if (pedantic
5909 && TREE_CODE (type) == POINTER_TYPE
5910 && TREE_CODE (otype) == POINTER_TYPE
5911 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5912 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5913 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5914 "conversion of function pointer to object pointer type");
5916 if (pedantic
5917 && TREE_CODE (type) == POINTER_TYPE
5918 && TREE_CODE (otype) == POINTER_TYPE
5919 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5920 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5921 && !null_pointer_constant_p (value))
5922 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5923 "conversion of object pointer to function pointer type");
5925 if (TREE_CODE (type) == POINTER_TYPE
5926 && TREE_CODE (otype) == POINTER_TYPE
5927 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5928 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5929 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5930 TREE_TYPE (otype)))
5931 warning_at (loc, OPT_Wcast_function_type,
5932 "cast between incompatible function types"
5933 " from %qT to %qT", otype, type);
5935 ovalue = value;
5936 value = convert (type, value);
5938 /* Ignore any integer overflow caused by the cast. */
5939 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5941 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5943 if (!TREE_OVERFLOW (value))
5945 /* Avoid clobbering a shared constant. */
5946 value = copy_node (value);
5947 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5950 else if (TREE_OVERFLOW (value))
5951 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5952 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5956 /* Don't let a cast be an lvalue. */
5957 if (lvalue_p (value))
5958 value = non_lvalue_loc (loc, value);
5960 /* Don't allow the results of casting to floating-point or complex
5961 types be confused with actual constants, or casts involving
5962 integer and pointer types other than direct integer-to-integer
5963 and integer-to-pointer be confused with integer constant
5964 expressions and null pointer constants. */
5965 if (TREE_CODE (value) == REAL_CST
5966 || TREE_CODE (value) == COMPLEX_CST
5967 || (TREE_CODE (value) == INTEGER_CST
5968 && !((TREE_CODE (expr) == INTEGER_CST
5969 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5970 || TREE_CODE (expr) == REAL_CST
5971 || TREE_CODE (expr) == COMPLEX_CST)))
5972 value = build1 (NOP_EXPR, type, value);
5974 /* If the expression has integer operands and so can occur in an
5975 unevaluated part of an integer constant expression, ensure the
5976 return value reflects this. */
5977 if (int_operands
5978 && INTEGRAL_TYPE_P (type)
5979 && !EXPR_INT_CONST_OPERANDS (value))
5980 value = note_integer_operands (value);
5982 protected_set_expr_location (value, loc);
5983 return value;
5986 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5987 location of the open paren of the cast, or the position of the cast
5988 expr. */
5989 tree
5990 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5992 tree type;
5993 tree type_expr = NULL_TREE;
5994 bool type_expr_const = true;
5995 tree ret;
5996 int saved_wsp = warn_strict_prototypes;
5998 /* This avoids warnings about unprototyped casts on
5999 integers. E.g. "#define SIG_DFL (void(*)())0". */
6000 if (TREE_CODE (expr) == INTEGER_CST)
6001 warn_strict_prototypes = 0;
6002 type = groktypename (type_name, &type_expr, &type_expr_const);
6003 warn_strict_prototypes = saved_wsp;
6005 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6006 && reject_gcc_builtin (expr))
6007 return error_mark_node;
6009 ret = build_c_cast (loc, type, expr);
6010 if (type_expr)
6012 bool inner_expr_const = true;
6013 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6014 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6015 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6016 && inner_expr_const);
6017 SET_EXPR_LOCATION (ret, loc);
6020 if (!EXPR_HAS_LOCATION (ret))
6021 protected_set_expr_location (ret, loc);
6023 /* C++ does not permits types to be defined in a cast, but it
6024 allows references to incomplete types. */
6025 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6026 warning_at (loc, OPT_Wc___compat,
6027 "defining a type in a cast is invalid in C++");
6029 return ret;
6032 /* Build an assignment expression of lvalue LHS from value RHS.
6033 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6034 may differ from TREE_TYPE (LHS) for an enum bitfield.
6035 MODIFYCODE is the code for a binary operator that we use
6036 to combine the old value of LHS with RHS to get the new value.
6037 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6038 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6039 which may differ from TREE_TYPE (RHS) for an enum value.
6041 LOCATION is the location of the MODIFYCODE operator.
6042 RHS_LOC is the location of the RHS. */
6044 tree
6045 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6046 enum tree_code modifycode,
6047 location_t rhs_loc, tree rhs, tree rhs_origtype)
6049 tree result;
6050 tree newrhs;
6051 tree rhseval = NULL_TREE;
6052 tree lhstype = TREE_TYPE (lhs);
6053 tree olhstype = lhstype;
6054 bool npc;
6055 bool is_atomic_op;
6057 /* Types that aren't fully specified cannot be used in assignments. */
6058 lhs = require_complete_type (location, lhs);
6060 /* Avoid duplicate error messages from operands that had errors. */
6061 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6062 return error_mark_node;
6064 /* Ensure an error for assigning a non-lvalue array to an array in
6065 C90. */
6066 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6068 error_at (location, "assignment to expression with array type");
6069 return error_mark_node;
6072 /* For ObjC properties, defer this check. */
6073 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6074 return error_mark_node;
6076 is_atomic_op = really_atomic_lvalue (lhs);
6078 newrhs = rhs;
6080 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6082 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6083 lhs_origtype, modifycode, rhs_loc, rhs,
6084 rhs_origtype);
6085 if (inner == error_mark_node)
6086 return error_mark_node;
6087 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6088 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6089 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6090 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6091 protected_set_expr_location (result, location);
6092 return result;
6095 /* If a binary op has been requested, combine the old LHS value with the RHS
6096 producing the value we should actually store into the LHS. */
6098 if (modifycode != NOP_EXPR)
6100 lhs = c_fully_fold (lhs, false, NULL, true);
6101 lhs = stabilize_reference (lhs);
6103 /* Construct the RHS for any non-atomic compound assignemnt. */
6104 if (!is_atomic_op)
6106 /* If in LHS op= RHS the RHS has side-effects, ensure they
6107 are preevaluated before the rest of the assignment expression's
6108 side-effects, because RHS could contain e.g. function calls
6109 that modify LHS. */
6110 if (TREE_SIDE_EFFECTS (rhs))
6112 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6113 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6114 else
6115 newrhs = save_expr (rhs);
6116 rhseval = newrhs;
6117 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6118 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6119 newrhs);
6121 newrhs = build_binary_op (location,
6122 modifycode, lhs, newrhs, true);
6124 /* The original type of the right hand side is no longer
6125 meaningful. */
6126 rhs_origtype = NULL_TREE;
6130 if (c_dialect_objc ())
6132 /* Check if we are modifying an Objective-C property reference;
6133 if so, we need to generate setter calls. */
6134 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6135 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6136 else
6137 result = objc_maybe_build_modify_expr (lhs, newrhs);
6138 if (result)
6139 goto return_result;
6141 /* Else, do the check that we postponed for Objective-C. */
6142 if (!lvalue_or_else (location, lhs, lv_assign))
6143 return error_mark_node;
6146 /* Give an error for storing in something that is 'const'. */
6148 if (TYPE_READONLY (lhstype)
6149 || (RECORD_OR_UNION_TYPE_P (lhstype)
6150 && C_TYPE_FIELDS_READONLY (lhstype)))
6152 readonly_error (location, lhs, lv_assign);
6153 return error_mark_node;
6155 else if (TREE_READONLY (lhs))
6156 readonly_warning (lhs, lv_assign);
6158 /* If storing into a structure or union member,
6159 it has probably been given type `int'.
6160 Compute the type that would go with
6161 the actual amount of storage the member occupies. */
6163 if (TREE_CODE (lhs) == COMPONENT_REF
6164 && (TREE_CODE (lhstype) == INTEGER_TYPE
6165 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6166 || TREE_CODE (lhstype) == REAL_TYPE
6167 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6168 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6170 /* If storing in a field that is in actuality a short or narrower than one,
6171 we must store in the field in its actual type. */
6173 if (lhstype != TREE_TYPE (lhs))
6175 lhs = copy_node (lhs);
6176 TREE_TYPE (lhs) = lhstype;
6179 /* Issue -Wc++-compat warnings about an assignment to an enum type
6180 when LHS does not have its original type. This happens for,
6181 e.g., an enum bitfield in a struct. */
6182 if (warn_cxx_compat
6183 && lhs_origtype != NULL_TREE
6184 && lhs_origtype != lhstype
6185 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6187 tree checktype = (rhs_origtype != NULL_TREE
6188 ? rhs_origtype
6189 : TREE_TYPE (rhs));
6190 if (checktype != error_mark_node
6191 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6192 || (is_atomic_op && modifycode != NOP_EXPR)))
6193 warning_at (location, OPT_Wc___compat,
6194 "enum conversion in assignment is invalid in C++");
6197 /* If the lhs is atomic, remove that qualifier. */
6198 if (is_atomic_op)
6200 lhstype = build_qualified_type (lhstype,
6201 (TYPE_QUALS (lhstype)
6202 & ~TYPE_QUAL_ATOMIC));
6203 olhstype = build_qualified_type (olhstype,
6204 (TYPE_QUALS (lhstype)
6205 & ~TYPE_QUAL_ATOMIC));
6208 /* Convert new value to destination type. Fold it first, then
6209 restore any excess precision information, for the sake of
6210 conversion warnings. */
6212 if (!(is_atomic_op && modifycode != NOP_EXPR))
6214 tree rhs_semantic_type = NULL_TREE;
6215 if (!c_in_omp_for)
6217 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6219 rhs_semantic_type = TREE_TYPE (newrhs);
6220 newrhs = TREE_OPERAND (newrhs, 0);
6222 npc = null_pointer_constant_p (newrhs);
6223 newrhs = c_fully_fold (newrhs, false, NULL);
6224 if (rhs_semantic_type)
6225 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6227 else
6228 npc = null_pointer_constant_p (newrhs);
6229 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6230 rhs_origtype, ic_assign, npc,
6231 NULL_TREE, NULL_TREE, 0);
6232 if (TREE_CODE (newrhs) == ERROR_MARK)
6233 return error_mark_node;
6236 /* Emit ObjC write barrier, if necessary. */
6237 if (c_dialect_objc () && flag_objc_gc)
6239 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6240 if (result)
6242 protected_set_expr_location (result, location);
6243 goto return_result;
6247 /* Scan operands. */
6249 if (is_atomic_op)
6250 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6251 else
6253 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6254 TREE_SIDE_EFFECTS (result) = 1;
6255 protected_set_expr_location (result, location);
6258 /* If we got the LHS in a different type for storing in,
6259 convert the result back to the nominal type of LHS
6260 so that the value we return always has the same type
6261 as the LHS argument. */
6263 if (olhstype == TREE_TYPE (result))
6264 goto return_result;
6266 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6267 rhs_origtype, ic_assign, false, NULL_TREE,
6268 NULL_TREE, 0);
6269 protected_set_expr_location (result, location);
6271 return_result:
6272 if (rhseval)
6273 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6274 return result;
6277 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6278 This is used to implement -fplan9-extensions. */
6280 static bool
6281 find_anonymous_field_with_type (tree struct_type, tree type)
6283 tree field;
6284 bool found;
6286 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6287 found = false;
6288 for (field = TYPE_FIELDS (struct_type);
6289 field != NULL_TREE;
6290 field = TREE_CHAIN (field))
6292 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6293 ? c_build_qualified_type (TREE_TYPE (field),
6294 TYPE_QUAL_ATOMIC)
6295 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6296 if (DECL_NAME (field) == NULL
6297 && comptypes (type, fieldtype))
6299 if (found)
6300 return false;
6301 found = true;
6303 else if (DECL_NAME (field) == NULL
6304 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6305 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6307 if (found)
6308 return false;
6309 found = true;
6312 return found;
6315 /* RHS is an expression whose type is pointer to struct. If there is
6316 an anonymous field in RHS with type TYPE, then return a pointer to
6317 that field in RHS. This is used with -fplan9-extensions. This
6318 returns NULL if no conversion could be found. */
6320 static tree
6321 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6323 tree rhs_struct_type, lhs_main_type;
6324 tree field, found_field;
6325 bool found_sub_field;
6326 tree ret;
6328 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6329 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6330 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6332 gcc_assert (POINTER_TYPE_P (type));
6333 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6334 ? c_build_qualified_type (TREE_TYPE (type),
6335 TYPE_QUAL_ATOMIC)
6336 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6338 found_field = NULL_TREE;
6339 found_sub_field = false;
6340 for (field = TYPE_FIELDS (rhs_struct_type);
6341 field != NULL_TREE;
6342 field = TREE_CHAIN (field))
6344 if (DECL_NAME (field) != NULL_TREE
6345 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6346 continue;
6347 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6348 ? c_build_qualified_type (TREE_TYPE (field),
6349 TYPE_QUAL_ATOMIC)
6350 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6351 if (comptypes (lhs_main_type, fieldtype))
6353 if (found_field != NULL_TREE)
6354 return NULL_TREE;
6355 found_field = field;
6357 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6358 lhs_main_type))
6360 if (found_field != NULL_TREE)
6361 return NULL_TREE;
6362 found_field = field;
6363 found_sub_field = true;
6367 if (found_field == NULL_TREE)
6368 return NULL_TREE;
6370 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6371 build_fold_indirect_ref (rhs), found_field,
6372 NULL_TREE);
6373 ret = build_fold_addr_expr_loc (location, ret);
6375 if (found_sub_field)
6377 ret = convert_to_anonymous_field (location, type, ret);
6378 gcc_assert (ret != NULL_TREE);
6381 return ret;
6384 /* Issue an error message for a bad initializer component.
6385 GMSGID identifies the message.
6386 The component name is taken from the spelling stack. */
6388 static void ATTRIBUTE_GCC_DIAG (2,0)
6389 error_init (location_t loc, const char *gmsgid, ...)
6391 char *ofwhat;
6393 auto_diagnostic_group d;
6395 /* The gmsgid may be a format string with %< and %>. */
6396 va_list ap;
6397 va_start (ap, gmsgid);
6398 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6399 va_end (ap);
6401 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6402 if (*ofwhat && warned)
6403 inform (loc, "(near initialization for %qs)", ofwhat);
6406 /* Issue a pedantic warning for a bad initializer component. OPT is
6407 the option OPT_* (from options.h) controlling this warning or 0 if
6408 it is unconditionally given. GMSGID identifies the message. The
6409 component name is taken from the spelling stack. */
6411 static void ATTRIBUTE_GCC_DIAG (3,0)
6412 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6414 /* Use the location where a macro was expanded rather than where
6415 it was defined to make sure macros defined in system headers
6416 but used incorrectly elsewhere are diagnosed. */
6417 location_t exploc = expansion_point_location_if_in_system_header (loc);
6418 auto_diagnostic_group d;
6419 va_list ap;
6420 va_start (ap, gmsgid);
6421 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6422 va_end (ap);
6423 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6424 if (*ofwhat && warned)
6425 inform (exploc, "(near initialization for %qs)", ofwhat);
6428 /* Issue a warning for a bad initializer component.
6430 OPT is the OPT_W* value corresponding to the warning option that
6431 controls this warning. GMSGID identifies the message. The
6432 component name is taken from the spelling stack. */
6434 static void
6435 warning_init (location_t loc, int opt, const char *gmsgid)
6437 char *ofwhat;
6438 bool warned;
6440 auto_diagnostic_group d;
6442 /* Use the location where a macro was expanded rather than where
6443 it was defined to make sure macros defined in system headers
6444 but used incorrectly elsewhere are diagnosed. */
6445 location_t exploc = expansion_point_location_if_in_system_header (loc);
6447 /* The gmsgid may be a format string with %< and %>. */
6448 warned = warning_at (exploc, opt, gmsgid);
6449 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6450 if (*ofwhat && warned)
6451 inform (exploc, "(near initialization for %qs)", ofwhat);
6454 /* If TYPE is an array type and EXPR is a parenthesized string
6455 constant, warn if pedantic that EXPR is being used to initialize an
6456 object of type TYPE. */
6458 void
6459 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6461 if (pedantic
6462 && TREE_CODE (type) == ARRAY_TYPE
6463 && TREE_CODE (expr.value) == STRING_CST
6464 && expr.original_code != STRING_CST)
6465 pedwarn_init (loc, OPT_Wpedantic,
6466 "array initialized from parenthesized string constant");
6469 /* Attempt to locate the parameter with the given index within FNDECL,
6470 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6472 static location_t
6473 get_fndecl_argument_location (tree fndecl, int argnum)
6475 int i;
6476 tree param;
6478 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6479 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6480 i < argnum && param;
6481 i++, param = TREE_CHAIN (param))
6484 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6485 return DECL_SOURCE_LOCATION (FNDECL). */
6486 if (param == NULL)
6487 return DECL_SOURCE_LOCATION (fndecl);
6489 return DECL_SOURCE_LOCATION (param);
6492 /* Issue a note about a mismatching argument for parameter PARMNUM
6493 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6494 Attempt to issue the note at the pertinent parameter of the decl;
6495 failing that issue it at the location of FUNDECL; failing that
6496 issue it at PLOC. */
6498 static void
6499 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6500 tree expected_type, tree actual_type)
6502 location_t loc;
6503 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6504 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6505 else
6506 loc = ploc;
6508 inform (loc,
6509 "expected %qT but argument is of type %qT",
6510 expected_type, actual_type);
6513 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6514 function FUNDECL declared without prototype to parameter PARMNUM of
6515 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6517 static void
6518 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6519 tree parmtype, tree argtype)
6521 tree_code parmcode = TREE_CODE (parmtype);
6522 tree_code argcode = TREE_CODE (argtype);
6523 tree promoted = c_type_promotes_to (argtype);
6525 /* Avoid warning for enum arguments that promote to an integer type
6526 of the same size/mode. */
6527 if (parmcode == INTEGER_TYPE
6528 && argcode == ENUMERAL_TYPE
6529 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6530 return;
6532 if ((parmcode == argcode
6533 || (parmcode == INTEGER_TYPE
6534 && argcode == ENUMERAL_TYPE))
6535 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6536 return;
6538 /* This diagnoses even signed/unsigned mismatches. Those might be
6539 safe in many cases but GCC may emit suboptimal code for them so
6540 warning on those cases drives efficiency improvements. */
6541 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6542 TYPE_MAIN_VARIANT (promoted) == argtype
6543 ? G_("%qD argument %d type is %qT where %qT is expected "
6544 "in a call to built-in function declared without "
6545 "prototype")
6546 : G_("%qD argument %d promotes to %qT where %qT is expected "
6547 "in a call to built-in function declared without "
6548 "prototype"),
6549 fundecl, parmnum, promoted, parmtype))
6550 inform (DECL_SOURCE_LOCATION (fundecl),
6551 "built-in %qD declared here",
6552 fundecl);
6555 /* Convert value RHS to type TYPE as preparation for an assignment to
6556 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6557 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6558 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6559 constant before any folding.
6560 The real work of conversion is done by `convert'.
6561 The purpose of this function is to generate error messages
6562 for assignments that are not allowed in C.
6563 ERRTYPE says whether it is argument passing, assignment,
6564 initialization or return.
6566 In the following example, '~' denotes where EXPR_LOC and '^' where
6567 LOCATION point to:
6569 f (var); [ic_argpass]
6570 ^ ~~~
6571 x = var; [ic_assign]
6572 ^ ~~~;
6573 int x = var; [ic_init]
6575 return x; [ic_return]
6578 FUNCTION is a tree for the function being called.
6579 PARMNUM is the number of the argument, for printing in error messages.
6580 WARNOPT may be set to a warning option to issue the corresponding warning
6581 rather than an error for invalid conversions. Used for calls to built-in
6582 functions declared without a prototype. */
6584 static tree
6585 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6586 tree rhs, tree origtype, enum impl_conv errtype,
6587 bool null_pointer_constant, tree fundecl,
6588 tree function, int parmnum, int warnopt /* = 0 */)
6590 enum tree_code codel = TREE_CODE (type);
6591 tree orig_rhs = rhs;
6592 tree rhstype;
6593 enum tree_code coder;
6594 tree rname = NULL_TREE;
6595 bool objc_ok = false;
6597 /* Use the expansion point location to handle cases such as user's
6598 function returning a wrong-type macro defined in a system header. */
6599 location = expansion_point_location_if_in_system_header (location);
6601 if (errtype == ic_argpass)
6603 tree selector;
6604 /* Change pointer to function to the function itself for
6605 diagnostics. */
6606 if (TREE_CODE (function) == ADDR_EXPR
6607 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6608 function = TREE_OPERAND (function, 0);
6610 /* Handle an ObjC selector specially for diagnostics. */
6611 selector = objc_message_selector ();
6612 rname = function;
6613 if (selector && parmnum > 2)
6615 rname = selector;
6616 parmnum -= 2;
6620 /* This macro is used to emit diagnostics to ensure that all format
6621 strings are complete sentences, visible to gettext and checked at
6622 compile time. */
6623 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6624 do { \
6625 switch (errtype) \
6627 case ic_argpass: \
6629 auto_diagnostic_group d; \
6630 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6631 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6633 break; \
6634 case ic_assign: \
6635 pedwarn (LOCATION, OPT, AS); \
6636 break; \
6637 case ic_init: \
6638 pedwarn_init (LOCATION, OPT, IN); \
6639 break; \
6640 case ic_return: \
6641 pedwarn (LOCATION, OPT, RE); \
6642 break; \
6643 default: \
6644 gcc_unreachable (); \
6646 } while (0)
6648 /* This macro is used to emit diagnostics to ensure that all format
6649 strings are complete sentences, visible to gettext and checked at
6650 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6651 extra parameter to enumerate qualifiers. */
6652 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6653 do { \
6654 switch (errtype) \
6656 case ic_argpass: \
6658 auto_diagnostic_group d; \
6659 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6660 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6662 break; \
6663 case ic_assign: \
6664 pedwarn (LOCATION, OPT, AS, QUALS); \
6665 break; \
6666 case ic_init: \
6667 pedwarn (LOCATION, OPT, IN, QUALS); \
6668 break; \
6669 case ic_return: \
6670 pedwarn (LOCATION, OPT, RE, QUALS); \
6671 break; \
6672 default: \
6673 gcc_unreachable (); \
6675 } while (0)
6677 /* This macro is used to emit diagnostics to ensure that all format
6678 strings are complete sentences, visible to gettext and checked at
6679 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6680 warning_at instead of pedwarn. */
6681 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6682 do { \
6683 switch (errtype) \
6685 case ic_argpass: \
6687 auto_diagnostic_group d; \
6688 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6689 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6691 break; \
6692 case ic_assign: \
6693 warning_at (LOCATION, OPT, AS, QUALS); \
6694 break; \
6695 case ic_init: \
6696 warning_at (LOCATION, OPT, IN, QUALS); \
6697 break; \
6698 case ic_return: \
6699 warning_at (LOCATION, OPT, RE, QUALS); \
6700 break; \
6701 default: \
6702 gcc_unreachable (); \
6704 } while (0)
6706 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6707 rhs = TREE_OPERAND (rhs, 0);
6709 rhstype = TREE_TYPE (rhs);
6710 coder = TREE_CODE (rhstype);
6712 if (coder == ERROR_MARK)
6713 return error_mark_node;
6715 if (c_dialect_objc ())
6717 int parmno;
6719 switch (errtype)
6721 case ic_return:
6722 parmno = 0;
6723 break;
6725 case ic_assign:
6726 parmno = -1;
6727 break;
6729 case ic_init:
6730 parmno = -2;
6731 break;
6733 default:
6734 parmno = parmnum;
6735 break;
6738 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6741 if (warn_cxx_compat)
6743 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6744 if (checktype != error_mark_node
6745 && TREE_CODE (type) == ENUMERAL_TYPE
6746 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6747 switch (errtype)
6749 case ic_argpass:
6750 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6751 "passing argument %d of %qE is invalid in C++",
6752 parmnum, rname))
6753 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6754 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6755 "expected %qT but argument is of type %qT",
6756 type, rhstype);
6757 break;
6758 case ic_assign:
6759 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6760 "%qT in assignment is invalid in C++", rhstype, type);
6761 break;
6762 case ic_init:
6763 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6764 "%qT to %qT in initialization is invalid in C++",
6765 rhstype, type);
6766 break;
6767 case ic_return:
6768 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6769 "%qT in return is invalid in C++", rhstype, type);
6770 break;
6771 default:
6772 gcc_unreachable ();
6776 if (warn_enum_conversion)
6778 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6779 if (checktype != error_mark_node
6780 && TREE_CODE (checktype) == ENUMERAL_TYPE
6781 && TREE_CODE (type) == ENUMERAL_TYPE
6782 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6784 gcc_rich_location loc (location);
6785 warning_at (&loc, OPT_Wenum_conversion,
6786 "implicit conversion from %qT to %qT",
6787 checktype, type);
6791 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6793 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6794 return rhs;
6797 if (coder == VOID_TYPE)
6799 /* Except for passing an argument to an unprototyped function,
6800 this is a constraint violation. When passing an argument to
6801 an unprototyped function, it is compile-time undefined;
6802 making it a constraint in that case was rejected in
6803 DR#252. */
6804 const char msg[] = "void value not ignored as it ought to be";
6805 if (warnopt)
6806 warning_at (location, warnopt, msg);
6807 else
6808 error_at (location, msg);
6809 return error_mark_node;
6811 rhs = require_complete_type (location, rhs);
6812 if (rhs == error_mark_node)
6813 return error_mark_node;
6815 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6816 return error_mark_node;
6818 /* A non-reference type can convert to a reference. This handles
6819 va_start, va_copy and possibly port built-ins. */
6820 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6822 if (!lvalue_p (rhs))
6824 const char msg[] = "cannot pass rvalue to reference parameter";
6825 if (warnopt)
6826 warning_at (location, warnopt, msg);
6827 else
6828 error_at (location, msg);
6829 return error_mark_node;
6831 if (!c_mark_addressable (rhs))
6832 return error_mark_node;
6833 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6834 SET_EXPR_LOCATION (rhs, location);
6836 rhs = convert_for_assignment (location, expr_loc,
6837 build_pointer_type (TREE_TYPE (type)),
6838 rhs, origtype, errtype,
6839 null_pointer_constant, fundecl, function,
6840 parmnum, warnopt);
6841 if (rhs == error_mark_node)
6842 return error_mark_node;
6844 rhs = build1 (NOP_EXPR, type, rhs);
6845 SET_EXPR_LOCATION (rhs, location);
6846 return rhs;
6848 /* Some types can interconvert without explicit casts. */
6849 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6850 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6851 return convert (type, rhs);
6852 /* Arithmetic types all interconvert, and enum is treated like int. */
6853 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6854 || codel == FIXED_POINT_TYPE
6855 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6856 || codel == BOOLEAN_TYPE)
6857 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6858 || coder == FIXED_POINT_TYPE
6859 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6860 || coder == BOOLEAN_TYPE))
6862 if (warnopt && errtype == ic_argpass)
6863 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
6864 rhstype);
6866 bool save = in_late_binary_op;
6867 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6868 || (coder == REAL_TYPE
6869 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6870 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6871 in_late_binary_op = true;
6872 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6873 ? expr_loc : location, type, orig_rhs);
6874 in_late_binary_op = save;
6875 return ret;
6878 /* Aggregates in different TUs might need conversion. */
6879 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6880 && codel == coder
6881 && comptypes (type, rhstype))
6882 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6883 ? expr_loc : location, type, rhs);
6885 /* Conversion to a transparent union or record from its member types.
6886 This applies only to function arguments. */
6887 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6888 && TYPE_TRANSPARENT_AGGR (type))
6889 && errtype == ic_argpass)
6891 tree memb, marginal_memb = NULL_TREE;
6893 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6895 tree memb_type = TREE_TYPE (memb);
6897 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6898 TYPE_MAIN_VARIANT (rhstype)))
6899 break;
6901 if (TREE_CODE (memb_type) != POINTER_TYPE)
6902 continue;
6904 if (coder == POINTER_TYPE)
6906 tree ttl = TREE_TYPE (memb_type);
6907 tree ttr = TREE_TYPE (rhstype);
6909 /* Any non-function converts to a [const][volatile] void *
6910 and vice versa; otherwise, targets must be the same.
6911 Meanwhile, the lhs target must have all the qualifiers of
6912 the rhs. */
6913 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6914 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6915 || comp_target_types (location, memb_type, rhstype))
6917 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6918 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6919 /* If this type won't generate any warnings, use it. */
6920 if (lquals == rquals
6921 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6922 && TREE_CODE (ttl) == FUNCTION_TYPE)
6923 ? ((lquals | rquals) == rquals)
6924 : ((lquals | rquals) == lquals)))
6925 break;
6927 /* Keep looking for a better type, but remember this one. */
6928 if (!marginal_memb)
6929 marginal_memb = memb;
6933 /* Can convert integer zero to any pointer type. */
6934 if (null_pointer_constant)
6936 rhs = null_pointer_node;
6937 break;
6941 if (memb || marginal_memb)
6943 if (!memb)
6945 /* We have only a marginally acceptable member type;
6946 it needs a warning. */
6947 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6948 tree ttr = TREE_TYPE (rhstype);
6950 /* Const and volatile mean something different for function
6951 types, so the usual warnings are not appropriate. */
6952 if (TREE_CODE (ttr) == FUNCTION_TYPE
6953 && TREE_CODE (ttl) == FUNCTION_TYPE)
6955 /* Because const and volatile on functions are
6956 restrictions that say the function will not do
6957 certain things, it is okay to use a const or volatile
6958 function where an ordinary one is wanted, but not
6959 vice-versa. */
6960 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6961 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6962 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6963 OPT_Wdiscarded_qualifiers,
6964 G_("passing argument %d of %qE "
6965 "makes %q#v qualified function "
6966 "pointer from unqualified"),
6967 G_("assignment makes %q#v qualified "
6968 "function pointer from "
6969 "unqualified"),
6970 G_("initialization makes %q#v qualified "
6971 "function pointer from "
6972 "unqualified"),
6973 G_("return makes %q#v qualified function "
6974 "pointer from unqualified"),
6975 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6977 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6978 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6979 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6980 OPT_Wdiscarded_qualifiers,
6981 G_("passing argument %d of %qE discards "
6982 "%qv qualifier from pointer target type"),
6983 G_("assignment discards %qv qualifier "
6984 "from pointer target type"),
6985 G_("initialization discards %qv qualifier "
6986 "from pointer target type"),
6987 G_("return discards %qv qualifier from "
6988 "pointer target type"),
6989 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6991 memb = marginal_memb;
6994 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6995 pedwarn (location, OPT_Wpedantic,
6996 "ISO C prohibits argument conversion to union type");
6998 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6999 return build_constructor_single (type, memb, rhs);
7003 /* Conversions among pointers */
7004 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7005 && (coder == codel))
7007 /* If RHS refers to a built-in declared without a prototype
7008 BLTIN is the declaration of the built-in with a prototype
7009 and RHSTYPE is set to the actual type of the built-in. */
7010 tree bltin;
7011 rhstype = type_or_builtin_type (rhs, &bltin);
7013 tree ttl = TREE_TYPE (type);
7014 tree ttr = TREE_TYPE (rhstype);
7015 tree mvl = ttl;
7016 tree mvr = ttr;
7017 bool is_opaque_pointer;
7018 int target_cmp = 0; /* Cache comp_target_types () result. */
7019 addr_space_t asl;
7020 addr_space_t asr;
7022 if (TREE_CODE (mvl) != ARRAY_TYPE)
7023 mvl = (TYPE_ATOMIC (mvl)
7024 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7025 TYPE_QUAL_ATOMIC)
7026 : TYPE_MAIN_VARIANT (mvl));
7027 if (TREE_CODE (mvr) != ARRAY_TYPE)
7028 mvr = (TYPE_ATOMIC (mvr)
7029 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7030 TYPE_QUAL_ATOMIC)
7031 : TYPE_MAIN_VARIANT (mvr));
7032 /* Opaque pointers are treated like void pointers. */
7033 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7035 /* The Plan 9 compiler permits a pointer to a struct to be
7036 automatically converted into a pointer to an anonymous field
7037 within the struct. */
7038 if (flag_plan9_extensions
7039 && RECORD_OR_UNION_TYPE_P (mvl)
7040 && RECORD_OR_UNION_TYPE_P (mvr)
7041 && mvl != mvr)
7043 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7044 if (new_rhs != NULL_TREE)
7046 rhs = new_rhs;
7047 rhstype = TREE_TYPE (rhs);
7048 coder = TREE_CODE (rhstype);
7049 ttr = TREE_TYPE (rhstype);
7050 mvr = TYPE_MAIN_VARIANT (ttr);
7054 /* C++ does not allow the implicit conversion void* -> T*. However,
7055 for the purpose of reducing the number of false positives, we
7056 tolerate the special case of
7058 int *p = NULL;
7060 where NULL is typically defined in C to be '(void *) 0'. */
7061 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7062 warning_at (errtype == ic_argpass ? expr_loc : location,
7063 OPT_Wc___compat,
7064 "request for implicit conversion "
7065 "from %qT to %qT not permitted in C++", rhstype, type);
7067 /* See if the pointers point to incompatible address spaces. */
7068 asl = TYPE_ADDR_SPACE (ttl);
7069 asr = TYPE_ADDR_SPACE (ttr);
7070 if (!null_pointer_constant_p (rhs)
7071 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7073 switch (errtype)
7075 case ic_argpass:
7077 const char msg[] = G_("passing argument %d of %qE from "
7078 "pointer to non-enclosed address space");
7079 if (warnopt)
7080 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7081 else
7082 error_at (expr_loc, msg, parmnum, rname);
7083 break;
7085 case ic_assign:
7087 const char msg[] = G_("assignment from pointer to "
7088 "non-enclosed address space");
7089 if (warnopt)
7090 warning_at (location, warnopt, msg);
7091 else
7092 error_at (location, msg);
7093 break;
7095 case ic_init:
7097 const char msg[] = G_("initialization from pointer to "
7098 "non-enclosed address space");
7099 if (warnopt)
7100 warning_at (location, warnopt, msg);
7101 else
7102 error_at (location, msg);
7103 break;
7105 case ic_return:
7107 const char msg[] = G_("return from pointer to "
7108 "non-enclosed address space");
7109 if (warnopt)
7110 warning_at (location, warnopt, msg);
7111 else
7112 error_at (location, msg);
7113 break;
7115 default:
7116 gcc_unreachable ();
7118 return error_mark_node;
7121 /* Check if the right-hand side has a format attribute but the
7122 left-hand side doesn't. */
7123 if (warn_suggest_attribute_format
7124 && check_missing_format_attribute (type, rhstype))
7126 switch (errtype)
7128 case ic_argpass:
7129 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7130 "argument %d of %qE might be "
7131 "a candidate for a format attribute",
7132 parmnum, rname);
7133 break;
7134 case ic_assign:
7135 warning_at (location, OPT_Wsuggest_attribute_format,
7136 "assignment left-hand side might be "
7137 "a candidate for a format attribute");
7138 break;
7139 case ic_init:
7140 warning_at (location, OPT_Wsuggest_attribute_format,
7141 "initialization left-hand side might be "
7142 "a candidate for a format attribute");
7143 break;
7144 case ic_return:
7145 warning_at (location, OPT_Wsuggest_attribute_format,
7146 "return type might be "
7147 "a candidate for a format attribute");
7148 break;
7149 default:
7150 gcc_unreachable ();
7154 /* See if the pointers point to incompatible scalar storage orders. */
7155 if (warn_scalar_storage_order
7156 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7157 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7159 switch (errtype)
7161 case ic_argpass:
7162 /* Do not warn for built-in functions, for example memcpy, since we
7163 control how they behave and they can be useful in this area. */
7164 if (TREE_CODE (rname) != FUNCTION_DECL || !DECL_IS_BUILTIN (rname))
7165 warning_at (location, OPT_Wscalar_storage_order,
7166 "passing argument %d of %qE from incompatible "
7167 "scalar storage order", parmnum, rname);
7168 break;
7169 case ic_assign:
7170 warning_at (location, OPT_Wscalar_storage_order,
7171 "assignment to %qT from pointer type %qT with "
7172 "incompatible scalar storage order", type, rhstype);
7173 break;
7174 case ic_init:
7175 warning_at (location, OPT_Wscalar_storage_order,
7176 "initialization of %qT from pointer type %qT with "
7177 "incompatible scalar storage order", type, rhstype);
7178 break;
7179 case ic_return:
7180 warning_at (location, OPT_Wscalar_storage_order,
7181 "returning %qT from pointer type with incompatible "
7182 "scalar storage order %qT", rhstype, type);
7183 break;
7184 default:
7185 gcc_unreachable ();
7189 /* Any non-function converts to a [const][volatile] void *
7190 and vice versa; otherwise, targets must be the same.
7191 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7192 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7193 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7194 || (target_cmp = comp_target_types (location, type, rhstype))
7195 || is_opaque_pointer
7196 || ((c_common_unsigned_type (mvl)
7197 == c_common_unsigned_type (mvr))
7198 && (c_common_signed_type (mvl)
7199 == c_common_signed_type (mvr))
7200 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7202 /* Warn about loss of qualifers from pointers to arrays with
7203 qualifiers on the element type. */
7204 if (TREE_CODE (ttr) == ARRAY_TYPE)
7206 ttr = strip_array_types (ttr);
7207 ttl = strip_array_types (ttl);
7209 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7210 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7211 WARNING_FOR_QUALIFIERS (location, expr_loc,
7212 OPT_Wdiscarded_array_qualifiers,
7213 G_("passing argument %d of %qE discards "
7214 "%qv qualifier from pointer target type"),
7215 G_("assignment discards %qv qualifier "
7216 "from pointer target type"),
7217 G_("initialization discards %qv qualifier "
7218 "from pointer target type"),
7219 G_("return discards %qv qualifier from "
7220 "pointer target type"),
7221 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7223 else if (pedantic
7224 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7226 (VOID_TYPE_P (ttr)
7227 && !null_pointer_constant
7228 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7229 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7230 G_("ISO C forbids passing argument %d of "
7231 "%qE between function pointer "
7232 "and %<void *%>"),
7233 G_("ISO C forbids assignment between "
7234 "function pointer and %<void *%>"),
7235 G_("ISO C forbids initialization between "
7236 "function pointer and %<void *%>"),
7237 G_("ISO C forbids return between function "
7238 "pointer and %<void *%>"));
7239 /* Const and volatile mean something different for function types,
7240 so the usual warnings are not appropriate. */
7241 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7242 && TREE_CODE (ttl) != FUNCTION_TYPE)
7244 /* Don't warn about loss of qualifier for conversions from
7245 qualified void* to pointers to arrays with corresponding
7246 qualifier on the element type. */
7247 if (!pedantic)
7248 ttl = strip_array_types (ttl);
7250 /* Assignments between atomic and non-atomic objects are OK. */
7251 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7252 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7254 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7255 OPT_Wdiscarded_qualifiers,
7256 G_("passing argument %d of %qE discards "
7257 "%qv qualifier from pointer target type"),
7258 G_("assignment discards %qv qualifier "
7259 "from pointer target type"),
7260 G_("initialization discards %qv qualifier "
7261 "from pointer target type"),
7262 G_("return discards %qv qualifier from "
7263 "pointer target type"),
7264 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7266 /* If this is not a case of ignoring a mismatch in signedness,
7267 no warning. */
7268 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7269 || target_cmp)
7271 /* If there is a mismatch, do warn. */
7272 else if (warn_pointer_sign)
7273 switch (errtype)
7275 case ic_argpass:
7277 auto_diagnostic_group d;
7278 range_label_for_type_mismatch rhs_label (rhstype, type);
7279 gcc_rich_location richloc (expr_loc, &rhs_label);
7280 if (pedwarn (&richloc, OPT_Wpointer_sign,
7281 "pointer targets in passing argument %d of "
7282 "%qE differ in signedness", parmnum, rname))
7283 inform_for_arg (fundecl, expr_loc, parmnum, type,
7284 rhstype);
7286 break;
7287 case ic_assign:
7288 pedwarn (location, OPT_Wpointer_sign,
7289 "pointer targets in assignment from %qT to %qT "
7290 "differ in signedness", rhstype, type);
7291 break;
7292 case ic_init:
7293 pedwarn_init (location, OPT_Wpointer_sign,
7294 "pointer targets in initialization of %qT "
7295 "from %qT differ in signedness", type,
7296 rhstype);
7297 break;
7298 case ic_return:
7299 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7300 "returning %qT from a function with return type "
7301 "%qT differ in signedness", rhstype, type);
7302 break;
7303 default:
7304 gcc_unreachable ();
7307 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7308 && TREE_CODE (ttr) == FUNCTION_TYPE)
7310 /* Because const and volatile on functions are restrictions
7311 that say the function will not do certain things,
7312 it is okay to use a const or volatile function
7313 where an ordinary one is wanted, but not vice-versa. */
7314 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7315 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7316 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7317 OPT_Wdiscarded_qualifiers,
7318 G_("passing argument %d of %qE makes "
7319 "%q#v qualified function pointer "
7320 "from unqualified"),
7321 G_("assignment makes %q#v qualified function "
7322 "pointer from unqualified"),
7323 G_("initialization makes %q#v qualified "
7324 "function pointer from unqualified"),
7325 G_("return makes %q#v qualified function "
7326 "pointer from unqualified"),
7327 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7330 /* Avoid warning about the volatile ObjC EH puts on decls. */
7331 else if (!objc_ok)
7333 switch (errtype)
7335 case ic_argpass:
7337 auto_diagnostic_group d;
7338 range_label_for_type_mismatch rhs_label (rhstype, type);
7339 gcc_rich_location richloc (expr_loc, &rhs_label);
7340 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7341 "passing argument %d of %qE from incompatible "
7342 "pointer type", parmnum, rname))
7343 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7345 break;
7346 case ic_assign:
7347 if (bltin)
7348 pedwarn (location, OPT_Wincompatible_pointer_types,
7349 "assignment to %qT from pointer to "
7350 "%qD with incompatible type %qT",
7351 type, bltin, rhstype);
7352 else
7353 pedwarn (location, OPT_Wincompatible_pointer_types,
7354 "assignment to %qT from incompatible pointer type %qT",
7355 type, rhstype);
7356 break;
7357 case ic_init:
7358 if (bltin)
7359 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7360 "initialization of %qT from pointer to "
7361 "%qD with incompatible type %qT",
7362 type, bltin, rhstype);
7363 else
7364 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7365 "initialization of %qT from incompatible "
7366 "pointer type %qT",
7367 type, rhstype);
7368 break;
7369 case ic_return:
7370 if (bltin)
7371 pedwarn (location, OPT_Wincompatible_pointer_types,
7372 "returning pointer to %qD of type %qT from "
7373 "a function with incompatible type %qT",
7374 bltin, rhstype, type);
7375 else
7376 pedwarn (location, OPT_Wincompatible_pointer_types,
7377 "returning %qT from a function with incompatible "
7378 "return type %qT", rhstype, type);
7379 break;
7380 default:
7381 gcc_unreachable ();
7385 /* If RHS isn't an address, check pointer or array of packed
7386 struct or union. */
7387 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7389 return convert (type, rhs);
7391 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7393 /* ??? This should not be an error when inlining calls to
7394 unprototyped functions. */
7395 const char msg[] = "invalid use of non-lvalue array";
7396 if (warnopt)
7397 warning_at (location, warnopt, msg);
7398 else
7399 error_at (location, msg);
7400 return error_mark_node;
7402 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7404 /* An explicit constant 0 can convert to a pointer,
7405 or one that results from arithmetic, even including
7406 a cast to integer type. */
7407 if (!null_pointer_constant)
7408 switch (errtype)
7410 case ic_argpass:
7412 auto_diagnostic_group d;
7413 range_label_for_type_mismatch rhs_label (rhstype, type);
7414 gcc_rich_location richloc (expr_loc, &rhs_label);
7415 if (pedwarn (&richloc, OPT_Wint_conversion,
7416 "passing argument %d of %qE makes pointer from "
7417 "integer without a cast", parmnum, rname))
7418 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7420 break;
7421 case ic_assign:
7422 pedwarn (location, OPT_Wint_conversion,
7423 "assignment to %qT from %qT makes pointer from integer "
7424 "without a cast", type, rhstype);
7425 break;
7426 case ic_init:
7427 pedwarn_init (location, OPT_Wint_conversion,
7428 "initialization of %qT from %qT makes pointer from "
7429 "integer without a cast", type, rhstype);
7430 break;
7431 case ic_return:
7432 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7433 "function with return type %qT makes pointer from "
7434 "integer without a cast", rhstype, type);
7435 break;
7436 default:
7437 gcc_unreachable ();
7440 return convert (type, rhs);
7442 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7444 switch (errtype)
7446 case ic_argpass:
7448 auto_diagnostic_group d;
7449 range_label_for_type_mismatch rhs_label (rhstype, type);
7450 gcc_rich_location richloc (expr_loc, &rhs_label);
7451 if (pedwarn (&richloc, OPT_Wint_conversion,
7452 "passing argument %d of %qE makes integer from "
7453 "pointer without a cast", parmnum, rname))
7454 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7456 break;
7457 case ic_assign:
7458 pedwarn (location, OPT_Wint_conversion,
7459 "assignment to %qT from %qT makes integer from pointer "
7460 "without a cast", type, rhstype);
7461 break;
7462 case ic_init:
7463 pedwarn_init (location, OPT_Wint_conversion,
7464 "initialization of %qT from %qT makes integer from "
7465 "pointer without a cast", type, rhstype);
7466 break;
7467 case ic_return:
7468 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7469 "function with return type %qT makes integer from "
7470 "pointer without a cast", rhstype, type);
7471 break;
7472 default:
7473 gcc_unreachable ();
7476 return convert (type, rhs);
7478 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7480 tree ret;
7481 bool save = in_late_binary_op;
7482 in_late_binary_op = true;
7483 ret = convert (type, rhs);
7484 in_late_binary_op = save;
7485 return ret;
7488 switch (errtype)
7490 case ic_argpass:
7492 auto_diagnostic_group d;
7493 range_label_for_type_mismatch rhs_label (rhstype, type);
7494 gcc_rich_location richloc (expr_loc, &rhs_label);
7495 const char msg[] = G_("incompatible type for argument %d of %qE");
7496 if (warnopt)
7497 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7498 else
7499 error_at (&richloc, msg, parmnum, rname);
7500 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7502 break;
7503 case ic_assign:
7505 const char msg[]
7506 = G_("incompatible types when assigning to type %qT from type %qT");
7507 if (warnopt)
7508 warning_at (expr_loc, 0, msg, type, rhstype);
7509 else
7510 error_at (expr_loc, msg, type, rhstype);
7511 break;
7513 case ic_init:
7515 const char msg[]
7516 = G_("incompatible types when initializing type %qT using type %qT");
7517 if (warnopt)
7518 warning_at (location, 0, msg, type, rhstype);
7519 else
7520 error_at (location, msg, type, rhstype);
7521 break;
7523 case ic_return:
7525 const char msg[]
7526 = G_("incompatible types when returning type %qT but %qT was expected");
7527 if (warnopt)
7528 warning_at (location, 0, msg, rhstype, type);
7529 else
7530 error_at (location, msg, rhstype, type);
7531 break;
7533 default:
7534 gcc_unreachable ();
7537 return error_mark_node;
7540 /* If VALUE is a compound expr all of whose expressions are constant, then
7541 return its value. Otherwise, return error_mark_node.
7543 This is for handling COMPOUND_EXPRs as initializer elements
7544 which is allowed with a warning when -pedantic is specified. */
7546 static tree
7547 valid_compound_expr_initializer (tree value, tree endtype)
7549 if (TREE_CODE (value) == COMPOUND_EXPR)
7551 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7552 == error_mark_node)
7553 return error_mark_node;
7554 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7555 endtype);
7557 else if (!initializer_constant_valid_p (value, endtype))
7558 return error_mark_node;
7559 else
7560 return value;
7563 /* Perform appropriate conversions on the initial value of a variable,
7564 store it in the declaration DECL,
7565 and print any error messages that are appropriate.
7566 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7567 If the init is invalid, store an ERROR_MARK.
7569 INIT_LOC is the location of the initial value. */
7571 void
7572 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7574 tree value, type;
7575 bool npc = false;
7577 /* If variable's type was invalidly declared, just ignore it. */
7579 type = TREE_TYPE (decl);
7580 if (TREE_CODE (type) == ERROR_MARK)
7581 return;
7583 /* Digest the specified initializer into an expression. */
7585 if (init)
7586 npc = null_pointer_constant_p (init);
7587 value = digest_init (init_loc, type, init, origtype, npc,
7588 true, TREE_STATIC (decl));
7590 /* Store the expression if valid; else report error. */
7592 if (!in_system_header_at (input_location)
7593 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7594 warning (OPT_Wtraditional, "traditional C rejects automatic "
7595 "aggregate initialization");
7597 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7598 DECL_INITIAL (decl) = value;
7600 /* ANSI wants warnings about out-of-range constant initializers. */
7601 STRIP_TYPE_NOPS (value);
7602 if (TREE_STATIC (decl))
7603 constant_expression_warning (value);
7605 /* Check if we need to set array size from compound literal size. */
7606 if (TREE_CODE (type) == ARRAY_TYPE
7607 && TYPE_DOMAIN (type) == NULL_TREE
7608 && value != error_mark_node)
7610 tree inside_init = init;
7612 STRIP_TYPE_NOPS (inside_init);
7613 inside_init = fold (inside_init);
7615 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7617 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7619 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7621 /* For int foo[] = (int [3]){1}; we need to set array size
7622 now since later on array initializer will be just the
7623 brace enclosed list of the compound literal. */
7624 tree etype = strip_array_types (TREE_TYPE (decl));
7625 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7626 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7627 layout_type (type);
7628 layout_decl (cldecl, 0);
7629 TREE_TYPE (decl)
7630 = c_build_qualified_type (type, TYPE_QUALS (etype));
7636 /* Methods for storing and printing names for error messages. */
7638 /* Implement a spelling stack that allows components of a name to be pushed
7639 and popped. Each element on the stack is this structure. */
7641 struct spelling
7643 int kind;
7644 union
7646 unsigned HOST_WIDE_INT i;
7647 const char *s;
7648 } u;
7651 #define SPELLING_STRING 1
7652 #define SPELLING_MEMBER 2
7653 #define SPELLING_BOUNDS 3
7655 static struct spelling *spelling; /* Next stack element (unused). */
7656 static struct spelling *spelling_base; /* Spelling stack base. */
7657 static int spelling_size; /* Size of the spelling stack. */
7659 /* Macros to save and restore the spelling stack around push_... functions.
7660 Alternative to SAVE_SPELLING_STACK. */
7662 #define SPELLING_DEPTH() (spelling - spelling_base)
7663 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7665 /* Push an element on the spelling stack with type KIND and assign VALUE
7666 to MEMBER. */
7668 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7670 int depth = SPELLING_DEPTH (); \
7672 if (depth >= spelling_size) \
7674 spelling_size += 10; \
7675 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7676 spelling_size); \
7677 RESTORE_SPELLING_DEPTH (depth); \
7680 spelling->kind = (KIND); \
7681 spelling->MEMBER = (VALUE); \
7682 spelling++; \
7685 /* Push STRING on the stack. Printed literally. */
7687 static void
7688 push_string (const char *string)
7690 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7693 /* Push a member name on the stack. Printed as '.' STRING. */
7695 static void
7696 push_member_name (tree decl)
7698 const char *const string
7699 = (DECL_NAME (decl)
7700 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7701 : _("<anonymous>"));
7702 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7705 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7707 static void
7708 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7710 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7713 /* Compute the maximum size in bytes of the printed spelling. */
7715 static int
7716 spelling_length (void)
7718 int size = 0;
7719 struct spelling *p;
7721 for (p = spelling_base; p < spelling; p++)
7723 if (p->kind == SPELLING_BOUNDS)
7724 size += 25;
7725 else
7726 size += strlen (p->u.s) + 1;
7729 return size;
7732 /* Print the spelling to BUFFER and return it. */
7734 static char *
7735 print_spelling (char *buffer)
7737 char *d = buffer;
7738 struct spelling *p;
7740 for (p = spelling_base; p < spelling; p++)
7741 if (p->kind == SPELLING_BOUNDS)
7743 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7744 d += strlen (d);
7746 else
7748 const char *s;
7749 if (p->kind == SPELLING_MEMBER)
7750 *d++ = '.';
7751 for (s = p->u.s; (*d = *s++); d++)
7754 *d++ = '\0';
7755 return buffer;
7758 /* Digest the parser output INIT as an initializer for type TYPE.
7759 Return a C expression of type TYPE to represent the initial value.
7761 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7763 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7765 If INIT is a string constant, STRICT_STRING is true if it is
7766 unparenthesized or we should not warn here for it being parenthesized.
7767 For other types of INIT, STRICT_STRING is not used.
7769 INIT_LOC is the location of the INIT.
7771 REQUIRE_CONSTANT requests an error if non-constant initializers or
7772 elements are seen. */
7774 static tree
7775 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7776 bool null_pointer_constant, bool strict_string,
7777 int require_constant)
7779 enum tree_code code = TREE_CODE (type);
7780 tree inside_init = init;
7781 tree semantic_type = NULL_TREE;
7782 bool maybe_const = true;
7784 if (type == error_mark_node
7785 || !init
7786 || error_operand_p (init))
7787 return error_mark_node;
7789 STRIP_TYPE_NOPS (inside_init);
7791 if (!c_in_omp_for)
7793 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7795 semantic_type = TREE_TYPE (inside_init);
7796 inside_init = TREE_OPERAND (inside_init, 0);
7798 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7801 /* Initialization of an array of chars from a string constant
7802 optionally enclosed in braces. */
7804 if (code == ARRAY_TYPE && inside_init
7805 && TREE_CODE (inside_init) == STRING_CST)
7807 tree typ1
7808 = (TYPE_ATOMIC (TREE_TYPE (type))
7809 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7810 TYPE_QUAL_ATOMIC)
7811 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7812 /* Note that an array could be both an array of character type
7813 and an array of wchar_t if wchar_t is signed char or unsigned
7814 char. */
7815 bool char_array = (typ1 == char_type_node
7816 || typ1 == signed_char_type_node
7817 || typ1 == unsigned_char_type_node);
7818 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7819 bool char16_array = !!comptypes (typ1, char16_type_node);
7820 bool char32_array = !!comptypes (typ1, char32_type_node);
7822 if (char_array || wchar_array || char16_array || char32_array)
7824 struct c_expr expr;
7825 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7826 bool incompat_string_cst = false;
7827 expr.value = inside_init;
7828 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7829 expr.original_type = NULL;
7830 maybe_warn_string_init (init_loc, type, expr);
7832 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7833 pedwarn_init (init_loc, OPT_Wpedantic,
7834 "initialization of a flexible array member");
7836 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7837 TYPE_MAIN_VARIANT (type)))
7838 return inside_init;
7840 if (char_array)
7842 if (typ2 != char_type_node)
7843 incompat_string_cst = true;
7845 else if (!comptypes (typ1, typ2))
7846 incompat_string_cst = true;
7848 if (incompat_string_cst)
7850 error_init (init_loc, "cannot initialize array of %qT from "
7851 "a string literal with type array of %qT",
7852 typ1, typ2);
7853 return error_mark_node;
7856 if (TYPE_DOMAIN (type) != NULL_TREE
7857 && TYPE_SIZE (type) != NULL_TREE
7858 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7860 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7861 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
7863 /* Subtract the size of a single (possibly wide) character
7864 because it's ok to ignore the terminating null char
7865 that is counted in the length of the constant. */
7866 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
7867 pedwarn_init (init_loc, 0,
7868 ("initializer-string for array of %qT "
7869 "is too long"), typ1);
7870 else if (warn_cxx_compat
7871 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7872 warning_at (init_loc, OPT_Wc___compat,
7873 ("initializer-string for array of %qT "
7874 "is too long for C++"), typ1);
7875 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7877 unsigned HOST_WIDE_INT size
7878 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7879 const char *p = TREE_STRING_POINTER (inside_init);
7881 inside_init = build_string (size, p);
7885 TREE_TYPE (inside_init) = type;
7886 return inside_init;
7888 else if (INTEGRAL_TYPE_P (typ1))
7890 error_init (init_loc, "array of inappropriate type initialized "
7891 "from string constant");
7892 return error_mark_node;
7896 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7897 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7898 below and handle as a constructor. */
7899 if (code == VECTOR_TYPE
7900 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7901 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7902 && TREE_CONSTANT (inside_init))
7904 if (TREE_CODE (inside_init) == VECTOR_CST
7905 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7906 TYPE_MAIN_VARIANT (type)))
7907 return inside_init;
7909 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7911 unsigned HOST_WIDE_INT ix;
7912 tree value;
7913 bool constant_p = true;
7915 /* Iterate through elements and check if all constructor
7916 elements are *_CSTs. */
7917 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7918 if (!CONSTANT_CLASS_P (value))
7920 constant_p = false;
7921 break;
7924 if (constant_p)
7925 return build_vector_from_ctor (type,
7926 CONSTRUCTOR_ELTS (inside_init));
7930 if (warn_sequence_point)
7931 verify_sequence_points (inside_init);
7933 /* Any type can be initialized
7934 from an expression of the same type, optionally with braces. */
7936 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7937 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7938 TYPE_MAIN_VARIANT (type))
7939 || (code == ARRAY_TYPE
7940 && comptypes (TREE_TYPE (inside_init), type))
7941 || (gnu_vector_type_p (type)
7942 && comptypes (TREE_TYPE (inside_init), type))
7943 || (code == POINTER_TYPE
7944 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7945 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7946 TREE_TYPE (type)))))
7948 if (code == POINTER_TYPE)
7950 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7952 if (TREE_CODE (inside_init) == STRING_CST
7953 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7954 inside_init = array_to_pointer_conversion
7955 (init_loc, inside_init);
7956 else
7958 error_init (init_loc, "invalid use of non-lvalue array");
7959 return error_mark_node;
7964 if (code == VECTOR_TYPE)
7965 /* Although the types are compatible, we may require a
7966 conversion. */
7967 inside_init = convert (type, inside_init);
7969 if (require_constant
7970 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7972 /* As an extension, allow initializing objects with static storage
7973 duration with compound literals (which are then treated just as
7974 the brace enclosed list they contain). Also allow this for
7975 vectors, as we can only assign them with compound literals. */
7976 if (flag_isoc99 && code != VECTOR_TYPE)
7977 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7978 "is not constant");
7979 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7980 inside_init = DECL_INITIAL (decl);
7983 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7984 && TREE_CODE (inside_init) != CONSTRUCTOR)
7986 error_init (init_loc, "array initialized from non-constant array "
7987 "expression");
7988 return error_mark_node;
7991 /* Compound expressions can only occur here if -Wpedantic or
7992 -pedantic-errors is specified. In the later case, we always want
7993 an error. In the former case, we simply want a warning. */
7994 if (require_constant && pedantic
7995 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7997 inside_init
7998 = valid_compound_expr_initializer (inside_init,
7999 TREE_TYPE (inside_init));
8000 if (inside_init == error_mark_node)
8001 error_init (init_loc, "initializer element is not constant");
8002 else
8003 pedwarn_init (init_loc, OPT_Wpedantic,
8004 "initializer element is not constant");
8005 if (flag_pedantic_errors)
8006 inside_init = error_mark_node;
8008 else if (require_constant
8009 && !initializer_constant_valid_p (inside_init,
8010 TREE_TYPE (inside_init)))
8012 error_init (init_loc, "initializer element is not constant");
8013 inside_init = error_mark_node;
8015 else if (require_constant && !maybe_const)
8016 pedwarn_init (init_loc, OPT_Wpedantic,
8017 "initializer element is not a constant expression");
8019 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8020 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8021 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8022 type, inside_init, origtype,
8023 ic_init, null_pointer_constant,
8024 NULL_TREE, NULL_TREE, 0);
8025 return inside_init;
8028 /* Handle scalar types, including conversions. */
8030 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8031 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8032 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8034 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8035 && (TREE_CODE (init) == STRING_CST
8036 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8037 inside_init = init = array_to_pointer_conversion (init_loc, init);
8038 if (semantic_type)
8039 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8040 inside_init);
8041 inside_init
8042 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8043 inside_init, origtype, ic_init,
8044 null_pointer_constant, NULL_TREE, NULL_TREE,
8047 /* Check to see if we have already given an error message. */
8048 if (inside_init == error_mark_node)
8050 else if (require_constant && !TREE_CONSTANT (inside_init))
8052 error_init (init_loc, "initializer element is not constant");
8053 inside_init = error_mark_node;
8055 else if (require_constant
8056 && !initializer_constant_valid_p (inside_init,
8057 TREE_TYPE (inside_init)))
8059 error_init (init_loc, "initializer element is not computable at "
8060 "load time");
8061 inside_init = error_mark_node;
8063 else if (require_constant && !maybe_const)
8064 pedwarn_init (init_loc, OPT_Wpedantic,
8065 "initializer element is not a constant expression");
8067 return inside_init;
8070 /* Come here only for records and arrays. */
8072 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8074 error_init (init_loc, "variable-sized object may not be initialized");
8075 return error_mark_node;
8078 error_init (init_loc, "invalid initializer");
8079 return error_mark_node;
8082 /* Handle initializers that use braces. */
8084 /* Type of object we are accumulating a constructor for.
8085 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8086 static tree constructor_type;
8088 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8089 left to fill. */
8090 static tree constructor_fields;
8092 /* For an ARRAY_TYPE, this is the specified index
8093 at which to store the next element we get. */
8094 static tree constructor_index;
8096 /* For an ARRAY_TYPE, this is the maximum index. */
8097 static tree constructor_max_index;
8099 /* For a RECORD_TYPE, this is the first field not yet written out. */
8100 static tree constructor_unfilled_fields;
8102 /* For an ARRAY_TYPE, this is the index of the first element
8103 not yet written out. */
8104 static tree constructor_unfilled_index;
8106 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8107 This is so we can generate gaps between fields, when appropriate. */
8108 static tree constructor_bit_index;
8110 /* If we are saving up the elements rather than allocating them,
8111 this is the list of elements so far (in reverse order,
8112 most recent first). */
8113 static vec<constructor_elt, va_gc> *constructor_elements;
8115 /* 1 if constructor should be incrementally stored into a constructor chain,
8116 0 if all the elements should be kept in AVL tree. */
8117 static int constructor_incremental;
8119 /* 1 if so far this constructor's elements are all compile-time constants. */
8120 static int constructor_constant;
8122 /* 1 if so far this constructor's elements are all valid address constants. */
8123 static int constructor_simple;
8125 /* 1 if this constructor has an element that cannot be part of a
8126 constant expression. */
8127 static int constructor_nonconst;
8129 /* 1 if this constructor is erroneous so far. */
8130 static int constructor_erroneous;
8132 /* 1 if this constructor is the universal zero initializer { 0 }. */
8133 static int constructor_zeroinit;
8135 /* Structure for managing pending initializer elements, organized as an
8136 AVL tree. */
8138 struct init_node
8140 struct init_node *left, *right;
8141 struct init_node *parent;
8142 int balance;
8143 tree purpose;
8144 tree value;
8145 tree origtype;
8148 /* Tree of pending elements at this constructor level.
8149 These are elements encountered out of order
8150 which belong at places we haven't reached yet in actually
8151 writing the output.
8152 Will never hold tree nodes across GC runs. */
8153 static struct init_node *constructor_pending_elts;
8155 /* The SPELLING_DEPTH of this constructor. */
8156 static int constructor_depth;
8158 /* DECL node for which an initializer is being read.
8159 0 means we are reading a constructor expression
8160 such as (struct foo) {...}. */
8161 static tree constructor_decl;
8163 /* Nonzero if this is an initializer for a top-level decl. */
8164 static int constructor_top_level;
8166 /* Nonzero if there were any member designators in this initializer. */
8167 static int constructor_designated;
8169 /* Nesting depth of designator list. */
8170 static int designator_depth;
8172 /* Nonzero if there were diagnosed errors in this designator list. */
8173 static int designator_erroneous;
8176 /* This stack has a level for each implicit or explicit level of
8177 structuring in the initializer, including the outermost one. It
8178 saves the values of most of the variables above. */
8180 struct constructor_range_stack;
8182 struct constructor_stack
8184 struct constructor_stack *next;
8185 tree type;
8186 tree fields;
8187 tree index;
8188 tree max_index;
8189 tree unfilled_index;
8190 tree unfilled_fields;
8191 tree bit_index;
8192 vec<constructor_elt, va_gc> *elements;
8193 struct init_node *pending_elts;
8194 int offset;
8195 int depth;
8196 /* If value nonzero, this value should replace the entire
8197 constructor at this level. */
8198 struct c_expr replacement_value;
8199 struct constructor_range_stack *range_stack;
8200 char constant;
8201 char simple;
8202 char nonconst;
8203 char implicit;
8204 char erroneous;
8205 char outer;
8206 char incremental;
8207 char designated;
8208 int designator_depth;
8211 static struct constructor_stack *constructor_stack;
8213 /* This stack represents designators from some range designator up to
8214 the last designator in the list. */
8216 struct constructor_range_stack
8218 struct constructor_range_stack *next, *prev;
8219 struct constructor_stack *stack;
8220 tree range_start;
8221 tree index;
8222 tree range_end;
8223 tree fields;
8226 static struct constructor_range_stack *constructor_range_stack;
8228 /* This stack records separate initializers that are nested.
8229 Nested initializers can't happen in ANSI C, but GNU C allows them
8230 in cases like { ... (struct foo) { ... } ... }. */
8232 struct initializer_stack
8234 struct initializer_stack *next;
8235 tree decl;
8236 struct constructor_stack *constructor_stack;
8237 struct constructor_range_stack *constructor_range_stack;
8238 vec<constructor_elt, va_gc> *elements;
8239 struct spelling *spelling;
8240 struct spelling *spelling_base;
8241 int spelling_size;
8242 char top_level;
8243 char require_constant_value;
8244 char require_constant_elements;
8245 rich_location *missing_brace_richloc;
8248 static struct initializer_stack *initializer_stack;
8250 /* Prepare to parse and output the initializer for variable DECL. */
8252 void
8253 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8254 rich_location *richloc)
8256 const char *locus;
8257 struct initializer_stack *p = XNEW (struct initializer_stack);
8259 p->decl = constructor_decl;
8260 p->require_constant_value = require_constant_value;
8261 p->require_constant_elements = require_constant_elements;
8262 p->constructor_stack = constructor_stack;
8263 p->constructor_range_stack = constructor_range_stack;
8264 p->elements = constructor_elements;
8265 p->spelling = spelling;
8266 p->spelling_base = spelling_base;
8267 p->spelling_size = spelling_size;
8268 p->top_level = constructor_top_level;
8269 p->next = initializer_stack;
8270 p->missing_brace_richloc = richloc;
8271 initializer_stack = p;
8273 constructor_decl = decl;
8274 constructor_designated = 0;
8275 constructor_top_level = top_level;
8277 if (decl != NULL_TREE && decl != error_mark_node)
8279 require_constant_value = TREE_STATIC (decl);
8280 require_constant_elements
8281 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8282 /* For a scalar, you can always use any value to initialize,
8283 even within braces. */
8284 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8285 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8287 else
8289 require_constant_value = 0;
8290 require_constant_elements = 0;
8291 locus = _("(anonymous)");
8294 constructor_stack = 0;
8295 constructor_range_stack = 0;
8297 found_missing_braces = 0;
8299 spelling_base = 0;
8300 spelling_size = 0;
8301 RESTORE_SPELLING_DEPTH (0);
8303 if (locus)
8304 push_string (locus);
8307 void
8308 finish_init (void)
8310 struct initializer_stack *p = initializer_stack;
8312 /* Free the whole constructor stack of this initializer. */
8313 while (constructor_stack)
8315 struct constructor_stack *q = constructor_stack;
8316 constructor_stack = q->next;
8317 free (q);
8320 gcc_assert (!constructor_range_stack);
8322 /* Pop back to the data of the outer initializer (if any). */
8323 free (spelling_base);
8325 constructor_decl = p->decl;
8326 require_constant_value = p->require_constant_value;
8327 require_constant_elements = p->require_constant_elements;
8328 constructor_stack = p->constructor_stack;
8329 constructor_range_stack = p->constructor_range_stack;
8330 constructor_elements = p->elements;
8331 spelling = p->spelling;
8332 spelling_base = p->spelling_base;
8333 spelling_size = p->spelling_size;
8334 constructor_top_level = p->top_level;
8335 initializer_stack = p->next;
8336 free (p);
8339 /* Call here when we see the initializer is surrounded by braces.
8340 This is instead of a call to push_init_level;
8341 it is matched by a call to pop_init_level.
8343 TYPE is the type to initialize, for a constructor expression.
8344 For an initializer for a decl, TYPE is zero. */
8346 void
8347 really_start_incremental_init (tree type)
8349 struct constructor_stack *p = XNEW (struct constructor_stack);
8351 if (type == NULL_TREE)
8352 type = TREE_TYPE (constructor_decl);
8354 if (VECTOR_TYPE_P (type)
8355 && TYPE_VECTOR_OPAQUE (type))
8356 error ("opaque vector types cannot be initialized");
8358 p->type = constructor_type;
8359 p->fields = constructor_fields;
8360 p->index = constructor_index;
8361 p->max_index = constructor_max_index;
8362 p->unfilled_index = constructor_unfilled_index;
8363 p->unfilled_fields = constructor_unfilled_fields;
8364 p->bit_index = constructor_bit_index;
8365 p->elements = constructor_elements;
8366 p->constant = constructor_constant;
8367 p->simple = constructor_simple;
8368 p->nonconst = constructor_nonconst;
8369 p->erroneous = constructor_erroneous;
8370 p->pending_elts = constructor_pending_elts;
8371 p->depth = constructor_depth;
8372 p->replacement_value.value = 0;
8373 p->replacement_value.original_code = ERROR_MARK;
8374 p->replacement_value.original_type = NULL;
8375 p->implicit = 0;
8376 p->range_stack = 0;
8377 p->outer = 0;
8378 p->incremental = constructor_incremental;
8379 p->designated = constructor_designated;
8380 p->designator_depth = designator_depth;
8381 p->next = 0;
8382 constructor_stack = p;
8384 constructor_constant = 1;
8385 constructor_simple = 1;
8386 constructor_nonconst = 0;
8387 constructor_depth = SPELLING_DEPTH ();
8388 constructor_elements = NULL;
8389 constructor_pending_elts = 0;
8390 constructor_type = type;
8391 constructor_incremental = 1;
8392 constructor_designated = 0;
8393 constructor_zeroinit = 1;
8394 designator_depth = 0;
8395 designator_erroneous = 0;
8397 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8399 constructor_fields = TYPE_FIELDS (constructor_type);
8400 /* Skip any nameless bit fields at the beginning. */
8401 while (constructor_fields != NULL_TREE
8402 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8403 constructor_fields = DECL_CHAIN (constructor_fields);
8405 constructor_unfilled_fields = constructor_fields;
8406 constructor_bit_index = bitsize_zero_node;
8408 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8410 if (TYPE_DOMAIN (constructor_type))
8412 constructor_max_index
8413 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8415 /* Detect non-empty initializations of zero-length arrays. */
8416 if (constructor_max_index == NULL_TREE
8417 && TYPE_SIZE (constructor_type))
8418 constructor_max_index = integer_minus_one_node;
8420 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8421 to initialize VLAs will cause a proper error; avoid tree
8422 checking errors as well by setting a safe value. */
8423 if (constructor_max_index
8424 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8425 constructor_max_index = integer_minus_one_node;
8427 constructor_index
8428 = convert (bitsizetype,
8429 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8431 else
8433 constructor_index = bitsize_zero_node;
8434 constructor_max_index = NULL_TREE;
8437 constructor_unfilled_index = constructor_index;
8439 else if (gnu_vector_type_p (constructor_type))
8441 /* Vectors are like simple fixed-size arrays. */
8442 constructor_max_index =
8443 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8444 constructor_index = bitsize_zero_node;
8445 constructor_unfilled_index = constructor_index;
8447 else
8449 /* Handle the case of int x = {5}; */
8450 constructor_fields = constructor_type;
8451 constructor_unfilled_fields = constructor_type;
8455 extern location_t last_init_list_comma;
8457 /* Called when we see an open brace for a nested initializer. Finish
8458 off any pending levels with implicit braces. */
8459 void
8460 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8462 while (constructor_stack->implicit)
8464 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8465 && constructor_fields == NULL_TREE)
8466 process_init_element (input_location,
8467 pop_init_level (loc, 1, braced_init_obstack,
8468 last_init_list_comma),
8469 true, braced_init_obstack);
8470 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8471 && constructor_max_index
8472 && tree_int_cst_lt (constructor_max_index,
8473 constructor_index))
8474 process_init_element (input_location,
8475 pop_init_level (loc, 1, braced_init_obstack,
8476 last_init_list_comma),
8477 true, braced_init_obstack);
8478 else
8479 break;
8483 /* Push down into a subobject, for initialization.
8484 If this is for an explicit set of braces, IMPLICIT is 0.
8485 If it is because the next element belongs at a lower level,
8486 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8488 void
8489 push_init_level (location_t loc, int implicit,
8490 struct obstack *braced_init_obstack)
8492 struct constructor_stack *p;
8493 tree value = NULL_TREE;
8495 /* Unless this is an explicit brace, we need to preserve previous
8496 content if any. */
8497 if (implicit)
8499 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8500 value = find_init_member (constructor_fields, braced_init_obstack);
8501 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8502 value = find_init_member (constructor_index, braced_init_obstack);
8505 p = XNEW (struct constructor_stack);
8506 p->type = constructor_type;
8507 p->fields = constructor_fields;
8508 p->index = constructor_index;
8509 p->max_index = constructor_max_index;
8510 p->unfilled_index = constructor_unfilled_index;
8511 p->unfilled_fields = constructor_unfilled_fields;
8512 p->bit_index = constructor_bit_index;
8513 p->elements = constructor_elements;
8514 p->constant = constructor_constant;
8515 p->simple = constructor_simple;
8516 p->nonconst = constructor_nonconst;
8517 p->erroneous = constructor_erroneous;
8518 p->pending_elts = constructor_pending_elts;
8519 p->depth = constructor_depth;
8520 p->replacement_value.value = NULL_TREE;
8521 p->replacement_value.original_code = ERROR_MARK;
8522 p->replacement_value.original_type = NULL;
8523 p->implicit = implicit;
8524 p->outer = 0;
8525 p->incremental = constructor_incremental;
8526 p->designated = constructor_designated;
8527 p->designator_depth = designator_depth;
8528 p->next = constructor_stack;
8529 p->range_stack = 0;
8530 constructor_stack = p;
8532 constructor_constant = 1;
8533 constructor_simple = 1;
8534 constructor_nonconst = 0;
8535 constructor_depth = SPELLING_DEPTH ();
8536 constructor_elements = NULL;
8537 constructor_incremental = 1;
8538 constructor_designated = 0;
8539 constructor_pending_elts = 0;
8540 if (!implicit)
8542 p->range_stack = constructor_range_stack;
8543 constructor_range_stack = 0;
8544 designator_depth = 0;
8545 designator_erroneous = 0;
8548 /* Don't die if an entire brace-pair level is superfluous
8549 in the containing level. */
8550 if (constructor_type == NULL_TREE)
8552 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8554 /* Don't die if there are extra init elts at the end. */
8555 if (constructor_fields == NULL_TREE)
8556 constructor_type = NULL_TREE;
8557 else
8559 constructor_type = TREE_TYPE (constructor_fields);
8560 push_member_name (constructor_fields);
8561 constructor_depth++;
8563 /* If upper initializer is designated, then mark this as
8564 designated too to prevent bogus warnings. */
8565 constructor_designated = p->designated;
8567 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8569 constructor_type = TREE_TYPE (constructor_type);
8570 push_array_bounds (tree_to_uhwi (constructor_index));
8571 constructor_depth++;
8574 if (constructor_type == NULL_TREE)
8576 error_init (loc, "extra brace group at end of initializer");
8577 constructor_fields = NULL_TREE;
8578 constructor_unfilled_fields = NULL_TREE;
8579 return;
8582 if (value && TREE_CODE (value) == CONSTRUCTOR)
8584 constructor_constant = TREE_CONSTANT (value);
8585 constructor_simple = TREE_STATIC (value);
8586 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8587 constructor_elements = CONSTRUCTOR_ELTS (value);
8588 if (!vec_safe_is_empty (constructor_elements)
8589 && (TREE_CODE (constructor_type) == RECORD_TYPE
8590 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8591 set_nonincremental_init (braced_init_obstack);
8594 if (implicit == 1)
8596 found_missing_braces = 1;
8597 if (initializer_stack->missing_brace_richloc)
8598 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8599 (loc, "{");
8602 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8604 constructor_fields = TYPE_FIELDS (constructor_type);
8605 /* Skip any nameless bit fields at the beginning. */
8606 while (constructor_fields != NULL_TREE
8607 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8608 constructor_fields = DECL_CHAIN (constructor_fields);
8610 constructor_unfilled_fields = constructor_fields;
8611 constructor_bit_index = bitsize_zero_node;
8613 else if (gnu_vector_type_p (constructor_type))
8615 /* Vectors are like simple fixed-size arrays. */
8616 constructor_max_index =
8617 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8618 constructor_index = bitsize_int (0);
8619 constructor_unfilled_index = constructor_index;
8621 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8623 if (TYPE_DOMAIN (constructor_type))
8625 constructor_max_index
8626 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8628 /* Detect non-empty initializations of zero-length arrays. */
8629 if (constructor_max_index == NULL_TREE
8630 && TYPE_SIZE (constructor_type))
8631 constructor_max_index = integer_minus_one_node;
8633 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8634 to initialize VLAs will cause a proper error; avoid tree
8635 checking errors as well by setting a safe value. */
8636 if (constructor_max_index
8637 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8638 constructor_max_index = integer_minus_one_node;
8640 constructor_index
8641 = convert (bitsizetype,
8642 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8644 else
8645 constructor_index = bitsize_zero_node;
8647 constructor_unfilled_index = constructor_index;
8648 if (value && TREE_CODE (value) == STRING_CST)
8650 /* We need to split the char/wchar array into individual
8651 characters, so that we don't have to special case it
8652 everywhere. */
8653 set_nonincremental_init_from_string (value, braced_init_obstack);
8656 else
8658 if (constructor_type != error_mark_node)
8659 warning_init (input_location, 0, "braces around scalar initializer");
8660 constructor_fields = constructor_type;
8661 constructor_unfilled_fields = constructor_type;
8665 /* At the end of an implicit or explicit brace level,
8666 finish up that level of constructor. If a single expression
8667 with redundant braces initialized that level, return the
8668 c_expr structure for that expression. Otherwise, the original_code
8669 element is set to ERROR_MARK.
8670 If we were outputting the elements as they are read, return 0 as the value
8671 from inner levels (process_init_element ignores that),
8672 but return error_mark_node as the value from the outermost level
8673 (that's what we want to put in DECL_INITIAL).
8674 Otherwise, return a CONSTRUCTOR expression as the value. */
8676 struct c_expr
8677 pop_init_level (location_t loc, int implicit,
8678 struct obstack *braced_init_obstack,
8679 location_t insert_before)
8681 struct constructor_stack *p;
8682 struct c_expr ret;
8683 ret.value = NULL_TREE;
8684 ret.original_code = ERROR_MARK;
8685 ret.original_type = NULL;
8687 if (implicit == 0)
8689 /* When we come to an explicit close brace,
8690 pop any inner levels that didn't have explicit braces. */
8691 while (constructor_stack->implicit)
8692 process_init_element (input_location,
8693 pop_init_level (loc, 1, braced_init_obstack,
8694 insert_before),
8695 true, braced_init_obstack);
8696 gcc_assert (!constructor_range_stack);
8698 else
8699 if (initializer_stack->missing_brace_richloc)
8700 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8701 (insert_before, "}");
8703 /* Now output all pending elements. */
8704 constructor_incremental = 1;
8705 output_pending_init_elements (1, braced_init_obstack);
8707 p = constructor_stack;
8709 /* Error for initializing a flexible array member, or a zero-length
8710 array member in an inappropriate context. */
8711 if (constructor_type && constructor_fields
8712 && TREE_CODE (constructor_type) == ARRAY_TYPE
8713 && TYPE_DOMAIN (constructor_type)
8714 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8716 /* Silently discard empty initializations. The parser will
8717 already have pedwarned for empty brackets. */
8718 if (integer_zerop (constructor_unfilled_index))
8719 constructor_type = NULL_TREE;
8720 else
8722 gcc_assert (!TYPE_SIZE (constructor_type));
8724 if (constructor_depth > 2)
8725 error_init (loc, "initialization of flexible array member in a nested context");
8726 else
8727 pedwarn_init (loc, OPT_Wpedantic,
8728 "initialization of a flexible array member");
8730 /* We have already issued an error message for the existence
8731 of a flexible array member not at the end of the structure.
8732 Discard the initializer so that we do not die later. */
8733 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8734 constructor_type = NULL_TREE;
8738 switch (vec_safe_length (constructor_elements))
8740 case 0:
8741 /* Initialization with { } counts as zeroinit. */
8742 constructor_zeroinit = 1;
8743 break;
8744 case 1:
8745 /* This might be zeroinit as well. */
8746 if (integer_zerop ((*constructor_elements)[0].value))
8747 constructor_zeroinit = 1;
8748 break;
8749 default:
8750 /* If the constructor has more than one element, it can't be { 0 }. */
8751 constructor_zeroinit = 0;
8752 break;
8755 /* Warn when some structs are initialized with direct aggregation. */
8756 if (!implicit && found_missing_braces && warn_missing_braces
8757 && !constructor_zeroinit)
8759 gcc_assert (initializer_stack->missing_brace_richloc);
8760 warning_at (initializer_stack->missing_brace_richloc,
8761 OPT_Wmissing_braces,
8762 "missing braces around initializer");
8765 /* Warn when some struct elements are implicitly initialized to zero. */
8766 if (warn_missing_field_initializers
8767 && constructor_type
8768 && TREE_CODE (constructor_type) == RECORD_TYPE
8769 && constructor_unfilled_fields)
8771 /* Do not warn for flexible array members or zero-length arrays. */
8772 while (constructor_unfilled_fields
8773 && (!DECL_SIZE (constructor_unfilled_fields)
8774 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8775 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8777 if (constructor_unfilled_fields
8778 /* Do not warn if this level of the initializer uses member
8779 designators; it is likely to be deliberate. */
8780 && !constructor_designated
8781 /* Do not warn about initializing with { 0 } or with { }. */
8782 && !constructor_zeroinit)
8784 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8785 "missing initializer for field %qD of %qT",
8786 constructor_unfilled_fields,
8787 constructor_type))
8788 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8789 "%qD declared here", constructor_unfilled_fields);
8793 /* Pad out the end of the structure. */
8794 if (p->replacement_value.value)
8795 /* If this closes a superfluous brace pair,
8796 just pass out the element between them. */
8797 ret = p->replacement_value;
8798 else if (constructor_type == NULL_TREE)
8800 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8801 && TREE_CODE (constructor_type) != ARRAY_TYPE
8802 && !gnu_vector_type_p (constructor_type))
8804 /* A nonincremental scalar initializer--just return
8805 the element, after verifying there is just one. */
8806 if (vec_safe_is_empty (constructor_elements))
8808 if (!constructor_erroneous && constructor_type != error_mark_node)
8809 error_init (loc, "empty scalar initializer");
8810 ret.value = error_mark_node;
8812 else if (vec_safe_length (constructor_elements) != 1)
8814 error_init (loc, "extra elements in scalar initializer");
8815 ret.value = (*constructor_elements)[0].value;
8817 else
8818 ret.value = (*constructor_elements)[0].value;
8820 else
8822 if (constructor_erroneous)
8823 ret.value = error_mark_node;
8824 else
8826 ret.value = build_constructor (constructor_type,
8827 constructor_elements);
8828 if (constructor_constant)
8829 TREE_CONSTANT (ret.value) = 1;
8830 if (constructor_constant && constructor_simple)
8831 TREE_STATIC (ret.value) = 1;
8832 if (constructor_nonconst)
8833 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8837 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8839 if (constructor_nonconst)
8840 ret.original_code = C_MAYBE_CONST_EXPR;
8841 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8842 ret.original_code = ERROR_MARK;
8845 constructor_type = p->type;
8846 constructor_fields = p->fields;
8847 constructor_index = p->index;
8848 constructor_max_index = p->max_index;
8849 constructor_unfilled_index = p->unfilled_index;
8850 constructor_unfilled_fields = p->unfilled_fields;
8851 constructor_bit_index = p->bit_index;
8852 constructor_elements = p->elements;
8853 constructor_constant = p->constant;
8854 constructor_simple = p->simple;
8855 constructor_nonconst = p->nonconst;
8856 constructor_erroneous = p->erroneous;
8857 constructor_incremental = p->incremental;
8858 constructor_designated = p->designated;
8859 designator_depth = p->designator_depth;
8860 constructor_pending_elts = p->pending_elts;
8861 constructor_depth = p->depth;
8862 if (!p->implicit)
8863 constructor_range_stack = p->range_stack;
8864 RESTORE_SPELLING_DEPTH (constructor_depth);
8866 constructor_stack = p->next;
8867 free (p);
8869 if (ret.value == NULL_TREE && constructor_stack == 0)
8870 ret.value = error_mark_node;
8871 return ret;
8874 /* Common handling for both array range and field name designators.
8875 ARRAY argument is nonzero for array ranges. Returns false for success. */
8877 static bool
8878 set_designator (location_t loc, bool array,
8879 struct obstack *braced_init_obstack)
8881 tree subtype;
8882 enum tree_code subcode;
8884 /* Don't die if an entire brace-pair level is superfluous
8885 in the containing level, or for an erroneous type. */
8886 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
8887 return true;
8889 /* If there were errors in this designator list already, bail out
8890 silently. */
8891 if (designator_erroneous)
8892 return true;
8894 /* Likewise for an initializer for a variable-size type. Those are
8895 diagnosed in digest_init. */
8896 if (COMPLETE_TYPE_P (constructor_type)
8897 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
8898 return true;
8900 if (!designator_depth)
8902 gcc_assert (!constructor_range_stack);
8904 /* Designator list starts at the level of closest explicit
8905 braces. */
8906 while (constructor_stack->implicit)
8907 process_init_element (input_location,
8908 pop_init_level (loc, 1, braced_init_obstack,
8909 last_init_list_comma),
8910 true, braced_init_obstack);
8911 constructor_designated = 1;
8912 return false;
8915 switch (TREE_CODE (constructor_type))
8917 case RECORD_TYPE:
8918 case UNION_TYPE:
8919 subtype = TREE_TYPE (constructor_fields);
8920 if (subtype != error_mark_node)
8921 subtype = TYPE_MAIN_VARIANT (subtype);
8922 break;
8923 case ARRAY_TYPE:
8924 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8925 break;
8926 default:
8927 gcc_unreachable ();
8930 subcode = TREE_CODE (subtype);
8931 if (array && subcode != ARRAY_TYPE)
8933 error_init (loc, "array index in non-array initializer");
8934 return true;
8936 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8938 error_init (loc, "field name not in record or union initializer");
8939 return true;
8942 constructor_designated = 1;
8943 finish_implicit_inits (loc, braced_init_obstack);
8944 push_init_level (loc, 2, braced_init_obstack);
8945 return false;
8948 /* If there are range designators in designator list, push a new designator
8949 to constructor_range_stack. RANGE_END is end of such stack range or
8950 NULL_TREE if there is no range designator at this level. */
8952 static void
8953 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8955 struct constructor_range_stack *p;
8957 p = (struct constructor_range_stack *)
8958 obstack_alloc (braced_init_obstack,
8959 sizeof (struct constructor_range_stack));
8960 p->prev = constructor_range_stack;
8961 p->next = 0;
8962 p->fields = constructor_fields;
8963 p->range_start = constructor_index;
8964 p->index = constructor_index;
8965 p->stack = constructor_stack;
8966 p->range_end = range_end;
8967 if (constructor_range_stack)
8968 constructor_range_stack->next = p;
8969 constructor_range_stack = p;
8972 /* Within an array initializer, specify the next index to be initialized.
8973 FIRST is that index. If LAST is nonzero, then initialize a range
8974 of indices, running from FIRST through LAST. */
8976 void
8977 set_init_index (location_t loc, tree first, tree last,
8978 struct obstack *braced_init_obstack)
8980 if (set_designator (loc, true, braced_init_obstack))
8981 return;
8983 designator_erroneous = 1;
8985 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8986 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8988 error_init (loc, "array index in initializer not of integer type");
8989 return;
8992 if (TREE_CODE (first) != INTEGER_CST)
8994 first = c_fully_fold (first, false, NULL);
8995 if (TREE_CODE (first) == INTEGER_CST)
8996 pedwarn_init (loc, OPT_Wpedantic,
8997 "array index in initializer is not "
8998 "an integer constant expression");
9001 if (last && TREE_CODE (last) != INTEGER_CST)
9003 last = c_fully_fold (last, false, NULL);
9004 if (TREE_CODE (last) == INTEGER_CST)
9005 pedwarn_init (loc, OPT_Wpedantic,
9006 "array index in initializer is not "
9007 "an integer constant expression");
9010 if (TREE_CODE (first) != INTEGER_CST)
9011 error_init (loc, "nonconstant array index in initializer");
9012 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9013 error_init (loc, "nonconstant array index in initializer");
9014 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9015 error_init (loc, "array index in non-array initializer");
9016 else if (tree_int_cst_sgn (first) == -1)
9017 error_init (loc, "array index in initializer exceeds array bounds");
9018 else if (constructor_max_index
9019 && tree_int_cst_lt (constructor_max_index, first))
9020 error_init (loc, "array index in initializer exceeds array bounds");
9021 else
9023 constant_expression_warning (first);
9024 if (last)
9025 constant_expression_warning (last);
9026 constructor_index = convert (bitsizetype, first);
9027 if (tree_int_cst_lt (constructor_index, first))
9029 constructor_index = copy_node (constructor_index);
9030 TREE_OVERFLOW (constructor_index) = 1;
9033 if (last)
9035 if (tree_int_cst_equal (first, last))
9036 last = NULL_TREE;
9037 else if (tree_int_cst_lt (last, first))
9039 error_init (loc, "empty index range in initializer");
9040 last = NULL_TREE;
9042 else
9044 last = convert (bitsizetype, last);
9045 if (constructor_max_index != NULL_TREE
9046 && tree_int_cst_lt (constructor_max_index, last))
9048 error_init (loc, "array index range in initializer exceeds "
9049 "array bounds");
9050 last = NULL_TREE;
9055 designator_depth++;
9056 designator_erroneous = 0;
9057 if (constructor_range_stack || last)
9058 push_range_stack (last, braced_init_obstack);
9062 /* Within a struct initializer, specify the next field to be initialized. */
9064 void
9065 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9066 struct obstack *braced_init_obstack)
9068 tree field;
9070 if (set_designator (loc, false, braced_init_obstack))
9071 return;
9073 designator_erroneous = 1;
9075 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9077 error_init (loc, "field name not in record or union initializer");
9078 return;
9081 field = lookup_field (constructor_type, fieldname);
9083 if (field == NULL_TREE)
9085 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9086 if (guessed_id)
9088 gcc_rich_location rich_loc (fieldname_loc);
9089 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9090 error_at (&rich_loc,
9091 "%qT has no member named %qE; did you mean %qE?",
9092 constructor_type, fieldname, guessed_id);
9094 else
9095 error_at (fieldname_loc, "%qT has no member named %qE",
9096 constructor_type, fieldname);
9098 else
9101 constructor_fields = TREE_VALUE (field);
9102 designator_depth++;
9103 designator_erroneous = 0;
9104 if (constructor_range_stack)
9105 push_range_stack (NULL_TREE, braced_init_obstack);
9106 field = TREE_CHAIN (field);
9107 if (field)
9109 if (set_designator (loc, false, braced_init_obstack))
9110 return;
9113 while (field != NULL_TREE);
9116 /* Add a new initializer to the tree of pending initializers. PURPOSE
9117 identifies the initializer, either array index or field in a structure.
9118 VALUE is the value of that index or field. If ORIGTYPE is not
9119 NULL_TREE, it is the original type of VALUE.
9121 IMPLICIT is true if value comes from pop_init_level (1),
9122 the new initializer has been merged with the existing one
9123 and thus no warnings should be emitted about overriding an
9124 existing initializer. */
9126 static void
9127 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9128 bool implicit, struct obstack *braced_init_obstack)
9130 struct init_node *p, **q, *r;
9132 q = &constructor_pending_elts;
9133 p = 0;
9135 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9137 while (*q != 0)
9139 p = *q;
9140 if (tree_int_cst_lt (purpose, p->purpose))
9141 q = &p->left;
9142 else if (tree_int_cst_lt (p->purpose, purpose))
9143 q = &p->right;
9144 else
9146 if (!implicit)
9148 if (TREE_SIDE_EFFECTS (p->value))
9149 warning_init (loc, OPT_Woverride_init_side_effects,
9150 "initialized field with side-effects "
9151 "overwritten");
9152 else if (warn_override_init)
9153 warning_init (loc, OPT_Woverride_init,
9154 "initialized field overwritten");
9156 p->value = value;
9157 p->origtype = origtype;
9158 return;
9162 else
9164 tree bitpos;
9166 bitpos = bit_position (purpose);
9167 while (*q != NULL)
9169 p = *q;
9170 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9171 q = &p->left;
9172 else if (p->purpose != purpose)
9173 q = &p->right;
9174 else
9176 if (!implicit)
9178 if (TREE_SIDE_EFFECTS (p->value))
9179 warning_init (loc, OPT_Woverride_init_side_effects,
9180 "initialized field with side-effects "
9181 "overwritten");
9182 else if (warn_override_init)
9183 warning_init (loc, OPT_Woverride_init,
9184 "initialized field overwritten");
9186 p->value = value;
9187 p->origtype = origtype;
9188 return;
9193 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9194 sizeof (struct init_node));
9195 r->purpose = purpose;
9196 r->value = value;
9197 r->origtype = origtype;
9199 *q = r;
9200 r->parent = p;
9201 r->left = 0;
9202 r->right = 0;
9203 r->balance = 0;
9205 while (p)
9207 struct init_node *s;
9209 if (r == p->left)
9211 if (p->balance == 0)
9212 p->balance = -1;
9213 else if (p->balance < 0)
9215 if (r->balance < 0)
9217 /* L rotation. */
9218 p->left = r->right;
9219 if (p->left)
9220 p->left->parent = p;
9221 r->right = p;
9223 p->balance = 0;
9224 r->balance = 0;
9226 s = p->parent;
9227 p->parent = r;
9228 r->parent = s;
9229 if (s)
9231 if (s->left == p)
9232 s->left = r;
9233 else
9234 s->right = r;
9236 else
9237 constructor_pending_elts = r;
9239 else
9241 /* LR rotation. */
9242 struct init_node *t = r->right;
9244 r->right = t->left;
9245 if (r->right)
9246 r->right->parent = r;
9247 t->left = r;
9249 p->left = t->right;
9250 if (p->left)
9251 p->left->parent = p;
9252 t->right = p;
9254 p->balance = t->balance < 0;
9255 r->balance = -(t->balance > 0);
9256 t->balance = 0;
9258 s = p->parent;
9259 p->parent = t;
9260 r->parent = t;
9261 t->parent = s;
9262 if (s)
9264 if (s->left == p)
9265 s->left = t;
9266 else
9267 s->right = t;
9269 else
9270 constructor_pending_elts = t;
9272 break;
9274 else
9276 /* p->balance == +1; growth of left side balances the node. */
9277 p->balance = 0;
9278 break;
9281 else /* r == p->right */
9283 if (p->balance == 0)
9284 /* Growth propagation from right side. */
9285 p->balance++;
9286 else if (p->balance > 0)
9288 if (r->balance > 0)
9290 /* R rotation. */
9291 p->right = r->left;
9292 if (p->right)
9293 p->right->parent = p;
9294 r->left = p;
9296 p->balance = 0;
9297 r->balance = 0;
9299 s = p->parent;
9300 p->parent = r;
9301 r->parent = s;
9302 if (s)
9304 if (s->left == p)
9305 s->left = r;
9306 else
9307 s->right = r;
9309 else
9310 constructor_pending_elts = r;
9312 else /* r->balance == -1 */
9314 /* RL rotation */
9315 struct init_node *t = r->left;
9317 r->left = t->right;
9318 if (r->left)
9319 r->left->parent = r;
9320 t->right = r;
9322 p->right = t->left;
9323 if (p->right)
9324 p->right->parent = p;
9325 t->left = p;
9327 r->balance = (t->balance < 0);
9328 p->balance = -(t->balance > 0);
9329 t->balance = 0;
9331 s = p->parent;
9332 p->parent = t;
9333 r->parent = t;
9334 t->parent = s;
9335 if (s)
9337 if (s->left == p)
9338 s->left = t;
9339 else
9340 s->right = t;
9342 else
9343 constructor_pending_elts = t;
9345 break;
9347 else
9349 /* p->balance == -1; growth of right side balances the node. */
9350 p->balance = 0;
9351 break;
9355 r = p;
9356 p = p->parent;
9360 /* Build AVL tree from a sorted chain. */
9362 static void
9363 set_nonincremental_init (struct obstack * braced_init_obstack)
9365 unsigned HOST_WIDE_INT ix;
9366 tree index, value;
9368 if (TREE_CODE (constructor_type) != RECORD_TYPE
9369 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9370 return;
9372 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9373 add_pending_init (input_location, index, value, NULL_TREE, true,
9374 braced_init_obstack);
9375 constructor_elements = NULL;
9376 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9378 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9379 /* Skip any nameless bit fields at the beginning. */
9380 while (constructor_unfilled_fields != NULL_TREE
9381 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9382 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9385 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9387 if (TYPE_DOMAIN (constructor_type))
9388 constructor_unfilled_index
9389 = convert (bitsizetype,
9390 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9391 else
9392 constructor_unfilled_index = bitsize_zero_node;
9394 constructor_incremental = 0;
9397 /* Build AVL tree from a string constant. */
9399 static void
9400 set_nonincremental_init_from_string (tree str,
9401 struct obstack * braced_init_obstack)
9403 tree value, purpose, type;
9404 HOST_WIDE_INT val[2];
9405 const char *p, *end;
9406 int byte, wchar_bytes, charwidth, bitpos;
9408 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9410 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9411 charwidth = TYPE_PRECISION (char_type_node);
9412 gcc_assert ((size_t) wchar_bytes * charwidth
9413 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9414 type = TREE_TYPE (constructor_type);
9415 p = TREE_STRING_POINTER (str);
9416 end = p + TREE_STRING_LENGTH (str);
9418 for (purpose = bitsize_zero_node;
9419 p < end
9420 && !(constructor_max_index
9421 && tree_int_cst_lt (constructor_max_index, purpose));
9422 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9424 if (wchar_bytes == 1)
9426 val[0] = (unsigned char) *p++;
9427 val[1] = 0;
9429 else
9431 val[1] = 0;
9432 val[0] = 0;
9433 for (byte = 0; byte < wchar_bytes; byte++)
9435 if (BYTES_BIG_ENDIAN)
9436 bitpos = (wchar_bytes - byte - 1) * charwidth;
9437 else
9438 bitpos = byte * charwidth;
9439 val[bitpos / HOST_BITS_PER_WIDE_INT]
9440 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9441 << (bitpos % HOST_BITS_PER_WIDE_INT);
9445 if (!TYPE_UNSIGNED (type))
9447 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9448 if (bitpos < HOST_BITS_PER_WIDE_INT)
9450 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9452 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9453 val[1] = -1;
9456 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9458 if (val[0] < 0)
9459 val[1] = -1;
9461 else if (val[1] & (HOST_WIDE_INT_1
9462 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9463 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9466 value = wide_int_to_tree (type,
9467 wide_int::from_array (val, 2,
9468 HOST_BITS_PER_WIDE_INT * 2));
9469 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9470 braced_init_obstack);
9473 constructor_incremental = 0;
9476 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9477 not initialized yet. */
9479 static tree
9480 find_init_member (tree field, struct obstack * braced_init_obstack)
9482 struct init_node *p;
9484 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9486 if (constructor_incremental
9487 && tree_int_cst_lt (field, constructor_unfilled_index))
9488 set_nonincremental_init (braced_init_obstack);
9490 p = constructor_pending_elts;
9491 while (p)
9493 if (tree_int_cst_lt (field, p->purpose))
9494 p = p->left;
9495 else if (tree_int_cst_lt (p->purpose, field))
9496 p = p->right;
9497 else
9498 return p->value;
9501 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9503 tree bitpos = bit_position (field);
9505 if (constructor_incremental
9506 && (!constructor_unfilled_fields
9507 || tree_int_cst_lt (bitpos,
9508 bit_position (constructor_unfilled_fields))))
9509 set_nonincremental_init (braced_init_obstack);
9511 p = constructor_pending_elts;
9512 while (p)
9514 if (field == p->purpose)
9515 return p->value;
9516 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9517 p = p->left;
9518 else
9519 p = p->right;
9522 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9524 if (!vec_safe_is_empty (constructor_elements)
9525 && (constructor_elements->last ().index == field))
9526 return constructor_elements->last ().value;
9528 return NULL_TREE;
9531 /* "Output" the next constructor element.
9532 At top level, really output it to assembler code now.
9533 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9534 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9535 TYPE is the data type that the containing data type wants here.
9536 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9537 If VALUE is a string constant, STRICT_STRING is true if it is
9538 unparenthesized or we should not warn here for it being parenthesized.
9539 For other types of VALUE, STRICT_STRING is not used.
9541 PENDING if true means output pending elements that belong
9542 right after this element. (PENDING is normally true;
9543 it is false while outputting pending elements, to avoid recursion.)
9545 IMPLICIT is true if value comes from pop_init_level (1),
9546 the new initializer has been merged with the existing one
9547 and thus no warnings should be emitted about overriding an
9548 existing initializer. */
9550 static void
9551 output_init_element (location_t loc, tree value, tree origtype,
9552 bool strict_string, tree type, tree field, bool pending,
9553 bool implicit, struct obstack * braced_init_obstack)
9555 tree semantic_type = NULL_TREE;
9556 bool maybe_const = true;
9557 bool npc;
9559 if (type == error_mark_node || value == error_mark_node)
9561 constructor_erroneous = 1;
9562 return;
9564 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9565 && (TREE_CODE (value) == STRING_CST
9566 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9567 && !(TREE_CODE (value) == STRING_CST
9568 && TREE_CODE (type) == ARRAY_TYPE
9569 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9570 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9571 TYPE_MAIN_VARIANT (type)))
9572 value = array_to_pointer_conversion (input_location, value);
9574 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9575 && require_constant_value && pending)
9577 /* As an extension, allow initializing objects with static storage
9578 duration with compound literals (which are then treated just as
9579 the brace enclosed list they contain). */
9580 if (flag_isoc99)
9581 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9582 "constant");
9583 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9584 value = DECL_INITIAL (decl);
9587 npc = null_pointer_constant_p (value);
9588 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9590 semantic_type = TREE_TYPE (value);
9591 value = TREE_OPERAND (value, 0);
9593 value = c_fully_fold (value, require_constant_value, &maybe_const);
9595 if (value == error_mark_node)
9596 constructor_erroneous = 1;
9597 else if (!TREE_CONSTANT (value))
9598 constructor_constant = 0;
9599 else if (!initializer_constant_valid_p (value,
9600 TREE_TYPE (value),
9601 AGGREGATE_TYPE_P (constructor_type)
9602 && TYPE_REVERSE_STORAGE_ORDER
9603 (constructor_type))
9604 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9605 && DECL_C_BIT_FIELD (field)
9606 && TREE_CODE (value) != INTEGER_CST))
9607 constructor_simple = 0;
9608 if (!maybe_const)
9609 constructor_nonconst = 1;
9611 /* Digest the initializer and issue any errors about incompatible
9612 types before issuing errors about non-constant initializers. */
9613 tree new_value = value;
9614 if (semantic_type)
9615 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9616 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9617 require_constant_value);
9618 if (new_value == error_mark_node)
9620 constructor_erroneous = 1;
9621 return;
9623 if (require_constant_value || require_constant_elements)
9624 constant_expression_warning (new_value);
9626 /* Proceed to check the constness of the original initializer. */
9627 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9629 if (require_constant_value)
9631 error_init (loc, "initializer element is not constant");
9632 value = error_mark_node;
9634 else if (require_constant_elements)
9635 pedwarn (loc, OPT_Wpedantic,
9636 "initializer element is not computable at load time");
9638 else if (!maybe_const
9639 && (require_constant_value || require_constant_elements))
9640 pedwarn_init (loc, OPT_Wpedantic,
9641 "initializer element is not a constant expression");
9643 /* Issue -Wc++-compat warnings about initializing a bitfield with
9644 enum type. */
9645 if (warn_cxx_compat
9646 && field != NULL_TREE
9647 && TREE_CODE (field) == FIELD_DECL
9648 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9649 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9650 != TYPE_MAIN_VARIANT (type))
9651 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9653 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9654 if (checktype != error_mark_node
9655 && (TYPE_MAIN_VARIANT (checktype)
9656 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9657 warning_init (loc, OPT_Wc___compat,
9658 "enum conversion in initialization is invalid in C++");
9661 /* If this field is empty and does not have side effects (and is not at
9662 the end of structure), don't do anything other than checking the
9663 initializer. */
9664 if (field
9665 && (TREE_TYPE (field) == error_mark_node
9666 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9667 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9668 && !TREE_SIDE_EFFECTS (new_value)
9669 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9670 || DECL_CHAIN (field)))))
9671 return;
9673 /* Finally, set VALUE to the initializer value digested above. */
9674 value = new_value;
9676 /* If this element doesn't come next in sequence,
9677 put it on constructor_pending_elts. */
9678 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9679 && (!constructor_incremental
9680 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9682 if (constructor_incremental
9683 && tree_int_cst_lt (field, constructor_unfilled_index))
9684 set_nonincremental_init (braced_init_obstack);
9686 add_pending_init (loc, field, value, origtype, implicit,
9687 braced_init_obstack);
9688 return;
9690 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9691 && (!constructor_incremental
9692 || field != constructor_unfilled_fields))
9694 /* We do this for records but not for unions. In a union,
9695 no matter which field is specified, it can be initialized
9696 right away since it starts at the beginning of the union. */
9697 if (constructor_incremental)
9699 if (!constructor_unfilled_fields)
9700 set_nonincremental_init (braced_init_obstack);
9701 else
9703 tree bitpos, unfillpos;
9705 bitpos = bit_position (field);
9706 unfillpos = bit_position (constructor_unfilled_fields);
9708 if (tree_int_cst_lt (bitpos, unfillpos))
9709 set_nonincremental_init (braced_init_obstack);
9713 add_pending_init (loc, field, value, origtype, implicit,
9714 braced_init_obstack);
9715 return;
9717 else if (TREE_CODE (constructor_type) == UNION_TYPE
9718 && !vec_safe_is_empty (constructor_elements))
9720 if (!implicit)
9722 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9723 warning_init (loc, OPT_Woverride_init_side_effects,
9724 "initialized field with side-effects overwritten");
9725 else if (warn_override_init)
9726 warning_init (loc, OPT_Woverride_init,
9727 "initialized field overwritten");
9730 /* We can have just one union field set. */
9731 constructor_elements = NULL;
9734 /* Otherwise, output this element either to
9735 constructor_elements or to the assembler file. */
9737 constructor_elt celt = {field, value};
9738 vec_safe_push (constructor_elements, celt);
9740 /* Advance the variable that indicates sequential elements output. */
9741 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9742 constructor_unfilled_index
9743 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9744 bitsize_one_node);
9745 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9747 constructor_unfilled_fields
9748 = DECL_CHAIN (constructor_unfilled_fields);
9750 /* Skip any nameless bit fields. */
9751 while (constructor_unfilled_fields != NULL_TREE
9752 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9753 constructor_unfilled_fields =
9754 DECL_CHAIN (constructor_unfilled_fields);
9756 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9757 constructor_unfilled_fields = NULL_TREE;
9759 /* Now output any pending elements which have become next. */
9760 if (pending)
9761 output_pending_init_elements (0, braced_init_obstack);
9764 /* For two FIELD_DECLs in the same chain, return -1 if field1
9765 comes before field2, 1 if field1 comes after field2 and
9766 0 if field1 == field2. */
9768 static int
9769 init_field_decl_cmp (tree field1, tree field2)
9771 if (field1 == field2)
9772 return 0;
9774 tree bitpos1 = bit_position (field1);
9775 tree bitpos2 = bit_position (field2);
9776 if (tree_int_cst_equal (bitpos1, bitpos2))
9778 /* If one of the fields has non-zero bitsize, then that
9779 field must be the last one in a sequence of zero
9780 sized fields, fields after it will have bigger
9781 bit_position. */
9782 if (TREE_TYPE (field1) != error_mark_node
9783 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9784 && integer_nonzerop (TREE_TYPE (field1)))
9785 return 1;
9786 if (TREE_TYPE (field2) != error_mark_node
9787 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9788 && integer_nonzerop (TREE_TYPE (field2)))
9789 return -1;
9790 /* Otherwise, fallback to DECL_CHAIN walk to find out
9791 which field comes earlier. Walk chains of both
9792 fields, so that if field1 and field2 are close to each
9793 other in either order, it is found soon even for large
9794 sequences of zero sized fields. */
9795 tree f1 = field1, f2 = field2;
9796 while (1)
9798 f1 = DECL_CHAIN (f1);
9799 f2 = DECL_CHAIN (f2);
9800 if (f1 == NULL_TREE)
9802 gcc_assert (f2);
9803 return 1;
9805 if (f2 == NULL_TREE)
9806 return -1;
9807 if (f1 == field2)
9808 return -1;
9809 if (f2 == field1)
9810 return 1;
9811 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9812 return 1;
9813 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9814 return -1;
9817 else if (tree_int_cst_lt (bitpos1, bitpos2))
9818 return -1;
9819 else
9820 return 1;
9823 /* Output any pending elements which have become next.
9824 As we output elements, constructor_unfilled_{fields,index}
9825 advances, which may cause other elements to become next;
9826 if so, they too are output.
9828 If ALL is 0, we return when there are
9829 no more pending elements to output now.
9831 If ALL is 1, we output space as necessary so that
9832 we can output all the pending elements. */
9833 static void
9834 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9836 struct init_node *elt = constructor_pending_elts;
9837 tree next;
9839 retry:
9841 /* Look through the whole pending tree.
9842 If we find an element that should be output now,
9843 output it. Otherwise, set NEXT to the element
9844 that comes first among those still pending. */
9846 next = NULL_TREE;
9847 while (elt)
9849 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9851 if (tree_int_cst_equal (elt->purpose,
9852 constructor_unfilled_index))
9853 output_init_element (input_location, elt->value, elt->origtype,
9854 true, TREE_TYPE (constructor_type),
9855 constructor_unfilled_index, false, false,
9856 braced_init_obstack);
9857 else if (tree_int_cst_lt (constructor_unfilled_index,
9858 elt->purpose))
9860 /* Advance to the next smaller node. */
9861 if (elt->left)
9862 elt = elt->left;
9863 else
9865 /* We have reached the smallest node bigger than the
9866 current unfilled index. Fill the space first. */
9867 next = elt->purpose;
9868 break;
9871 else
9873 /* Advance to the next bigger node. */
9874 if (elt->right)
9875 elt = elt->right;
9876 else
9878 /* We have reached the biggest node in a subtree. Find
9879 the parent of it, which is the next bigger node. */
9880 while (elt->parent && elt->parent->right == elt)
9881 elt = elt->parent;
9882 elt = elt->parent;
9883 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9884 elt->purpose))
9886 next = elt->purpose;
9887 break;
9892 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9894 /* If the current record is complete we are done. */
9895 if (constructor_unfilled_fields == NULL_TREE)
9896 break;
9898 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9899 elt->purpose);
9900 if (cmp == 0)
9901 output_init_element (input_location, elt->value, elt->origtype,
9902 true, TREE_TYPE (elt->purpose),
9903 elt->purpose, false, false,
9904 braced_init_obstack);
9905 else if (cmp < 0)
9907 /* Advance to the next smaller node. */
9908 if (elt->left)
9909 elt = elt->left;
9910 else
9912 /* We have reached the smallest node bigger than the
9913 current unfilled field. Fill the space first. */
9914 next = elt->purpose;
9915 break;
9918 else
9920 /* Advance to the next bigger node. */
9921 if (elt->right)
9922 elt = elt->right;
9923 else
9925 /* We have reached the biggest node in a subtree. Find
9926 the parent of it, which is the next bigger node. */
9927 while (elt->parent && elt->parent->right == elt)
9928 elt = elt->parent;
9929 elt = elt->parent;
9930 if (elt
9931 && init_field_decl_cmp (constructor_unfilled_fields,
9932 elt->purpose) < 0)
9934 next = elt->purpose;
9935 break;
9942 /* Ordinarily return, but not if we want to output all
9943 and there are elements left. */
9944 if (!(all && next != NULL_TREE))
9945 return;
9947 /* If it's not incremental, just skip over the gap, so that after
9948 jumping to retry we will output the next successive element. */
9949 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9950 constructor_unfilled_fields = next;
9951 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9952 constructor_unfilled_index = next;
9954 /* ELT now points to the node in the pending tree with the next
9955 initializer to output. */
9956 goto retry;
9959 /* Expression VALUE coincides with the start of type TYPE in a braced
9960 initializer. Return true if we should treat VALUE as initializing
9961 the first element of TYPE, false if we should treat it as initializing
9962 TYPE as a whole.
9964 If the initializer is clearly invalid, the question becomes:
9965 which choice gives the best error message? */
9967 static bool
9968 initialize_elementwise_p (tree type, tree value)
9970 if (type == error_mark_node || value == error_mark_node)
9971 return false;
9973 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
9975 tree value_type = TREE_TYPE (value);
9976 if (value_type == error_mark_node)
9977 return false;
9979 /* GNU vectors can be initialized elementwise. However, treat any
9980 kind of vector value as initializing the vector type as a whole,
9981 regardless of whether the value is a GNU vector. Such initializers
9982 are valid if and only if they would have been valid in a non-braced
9983 initializer like:
9985 TYPE foo = VALUE;
9987 so recursing into the vector type would be at best confusing or at
9988 worst wrong. For example, when -flax-vector-conversions is in effect,
9989 it's possible to initialize a V8HI from a V4SI, even though the vectors
9990 have different element types and different numbers of elements. */
9991 if (gnu_vector_type_p (type))
9992 return !VECTOR_TYPE_P (value_type);
9994 if (AGGREGATE_TYPE_P (type))
9995 return type != TYPE_MAIN_VARIANT (value_type);
9997 return false;
10000 /* Add one non-braced element to the current constructor level.
10001 This adjusts the current position within the constructor's type.
10002 This may also start or terminate implicit levels
10003 to handle a partly-braced initializer.
10005 Once this has found the correct level for the new element,
10006 it calls output_init_element.
10008 IMPLICIT is true if value comes from pop_init_level (1),
10009 the new initializer has been merged with the existing one
10010 and thus no warnings should be emitted about overriding an
10011 existing initializer. */
10013 void
10014 process_init_element (location_t loc, struct c_expr value, bool implicit,
10015 struct obstack * braced_init_obstack)
10017 tree orig_value = value.value;
10018 int string_flag
10019 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10020 bool strict_string = value.original_code == STRING_CST;
10021 bool was_designated = designator_depth != 0;
10023 designator_depth = 0;
10024 designator_erroneous = 0;
10026 if (!implicit && value.value && !integer_zerop (value.value))
10027 constructor_zeroinit = 0;
10029 /* Handle superfluous braces around string cst as in
10030 char x[] = {"foo"}; */
10031 if (string_flag
10032 && constructor_type
10033 && !was_designated
10034 && TREE_CODE (constructor_type) == ARRAY_TYPE
10035 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10036 && integer_zerop (constructor_unfilled_index))
10038 if (constructor_stack->replacement_value.value)
10039 error_init (loc, "excess elements in %<char%> array initializer");
10040 constructor_stack->replacement_value = value;
10041 return;
10044 if (constructor_stack->replacement_value.value != NULL_TREE)
10046 error_init (loc, "excess elements in struct initializer");
10047 return;
10050 /* Ignore elements of a brace group if it is entirely superfluous
10051 and has already been diagnosed, or if the type is erroneous. */
10052 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10053 return;
10055 /* Ignore elements of an initializer for a variable-size type.
10056 Those are diagnosed in digest_init. */
10057 if (COMPLETE_TYPE_P (constructor_type)
10058 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10059 return;
10061 if (!implicit && warn_designated_init && !was_designated
10062 && TREE_CODE (constructor_type) == RECORD_TYPE
10063 && lookup_attribute ("designated_init",
10064 TYPE_ATTRIBUTES (constructor_type)))
10065 warning_init (loc,
10066 OPT_Wdesignated_init,
10067 "positional initialization of field "
10068 "in %<struct%> declared with %<designated_init%> attribute");
10070 /* If we've exhausted any levels that didn't have braces,
10071 pop them now. */
10072 while (constructor_stack->implicit)
10074 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10075 && constructor_fields == NULL_TREE)
10076 process_init_element (loc,
10077 pop_init_level (loc, 1, braced_init_obstack,
10078 last_init_list_comma),
10079 true, braced_init_obstack);
10080 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10081 || gnu_vector_type_p (constructor_type))
10082 && constructor_max_index
10083 && tree_int_cst_lt (constructor_max_index,
10084 constructor_index))
10085 process_init_element (loc,
10086 pop_init_level (loc, 1, braced_init_obstack,
10087 last_init_list_comma),
10088 true, braced_init_obstack);
10089 else
10090 break;
10093 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10094 if (constructor_range_stack)
10096 /* If value is a compound literal and we'll be just using its
10097 content, don't put it into a SAVE_EXPR. */
10098 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10099 || !require_constant_value)
10101 tree semantic_type = NULL_TREE;
10102 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10104 semantic_type = TREE_TYPE (value.value);
10105 value.value = TREE_OPERAND (value.value, 0);
10107 value.value = save_expr (value.value);
10108 if (semantic_type)
10109 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10110 value.value);
10114 while (1)
10116 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10118 tree fieldtype;
10119 enum tree_code fieldcode;
10121 if (constructor_fields == NULL_TREE)
10123 pedwarn_init (loc, 0, "excess elements in struct initializer");
10124 break;
10127 fieldtype = TREE_TYPE (constructor_fields);
10128 if (fieldtype != error_mark_node)
10129 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10130 fieldcode = TREE_CODE (fieldtype);
10132 /* Error for non-static initialization of a flexible array member. */
10133 if (fieldcode == ARRAY_TYPE
10134 && !require_constant_value
10135 && TYPE_SIZE (fieldtype) == NULL_TREE
10136 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10138 error_init (loc, "non-static initialization of a flexible "
10139 "array member");
10140 break;
10143 /* Error for initialization of a flexible array member with
10144 a string constant if the structure is in an array. E.g.:
10145 struct S { int x; char y[]; };
10146 struct S s[] = { { 1, "foo" } };
10147 is invalid. */
10148 if (string_flag
10149 && fieldcode == ARRAY_TYPE
10150 && constructor_depth > 1
10151 && TYPE_SIZE (fieldtype) == NULL_TREE
10152 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10154 bool in_array_p = false;
10155 for (struct constructor_stack *p = constructor_stack;
10156 p && p->type; p = p->next)
10157 if (TREE_CODE (p->type) == ARRAY_TYPE)
10159 in_array_p = true;
10160 break;
10162 if (in_array_p)
10164 error_init (loc, "initialization of flexible array "
10165 "member in a nested context");
10166 break;
10170 /* Accept a string constant to initialize a subarray. */
10171 if (value.value != NULL_TREE
10172 && fieldcode == ARRAY_TYPE
10173 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10174 && string_flag)
10175 value.value = orig_value;
10176 /* Otherwise, if we have come to a subaggregate,
10177 and we don't have an element of its type, push into it. */
10178 else if (value.value != NULL_TREE
10179 && initialize_elementwise_p (fieldtype, value.value))
10181 push_init_level (loc, 1, braced_init_obstack);
10182 continue;
10185 if (value.value)
10187 push_member_name (constructor_fields);
10188 output_init_element (loc, value.value, value.original_type,
10189 strict_string, fieldtype,
10190 constructor_fields, true, implicit,
10191 braced_init_obstack);
10192 RESTORE_SPELLING_DEPTH (constructor_depth);
10194 else
10195 /* Do the bookkeeping for an element that was
10196 directly output as a constructor. */
10198 /* For a record, keep track of end position of last field. */
10199 if (DECL_SIZE (constructor_fields))
10200 constructor_bit_index
10201 = size_binop_loc (input_location, PLUS_EXPR,
10202 bit_position (constructor_fields),
10203 DECL_SIZE (constructor_fields));
10205 /* If the current field was the first one not yet written out,
10206 it isn't now, so update. */
10207 if (constructor_unfilled_fields == constructor_fields)
10209 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10210 /* Skip any nameless bit fields. */
10211 while (constructor_unfilled_fields != 0
10212 && (DECL_UNNAMED_BIT_FIELD
10213 (constructor_unfilled_fields)))
10214 constructor_unfilled_fields =
10215 DECL_CHAIN (constructor_unfilled_fields);
10219 constructor_fields = DECL_CHAIN (constructor_fields);
10220 /* Skip any nameless bit fields at the beginning. */
10221 while (constructor_fields != NULL_TREE
10222 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10223 constructor_fields = DECL_CHAIN (constructor_fields);
10225 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10227 tree fieldtype;
10228 enum tree_code fieldcode;
10230 if (constructor_fields == NULL_TREE)
10232 pedwarn_init (loc, 0,
10233 "excess elements in union initializer");
10234 break;
10237 fieldtype = TREE_TYPE (constructor_fields);
10238 if (fieldtype != error_mark_node)
10239 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10240 fieldcode = TREE_CODE (fieldtype);
10242 /* Warn that traditional C rejects initialization of unions.
10243 We skip the warning if the value is zero. This is done
10244 under the assumption that the zero initializer in user
10245 code appears conditioned on e.g. __STDC__ to avoid
10246 "missing initializer" warnings and relies on default
10247 initialization to zero in the traditional C case.
10248 We also skip the warning if the initializer is designated,
10249 again on the assumption that this must be conditional on
10250 __STDC__ anyway (and we've already complained about the
10251 member-designator already). */
10252 if (!in_system_header_at (input_location) && !constructor_designated
10253 && !(value.value && (integer_zerop (value.value)
10254 || real_zerop (value.value))))
10255 warning (OPT_Wtraditional, "traditional C rejects initialization "
10256 "of unions");
10258 /* Accept a string constant to initialize a subarray. */
10259 if (value.value != NULL_TREE
10260 && fieldcode == ARRAY_TYPE
10261 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10262 && string_flag)
10263 value.value = orig_value;
10264 /* Otherwise, if we have come to a subaggregate,
10265 and we don't have an element of its type, push into it. */
10266 else if (value.value != NULL_TREE
10267 && initialize_elementwise_p (fieldtype, value.value))
10269 push_init_level (loc, 1, braced_init_obstack);
10270 continue;
10273 if (value.value)
10275 push_member_name (constructor_fields);
10276 output_init_element (loc, value.value, value.original_type,
10277 strict_string, fieldtype,
10278 constructor_fields, true, implicit,
10279 braced_init_obstack);
10280 RESTORE_SPELLING_DEPTH (constructor_depth);
10282 else
10283 /* Do the bookkeeping for an element that was
10284 directly output as a constructor. */
10286 constructor_bit_index = DECL_SIZE (constructor_fields);
10287 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10290 constructor_fields = NULL_TREE;
10292 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10294 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10295 enum tree_code eltcode = TREE_CODE (elttype);
10297 /* Accept a string constant to initialize a subarray. */
10298 if (value.value != NULL_TREE
10299 && eltcode == ARRAY_TYPE
10300 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10301 && string_flag)
10302 value.value = orig_value;
10303 /* Otherwise, if we have come to a subaggregate,
10304 and we don't have an element of its type, push into it. */
10305 else if (value.value != NULL_TREE
10306 && initialize_elementwise_p (elttype, value.value))
10308 push_init_level (loc, 1, braced_init_obstack);
10309 continue;
10312 if (constructor_max_index != NULL_TREE
10313 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10314 || integer_all_onesp (constructor_max_index)))
10316 pedwarn_init (loc, 0,
10317 "excess elements in array initializer");
10318 break;
10321 /* Now output the actual element. */
10322 if (value.value)
10324 push_array_bounds (tree_to_uhwi (constructor_index));
10325 output_init_element (loc, value.value, value.original_type,
10326 strict_string, elttype,
10327 constructor_index, true, implicit,
10328 braced_init_obstack);
10329 RESTORE_SPELLING_DEPTH (constructor_depth);
10332 constructor_index
10333 = size_binop_loc (input_location, PLUS_EXPR,
10334 constructor_index, bitsize_one_node);
10336 if (!value.value)
10337 /* If we are doing the bookkeeping for an element that was
10338 directly output as a constructor, we must update
10339 constructor_unfilled_index. */
10340 constructor_unfilled_index = constructor_index;
10342 else if (gnu_vector_type_p (constructor_type))
10344 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10346 /* Do a basic check of initializer size. Note that vectors
10347 always have a fixed size derived from their type. */
10348 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10350 pedwarn_init (loc, 0,
10351 "excess elements in vector initializer");
10352 break;
10355 /* Now output the actual element. */
10356 if (value.value)
10358 if (TREE_CODE (value.value) == VECTOR_CST)
10359 elttype = TYPE_MAIN_VARIANT (constructor_type);
10360 output_init_element (loc, value.value, value.original_type,
10361 strict_string, elttype,
10362 constructor_index, true, implicit,
10363 braced_init_obstack);
10366 constructor_index
10367 = size_binop_loc (input_location,
10368 PLUS_EXPR, constructor_index, bitsize_one_node);
10370 if (!value.value)
10371 /* If we are doing the bookkeeping for an element that was
10372 directly output as a constructor, we must update
10373 constructor_unfilled_index. */
10374 constructor_unfilled_index = constructor_index;
10377 /* Handle the sole element allowed in a braced initializer
10378 for a scalar variable. */
10379 else if (constructor_type != error_mark_node
10380 && constructor_fields == NULL_TREE)
10382 pedwarn_init (loc, 0,
10383 "excess elements in scalar initializer");
10384 break;
10386 else
10388 if (value.value)
10389 output_init_element (loc, value.value, value.original_type,
10390 strict_string, constructor_type,
10391 NULL_TREE, true, implicit,
10392 braced_init_obstack);
10393 constructor_fields = NULL_TREE;
10396 /* Handle range initializers either at this level or anywhere higher
10397 in the designator stack. */
10398 if (constructor_range_stack)
10400 struct constructor_range_stack *p, *range_stack;
10401 int finish = 0;
10403 range_stack = constructor_range_stack;
10404 constructor_range_stack = 0;
10405 while (constructor_stack != range_stack->stack)
10407 gcc_assert (constructor_stack->implicit);
10408 process_init_element (loc,
10409 pop_init_level (loc, 1,
10410 braced_init_obstack,
10411 last_init_list_comma),
10412 true, braced_init_obstack);
10414 for (p = range_stack;
10415 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10416 p = p->prev)
10418 gcc_assert (constructor_stack->implicit);
10419 process_init_element (loc,
10420 pop_init_level (loc, 1,
10421 braced_init_obstack,
10422 last_init_list_comma),
10423 true, braced_init_obstack);
10426 p->index = size_binop_loc (input_location,
10427 PLUS_EXPR, p->index, bitsize_one_node);
10428 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10429 finish = 1;
10431 while (1)
10433 constructor_index = p->index;
10434 constructor_fields = p->fields;
10435 if (finish && p->range_end && p->index == p->range_start)
10437 finish = 0;
10438 p->prev = 0;
10440 p = p->next;
10441 if (!p)
10442 break;
10443 finish_implicit_inits (loc, braced_init_obstack);
10444 push_init_level (loc, 2, braced_init_obstack);
10445 p->stack = constructor_stack;
10446 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10447 p->index = p->range_start;
10450 if (!finish)
10451 constructor_range_stack = range_stack;
10452 continue;
10455 break;
10458 constructor_range_stack = 0;
10461 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10462 (guaranteed to be 'volatile' or null) and ARGS (represented using
10463 an ASM_EXPR node). */
10464 tree
10465 build_asm_stmt (bool is_volatile, tree args)
10467 if (is_volatile)
10468 ASM_VOLATILE_P (args) = 1;
10469 return add_stmt (args);
10472 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10473 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10474 SIMPLE indicates whether there was anything at all after the
10475 string in the asm expression -- asm("blah") and asm("blah" : )
10476 are subtly different. We use a ASM_EXPR node to represent this.
10477 LOC is the location of the asm, and IS_INLINE says whether this
10478 is asm inline. */
10479 tree
10480 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10481 tree clobbers, tree labels, bool simple, bool is_inline)
10483 tree tail;
10484 tree args;
10485 int i;
10486 const char *constraint;
10487 const char **oconstraints;
10488 bool allows_mem, allows_reg, is_inout;
10489 int ninputs, noutputs;
10491 ninputs = list_length (inputs);
10492 noutputs = list_length (outputs);
10493 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10495 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10497 /* Remove output conversions that change the type but not the mode. */
10498 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10500 tree output = TREE_VALUE (tail);
10502 output = c_fully_fold (output, false, NULL, true);
10504 /* ??? Really, this should not be here. Users should be using a
10505 proper lvalue, dammit. But there's a long history of using casts
10506 in the output operands. In cases like longlong.h, this becomes a
10507 primitive form of typechecking -- if the cast can be removed, then
10508 the output operand had a type of the proper width; otherwise we'll
10509 get an error. Gross, but ... */
10510 STRIP_NOPS (output);
10512 if (!lvalue_or_else (loc, output, lv_asm))
10513 output = error_mark_node;
10515 if (output != error_mark_node
10516 && (TREE_READONLY (output)
10517 || TYPE_READONLY (TREE_TYPE (output))
10518 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10519 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10520 readonly_error (loc, output, lv_asm);
10522 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10523 oconstraints[i] = constraint;
10525 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10526 &allows_mem, &allows_reg, &is_inout))
10528 /* If the operand is going to end up in memory,
10529 mark it addressable. */
10530 if (!allows_reg && !c_mark_addressable (output))
10531 output = error_mark_node;
10532 if (!(!allows_reg && allows_mem)
10533 && output != error_mark_node
10534 && VOID_TYPE_P (TREE_TYPE (output)))
10536 error_at (loc, "invalid use of void expression");
10537 output = error_mark_node;
10540 else
10541 output = error_mark_node;
10543 TREE_VALUE (tail) = output;
10546 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10548 tree input;
10550 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10551 input = TREE_VALUE (tail);
10553 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10554 oconstraints, &allows_mem, &allows_reg))
10556 /* If the operand is going to end up in memory,
10557 mark it addressable. */
10558 if (!allows_reg && allows_mem)
10560 input = c_fully_fold (input, false, NULL, true);
10562 /* Strip the nops as we allow this case. FIXME, this really
10563 should be rejected or made deprecated. */
10564 STRIP_NOPS (input);
10565 if (!c_mark_addressable (input))
10566 input = error_mark_node;
10568 else
10570 struct c_expr expr;
10571 memset (&expr, 0, sizeof (expr));
10572 expr.value = input;
10573 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10574 input = c_fully_fold (expr.value, false, NULL);
10576 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10578 error_at (loc, "invalid use of void expression");
10579 input = error_mark_node;
10583 else
10584 input = error_mark_node;
10586 TREE_VALUE (tail) = input;
10589 /* ASMs with labels cannot have outputs. This should have been
10590 enforced by the parser. */
10591 gcc_assert (outputs == NULL || labels == NULL);
10593 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10595 /* asm statements without outputs, including simple ones, are treated
10596 as volatile. */
10597 ASM_INPUT_P (args) = simple;
10598 ASM_VOLATILE_P (args) = (noutputs == 0);
10599 ASM_INLINE_P (args) = is_inline;
10601 return args;
10604 /* Generate a goto statement to LABEL. LOC is the location of the
10605 GOTO. */
10607 tree
10608 c_finish_goto_label (location_t loc, tree label)
10610 tree decl = lookup_label_for_goto (loc, label);
10611 if (!decl)
10612 return NULL_TREE;
10613 TREE_USED (decl) = 1;
10615 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10616 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10617 SET_EXPR_LOCATION (t, loc);
10618 return add_stmt (t);
10622 /* Generate a computed goto statement to EXPR. LOC is the location of
10623 the GOTO. */
10625 tree
10626 c_finish_goto_ptr (location_t loc, tree expr)
10628 tree t;
10629 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10630 expr = c_fully_fold (expr, false, NULL);
10631 expr = convert (ptr_type_node, expr);
10632 t = build1 (GOTO_EXPR, void_type_node, expr);
10633 SET_EXPR_LOCATION (t, loc);
10634 return add_stmt (t);
10637 /* Generate a C `return' statement. RETVAL is the expression for what
10638 to return, or a null pointer for `return;' with no value. LOC is
10639 the location of the return statement, or the location of the expression,
10640 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10641 is the original type of RETVAL. */
10643 tree
10644 c_finish_return (location_t loc, tree retval, tree origtype)
10646 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10647 bool no_warning = false;
10648 bool npc = false;
10650 /* Use the expansion point to handle cases such as returning NULL
10651 in a function returning void. */
10652 location_t xloc = expansion_point_location_if_in_system_header (loc);
10654 if (TREE_THIS_VOLATILE (current_function_decl))
10655 warning_at (xloc, 0,
10656 "function declared %<noreturn%> has a %<return%> statement");
10658 if (retval)
10660 tree semantic_type = NULL_TREE;
10661 npc = null_pointer_constant_p (retval);
10662 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10664 semantic_type = TREE_TYPE (retval);
10665 retval = TREE_OPERAND (retval, 0);
10667 retval = c_fully_fold (retval, false, NULL);
10668 if (semantic_type)
10669 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10672 if (!retval)
10674 current_function_returns_null = 1;
10675 if ((warn_return_type >= 0 || flag_isoc99)
10676 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10678 bool warned_here;
10679 if (flag_isoc99)
10680 warned_here = pedwarn
10681 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10682 "%<return%> with no value, in function returning non-void");
10683 else
10684 warned_here = warning_at
10685 (loc, OPT_Wreturn_type,
10686 "%<return%> with no value, in function returning non-void");
10687 no_warning = true;
10688 if (warned_here)
10689 inform (DECL_SOURCE_LOCATION (current_function_decl),
10690 "declared here");
10693 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10695 current_function_returns_null = 1;
10696 bool warned_here;
10697 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10698 warned_here = pedwarn
10699 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10700 "%<return%> with a value, in function returning void");
10701 else
10702 warned_here = pedwarn
10703 (xloc, OPT_Wpedantic, "ISO C forbids "
10704 "%<return%> with expression, in function returning void");
10705 if (warned_here)
10706 inform (DECL_SOURCE_LOCATION (current_function_decl),
10707 "declared here");
10709 else
10711 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10712 retval, origtype, ic_return,
10713 npc, NULL_TREE, NULL_TREE, 0);
10714 tree res = DECL_RESULT (current_function_decl);
10715 tree inner;
10716 bool save;
10718 current_function_returns_value = 1;
10719 if (t == error_mark_node)
10720 return NULL_TREE;
10722 save = in_late_binary_op;
10723 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10724 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10725 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10726 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10727 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10728 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10729 in_late_binary_op = true;
10730 inner = t = convert (TREE_TYPE (res), t);
10731 in_late_binary_op = save;
10733 /* Strip any conversions, additions, and subtractions, and see if
10734 we are returning the address of a local variable. Warn if so. */
10735 while (1)
10737 switch (TREE_CODE (inner))
10739 CASE_CONVERT:
10740 case NON_LVALUE_EXPR:
10741 case PLUS_EXPR:
10742 case POINTER_PLUS_EXPR:
10743 inner = TREE_OPERAND (inner, 0);
10744 continue;
10746 case MINUS_EXPR:
10747 /* If the second operand of the MINUS_EXPR has a pointer
10748 type (or is converted from it), this may be valid, so
10749 don't give a warning. */
10751 tree op1 = TREE_OPERAND (inner, 1);
10753 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10754 && (CONVERT_EXPR_P (op1)
10755 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10756 op1 = TREE_OPERAND (op1, 0);
10758 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10759 break;
10761 inner = TREE_OPERAND (inner, 0);
10762 continue;
10765 case ADDR_EXPR:
10766 inner = TREE_OPERAND (inner, 0);
10768 while (REFERENCE_CLASS_P (inner)
10769 && !INDIRECT_REF_P (inner))
10770 inner = TREE_OPERAND (inner, 0);
10772 if (DECL_P (inner)
10773 && !DECL_EXTERNAL (inner)
10774 && !TREE_STATIC (inner)
10775 && DECL_CONTEXT (inner) == current_function_decl
10776 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10778 if (TREE_CODE (inner) == LABEL_DECL)
10779 warning_at (loc, OPT_Wreturn_local_addr,
10780 "function returns address of label");
10781 else
10783 warning_at (loc, OPT_Wreturn_local_addr,
10784 "function returns address of local variable");
10785 tree zero = build_zero_cst (TREE_TYPE (res));
10786 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10789 break;
10791 default:
10792 break;
10795 break;
10798 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10799 SET_EXPR_LOCATION (retval, loc);
10801 if (warn_sequence_point)
10802 verify_sequence_points (retval);
10805 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10806 TREE_NO_WARNING (ret_stmt) |= no_warning;
10807 return add_stmt (ret_stmt);
10810 struct c_switch {
10811 /* The SWITCH_EXPR being built. */
10812 tree switch_expr;
10814 /* The original type of the testing expression, i.e. before the
10815 default conversion is applied. */
10816 tree orig_type;
10818 /* A splay-tree mapping the low element of a case range to the high
10819 element, or NULL_TREE if there is no high element. Used to
10820 determine whether or not a new case label duplicates an old case
10821 label. We need a tree, rather than simply a hash table, because
10822 of the GNU case range extension. */
10823 splay_tree cases;
10825 /* The bindings at the point of the switch. This is used for
10826 warnings crossing decls when branching to a case label. */
10827 struct c_spot_bindings *bindings;
10829 /* The next node on the stack. */
10830 struct c_switch *next;
10832 /* Remember whether the controlling expression had boolean type
10833 before integer promotions for the sake of -Wswitch-bool. */
10834 bool bool_cond_p;
10837 /* A stack of the currently active switch statements. The innermost
10838 switch statement is on the top of the stack. There is no need to
10839 mark the stack for garbage collection because it is only active
10840 during the processing of the body of a function, and we never
10841 collect at that point. */
10843 struct c_switch *c_switch_stack;
10845 /* Start a C switch statement, testing expression EXP. Return the new
10846 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10847 SWITCH_COND_LOC is the location of the switch's condition.
10848 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10850 tree
10851 c_start_case (location_t switch_loc,
10852 location_t switch_cond_loc,
10853 tree exp, bool explicit_cast_p)
10855 tree orig_type = error_mark_node;
10856 bool bool_cond_p = false;
10857 struct c_switch *cs;
10859 if (exp != error_mark_node)
10861 orig_type = TREE_TYPE (exp);
10863 if (!INTEGRAL_TYPE_P (orig_type))
10865 if (orig_type != error_mark_node)
10867 error_at (switch_cond_loc, "switch quantity not an integer");
10868 orig_type = error_mark_node;
10870 exp = integer_zero_node;
10872 else
10874 tree type = TYPE_MAIN_VARIANT (orig_type);
10875 tree e = exp;
10877 /* Warn if the condition has boolean value. */
10878 while (TREE_CODE (e) == COMPOUND_EXPR)
10879 e = TREE_OPERAND (e, 1);
10881 if ((TREE_CODE (type) == BOOLEAN_TYPE
10882 || truth_value_p (TREE_CODE (e)))
10883 /* Explicit cast to int suppresses this warning. */
10884 && !(TREE_CODE (type) == INTEGER_TYPE
10885 && explicit_cast_p))
10886 bool_cond_p = true;
10888 if (!in_system_header_at (input_location)
10889 && (type == long_integer_type_node
10890 || type == long_unsigned_type_node))
10891 warning_at (switch_cond_loc,
10892 OPT_Wtraditional, "%<long%> switch expression not "
10893 "converted to %<int%> in ISO C");
10895 exp = c_fully_fold (exp, false, NULL);
10896 exp = default_conversion (exp);
10898 if (warn_sequence_point)
10899 verify_sequence_points (exp);
10903 /* Add this new SWITCH_EXPR to the stack. */
10904 cs = XNEW (struct c_switch);
10905 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10906 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10907 cs->orig_type = orig_type;
10908 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10909 cs->bindings = c_get_switch_bindings ();
10910 cs->bool_cond_p = bool_cond_p;
10911 cs->next = c_switch_stack;
10912 c_switch_stack = cs;
10914 return add_stmt (cs->switch_expr);
10917 /* Process a case label at location LOC. */
10919 tree
10920 do_case (location_t loc, tree low_value, tree high_value)
10922 tree label = NULL_TREE;
10924 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10926 low_value = c_fully_fold (low_value, false, NULL);
10927 if (TREE_CODE (low_value) == INTEGER_CST)
10928 pedwarn (loc, OPT_Wpedantic,
10929 "case label is not an integer constant expression");
10932 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10934 high_value = c_fully_fold (high_value, false, NULL);
10935 if (TREE_CODE (high_value) == INTEGER_CST)
10936 pedwarn (input_location, OPT_Wpedantic,
10937 "case label is not an integer constant expression");
10940 if (c_switch_stack == NULL)
10942 if (low_value)
10943 error_at (loc, "case label not within a switch statement");
10944 else
10945 error_at (loc, "%<default%> label not within a switch statement");
10946 return NULL_TREE;
10949 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10950 EXPR_LOCATION (c_switch_stack->switch_expr),
10951 loc))
10952 return NULL_TREE;
10954 label = c_add_case_label (loc, c_switch_stack->cases,
10955 SWITCH_COND (c_switch_stack->switch_expr),
10956 low_value, high_value);
10957 if (label == error_mark_node)
10958 label = NULL_TREE;
10959 return label;
10962 /* Finish the switch statement. TYPE is the original type of the
10963 controlling expression of the switch, or NULL_TREE. */
10965 void
10966 c_finish_case (tree body, tree type)
10968 struct c_switch *cs = c_switch_stack;
10969 location_t switch_location;
10971 SWITCH_BODY (cs->switch_expr) = body;
10973 /* Emit warnings as needed. */
10974 switch_location = EXPR_LOCATION (cs->switch_expr);
10975 c_do_switch_warnings (cs->cases, switch_location,
10976 type ? type : TREE_TYPE (cs->switch_expr),
10977 SWITCH_COND (cs->switch_expr), cs->bool_cond_p);
10978 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10979 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10981 /* Pop the stack. */
10982 c_switch_stack = cs->next;
10983 splay_tree_delete (cs->cases);
10984 c_release_switch_bindings (cs->bindings);
10985 XDELETE (cs);
10988 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10989 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10990 may be null. */
10992 void
10993 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10994 tree else_block)
10996 tree stmt;
10998 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10999 SET_EXPR_LOCATION (stmt, if_locus);
11000 add_stmt (stmt);
11003 /* Emit a general-purpose loop construct. START_LOCUS is the location of
11004 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
11005 is false for DO loops. INCR is the FOR increment expression. BODY is
11006 the statement controlled by the loop. BLAB is the break label. CLAB is
11007 the continue label. Everything is allowed to be NULL.
11008 COND_LOCUS is the location of the loop condition, INCR_LOCUS is the
11009 location of the FOR increment expression. */
11011 void
11012 c_finish_loop (location_t start_locus, location_t cond_locus, tree cond,
11013 location_t incr_locus, tree incr, tree body, tree blab,
11014 tree clab, bool cond_is_first)
11016 tree entry = NULL, exit = NULL, t;
11018 /* If the condition is zero don't generate a loop construct. */
11019 if (cond && integer_zerop (cond))
11021 if (cond_is_first)
11023 t = build_and_jump (&blab);
11024 SET_EXPR_LOCATION (t, start_locus);
11025 add_stmt (t);
11028 else
11030 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
11032 /* If we have an exit condition, then we build an IF with gotos either
11033 out of the loop, or to the top of it. If there's no exit condition,
11034 then we just build a jump back to the top. */
11035 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
11037 if (cond && !integer_nonzerop (cond))
11039 /* Canonicalize the loop condition to the end. This means
11040 generating a branch to the loop condition. Reuse the
11041 continue label, if possible. */
11042 if (cond_is_first)
11044 if (incr || !clab)
11046 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
11047 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
11049 else
11050 t = build1 (GOTO_EXPR, void_type_node, clab);
11051 SET_EXPR_LOCATION (t, start_locus);
11052 add_stmt (t);
11055 t = build_and_jump (&blab);
11056 exit = fold_build3_loc (cond_is_first ? start_locus : input_location,
11057 COND_EXPR, void_type_node, cond, exit, t);
11059 else
11061 /* For the backward-goto's location of an unconditional loop
11062 use the beginning of the body, or, if there is none, the
11063 top of the loop. */
11064 location_t loc = EXPR_LOCATION (expr_first (body));
11065 if (loc == UNKNOWN_LOCATION)
11066 loc = start_locus;
11067 SET_EXPR_LOCATION (exit, loc);
11070 add_stmt (top);
11073 if (body)
11074 add_stmt (body);
11075 if (clab)
11076 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
11077 if (incr)
11079 if (MAY_HAVE_DEBUG_MARKER_STMTS && incr_locus != UNKNOWN_LOCATION)
11081 t = build0 (DEBUG_BEGIN_STMT, void_type_node);
11082 SET_EXPR_LOCATION (t, incr_locus);
11083 add_stmt (t);
11085 add_stmt (incr);
11087 if (entry)
11088 add_stmt (entry);
11089 if (MAY_HAVE_DEBUG_MARKER_STMTS && cond_locus != UNKNOWN_LOCATION)
11091 t = build0 (DEBUG_BEGIN_STMT, void_type_node);
11092 SET_EXPR_LOCATION (t, cond_locus);
11093 add_stmt (t);
11095 if (exit)
11096 add_stmt (exit);
11097 if (blab)
11098 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
11101 tree
11102 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
11104 bool skip;
11105 tree label = *label_p;
11107 /* In switch statements break is sometimes stylistically used after
11108 a return statement. This can lead to spurious warnings about
11109 control reaching the end of a non-void function when it is
11110 inlined. Note that we are calling block_may_fallthru with
11111 language specific tree nodes; this works because
11112 block_may_fallthru returns true when given something it does not
11113 understand. */
11114 skip = !block_may_fallthru (cur_stmt_list);
11116 if (!label)
11118 if (!skip)
11119 *label_p = label = create_artificial_label (loc);
11121 else if (TREE_CODE (label) == LABEL_DECL)
11123 else switch (TREE_INT_CST_LOW (label))
11125 case 0:
11126 if (is_break)
11127 error_at (loc, "break statement not within loop or switch");
11128 else
11129 error_at (loc, "continue statement not within a loop");
11130 return NULL_TREE;
11132 case 1:
11133 gcc_assert (is_break);
11134 error_at (loc, "break statement used with OpenMP for loop");
11135 return NULL_TREE;
11137 case 2:
11138 if (is_break)
11139 error ("break statement within %<#pragma simd%> loop body");
11140 else
11141 error ("continue statement within %<#pragma simd%> loop body");
11142 return NULL_TREE;
11144 default:
11145 gcc_unreachable ();
11148 if (skip)
11149 return NULL_TREE;
11151 if (!is_break)
11152 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
11154 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
11157 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11159 static void
11160 emit_side_effect_warnings (location_t loc, tree expr)
11162 if (expr == error_mark_node)
11164 else if (!TREE_SIDE_EFFECTS (expr))
11166 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
11167 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11169 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11171 tree r = expr;
11172 location_t cloc = loc;
11173 while (TREE_CODE (r) == COMPOUND_EXPR)
11175 if (EXPR_HAS_LOCATION (r))
11176 cloc = EXPR_LOCATION (r);
11177 r = TREE_OPERAND (r, 1);
11179 if (!TREE_SIDE_EFFECTS (r)
11180 && !VOID_TYPE_P (TREE_TYPE (r))
11181 && !CONVERT_EXPR_P (r)
11182 && !TREE_NO_WARNING (r)
11183 && !TREE_NO_WARNING (expr))
11184 warning_at (cloc, OPT_Wunused_value,
11185 "right-hand operand of comma expression has no effect");
11187 else
11188 warn_if_unused_value (expr, loc);
11191 /* Process an expression as if it were a complete statement. Emit
11192 diagnostics, but do not call ADD_STMT. LOC is the location of the
11193 statement. */
11195 tree
11196 c_process_expr_stmt (location_t loc, tree expr)
11198 tree exprv;
11200 if (!expr)
11201 return NULL_TREE;
11203 expr = c_fully_fold (expr, false, NULL);
11205 if (warn_sequence_point)
11206 verify_sequence_points (expr);
11208 if (TREE_TYPE (expr) != error_mark_node
11209 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11210 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11211 error_at (loc, "expression statement has incomplete type");
11213 /* If we're not processing a statement expression, warn about unused values.
11214 Warnings for statement expressions will be emitted later, once we figure
11215 out which is the result. */
11216 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11217 && warn_unused_value)
11218 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11220 exprv = expr;
11221 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11222 exprv = TREE_OPERAND (exprv, 1);
11223 while (CONVERT_EXPR_P (exprv))
11224 exprv = TREE_OPERAND (exprv, 0);
11225 if (DECL_P (exprv)
11226 || handled_component_p (exprv)
11227 || TREE_CODE (exprv) == ADDR_EXPR)
11228 mark_exp_read (exprv);
11230 /* If the expression is not of a type to which we cannot assign a line
11231 number, wrap the thing in a no-op NOP_EXPR. */
11232 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11234 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11235 SET_EXPR_LOCATION (expr, loc);
11238 return expr;
11241 /* Emit an expression as a statement. LOC is the location of the
11242 expression. */
11244 tree
11245 c_finish_expr_stmt (location_t loc, tree expr)
11247 if (expr)
11248 return add_stmt (c_process_expr_stmt (loc, expr));
11249 else
11250 return NULL;
11253 /* Do the opposite and emit a statement as an expression. To begin,
11254 create a new binding level and return it. */
11256 tree
11257 c_begin_stmt_expr (void)
11259 tree ret;
11261 /* We must force a BLOCK for this level so that, if it is not expanded
11262 later, there is a way to turn off the entire subtree of blocks that
11263 are contained in it. */
11264 keep_next_level ();
11265 ret = c_begin_compound_stmt (true);
11267 c_bindings_start_stmt_expr (c_switch_stack == NULL
11268 ? NULL
11269 : c_switch_stack->bindings);
11271 /* Mark the current statement list as belonging to a statement list. */
11272 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11274 return ret;
11277 /* LOC is the location of the compound statement to which this body
11278 belongs. */
11280 tree
11281 c_finish_stmt_expr (location_t loc, tree body)
11283 tree last, type, tmp, val;
11284 tree *last_p;
11286 body = c_end_compound_stmt (loc, body, true);
11288 c_bindings_end_stmt_expr (c_switch_stack == NULL
11289 ? NULL
11290 : c_switch_stack->bindings);
11292 /* Locate the last statement in BODY. See c_end_compound_stmt
11293 about always returning a BIND_EXPR. */
11294 last_p = &BIND_EXPR_BODY (body);
11295 last = BIND_EXPR_BODY (body);
11297 continue_searching:
11298 if (TREE_CODE (last) == STATEMENT_LIST)
11300 tree_stmt_iterator l = tsi_last (last);
11302 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11303 tsi_prev (&l);
11305 /* This can happen with degenerate cases like ({ }). No value. */
11306 if (tsi_end_p (l))
11307 return body;
11309 /* If we're supposed to generate side effects warnings, process
11310 all of the statements except the last. */
11311 if (warn_unused_value)
11313 for (tree_stmt_iterator i = tsi_start (last);
11314 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11316 location_t tloc;
11317 tree t = tsi_stmt (i);
11319 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11320 emit_side_effect_warnings (tloc, t);
11323 last_p = tsi_stmt_ptr (l);
11324 last = *last_p;
11327 /* If the end of the list is exception related, then the list was split
11328 by a call to push_cleanup. Continue searching. */
11329 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11330 || TREE_CODE (last) == TRY_CATCH_EXPR)
11332 last_p = &TREE_OPERAND (last, 0);
11333 last = *last_p;
11334 goto continue_searching;
11337 if (last == error_mark_node)
11338 return last;
11340 /* In the case that the BIND_EXPR is not necessary, return the
11341 expression out from inside it. */
11342 if ((last == BIND_EXPR_BODY (body)
11343 /* Skip nested debug stmts. */
11344 || last == expr_first (BIND_EXPR_BODY (body)))
11345 && BIND_EXPR_VARS (body) == NULL)
11347 /* Even if this looks constant, do not allow it in a constant
11348 expression. */
11349 last = c_wrap_maybe_const (last, true);
11350 /* Do not warn if the return value of a statement expression is
11351 unused. */
11352 TREE_NO_WARNING (last) = 1;
11353 return last;
11356 /* Extract the type of said expression. */
11357 type = TREE_TYPE (last);
11359 /* If we're not returning a value at all, then the BIND_EXPR that
11360 we already have is a fine expression to return. */
11361 if (!type || VOID_TYPE_P (type))
11362 return body;
11364 /* Now that we've located the expression containing the value, it seems
11365 silly to make voidify_wrapper_expr repeat the process. Create a
11366 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11367 tmp = create_tmp_var_raw (type);
11369 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11370 tree_expr_nonnegative_p giving up immediately. */
11371 val = last;
11372 if (TREE_CODE (val) == NOP_EXPR
11373 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11374 val = TREE_OPERAND (val, 0);
11376 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11377 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11380 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11381 SET_EXPR_LOCATION (t, loc);
11382 return t;
11386 /* Begin and end compound statements. This is as simple as pushing
11387 and popping new statement lists from the tree. */
11389 tree
11390 c_begin_compound_stmt (bool do_scope)
11392 tree stmt = push_stmt_list ();
11393 if (do_scope)
11394 push_scope ();
11395 return stmt;
11398 /* End a compound statement. STMT is the statement. LOC is the
11399 location of the compound statement-- this is usually the location
11400 of the opening brace. */
11402 tree
11403 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11405 tree block = NULL;
11407 if (do_scope)
11409 if (c_dialect_objc ())
11410 objc_clear_super_receiver ();
11411 block = pop_scope ();
11414 stmt = pop_stmt_list (stmt);
11415 stmt = c_build_bind_expr (loc, block, stmt);
11417 /* If this compound statement is nested immediately inside a statement
11418 expression, then force a BIND_EXPR to be created. Otherwise we'll
11419 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11420 STATEMENT_LISTs merge, and thus we can lose track of what statement
11421 was really last. */
11422 if (building_stmt_list_p ()
11423 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11424 && TREE_CODE (stmt) != BIND_EXPR)
11426 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11427 TREE_SIDE_EFFECTS (stmt) = 1;
11428 SET_EXPR_LOCATION (stmt, loc);
11431 return stmt;
11434 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11435 when the current scope is exited. EH_ONLY is true when this is not
11436 meant to apply to normal control flow transfer. */
11438 void
11439 push_cleanup (tree decl, tree cleanup, bool eh_only)
11441 enum tree_code code;
11442 tree stmt, list;
11443 bool stmt_expr;
11445 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11446 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11447 add_stmt (stmt);
11448 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11449 list = push_stmt_list ();
11450 TREE_OPERAND (stmt, 0) = list;
11451 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11454 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11455 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11457 static tree
11458 build_vec_cmp (tree_code code, tree type,
11459 tree arg0, tree arg1)
11461 tree zero_vec = build_zero_cst (type);
11462 tree minus_one_vec = build_minus_one_cst (type);
11463 tree cmp_type = truth_type_for (type);
11464 tree cmp = build2 (code, cmp_type, arg0, arg1);
11465 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11468 /* Build a binary-operation expression without default conversions.
11469 CODE is the kind of expression to build.
11470 LOCATION is the operator's location.
11471 This function differs from `build' in several ways:
11472 the data type of the result is computed and recorded in it,
11473 warnings are generated if arg data types are invalid,
11474 special handling for addition and subtraction of pointers is known,
11475 and some optimization is done (operations on narrow ints
11476 are done in the narrower type when that gives the same result).
11477 Constant folding is also done before the result is returned.
11479 Note that the operands will never have enumeral types, or function
11480 or array types, because either they will have the default conversions
11481 performed or they have both just been converted to some other type in which
11482 the arithmetic is to be done. */
11484 tree
11485 build_binary_op (location_t location, enum tree_code code,
11486 tree orig_op0, tree orig_op1, bool convert_p)
11488 tree type0, type1, orig_type0, orig_type1;
11489 tree eptype;
11490 enum tree_code code0, code1;
11491 tree op0, op1;
11492 tree ret = error_mark_node;
11493 const char *invalid_op_diag;
11494 bool op0_int_operands, op1_int_operands;
11495 bool int_const, int_const_or_overflow, int_operands;
11497 /* Expression code to give to the expression when it is built.
11498 Normally this is CODE, which is what the caller asked for,
11499 but in some special cases we change it. */
11500 enum tree_code resultcode = code;
11502 /* Data type in which the computation is to be performed.
11503 In the simplest cases this is the common type of the arguments. */
11504 tree result_type = NULL;
11506 /* When the computation is in excess precision, the type of the
11507 final EXCESS_PRECISION_EXPR. */
11508 tree semantic_result_type = NULL;
11510 /* Nonzero means operands have already been type-converted
11511 in whatever way is necessary.
11512 Zero means they need to be converted to RESULT_TYPE. */
11513 int converted = 0;
11515 /* Nonzero means create the expression with this type, rather than
11516 RESULT_TYPE. */
11517 tree build_type = NULL_TREE;
11519 /* Nonzero means after finally constructing the expression
11520 convert it to this type. */
11521 tree final_type = NULL_TREE;
11523 /* Nonzero if this is an operation like MIN or MAX which can
11524 safely be computed in short if both args are promoted shorts.
11525 Also implies COMMON.
11526 -1 indicates a bitwise operation; this makes a difference
11527 in the exact conditions for when it is safe to do the operation
11528 in a narrower mode. */
11529 int shorten = 0;
11531 /* Nonzero if this is a comparison operation;
11532 if both args are promoted shorts, compare the original shorts.
11533 Also implies COMMON. */
11534 int short_compare = 0;
11536 /* Nonzero if this is a right-shift operation, which can be computed on the
11537 original short and then promoted if the operand is a promoted short. */
11538 int short_shift = 0;
11540 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11541 int common = 0;
11543 /* True means types are compatible as far as ObjC is concerned. */
11544 bool objc_ok;
11546 /* True means this is an arithmetic operation that may need excess
11547 precision. */
11548 bool may_need_excess_precision;
11550 /* True means this is a boolean operation that converts both its
11551 operands to truth-values. */
11552 bool boolean_op = false;
11554 /* Remember whether we're doing / or %. */
11555 bool doing_div_or_mod = false;
11557 /* Remember whether we're doing << or >>. */
11558 bool doing_shift = false;
11560 /* Tree holding instrumentation expression. */
11561 tree instrument_expr = NULL;
11563 if (location == UNKNOWN_LOCATION)
11564 location = input_location;
11566 op0 = orig_op0;
11567 op1 = orig_op1;
11569 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11570 if (op0_int_operands)
11571 op0 = remove_c_maybe_const_expr (op0);
11572 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11573 if (op1_int_operands)
11574 op1 = remove_c_maybe_const_expr (op1);
11575 int_operands = (op0_int_operands && op1_int_operands);
11576 if (int_operands)
11578 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11579 && TREE_CODE (orig_op1) == INTEGER_CST);
11580 int_const = (int_const_or_overflow
11581 && !TREE_OVERFLOW (orig_op0)
11582 && !TREE_OVERFLOW (orig_op1));
11584 else
11585 int_const = int_const_or_overflow = false;
11587 /* Do not apply default conversion in mixed vector/scalar expression. */
11588 if (convert_p
11589 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11591 op0 = default_conversion (op0);
11592 op1 = default_conversion (op1);
11595 orig_type0 = type0 = TREE_TYPE (op0);
11597 orig_type1 = type1 = TREE_TYPE (op1);
11599 /* The expression codes of the data types of the arguments tell us
11600 whether the arguments are integers, floating, pointers, etc. */
11601 code0 = TREE_CODE (type0);
11602 code1 = TREE_CODE (type1);
11604 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11605 STRIP_TYPE_NOPS (op0);
11606 STRIP_TYPE_NOPS (op1);
11608 /* If an error was already reported for one of the arguments,
11609 avoid reporting another error. */
11611 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11612 return error_mark_node;
11614 if (code0 == POINTER_TYPE
11615 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11616 return error_mark_node;
11618 if (code1 == POINTER_TYPE
11619 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11620 return error_mark_node;
11622 if ((invalid_op_diag
11623 = targetm.invalid_binary_op (code, type0, type1)))
11625 error_at (location, invalid_op_diag);
11626 return error_mark_node;
11629 switch (code)
11631 case PLUS_EXPR:
11632 case MINUS_EXPR:
11633 case MULT_EXPR:
11634 case TRUNC_DIV_EXPR:
11635 case CEIL_DIV_EXPR:
11636 case FLOOR_DIV_EXPR:
11637 case ROUND_DIV_EXPR:
11638 case EXACT_DIV_EXPR:
11639 may_need_excess_precision = true;
11640 break;
11642 case EQ_EXPR:
11643 case NE_EXPR:
11644 case LE_EXPR:
11645 case GE_EXPR:
11646 case LT_EXPR:
11647 case GT_EXPR:
11648 /* Excess precision for implicit conversions of integers to
11649 floating point in C11 and later. */
11650 may_need_excess_precision = (flag_isoc11
11651 && (ANY_INTEGRAL_TYPE_P (type0)
11652 || ANY_INTEGRAL_TYPE_P (type1)));
11653 break;
11655 default:
11656 may_need_excess_precision = false;
11657 break;
11659 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11661 op0 = TREE_OPERAND (op0, 0);
11662 type0 = TREE_TYPE (op0);
11664 else if (may_need_excess_precision
11665 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11667 type0 = eptype;
11668 op0 = convert (eptype, op0);
11670 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11672 op1 = TREE_OPERAND (op1, 0);
11673 type1 = TREE_TYPE (op1);
11675 else if (may_need_excess_precision
11676 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11678 type1 = eptype;
11679 op1 = convert (eptype, op1);
11682 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11684 /* In case when one of the operands of the binary operation is
11685 a vector and another is a scalar -- convert scalar to vector. */
11686 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11687 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11689 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11690 true);
11692 switch (convert_flag)
11694 case stv_error:
11695 return error_mark_node;
11696 case stv_firstarg:
11698 bool maybe_const = true;
11699 tree sc;
11700 sc = c_fully_fold (op0, false, &maybe_const);
11701 sc = save_expr (sc);
11702 sc = convert (TREE_TYPE (type1), sc);
11703 op0 = build_vector_from_val (type1, sc);
11704 if (!maybe_const)
11705 op0 = c_wrap_maybe_const (op0, true);
11706 orig_type0 = type0 = TREE_TYPE (op0);
11707 code0 = TREE_CODE (type0);
11708 converted = 1;
11709 break;
11711 case stv_secondarg:
11713 bool maybe_const = true;
11714 tree sc;
11715 sc = c_fully_fold (op1, false, &maybe_const);
11716 sc = save_expr (sc);
11717 sc = convert (TREE_TYPE (type0), sc);
11718 op1 = build_vector_from_val (type0, sc);
11719 if (!maybe_const)
11720 op1 = c_wrap_maybe_const (op1, true);
11721 orig_type1 = type1 = TREE_TYPE (op1);
11722 code1 = TREE_CODE (type1);
11723 converted = 1;
11724 break;
11726 default:
11727 break;
11731 switch (code)
11733 case PLUS_EXPR:
11734 /* Handle the pointer + int case. */
11735 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11737 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11738 goto return_build_binary_op;
11740 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11742 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11743 goto return_build_binary_op;
11745 else
11746 common = 1;
11747 break;
11749 case MINUS_EXPR:
11750 /* Subtraction of two similar pointers.
11751 We must subtract them as integers, then divide by object size. */
11752 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11753 && comp_target_types (location, type0, type1))
11755 ret = pointer_diff (location, op0, op1, &instrument_expr);
11756 goto return_build_binary_op;
11758 /* Handle pointer minus int. Just like pointer plus int. */
11759 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11761 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11762 goto return_build_binary_op;
11764 else
11765 common = 1;
11766 break;
11768 case MULT_EXPR:
11769 common = 1;
11770 break;
11772 case TRUNC_DIV_EXPR:
11773 case CEIL_DIV_EXPR:
11774 case FLOOR_DIV_EXPR:
11775 case ROUND_DIV_EXPR:
11776 case EXACT_DIV_EXPR:
11777 doing_div_or_mod = true;
11778 warn_for_div_by_zero (location, op1);
11780 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11781 || code0 == FIXED_POINT_TYPE
11782 || code0 == COMPLEX_TYPE
11783 || gnu_vector_type_p (type0))
11784 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11785 || code1 == FIXED_POINT_TYPE
11786 || code1 == COMPLEX_TYPE
11787 || gnu_vector_type_p (type1)))
11789 enum tree_code tcode0 = code0, tcode1 = code1;
11791 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11792 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11793 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11794 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11796 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11797 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11798 resultcode = RDIV_EXPR;
11799 else
11800 /* Although it would be tempting to shorten always here, that
11801 loses on some targets, since the modulo instruction is
11802 undefined if the quotient can't be represented in the
11803 computation mode. We shorten only if unsigned or if
11804 dividing by something we know != -1. */
11805 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11806 || (TREE_CODE (op1) == INTEGER_CST
11807 && !integer_all_onesp (op1)));
11808 common = 1;
11810 break;
11812 case BIT_AND_EXPR:
11813 case BIT_IOR_EXPR:
11814 case BIT_XOR_EXPR:
11815 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11816 shorten = -1;
11817 /* Allow vector types which are not floating point types. */
11818 else if (gnu_vector_type_p (type0)
11819 && gnu_vector_type_p (type1)
11820 && !VECTOR_FLOAT_TYPE_P (type0)
11821 && !VECTOR_FLOAT_TYPE_P (type1))
11822 common = 1;
11823 break;
11825 case TRUNC_MOD_EXPR:
11826 case FLOOR_MOD_EXPR:
11827 doing_div_or_mod = true;
11828 warn_for_div_by_zero (location, op1);
11830 if (gnu_vector_type_p (type0)
11831 && gnu_vector_type_p (type1)
11832 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11833 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11834 common = 1;
11835 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11837 /* Although it would be tempting to shorten always here, that loses
11838 on some targets, since the modulo instruction is undefined if the
11839 quotient can't be represented in the computation mode. We shorten
11840 only if unsigned or if dividing by something we know != -1. */
11841 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11842 || (TREE_CODE (op1) == INTEGER_CST
11843 && !integer_all_onesp (op1)));
11844 common = 1;
11846 break;
11848 case TRUTH_ANDIF_EXPR:
11849 case TRUTH_ORIF_EXPR:
11850 case TRUTH_AND_EXPR:
11851 case TRUTH_OR_EXPR:
11852 case TRUTH_XOR_EXPR:
11853 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11854 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11855 || code0 == FIXED_POINT_TYPE)
11856 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11857 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11858 || code1 == FIXED_POINT_TYPE))
11860 /* Result of these operations is always an int,
11861 but that does not mean the operands should be
11862 converted to ints! */
11863 result_type = integer_type_node;
11864 if (op0_int_operands)
11866 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11867 op0 = remove_c_maybe_const_expr (op0);
11869 else
11870 op0 = c_objc_common_truthvalue_conversion (location, op0);
11871 if (op1_int_operands)
11873 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11874 op1 = remove_c_maybe_const_expr (op1);
11876 else
11877 op1 = c_objc_common_truthvalue_conversion (location, op1);
11878 converted = 1;
11879 boolean_op = true;
11881 if (code == TRUTH_ANDIF_EXPR)
11883 int_const_or_overflow = (int_operands
11884 && TREE_CODE (orig_op0) == INTEGER_CST
11885 && (op0 == truthvalue_false_node
11886 || TREE_CODE (orig_op1) == INTEGER_CST));
11887 int_const = (int_const_or_overflow
11888 && !TREE_OVERFLOW (orig_op0)
11889 && (op0 == truthvalue_false_node
11890 || !TREE_OVERFLOW (orig_op1)));
11892 else if (code == TRUTH_ORIF_EXPR)
11894 int_const_or_overflow = (int_operands
11895 && TREE_CODE (orig_op0) == INTEGER_CST
11896 && (op0 == truthvalue_true_node
11897 || TREE_CODE (orig_op1) == INTEGER_CST));
11898 int_const = (int_const_or_overflow
11899 && !TREE_OVERFLOW (orig_op0)
11900 && (op0 == truthvalue_true_node
11901 || !TREE_OVERFLOW (orig_op1)));
11903 break;
11905 /* Shift operations: result has same type as first operand;
11906 always convert second operand to int.
11907 Also set SHORT_SHIFT if shifting rightward. */
11909 case RSHIFT_EXPR:
11910 if (gnu_vector_type_p (type0)
11911 && gnu_vector_type_p (type1)
11912 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11913 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11914 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11915 TYPE_VECTOR_SUBPARTS (type1)))
11917 result_type = type0;
11918 converted = 1;
11920 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11921 || (gnu_vector_type_p (type0)
11922 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11923 && code1 == INTEGER_TYPE)
11925 doing_shift = true;
11926 if (TREE_CODE (op1) == INTEGER_CST)
11928 if (tree_int_cst_sgn (op1) < 0)
11930 int_const = false;
11931 if (c_inhibit_evaluation_warnings == 0)
11932 warning_at (location, OPT_Wshift_count_negative,
11933 "right shift count is negative");
11935 else if (code0 == VECTOR_TYPE)
11937 if (compare_tree_int (op1,
11938 TYPE_PRECISION (TREE_TYPE (type0)))
11939 >= 0)
11941 int_const = false;
11942 if (c_inhibit_evaluation_warnings == 0)
11943 warning_at (location, OPT_Wshift_count_overflow,
11944 "right shift count >= width of vector element");
11947 else
11949 if (!integer_zerop (op1))
11950 short_shift = 1;
11952 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11954 int_const = false;
11955 if (c_inhibit_evaluation_warnings == 0)
11956 warning_at (location, OPT_Wshift_count_overflow,
11957 "right shift count >= width of type");
11962 /* Use the type of the value to be shifted. */
11963 result_type = type0;
11964 /* Avoid converting op1 to result_type later. */
11965 converted = 1;
11967 break;
11969 case LSHIFT_EXPR:
11970 if (gnu_vector_type_p (type0)
11971 && gnu_vector_type_p (type1)
11972 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11973 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11974 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11975 TYPE_VECTOR_SUBPARTS (type1)))
11977 result_type = type0;
11978 converted = 1;
11980 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11981 || (gnu_vector_type_p (type0)
11982 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11983 && code1 == INTEGER_TYPE)
11985 doing_shift = true;
11986 if (TREE_CODE (op0) == INTEGER_CST
11987 && tree_int_cst_sgn (op0) < 0)
11989 /* Don't reject a left shift of a negative value in a context
11990 where a constant expression is needed in C90. */
11991 if (flag_isoc99)
11992 int_const = false;
11993 if (c_inhibit_evaluation_warnings == 0)
11994 warning_at (location, OPT_Wshift_negative_value,
11995 "left shift of negative value");
11997 if (TREE_CODE (op1) == INTEGER_CST)
11999 if (tree_int_cst_sgn (op1) < 0)
12001 int_const = false;
12002 if (c_inhibit_evaluation_warnings == 0)
12003 warning_at (location, OPT_Wshift_count_negative,
12004 "left shift count is negative");
12006 else if (code0 == VECTOR_TYPE)
12008 if (compare_tree_int (op1,
12009 TYPE_PRECISION (TREE_TYPE (type0)))
12010 >= 0)
12012 int_const = false;
12013 if (c_inhibit_evaluation_warnings == 0)
12014 warning_at (location, OPT_Wshift_count_overflow,
12015 "left shift count >= width of vector element");
12018 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12020 int_const = false;
12021 if (c_inhibit_evaluation_warnings == 0)
12022 warning_at (location, OPT_Wshift_count_overflow,
12023 "left shift count >= width of type");
12025 else if (TREE_CODE (op0) == INTEGER_CST
12026 && maybe_warn_shift_overflow (location, op0, op1)
12027 && flag_isoc99)
12028 int_const = false;
12031 /* Use the type of the value to be shifted. */
12032 result_type = type0;
12033 /* Avoid converting op1 to result_type later. */
12034 converted = 1;
12036 break;
12038 case EQ_EXPR:
12039 case NE_EXPR:
12040 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12042 tree intt;
12043 if (!vector_types_compatible_elements_p (type0, type1))
12045 error_at (location, "comparing vectors with different "
12046 "element types");
12047 return error_mark_node;
12050 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12051 TYPE_VECTOR_SUBPARTS (type1)))
12053 error_at (location, "comparing vectors with different "
12054 "number of elements");
12055 return error_mark_node;
12058 /* It's not precisely specified how the usual arithmetic
12059 conversions apply to the vector types. Here, we use
12060 the unsigned type if one of the operands is signed and
12061 the other one is unsigned. */
12062 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12064 if (!TYPE_UNSIGNED (type0))
12065 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12066 else
12067 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12068 warning_at (location, OPT_Wsign_compare, "comparison between "
12069 "types %qT and %qT", type0, type1);
12072 /* Always construct signed integer vector type. */
12073 intt = c_common_type_for_size (GET_MODE_BITSIZE
12074 (SCALAR_TYPE_MODE
12075 (TREE_TYPE (type0))), 0);
12076 if (!intt)
12078 error_at (location, "could not find an integer type "
12079 "of the same size as %qT",
12080 TREE_TYPE (type0));
12081 return error_mark_node;
12083 result_type = build_opaque_vector_type (intt,
12084 TYPE_VECTOR_SUBPARTS (type0));
12085 converted = 1;
12086 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12087 goto return_build_binary_op;
12089 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12090 warning_at (location,
12091 OPT_Wfloat_equal,
12092 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12093 /* Result of comparison is always int,
12094 but don't convert the args to int! */
12095 build_type = integer_type_node;
12096 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12097 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12098 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12099 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12100 short_compare = 1;
12101 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12103 if (TREE_CODE (op0) == ADDR_EXPR
12104 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
12105 && !from_macro_expansion_at (location))
12107 if (code == EQ_EXPR)
12108 warning_at (location,
12109 OPT_Waddress,
12110 "the comparison will always evaluate as %<false%> "
12111 "for the address of %qD will never be NULL",
12112 TREE_OPERAND (op0, 0));
12113 else
12114 warning_at (location,
12115 OPT_Waddress,
12116 "the comparison will always evaluate as %<true%> "
12117 "for the address of %qD will never be NULL",
12118 TREE_OPERAND (op0, 0));
12120 result_type = type0;
12122 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12124 if (TREE_CODE (op1) == ADDR_EXPR
12125 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
12126 && !from_macro_expansion_at (location))
12128 if (code == EQ_EXPR)
12129 warning_at (location,
12130 OPT_Waddress,
12131 "the comparison will always evaluate as %<false%> "
12132 "for the address of %qD will never be NULL",
12133 TREE_OPERAND (op1, 0));
12134 else
12135 warning_at (location,
12136 OPT_Waddress,
12137 "the comparison will always evaluate as %<true%> "
12138 "for the address of %qD will never be NULL",
12139 TREE_OPERAND (op1, 0));
12141 result_type = type1;
12143 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12145 tree tt0 = TREE_TYPE (type0);
12146 tree tt1 = TREE_TYPE (type1);
12147 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12148 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12149 addr_space_t as_common = ADDR_SPACE_GENERIC;
12151 /* Anything compares with void *. void * compares with anything.
12152 Otherwise, the targets must be compatible
12153 and both must be object or both incomplete. */
12154 if (comp_target_types (location, type0, type1))
12155 result_type = common_pointer_type (type0, type1);
12156 else if (!addr_space_superset (as0, as1, &as_common))
12158 error_at (location, "comparison of pointers to "
12159 "disjoint address spaces");
12160 return error_mark_node;
12162 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12164 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12165 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12166 "comparison of %<void *%> with function pointer");
12168 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12170 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12171 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12172 "comparison of %<void *%> with function pointer");
12174 else
12175 /* Avoid warning about the volatile ObjC EH puts on decls. */
12176 if (!objc_ok)
12177 pedwarn (location, 0,
12178 "comparison of distinct pointer types lacks a cast");
12180 if (result_type == NULL_TREE)
12182 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12183 result_type = build_pointer_type
12184 (build_qualified_type (void_type_node, qual));
12187 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12189 result_type = type0;
12190 pedwarn (location, 0, "comparison between pointer and integer");
12192 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12194 result_type = type1;
12195 pedwarn (location, 0, "comparison between pointer and integer");
12197 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12198 || truth_value_p (TREE_CODE (orig_op0)))
12199 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12200 || truth_value_p (TREE_CODE (orig_op1))))
12201 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12202 break;
12204 case LE_EXPR:
12205 case GE_EXPR:
12206 case LT_EXPR:
12207 case GT_EXPR:
12208 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12210 tree intt;
12211 if (!vector_types_compatible_elements_p (type0, type1))
12213 error_at (location, "comparing vectors with different "
12214 "element types");
12215 return error_mark_node;
12218 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12219 TYPE_VECTOR_SUBPARTS (type1)))
12221 error_at (location, "comparing vectors with different "
12222 "number of elements");
12223 return error_mark_node;
12226 /* It's not precisely specified how the usual arithmetic
12227 conversions apply to the vector types. Here, we use
12228 the unsigned type if one of the operands is signed and
12229 the other one is unsigned. */
12230 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12232 if (!TYPE_UNSIGNED (type0))
12233 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12234 else
12235 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12236 warning_at (location, OPT_Wsign_compare, "comparison between "
12237 "types %qT and %qT", type0, type1);
12240 /* Always construct signed integer vector type. */
12241 intt = c_common_type_for_size (GET_MODE_BITSIZE
12242 (SCALAR_TYPE_MODE
12243 (TREE_TYPE (type0))), 0);
12244 if (!intt)
12246 error_at (location, "could not find an integer type "
12247 "of the same size as %qT",
12248 TREE_TYPE (type0));
12249 return error_mark_node;
12251 result_type = build_opaque_vector_type (intt,
12252 TYPE_VECTOR_SUBPARTS (type0));
12253 converted = 1;
12254 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12255 goto return_build_binary_op;
12257 build_type = integer_type_node;
12258 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12259 || code0 == FIXED_POINT_TYPE)
12260 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12261 || code1 == FIXED_POINT_TYPE))
12262 short_compare = 1;
12263 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12265 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12266 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12267 addr_space_t as_common;
12269 if (comp_target_types (location, type0, type1))
12271 result_type = common_pointer_type (type0, type1);
12272 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12273 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12274 pedwarn (location, 0,
12275 "comparison of complete and incomplete pointers");
12276 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12277 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12278 "ordered comparisons of pointers to functions");
12279 else if (null_pointer_constant_p (orig_op0)
12280 || null_pointer_constant_p (orig_op1))
12281 warning_at (location, OPT_Wextra,
12282 "ordered comparison of pointer with null pointer");
12285 else if (!addr_space_superset (as0, as1, &as_common))
12287 error_at (location, "comparison of pointers to "
12288 "disjoint address spaces");
12289 return error_mark_node;
12291 else
12293 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12294 result_type = build_pointer_type
12295 (build_qualified_type (void_type_node, qual));
12296 pedwarn (location, 0,
12297 "comparison of distinct pointer types lacks a cast");
12300 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12302 result_type = type0;
12303 if (pedantic)
12304 pedwarn (location, OPT_Wpedantic,
12305 "ordered comparison of pointer with integer zero");
12306 else if (extra_warnings)
12307 warning_at (location, OPT_Wextra,
12308 "ordered comparison of pointer with integer zero");
12310 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12312 result_type = type1;
12313 if (pedantic)
12314 pedwarn (location, OPT_Wpedantic,
12315 "ordered comparison of pointer with integer zero");
12316 else if (extra_warnings)
12317 warning_at (location, OPT_Wextra,
12318 "ordered comparison of pointer with integer zero");
12320 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12322 result_type = type0;
12323 pedwarn (location, 0, "comparison between pointer and integer");
12325 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12327 result_type = type1;
12328 pedwarn (location, 0, "comparison between pointer and integer");
12331 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12332 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12334 op0 = save_expr (op0);
12335 op1 = save_expr (op1);
12337 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12338 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12341 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12342 || truth_value_p (TREE_CODE (orig_op0)))
12343 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12344 || truth_value_p (TREE_CODE (orig_op1))))
12345 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12346 break;
12348 default:
12349 gcc_unreachable ();
12352 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12353 return error_mark_node;
12355 if (gnu_vector_type_p (type0)
12356 && gnu_vector_type_p (type1)
12357 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12358 || !vector_types_compatible_elements_p (type0, type1)))
12360 gcc_rich_location richloc (location);
12361 maybe_range_label_for_tree_type_mismatch
12362 label_for_op0 (orig_op0, orig_op1),
12363 label_for_op1 (orig_op1, orig_op0);
12364 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12365 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12366 binary_op_error (&richloc, code, type0, type1);
12367 return error_mark_node;
12370 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12371 || code0 == FIXED_POINT_TYPE
12372 || gnu_vector_type_p (type0))
12374 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12375 || code1 == FIXED_POINT_TYPE
12376 || gnu_vector_type_p (type1)))
12378 bool first_complex = (code0 == COMPLEX_TYPE);
12379 bool second_complex = (code1 == COMPLEX_TYPE);
12380 int none_complex = (!first_complex && !second_complex);
12382 if (shorten || common || short_compare)
12384 result_type = c_common_type (type0, type1);
12385 do_warn_double_promotion (result_type, type0, type1,
12386 "implicit conversion from %qT to %qT "
12387 "to match other operand of binary "
12388 "expression",
12389 location);
12390 if (result_type == error_mark_node)
12391 return error_mark_node;
12394 if (first_complex != second_complex
12395 && (code == PLUS_EXPR
12396 || code == MINUS_EXPR
12397 || code == MULT_EXPR
12398 || (code == TRUNC_DIV_EXPR && first_complex))
12399 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12400 && flag_signed_zeros)
12402 /* An operation on mixed real/complex operands must be
12403 handled specially, but the language-independent code can
12404 more easily optimize the plain complex arithmetic if
12405 -fno-signed-zeros. */
12406 tree real_type = TREE_TYPE (result_type);
12407 tree real, imag;
12408 if (type0 != orig_type0 || type1 != orig_type1)
12410 gcc_assert (may_need_excess_precision && common);
12411 semantic_result_type = c_common_type (orig_type0, orig_type1);
12413 if (first_complex)
12415 if (TREE_TYPE (op0) != result_type)
12416 op0 = convert_and_check (location, result_type, op0);
12417 if (TREE_TYPE (op1) != real_type)
12418 op1 = convert_and_check (location, real_type, op1);
12420 else
12422 if (TREE_TYPE (op0) != real_type)
12423 op0 = convert_and_check (location, real_type, op0);
12424 if (TREE_TYPE (op1) != result_type)
12425 op1 = convert_and_check (location, result_type, op1);
12427 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12428 return error_mark_node;
12429 if (first_complex)
12431 op0 = save_expr (op0);
12432 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12433 op0, true);
12434 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12435 op0, true);
12436 switch (code)
12438 case MULT_EXPR:
12439 case TRUNC_DIV_EXPR:
12440 op1 = save_expr (op1);
12441 imag = build2 (resultcode, real_type, imag, op1);
12442 /* Fall through. */
12443 case PLUS_EXPR:
12444 case MINUS_EXPR:
12445 real = build2 (resultcode, real_type, real, op1);
12446 break;
12447 default:
12448 gcc_unreachable();
12451 else
12453 op1 = save_expr (op1);
12454 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12455 op1, true);
12456 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12457 op1, true);
12458 switch (code)
12460 case MULT_EXPR:
12461 op0 = save_expr (op0);
12462 imag = build2 (resultcode, real_type, op0, imag);
12463 /* Fall through. */
12464 case PLUS_EXPR:
12465 real = build2 (resultcode, real_type, op0, real);
12466 break;
12467 case MINUS_EXPR:
12468 real = build2 (resultcode, real_type, op0, real);
12469 imag = build1 (NEGATE_EXPR, real_type, imag);
12470 break;
12471 default:
12472 gcc_unreachable();
12475 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12476 goto return_build_binary_op;
12479 /* For certain operations (which identify themselves by shorten != 0)
12480 if both args were extended from the same smaller type,
12481 do the arithmetic in that type and then extend.
12483 shorten !=0 and !=1 indicates a bitwise operation.
12484 For them, this optimization is safe only if
12485 both args are zero-extended or both are sign-extended.
12486 Otherwise, we might change the result.
12487 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12488 but calculated in (unsigned short) it would be (unsigned short)-1. */
12490 if (shorten && none_complex)
12492 final_type = result_type;
12493 result_type = shorten_binary_op (result_type, op0, op1,
12494 shorten == -1);
12497 /* Shifts can be shortened if shifting right. */
12499 if (short_shift)
12501 int unsigned_arg;
12502 tree arg0 = get_narrower (op0, &unsigned_arg);
12504 final_type = result_type;
12506 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12507 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12509 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12510 && tree_int_cst_sgn (op1) > 0
12511 /* We can shorten only if the shift count is less than the
12512 number of bits in the smaller type size. */
12513 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12514 /* We cannot drop an unsigned shift after sign-extension. */
12515 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12517 /* Do an unsigned shift if the operand was zero-extended. */
12518 result_type
12519 = c_common_signed_or_unsigned_type (unsigned_arg,
12520 TREE_TYPE (arg0));
12521 /* Convert value-to-be-shifted to that type. */
12522 if (TREE_TYPE (op0) != result_type)
12523 op0 = convert (result_type, op0);
12524 converted = 1;
12528 /* Comparison operations are shortened too but differently.
12529 They identify themselves by setting short_compare = 1. */
12531 if (short_compare)
12533 /* Don't write &op0, etc., because that would prevent op0
12534 from being kept in a register.
12535 Instead, make copies of the our local variables and
12536 pass the copies by reference, then copy them back afterward. */
12537 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12538 enum tree_code xresultcode = resultcode;
12539 tree val
12540 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12541 &xresultcode);
12543 if (val != NULL_TREE)
12545 ret = val;
12546 goto return_build_binary_op;
12549 op0 = xop0, op1 = xop1;
12550 converted = 1;
12551 resultcode = xresultcode;
12553 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12555 bool op0_maybe_const = true;
12556 bool op1_maybe_const = true;
12557 tree orig_op0_folded, orig_op1_folded;
12559 if (in_late_binary_op)
12561 orig_op0_folded = orig_op0;
12562 orig_op1_folded = orig_op1;
12564 else
12566 /* Fold for the sake of possible warnings, as in
12567 build_conditional_expr. This requires the
12568 "original" values to be folded, not just op0 and
12569 op1. */
12570 c_inhibit_evaluation_warnings++;
12571 op0 = c_fully_fold (op0, require_constant_value,
12572 &op0_maybe_const);
12573 op1 = c_fully_fold (op1, require_constant_value,
12574 &op1_maybe_const);
12575 c_inhibit_evaluation_warnings--;
12576 orig_op0_folded = c_fully_fold (orig_op0,
12577 require_constant_value,
12578 NULL);
12579 orig_op1_folded = c_fully_fold (orig_op1,
12580 require_constant_value,
12581 NULL);
12584 if (warn_sign_compare)
12585 warn_for_sign_compare (location, orig_op0_folded,
12586 orig_op1_folded, op0, op1,
12587 result_type, resultcode);
12588 if (!in_late_binary_op && !int_operands)
12590 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12591 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12592 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12593 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12599 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12600 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12601 Then the expression will be built.
12602 It will be given type FINAL_TYPE if that is nonzero;
12603 otherwise, it will be given type RESULT_TYPE. */
12605 if (!result_type)
12607 /* Favor showing any expression locations that are available. */
12608 op_location_t oploc (location, UNKNOWN_LOCATION);
12609 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12610 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12611 return error_mark_node;
12614 if (build_type == NULL_TREE)
12616 build_type = result_type;
12617 if ((type0 != orig_type0 || type1 != orig_type1)
12618 && !boolean_op)
12620 gcc_assert (may_need_excess_precision && common);
12621 semantic_result_type = c_common_type (orig_type0, orig_type1);
12625 if (!converted)
12627 op0 = ep_convert_and_check (location, result_type, op0,
12628 semantic_result_type);
12629 op1 = ep_convert_and_check (location, result_type, op1,
12630 semantic_result_type);
12632 /* This can happen if one operand has a vector type, and the other
12633 has a different type. */
12634 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12635 return error_mark_node;
12638 if (sanitize_flags_p ((SANITIZE_SHIFT
12639 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12640 && current_function_decl != NULL_TREE
12641 && (doing_div_or_mod || doing_shift)
12642 && !require_constant_value)
12644 /* OP0 and/or OP1 might have side-effects. */
12645 op0 = save_expr (op0);
12646 op1 = save_expr (op1);
12647 op0 = c_fully_fold (op0, false, NULL);
12648 op1 = c_fully_fold (op1, false, NULL);
12649 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12650 | SANITIZE_FLOAT_DIVIDE))))
12651 instrument_expr = ubsan_instrument_division (location, op0, op1);
12652 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12653 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12656 /* Treat expressions in initializers specially as they can't trap. */
12657 if (int_const_or_overflow)
12658 ret = (require_constant_value
12659 ? fold_build2_initializer_loc (location, resultcode, build_type,
12660 op0, op1)
12661 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12662 else
12663 ret = build2 (resultcode, build_type, op0, op1);
12664 if (final_type != NULL_TREE)
12665 ret = convert (final_type, ret);
12667 return_build_binary_op:
12668 gcc_assert (ret != error_mark_node);
12669 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12670 ret = (int_operands
12671 ? note_integer_operands (ret)
12672 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12673 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12674 && !in_late_binary_op)
12675 ret = note_integer_operands (ret);
12676 protected_set_expr_location (ret, location);
12678 if (instrument_expr != NULL)
12679 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12680 instrument_expr, ret);
12682 if (semantic_result_type)
12683 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12684 semantic_result_type, ret);
12686 return ret;
12690 /* Convert EXPR to be a truth-value, validating its type for this
12691 purpose. LOCATION is the source location for the expression. */
12693 tree
12694 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12696 bool int_const, int_operands;
12698 switch (TREE_CODE (TREE_TYPE (expr)))
12700 case ARRAY_TYPE:
12701 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12702 return error_mark_node;
12704 case RECORD_TYPE:
12705 error_at (location, "used struct type value where scalar is required");
12706 return error_mark_node;
12708 case UNION_TYPE:
12709 error_at (location, "used union type value where scalar is required");
12710 return error_mark_node;
12712 case VOID_TYPE:
12713 error_at (location, "void value not ignored as it ought to be");
12714 return error_mark_node;
12716 case POINTER_TYPE:
12717 if (reject_gcc_builtin (expr))
12718 return error_mark_node;
12719 break;
12721 case FUNCTION_TYPE:
12722 gcc_unreachable ();
12724 case VECTOR_TYPE:
12725 error_at (location, "used vector type where scalar is required");
12726 return error_mark_node;
12728 default:
12729 break;
12732 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12733 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12734 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12736 expr = remove_c_maybe_const_expr (expr);
12737 expr = build2 (NE_EXPR, integer_type_node, expr,
12738 convert (TREE_TYPE (expr), integer_zero_node));
12739 expr = note_integer_operands (expr);
12741 else
12742 /* ??? Should we also give an error for vectors rather than leaving
12743 those to give errors later? */
12744 expr = c_common_truthvalue_conversion (location, expr);
12746 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12748 if (TREE_OVERFLOW (expr))
12749 return expr;
12750 else
12751 return note_integer_operands (expr);
12753 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12754 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12755 return expr;
12759 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12760 required. */
12762 tree
12763 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12765 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12767 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12768 /* Executing a compound literal inside a function reinitializes
12769 it. */
12770 if (!TREE_STATIC (decl))
12771 *se = true;
12772 return decl;
12774 else
12775 return expr;
12778 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12779 statement. LOC is the location of the construct. */
12781 tree
12782 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12783 tree clauses)
12785 body = c_end_compound_stmt (loc, body, true);
12787 tree stmt = make_node (code);
12788 TREE_TYPE (stmt) = void_type_node;
12789 OMP_BODY (stmt) = body;
12790 OMP_CLAUSES (stmt) = clauses;
12791 SET_EXPR_LOCATION (stmt, loc);
12793 return add_stmt (stmt);
12796 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12797 statement. LOC is the location of the OACC_DATA. */
12799 tree
12800 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12802 tree stmt;
12804 block = c_end_compound_stmt (loc, block, true);
12806 stmt = make_node (OACC_DATA);
12807 TREE_TYPE (stmt) = void_type_node;
12808 OACC_DATA_CLAUSES (stmt) = clauses;
12809 OACC_DATA_BODY (stmt) = block;
12810 SET_EXPR_LOCATION (stmt, loc);
12812 return add_stmt (stmt);
12815 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12816 statement. LOC is the location of the OACC_HOST_DATA. */
12818 tree
12819 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12821 tree stmt;
12823 block = c_end_compound_stmt (loc, block, true);
12825 stmt = make_node (OACC_HOST_DATA);
12826 TREE_TYPE (stmt) = void_type_node;
12827 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12828 OACC_HOST_DATA_BODY (stmt) = block;
12829 SET_EXPR_LOCATION (stmt, loc);
12831 return add_stmt (stmt);
12834 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12836 tree
12837 c_begin_omp_parallel (void)
12839 tree block;
12841 keep_next_level ();
12842 block = c_begin_compound_stmt (true);
12844 return block;
12847 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12848 statement. LOC is the location of the OMP_PARALLEL. */
12850 tree
12851 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12853 tree stmt;
12855 block = c_end_compound_stmt (loc, block, true);
12857 stmt = make_node (OMP_PARALLEL);
12858 TREE_TYPE (stmt) = void_type_node;
12859 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12860 OMP_PARALLEL_BODY (stmt) = block;
12861 SET_EXPR_LOCATION (stmt, loc);
12863 return add_stmt (stmt);
12866 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12868 tree
12869 c_begin_omp_task (void)
12871 tree block;
12873 keep_next_level ();
12874 block = c_begin_compound_stmt (true);
12876 return block;
12879 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12880 statement. LOC is the location of the #pragma. */
12882 tree
12883 c_finish_omp_task (location_t loc, tree clauses, tree block)
12885 tree stmt;
12887 block = c_end_compound_stmt (loc, block, true);
12889 stmt = make_node (OMP_TASK);
12890 TREE_TYPE (stmt) = void_type_node;
12891 OMP_TASK_CLAUSES (stmt) = clauses;
12892 OMP_TASK_BODY (stmt) = block;
12893 SET_EXPR_LOCATION (stmt, loc);
12895 return add_stmt (stmt);
12898 /* Generate GOMP_cancel call for #pragma omp cancel. */
12900 void
12901 c_finish_omp_cancel (location_t loc, tree clauses)
12903 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12904 int mask = 0;
12905 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12906 mask = 1;
12907 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12908 mask = 2;
12909 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12910 mask = 4;
12911 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12912 mask = 8;
12913 else
12915 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12916 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12917 "clauses");
12918 return;
12920 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12921 if (ifc != NULL_TREE)
12923 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12924 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12925 error_at (OMP_CLAUSE_LOCATION (ifc),
12926 "expected %<cancel%> %<if%> clause modifier");
12927 else
12929 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
12930 if (ifc2 != NULL_TREE)
12932 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
12933 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
12934 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
12935 error_at (OMP_CLAUSE_LOCATION (ifc2),
12936 "expected %<cancel%> %<if%> clause modifier");
12940 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12941 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12942 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12943 build_zero_cst (type));
12945 else
12946 ifc = boolean_true_node;
12947 tree stmt = build_call_expr_loc (loc, fn, 2,
12948 build_int_cst (integer_type_node, mask),
12949 ifc);
12950 add_stmt (stmt);
12953 /* Generate GOMP_cancellation_point call for
12954 #pragma omp cancellation point. */
12956 void
12957 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12959 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12960 int mask = 0;
12961 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12962 mask = 1;
12963 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12964 mask = 2;
12965 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12966 mask = 4;
12967 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12968 mask = 8;
12969 else
12971 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12972 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12973 "clauses");
12974 return;
12976 tree stmt = build_call_expr_loc (loc, fn, 1,
12977 build_int_cst (integer_type_node, mask));
12978 add_stmt (stmt);
12981 /* Helper function for handle_omp_array_sections. Called recursively
12982 to handle multiple array-section-subscripts. C is the clause,
12983 T current expression (initially OMP_CLAUSE_DECL), which is either
12984 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12985 expression if specified, TREE_VALUE length expression if specified,
12986 TREE_CHAIN is what it has been specified after, or some decl.
12987 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12988 set to true if any of the array-section-subscript could have length
12989 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12990 first array-section-subscript which is known not to have length
12991 of one. Given say:
12992 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12993 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12994 all are or may have length of 1, array-section-subscript [:2] is the
12995 first one known not to have length 1. For array-section-subscript
12996 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12997 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12998 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12999 case though, as some lengths could be zero. */
13001 static tree
13002 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13003 bool &maybe_zero_len, unsigned int &first_non_one,
13004 enum c_omp_region_type ort)
13006 tree ret, low_bound, length, type;
13007 if (TREE_CODE (t) != TREE_LIST)
13009 if (error_operand_p (t))
13010 return error_mark_node;
13011 ret = t;
13012 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13013 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13015 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13016 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13017 return error_mark_node;
13019 if (TREE_CODE (t) == COMPONENT_REF
13020 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13021 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13022 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13024 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13026 error_at (OMP_CLAUSE_LOCATION (c),
13027 "bit-field %qE in %qs clause",
13028 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13029 return error_mark_node;
13031 while (TREE_CODE (t) == COMPONENT_REF)
13033 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13035 error_at (OMP_CLAUSE_LOCATION (c),
13036 "%qE is a member of a union", t);
13037 return error_mark_node;
13039 t = TREE_OPERAND (t, 0);
13040 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13042 if (maybe_ne (mem_ref_offset (t), 0))
13043 error_at (OMP_CLAUSE_LOCATION (c),
13044 "cannot dereference %qE in %qs clause", t,
13045 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13046 else
13047 t = TREE_OPERAND (t, 0);
13051 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13053 if (DECL_P (t))
13054 error_at (OMP_CLAUSE_LOCATION (c),
13055 "%qD is not a variable in %qs clause", t,
13056 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13057 else
13058 error_at (OMP_CLAUSE_LOCATION (c),
13059 "%qE is not a variable in %qs clause", t,
13060 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13061 return error_mark_node;
13063 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13064 && TYPE_ATOMIC (TREE_TYPE (t)))
13066 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13067 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13068 return error_mark_node;
13070 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13071 && VAR_P (t)
13072 && DECL_THREAD_LOCAL_P (t))
13074 error_at (OMP_CLAUSE_LOCATION (c),
13075 "%qD is threadprivate variable in %qs clause", t,
13076 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13077 return error_mark_node;
13079 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13080 && TYPE_ATOMIC (TREE_TYPE (t))
13081 && POINTER_TYPE_P (TREE_TYPE (t)))
13083 /* If the array section is pointer based and the pointer
13084 itself is _Atomic qualified, we need to atomically load
13085 the pointer. */
13086 c_expr expr;
13087 memset (&expr, 0, sizeof (expr));
13088 expr.value = ret;
13089 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13090 expr, false, false);
13091 ret = expr.value;
13093 return ret;
13096 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13097 maybe_zero_len, first_non_one, ort);
13098 if (ret == error_mark_node || ret == NULL_TREE)
13099 return ret;
13101 type = TREE_TYPE (ret);
13102 low_bound = TREE_PURPOSE (t);
13103 length = TREE_VALUE (t);
13105 if (low_bound == error_mark_node || length == error_mark_node)
13106 return error_mark_node;
13108 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13110 error_at (OMP_CLAUSE_LOCATION (c),
13111 "low bound %qE of array section does not have integral type",
13112 low_bound);
13113 return error_mark_node;
13115 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13117 error_at (OMP_CLAUSE_LOCATION (c),
13118 "length %qE of array section does not have integral type",
13119 length);
13120 return error_mark_node;
13122 if (low_bound
13123 && TREE_CODE (low_bound) == INTEGER_CST
13124 && TYPE_PRECISION (TREE_TYPE (low_bound))
13125 > TYPE_PRECISION (sizetype))
13126 low_bound = fold_convert (sizetype, low_bound);
13127 if (length
13128 && TREE_CODE (length) == INTEGER_CST
13129 && TYPE_PRECISION (TREE_TYPE (length))
13130 > TYPE_PRECISION (sizetype))
13131 length = fold_convert (sizetype, length);
13132 if (low_bound == NULL_TREE)
13133 low_bound = integer_zero_node;
13134 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13135 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13136 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13138 if (length != integer_one_node)
13140 error_at (OMP_CLAUSE_LOCATION (c),
13141 "expected single pointer in %qs clause",
13142 c_omp_map_clause_name (c, ort == C_ORT_ACC));
13143 return error_mark_node;
13146 if (length != NULL_TREE)
13148 if (!integer_nonzerop (length))
13150 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13151 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13152 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13153 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13155 if (integer_zerop (length))
13157 error_at (OMP_CLAUSE_LOCATION (c),
13158 "zero length array section in %qs clause",
13159 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13160 return error_mark_node;
13163 else
13164 maybe_zero_len = true;
13166 if (first_non_one == types.length ()
13167 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13168 first_non_one++;
13170 if (TREE_CODE (type) == ARRAY_TYPE)
13172 if (length == NULL_TREE
13173 && (TYPE_DOMAIN (type) == NULL_TREE
13174 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13176 error_at (OMP_CLAUSE_LOCATION (c),
13177 "for unknown bound array type length expression must "
13178 "be specified");
13179 return error_mark_node;
13181 if (TREE_CODE (low_bound) == INTEGER_CST
13182 && tree_int_cst_sgn (low_bound) == -1)
13184 error_at (OMP_CLAUSE_LOCATION (c),
13185 "negative low bound in array section in %qs clause",
13186 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13187 return error_mark_node;
13189 if (length != NULL_TREE
13190 && TREE_CODE (length) == INTEGER_CST
13191 && tree_int_cst_sgn (length) == -1)
13193 error_at (OMP_CLAUSE_LOCATION (c),
13194 "negative length in array section in %qs clause",
13195 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13196 return error_mark_node;
13198 if (TYPE_DOMAIN (type)
13199 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13200 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13201 == INTEGER_CST)
13203 tree size
13204 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13205 size = size_binop (PLUS_EXPR, size, size_one_node);
13206 if (TREE_CODE (low_bound) == INTEGER_CST)
13208 if (tree_int_cst_lt (size, low_bound))
13210 error_at (OMP_CLAUSE_LOCATION (c),
13211 "low bound %qE above array section size "
13212 "in %qs clause", low_bound,
13213 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13214 return error_mark_node;
13216 if (tree_int_cst_equal (size, low_bound))
13218 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13219 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13220 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13221 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13223 error_at (OMP_CLAUSE_LOCATION (c),
13224 "zero length array section in %qs clause",
13225 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13226 return error_mark_node;
13228 maybe_zero_len = true;
13230 else if (length == NULL_TREE
13231 && first_non_one == types.length ()
13232 && tree_int_cst_equal
13233 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13234 low_bound))
13235 first_non_one++;
13237 else if (length == NULL_TREE)
13239 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13240 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13241 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13242 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13243 maybe_zero_len = true;
13244 if (first_non_one == types.length ())
13245 first_non_one++;
13247 if (length && TREE_CODE (length) == INTEGER_CST)
13249 if (tree_int_cst_lt (size, length))
13251 error_at (OMP_CLAUSE_LOCATION (c),
13252 "length %qE above array section size "
13253 "in %qs clause", length,
13254 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13255 return error_mark_node;
13257 if (TREE_CODE (low_bound) == INTEGER_CST)
13259 tree lbpluslen
13260 = size_binop (PLUS_EXPR,
13261 fold_convert (sizetype, low_bound),
13262 fold_convert (sizetype, length));
13263 if (TREE_CODE (lbpluslen) == INTEGER_CST
13264 && tree_int_cst_lt (size, lbpluslen))
13266 error_at (OMP_CLAUSE_LOCATION (c),
13267 "high bound %qE above array section size "
13268 "in %qs clause", lbpluslen,
13269 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13270 return error_mark_node;
13275 else if (length == NULL_TREE)
13277 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13278 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13279 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13280 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13281 maybe_zero_len = true;
13282 if (first_non_one == types.length ())
13283 first_non_one++;
13286 /* For [lb:] we will need to evaluate lb more than once. */
13287 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13289 tree lb = save_expr (low_bound);
13290 if (lb != low_bound)
13292 TREE_PURPOSE (t) = lb;
13293 low_bound = lb;
13297 else if (TREE_CODE (type) == POINTER_TYPE)
13299 if (length == NULL_TREE)
13301 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13302 error_at (OMP_CLAUSE_LOCATION (c),
13303 "for array function parameter length expression "
13304 "must be specified");
13305 else
13306 error_at (OMP_CLAUSE_LOCATION (c),
13307 "for pointer type length expression must be specified");
13308 return error_mark_node;
13310 if (length != NULL_TREE
13311 && TREE_CODE (length) == INTEGER_CST
13312 && tree_int_cst_sgn (length) == -1)
13314 error_at (OMP_CLAUSE_LOCATION (c),
13315 "negative length in array section in %qs clause",
13316 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13317 return error_mark_node;
13319 /* If there is a pointer type anywhere but in the very first
13320 array-section-subscript, the array section can't be contiguous. */
13321 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13322 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13324 error_at (OMP_CLAUSE_LOCATION (c),
13325 "array section is not contiguous in %qs clause",
13326 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13327 return error_mark_node;
13330 else
13332 error_at (OMP_CLAUSE_LOCATION (c),
13333 "%qE does not have pointer or array type", ret);
13334 return error_mark_node;
13336 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13337 types.safe_push (TREE_TYPE (ret));
13338 /* We will need to evaluate lb more than once. */
13339 tree lb = save_expr (low_bound);
13340 if (lb != low_bound)
13342 TREE_PURPOSE (t) = lb;
13343 low_bound = lb;
13345 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13346 return ret;
13349 /* Handle array sections for clause C. */
13351 static bool
13352 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13354 bool maybe_zero_len = false;
13355 unsigned int first_non_one = 0;
13356 auto_vec<tree, 10> types;
13357 tree *tp = &OMP_CLAUSE_DECL (c);
13358 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13359 && TREE_CODE (*tp) == TREE_LIST
13360 && TREE_PURPOSE (*tp)
13361 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13362 tp = &TREE_VALUE (*tp);
13363 tree first = handle_omp_array_sections_1 (c, *tp, types,
13364 maybe_zero_len, first_non_one,
13365 ort);
13366 if (first == error_mark_node)
13367 return true;
13368 if (first == NULL_TREE)
13369 return false;
13370 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13372 tree t = *tp;
13373 tree tem = NULL_TREE;
13374 /* Need to evaluate side effects in the length expressions
13375 if any. */
13376 while (TREE_CODE (t) == TREE_LIST)
13378 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13380 if (tem == NULL_TREE)
13381 tem = TREE_VALUE (t);
13382 else
13383 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13384 TREE_VALUE (t), tem);
13386 t = TREE_CHAIN (t);
13388 if (tem)
13389 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13390 first = c_fully_fold (first, false, NULL, true);
13391 *tp = first;
13393 else
13395 unsigned int num = types.length (), i;
13396 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13397 tree condition = NULL_TREE;
13399 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13400 maybe_zero_len = true;
13402 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13403 t = TREE_CHAIN (t))
13405 tree low_bound = TREE_PURPOSE (t);
13406 tree length = TREE_VALUE (t);
13408 i--;
13409 if (low_bound
13410 && TREE_CODE (low_bound) == INTEGER_CST
13411 && TYPE_PRECISION (TREE_TYPE (low_bound))
13412 > TYPE_PRECISION (sizetype))
13413 low_bound = fold_convert (sizetype, low_bound);
13414 if (length
13415 && TREE_CODE (length) == INTEGER_CST
13416 && TYPE_PRECISION (TREE_TYPE (length))
13417 > TYPE_PRECISION (sizetype))
13418 length = fold_convert (sizetype, length);
13419 if (low_bound == NULL_TREE)
13420 low_bound = integer_zero_node;
13421 if (!maybe_zero_len && i > first_non_one)
13423 if (integer_nonzerop (low_bound))
13424 goto do_warn_noncontiguous;
13425 if (length != NULL_TREE
13426 && TREE_CODE (length) == INTEGER_CST
13427 && TYPE_DOMAIN (types[i])
13428 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13429 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13430 == INTEGER_CST)
13432 tree size;
13433 size = size_binop (PLUS_EXPR,
13434 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13435 size_one_node);
13436 if (!tree_int_cst_equal (length, size))
13438 do_warn_noncontiguous:
13439 error_at (OMP_CLAUSE_LOCATION (c),
13440 "array section is not contiguous in %qs "
13441 "clause",
13442 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13443 return true;
13446 if (length != NULL_TREE
13447 && TREE_SIDE_EFFECTS (length))
13449 if (side_effects == NULL_TREE)
13450 side_effects = length;
13451 else
13452 side_effects = build2 (COMPOUND_EXPR,
13453 TREE_TYPE (side_effects),
13454 length, side_effects);
13457 else
13459 tree l;
13461 if (i > first_non_one
13462 && ((length && integer_nonzerop (length))
13463 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13464 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13465 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13466 continue;
13467 if (length)
13468 l = fold_convert (sizetype, length);
13469 else
13471 l = size_binop (PLUS_EXPR,
13472 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13473 size_one_node);
13474 l = size_binop (MINUS_EXPR, l,
13475 fold_convert (sizetype, low_bound));
13477 if (i > first_non_one)
13479 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13480 size_zero_node);
13481 if (condition == NULL_TREE)
13482 condition = l;
13483 else
13484 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13485 l, condition);
13487 else if (size == NULL_TREE)
13489 size = size_in_bytes (TREE_TYPE (types[i]));
13490 tree eltype = TREE_TYPE (types[num - 1]);
13491 while (TREE_CODE (eltype) == ARRAY_TYPE)
13492 eltype = TREE_TYPE (eltype);
13493 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13494 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13495 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13497 if (integer_zerop (size)
13498 || integer_zerop (size_in_bytes (eltype)))
13500 error_at (OMP_CLAUSE_LOCATION (c),
13501 "zero length array section in %qs clause",
13502 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13503 return error_mark_node;
13505 size = size_binop (EXACT_DIV_EXPR, size,
13506 size_in_bytes (eltype));
13508 size = size_binop (MULT_EXPR, size, l);
13509 if (condition)
13510 size = fold_build3 (COND_EXPR, sizetype, condition,
13511 size, size_zero_node);
13513 else
13514 size = size_binop (MULT_EXPR, size, l);
13517 if (side_effects)
13518 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13519 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13520 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13521 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13523 size = size_binop (MINUS_EXPR, size, size_one_node);
13524 size = c_fully_fold (size, false, NULL);
13525 size = save_expr (size);
13526 tree index_type = build_index_type (size);
13527 tree eltype = TREE_TYPE (first);
13528 while (TREE_CODE (eltype) == ARRAY_TYPE)
13529 eltype = TREE_TYPE (eltype);
13530 tree type = build_array_type (eltype, index_type);
13531 tree ptype = build_pointer_type (eltype);
13532 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13533 t = build_fold_addr_expr (t);
13534 tree t2 = build_fold_addr_expr (first);
13535 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13536 ptrdiff_type_node, t2);
13537 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13538 ptrdiff_type_node, t2,
13539 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13540 ptrdiff_type_node, t));
13541 t2 = c_fully_fold (t2, false, NULL);
13542 if (tree_fits_shwi_p (t2))
13543 t = build2 (MEM_REF, type, t,
13544 build_int_cst (ptype, tree_to_shwi (t2)));
13545 else
13547 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13548 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13549 TREE_TYPE (t), t, t2);
13550 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13552 OMP_CLAUSE_DECL (c) = t;
13553 return false;
13555 first = c_fully_fold (first, false, NULL);
13556 OMP_CLAUSE_DECL (c) = first;
13557 if (size)
13558 size = c_fully_fold (size, false, NULL);
13559 OMP_CLAUSE_SIZE (c) = size;
13560 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13561 || (TREE_CODE (t) == COMPONENT_REF
13562 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13563 return false;
13564 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13565 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13566 switch (OMP_CLAUSE_MAP_KIND (c))
13568 case GOMP_MAP_ALLOC:
13569 case GOMP_MAP_IF_PRESENT:
13570 case GOMP_MAP_TO:
13571 case GOMP_MAP_FROM:
13572 case GOMP_MAP_TOFROM:
13573 case GOMP_MAP_ALWAYS_TO:
13574 case GOMP_MAP_ALWAYS_FROM:
13575 case GOMP_MAP_ALWAYS_TOFROM:
13576 case GOMP_MAP_RELEASE:
13577 case GOMP_MAP_DELETE:
13578 case GOMP_MAP_FORCE_TO:
13579 case GOMP_MAP_FORCE_FROM:
13580 case GOMP_MAP_FORCE_TOFROM:
13581 case GOMP_MAP_FORCE_PRESENT:
13582 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13583 break;
13584 default:
13585 break;
13587 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13588 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13589 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13590 else if (TREE_CODE (t) == COMPONENT_REF)
13592 gomp_map_kind k = (ort == C_ORT_ACC) ? GOMP_MAP_ATTACH_DETACH
13593 : GOMP_MAP_ALWAYS_POINTER;
13594 OMP_CLAUSE_SET_MAP_KIND (c2, k);
13596 else
13597 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13598 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13599 && !c_mark_addressable (t))
13600 return false;
13601 OMP_CLAUSE_DECL (c2) = t;
13602 t = build_fold_addr_expr (first);
13603 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13604 tree ptr = OMP_CLAUSE_DECL (c2);
13605 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13606 ptr = build_fold_addr_expr (ptr);
13607 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13608 ptrdiff_type_node, t,
13609 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13610 ptrdiff_type_node, ptr));
13611 t = c_fully_fold (t, false, NULL);
13612 OMP_CLAUSE_SIZE (c2) = t;
13613 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13614 OMP_CLAUSE_CHAIN (c) = c2;
13616 return false;
13619 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13620 an inline call. But, remap
13621 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13622 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13624 static tree
13625 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13626 tree decl, tree placeholder)
13628 copy_body_data id;
13629 hash_map<tree, tree> decl_map;
13631 decl_map.put (omp_decl1, placeholder);
13632 decl_map.put (omp_decl2, decl);
13633 memset (&id, 0, sizeof (id));
13634 id.src_fn = DECL_CONTEXT (omp_decl1);
13635 id.dst_fn = current_function_decl;
13636 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13637 id.decl_map = &decl_map;
13639 id.copy_decl = copy_decl_no_change;
13640 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13641 id.transform_new_cfg = true;
13642 id.transform_return_to_modify = false;
13643 id.transform_lang_insert_block = NULL;
13644 id.eh_lp_nr = 0;
13645 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13646 return stmt;
13649 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13650 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13652 static tree
13653 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13655 if (*tp == (tree) data)
13656 return *tp;
13657 return NULL_TREE;
13660 /* Similarly, but also walk aggregate fields. */
13662 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13664 static tree
13665 c_find_omp_var_r (tree *tp, int *, void *data)
13667 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13668 return *tp;
13669 if (RECORD_OR_UNION_TYPE_P (*tp))
13671 tree field;
13672 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13674 for (field = TYPE_FIELDS (*tp); field;
13675 field = DECL_CHAIN (field))
13676 if (TREE_CODE (field) == FIELD_DECL)
13678 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13679 c_find_omp_var_r, data, pset);
13680 if (ret)
13681 return ret;
13682 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13683 if (ret)
13684 return ret;
13685 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13686 pset);
13687 if (ret)
13688 return ret;
13689 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13690 if (ret)
13691 return ret;
13694 else if (INTEGRAL_TYPE_P (*tp))
13695 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13696 ((struct c_find_omp_var_s *) data)->pset);
13697 return NULL_TREE;
13700 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13701 and clauses containing them should be removed. */
13703 static bool
13704 c_omp_finish_iterators (tree iter)
13706 bool ret = false;
13707 for (tree it = iter; it; it = TREE_CHAIN (it))
13709 tree var = TREE_VEC_ELT (it, 0);
13710 tree begin = TREE_VEC_ELT (it, 1);
13711 tree end = TREE_VEC_ELT (it, 2);
13712 tree step = TREE_VEC_ELT (it, 3);
13713 tree orig_step;
13714 tree type = TREE_TYPE (var);
13715 location_t loc = DECL_SOURCE_LOCATION (var);
13716 if (type == error_mark_node)
13718 ret = true;
13719 continue;
13721 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13723 error_at (loc, "iterator %qD has neither integral nor pointer type",
13724 var);
13725 ret = true;
13726 continue;
13728 else if (TYPE_ATOMIC (type))
13730 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13731 ret = true;
13732 continue;
13734 else if (TYPE_READONLY (type))
13736 error_at (loc, "iterator %qD has const qualified type", var);
13737 ret = true;
13738 continue;
13740 else if (step == error_mark_node
13741 || TREE_TYPE (step) == error_mark_node)
13743 ret = true;
13744 continue;
13746 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13748 error_at (EXPR_LOC_OR_LOC (step, loc),
13749 "iterator step with non-integral type");
13750 ret = true;
13751 continue;
13753 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13754 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13755 orig_step = save_expr (c_fully_fold (step, false, NULL));
13756 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13757 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13758 if (POINTER_TYPE_P (type))
13760 begin = save_expr (begin);
13761 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13762 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13763 fold_convert (sizetype, step),
13764 fold_convert (sizetype, begin));
13765 step = fold_convert (ssizetype, step);
13767 if (integer_zerop (step))
13769 error_at (loc, "iterator %qD has zero step", var);
13770 ret = true;
13771 continue;
13774 if (begin == error_mark_node
13775 || end == error_mark_node
13776 || step == error_mark_node
13777 || orig_step == error_mark_node)
13779 ret = true;
13780 continue;
13782 hash_set<tree> pset;
13783 tree it2;
13784 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13786 tree var2 = TREE_VEC_ELT (it2, 0);
13787 tree begin2 = TREE_VEC_ELT (it2, 1);
13788 tree end2 = TREE_VEC_ELT (it2, 2);
13789 tree step2 = TREE_VEC_ELT (it2, 3);
13790 tree type2 = TREE_TYPE (var2);
13791 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13792 struct c_find_omp_var_s data = { var, &pset };
13793 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13795 error_at (loc2,
13796 "type of iterator %qD refers to outer iterator %qD",
13797 var2, var);
13798 break;
13800 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13802 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13803 "begin expression refers to outer iterator %qD", var);
13804 break;
13806 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13808 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13809 "end expression refers to outer iterator %qD", var);
13810 break;
13812 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13814 error_at (EXPR_LOC_OR_LOC (step2, loc2),
13815 "step expression refers to outer iterator %qD", var);
13816 break;
13819 if (it2)
13821 ret = true;
13822 continue;
13824 TREE_VEC_ELT (it, 1) = begin;
13825 TREE_VEC_ELT (it, 2) = end;
13826 TREE_VEC_ELT (it, 3) = step;
13827 TREE_VEC_ELT (it, 4) = orig_step;
13829 return ret;
13832 /* Ensure that pointers are used in OpenACC attach and detach clauses.
13833 Return true if an error has been detected. */
13835 static bool
13836 c_oacc_check_attachments (tree c)
13838 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13839 return false;
13841 /* OpenACC attach / detach clauses must be pointers. */
13842 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13843 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
13845 tree t = OMP_CLAUSE_DECL (c);
13847 while (TREE_CODE (t) == TREE_LIST)
13848 t = TREE_CHAIN (t);
13850 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13852 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
13853 c_omp_map_clause_name (c, true));
13854 return true;
13858 return false;
13861 /* For all elements of CLAUSES, validate them against their constraints.
13862 Remove any elements from the list that are invalid. */
13864 tree
13865 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13867 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13868 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13869 tree c, t, type, *pc;
13870 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13871 bool branch_seen = false;
13872 bool copyprivate_seen = false;
13873 bool linear_variable_step_check = false;
13874 tree *nowait_clause = NULL;
13875 tree ordered_clause = NULL_TREE;
13876 tree schedule_clause = NULL_TREE;
13877 bool oacc_async = false;
13878 tree last_iterators = NULL_TREE;
13879 bool last_iterators_remove = false;
13880 tree *nogroup_seen = NULL;
13881 tree *order_clause = NULL;
13882 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
13883 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
13884 int reduction_seen = 0;
13886 bitmap_obstack_initialize (NULL);
13887 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13888 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13889 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13890 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13891 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
13892 bitmap_initialize (&map_head, &bitmap_default_obstack);
13893 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13894 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
13895 instead. */
13896 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13898 if (ort & C_ORT_ACC)
13899 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13900 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13902 oacc_async = true;
13903 break;
13906 for (pc = &clauses, c = clauses; c ; c = *pc)
13908 bool remove = false;
13909 bool need_complete = false;
13910 bool need_implicitly_determined = false;
13912 switch (OMP_CLAUSE_CODE (c))
13914 case OMP_CLAUSE_SHARED:
13915 need_implicitly_determined = true;
13916 goto check_dup_generic;
13918 case OMP_CLAUSE_PRIVATE:
13919 need_complete = true;
13920 need_implicitly_determined = true;
13921 goto check_dup_generic;
13923 case OMP_CLAUSE_REDUCTION:
13924 if (reduction_seen == 0)
13925 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
13926 else if (reduction_seen != -2
13927 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
13928 ? -1 : 1))
13930 error_at (OMP_CLAUSE_LOCATION (c),
13931 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
13932 "on the same construct");
13933 reduction_seen = -2;
13935 /* FALLTHRU */
13936 case OMP_CLAUSE_IN_REDUCTION:
13937 case OMP_CLAUSE_TASK_REDUCTION:
13938 need_implicitly_determined = true;
13939 t = OMP_CLAUSE_DECL (c);
13940 if (TREE_CODE (t) == TREE_LIST)
13942 if (handle_omp_array_sections (c, ort))
13944 remove = true;
13945 break;
13948 t = OMP_CLAUSE_DECL (c);
13949 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13950 && OMP_CLAUSE_REDUCTION_INSCAN (c))
13952 error_at (OMP_CLAUSE_LOCATION (c),
13953 "%<inscan%> %<reduction%> clause with array "
13954 "section");
13955 remove = true;
13956 break;
13959 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13960 if (t == error_mark_node)
13962 remove = true;
13963 break;
13965 if (oacc_async)
13966 c_mark_addressable (t);
13967 type = TREE_TYPE (t);
13968 if (TREE_CODE (t) == MEM_REF)
13969 type = TREE_TYPE (type);
13970 if (TREE_CODE (type) == ARRAY_TYPE)
13972 tree oatype = type;
13973 gcc_assert (TREE_CODE (t) != MEM_REF);
13974 while (TREE_CODE (type) == ARRAY_TYPE)
13975 type = TREE_TYPE (type);
13976 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13978 error_at (OMP_CLAUSE_LOCATION (c),
13979 "%qD in %<reduction%> clause is a zero size array",
13981 remove = true;
13982 break;
13984 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13985 TYPE_SIZE_UNIT (type));
13986 if (integer_zerop (size))
13988 error_at (OMP_CLAUSE_LOCATION (c),
13989 "%qD in %<reduction%> clause is a zero size array",
13991 remove = true;
13992 break;
13994 size = size_binop (MINUS_EXPR, size, size_one_node);
13995 size = save_expr (size);
13996 tree index_type = build_index_type (size);
13997 tree atype = build_array_type (type, index_type);
13998 tree ptype = build_pointer_type (type);
13999 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14000 t = build_fold_addr_expr (t);
14001 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14002 OMP_CLAUSE_DECL (c) = t;
14004 if (TYPE_ATOMIC (type))
14006 error_at (OMP_CLAUSE_LOCATION (c),
14007 "%<_Atomic%> %qE in %<reduction%> clause", t);
14008 remove = true;
14009 break;
14011 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14012 || OMP_CLAUSE_REDUCTION_TASK (c))
14014 /* Disallow zero sized or potentially zero sized task
14015 reductions. */
14016 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14018 error_at (OMP_CLAUSE_LOCATION (c),
14019 "zero sized type %qT in %qs clause", type,
14020 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14021 remove = true;
14022 break;
14024 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14026 error_at (OMP_CLAUSE_LOCATION (c),
14027 "variable sized type %qT in %qs clause", type,
14028 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14029 remove = true;
14030 break;
14033 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14034 && (FLOAT_TYPE_P (type)
14035 || TREE_CODE (type) == COMPLEX_TYPE))
14037 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14038 const char *r_name = NULL;
14040 switch (r_code)
14042 case PLUS_EXPR:
14043 case MULT_EXPR:
14044 case MINUS_EXPR:
14045 break;
14046 case MIN_EXPR:
14047 if (TREE_CODE (type) == COMPLEX_TYPE)
14048 r_name = "min";
14049 break;
14050 case MAX_EXPR:
14051 if (TREE_CODE (type) == COMPLEX_TYPE)
14052 r_name = "max";
14053 break;
14054 case BIT_AND_EXPR:
14055 r_name = "&";
14056 break;
14057 case BIT_XOR_EXPR:
14058 r_name = "^";
14059 break;
14060 case BIT_IOR_EXPR:
14061 r_name = "|";
14062 break;
14063 case TRUTH_ANDIF_EXPR:
14064 if (FLOAT_TYPE_P (type))
14065 r_name = "&&";
14066 break;
14067 case TRUTH_ORIF_EXPR:
14068 if (FLOAT_TYPE_P (type))
14069 r_name = "||";
14070 break;
14071 default:
14072 gcc_unreachable ();
14074 if (r_name)
14076 error_at (OMP_CLAUSE_LOCATION (c),
14077 "%qE has invalid type for %<reduction(%s)%>",
14078 t, r_name);
14079 remove = true;
14080 break;
14083 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14085 error_at (OMP_CLAUSE_LOCATION (c),
14086 "user defined reduction not found for %qE", t);
14087 remove = true;
14088 break;
14090 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14092 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14093 type = TYPE_MAIN_VARIANT (type);
14094 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14095 VAR_DECL, NULL_TREE, type);
14096 tree decl_placeholder = NULL_TREE;
14097 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14098 DECL_ARTIFICIAL (placeholder) = 1;
14099 DECL_IGNORED_P (placeholder) = 1;
14100 if (TREE_CODE (t) == MEM_REF)
14102 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14103 VAR_DECL, NULL_TREE, type);
14104 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14105 DECL_ARTIFICIAL (decl_placeholder) = 1;
14106 DECL_IGNORED_P (decl_placeholder) = 1;
14108 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14109 c_mark_addressable (placeholder);
14110 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14111 c_mark_addressable (decl_placeholder ? decl_placeholder
14112 : OMP_CLAUSE_DECL (c));
14113 OMP_CLAUSE_REDUCTION_MERGE (c)
14114 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14115 TREE_VEC_ELT (list, 0),
14116 TREE_VEC_ELT (list, 1),
14117 decl_placeholder ? decl_placeholder
14118 : OMP_CLAUSE_DECL (c), placeholder);
14119 OMP_CLAUSE_REDUCTION_MERGE (c)
14120 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14121 void_type_node, NULL_TREE,
14122 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14123 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14124 if (TREE_VEC_LENGTH (list) == 6)
14126 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14127 c_mark_addressable (decl_placeholder ? decl_placeholder
14128 : OMP_CLAUSE_DECL (c));
14129 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14130 c_mark_addressable (placeholder);
14131 tree init = TREE_VEC_ELT (list, 5);
14132 if (init == error_mark_node)
14133 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14134 OMP_CLAUSE_REDUCTION_INIT (c)
14135 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14136 TREE_VEC_ELT (list, 3),
14137 decl_placeholder ? decl_placeholder
14138 : OMP_CLAUSE_DECL (c), placeholder);
14139 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14141 tree v = decl_placeholder ? decl_placeholder : t;
14142 OMP_CLAUSE_REDUCTION_INIT (c)
14143 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14144 OMP_CLAUSE_REDUCTION_INIT (c));
14146 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14147 c_find_omp_placeholder_r,
14148 placeholder, NULL))
14149 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14151 else
14153 tree init;
14154 tree v = decl_placeholder ? decl_placeholder : t;
14155 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14156 init = build_constructor (TREE_TYPE (v), NULL);
14157 else
14158 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14159 OMP_CLAUSE_REDUCTION_INIT (c)
14160 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14162 OMP_CLAUSE_REDUCTION_INIT (c)
14163 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14164 void_type_node, NULL_TREE,
14165 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14166 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14168 if (TREE_CODE (t) == MEM_REF)
14170 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14171 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14172 != INTEGER_CST)
14174 sorry ("variable length element type in array "
14175 "%<reduction%> clause");
14176 remove = true;
14177 break;
14179 t = TREE_OPERAND (t, 0);
14180 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14181 t = TREE_OPERAND (t, 0);
14182 if (TREE_CODE (t) == ADDR_EXPR)
14183 t = TREE_OPERAND (t, 0);
14185 goto check_dup_generic_t;
14187 case OMP_CLAUSE_COPYPRIVATE:
14188 copyprivate_seen = true;
14189 if (nowait_clause)
14191 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14192 "%<nowait%> clause must not be used together "
14193 "with %<copyprivate%>");
14194 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14195 nowait_clause = NULL;
14197 goto check_dup_generic;
14199 case OMP_CLAUSE_COPYIN:
14200 t = OMP_CLAUSE_DECL (c);
14201 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14203 error_at (OMP_CLAUSE_LOCATION (c),
14204 "%qE must be %<threadprivate%> for %<copyin%>", t);
14205 remove = true;
14206 break;
14208 goto check_dup_generic;
14210 case OMP_CLAUSE_LINEAR:
14211 if (ort != C_ORT_OMP_DECLARE_SIMD)
14212 need_implicitly_determined = true;
14213 t = OMP_CLAUSE_DECL (c);
14214 if (ort != C_ORT_OMP_DECLARE_SIMD
14215 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
14217 error_at (OMP_CLAUSE_LOCATION (c),
14218 "modifier should not be specified in %<linear%> "
14219 "clause on %<simd%> or %<for%> constructs");
14220 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14222 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14223 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14225 error_at (OMP_CLAUSE_LOCATION (c),
14226 "linear clause applied to non-integral non-pointer "
14227 "variable with type %qT", TREE_TYPE (t));
14228 remove = true;
14229 break;
14231 if (TYPE_ATOMIC (TREE_TYPE (t)))
14233 error_at (OMP_CLAUSE_LOCATION (c),
14234 "%<_Atomic%> %qD in %<linear%> clause", t);
14235 remove = true;
14236 break;
14238 if (ort == C_ORT_OMP_DECLARE_SIMD)
14240 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14241 if (TREE_CODE (s) == PARM_DECL)
14243 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14244 /* map_head bitmap is used as uniform_head if
14245 declare_simd. */
14246 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14247 linear_variable_step_check = true;
14248 goto check_dup_generic;
14250 if (TREE_CODE (s) != INTEGER_CST)
14252 error_at (OMP_CLAUSE_LOCATION (c),
14253 "%<linear%> clause step %qE is neither constant "
14254 "nor a parameter", s);
14255 remove = true;
14256 break;
14259 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14261 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14262 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14263 OMP_CLAUSE_DECL (c), s);
14264 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14265 sizetype, fold_convert (sizetype, s),
14266 fold_convert
14267 (sizetype, OMP_CLAUSE_DECL (c)));
14268 if (s == error_mark_node)
14269 s = size_one_node;
14270 OMP_CLAUSE_LINEAR_STEP (c) = s;
14272 else
14273 OMP_CLAUSE_LINEAR_STEP (c)
14274 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14275 goto check_dup_generic;
14277 check_dup_generic:
14278 t = OMP_CLAUSE_DECL (c);
14279 check_dup_generic_t:
14280 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14282 error_at (OMP_CLAUSE_LOCATION (c),
14283 "%qE is not a variable in clause %qs", t,
14284 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14285 remove = true;
14287 else if ((ort == C_ORT_ACC
14288 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14289 || (ort == C_ORT_OMP
14290 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14291 || (OMP_CLAUSE_CODE (c)
14292 == OMP_CLAUSE_USE_DEVICE_ADDR))))
14294 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14296 error_at (OMP_CLAUSE_LOCATION (c),
14297 ort == C_ORT_ACC
14298 ? "%qD appears more than once in reduction clauses"
14299 : "%qD appears more than once in data clauses",
14301 remove = true;
14303 else
14304 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14306 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14307 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14308 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14310 error_at (OMP_CLAUSE_LOCATION (c),
14311 "%qE appears more than once in data clauses", t);
14312 remove = true;
14314 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14315 && bitmap_bit_p (&map_head, DECL_UID (t)))
14317 if (ort == C_ORT_ACC)
14318 error_at (OMP_CLAUSE_LOCATION (c),
14319 "%qD appears more than once in data clauses", t);
14320 else
14321 error_at (OMP_CLAUSE_LOCATION (c),
14322 "%qD appears both in data and map clauses", t);
14323 remove = true;
14325 else
14326 bitmap_set_bit (&generic_head, DECL_UID (t));
14327 break;
14329 case OMP_CLAUSE_FIRSTPRIVATE:
14330 t = OMP_CLAUSE_DECL (c);
14331 need_complete = true;
14332 need_implicitly_determined = true;
14333 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14335 error_at (OMP_CLAUSE_LOCATION (c),
14336 "%qE is not a variable in clause %<firstprivate%>", t);
14337 remove = true;
14339 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14340 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14342 error_at (OMP_CLAUSE_LOCATION (c),
14343 "%qE appears more than once in data clauses", t);
14344 remove = true;
14346 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14348 if (ort == C_ORT_ACC)
14349 error_at (OMP_CLAUSE_LOCATION (c),
14350 "%qD appears more than once in data clauses", t);
14351 else
14352 error_at (OMP_CLAUSE_LOCATION (c),
14353 "%qD appears both in data and map clauses", t);
14354 remove = true;
14356 else
14357 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14358 break;
14360 case OMP_CLAUSE_LASTPRIVATE:
14361 t = OMP_CLAUSE_DECL (c);
14362 need_complete = true;
14363 need_implicitly_determined = true;
14364 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14366 error_at (OMP_CLAUSE_LOCATION (c),
14367 "%qE is not a variable in clause %<lastprivate%>", t);
14368 remove = true;
14370 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14371 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14373 error_at (OMP_CLAUSE_LOCATION (c),
14374 "%qE appears more than once in data clauses", t);
14375 remove = true;
14377 else
14378 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14379 break;
14381 case OMP_CLAUSE_ALIGNED:
14382 t = OMP_CLAUSE_DECL (c);
14383 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14385 error_at (OMP_CLAUSE_LOCATION (c),
14386 "%qE is not a variable in %<aligned%> clause", t);
14387 remove = true;
14389 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14390 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14392 error_at (OMP_CLAUSE_LOCATION (c),
14393 "%qE in %<aligned%> clause is neither a pointer nor "
14394 "an array", t);
14395 remove = true;
14397 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14399 error_at (OMP_CLAUSE_LOCATION (c),
14400 "%<_Atomic%> %qD in %<aligned%> clause", t);
14401 remove = true;
14402 break;
14404 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14406 error_at (OMP_CLAUSE_LOCATION (c),
14407 "%qE appears more than once in %<aligned%> clauses",
14409 remove = true;
14411 else
14412 bitmap_set_bit (&aligned_head, DECL_UID (t));
14413 break;
14415 case OMP_CLAUSE_NONTEMPORAL:
14416 t = OMP_CLAUSE_DECL (c);
14417 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14419 error_at (OMP_CLAUSE_LOCATION (c),
14420 "%qE is not a variable in %<nontemporal%> clause", t);
14421 remove = true;
14423 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14425 error_at (OMP_CLAUSE_LOCATION (c),
14426 "%qE appears more than once in %<nontemporal%> "
14427 "clauses", t);
14428 remove = true;
14430 else
14431 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14432 break;
14434 case OMP_CLAUSE_DEPEND:
14435 t = OMP_CLAUSE_DECL (c);
14436 if (t == NULL_TREE)
14438 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14439 == OMP_CLAUSE_DEPEND_SOURCE);
14440 break;
14442 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14444 gcc_assert (TREE_CODE (t) == TREE_LIST);
14445 for (; t; t = TREE_CHAIN (t))
14447 tree decl = TREE_VALUE (t);
14448 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14450 tree offset = TREE_PURPOSE (t);
14451 bool neg = wi::neg_p (wi::to_wide (offset));
14452 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14453 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14454 neg ? MINUS_EXPR : PLUS_EXPR,
14455 decl, offset);
14456 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14457 sizetype,
14458 fold_convert (sizetype, t2),
14459 fold_convert (sizetype, decl));
14460 if (t2 == error_mark_node)
14462 remove = true;
14463 break;
14465 TREE_PURPOSE (t) = t2;
14468 break;
14470 if (TREE_CODE (t) == TREE_LIST
14471 && TREE_PURPOSE (t)
14472 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14474 if (TREE_PURPOSE (t) != last_iterators)
14475 last_iterators_remove
14476 = c_omp_finish_iterators (TREE_PURPOSE (t));
14477 last_iterators = TREE_PURPOSE (t);
14478 t = TREE_VALUE (t);
14479 if (last_iterators_remove)
14480 t = error_mark_node;
14482 else
14483 last_iterators = NULL_TREE;
14484 if (TREE_CODE (t) == TREE_LIST)
14486 if (handle_omp_array_sections (c, ort))
14487 remove = true;
14488 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14490 error_at (OMP_CLAUSE_LOCATION (c),
14491 "%<depend%> clause with %<depobj%> dependence "
14492 "type on array section");
14493 remove = true;
14495 break;
14497 if (t == error_mark_node)
14498 remove = true;
14499 else if (!lvalue_p (t))
14501 error_at (OMP_CLAUSE_LOCATION (c),
14502 "%qE is not lvalue expression nor array section in "
14503 "%<depend%> clause", t);
14504 remove = true;
14506 else if (TREE_CODE (t) == COMPONENT_REF
14507 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14509 error_at (OMP_CLAUSE_LOCATION (c),
14510 "bit-field %qE in %qs clause", t, "depend");
14511 remove = true;
14513 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14515 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14517 error_at (OMP_CLAUSE_LOCATION (c),
14518 "%qE does not have %<omp_depend_t%> type in "
14519 "%<depend%> clause with %<depobj%> dependence "
14520 "type", t);
14521 remove = true;
14524 else if (c_omp_depend_t_p (TREE_TYPE (t)))
14526 error_at (OMP_CLAUSE_LOCATION (c),
14527 "%qE should not have %<omp_depend_t%> type in "
14528 "%<depend%> clause with dependence type other than "
14529 "%<depobj%>", t);
14530 remove = true;
14532 if (!remove)
14534 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14535 t, false);
14536 if (addr == error_mark_node)
14537 remove = true;
14538 else
14540 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14541 RO_UNARY_STAR);
14542 if (t == error_mark_node)
14543 remove = true;
14544 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14545 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14546 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14547 == TREE_VEC))
14548 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14549 else
14550 OMP_CLAUSE_DECL (c) = t;
14553 break;
14555 case OMP_CLAUSE_MAP:
14556 case OMP_CLAUSE_TO:
14557 case OMP_CLAUSE_FROM:
14558 case OMP_CLAUSE__CACHE_:
14559 t = OMP_CLAUSE_DECL (c);
14560 if (TREE_CODE (t) == TREE_LIST)
14562 if (handle_omp_array_sections (c, ort))
14563 remove = true;
14564 else
14566 t = OMP_CLAUSE_DECL (c);
14567 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14569 error_at (OMP_CLAUSE_LOCATION (c),
14570 "array section does not have mappable type "
14571 "in %qs clause",
14572 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14573 remove = true;
14575 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14577 error_at (OMP_CLAUSE_LOCATION (c),
14578 "%<_Atomic%> %qE in %qs clause", t,
14579 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14580 remove = true;
14582 while (TREE_CODE (t) == ARRAY_REF)
14583 t = TREE_OPERAND (t, 0);
14584 if (TREE_CODE (t) == COMPONENT_REF
14585 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14587 while (TREE_CODE (t) == COMPONENT_REF)
14588 t = TREE_OPERAND (t, 0);
14589 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14590 break;
14591 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14593 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14594 error_at (OMP_CLAUSE_LOCATION (c),
14595 "%qD appears more than once in motion "
14596 "clauses", t);
14597 else if (ort == C_ORT_ACC)
14598 error_at (OMP_CLAUSE_LOCATION (c),
14599 "%qD appears more than once in data "
14600 "clauses", t);
14601 else
14602 error_at (OMP_CLAUSE_LOCATION (c),
14603 "%qD appears more than once in map "
14604 "clauses", t);
14605 remove = true;
14607 else
14609 bitmap_set_bit (&map_head, DECL_UID (t));
14610 bitmap_set_bit (&map_field_head, DECL_UID (t));
14614 if (c_oacc_check_attachments (c))
14615 remove = true;
14616 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14617 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14618 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14619 /* In this case, we have a single array element which is a
14620 pointer, and we already set OMP_CLAUSE_SIZE in
14621 handle_omp_array_sections above. For attach/detach clauses,
14622 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
14623 here. */
14624 OMP_CLAUSE_SIZE (c) = size_zero_node;
14625 break;
14627 if (t == error_mark_node)
14629 remove = true;
14630 break;
14632 /* OpenACC attach / detach clauses must be pointers. */
14633 if (c_oacc_check_attachments (c))
14635 remove = true;
14636 break;
14638 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14639 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14640 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14641 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
14642 bias) to zero here, so it is not set erroneously to the pointer
14643 size later on in gimplify.c. */
14644 OMP_CLAUSE_SIZE (c) = size_zero_node;
14645 if (TREE_CODE (t) == COMPONENT_REF
14646 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14648 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14650 error_at (OMP_CLAUSE_LOCATION (c),
14651 "bit-field %qE in %qs clause",
14652 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14653 remove = true;
14655 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14657 error_at (OMP_CLAUSE_LOCATION (c),
14658 "%qE does not have a mappable type in %qs clause",
14659 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14660 remove = true;
14662 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14664 error_at (OMP_CLAUSE_LOCATION (c),
14665 "%<_Atomic%> %qE in %qs clause", t,
14666 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14667 remove = true;
14669 while (TREE_CODE (t) == COMPONENT_REF)
14671 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14672 == UNION_TYPE)
14674 error_at (OMP_CLAUSE_LOCATION (c),
14675 "%qE is a member of a union", t);
14676 remove = true;
14677 break;
14679 t = TREE_OPERAND (t, 0);
14680 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
14682 if (maybe_ne (mem_ref_offset (t), 0))
14683 error_at (OMP_CLAUSE_LOCATION (c),
14684 "cannot dereference %qE in %qs clause", t,
14685 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14686 else
14687 t = TREE_OPERAND (t, 0);
14690 if (remove)
14691 break;
14692 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14694 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14695 break;
14698 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14700 error_at (OMP_CLAUSE_LOCATION (c),
14701 "%qE is not a variable in %qs clause", t,
14702 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14703 remove = true;
14705 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14707 error_at (OMP_CLAUSE_LOCATION (c),
14708 "%qD is threadprivate variable in %qs clause", t,
14709 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14710 remove = true;
14712 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14713 || (OMP_CLAUSE_MAP_KIND (c)
14714 != GOMP_MAP_FIRSTPRIVATE_POINTER))
14715 && !c_mark_addressable (t))
14716 remove = true;
14717 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14718 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14719 || (OMP_CLAUSE_MAP_KIND (c)
14720 == GOMP_MAP_FIRSTPRIVATE_POINTER)
14721 || (OMP_CLAUSE_MAP_KIND (c)
14722 == GOMP_MAP_FORCE_DEVICEPTR)))
14723 && t == OMP_CLAUSE_DECL (c)
14724 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14726 error_at (OMP_CLAUSE_LOCATION (c),
14727 "%qD does not have a mappable type in %qs clause", t,
14728 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14729 remove = true;
14731 else if (TREE_TYPE (t) == error_mark_node)
14732 remove = true;
14733 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14735 error_at (OMP_CLAUSE_LOCATION (c),
14736 "%<_Atomic%> %qE in %qs clause", t,
14737 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14738 remove = true;
14740 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14741 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14743 if (bitmap_bit_p (&generic_head, DECL_UID (t))
14744 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14746 error_at (OMP_CLAUSE_LOCATION (c),
14747 "%qD appears more than once in data clauses", t);
14748 remove = true;
14750 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14752 if (ort == C_ORT_ACC)
14753 error_at (OMP_CLAUSE_LOCATION (c),
14754 "%qD appears more than once in data clauses", t);
14755 else
14756 error_at (OMP_CLAUSE_LOCATION (c),
14757 "%qD appears both in data and map clauses", t);
14758 remove = true;
14760 else
14761 bitmap_set_bit (&generic_head, DECL_UID (t));
14763 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14765 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14766 error_at (OMP_CLAUSE_LOCATION (c),
14767 "%qD appears more than once in motion clauses", t);
14768 else if (ort == C_ORT_ACC)
14769 error_at (OMP_CLAUSE_LOCATION (c),
14770 "%qD appears more than once in data clauses", t);
14771 else
14772 error_at (OMP_CLAUSE_LOCATION (c),
14773 "%qD appears more than once in map clauses", t);
14774 remove = true;
14776 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14777 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14779 if (ort == C_ORT_ACC)
14780 error_at (OMP_CLAUSE_LOCATION (c),
14781 "%qD appears more than once in data clauses", t);
14782 else
14783 error_at (OMP_CLAUSE_LOCATION (c),
14784 "%qD appears both in data and map clauses", t);
14785 remove = true;
14787 else
14789 bitmap_set_bit (&map_head, DECL_UID (t));
14790 if (t != OMP_CLAUSE_DECL (c)
14791 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14792 bitmap_set_bit (&map_field_head, DECL_UID (t));
14794 break;
14796 case OMP_CLAUSE_TO_DECLARE:
14797 case OMP_CLAUSE_LINK:
14798 t = OMP_CLAUSE_DECL (c);
14799 if (TREE_CODE (t) == FUNCTION_DECL
14800 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14802 else if (!VAR_P (t))
14804 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14805 error_at (OMP_CLAUSE_LOCATION (c),
14806 "%qE is neither a variable nor a function name in "
14807 "clause %qs", t,
14808 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14809 else
14810 error_at (OMP_CLAUSE_LOCATION (c),
14811 "%qE is not a variable in clause %qs", t,
14812 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14813 remove = true;
14815 else if (DECL_THREAD_LOCAL_P (t))
14817 error_at (OMP_CLAUSE_LOCATION (c),
14818 "%qD is threadprivate variable in %qs clause", t,
14819 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14820 remove = true;
14822 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14824 error_at (OMP_CLAUSE_LOCATION (c),
14825 "%qD does not have a mappable type in %qs clause", t,
14826 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14827 remove = true;
14829 if (remove)
14830 break;
14831 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
14833 error_at (OMP_CLAUSE_LOCATION (c),
14834 "%qE appears more than once on the same "
14835 "%<declare target%> directive", t);
14836 remove = true;
14838 else
14839 bitmap_set_bit (&generic_head, DECL_UID (t));
14840 break;
14842 case OMP_CLAUSE_UNIFORM:
14843 t = OMP_CLAUSE_DECL (c);
14844 if (TREE_CODE (t) != PARM_DECL)
14846 if (DECL_P (t))
14847 error_at (OMP_CLAUSE_LOCATION (c),
14848 "%qD is not an argument in %<uniform%> clause", t);
14849 else
14850 error_at (OMP_CLAUSE_LOCATION (c),
14851 "%qE is not an argument in %<uniform%> clause", t);
14852 remove = true;
14853 break;
14855 /* map_head bitmap is used as uniform_head if declare_simd. */
14856 bitmap_set_bit (&map_head, DECL_UID (t));
14857 goto check_dup_generic;
14859 case OMP_CLAUSE_IS_DEVICE_PTR:
14860 case OMP_CLAUSE_USE_DEVICE_PTR:
14861 t = OMP_CLAUSE_DECL (c);
14862 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14864 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14865 && ort == C_ORT_OMP)
14867 error_at (OMP_CLAUSE_LOCATION (c),
14868 "%qs variable is not a pointer",
14869 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14870 remove = true;
14872 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14874 error_at (OMP_CLAUSE_LOCATION (c),
14875 "%qs variable is neither a pointer nor an array",
14876 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14877 remove = true;
14880 goto check_dup_generic;
14882 case OMP_CLAUSE_USE_DEVICE_ADDR:
14883 t = OMP_CLAUSE_DECL (c);
14884 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14885 c_mark_addressable (t);
14886 goto check_dup_generic;
14888 case OMP_CLAUSE_NOWAIT:
14889 if (copyprivate_seen)
14891 error_at (OMP_CLAUSE_LOCATION (c),
14892 "%<nowait%> clause must not be used together "
14893 "with %<copyprivate%>");
14894 remove = true;
14895 break;
14897 nowait_clause = pc;
14898 pc = &OMP_CLAUSE_CHAIN (c);
14899 continue;
14901 case OMP_CLAUSE_ORDER:
14902 if (ordered_clause)
14904 error_at (OMP_CLAUSE_LOCATION (c),
14905 "%<order%> clause must not be used together "
14906 "with %<ordered%>");
14907 remove = true;
14908 break;
14910 else if (order_clause)
14912 /* Silently remove duplicates. */
14913 remove = true;
14914 break;
14916 order_clause = pc;
14917 pc = &OMP_CLAUSE_CHAIN (c);
14918 continue;
14920 case OMP_CLAUSE_IF:
14921 case OMP_CLAUSE_NUM_THREADS:
14922 case OMP_CLAUSE_NUM_TEAMS:
14923 case OMP_CLAUSE_THREAD_LIMIT:
14924 case OMP_CLAUSE_DEFAULT:
14925 case OMP_CLAUSE_UNTIED:
14926 case OMP_CLAUSE_COLLAPSE:
14927 case OMP_CLAUSE_FINAL:
14928 case OMP_CLAUSE_MERGEABLE:
14929 case OMP_CLAUSE_DEVICE:
14930 case OMP_CLAUSE_DIST_SCHEDULE:
14931 case OMP_CLAUSE_PARALLEL:
14932 case OMP_CLAUSE_FOR:
14933 case OMP_CLAUSE_SECTIONS:
14934 case OMP_CLAUSE_TASKGROUP:
14935 case OMP_CLAUSE_PROC_BIND:
14936 case OMP_CLAUSE_DEVICE_TYPE:
14937 case OMP_CLAUSE_PRIORITY:
14938 case OMP_CLAUSE_GRAINSIZE:
14939 case OMP_CLAUSE_NUM_TASKS:
14940 case OMP_CLAUSE_THREADS:
14941 case OMP_CLAUSE_SIMD:
14942 case OMP_CLAUSE_HINT:
14943 case OMP_CLAUSE_DEFAULTMAP:
14944 case OMP_CLAUSE_BIND:
14945 case OMP_CLAUSE_NUM_GANGS:
14946 case OMP_CLAUSE_NUM_WORKERS:
14947 case OMP_CLAUSE_VECTOR_LENGTH:
14948 case OMP_CLAUSE_ASYNC:
14949 case OMP_CLAUSE_WAIT:
14950 case OMP_CLAUSE_AUTO:
14951 case OMP_CLAUSE_INDEPENDENT:
14952 case OMP_CLAUSE_SEQ:
14953 case OMP_CLAUSE_GANG:
14954 case OMP_CLAUSE_WORKER:
14955 case OMP_CLAUSE_VECTOR:
14956 case OMP_CLAUSE_TILE:
14957 case OMP_CLAUSE_IF_PRESENT:
14958 case OMP_CLAUSE_FINALIZE:
14959 pc = &OMP_CLAUSE_CHAIN (c);
14960 continue;
14962 case OMP_CLAUSE_NOGROUP:
14963 nogroup_seen = pc;
14964 pc = &OMP_CLAUSE_CHAIN (c);
14965 continue;
14967 case OMP_CLAUSE_SCHEDULE:
14968 schedule_clause = c;
14969 pc = &OMP_CLAUSE_CHAIN (c);
14970 continue;
14972 case OMP_CLAUSE_ORDERED:
14973 ordered_clause = c;
14974 if (order_clause)
14976 error_at (OMP_CLAUSE_LOCATION (*order_clause),
14977 "%<order%> clause must not be used together "
14978 "with %<ordered%>");
14979 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
14980 order_clause = NULL;
14982 pc = &OMP_CLAUSE_CHAIN (c);
14983 continue;
14985 case OMP_CLAUSE_SAFELEN:
14986 safelen = c;
14987 pc = &OMP_CLAUSE_CHAIN (c);
14988 continue;
14989 case OMP_CLAUSE_SIMDLEN:
14990 simdlen = c;
14991 pc = &OMP_CLAUSE_CHAIN (c);
14992 continue;
14994 case OMP_CLAUSE_INBRANCH:
14995 case OMP_CLAUSE_NOTINBRANCH:
14996 if (branch_seen)
14998 error_at (OMP_CLAUSE_LOCATION (c),
14999 "%<inbranch%> clause is incompatible with "
15000 "%<notinbranch%>");
15001 remove = true;
15002 break;
15004 branch_seen = true;
15005 pc = &OMP_CLAUSE_CHAIN (c);
15006 continue;
15008 case OMP_CLAUSE_INCLUSIVE:
15009 case OMP_CLAUSE_EXCLUSIVE:
15010 need_complete = true;
15011 need_implicitly_determined = true;
15012 t = OMP_CLAUSE_DECL (c);
15013 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15015 error_at (OMP_CLAUSE_LOCATION (c),
15016 "%qE is not a variable in clause %qs", t,
15017 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15018 remove = true;
15020 break;
15022 default:
15023 gcc_unreachable ();
15026 if (!remove)
15028 t = OMP_CLAUSE_DECL (c);
15030 if (need_complete)
15032 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15033 if (t == error_mark_node)
15034 remove = true;
15037 if (need_implicitly_determined)
15039 const char *share_name = NULL;
15041 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15042 share_name = "threadprivate";
15043 else switch (c_omp_predetermined_sharing (t))
15045 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15046 break;
15047 case OMP_CLAUSE_DEFAULT_SHARED:
15048 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15049 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15050 && c_omp_predefined_variable (t))
15051 /* The __func__ variable and similar function-local
15052 predefined variables may be listed in a shared or
15053 firstprivate clause. */
15054 break;
15055 share_name = "shared";
15056 break;
15057 case OMP_CLAUSE_DEFAULT_PRIVATE:
15058 share_name = "private";
15059 break;
15060 default:
15061 gcc_unreachable ();
15063 if (share_name)
15065 error_at (OMP_CLAUSE_LOCATION (c),
15066 "%qE is predetermined %qs for %qs",
15067 t, share_name,
15068 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15069 remove = true;
15071 else if (TREE_READONLY (t)
15072 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15073 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15075 error_at (OMP_CLAUSE_LOCATION (c),
15076 "%<const%> qualified %qE may appear only in "
15077 "%<shared%> or %<firstprivate%> clauses", t);
15078 remove = true;
15083 if (remove)
15084 *pc = OMP_CLAUSE_CHAIN (c);
15085 else
15086 pc = &OMP_CLAUSE_CHAIN (c);
15089 if (simdlen
15090 && safelen
15091 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15092 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15094 error_at (OMP_CLAUSE_LOCATION (simdlen),
15095 "%<simdlen%> clause value is bigger than "
15096 "%<safelen%> clause value");
15097 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15098 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15101 if (ordered_clause
15102 && schedule_clause
15103 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15104 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15106 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15107 "%<nonmonotonic%> schedule modifier specified together "
15108 "with %<ordered%> clause");
15109 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15110 = (enum omp_clause_schedule_kind)
15111 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15112 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15115 if (reduction_seen < 0 && ordered_clause)
15117 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15118 "%qs clause specified together with %<inscan%> "
15119 "%<reduction%> clause", "ordered");
15120 reduction_seen = -2;
15123 if (reduction_seen < 0 && schedule_clause)
15125 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15126 "%qs clause specified together with %<inscan%> "
15127 "%<reduction%> clause", "schedule");
15128 reduction_seen = -2;
15131 if (linear_variable_step_check || reduction_seen == -2)
15132 for (pc = &clauses, c = clauses; c ; c = *pc)
15134 bool remove = false;
15135 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15136 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15137 && !bitmap_bit_p (&map_head,
15138 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15140 error_at (OMP_CLAUSE_LOCATION (c),
15141 "%<linear%> clause step is a parameter %qD not "
15142 "specified in %<uniform%> clause",
15143 OMP_CLAUSE_LINEAR_STEP (c));
15144 remove = true;
15146 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
15147 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15149 if (remove)
15150 *pc = OMP_CLAUSE_CHAIN (c);
15151 else
15152 pc = &OMP_CLAUSE_CHAIN (c);
15155 if (nogroup_seen && reduction_seen)
15157 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15158 "%<nogroup%> clause must not be used together with "
15159 "%<reduction%> clause");
15160 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15163 bitmap_obstack_release (NULL);
15164 return clauses;
15167 /* Return code to initialize DST with a copy constructor from SRC.
15168 C doesn't have copy constructors nor assignment operators, only for
15169 _Atomic vars we need to perform __atomic_load from src into a temporary
15170 followed by __atomic_store of the temporary to dst. */
15172 tree
15173 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15175 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15176 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15178 location_t loc = OMP_CLAUSE_LOCATION (clause);
15179 tree type = TREE_TYPE (dst);
15180 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15181 tree tmp = create_tmp_var (nonatomic_type);
15182 tree tmp_addr = build_fold_addr_expr (tmp);
15183 TREE_ADDRESSABLE (tmp) = 1;
15184 TREE_NO_WARNING (tmp) = 1;
15185 tree src_addr = build_fold_addr_expr (src);
15186 tree dst_addr = build_fold_addr_expr (dst);
15187 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15188 vec<tree, va_gc> *params;
15189 /* Expansion of a generic atomic load may require an addition
15190 element, so allocate enough to prevent a resize. */
15191 vec_alloc (params, 4);
15193 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15194 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15195 params->quick_push (src_addr);
15196 params->quick_push (tmp_addr);
15197 params->quick_push (seq_cst);
15198 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15200 vec_alloc (params, 4);
15202 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15203 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15204 params->quick_push (dst_addr);
15205 params->quick_push (tmp_addr);
15206 params->quick_push (seq_cst);
15207 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15208 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15211 /* Create a transaction node. */
15213 tree
15214 c_finish_transaction (location_t loc, tree block, int flags)
15216 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15217 if (flags & TM_STMT_ATTR_OUTER)
15218 TRANSACTION_EXPR_OUTER (stmt) = 1;
15219 if (flags & TM_STMT_ATTR_RELAXED)
15220 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15221 return add_stmt (stmt);
15224 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15225 down to the element type of an array. If ORIG_QUAL_TYPE is not
15226 NULL, then it should be used as the qualified type
15227 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15228 preserve information about the typedef name from which an array
15229 type was derived). */
15231 tree
15232 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15233 size_t orig_qual_indirect)
15235 if (type == error_mark_node)
15236 return type;
15238 if (TREE_CODE (type) == ARRAY_TYPE)
15240 tree t;
15241 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15242 type_quals, orig_qual_type,
15243 orig_qual_indirect - 1);
15245 /* See if we already have an identically qualified type. */
15246 if (orig_qual_type && orig_qual_indirect == 0)
15247 t = orig_qual_type;
15248 else
15249 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15251 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15252 && TYPE_NAME (t) == TYPE_NAME (type)
15253 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15254 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15255 TYPE_ATTRIBUTES (type)))
15256 break;
15258 if (!t)
15260 tree domain = TYPE_DOMAIN (type);
15262 t = build_variant_type_copy (type);
15263 TREE_TYPE (t) = element_type;
15265 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15266 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15267 SET_TYPE_STRUCTURAL_EQUALITY (t);
15268 else if (TYPE_CANONICAL (element_type) != element_type
15269 || (domain && TYPE_CANONICAL (domain) != domain))
15271 tree unqualified_canon
15272 = build_array_type (TYPE_CANONICAL (element_type),
15273 domain? TYPE_CANONICAL (domain)
15274 : NULL_TREE);
15275 if (TYPE_REVERSE_STORAGE_ORDER (type))
15277 unqualified_canon
15278 = build_distinct_type_copy (unqualified_canon);
15279 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15281 TYPE_CANONICAL (t)
15282 = c_build_qualified_type (unqualified_canon, type_quals);
15284 else
15285 TYPE_CANONICAL (t) = t;
15287 return t;
15290 /* A restrict-qualified pointer type must be a pointer to object or
15291 incomplete type. Note that the use of POINTER_TYPE_P also allows
15292 REFERENCE_TYPEs, which is appropriate for C++. */
15293 if ((type_quals & TYPE_QUAL_RESTRICT)
15294 && (!POINTER_TYPE_P (type)
15295 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15297 error ("invalid use of %<restrict%>");
15298 type_quals &= ~TYPE_QUAL_RESTRICT;
15301 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15302 ? orig_qual_type
15303 : build_qualified_type (type, type_quals));
15304 /* A variant type does not inherit the list of incomplete vars from the
15305 type main variant. */
15306 if ((RECORD_OR_UNION_TYPE_P (var_type)
15307 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15308 && TYPE_MAIN_VARIANT (var_type) != var_type)
15309 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15310 return var_type;
15313 /* Build a VA_ARG_EXPR for the C parser. */
15315 tree
15316 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15318 if (error_operand_p (type))
15319 return error_mark_node;
15320 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15321 order because it takes the address of the expression. */
15322 else if (handled_component_p (expr)
15323 && reverse_storage_order_for_component_p (expr))
15325 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15326 return error_mark_node;
15328 else if (!COMPLETE_TYPE_P (type))
15330 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15331 "type %qT", type);
15332 return error_mark_node;
15334 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15335 warning_at (loc2, OPT_Wc___compat,
15336 "C++ requires promoted type, not enum type, in %<va_arg%>");
15337 return build_va_arg (loc2, expr, type);
15340 /* Return truthvalue of whether T1 is the same tree structure as T2.
15341 Return 1 if they are the same. Return false if they are different. */
15343 bool
15344 c_tree_equal (tree t1, tree t2)
15346 enum tree_code code1, code2;
15348 if (t1 == t2)
15349 return true;
15350 if (!t1 || !t2)
15351 return false;
15353 for (code1 = TREE_CODE (t1);
15354 CONVERT_EXPR_CODE_P (code1)
15355 || code1 == NON_LVALUE_EXPR;
15356 code1 = TREE_CODE (t1))
15357 t1 = TREE_OPERAND (t1, 0);
15358 for (code2 = TREE_CODE (t2);
15359 CONVERT_EXPR_CODE_P (code2)
15360 || code2 == NON_LVALUE_EXPR;
15361 code2 = TREE_CODE (t2))
15362 t2 = TREE_OPERAND (t2, 0);
15364 /* They might have become equal now. */
15365 if (t1 == t2)
15366 return true;
15368 if (code1 != code2)
15369 return false;
15371 switch (code1)
15373 case INTEGER_CST:
15374 return wi::to_wide (t1) == wi::to_wide (t2);
15376 case REAL_CST:
15377 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15379 case STRING_CST:
15380 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15381 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15382 TREE_STRING_LENGTH (t1));
15384 case FIXED_CST:
15385 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15386 TREE_FIXED_CST (t2));
15388 case COMPLEX_CST:
15389 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15390 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15392 case VECTOR_CST:
15393 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15395 case CONSTRUCTOR:
15396 /* We need to do this when determining whether or not two
15397 non-type pointer to member function template arguments
15398 are the same. */
15399 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15400 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15401 return false;
15403 tree field, value;
15404 unsigned int i;
15405 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15407 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15408 if (!c_tree_equal (field, elt2->index)
15409 || !c_tree_equal (value, elt2->value))
15410 return false;
15413 return true;
15415 case TREE_LIST:
15416 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15417 return false;
15418 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15419 return false;
15420 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15422 case SAVE_EXPR:
15423 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15425 case CALL_EXPR:
15427 tree arg1, arg2;
15428 call_expr_arg_iterator iter1, iter2;
15429 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15430 return false;
15431 for (arg1 = first_call_expr_arg (t1, &iter1),
15432 arg2 = first_call_expr_arg (t2, &iter2);
15433 arg1 && arg2;
15434 arg1 = next_call_expr_arg (&iter1),
15435 arg2 = next_call_expr_arg (&iter2))
15436 if (!c_tree_equal (arg1, arg2))
15437 return false;
15438 if (arg1 || arg2)
15439 return false;
15440 return true;
15443 case TARGET_EXPR:
15445 tree o1 = TREE_OPERAND (t1, 0);
15446 tree o2 = TREE_OPERAND (t2, 0);
15448 /* Special case: if either target is an unallocated VAR_DECL,
15449 it means that it's going to be unified with whatever the
15450 TARGET_EXPR is really supposed to initialize, so treat it
15451 as being equivalent to anything. */
15452 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15453 && !DECL_RTL_SET_P (o1))
15454 /*Nop*/;
15455 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15456 && !DECL_RTL_SET_P (o2))
15457 /*Nop*/;
15458 else if (!c_tree_equal (o1, o2))
15459 return false;
15461 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15464 case COMPONENT_REF:
15465 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15466 return false;
15467 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15469 case PARM_DECL:
15470 case VAR_DECL:
15471 case CONST_DECL:
15472 case FIELD_DECL:
15473 case FUNCTION_DECL:
15474 case IDENTIFIER_NODE:
15475 case SSA_NAME:
15476 return false;
15478 case TREE_VEC:
15480 unsigned ix;
15481 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15482 return false;
15483 for (ix = TREE_VEC_LENGTH (t1); ix--;)
15484 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15485 TREE_VEC_ELT (t2, ix)))
15486 return false;
15487 return true;
15490 default:
15491 break;
15494 switch (TREE_CODE_CLASS (code1))
15496 case tcc_unary:
15497 case tcc_binary:
15498 case tcc_comparison:
15499 case tcc_expression:
15500 case tcc_vl_exp:
15501 case tcc_reference:
15502 case tcc_statement:
15504 int i, n = TREE_OPERAND_LENGTH (t1);
15506 switch (code1)
15508 case PREINCREMENT_EXPR:
15509 case PREDECREMENT_EXPR:
15510 case POSTINCREMENT_EXPR:
15511 case POSTDECREMENT_EXPR:
15512 n = 1;
15513 break;
15514 case ARRAY_REF:
15515 n = 2;
15516 break;
15517 default:
15518 break;
15521 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15522 && n != TREE_OPERAND_LENGTH (t2))
15523 return false;
15525 for (i = 0; i < n; ++i)
15526 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15527 return false;
15529 return true;
15532 case tcc_type:
15533 return comptypes (t1, t2);
15534 default:
15535 gcc_unreachable ();
15537 /* We can get here with --disable-checking. */
15538 return false;
15541 /* Returns true when the function declaration FNDECL is implicit,
15542 introduced as a result of a call to an otherwise undeclared
15543 function, and false otherwise. */
15545 bool
15546 c_decl_implicit (const_tree fndecl)
15548 return C_DECL_IMPLICIT (fndecl);