Eliminate source_location in favor of location_t
[official-gcc.git] / gcc / c / c-typeck.c
blob5d4e973c89e4da532848683e95133b599aef5253
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2018 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 /* The argument of last parsed sizeof expression, only to be tested
75 if expr.original_code == SIZEOF_EXPR. */
76 tree c_last_sizeof_arg;
77 location_t c_last_sizeof_loc;
79 /* Nonzero if we might need to print a "missing braces around
80 initializer" message within this initializer. */
81 static int found_missing_braces;
83 static int require_constant_value;
84 static int require_constant_elements;
86 static bool null_pointer_constant_p (const_tree);
87 static tree qualify_type (tree, tree);
88 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90 static int comp_target_types (location_t, tree, tree);
91 static int function_types_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
94 static tree lookup_field (tree, tree);
95 static int convert_arguments (location_t, vec<location_t>, tree,
96 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
97 tree);
98 static tree pointer_diff (location_t, tree, tree, tree *);
99 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
100 enum impl_conv, bool, tree, tree, int);
101 static tree valid_compound_expr_initializer (tree, tree);
102 static void push_string (const char *);
103 static void push_member_name (tree);
104 static int spelling_length (void);
105 static char *print_spelling (char *);
106 static void warning_init (location_t, int, const char *);
107 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
108 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
109 bool, struct obstack *);
110 static void output_pending_init_elements (int, struct obstack *);
111 static bool set_designator (location_t, bool, struct obstack *);
112 static void push_range_stack (tree, struct obstack *);
113 static void add_pending_init (location_t, tree, tree, tree, bool,
114 struct obstack *);
115 static void set_nonincremental_init (struct obstack *);
116 static void set_nonincremental_init_from_string (tree, struct obstack *);
117 static tree find_init_member (tree, struct obstack *);
118 static void readonly_warning (tree, enum lvalue_use);
119 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
120 static void record_maybe_used_decl (tree);
121 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
123 /* Return true if EXP is a null pointer constant, false otherwise. */
125 static bool
126 null_pointer_constant_p (const_tree expr)
128 /* This should really operate on c_expr structures, but they aren't
129 yet available everywhere required. */
130 tree type = TREE_TYPE (expr);
131 return (TREE_CODE (expr) == INTEGER_CST
132 && !TREE_OVERFLOW (expr)
133 && integer_zerop (expr)
134 && (INTEGRAL_TYPE_P (type)
135 || (TREE_CODE (type) == POINTER_TYPE
136 && VOID_TYPE_P (TREE_TYPE (type))
137 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
140 /* EXPR may appear in an unevaluated part of an integer constant
141 expression, but not in an evaluated part. Wrap it in a
142 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
143 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
145 static tree
146 note_integer_operands (tree expr)
148 tree ret;
149 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
151 ret = copy_node (expr);
152 TREE_OVERFLOW (ret) = 1;
154 else
156 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
157 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
159 return ret;
162 /* Having checked whether EXPR may appear in an unevaluated part of an
163 integer constant expression and found that it may, remove any
164 C_MAYBE_CONST_EXPR noting this fact and return the resulting
165 expression. */
167 static inline tree
168 remove_c_maybe_const_expr (tree expr)
170 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
171 return C_MAYBE_CONST_EXPR_EXPR (expr);
172 else
173 return expr;
176 \f/* This is a cache to hold if two types are compatible or not. */
178 struct tagged_tu_seen_cache {
179 const struct tagged_tu_seen_cache * next;
180 const_tree t1;
181 const_tree t2;
182 /* The return value of tagged_types_tu_compatible_p if we had seen
183 these two types already. */
184 int val;
187 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
188 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
190 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
191 does not have an incomplete type. (That includes void types.)
192 LOC is the location of the use. */
194 tree
195 require_complete_type (location_t loc, tree value)
197 tree type = TREE_TYPE (value);
199 if (error_operand_p (value))
200 return error_mark_node;
202 /* First, detect a valid value with a complete type. */
203 if (COMPLETE_TYPE_P (type))
204 return value;
206 c_incomplete_type_error (loc, value, type);
207 return error_mark_node;
210 /* Print an error message for invalid use of an incomplete type.
211 VALUE is the expression that was used (or 0 if that isn't known)
212 and TYPE is the type that was invalid. LOC is the location for
213 the error. */
215 void
216 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
218 /* Avoid duplicate error message. */
219 if (TREE_CODE (type) == ERROR_MARK)
220 return;
222 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
223 error_at (loc, "%qD has an incomplete type %qT", value, type);
224 else
226 retry:
227 /* We must print an error message. Be clever about what it says. */
229 switch (TREE_CODE (type))
231 case RECORD_TYPE:
232 case UNION_TYPE:
233 case ENUMERAL_TYPE:
234 break;
236 case VOID_TYPE:
237 error_at (loc, "invalid use of void expression");
238 return;
240 case ARRAY_TYPE:
241 if (TYPE_DOMAIN (type))
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 error_at (loc, "invalid use of flexible array member");
246 return;
248 type = TREE_TYPE (type);
249 goto retry;
251 error_at (loc, "invalid use of array with unspecified bounds");
252 return;
254 default:
255 gcc_unreachable ();
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
259 error_at (loc, "invalid use of undefined type %qT", type);
260 else
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
262 error_at (loc, "invalid use of incomplete typedef %qT", type);
266 /* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
269 tree
270 c_type_promotes_to (tree type)
272 tree ret = NULL_TREE;
274 if (TYPE_MAIN_VARIANT (type) == float_type_node)
275 ret = double_type_node;
276 else if (c_promoting_integer_type_p (type))
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type)
280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
281 ret = unsigned_type_node;
282 else
283 ret = integer_type_node;
286 if (ret != NULL_TREE)
287 return (TYPE_ATOMIC (type)
288 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
289 : ret);
291 return type;
294 /* Return true if between two named address spaces, whether there is a superset
295 named address space that encompasses both address spaces. If there is a
296 superset, return which address space is the superset. */
298 static bool
299 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
301 if (as1 == as2)
303 *common = as1;
304 return true;
306 else if (targetm.addr_space.subset_p (as1, as2))
308 *common = as2;
309 return true;
311 else if (targetm.addr_space.subset_p (as2, as1))
313 *common = as1;
314 return true;
316 else
317 return false;
320 /* Return a variant of TYPE which has all the type qualifiers of LIKE
321 as well as those of TYPE. */
323 static tree
324 qualify_type (tree type, tree like)
326 addr_space_t as_type = TYPE_ADDR_SPACE (type);
327 addr_space_t as_like = TYPE_ADDR_SPACE (like);
328 addr_space_t as_common;
330 /* If the two named address spaces are different, determine the common
331 superset address space. If there isn't one, raise an error. */
332 if (!addr_space_superset (as_type, as_like, &as_common))
334 as_common = as_type;
335 error ("%qT and %qT are in disjoint named address spaces",
336 type, like);
339 return c_build_qualified_type (type,
340 TYPE_QUALS_NO_ADDR_SPACE (type)
341 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
342 | ENCODE_QUAL_ADDR_SPACE (as_common));
345 /* Return true iff the given tree T is a variable length array. */
347 bool
348 c_vla_type_p (const_tree t)
350 if (TREE_CODE (t) == ARRAY_TYPE
351 && C_TYPE_VARIABLE_SIZE (t))
352 return true;
353 return false;
356 /* Return the composite type of two compatible types.
358 We assume that comptypes has already been done and returned
359 nonzero; if that isn't so, this may crash. In particular, we
360 assume that qualifiers match. */
362 tree
363 composite_type (tree t1, tree t2)
365 enum tree_code code1;
366 enum tree_code code2;
367 tree attributes;
369 /* Save time if the two types are the same. */
371 if (t1 == t2) return t1;
373 /* If one type is nonsense, use the other. */
374 if (t1 == error_mark_node)
375 return t2;
376 if (t2 == error_mark_node)
377 return t1;
379 code1 = TREE_CODE (t1);
380 code2 = TREE_CODE (t2);
382 /* Merge the attributes. */
383 attributes = targetm.merge_type_attributes (t1, t2);
385 /* If one is an enumerated type and the other is the compatible
386 integer type, the composite type might be either of the two
387 (DR#013 question 3). For consistency, use the enumerated type as
388 the composite type. */
390 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
391 return t1;
392 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
393 return t2;
395 gcc_assert (code1 == code2);
397 switch (code1)
399 case POINTER_TYPE:
400 /* For two pointers, do this recursively on the target type. */
402 tree pointed_to_1 = TREE_TYPE (t1);
403 tree pointed_to_2 = TREE_TYPE (t2);
404 tree target = composite_type (pointed_to_1, pointed_to_2);
405 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
406 t1 = build_type_attribute_variant (t1, attributes);
407 return qualify_type (t1, t2);
410 case ARRAY_TYPE:
412 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
413 int quals;
414 tree unqual_elt;
415 tree d1 = TYPE_DOMAIN (t1);
416 tree d2 = TYPE_DOMAIN (t2);
417 bool d1_variable, d2_variable;
418 bool d1_zero, d2_zero;
419 bool t1_complete, t2_complete;
421 /* We should not have any type quals on arrays at all. */
422 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
423 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
425 t1_complete = COMPLETE_TYPE_P (t1);
426 t2_complete = COMPLETE_TYPE_P (t2);
428 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
429 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
431 d1_variable = (!d1_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
434 d2_variable = (!d2_zero
435 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
436 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
437 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
438 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
440 /* Save space: see if the result is identical to one of the args. */
441 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
442 && (d2_variable || d2_zero || !d1_variable))
443 return build_type_attribute_variant (t1, attributes);
444 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
445 && (d1_variable || d1_zero || !d2_variable))
446 return build_type_attribute_variant (t2, attributes);
448 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
449 return build_type_attribute_variant (t1, attributes);
450 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
451 return build_type_attribute_variant (t2, attributes);
453 /* Merge the element types, and have a size if either arg has
454 one. We may have qualifiers on the element types. To set
455 up TYPE_MAIN_VARIANT correctly, we need to form the
456 composite of the unqualified types and add the qualifiers
457 back at the end. */
458 quals = TYPE_QUALS (strip_array_types (elt));
459 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
460 t1 = build_array_type (unqual_elt,
461 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
462 && (d2_variable
463 || d2_zero
464 || !d1_variable))
465 ? t1
466 : t2));
467 /* Ensure a composite type involving a zero-length array type
468 is a zero-length type not an incomplete type. */
469 if (d1_zero && d2_zero
470 && (t1_complete || t2_complete)
471 && !COMPLETE_TYPE_P (t1))
473 TYPE_SIZE (t1) = bitsize_zero_node;
474 TYPE_SIZE_UNIT (t1) = size_zero_node;
476 t1 = c_build_qualified_type (t1, quals);
477 return build_type_attribute_variant (t1, attributes);
480 case ENUMERAL_TYPE:
481 case RECORD_TYPE:
482 case UNION_TYPE:
483 if (attributes != NULL)
485 /* Try harder not to create a new aggregate type. */
486 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
487 return t1;
488 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
489 return t2;
491 return build_type_attribute_variant (t1, attributes);
493 case FUNCTION_TYPE:
494 /* Function types: prefer the one that specified arg types.
495 If both do, merge the arg types. Also merge the return types. */
497 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
498 tree p1 = TYPE_ARG_TYPES (t1);
499 tree p2 = TYPE_ARG_TYPES (t2);
500 int len;
501 tree newargs, n;
502 int i;
504 /* Save space: see if the result is identical to one of the args. */
505 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
506 return build_type_attribute_variant (t1, attributes);
507 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
508 return build_type_attribute_variant (t2, attributes);
510 /* Simple way if one arg fails to specify argument types. */
511 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
517 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
519 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
520 t1 = build_type_attribute_variant (t1, attributes);
521 return qualify_type (t1, t2);
524 /* If both args specify argument types, we must merge the two
525 lists, argument by argument. */
527 for (len = 0, newargs = p1;
528 newargs && newargs != void_list_node;
529 len++, newargs = TREE_CHAIN (newargs))
532 for (i = 0; i < len; i++)
533 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
535 n = newargs;
537 for (; p1 && p1 != void_list_node;
538 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
540 /* A null type means arg type is not specified.
541 Take whatever the other function type has. */
542 if (TREE_VALUE (p1) == NULL_TREE)
544 TREE_VALUE (n) = TREE_VALUE (p2);
545 goto parm_done;
547 if (TREE_VALUE (p2) == NULL_TREE)
549 TREE_VALUE (n) = TREE_VALUE (p1);
550 goto parm_done;
553 /* Given wait (union {union wait *u; int *i} *)
554 and wait (union wait *),
555 prefer union wait * as type of parm. */
556 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
557 && TREE_VALUE (p1) != TREE_VALUE (p2))
559 tree memb;
560 tree mv2 = TREE_VALUE (p2);
561 if (mv2 && mv2 != error_mark_node
562 && TREE_CODE (mv2) != ARRAY_TYPE)
563 mv2 = TYPE_MAIN_VARIANT (mv2);
564 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
565 memb; memb = DECL_CHAIN (memb))
567 tree mv3 = TREE_TYPE (memb);
568 if (mv3 && mv3 != error_mark_node
569 && TREE_CODE (mv3) != ARRAY_TYPE)
570 mv3 = TYPE_MAIN_VARIANT (mv3);
571 if (comptypes (mv3, mv2))
573 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
574 TREE_VALUE (p2));
575 pedwarn (input_location, OPT_Wpedantic,
576 "function types not truly compatible in ISO C");
577 goto parm_done;
581 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
582 && TREE_VALUE (p2) != TREE_VALUE (p1))
584 tree memb;
585 tree mv1 = TREE_VALUE (p1);
586 if (mv1 && mv1 != error_mark_node
587 && TREE_CODE (mv1) != ARRAY_TYPE)
588 mv1 = TYPE_MAIN_VARIANT (mv1);
589 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
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, mv1))
598 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
599 TREE_VALUE (p1));
600 pedwarn (input_location, OPT_Wpedantic,
601 "function types not truly compatible in ISO C");
602 goto parm_done;
606 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
607 parm_done: ;
610 t1 = build_function_type (valtype, newargs);
611 t1 = qualify_type (t1, t2);
613 /* FALLTHRU */
615 default:
616 return build_type_attribute_variant (t1, attributes);
621 /* Return the type of a conditional expression between pointers to
622 possibly differently qualified versions of compatible types.
624 We assume that comp_target_types has already been done and returned
625 nonzero; if that isn't so, this may crash. */
627 static tree
628 common_pointer_type (tree t1, tree t2)
630 tree attributes;
631 tree pointed_to_1, mv1;
632 tree pointed_to_2, mv2;
633 tree target;
634 unsigned target_quals;
635 addr_space_t as1, as2, as_common;
636 int quals1, quals2;
638 /* Save time if the two types are the same. */
640 if (t1 == t2) return t1;
642 /* If one type is nonsense, use the other. */
643 if (t1 == error_mark_node)
644 return t2;
645 if (t2 == error_mark_node)
646 return t1;
648 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
649 && TREE_CODE (t2) == POINTER_TYPE);
651 /* Merge the attributes. */
652 attributes = targetm.merge_type_attributes (t1, t2);
654 /* Find the composite type of the target types, and combine the
655 qualifiers of the two types' targets. Do not lose qualifiers on
656 array element types by taking the TYPE_MAIN_VARIANT. */
657 mv1 = pointed_to_1 = TREE_TYPE (t1);
658 mv2 = pointed_to_2 = TREE_TYPE (t2);
659 if (TREE_CODE (mv1) != ARRAY_TYPE)
660 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
661 if (TREE_CODE (mv2) != ARRAY_TYPE)
662 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
663 target = composite_type (mv1, mv2);
665 /* Strip array types to get correct qualifier for pointers to arrays */
666 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
667 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
669 /* For function types do not merge const qualifiers, but drop them
670 if used inconsistently. The middle-end uses these to mark const
671 and noreturn functions. */
672 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
673 target_quals = (quals1 & quals2);
674 else
675 target_quals = (quals1 | quals2);
677 /* If the two named address spaces are different, determine the common
678 superset address space. This is guaranteed to exist due to the
679 assumption that comp_target_type returned non-zero. */
680 as1 = TYPE_ADDR_SPACE (pointed_to_1);
681 as2 = TYPE_ADDR_SPACE (pointed_to_2);
682 if (!addr_space_superset (as1, as2, &as_common))
683 gcc_unreachable ();
685 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
687 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
688 return build_type_attribute_variant (t1, attributes);
691 /* Return the common type for two arithmetic types under the usual
692 arithmetic conversions. The default conversions have already been
693 applied, and enumerated types converted to their compatible integer
694 types. The resulting type is unqualified and has no attributes.
696 This is the type for the result of most arithmetic operations
697 if the operands have the given two types. */
699 static tree
700 c_common_type (tree t1, tree t2)
702 enum tree_code code1;
703 enum tree_code code2;
705 /* If one type is nonsense, use the other. */
706 if (t1 == error_mark_node)
707 return t2;
708 if (t2 == error_mark_node)
709 return t1;
711 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
712 t1 = TYPE_MAIN_VARIANT (t1);
714 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
715 t2 = TYPE_MAIN_VARIANT (t2);
717 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
718 t1 = build_type_attribute_variant (t1, NULL_TREE);
720 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
721 t2 = build_type_attribute_variant (t2, NULL_TREE);
723 /* Save time if the two types are the same. */
725 if (t1 == t2) return t1;
727 code1 = TREE_CODE (t1);
728 code2 = TREE_CODE (t2);
730 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
731 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
732 || code1 == INTEGER_TYPE);
733 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
734 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
735 || code2 == INTEGER_TYPE);
737 /* When one operand is a decimal float type, the other operand cannot be
738 a generic float type or a complex type. We also disallow vector types
739 here. */
740 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
741 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
743 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
745 error ("can%'t mix operands of decimal float and vector types");
746 return error_mark_node;
748 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
750 error ("can%'t mix operands of decimal float and complex types");
751 return error_mark_node;
753 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
755 error ("can%'t mix operands of decimal float and other float types");
756 return error_mark_node;
760 /* If one type is a vector type, return that type. (How the usual
761 arithmetic conversions apply to the vector types extension is not
762 precisely specified.) */
763 if (code1 == VECTOR_TYPE)
764 return t1;
766 if (code2 == VECTOR_TYPE)
767 return t2;
769 /* If one type is complex, form the common type of the non-complex
770 components, then make that complex. Use T1 or T2 if it is the
771 required type. */
772 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
774 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
775 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
776 tree subtype = c_common_type (subtype1, subtype2);
778 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
779 return t1;
780 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
781 return t2;
782 else
783 return build_complex_type (subtype);
786 /* If only one is real, use it as the result. */
788 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
789 return t1;
791 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
792 return t2;
794 /* If both are real and either are decimal floating point types, use
795 the decimal floating point type with the greater precision. */
797 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
799 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
801 return dfloat128_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
804 return dfloat64_type_node;
805 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
806 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
807 return dfloat32_type_node;
810 /* Deal with fixed-point types. */
811 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
813 unsigned int unsignedp = 0, satp = 0;
814 scalar_mode m1, m2;
815 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
817 m1 = SCALAR_TYPE_MODE (t1);
818 m2 = SCALAR_TYPE_MODE (t2);
820 /* If one input type is saturating, the result type is saturating. */
821 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
822 satp = 1;
824 /* If both fixed-point types are unsigned, the result type is unsigned.
825 When mixing fixed-point and integer types, follow the sign of the
826 fixed-point type.
827 Otherwise, the result type is signed. */
828 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
829 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
830 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
831 && TYPE_UNSIGNED (t1))
832 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t2)))
834 unsignedp = 1;
836 /* The result type is signed. */
837 if (unsignedp == 0)
839 /* If the input type is unsigned, we need to convert to the
840 signed type. */
841 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
843 enum mode_class mclass = (enum mode_class) 0;
844 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
845 mclass = MODE_FRACT;
846 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
847 mclass = MODE_ACCUM;
848 else
849 gcc_unreachable ();
850 m1 = as_a <scalar_mode>
851 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
853 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
855 enum mode_class mclass = (enum mode_class) 0;
856 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
857 mclass = MODE_FRACT;
858 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
859 mclass = MODE_ACCUM;
860 else
861 gcc_unreachable ();
862 m2 = as_a <scalar_mode>
863 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
867 if (code1 == FIXED_POINT_TYPE)
869 fbit1 = GET_MODE_FBIT (m1);
870 ibit1 = GET_MODE_IBIT (m1);
872 else
874 fbit1 = 0;
875 /* Signed integers need to subtract one sign bit. */
876 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
879 if (code2 == FIXED_POINT_TYPE)
881 fbit2 = GET_MODE_FBIT (m2);
882 ibit2 = GET_MODE_IBIT (m2);
884 else
886 fbit2 = 0;
887 /* Signed integers need to subtract one sign bit. */
888 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
891 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
892 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
893 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
894 satp);
897 /* Both real or both integers; use the one with greater precision. */
899 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
900 return t1;
901 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
902 return t2;
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
908 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
910 return long_long_unsigned_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
915 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
916 return long_long_unsigned_type_node;
917 else
918 return long_long_integer_type_node;
921 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
923 return long_unsigned_type_node;
925 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
931 return long_unsigned_type_node;
932 else
933 return long_integer_type_node;
936 /* For floating types of the same TYPE_PRECISION (which we here
937 assume means either the same set of values, or sets of values
938 neither a subset of the other, with behavior being undefined in
939 the latter case), follow the rules from TS 18661-3: prefer
940 interchange types _FloatN, then standard types long double,
941 double, float, then extended types _FloatNx. For extended types,
942 check them starting with _Float128x as that seems most consistent
943 in spirit with preferring long double to double; for interchange
944 types, also check in that order for consistency although it's not
945 possible for more than one of them to have the same
946 precision. */
947 tree mv1 = TYPE_MAIN_VARIANT (t1);
948 tree mv2 = TYPE_MAIN_VARIANT (t2);
950 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
951 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
952 return FLOATN_TYPE_NODE (i);
954 /* Likewise, prefer long double to double even if same size. */
955 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
956 return long_double_type_node;
958 /* Likewise, prefer double to float even if same size.
959 We got a couple of embedded targets with 32 bit doubles, and the
960 pdp11 might have 64 bit floats. */
961 if (mv1 == double_type_node || mv2 == double_type_node)
962 return double_type_node;
964 if (mv1 == float_type_node || mv2 == float_type_node)
965 return float_type_node;
967 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
968 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
969 return FLOATNX_TYPE_NODE (i);
971 /* Otherwise prefer the unsigned one. */
973 if (TYPE_UNSIGNED (t1))
974 return t1;
975 else
976 return t2;
979 /* Wrapper around c_common_type that is used by c-common.c and other
980 front end optimizations that remove promotions. ENUMERAL_TYPEs
981 are allowed here and are converted to their compatible integer types.
982 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
983 preferably a non-Boolean type as the common type. */
984 tree
985 common_type (tree t1, tree t2)
987 if (TREE_CODE (t1) == ENUMERAL_TYPE)
988 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
989 if (TREE_CODE (t2) == ENUMERAL_TYPE)
990 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
992 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
993 if (TREE_CODE (t1) == BOOLEAN_TYPE
994 && TREE_CODE (t2) == BOOLEAN_TYPE)
995 return boolean_type_node;
997 /* If either type is BOOLEAN_TYPE, then return the other. */
998 if (TREE_CODE (t1) == BOOLEAN_TYPE)
999 return t2;
1000 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1001 return t1;
1003 return c_common_type (t1, t2);
1006 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1007 or various other operations. Return 2 if they are compatible
1008 but a warning may be needed if you use them together. */
1011 comptypes (tree type1, tree type2)
1013 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1014 int val;
1016 val = comptypes_internal (type1, type2, NULL, NULL);
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1019 return val;
1022 /* Like comptypes, but if it returns non-zero because enum and int are
1023 compatible, it sets *ENUM_AND_INT_P to true. */
1025 static int
1026 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1028 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1029 int val;
1031 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1032 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1034 return val;
1037 /* Like comptypes, but if it returns nonzero for different types, it
1038 sets *DIFFERENT_TYPES_P to true. */
1041 comptypes_check_different_types (tree type1, tree type2,
1042 bool *different_types_p)
1044 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1045 int val;
1047 val = comptypes_internal (type1, type2, NULL, different_types_p);
1048 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1050 return val;
1053 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1054 or various other operations. Return 2 if they are compatible
1055 but a warning may be needed if you use them together. If
1056 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1057 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1058 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1059 NULL, and the types are compatible but different enough not to be
1060 permitted in C11 typedef redeclarations, then this sets
1061 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1062 false, but may or may not be set if the types are incompatible.
1063 This differs from comptypes, in that we don't free the seen
1064 types. */
1066 static int
1067 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1068 bool *different_types_p)
1070 const_tree t1 = type1;
1071 const_tree t2 = type2;
1072 int attrval, val;
1074 /* Suppress errors caused by previously reported errors. */
1076 if (t1 == t2 || !t1 || !t2
1077 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1078 return 1;
1080 /* Enumerated types are compatible with integer types, but this is
1081 not transitive: two enumerated types in the same translation unit
1082 are compatible with each other only if they are the same type. */
1084 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1086 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1087 if (TREE_CODE (t2) != VOID_TYPE)
1089 if (enum_and_int_p != NULL)
1090 *enum_and_int_p = true;
1091 if (different_types_p != NULL)
1092 *different_types_p = true;
1095 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1097 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1098 if (TREE_CODE (t1) != VOID_TYPE)
1100 if (enum_and_int_p != NULL)
1101 *enum_and_int_p = true;
1102 if (different_types_p != NULL)
1103 *different_types_p = true;
1107 if (t1 == t2)
1108 return 1;
1110 /* Different classes of types can't be compatible. */
1112 if (TREE_CODE (t1) != TREE_CODE (t2))
1113 return 0;
1115 /* Qualifiers must match. C99 6.7.3p9 */
1117 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1118 return 0;
1120 /* Allow for two different type nodes which have essentially the same
1121 definition. Note that we already checked for equality of the type
1122 qualifiers (just above). */
1124 if (TREE_CODE (t1) != ARRAY_TYPE
1125 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1126 return 1;
1128 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1129 if (!(attrval = comp_type_attributes (t1, t2)))
1130 return 0;
1132 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1133 val = 0;
1135 switch (TREE_CODE (t1))
1137 case INTEGER_TYPE:
1138 case FIXED_POINT_TYPE:
1139 case REAL_TYPE:
1140 /* With these nodes, we can't determine type equivalence by
1141 looking at what is stored in the nodes themselves, because
1142 two nodes might have different TYPE_MAIN_VARIANTs but still
1143 represent the same type. For example, wchar_t and int could
1144 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1145 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1146 and are distinct types. On the other hand, int and the
1147 following typedef
1149 typedef int INT __attribute((may_alias));
1151 have identical properties, different TYPE_MAIN_VARIANTs, but
1152 represent the same type. The canonical type system keeps
1153 track of equivalence in this case, so we fall back on it. */
1154 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1156 case POINTER_TYPE:
1157 /* Do not remove mode information. */
1158 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1159 break;
1160 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1161 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1162 enum_and_int_p, different_types_p));
1163 break;
1165 case FUNCTION_TYPE:
1166 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1167 different_types_p);
1168 break;
1170 case ARRAY_TYPE:
1172 tree d1 = TYPE_DOMAIN (t1);
1173 tree d2 = TYPE_DOMAIN (t2);
1174 bool d1_variable, d2_variable;
1175 bool d1_zero, d2_zero;
1176 val = 1;
1178 /* Target types must match incl. qualifiers. */
1179 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1180 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p,
1182 different_types_p)) == 0)
1183 return 0;
1185 if (different_types_p != NULL
1186 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1187 *different_types_p = true;
1188 /* Sizes must match unless one is missing or variable. */
1189 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1190 break;
1192 d1_zero = !TYPE_MAX_VALUE (d1);
1193 d2_zero = !TYPE_MAX_VALUE (d2);
1195 d1_variable = (!d1_zero
1196 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1197 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1198 d2_variable = (!d2_zero
1199 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1200 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1201 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1202 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1204 if (different_types_p != NULL
1205 && d1_variable != d2_variable)
1206 *different_types_p = true;
1207 if (d1_variable || d2_variable)
1208 break;
1209 if (d1_zero && d2_zero)
1210 break;
1211 if (d1_zero || d2_zero
1212 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1213 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1214 val = 0;
1216 break;
1219 case ENUMERAL_TYPE:
1220 case RECORD_TYPE:
1221 case UNION_TYPE:
1222 if (val != 1 && !same_translation_unit_p (t1, t2))
1224 tree a1 = TYPE_ATTRIBUTES (t1);
1225 tree a2 = TYPE_ATTRIBUTES (t2);
1227 if (! attribute_list_contained (a1, a2)
1228 && ! attribute_list_contained (a2, a1))
1229 break;
1231 if (attrval != 2)
1232 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1233 different_types_p);
1234 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1235 different_types_p);
1237 break;
1239 case VECTOR_TYPE:
1240 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1241 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1242 enum_and_int_p, different_types_p));
1243 break;
1245 default:
1246 break;
1248 return attrval == 2 && val == 1 ? 2 : val;
1251 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1252 their qualifiers, except for named address spaces. If the pointers point to
1253 different named addresses, then we must determine if one address space is a
1254 subset of the other. */
1256 static int
1257 comp_target_types (location_t location, tree ttl, tree ttr)
1259 int val;
1260 int val_ped;
1261 tree mvl = TREE_TYPE (ttl);
1262 tree mvr = TREE_TYPE (ttr);
1263 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1264 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1265 addr_space_t as_common;
1266 bool enum_and_int_p;
1268 /* Fail if pointers point to incompatible address spaces. */
1269 if (!addr_space_superset (asl, asr, &as_common))
1270 return 0;
1272 /* For pedantic record result of comptypes on arrays before losing
1273 qualifiers on the element type below. */
1274 val_ped = 1;
1276 if (TREE_CODE (mvl) == ARRAY_TYPE
1277 && TREE_CODE (mvr) == ARRAY_TYPE)
1278 val_ped = comptypes (mvl, mvr);
1280 /* Qualifiers on element types of array types that are
1281 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1283 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1284 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1285 : TYPE_MAIN_VARIANT (mvl));
1287 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1288 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1289 : TYPE_MAIN_VARIANT (mvr));
1291 enum_and_int_p = false;
1292 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1294 if (val == 1 && val_ped != 1)
1295 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1296 "are incompatible in ISO C");
1298 if (val == 2)
1299 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1301 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1302 warning_at (location, OPT_Wc___compat,
1303 "pointer target types incompatible in C++");
1305 return val;
1308 /* Subroutines of `comptypes'. */
1310 /* Determine whether two trees derive from the same translation unit.
1311 If the CONTEXT chain ends in a null, that tree's context is still
1312 being parsed, so if two trees have context chains ending in null,
1313 they're in the same translation unit. */
1315 bool
1316 same_translation_unit_p (const_tree t1, const_tree t2)
1318 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1319 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1321 case tcc_declaration:
1322 t1 = DECL_CONTEXT (t1); break;
1323 case tcc_type:
1324 t1 = TYPE_CONTEXT (t1); break;
1325 case tcc_exceptional:
1326 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1327 default: gcc_unreachable ();
1330 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1331 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1333 case tcc_declaration:
1334 t2 = DECL_CONTEXT (t2); break;
1335 case tcc_type:
1336 t2 = TYPE_CONTEXT (t2); break;
1337 case tcc_exceptional:
1338 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1339 default: gcc_unreachable ();
1342 return t1 == t2;
1345 /* Allocate the seen two types, assuming that they are compatible. */
1347 static struct tagged_tu_seen_cache *
1348 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1350 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1351 tu->next = tagged_tu_seen_base;
1352 tu->t1 = t1;
1353 tu->t2 = t2;
1355 tagged_tu_seen_base = tu;
1357 /* The C standard says that two structures in different translation
1358 units are compatible with each other only if the types of their
1359 fields are compatible (among other things). We assume that they
1360 are compatible until proven otherwise when building the cache.
1361 An example where this can occur is:
1362 struct a
1364 struct a *next;
1366 If we are comparing this against a similar struct in another TU,
1367 and did not assume they were compatible, we end up with an infinite
1368 loop. */
1369 tu->val = 1;
1370 return tu;
1373 /* Free the seen types until we get to TU_TIL. */
1375 static void
1376 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1378 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1379 while (tu != tu_til)
1381 const struct tagged_tu_seen_cache *const tu1
1382 = (const struct tagged_tu_seen_cache *) tu;
1383 tu = tu1->next;
1384 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1386 tagged_tu_seen_base = tu_til;
1389 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1390 compatible. If the two types are not the same (which has been
1391 checked earlier), this can only happen when multiple translation
1392 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1393 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1394 comptypes_internal. */
1396 static int
1397 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1398 bool *enum_and_int_p, bool *different_types_p)
1400 tree s1, s2;
1401 bool needs_warning = false;
1403 /* We have to verify that the tags of the types are the same. This
1404 is harder than it looks because this may be a typedef, so we have
1405 to go look at the original type. It may even be a typedef of a
1406 typedef...
1407 In the case of compiler-created builtin structs the TYPE_DECL
1408 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1409 while (TYPE_NAME (t1)
1410 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1411 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1412 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1414 while (TYPE_NAME (t2)
1415 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1416 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1417 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1419 /* C90 didn't have the requirement that the two tags be the same. */
1420 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1421 return 0;
1423 /* C90 didn't say what happened if one or both of the types were
1424 incomplete; we choose to follow C99 rules here, which is that they
1425 are compatible. */
1426 if (TYPE_SIZE (t1) == NULL
1427 || TYPE_SIZE (t2) == NULL)
1428 return 1;
1431 const struct tagged_tu_seen_cache * tts_i;
1432 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1433 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1434 return tts_i->val;
1437 switch (TREE_CODE (t1))
1439 case ENUMERAL_TYPE:
1441 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1442 /* Speed up the case where the type values are in the same order. */
1443 tree tv1 = TYPE_VALUES (t1);
1444 tree tv2 = TYPE_VALUES (t2);
1446 if (tv1 == tv2)
1448 return 1;
1451 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1453 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1454 break;
1455 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1457 tu->val = 0;
1458 return 0;
1462 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1464 return 1;
1466 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1468 tu->val = 0;
1469 return 0;
1472 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1474 tu->val = 0;
1475 return 0;
1478 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1480 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1481 if (s2 == NULL
1482 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1484 tu->val = 0;
1485 return 0;
1488 return 1;
1491 case UNION_TYPE:
1493 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1494 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1496 tu->val = 0;
1497 return 0;
1500 /* Speed up the common case where the fields are in the same order. */
1501 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1502 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1504 int result;
1506 if (DECL_NAME (s1) != DECL_NAME (s2))
1507 break;
1508 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1509 enum_and_int_p, different_types_p);
1511 if (result != 1 && !DECL_NAME (s1))
1512 break;
1513 if (result == 0)
1515 tu->val = 0;
1516 return 0;
1518 if (result == 2)
1519 needs_warning = true;
1521 if (TREE_CODE (s1) == FIELD_DECL
1522 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1523 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1525 tu->val = 0;
1526 return 0;
1529 if (!s1 && !s2)
1531 tu->val = needs_warning ? 2 : 1;
1532 return tu->val;
1535 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1537 bool ok = false;
1539 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1540 if (DECL_NAME (s1) == DECL_NAME (s2))
1542 int result;
1544 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1545 enum_and_int_p,
1546 different_types_p);
1548 if (result != 1 && !DECL_NAME (s1))
1549 continue;
1550 if (result == 0)
1552 tu->val = 0;
1553 return 0;
1555 if (result == 2)
1556 needs_warning = true;
1558 if (TREE_CODE (s1) == FIELD_DECL
1559 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1560 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1561 break;
1563 ok = true;
1564 break;
1566 if (!ok)
1568 tu->val = 0;
1569 return 0;
1572 tu->val = needs_warning ? 2 : 10;
1573 return tu->val;
1576 case RECORD_TYPE:
1578 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1580 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1581 s1 && s2;
1582 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1584 int result;
1585 if (TREE_CODE (s1) != TREE_CODE (s2)
1586 || DECL_NAME (s1) != DECL_NAME (s2))
1587 break;
1588 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1589 enum_and_int_p, different_types_p);
1590 if (result == 0)
1591 break;
1592 if (result == 2)
1593 needs_warning = true;
1595 if (TREE_CODE (s1) == FIELD_DECL
1596 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1597 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1598 break;
1600 if (s1 && s2)
1601 tu->val = 0;
1602 else
1603 tu->val = needs_warning ? 2 : 1;
1604 return tu->val;
1607 default:
1608 gcc_unreachable ();
1612 /* Return 1 if two function types F1 and F2 are compatible.
1613 If either type specifies no argument types,
1614 the other must specify a fixed number of self-promoting arg types.
1615 Otherwise, if one type specifies only the number of arguments,
1616 the other must specify that number of self-promoting arg types.
1617 Otherwise, the argument types must match.
1618 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1620 static int
1621 function_types_compatible_p (const_tree f1, const_tree f2,
1622 bool *enum_and_int_p, bool *different_types_p)
1624 tree args1, args2;
1625 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1626 int val = 1;
1627 int val1;
1628 tree ret1, ret2;
1630 ret1 = TREE_TYPE (f1);
1631 ret2 = TREE_TYPE (f2);
1633 /* 'volatile' qualifiers on a function's return type used to mean
1634 the function is noreturn. */
1635 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1636 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1637 if (TYPE_VOLATILE (ret1))
1638 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1639 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1640 if (TYPE_VOLATILE (ret2))
1641 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1642 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1643 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1644 if (val == 0)
1645 return 0;
1647 args1 = TYPE_ARG_TYPES (f1);
1648 args2 = TYPE_ARG_TYPES (f2);
1650 if (different_types_p != NULL
1651 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1652 *different_types_p = true;
1654 /* An unspecified parmlist matches any specified parmlist
1655 whose argument types don't need default promotions. */
1657 if (args1 == NULL_TREE)
1659 if (!self_promoting_args_p (args2))
1660 return 0;
1661 /* If one of these types comes from a non-prototype fn definition,
1662 compare that with the other type's arglist.
1663 If they don't match, ask for a warning (but no error). */
1664 if (TYPE_ACTUAL_ARG_TYPES (f1)
1665 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1666 enum_and_int_p, different_types_p) != 1)
1667 val = 2;
1668 return val;
1670 if (args2 == NULL_TREE)
1672 if (!self_promoting_args_p (args1))
1673 return 0;
1674 if (TYPE_ACTUAL_ARG_TYPES (f2)
1675 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1676 enum_and_int_p, different_types_p) != 1)
1677 val = 2;
1678 return val;
1681 /* Both types have argument lists: compare them and propagate results. */
1682 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1683 different_types_p);
1684 return val1 != 1 ? val1 : val;
1687 /* Check two lists of types for compatibility, returning 0 for
1688 incompatible, 1 for compatible, or 2 for compatible with
1689 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1690 comptypes_internal. */
1692 static int
1693 type_lists_compatible_p (const_tree args1, const_tree args2,
1694 bool *enum_and_int_p, bool *different_types_p)
1696 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1697 int val = 1;
1698 int newval = 0;
1700 while (1)
1702 tree a1, mv1, a2, mv2;
1703 if (args1 == NULL_TREE && args2 == NULL_TREE)
1704 return val;
1705 /* If one list is shorter than the other,
1706 they fail to match. */
1707 if (args1 == NULL_TREE || args2 == NULL_TREE)
1708 return 0;
1709 mv1 = a1 = TREE_VALUE (args1);
1710 mv2 = a2 = TREE_VALUE (args2);
1711 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1712 mv1 = (TYPE_ATOMIC (mv1)
1713 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1714 TYPE_QUAL_ATOMIC)
1715 : TYPE_MAIN_VARIANT (mv1));
1716 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1717 mv2 = (TYPE_ATOMIC (mv2)
1718 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1719 TYPE_QUAL_ATOMIC)
1720 : TYPE_MAIN_VARIANT (mv2));
1721 /* A null pointer instead of a type
1722 means there is supposed to be an argument
1723 but nothing is specified about what type it has.
1724 So match anything that self-promotes. */
1725 if (different_types_p != NULL
1726 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1727 *different_types_p = true;
1728 if (a1 == NULL_TREE)
1730 if (c_type_promotes_to (a2) != a2)
1731 return 0;
1733 else if (a2 == NULL_TREE)
1735 if (c_type_promotes_to (a1) != a1)
1736 return 0;
1738 /* If one of the lists has an error marker, ignore this arg. */
1739 else if (TREE_CODE (a1) == ERROR_MARK
1740 || TREE_CODE (a2) == ERROR_MARK)
1742 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1743 different_types_p)))
1745 if (different_types_p != NULL)
1746 *different_types_p = true;
1747 /* Allow wait (union {union wait *u; int *i} *)
1748 and wait (union wait *) to be compatible. */
1749 if (TREE_CODE (a1) == UNION_TYPE
1750 && (TYPE_NAME (a1) == NULL_TREE
1751 || TYPE_TRANSPARENT_AGGR (a1))
1752 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1753 && tree_int_cst_equal (TYPE_SIZE (a1),
1754 TYPE_SIZE (a2)))
1756 tree memb;
1757 for (memb = TYPE_FIELDS (a1);
1758 memb; memb = DECL_CHAIN (memb))
1760 tree mv3 = TREE_TYPE (memb);
1761 if (mv3 && mv3 != error_mark_node
1762 && TREE_CODE (mv3) != ARRAY_TYPE)
1763 mv3 = (TYPE_ATOMIC (mv3)
1764 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1765 TYPE_QUAL_ATOMIC)
1766 : TYPE_MAIN_VARIANT (mv3));
1767 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1768 different_types_p))
1769 break;
1771 if (memb == NULL_TREE)
1772 return 0;
1774 else if (TREE_CODE (a2) == UNION_TYPE
1775 && (TYPE_NAME (a2) == NULL_TREE
1776 || TYPE_TRANSPARENT_AGGR (a2))
1777 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1778 && tree_int_cst_equal (TYPE_SIZE (a2),
1779 TYPE_SIZE (a1)))
1781 tree memb;
1782 for (memb = TYPE_FIELDS (a2);
1783 memb; memb = DECL_CHAIN (memb))
1785 tree mv3 = TREE_TYPE (memb);
1786 if (mv3 && mv3 != error_mark_node
1787 && TREE_CODE (mv3) != ARRAY_TYPE)
1788 mv3 = (TYPE_ATOMIC (mv3)
1789 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1790 TYPE_QUAL_ATOMIC)
1791 : TYPE_MAIN_VARIANT (mv3));
1792 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1793 different_types_p))
1794 break;
1796 if (memb == NULL_TREE)
1797 return 0;
1799 else
1800 return 0;
1803 /* comptypes said ok, but record if it said to warn. */
1804 if (newval > val)
1805 val = newval;
1807 args1 = TREE_CHAIN (args1);
1808 args2 = TREE_CHAIN (args2);
1812 /* Compute the size to increment a pointer by. When a function type or void
1813 type or incomplete type is passed, size_one_node is returned.
1814 This function does not emit any diagnostics; the caller is responsible
1815 for that. */
1817 static tree
1818 c_size_in_bytes (const_tree type)
1820 enum tree_code code = TREE_CODE (type);
1822 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1823 || !COMPLETE_TYPE_P (type))
1824 return size_one_node;
1826 /* Convert in case a char is more than one unit. */
1827 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1828 size_int (TYPE_PRECISION (char_type_node)
1829 / BITS_PER_UNIT));
1832 /* Return either DECL or its known constant value (if it has one). */
1834 tree
1835 decl_constant_value_1 (tree decl, bool in_init)
1837 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1838 TREE_CODE (decl) != PARM_DECL
1839 && !TREE_THIS_VOLATILE (decl)
1840 && TREE_READONLY (decl)
1841 && DECL_INITIAL (decl) != NULL_TREE
1842 && !error_operand_p (DECL_INITIAL (decl))
1843 /* This is invalid if initial value is not constant.
1844 If it has either a function call, a memory reference,
1845 or a variable, then re-evaluating it could give different results. */
1846 && TREE_CONSTANT (DECL_INITIAL (decl))
1847 /* Check for cases where this is sub-optimal, even though valid. */
1848 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1849 return DECL_INITIAL (decl);
1850 return decl;
1853 /* Return either DECL or its known constant value (if it has one).
1854 Like the above, but always return decl outside of functions. */
1856 tree
1857 decl_constant_value (tree decl)
1859 /* Don't change a variable array bound or initial value to a constant
1860 in a place where a variable is invalid. */
1861 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1864 /* Convert the array expression EXP to a pointer. */
1865 static tree
1866 array_to_pointer_conversion (location_t loc, tree exp)
1868 tree orig_exp = exp;
1869 tree type = TREE_TYPE (exp);
1870 tree adr;
1871 tree restype = TREE_TYPE (type);
1872 tree ptrtype;
1874 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1876 STRIP_TYPE_NOPS (exp);
1878 if (TREE_NO_WARNING (orig_exp))
1879 TREE_NO_WARNING (exp) = 1;
1881 ptrtype = build_pointer_type (restype);
1883 if (INDIRECT_REF_P (exp))
1884 return convert (ptrtype, TREE_OPERAND (exp, 0));
1886 /* In C++ array compound literals are temporary objects unless they are
1887 const or appear in namespace scope, so they are destroyed too soon
1888 to use them for much of anything (c++/53220). */
1889 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1891 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1892 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1893 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1894 "converting an array compound literal to a pointer "
1895 "is ill-formed in C++");
1898 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1899 return convert (ptrtype, adr);
1902 /* Convert the function expression EXP to a pointer. */
1903 static tree
1904 function_to_pointer_conversion (location_t loc, tree exp)
1906 tree orig_exp = exp;
1908 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1910 STRIP_TYPE_NOPS (exp);
1912 if (TREE_NO_WARNING (orig_exp))
1913 TREE_NO_WARNING (exp) = 1;
1915 return build_unary_op (loc, ADDR_EXPR, exp, false);
1918 /* Mark EXP as read, not just set, for set but not used -Wunused
1919 warning purposes. */
1921 void
1922 mark_exp_read (tree exp)
1924 switch (TREE_CODE (exp))
1926 case VAR_DECL:
1927 case PARM_DECL:
1928 DECL_READ_P (exp) = 1;
1929 break;
1930 case ARRAY_REF:
1931 case COMPONENT_REF:
1932 case MODIFY_EXPR:
1933 case REALPART_EXPR:
1934 case IMAGPART_EXPR:
1935 CASE_CONVERT:
1936 case ADDR_EXPR:
1937 case VIEW_CONVERT_EXPR:
1938 mark_exp_read (TREE_OPERAND (exp, 0));
1939 break;
1940 case COMPOUND_EXPR:
1941 case C_MAYBE_CONST_EXPR:
1942 mark_exp_read (TREE_OPERAND (exp, 1));
1943 break;
1944 default:
1945 break;
1949 /* Perform the default conversion of arrays and functions to pointers.
1950 Return the result of converting EXP. For any other expression, just
1951 return EXP.
1953 LOC is the location of the expression. */
1955 struct c_expr
1956 default_function_array_conversion (location_t loc, struct c_expr exp)
1958 tree orig_exp = exp.value;
1959 tree type = TREE_TYPE (exp.value);
1960 enum tree_code code = TREE_CODE (type);
1962 switch (code)
1964 case ARRAY_TYPE:
1966 bool not_lvalue = false;
1967 bool lvalue_array_p;
1969 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1970 || CONVERT_EXPR_P (exp.value))
1971 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1973 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1974 not_lvalue = true;
1975 exp.value = TREE_OPERAND (exp.value, 0);
1978 if (TREE_NO_WARNING (orig_exp))
1979 TREE_NO_WARNING (exp.value) = 1;
1981 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1982 if (!flag_isoc99 && !lvalue_array_p)
1984 /* Before C99, non-lvalue arrays do not decay to pointers.
1985 Normally, using such an array would be invalid; but it can
1986 be used correctly inside sizeof or as a statement expression.
1987 Thus, do not give an error here; an error will result later. */
1988 return exp;
1991 exp.value = array_to_pointer_conversion (loc, exp.value);
1993 break;
1994 case FUNCTION_TYPE:
1995 exp.value = function_to_pointer_conversion (loc, exp.value);
1996 break;
1997 default:
1998 break;
2001 return exp;
2004 struct c_expr
2005 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2007 mark_exp_read (exp.value);
2008 return default_function_array_conversion (loc, exp);
2011 /* Return whether EXPR should be treated as an atomic lvalue for the
2012 purposes of load and store handling. */
2014 static bool
2015 really_atomic_lvalue (tree expr)
2017 if (error_operand_p (expr))
2018 return false;
2019 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2020 return false;
2021 if (!lvalue_p (expr))
2022 return false;
2024 /* Ignore _Atomic on register variables, since their addresses can't
2025 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2026 sequences wouldn't work. Ignore _Atomic on structures containing
2027 bit-fields, since accessing elements of atomic structures or
2028 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2029 it's undefined at translation time or execution time, and the
2030 normal atomic sequences again wouldn't work. */
2031 while (handled_component_p (expr))
2033 if (TREE_CODE (expr) == COMPONENT_REF
2034 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2035 return false;
2036 expr = TREE_OPERAND (expr, 0);
2038 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2039 return false;
2040 return true;
2043 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2044 including converting functions and arrays to pointers if CONVERT_P.
2045 If READ_P, also mark the expression as having been read. */
2047 struct c_expr
2048 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2049 bool convert_p, bool read_p)
2051 if (read_p)
2052 mark_exp_read (exp.value);
2053 if (convert_p)
2054 exp = default_function_array_conversion (loc, exp);
2055 if (really_atomic_lvalue (exp.value))
2057 vec<tree, va_gc> *params;
2058 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2059 tree expr_type = TREE_TYPE (exp.value);
2060 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2061 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2063 gcc_assert (TYPE_ATOMIC (expr_type));
2065 /* Expansion of a generic atomic load may require an addition
2066 element, so allocate enough to prevent a resize. */
2067 vec_alloc (params, 4);
2069 /* Remove the qualifiers for the rest of the expressions and
2070 create the VAL temp variable to hold the RHS. */
2071 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2072 tmp = create_tmp_var_raw (nonatomic_type);
2073 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2074 TREE_ADDRESSABLE (tmp) = 1;
2075 TREE_NO_WARNING (tmp) = 1;
2077 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2078 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2079 params->quick_push (expr_addr);
2080 params->quick_push (tmp_addr);
2081 params->quick_push (seq_cst);
2082 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2084 /* EXPR is always read. */
2085 mark_exp_read (exp.value);
2087 /* Return tmp which contains the value loaded. */
2088 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2089 NULL_TREE, NULL_TREE);
2091 return exp;
2094 /* EXP is an expression of integer type. Apply the integer promotions
2095 to it and return the promoted value. */
2097 tree
2098 perform_integral_promotions (tree exp)
2100 tree type = TREE_TYPE (exp);
2101 enum tree_code code = TREE_CODE (type);
2103 gcc_assert (INTEGRAL_TYPE_P (type));
2105 /* Normally convert enums to int,
2106 but convert wide enums to something wider. */
2107 if (code == ENUMERAL_TYPE)
2109 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2110 TYPE_PRECISION (integer_type_node)),
2111 ((TYPE_PRECISION (type)
2112 >= TYPE_PRECISION (integer_type_node))
2113 && TYPE_UNSIGNED (type)));
2115 return convert (type, exp);
2118 /* ??? This should no longer be needed now bit-fields have their
2119 proper types. */
2120 if (TREE_CODE (exp) == COMPONENT_REF
2121 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2122 /* If it's thinner than an int, promote it like a
2123 c_promoting_integer_type_p, otherwise leave it alone. */
2124 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2125 TYPE_PRECISION (integer_type_node)) < 0)
2126 return convert (integer_type_node, exp);
2128 if (c_promoting_integer_type_p (type))
2130 /* Preserve unsignedness if not really getting any wider. */
2131 if (TYPE_UNSIGNED (type)
2132 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2133 return convert (unsigned_type_node, exp);
2135 return convert (integer_type_node, exp);
2138 return exp;
2142 /* Perform default promotions for C data used in expressions.
2143 Enumeral types or short or char are converted to int.
2144 In addition, manifest constants symbols are replaced by their values. */
2146 tree
2147 default_conversion (tree exp)
2149 tree orig_exp;
2150 tree type = TREE_TYPE (exp);
2151 enum tree_code code = TREE_CODE (type);
2152 tree promoted_type;
2154 mark_exp_read (exp);
2156 /* Functions and arrays have been converted during parsing. */
2157 gcc_assert (code != FUNCTION_TYPE);
2158 if (code == ARRAY_TYPE)
2159 return exp;
2161 /* Constants can be used directly unless they're not loadable. */
2162 if (TREE_CODE (exp) == CONST_DECL)
2163 exp = DECL_INITIAL (exp);
2165 /* Strip no-op conversions. */
2166 orig_exp = exp;
2167 STRIP_TYPE_NOPS (exp);
2169 if (TREE_NO_WARNING (orig_exp))
2170 TREE_NO_WARNING (exp) = 1;
2172 if (code == VOID_TYPE)
2174 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2175 "void value not ignored as it ought to be");
2176 return error_mark_node;
2179 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2180 if (exp == error_mark_node)
2181 return error_mark_node;
2183 promoted_type = targetm.promoted_type (type);
2184 if (promoted_type)
2185 return convert (promoted_type, exp);
2187 if (INTEGRAL_TYPE_P (type))
2188 return perform_integral_promotions (exp);
2190 return exp;
2193 /* Look up COMPONENT in a structure or union TYPE.
2195 If the component name is not found, returns NULL_TREE. Otherwise,
2196 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2197 stepping down the chain to the component, which is in the last
2198 TREE_VALUE of the list. Normally the list is of length one, but if
2199 the component is embedded within (nested) anonymous structures or
2200 unions, the list steps down the chain to the component. */
2202 static tree
2203 lookup_field (tree type, tree component)
2205 tree field;
2207 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2208 to the field elements. Use a binary search on this array to quickly
2209 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2210 will always be set for structures which have many elements.
2212 Duplicate field checking replaces duplicates with NULL_TREE so
2213 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2214 case just iterate using DECL_CHAIN. */
2216 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2217 && !seen_error ())
2219 int bot, top, half;
2220 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2222 field = TYPE_FIELDS (type);
2223 bot = 0;
2224 top = TYPE_LANG_SPECIFIC (type)->s->len;
2225 while (top - bot > 1)
2227 half = (top - bot + 1) >> 1;
2228 field = field_array[bot+half];
2230 if (DECL_NAME (field) == NULL_TREE)
2232 /* Step through all anon unions in linear fashion. */
2233 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2235 field = field_array[bot++];
2236 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2238 tree anon = lookup_field (TREE_TYPE (field), component);
2240 if (anon)
2241 return tree_cons (NULL_TREE, field, anon);
2243 /* The Plan 9 compiler permits referring
2244 directly to an anonymous struct/union field
2245 using a typedef name. */
2246 if (flag_plan9_extensions
2247 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2248 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2249 == TYPE_DECL)
2250 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2251 == component))
2252 break;
2256 /* Entire record is only anon unions. */
2257 if (bot > top)
2258 return NULL_TREE;
2260 /* Restart the binary search, with new lower bound. */
2261 continue;
2264 if (DECL_NAME (field) == component)
2265 break;
2266 if (DECL_NAME (field) < component)
2267 bot += half;
2268 else
2269 top = bot + half;
2272 if (DECL_NAME (field_array[bot]) == component)
2273 field = field_array[bot];
2274 else if (DECL_NAME (field) != component)
2275 return NULL_TREE;
2277 else
2279 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2281 if (DECL_NAME (field) == NULL_TREE
2282 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2284 tree anon = lookup_field (TREE_TYPE (field), component);
2286 if (anon)
2287 return tree_cons (NULL_TREE, field, anon);
2289 /* The Plan 9 compiler permits referring directly to an
2290 anonymous struct/union field using a typedef
2291 name. */
2292 if (flag_plan9_extensions
2293 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2294 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2295 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2296 == component))
2297 break;
2300 if (DECL_NAME (field) == component)
2301 break;
2304 if (field == NULL_TREE)
2305 return NULL_TREE;
2308 return tree_cons (NULL_TREE, field, NULL_TREE);
2311 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2313 static void
2314 lookup_field_fuzzy_find_candidates (tree type, tree component,
2315 vec<tree> *candidates)
2317 tree field;
2318 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2320 if (DECL_NAME (field) == NULL_TREE
2321 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2322 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2323 candidates);
2325 if (DECL_NAME (field))
2326 candidates->safe_push (DECL_NAME (field));
2330 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2331 rather than returning a TREE_LIST for an exact match. */
2333 static tree
2334 lookup_field_fuzzy (tree type, tree component)
2336 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2338 /* First, gather a list of candidates. */
2339 auto_vec <tree> candidates;
2341 lookup_field_fuzzy_find_candidates (type, component,
2342 &candidates);
2344 return find_closest_identifier (component, &candidates);
2347 /* Support function for build_component_ref's error-handling.
2349 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2350 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2352 static bool
2353 should_suggest_deref_p (tree datum_type)
2355 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2356 allows "." for ptrs; we could be handling a failed attempt
2357 to access a property. */
2358 if (c_dialect_objc ())
2359 return false;
2361 /* Only suggest it for pointers... */
2362 if (TREE_CODE (datum_type) != POINTER_TYPE)
2363 return false;
2365 /* ...to structs/unions. */
2366 tree underlying_type = TREE_TYPE (datum_type);
2367 enum tree_code code = TREE_CODE (underlying_type);
2368 if (code == RECORD_TYPE || code == UNION_TYPE)
2369 return true;
2370 else
2371 return false;
2374 /* Make an expression to refer to the COMPONENT field of structure or
2375 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2376 location of the COMPONENT_REF. COMPONENT_LOC is the location
2377 of COMPONENT. */
2379 tree
2380 build_component_ref (location_t loc, tree datum, tree component,
2381 location_t component_loc)
2383 tree type = TREE_TYPE (datum);
2384 enum tree_code code = TREE_CODE (type);
2385 tree field = NULL;
2386 tree ref;
2387 bool datum_lvalue = lvalue_p (datum);
2389 if (!objc_is_public (datum, component))
2390 return error_mark_node;
2392 /* Detect Objective-C property syntax object.property. */
2393 if (c_dialect_objc ()
2394 && (ref = objc_maybe_build_component_ref (datum, component)))
2395 return ref;
2397 /* See if there is a field or component with name COMPONENT. */
2399 if (code == RECORD_TYPE || code == UNION_TYPE)
2401 if (!COMPLETE_TYPE_P (type))
2403 c_incomplete_type_error (loc, NULL_TREE, type);
2404 return error_mark_node;
2407 field = lookup_field (type, component);
2409 if (!field)
2411 tree guessed_id = lookup_field_fuzzy (type, component);
2412 if (guessed_id)
2414 /* Attempt to provide a fixit replacement hint, if
2415 we have a valid range for the component. */
2416 location_t reported_loc
2417 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2418 gcc_rich_location rich_loc (reported_loc);
2419 if (component_loc != UNKNOWN_LOCATION)
2420 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2421 error_at (&rich_loc,
2422 "%qT has no member named %qE; did you mean %qE?",
2423 type, component, guessed_id);
2425 else
2426 error_at (loc, "%qT has no member named %qE", type, component);
2427 return error_mark_node;
2430 /* Accessing elements of atomic structures or unions is undefined
2431 behavior (C11 6.5.2.3#5). */
2432 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2434 if (code == RECORD_TYPE)
2435 warning_at (loc, 0, "accessing a member %qE of an atomic "
2436 "structure %qE", component, datum);
2437 else
2438 warning_at (loc, 0, "accessing a member %qE of an atomic "
2439 "union %qE", component, datum);
2442 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2443 This might be better solved in future the way the C++ front
2444 end does it - by giving the anonymous entities each a
2445 separate name and type, and then have build_component_ref
2446 recursively call itself. We can't do that here. */
2449 tree subdatum = TREE_VALUE (field);
2450 int quals;
2451 tree subtype;
2452 bool use_datum_quals;
2454 if (TREE_TYPE (subdatum) == error_mark_node)
2455 return error_mark_node;
2457 /* If this is an rvalue, it does not have qualifiers in C
2458 standard terms and we must avoid propagating such
2459 qualifiers down to a non-lvalue array that is then
2460 converted to a pointer. */
2461 use_datum_quals = (datum_lvalue
2462 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2464 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2465 if (use_datum_quals)
2466 quals |= TYPE_QUALS (TREE_TYPE (datum));
2467 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2469 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2470 NULL_TREE);
2471 SET_EXPR_LOCATION (ref, loc);
2472 if (TREE_READONLY (subdatum)
2473 || (use_datum_quals && TREE_READONLY (datum)))
2474 TREE_READONLY (ref) = 1;
2475 if (TREE_THIS_VOLATILE (subdatum)
2476 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2477 TREE_THIS_VOLATILE (ref) = 1;
2479 if (TREE_DEPRECATED (subdatum))
2480 warn_deprecated_use (subdatum, NULL_TREE);
2482 datum = ref;
2484 field = TREE_CHAIN (field);
2486 while (field);
2488 return ref;
2490 else if (should_suggest_deref_p (type))
2492 /* Special-case the error message for "ptr.field" for the case
2493 where the user has confused "." vs "->". */
2494 rich_location richloc (line_table, loc);
2495 /* "loc" should be the "." token. */
2496 richloc.add_fixit_replace ("->");
2497 error_at (&richloc,
2498 "%qE is a pointer; did you mean to use %<->%>?",
2499 datum);
2500 return error_mark_node;
2502 else if (code != ERROR_MARK)
2503 error_at (loc,
2504 "request for member %qE in something not a structure or union",
2505 component);
2507 return error_mark_node;
2510 /* Given an expression PTR for a pointer, return an expression
2511 for the value pointed to.
2512 ERRORSTRING is the name of the operator to appear in error messages.
2514 LOC is the location to use for the generated tree. */
2516 tree
2517 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2519 tree pointer = default_conversion (ptr);
2520 tree type = TREE_TYPE (pointer);
2521 tree ref;
2523 if (TREE_CODE (type) == POINTER_TYPE)
2525 if (CONVERT_EXPR_P (pointer)
2526 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2528 /* If a warning is issued, mark it to avoid duplicates from
2529 the backend. This only needs to be done at
2530 warn_strict_aliasing > 2. */
2531 if (warn_strict_aliasing > 2)
2532 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2533 type, TREE_OPERAND (pointer, 0)))
2534 TREE_NO_WARNING (pointer) = 1;
2537 if (TREE_CODE (pointer) == ADDR_EXPR
2538 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2539 == TREE_TYPE (type)))
2541 ref = TREE_OPERAND (pointer, 0);
2542 protected_set_expr_location (ref, loc);
2543 return ref;
2545 else
2547 tree t = TREE_TYPE (type);
2549 ref = build1 (INDIRECT_REF, t, pointer);
2551 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2553 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2555 error_at (loc, "dereferencing pointer to incomplete type "
2556 "%qT", t);
2557 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2559 return error_mark_node;
2561 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2562 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2564 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2565 so that we get the proper error message if the result is used
2566 to assign to. Also, &* is supposed to be a no-op.
2567 And ANSI C seems to specify that the type of the result
2568 should be the const type. */
2569 /* A de-reference of a pointer to const is not a const. It is valid
2570 to change it via some other pointer. */
2571 TREE_READONLY (ref) = TYPE_READONLY (t);
2572 TREE_SIDE_EFFECTS (ref)
2573 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2574 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2575 protected_set_expr_location (ref, loc);
2576 return ref;
2579 else if (TREE_CODE (pointer) != ERROR_MARK)
2580 invalid_indirection_error (loc, type, errstring);
2582 return error_mark_node;
2585 /* This handles expressions of the form "a[i]", which denotes
2586 an array reference.
2588 This is logically equivalent in C to *(a+i), but we may do it differently.
2589 If A is a variable or a member, we generate a primitive ARRAY_REF.
2590 This avoids forcing the array out of registers, and can work on
2591 arrays that are not lvalues (for example, members of structures returned
2592 by functions).
2594 For vector types, allow vector[i] but not i[vector], and create
2595 *(((type*)&vectortype) + i) for the expression.
2597 LOC is the location to use for the returned expression. */
2599 tree
2600 build_array_ref (location_t loc, tree array, tree index)
2602 tree ret;
2603 bool swapped = false;
2604 if (TREE_TYPE (array) == error_mark_node
2605 || TREE_TYPE (index) == error_mark_node)
2606 return error_mark_node;
2608 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2609 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2610 /* Allow vector[index] but not index[vector]. */
2611 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2613 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2614 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2616 error_at (loc,
2617 "subscripted value is neither array nor pointer nor vector");
2619 return error_mark_node;
2621 std::swap (array, index);
2622 swapped = true;
2625 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2627 error_at (loc, "array subscript is not an integer");
2628 return error_mark_node;
2631 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2633 error_at (loc, "subscripted value is pointer to function");
2634 return error_mark_node;
2637 /* ??? Existing practice has been to warn only when the char
2638 index is syntactically the index, not for char[array]. */
2639 if (!swapped)
2640 warn_array_subscript_with_type_char (loc, index);
2642 /* Apply default promotions *after* noticing character types. */
2643 index = default_conversion (index);
2644 if (index == error_mark_node)
2645 return error_mark_node;
2647 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2649 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2650 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2652 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2654 tree rval, type;
2656 /* An array that is indexed by a non-constant
2657 cannot be stored in a register; we must be able to do
2658 address arithmetic on its address.
2659 Likewise an array of elements of variable size. */
2660 if (TREE_CODE (index) != INTEGER_CST
2661 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2662 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2664 if (!c_mark_addressable (array, true))
2665 return error_mark_node;
2667 /* An array that is indexed by a constant value which is not within
2668 the array bounds cannot be stored in a register either; because we
2669 would get a crash in store_bit_field/extract_bit_field when trying
2670 to access a non-existent part of the register. */
2671 if (TREE_CODE (index) == INTEGER_CST
2672 && TYPE_DOMAIN (TREE_TYPE (array))
2673 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2675 if (!c_mark_addressable (array))
2676 return error_mark_node;
2679 if ((pedantic || warn_c90_c99_compat)
2680 && ! was_vector)
2682 tree foo = array;
2683 while (TREE_CODE (foo) == COMPONENT_REF)
2684 foo = TREE_OPERAND (foo, 0);
2685 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2686 pedwarn (loc, OPT_Wpedantic,
2687 "ISO C forbids subscripting %<register%> array");
2688 else if (!lvalue_p (foo))
2689 pedwarn_c90 (loc, OPT_Wpedantic,
2690 "ISO C90 forbids subscripting non-lvalue "
2691 "array");
2694 type = TREE_TYPE (TREE_TYPE (array));
2695 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2696 /* Array ref is const/volatile if the array elements are
2697 or if the array is. */
2698 TREE_READONLY (rval)
2699 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2700 | TREE_READONLY (array));
2701 TREE_SIDE_EFFECTS (rval)
2702 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2703 | TREE_SIDE_EFFECTS (array));
2704 TREE_THIS_VOLATILE (rval)
2705 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2706 /* This was added by rms on 16 Nov 91.
2707 It fixes vol struct foo *a; a->elts[1]
2708 in an inline function.
2709 Hope it doesn't break something else. */
2710 | TREE_THIS_VOLATILE (array));
2711 ret = require_complete_type (loc, rval);
2712 protected_set_expr_location (ret, loc);
2713 if (non_lvalue)
2714 ret = non_lvalue_loc (loc, ret);
2715 return ret;
2717 else
2719 tree ar = default_conversion (array);
2721 if (ar == error_mark_node)
2722 return ar;
2724 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2725 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2727 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2728 index, false),
2729 RO_ARRAY_INDEXING);
2730 if (non_lvalue)
2731 ret = non_lvalue_loc (loc, ret);
2732 return ret;
2736 /* Build an external reference to identifier ID. FUN indicates
2737 whether this will be used for a function call. LOC is the source
2738 location of the identifier. This sets *TYPE to the type of the
2739 identifier, which is not the same as the type of the returned value
2740 for CONST_DECLs defined as enum constants. If the type of the
2741 identifier is not available, *TYPE is set to NULL. */
2742 tree
2743 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2745 tree ref;
2746 tree decl = lookup_name (id);
2748 /* In Objective-C, an instance variable (ivar) may be preferred to
2749 whatever lookup_name() found. */
2750 decl = objc_lookup_ivar (decl, id);
2752 *type = NULL;
2753 if (decl && decl != error_mark_node)
2755 ref = decl;
2756 *type = TREE_TYPE (ref);
2758 else if (fun)
2759 /* Implicit function declaration. */
2760 ref = implicitly_declare (loc, id);
2761 else if (decl == error_mark_node)
2762 /* Don't complain about something that's already been
2763 complained about. */
2764 return error_mark_node;
2765 else
2767 undeclared_variable (loc, id);
2768 return error_mark_node;
2771 if (TREE_TYPE (ref) == error_mark_node)
2772 return error_mark_node;
2774 if (TREE_DEPRECATED (ref))
2775 warn_deprecated_use (ref, NULL_TREE);
2777 /* Recursive call does not count as usage. */
2778 if (ref != current_function_decl)
2780 TREE_USED (ref) = 1;
2783 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2785 if (!in_sizeof && !in_typeof)
2786 C_DECL_USED (ref) = 1;
2787 else if (DECL_INITIAL (ref) == NULL_TREE
2788 && DECL_EXTERNAL (ref)
2789 && !TREE_PUBLIC (ref))
2790 record_maybe_used_decl (ref);
2793 if (TREE_CODE (ref) == CONST_DECL)
2795 used_types_insert (TREE_TYPE (ref));
2797 if (warn_cxx_compat
2798 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2799 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2801 warning_at (loc, OPT_Wc___compat,
2802 ("enum constant defined in struct or union "
2803 "is not visible in C++"));
2804 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2807 ref = DECL_INITIAL (ref);
2808 TREE_CONSTANT (ref) = 1;
2810 else if (current_function_decl != NULL_TREE
2811 && !DECL_FILE_SCOPE_P (current_function_decl)
2812 && (VAR_OR_FUNCTION_DECL_P (ref)
2813 || TREE_CODE (ref) == PARM_DECL))
2815 tree context = decl_function_context (ref);
2817 if (context != NULL_TREE && context != current_function_decl)
2818 DECL_NONLOCAL (ref) = 1;
2820 /* C99 6.7.4p3: An inline definition of a function with external
2821 linkage ... shall not contain a reference to an identifier with
2822 internal linkage. */
2823 else if (current_function_decl != NULL_TREE
2824 && DECL_DECLARED_INLINE_P (current_function_decl)
2825 && DECL_EXTERNAL (current_function_decl)
2826 && VAR_OR_FUNCTION_DECL_P (ref)
2827 && (!VAR_P (ref) || TREE_STATIC (ref))
2828 && ! TREE_PUBLIC (ref)
2829 && DECL_CONTEXT (ref) != current_function_decl)
2830 record_inline_static (loc, current_function_decl, ref,
2831 csi_internal);
2833 return ref;
2836 /* Record details of decls possibly used inside sizeof or typeof. */
2837 struct maybe_used_decl
2839 /* The decl. */
2840 tree decl;
2841 /* The level seen at (in_sizeof + in_typeof). */
2842 int level;
2843 /* The next one at this level or above, or NULL. */
2844 struct maybe_used_decl *next;
2847 static struct maybe_used_decl *maybe_used_decls;
2849 /* Record that DECL, an undefined static function reference seen
2850 inside sizeof or typeof, might be used if the operand of sizeof is
2851 a VLA type or the operand of typeof is a variably modified
2852 type. */
2854 static void
2855 record_maybe_used_decl (tree decl)
2857 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2858 t->decl = decl;
2859 t->level = in_sizeof + in_typeof;
2860 t->next = maybe_used_decls;
2861 maybe_used_decls = t;
2864 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2865 USED is false, just discard them. If it is true, mark them used
2866 (if no longer inside sizeof or typeof) or move them to the next
2867 level up (if still inside sizeof or typeof). */
2869 void
2870 pop_maybe_used (bool used)
2872 struct maybe_used_decl *p = maybe_used_decls;
2873 int cur_level = in_sizeof + in_typeof;
2874 while (p && p->level > cur_level)
2876 if (used)
2878 if (cur_level == 0)
2879 C_DECL_USED (p->decl) = 1;
2880 else
2881 p->level = cur_level;
2883 p = p->next;
2885 if (!used || cur_level == 0)
2886 maybe_used_decls = p;
2889 /* Return the result of sizeof applied to EXPR. */
2891 struct c_expr
2892 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2894 struct c_expr ret;
2895 if (expr.value == error_mark_node)
2897 ret.value = error_mark_node;
2898 ret.original_code = ERROR_MARK;
2899 ret.original_type = NULL;
2900 pop_maybe_used (false);
2902 else
2904 bool expr_const_operands = true;
2906 if (TREE_CODE (expr.value) == PARM_DECL
2907 && C_ARRAY_PARAMETER (expr.value))
2909 auto_diagnostic_group d;
2910 if (warning_at (loc, OPT_Wsizeof_array_argument,
2911 "%<sizeof%> on array function parameter %qE will "
2912 "return size of %qT", expr.value,
2913 TREE_TYPE (expr.value)))
2914 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2916 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2917 &expr_const_operands);
2918 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2919 c_last_sizeof_arg = expr.value;
2920 c_last_sizeof_loc = loc;
2921 ret.original_code = SIZEOF_EXPR;
2922 ret.original_type = NULL;
2923 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2925 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2926 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2927 folded_expr, ret.value);
2928 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2929 SET_EXPR_LOCATION (ret.value, loc);
2931 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2933 return ret;
2936 /* Return the result of sizeof applied to T, a structure for the type
2937 name passed to sizeof (rather than the type itself). LOC is the
2938 location of the original expression. */
2940 struct c_expr
2941 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2943 tree type;
2944 struct c_expr ret;
2945 tree type_expr = NULL_TREE;
2946 bool type_expr_const = true;
2947 type = groktypename (t, &type_expr, &type_expr_const);
2948 ret.value = c_sizeof (loc, type);
2949 c_last_sizeof_arg = type;
2950 c_last_sizeof_loc = loc;
2951 ret.original_code = SIZEOF_EXPR;
2952 ret.original_type = NULL;
2953 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2954 && c_vla_type_p (type))
2956 /* If the type is a [*] array, it is a VLA but is represented as
2957 having a size of zero. In such a case we must ensure that
2958 the result of sizeof does not get folded to a constant by
2959 c_fully_fold, because if the size is evaluated the result is
2960 not constant and so constraints on zero or negative size
2961 arrays must not be applied when this sizeof call is inside
2962 another array declarator. */
2963 if (!type_expr)
2964 type_expr = integer_zero_node;
2965 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2966 type_expr, ret.value);
2967 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2969 pop_maybe_used (type != error_mark_node
2970 ? C_TYPE_VARIABLE_SIZE (type) : false);
2971 return ret;
2974 /* Build a function call to function FUNCTION with parameters PARAMS.
2975 The function call is at LOC.
2976 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2977 TREE_VALUE of each node is a parameter-expression.
2978 FUNCTION's data type may be a function type or a pointer-to-function. */
2980 tree
2981 build_function_call (location_t loc, tree function, tree params)
2983 vec<tree, va_gc> *v;
2984 tree ret;
2986 vec_alloc (v, list_length (params));
2987 for (; params; params = TREE_CHAIN (params))
2988 v->quick_push (TREE_VALUE (params));
2989 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2990 vec_free (v);
2991 return ret;
2994 /* Give a note about the location of the declaration of DECL. */
2996 static void
2997 inform_declaration (tree decl)
2999 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
3000 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3003 /* Build a function call to function FUNCTION with parameters PARAMS.
3004 ORIGTYPES, if not NULL, is a vector of types; each element is
3005 either NULL or the original type of the corresponding element in
3006 PARAMS. The original type may differ from TREE_TYPE of the
3007 parameter for enums. FUNCTION's data type may be a function type
3008 or pointer-to-function. This function changes the elements of
3009 PARAMS. */
3011 tree
3012 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3013 tree function, vec<tree, va_gc> *params,
3014 vec<tree, va_gc> *origtypes)
3016 tree fntype, fundecl = NULL_TREE;
3017 tree name = NULL_TREE, result;
3018 tree tem;
3019 int nargs;
3020 tree *argarray;
3023 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3024 STRIP_TYPE_NOPS (function);
3026 /* Convert anything with function type to a pointer-to-function. */
3027 if (TREE_CODE (function) == FUNCTION_DECL)
3029 name = DECL_NAME (function);
3031 if (flag_tm)
3032 tm_malloc_replacement (function);
3033 fundecl = function;
3034 /* Atomic functions have type checking/casting already done. They are
3035 often rewritten and don't match the original parameter list. */
3036 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3037 origtypes = NULL;
3039 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3040 function = function_to_pointer_conversion (loc, function);
3042 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3043 expressions, like those used for ObjC messenger dispatches. */
3044 if (params && !params->is_empty ())
3045 function = objc_rewrite_function_call (function, (*params)[0]);
3047 function = c_fully_fold (function, false, NULL);
3049 fntype = TREE_TYPE (function);
3051 if (TREE_CODE (fntype) == ERROR_MARK)
3052 return error_mark_node;
3054 if (!(TREE_CODE (fntype) == POINTER_TYPE
3055 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3057 if (!flag_diagnostics_show_caret)
3058 error_at (loc,
3059 "called object %qE is not a function or function pointer",
3060 function);
3061 else if (DECL_P (function))
3063 error_at (loc,
3064 "called object %qD is not a function or function pointer",
3065 function);
3066 inform_declaration (function);
3068 else
3069 error_at (loc,
3070 "called object is not a function or function pointer");
3071 return error_mark_node;
3074 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3075 current_function_returns_abnormally = 1;
3077 /* fntype now gets the type of function pointed to. */
3078 fntype = TREE_TYPE (fntype);
3080 /* Convert the parameters to the types declared in the
3081 function prototype, or apply default promotions. */
3083 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3084 origtypes, function, fundecl);
3085 if (nargs < 0)
3086 return error_mark_node;
3088 /* Check that the function is called through a compatible prototype.
3089 If it is not, warn. */
3090 if (CONVERT_EXPR_P (function)
3091 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3092 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3093 && !comptypes (fntype, TREE_TYPE (tem)))
3095 tree return_type = TREE_TYPE (fntype);
3097 /* This situation leads to run-time undefined behavior. We can't,
3098 therefore, simply error unless we can prove that all possible
3099 executions of the program must execute the code. */
3100 warning_at (loc, 0, "function called through a non-compatible type");
3102 if (VOID_TYPE_P (return_type)
3103 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3104 pedwarn (loc, 0,
3105 "function with qualified void return type called");
3108 argarray = vec_safe_address (params);
3110 /* Check that arguments to builtin functions match the expectations. */
3111 if (fundecl && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL)
3112 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3113 argarray))
3114 return error_mark_node;
3116 /* Check that the arguments to the function are valid. */
3117 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3118 nargs, argarray, &arg_loc);
3120 if (name != NULL_TREE
3121 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3123 if (require_constant_value)
3124 result
3125 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3126 function, nargs, argarray);
3127 else
3128 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3129 function, nargs, argarray);
3130 if (TREE_CODE (result) == NOP_EXPR
3131 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3132 STRIP_TYPE_NOPS (result);
3134 else
3135 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3136 function, nargs, argarray);
3137 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3138 later. */
3139 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3140 TREE_NO_WARNING (result) = 1;
3142 /* In this improbable scenario, a nested function returns a VM type.
3143 Create a TARGET_EXPR so that the call always has a LHS, much as
3144 what the C++ FE does for functions returning non-PODs. */
3145 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3147 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3148 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3149 NULL_TREE, NULL_TREE);
3152 if (VOID_TYPE_P (TREE_TYPE (result)))
3154 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3155 pedwarn (loc, 0,
3156 "function with qualified void return type called");
3157 return result;
3159 return require_complete_type (loc, result);
3162 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3164 tree
3165 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3166 tree function, vec<tree, va_gc> *params,
3167 vec<tree, va_gc> *origtypes)
3169 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3170 STRIP_TYPE_NOPS (function);
3172 /* Convert anything with function type to a pointer-to-function. */
3173 if (TREE_CODE (function) == FUNCTION_DECL)
3175 /* Implement type-directed function overloading for builtins.
3176 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3177 handle all the type checking. The result is a complete expression
3178 that implements this function call. */
3179 tree tem = resolve_overloaded_builtin (loc, function, params);
3180 if (tem)
3181 return tem;
3183 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3186 /* Convert the argument expressions in the vector VALUES
3187 to the types in the list TYPELIST.
3189 If TYPELIST is exhausted, or when an element has NULL as its type,
3190 perform the default conversions.
3192 ORIGTYPES is the original types of the expressions in VALUES. This
3193 holds the type of enum values which have been converted to integral
3194 types. It may be NULL.
3196 FUNCTION is a tree for the called function. It is used only for
3197 error messages, where it is formatted with %qE.
3199 This is also where warnings about wrong number of args are generated.
3201 ARG_LOC are locations of function arguments (if any).
3203 Returns the actual number of arguments processed (which may be less
3204 than the length of VALUES in some error situations), or -1 on
3205 failure. */
3207 static int
3208 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3209 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3210 tree function, tree fundecl)
3212 tree typetail, val;
3213 unsigned int parmnum;
3214 bool error_args = false;
3215 const bool type_generic = fundecl
3216 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3217 bool type_generic_remove_excess_precision = false;
3218 bool type_generic_overflow_p = false;
3219 tree selector;
3221 /* Change pointer to function to the function itself for
3222 diagnostics. */
3223 if (TREE_CODE (function) == ADDR_EXPR
3224 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3225 function = TREE_OPERAND (function, 0);
3227 /* Handle an ObjC selector specially for diagnostics. */
3228 selector = objc_message_selector ();
3230 /* For type-generic built-in functions, determine whether excess
3231 precision should be removed (classification) or not
3232 (comparison). */
3233 if (type_generic
3234 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3236 switch (DECL_FUNCTION_CODE (fundecl))
3238 case BUILT_IN_ISFINITE:
3239 case BUILT_IN_ISINF:
3240 case BUILT_IN_ISINF_SIGN:
3241 case BUILT_IN_ISNAN:
3242 case BUILT_IN_ISNORMAL:
3243 case BUILT_IN_FPCLASSIFY:
3244 type_generic_remove_excess_precision = true;
3245 break;
3247 case BUILT_IN_ADD_OVERFLOW_P:
3248 case BUILT_IN_SUB_OVERFLOW_P:
3249 case BUILT_IN_MUL_OVERFLOW_P:
3250 /* The last argument of these type-generic builtins
3251 should not be promoted. */
3252 type_generic_overflow_p = true;
3253 break;
3255 default:
3256 break;
3260 /* Scan the given expressions and types, producing individual
3261 converted arguments. */
3263 for (typetail = typelist, parmnum = 0;
3264 values && values->iterate (parmnum, &val);
3265 ++parmnum)
3267 tree type = typetail ? TREE_VALUE (typetail) : 0;
3268 tree valtype = TREE_TYPE (val);
3269 tree rname = function;
3270 int argnum = parmnum + 1;
3271 const char *invalid_func_diag;
3272 bool excess_precision = false;
3273 bool npc;
3274 tree parmval;
3275 /* Some __atomic_* builtins have additional hidden argument at
3276 position 0. */
3277 location_t ploc
3278 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3279 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3280 : input_location;
3282 if (type == void_type_node)
3284 if (selector)
3285 error_at (loc, "too many arguments to method %qE", selector);
3286 else
3287 error_at (loc, "too many arguments to function %qE", function);
3288 inform_declaration (fundecl);
3289 return error_args ? -1 : (int) parmnum;
3292 if (selector && argnum > 2)
3294 rname = selector;
3295 argnum -= 2;
3298 npc = null_pointer_constant_p (val);
3300 /* If there is excess precision and a prototype, convert once to
3301 the required type rather than converting via the semantic
3302 type. Likewise without a prototype a float value represented
3303 as long double should be converted once to double. But for
3304 type-generic classification functions excess precision must
3305 be removed here. */
3306 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3307 && (type || !type_generic || !type_generic_remove_excess_precision))
3309 val = TREE_OPERAND (val, 0);
3310 excess_precision = true;
3312 val = c_fully_fold (val, false, NULL);
3313 STRIP_TYPE_NOPS (val);
3315 val = require_complete_type (ploc, val);
3317 /* Some floating-point arguments must be promoted to double when
3318 no type is specified by a prototype. This applies to
3319 arguments of type float, and to architecture-specific types
3320 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3321 bool promote_float_arg = false;
3322 if (type == NULL_TREE
3323 && TREE_CODE (valtype) == REAL_TYPE
3324 && (TYPE_PRECISION (valtype)
3325 <= TYPE_PRECISION (double_type_node))
3326 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3327 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3328 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3330 /* Promote this argument, unless it has a _FloatN or
3331 _FloatNx type. */
3332 promote_float_arg = true;
3333 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3334 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3336 promote_float_arg = false;
3337 break;
3341 if (type != NULL_TREE)
3343 /* Formal parm type is specified by a function prototype. */
3345 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3347 error_at (ploc, "type of formal parameter %d is incomplete",
3348 parmnum + 1);
3349 parmval = val;
3351 else
3353 tree origtype;
3355 /* Optionally warn about conversions that
3356 differ from the default conversions. */
3357 if (warn_traditional_conversion || warn_traditional)
3359 unsigned int formal_prec = TYPE_PRECISION (type);
3361 if (INTEGRAL_TYPE_P (type)
3362 && TREE_CODE (valtype) == REAL_TYPE)
3363 warning_at (ploc, OPT_Wtraditional_conversion,
3364 "passing argument %d of %qE as integer rather "
3365 "than floating due to prototype",
3366 argnum, rname);
3367 if (INTEGRAL_TYPE_P (type)
3368 && TREE_CODE (valtype) == COMPLEX_TYPE)
3369 warning_at (ploc, OPT_Wtraditional_conversion,
3370 "passing argument %d of %qE as integer rather "
3371 "than complex due to prototype",
3372 argnum, rname);
3373 else if (TREE_CODE (type) == COMPLEX_TYPE
3374 && TREE_CODE (valtype) == REAL_TYPE)
3375 warning_at (ploc, OPT_Wtraditional_conversion,
3376 "passing argument %d of %qE as complex rather "
3377 "than floating due to prototype",
3378 argnum, rname);
3379 else if (TREE_CODE (type) == REAL_TYPE
3380 && INTEGRAL_TYPE_P (valtype))
3381 warning_at (ploc, OPT_Wtraditional_conversion,
3382 "passing argument %d of %qE as floating rather "
3383 "than integer due to prototype",
3384 argnum, rname);
3385 else if (TREE_CODE (type) == COMPLEX_TYPE
3386 && INTEGRAL_TYPE_P (valtype))
3387 warning_at (ploc, OPT_Wtraditional_conversion,
3388 "passing argument %d of %qE as complex rather "
3389 "than integer due to prototype",
3390 argnum, rname);
3391 else if (TREE_CODE (type) == REAL_TYPE
3392 && TREE_CODE (valtype) == COMPLEX_TYPE)
3393 warning_at (ploc, OPT_Wtraditional_conversion,
3394 "passing argument %d of %qE as floating rather "
3395 "than complex due to prototype",
3396 argnum, rname);
3397 /* ??? At some point, messages should be written about
3398 conversions between complex types, but that's too messy
3399 to do now. */
3400 else if (TREE_CODE (type) == REAL_TYPE
3401 && TREE_CODE (valtype) == REAL_TYPE)
3403 /* Warn if any argument is passed as `float',
3404 since without a prototype it would be `double'. */
3405 if (formal_prec == TYPE_PRECISION (float_type_node)
3406 && type != dfloat32_type_node)
3407 warning_at (ploc, 0,
3408 "passing argument %d of %qE as %<float%> "
3409 "rather than %<double%> due to prototype",
3410 argnum, rname);
3412 /* Warn if mismatch between argument and prototype
3413 for decimal float types. Warn of conversions with
3414 binary float types and of precision narrowing due to
3415 prototype. */
3416 else if (type != valtype
3417 && (type == dfloat32_type_node
3418 || type == dfloat64_type_node
3419 || type == dfloat128_type_node
3420 || valtype == dfloat32_type_node
3421 || valtype == dfloat64_type_node
3422 || valtype == dfloat128_type_node)
3423 && (formal_prec
3424 <= TYPE_PRECISION (valtype)
3425 || (type == dfloat128_type_node
3426 && (valtype
3427 != dfloat64_type_node
3428 && (valtype
3429 != dfloat32_type_node)))
3430 || (type == dfloat64_type_node
3431 && (valtype
3432 != dfloat32_type_node))))
3433 warning_at (ploc, 0,
3434 "passing argument %d of %qE as %qT "
3435 "rather than %qT due to prototype",
3436 argnum, rname, type, valtype);
3439 /* Detect integer changing in width or signedness.
3440 These warnings are only activated with
3441 -Wtraditional-conversion, not with -Wtraditional. */
3442 else if (warn_traditional_conversion
3443 && INTEGRAL_TYPE_P (type)
3444 && INTEGRAL_TYPE_P (valtype))
3446 tree would_have_been = default_conversion (val);
3447 tree type1 = TREE_TYPE (would_have_been);
3449 if (val == error_mark_node)
3450 /* VAL could have been of incomplete type. */;
3451 else if (TREE_CODE (type) == ENUMERAL_TYPE
3452 && (TYPE_MAIN_VARIANT (type)
3453 == TYPE_MAIN_VARIANT (valtype)))
3454 /* No warning if function asks for enum
3455 and the actual arg is that enum type. */
3457 else if (formal_prec != TYPE_PRECISION (type1))
3458 warning_at (ploc, OPT_Wtraditional_conversion,
3459 "passing argument %d of %qE "
3460 "with different width due to prototype",
3461 argnum, rname);
3462 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3464 /* Don't complain if the formal parameter type
3465 is an enum, because we can't tell now whether
3466 the value was an enum--even the same enum. */
3467 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3469 else if (TREE_CODE (val) == INTEGER_CST
3470 && int_fits_type_p (val, type))
3471 /* Change in signedness doesn't matter
3472 if a constant value is unaffected. */
3474 /* If the value is extended from a narrower
3475 unsigned type, it doesn't matter whether we
3476 pass it as signed or unsigned; the value
3477 certainly is the same either way. */
3478 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3479 && TYPE_UNSIGNED (valtype))
3481 else if (TYPE_UNSIGNED (type))
3482 warning_at (ploc, OPT_Wtraditional_conversion,
3483 "passing argument %d of %qE "
3484 "as unsigned due to prototype",
3485 argnum, rname);
3486 else
3487 warning_at (ploc, OPT_Wtraditional_conversion,
3488 "passing argument %d of %qE "
3489 "as signed due to prototype",
3490 argnum, rname);
3494 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3495 sake of better warnings from convert_and_check. */
3496 if (excess_precision)
3497 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3498 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3499 parmval = convert_for_assignment (loc, ploc, type,
3500 val, origtype, ic_argpass,
3501 npc, fundecl, function,
3502 parmnum + 1);
3504 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3505 && INTEGRAL_TYPE_P (type)
3506 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3507 parmval = default_conversion (parmval);
3510 else if (promote_float_arg)
3512 if (type_generic)
3513 parmval = val;
3514 else
3516 /* Convert `float' to `double'. */
3517 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3518 warning_at (ploc, OPT_Wdouble_promotion,
3519 "implicit conversion from %qT to %qT when passing "
3520 "argument to function",
3521 valtype, double_type_node);
3522 parmval = convert (double_type_node, val);
3525 else if ((excess_precision && !type_generic)
3526 || (type_generic_overflow_p && parmnum == 2))
3527 /* A "double" argument with excess precision being passed
3528 without a prototype or in variable arguments.
3529 The last argument of __builtin_*_overflow_p should not be
3530 promoted. */
3531 parmval = convert (valtype, val);
3532 else if ((invalid_func_diag =
3533 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3535 error (invalid_func_diag);
3536 return -1;
3538 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3540 return -1;
3542 else
3543 /* Convert `short' and `char' to full-size `int'. */
3544 parmval = default_conversion (val);
3546 (*values)[parmnum] = parmval;
3547 if (parmval == error_mark_node)
3548 error_args = true;
3550 if (typetail)
3551 typetail = TREE_CHAIN (typetail);
3554 gcc_assert (parmnum == vec_safe_length (values));
3556 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3558 error_at (loc, "too few arguments to function %qE", function);
3559 inform_declaration (fundecl);
3560 return -1;
3563 return error_args ? -1 : (int) parmnum;
3566 /* This is the entry point used by the parser to build unary operators
3567 in the input. CODE, a tree_code, specifies the unary operator, and
3568 ARG is the operand. For unary plus, the C parser currently uses
3569 CONVERT_EXPR for code.
3571 LOC is the location to use for the tree generated.
3574 struct c_expr
3575 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3577 struct c_expr result;
3579 result.original_code = code;
3580 result.original_type = NULL;
3582 if (reject_gcc_builtin (arg.value))
3584 result.value = error_mark_node;
3586 else
3588 result.value = build_unary_op (loc, code, arg.value, false);
3590 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3591 overflow_warning (loc, result.value, arg.value);
3594 /* We are typically called when parsing a prefix token at LOC acting on
3595 ARG. Reflect this by updating the source range of the result to
3596 start at LOC and end at the end of ARG. */
3597 set_c_expr_source_range (&result,
3598 loc, arg.get_finish ());
3600 return result;
3603 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3605 static bool
3606 char_type_p (tree type)
3608 return (type == char_type_node
3609 || type == unsigned_char_type_node
3610 || type == signed_char_type_node
3611 || type == char16_type_node
3612 || type == char32_type_node);
3615 /* This is the entry point used by the parser to build binary operators
3616 in the input. CODE, a tree_code, specifies the binary operator, and
3617 ARG1 and ARG2 are the operands. In addition to constructing the
3618 expression, we check for operands that were written with other binary
3619 operators in a way that is likely to confuse the user.
3621 LOCATION is the location of the binary operator. */
3623 struct c_expr
3624 parser_build_binary_op (location_t location, enum tree_code code,
3625 struct c_expr arg1, struct c_expr arg2)
3627 struct c_expr result;
3629 enum tree_code code1 = arg1.original_code;
3630 enum tree_code code2 = arg2.original_code;
3631 tree type1 = (arg1.original_type
3632 ? arg1.original_type
3633 : TREE_TYPE (arg1.value));
3634 tree type2 = (arg2.original_type
3635 ? arg2.original_type
3636 : TREE_TYPE (arg2.value));
3638 result.value = build_binary_op (location, code,
3639 arg1.value, arg2.value, true);
3640 result.original_code = code;
3641 result.original_type = NULL;
3643 if (TREE_CODE (result.value) == ERROR_MARK)
3645 set_c_expr_source_range (&result,
3646 arg1.get_start (),
3647 arg2.get_finish ());
3648 return result;
3651 if (location != UNKNOWN_LOCATION)
3652 protected_set_expr_location (result.value, location);
3654 set_c_expr_source_range (&result,
3655 arg1.get_start (),
3656 arg2.get_finish ());
3658 /* Check for cases such as x+y<<z which users are likely
3659 to misinterpret. */
3660 if (warn_parentheses)
3661 warn_about_parentheses (location, code, code1, arg1.value, code2,
3662 arg2.value);
3664 if (warn_logical_op)
3665 warn_logical_operator (location, code, TREE_TYPE (result.value),
3666 code1, arg1.value, code2, arg2.value);
3668 if (warn_tautological_compare)
3670 tree lhs = arg1.value;
3671 tree rhs = arg2.value;
3672 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3674 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3675 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3676 lhs = NULL_TREE;
3677 else
3678 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3680 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3682 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3683 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3684 rhs = NULL_TREE;
3685 else
3686 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3688 if (lhs != NULL_TREE && rhs != NULL_TREE)
3689 warn_tautological_cmp (location, code, lhs, rhs);
3692 if (warn_logical_not_paren
3693 && TREE_CODE_CLASS (code) == tcc_comparison
3694 && code1 == TRUTH_NOT_EXPR
3695 && code2 != TRUTH_NOT_EXPR
3696 /* Avoid warning for !!x == y. */
3697 && (TREE_CODE (arg1.value) != NE_EXPR
3698 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3700 /* Avoid warning for !b == y where b has _Bool type. */
3701 tree t = integer_zero_node;
3702 if (TREE_CODE (arg1.value) == EQ_EXPR
3703 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3704 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3706 t = TREE_OPERAND (arg1.value, 0);
3709 if (TREE_TYPE (t) != integer_type_node)
3710 break;
3711 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3712 t = C_MAYBE_CONST_EXPR_EXPR (t);
3713 else if (CONVERT_EXPR_P (t))
3714 t = TREE_OPERAND (t, 0);
3715 else
3716 break;
3718 while (1);
3720 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3721 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3724 /* Warn about comparisons against string literals, with the exception
3725 of testing for equality or inequality of a string literal with NULL. */
3726 if (code == EQ_EXPR || code == NE_EXPR)
3728 if ((code1 == STRING_CST
3729 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3730 || (code2 == STRING_CST
3731 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3732 warning_at (location, OPT_Waddress,
3733 "comparison with string literal results in unspecified behavior");
3734 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3735 if (POINTER_TYPE_P (type1)
3736 && null_pointer_constant_p (arg2.value)
3737 && char_type_p (type2))
3739 auto_diagnostic_group d;
3740 if (warning_at (location, OPT_Wpointer_compare,
3741 "comparison between pointer and zero character "
3742 "constant"))
3743 inform (arg1.get_start (),
3744 "did you mean to dereference the pointer?");
3746 else if (POINTER_TYPE_P (type2)
3747 && null_pointer_constant_p (arg1.value)
3748 && char_type_p (type1))
3750 auto_diagnostic_group d;
3751 if (warning_at (location, OPT_Wpointer_compare,
3752 "comparison between pointer and zero character "
3753 "constant"))
3754 inform (arg2.get_start (),
3755 "did you mean to dereference the pointer?");
3758 else if (TREE_CODE_CLASS (code) == tcc_comparison
3759 && (code1 == STRING_CST || code2 == STRING_CST))
3760 warning_at (location, OPT_Waddress,
3761 "comparison with string literal results in unspecified behavior");
3763 if (TREE_OVERFLOW_P (result.value)
3764 && !TREE_OVERFLOW_P (arg1.value)
3765 && !TREE_OVERFLOW_P (arg2.value))
3766 overflow_warning (location, result.value);
3768 /* Warn about comparisons of different enum types. */
3769 if (warn_enum_compare
3770 && TREE_CODE_CLASS (code) == tcc_comparison
3771 && TREE_CODE (type1) == ENUMERAL_TYPE
3772 && TREE_CODE (type2) == ENUMERAL_TYPE
3773 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3774 warning_at (location, OPT_Wenum_compare,
3775 "comparison between %qT and %qT",
3776 type1, type2);
3778 return result;
3781 /* Return a tree for the difference of pointers OP0 and OP1.
3782 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3783 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3785 static tree
3786 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3788 tree restype = ptrdiff_type_node;
3789 tree result, inttype;
3791 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3792 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3793 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3794 tree orig_op1 = op1;
3796 /* If the operands point into different address spaces, we need to
3797 explicitly convert them to pointers into the common address space
3798 before we can subtract the numerical address values. */
3799 if (as0 != as1)
3801 addr_space_t as_common;
3802 tree common_type;
3804 /* Determine the common superset address space. This is guaranteed
3805 to exist because the caller verified that comp_target_types
3806 returned non-zero. */
3807 if (!addr_space_superset (as0, as1, &as_common))
3808 gcc_unreachable ();
3810 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3811 op0 = convert (common_type, op0);
3812 op1 = convert (common_type, op1);
3815 /* Determine integer type result of the subtraction. This will usually
3816 be the same as the result type (ptrdiff_t), but may need to be a wider
3817 type if pointers for the address space are wider than ptrdiff_t. */
3818 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3819 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3820 else
3821 inttype = restype;
3823 if (TREE_CODE (target_type) == VOID_TYPE)
3824 pedwarn (loc, OPT_Wpointer_arith,
3825 "pointer of type %<void *%> used in subtraction");
3826 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3827 pedwarn (loc, OPT_Wpointer_arith,
3828 "pointer to a function used in subtraction");
3830 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3832 gcc_assert (current_function_decl != NULL_TREE);
3834 op0 = save_expr (op0);
3835 op1 = save_expr (op1);
3837 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3838 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3841 /* First do the subtraction, then build the divide operator
3842 and only convert at the very end.
3843 Do not do default conversions in case restype is a short type. */
3845 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3846 pointers. If some platform cannot provide that, or has a larger
3847 ptrdiff_type to support differences larger than half the address
3848 space, cast the pointers to some larger integer type and do the
3849 computations in that type. */
3850 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3851 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3852 convert (inttype, op1), false);
3853 else
3855 /* Cast away qualifiers. */
3856 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3857 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3858 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3861 /* This generates an error if op1 is pointer to incomplete type. */
3862 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3863 error_at (loc, "arithmetic on pointer to an incomplete type");
3865 op1 = c_size_in_bytes (target_type);
3867 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3868 error_at (loc, "arithmetic on pointer to an empty aggregate");
3870 /* Divide by the size, in easiest possible way. */
3871 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3872 op0, convert (inttype, op1));
3874 /* Convert to final result type if necessary. */
3875 return convert (restype, result);
3878 /* Expand atomic compound assignments into an appropriate sequence as
3879 specified by the C11 standard section 6.5.16.2.
3881 _Atomic T1 E1
3882 T2 E2
3883 E1 op= E2
3885 This sequence is used for all types for which these operations are
3886 supported.
3888 In addition, built-in versions of the 'fe' prefixed routines may
3889 need to be invoked for floating point (real, complex or vector) when
3890 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3892 T1 newval;
3893 T1 old;
3894 T1 *addr
3895 T2 val
3896 fenv_t fenv
3898 addr = &E1;
3899 val = (E2);
3900 __atomic_load (addr, &old, SEQ_CST);
3901 feholdexcept (&fenv);
3902 loop:
3903 newval = old op val;
3904 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3905 SEQ_CST))
3906 goto done;
3907 feclearexcept (FE_ALL_EXCEPT);
3908 goto loop:
3909 done:
3910 feupdateenv (&fenv);
3912 The compiler will issue the __atomic_fetch_* built-in when possible,
3913 otherwise it will generate the generic form of the atomic operations.
3914 This requires temp(s) and has their address taken. The atomic processing
3915 is smart enough to figure out when the size of an object can utilize
3916 a lock-free version, and convert the built-in call to the appropriate
3917 lock-free routine. The optimizers will then dispose of any temps that
3918 are no longer required, and lock-free implementations are utilized as
3919 long as there is target support for the required size.
3921 If the operator is NOP_EXPR, then this is a simple assignment, and
3922 an __atomic_store is issued to perform the assignment rather than
3923 the above loop. */
3925 /* Build an atomic assignment at LOC, expanding into the proper
3926 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3927 the result of the operation, unless RETURN_OLD_P, in which case
3928 return the old value of LHS (this is only for postincrement and
3929 postdecrement). */
3931 static tree
3932 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3933 tree rhs, bool return_old_p)
3935 tree fndecl, func_call;
3936 vec<tree, va_gc> *params;
3937 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3938 tree old, old_addr;
3939 tree compound_stmt;
3940 tree stmt, goto_stmt;
3941 tree loop_label, loop_decl, done_label, done_decl;
3943 tree lhs_type = TREE_TYPE (lhs);
3944 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3945 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3946 tree rhs_semantic_type = TREE_TYPE (rhs);
3947 tree nonatomic_rhs_semantic_type;
3948 tree rhs_type;
3950 gcc_assert (TYPE_ATOMIC (lhs_type));
3952 if (return_old_p)
3953 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3955 /* Allocate enough vector items for a compare_exchange. */
3956 vec_alloc (params, 6);
3958 /* Create a compound statement to hold the sequence of statements
3959 with a loop. */
3960 compound_stmt = c_begin_compound_stmt (false);
3962 /* Remove any excess precision (which is only present here in the
3963 case of compound assignments). */
3964 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3966 gcc_assert (modifycode != NOP_EXPR);
3967 rhs = TREE_OPERAND (rhs, 0);
3969 rhs_type = TREE_TYPE (rhs);
3971 /* Fold the RHS if it hasn't already been folded. */
3972 if (modifycode != NOP_EXPR)
3973 rhs = c_fully_fold (rhs, false, NULL);
3975 /* Remove the qualifiers for the rest of the expressions and create
3976 the VAL temp variable to hold the RHS. */
3977 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3978 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3979 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3980 TYPE_UNQUALIFIED);
3981 val = create_tmp_var_raw (nonatomic_rhs_type);
3982 TREE_ADDRESSABLE (val) = 1;
3983 TREE_NO_WARNING (val) = 1;
3984 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3985 NULL_TREE);
3986 SET_EXPR_LOCATION (rhs, loc);
3987 add_stmt (rhs);
3989 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3990 an atomic_store. */
3991 if (modifycode == NOP_EXPR)
3993 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3994 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3995 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3996 params->quick_push (lhs_addr);
3997 params->quick_push (rhs);
3998 params->quick_push (seq_cst);
3999 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4000 add_stmt (func_call);
4002 /* Finish the compound statement. */
4003 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4005 /* VAL is the value which was stored, return a COMPOUND_STMT of
4006 the statement and that value. */
4007 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4010 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4011 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4012 isn't applicable for such builtins. ??? Do we want to handle enums? */
4013 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4014 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4016 built_in_function fncode;
4017 switch (modifycode)
4019 case PLUS_EXPR:
4020 case POINTER_PLUS_EXPR:
4021 fncode = (return_old_p
4022 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4023 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4024 break;
4025 case MINUS_EXPR:
4026 fncode = (return_old_p
4027 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4028 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4029 break;
4030 case BIT_AND_EXPR:
4031 fncode = (return_old_p
4032 ? BUILT_IN_ATOMIC_FETCH_AND_N
4033 : BUILT_IN_ATOMIC_AND_FETCH_N);
4034 break;
4035 case BIT_IOR_EXPR:
4036 fncode = (return_old_p
4037 ? BUILT_IN_ATOMIC_FETCH_OR_N
4038 : BUILT_IN_ATOMIC_OR_FETCH_N);
4039 break;
4040 case BIT_XOR_EXPR:
4041 fncode = (return_old_p
4042 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4043 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4044 break;
4045 default:
4046 goto cas_loop;
4049 /* We can only use "_1" through "_16" variants of the atomic fetch
4050 built-ins. */
4051 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4052 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4053 goto cas_loop;
4055 /* If this is a pointer type, we need to multiply by the size of
4056 the pointer target type. */
4057 if (POINTER_TYPE_P (lhs_type))
4059 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4060 /* ??? This would introduce -Wdiscarded-qualifiers
4061 warning: __atomic_fetch_* expect volatile void *
4062 type as the first argument. (Assignments between
4063 atomic and non-atomic objects are OK.) */
4064 || TYPE_RESTRICT (lhs_type))
4065 goto cas_loop;
4066 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4067 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4068 convert (ptrdiff_type_node, rhs),
4069 convert (ptrdiff_type_node, sz));
4072 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4073 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4074 fndecl = builtin_decl_explicit (fncode);
4075 params->quick_push (lhs_addr);
4076 params->quick_push (rhs);
4077 params->quick_push (seq_cst);
4078 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4080 newval = create_tmp_var_raw (nonatomic_lhs_type);
4081 TREE_ADDRESSABLE (newval) = 1;
4082 TREE_NO_WARNING (newval) = 1;
4083 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4084 NULL_TREE, NULL_TREE);
4085 SET_EXPR_LOCATION (rhs, loc);
4086 add_stmt (rhs);
4088 /* Finish the compound statement. */
4089 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4091 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4092 the statement and that value. */
4093 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4096 cas_loop:
4097 /* Create the variables and labels required for the op= form. */
4098 old = create_tmp_var_raw (nonatomic_lhs_type);
4099 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4100 TREE_ADDRESSABLE (old) = 1;
4101 TREE_NO_WARNING (old) = 1;
4103 newval = create_tmp_var_raw (nonatomic_lhs_type);
4104 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4105 TREE_ADDRESSABLE (newval) = 1;
4106 TREE_NO_WARNING (newval) = 1;
4108 loop_decl = create_artificial_label (loc);
4109 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4111 done_decl = create_artificial_label (loc);
4112 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4114 /* __atomic_load (addr, &old, SEQ_CST). */
4115 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4116 params->quick_push (lhs_addr);
4117 params->quick_push (old_addr);
4118 params->quick_push (seq_cst);
4119 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4120 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4121 NULL_TREE);
4122 add_stmt (old);
4123 params->truncate (0);
4125 /* Create the expressions for floating-point environment
4126 manipulation, if required. */
4127 bool need_fenv = (flag_trapping_math
4128 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4129 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4130 if (need_fenv)
4131 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4133 if (hold_call)
4134 add_stmt (hold_call);
4136 /* loop: */
4137 add_stmt (loop_label);
4139 /* newval = old + val; */
4140 if (rhs_type != rhs_semantic_type)
4141 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4142 rhs = build_binary_op (loc, modifycode, old, val, true);
4143 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4145 tree eptype = TREE_TYPE (rhs);
4146 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4147 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4149 else
4150 rhs = c_fully_fold (rhs, false, NULL);
4151 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4152 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4153 NULL_TREE, 0);
4154 if (rhs != error_mark_node)
4156 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4157 NULL_TREE);
4158 SET_EXPR_LOCATION (rhs, loc);
4159 add_stmt (rhs);
4162 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4163 goto done; */
4164 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4165 params->quick_push (lhs_addr);
4166 params->quick_push (old_addr);
4167 params->quick_push (newval_addr);
4168 params->quick_push (integer_zero_node);
4169 params->quick_push (seq_cst);
4170 params->quick_push (seq_cst);
4171 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4173 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4174 SET_EXPR_LOCATION (goto_stmt, loc);
4176 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4177 SET_EXPR_LOCATION (stmt, loc);
4178 add_stmt (stmt);
4180 if (clear_call)
4181 add_stmt (clear_call);
4183 /* goto loop; */
4184 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4185 SET_EXPR_LOCATION (goto_stmt, loc);
4186 add_stmt (goto_stmt);
4188 /* done: */
4189 add_stmt (done_label);
4191 if (update_call)
4192 add_stmt (update_call);
4194 /* Finish the compound statement. */
4195 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4197 /* NEWVAL is the value that was successfully stored, return a
4198 COMPOUND_EXPR of the statement and the appropriate value. */
4199 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4200 return_old_p ? old : newval);
4203 /* Construct and perhaps optimize a tree representation
4204 for a unary operation. CODE, a tree_code, specifies the operation
4205 and XARG is the operand.
4206 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4207 promotions (such as from short to int).
4208 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4209 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4210 to pointers in C99.
4212 LOCATION is the location of the operator. */
4214 tree
4215 build_unary_op (location_t location, enum tree_code code, tree xarg,
4216 bool noconvert)
4218 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4219 tree arg = xarg;
4220 tree argtype = NULL_TREE;
4221 enum tree_code typecode;
4222 tree val;
4223 tree ret = error_mark_node;
4224 tree eptype = NULL_TREE;
4225 const char *invalid_op_diag;
4226 bool int_operands;
4228 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4229 if (int_operands)
4230 arg = remove_c_maybe_const_expr (arg);
4232 if (code != ADDR_EXPR)
4233 arg = require_complete_type (location, arg);
4235 typecode = TREE_CODE (TREE_TYPE (arg));
4236 if (typecode == ERROR_MARK)
4237 return error_mark_node;
4238 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4239 typecode = INTEGER_TYPE;
4241 if ((invalid_op_diag
4242 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4244 error_at (location, invalid_op_diag);
4245 return error_mark_node;
4248 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4250 eptype = TREE_TYPE (arg);
4251 arg = TREE_OPERAND (arg, 0);
4254 switch (code)
4256 case CONVERT_EXPR:
4257 /* This is used for unary plus, because a CONVERT_EXPR
4258 is enough to prevent anybody from looking inside for
4259 associativity, but won't generate any code. */
4260 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4261 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4262 || typecode == VECTOR_TYPE))
4264 error_at (location, "wrong type argument to unary plus");
4265 return error_mark_node;
4267 else if (!noconvert)
4268 arg = default_conversion (arg);
4269 arg = non_lvalue_loc (location, arg);
4270 break;
4272 case NEGATE_EXPR:
4273 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4274 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4275 || typecode == VECTOR_TYPE))
4277 error_at (location, "wrong type argument to unary minus");
4278 return error_mark_node;
4280 else if (!noconvert)
4281 arg = default_conversion (arg);
4282 break;
4284 case BIT_NOT_EXPR:
4285 /* ~ works on integer types and non float vectors. */
4286 if (typecode == INTEGER_TYPE
4287 || (typecode == VECTOR_TYPE
4288 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4290 tree e = arg;
4292 /* Warn if the expression has boolean value. */
4293 while (TREE_CODE (e) == COMPOUND_EXPR)
4294 e = TREE_OPERAND (e, 1);
4296 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4297 || truth_value_p (TREE_CODE (e))))
4299 auto_diagnostic_group d;
4300 if (warning_at (location, OPT_Wbool_operation,
4301 "%<~%> on a boolean expression"))
4303 gcc_rich_location richloc (location);
4304 richloc.add_fixit_insert_before (location, "!");
4305 inform (&richloc, "did you mean to use logical not?");
4308 if (!noconvert)
4309 arg = default_conversion (arg);
4311 else if (typecode == COMPLEX_TYPE)
4313 code = CONJ_EXPR;
4314 pedwarn (location, OPT_Wpedantic,
4315 "ISO C does not support %<~%> for complex conjugation");
4316 if (!noconvert)
4317 arg = default_conversion (arg);
4319 else
4321 error_at (location, "wrong type argument to bit-complement");
4322 return error_mark_node;
4324 break;
4326 case ABS_EXPR:
4327 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4329 error_at (location, "wrong type argument to abs");
4330 return error_mark_node;
4332 else if (!noconvert)
4333 arg = default_conversion (arg);
4334 break;
4336 case ABSU_EXPR:
4337 if (!(typecode == INTEGER_TYPE))
4339 error_at (location, "wrong type argument to absu");
4340 return error_mark_node;
4342 else if (!noconvert)
4343 arg = default_conversion (arg);
4344 break;
4346 case CONJ_EXPR:
4347 /* Conjugating a real value is a no-op, but allow it anyway. */
4348 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4349 || typecode == COMPLEX_TYPE))
4351 error_at (location, "wrong type argument to conjugation");
4352 return error_mark_node;
4354 else if (!noconvert)
4355 arg = default_conversion (arg);
4356 break;
4358 case TRUTH_NOT_EXPR:
4359 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4360 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4361 && typecode != COMPLEX_TYPE)
4363 error_at (location,
4364 "wrong type argument to unary exclamation mark");
4365 return error_mark_node;
4367 if (int_operands)
4369 arg = c_objc_common_truthvalue_conversion (location, xarg);
4370 arg = remove_c_maybe_const_expr (arg);
4372 else
4373 arg = c_objc_common_truthvalue_conversion (location, arg);
4374 ret = invert_truthvalue_loc (location, arg);
4375 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4376 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4377 location = EXPR_LOCATION (ret);
4378 goto return_build_unary_op;
4380 case REALPART_EXPR:
4381 case IMAGPART_EXPR:
4382 ret = build_real_imag_expr (location, code, arg);
4383 if (ret == error_mark_node)
4384 return error_mark_node;
4385 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4386 eptype = TREE_TYPE (eptype);
4387 goto return_build_unary_op;
4389 case PREINCREMENT_EXPR:
4390 case POSTINCREMENT_EXPR:
4391 case PREDECREMENT_EXPR:
4392 case POSTDECREMENT_EXPR:
4394 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4396 tree inner = build_unary_op (location, code,
4397 C_MAYBE_CONST_EXPR_EXPR (arg),
4398 noconvert);
4399 if (inner == error_mark_node)
4400 return error_mark_node;
4401 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4402 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4403 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4404 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4405 goto return_build_unary_op;
4408 /* Complain about anything that is not a true lvalue. In
4409 Objective-C, skip this check for property_refs. */
4410 if (!objc_is_property_ref (arg)
4411 && !lvalue_or_else (location,
4412 arg, ((code == PREINCREMENT_EXPR
4413 || code == POSTINCREMENT_EXPR)
4414 ? lv_increment
4415 : lv_decrement)))
4416 return error_mark_node;
4418 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4420 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4421 warning_at (location, OPT_Wc___compat,
4422 "increment of enumeration value is invalid in C++");
4423 else
4424 warning_at (location, OPT_Wc___compat,
4425 "decrement of enumeration value is invalid in C++");
4428 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4430 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4431 warning_at (location, OPT_Wbool_operation,
4432 "increment of a boolean expression");
4433 else
4434 warning_at (location, OPT_Wbool_operation,
4435 "decrement of a boolean expression");
4438 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4439 arg = c_fully_fold (arg, false, NULL, true);
4441 bool atomic_op;
4442 atomic_op = really_atomic_lvalue (arg);
4444 /* Increment or decrement the real part of the value,
4445 and don't change the imaginary part. */
4446 if (typecode == COMPLEX_TYPE)
4448 tree real, imag;
4450 pedwarn (location, OPT_Wpedantic,
4451 "ISO C does not support %<++%> and %<--%> on complex types");
4453 if (!atomic_op)
4455 arg = stabilize_reference (arg);
4456 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4457 true);
4458 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4459 true);
4460 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4461 if (real == error_mark_node || imag == error_mark_node)
4462 return error_mark_node;
4463 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4464 real, imag);
4465 goto return_build_unary_op;
4469 /* Report invalid types. */
4471 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4472 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4473 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4475 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4476 error_at (location, "wrong type argument to increment");
4477 else
4478 error_at (location, "wrong type argument to decrement");
4480 return error_mark_node;
4484 tree inc;
4486 argtype = TREE_TYPE (arg);
4488 /* Compute the increment. */
4490 if (typecode == POINTER_TYPE)
4492 /* If pointer target is an incomplete type,
4493 we just cannot know how to do the arithmetic. */
4494 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4496 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4497 error_at (location,
4498 "increment of pointer to an incomplete type %qT",
4499 TREE_TYPE (argtype));
4500 else
4501 error_at (location,
4502 "decrement of pointer to an incomplete type %qT",
4503 TREE_TYPE (argtype));
4505 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4506 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4508 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4509 pedwarn (location, OPT_Wpointer_arith,
4510 "wrong type argument to increment");
4511 else
4512 pedwarn (location, OPT_Wpointer_arith,
4513 "wrong type argument to decrement");
4516 inc = c_size_in_bytes (TREE_TYPE (argtype));
4517 inc = convert_to_ptrofftype_loc (location, inc);
4519 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4521 /* For signed fract types, we invert ++ to -- or
4522 -- to ++, and change inc from 1 to -1, because
4523 it is not possible to represent 1 in signed fract constants.
4524 For unsigned fract types, the result always overflows and
4525 we get an undefined (original) or the maximum value. */
4526 if (code == PREINCREMENT_EXPR)
4527 code = PREDECREMENT_EXPR;
4528 else if (code == PREDECREMENT_EXPR)
4529 code = PREINCREMENT_EXPR;
4530 else if (code == POSTINCREMENT_EXPR)
4531 code = POSTDECREMENT_EXPR;
4532 else /* code == POSTDECREMENT_EXPR */
4533 code = POSTINCREMENT_EXPR;
4535 inc = integer_minus_one_node;
4536 inc = convert (argtype, inc);
4538 else
4540 inc = VECTOR_TYPE_P (argtype)
4541 ? build_one_cst (argtype)
4542 : integer_one_node;
4543 inc = convert (argtype, inc);
4546 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4547 need to ask Objective-C to build the increment or decrement
4548 expression for it. */
4549 if (objc_is_property_ref (arg))
4550 return objc_build_incr_expr_for_property_ref (location, code,
4551 arg, inc);
4553 /* Report a read-only lvalue. */
4554 if (TYPE_READONLY (argtype))
4556 readonly_error (location, arg,
4557 ((code == PREINCREMENT_EXPR
4558 || code == POSTINCREMENT_EXPR)
4559 ? lv_increment : lv_decrement));
4560 return error_mark_node;
4562 else if (TREE_READONLY (arg))
4563 readonly_warning (arg,
4564 ((code == PREINCREMENT_EXPR
4565 || code == POSTINCREMENT_EXPR)
4566 ? lv_increment : lv_decrement));
4568 /* If the argument is atomic, use the special code sequences for
4569 atomic compound assignment. */
4570 if (atomic_op)
4572 arg = stabilize_reference (arg);
4573 ret = build_atomic_assign (location, arg,
4574 ((code == PREINCREMENT_EXPR
4575 || code == POSTINCREMENT_EXPR)
4576 ? PLUS_EXPR
4577 : MINUS_EXPR),
4578 (FRACT_MODE_P (TYPE_MODE (argtype))
4579 ? inc
4580 : integer_one_node),
4581 (code == POSTINCREMENT_EXPR
4582 || code == POSTDECREMENT_EXPR));
4583 goto return_build_unary_op;
4586 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4587 val = boolean_increment (code, arg);
4588 else
4589 val = build2 (code, TREE_TYPE (arg), arg, inc);
4590 TREE_SIDE_EFFECTS (val) = 1;
4591 if (TREE_CODE (val) != code)
4592 TREE_NO_WARNING (val) = 1;
4593 ret = val;
4594 goto return_build_unary_op;
4597 case ADDR_EXPR:
4598 /* Note that this operation never does default_conversion. */
4600 /* The operand of unary '&' must be an lvalue (which excludes
4601 expressions of type void), or, in C99, the result of a [] or
4602 unary '*' operator. */
4603 if (VOID_TYPE_P (TREE_TYPE (arg))
4604 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4605 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4606 pedwarn (location, 0, "taking address of expression of type %<void%>");
4608 /* Let &* cancel out to simplify resulting code. */
4609 if (INDIRECT_REF_P (arg))
4611 /* Don't let this be an lvalue. */
4612 if (lvalue_p (TREE_OPERAND (arg, 0)))
4613 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4614 ret = TREE_OPERAND (arg, 0);
4615 goto return_build_unary_op;
4618 /* Anything not already handled and not a true memory reference
4619 or a non-lvalue array is an error. */
4620 if (typecode != FUNCTION_TYPE && !noconvert
4621 && !lvalue_or_else (location, arg, lv_addressof))
4622 return error_mark_node;
4624 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4625 folding later. */
4626 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4628 tree inner = build_unary_op (location, code,
4629 C_MAYBE_CONST_EXPR_EXPR (arg),
4630 noconvert);
4631 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4632 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4633 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4634 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4635 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4636 goto return_build_unary_op;
4639 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4640 argtype = TREE_TYPE (arg);
4642 /* If the lvalue is const or volatile, merge that into the type
4643 to which the address will point. This is only needed
4644 for function types. */
4645 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4646 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4647 && TREE_CODE (argtype) == FUNCTION_TYPE)
4649 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4650 int quals = orig_quals;
4652 if (TREE_READONLY (arg))
4653 quals |= TYPE_QUAL_CONST;
4654 if (TREE_THIS_VOLATILE (arg))
4655 quals |= TYPE_QUAL_VOLATILE;
4657 argtype = c_build_qualified_type (argtype, quals);
4660 switch (TREE_CODE (arg))
4662 case COMPONENT_REF:
4663 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4665 error_at (location, "cannot take address of bit-field %qD",
4666 TREE_OPERAND (arg, 1));
4667 return error_mark_node;
4670 /* fall through */
4672 case ARRAY_REF:
4673 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4675 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4676 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4678 error_at (location, "cannot take address of scalar with "
4679 "reverse storage order");
4680 return error_mark_node;
4683 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4684 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4685 warning_at (location, OPT_Wscalar_storage_order,
4686 "address of array with reverse scalar storage "
4687 "order requested");
4690 default:
4691 break;
4694 if (!c_mark_addressable (arg))
4695 return error_mark_node;
4697 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4698 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4700 argtype = build_pointer_type (argtype);
4702 /* ??? Cope with user tricks that amount to offsetof. Delete this
4703 when we have proper support for integer constant expressions. */
4704 val = get_base_address (arg);
4705 if (val && INDIRECT_REF_P (val)
4706 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4708 ret = fold_offsetof (arg, argtype);
4709 goto return_build_unary_op;
4712 val = build1 (ADDR_EXPR, argtype, arg);
4714 ret = val;
4715 goto return_build_unary_op;
4717 default:
4718 gcc_unreachable ();
4721 if (argtype == NULL_TREE)
4722 argtype = TREE_TYPE (arg);
4723 if (TREE_CODE (arg) == INTEGER_CST)
4724 ret = (require_constant_value
4725 ? fold_build1_initializer_loc (location, code, argtype, arg)
4726 : fold_build1_loc (location, code, argtype, arg));
4727 else
4728 ret = build1 (code, argtype, arg);
4729 return_build_unary_op:
4730 gcc_assert (ret != error_mark_node);
4731 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4732 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4733 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4734 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4735 ret = note_integer_operands (ret);
4736 if (eptype)
4737 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4738 protected_set_expr_location (ret, location);
4739 return ret;
4742 /* Return nonzero if REF is an lvalue valid for this language.
4743 Lvalues can be assigned, unless their type has TYPE_READONLY.
4744 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4746 bool
4747 lvalue_p (const_tree ref)
4749 const enum tree_code code = TREE_CODE (ref);
4751 switch (code)
4753 case REALPART_EXPR:
4754 case IMAGPART_EXPR:
4755 case COMPONENT_REF:
4756 return lvalue_p (TREE_OPERAND (ref, 0));
4758 case C_MAYBE_CONST_EXPR:
4759 return lvalue_p (TREE_OPERAND (ref, 1));
4761 case COMPOUND_LITERAL_EXPR:
4762 case STRING_CST:
4763 return true;
4765 case INDIRECT_REF:
4766 case ARRAY_REF:
4767 case VAR_DECL:
4768 case PARM_DECL:
4769 case RESULT_DECL:
4770 case ERROR_MARK:
4771 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4772 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4774 case BIND_EXPR:
4775 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4777 default:
4778 return false;
4782 /* Give a warning for storing in something that is read-only in GCC
4783 terms but not const in ISO C terms. */
4785 static void
4786 readonly_warning (tree arg, enum lvalue_use use)
4788 switch (use)
4790 case lv_assign:
4791 warning (0, "assignment of read-only location %qE", arg);
4792 break;
4793 case lv_increment:
4794 warning (0, "increment of read-only location %qE", arg);
4795 break;
4796 case lv_decrement:
4797 warning (0, "decrement of read-only location %qE", arg);
4798 break;
4799 default:
4800 gcc_unreachable ();
4802 return;
4806 /* Return nonzero if REF is an lvalue valid for this language;
4807 otherwise, print an error message and return zero. USE says
4808 how the lvalue is being used and so selects the error message.
4809 LOCATION is the location at which any error should be reported. */
4811 static int
4812 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4814 int win = lvalue_p (ref);
4816 if (!win)
4817 lvalue_error (loc, use);
4819 return win;
4822 /* Mark EXP saying that we need to be able to take the
4823 address of it; it should not be allocated in a register.
4824 Returns true if successful. ARRAY_REF_P is true if this
4825 is for ARRAY_REF construction - in that case we don't want
4826 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4827 it is fine to use ARRAY_REFs for vector subscripts on vector
4828 register variables. */
4830 bool
4831 c_mark_addressable (tree exp, bool array_ref_p)
4833 tree x = exp;
4835 while (1)
4836 switch (TREE_CODE (x))
4838 case VIEW_CONVERT_EXPR:
4839 if (array_ref_p
4840 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4841 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4842 return true;
4843 /* FALLTHRU */
4844 case COMPONENT_REF:
4845 case ADDR_EXPR:
4846 case ARRAY_REF:
4847 case REALPART_EXPR:
4848 case IMAGPART_EXPR:
4849 x = TREE_OPERAND (x, 0);
4850 break;
4852 case COMPOUND_LITERAL_EXPR:
4853 TREE_ADDRESSABLE (x) = 1;
4854 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
4855 return true;
4857 case CONSTRUCTOR:
4858 TREE_ADDRESSABLE (x) = 1;
4859 return true;
4861 case VAR_DECL:
4862 case CONST_DECL:
4863 case PARM_DECL:
4864 case RESULT_DECL:
4865 if (C_DECL_REGISTER (x)
4866 && DECL_NONLOCAL (x))
4868 if (TREE_PUBLIC (x) || is_global_var (x))
4870 error
4871 ("global register variable %qD used in nested function", x);
4872 return false;
4874 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4876 else if (C_DECL_REGISTER (x))
4878 if (TREE_PUBLIC (x) || is_global_var (x))
4879 error ("address of global register variable %qD requested", x);
4880 else
4881 error ("address of register variable %qD requested", x);
4882 return false;
4885 /* FALLTHRU */
4886 case FUNCTION_DECL:
4887 TREE_ADDRESSABLE (x) = 1;
4888 /* FALLTHRU */
4889 default:
4890 return true;
4894 /* Convert EXPR to TYPE, warning about conversion problems with
4895 constants. SEMANTIC_TYPE is the type this conversion would use
4896 without excess precision. If SEMANTIC_TYPE is NULL, this function
4897 is equivalent to convert_and_check. This function is a wrapper that
4898 handles conversions that may be different than
4899 the usual ones because of excess precision. */
4901 static tree
4902 ep_convert_and_check (location_t loc, tree type, tree expr,
4903 tree semantic_type)
4905 if (TREE_TYPE (expr) == type)
4906 return expr;
4908 /* For C11, integer conversions may have results with excess
4909 precision. */
4910 if (flag_isoc11 || !semantic_type)
4911 return convert_and_check (loc, type, expr);
4913 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4914 && TREE_TYPE (expr) != semantic_type)
4916 /* For integers, we need to check the real conversion, not
4917 the conversion to the excess precision type. */
4918 expr = convert_and_check (loc, semantic_type, expr);
4920 /* Result type is the excess precision type, which should be
4921 large enough, so do not check. */
4922 return convert (type, expr);
4925 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4926 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4927 if folded to an integer constant then the unselected half may
4928 contain arbitrary operations not normally permitted in constant
4929 expressions. Set the location of the expression to LOC. */
4931 tree
4932 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4933 tree op1, tree op1_original_type, location_t op1_loc,
4934 tree op2, tree op2_original_type, location_t op2_loc)
4936 tree type1;
4937 tree type2;
4938 enum tree_code code1;
4939 enum tree_code code2;
4940 tree result_type = NULL;
4941 tree semantic_result_type = NULL;
4942 tree orig_op1 = op1, orig_op2 = op2;
4943 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4944 bool ifexp_int_operands;
4945 tree ret;
4947 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4948 if (op1_int_operands)
4949 op1 = remove_c_maybe_const_expr (op1);
4950 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4951 if (op2_int_operands)
4952 op2 = remove_c_maybe_const_expr (op2);
4953 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4954 if (ifexp_int_operands)
4955 ifexp = remove_c_maybe_const_expr (ifexp);
4957 /* Promote both alternatives. */
4959 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4960 op1 = default_conversion (op1);
4961 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4962 op2 = default_conversion (op2);
4964 if (TREE_CODE (ifexp) == ERROR_MARK
4965 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4966 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4967 return error_mark_node;
4969 type1 = TREE_TYPE (op1);
4970 code1 = TREE_CODE (type1);
4971 type2 = TREE_TYPE (op2);
4972 code2 = TREE_CODE (type2);
4974 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4975 return error_mark_node;
4977 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4978 return error_mark_node;
4980 /* C90 does not permit non-lvalue arrays in conditional expressions.
4981 In C99 they will be pointers by now. */
4982 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4984 error_at (colon_loc, "non-lvalue array in conditional expression");
4985 return error_mark_node;
4988 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4989 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4990 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4991 || code1 == COMPLEX_TYPE)
4992 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4993 || code2 == COMPLEX_TYPE))
4995 semantic_result_type = c_common_type (type1, type2);
4996 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4998 op1 = TREE_OPERAND (op1, 0);
4999 type1 = TREE_TYPE (op1);
5000 gcc_assert (TREE_CODE (type1) == code1);
5002 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5004 op2 = TREE_OPERAND (op2, 0);
5005 type2 = TREE_TYPE (op2);
5006 gcc_assert (TREE_CODE (type2) == code2);
5010 if (warn_cxx_compat)
5012 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5013 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5015 if (TREE_CODE (t1) == ENUMERAL_TYPE
5016 && TREE_CODE (t2) == ENUMERAL_TYPE
5017 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5018 warning_at (colon_loc, OPT_Wc___compat,
5019 ("different enum types in conditional is "
5020 "invalid in C++: %qT vs %qT"),
5021 t1, t2);
5024 /* Quickly detect the usual case where op1 and op2 have the same type
5025 after promotion. */
5026 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5028 if (type1 == type2)
5029 result_type = type1;
5030 else
5031 result_type = TYPE_MAIN_VARIANT (type1);
5033 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5034 || code1 == COMPLEX_TYPE)
5035 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5036 || code2 == COMPLEX_TYPE))
5038 /* In C11, a conditional expression between a floating-point
5039 type and an integer type should convert the integer type to
5040 the evaluation format of the floating-point type, with
5041 possible excess precision. */
5042 tree eptype1 = type1;
5043 tree eptype2 = type2;
5044 if (flag_isoc11)
5046 tree eptype;
5047 if (ANY_INTEGRAL_TYPE_P (type1)
5048 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5050 eptype2 = eptype;
5051 if (!semantic_result_type)
5052 semantic_result_type = c_common_type (type1, type2);
5054 else if (ANY_INTEGRAL_TYPE_P (type2)
5055 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5057 eptype1 = eptype;
5058 if (!semantic_result_type)
5059 semantic_result_type = c_common_type (type1, type2);
5062 result_type = c_common_type (eptype1, eptype2);
5063 if (result_type == error_mark_node)
5064 return error_mark_node;
5065 do_warn_double_promotion (result_type, type1, type2,
5066 "implicit conversion from %qT to %qT to "
5067 "match other result of conditional",
5068 colon_loc);
5070 /* If -Wsign-compare, warn here if type1 and type2 have
5071 different signedness. We'll promote the signed to unsigned
5072 and later code won't know it used to be different.
5073 Do this check on the original types, so that explicit casts
5074 will be considered, but default promotions won't. */
5075 if (c_inhibit_evaluation_warnings == 0)
5077 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5078 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5080 if (unsigned_op1 ^ unsigned_op2)
5082 bool ovf;
5084 /* Do not warn if the result type is signed, since the
5085 signed type will only be chosen if it can represent
5086 all the values of the unsigned type. */
5087 if (!TYPE_UNSIGNED (result_type))
5088 /* OK */;
5089 else
5091 bool op1_maybe_const = true;
5092 bool op2_maybe_const = true;
5094 /* Do not warn if the signed quantity is an
5095 unsuffixed integer literal (or some static
5096 constant expression involving such literals) and
5097 it is non-negative. This warning requires the
5098 operands to be folded for best results, so do
5099 that folding in this case even without
5100 warn_sign_compare to avoid warning options
5101 possibly affecting code generation. */
5102 c_inhibit_evaluation_warnings
5103 += (ifexp == truthvalue_false_node);
5104 op1 = c_fully_fold (op1, require_constant_value,
5105 &op1_maybe_const);
5106 c_inhibit_evaluation_warnings
5107 -= (ifexp == truthvalue_false_node);
5109 c_inhibit_evaluation_warnings
5110 += (ifexp == truthvalue_true_node);
5111 op2 = c_fully_fold (op2, require_constant_value,
5112 &op2_maybe_const);
5113 c_inhibit_evaluation_warnings
5114 -= (ifexp == truthvalue_true_node);
5116 if (warn_sign_compare)
5118 if ((unsigned_op2
5119 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5120 || (unsigned_op1
5121 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5122 /* OK */;
5123 else if (unsigned_op2)
5124 warning_at (op1_loc, OPT_Wsign_compare,
5125 "operand of ?: changes signedness from "
5126 "%qT to %qT due to unsignedness of other "
5127 "operand", TREE_TYPE (orig_op1),
5128 TREE_TYPE (orig_op2));
5129 else
5130 warning_at (op2_loc, OPT_Wsign_compare,
5131 "operand of ?: changes signedness from "
5132 "%qT to %qT due to unsignedness of other "
5133 "operand", TREE_TYPE (orig_op2),
5134 TREE_TYPE (orig_op1));
5136 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5137 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5138 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5139 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5144 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5146 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5147 pedwarn (colon_loc, OPT_Wpedantic,
5148 "ISO C forbids conditional expr with only one void side");
5149 result_type = void_type_node;
5151 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5153 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5154 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5155 addr_space_t as_common;
5157 if (comp_target_types (colon_loc, type1, type2))
5158 result_type = common_pointer_type (type1, type2);
5159 else if (null_pointer_constant_p (orig_op1))
5160 result_type = type2;
5161 else if (null_pointer_constant_p (orig_op2))
5162 result_type = type1;
5163 else if (!addr_space_superset (as1, as2, &as_common))
5165 error_at (colon_loc, "pointers to disjoint address spaces "
5166 "used in conditional expression");
5167 return error_mark_node;
5169 else if (VOID_TYPE_P (TREE_TYPE (type1))
5170 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5172 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5173 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5174 & ~TYPE_QUALS (TREE_TYPE (type1))))
5175 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5176 "pointer to array loses qualifier "
5177 "in conditional expression");
5179 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5180 pedwarn (colon_loc, OPT_Wpedantic,
5181 "ISO C forbids conditional expr between "
5182 "%<void *%> and function pointer");
5183 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5184 TREE_TYPE (type2)));
5186 else if (VOID_TYPE_P (TREE_TYPE (type2))
5187 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5189 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5190 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5191 & ~TYPE_QUALS (TREE_TYPE (type2))))
5192 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5193 "pointer to array loses qualifier "
5194 "in conditional expression");
5196 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5197 pedwarn (colon_loc, OPT_Wpedantic,
5198 "ISO C forbids conditional expr between "
5199 "%<void *%> and function pointer");
5200 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5201 TREE_TYPE (type1)));
5203 /* Objective-C pointer comparisons are a bit more lenient. */
5204 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5205 result_type = objc_common_type (type1, type2);
5206 else
5208 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5210 pedwarn (colon_loc, 0,
5211 "pointer type mismatch in conditional expression");
5212 result_type = build_pointer_type
5213 (build_qualified_type (void_type_node, qual));
5216 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5218 if (!null_pointer_constant_p (orig_op2))
5219 pedwarn (colon_loc, 0,
5220 "pointer/integer type mismatch in conditional expression");
5221 else
5223 op2 = null_pointer_node;
5225 result_type = type1;
5227 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5229 if (!null_pointer_constant_p (orig_op1))
5230 pedwarn (colon_loc, 0,
5231 "pointer/integer type mismatch in conditional expression");
5232 else
5234 op1 = null_pointer_node;
5236 result_type = type2;
5239 if (!result_type)
5241 if (flag_cond_mismatch)
5242 result_type = void_type_node;
5243 else
5245 error_at (colon_loc, "type mismatch in conditional expression");
5246 return error_mark_node;
5250 /* Merge const and volatile flags of the incoming types. */
5251 result_type
5252 = build_type_variant (result_type,
5253 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5254 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5256 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5257 semantic_result_type);
5258 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5259 semantic_result_type);
5261 if (ifexp_bcp && ifexp == truthvalue_true_node)
5263 op2_int_operands = true;
5264 op1 = c_fully_fold (op1, require_constant_value, NULL);
5266 if (ifexp_bcp && ifexp == truthvalue_false_node)
5268 op1_int_operands = true;
5269 op2 = c_fully_fold (op2, require_constant_value, NULL);
5271 int_const = int_operands = (ifexp_int_operands
5272 && op1_int_operands
5273 && op2_int_operands);
5274 if (int_operands)
5276 int_const = ((ifexp == truthvalue_true_node
5277 && TREE_CODE (orig_op1) == INTEGER_CST
5278 && !TREE_OVERFLOW (orig_op1))
5279 || (ifexp == truthvalue_false_node
5280 && TREE_CODE (orig_op2) == INTEGER_CST
5281 && !TREE_OVERFLOW (orig_op2)));
5284 /* Need to convert condition operand into a vector mask. */
5285 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5287 tree vectype = TREE_TYPE (ifexp);
5288 tree elem_type = TREE_TYPE (vectype);
5289 tree zero = build_int_cst (elem_type, 0);
5290 tree zero_vec = build_vector_from_val (vectype, zero);
5291 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5292 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5295 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5296 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5297 else
5299 if (int_operands)
5301 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5302 nested inside of the expression. */
5303 op1 = c_fully_fold (op1, false, NULL);
5304 op2 = c_fully_fold (op2, false, NULL);
5306 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5307 if (int_operands)
5308 ret = note_integer_operands (ret);
5310 if (semantic_result_type)
5311 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5313 protected_set_expr_location (ret, colon_loc);
5315 /* If the OP1 and OP2 are the same and don't have side-effects,
5316 warn here, because the COND_EXPR will be turned into OP1. */
5317 if (warn_duplicated_branches
5318 && TREE_CODE (ret) == COND_EXPR
5319 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5320 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5321 "this condition has identical branches");
5323 return ret;
5326 /* Return a compound expression that performs two expressions and
5327 returns the value of the second of them.
5329 LOC is the location of the COMPOUND_EXPR. */
5331 tree
5332 build_compound_expr (location_t loc, tree expr1, tree expr2)
5334 bool expr1_int_operands, expr2_int_operands;
5335 tree eptype = NULL_TREE;
5336 tree ret;
5338 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5339 if (expr1_int_operands)
5340 expr1 = remove_c_maybe_const_expr (expr1);
5341 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5342 if (expr2_int_operands)
5343 expr2 = remove_c_maybe_const_expr (expr2);
5345 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5346 expr1 = TREE_OPERAND (expr1, 0);
5347 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5349 eptype = TREE_TYPE (expr2);
5350 expr2 = TREE_OPERAND (expr2, 0);
5353 if (!TREE_SIDE_EFFECTS (expr1))
5355 /* The left-hand operand of a comma expression is like an expression
5356 statement: with -Wunused, we should warn if it doesn't have
5357 any side-effects, unless it was explicitly cast to (void). */
5358 if (warn_unused_value)
5360 if (VOID_TYPE_P (TREE_TYPE (expr1))
5361 && CONVERT_EXPR_P (expr1))
5362 ; /* (void) a, b */
5363 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5364 && TREE_CODE (expr1) == COMPOUND_EXPR
5365 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5366 ; /* (void) a, (void) b, c */
5367 else
5368 warning_at (loc, OPT_Wunused_value,
5369 "left-hand operand of comma expression has no effect");
5372 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5373 && warn_unused_value)
5375 tree r = expr1;
5376 location_t cloc = loc;
5377 while (TREE_CODE (r) == COMPOUND_EXPR)
5379 if (EXPR_HAS_LOCATION (r))
5380 cloc = EXPR_LOCATION (r);
5381 r = TREE_OPERAND (r, 1);
5383 if (!TREE_SIDE_EFFECTS (r)
5384 && !VOID_TYPE_P (TREE_TYPE (r))
5385 && !CONVERT_EXPR_P (r))
5386 warning_at (cloc, OPT_Wunused_value,
5387 "right-hand operand of comma expression has no effect");
5390 /* With -Wunused, we should also warn if the left-hand operand does have
5391 side-effects, but computes a value which is not used. For example, in
5392 `foo() + bar(), baz()' the result of the `+' operator is not used,
5393 so we should issue a warning. */
5394 else if (warn_unused_value)
5395 warn_if_unused_value (expr1, loc);
5397 if (expr2 == error_mark_node)
5398 return error_mark_node;
5400 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5402 if (flag_isoc99
5403 && expr1_int_operands
5404 && expr2_int_operands)
5405 ret = note_integer_operands (ret);
5407 if (eptype)
5408 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5410 protected_set_expr_location (ret, loc);
5411 return ret;
5414 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5415 which we are casting. OTYPE is the type of the expression being
5416 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5417 of the cast. -Wcast-qual appeared on the command line. Named
5418 address space qualifiers are not handled here, because they result
5419 in different warnings. */
5421 static void
5422 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5424 tree in_type = type;
5425 tree in_otype = otype;
5426 int added = 0;
5427 int discarded = 0;
5428 bool is_const;
5430 /* Check that the qualifiers on IN_TYPE are a superset of the
5431 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5432 nodes is uninteresting and we stop as soon as we hit a
5433 non-POINTER_TYPE node on either type. */
5436 in_otype = TREE_TYPE (in_otype);
5437 in_type = TREE_TYPE (in_type);
5439 /* GNU C allows cv-qualified function types. 'const' means the
5440 function is very pure, 'volatile' means it can't return. We
5441 need to warn when such qualifiers are added, not when they're
5442 taken away. */
5443 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5444 && TREE_CODE (in_type) == FUNCTION_TYPE)
5445 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5446 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5447 else
5448 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5449 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5451 while (TREE_CODE (in_type) == POINTER_TYPE
5452 && TREE_CODE (in_otype) == POINTER_TYPE);
5454 if (added)
5455 warning_at (loc, OPT_Wcast_qual,
5456 "cast adds %q#v qualifier to function type", added);
5458 if (discarded)
5459 /* There are qualifiers present in IN_OTYPE that are not present
5460 in IN_TYPE. */
5461 warning_at (loc, OPT_Wcast_qual,
5462 "cast discards %qv qualifier from pointer target type",
5463 discarded);
5465 if (added || discarded)
5466 return;
5468 /* A cast from **T to const **T is unsafe, because it can cause a
5469 const value to be changed with no additional warning. We only
5470 issue this warning if T is the same on both sides, and we only
5471 issue the warning if there are the same number of pointers on
5472 both sides, as otherwise the cast is clearly unsafe anyhow. A
5473 cast is unsafe when a qualifier is added at one level and const
5474 is not present at all outer levels.
5476 To issue this warning, we check at each level whether the cast
5477 adds new qualifiers not already seen. We don't need to special
5478 case function types, as they won't have the same
5479 TYPE_MAIN_VARIANT. */
5481 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5482 return;
5483 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5484 return;
5486 in_type = type;
5487 in_otype = otype;
5488 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5491 in_type = TREE_TYPE (in_type);
5492 in_otype = TREE_TYPE (in_otype);
5493 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5494 && !is_const)
5496 warning_at (loc, OPT_Wcast_qual,
5497 "to be safe all intermediate pointers in cast from "
5498 "%qT to %qT must be %<const%> qualified",
5499 otype, type);
5500 break;
5502 if (is_const)
5503 is_const = TYPE_READONLY (in_type);
5505 while (TREE_CODE (in_type) == POINTER_TYPE);
5508 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5510 static bool
5511 c_safe_arg_type_equiv_p (tree t1, tree t2)
5513 t1 = TYPE_MAIN_VARIANT (t1);
5514 t2 = TYPE_MAIN_VARIANT (t2);
5516 if (TREE_CODE (t1) == POINTER_TYPE
5517 && TREE_CODE (t2) == POINTER_TYPE)
5518 return true;
5520 /* The signedness of the parameter matters only when an integral
5521 type smaller than int is promoted to int, otherwise only the
5522 precision of the parameter matters.
5523 This check should make sure that the callee does not see
5524 undefined values in argument registers. */
5525 if (INTEGRAL_TYPE_P (t1)
5526 && INTEGRAL_TYPE_P (t2)
5527 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5528 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5529 || !targetm.calls.promote_prototypes (NULL_TREE)
5530 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5531 return true;
5533 return comptypes (t1, t2);
5536 /* Check if a type cast between two function types can be considered safe. */
5538 static bool
5539 c_safe_function_type_cast_p (tree t1, tree t2)
5541 if (TREE_TYPE (t1) == void_type_node &&
5542 TYPE_ARG_TYPES (t1) == void_list_node)
5543 return true;
5545 if (TREE_TYPE (t2) == void_type_node &&
5546 TYPE_ARG_TYPES (t2) == void_list_node)
5547 return true;
5549 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5550 return false;
5552 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5553 t1 && t2;
5554 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5555 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5556 return false;
5558 return true;
5561 /* Build an expression representing a cast to type TYPE of expression EXPR.
5562 LOC is the location of the cast-- typically the open paren of the cast. */
5564 tree
5565 build_c_cast (location_t loc, tree type, tree expr)
5567 tree value;
5569 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5570 expr = TREE_OPERAND (expr, 0);
5572 value = expr;
5574 if (type == error_mark_node || expr == error_mark_node)
5575 return error_mark_node;
5577 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5578 only in <protocol> qualifications. But when constructing cast expressions,
5579 the protocols do matter and must be kept around. */
5580 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5581 return build1 (NOP_EXPR, type, expr);
5583 type = TYPE_MAIN_VARIANT (type);
5585 if (TREE_CODE (type) == ARRAY_TYPE)
5587 error_at (loc, "cast specifies array type");
5588 return error_mark_node;
5591 if (TREE_CODE (type) == FUNCTION_TYPE)
5593 error_at (loc, "cast specifies function type");
5594 return error_mark_node;
5597 if (!VOID_TYPE_P (type))
5599 value = require_complete_type (loc, value);
5600 if (value == error_mark_node)
5601 return error_mark_node;
5604 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5606 if (RECORD_OR_UNION_TYPE_P (type))
5607 pedwarn (loc, OPT_Wpedantic,
5608 "ISO C forbids casting nonscalar to the same type");
5610 /* Convert to remove any qualifiers from VALUE's type. */
5611 value = convert (type, value);
5613 else if (TREE_CODE (type) == UNION_TYPE)
5615 tree field;
5617 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5618 if (TREE_TYPE (field) != error_mark_node
5619 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5620 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5621 break;
5623 if (field)
5625 tree t;
5626 bool maybe_const = true;
5628 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5629 t = c_fully_fold (value, false, &maybe_const);
5630 t = build_constructor_single (type, field, t);
5631 if (!maybe_const)
5632 t = c_wrap_maybe_const (t, true);
5633 t = digest_init (loc, type, t,
5634 NULL_TREE, false, true, 0);
5635 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5636 return t;
5638 error_at (loc, "cast to union type from type not present in union");
5639 return error_mark_node;
5641 else
5643 tree otype, ovalue;
5645 if (type == void_type_node)
5647 tree t = build1 (CONVERT_EXPR, type, value);
5648 SET_EXPR_LOCATION (t, loc);
5649 return t;
5652 otype = TREE_TYPE (value);
5654 /* Optionally warn about potentially worrisome casts. */
5655 if (warn_cast_qual
5656 && TREE_CODE (type) == POINTER_TYPE
5657 && TREE_CODE (otype) == POINTER_TYPE)
5658 handle_warn_cast_qual (loc, type, otype);
5660 /* Warn about conversions between pointers to disjoint
5661 address spaces. */
5662 if (TREE_CODE (type) == POINTER_TYPE
5663 && TREE_CODE (otype) == POINTER_TYPE
5664 && !null_pointer_constant_p (value))
5666 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5667 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5668 addr_space_t as_common;
5670 if (!addr_space_superset (as_to, as_from, &as_common))
5672 if (ADDR_SPACE_GENERIC_P (as_from))
5673 warning_at (loc, 0, "cast to %s address space pointer "
5674 "from disjoint generic address space pointer",
5675 c_addr_space_name (as_to));
5677 else if (ADDR_SPACE_GENERIC_P (as_to))
5678 warning_at (loc, 0, "cast to generic address space pointer "
5679 "from disjoint %s address space pointer",
5680 c_addr_space_name (as_from));
5682 else
5683 warning_at (loc, 0, "cast to %s address space pointer "
5684 "from disjoint %s address space pointer",
5685 c_addr_space_name (as_to),
5686 c_addr_space_name (as_from));
5690 /* Warn about possible alignment problems. */
5691 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5692 && TREE_CODE (type) == POINTER_TYPE
5693 && TREE_CODE (otype) == POINTER_TYPE
5694 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5695 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5696 /* Don't warn about opaque types, where the actual alignment
5697 restriction is unknown. */
5698 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5699 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5700 && min_align_of_type (TREE_TYPE (type))
5701 > min_align_of_type (TREE_TYPE (otype)))
5702 warning_at (loc, OPT_Wcast_align,
5703 "cast increases required alignment of target type");
5705 if (TREE_CODE (type) == INTEGER_TYPE
5706 && TREE_CODE (otype) == POINTER_TYPE
5707 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5708 /* Unlike conversion of integers to pointers, where the
5709 warning is disabled for converting constants because
5710 of cases such as SIG_*, warn about converting constant
5711 pointers to integers. In some cases it may cause unwanted
5712 sign extension, and a warning is appropriate. */
5713 warning_at (loc, OPT_Wpointer_to_int_cast,
5714 "cast from pointer to integer of different size");
5716 if (TREE_CODE (value) == CALL_EXPR
5717 && TREE_CODE (type) != TREE_CODE (otype))
5718 warning_at (loc, OPT_Wbad_function_cast,
5719 "cast from function call of type %qT "
5720 "to non-matching type %qT", otype, type);
5722 if (TREE_CODE (type) == POINTER_TYPE
5723 && TREE_CODE (otype) == INTEGER_TYPE
5724 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5725 /* Don't warn about converting any constant. */
5726 && !TREE_CONSTANT (value))
5727 warning_at (loc,
5728 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5729 "of different size");
5731 if (warn_strict_aliasing <= 2)
5732 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5734 /* If pedantic, warn for conversions between function and object
5735 pointer types, except for converting a null pointer constant
5736 to function pointer type. */
5737 if (pedantic
5738 && TREE_CODE (type) == POINTER_TYPE
5739 && TREE_CODE (otype) == POINTER_TYPE
5740 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5741 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5742 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5743 "conversion of function pointer to object pointer type");
5745 if (pedantic
5746 && TREE_CODE (type) == POINTER_TYPE
5747 && TREE_CODE (otype) == POINTER_TYPE
5748 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5749 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5750 && !null_pointer_constant_p (value))
5751 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5752 "conversion of object pointer to function pointer type");
5754 if (TREE_CODE (type) == POINTER_TYPE
5755 && TREE_CODE (otype) == POINTER_TYPE
5756 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5757 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5758 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5759 TREE_TYPE (otype)))
5760 warning_at (loc, OPT_Wcast_function_type,
5761 "cast between incompatible function types"
5762 " from %qT to %qT", otype, type);
5764 ovalue = value;
5765 value = convert (type, value);
5767 /* Ignore any integer overflow caused by the cast. */
5768 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5770 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5772 if (!TREE_OVERFLOW (value))
5774 /* Avoid clobbering a shared constant. */
5775 value = copy_node (value);
5776 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5779 else if (TREE_OVERFLOW (value))
5780 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5781 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5785 /* Don't let a cast be an lvalue. */
5786 if (lvalue_p (value))
5787 value = non_lvalue_loc (loc, value);
5789 /* Don't allow the results of casting to floating-point or complex
5790 types be confused with actual constants, or casts involving
5791 integer and pointer types other than direct integer-to-integer
5792 and integer-to-pointer be confused with integer constant
5793 expressions and null pointer constants. */
5794 if (TREE_CODE (value) == REAL_CST
5795 || TREE_CODE (value) == COMPLEX_CST
5796 || (TREE_CODE (value) == INTEGER_CST
5797 && !((TREE_CODE (expr) == INTEGER_CST
5798 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5799 || TREE_CODE (expr) == REAL_CST
5800 || TREE_CODE (expr) == COMPLEX_CST)))
5801 value = build1 (NOP_EXPR, type, value);
5803 protected_set_expr_location (value, loc);
5804 return value;
5807 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5808 location of the open paren of the cast, or the position of the cast
5809 expr. */
5810 tree
5811 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5813 tree type;
5814 tree type_expr = NULL_TREE;
5815 bool type_expr_const = true;
5816 tree ret;
5817 int saved_wsp = warn_strict_prototypes;
5819 /* This avoids warnings about unprototyped casts on
5820 integers. E.g. "#define SIG_DFL (void(*)())0". */
5821 if (TREE_CODE (expr) == INTEGER_CST)
5822 warn_strict_prototypes = 0;
5823 type = groktypename (type_name, &type_expr, &type_expr_const);
5824 warn_strict_prototypes = saved_wsp;
5826 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5827 && reject_gcc_builtin (expr))
5828 return error_mark_node;
5830 ret = build_c_cast (loc, type, expr);
5831 if (type_expr)
5833 bool inner_expr_const = true;
5834 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5835 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5836 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5837 && inner_expr_const);
5838 SET_EXPR_LOCATION (ret, loc);
5841 if (!EXPR_HAS_LOCATION (ret))
5842 protected_set_expr_location (ret, loc);
5844 /* C++ does not permits types to be defined in a cast, but it
5845 allows references to incomplete types. */
5846 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5847 warning_at (loc, OPT_Wc___compat,
5848 "defining a type in a cast is invalid in C++");
5850 return ret;
5853 /* Build an assignment expression of lvalue LHS from value RHS.
5854 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5855 may differ from TREE_TYPE (LHS) for an enum bitfield.
5856 MODIFYCODE is the code for a binary operator that we use
5857 to combine the old value of LHS with RHS to get the new value.
5858 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5859 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5860 which may differ from TREE_TYPE (RHS) for an enum value.
5862 LOCATION is the location of the MODIFYCODE operator.
5863 RHS_LOC is the location of the RHS. */
5865 tree
5866 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5867 enum tree_code modifycode,
5868 location_t rhs_loc, tree rhs, tree rhs_origtype)
5870 tree result;
5871 tree newrhs;
5872 tree rhseval = NULL_TREE;
5873 tree lhstype = TREE_TYPE (lhs);
5874 tree olhstype = lhstype;
5875 bool npc;
5876 bool is_atomic_op;
5878 /* Types that aren't fully specified cannot be used in assignments. */
5879 lhs = require_complete_type (location, lhs);
5881 /* Avoid duplicate error messages from operands that had errors. */
5882 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5883 return error_mark_node;
5885 /* Ensure an error for assigning a non-lvalue array to an array in
5886 C90. */
5887 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5889 error_at (location, "assignment to expression with array type");
5890 return error_mark_node;
5893 /* For ObjC properties, defer this check. */
5894 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5895 return error_mark_node;
5897 is_atomic_op = really_atomic_lvalue (lhs);
5899 newrhs = rhs;
5901 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5903 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5904 lhs_origtype, modifycode, rhs_loc, rhs,
5905 rhs_origtype);
5906 if (inner == error_mark_node)
5907 return error_mark_node;
5908 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5909 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5910 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5911 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5912 protected_set_expr_location (result, location);
5913 return result;
5916 /* If a binary op has been requested, combine the old LHS value with the RHS
5917 producing the value we should actually store into the LHS. */
5919 if (modifycode != NOP_EXPR)
5921 lhs = c_fully_fold (lhs, false, NULL, true);
5922 lhs = stabilize_reference (lhs);
5924 /* Construct the RHS for any non-atomic compound assignemnt. */
5925 if (!is_atomic_op)
5927 /* If in LHS op= RHS the RHS has side-effects, ensure they
5928 are preevaluated before the rest of the assignment expression's
5929 side-effects, because RHS could contain e.g. function calls
5930 that modify LHS. */
5931 if (TREE_SIDE_EFFECTS (rhs))
5933 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5934 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5935 else
5936 newrhs = save_expr (rhs);
5937 rhseval = newrhs;
5938 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5939 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5940 newrhs);
5942 newrhs = build_binary_op (location,
5943 modifycode, lhs, newrhs, true);
5945 /* The original type of the right hand side is no longer
5946 meaningful. */
5947 rhs_origtype = NULL_TREE;
5951 if (c_dialect_objc ())
5953 /* Check if we are modifying an Objective-C property reference;
5954 if so, we need to generate setter calls. */
5955 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5956 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5957 else
5958 result = objc_maybe_build_modify_expr (lhs, newrhs);
5959 if (result)
5960 goto return_result;
5962 /* Else, do the check that we postponed for Objective-C. */
5963 if (!lvalue_or_else (location, lhs, lv_assign))
5964 return error_mark_node;
5967 /* Give an error for storing in something that is 'const'. */
5969 if (TYPE_READONLY (lhstype)
5970 || (RECORD_OR_UNION_TYPE_P (lhstype)
5971 && C_TYPE_FIELDS_READONLY (lhstype)))
5973 readonly_error (location, lhs, lv_assign);
5974 return error_mark_node;
5976 else if (TREE_READONLY (lhs))
5977 readonly_warning (lhs, lv_assign);
5979 /* If storing into a structure or union member,
5980 it has probably been given type `int'.
5981 Compute the type that would go with
5982 the actual amount of storage the member occupies. */
5984 if (TREE_CODE (lhs) == COMPONENT_REF
5985 && (TREE_CODE (lhstype) == INTEGER_TYPE
5986 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5987 || TREE_CODE (lhstype) == REAL_TYPE
5988 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5989 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5991 /* If storing in a field that is in actuality a short or narrower than one,
5992 we must store in the field in its actual type. */
5994 if (lhstype != TREE_TYPE (lhs))
5996 lhs = copy_node (lhs);
5997 TREE_TYPE (lhs) = lhstype;
6000 /* Issue -Wc++-compat warnings about an assignment to an enum type
6001 when LHS does not have its original type. This happens for,
6002 e.g., an enum bitfield in a struct. */
6003 if (warn_cxx_compat
6004 && lhs_origtype != NULL_TREE
6005 && lhs_origtype != lhstype
6006 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6008 tree checktype = (rhs_origtype != NULL_TREE
6009 ? rhs_origtype
6010 : TREE_TYPE (rhs));
6011 if (checktype != error_mark_node
6012 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6013 || (is_atomic_op && modifycode != NOP_EXPR)))
6014 warning_at (location, OPT_Wc___compat,
6015 "enum conversion in assignment is invalid in C++");
6018 /* If the lhs is atomic, remove that qualifier. */
6019 if (is_atomic_op)
6021 lhstype = build_qualified_type (lhstype,
6022 (TYPE_QUALS (lhstype)
6023 & ~TYPE_QUAL_ATOMIC));
6024 olhstype = build_qualified_type (olhstype,
6025 (TYPE_QUALS (lhstype)
6026 & ~TYPE_QUAL_ATOMIC));
6029 /* Convert new value to destination type. Fold it first, then
6030 restore any excess precision information, for the sake of
6031 conversion warnings. */
6033 if (!(is_atomic_op && modifycode != NOP_EXPR))
6035 tree rhs_semantic_type = NULL_TREE;
6036 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6038 rhs_semantic_type = TREE_TYPE (newrhs);
6039 newrhs = TREE_OPERAND (newrhs, 0);
6041 npc = null_pointer_constant_p (newrhs);
6042 newrhs = c_fully_fold (newrhs, false, NULL);
6043 if (rhs_semantic_type)
6044 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6045 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6046 rhs_origtype, ic_assign, npc,
6047 NULL_TREE, NULL_TREE, 0);
6048 if (TREE_CODE (newrhs) == ERROR_MARK)
6049 return error_mark_node;
6052 /* Emit ObjC write barrier, if necessary. */
6053 if (c_dialect_objc () && flag_objc_gc)
6055 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6056 if (result)
6058 protected_set_expr_location (result, location);
6059 goto return_result;
6063 /* Scan operands. */
6065 if (is_atomic_op)
6066 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6067 else
6069 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6070 TREE_SIDE_EFFECTS (result) = 1;
6071 protected_set_expr_location (result, location);
6074 /* If we got the LHS in a different type for storing in,
6075 convert the result back to the nominal type of LHS
6076 so that the value we return always has the same type
6077 as the LHS argument. */
6079 if (olhstype == TREE_TYPE (result))
6080 goto return_result;
6082 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6083 rhs_origtype, ic_assign, false, NULL_TREE,
6084 NULL_TREE, 0);
6085 protected_set_expr_location (result, location);
6087 return_result:
6088 if (rhseval)
6089 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6090 return result;
6093 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6094 This is used to implement -fplan9-extensions. */
6096 static bool
6097 find_anonymous_field_with_type (tree struct_type, tree type)
6099 tree field;
6100 bool found;
6102 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6103 found = false;
6104 for (field = TYPE_FIELDS (struct_type);
6105 field != NULL_TREE;
6106 field = TREE_CHAIN (field))
6108 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6109 ? c_build_qualified_type (TREE_TYPE (field),
6110 TYPE_QUAL_ATOMIC)
6111 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6112 if (DECL_NAME (field) == NULL
6113 && comptypes (type, fieldtype))
6115 if (found)
6116 return false;
6117 found = true;
6119 else if (DECL_NAME (field) == NULL
6120 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6121 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6123 if (found)
6124 return false;
6125 found = true;
6128 return found;
6131 /* RHS is an expression whose type is pointer to struct. If there is
6132 an anonymous field in RHS with type TYPE, then return a pointer to
6133 that field in RHS. This is used with -fplan9-extensions. This
6134 returns NULL if no conversion could be found. */
6136 static tree
6137 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6139 tree rhs_struct_type, lhs_main_type;
6140 tree field, found_field;
6141 bool found_sub_field;
6142 tree ret;
6144 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6145 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6146 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6148 gcc_assert (POINTER_TYPE_P (type));
6149 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6150 ? c_build_qualified_type (TREE_TYPE (type),
6151 TYPE_QUAL_ATOMIC)
6152 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6154 found_field = NULL_TREE;
6155 found_sub_field = false;
6156 for (field = TYPE_FIELDS (rhs_struct_type);
6157 field != NULL_TREE;
6158 field = TREE_CHAIN (field))
6160 if (DECL_NAME (field) != NULL_TREE
6161 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6162 continue;
6163 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6164 ? c_build_qualified_type (TREE_TYPE (field),
6165 TYPE_QUAL_ATOMIC)
6166 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6167 if (comptypes (lhs_main_type, fieldtype))
6169 if (found_field != NULL_TREE)
6170 return NULL_TREE;
6171 found_field = field;
6173 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6174 lhs_main_type))
6176 if (found_field != NULL_TREE)
6177 return NULL_TREE;
6178 found_field = field;
6179 found_sub_field = true;
6183 if (found_field == NULL_TREE)
6184 return NULL_TREE;
6186 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6187 build_fold_indirect_ref (rhs), found_field,
6188 NULL_TREE);
6189 ret = build_fold_addr_expr_loc (location, ret);
6191 if (found_sub_field)
6193 ret = convert_to_anonymous_field (location, type, ret);
6194 gcc_assert (ret != NULL_TREE);
6197 return ret;
6200 /* Issue an error message for a bad initializer component.
6201 GMSGID identifies the message.
6202 The component name is taken from the spelling stack. */
6204 static void
6205 error_init (location_t loc, const char *gmsgid)
6207 char *ofwhat;
6209 auto_diagnostic_group d;
6211 /* The gmsgid may be a format string with %< and %>. */
6212 error_at (loc, gmsgid);
6213 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6214 if (*ofwhat)
6215 inform (loc, "(near initialization for %qs)", ofwhat);
6218 /* Issue a pedantic warning for a bad initializer component. OPT is
6219 the option OPT_* (from options.h) controlling this warning or 0 if
6220 it is unconditionally given. GMSGID identifies the message. The
6221 component name is taken from the spelling stack. */
6223 static void ATTRIBUTE_GCC_DIAG (3,0)
6224 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6226 /* Use the location where a macro was expanded rather than where
6227 it was defined to make sure macros defined in system headers
6228 but used incorrectly elsewhere are diagnosed. */
6229 location_t exploc = expansion_point_location_if_in_system_header (loc);
6230 auto_diagnostic_group d;
6231 va_list ap;
6232 va_start (ap, gmsgid);
6233 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6234 va_end (ap);
6235 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6236 if (*ofwhat && warned)
6237 inform (exploc, "(near initialization for %qs)", ofwhat);
6240 /* Issue a warning for a bad initializer component.
6242 OPT is the OPT_W* value corresponding to the warning option that
6243 controls this warning. GMSGID identifies the message. The
6244 component name is taken from the spelling stack. */
6246 static void
6247 warning_init (location_t loc, int opt, const char *gmsgid)
6249 char *ofwhat;
6250 bool warned;
6252 auto_diagnostic_group d;
6254 /* Use the location where a macro was expanded rather than where
6255 it was defined to make sure macros defined in system headers
6256 but used incorrectly elsewhere are diagnosed. */
6257 location_t exploc = expansion_point_location_if_in_system_header (loc);
6259 /* The gmsgid may be a format string with %< and %>. */
6260 warned = warning_at (exploc, opt, gmsgid);
6261 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6262 if (*ofwhat && warned)
6263 inform (exploc, "(near initialization for %qs)", ofwhat);
6266 /* If TYPE is an array type and EXPR is a parenthesized string
6267 constant, warn if pedantic that EXPR is being used to initialize an
6268 object of type TYPE. */
6270 void
6271 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6273 if (pedantic
6274 && TREE_CODE (type) == ARRAY_TYPE
6275 && TREE_CODE (expr.value) == STRING_CST
6276 && expr.original_code != STRING_CST)
6277 pedwarn_init (loc, OPT_Wpedantic,
6278 "array initialized from parenthesized string constant");
6281 /* Attempt to locate the parameter with the given index within FNDECL,
6282 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6284 static location_t
6285 get_fndecl_argument_location (tree fndecl, int argnum)
6287 int i;
6288 tree param;
6290 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6291 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6292 i < argnum && param;
6293 i++, param = TREE_CHAIN (param))
6296 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6297 return DECL_SOURCE_LOCATION (FNDECL). */
6298 if (param == NULL)
6299 return DECL_SOURCE_LOCATION (fndecl);
6301 return DECL_SOURCE_LOCATION (param);
6304 /* Issue a note about a mismatching argument for parameter PARMNUM
6305 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6306 Attempt to issue the note at the pertinent parameter of the decl;
6307 failing that issue it at the location of FUNDECL; failing that
6308 issue it at PLOC. */
6310 static void
6311 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6312 tree expected_type, tree actual_type)
6314 location_t loc;
6315 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6316 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6317 else
6318 loc = ploc;
6320 inform (loc,
6321 "expected %qT but argument is of type %qT",
6322 expected_type, actual_type);
6325 /* Convert value RHS to type TYPE as preparation for an assignment to
6326 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6327 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6328 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6329 constant before any folding.
6330 The real work of conversion is done by `convert'.
6331 The purpose of this function is to generate error messages
6332 for assignments that are not allowed in C.
6333 ERRTYPE says whether it is argument passing, assignment,
6334 initialization or return.
6336 In the following example, '~' denotes where EXPR_LOC and '^' where
6337 LOCATION point to:
6339 f (var); [ic_argpass]
6340 ^ ~~~
6341 x = var; [ic_assign]
6342 ^ ~~~;
6343 int x = var; [ic_init]
6345 return x; [ic_return]
6348 FUNCTION is a tree for the function being called.
6349 PARMNUM is the number of the argument, for printing in error messages. */
6351 static tree
6352 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6353 tree rhs, tree origtype, enum impl_conv errtype,
6354 bool null_pointer_constant, tree fundecl,
6355 tree function, int parmnum)
6357 enum tree_code codel = TREE_CODE (type);
6358 tree orig_rhs = rhs;
6359 tree rhstype;
6360 enum tree_code coder;
6361 tree rname = NULL_TREE;
6362 bool objc_ok = false;
6364 /* Use the expansion point location to handle cases such as user's
6365 function returning a wrong-type macro defined in a system header. */
6366 location = expansion_point_location_if_in_system_header (location);
6368 if (errtype == ic_argpass)
6370 tree selector;
6371 /* Change pointer to function to the function itself for
6372 diagnostics. */
6373 if (TREE_CODE (function) == ADDR_EXPR
6374 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6375 function = TREE_OPERAND (function, 0);
6377 /* Handle an ObjC selector specially for diagnostics. */
6378 selector = objc_message_selector ();
6379 rname = function;
6380 if (selector && parmnum > 2)
6382 rname = selector;
6383 parmnum -= 2;
6387 /* This macro is used to emit diagnostics to ensure that all format
6388 strings are complete sentences, visible to gettext and checked at
6389 compile time. */
6390 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6391 do { \
6392 switch (errtype) \
6394 case ic_argpass: \
6396 auto_diagnostic_group d; \
6397 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6398 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6400 break; \
6401 case ic_assign: \
6402 pedwarn (LOCATION, OPT, AS); \
6403 break; \
6404 case ic_init: \
6405 pedwarn_init (LOCATION, OPT, IN); \
6406 break; \
6407 case ic_return: \
6408 pedwarn (LOCATION, OPT, RE); \
6409 break; \
6410 default: \
6411 gcc_unreachable (); \
6413 } while (0)
6415 /* This macro is used to emit diagnostics to ensure that all format
6416 strings are complete sentences, visible to gettext and checked at
6417 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6418 extra parameter to enumerate qualifiers. */
6419 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6420 do { \
6421 switch (errtype) \
6423 case ic_argpass: \
6425 auto_diagnostic_group d; \
6426 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6427 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6429 break; \
6430 case ic_assign: \
6431 pedwarn (LOCATION, OPT, AS, QUALS); \
6432 break; \
6433 case ic_init: \
6434 pedwarn (LOCATION, OPT, IN, QUALS); \
6435 break; \
6436 case ic_return: \
6437 pedwarn (LOCATION, OPT, RE, QUALS); \
6438 break; \
6439 default: \
6440 gcc_unreachable (); \
6442 } while (0)
6444 /* This macro is used to emit diagnostics to ensure that all format
6445 strings are complete sentences, visible to gettext and checked at
6446 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6447 warning_at instead of pedwarn. */
6448 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6449 do { \
6450 switch (errtype) \
6452 case ic_argpass: \
6454 auto_diagnostic_group d; \
6455 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6456 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6458 break; \
6459 case ic_assign: \
6460 warning_at (LOCATION, OPT, AS, QUALS); \
6461 break; \
6462 case ic_init: \
6463 warning_at (LOCATION, OPT, IN, QUALS); \
6464 break; \
6465 case ic_return: \
6466 warning_at (LOCATION, OPT, RE, QUALS); \
6467 break; \
6468 default: \
6469 gcc_unreachable (); \
6471 } while (0)
6473 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6474 rhs = TREE_OPERAND (rhs, 0);
6476 rhstype = TREE_TYPE (rhs);
6477 coder = TREE_CODE (rhstype);
6479 if (coder == ERROR_MARK)
6480 return error_mark_node;
6482 if (c_dialect_objc ())
6484 int parmno;
6486 switch (errtype)
6488 case ic_return:
6489 parmno = 0;
6490 break;
6492 case ic_assign:
6493 parmno = -1;
6494 break;
6496 case ic_init:
6497 parmno = -2;
6498 break;
6500 default:
6501 parmno = parmnum;
6502 break;
6505 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6508 if (warn_cxx_compat)
6510 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6511 if (checktype != error_mark_node
6512 && TREE_CODE (type) == ENUMERAL_TYPE
6513 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6514 switch (errtype)
6516 case ic_argpass:
6517 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6518 "passing argument %d of %qE is invalid in C++",
6519 parmnum, rname))
6520 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6521 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6522 "expected %qT but argument is of type %qT",
6523 type, rhstype);
6524 break;
6525 case ic_assign:
6526 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6527 "%qT in assignment is invalid in C++", rhstype, type);
6528 break;
6529 case ic_init:
6530 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6531 "%qT to %qT in initialization is invalid in C++",
6532 rhstype, type);
6533 break;
6534 case ic_return:
6535 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6536 "%qT in return is invalid in C++", rhstype, type);
6537 break;
6538 default:
6539 gcc_unreachable ();
6543 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6544 return rhs;
6546 if (coder == VOID_TYPE)
6548 /* Except for passing an argument to an unprototyped function,
6549 this is a constraint violation. When passing an argument to
6550 an unprototyped function, it is compile-time undefined;
6551 making it a constraint in that case was rejected in
6552 DR#252. */
6553 error_at (location, "void value not ignored as it ought to be");
6554 return error_mark_node;
6556 rhs = require_complete_type (location, rhs);
6557 if (rhs == error_mark_node)
6558 return error_mark_node;
6560 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6561 return error_mark_node;
6563 /* A non-reference type can convert to a reference. This handles
6564 va_start, va_copy and possibly port built-ins. */
6565 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6567 if (!lvalue_p (rhs))
6569 error_at (location, "cannot pass rvalue to reference parameter");
6570 return error_mark_node;
6572 if (!c_mark_addressable (rhs))
6573 return error_mark_node;
6574 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6575 SET_EXPR_LOCATION (rhs, location);
6577 rhs = convert_for_assignment (location, expr_loc,
6578 build_pointer_type (TREE_TYPE (type)),
6579 rhs, origtype, errtype,
6580 null_pointer_constant, fundecl, function,
6581 parmnum);
6582 if (rhs == error_mark_node)
6583 return error_mark_node;
6585 rhs = build1 (NOP_EXPR, type, rhs);
6586 SET_EXPR_LOCATION (rhs, location);
6587 return rhs;
6589 /* Some types can interconvert without explicit casts. */
6590 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6591 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6592 return convert (type, rhs);
6593 /* Arithmetic types all interconvert, and enum is treated like int. */
6594 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6595 || codel == FIXED_POINT_TYPE
6596 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6597 || codel == BOOLEAN_TYPE)
6598 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6599 || coder == FIXED_POINT_TYPE
6600 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6601 || coder == BOOLEAN_TYPE))
6603 tree ret;
6604 bool save = in_late_binary_op;
6605 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6606 || (coder == REAL_TYPE
6607 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6608 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6609 in_late_binary_op = true;
6610 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6611 ? expr_loc : location, type, orig_rhs);
6612 in_late_binary_op = save;
6613 return ret;
6616 /* Aggregates in different TUs might need conversion. */
6617 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6618 && codel == coder
6619 && comptypes (type, rhstype))
6620 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6621 ? expr_loc : location, type, rhs);
6623 /* Conversion to a transparent union or record from its member types.
6624 This applies only to function arguments. */
6625 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6626 && TYPE_TRANSPARENT_AGGR (type))
6627 && errtype == ic_argpass)
6629 tree memb, marginal_memb = NULL_TREE;
6631 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6633 tree memb_type = TREE_TYPE (memb);
6635 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6636 TYPE_MAIN_VARIANT (rhstype)))
6637 break;
6639 if (TREE_CODE (memb_type) != POINTER_TYPE)
6640 continue;
6642 if (coder == POINTER_TYPE)
6644 tree ttl = TREE_TYPE (memb_type);
6645 tree ttr = TREE_TYPE (rhstype);
6647 /* Any non-function converts to a [const][volatile] void *
6648 and vice versa; otherwise, targets must be the same.
6649 Meanwhile, the lhs target must have all the qualifiers of
6650 the rhs. */
6651 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6652 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6653 || comp_target_types (location, memb_type, rhstype))
6655 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6656 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6657 /* If this type won't generate any warnings, use it. */
6658 if (lquals == rquals
6659 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6660 && TREE_CODE (ttl) == FUNCTION_TYPE)
6661 ? ((lquals | rquals) == rquals)
6662 : ((lquals | rquals) == lquals)))
6663 break;
6665 /* Keep looking for a better type, but remember this one. */
6666 if (!marginal_memb)
6667 marginal_memb = memb;
6671 /* Can convert integer zero to any pointer type. */
6672 if (null_pointer_constant)
6674 rhs = null_pointer_node;
6675 break;
6679 if (memb || marginal_memb)
6681 if (!memb)
6683 /* We have only a marginally acceptable member type;
6684 it needs a warning. */
6685 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6686 tree ttr = TREE_TYPE (rhstype);
6688 /* Const and volatile mean something different for function
6689 types, so the usual warnings are not appropriate. */
6690 if (TREE_CODE (ttr) == FUNCTION_TYPE
6691 && TREE_CODE (ttl) == FUNCTION_TYPE)
6693 /* Because const and volatile on functions are
6694 restrictions that say the function will not do
6695 certain things, it is okay to use a const or volatile
6696 function where an ordinary one is wanted, but not
6697 vice-versa. */
6698 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6699 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6700 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6701 OPT_Wdiscarded_qualifiers,
6702 G_("passing argument %d of %qE "
6703 "makes %q#v qualified function "
6704 "pointer from unqualified"),
6705 G_("assignment makes %q#v qualified "
6706 "function pointer from "
6707 "unqualified"),
6708 G_("initialization makes %q#v qualified "
6709 "function pointer from "
6710 "unqualified"),
6711 G_("return makes %q#v qualified function "
6712 "pointer from unqualified"),
6713 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6715 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6716 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6717 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6718 OPT_Wdiscarded_qualifiers,
6719 G_("passing argument %d of %qE discards "
6720 "%qv qualifier from pointer target type"),
6721 G_("assignment discards %qv qualifier "
6722 "from pointer target type"),
6723 G_("initialization discards %qv qualifier "
6724 "from pointer target type"),
6725 G_("return discards %qv qualifier from "
6726 "pointer target type"),
6727 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6729 memb = marginal_memb;
6732 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6733 pedwarn (location, OPT_Wpedantic,
6734 "ISO C prohibits argument conversion to union type");
6736 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6737 return build_constructor_single (type, memb, rhs);
6741 /* Conversions among pointers */
6742 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6743 && (coder == codel))
6745 tree ttl = TREE_TYPE (type);
6746 tree ttr = TREE_TYPE (rhstype);
6747 tree mvl = ttl;
6748 tree mvr = ttr;
6749 bool is_opaque_pointer;
6750 int target_cmp = 0; /* Cache comp_target_types () result. */
6751 addr_space_t asl;
6752 addr_space_t asr;
6754 if (TREE_CODE (mvl) != ARRAY_TYPE)
6755 mvl = (TYPE_ATOMIC (mvl)
6756 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6757 TYPE_QUAL_ATOMIC)
6758 : TYPE_MAIN_VARIANT (mvl));
6759 if (TREE_CODE (mvr) != ARRAY_TYPE)
6760 mvr = (TYPE_ATOMIC (mvr)
6761 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6762 TYPE_QUAL_ATOMIC)
6763 : TYPE_MAIN_VARIANT (mvr));
6764 /* Opaque pointers are treated like void pointers. */
6765 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6767 /* The Plan 9 compiler permits a pointer to a struct to be
6768 automatically converted into a pointer to an anonymous field
6769 within the struct. */
6770 if (flag_plan9_extensions
6771 && RECORD_OR_UNION_TYPE_P (mvl)
6772 && RECORD_OR_UNION_TYPE_P (mvr)
6773 && mvl != mvr)
6775 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6776 if (new_rhs != NULL_TREE)
6778 rhs = new_rhs;
6779 rhstype = TREE_TYPE (rhs);
6780 coder = TREE_CODE (rhstype);
6781 ttr = TREE_TYPE (rhstype);
6782 mvr = TYPE_MAIN_VARIANT (ttr);
6786 /* C++ does not allow the implicit conversion void* -> T*. However,
6787 for the purpose of reducing the number of false positives, we
6788 tolerate the special case of
6790 int *p = NULL;
6792 where NULL is typically defined in C to be '(void *) 0'. */
6793 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6794 warning_at (errtype == ic_argpass ? expr_loc : location,
6795 OPT_Wc___compat,
6796 "request for implicit conversion "
6797 "from %qT to %qT not permitted in C++", rhstype, type);
6799 /* See if the pointers point to incompatible address spaces. */
6800 asl = TYPE_ADDR_SPACE (ttl);
6801 asr = TYPE_ADDR_SPACE (ttr);
6802 if (!null_pointer_constant_p (rhs)
6803 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6805 switch (errtype)
6807 case ic_argpass:
6808 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6809 "non-enclosed address space", parmnum, rname);
6810 break;
6811 case ic_assign:
6812 error_at (location, "assignment from pointer to "
6813 "non-enclosed address space");
6814 break;
6815 case ic_init:
6816 error_at (location, "initialization from pointer to "
6817 "non-enclosed address space");
6818 break;
6819 case ic_return:
6820 error_at (location, "return from pointer to "
6821 "non-enclosed address space");
6822 break;
6823 default:
6824 gcc_unreachable ();
6826 return error_mark_node;
6829 /* Check if the right-hand side has a format attribute but the
6830 left-hand side doesn't. */
6831 if (warn_suggest_attribute_format
6832 && check_missing_format_attribute (type, rhstype))
6834 switch (errtype)
6836 case ic_argpass:
6837 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6838 "argument %d of %qE might be "
6839 "a candidate for a format attribute",
6840 parmnum, rname);
6841 break;
6842 case ic_assign:
6843 warning_at (location, OPT_Wsuggest_attribute_format,
6844 "assignment left-hand side might be "
6845 "a candidate for a format attribute");
6846 break;
6847 case ic_init:
6848 warning_at (location, OPT_Wsuggest_attribute_format,
6849 "initialization left-hand side might be "
6850 "a candidate for a format attribute");
6851 break;
6852 case ic_return:
6853 warning_at (location, OPT_Wsuggest_attribute_format,
6854 "return type might be "
6855 "a candidate for a format attribute");
6856 break;
6857 default:
6858 gcc_unreachable ();
6862 /* Any non-function converts to a [const][volatile] void *
6863 and vice versa; otherwise, targets must be the same.
6864 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6865 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6866 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6867 || (target_cmp = comp_target_types (location, type, rhstype))
6868 || is_opaque_pointer
6869 || ((c_common_unsigned_type (mvl)
6870 == c_common_unsigned_type (mvr))
6871 && (c_common_signed_type (mvl)
6872 == c_common_signed_type (mvr))
6873 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6875 /* Warn about loss of qualifers from pointers to arrays with
6876 qualifiers on the element type. */
6877 if (TREE_CODE (ttr) == ARRAY_TYPE)
6879 ttr = strip_array_types (ttr);
6880 ttl = strip_array_types (ttl);
6882 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6883 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6884 WARNING_FOR_QUALIFIERS (location, expr_loc,
6885 OPT_Wdiscarded_array_qualifiers,
6886 G_("passing argument %d of %qE discards "
6887 "%qv qualifier from pointer target type"),
6888 G_("assignment discards %qv qualifier "
6889 "from pointer target type"),
6890 G_("initialization discards %qv qualifier "
6891 "from pointer target type"),
6892 G_("return discards %qv qualifier from "
6893 "pointer target type"),
6894 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6896 else if (pedantic
6897 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6899 (VOID_TYPE_P (ttr)
6900 && !null_pointer_constant
6901 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6902 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6903 G_("ISO C forbids passing argument %d of "
6904 "%qE between function pointer "
6905 "and %<void *%>"),
6906 G_("ISO C forbids assignment between "
6907 "function pointer and %<void *%>"),
6908 G_("ISO C forbids initialization between "
6909 "function pointer and %<void *%>"),
6910 G_("ISO C forbids return between function "
6911 "pointer and %<void *%>"));
6912 /* Const and volatile mean something different for function types,
6913 so the usual warnings are not appropriate. */
6914 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6915 && TREE_CODE (ttl) != FUNCTION_TYPE)
6917 /* Don't warn about loss of qualifier for conversions from
6918 qualified void* to pointers to arrays with corresponding
6919 qualifier on the element type. */
6920 if (!pedantic)
6921 ttl = strip_array_types (ttl);
6923 /* Assignments between atomic and non-atomic objects are OK. */
6924 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6925 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6927 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6928 OPT_Wdiscarded_qualifiers,
6929 G_("passing argument %d of %qE discards "
6930 "%qv qualifier from pointer target type"),
6931 G_("assignment discards %qv qualifier "
6932 "from pointer target type"),
6933 G_("initialization discards %qv qualifier "
6934 "from pointer target type"),
6935 G_("return discards %qv qualifier from "
6936 "pointer target type"),
6937 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6939 /* If this is not a case of ignoring a mismatch in signedness,
6940 no warning. */
6941 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6942 || target_cmp)
6944 /* If there is a mismatch, do warn. */
6945 else if (warn_pointer_sign)
6946 switch (errtype)
6948 case ic_argpass:
6950 auto_diagnostic_group d;
6951 range_label_for_type_mismatch rhs_label (rhstype, type);
6952 gcc_rich_location richloc (expr_loc, &rhs_label);
6953 if (pedwarn (&richloc, OPT_Wpointer_sign,
6954 "pointer targets in passing argument %d of "
6955 "%qE differ in signedness", parmnum, rname))
6956 inform_for_arg (fundecl, expr_loc, parmnum, type,
6957 rhstype);
6959 break;
6960 case ic_assign:
6961 pedwarn (location, OPT_Wpointer_sign,
6962 "pointer targets in assignment from %qT to %qT "
6963 "differ in signedness", rhstype, type);
6964 break;
6965 case ic_init:
6966 pedwarn_init (location, OPT_Wpointer_sign,
6967 "pointer targets in initialization of %qT "
6968 "from %qT differ in signedness", type,
6969 rhstype);
6970 break;
6971 case ic_return:
6972 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6973 "returning %qT from a function with return type "
6974 "%qT differ in signedness", rhstype, type);
6975 break;
6976 default:
6977 gcc_unreachable ();
6980 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6981 && TREE_CODE (ttr) == FUNCTION_TYPE)
6983 /* Because const and volatile on functions are restrictions
6984 that say the function will not do certain things,
6985 it is okay to use a const or volatile function
6986 where an ordinary one is wanted, but not vice-versa. */
6987 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6988 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6989 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6990 OPT_Wdiscarded_qualifiers,
6991 G_("passing argument %d of %qE makes "
6992 "%q#v qualified function pointer "
6993 "from unqualified"),
6994 G_("assignment makes %q#v qualified function "
6995 "pointer from unqualified"),
6996 G_("initialization makes %q#v qualified "
6997 "function pointer from unqualified"),
6998 G_("return makes %q#v qualified function "
6999 "pointer from unqualified"),
7000 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7003 /* Avoid warning about the volatile ObjC EH puts on decls. */
7004 else if (!objc_ok)
7006 switch (errtype)
7008 case ic_argpass:
7010 auto_diagnostic_group d;
7011 range_label_for_type_mismatch rhs_label (rhstype, type);
7012 gcc_rich_location richloc (expr_loc, &rhs_label);
7013 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7014 "passing argument %d of %qE from incompatible "
7015 "pointer type", parmnum, rname))
7016 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7018 break;
7019 case ic_assign:
7020 pedwarn (location, OPT_Wincompatible_pointer_types,
7021 "assignment to %qT from incompatible pointer type %qT",
7022 type, rhstype);
7023 break;
7024 case ic_init:
7025 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7026 "initialization of %qT from incompatible pointer "
7027 "type %qT", type, rhstype);
7028 break;
7029 case ic_return:
7030 pedwarn (location, OPT_Wincompatible_pointer_types,
7031 "returning %qT from a function with incompatible "
7032 "return type %qT", rhstype, type);
7033 break;
7034 default:
7035 gcc_unreachable ();
7039 return convert (type, rhs);
7041 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7043 /* ??? This should not be an error when inlining calls to
7044 unprototyped functions. */
7045 error_at (location, "invalid use of non-lvalue array");
7046 return error_mark_node;
7048 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7050 /* An explicit constant 0 can convert to a pointer,
7051 or one that results from arithmetic, even including
7052 a cast to integer type. */
7053 if (!null_pointer_constant)
7054 switch (errtype)
7056 case ic_argpass:
7058 auto_diagnostic_group d;
7059 range_label_for_type_mismatch rhs_label (rhstype, type);
7060 gcc_rich_location richloc (expr_loc, &rhs_label);
7061 if (pedwarn (&richloc, OPT_Wint_conversion,
7062 "passing argument %d of %qE makes pointer from "
7063 "integer without a cast", parmnum, rname))
7064 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7066 break;
7067 case ic_assign:
7068 pedwarn (location, OPT_Wint_conversion,
7069 "assignment to %qT from %qT makes pointer from integer "
7070 "without a cast", type, rhstype);
7071 break;
7072 case ic_init:
7073 pedwarn_init (location, OPT_Wint_conversion,
7074 "initialization of %qT from %qT makes pointer from "
7075 "integer without a cast", type, rhstype);
7076 break;
7077 case ic_return:
7078 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7079 "function with return type %qT makes pointer from "
7080 "integer without a cast", rhstype, type);
7081 break;
7082 default:
7083 gcc_unreachable ();
7086 return convert (type, rhs);
7088 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7090 switch (errtype)
7092 case ic_argpass:
7094 auto_diagnostic_group d;
7095 range_label_for_type_mismatch rhs_label (rhstype, type);
7096 gcc_rich_location richloc (expr_loc, &rhs_label);
7097 if (pedwarn (&richloc, OPT_Wint_conversion,
7098 "passing argument %d of %qE makes integer from "
7099 "pointer without a cast", parmnum, rname))
7100 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7102 break;
7103 case ic_assign:
7104 pedwarn (location, OPT_Wint_conversion,
7105 "assignment to %qT from %qT makes integer from pointer "
7106 "without a cast", type, rhstype);
7107 break;
7108 case ic_init:
7109 pedwarn_init (location, OPT_Wint_conversion,
7110 "initialization of %qT from %qT makes integer from "
7111 "pointer without a cast", type, rhstype);
7112 break;
7113 case ic_return:
7114 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7115 "function with return type %qT makes integer from "
7116 "pointer without a cast", rhstype, type);
7117 break;
7118 default:
7119 gcc_unreachable ();
7122 return convert (type, rhs);
7124 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7126 tree ret;
7127 bool save = in_late_binary_op;
7128 in_late_binary_op = true;
7129 ret = convert (type, rhs);
7130 in_late_binary_op = save;
7131 return ret;
7134 switch (errtype)
7136 case ic_argpass:
7138 auto_diagnostic_group d;
7139 range_label_for_type_mismatch rhs_label (rhstype, type);
7140 gcc_rich_location richloc (expr_loc, &rhs_label);
7141 error_at (&richloc, "incompatible type for argument %d of %qE", parmnum,
7142 rname);
7143 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7145 break;
7146 case ic_assign:
7147 error_at (location, "incompatible types when assigning to type %qT from "
7148 "type %qT", type, rhstype);
7149 break;
7150 case ic_init:
7151 error_at (location,
7152 "incompatible types when initializing type %qT using type %qT",
7153 type, rhstype);
7154 break;
7155 case ic_return:
7156 error_at (location,
7157 "incompatible types when returning type %qT but %qT was "
7158 "expected", rhstype, type);
7159 break;
7160 default:
7161 gcc_unreachable ();
7164 return error_mark_node;
7167 /* If VALUE is a compound expr all of whose expressions are constant, then
7168 return its value. Otherwise, return error_mark_node.
7170 This is for handling COMPOUND_EXPRs as initializer elements
7171 which is allowed with a warning when -pedantic is specified. */
7173 static tree
7174 valid_compound_expr_initializer (tree value, tree endtype)
7176 if (TREE_CODE (value) == COMPOUND_EXPR)
7178 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7179 == error_mark_node)
7180 return error_mark_node;
7181 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7182 endtype);
7184 else if (!initializer_constant_valid_p (value, endtype))
7185 return error_mark_node;
7186 else
7187 return value;
7190 /* Perform appropriate conversions on the initial value of a variable,
7191 store it in the declaration DECL,
7192 and print any error messages that are appropriate.
7193 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7194 If the init is invalid, store an ERROR_MARK.
7196 INIT_LOC is the location of the initial value. */
7198 void
7199 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7201 tree value, type;
7202 bool npc = false;
7204 /* If variable's type was invalidly declared, just ignore it. */
7206 type = TREE_TYPE (decl);
7207 if (TREE_CODE (type) == ERROR_MARK)
7208 return;
7210 /* Digest the specified initializer into an expression. */
7212 if (init)
7213 npc = null_pointer_constant_p (init);
7214 value = digest_init (init_loc, type, init, origtype, npc,
7215 true, TREE_STATIC (decl));
7217 /* Store the expression if valid; else report error. */
7219 if (!in_system_header_at (input_location)
7220 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7221 warning (OPT_Wtraditional, "traditional C rejects automatic "
7222 "aggregate initialization");
7224 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7225 DECL_INITIAL (decl) = value;
7227 /* ANSI wants warnings about out-of-range constant initializers. */
7228 STRIP_TYPE_NOPS (value);
7229 if (TREE_STATIC (decl))
7230 constant_expression_warning (value);
7232 /* Check if we need to set array size from compound literal size. */
7233 if (TREE_CODE (type) == ARRAY_TYPE
7234 && TYPE_DOMAIN (type) == NULL_TREE
7235 && value != error_mark_node)
7237 tree inside_init = init;
7239 STRIP_TYPE_NOPS (inside_init);
7240 inside_init = fold (inside_init);
7242 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7244 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7246 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7248 /* For int foo[] = (int [3]){1}; we need to set array size
7249 now since later on array initializer will be just the
7250 brace enclosed list of the compound literal. */
7251 tree etype = strip_array_types (TREE_TYPE (decl));
7252 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7253 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7254 layout_type (type);
7255 layout_decl (cldecl, 0);
7256 TREE_TYPE (decl)
7257 = c_build_qualified_type (type, TYPE_QUALS (etype));
7263 /* Methods for storing and printing names for error messages. */
7265 /* Implement a spelling stack that allows components of a name to be pushed
7266 and popped. Each element on the stack is this structure. */
7268 struct spelling
7270 int kind;
7271 union
7273 unsigned HOST_WIDE_INT i;
7274 const char *s;
7275 } u;
7278 #define SPELLING_STRING 1
7279 #define SPELLING_MEMBER 2
7280 #define SPELLING_BOUNDS 3
7282 static struct spelling *spelling; /* Next stack element (unused). */
7283 static struct spelling *spelling_base; /* Spelling stack base. */
7284 static int spelling_size; /* Size of the spelling stack. */
7286 /* Macros to save and restore the spelling stack around push_... functions.
7287 Alternative to SAVE_SPELLING_STACK. */
7289 #define SPELLING_DEPTH() (spelling - spelling_base)
7290 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7292 /* Push an element on the spelling stack with type KIND and assign VALUE
7293 to MEMBER. */
7295 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7297 int depth = SPELLING_DEPTH (); \
7299 if (depth >= spelling_size) \
7301 spelling_size += 10; \
7302 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7303 spelling_size); \
7304 RESTORE_SPELLING_DEPTH (depth); \
7307 spelling->kind = (KIND); \
7308 spelling->MEMBER = (VALUE); \
7309 spelling++; \
7312 /* Push STRING on the stack. Printed literally. */
7314 static void
7315 push_string (const char *string)
7317 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7320 /* Push a member name on the stack. Printed as '.' STRING. */
7322 static void
7323 push_member_name (tree decl)
7325 const char *const string
7326 = (DECL_NAME (decl)
7327 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7328 : _("<anonymous>"));
7329 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7332 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7334 static void
7335 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7337 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7340 /* Compute the maximum size in bytes of the printed spelling. */
7342 static int
7343 spelling_length (void)
7345 int size = 0;
7346 struct spelling *p;
7348 for (p = spelling_base; p < spelling; p++)
7350 if (p->kind == SPELLING_BOUNDS)
7351 size += 25;
7352 else
7353 size += strlen (p->u.s) + 1;
7356 return size;
7359 /* Print the spelling to BUFFER and return it. */
7361 static char *
7362 print_spelling (char *buffer)
7364 char *d = buffer;
7365 struct spelling *p;
7367 for (p = spelling_base; p < spelling; p++)
7368 if (p->kind == SPELLING_BOUNDS)
7370 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7371 d += strlen (d);
7373 else
7375 const char *s;
7376 if (p->kind == SPELLING_MEMBER)
7377 *d++ = '.';
7378 for (s = p->u.s; (*d = *s++); d++)
7381 *d++ = '\0';
7382 return buffer;
7385 /* Digest the parser output INIT as an initializer for type TYPE.
7386 Return a C expression of type TYPE to represent the initial value.
7388 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7390 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7392 If INIT is a string constant, STRICT_STRING is true if it is
7393 unparenthesized or we should not warn here for it being parenthesized.
7394 For other types of INIT, STRICT_STRING is not used.
7396 INIT_LOC is the location of the INIT.
7398 REQUIRE_CONSTANT requests an error if non-constant initializers or
7399 elements are seen. */
7401 static tree
7402 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7403 bool null_pointer_constant, bool strict_string,
7404 int require_constant)
7406 enum tree_code code = TREE_CODE (type);
7407 tree inside_init = init;
7408 tree semantic_type = NULL_TREE;
7409 bool maybe_const = true;
7411 if (type == error_mark_node
7412 || !init
7413 || error_operand_p (init))
7414 return error_mark_node;
7416 STRIP_TYPE_NOPS (inside_init);
7418 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7420 semantic_type = TREE_TYPE (inside_init);
7421 inside_init = TREE_OPERAND (inside_init, 0);
7423 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7425 /* Initialization of an array of chars from a string constant
7426 optionally enclosed in braces. */
7428 if (code == ARRAY_TYPE && inside_init
7429 && TREE_CODE (inside_init) == STRING_CST)
7431 tree typ1
7432 = (TYPE_ATOMIC (TREE_TYPE (type))
7433 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7434 TYPE_QUAL_ATOMIC)
7435 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7436 /* Note that an array could be both an array of character type
7437 and an array of wchar_t if wchar_t is signed char or unsigned
7438 char. */
7439 bool char_array = (typ1 == char_type_node
7440 || typ1 == signed_char_type_node
7441 || typ1 == unsigned_char_type_node);
7442 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7443 bool char16_array = !!comptypes (typ1, char16_type_node);
7444 bool char32_array = !!comptypes (typ1, char32_type_node);
7446 if (char_array || wchar_array || char16_array || char32_array)
7448 struct c_expr expr;
7449 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7450 expr.value = inside_init;
7451 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7452 expr.original_type = NULL;
7453 maybe_warn_string_init (init_loc, type, expr);
7455 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7456 pedwarn_init (init_loc, OPT_Wpedantic,
7457 "initialization of a flexible array member");
7459 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7460 TYPE_MAIN_VARIANT (type)))
7461 return inside_init;
7463 if (char_array)
7465 if (typ2 != char_type_node)
7467 error_init (init_loc, "char-array initialized from wide "
7468 "string");
7469 return error_mark_node;
7472 else
7474 if (typ2 == char_type_node)
7476 error_init (init_loc, "wide character array initialized "
7477 "from non-wide string");
7478 return error_mark_node;
7480 else if (!comptypes(typ1, typ2))
7482 error_init (init_loc, "wide character array initialized "
7483 "from incompatible wide string");
7484 return error_mark_node;
7488 if (TYPE_DOMAIN (type) != NULL_TREE
7489 && TYPE_SIZE (type) != NULL_TREE
7490 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7492 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7493 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
7495 /* Subtract the size of a single (possibly wide) character
7496 because it's ok to ignore the terminating null char
7497 that is counted in the length of the constant. */
7498 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
7499 pedwarn_init (init_loc, 0,
7500 ("initializer-string for array of chars "
7501 "is too long"));
7502 else if (warn_cxx_compat
7503 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7504 warning_at (init_loc, OPT_Wc___compat,
7505 ("initializer-string for array chars "
7506 "is too long for C++"));
7507 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7509 unsigned HOST_WIDE_INT size
7510 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7511 const char *p = TREE_STRING_POINTER (inside_init);
7513 inside_init = build_string (size, p);
7517 TREE_TYPE (inside_init) = type;
7518 return inside_init;
7520 else if (INTEGRAL_TYPE_P (typ1))
7522 error_init (init_loc, "array of inappropriate type initialized "
7523 "from string constant");
7524 return error_mark_node;
7528 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7529 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7530 below and handle as a constructor. */
7531 if (code == VECTOR_TYPE
7532 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7533 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7534 && TREE_CONSTANT (inside_init))
7536 if (TREE_CODE (inside_init) == VECTOR_CST
7537 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7538 TYPE_MAIN_VARIANT (type)))
7539 return inside_init;
7541 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7543 unsigned HOST_WIDE_INT ix;
7544 tree value;
7545 bool constant_p = true;
7547 /* Iterate through elements and check if all constructor
7548 elements are *_CSTs. */
7549 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7550 if (!CONSTANT_CLASS_P (value))
7552 constant_p = false;
7553 break;
7556 if (constant_p)
7557 return build_vector_from_ctor (type,
7558 CONSTRUCTOR_ELTS (inside_init));
7562 if (warn_sequence_point)
7563 verify_sequence_points (inside_init);
7565 /* Any type can be initialized
7566 from an expression of the same type, optionally with braces. */
7568 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7569 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7570 TYPE_MAIN_VARIANT (type))
7571 || (code == ARRAY_TYPE
7572 && comptypes (TREE_TYPE (inside_init), type))
7573 || (code == VECTOR_TYPE
7574 && comptypes (TREE_TYPE (inside_init), type))
7575 || (code == POINTER_TYPE
7576 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7577 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7578 TREE_TYPE (type)))))
7580 if (code == POINTER_TYPE)
7582 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7584 if (TREE_CODE (inside_init) == STRING_CST
7585 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7586 inside_init = array_to_pointer_conversion
7587 (init_loc, inside_init);
7588 else
7590 error_init (init_loc, "invalid use of non-lvalue array");
7591 return error_mark_node;
7596 if (code == VECTOR_TYPE)
7597 /* Although the types are compatible, we may require a
7598 conversion. */
7599 inside_init = convert (type, inside_init);
7601 if (require_constant
7602 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7604 /* As an extension, allow initializing objects with static storage
7605 duration with compound literals (which are then treated just as
7606 the brace enclosed list they contain). Also allow this for
7607 vectors, as we can only assign them with compound literals. */
7608 if (flag_isoc99 && code != VECTOR_TYPE)
7609 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7610 "is not constant");
7611 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7612 inside_init = DECL_INITIAL (decl);
7615 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7616 && TREE_CODE (inside_init) != CONSTRUCTOR)
7618 error_init (init_loc, "array initialized from non-constant array "
7619 "expression");
7620 return error_mark_node;
7623 /* Compound expressions can only occur here if -Wpedantic or
7624 -pedantic-errors is specified. In the later case, we always want
7625 an error. In the former case, we simply want a warning. */
7626 if (require_constant && pedantic
7627 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7629 inside_init
7630 = valid_compound_expr_initializer (inside_init,
7631 TREE_TYPE (inside_init));
7632 if (inside_init == error_mark_node)
7633 error_init (init_loc, "initializer element is not constant");
7634 else
7635 pedwarn_init (init_loc, OPT_Wpedantic,
7636 "initializer element is not constant");
7637 if (flag_pedantic_errors)
7638 inside_init = error_mark_node;
7640 else if (require_constant
7641 && !initializer_constant_valid_p (inside_init,
7642 TREE_TYPE (inside_init)))
7644 error_init (init_loc, "initializer element is not constant");
7645 inside_init = error_mark_node;
7647 else if (require_constant && !maybe_const)
7648 pedwarn_init (init_loc, OPT_Wpedantic,
7649 "initializer element is not a constant expression");
7651 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7652 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7653 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7654 type, inside_init, origtype,
7655 ic_init, null_pointer_constant,
7656 NULL_TREE, NULL_TREE, 0);
7657 return inside_init;
7660 /* Handle scalar types, including conversions. */
7662 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7663 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7664 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7666 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7667 && (TREE_CODE (init) == STRING_CST
7668 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7669 inside_init = init = array_to_pointer_conversion (init_loc, init);
7670 if (semantic_type)
7671 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7672 inside_init);
7673 inside_init
7674 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7675 inside_init, origtype, ic_init,
7676 null_pointer_constant, NULL_TREE, NULL_TREE,
7679 /* Check to see if we have already given an error message. */
7680 if (inside_init == error_mark_node)
7682 else if (require_constant && !TREE_CONSTANT (inside_init))
7684 error_init (init_loc, "initializer element is not constant");
7685 inside_init = error_mark_node;
7687 else if (require_constant
7688 && !initializer_constant_valid_p (inside_init,
7689 TREE_TYPE (inside_init)))
7691 error_init (init_loc, "initializer element is not computable at "
7692 "load time");
7693 inside_init = error_mark_node;
7695 else if (require_constant && !maybe_const)
7696 pedwarn_init (init_loc, OPT_Wpedantic,
7697 "initializer element is not a constant expression");
7699 return inside_init;
7702 /* Come here only for records and arrays. */
7704 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7706 error_init (init_loc, "variable-sized object may not be initialized");
7707 return error_mark_node;
7710 error_init (init_loc, "invalid initializer");
7711 return error_mark_node;
7714 /* Handle initializers that use braces. */
7716 /* Type of object we are accumulating a constructor for.
7717 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7718 static tree constructor_type;
7720 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7721 left to fill. */
7722 static tree constructor_fields;
7724 /* For an ARRAY_TYPE, this is the specified index
7725 at which to store the next element we get. */
7726 static tree constructor_index;
7728 /* For an ARRAY_TYPE, this is the maximum index. */
7729 static tree constructor_max_index;
7731 /* For a RECORD_TYPE, this is the first field not yet written out. */
7732 static tree constructor_unfilled_fields;
7734 /* For an ARRAY_TYPE, this is the index of the first element
7735 not yet written out. */
7736 static tree constructor_unfilled_index;
7738 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7739 This is so we can generate gaps between fields, when appropriate. */
7740 static tree constructor_bit_index;
7742 /* If we are saving up the elements rather than allocating them,
7743 this is the list of elements so far (in reverse order,
7744 most recent first). */
7745 static vec<constructor_elt, va_gc> *constructor_elements;
7747 /* 1 if constructor should be incrementally stored into a constructor chain,
7748 0 if all the elements should be kept in AVL tree. */
7749 static int constructor_incremental;
7751 /* 1 if so far this constructor's elements are all compile-time constants. */
7752 static int constructor_constant;
7754 /* 1 if so far this constructor's elements are all valid address constants. */
7755 static int constructor_simple;
7757 /* 1 if this constructor has an element that cannot be part of a
7758 constant expression. */
7759 static int constructor_nonconst;
7761 /* 1 if this constructor is erroneous so far. */
7762 static int constructor_erroneous;
7764 /* 1 if this constructor is the universal zero initializer { 0 }. */
7765 static int constructor_zeroinit;
7767 /* Structure for managing pending initializer elements, organized as an
7768 AVL tree. */
7770 struct init_node
7772 struct init_node *left, *right;
7773 struct init_node *parent;
7774 int balance;
7775 tree purpose;
7776 tree value;
7777 tree origtype;
7780 /* Tree of pending elements at this constructor level.
7781 These are elements encountered out of order
7782 which belong at places we haven't reached yet in actually
7783 writing the output.
7784 Will never hold tree nodes across GC runs. */
7785 static struct init_node *constructor_pending_elts;
7787 /* The SPELLING_DEPTH of this constructor. */
7788 static int constructor_depth;
7790 /* DECL node for which an initializer is being read.
7791 0 means we are reading a constructor expression
7792 such as (struct foo) {...}. */
7793 static tree constructor_decl;
7795 /* Nonzero if this is an initializer for a top-level decl. */
7796 static int constructor_top_level;
7798 /* Nonzero if there were any member designators in this initializer. */
7799 static int constructor_designated;
7801 /* Nesting depth of designator list. */
7802 static int designator_depth;
7804 /* Nonzero if there were diagnosed errors in this designator list. */
7805 static int designator_erroneous;
7808 /* This stack has a level for each implicit or explicit level of
7809 structuring in the initializer, including the outermost one. It
7810 saves the values of most of the variables above. */
7812 struct constructor_range_stack;
7814 struct constructor_stack
7816 struct constructor_stack *next;
7817 tree type;
7818 tree fields;
7819 tree index;
7820 tree max_index;
7821 tree unfilled_index;
7822 tree unfilled_fields;
7823 tree bit_index;
7824 vec<constructor_elt, va_gc> *elements;
7825 struct init_node *pending_elts;
7826 int offset;
7827 int depth;
7828 /* If value nonzero, this value should replace the entire
7829 constructor at this level. */
7830 struct c_expr replacement_value;
7831 struct constructor_range_stack *range_stack;
7832 char constant;
7833 char simple;
7834 char nonconst;
7835 char implicit;
7836 char erroneous;
7837 char outer;
7838 char incremental;
7839 char designated;
7840 int designator_depth;
7843 static struct constructor_stack *constructor_stack;
7845 /* This stack represents designators from some range designator up to
7846 the last designator in the list. */
7848 struct constructor_range_stack
7850 struct constructor_range_stack *next, *prev;
7851 struct constructor_stack *stack;
7852 tree range_start;
7853 tree index;
7854 tree range_end;
7855 tree fields;
7858 static struct constructor_range_stack *constructor_range_stack;
7860 /* This stack records separate initializers that are nested.
7861 Nested initializers can't happen in ANSI C, but GNU C allows them
7862 in cases like { ... (struct foo) { ... } ... }. */
7864 struct initializer_stack
7866 struct initializer_stack *next;
7867 tree decl;
7868 struct constructor_stack *constructor_stack;
7869 struct constructor_range_stack *constructor_range_stack;
7870 vec<constructor_elt, va_gc> *elements;
7871 struct spelling *spelling;
7872 struct spelling *spelling_base;
7873 int spelling_size;
7874 char top_level;
7875 char require_constant_value;
7876 char require_constant_elements;
7877 rich_location *missing_brace_richloc;
7880 static struct initializer_stack *initializer_stack;
7882 /* Prepare to parse and output the initializer for variable DECL. */
7884 void
7885 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7886 rich_location *richloc)
7888 const char *locus;
7889 struct initializer_stack *p = XNEW (struct initializer_stack);
7891 p->decl = constructor_decl;
7892 p->require_constant_value = require_constant_value;
7893 p->require_constant_elements = require_constant_elements;
7894 p->constructor_stack = constructor_stack;
7895 p->constructor_range_stack = constructor_range_stack;
7896 p->elements = constructor_elements;
7897 p->spelling = spelling;
7898 p->spelling_base = spelling_base;
7899 p->spelling_size = spelling_size;
7900 p->top_level = constructor_top_level;
7901 p->next = initializer_stack;
7902 p->missing_brace_richloc = richloc;
7903 initializer_stack = p;
7905 constructor_decl = decl;
7906 constructor_designated = 0;
7907 constructor_top_level = top_level;
7909 if (decl != NULL_TREE && decl != error_mark_node)
7911 require_constant_value = TREE_STATIC (decl);
7912 require_constant_elements
7913 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7914 /* For a scalar, you can always use any value to initialize,
7915 even within braces. */
7916 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7917 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7919 else
7921 require_constant_value = 0;
7922 require_constant_elements = 0;
7923 locus = _("(anonymous)");
7926 constructor_stack = 0;
7927 constructor_range_stack = 0;
7929 found_missing_braces = 0;
7931 spelling_base = 0;
7932 spelling_size = 0;
7933 RESTORE_SPELLING_DEPTH (0);
7935 if (locus)
7936 push_string (locus);
7939 void
7940 finish_init (void)
7942 struct initializer_stack *p = initializer_stack;
7944 /* Free the whole constructor stack of this initializer. */
7945 while (constructor_stack)
7947 struct constructor_stack *q = constructor_stack;
7948 constructor_stack = q->next;
7949 free (q);
7952 gcc_assert (!constructor_range_stack);
7954 /* Pop back to the data of the outer initializer (if any). */
7955 free (spelling_base);
7957 constructor_decl = p->decl;
7958 require_constant_value = p->require_constant_value;
7959 require_constant_elements = p->require_constant_elements;
7960 constructor_stack = p->constructor_stack;
7961 constructor_range_stack = p->constructor_range_stack;
7962 constructor_elements = p->elements;
7963 spelling = p->spelling;
7964 spelling_base = p->spelling_base;
7965 spelling_size = p->spelling_size;
7966 constructor_top_level = p->top_level;
7967 initializer_stack = p->next;
7968 free (p);
7971 /* Call here when we see the initializer is surrounded by braces.
7972 This is instead of a call to push_init_level;
7973 it is matched by a call to pop_init_level.
7975 TYPE is the type to initialize, for a constructor expression.
7976 For an initializer for a decl, TYPE is zero. */
7978 void
7979 really_start_incremental_init (tree type)
7981 struct constructor_stack *p = XNEW (struct constructor_stack);
7983 if (type == NULL_TREE)
7984 type = TREE_TYPE (constructor_decl);
7986 if (VECTOR_TYPE_P (type)
7987 && TYPE_VECTOR_OPAQUE (type))
7988 error ("opaque vector types cannot be initialized");
7990 p->type = constructor_type;
7991 p->fields = constructor_fields;
7992 p->index = constructor_index;
7993 p->max_index = constructor_max_index;
7994 p->unfilled_index = constructor_unfilled_index;
7995 p->unfilled_fields = constructor_unfilled_fields;
7996 p->bit_index = constructor_bit_index;
7997 p->elements = constructor_elements;
7998 p->constant = constructor_constant;
7999 p->simple = constructor_simple;
8000 p->nonconst = constructor_nonconst;
8001 p->erroneous = constructor_erroneous;
8002 p->pending_elts = constructor_pending_elts;
8003 p->depth = constructor_depth;
8004 p->replacement_value.value = 0;
8005 p->replacement_value.original_code = ERROR_MARK;
8006 p->replacement_value.original_type = NULL;
8007 p->implicit = 0;
8008 p->range_stack = 0;
8009 p->outer = 0;
8010 p->incremental = constructor_incremental;
8011 p->designated = constructor_designated;
8012 p->designator_depth = designator_depth;
8013 p->next = 0;
8014 constructor_stack = p;
8016 constructor_constant = 1;
8017 constructor_simple = 1;
8018 constructor_nonconst = 0;
8019 constructor_depth = SPELLING_DEPTH ();
8020 constructor_elements = NULL;
8021 constructor_pending_elts = 0;
8022 constructor_type = type;
8023 constructor_incremental = 1;
8024 constructor_designated = 0;
8025 constructor_zeroinit = 1;
8026 designator_depth = 0;
8027 designator_erroneous = 0;
8029 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8031 constructor_fields = TYPE_FIELDS (constructor_type);
8032 /* Skip any nameless bit fields at the beginning. */
8033 while (constructor_fields != NULL_TREE
8034 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8035 constructor_fields = DECL_CHAIN (constructor_fields);
8037 constructor_unfilled_fields = constructor_fields;
8038 constructor_bit_index = bitsize_zero_node;
8040 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8042 if (TYPE_DOMAIN (constructor_type))
8044 constructor_max_index
8045 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8047 /* Detect non-empty initializations of zero-length arrays. */
8048 if (constructor_max_index == NULL_TREE
8049 && TYPE_SIZE (constructor_type))
8050 constructor_max_index = integer_minus_one_node;
8052 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8053 to initialize VLAs will cause a proper error; avoid tree
8054 checking errors as well by setting a safe value. */
8055 if (constructor_max_index
8056 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8057 constructor_max_index = integer_minus_one_node;
8059 constructor_index
8060 = convert (bitsizetype,
8061 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8063 else
8065 constructor_index = bitsize_zero_node;
8066 constructor_max_index = NULL_TREE;
8069 constructor_unfilled_index = constructor_index;
8071 else if (VECTOR_TYPE_P (constructor_type))
8073 /* Vectors are like simple fixed-size arrays. */
8074 constructor_max_index =
8075 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8076 constructor_index = bitsize_zero_node;
8077 constructor_unfilled_index = constructor_index;
8079 else
8081 /* Handle the case of int x = {5}; */
8082 constructor_fields = constructor_type;
8083 constructor_unfilled_fields = constructor_type;
8087 extern location_t last_init_list_comma;
8089 /* Called when we see an open brace for a nested initializer. Finish
8090 off any pending levels with implicit braces. */
8091 void
8092 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8094 while (constructor_stack->implicit)
8096 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8097 && constructor_fields == NULL_TREE)
8098 process_init_element (input_location,
8099 pop_init_level (loc, 1, braced_init_obstack,
8100 last_init_list_comma),
8101 true, braced_init_obstack);
8102 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8103 && constructor_max_index
8104 && tree_int_cst_lt (constructor_max_index,
8105 constructor_index))
8106 process_init_element (input_location,
8107 pop_init_level (loc, 1, braced_init_obstack,
8108 last_init_list_comma),
8109 true, braced_init_obstack);
8110 else
8111 break;
8115 /* Push down into a subobject, for initialization.
8116 If this is for an explicit set of braces, IMPLICIT is 0.
8117 If it is because the next element belongs at a lower level,
8118 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8120 void
8121 push_init_level (location_t loc, int implicit,
8122 struct obstack *braced_init_obstack)
8124 struct constructor_stack *p;
8125 tree value = NULL_TREE;
8127 /* Unless this is an explicit brace, we need to preserve previous
8128 content if any. */
8129 if (implicit)
8131 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8132 value = find_init_member (constructor_fields, braced_init_obstack);
8133 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8134 value = find_init_member (constructor_index, braced_init_obstack);
8137 p = XNEW (struct constructor_stack);
8138 p->type = constructor_type;
8139 p->fields = constructor_fields;
8140 p->index = constructor_index;
8141 p->max_index = constructor_max_index;
8142 p->unfilled_index = constructor_unfilled_index;
8143 p->unfilled_fields = constructor_unfilled_fields;
8144 p->bit_index = constructor_bit_index;
8145 p->elements = constructor_elements;
8146 p->constant = constructor_constant;
8147 p->simple = constructor_simple;
8148 p->nonconst = constructor_nonconst;
8149 p->erroneous = constructor_erroneous;
8150 p->pending_elts = constructor_pending_elts;
8151 p->depth = constructor_depth;
8152 p->replacement_value.value = NULL_TREE;
8153 p->replacement_value.original_code = ERROR_MARK;
8154 p->replacement_value.original_type = NULL;
8155 p->implicit = implicit;
8156 p->outer = 0;
8157 p->incremental = constructor_incremental;
8158 p->designated = constructor_designated;
8159 p->designator_depth = designator_depth;
8160 p->next = constructor_stack;
8161 p->range_stack = 0;
8162 constructor_stack = p;
8164 constructor_constant = 1;
8165 constructor_simple = 1;
8166 constructor_nonconst = 0;
8167 constructor_depth = SPELLING_DEPTH ();
8168 constructor_elements = NULL;
8169 constructor_incremental = 1;
8170 constructor_designated = 0;
8171 constructor_pending_elts = 0;
8172 if (!implicit)
8174 p->range_stack = constructor_range_stack;
8175 constructor_range_stack = 0;
8176 designator_depth = 0;
8177 designator_erroneous = 0;
8180 /* Don't die if an entire brace-pair level is superfluous
8181 in the containing level. */
8182 if (constructor_type == NULL_TREE)
8184 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8186 /* Don't die if there are extra init elts at the end. */
8187 if (constructor_fields == NULL_TREE)
8188 constructor_type = NULL_TREE;
8189 else
8191 constructor_type = TREE_TYPE (constructor_fields);
8192 push_member_name (constructor_fields);
8193 constructor_depth++;
8195 /* If upper initializer is designated, then mark this as
8196 designated too to prevent bogus warnings. */
8197 constructor_designated = p->designated;
8199 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8201 constructor_type = TREE_TYPE (constructor_type);
8202 push_array_bounds (tree_to_uhwi (constructor_index));
8203 constructor_depth++;
8206 if (constructor_type == NULL_TREE)
8208 error_init (loc, "extra brace group at end of initializer");
8209 constructor_fields = NULL_TREE;
8210 constructor_unfilled_fields = NULL_TREE;
8211 return;
8214 if (value && TREE_CODE (value) == CONSTRUCTOR)
8216 constructor_constant = TREE_CONSTANT (value);
8217 constructor_simple = TREE_STATIC (value);
8218 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8219 constructor_elements = CONSTRUCTOR_ELTS (value);
8220 if (!vec_safe_is_empty (constructor_elements)
8221 && (TREE_CODE (constructor_type) == RECORD_TYPE
8222 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8223 set_nonincremental_init (braced_init_obstack);
8226 if (implicit == 1)
8228 found_missing_braces = 1;
8229 if (initializer_stack->missing_brace_richloc)
8230 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8231 (loc, "{");
8234 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8236 constructor_fields = TYPE_FIELDS (constructor_type);
8237 /* Skip any nameless bit fields at the beginning. */
8238 while (constructor_fields != NULL_TREE
8239 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8240 constructor_fields = DECL_CHAIN (constructor_fields);
8242 constructor_unfilled_fields = constructor_fields;
8243 constructor_bit_index = bitsize_zero_node;
8245 else if (VECTOR_TYPE_P (constructor_type))
8247 /* Vectors are like simple fixed-size arrays. */
8248 constructor_max_index =
8249 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8250 constructor_index = bitsize_int (0);
8251 constructor_unfilled_index = constructor_index;
8253 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8255 if (TYPE_DOMAIN (constructor_type))
8257 constructor_max_index
8258 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8260 /* Detect non-empty initializations of zero-length arrays. */
8261 if (constructor_max_index == NULL_TREE
8262 && TYPE_SIZE (constructor_type))
8263 constructor_max_index = integer_minus_one_node;
8265 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8266 to initialize VLAs will cause a proper error; avoid tree
8267 checking errors as well by setting a safe value. */
8268 if (constructor_max_index
8269 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8270 constructor_max_index = integer_minus_one_node;
8272 constructor_index
8273 = convert (bitsizetype,
8274 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8276 else
8277 constructor_index = bitsize_zero_node;
8279 constructor_unfilled_index = constructor_index;
8280 if (value && TREE_CODE (value) == STRING_CST)
8282 /* We need to split the char/wchar array into individual
8283 characters, so that we don't have to special case it
8284 everywhere. */
8285 set_nonincremental_init_from_string (value, braced_init_obstack);
8288 else
8290 if (constructor_type != error_mark_node)
8291 warning_init (input_location, 0, "braces around scalar initializer");
8292 constructor_fields = constructor_type;
8293 constructor_unfilled_fields = constructor_type;
8297 /* At the end of an implicit or explicit brace level,
8298 finish up that level of constructor. If a single expression
8299 with redundant braces initialized that level, return the
8300 c_expr structure for that expression. Otherwise, the original_code
8301 element is set to ERROR_MARK.
8302 If we were outputting the elements as they are read, return 0 as the value
8303 from inner levels (process_init_element ignores that),
8304 but return error_mark_node as the value from the outermost level
8305 (that's what we want to put in DECL_INITIAL).
8306 Otherwise, return a CONSTRUCTOR expression as the value. */
8308 struct c_expr
8309 pop_init_level (location_t loc, int implicit,
8310 struct obstack *braced_init_obstack,
8311 location_t insert_before)
8313 struct constructor_stack *p;
8314 struct c_expr ret;
8315 ret.value = NULL_TREE;
8316 ret.original_code = ERROR_MARK;
8317 ret.original_type = NULL;
8319 if (implicit == 0)
8321 /* When we come to an explicit close brace,
8322 pop any inner levels that didn't have explicit braces. */
8323 while (constructor_stack->implicit)
8324 process_init_element (input_location,
8325 pop_init_level (loc, 1, braced_init_obstack,
8326 insert_before),
8327 true, braced_init_obstack);
8328 gcc_assert (!constructor_range_stack);
8330 else
8331 if (initializer_stack->missing_brace_richloc)
8332 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8333 (insert_before, "}");
8335 /* Now output all pending elements. */
8336 constructor_incremental = 1;
8337 output_pending_init_elements (1, braced_init_obstack);
8339 p = constructor_stack;
8341 /* Error for initializing a flexible array member, or a zero-length
8342 array member in an inappropriate context. */
8343 if (constructor_type && constructor_fields
8344 && TREE_CODE (constructor_type) == ARRAY_TYPE
8345 && TYPE_DOMAIN (constructor_type)
8346 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8348 /* Silently discard empty initializations. The parser will
8349 already have pedwarned for empty brackets. */
8350 if (integer_zerop (constructor_unfilled_index))
8351 constructor_type = NULL_TREE;
8352 else
8354 gcc_assert (!TYPE_SIZE (constructor_type));
8356 if (constructor_depth > 2)
8357 error_init (loc, "initialization of flexible array member in a nested context");
8358 else
8359 pedwarn_init (loc, OPT_Wpedantic,
8360 "initialization of a flexible array member");
8362 /* We have already issued an error message for the existence
8363 of a flexible array member not at the end of the structure.
8364 Discard the initializer so that we do not die later. */
8365 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8366 constructor_type = NULL_TREE;
8370 switch (vec_safe_length (constructor_elements))
8372 case 0:
8373 /* Initialization with { } counts as zeroinit. */
8374 constructor_zeroinit = 1;
8375 break;
8376 case 1:
8377 /* This might be zeroinit as well. */
8378 if (integer_zerop ((*constructor_elements)[0].value))
8379 constructor_zeroinit = 1;
8380 break;
8381 default:
8382 /* If the constructor has more than one element, it can't be { 0 }. */
8383 constructor_zeroinit = 0;
8384 break;
8387 /* Warn when some structs are initialized with direct aggregation. */
8388 if (!implicit && found_missing_braces && warn_missing_braces
8389 && !constructor_zeroinit)
8391 gcc_assert (initializer_stack->missing_brace_richloc);
8392 warning_at (initializer_stack->missing_brace_richloc,
8393 OPT_Wmissing_braces,
8394 "missing braces around initializer");
8397 /* Warn when some struct elements are implicitly initialized to zero. */
8398 if (warn_missing_field_initializers
8399 && constructor_type
8400 && TREE_CODE (constructor_type) == RECORD_TYPE
8401 && constructor_unfilled_fields)
8403 /* Do not warn for flexible array members or zero-length arrays. */
8404 while (constructor_unfilled_fields
8405 && (!DECL_SIZE (constructor_unfilled_fields)
8406 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8407 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8409 if (constructor_unfilled_fields
8410 /* Do not warn if this level of the initializer uses member
8411 designators; it is likely to be deliberate. */
8412 && !constructor_designated
8413 /* Do not warn about initializing with { 0 } or with { }. */
8414 && !constructor_zeroinit)
8416 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8417 "missing initializer for field %qD of %qT",
8418 constructor_unfilled_fields,
8419 constructor_type))
8420 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8421 "%qD declared here", constructor_unfilled_fields);
8425 /* Pad out the end of the structure. */
8426 if (p->replacement_value.value)
8427 /* If this closes a superfluous brace pair,
8428 just pass out the element between them. */
8429 ret = p->replacement_value;
8430 else if (constructor_type == NULL_TREE)
8432 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8433 && TREE_CODE (constructor_type) != ARRAY_TYPE
8434 && !VECTOR_TYPE_P (constructor_type))
8436 /* A nonincremental scalar initializer--just return
8437 the element, after verifying there is just one. */
8438 if (vec_safe_is_empty (constructor_elements))
8440 if (!constructor_erroneous)
8441 error_init (loc, "empty scalar initializer");
8442 ret.value = error_mark_node;
8444 else if (vec_safe_length (constructor_elements) != 1)
8446 error_init (loc, "extra elements in scalar initializer");
8447 ret.value = (*constructor_elements)[0].value;
8449 else
8450 ret.value = (*constructor_elements)[0].value;
8452 else
8454 if (constructor_erroneous)
8455 ret.value = error_mark_node;
8456 else
8458 ret.value = build_constructor (constructor_type,
8459 constructor_elements);
8460 if (constructor_constant)
8461 TREE_CONSTANT (ret.value) = 1;
8462 if (constructor_constant && constructor_simple)
8463 TREE_STATIC (ret.value) = 1;
8464 if (constructor_nonconst)
8465 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8469 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8471 if (constructor_nonconst)
8472 ret.original_code = C_MAYBE_CONST_EXPR;
8473 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8474 ret.original_code = ERROR_MARK;
8477 constructor_type = p->type;
8478 constructor_fields = p->fields;
8479 constructor_index = p->index;
8480 constructor_max_index = p->max_index;
8481 constructor_unfilled_index = p->unfilled_index;
8482 constructor_unfilled_fields = p->unfilled_fields;
8483 constructor_bit_index = p->bit_index;
8484 constructor_elements = p->elements;
8485 constructor_constant = p->constant;
8486 constructor_simple = p->simple;
8487 constructor_nonconst = p->nonconst;
8488 constructor_erroneous = p->erroneous;
8489 constructor_incremental = p->incremental;
8490 constructor_designated = p->designated;
8491 designator_depth = p->designator_depth;
8492 constructor_pending_elts = p->pending_elts;
8493 constructor_depth = p->depth;
8494 if (!p->implicit)
8495 constructor_range_stack = p->range_stack;
8496 RESTORE_SPELLING_DEPTH (constructor_depth);
8498 constructor_stack = p->next;
8499 free (p);
8501 if (ret.value == NULL_TREE && constructor_stack == 0)
8502 ret.value = error_mark_node;
8503 return ret;
8506 /* Common handling for both array range and field name designators.
8507 ARRAY argument is nonzero for array ranges. Returns false for success. */
8509 static bool
8510 set_designator (location_t loc, bool array,
8511 struct obstack *braced_init_obstack)
8513 tree subtype;
8514 enum tree_code subcode;
8516 /* Don't die if an entire brace-pair level is superfluous
8517 in the containing level. */
8518 if (constructor_type == NULL_TREE)
8519 return true;
8521 /* If there were errors in this designator list already, bail out
8522 silently. */
8523 if (designator_erroneous)
8524 return true;
8526 if (!designator_depth)
8528 gcc_assert (!constructor_range_stack);
8530 /* Designator list starts at the level of closest explicit
8531 braces. */
8532 while (constructor_stack->implicit)
8533 process_init_element (input_location,
8534 pop_init_level (loc, 1, braced_init_obstack,
8535 last_init_list_comma),
8536 true, braced_init_obstack);
8537 constructor_designated = 1;
8538 return false;
8541 switch (TREE_CODE (constructor_type))
8543 case RECORD_TYPE:
8544 case UNION_TYPE:
8545 subtype = TREE_TYPE (constructor_fields);
8546 if (subtype != error_mark_node)
8547 subtype = TYPE_MAIN_VARIANT (subtype);
8548 break;
8549 case ARRAY_TYPE:
8550 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8551 break;
8552 default:
8553 gcc_unreachable ();
8556 subcode = TREE_CODE (subtype);
8557 if (array && subcode != ARRAY_TYPE)
8559 error_init (loc, "array index in non-array initializer");
8560 return true;
8562 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8564 error_init (loc, "field name not in record or union initializer");
8565 return true;
8568 constructor_designated = 1;
8569 finish_implicit_inits (loc, braced_init_obstack);
8570 push_init_level (loc, 2, braced_init_obstack);
8571 return false;
8574 /* If there are range designators in designator list, push a new designator
8575 to constructor_range_stack. RANGE_END is end of such stack range or
8576 NULL_TREE if there is no range designator at this level. */
8578 static void
8579 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8581 struct constructor_range_stack *p;
8583 p = (struct constructor_range_stack *)
8584 obstack_alloc (braced_init_obstack,
8585 sizeof (struct constructor_range_stack));
8586 p->prev = constructor_range_stack;
8587 p->next = 0;
8588 p->fields = constructor_fields;
8589 p->range_start = constructor_index;
8590 p->index = constructor_index;
8591 p->stack = constructor_stack;
8592 p->range_end = range_end;
8593 if (constructor_range_stack)
8594 constructor_range_stack->next = p;
8595 constructor_range_stack = p;
8598 /* Within an array initializer, specify the next index to be initialized.
8599 FIRST is that index. If LAST is nonzero, then initialize a range
8600 of indices, running from FIRST through LAST. */
8602 void
8603 set_init_index (location_t loc, tree first, tree last,
8604 struct obstack *braced_init_obstack)
8606 if (set_designator (loc, true, braced_init_obstack))
8607 return;
8609 designator_erroneous = 1;
8611 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8612 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8614 error_init (loc, "array index in initializer not of integer type");
8615 return;
8618 if (TREE_CODE (first) != INTEGER_CST)
8620 first = c_fully_fold (first, false, NULL);
8621 if (TREE_CODE (first) == INTEGER_CST)
8622 pedwarn_init (loc, OPT_Wpedantic,
8623 "array index in initializer is not "
8624 "an integer constant expression");
8627 if (last && TREE_CODE (last) != INTEGER_CST)
8629 last = c_fully_fold (last, false, NULL);
8630 if (TREE_CODE (last) == INTEGER_CST)
8631 pedwarn_init (loc, OPT_Wpedantic,
8632 "array index in initializer is not "
8633 "an integer constant expression");
8636 if (TREE_CODE (first) != INTEGER_CST)
8637 error_init (loc, "nonconstant array index in initializer");
8638 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8639 error_init (loc, "nonconstant array index in initializer");
8640 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8641 error_init (loc, "array index in non-array initializer");
8642 else if (tree_int_cst_sgn (first) == -1)
8643 error_init (loc, "array index in initializer exceeds array bounds");
8644 else if (constructor_max_index
8645 && tree_int_cst_lt (constructor_max_index, first))
8646 error_init (loc, "array index in initializer exceeds array bounds");
8647 else
8649 constant_expression_warning (first);
8650 if (last)
8651 constant_expression_warning (last);
8652 constructor_index = convert (bitsizetype, first);
8653 if (tree_int_cst_lt (constructor_index, first))
8655 constructor_index = copy_node (constructor_index);
8656 TREE_OVERFLOW (constructor_index) = 1;
8659 if (last)
8661 if (tree_int_cst_equal (first, last))
8662 last = NULL_TREE;
8663 else if (tree_int_cst_lt (last, first))
8665 error_init (loc, "empty index range in initializer");
8666 last = NULL_TREE;
8668 else
8670 last = convert (bitsizetype, last);
8671 if (constructor_max_index != NULL_TREE
8672 && tree_int_cst_lt (constructor_max_index, last))
8674 error_init (loc, "array index range in initializer exceeds "
8675 "array bounds");
8676 last = NULL_TREE;
8681 designator_depth++;
8682 designator_erroneous = 0;
8683 if (constructor_range_stack || last)
8684 push_range_stack (last, braced_init_obstack);
8688 /* Within a struct initializer, specify the next field to be initialized. */
8690 void
8691 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8692 struct obstack *braced_init_obstack)
8694 tree field;
8696 if (set_designator (loc, false, braced_init_obstack))
8697 return;
8699 designator_erroneous = 1;
8701 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8703 error_init (loc, "field name not in record or union initializer");
8704 return;
8707 field = lookup_field (constructor_type, fieldname);
8709 if (field == NULL_TREE)
8711 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8712 if (guessed_id)
8714 gcc_rich_location rich_loc (fieldname_loc);
8715 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8716 error_at (&rich_loc,
8717 "%qT has no member named %qE; did you mean %qE?",
8718 constructor_type, fieldname, guessed_id);
8720 else
8721 error_at (fieldname_loc, "%qT has no member named %qE",
8722 constructor_type, fieldname);
8724 else
8727 constructor_fields = TREE_VALUE (field);
8728 designator_depth++;
8729 designator_erroneous = 0;
8730 if (constructor_range_stack)
8731 push_range_stack (NULL_TREE, braced_init_obstack);
8732 field = TREE_CHAIN (field);
8733 if (field)
8735 if (set_designator (loc, false, braced_init_obstack))
8736 return;
8739 while (field != NULL_TREE);
8742 /* Add a new initializer to the tree of pending initializers. PURPOSE
8743 identifies the initializer, either array index or field in a structure.
8744 VALUE is the value of that index or field. If ORIGTYPE is not
8745 NULL_TREE, it is the original type of VALUE.
8747 IMPLICIT is true if value comes from pop_init_level (1),
8748 the new initializer has been merged with the existing one
8749 and thus no warnings should be emitted about overriding an
8750 existing initializer. */
8752 static void
8753 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8754 bool implicit, struct obstack *braced_init_obstack)
8756 struct init_node *p, **q, *r;
8758 q = &constructor_pending_elts;
8759 p = 0;
8761 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8763 while (*q != 0)
8765 p = *q;
8766 if (tree_int_cst_lt (purpose, p->purpose))
8767 q = &p->left;
8768 else if (tree_int_cst_lt (p->purpose, purpose))
8769 q = &p->right;
8770 else
8772 if (!implicit)
8774 if (TREE_SIDE_EFFECTS (p->value))
8775 warning_init (loc, OPT_Woverride_init_side_effects,
8776 "initialized field with side-effects "
8777 "overwritten");
8778 else if (warn_override_init)
8779 warning_init (loc, OPT_Woverride_init,
8780 "initialized field overwritten");
8782 p->value = value;
8783 p->origtype = origtype;
8784 return;
8788 else
8790 tree bitpos;
8792 bitpos = bit_position (purpose);
8793 while (*q != NULL)
8795 p = *q;
8796 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8797 q = &p->left;
8798 else if (p->purpose != purpose)
8799 q = &p->right;
8800 else
8802 if (!implicit)
8804 if (TREE_SIDE_EFFECTS (p->value))
8805 warning_init (loc, OPT_Woverride_init_side_effects,
8806 "initialized field with side-effects "
8807 "overwritten");
8808 else if (warn_override_init)
8809 warning_init (loc, OPT_Woverride_init,
8810 "initialized field overwritten");
8812 p->value = value;
8813 p->origtype = origtype;
8814 return;
8819 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8820 sizeof (struct init_node));
8821 r->purpose = purpose;
8822 r->value = value;
8823 r->origtype = origtype;
8825 *q = r;
8826 r->parent = p;
8827 r->left = 0;
8828 r->right = 0;
8829 r->balance = 0;
8831 while (p)
8833 struct init_node *s;
8835 if (r == p->left)
8837 if (p->balance == 0)
8838 p->balance = -1;
8839 else if (p->balance < 0)
8841 if (r->balance < 0)
8843 /* L rotation. */
8844 p->left = r->right;
8845 if (p->left)
8846 p->left->parent = p;
8847 r->right = p;
8849 p->balance = 0;
8850 r->balance = 0;
8852 s = p->parent;
8853 p->parent = r;
8854 r->parent = s;
8855 if (s)
8857 if (s->left == p)
8858 s->left = r;
8859 else
8860 s->right = r;
8862 else
8863 constructor_pending_elts = r;
8865 else
8867 /* LR rotation. */
8868 struct init_node *t = r->right;
8870 r->right = t->left;
8871 if (r->right)
8872 r->right->parent = r;
8873 t->left = r;
8875 p->left = t->right;
8876 if (p->left)
8877 p->left->parent = p;
8878 t->right = p;
8880 p->balance = t->balance < 0;
8881 r->balance = -(t->balance > 0);
8882 t->balance = 0;
8884 s = p->parent;
8885 p->parent = t;
8886 r->parent = t;
8887 t->parent = s;
8888 if (s)
8890 if (s->left == p)
8891 s->left = t;
8892 else
8893 s->right = t;
8895 else
8896 constructor_pending_elts = t;
8898 break;
8900 else
8902 /* p->balance == +1; growth of left side balances the node. */
8903 p->balance = 0;
8904 break;
8907 else /* r == p->right */
8909 if (p->balance == 0)
8910 /* Growth propagation from right side. */
8911 p->balance++;
8912 else if (p->balance > 0)
8914 if (r->balance > 0)
8916 /* R rotation. */
8917 p->right = r->left;
8918 if (p->right)
8919 p->right->parent = p;
8920 r->left = p;
8922 p->balance = 0;
8923 r->balance = 0;
8925 s = p->parent;
8926 p->parent = r;
8927 r->parent = s;
8928 if (s)
8930 if (s->left == p)
8931 s->left = r;
8932 else
8933 s->right = r;
8935 else
8936 constructor_pending_elts = r;
8938 else /* r->balance == -1 */
8940 /* RL rotation */
8941 struct init_node *t = r->left;
8943 r->left = t->right;
8944 if (r->left)
8945 r->left->parent = r;
8946 t->right = r;
8948 p->right = t->left;
8949 if (p->right)
8950 p->right->parent = p;
8951 t->left = p;
8953 r->balance = (t->balance < 0);
8954 p->balance = -(t->balance > 0);
8955 t->balance = 0;
8957 s = p->parent;
8958 p->parent = t;
8959 r->parent = t;
8960 t->parent = s;
8961 if (s)
8963 if (s->left == p)
8964 s->left = t;
8965 else
8966 s->right = t;
8968 else
8969 constructor_pending_elts = t;
8971 break;
8973 else
8975 /* p->balance == -1; growth of right side balances the node. */
8976 p->balance = 0;
8977 break;
8981 r = p;
8982 p = p->parent;
8986 /* Build AVL tree from a sorted chain. */
8988 static void
8989 set_nonincremental_init (struct obstack * braced_init_obstack)
8991 unsigned HOST_WIDE_INT ix;
8992 tree index, value;
8994 if (TREE_CODE (constructor_type) != RECORD_TYPE
8995 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8996 return;
8998 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8999 add_pending_init (input_location, index, value, NULL_TREE, true,
9000 braced_init_obstack);
9001 constructor_elements = NULL;
9002 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9004 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9005 /* Skip any nameless bit fields at the beginning. */
9006 while (constructor_unfilled_fields != NULL_TREE
9007 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9008 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9011 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9013 if (TYPE_DOMAIN (constructor_type))
9014 constructor_unfilled_index
9015 = convert (bitsizetype,
9016 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9017 else
9018 constructor_unfilled_index = bitsize_zero_node;
9020 constructor_incremental = 0;
9023 /* Build AVL tree from a string constant. */
9025 static void
9026 set_nonincremental_init_from_string (tree str,
9027 struct obstack * braced_init_obstack)
9029 tree value, purpose, type;
9030 HOST_WIDE_INT val[2];
9031 const char *p, *end;
9032 int byte, wchar_bytes, charwidth, bitpos;
9034 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9036 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9037 charwidth = TYPE_PRECISION (char_type_node);
9038 gcc_assert ((size_t) wchar_bytes * charwidth
9039 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9040 type = TREE_TYPE (constructor_type);
9041 p = TREE_STRING_POINTER (str);
9042 end = p + TREE_STRING_LENGTH (str);
9044 for (purpose = bitsize_zero_node;
9045 p < end
9046 && !(constructor_max_index
9047 && tree_int_cst_lt (constructor_max_index, purpose));
9048 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9050 if (wchar_bytes == 1)
9052 val[0] = (unsigned char) *p++;
9053 val[1] = 0;
9055 else
9057 val[1] = 0;
9058 val[0] = 0;
9059 for (byte = 0; byte < wchar_bytes; byte++)
9061 if (BYTES_BIG_ENDIAN)
9062 bitpos = (wchar_bytes - byte - 1) * charwidth;
9063 else
9064 bitpos = byte * charwidth;
9065 val[bitpos / HOST_BITS_PER_WIDE_INT]
9066 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9067 << (bitpos % HOST_BITS_PER_WIDE_INT);
9071 if (!TYPE_UNSIGNED (type))
9073 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9074 if (bitpos < HOST_BITS_PER_WIDE_INT)
9076 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9078 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9079 val[1] = -1;
9082 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9084 if (val[0] < 0)
9085 val[1] = -1;
9087 else if (val[1] & (HOST_WIDE_INT_1
9088 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9089 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9092 value = wide_int_to_tree (type,
9093 wide_int::from_array (val, 2,
9094 HOST_BITS_PER_WIDE_INT * 2));
9095 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9096 braced_init_obstack);
9099 constructor_incremental = 0;
9102 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9103 not initialized yet. */
9105 static tree
9106 find_init_member (tree field, struct obstack * braced_init_obstack)
9108 struct init_node *p;
9110 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9112 if (constructor_incremental
9113 && tree_int_cst_lt (field, constructor_unfilled_index))
9114 set_nonincremental_init (braced_init_obstack);
9116 p = constructor_pending_elts;
9117 while (p)
9119 if (tree_int_cst_lt (field, p->purpose))
9120 p = p->left;
9121 else if (tree_int_cst_lt (p->purpose, field))
9122 p = p->right;
9123 else
9124 return p->value;
9127 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9129 tree bitpos = bit_position (field);
9131 if (constructor_incremental
9132 && (!constructor_unfilled_fields
9133 || tree_int_cst_lt (bitpos,
9134 bit_position (constructor_unfilled_fields))))
9135 set_nonincremental_init (braced_init_obstack);
9137 p = constructor_pending_elts;
9138 while (p)
9140 if (field == p->purpose)
9141 return p->value;
9142 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9143 p = p->left;
9144 else
9145 p = p->right;
9148 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9150 if (!vec_safe_is_empty (constructor_elements)
9151 && (constructor_elements->last ().index == field))
9152 return constructor_elements->last ().value;
9154 return NULL_TREE;
9157 /* "Output" the next constructor element.
9158 At top level, really output it to assembler code now.
9159 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9160 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9161 TYPE is the data type that the containing data type wants here.
9162 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9163 If VALUE is a string constant, STRICT_STRING is true if it is
9164 unparenthesized or we should not warn here for it being parenthesized.
9165 For other types of VALUE, STRICT_STRING is not used.
9167 PENDING if true means output pending elements that belong
9168 right after this element. (PENDING is normally true;
9169 it is false while outputting pending elements, to avoid recursion.)
9171 IMPLICIT is true if value comes from pop_init_level (1),
9172 the new initializer has been merged with the existing one
9173 and thus no warnings should be emitted about overriding an
9174 existing initializer. */
9176 static void
9177 output_init_element (location_t loc, tree value, tree origtype,
9178 bool strict_string, tree type, tree field, bool pending,
9179 bool implicit, struct obstack * braced_init_obstack)
9181 tree semantic_type = NULL_TREE;
9182 bool maybe_const = true;
9183 bool npc;
9185 if (type == error_mark_node || value == error_mark_node)
9187 constructor_erroneous = 1;
9188 return;
9190 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9191 && (TREE_CODE (value) == STRING_CST
9192 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9193 && !(TREE_CODE (value) == STRING_CST
9194 && TREE_CODE (type) == ARRAY_TYPE
9195 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9196 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9197 TYPE_MAIN_VARIANT (type)))
9198 value = array_to_pointer_conversion (input_location, value);
9200 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9201 && require_constant_value && pending)
9203 /* As an extension, allow initializing objects with static storage
9204 duration with compound literals (which are then treated just as
9205 the brace enclosed list they contain). */
9206 if (flag_isoc99)
9207 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9208 "constant");
9209 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9210 value = DECL_INITIAL (decl);
9213 npc = null_pointer_constant_p (value);
9214 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9216 semantic_type = TREE_TYPE (value);
9217 value = TREE_OPERAND (value, 0);
9219 value = c_fully_fold (value, require_constant_value, &maybe_const);
9221 if (value == error_mark_node)
9222 constructor_erroneous = 1;
9223 else if (!TREE_CONSTANT (value))
9224 constructor_constant = 0;
9225 else if (!initializer_constant_valid_p (value,
9226 TREE_TYPE (value),
9227 AGGREGATE_TYPE_P (constructor_type)
9228 && TYPE_REVERSE_STORAGE_ORDER
9229 (constructor_type))
9230 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9231 && DECL_C_BIT_FIELD (field)
9232 && TREE_CODE (value) != INTEGER_CST))
9233 constructor_simple = 0;
9234 if (!maybe_const)
9235 constructor_nonconst = 1;
9237 /* Digest the initializer and issue any errors about incompatible
9238 types before issuing errors about non-constant initializers. */
9239 tree new_value = value;
9240 if (semantic_type)
9241 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9242 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9243 require_constant_value);
9244 if (new_value == error_mark_node)
9246 constructor_erroneous = 1;
9247 return;
9249 if (require_constant_value || require_constant_elements)
9250 constant_expression_warning (new_value);
9252 /* Proceed to check the constness of the original initializer. */
9253 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9255 if (require_constant_value)
9257 error_init (loc, "initializer element is not constant");
9258 value = error_mark_node;
9260 else if (require_constant_elements)
9261 pedwarn (loc, OPT_Wpedantic,
9262 "initializer element is not computable at load time");
9264 else if (!maybe_const
9265 && (require_constant_value || require_constant_elements))
9266 pedwarn_init (loc, OPT_Wpedantic,
9267 "initializer element is not a constant expression");
9269 /* Issue -Wc++-compat warnings about initializing a bitfield with
9270 enum type. */
9271 if (warn_cxx_compat
9272 && field != NULL_TREE
9273 && TREE_CODE (field) == FIELD_DECL
9274 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9275 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9276 != TYPE_MAIN_VARIANT (type))
9277 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9279 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9280 if (checktype != error_mark_node
9281 && (TYPE_MAIN_VARIANT (checktype)
9282 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9283 warning_init (loc, OPT_Wc___compat,
9284 "enum conversion in initialization is invalid in C++");
9287 /* If this field is empty and does not have side effects (and is not at
9288 the end of structure), don't do anything other than checking the
9289 initializer. */
9290 if (field
9291 && (TREE_TYPE (field) == error_mark_node
9292 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9293 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9294 && !TREE_SIDE_EFFECTS (new_value)
9295 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9296 || DECL_CHAIN (field)))))
9297 return;
9299 /* Finally, set VALUE to the initializer value digested above. */
9300 value = new_value;
9302 /* If this element doesn't come next in sequence,
9303 put it on constructor_pending_elts. */
9304 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9305 && (!constructor_incremental
9306 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9308 if (constructor_incremental
9309 && tree_int_cst_lt (field, constructor_unfilled_index))
9310 set_nonincremental_init (braced_init_obstack);
9312 add_pending_init (loc, field, value, origtype, implicit,
9313 braced_init_obstack);
9314 return;
9316 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9317 && (!constructor_incremental
9318 || field != constructor_unfilled_fields))
9320 /* We do this for records but not for unions. In a union,
9321 no matter which field is specified, it can be initialized
9322 right away since it starts at the beginning of the union. */
9323 if (constructor_incremental)
9325 if (!constructor_unfilled_fields)
9326 set_nonincremental_init (braced_init_obstack);
9327 else
9329 tree bitpos, unfillpos;
9331 bitpos = bit_position (field);
9332 unfillpos = bit_position (constructor_unfilled_fields);
9334 if (tree_int_cst_lt (bitpos, unfillpos))
9335 set_nonincremental_init (braced_init_obstack);
9339 add_pending_init (loc, field, value, origtype, implicit,
9340 braced_init_obstack);
9341 return;
9343 else if (TREE_CODE (constructor_type) == UNION_TYPE
9344 && !vec_safe_is_empty (constructor_elements))
9346 if (!implicit)
9348 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9349 warning_init (loc, OPT_Woverride_init_side_effects,
9350 "initialized field with side-effects overwritten");
9351 else if (warn_override_init)
9352 warning_init (loc, OPT_Woverride_init,
9353 "initialized field overwritten");
9356 /* We can have just one union field set. */
9357 constructor_elements = NULL;
9360 /* Otherwise, output this element either to
9361 constructor_elements or to the assembler file. */
9363 constructor_elt celt = {field, value};
9364 vec_safe_push (constructor_elements, celt);
9366 /* Advance the variable that indicates sequential elements output. */
9367 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9368 constructor_unfilled_index
9369 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9370 bitsize_one_node);
9371 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9373 constructor_unfilled_fields
9374 = DECL_CHAIN (constructor_unfilled_fields);
9376 /* Skip any nameless bit fields. */
9377 while (constructor_unfilled_fields != NULL_TREE
9378 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9379 constructor_unfilled_fields =
9380 DECL_CHAIN (constructor_unfilled_fields);
9382 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9383 constructor_unfilled_fields = NULL_TREE;
9385 /* Now output any pending elements which have become next. */
9386 if (pending)
9387 output_pending_init_elements (0, braced_init_obstack);
9390 /* For two FIELD_DECLs in the same chain, return -1 if field1
9391 comes before field2, 1 if field1 comes after field2 and
9392 0 if field1 == field2. */
9394 static int
9395 init_field_decl_cmp (tree field1, tree field2)
9397 if (field1 == field2)
9398 return 0;
9400 tree bitpos1 = bit_position (field1);
9401 tree bitpos2 = bit_position (field2);
9402 if (tree_int_cst_equal (bitpos1, bitpos2))
9404 /* If one of the fields has non-zero bitsize, then that
9405 field must be the last one in a sequence of zero
9406 sized fields, fields after it will have bigger
9407 bit_position. */
9408 if (TREE_TYPE (field1) != error_mark_node
9409 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9410 && integer_nonzerop (TREE_TYPE (field1)))
9411 return 1;
9412 if (TREE_TYPE (field2) != error_mark_node
9413 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9414 && integer_nonzerop (TREE_TYPE (field2)))
9415 return -1;
9416 /* Otherwise, fallback to DECL_CHAIN walk to find out
9417 which field comes earlier. Walk chains of both
9418 fields, so that if field1 and field2 are close to each
9419 other in either order, it is found soon even for large
9420 sequences of zero sized fields. */
9421 tree f1 = field1, f2 = field2;
9422 while (1)
9424 f1 = DECL_CHAIN (f1);
9425 f2 = DECL_CHAIN (f2);
9426 if (f1 == NULL_TREE)
9428 gcc_assert (f2);
9429 return 1;
9431 if (f2 == NULL_TREE)
9432 return -1;
9433 if (f1 == field2)
9434 return -1;
9435 if (f2 == field1)
9436 return 1;
9437 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9438 return 1;
9439 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9440 return -1;
9443 else if (tree_int_cst_lt (bitpos1, bitpos2))
9444 return -1;
9445 else
9446 return 1;
9449 /* Output any pending elements which have become next.
9450 As we output elements, constructor_unfilled_{fields,index}
9451 advances, which may cause other elements to become next;
9452 if so, they too are output.
9454 If ALL is 0, we return when there are
9455 no more pending elements to output now.
9457 If ALL is 1, we output space as necessary so that
9458 we can output all the pending elements. */
9459 static void
9460 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9462 struct init_node *elt = constructor_pending_elts;
9463 tree next;
9465 retry:
9467 /* Look through the whole pending tree.
9468 If we find an element that should be output now,
9469 output it. Otherwise, set NEXT to the element
9470 that comes first among those still pending. */
9472 next = NULL_TREE;
9473 while (elt)
9475 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9477 if (tree_int_cst_equal (elt->purpose,
9478 constructor_unfilled_index))
9479 output_init_element (input_location, elt->value, elt->origtype,
9480 true, TREE_TYPE (constructor_type),
9481 constructor_unfilled_index, false, false,
9482 braced_init_obstack);
9483 else if (tree_int_cst_lt (constructor_unfilled_index,
9484 elt->purpose))
9486 /* Advance to the next smaller node. */
9487 if (elt->left)
9488 elt = elt->left;
9489 else
9491 /* We have reached the smallest node bigger than the
9492 current unfilled index. Fill the space first. */
9493 next = elt->purpose;
9494 break;
9497 else
9499 /* Advance to the next bigger node. */
9500 if (elt->right)
9501 elt = elt->right;
9502 else
9504 /* We have reached the biggest node in a subtree. Find
9505 the parent of it, which is the next bigger node. */
9506 while (elt->parent && elt->parent->right == elt)
9507 elt = elt->parent;
9508 elt = elt->parent;
9509 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9510 elt->purpose))
9512 next = elt->purpose;
9513 break;
9518 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9520 /* If the current record is complete we are done. */
9521 if (constructor_unfilled_fields == NULL_TREE)
9522 break;
9524 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9525 elt->purpose);
9526 if (cmp == 0)
9527 output_init_element (input_location, elt->value, elt->origtype,
9528 true, TREE_TYPE (elt->purpose),
9529 elt->purpose, false, false,
9530 braced_init_obstack);
9531 else if (cmp < 0)
9533 /* Advance to the next smaller node. */
9534 if (elt->left)
9535 elt = elt->left;
9536 else
9538 /* We have reached the smallest node bigger than the
9539 current unfilled field. Fill the space first. */
9540 next = elt->purpose;
9541 break;
9544 else
9546 /* Advance to the next bigger node. */
9547 if (elt->right)
9548 elt = elt->right;
9549 else
9551 /* We have reached the biggest node in a subtree. Find
9552 the parent of it, which is the next bigger node. */
9553 while (elt->parent && elt->parent->right == elt)
9554 elt = elt->parent;
9555 elt = elt->parent;
9556 if (elt
9557 && init_field_decl_cmp (constructor_unfilled_fields,
9558 elt->purpose) < 0)
9560 next = elt->purpose;
9561 break;
9568 /* Ordinarily return, but not if we want to output all
9569 and there are elements left. */
9570 if (!(all && next != NULL_TREE))
9571 return;
9573 /* If it's not incremental, just skip over the gap, so that after
9574 jumping to retry we will output the next successive element. */
9575 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9576 constructor_unfilled_fields = next;
9577 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9578 constructor_unfilled_index = next;
9580 /* ELT now points to the node in the pending tree with the next
9581 initializer to output. */
9582 goto retry;
9585 /* Add one non-braced element to the current constructor level.
9586 This adjusts the current position within the constructor's type.
9587 This may also start or terminate implicit levels
9588 to handle a partly-braced initializer.
9590 Once this has found the correct level for the new element,
9591 it calls output_init_element.
9593 IMPLICIT is true if value comes from pop_init_level (1),
9594 the new initializer has been merged with the existing one
9595 and thus no warnings should be emitted about overriding an
9596 existing initializer. */
9598 void
9599 process_init_element (location_t loc, struct c_expr value, bool implicit,
9600 struct obstack * braced_init_obstack)
9602 tree orig_value = value.value;
9603 int string_flag
9604 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9605 bool strict_string = value.original_code == STRING_CST;
9606 bool was_designated = designator_depth != 0;
9608 designator_depth = 0;
9609 designator_erroneous = 0;
9611 if (!implicit && value.value && !integer_zerop (value.value))
9612 constructor_zeroinit = 0;
9614 /* Handle superfluous braces around string cst as in
9615 char x[] = {"foo"}; */
9616 if (string_flag
9617 && constructor_type
9618 && !was_designated
9619 && TREE_CODE (constructor_type) == ARRAY_TYPE
9620 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9621 && integer_zerop (constructor_unfilled_index))
9623 if (constructor_stack->replacement_value.value)
9624 error_init (loc, "excess elements in char array initializer");
9625 constructor_stack->replacement_value = value;
9626 return;
9629 if (constructor_stack->replacement_value.value != NULL_TREE)
9631 error_init (loc, "excess elements in struct initializer");
9632 return;
9635 /* Ignore elements of a brace group if it is entirely superfluous
9636 and has already been diagnosed. */
9637 if (constructor_type == NULL_TREE)
9638 return;
9640 if (!implicit && warn_designated_init && !was_designated
9641 && TREE_CODE (constructor_type) == RECORD_TYPE
9642 && lookup_attribute ("designated_init",
9643 TYPE_ATTRIBUTES (constructor_type)))
9644 warning_init (loc,
9645 OPT_Wdesignated_init,
9646 "positional initialization of field "
9647 "in %<struct%> declared with %<designated_init%> attribute");
9649 /* If we've exhausted any levels that didn't have braces,
9650 pop them now. */
9651 while (constructor_stack->implicit)
9653 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9654 && constructor_fields == NULL_TREE)
9655 process_init_element (loc,
9656 pop_init_level (loc, 1, braced_init_obstack,
9657 last_init_list_comma),
9658 true, braced_init_obstack);
9659 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9660 || VECTOR_TYPE_P (constructor_type))
9661 && constructor_max_index
9662 && tree_int_cst_lt (constructor_max_index,
9663 constructor_index))
9664 process_init_element (loc,
9665 pop_init_level (loc, 1, braced_init_obstack,
9666 last_init_list_comma),
9667 true, braced_init_obstack);
9668 else
9669 break;
9672 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9673 if (constructor_range_stack)
9675 /* If value is a compound literal and we'll be just using its
9676 content, don't put it into a SAVE_EXPR. */
9677 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9678 || !require_constant_value)
9680 tree semantic_type = NULL_TREE;
9681 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9683 semantic_type = TREE_TYPE (value.value);
9684 value.value = TREE_OPERAND (value.value, 0);
9686 value.value = save_expr (value.value);
9687 if (semantic_type)
9688 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9689 value.value);
9693 while (1)
9695 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9697 tree fieldtype;
9698 enum tree_code fieldcode;
9700 if (constructor_fields == NULL_TREE)
9702 pedwarn_init (loc, 0, "excess elements in struct initializer");
9703 break;
9706 fieldtype = TREE_TYPE (constructor_fields);
9707 if (fieldtype != error_mark_node)
9708 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9709 fieldcode = TREE_CODE (fieldtype);
9711 /* Error for non-static initialization of a flexible array member. */
9712 if (fieldcode == ARRAY_TYPE
9713 && !require_constant_value
9714 && TYPE_SIZE (fieldtype) == NULL_TREE
9715 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9717 error_init (loc, "non-static initialization of a flexible "
9718 "array member");
9719 break;
9722 /* Error for initialization of a flexible array member with
9723 a string constant if the structure is in an array. E.g.:
9724 struct S { int x; char y[]; };
9725 struct S s[] = { { 1, "foo" } };
9726 is invalid. */
9727 if (string_flag
9728 && fieldcode == ARRAY_TYPE
9729 && constructor_depth > 1
9730 && TYPE_SIZE (fieldtype) == NULL_TREE
9731 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9733 bool in_array_p = false;
9734 for (struct constructor_stack *p = constructor_stack;
9735 p && p->type; p = p->next)
9736 if (TREE_CODE (p->type) == ARRAY_TYPE)
9738 in_array_p = true;
9739 break;
9741 if (in_array_p)
9743 error_init (loc, "initialization of flexible array "
9744 "member in a nested context");
9745 break;
9749 /* Accept a string constant to initialize a subarray. */
9750 if (value.value != NULL_TREE
9751 && fieldcode == ARRAY_TYPE
9752 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9753 && string_flag)
9754 value.value = orig_value;
9755 /* Otherwise, if we have come to a subaggregate,
9756 and we don't have an element of its type, push into it. */
9757 else if (value.value != NULL_TREE
9758 && value.value != error_mark_node
9759 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9760 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9761 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9763 push_init_level (loc, 1, braced_init_obstack);
9764 continue;
9767 if (value.value)
9769 push_member_name (constructor_fields);
9770 output_init_element (loc, value.value, value.original_type,
9771 strict_string, fieldtype,
9772 constructor_fields, true, implicit,
9773 braced_init_obstack);
9774 RESTORE_SPELLING_DEPTH (constructor_depth);
9776 else
9777 /* Do the bookkeeping for an element that was
9778 directly output as a constructor. */
9780 /* For a record, keep track of end position of last field. */
9781 if (DECL_SIZE (constructor_fields))
9782 constructor_bit_index
9783 = size_binop_loc (input_location, PLUS_EXPR,
9784 bit_position (constructor_fields),
9785 DECL_SIZE (constructor_fields));
9787 /* If the current field was the first one not yet written out,
9788 it isn't now, so update. */
9789 if (constructor_unfilled_fields == constructor_fields)
9791 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9792 /* Skip any nameless bit fields. */
9793 while (constructor_unfilled_fields != 0
9794 && (DECL_UNNAMED_BIT_FIELD
9795 (constructor_unfilled_fields)))
9796 constructor_unfilled_fields =
9797 DECL_CHAIN (constructor_unfilled_fields);
9801 constructor_fields = DECL_CHAIN (constructor_fields);
9802 /* Skip any nameless bit fields at the beginning. */
9803 while (constructor_fields != NULL_TREE
9804 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9805 constructor_fields = DECL_CHAIN (constructor_fields);
9807 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9809 tree fieldtype;
9810 enum tree_code fieldcode;
9812 if (constructor_fields == NULL_TREE)
9814 pedwarn_init (loc, 0,
9815 "excess elements in union initializer");
9816 break;
9819 fieldtype = TREE_TYPE (constructor_fields);
9820 if (fieldtype != error_mark_node)
9821 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9822 fieldcode = TREE_CODE (fieldtype);
9824 /* Warn that traditional C rejects initialization of unions.
9825 We skip the warning if the value is zero. This is done
9826 under the assumption that the zero initializer in user
9827 code appears conditioned on e.g. __STDC__ to avoid
9828 "missing initializer" warnings and relies on default
9829 initialization to zero in the traditional C case.
9830 We also skip the warning if the initializer is designated,
9831 again on the assumption that this must be conditional on
9832 __STDC__ anyway (and we've already complained about the
9833 member-designator already). */
9834 if (!in_system_header_at (input_location) && !constructor_designated
9835 && !(value.value && (integer_zerop (value.value)
9836 || real_zerop (value.value))))
9837 warning (OPT_Wtraditional, "traditional C rejects initialization "
9838 "of unions");
9840 /* Accept a string constant to initialize a subarray. */
9841 if (value.value != NULL_TREE
9842 && fieldcode == ARRAY_TYPE
9843 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9844 && string_flag)
9845 value.value = orig_value;
9846 /* Otherwise, if we have come to a subaggregate,
9847 and we don't have an element of its type, push into it. */
9848 else if (value.value != NULL_TREE
9849 && value.value != error_mark_node
9850 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9851 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9852 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9854 push_init_level (loc, 1, braced_init_obstack);
9855 continue;
9858 if (value.value)
9860 push_member_name (constructor_fields);
9861 output_init_element (loc, value.value, value.original_type,
9862 strict_string, fieldtype,
9863 constructor_fields, true, implicit,
9864 braced_init_obstack);
9865 RESTORE_SPELLING_DEPTH (constructor_depth);
9867 else
9868 /* Do the bookkeeping for an element that was
9869 directly output as a constructor. */
9871 constructor_bit_index = DECL_SIZE (constructor_fields);
9872 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9875 constructor_fields = NULL_TREE;
9877 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9879 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9880 enum tree_code eltcode = TREE_CODE (elttype);
9882 /* Accept a string constant to initialize a subarray. */
9883 if (value.value != NULL_TREE
9884 && eltcode == ARRAY_TYPE
9885 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9886 && string_flag)
9887 value.value = orig_value;
9888 /* Otherwise, if we have come to a subaggregate,
9889 and we don't have an element of its type, push into it. */
9890 else if (value.value != NULL_TREE
9891 && value.value != error_mark_node
9892 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9893 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9894 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9896 push_init_level (loc, 1, braced_init_obstack);
9897 continue;
9900 if (constructor_max_index != NULL_TREE
9901 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9902 || integer_all_onesp (constructor_max_index)))
9904 pedwarn_init (loc, 0,
9905 "excess elements in array initializer");
9906 break;
9909 /* Now output the actual element. */
9910 if (value.value)
9912 push_array_bounds (tree_to_uhwi (constructor_index));
9913 output_init_element (loc, value.value, value.original_type,
9914 strict_string, elttype,
9915 constructor_index, true, implicit,
9916 braced_init_obstack);
9917 RESTORE_SPELLING_DEPTH (constructor_depth);
9920 constructor_index
9921 = size_binop_loc (input_location, PLUS_EXPR,
9922 constructor_index, bitsize_one_node);
9924 if (!value.value)
9925 /* If we are doing the bookkeeping for an element that was
9926 directly output as a constructor, we must update
9927 constructor_unfilled_index. */
9928 constructor_unfilled_index = constructor_index;
9930 else if (VECTOR_TYPE_P (constructor_type))
9932 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9934 /* Do a basic check of initializer size. Note that vectors
9935 always have a fixed size derived from their type. */
9936 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9938 pedwarn_init (loc, 0,
9939 "excess elements in vector initializer");
9940 break;
9943 /* Now output the actual element. */
9944 if (value.value)
9946 if (TREE_CODE (value.value) == VECTOR_CST)
9947 elttype = TYPE_MAIN_VARIANT (constructor_type);
9948 output_init_element (loc, value.value, value.original_type,
9949 strict_string, elttype,
9950 constructor_index, true, implicit,
9951 braced_init_obstack);
9954 constructor_index
9955 = size_binop_loc (input_location,
9956 PLUS_EXPR, constructor_index, bitsize_one_node);
9958 if (!value.value)
9959 /* If we are doing the bookkeeping for an element that was
9960 directly output as a constructor, we must update
9961 constructor_unfilled_index. */
9962 constructor_unfilled_index = constructor_index;
9965 /* Handle the sole element allowed in a braced initializer
9966 for a scalar variable. */
9967 else if (constructor_type != error_mark_node
9968 && constructor_fields == NULL_TREE)
9970 pedwarn_init (loc, 0,
9971 "excess elements in scalar initializer");
9972 break;
9974 else
9976 if (value.value)
9977 output_init_element (loc, value.value, value.original_type,
9978 strict_string, constructor_type,
9979 NULL_TREE, true, implicit,
9980 braced_init_obstack);
9981 constructor_fields = NULL_TREE;
9984 /* Handle range initializers either at this level or anywhere higher
9985 in the designator stack. */
9986 if (constructor_range_stack)
9988 struct constructor_range_stack *p, *range_stack;
9989 int finish = 0;
9991 range_stack = constructor_range_stack;
9992 constructor_range_stack = 0;
9993 while (constructor_stack != range_stack->stack)
9995 gcc_assert (constructor_stack->implicit);
9996 process_init_element (loc,
9997 pop_init_level (loc, 1,
9998 braced_init_obstack,
9999 last_init_list_comma),
10000 true, braced_init_obstack);
10002 for (p = range_stack;
10003 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10004 p = p->prev)
10006 gcc_assert (constructor_stack->implicit);
10007 process_init_element (loc,
10008 pop_init_level (loc, 1,
10009 braced_init_obstack,
10010 last_init_list_comma),
10011 true, braced_init_obstack);
10014 p->index = size_binop_loc (input_location,
10015 PLUS_EXPR, p->index, bitsize_one_node);
10016 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10017 finish = 1;
10019 while (1)
10021 constructor_index = p->index;
10022 constructor_fields = p->fields;
10023 if (finish && p->range_end && p->index == p->range_start)
10025 finish = 0;
10026 p->prev = 0;
10028 p = p->next;
10029 if (!p)
10030 break;
10031 finish_implicit_inits (loc, braced_init_obstack);
10032 push_init_level (loc, 2, braced_init_obstack);
10033 p->stack = constructor_stack;
10034 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10035 p->index = p->range_start;
10038 if (!finish)
10039 constructor_range_stack = range_stack;
10040 continue;
10043 break;
10046 constructor_range_stack = 0;
10049 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10050 (guaranteed to be 'volatile' or null) and ARGS (represented using
10051 an ASM_EXPR node). */
10052 tree
10053 build_asm_stmt (tree cv_qualifier, tree args)
10055 if (!ASM_VOLATILE_P (args) && cv_qualifier)
10056 ASM_VOLATILE_P (args) = 1;
10057 return add_stmt (args);
10060 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10061 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10062 SIMPLE indicates whether there was anything at all after the
10063 string in the asm expression -- asm("blah") and asm("blah" : )
10064 are subtly different. We use a ASM_EXPR node to represent this. */
10065 tree
10066 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10067 tree clobbers, tree labels, bool simple)
10069 tree tail;
10070 tree args;
10071 int i;
10072 const char *constraint;
10073 const char **oconstraints;
10074 bool allows_mem, allows_reg, is_inout;
10075 int ninputs, noutputs;
10077 ninputs = list_length (inputs);
10078 noutputs = list_length (outputs);
10079 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10081 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10083 /* Remove output conversions that change the type but not the mode. */
10084 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10086 tree output = TREE_VALUE (tail);
10088 output = c_fully_fold (output, false, NULL, true);
10090 /* ??? Really, this should not be here. Users should be using a
10091 proper lvalue, dammit. But there's a long history of using casts
10092 in the output operands. In cases like longlong.h, this becomes a
10093 primitive form of typechecking -- if the cast can be removed, then
10094 the output operand had a type of the proper width; otherwise we'll
10095 get an error. Gross, but ... */
10096 STRIP_NOPS (output);
10098 if (!lvalue_or_else (loc, output, lv_asm))
10099 output = error_mark_node;
10101 if (output != error_mark_node
10102 && (TREE_READONLY (output)
10103 || TYPE_READONLY (TREE_TYPE (output))
10104 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10105 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10106 readonly_error (loc, output, lv_asm);
10108 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10109 oconstraints[i] = constraint;
10111 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10112 &allows_mem, &allows_reg, &is_inout))
10114 /* If the operand is going to end up in memory,
10115 mark it addressable. */
10116 if (!allows_reg && !c_mark_addressable (output))
10117 output = error_mark_node;
10118 if (!(!allows_reg && allows_mem)
10119 && output != error_mark_node
10120 && VOID_TYPE_P (TREE_TYPE (output)))
10122 error_at (loc, "invalid use of void expression");
10123 output = error_mark_node;
10126 else
10127 output = error_mark_node;
10129 TREE_VALUE (tail) = output;
10132 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10134 tree input;
10136 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10137 input = TREE_VALUE (tail);
10139 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10140 oconstraints, &allows_mem, &allows_reg))
10142 /* If the operand is going to end up in memory,
10143 mark it addressable. */
10144 if (!allows_reg && allows_mem)
10146 input = c_fully_fold (input, false, NULL, true);
10148 /* Strip the nops as we allow this case. FIXME, this really
10149 should be rejected or made deprecated. */
10150 STRIP_NOPS (input);
10151 if (!c_mark_addressable (input))
10152 input = error_mark_node;
10154 else
10156 struct c_expr expr;
10157 memset (&expr, 0, sizeof (expr));
10158 expr.value = input;
10159 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10160 input = c_fully_fold (expr.value, false, NULL);
10162 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10164 error_at (loc, "invalid use of void expression");
10165 input = error_mark_node;
10169 else
10170 input = error_mark_node;
10172 TREE_VALUE (tail) = input;
10175 /* ASMs with labels cannot have outputs. This should have been
10176 enforced by the parser. */
10177 gcc_assert (outputs == NULL || labels == NULL);
10179 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10181 /* asm statements without outputs, including simple ones, are treated
10182 as volatile. */
10183 ASM_INPUT_P (args) = simple;
10184 ASM_VOLATILE_P (args) = (noutputs == 0);
10186 return args;
10189 /* Generate a goto statement to LABEL. LOC is the location of the
10190 GOTO. */
10192 tree
10193 c_finish_goto_label (location_t loc, tree label)
10195 tree decl = lookup_label_for_goto (loc, label);
10196 if (!decl)
10197 return NULL_TREE;
10198 TREE_USED (decl) = 1;
10200 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10201 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10202 SET_EXPR_LOCATION (t, loc);
10203 return add_stmt (t);
10207 /* Generate a computed goto statement to EXPR. LOC is the location of
10208 the GOTO. */
10210 tree
10211 c_finish_goto_ptr (location_t loc, tree expr)
10213 tree t;
10214 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10215 expr = c_fully_fold (expr, false, NULL);
10216 expr = convert (ptr_type_node, expr);
10217 t = build1 (GOTO_EXPR, void_type_node, expr);
10218 SET_EXPR_LOCATION (t, loc);
10219 return add_stmt (t);
10222 /* Generate a C `return' statement. RETVAL is the expression for what
10223 to return, or a null pointer for `return;' with no value. LOC is
10224 the location of the return statement, or the location of the expression,
10225 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10226 is the original type of RETVAL. */
10228 tree
10229 c_finish_return (location_t loc, tree retval, tree origtype)
10231 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10232 bool no_warning = false;
10233 bool npc = false;
10235 /* Use the expansion point to handle cases such as returning NULL
10236 in a function returning void. */
10237 location_t xloc = expansion_point_location_if_in_system_header (loc);
10239 if (TREE_THIS_VOLATILE (current_function_decl))
10240 warning_at (xloc, 0,
10241 "function declared %<noreturn%> has a %<return%> statement");
10243 if (retval)
10245 tree semantic_type = NULL_TREE;
10246 npc = null_pointer_constant_p (retval);
10247 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10249 semantic_type = TREE_TYPE (retval);
10250 retval = TREE_OPERAND (retval, 0);
10252 retval = c_fully_fold (retval, false, NULL);
10253 if (semantic_type)
10254 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10257 if (!retval)
10259 current_function_returns_null = 1;
10260 if ((warn_return_type >= 0 || flag_isoc99)
10261 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10263 bool warned_here;
10264 if (flag_isoc99)
10265 warned_here = pedwarn
10266 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10267 "%<return%> with no value, in function returning non-void");
10268 else
10269 warned_here = warning_at
10270 (loc, OPT_Wreturn_type,
10271 "%<return%> with no value, in function returning non-void");
10272 no_warning = true;
10273 if (warned_here)
10274 inform (DECL_SOURCE_LOCATION (current_function_decl),
10275 "declared here");
10278 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10280 current_function_returns_null = 1;
10281 bool warned_here;
10282 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10283 warned_here = pedwarn
10284 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10285 "%<return%> with a value, in function returning void");
10286 else
10287 warned_here = pedwarn
10288 (xloc, OPT_Wpedantic, "ISO C forbids "
10289 "%<return%> with expression, in function returning void");
10290 if (warned_here)
10291 inform (DECL_SOURCE_LOCATION (current_function_decl),
10292 "declared here");
10294 else
10296 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10297 retval, origtype, ic_return,
10298 npc, NULL_TREE, NULL_TREE, 0);
10299 tree res = DECL_RESULT (current_function_decl);
10300 tree inner;
10301 bool save;
10303 current_function_returns_value = 1;
10304 if (t == error_mark_node)
10305 return NULL_TREE;
10307 save = in_late_binary_op;
10308 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10309 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10310 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10311 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10312 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10313 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10314 in_late_binary_op = true;
10315 inner = t = convert (TREE_TYPE (res), t);
10316 in_late_binary_op = save;
10318 /* Strip any conversions, additions, and subtractions, and see if
10319 we are returning the address of a local variable. Warn if so. */
10320 while (1)
10322 switch (TREE_CODE (inner))
10324 CASE_CONVERT:
10325 case NON_LVALUE_EXPR:
10326 case PLUS_EXPR:
10327 case POINTER_PLUS_EXPR:
10328 inner = TREE_OPERAND (inner, 0);
10329 continue;
10331 case MINUS_EXPR:
10332 /* If the second operand of the MINUS_EXPR has a pointer
10333 type (or is converted from it), this may be valid, so
10334 don't give a warning. */
10336 tree op1 = TREE_OPERAND (inner, 1);
10338 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10339 && (CONVERT_EXPR_P (op1)
10340 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10341 op1 = TREE_OPERAND (op1, 0);
10343 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10344 break;
10346 inner = TREE_OPERAND (inner, 0);
10347 continue;
10350 case ADDR_EXPR:
10351 inner = TREE_OPERAND (inner, 0);
10353 while (REFERENCE_CLASS_P (inner)
10354 && !INDIRECT_REF_P (inner))
10355 inner = TREE_OPERAND (inner, 0);
10357 if (DECL_P (inner)
10358 && !DECL_EXTERNAL (inner)
10359 && !TREE_STATIC (inner)
10360 && DECL_CONTEXT (inner) == current_function_decl)
10362 if (TREE_CODE (inner) == LABEL_DECL)
10363 warning_at (loc, OPT_Wreturn_local_addr,
10364 "function returns address of label");
10365 else
10367 warning_at (loc, OPT_Wreturn_local_addr,
10368 "function returns address of local variable");
10369 tree zero = build_zero_cst (TREE_TYPE (res));
10370 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10373 break;
10375 default:
10376 break;
10379 break;
10382 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10383 SET_EXPR_LOCATION (retval, loc);
10385 if (warn_sequence_point)
10386 verify_sequence_points (retval);
10389 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10390 TREE_NO_WARNING (ret_stmt) |= no_warning;
10391 return add_stmt (ret_stmt);
10394 struct c_switch {
10395 /* The SWITCH_EXPR being built. */
10396 tree switch_expr;
10398 /* The original type of the testing expression, i.e. before the
10399 default conversion is applied. */
10400 tree orig_type;
10402 /* A splay-tree mapping the low element of a case range to the high
10403 element, or NULL_TREE if there is no high element. Used to
10404 determine whether or not a new case label duplicates an old case
10405 label. We need a tree, rather than simply a hash table, because
10406 of the GNU case range extension. */
10407 splay_tree cases;
10409 /* The bindings at the point of the switch. This is used for
10410 warnings crossing decls when branching to a case label. */
10411 struct c_spot_bindings *bindings;
10413 /* The next node on the stack. */
10414 struct c_switch *next;
10416 /* Remember whether the controlling expression had boolean type
10417 before integer promotions for the sake of -Wswitch-bool. */
10418 bool bool_cond_p;
10420 /* Remember whether there was a case value that is outside the
10421 range of the ORIG_TYPE. */
10422 bool outside_range_p;
10425 /* A stack of the currently active switch statements. The innermost
10426 switch statement is on the top of the stack. There is no need to
10427 mark the stack for garbage collection because it is only active
10428 during the processing of the body of a function, and we never
10429 collect at that point. */
10431 struct c_switch *c_switch_stack;
10433 /* Start a C switch statement, testing expression EXP. Return the new
10434 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10435 SWITCH_COND_LOC is the location of the switch's condition.
10436 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10438 tree
10439 c_start_case (location_t switch_loc,
10440 location_t switch_cond_loc,
10441 tree exp, bool explicit_cast_p)
10443 tree orig_type = error_mark_node;
10444 bool bool_cond_p = false;
10445 struct c_switch *cs;
10447 if (exp != error_mark_node)
10449 orig_type = TREE_TYPE (exp);
10451 if (!INTEGRAL_TYPE_P (orig_type))
10453 if (orig_type != error_mark_node)
10455 error_at (switch_cond_loc, "switch quantity not an integer");
10456 orig_type = error_mark_node;
10458 exp = integer_zero_node;
10460 else
10462 tree type = TYPE_MAIN_VARIANT (orig_type);
10463 tree e = exp;
10465 /* Warn if the condition has boolean value. */
10466 while (TREE_CODE (e) == COMPOUND_EXPR)
10467 e = TREE_OPERAND (e, 1);
10469 if ((TREE_CODE (type) == BOOLEAN_TYPE
10470 || truth_value_p (TREE_CODE (e)))
10471 /* Explicit cast to int suppresses this warning. */
10472 && !(TREE_CODE (type) == INTEGER_TYPE
10473 && explicit_cast_p))
10474 bool_cond_p = true;
10476 if (!in_system_header_at (input_location)
10477 && (type == long_integer_type_node
10478 || type == long_unsigned_type_node))
10479 warning_at (switch_cond_loc,
10480 OPT_Wtraditional, "%<long%> switch expression not "
10481 "converted to %<int%> in ISO C");
10483 exp = c_fully_fold (exp, false, NULL);
10484 exp = default_conversion (exp);
10486 if (warn_sequence_point)
10487 verify_sequence_points (exp);
10491 /* Add this new SWITCH_EXPR to the stack. */
10492 cs = XNEW (struct c_switch);
10493 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10494 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10495 cs->orig_type = orig_type;
10496 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10497 cs->bindings = c_get_switch_bindings ();
10498 cs->bool_cond_p = bool_cond_p;
10499 cs->outside_range_p = false;
10500 cs->next = c_switch_stack;
10501 c_switch_stack = cs;
10503 return add_stmt (cs->switch_expr);
10506 /* Process a case label at location LOC. */
10508 tree
10509 do_case (location_t loc, tree low_value, tree high_value)
10511 tree label = NULL_TREE;
10513 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10515 low_value = c_fully_fold (low_value, false, NULL);
10516 if (TREE_CODE (low_value) == INTEGER_CST)
10517 pedwarn (loc, OPT_Wpedantic,
10518 "case label is not an integer constant expression");
10521 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10523 high_value = c_fully_fold (high_value, false, NULL);
10524 if (TREE_CODE (high_value) == INTEGER_CST)
10525 pedwarn (input_location, OPT_Wpedantic,
10526 "case label is not an integer constant expression");
10529 if (c_switch_stack == NULL)
10531 if (low_value)
10532 error_at (loc, "case label not within a switch statement");
10533 else
10534 error_at (loc, "%<default%> label not within a switch statement");
10535 return NULL_TREE;
10538 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10539 EXPR_LOCATION (c_switch_stack->switch_expr),
10540 loc))
10541 return NULL_TREE;
10543 label = c_add_case_label (loc, c_switch_stack->cases,
10544 SWITCH_COND (c_switch_stack->switch_expr),
10545 c_switch_stack->orig_type,
10546 low_value, high_value,
10547 &c_switch_stack->outside_range_p);
10548 if (label == error_mark_node)
10549 label = NULL_TREE;
10550 return label;
10553 /* Finish the switch statement. TYPE is the original type of the
10554 controlling expression of the switch, or NULL_TREE. */
10556 void
10557 c_finish_case (tree body, tree type)
10559 struct c_switch *cs = c_switch_stack;
10560 location_t switch_location;
10562 SWITCH_BODY (cs->switch_expr) = body;
10564 /* Emit warnings as needed. */
10565 switch_location = EXPR_LOCATION (cs->switch_expr);
10566 c_do_switch_warnings (cs->cases, switch_location,
10567 type ? type : TREE_TYPE (cs->switch_expr),
10568 SWITCH_COND (cs->switch_expr),
10569 cs->bool_cond_p, cs->outside_range_p);
10570 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10571 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10573 /* Pop the stack. */
10574 c_switch_stack = cs->next;
10575 splay_tree_delete (cs->cases);
10576 c_release_switch_bindings (cs->bindings);
10577 XDELETE (cs);
10580 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10581 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10582 may be null. */
10584 void
10585 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10586 tree else_block)
10588 tree stmt;
10590 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10591 SET_EXPR_LOCATION (stmt, if_locus);
10592 add_stmt (stmt);
10595 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10596 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10597 is false for DO loops. INCR is the FOR increment expression. BODY is
10598 the statement controlled by the loop. BLAB is the break label. CLAB is
10599 the continue label. Everything is allowed to be NULL. */
10601 void
10602 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10603 tree blab, tree clab, bool cond_is_first)
10605 tree entry = NULL, exit = NULL, t;
10607 /* If the condition is zero don't generate a loop construct. */
10608 if (cond && integer_zerop (cond))
10610 if (cond_is_first)
10612 t = build_and_jump (&blab);
10613 SET_EXPR_LOCATION (t, start_locus);
10614 add_stmt (t);
10617 else
10619 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10621 /* If we have an exit condition, then we build an IF with gotos either
10622 out of the loop, or to the top of it. If there's no exit condition,
10623 then we just build a jump back to the top. */
10624 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10626 if (cond && !integer_nonzerop (cond))
10628 /* Canonicalize the loop condition to the end. This means
10629 generating a branch to the loop condition. Reuse the
10630 continue label, if possible. */
10631 if (cond_is_first)
10633 if (incr || !clab)
10635 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10636 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10638 else
10639 t = build1 (GOTO_EXPR, void_type_node, clab);
10640 SET_EXPR_LOCATION (t, start_locus);
10641 add_stmt (t);
10644 t = build_and_jump (&blab);
10645 if (cond_is_first)
10646 exit = fold_build3_loc (start_locus,
10647 COND_EXPR, void_type_node, cond, exit, t);
10648 else
10649 exit = fold_build3_loc (input_location,
10650 COND_EXPR, void_type_node, cond, exit, t);
10652 else
10654 /* For the backward-goto's location of an unconditional loop
10655 use the beginning of the body, or, if there is none, the
10656 top of the loop. */
10657 location_t loc = EXPR_LOCATION (expr_first (body));
10658 if (loc == UNKNOWN_LOCATION)
10659 loc = start_locus;
10660 SET_EXPR_LOCATION (exit, loc);
10663 add_stmt (top);
10666 if (body)
10667 add_stmt (body);
10668 if (clab)
10669 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10670 if (incr)
10671 add_stmt (incr);
10672 if (entry)
10673 add_stmt (entry);
10674 if (exit)
10675 add_stmt (exit);
10676 if (blab)
10677 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10680 tree
10681 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10683 bool skip;
10684 tree label = *label_p;
10686 /* In switch statements break is sometimes stylistically used after
10687 a return statement. This can lead to spurious warnings about
10688 control reaching the end of a non-void function when it is
10689 inlined. Note that we are calling block_may_fallthru with
10690 language specific tree nodes; this works because
10691 block_may_fallthru returns true when given something it does not
10692 understand. */
10693 skip = !block_may_fallthru (cur_stmt_list);
10695 if (!label)
10697 if (!skip)
10698 *label_p = label = create_artificial_label (loc);
10700 else if (TREE_CODE (label) == LABEL_DECL)
10702 else switch (TREE_INT_CST_LOW (label))
10704 case 0:
10705 if (is_break)
10706 error_at (loc, "break statement not within loop or switch");
10707 else
10708 error_at (loc, "continue statement not within a loop");
10709 return NULL_TREE;
10711 case 1:
10712 gcc_assert (is_break);
10713 error_at (loc, "break statement used with OpenMP for loop");
10714 return NULL_TREE;
10716 case 2:
10717 if (is_break)
10718 error ("break statement within %<#pragma simd%> loop body");
10719 else
10720 error ("continue statement within %<#pragma simd%> loop body");
10721 return NULL_TREE;
10723 default:
10724 gcc_unreachable ();
10727 if (skip)
10728 return NULL_TREE;
10730 if (!is_break)
10731 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10733 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10736 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10738 static void
10739 emit_side_effect_warnings (location_t loc, tree expr)
10741 if (expr == error_mark_node)
10743 else if (!TREE_SIDE_EFFECTS (expr))
10745 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10746 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10748 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10750 tree r = expr;
10751 location_t cloc = loc;
10752 while (TREE_CODE (r) == COMPOUND_EXPR)
10754 if (EXPR_HAS_LOCATION (r))
10755 cloc = EXPR_LOCATION (r);
10756 r = TREE_OPERAND (r, 1);
10758 if (!TREE_SIDE_EFFECTS (r)
10759 && !VOID_TYPE_P (TREE_TYPE (r))
10760 && !CONVERT_EXPR_P (r)
10761 && !TREE_NO_WARNING (r)
10762 && !TREE_NO_WARNING (expr))
10763 warning_at (cloc, OPT_Wunused_value,
10764 "right-hand operand of comma expression has no effect");
10766 else
10767 warn_if_unused_value (expr, loc);
10770 /* Process an expression as if it were a complete statement. Emit
10771 diagnostics, but do not call ADD_STMT. LOC is the location of the
10772 statement. */
10774 tree
10775 c_process_expr_stmt (location_t loc, tree expr)
10777 tree exprv;
10779 if (!expr)
10780 return NULL_TREE;
10782 expr = c_fully_fold (expr, false, NULL);
10784 if (warn_sequence_point)
10785 verify_sequence_points (expr);
10787 if (TREE_TYPE (expr) != error_mark_node
10788 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10789 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10790 error_at (loc, "expression statement has incomplete type");
10792 /* If we're not processing a statement expression, warn about unused values.
10793 Warnings for statement expressions will be emitted later, once we figure
10794 out which is the result. */
10795 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10796 && warn_unused_value)
10797 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10799 exprv = expr;
10800 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10801 exprv = TREE_OPERAND (exprv, 1);
10802 while (CONVERT_EXPR_P (exprv))
10803 exprv = TREE_OPERAND (exprv, 0);
10804 if (DECL_P (exprv)
10805 || handled_component_p (exprv)
10806 || TREE_CODE (exprv) == ADDR_EXPR)
10807 mark_exp_read (exprv);
10809 /* If the expression is not of a type to which we cannot assign a line
10810 number, wrap the thing in a no-op NOP_EXPR. */
10811 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10813 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10814 SET_EXPR_LOCATION (expr, loc);
10817 return expr;
10820 /* Emit an expression as a statement. LOC is the location of the
10821 expression. */
10823 tree
10824 c_finish_expr_stmt (location_t loc, tree expr)
10826 if (expr)
10827 return add_stmt (c_process_expr_stmt (loc, expr));
10828 else
10829 return NULL;
10832 /* Do the opposite and emit a statement as an expression. To begin,
10833 create a new binding level and return it. */
10835 tree
10836 c_begin_stmt_expr (void)
10838 tree ret;
10840 /* We must force a BLOCK for this level so that, if it is not expanded
10841 later, there is a way to turn off the entire subtree of blocks that
10842 are contained in it. */
10843 keep_next_level ();
10844 ret = c_begin_compound_stmt (true);
10846 c_bindings_start_stmt_expr (c_switch_stack == NULL
10847 ? NULL
10848 : c_switch_stack->bindings);
10850 /* Mark the current statement list as belonging to a statement list. */
10851 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10853 return ret;
10856 /* LOC is the location of the compound statement to which this body
10857 belongs. */
10859 tree
10860 c_finish_stmt_expr (location_t loc, tree body)
10862 tree last, type, tmp, val;
10863 tree *last_p;
10865 body = c_end_compound_stmt (loc, body, true);
10867 c_bindings_end_stmt_expr (c_switch_stack == NULL
10868 ? NULL
10869 : c_switch_stack->bindings);
10871 /* Locate the last statement in BODY. See c_end_compound_stmt
10872 about always returning a BIND_EXPR. */
10873 last_p = &BIND_EXPR_BODY (body);
10874 last = BIND_EXPR_BODY (body);
10876 continue_searching:
10877 if (TREE_CODE (last) == STATEMENT_LIST)
10879 tree_stmt_iterator l = tsi_last (last);
10881 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
10882 tsi_prev (&l);
10884 /* This can happen with degenerate cases like ({ }). No value. */
10885 if (tsi_end_p (l))
10886 return body;
10888 /* If we're supposed to generate side effects warnings, process
10889 all of the statements except the last. */
10890 if (warn_unused_value)
10892 for (tree_stmt_iterator i = tsi_start (last);
10893 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
10895 location_t tloc;
10896 tree t = tsi_stmt (i);
10898 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10899 emit_side_effect_warnings (tloc, t);
10902 last_p = tsi_stmt_ptr (l);
10903 last = *last_p;
10906 /* If the end of the list is exception related, then the list was split
10907 by a call to push_cleanup. Continue searching. */
10908 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10909 || TREE_CODE (last) == TRY_CATCH_EXPR)
10911 last_p = &TREE_OPERAND (last, 0);
10912 last = *last_p;
10913 goto continue_searching;
10916 if (last == error_mark_node)
10917 return last;
10919 /* In the case that the BIND_EXPR is not necessary, return the
10920 expression out from inside it. */
10921 if ((last == BIND_EXPR_BODY (body)
10922 /* Skip nested debug stmts. */
10923 || last == expr_first (BIND_EXPR_BODY (body)))
10924 && BIND_EXPR_VARS (body) == NULL)
10926 /* Even if this looks constant, do not allow it in a constant
10927 expression. */
10928 last = c_wrap_maybe_const (last, true);
10929 /* Do not warn if the return value of a statement expression is
10930 unused. */
10931 TREE_NO_WARNING (last) = 1;
10932 return last;
10935 /* Extract the type of said expression. */
10936 type = TREE_TYPE (last);
10938 /* If we're not returning a value at all, then the BIND_EXPR that
10939 we already have is a fine expression to return. */
10940 if (!type || VOID_TYPE_P (type))
10941 return body;
10943 /* Now that we've located the expression containing the value, it seems
10944 silly to make voidify_wrapper_expr repeat the process. Create a
10945 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10946 tmp = create_tmp_var_raw (type);
10948 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10949 tree_expr_nonnegative_p giving up immediately. */
10950 val = last;
10951 if (TREE_CODE (val) == NOP_EXPR
10952 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10953 val = TREE_OPERAND (val, 0);
10955 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10956 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10959 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10960 SET_EXPR_LOCATION (t, loc);
10961 return t;
10965 /* Begin and end compound statements. This is as simple as pushing
10966 and popping new statement lists from the tree. */
10968 tree
10969 c_begin_compound_stmt (bool do_scope)
10971 tree stmt = push_stmt_list ();
10972 if (do_scope)
10973 push_scope ();
10974 return stmt;
10977 /* End a compound statement. STMT is the statement. LOC is the
10978 location of the compound statement-- this is usually the location
10979 of the opening brace. */
10981 tree
10982 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10984 tree block = NULL;
10986 if (do_scope)
10988 if (c_dialect_objc ())
10989 objc_clear_super_receiver ();
10990 block = pop_scope ();
10993 stmt = pop_stmt_list (stmt);
10994 stmt = c_build_bind_expr (loc, block, stmt);
10996 /* If this compound statement is nested immediately inside a statement
10997 expression, then force a BIND_EXPR to be created. Otherwise we'll
10998 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10999 STATEMENT_LISTs merge, and thus we can lose track of what statement
11000 was really last. */
11001 if (building_stmt_list_p ()
11002 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11003 && TREE_CODE (stmt) != BIND_EXPR)
11005 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11006 TREE_SIDE_EFFECTS (stmt) = 1;
11007 SET_EXPR_LOCATION (stmt, loc);
11010 return stmt;
11013 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11014 when the current scope is exited. EH_ONLY is true when this is not
11015 meant to apply to normal control flow transfer. */
11017 void
11018 push_cleanup (tree decl, tree cleanup, bool eh_only)
11020 enum tree_code code;
11021 tree stmt, list;
11022 bool stmt_expr;
11024 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11025 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11026 add_stmt (stmt);
11027 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11028 list = push_stmt_list ();
11029 TREE_OPERAND (stmt, 0) = list;
11030 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11033 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11034 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11036 static tree
11037 build_vec_cmp (tree_code code, tree type,
11038 tree arg0, tree arg1)
11040 tree zero_vec = build_zero_cst (type);
11041 tree minus_one_vec = build_minus_one_cst (type);
11042 tree cmp_type = build_same_sized_truth_vector_type (type);
11043 tree cmp = build2 (code, cmp_type, arg0, arg1);
11044 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11047 /* Subclass of range_label for labelling the type of EXPR when reporting
11048 a type mismatch between EXPR and OTHER_EXPR.
11049 Either or both of EXPR and OTHER_EXPR could be NULL. */
11051 class maybe_range_label_for_tree_type_mismatch : public range_label
11053 public:
11054 maybe_range_label_for_tree_type_mismatch (tree expr, tree other_expr)
11055 : m_expr (expr), m_other_expr (other_expr)
11059 label_text get_text (unsigned range_idx) const FINAL OVERRIDE
11061 if (m_expr == NULL_TREE
11062 || !EXPR_P (m_expr))
11063 return label_text (NULL, false);
11064 tree expr_type = TREE_TYPE (m_expr);
11066 tree other_type = NULL_TREE;
11067 if (m_other_expr && EXPR_P (m_other_expr))
11068 other_type = TREE_TYPE (m_other_expr);
11070 range_label_for_type_mismatch inner (expr_type, other_type);
11071 return inner.get_text (range_idx);
11074 private:
11075 tree m_expr;
11076 tree m_other_expr;
11079 /* Build a binary-operation expression without default conversions.
11080 CODE is the kind of expression to build.
11081 LOCATION is the operator's location.
11082 This function differs from `build' in several ways:
11083 the data type of the result is computed and recorded in it,
11084 warnings are generated if arg data types are invalid,
11085 special handling for addition and subtraction of pointers is known,
11086 and some optimization is done (operations on narrow ints
11087 are done in the narrower type when that gives the same result).
11088 Constant folding is also done before the result is returned.
11090 Note that the operands will never have enumeral types, or function
11091 or array types, because either they will have the default conversions
11092 performed or they have both just been converted to some other type in which
11093 the arithmetic is to be done. */
11095 tree
11096 build_binary_op (location_t location, enum tree_code code,
11097 tree orig_op0, tree orig_op1, bool convert_p)
11099 tree type0, type1, orig_type0, orig_type1;
11100 tree eptype;
11101 enum tree_code code0, code1;
11102 tree op0, op1;
11103 tree ret = error_mark_node;
11104 const char *invalid_op_diag;
11105 bool op0_int_operands, op1_int_operands;
11106 bool int_const, int_const_or_overflow, int_operands;
11108 /* Expression code to give to the expression when it is built.
11109 Normally this is CODE, which is what the caller asked for,
11110 but in some special cases we change it. */
11111 enum tree_code resultcode = code;
11113 /* Data type in which the computation is to be performed.
11114 In the simplest cases this is the common type of the arguments. */
11115 tree result_type = NULL;
11117 /* When the computation is in excess precision, the type of the
11118 final EXCESS_PRECISION_EXPR. */
11119 tree semantic_result_type = NULL;
11121 /* Nonzero means operands have already been type-converted
11122 in whatever way is necessary.
11123 Zero means they need to be converted to RESULT_TYPE. */
11124 int converted = 0;
11126 /* Nonzero means create the expression with this type, rather than
11127 RESULT_TYPE. */
11128 tree build_type = NULL_TREE;
11130 /* Nonzero means after finally constructing the expression
11131 convert it to this type. */
11132 tree final_type = NULL_TREE;
11134 /* Nonzero if this is an operation like MIN or MAX which can
11135 safely be computed in short if both args are promoted shorts.
11136 Also implies COMMON.
11137 -1 indicates a bitwise operation; this makes a difference
11138 in the exact conditions for when it is safe to do the operation
11139 in a narrower mode. */
11140 int shorten = 0;
11142 /* Nonzero if this is a comparison operation;
11143 if both args are promoted shorts, compare the original shorts.
11144 Also implies COMMON. */
11145 int short_compare = 0;
11147 /* Nonzero if this is a right-shift operation, which can be computed on the
11148 original short and then promoted if the operand is a promoted short. */
11149 int short_shift = 0;
11151 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11152 int common = 0;
11154 /* True means types are compatible as far as ObjC is concerned. */
11155 bool objc_ok;
11157 /* True means this is an arithmetic operation that may need excess
11158 precision. */
11159 bool may_need_excess_precision;
11161 /* True means this is a boolean operation that converts both its
11162 operands to truth-values. */
11163 bool boolean_op = false;
11165 /* Remember whether we're doing / or %. */
11166 bool doing_div_or_mod = false;
11168 /* Remember whether we're doing << or >>. */
11169 bool doing_shift = false;
11171 /* Tree holding instrumentation expression. */
11172 tree instrument_expr = NULL;
11174 if (location == UNKNOWN_LOCATION)
11175 location = input_location;
11177 op0 = orig_op0;
11178 op1 = orig_op1;
11180 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11181 if (op0_int_operands)
11182 op0 = remove_c_maybe_const_expr (op0);
11183 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11184 if (op1_int_operands)
11185 op1 = remove_c_maybe_const_expr (op1);
11186 int_operands = (op0_int_operands && op1_int_operands);
11187 if (int_operands)
11189 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11190 && TREE_CODE (orig_op1) == INTEGER_CST);
11191 int_const = (int_const_or_overflow
11192 && !TREE_OVERFLOW (orig_op0)
11193 && !TREE_OVERFLOW (orig_op1));
11195 else
11196 int_const = int_const_or_overflow = false;
11198 /* Do not apply default conversion in mixed vector/scalar expression. */
11199 if (convert_p
11200 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11202 op0 = default_conversion (op0);
11203 op1 = default_conversion (op1);
11206 orig_type0 = type0 = TREE_TYPE (op0);
11208 orig_type1 = type1 = TREE_TYPE (op1);
11210 /* The expression codes of the data types of the arguments tell us
11211 whether the arguments are integers, floating, pointers, etc. */
11212 code0 = TREE_CODE (type0);
11213 code1 = TREE_CODE (type1);
11215 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11216 STRIP_TYPE_NOPS (op0);
11217 STRIP_TYPE_NOPS (op1);
11219 /* If an error was already reported for one of the arguments,
11220 avoid reporting another error. */
11222 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11223 return error_mark_node;
11225 if (code0 == POINTER_TYPE
11226 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11227 return error_mark_node;
11229 if (code1 == POINTER_TYPE
11230 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11231 return error_mark_node;
11233 if ((invalid_op_diag
11234 = targetm.invalid_binary_op (code, type0, type1)))
11236 error_at (location, invalid_op_diag);
11237 return error_mark_node;
11240 switch (code)
11242 case PLUS_EXPR:
11243 case MINUS_EXPR:
11244 case MULT_EXPR:
11245 case TRUNC_DIV_EXPR:
11246 case CEIL_DIV_EXPR:
11247 case FLOOR_DIV_EXPR:
11248 case ROUND_DIV_EXPR:
11249 case EXACT_DIV_EXPR:
11250 may_need_excess_precision = true;
11251 break;
11253 case EQ_EXPR:
11254 case NE_EXPR:
11255 case LE_EXPR:
11256 case GE_EXPR:
11257 case LT_EXPR:
11258 case GT_EXPR:
11259 /* Excess precision for implicit conversions of integers to
11260 floating point in C11 and later. */
11261 may_need_excess_precision = (flag_isoc11
11262 && (ANY_INTEGRAL_TYPE_P (type0)
11263 || ANY_INTEGRAL_TYPE_P (type1)));
11264 break;
11266 default:
11267 may_need_excess_precision = false;
11268 break;
11270 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11272 op0 = TREE_OPERAND (op0, 0);
11273 type0 = TREE_TYPE (op0);
11275 else if (may_need_excess_precision
11276 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11278 type0 = eptype;
11279 op0 = convert (eptype, op0);
11281 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11283 op1 = TREE_OPERAND (op1, 0);
11284 type1 = TREE_TYPE (op1);
11286 else if (may_need_excess_precision
11287 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11289 type1 = eptype;
11290 op1 = convert (eptype, op1);
11293 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11295 /* In case when one of the operands of the binary operation is
11296 a vector and another is a scalar -- convert scalar to vector. */
11297 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11299 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11300 true);
11302 switch (convert_flag)
11304 case stv_error:
11305 return error_mark_node;
11306 case stv_firstarg:
11308 bool maybe_const = true;
11309 tree sc;
11310 sc = c_fully_fold (op0, false, &maybe_const);
11311 sc = save_expr (sc);
11312 sc = convert (TREE_TYPE (type1), sc);
11313 op0 = build_vector_from_val (type1, sc);
11314 if (!maybe_const)
11315 op0 = c_wrap_maybe_const (op0, true);
11316 orig_type0 = type0 = TREE_TYPE (op0);
11317 code0 = TREE_CODE (type0);
11318 converted = 1;
11319 break;
11321 case stv_secondarg:
11323 bool maybe_const = true;
11324 tree sc;
11325 sc = c_fully_fold (op1, false, &maybe_const);
11326 sc = save_expr (sc);
11327 sc = convert (TREE_TYPE (type0), sc);
11328 op1 = build_vector_from_val (type0, sc);
11329 if (!maybe_const)
11330 op1 = c_wrap_maybe_const (op1, true);
11331 orig_type1 = type1 = TREE_TYPE (op1);
11332 code1 = TREE_CODE (type1);
11333 converted = 1;
11334 break;
11336 default:
11337 break;
11341 switch (code)
11343 case PLUS_EXPR:
11344 /* Handle the pointer + int case. */
11345 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11347 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11348 goto return_build_binary_op;
11350 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11352 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11353 goto return_build_binary_op;
11355 else
11356 common = 1;
11357 break;
11359 case MINUS_EXPR:
11360 /* Subtraction of two similar pointers.
11361 We must subtract them as integers, then divide by object size. */
11362 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11363 && comp_target_types (location, type0, type1))
11365 ret = pointer_diff (location, op0, op1, &instrument_expr);
11366 goto return_build_binary_op;
11368 /* Handle pointer minus int. Just like pointer plus int. */
11369 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11371 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11372 goto return_build_binary_op;
11374 else
11375 common = 1;
11376 break;
11378 case MULT_EXPR:
11379 common = 1;
11380 break;
11382 case TRUNC_DIV_EXPR:
11383 case CEIL_DIV_EXPR:
11384 case FLOOR_DIV_EXPR:
11385 case ROUND_DIV_EXPR:
11386 case EXACT_DIV_EXPR:
11387 doing_div_or_mod = true;
11388 warn_for_div_by_zero (location, op1);
11390 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11391 || code0 == FIXED_POINT_TYPE
11392 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11393 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11394 || code1 == FIXED_POINT_TYPE
11395 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11397 enum tree_code tcode0 = code0, tcode1 = code1;
11399 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11400 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11401 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11402 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11404 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11405 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11406 resultcode = RDIV_EXPR;
11407 else
11408 /* Although it would be tempting to shorten always here, that
11409 loses on some targets, since the modulo instruction is
11410 undefined if the quotient can't be represented in the
11411 computation mode. We shorten only if unsigned or if
11412 dividing by something we know != -1. */
11413 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11414 || (TREE_CODE (op1) == INTEGER_CST
11415 && !integer_all_onesp (op1)));
11416 common = 1;
11418 break;
11420 case BIT_AND_EXPR:
11421 case BIT_IOR_EXPR:
11422 case BIT_XOR_EXPR:
11423 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11424 shorten = -1;
11425 /* Allow vector types which are not floating point types. */
11426 else if (code0 == VECTOR_TYPE
11427 && code1 == VECTOR_TYPE
11428 && !VECTOR_FLOAT_TYPE_P (type0)
11429 && !VECTOR_FLOAT_TYPE_P (type1))
11430 common = 1;
11431 break;
11433 case TRUNC_MOD_EXPR:
11434 case FLOOR_MOD_EXPR:
11435 doing_div_or_mod = true;
11436 warn_for_div_by_zero (location, op1);
11438 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11439 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11440 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11441 common = 1;
11442 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11444 /* Although it would be tempting to shorten always here, that loses
11445 on some targets, since the modulo instruction is undefined if the
11446 quotient can't be represented in the computation mode. We shorten
11447 only if unsigned or if dividing by something we know != -1. */
11448 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11449 || (TREE_CODE (op1) == INTEGER_CST
11450 && !integer_all_onesp (op1)));
11451 common = 1;
11453 break;
11455 case TRUTH_ANDIF_EXPR:
11456 case TRUTH_ORIF_EXPR:
11457 case TRUTH_AND_EXPR:
11458 case TRUTH_OR_EXPR:
11459 case TRUTH_XOR_EXPR:
11460 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11461 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11462 || code0 == FIXED_POINT_TYPE)
11463 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11464 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11465 || code1 == FIXED_POINT_TYPE))
11467 /* Result of these operations is always an int,
11468 but that does not mean the operands should be
11469 converted to ints! */
11470 result_type = integer_type_node;
11471 if (op0_int_operands)
11473 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11474 op0 = remove_c_maybe_const_expr (op0);
11476 else
11477 op0 = c_objc_common_truthvalue_conversion (location, op0);
11478 if (op1_int_operands)
11480 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11481 op1 = remove_c_maybe_const_expr (op1);
11483 else
11484 op1 = c_objc_common_truthvalue_conversion (location, op1);
11485 converted = 1;
11486 boolean_op = true;
11488 if (code == TRUTH_ANDIF_EXPR)
11490 int_const_or_overflow = (int_operands
11491 && TREE_CODE (orig_op0) == INTEGER_CST
11492 && (op0 == truthvalue_false_node
11493 || TREE_CODE (orig_op1) == INTEGER_CST));
11494 int_const = (int_const_or_overflow
11495 && !TREE_OVERFLOW (orig_op0)
11496 && (op0 == truthvalue_false_node
11497 || !TREE_OVERFLOW (orig_op1)));
11499 else if (code == TRUTH_ORIF_EXPR)
11501 int_const_or_overflow = (int_operands
11502 && TREE_CODE (orig_op0) == INTEGER_CST
11503 && (op0 == truthvalue_true_node
11504 || TREE_CODE (orig_op1) == INTEGER_CST));
11505 int_const = (int_const_or_overflow
11506 && !TREE_OVERFLOW (orig_op0)
11507 && (op0 == truthvalue_true_node
11508 || !TREE_OVERFLOW (orig_op1)));
11510 break;
11512 /* Shift operations: result has same type as first operand;
11513 always convert second operand to int.
11514 Also set SHORT_SHIFT if shifting rightward. */
11516 case RSHIFT_EXPR:
11517 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11518 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11519 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11520 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11521 TYPE_VECTOR_SUBPARTS (type1)))
11523 result_type = type0;
11524 converted = 1;
11526 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11527 || (code0 == VECTOR_TYPE
11528 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11529 && code1 == INTEGER_TYPE)
11531 doing_shift = true;
11532 if (TREE_CODE (op1) == INTEGER_CST)
11534 if (tree_int_cst_sgn (op1) < 0)
11536 int_const = false;
11537 if (c_inhibit_evaluation_warnings == 0)
11538 warning_at (location, OPT_Wshift_count_negative,
11539 "right shift count is negative");
11541 else if (code0 == VECTOR_TYPE)
11543 if (compare_tree_int (op1,
11544 TYPE_PRECISION (TREE_TYPE (type0)))
11545 >= 0)
11547 int_const = false;
11548 if (c_inhibit_evaluation_warnings == 0)
11549 warning_at (location, OPT_Wshift_count_overflow,
11550 "right shift count >= width of vector element");
11553 else
11555 if (!integer_zerop (op1))
11556 short_shift = 1;
11558 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11560 int_const = false;
11561 if (c_inhibit_evaluation_warnings == 0)
11562 warning_at (location, OPT_Wshift_count_overflow,
11563 "right shift count >= width of type");
11568 /* Use the type of the value to be shifted. */
11569 result_type = type0;
11570 /* Avoid converting op1 to result_type later. */
11571 converted = 1;
11573 break;
11575 case LSHIFT_EXPR:
11576 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11577 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11578 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11579 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11580 TYPE_VECTOR_SUBPARTS (type1)))
11582 result_type = type0;
11583 converted = 1;
11585 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11586 || (code0 == VECTOR_TYPE
11587 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11588 && code1 == INTEGER_TYPE)
11590 doing_shift = true;
11591 if (TREE_CODE (op0) == INTEGER_CST
11592 && tree_int_cst_sgn (op0) < 0)
11594 /* Don't reject a left shift of a negative value in a context
11595 where a constant expression is needed in C90. */
11596 if (flag_isoc99)
11597 int_const = false;
11598 if (c_inhibit_evaluation_warnings == 0)
11599 warning_at (location, OPT_Wshift_negative_value,
11600 "left shift of negative value");
11602 if (TREE_CODE (op1) == INTEGER_CST)
11604 if (tree_int_cst_sgn (op1) < 0)
11606 int_const = false;
11607 if (c_inhibit_evaluation_warnings == 0)
11608 warning_at (location, OPT_Wshift_count_negative,
11609 "left shift count is negative");
11611 else if (code0 == VECTOR_TYPE)
11613 if (compare_tree_int (op1,
11614 TYPE_PRECISION (TREE_TYPE (type0)))
11615 >= 0)
11617 int_const = false;
11618 if (c_inhibit_evaluation_warnings == 0)
11619 warning_at (location, OPT_Wshift_count_overflow,
11620 "left shift count >= width of vector element");
11623 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11625 int_const = false;
11626 if (c_inhibit_evaluation_warnings == 0)
11627 warning_at (location, OPT_Wshift_count_overflow,
11628 "left shift count >= width of type");
11630 else if (TREE_CODE (op0) == INTEGER_CST
11631 && maybe_warn_shift_overflow (location, op0, op1)
11632 && flag_isoc99)
11633 int_const = false;
11636 /* Use the type of the value to be shifted. */
11637 result_type = type0;
11638 /* Avoid converting op1 to result_type later. */
11639 converted = 1;
11641 break;
11643 case EQ_EXPR:
11644 case NE_EXPR:
11645 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11647 tree intt;
11648 if (!vector_types_compatible_elements_p (type0, type1))
11650 error_at (location, "comparing vectors with different "
11651 "element types");
11652 return error_mark_node;
11655 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11656 TYPE_VECTOR_SUBPARTS (type1)))
11658 error_at (location, "comparing vectors with different "
11659 "number of elements");
11660 return error_mark_node;
11663 /* It's not precisely specified how the usual arithmetic
11664 conversions apply to the vector types. Here, we use
11665 the unsigned type if one of the operands is signed and
11666 the other one is unsigned. */
11667 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11669 if (!TYPE_UNSIGNED (type0))
11670 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11671 else
11672 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11673 warning_at (location, OPT_Wsign_compare, "comparison between "
11674 "types %qT and %qT", type0, type1);
11677 /* Always construct signed integer vector type. */
11678 intt = c_common_type_for_size (GET_MODE_BITSIZE
11679 (SCALAR_TYPE_MODE
11680 (TREE_TYPE (type0))), 0);
11681 if (!intt)
11683 error_at (location, "could not find an integer type "
11684 "of the same size as %qT",
11685 TREE_TYPE (type0));
11686 return error_mark_node;
11688 result_type = build_opaque_vector_type (intt,
11689 TYPE_VECTOR_SUBPARTS (type0));
11690 converted = 1;
11691 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11692 goto return_build_binary_op;
11694 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11695 warning_at (location,
11696 OPT_Wfloat_equal,
11697 "comparing floating point with == or != is unsafe");
11698 /* Result of comparison is always int,
11699 but don't convert the args to int! */
11700 build_type = integer_type_node;
11701 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11702 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11703 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11704 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11705 short_compare = 1;
11706 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11708 if (TREE_CODE (op0) == ADDR_EXPR
11709 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11710 && !from_macro_expansion_at (location))
11712 if (code == EQ_EXPR)
11713 warning_at (location,
11714 OPT_Waddress,
11715 "the comparison will always evaluate as %<false%> "
11716 "for the address of %qD will never be NULL",
11717 TREE_OPERAND (op0, 0));
11718 else
11719 warning_at (location,
11720 OPT_Waddress,
11721 "the comparison will always evaluate as %<true%> "
11722 "for the address of %qD will never be NULL",
11723 TREE_OPERAND (op0, 0));
11725 result_type = type0;
11727 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11729 if (TREE_CODE (op1) == ADDR_EXPR
11730 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11731 && !from_macro_expansion_at (location))
11733 if (code == EQ_EXPR)
11734 warning_at (location,
11735 OPT_Waddress,
11736 "the comparison will always evaluate as %<false%> "
11737 "for the address of %qD will never be NULL",
11738 TREE_OPERAND (op1, 0));
11739 else
11740 warning_at (location,
11741 OPT_Waddress,
11742 "the comparison will always evaluate as %<true%> "
11743 "for the address of %qD will never be NULL",
11744 TREE_OPERAND (op1, 0));
11746 result_type = type1;
11748 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11750 tree tt0 = TREE_TYPE (type0);
11751 tree tt1 = TREE_TYPE (type1);
11752 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11753 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11754 addr_space_t as_common = ADDR_SPACE_GENERIC;
11756 /* Anything compares with void *. void * compares with anything.
11757 Otherwise, the targets must be compatible
11758 and both must be object or both incomplete. */
11759 if (comp_target_types (location, type0, type1))
11760 result_type = common_pointer_type (type0, type1);
11761 else if (!addr_space_superset (as0, as1, &as_common))
11763 error_at (location, "comparison of pointers to "
11764 "disjoint address spaces");
11765 return error_mark_node;
11767 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11769 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11770 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11771 "comparison of %<void *%> with function pointer");
11773 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11775 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11776 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11777 "comparison of %<void *%> with function pointer");
11779 else
11780 /* Avoid warning about the volatile ObjC EH puts on decls. */
11781 if (!objc_ok)
11782 pedwarn (location, 0,
11783 "comparison of distinct pointer types lacks a cast");
11785 if (result_type == NULL_TREE)
11787 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11788 result_type = build_pointer_type
11789 (build_qualified_type (void_type_node, qual));
11792 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11794 result_type = type0;
11795 pedwarn (location, 0, "comparison between pointer and integer");
11797 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11799 result_type = type1;
11800 pedwarn (location, 0, "comparison between pointer and integer");
11802 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11803 || truth_value_p (TREE_CODE (orig_op0)))
11804 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11805 || truth_value_p (TREE_CODE (orig_op1))))
11806 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11807 break;
11809 case LE_EXPR:
11810 case GE_EXPR:
11811 case LT_EXPR:
11812 case GT_EXPR:
11813 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11815 tree intt;
11816 if (!vector_types_compatible_elements_p (type0, type1))
11818 error_at (location, "comparing vectors with different "
11819 "element types");
11820 return error_mark_node;
11823 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11824 TYPE_VECTOR_SUBPARTS (type1)))
11826 error_at (location, "comparing vectors with different "
11827 "number of elements");
11828 return error_mark_node;
11831 /* It's not precisely specified how the usual arithmetic
11832 conversions apply to the vector types. Here, we use
11833 the unsigned type if one of the operands is signed and
11834 the other one is unsigned. */
11835 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11837 if (!TYPE_UNSIGNED (type0))
11838 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11839 else
11840 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11841 warning_at (location, OPT_Wsign_compare, "comparison between "
11842 "types %qT and %qT", type0, type1);
11845 /* Always construct signed integer vector type. */
11846 intt = c_common_type_for_size (GET_MODE_BITSIZE
11847 (SCALAR_TYPE_MODE
11848 (TREE_TYPE (type0))), 0);
11849 if (!intt)
11851 error_at (location, "could not find an integer type "
11852 "of the same size as %qT",
11853 TREE_TYPE (type0));
11854 return error_mark_node;
11856 result_type = build_opaque_vector_type (intt,
11857 TYPE_VECTOR_SUBPARTS (type0));
11858 converted = 1;
11859 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11860 goto return_build_binary_op;
11862 build_type = integer_type_node;
11863 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11864 || code0 == FIXED_POINT_TYPE)
11865 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11866 || code1 == FIXED_POINT_TYPE))
11867 short_compare = 1;
11868 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11870 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11871 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11872 addr_space_t as_common;
11874 if (comp_target_types (location, type0, type1))
11876 result_type = common_pointer_type (type0, type1);
11877 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11878 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11879 pedwarn (location, 0,
11880 "comparison of complete and incomplete pointers");
11881 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11882 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11883 "ordered comparisons of pointers to functions");
11884 else if (null_pointer_constant_p (orig_op0)
11885 || null_pointer_constant_p (orig_op1))
11886 warning_at (location, OPT_Wextra,
11887 "ordered comparison of pointer with null pointer");
11890 else if (!addr_space_superset (as0, as1, &as_common))
11892 error_at (location, "comparison of pointers to "
11893 "disjoint address spaces");
11894 return error_mark_node;
11896 else
11898 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11899 result_type = build_pointer_type
11900 (build_qualified_type (void_type_node, qual));
11901 pedwarn (location, 0,
11902 "comparison of distinct pointer types lacks a cast");
11905 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11907 result_type = type0;
11908 if (pedantic)
11909 pedwarn (location, OPT_Wpedantic,
11910 "ordered comparison of pointer with integer zero");
11911 else if (extra_warnings)
11912 warning_at (location, OPT_Wextra,
11913 "ordered comparison of pointer with integer zero");
11915 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11917 result_type = type1;
11918 if (pedantic)
11919 pedwarn (location, OPT_Wpedantic,
11920 "ordered comparison of pointer with integer zero");
11921 else if (extra_warnings)
11922 warning_at (location, OPT_Wextra,
11923 "ordered comparison of pointer with integer zero");
11925 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11927 result_type = type0;
11928 pedwarn (location, 0, "comparison between pointer and integer");
11930 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11932 result_type = type1;
11933 pedwarn (location, 0, "comparison between pointer and integer");
11936 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
11937 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
11939 op0 = save_expr (op0);
11940 op1 = save_expr (op1);
11942 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
11943 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
11946 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11947 || truth_value_p (TREE_CODE (orig_op0)))
11948 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11949 || truth_value_p (TREE_CODE (orig_op1))))
11950 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11951 break;
11953 default:
11954 gcc_unreachable ();
11957 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11958 return error_mark_node;
11960 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11961 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11962 || !vector_types_compatible_elements_p (type0, type1)))
11964 gcc_rich_location richloc (location);
11965 maybe_range_label_for_tree_type_mismatch
11966 label_for_op0 (orig_op0, orig_op1),
11967 label_for_op1 (orig_op1, orig_op0);
11968 richloc.maybe_add_expr (orig_op0, &label_for_op0);
11969 richloc.maybe_add_expr (orig_op1, &label_for_op1);
11970 binary_op_error (&richloc, code, type0, type1);
11971 return error_mark_node;
11974 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11975 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11977 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11978 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11980 bool first_complex = (code0 == COMPLEX_TYPE);
11981 bool second_complex = (code1 == COMPLEX_TYPE);
11982 int none_complex = (!first_complex && !second_complex);
11984 if (shorten || common || short_compare)
11986 result_type = c_common_type (type0, type1);
11987 do_warn_double_promotion (result_type, type0, type1,
11988 "implicit conversion from %qT to %qT "
11989 "to match other operand of binary "
11990 "expression",
11991 location);
11992 if (result_type == error_mark_node)
11993 return error_mark_node;
11996 if (first_complex != second_complex
11997 && (code == PLUS_EXPR
11998 || code == MINUS_EXPR
11999 || code == MULT_EXPR
12000 || (code == TRUNC_DIV_EXPR && first_complex))
12001 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12002 && flag_signed_zeros)
12004 /* An operation on mixed real/complex operands must be
12005 handled specially, but the language-independent code can
12006 more easily optimize the plain complex arithmetic if
12007 -fno-signed-zeros. */
12008 tree real_type = TREE_TYPE (result_type);
12009 tree real, imag;
12010 if (type0 != orig_type0 || type1 != orig_type1)
12012 gcc_assert (may_need_excess_precision && common);
12013 semantic_result_type = c_common_type (orig_type0, orig_type1);
12015 if (first_complex)
12017 if (TREE_TYPE (op0) != result_type)
12018 op0 = convert_and_check (location, result_type, op0);
12019 if (TREE_TYPE (op1) != real_type)
12020 op1 = convert_and_check (location, real_type, op1);
12022 else
12024 if (TREE_TYPE (op0) != real_type)
12025 op0 = convert_and_check (location, real_type, op0);
12026 if (TREE_TYPE (op1) != result_type)
12027 op1 = convert_and_check (location, result_type, op1);
12029 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12030 return error_mark_node;
12031 if (first_complex)
12033 op0 = save_expr (op0);
12034 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12035 op0, true);
12036 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12037 op0, true);
12038 switch (code)
12040 case MULT_EXPR:
12041 case TRUNC_DIV_EXPR:
12042 op1 = save_expr (op1);
12043 imag = build2 (resultcode, real_type, imag, op1);
12044 /* Fall through. */
12045 case PLUS_EXPR:
12046 case MINUS_EXPR:
12047 real = build2 (resultcode, real_type, real, op1);
12048 break;
12049 default:
12050 gcc_unreachable();
12053 else
12055 op1 = save_expr (op1);
12056 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12057 op1, true);
12058 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12059 op1, true);
12060 switch (code)
12062 case MULT_EXPR:
12063 op0 = save_expr (op0);
12064 imag = build2 (resultcode, real_type, op0, imag);
12065 /* Fall through. */
12066 case PLUS_EXPR:
12067 real = build2 (resultcode, real_type, op0, real);
12068 break;
12069 case MINUS_EXPR:
12070 real = build2 (resultcode, real_type, op0, real);
12071 imag = build1 (NEGATE_EXPR, real_type, imag);
12072 break;
12073 default:
12074 gcc_unreachable();
12077 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12078 goto return_build_binary_op;
12081 /* For certain operations (which identify themselves by shorten != 0)
12082 if both args were extended from the same smaller type,
12083 do the arithmetic in that type and then extend.
12085 shorten !=0 and !=1 indicates a bitwise operation.
12086 For them, this optimization is safe only if
12087 both args are zero-extended or both are sign-extended.
12088 Otherwise, we might change the result.
12089 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12090 but calculated in (unsigned short) it would be (unsigned short)-1. */
12092 if (shorten && none_complex)
12094 final_type = result_type;
12095 result_type = shorten_binary_op (result_type, op0, op1,
12096 shorten == -1);
12099 /* Shifts can be shortened if shifting right. */
12101 if (short_shift)
12103 int unsigned_arg;
12104 tree arg0 = get_narrower (op0, &unsigned_arg);
12106 final_type = result_type;
12108 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12109 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12111 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12112 && tree_int_cst_sgn (op1) > 0
12113 /* We can shorten only if the shift count is less than the
12114 number of bits in the smaller type size. */
12115 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12116 /* We cannot drop an unsigned shift after sign-extension. */
12117 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12119 /* Do an unsigned shift if the operand was zero-extended. */
12120 result_type
12121 = c_common_signed_or_unsigned_type (unsigned_arg,
12122 TREE_TYPE (arg0));
12123 /* Convert value-to-be-shifted to that type. */
12124 if (TREE_TYPE (op0) != result_type)
12125 op0 = convert (result_type, op0);
12126 converted = 1;
12130 /* Comparison operations are shortened too but differently.
12131 They identify themselves by setting short_compare = 1. */
12133 if (short_compare)
12135 /* Don't write &op0, etc., because that would prevent op0
12136 from being kept in a register.
12137 Instead, make copies of the our local variables and
12138 pass the copies by reference, then copy them back afterward. */
12139 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12140 enum tree_code xresultcode = resultcode;
12141 tree val
12142 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12143 &xresultcode);
12145 if (val != NULL_TREE)
12147 ret = val;
12148 goto return_build_binary_op;
12151 op0 = xop0, op1 = xop1;
12152 converted = 1;
12153 resultcode = xresultcode;
12155 if (c_inhibit_evaluation_warnings == 0)
12157 bool op0_maybe_const = true;
12158 bool op1_maybe_const = true;
12159 tree orig_op0_folded, orig_op1_folded;
12161 if (in_late_binary_op)
12163 orig_op0_folded = orig_op0;
12164 orig_op1_folded = orig_op1;
12166 else
12168 /* Fold for the sake of possible warnings, as in
12169 build_conditional_expr. This requires the
12170 "original" values to be folded, not just op0 and
12171 op1. */
12172 c_inhibit_evaluation_warnings++;
12173 op0 = c_fully_fold (op0, require_constant_value,
12174 &op0_maybe_const);
12175 op1 = c_fully_fold (op1, require_constant_value,
12176 &op1_maybe_const);
12177 c_inhibit_evaluation_warnings--;
12178 orig_op0_folded = c_fully_fold (orig_op0,
12179 require_constant_value,
12180 NULL);
12181 orig_op1_folded = c_fully_fold (orig_op1,
12182 require_constant_value,
12183 NULL);
12186 if (warn_sign_compare)
12187 warn_for_sign_compare (location, orig_op0_folded,
12188 orig_op1_folded, op0, op1,
12189 result_type, resultcode);
12190 if (!in_late_binary_op && !int_operands)
12192 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12193 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12194 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12195 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12201 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12202 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12203 Then the expression will be built.
12204 It will be given type FINAL_TYPE if that is nonzero;
12205 otherwise, it will be given type RESULT_TYPE. */
12207 if (!result_type)
12209 gcc_rich_location richloc (location);
12210 maybe_range_label_for_tree_type_mismatch
12211 label_for_op0 (orig_op0, orig_op1),
12212 label_for_op1 (orig_op1, orig_op0);
12213 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12214 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12215 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12216 return error_mark_node;
12219 if (build_type == NULL_TREE)
12221 build_type = result_type;
12222 if ((type0 != orig_type0 || type1 != orig_type1)
12223 && !boolean_op)
12225 gcc_assert (may_need_excess_precision && common);
12226 semantic_result_type = c_common_type (orig_type0, orig_type1);
12230 if (!converted)
12232 op0 = ep_convert_and_check (location, result_type, op0,
12233 semantic_result_type);
12234 op1 = ep_convert_and_check (location, result_type, op1,
12235 semantic_result_type);
12237 /* This can happen if one operand has a vector type, and the other
12238 has a different type. */
12239 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12240 return error_mark_node;
12243 if (sanitize_flags_p ((SANITIZE_SHIFT
12244 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12245 && current_function_decl != NULL_TREE
12246 && (doing_div_or_mod || doing_shift)
12247 && !require_constant_value)
12249 /* OP0 and/or OP1 might have side-effects. */
12250 op0 = save_expr (op0);
12251 op1 = save_expr (op1);
12252 op0 = c_fully_fold (op0, false, NULL);
12253 op1 = c_fully_fold (op1, false, NULL);
12254 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12255 | SANITIZE_FLOAT_DIVIDE))))
12256 instrument_expr = ubsan_instrument_division (location, op0, op1);
12257 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12258 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12261 /* Treat expressions in initializers specially as they can't trap. */
12262 if (int_const_or_overflow)
12263 ret = (require_constant_value
12264 ? fold_build2_initializer_loc (location, resultcode, build_type,
12265 op0, op1)
12266 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12267 else
12268 ret = build2 (resultcode, build_type, op0, op1);
12269 if (final_type != NULL_TREE)
12270 ret = convert (final_type, ret);
12272 return_build_binary_op:
12273 gcc_assert (ret != error_mark_node);
12274 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12275 ret = (int_operands
12276 ? note_integer_operands (ret)
12277 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12278 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12279 && !in_late_binary_op)
12280 ret = note_integer_operands (ret);
12281 protected_set_expr_location (ret, location);
12283 if (instrument_expr != NULL)
12284 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12285 instrument_expr, ret);
12287 if (semantic_result_type)
12288 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12289 semantic_result_type, ret);
12291 return ret;
12295 /* Convert EXPR to be a truth-value, validating its type for this
12296 purpose. LOCATION is the source location for the expression. */
12298 tree
12299 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12301 bool int_const, int_operands;
12303 switch (TREE_CODE (TREE_TYPE (expr)))
12305 case ARRAY_TYPE:
12306 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12307 return error_mark_node;
12309 case RECORD_TYPE:
12310 error_at (location, "used struct type value where scalar is required");
12311 return error_mark_node;
12313 case UNION_TYPE:
12314 error_at (location, "used union type value where scalar is required");
12315 return error_mark_node;
12317 case VOID_TYPE:
12318 error_at (location, "void value not ignored as it ought to be");
12319 return error_mark_node;
12321 case POINTER_TYPE:
12322 if (reject_gcc_builtin (expr))
12323 return error_mark_node;
12324 break;
12326 case FUNCTION_TYPE:
12327 gcc_unreachable ();
12329 case VECTOR_TYPE:
12330 error_at (location, "used vector type where scalar is required");
12331 return error_mark_node;
12333 default:
12334 break;
12337 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12338 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12339 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12341 expr = remove_c_maybe_const_expr (expr);
12342 expr = build2 (NE_EXPR, integer_type_node, expr,
12343 convert (TREE_TYPE (expr), integer_zero_node));
12344 expr = note_integer_operands (expr);
12346 else
12347 /* ??? Should we also give an error for vectors rather than leaving
12348 those to give errors later? */
12349 expr = c_common_truthvalue_conversion (location, expr);
12351 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12353 if (TREE_OVERFLOW (expr))
12354 return expr;
12355 else
12356 return note_integer_operands (expr);
12358 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12359 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12360 return expr;
12364 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12365 required. */
12367 tree
12368 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12370 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12372 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12373 /* Executing a compound literal inside a function reinitializes
12374 it. */
12375 if (!TREE_STATIC (decl))
12376 *se = true;
12377 return decl;
12379 else
12380 return expr;
12383 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12384 statement. LOC is the location of the construct. */
12386 tree
12387 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12388 tree clauses)
12390 body = c_end_compound_stmt (loc, body, true);
12392 tree stmt = make_node (code);
12393 TREE_TYPE (stmt) = void_type_node;
12394 OMP_BODY (stmt) = body;
12395 OMP_CLAUSES (stmt) = clauses;
12396 SET_EXPR_LOCATION (stmt, loc);
12398 return add_stmt (stmt);
12401 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12402 statement. LOC is the location of the OACC_DATA. */
12404 tree
12405 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12407 tree stmt;
12409 block = c_end_compound_stmt (loc, block, true);
12411 stmt = make_node (OACC_DATA);
12412 TREE_TYPE (stmt) = void_type_node;
12413 OACC_DATA_CLAUSES (stmt) = clauses;
12414 OACC_DATA_BODY (stmt) = block;
12415 SET_EXPR_LOCATION (stmt, loc);
12417 return add_stmt (stmt);
12420 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12421 statement. LOC is the location of the OACC_HOST_DATA. */
12423 tree
12424 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12426 tree stmt;
12428 block = c_end_compound_stmt (loc, block, true);
12430 stmt = make_node (OACC_HOST_DATA);
12431 TREE_TYPE (stmt) = void_type_node;
12432 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12433 OACC_HOST_DATA_BODY (stmt) = block;
12434 SET_EXPR_LOCATION (stmt, loc);
12436 return add_stmt (stmt);
12439 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12441 tree
12442 c_begin_omp_parallel (void)
12444 tree block;
12446 keep_next_level ();
12447 block = c_begin_compound_stmt (true);
12449 return block;
12452 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12453 statement. LOC is the location of the OMP_PARALLEL. */
12455 tree
12456 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12458 tree stmt;
12460 block = c_end_compound_stmt (loc, block, true);
12462 stmt = make_node (OMP_PARALLEL);
12463 TREE_TYPE (stmt) = void_type_node;
12464 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12465 OMP_PARALLEL_BODY (stmt) = block;
12466 SET_EXPR_LOCATION (stmt, loc);
12468 return add_stmt (stmt);
12471 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12473 tree
12474 c_begin_omp_task (void)
12476 tree block;
12478 keep_next_level ();
12479 block = c_begin_compound_stmt (true);
12481 return block;
12484 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12485 statement. LOC is the location of the #pragma. */
12487 tree
12488 c_finish_omp_task (location_t loc, tree clauses, tree block)
12490 tree stmt;
12492 block = c_end_compound_stmt (loc, block, true);
12494 stmt = make_node (OMP_TASK);
12495 TREE_TYPE (stmt) = void_type_node;
12496 OMP_TASK_CLAUSES (stmt) = clauses;
12497 OMP_TASK_BODY (stmt) = block;
12498 SET_EXPR_LOCATION (stmt, loc);
12500 return add_stmt (stmt);
12503 /* Generate GOMP_cancel call for #pragma omp cancel. */
12505 void
12506 c_finish_omp_cancel (location_t loc, tree clauses)
12508 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12509 int mask = 0;
12510 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12511 mask = 1;
12512 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12513 mask = 2;
12514 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12515 mask = 4;
12516 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12517 mask = 8;
12518 else
12520 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12521 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12522 "clauses");
12523 return;
12525 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12526 if (ifc != NULL_TREE)
12528 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12529 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12530 error_at (OMP_CLAUSE_LOCATION (ifc),
12531 "expected %<cancel%> %<if%> clause modifier");
12533 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12534 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12535 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12536 build_zero_cst (type));
12538 else
12539 ifc = boolean_true_node;
12540 tree stmt = build_call_expr_loc (loc, fn, 2,
12541 build_int_cst (integer_type_node, mask),
12542 ifc);
12543 add_stmt (stmt);
12546 /* Generate GOMP_cancellation_point call for
12547 #pragma omp cancellation point. */
12549 void
12550 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12552 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12553 int mask = 0;
12554 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12555 mask = 1;
12556 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12557 mask = 2;
12558 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12559 mask = 4;
12560 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12561 mask = 8;
12562 else
12564 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12565 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12566 "clauses");
12567 return;
12569 tree stmt = build_call_expr_loc (loc, fn, 1,
12570 build_int_cst (integer_type_node, mask));
12571 add_stmt (stmt);
12574 /* Helper function for handle_omp_array_sections. Called recursively
12575 to handle multiple array-section-subscripts. C is the clause,
12576 T current expression (initially OMP_CLAUSE_DECL), which is either
12577 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12578 expression if specified, TREE_VALUE length expression if specified,
12579 TREE_CHAIN is what it has been specified after, or some decl.
12580 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12581 set to true if any of the array-section-subscript could have length
12582 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12583 first array-section-subscript which is known not to have length
12584 of one. Given say:
12585 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12586 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12587 all are or may have length of 1, array-section-subscript [:2] is the
12588 first one known not to have length 1. For array-section-subscript
12589 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12590 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12591 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12592 case though, as some lengths could be zero. */
12594 static tree
12595 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12596 bool &maybe_zero_len, unsigned int &first_non_one,
12597 enum c_omp_region_type ort)
12599 tree ret, low_bound, length, type;
12600 if (TREE_CODE (t) != TREE_LIST)
12602 if (error_operand_p (t))
12603 return error_mark_node;
12604 ret = t;
12605 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12606 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12608 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12609 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12610 return error_mark_node;
12612 if (TREE_CODE (t) == COMPONENT_REF
12613 && ort == C_ORT_OMP
12614 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12615 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12616 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12618 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12620 error_at (OMP_CLAUSE_LOCATION (c),
12621 "bit-field %qE in %qs clause",
12622 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12623 return error_mark_node;
12625 while (TREE_CODE (t) == COMPONENT_REF)
12627 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12629 error_at (OMP_CLAUSE_LOCATION (c),
12630 "%qE is a member of a union", t);
12631 return error_mark_node;
12633 t = TREE_OPERAND (t, 0);
12636 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12638 if (DECL_P (t))
12639 error_at (OMP_CLAUSE_LOCATION (c),
12640 "%qD is not a variable in %qs clause", t,
12641 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12642 else
12643 error_at (OMP_CLAUSE_LOCATION (c),
12644 "%qE is not a variable in %qs clause", t,
12645 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12646 return error_mark_node;
12648 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12649 && TYPE_ATOMIC (TREE_TYPE (t)))
12651 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12652 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12653 return error_mark_node;
12655 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12656 && VAR_P (t)
12657 && DECL_THREAD_LOCAL_P (t))
12659 error_at (OMP_CLAUSE_LOCATION (c),
12660 "%qD is threadprivate variable in %qs clause", t,
12661 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12662 return error_mark_node;
12664 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12665 && TYPE_ATOMIC (TREE_TYPE (t))
12666 && POINTER_TYPE_P (TREE_TYPE (t)))
12668 /* If the array section is pointer based and the pointer
12669 itself is _Atomic qualified, we need to atomically load
12670 the pointer. */
12671 c_expr expr;
12672 memset (&expr, 0, sizeof (expr));
12673 expr.value = ret;
12674 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12675 expr, false, false);
12676 ret = expr.value;
12678 return ret;
12681 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12682 maybe_zero_len, first_non_one, ort);
12683 if (ret == error_mark_node || ret == NULL_TREE)
12684 return ret;
12686 type = TREE_TYPE (ret);
12687 low_bound = TREE_PURPOSE (t);
12688 length = TREE_VALUE (t);
12690 if (low_bound == error_mark_node || length == error_mark_node)
12691 return error_mark_node;
12693 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12695 error_at (OMP_CLAUSE_LOCATION (c),
12696 "low bound %qE of array section does not have integral type",
12697 low_bound);
12698 return error_mark_node;
12700 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12702 error_at (OMP_CLAUSE_LOCATION (c),
12703 "length %qE of array section does not have integral type",
12704 length);
12705 return error_mark_node;
12707 if (low_bound
12708 && TREE_CODE (low_bound) == INTEGER_CST
12709 && TYPE_PRECISION (TREE_TYPE (low_bound))
12710 > TYPE_PRECISION (sizetype))
12711 low_bound = fold_convert (sizetype, low_bound);
12712 if (length
12713 && TREE_CODE (length) == INTEGER_CST
12714 && TYPE_PRECISION (TREE_TYPE (length))
12715 > TYPE_PRECISION (sizetype))
12716 length = fold_convert (sizetype, length);
12717 if (low_bound == NULL_TREE)
12718 low_bound = integer_zero_node;
12720 if (length != NULL_TREE)
12722 if (!integer_nonzerop (length))
12724 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12725 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12726 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
12727 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
12729 if (integer_zerop (length))
12731 error_at (OMP_CLAUSE_LOCATION (c),
12732 "zero length array section in %qs clause",
12733 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12734 return error_mark_node;
12737 else
12738 maybe_zero_len = true;
12740 if (first_non_one == types.length ()
12741 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12742 first_non_one++;
12744 if (TREE_CODE (type) == ARRAY_TYPE)
12746 if (length == NULL_TREE
12747 && (TYPE_DOMAIN (type) == NULL_TREE
12748 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12750 error_at (OMP_CLAUSE_LOCATION (c),
12751 "for unknown bound array type length expression must "
12752 "be specified");
12753 return error_mark_node;
12755 if (TREE_CODE (low_bound) == INTEGER_CST
12756 && tree_int_cst_sgn (low_bound) == -1)
12758 error_at (OMP_CLAUSE_LOCATION (c),
12759 "negative low bound in array section in %qs clause",
12760 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12761 return error_mark_node;
12763 if (length != NULL_TREE
12764 && TREE_CODE (length) == INTEGER_CST
12765 && tree_int_cst_sgn (length) == -1)
12767 error_at (OMP_CLAUSE_LOCATION (c),
12768 "negative length in array section in %qs clause",
12769 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12770 return error_mark_node;
12772 if (TYPE_DOMAIN (type)
12773 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12774 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12775 == INTEGER_CST)
12777 tree size
12778 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12779 size = size_binop (PLUS_EXPR, size, size_one_node);
12780 if (TREE_CODE (low_bound) == INTEGER_CST)
12782 if (tree_int_cst_lt (size, low_bound))
12784 error_at (OMP_CLAUSE_LOCATION (c),
12785 "low bound %qE above array section size "
12786 "in %qs clause", low_bound,
12787 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12788 return error_mark_node;
12790 if (tree_int_cst_equal (size, low_bound))
12792 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12793 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12794 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
12795 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
12797 error_at (OMP_CLAUSE_LOCATION (c),
12798 "zero length array section in %qs clause",
12799 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12800 return error_mark_node;
12802 maybe_zero_len = true;
12804 else if (length == NULL_TREE
12805 && first_non_one == types.length ()
12806 && tree_int_cst_equal
12807 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12808 low_bound))
12809 first_non_one++;
12811 else if (length == NULL_TREE)
12813 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12814 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
12815 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
12816 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
12817 maybe_zero_len = true;
12818 if (first_non_one == types.length ())
12819 first_non_one++;
12821 if (length && TREE_CODE (length) == INTEGER_CST)
12823 if (tree_int_cst_lt (size, length))
12825 error_at (OMP_CLAUSE_LOCATION (c),
12826 "length %qE above array section size "
12827 "in %qs clause", length,
12828 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12829 return error_mark_node;
12831 if (TREE_CODE (low_bound) == INTEGER_CST)
12833 tree lbpluslen
12834 = size_binop (PLUS_EXPR,
12835 fold_convert (sizetype, low_bound),
12836 fold_convert (sizetype, length));
12837 if (TREE_CODE (lbpluslen) == INTEGER_CST
12838 && tree_int_cst_lt (size, lbpluslen))
12840 error_at (OMP_CLAUSE_LOCATION (c),
12841 "high bound %qE above array section size "
12842 "in %qs clause", lbpluslen,
12843 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12844 return error_mark_node;
12849 else if (length == NULL_TREE)
12851 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12852 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
12853 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
12854 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
12855 maybe_zero_len = true;
12856 if (first_non_one == types.length ())
12857 first_non_one++;
12860 /* For [lb:] we will need to evaluate lb more than once. */
12861 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12863 tree lb = save_expr (low_bound);
12864 if (lb != low_bound)
12866 TREE_PURPOSE (t) = lb;
12867 low_bound = lb;
12871 else if (TREE_CODE (type) == POINTER_TYPE)
12873 if (length == NULL_TREE)
12875 error_at (OMP_CLAUSE_LOCATION (c),
12876 "for pointer type length expression must be specified");
12877 return error_mark_node;
12879 if (length != NULL_TREE
12880 && TREE_CODE (length) == INTEGER_CST
12881 && tree_int_cst_sgn (length) == -1)
12883 error_at (OMP_CLAUSE_LOCATION (c),
12884 "negative length in array section in %qs clause",
12885 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12886 return error_mark_node;
12888 /* If there is a pointer type anywhere but in the very first
12889 array-section-subscript, the array section can't be contiguous. */
12890 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12891 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12893 error_at (OMP_CLAUSE_LOCATION (c),
12894 "array section is not contiguous in %qs clause",
12895 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12896 return error_mark_node;
12899 else
12901 error_at (OMP_CLAUSE_LOCATION (c),
12902 "%qE does not have pointer or array type", ret);
12903 return error_mark_node;
12905 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12906 types.safe_push (TREE_TYPE (ret));
12907 /* We will need to evaluate lb more than once. */
12908 tree lb = save_expr (low_bound);
12909 if (lb != low_bound)
12911 TREE_PURPOSE (t) = lb;
12912 low_bound = lb;
12914 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12915 return ret;
12918 /* Handle array sections for clause C. */
12920 static bool
12921 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12923 bool maybe_zero_len = false;
12924 unsigned int first_non_one = 0;
12925 auto_vec<tree, 10> types;
12926 tree *tp = &OMP_CLAUSE_DECL (c);
12927 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12928 && TREE_CODE (*tp) == TREE_LIST
12929 && TREE_PURPOSE (*tp)
12930 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
12931 tp = &TREE_VALUE (*tp);
12932 tree first = handle_omp_array_sections_1 (c, *tp, types,
12933 maybe_zero_len, first_non_one,
12934 ort);
12935 if (first == error_mark_node)
12936 return true;
12937 if (first == NULL_TREE)
12938 return false;
12939 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12941 tree t = *tp;
12942 tree tem = NULL_TREE;
12943 /* Need to evaluate side effects in the length expressions
12944 if any. */
12945 while (TREE_CODE (t) == TREE_LIST)
12947 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12949 if (tem == NULL_TREE)
12950 tem = TREE_VALUE (t);
12951 else
12952 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12953 TREE_VALUE (t), tem);
12955 t = TREE_CHAIN (t);
12957 if (tem)
12958 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12959 first = c_fully_fold (first, false, NULL, true);
12960 *tp = first;
12962 else
12964 unsigned int num = types.length (), i;
12965 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12966 tree condition = NULL_TREE;
12968 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12969 maybe_zero_len = true;
12971 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12972 t = TREE_CHAIN (t))
12974 tree low_bound = TREE_PURPOSE (t);
12975 tree length = TREE_VALUE (t);
12977 i--;
12978 if (low_bound
12979 && TREE_CODE (low_bound) == INTEGER_CST
12980 && TYPE_PRECISION (TREE_TYPE (low_bound))
12981 > TYPE_PRECISION (sizetype))
12982 low_bound = fold_convert (sizetype, low_bound);
12983 if (length
12984 && TREE_CODE (length) == INTEGER_CST
12985 && TYPE_PRECISION (TREE_TYPE (length))
12986 > TYPE_PRECISION (sizetype))
12987 length = fold_convert (sizetype, length);
12988 if (low_bound == NULL_TREE)
12989 low_bound = integer_zero_node;
12990 if (!maybe_zero_len && i > first_non_one)
12992 if (integer_nonzerop (low_bound))
12993 goto do_warn_noncontiguous;
12994 if (length != NULL_TREE
12995 && TREE_CODE (length) == INTEGER_CST
12996 && TYPE_DOMAIN (types[i])
12997 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12998 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12999 == INTEGER_CST)
13001 tree size;
13002 size = size_binop (PLUS_EXPR,
13003 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13004 size_one_node);
13005 if (!tree_int_cst_equal (length, size))
13007 do_warn_noncontiguous:
13008 error_at (OMP_CLAUSE_LOCATION (c),
13009 "array section is not contiguous in %qs "
13010 "clause",
13011 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13012 return true;
13015 if (length != NULL_TREE
13016 && TREE_SIDE_EFFECTS (length))
13018 if (side_effects == NULL_TREE)
13019 side_effects = length;
13020 else
13021 side_effects = build2 (COMPOUND_EXPR,
13022 TREE_TYPE (side_effects),
13023 length, side_effects);
13026 else
13028 tree l;
13030 if (i > first_non_one
13031 && ((length && integer_nonzerop (length))
13032 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13033 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13034 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13035 continue;
13036 if (length)
13037 l = fold_convert (sizetype, length);
13038 else
13040 l = size_binop (PLUS_EXPR,
13041 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13042 size_one_node);
13043 l = size_binop (MINUS_EXPR, l,
13044 fold_convert (sizetype, low_bound));
13046 if (i > first_non_one)
13048 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13049 size_zero_node);
13050 if (condition == NULL_TREE)
13051 condition = l;
13052 else
13053 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13054 l, condition);
13056 else if (size == NULL_TREE)
13058 size = size_in_bytes (TREE_TYPE (types[i]));
13059 tree eltype = TREE_TYPE (types[num - 1]);
13060 while (TREE_CODE (eltype) == ARRAY_TYPE)
13061 eltype = TREE_TYPE (eltype);
13062 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13063 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13064 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13066 if (integer_zerop (size)
13067 || integer_zerop (size_in_bytes (eltype)))
13069 error_at (OMP_CLAUSE_LOCATION (c),
13070 "zero length array section in %qs clause",
13071 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13072 return error_mark_node;
13074 size = size_binop (EXACT_DIV_EXPR, size,
13075 size_in_bytes (eltype));
13077 size = size_binop (MULT_EXPR, size, l);
13078 if (condition)
13079 size = fold_build3 (COND_EXPR, sizetype, condition,
13080 size, size_zero_node);
13082 else
13083 size = size_binop (MULT_EXPR, size, l);
13086 if (side_effects)
13087 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13088 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13089 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13090 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13092 size = size_binop (MINUS_EXPR, size, size_one_node);
13093 size = c_fully_fold (size, false, NULL);
13094 size = save_expr (size);
13095 tree index_type = build_index_type (size);
13096 tree eltype = TREE_TYPE (first);
13097 while (TREE_CODE (eltype) == ARRAY_TYPE)
13098 eltype = TREE_TYPE (eltype);
13099 tree type = build_array_type (eltype, index_type);
13100 tree ptype = build_pointer_type (eltype);
13101 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13102 t = build_fold_addr_expr (t);
13103 tree t2 = build_fold_addr_expr (first);
13104 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13105 ptrdiff_type_node, t2);
13106 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13107 ptrdiff_type_node, t2,
13108 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13109 ptrdiff_type_node, t));
13110 t2 = c_fully_fold (t2, false, NULL);
13111 if (tree_fits_shwi_p (t2))
13112 t = build2 (MEM_REF, type, t,
13113 build_int_cst (ptype, tree_to_shwi (t2)));
13114 else
13116 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13117 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13118 TREE_TYPE (t), t, t2);
13119 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13121 OMP_CLAUSE_DECL (c) = t;
13122 return false;
13124 first = c_fully_fold (first, false, NULL);
13125 OMP_CLAUSE_DECL (c) = first;
13126 if (size)
13127 size = c_fully_fold (size, false, NULL);
13128 OMP_CLAUSE_SIZE (c) = size;
13129 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13130 || (TREE_CODE (t) == COMPONENT_REF
13131 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13132 return false;
13133 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13134 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13135 switch (OMP_CLAUSE_MAP_KIND (c))
13137 case GOMP_MAP_ALLOC:
13138 case GOMP_MAP_TO:
13139 case GOMP_MAP_FROM:
13140 case GOMP_MAP_TOFROM:
13141 case GOMP_MAP_ALWAYS_TO:
13142 case GOMP_MAP_ALWAYS_FROM:
13143 case GOMP_MAP_ALWAYS_TOFROM:
13144 case GOMP_MAP_RELEASE:
13145 case GOMP_MAP_DELETE:
13146 case GOMP_MAP_FORCE_TO:
13147 case GOMP_MAP_FORCE_FROM:
13148 case GOMP_MAP_FORCE_TOFROM:
13149 case GOMP_MAP_FORCE_PRESENT:
13150 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13151 break;
13152 default:
13153 break;
13155 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13156 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13157 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13158 else if (TREE_CODE (t) == COMPONENT_REF)
13159 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
13160 else
13161 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13162 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13163 && !c_mark_addressable (t))
13164 return false;
13165 OMP_CLAUSE_DECL (c2) = t;
13166 t = build_fold_addr_expr (first);
13167 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13168 tree ptr = OMP_CLAUSE_DECL (c2);
13169 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13170 ptr = build_fold_addr_expr (ptr);
13171 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13172 ptrdiff_type_node, t,
13173 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13174 ptrdiff_type_node, ptr));
13175 t = c_fully_fold (t, false, NULL);
13176 OMP_CLAUSE_SIZE (c2) = t;
13177 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13178 OMP_CLAUSE_CHAIN (c) = c2;
13180 return false;
13183 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13184 an inline call. But, remap
13185 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13186 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13188 static tree
13189 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13190 tree decl, tree placeholder)
13192 copy_body_data id;
13193 hash_map<tree, tree> decl_map;
13195 decl_map.put (omp_decl1, placeholder);
13196 decl_map.put (omp_decl2, decl);
13197 memset (&id, 0, sizeof (id));
13198 id.src_fn = DECL_CONTEXT (omp_decl1);
13199 id.dst_fn = current_function_decl;
13200 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13201 id.decl_map = &decl_map;
13203 id.copy_decl = copy_decl_no_change;
13204 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13205 id.transform_new_cfg = true;
13206 id.transform_return_to_modify = false;
13207 id.transform_lang_insert_block = NULL;
13208 id.eh_lp_nr = 0;
13209 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13210 return stmt;
13213 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13214 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13216 static tree
13217 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13219 if (*tp == (tree) data)
13220 return *tp;
13221 return NULL_TREE;
13224 /* Similarly, but also walk aggregate fields. */
13226 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13228 static tree
13229 c_find_omp_var_r (tree *tp, int *, void *data)
13231 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13232 return *tp;
13233 if (RECORD_OR_UNION_TYPE_P (*tp))
13235 tree field;
13236 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13238 for (field = TYPE_FIELDS (*tp); field;
13239 field = DECL_CHAIN (field))
13240 if (TREE_CODE (field) == FIELD_DECL)
13242 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13243 c_find_omp_var_r, data, pset);
13244 if (ret)
13245 return ret;
13246 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13247 if (ret)
13248 return ret;
13249 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13250 pset);
13251 if (ret)
13252 return ret;
13253 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13254 if (ret)
13255 return ret;
13258 else if (INTEGRAL_TYPE_P (*tp))
13259 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13260 ((struct c_find_omp_var_s *) data)->pset);
13261 return NULL_TREE;
13264 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13265 and clauses containing them should be removed. */
13267 static bool
13268 c_omp_finish_iterators (tree iter)
13270 bool ret = false;
13271 for (tree it = iter; it; it = TREE_CHAIN (it))
13273 tree var = TREE_VEC_ELT (it, 0);
13274 tree begin = TREE_VEC_ELT (it, 1);
13275 tree end = TREE_VEC_ELT (it, 2);
13276 tree step = TREE_VEC_ELT (it, 3);
13277 tree orig_step;
13278 tree type = TREE_TYPE (var);
13279 location_t loc = DECL_SOURCE_LOCATION (var);
13280 if (type == error_mark_node)
13282 ret = true;
13283 continue;
13285 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13287 error_at (loc, "iterator %qD has neither integral nor pointer type",
13288 var);
13289 ret = true;
13290 continue;
13292 else if (TYPE_ATOMIC (type))
13294 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13295 ret = true;
13296 continue;
13298 else if (TYPE_READONLY (type))
13300 error_at (loc, "iterator %qD has const qualified type", var);
13301 ret = true;
13302 continue;
13304 else if (step == error_mark_node
13305 || TREE_TYPE (step) == error_mark_node)
13307 ret = true;
13308 continue;
13310 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13312 error_at (EXPR_LOC_OR_LOC (step, loc),
13313 "iterator step with non-integral type");
13314 ret = true;
13315 continue;
13317 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13318 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13319 orig_step = save_expr (c_fully_fold (step, false, NULL));
13320 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13321 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13322 if (POINTER_TYPE_P (type))
13324 begin = save_expr (begin);
13325 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13326 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13327 fold_convert (sizetype, step),
13328 fold_convert (sizetype, begin));
13329 step = fold_convert (ssizetype, step);
13331 if (integer_zerop (step))
13333 error_at (loc, "iterator %qD has zero step", var);
13334 ret = true;
13335 continue;
13338 if (begin == error_mark_node
13339 || end == error_mark_node
13340 || step == error_mark_node
13341 || orig_step == error_mark_node)
13343 ret = true;
13344 continue;
13346 hash_set<tree> pset;
13347 tree it2;
13348 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13350 tree var2 = TREE_VEC_ELT (it2, 0);
13351 tree begin2 = TREE_VEC_ELT (it2, 1);
13352 tree end2 = TREE_VEC_ELT (it2, 2);
13353 tree step2 = TREE_VEC_ELT (it2, 3);
13354 tree type2 = TREE_TYPE (var2);
13355 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13356 struct c_find_omp_var_s data = { var, &pset };
13357 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13359 error_at (loc2,
13360 "type of iterator %qD refers to outer iterator %qD",
13361 var2, var);
13362 break;
13364 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13366 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13367 "begin expression refers to outer iterator %qD", var);
13368 break;
13370 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13372 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13373 "end expression refers to outer iterator %qD", var);
13374 break;
13376 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13378 error_at (EXPR_LOC_OR_LOC (step2, loc2),
13379 "step expression refers to outer iterator %qD", var);
13380 break;
13383 if (it2)
13385 ret = true;
13386 continue;
13388 TREE_VEC_ELT (it, 1) = begin;
13389 TREE_VEC_ELT (it, 2) = end;
13390 TREE_VEC_ELT (it, 3) = step;
13391 TREE_VEC_ELT (it, 4) = orig_step;
13393 return ret;
13396 /* For all elements of CLAUSES, validate them against their constraints.
13397 Remove any elements from the list that are invalid. */
13399 tree
13400 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13402 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13403 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13404 tree c, t, type, *pc;
13405 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13406 bool branch_seen = false;
13407 bool copyprivate_seen = false;
13408 bool linear_variable_step_check = false;
13409 tree *nowait_clause = NULL;
13410 bool ordered_seen = false;
13411 tree schedule_clause = NULL_TREE;
13412 bool oacc_async = false;
13413 tree last_iterators = NULL_TREE;
13414 bool last_iterators_remove = false;
13415 tree *nogroup_seen = NULL;
13416 bool reduction_seen = false;
13418 bitmap_obstack_initialize (NULL);
13419 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13420 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13421 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13422 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13423 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
13424 bitmap_initialize (&map_head, &bitmap_default_obstack);
13425 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13426 /* If ort == C_ORT_OMP used as nontemporal_head instead. */
13427 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13429 if (ort & C_ORT_ACC)
13430 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13431 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13433 oacc_async = true;
13434 break;
13437 for (pc = &clauses, c = clauses; c ; c = *pc)
13439 bool remove = false;
13440 bool need_complete = false;
13441 bool need_implicitly_determined = false;
13443 switch (OMP_CLAUSE_CODE (c))
13445 case OMP_CLAUSE_SHARED:
13446 need_implicitly_determined = true;
13447 goto check_dup_generic;
13449 case OMP_CLAUSE_PRIVATE:
13450 need_complete = true;
13451 need_implicitly_determined = true;
13452 goto check_dup_generic;
13454 case OMP_CLAUSE_REDUCTION:
13455 reduction_seen = true;
13456 /* FALLTHRU */
13457 case OMP_CLAUSE_IN_REDUCTION:
13458 case OMP_CLAUSE_TASK_REDUCTION:
13459 need_implicitly_determined = true;
13460 t = OMP_CLAUSE_DECL (c);
13461 if (TREE_CODE (t) == TREE_LIST)
13463 if (handle_omp_array_sections (c, ort))
13465 remove = true;
13466 break;
13469 t = OMP_CLAUSE_DECL (c);
13471 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13472 if (t == error_mark_node)
13474 remove = true;
13475 break;
13477 if (oacc_async)
13478 c_mark_addressable (t);
13479 type = TREE_TYPE (t);
13480 if (TREE_CODE (t) == MEM_REF)
13481 type = TREE_TYPE (type);
13482 if (TREE_CODE (type) == ARRAY_TYPE)
13484 tree oatype = type;
13485 gcc_assert (TREE_CODE (t) != MEM_REF);
13486 while (TREE_CODE (type) == ARRAY_TYPE)
13487 type = TREE_TYPE (type);
13488 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13490 error_at (OMP_CLAUSE_LOCATION (c),
13491 "%qD in %<reduction%> clause is a zero size array",
13493 remove = true;
13494 break;
13496 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13497 TYPE_SIZE_UNIT (type));
13498 if (integer_zerop (size))
13500 error_at (OMP_CLAUSE_LOCATION (c),
13501 "%qD in %<reduction%> clause is a zero size array",
13503 remove = true;
13504 break;
13506 size = size_binop (MINUS_EXPR, size, size_one_node);
13507 size = save_expr (size);
13508 tree index_type = build_index_type (size);
13509 tree atype = build_array_type (type, index_type);
13510 tree ptype = build_pointer_type (type);
13511 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13512 t = build_fold_addr_expr (t);
13513 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13514 OMP_CLAUSE_DECL (c) = t;
13516 if (TYPE_ATOMIC (type))
13518 error_at (OMP_CLAUSE_LOCATION (c),
13519 "%<_Atomic%> %qE in %<reduction%> clause", t);
13520 remove = true;
13521 break;
13523 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13524 || OMP_CLAUSE_REDUCTION_TASK (c))
13526 /* Disallow zero sized or potentially zero sized task
13527 reductions. */
13528 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13530 error_at (OMP_CLAUSE_LOCATION (c),
13531 "zero sized type %qT in %qs clause", type,
13532 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13533 remove = true;
13534 break;
13536 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
13538 error_at (OMP_CLAUSE_LOCATION (c),
13539 "variable sized type %qT in %qs clause", type,
13540 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13541 remove = true;
13542 break;
13545 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13546 && (FLOAT_TYPE_P (type)
13547 || TREE_CODE (type) == COMPLEX_TYPE))
13549 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13550 const char *r_name = NULL;
13552 switch (r_code)
13554 case PLUS_EXPR:
13555 case MULT_EXPR:
13556 case MINUS_EXPR:
13557 break;
13558 case MIN_EXPR:
13559 if (TREE_CODE (type) == COMPLEX_TYPE)
13560 r_name = "min";
13561 break;
13562 case MAX_EXPR:
13563 if (TREE_CODE (type) == COMPLEX_TYPE)
13564 r_name = "max";
13565 break;
13566 case BIT_AND_EXPR:
13567 r_name = "&";
13568 break;
13569 case BIT_XOR_EXPR:
13570 r_name = "^";
13571 break;
13572 case BIT_IOR_EXPR:
13573 r_name = "|";
13574 break;
13575 case TRUTH_ANDIF_EXPR:
13576 if (FLOAT_TYPE_P (type))
13577 r_name = "&&";
13578 break;
13579 case TRUTH_ORIF_EXPR:
13580 if (FLOAT_TYPE_P (type))
13581 r_name = "||";
13582 break;
13583 default:
13584 gcc_unreachable ();
13586 if (r_name)
13588 error_at (OMP_CLAUSE_LOCATION (c),
13589 "%qE has invalid type for %<reduction(%s)%>",
13590 t, r_name);
13591 remove = true;
13592 break;
13595 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13597 error_at (OMP_CLAUSE_LOCATION (c),
13598 "user defined reduction not found for %qE", t);
13599 remove = true;
13600 break;
13602 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13604 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13605 type = TYPE_MAIN_VARIANT (type);
13606 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13607 VAR_DECL, NULL_TREE, type);
13608 tree decl_placeholder = NULL_TREE;
13609 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13610 DECL_ARTIFICIAL (placeholder) = 1;
13611 DECL_IGNORED_P (placeholder) = 1;
13612 if (TREE_CODE (t) == MEM_REF)
13614 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13615 VAR_DECL, NULL_TREE, type);
13616 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13617 DECL_ARTIFICIAL (decl_placeholder) = 1;
13618 DECL_IGNORED_P (decl_placeholder) = 1;
13620 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13621 c_mark_addressable (placeholder);
13622 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13623 c_mark_addressable (decl_placeholder ? decl_placeholder
13624 : OMP_CLAUSE_DECL (c));
13625 OMP_CLAUSE_REDUCTION_MERGE (c)
13626 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13627 TREE_VEC_ELT (list, 0),
13628 TREE_VEC_ELT (list, 1),
13629 decl_placeholder ? decl_placeholder
13630 : OMP_CLAUSE_DECL (c), placeholder);
13631 OMP_CLAUSE_REDUCTION_MERGE (c)
13632 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13633 void_type_node, NULL_TREE,
13634 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13635 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13636 if (TREE_VEC_LENGTH (list) == 6)
13638 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13639 c_mark_addressable (decl_placeholder ? decl_placeholder
13640 : OMP_CLAUSE_DECL (c));
13641 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13642 c_mark_addressable (placeholder);
13643 tree init = TREE_VEC_ELT (list, 5);
13644 if (init == error_mark_node)
13645 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13646 OMP_CLAUSE_REDUCTION_INIT (c)
13647 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13648 TREE_VEC_ELT (list, 3),
13649 decl_placeholder ? decl_placeholder
13650 : OMP_CLAUSE_DECL (c), placeholder);
13651 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13653 tree v = decl_placeholder ? decl_placeholder : t;
13654 OMP_CLAUSE_REDUCTION_INIT (c)
13655 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13656 OMP_CLAUSE_REDUCTION_INIT (c));
13658 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13659 c_find_omp_placeholder_r,
13660 placeholder, NULL))
13661 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13663 else
13665 tree init;
13666 tree v = decl_placeholder ? decl_placeholder : t;
13667 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13668 init = build_constructor (TREE_TYPE (v), NULL);
13669 else
13670 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13671 OMP_CLAUSE_REDUCTION_INIT (c)
13672 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13674 OMP_CLAUSE_REDUCTION_INIT (c)
13675 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13676 void_type_node, NULL_TREE,
13677 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13678 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13680 if (TREE_CODE (t) == MEM_REF)
13682 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13683 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13684 != INTEGER_CST)
13686 sorry ("variable length element type in array "
13687 "%<reduction%> clause");
13688 remove = true;
13689 break;
13691 t = TREE_OPERAND (t, 0);
13692 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13693 t = TREE_OPERAND (t, 0);
13694 if (TREE_CODE (t) == ADDR_EXPR)
13695 t = TREE_OPERAND (t, 0);
13697 goto check_dup_generic_t;
13699 case OMP_CLAUSE_COPYPRIVATE:
13700 copyprivate_seen = true;
13701 if (nowait_clause)
13703 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13704 "%<nowait%> clause must not be used together "
13705 "with %<copyprivate%>");
13706 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13707 nowait_clause = NULL;
13709 goto check_dup_generic;
13711 case OMP_CLAUSE_COPYIN:
13712 t = OMP_CLAUSE_DECL (c);
13713 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13715 error_at (OMP_CLAUSE_LOCATION (c),
13716 "%qE must be %<threadprivate%> for %<copyin%>", t);
13717 remove = true;
13718 break;
13720 goto check_dup_generic;
13722 case OMP_CLAUSE_LINEAR:
13723 if (ort != C_ORT_OMP_DECLARE_SIMD)
13724 need_implicitly_determined = true;
13725 t = OMP_CLAUSE_DECL (c);
13726 if (ort != C_ORT_OMP_DECLARE_SIMD
13727 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13729 error_at (OMP_CLAUSE_LOCATION (c),
13730 "modifier should not be specified in %<linear%> "
13731 "clause on %<simd%> or %<for%> constructs");
13732 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13734 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13735 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13737 error_at (OMP_CLAUSE_LOCATION (c),
13738 "linear clause applied to non-integral non-pointer "
13739 "variable with type %qT", TREE_TYPE (t));
13740 remove = true;
13741 break;
13743 if (TYPE_ATOMIC (TREE_TYPE (t)))
13745 error_at (OMP_CLAUSE_LOCATION (c),
13746 "%<_Atomic%> %qD in %<linear%> clause", t);
13747 remove = true;
13748 break;
13750 if (ort == C_ORT_OMP_DECLARE_SIMD)
13752 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13753 if (TREE_CODE (s) == PARM_DECL)
13755 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13756 /* map_head bitmap is used as uniform_head if
13757 declare_simd. */
13758 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13759 linear_variable_step_check = true;
13760 goto check_dup_generic;
13762 if (TREE_CODE (s) != INTEGER_CST)
13764 error_at (OMP_CLAUSE_LOCATION (c),
13765 "%<linear%> clause step %qE is neither constant "
13766 "nor a parameter", s);
13767 remove = true;
13768 break;
13771 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13773 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13774 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13775 OMP_CLAUSE_DECL (c), s);
13776 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13777 sizetype, fold_convert (sizetype, s),
13778 fold_convert
13779 (sizetype, OMP_CLAUSE_DECL (c)));
13780 if (s == error_mark_node)
13781 s = size_one_node;
13782 OMP_CLAUSE_LINEAR_STEP (c) = s;
13784 else
13785 OMP_CLAUSE_LINEAR_STEP (c)
13786 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13787 goto check_dup_generic;
13789 check_dup_generic:
13790 t = OMP_CLAUSE_DECL (c);
13791 check_dup_generic_t:
13792 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13794 error_at (OMP_CLAUSE_LOCATION (c),
13795 "%qE is not a variable in clause %qs", t,
13796 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13797 remove = true;
13799 else if (ort == C_ORT_ACC
13800 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13802 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13804 error_at (OMP_CLAUSE_LOCATION (c),
13805 "%qD appears more than once in reduction clauses",
13807 remove = true;
13809 else
13810 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13812 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13813 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13814 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13816 error_at (OMP_CLAUSE_LOCATION (c),
13817 "%qE appears more than once in data clauses", t);
13818 remove = true;
13820 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13821 && bitmap_bit_p (&map_head, DECL_UID (t)))
13823 if (ort == C_ORT_ACC)
13824 error_at (OMP_CLAUSE_LOCATION (c),
13825 "%qD appears more than once in data clauses", t);
13826 else
13827 error_at (OMP_CLAUSE_LOCATION (c),
13828 "%qD appears both in data and map clauses", t);
13829 remove = true;
13831 else
13832 bitmap_set_bit (&generic_head, DECL_UID (t));
13833 break;
13835 case OMP_CLAUSE_FIRSTPRIVATE:
13836 t = OMP_CLAUSE_DECL (c);
13837 need_complete = true;
13838 need_implicitly_determined = true;
13839 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13841 error_at (OMP_CLAUSE_LOCATION (c),
13842 "%qE is not a variable in clause %<firstprivate%>", t);
13843 remove = true;
13845 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13846 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13848 error_at (OMP_CLAUSE_LOCATION (c),
13849 "%qE appears more than once in data clauses", t);
13850 remove = true;
13852 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13854 if (ort == C_ORT_ACC)
13855 error_at (OMP_CLAUSE_LOCATION (c),
13856 "%qD appears more than once in data clauses", t);
13857 else
13858 error_at (OMP_CLAUSE_LOCATION (c),
13859 "%qD appears both in data and map clauses", t);
13860 remove = true;
13862 else
13863 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13864 break;
13866 case OMP_CLAUSE_LASTPRIVATE:
13867 t = OMP_CLAUSE_DECL (c);
13868 need_complete = true;
13869 need_implicitly_determined = true;
13870 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13872 error_at (OMP_CLAUSE_LOCATION (c),
13873 "%qE is not a variable in clause %<lastprivate%>", t);
13874 remove = true;
13876 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13877 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13879 error_at (OMP_CLAUSE_LOCATION (c),
13880 "%qE appears more than once in data clauses", t);
13881 remove = true;
13883 else
13884 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13885 break;
13887 case OMP_CLAUSE_ALIGNED:
13888 t = OMP_CLAUSE_DECL (c);
13889 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13891 error_at (OMP_CLAUSE_LOCATION (c),
13892 "%qE is not a variable in %<aligned%> clause", t);
13893 remove = true;
13895 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13896 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13898 error_at (OMP_CLAUSE_LOCATION (c),
13899 "%qE in %<aligned%> clause is neither a pointer nor "
13900 "an array", t);
13901 remove = true;
13903 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13905 error_at (OMP_CLAUSE_LOCATION (c),
13906 "%<_Atomic%> %qD in %<aligned%> clause", t);
13907 remove = true;
13908 break;
13910 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13912 error_at (OMP_CLAUSE_LOCATION (c),
13913 "%qE appears more than once in %<aligned%> clauses",
13915 remove = true;
13917 else
13918 bitmap_set_bit (&aligned_head, DECL_UID (t));
13919 break;
13921 case OMP_CLAUSE_NONTEMPORAL:
13922 t = OMP_CLAUSE_DECL (c);
13923 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13925 error_at (OMP_CLAUSE_LOCATION (c),
13926 "%qE is not a variable in %<nontemporal%> clause", t);
13927 remove = true;
13929 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13931 error_at (OMP_CLAUSE_LOCATION (c),
13932 "%qE appears more than once in %<nontemporal%> "
13933 "clauses", t);
13934 remove = true;
13936 else
13937 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13938 break;
13940 case OMP_CLAUSE_DEPEND:
13941 t = OMP_CLAUSE_DECL (c);
13942 if (t == NULL_TREE)
13944 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13945 == OMP_CLAUSE_DEPEND_SOURCE);
13946 break;
13948 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13950 gcc_assert (TREE_CODE (t) == TREE_LIST);
13951 for (; t; t = TREE_CHAIN (t))
13953 tree decl = TREE_VALUE (t);
13954 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13956 tree offset = TREE_PURPOSE (t);
13957 bool neg = wi::neg_p (wi::to_wide (offset));
13958 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13959 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13960 neg ? MINUS_EXPR : PLUS_EXPR,
13961 decl, offset);
13962 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13963 sizetype,
13964 fold_convert (sizetype, t2),
13965 fold_convert (sizetype, decl));
13966 if (t2 == error_mark_node)
13968 remove = true;
13969 break;
13971 TREE_PURPOSE (t) = t2;
13974 break;
13976 if (TREE_CODE (t) == TREE_LIST
13977 && TREE_PURPOSE (t)
13978 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
13980 if (TREE_PURPOSE (t) != last_iterators)
13981 last_iterators_remove
13982 = c_omp_finish_iterators (TREE_PURPOSE (t));
13983 last_iterators = TREE_PURPOSE (t);
13984 t = TREE_VALUE (t);
13985 if (last_iterators_remove)
13986 t = error_mark_node;
13988 else
13989 last_iterators = NULL_TREE;
13990 if (TREE_CODE (t) == TREE_LIST)
13992 if (handle_omp_array_sections (c, ort))
13993 remove = true;
13994 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
13996 error_at (OMP_CLAUSE_LOCATION (c),
13997 "%<depend%> clause with %<depobj%> dependence "
13998 "type on array section");
13999 remove = true;
14001 break;
14003 if (t == error_mark_node)
14004 remove = true;
14005 else if (!lvalue_p (t))
14007 error_at (OMP_CLAUSE_LOCATION (c),
14008 "%qE is not lvalue expression nor array section in "
14009 "%<depend%> clause", t);
14010 remove = true;
14012 else if (TREE_CODE (t) == COMPONENT_REF
14013 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14015 error_at (OMP_CLAUSE_LOCATION (c),
14016 "bit-field %qE in %qs clause", t, "depend");
14017 remove = true;
14019 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14021 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14023 error_at (OMP_CLAUSE_LOCATION (c),
14024 "%qE does not have %<omp_depend_t%> type in "
14025 "%<depend%> clause with %<depobj%> dependence "
14026 "type", t);
14027 remove = true;
14030 else if (c_omp_depend_t_p (TREE_TYPE (t)))
14032 error_at (OMP_CLAUSE_LOCATION (c),
14033 "%qE should not have %<omp_depend_t%> type in "
14034 "%<depend%> clause with dependence type other than "
14035 "%<depobj%>", t);
14036 remove = true;
14038 if (!remove)
14040 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14041 t, false);
14042 if (addr == error_mark_node)
14043 remove = true;
14044 else
14046 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14047 RO_UNARY_STAR);
14048 if (t == error_mark_node)
14049 remove = true;
14050 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14051 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14052 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14053 == TREE_VEC))
14054 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14055 else
14056 OMP_CLAUSE_DECL (c) = t;
14059 break;
14061 case OMP_CLAUSE_MAP:
14062 case OMP_CLAUSE_TO:
14063 case OMP_CLAUSE_FROM:
14064 case OMP_CLAUSE__CACHE_:
14065 t = OMP_CLAUSE_DECL (c);
14066 if (TREE_CODE (t) == TREE_LIST)
14068 if (handle_omp_array_sections (c, ort))
14069 remove = true;
14070 else
14072 t = OMP_CLAUSE_DECL (c);
14073 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14075 error_at (OMP_CLAUSE_LOCATION (c),
14076 "array section does not have mappable type "
14077 "in %qs clause",
14078 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14079 remove = true;
14081 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14083 error_at (OMP_CLAUSE_LOCATION (c),
14084 "%<_Atomic%> %qE in %qs clause", t,
14085 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14086 remove = true;
14088 while (TREE_CODE (t) == ARRAY_REF)
14089 t = TREE_OPERAND (t, 0);
14090 if (TREE_CODE (t) == COMPONENT_REF
14091 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14093 while (TREE_CODE (t) == COMPONENT_REF)
14094 t = TREE_OPERAND (t, 0);
14095 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14096 break;
14097 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14099 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14100 error_at (OMP_CLAUSE_LOCATION (c),
14101 "%qD appears more than once in motion "
14102 "clauses", t);
14103 else if (ort == C_ORT_ACC)
14104 error_at (OMP_CLAUSE_LOCATION (c),
14105 "%qD appears more than once in data "
14106 "clauses", t);
14107 else
14108 error_at (OMP_CLAUSE_LOCATION (c),
14109 "%qD appears more than once in map "
14110 "clauses", t);
14111 remove = true;
14113 else
14115 bitmap_set_bit (&map_head, DECL_UID (t));
14116 bitmap_set_bit (&map_field_head, DECL_UID (t));
14120 break;
14122 if (t == error_mark_node)
14124 remove = true;
14125 break;
14127 if (TREE_CODE (t) == COMPONENT_REF
14128 && (ort & C_ORT_OMP)
14129 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14131 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14133 error_at (OMP_CLAUSE_LOCATION (c),
14134 "bit-field %qE in %qs clause",
14135 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14136 remove = true;
14138 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14140 error_at (OMP_CLAUSE_LOCATION (c),
14141 "%qE does not have a mappable type in %qs clause",
14142 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14143 remove = true;
14145 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14147 error_at (OMP_CLAUSE_LOCATION (c),
14148 "%<_Atomic%> %qE in %qs clause", t,
14149 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14150 remove = true;
14152 while (TREE_CODE (t) == COMPONENT_REF)
14154 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14155 == UNION_TYPE)
14157 error_at (OMP_CLAUSE_LOCATION (c),
14158 "%qE is a member of a union", t);
14159 remove = true;
14160 break;
14162 t = TREE_OPERAND (t, 0);
14164 if (remove)
14165 break;
14166 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14168 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14169 break;
14172 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14174 error_at (OMP_CLAUSE_LOCATION (c),
14175 "%qE is not a variable in %qs clause", t,
14176 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14177 remove = true;
14179 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14181 error_at (OMP_CLAUSE_LOCATION (c),
14182 "%qD is threadprivate variable in %qs clause", t,
14183 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14184 remove = true;
14186 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14187 || (OMP_CLAUSE_MAP_KIND (c)
14188 != GOMP_MAP_FIRSTPRIVATE_POINTER))
14189 && !c_mark_addressable (t))
14190 remove = true;
14191 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14192 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14193 || (OMP_CLAUSE_MAP_KIND (c)
14194 == GOMP_MAP_FIRSTPRIVATE_POINTER)
14195 || (OMP_CLAUSE_MAP_KIND (c)
14196 == GOMP_MAP_FORCE_DEVICEPTR)))
14197 && t == OMP_CLAUSE_DECL (c)
14198 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14200 error_at (OMP_CLAUSE_LOCATION (c),
14201 "%qD does not have a mappable type in %qs clause", t,
14202 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14203 remove = true;
14205 else if (TREE_TYPE (t) == error_mark_node)
14206 remove = true;
14207 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14209 error_at (OMP_CLAUSE_LOCATION (c),
14210 "%<_Atomic%> %qE in %qs clause", t,
14211 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14212 remove = true;
14214 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14215 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14217 if (bitmap_bit_p (&generic_head, DECL_UID (t))
14218 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14220 error_at (OMP_CLAUSE_LOCATION (c),
14221 "%qD appears more than once in data clauses", t);
14222 remove = true;
14224 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14226 if (ort == C_ORT_ACC)
14227 error_at (OMP_CLAUSE_LOCATION (c),
14228 "%qD appears more than once in data clauses", t);
14229 else
14230 error_at (OMP_CLAUSE_LOCATION (c),
14231 "%qD appears both in data and map clauses", t);
14232 remove = true;
14234 else
14235 bitmap_set_bit (&generic_head, DECL_UID (t));
14237 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14239 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14240 error_at (OMP_CLAUSE_LOCATION (c),
14241 "%qD appears more than once in motion clauses", t);
14242 else if (ort == C_ORT_ACC)
14243 error_at (OMP_CLAUSE_LOCATION (c),
14244 "%qD appears more than once in data clauses", t);
14245 else
14246 error_at (OMP_CLAUSE_LOCATION (c),
14247 "%qD appears more than once in map clauses", t);
14248 remove = true;
14250 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14251 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14253 if (ort == C_ORT_ACC)
14254 error_at (OMP_CLAUSE_LOCATION (c),
14255 "%qD appears more than once in data clauses", t);
14256 else
14257 error_at (OMP_CLAUSE_LOCATION (c),
14258 "%qD appears both in data and map clauses", t);
14259 remove = true;
14261 else
14263 bitmap_set_bit (&map_head, DECL_UID (t));
14264 if (t != OMP_CLAUSE_DECL (c)
14265 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14266 bitmap_set_bit (&map_field_head, DECL_UID (t));
14268 break;
14270 case OMP_CLAUSE_TO_DECLARE:
14271 case OMP_CLAUSE_LINK:
14272 t = OMP_CLAUSE_DECL (c);
14273 if (TREE_CODE (t) == FUNCTION_DECL
14274 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14276 else if (!VAR_P (t))
14278 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14279 error_at (OMP_CLAUSE_LOCATION (c),
14280 "%qE is neither a variable nor a function name in "
14281 "clause %qs", t,
14282 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14283 else
14284 error_at (OMP_CLAUSE_LOCATION (c),
14285 "%qE is not a variable in clause %qs", t,
14286 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14287 remove = true;
14289 else if (DECL_THREAD_LOCAL_P (t))
14291 error_at (OMP_CLAUSE_LOCATION (c),
14292 "%qD is threadprivate variable in %qs clause", t,
14293 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14294 remove = true;
14296 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14298 error_at (OMP_CLAUSE_LOCATION (c),
14299 "%qD does not have a mappable type in %qs clause", t,
14300 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14301 remove = true;
14303 if (remove)
14304 break;
14305 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
14307 error_at (OMP_CLAUSE_LOCATION (c),
14308 "%qE appears more than once on the same "
14309 "%<declare target%> directive", t);
14310 remove = true;
14312 else
14313 bitmap_set_bit (&generic_head, DECL_UID (t));
14314 break;
14316 case OMP_CLAUSE_UNIFORM:
14317 t = OMP_CLAUSE_DECL (c);
14318 if (TREE_CODE (t) != PARM_DECL)
14320 if (DECL_P (t))
14321 error_at (OMP_CLAUSE_LOCATION (c),
14322 "%qD is not an argument in %<uniform%> clause", t);
14323 else
14324 error_at (OMP_CLAUSE_LOCATION (c),
14325 "%qE is not an argument in %<uniform%> clause", t);
14326 remove = true;
14327 break;
14329 /* map_head bitmap is used as uniform_head if declare_simd. */
14330 bitmap_set_bit (&map_head, DECL_UID (t));
14331 goto check_dup_generic;
14333 case OMP_CLAUSE_IS_DEVICE_PTR:
14334 case OMP_CLAUSE_USE_DEVICE_PTR:
14335 t = OMP_CLAUSE_DECL (c);
14336 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
14337 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14339 error_at (OMP_CLAUSE_LOCATION (c),
14340 "%qs variable is neither a pointer nor an array",
14341 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14342 remove = true;
14344 goto check_dup_generic;
14346 case OMP_CLAUSE_NOWAIT:
14347 if (copyprivate_seen)
14349 error_at (OMP_CLAUSE_LOCATION (c),
14350 "%<nowait%> clause must not be used together "
14351 "with %<copyprivate%>");
14352 remove = true;
14353 break;
14355 nowait_clause = pc;
14356 pc = &OMP_CLAUSE_CHAIN (c);
14357 continue;
14359 case OMP_CLAUSE_IF:
14360 case OMP_CLAUSE_NUM_THREADS:
14361 case OMP_CLAUSE_NUM_TEAMS:
14362 case OMP_CLAUSE_THREAD_LIMIT:
14363 case OMP_CLAUSE_DEFAULT:
14364 case OMP_CLAUSE_UNTIED:
14365 case OMP_CLAUSE_COLLAPSE:
14366 case OMP_CLAUSE_FINAL:
14367 case OMP_CLAUSE_MERGEABLE:
14368 case OMP_CLAUSE_DEVICE:
14369 case OMP_CLAUSE_DIST_SCHEDULE:
14370 case OMP_CLAUSE_PARALLEL:
14371 case OMP_CLAUSE_FOR:
14372 case OMP_CLAUSE_SECTIONS:
14373 case OMP_CLAUSE_TASKGROUP:
14374 case OMP_CLAUSE_PROC_BIND:
14375 case OMP_CLAUSE_PRIORITY:
14376 case OMP_CLAUSE_GRAINSIZE:
14377 case OMP_CLAUSE_NUM_TASKS:
14378 case OMP_CLAUSE_THREADS:
14379 case OMP_CLAUSE_SIMD:
14380 case OMP_CLAUSE_HINT:
14381 case OMP_CLAUSE_DEFAULTMAP:
14382 case OMP_CLAUSE_NUM_GANGS:
14383 case OMP_CLAUSE_NUM_WORKERS:
14384 case OMP_CLAUSE_VECTOR_LENGTH:
14385 case OMP_CLAUSE_ASYNC:
14386 case OMP_CLAUSE_WAIT:
14387 case OMP_CLAUSE_AUTO:
14388 case OMP_CLAUSE_INDEPENDENT:
14389 case OMP_CLAUSE_SEQ:
14390 case OMP_CLAUSE_GANG:
14391 case OMP_CLAUSE_WORKER:
14392 case OMP_CLAUSE_VECTOR:
14393 case OMP_CLAUSE_TILE:
14394 case OMP_CLAUSE_IF_PRESENT:
14395 case OMP_CLAUSE_FINALIZE:
14396 pc = &OMP_CLAUSE_CHAIN (c);
14397 continue;
14399 case OMP_CLAUSE_NOGROUP:
14400 nogroup_seen = pc;
14401 pc = &OMP_CLAUSE_CHAIN (c);
14402 continue;
14404 case OMP_CLAUSE_SCHEDULE:
14405 schedule_clause = c;
14406 pc = &OMP_CLAUSE_CHAIN (c);
14407 continue;
14409 case OMP_CLAUSE_ORDERED:
14410 ordered_seen = true;
14411 pc = &OMP_CLAUSE_CHAIN (c);
14412 continue;
14414 case OMP_CLAUSE_SAFELEN:
14415 safelen = c;
14416 pc = &OMP_CLAUSE_CHAIN (c);
14417 continue;
14418 case OMP_CLAUSE_SIMDLEN:
14419 simdlen = c;
14420 pc = &OMP_CLAUSE_CHAIN (c);
14421 continue;
14423 case OMP_CLAUSE_INBRANCH:
14424 case OMP_CLAUSE_NOTINBRANCH:
14425 if (branch_seen)
14427 error_at (OMP_CLAUSE_LOCATION (c),
14428 "%<inbranch%> clause is incompatible with "
14429 "%<notinbranch%>");
14430 remove = true;
14431 break;
14433 branch_seen = true;
14434 pc = &OMP_CLAUSE_CHAIN (c);
14435 continue;
14437 default:
14438 gcc_unreachable ();
14441 if (!remove)
14443 t = OMP_CLAUSE_DECL (c);
14445 if (need_complete)
14447 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14448 if (t == error_mark_node)
14449 remove = true;
14452 if (need_implicitly_determined)
14454 const char *share_name = NULL;
14456 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14457 share_name = "threadprivate";
14458 else switch (c_omp_predetermined_sharing (t))
14460 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
14461 break;
14462 case OMP_CLAUSE_DEFAULT_SHARED:
14463 share_name = "shared";
14464 break;
14465 case OMP_CLAUSE_DEFAULT_PRIVATE:
14466 share_name = "private";
14467 break;
14468 default:
14469 gcc_unreachable ();
14471 if (share_name)
14473 error_at (OMP_CLAUSE_LOCATION (c),
14474 "%qE is predetermined %qs for %qs",
14475 t, share_name,
14476 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14477 remove = true;
14479 else if (TREE_READONLY (t)
14480 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
14481 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
14483 error_at (OMP_CLAUSE_LOCATION (c),
14484 "%<const%> qualified %qE may appear only in "
14485 "%<shared%> or %<firstprivate%> clauses", t);
14486 remove = true;
14491 if (remove)
14492 *pc = OMP_CLAUSE_CHAIN (c);
14493 else
14494 pc = &OMP_CLAUSE_CHAIN (c);
14497 if (simdlen
14498 && safelen
14499 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
14500 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
14502 error_at (OMP_CLAUSE_LOCATION (simdlen),
14503 "%<simdlen%> clause value is bigger than "
14504 "%<safelen%> clause value");
14505 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
14506 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
14509 if (ordered_seen
14510 && schedule_clause
14511 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14512 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14514 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14515 "%<nonmonotonic%> schedule modifier specified together "
14516 "with %<ordered%> clause");
14517 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14518 = (enum omp_clause_schedule_kind)
14519 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14520 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14523 if (linear_variable_step_check)
14524 for (pc = &clauses, c = clauses; c ; c = *pc)
14526 bool remove = false;
14527 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14528 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
14529 && !bitmap_bit_p (&map_head,
14530 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
14532 error_at (OMP_CLAUSE_LOCATION (c),
14533 "%<linear%> clause step is a parameter %qD not "
14534 "specified in %<uniform%> clause",
14535 OMP_CLAUSE_LINEAR_STEP (c));
14536 remove = true;
14539 if (remove)
14540 *pc = OMP_CLAUSE_CHAIN (c);
14541 else
14542 pc = &OMP_CLAUSE_CHAIN (c);
14545 if (nogroup_seen && reduction_seen)
14547 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
14548 "%<nogroup%> clause must not be used together with "
14549 "%<reduction%> clause");
14550 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
14553 bitmap_obstack_release (NULL);
14554 return clauses;
14557 /* Return code to initialize DST with a copy constructor from SRC.
14558 C doesn't have copy constructors nor assignment operators, only for
14559 _Atomic vars we need to perform __atomic_load from src into a temporary
14560 followed by __atomic_store of the temporary to dst. */
14562 tree
14563 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14565 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14566 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14568 location_t loc = OMP_CLAUSE_LOCATION (clause);
14569 tree type = TREE_TYPE (dst);
14570 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14571 tree tmp = create_tmp_var (nonatomic_type);
14572 tree tmp_addr = build_fold_addr_expr (tmp);
14573 TREE_ADDRESSABLE (tmp) = 1;
14574 TREE_NO_WARNING (tmp) = 1;
14575 tree src_addr = build_fold_addr_expr (src);
14576 tree dst_addr = build_fold_addr_expr (dst);
14577 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14578 vec<tree, va_gc> *params;
14579 /* Expansion of a generic atomic load may require an addition
14580 element, so allocate enough to prevent a resize. */
14581 vec_alloc (params, 4);
14583 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14584 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14585 params->quick_push (src_addr);
14586 params->quick_push (tmp_addr);
14587 params->quick_push (seq_cst);
14588 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14590 vec_alloc (params, 4);
14592 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14593 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14594 params->quick_push (dst_addr);
14595 params->quick_push (tmp_addr);
14596 params->quick_push (seq_cst);
14597 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14598 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14601 /* Create a transaction node. */
14603 tree
14604 c_finish_transaction (location_t loc, tree block, int flags)
14606 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14607 if (flags & TM_STMT_ATTR_OUTER)
14608 TRANSACTION_EXPR_OUTER (stmt) = 1;
14609 if (flags & TM_STMT_ATTR_RELAXED)
14610 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14611 return add_stmt (stmt);
14614 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14615 down to the element type of an array. If ORIG_QUAL_TYPE is not
14616 NULL, then it should be used as the qualified type
14617 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14618 preserve information about the typedef name from which an array
14619 type was derived). */
14621 tree
14622 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14623 size_t orig_qual_indirect)
14625 if (type == error_mark_node)
14626 return type;
14628 if (TREE_CODE (type) == ARRAY_TYPE)
14630 tree t;
14631 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14632 type_quals, orig_qual_type,
14633 orig_qual_indirect - 1);
14635 /* See if we already have an identically qualified type. */
14636 if (orig_qual_type && orig_qual_indirect == 0)
14637 t = orig_qual_type;
14638 else
14639 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14641 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14642 && TYPE_NAME (t) == TYPE_NAME (type)
14643 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14644 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14645 TYPE_ATTRIBUTES (type)))
14646 break;
14648 if (!t)
14650 tree domain = TYPE_DOMAIN (type);
14652 t = build_variant_type_copy (type);
14653 TREE_TYPE (t) = element_type;
14655 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14656 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14657 SET_TYPE_STRUCTURAL_EQUALITY (t);
14658 else if (TYPE_CANONICAL (element_type) != element_type
14659 || (domain && TYPE_CANONICAL (domain) != domain))
14661 tree unqualified_canon
14662 = build_array_type (TYPE_CANONICAL (element_type),
14663 domain? TYPE_CANONICAL (domain)
14664 : NULL_TREE);
14665 if (TYPE_REVERSE_STORAGE_ORDER (type))
14667 unqualified_canon
14668 = build_distinct_type_copy (unqualified_canon);
14669 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14671 TYPE_CANONICAL (t)
14672 = c_build_qualified_type (unqualified_canon, type_quals);
14674 else
14675 TYPE_CANONICAL (t) = t;
14677 return t;
14680 /* A restrict-qualified pointer type must be a pointer to object or
14681 incomplete type. Note that the use of POINTER_TYPE_P also allows
14682 REFERENCE_TYPEs, which is appropriate for C++. */
14683 if ((type_quals & TYPE_QUAL_RESTRICT)
14684 && (!POINTER_TYPE_P (type)
14685 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14687 error ("invalid use of %<restrict%>");
14688 type_quals &= ~TYPE_QUAL_RESTRICT;
14691 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14692 ? orig_qual_type
14693 : build_qualified_type (type, type_quals));
14694 /* A variant type does not inherit the list of incomplete vars from the
14695 type main variant. */
14696 if (RECORD_OR_UNION_TYPE_P (var_type)
14697 && TYPE_MAIN_VARIANT (var_type) != var_type)
14698 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14699 return var_type;
14702 /* Build a VA_ARG_EXPR for the C parser. */
14704 tree
14705 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14707 if (error_operand_p (type))
14708 return error_mark_node;
14709 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14710 order because it takes the address of the expression. */
14711 else if (handled_component_p (expr)
14712 && reverse_storage_order_for_component_p (expr))
14714 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14715 return error_mark_node;
14717 else if (!COMPLETE_TYPE_P (type))
14719 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14720 "type %qT", type);
14721 return error_mark_node;
14723 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14724 warning_at (loc2, OPT_Wc___compat,
14725 "C++ requires promoted type, not enum type, in %<va_arg%>");
14726 return build_va_arg (loc2, expr, type);
14729 /* Return truthvalue of whether T1 is the same tree structure as T2.
14730 Return 1 if they are the same. Return false if they are different. */
14732 bool
14733 c_tree_equal (tree t1, tree t2)
14735 enum tree_code code1, code2;
14737 if (t1 == t2)
14738 return true;
14739 if (!t1 || !t2)
14740 return false;
14742 for (code1 = TREE_CODE (t1);
14743 CONVERT_EXPR_CODE_P (code1)
14744 || code1 == NON_LVALUE_EXPR;
14745 code1 = TREE_CODE (t1))
14746 t1 = TREE_OPERAND (t1, 0);
14747 for (code2 = TREE_CODE (t2);
14748 CONVERT_EXPR_CODE_P (code2)
14749 || code2 == NON_LVALUE_EXPR;
14750 code2 = TREE_CODE (t2))
14751 t2 = TREE_OPERAND (t2, 0);
14753 /* They might have become equal now. */
14754 if (t1 == t2)
14755 return true;
14757 if (code1 != code2)
14758 return false;
14760 switch (code1)
14762 case INTEGER_CST:
14763 return wi::to_wide (t1) == wi::to_wide (t2);
14765 case REAL_CST:
14766 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14768 case STRING_CST:
14769 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14770 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14771 TREE_STRING_LENGTH (t1));
14773 case FIXED_CST:
14774 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14775 TREE_FIXED_CST (t2));
14777 case COMPLEX_CST:
14778 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14779 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14781 case VECTOR_CST:
14782 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14784 case CONSTRUCTOR:
14785 /* We need to do this when determining whether or not two
14786 non-type pointer to member function template arguments
14787 are the same. */
14788 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14789 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14790 return false;
14792 tree field, value;
14793 unsigned int i;
14794 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14796 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14797 if (!c_tree_equal (field, elt2->index)
14798 || !c_tree_equal (value, elt2->value))
14799 return false;
14802 return true;
14804 case TREE_LIST:
14805 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14806 return false;
14807 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14808 return false;
14809 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14811 case SAVE_EXPR:
14812 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14814 case CALL_EXPR:
14816 tree arg1, arg2;
14817 call_expr_arg_iterator iter1, iter2;
14818 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14819 return false;
14820 for (arg1 = first_call_expr_arg (t1, &iter1),
14821 arg2 = first_call_expr_arg (t2, &iter2);
14822 arg1 && arg2;
14823 arg1 = next_call_expr_arg (&iter1),
14824 arg2 = next_call_expr_arg (&iter2))
14825 if (!c_tree_equal (arg1, arg2))
14826 return false;
14827 if (arg1 || arg2)
14828 return false;
14829 return true;
14832 case TARGET_EXPR:
14834 tree o1 = TREE_OPERAND (t1, 0);
14835 tree o2 = TREE_OPERAND (t2, 0);
14837 /* Special case: if either target is an unallocated VAR_DECL,
14838 it means that it's going to be unified with whatever the
14839 TARGET_EXPR is really supposed to initialize, so treat it
14840 as being equivalent to anything. */
14841 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14842 && !DECL_RTL_SET_P (o1))
14843 /*Nop*/;
14844 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14845 && !DECL_RTL_SET_P (o2))
14846 /*Nop*/;
14847 else if (!c_tree_equal (o1, o2))
14848 return false;
14850 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14853 case COMPONENT_REF:
14854 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14855 return false;
14856 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14858 case PARM_DECL:
14859 case VAR_DECL:
14860 case CONST_DECL:
14861 case FIELD_DECL:
14862 case FUNCTION_DECL:
14863 case IDENTIFIER_NODE:
14864 case SSA_NAME:
14865 return false;
14867 case TREE_VEC:
14869 unsigned ix;
14870 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14871 return false;
14872 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14873 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14874 TREE_VEC_ELT (t2, ix)))
14875 return false;
14876 return true;
14879 default:
14880 break;
14883 switch (TREE_CODE_CLASS (code1))
14885 case tcc_unary:
14886 case tcc_binary:
14887 case tcc_comparison:
14888 case tcc_expression:
14889 case tcc_vl_exp:
14890 case tcc_reference:
14891 case tcc_statement:
14893 int i, n = TREE_OPERAND_LENGTH (t1);
14895 switch (code1)
14897 case PREINCREMENT_EXPR:
14898 case PREDECREMENT_EXPR:
14899 case POSTINCREMENT_EXPR:
14900 case POSTDECREMENT_EXPR:
14901 n = 1;
14902 break;
14903 case ARRAY_REF:
14904 n = 2;
14905 break;
14906 default:
14907 break;
14910 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14911 && n != TREE_OPERAND_LENGTH (t2))
14912 return false;
14914 for (i = 0; i < n; ++i)
14915 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14916 return false;
14918 return true;
14921 case tcc_type:
14922 return comptypes (t1, t2);
14923 default:
14924 gcc_unreachable ();
14926 /* We can get here with --disable-checking. */
14927 return false;
14930 /* Returns true when the function declaration FNDECL is implicit,
14931 introduced as a result of a call to an otherwise undeclared
14932 function, and false otherwise. */
14934 bool
14935 c_decl_implicit (const_tree fndecl)
14937 return C_DECL_IMPLICIT (fndecl);