[SFN] introduce statement frontier notes, still disabled
[official-gcc.git] / gcc / c / c-typeck.c
blob13b26845d97a8be17504a11f35fc3a2733ea1e4c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2017 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 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p,
1182 different_types_p)))
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 = (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 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1666 enum_and_int_p, different_types_p))
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 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1676 enum_and_int_p, different_types_p))
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)
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 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
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 && 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) : 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 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2125 TYPE_PRECISION (integer_type_node)))
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 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2214 int bot, top, half;
2215 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2217 field = TYPE_FIELDS (type);
2218 bot = 0;
2219 top = TYPE_LANG_SPECIFIC (type)->s->len;
2220 while (top - bot > 1)
2222 half = (top - bot + 1) >> 1;
2223 field = field_array[bot+half];
2225 if (DECL_NAME (field) == NULL_TREE)
2227 /* Step through all anon unions in linear fashion. */
2228 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2230 field = field_array[bot++];
2231 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2233 tree anon = lookup_field (TREE_TYPE (field), component);
2235 if (anon)
2236 return tree_cons (NULL_TREE, field, anon);
2238 /* The Plan 9 compiler permits referring
2239 directly to an anonymous struct/union field
2240 using a typedef name. */
2241 if (flag_plan9_extensions
2242 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2243 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2244 == TYPE_DECL)
2245 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2246 == component))
2247 break;
2251 /* Entire record is only anon unions. */
2252 if (bot > top)
2253 return NULL_TREE;
2255 /* Restart the binary search, with new lower bound. */
2256 continue;
2259 if (DECL_NAME (field) == component)
2260 break;
2261 if (DECL_NAME (field) < component)
2262 bot += half;
2263 else
2264 top = bot + half;
2267 if (DECL_NAME (field_array[bot]) == component)
2268 field = field_array[bot];
2269 else if (DECL_NAME (field) != component)
2270 return NULL_TREE;
2272 else
2274 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2276 if (DECL_NAME (field) == NULL_TREE
2277 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2279 tree anon = lookup_field (TREE_TYPE (field), component);
2281 if (anon)
2282 return tree_cons (NULL_TREE, field, anon);
2284 /* The Plan 9 compiler permits referring directly to an
2285 anonymous struct/union field using a typedef
2286 name. */
2287 if (flag_plan9_extensions
2288 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2289 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2290 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2291 == component))
2292 break;
2295 if (DECL_NAME (field) == component)
2296 break;
2299 if (field == NULL_TREE)
2300 return NULL_TREE;
2303 return tree_cons (NULL_TREE, field, NULL_TREE);
2306 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2308 static void
2309 lookup_field_fuzzy_find_candidates (tree type, tree component,
2310 vec<tree> *candidates)
2312 tree field;
2313 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2315 if (DECL_NAME (field) == NULL_TREE
2316 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2317 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2318 candidates);
2320 if (DECL_NAME (field))
2321 candidates->safe_push (DECL_NAME (field));
2325 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2326 rather than returning a TREE_LIST for an exact match. */
2328 static tree
2329 lookup_field_fuzzy (tree type, tree component)
2331 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2333 /* First, gather a list of candidates. */
2334 auto_vec <tree> candidates;
2336 lookup_field_fuzzy_find_candidates (type, component,
2337 &candidates);
2339 return find_closest_identifier (component, &candidates);
2342 /* Support function for build_component_ref's error-handling.
2344 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2345 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2347 static bool
2348 should_suggest_deref_p (tree datum_type)
2350 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2351 allows "." for ptrs; we could be handling a failed attempt
2352 to access a property. */
2353 if (c_dialect_objc ())
2354 return false;
2356 /* Only suggest it for pointers... */
2357 if (TREE_CODE (datum_type) != POINTER_TYPE)
2358 return false;
2360 /* ...to structs/unions. */
2361 tree underlying_type = TREE_TYPE (datum_type);
2362 enum tree_code code = TREE_CODE (underlying_type);
2363 if (code == RECORD_TYPE || code == UNION_TYPE)
2364 return true;
2365 else
2366 return false;
2369 /* Make an expression to refer to the COMPONENT field of structure or
2370 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2371 location of the COMPONENT_REF. COMPONENT_LOC is the location
2372 of COMPONENT. */
2374 tree
2375 build_component_ref (location_t loc, tree datum, tree component,
2376 location_t component_loc)
2378 tree type = TREE_TYPE (datum);
2379 enum tree_code code = TREE_CODE (type);
2380 tree field = NULL;
2381 tree ref;
2382 bool datum_lvalue = lvalue_p (datum);
2384 if (!objc_is_public (datum, component))
2385 return error_mark_node;
2387 /* Detect Objective-C property syntax object.property. */
2388 if (c_dialect_objc ()
2389 && (ref = objc_maybe_build_component_ref (datum, component)))
2390 return ref;
2392 /* See if there is a field or component with name COMPONENT. */
2394 if (code == RECORD_TYPE || code == UNION_TYPE)
2396 if (!COMPLETE_TYPE_P (type))
2398 c_incomplete_type_error (loc, NULL_TREE, type);
2399 return error_mark_node;
2402 field = lookup_field (type, component);
2404 if (!field)
2406 tree guessed_id = lookup_field_fuzzy (type, component);
2407 if (guessed_id)
2409 /* Attempt to provide a fixit replacement hint, if
2410 we have a valid range for the component. */
2411 location_t reported_loc
2412 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2413 gcc_rich_location rich_loc (reported_loc);
2414 if (component_loc != UNKNOWN_LOCATION)
2415 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2416 error_at (&rich_loc,
2417 "%qT has no member named %qE; did you mean %qE?",
2418 type, component, guessed_id);
2420 else
2421 error_at (loc, "%qT has no member named %qE", type, component);
2422 return error_mark_node;
2425 /* Accessing elements of atomic structures or unions is undefined
2426 behavior (C11 6.5.2.3#5). */
2427 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2429 if (code == RECORD_TYPE)
2430 warning_at (loc, 0, "accessing a member %qE of an atomic "
2431 "structure %qE", component, datum);
2432 else
2433 warning_at (loc, 0, "accessing a member %qE of an atomic "
2434 "union %qE", component, datum);
2437 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2438 This might be better solved in future the way the C++ front
2439 end does it - by giving the anonymous entities each a
2440 separate name and type, and then have build_component_ref
2441 recursively call itself. We can't do that here. */
2444 tree subdatum = TREE_VALUE (field);
2445 int quals;
2446 tree subtype;
2447 bool use_datum_quals;
2449 if (TREE_TYPE (subdatum) == error_mark_node)
2450 return error_mark_node;
2452 /* If this is an rvalue, it does not have qualifiers in C
2453 standard terms and we must avoid propagating such
2454 qualifiers down to a non-lvalue array that is then
2455 converted to a pointer. */
2456 use_datum_quals = (datum_lvalue
2457 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2459 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2460 if (use_datum_quals)
2461 quals |= TYPE_QUALS (TREE_TYPE (datum));
2462 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2464 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2465 NULL_TREE);
2466 SET_EXPR_LOCATION (ref, loc);
2467 if (TREE_READONLY (subdatum)
2468 || (use_datum_quals && TREE_READONLY (datum)))
2469 TREE_READONLY (ref) = 1;
2470 if (TREE_THIS_VOLATILE (subdatum)
2471 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2472 TREE_THIS_VOLATILE (ref) = 1;
2474 if (TREE_DEPRECATED (subdatum))
2475 warn_deprecated_use (subdatum, NULL_TREE);
2477 datum = ref;
2479 field = TREE_CHAIN (field);
2481 while (field);
2483 return ref;
2485 else if (should_suggest_deref_p (type))
2487 /* Special-case the error message for "ptr.field" for the case
2488 where the user has confused "." vs "->". */
2489 rich_location richloc (line_table, loc);
2490 /* "loc" should be the "." token. */
2491 richloc.add_fixit_replace ("->");
2492 error_at (&richloc,
2493 "%qE is a pointer; did you mean to use %<->%>?",
2494 datum);
2495 return error_mark_node;
2497 else if (code != ERROR_MARK)
2498 error_at (loc,
2499 "request for member %qE in something not a structure or union",
2500 component);
2502 return error_mark_node;
2505 /* Given an expression PTR for a pointer, return an expression
2506 for the value pointed to.
2507 ERRORSTRING is the name of the operator to appear in error messages.
2509 LOC is the location to use for the generated tree. */
2511 tree
2512 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2514 tree pointer = default_conversion (ptr);
2515 tree type = TREE_TYPE (pointer);
2516 tree ref;
2518 if (TREE_CODE (type) == POINTER_TYPE)
2520 if (CONVERT_EXPR_P (pointer)
2521 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2523 /* If a warning is issued, mark it to avoid duplicates from
2524 the backend. This only needs to be done at
2525 warn_strict_aliasing > 2. */
2526 if (warn_strict_aliasing > 2)
2527 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2528 type, TREE_OPERAND (pointer, 0)))
2529 TREE_NO_WARNING (pointer) = 1;
2532 if (TREE_CODE (pointer) == ADDR_EXPR
2533 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2534 == TREE_TYPE (type)))
2536 ref = TREE_OPERAND (pointer, 0);
2537 protected_set_expr_location (ref, loc);
2538 return ref;
2540 else
2542 tree t = TREE_TYPE (type);
2544 ref = build1 (INDIRECT_REF, t, pointer);
2546 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2548 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2550 error_at (loc, "dereferencing pointer to incomplete type "
2551 "%qT", t);
2552 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2554 return error_mark_node;
2556 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2557 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2559 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2560 so that we get the proper error message if the result is used
2561 to assign to. Also, &* is supposed to be a no-op.
2562 And ANSI C seems to specify that the type of the result
2563 should be the const type. */
2564 /* A de-reference of a pointer to const is not a const. It is valid
2565 to change it via some other pointer. */
2566 TREE_READONLY (ref) = TYPE_READONLY (t);
2567 TREE_SIDE_EFFECTS (ref)
2568 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2569 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2570 protected_set_expr_location (ref, loc);
2571 return ref;
2574 else if (TREE_CODE (pointer) != ERROR_MARK)
2575 invalid_indirection_error (loc, type, errstring);
2577 return error_mark_node;
2580 /* This handles expressions of the form "a[i]", which denotes
2581 an array reference.
2583 This is logically equivalent in C to *(a+i), but we may do it differently.
2584 If A is a variable or a member, we generate a primitive ARRAY_REF.
2585 This avoids forcing the array out of registers, and can work on
2586 arrays that are not lvalues (for example, members of structures returned
2587 by functions).
2589 For vector types, allow vector[i] but not i[vector], and create
2590 *(((type*)&vectortype) + i) for the expression.
2592 LOC is the location to use for the returned expression. */
2594 tree
2595 build_array_ref (location_t loc, tree array, tree index)
2597 tree ret;
2598 bool swapped = false;
2599 if (TREE_TYPE (array) == error_mark_node
2600 || TREE_TYPE (index) == error_mark_node)
2601 return error_mark_node;
2603 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2604 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2605 /* Allow vector[index] but not index[vector]. */
2606 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2608 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2609 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2611 error_at (loc,
2612 "subscripted value is neither array nor pointer nor vector");
2614 return error_mark_node;
2616 std::swap (array, index);
2617 swapped = true;
2620 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2622 error_at (loc, "array subscript is not an integer");
2623 return error_mark_node;
2626 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2628 error_at (loc, "subscripted value is pointer to function");
2629 return error_mark_node;
2632 /* ??? Existing practice has been to warn only when the char
2633 index is syntactically the index, not for char[array]. */
2634 if (!swapped)
2635 warn_array_subscript_with_type_char (loc, index);
2637 /* Apply default promotions *after* noticing character types. */
2638 index = default_conversion (index);
2639 if (index == error_mark_node)
2640 return error_mark_node;
2642 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2644 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2645 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2647 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2649 tree rval, type;
2651 /* An array that is indexed by a non-constant
2652 cannot be stored in a register; we must be able to do
2653 address arithmetic on its address.
2654 Likewise an array of elements of variable size. */
2655 if (TREE_CODE (index) != INTEGER_CST
2656 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2657 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2659 if (!c_mark_addressable (array, true))
2660 return error_mark_node;
2662 /* An array that is indexed by a constant value which is not within
2663 the array bounds cannot be stored in a register either; because we
2664 would get a crash in store_bit_field/extract_bit_field when trying
2665 to access a non-existent part of the register. */
2666 if (TREE_CODE (index) == INTEGER_CST
2667 && TYPE_DOMAIN (TREE_TYPE (array))
2668 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2670 if (!c_mark_addressable (array))
2671 return error_mark_node;
2674 if ((pedantic || warn_c90_c99_compat)
2675 && ! was_vector)
2677 tree foo = array;
2678 while (TREE_CODE (foo) == COMPONENT_REF)
2679 foo = TREE_OPERAND (foo, 0);
2680 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2681 pedwarn (loc, OPT_Wpedantic,
2682 "ISO C forbids subscripting %<register%> array");
2683 else if (!lvalue_p (foo))
2684 pedwarn_c90 (loc, OPT_Wpedantic,
2685 "ISO C90 forbids subscripting non-lvalue "
2686 "array");
2689 type = TREE_TYPE (TREE_TYPE (array));
2690 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2691 /* Array ref is const/volatile if the array elements are
2692 or if the array is. */
2693 TREE_READONLY (rval)
2694 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2695 | TREE_READONLY (array));
2696 TREE_SIDE_EFFECTS (rval)
2697 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2698 | TREE_SIDE_EFFECTS (array));
2699 TREE_THIS_VOLATILE (rval)
2700 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2701 /* This was added by rms on 16 Nov 91.
2702 It fixes vol struct foo *a; a->elts[1]
2703 in an inline function.
2704 Hope it doesn't break something else. */
2705 | TREE_THIS_VOLATILE (array));
2706 ret = require_complete_type (loc, rval);
2707 protected_set_expr_location (ret, loc);
2708 if (non_lvalue)
2709 ret = non_lvalue_loc (loc, ret);
2710 return ret;
2712 else
2714 tree ar = default_conversion (array);
2716 if (ar == error_mark_node)
2717 return ar;
2719 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2720 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2722 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2723 index, false),
2724 RO_ARRAY_INDEXING);
2725 if (non_lvalue)
2726 ret = non_lvalue_loc (loc, ret);
2727 return ret;
2731 /* Build an external reference to identifier ID. FUN indicates
2732 whether this will be used for a function call. LOC is the source
2733 location of the identifier. This sets *TYPE to the type of the
2734 identifier, which is not the same as the type of the returned value
2735 for CONST_DECLs defined as enum constants. If the type of the
2736 identifier is not available, *TYPE is set to NULL. */
2737 tree
2738 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2740 tree ref;
2741 tree decl = lookup_name (id);
2743 /* In Objective-C, an instance variable (ivar) may be preferred to
2744 whatever lookup_name() found. */
2745 decl = objc_lookup_ivar (decl, id);
2747 *type = NULL;
2748 if (decl && decl != error_mark_node)
2750 ref = decl;
2751 *type = TREE_TYPE (ref);
2753 else if (fun)
2754 /* Implicit function declaration. */
2755 ref = implicitly_declare (loc, id);
2756 else if (decl == error_mark_node)
2757 /* Don't complain about something that's already been
2758 complained about. */
2759 return error_mark_node;
2760 else
2762 undeclared_variable (loc, id);
2763 return error_mark_node;
2766 if (TREE_TYPE (ref) == error_mark_node)
2767 return error_mark_node;
2769 if (TREE_DEPRECATED (ref))
2770 warn_deprecated_use (ref, NULL_TREE);
2772 /* Recursive call does not count as usage. */
2773 if (ref != current_function_decl)
2775 TREE_USED (ref) = 1;
2778 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2780 if (!in_sizeof && !in_typeof)
2781 C_DECL_USED (ref) = 1;
2782 else if (DECL_INITIAL (ref) == NULL_TREE
2783 && DECL_EXTERNAL (ref)
2784 && !TREE_PUBLIC (ref))
2785 record_maybe_used_decl (ref);
2788 if (TREE_CODE (ref) == CONST_DECL)
2790 used_types_insert (TREE_TYPE (ref));
2792 if (warn_cxx_compat
2793 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2794 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2796 warning_at (loc, OPT_Wc___compat,
2797 ("enum constant defined in struct or union "
2798 "is not visible in C++"));
2799 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2802 ref = DECL_INITIAL (ref);
2803 TREE_CONSTANT (ref) = 1;
2805 else if (current_function_decl != NULL_TREE
2806 && !DECL_FILE_SCOPE_P (current_function_decl)
2807 && (VAR_OR_FUNCTION_DECL_P (ref)
2808 || TREE_CODE (ref) == PARM_DECL))
2810 tree context = decl_function_context (ref);
2812 if (context != NULL_TREE && context != current_function_decl)
2813 DECL_NONLOCAL (ref) = 1;
2815 /* C99 6.7.4p3: An inline definition of a function with external
2816 linkage ... shall not contain a reference to an identifier with
2817 internal linkage. */
2818 else if (current_function_decl != NULL_TREE
2819 && DECL_DECLARED_INLINE_P (current_function_decl)
2820 && DECL_EXTERNAL (current_function_decl)
2821 && VAR_OR_FUNCTION_DECL_P (ref)
2822 && (!VAR_P (ref) || TREE_STATIC (ref))
2823 && ! TREE_PUBLIC (ref)
2824 && DECL_CONTEXT (ref) != current_function_decl)
2825 record_inline_static (loc, current_function_decl, ref,
2826 csi_internal);
2828 return ref;
2831 /* Record details of decls possibly used inside sizeof or typeof. */
2832 struct maybe_used_decl
2834 /* The decl. */
2835 tree decl;
2836 /* The level seen at (in_sizeof + in_typeof). */
2837 int level;
2838 /* The next one at this level or above, or NULL. */
2839 struct maybe_used_decl *next;
2842 static struct maybe_used_decl *maybe_used_decls;
2844 /* Record that DECL, an undefined static function reference seen
2845 inside sizeof or typeof, might be used if the operand of sizeof is
2846 a VLA type or the operand of typeof is a variably modified
2847 type. */
2849 static void
2850 record_maybe_used_decl (tree decl)
2852 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2853 t->decl = decl;
2854 t->level = in_sizeof + in_typeof;
2855 t->next = maybe_used_decls;
2856 maybe_used_decls = t;
2859 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2860 USED is false, just discard them. If it is true, mark them used
2861 (if no longer inside sizeof or typeof) or move them to the next
2862 level up (if still inside sizeof or typeof). */
2864 void
2865 pop_maybe_used (bool used)
2867 struct maybe_used_decl *p = maybe_used_decls;
2868 int cur_level = in_sizeof + in_typeof;
2869 while (p && p->level > cur_level)
2871 if (used)
2873 if (cur_level == 0)
2874 C_DECL_USED (p->decl) = 1;
2875 else
2876 p->level = cur_level;
2878 p = p->next;
2880 if (!used || cur_level == 0)
2881 maybe_used_decls = p;
2884 /* Return the result of sizeof applied to EXPR. */
2886 struct c_expr
2887 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2889 struct c_expr ret;
2890 if (expr.value == error_mark_node)
2892 ret.value = error_mark_node;
2893 ret.original_code = ERROR_MARK;
2894 ret.original_type = NULL;
2895 pop_maybe_used (false);
2897 else
2899 bool expr_const_operands = true;
2901 if (TREE_CODE (expr.value) == PARM_DECL
2902 && C_ARRAY_PARAMETER (expr.value))
2904 if (warning_at (loc, OPT_Wsizeof_array_argument,
2905 "%<sizeof%> on array function parameter %qE will "
2906 "return size of %qT", expr.value,
2907 TREE_TYPE (expr.value)))
2908 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2910 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2911 &expr_const_operands);
2912 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2913 c_last_sizeof_arg = expr.value;
2914 c_last_sizeof_loc = loc;
2915 ret.original_code = SIZEOF_EXPR;
2916 ret.original_type = NULL;
2917 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2919 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2920 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2921 folded_expr, ret.value);
2922 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2923 SET_EXPR_LOCATION (ret.value, loc);
2925 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2927 return ret;
2930 /* Return the result of sizeof applied to T, a structure for the type
2931 name passed to sizeof (rather than the type itself). LOC is the
2932 location of the original expression. */
2934 struct c_expr
2935 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2937 tree type;
2938 struct c_expr ret;
2939 tree type_expr = NULL_TREE;
2940 bool type_expr_const = true;
2941 type = groktypename (t, &type_expr, &type_expr_const);
2942 ret.value = c_sizeof (loc, type);
2943 c_last_sizeof_arg = type;
2944 c_last_sizeof_loc = loc;
2945 ret.original_code = SIZEOF_EXPR;
2946 ret.original_type = NULL;
2947 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2948 && c_vla_type_p (type))
2950 /* If the type is a [*] array, it is a VLA but is represented as
2951 having a size of zero. In such a case we must ensure that
2952 the result of sizeof does not get folded to a constant by
2953 c_fully_fold, because if the size is evaluated the result is
2954 not constant and so constraints on zero or negative size
2955 arrays must not be applied when this sizeof call is inside
2956 another array declarator. */
2957 if (!type_expr)
2958 type_expr = integer_zero_node;
2959 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2960 type_expr, ret.value);
2961 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2963 pop_maybe_used (type != error_mark_node
2964 ? C_TYPE_VARIABLE_SIZE (type) : false);
2965 return ret;
2968 /* Build a function call to function FUNCTION with parameters PARAMS.
2969 The function call is at LOC.
2970 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2971 TREE_VALUE of each node is a parameter-expression.
2972 FUNCTION's data type may be a function type or a pointer-to-function. */
2974 tree
2975 build_function_call (location_t loc, tree function, tree params)
2977 vec<tree, va_gc> *v;
2978 tree ret;
2980 vec_alloc (v, list_length (params));
2981 for (; params; params = TREE_CHAIN (params))
2982 v->quick_push (TREE_VALUE (params));
2983 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2984 vec_free (v);
2985 return ret;
2988 /* Give a note about the location of the declaration of DECL. */
2990 static void
2991 inform_declaration (tree decl)
2993 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2994 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2997 /* Build a function call to function FUNCTION with parameters PARAMS.
2998 ORIGTYPES, if not NULL, is a vector of types; each element is
2999 either NULL or the original type of the corresponding element in
3000 PARAMS. The original type may differ from TREE_TYPE of the
3001 parameter for enums. FUNCTION's data type may be a function type
3002 or pointer-to-function. This function changes the elements of
3003 PARAMS. */
3005 tree
3006 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3007 tree function, vec<tree, va_gc> *params,
3008 vec<tree, va_gc> *origtypes)
3010 tree fntype, fundecl = NULL_TREE;
3011 tree name = NULL_TREE, result;
3012 tree tem;
3013 int nargs;
3014 tree *argarray;
3017 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3018 STRIP_TYPE_NOPS (function);
3020 /* Convert anything with function type to a pointer-to-function. */
3021 if (TREE_CODE (function) == FUNCTION_DECL)
3023 name = DECL_NAME (function);
3025 if (flag_tm)
3026 tm_malloc_replacement (function);
3027 fundecl = function;
3028 /* Atomic functions have type checking/casting already done. They are
3029 often rewritten and don't match the original parameter list. */
3030 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3031 origtypes = NULL;
3033 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3034 function = function_to_pointer_conversion (loc, function);
3036 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3037 expressions, like those used for ObjC messenger dispatches. */
3038 if (params && !params->is_empty ())
3039 function = objc_rewrite_function_call (function, (*params)[0]);
3041 function = c_fully_fold (function, false, NULL);
3043 fntype = TREE_TYPE (function);
3045 if (TREE_CODE (fntype) == ERROR_MARK)
3046 return error_mark_node;
3048 if (!(TREE_CODE (fntype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3051 if (!flag_diagnostics_show_caret)
3052 error_at (loc,
3053 "called object %qE is not a function or function pointer",
3054 function);
3055 else if (DECL_P (function))
3057 error_at (loc,
3058 "called object %qD is not a function or function pointer",
3059 function);
3060 inform_declaration (function);
3062 else
3063 error_at (loc,
3064 "called object is not a function or function pointer");
3065 return error_mark_node;
3068 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3069 current_function_returns_abnormally = 1;
3071 /* fntype now gets the type of function pointed to. */
3072 fntype = TREE_TYPE (fntype);
3074 /* Convert the parameters to the types declared in the
3075 function prototype, or apply default promotions. */
3077 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3078 origtypes, function, fundecl);
3079 if (nargs < 0)
3080 return error_mark_node;
3082 /* Check that the function is called through a compatible prototype.
3083 If it is not, warn. */
3084 if (CONVERT_EXPR_P (function)
3085 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3086 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3087 && !comptypes (fntype, TREE_TYPE (tem)))
3089 tree return_type = TREE_TYPE (fntype);
3091 /* This situation leads to run-time undefined behavior. We can't,
3092 therefore, simply error unless we can prove that all possible
3093 executions of the program must execute the code. */
3094 warning_at (loc, 0, "function called through a non-compatible type");
3096 if (VOID_TYPE_P (return_type)
3097 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3098 pedwarn (loc, 0,
3099 "function with qualified void return type called");
3102 argarray = vec_safe_address (params);
3104 /* Check that arguments to builtin functions match the expectations. */
3105 if (fundecl
3106 && DECL_BUILT_IN (fundecl)
3107 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3108 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3109 argarray))
3110 return error_mark_node;
3112 /* Check that the arguments to the function are valid. */
3113 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3114 nargs, argarray, &arg_loc);
3116 if (name != NULL_TREE
3117 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3119 if (require_constant_value)
3120 result
3121 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3122 function, nargs, argarray);
3123 else
3124 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3125 function, nargs, argarray);
3126 if (TREE_CODE (result) == NOP_EXPR
3127 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3128 STRIP_TYPE_NOPS (result);
3130 else
3131 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3132 function, nargs, argarray);
3133 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3134 later. */
3135 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3136 TREE_NO_WARNING (result) = 1;
3138 /* In this improbable scenario, a nested function returns a VM type.
3139 Create a TARGET_EXPR so that the call always has a LHS, much as
3140 what the C++ FE does for functions returning non-PODs. */
3141 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3143 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3144 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3145 NULL_TREE, NULL_TREE);
3148 if (VOID_TYPE_P (TREE_TYPE (result)))
3150 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3151 pedwarn (loc, 0,
3152 "function with qualified void return type called");
3153 return result;
3155 return require_complete_type (loc, result);
3158 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3160 tree
3161 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3162 tree function, vec<tree, va_gc> *params,
3163 vec<tree, va_gc> *origtypes)
3165 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3166 STRIP_TYPE_NOPS (function);
3168 /* Convert anything with function type to a pointer-to-function. */
3169 if (TREE_CODE (function) == FUNCTION_DECL)
3171 /* Implement type-directed function overloading for builtins.
3172 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3173 handle all the type checking. The result is a complete expression
3174 that implements this function call. */
3175 tree tem = resolve_overloaded_builtin (loc, function, params);
3176 if (tem)
3177 return tem;
3179 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3182 /* Convert the argument expressions in the vector VALUES
3183 to the types in the list TYPELIST.
3185 If TYPELIST is exhausted, or when an element has NULL as its type,
3186 perform the default conversions.
3188 ORIGTYPES is the original types of the expressions in VALUES. This
3189 holds the type of enum values which have been converted to integral
3190 types. It may be NULL.
3192 FUNCTION is a tree for the called function. It is used only for
3193 error messages, where it is formatted with %qE.
3195 This is also where warnings about wrong number of args are generated.
3197 ARG_LOC are locations of function arguments (if any).
3199 Returns the actual number of arguments processed (which may be less
3200 than the length of VALUES in some error situations), or -1 on
3201 failure. */
3203 static int
3204 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3205 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3206 tree function, tree fundecl)
3208 tree typetail, val;
3209 unsigned int parmnum;
3210 bool error_args = false;
3211 const bool type_generic = fundecl
3212 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3213 bool type_generic_remove_excess_precision = false;
3214 bool type_generic_overflow_p = false;
3215 tree selector;
3217 /* Change pointer to function to the function itself for
3218 diagnostics. */
3219 if (TREE_CODE (function) == ADDR_EXPR
3220 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3221 function = TREE_OPERAND (function, 0);
3223 /* Handle an ObjC selector specially for diagnostics. */
3224 selector = objc_message_selector ();
3226 /* For type-generic built-in functions, determine whether excess
3227 precision should be removed (classification) or not
3228 (comparison). */
3229 if (type_generic
3230 && DECL_BUILT_IN (fundecl)
3231 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3233 switch (DECL_FUNCTION_CODE (fundecl))
3235 case BUILT_IN_ISFINITE:
3236 case BUILT_IN_ISINF:
3237 case BUILT_IN_ISINF_SIGN:
3238 case BUILT_IN_ISNAN:
3239 case BUILT_IN_ISNORMAL:
3240 case BUILT_IN_FPCLASSIFY:
3241 type_generic_remove_excess_precision = true;
3242 break;
3244 case BUILT_IN_ADD_OVERFLOW_P:
3245 case BUILT_IN_SUB_OVERFLOW_P:
3246 case BUILT_IN_MUL_OVERFLOW_P:
3247 /* The last argument of these type-generic builtins
3248 should not be promoted. */
3249 type_generic_overflow_p = true;
3250 break;
3252 default:
3253 break;
3257 /* Scan the given expressions and types, producing individual
3258 converted arguments. */
3260 for (typetail = typelist, parmnum = 0;
3261 values && values->iterate (parmnum, &val);
3262 ++parmnum)
3264 tree type = typetail ? TREE_VALUE (typetail) : 0;
3265 tree valtype = TREE_TYPE (val);
3266 tree rname = function;
3267 int argnum = parmnum + 1;
3268 const char *invalid_func_diag;
3269 bool excess_precision = false;
3270 bool npc;
3271 tree parmval;
3272 /* Some __atomic_* builtins have additional hidden argument at
3273 position 0. */
3274 location_t ploc
3275 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3276 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3277 : input_location;
3279 if (type == void_type_node)
3281 if (selector)
3282 error_at (loc, "too many arguments to method %qE", selector);
3283 else
3284 error_at (loc, "too many arguments to function %qE", function);
3285 inform_declaration (fundecl);
3286 return error_args ? -1 : (int) parmnum;
3289 if (selector && argnum > 2)
3291 rname = selector;
3292 argnum -= 2;
3295 npc = null_pointer_constant_p (val);
3297 /* If there is excess precision and a prototype, convert once to
3298 the required type rather than converting via the semantic
3299 type. Likewise without a prototype a float value represented
3300 as long double should be converted once to double. But for
3301 type-generic classification functions excess precision must
3302 be removed here. */
3303 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3304 && (type || !type_generic || !type_generic_remove_excess_precision))
3306 val = TREE_OPERAND (val, 0);
3307 excess_precision = true;
3309 val = c_fully_fold (val, false, NULL);
3310 STRIP_TYPE_NOPS (val);
3312 val = require_complete_type (ploc, val);
3314 /* Some floating-point arguments must be promoted to double when
3315 no type is specified by a prototype. This applies to
3316 arguments of type float, and to architecture-specific types
3317 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3318 bool promote_float_arg = false;
3319 if (type == NULL_TREE
3320 && TREE_CODE (valtype) == REAL_TYPE
3321 && (TYPE_PRECISION (valtype)
3322 <= TYPE_PRECISION (double_type_node))
3323 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3324 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3325 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3327 /* Promote this argument, unless it has a _FloatN or
3328 _FloatNx type. */
3329 promote_float_arg = true;
3330 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3331 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3333 promote_float_arg = false;
3334 break;
3338 if (type != NULL_TREE)
3340 /* Formal parm type is specified by a function prototype. */
3342 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3344 error_at (ploc, "type of formal parameter %d is incomplete",
3345 parmnum + 1);
3346 parmval = val;
3348 else
3350 tree origtype;
3352 /* Optionally warn about conversions that
3353 differ from the default conversions. */
3354 if (warn_traditional_conversion || warn_traditional)
3356 unsigned int formal_prec = TYPE_PRECISION (type);
3358 if (INTEGRAL_TYPE_P (type)
3359 && TREE_CODE (valtype) == REAL_TYPE)
3360 warning_at (ploc, OPT_Wtraditional_conversion,
3361 "passing argument %d of %qE as integer rather "
3362 "than floating due to prototype",
3363 argnum, rname);
3364 if (INTEGRAL_TYPE_P (type)
3365 && TREE_CODE (valtype) == COMPLEX_TYPE)
3366 warning_at (ploc, OPT_Wtraditional_conversion,
3367 "passing argument %d of %qE as integer rather "
3368 "than complex due to prototype",
3369 argnum, rname);
3370 else if (TREE_CODE (type) == COMPLEX_TYPE
3371 && TREE_CODE (valtype) == REAL_TYPE)
3372 warning_at (ploc, OPT_Wtraditional_conversion,
3373 "passing argument %d of %qE as complex rather "
3374 "than floating due to prototype",
3375 argnum, rname);
3376 else if (TREE_CODE (type) == REAL_TYPE
3377 && INTEGRAL_TYPE_P (valtype))
3378 warning_at (ploc, OPT_Wtraditional_conversion,
3379 "passing argument %d of %qE as floating rather "
3380 "than integer due to prototype",
3381 argnum, rname);
3382 else if (TREE_CODE (type) == COMPLEX_TYPE
3383 && INTEGRAL_TYPE_P (valtype))
3384 warning_at (ploc, OPT_Wtraditional_conversion,
3385 "passing argument %d of %qE as complex rather "
3386 "than integer due to prototype",
3387 argnum, rname);
3388 else if (TREE_CODE (type) == REAL_TYPE
3389 && TREE_CODE (valtype) == COMPLEX_TYPE)
3390 warning_at (ploc, OPT_Wtraditional_conversion,
3391 "passing argument %d of %qE as floating rather "
3392 "than complex due to prototype",
3393 argnum, rname);
3394 /* ??? At some point, messages should be written about
3395 conversions between complex types, but that's too messy
3396 to do now. */
3397 else if (TREE_CODE (type) == REAL_TYPE
3398 && TREE_CODE (valtype) == REAL_TYPE)
3400 /* Warn if any argument is passed as `float',
3401 since without a prototype it would be `double'. */
3402 if (formal_prec == TYPE_PRECISION (float_type_node)
3403 && type != dfloat32_type_node)
3404 warning_at (ploc, 0,
3405 "passing argument %d of %qE as %<float%> "
3406 "rather than %<double%> due to prototype",
3407 argnum, rname);
3409 /* Warn if mismatch between argument and prototype
3410 for decimal float types. Warn of conversions with
3411 binary float types and of precision narrowing due to
3412 prototype. */
3413 else if (type != valtype
3414 && (type == dfloat32_type_node
3415 || type == dfloat64_type_node
3416 || type == dfloat128_type_node
3417 || valtype == dfloat32_type_node
3418 || valtype == dfloat64_type_node
3419 || valtype == dfloat128_type_node)
3420 && (formal_prec
3421 <= TYPE_PRECISION (valtype)
3422 || (type == dfloat128_type_node
3423 && (valtype
3424 != dfloat64_type_node
3425 && (valtype
3426 != dfloat32_type_node)))
3427 || (type == dfloat64_type_node
3428 && (valtype
3429 != dfloat32_type_node))))
3430 warning_at (ploc, 0,
3431 "passing argument %d of %qE as %qT "
3432 "rather than %qT due to prototype",
3433 argnum, rname, type, valtype);
3436 /* Detect integer changing in width or signedness.
3437 These warnings are only activated with
3438 -Wtraditional-conversion, not with -Wtraditional. */
3439 else if (warn_traditional_conversion
3440 && INTEGRAL_TYPE_P (type)
3441 && INTEGRAL_TYPE_P (valtype))
3443 tree would_have_been = default_conversion (val);
3444 tree type1 = TREE_TYPE (would_have_been);
3446 if (val == error_mark_node)
3447 /* VAL could have been of incomplete type. */;
3448 else if (TREE_CODE (type) == ENUMERAL_TYPE
3449 && (TYPE_MAIN_VARIANT (type)
3450 == TYPE_MAIN_VARIANT (valtype)))
3451 /* No warning if function asks for enum
3452 and the actual arg is that enum type. */
3454 else if (formal_prec != TYPE_PRECISION (type1))
3455 warning_at (ploc, OPT_Wtraditional_conversion,
3456 "passing argument %d of %qE "
3457 "with different width due to prototype",
3458 argnum, rname);
3459 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3461 /* Don't complain if the formal parameter type
3462 is an enum, because we can't tell now whether
3463 the value was an enum--even the same enum. */
3464 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3466 else if (TREE_CODE (val) == INTEGER_CST
3467 && int_fits_type_p (val, type))
3468 /* Change in signedness doesn't matter
3469 if a constant value is unaffected. */
3471 /* If the value is extended from a narrower
3472 unsigned type, it doesn't matter whether we
3473 pass it as signed or unsigned; the value
3474 certainly is the same either way. */
3475 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3476 && TYPE_UNSIGNED (valtype))
3478 else if (TYPE_UNSIGNED (type))
3479 warning_at (ploc, OPT_Wtraditional_conversion,
3480 "passing argument %d of %qE "
3481 "as unsigned due to prototype",
3482 argnum, rname);
3483 else
3484 warning_at (ploc, OPT_Wtraditional_conversion,
3485 "passing argument %d of %qE "
3486 "as signed due to prototype",
3487 argnum, rname);
3491 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3492 sake of better warnings from convert_and_check. */
3493 if (excess_precision)
3494 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3495 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3496 parmval = convert_for_assignment (loc, ploc, type,
3497 val, origtype, ic_argpass,
3498 npc, fundecl, function,
3499 parmnum + 1);
3501 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3502 && INTEGRAL_TYPE_P (type)
3503 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3504 parmval = default_conversion (parmval);
3507 else if (promote_float_arg)
3509 if (type_generic)
3510 parmval = val;
3511 else
3513 /* Convert `float' to `double'. */
3514 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3515 warning_at (ploc, OPT_Wdouble_promotion,
3516 "implicit conversion from %qT to %qT when passing "
3517 "argument to function",
3518 valtype, double_type_node);
3519 parmval = convert (double_type_node, val);
3522 else if ((excess_precision && !type_generic)
3523 || (type_generic_overflow_p && parmnum == 2))
3524 /* A "double" argument with excess precision being passed
3525 without a prototype or in variable arguments.
3526 The last argument of __builtin_*_overflow_p should not be
3527 promoted. */
3528 parmval = convert (valtype, val);
3529 else if ((invalid_func_diag =
3530 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3532 error (invalid_func_diag);
3533 return -1;
3535 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3537 return -1;
3539 else
3540 /* Convert `short' and `char' to full-size `int'. */
3541 parmval = default_conversion (val);
3543 (*values)[parmnum] = parmval;
3544 if (parmval == error_mark_node)
3545 error_args = true;
3547 if (typetail)
3548 typetail = TREE_CHAIN (typetail);
3551 gcc_assert (parmnum == vec_safe_length (values));
3553 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3555 error_at (loc, "too few arguments to function %qE", function);
3556 inform_declaration (fundecl);
3557 return -1;
3560 return error_args ? -1 : (int) parmnum;
3563 /* This is the entry point used by the parser to build unary operators
3564 in the input. CODE, a tree_code, specifies the unary operator, and
3565 ARG is the operand. For unary plus, the C parser currently uses
3566 CONVERT_EXPR for code.
3568 LOC is the location to use for the tree generated.
3571 struct c_expr
3572 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3574 struct c_expr result;
3576 result.original_code = code;
3577 result.original_type = NULL;
3579 if (reject_gcc_builtin (arg.value))
3581 result.value = error_mark_node;
3583 else
3585 result.value = build_unary_op (loc, code, arg.value, false);
3587 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3588 overflow_warning (loc, result.value, arg.value);
3591 /* We are typically called when parsing a prefix token at LOC acting on
3592 ARG. Reflect this by updating the source range of the result to
3593 start at LOC and end at the end of ARG. */
3594 set_c_expr_source_range (&result,
3595 loc, arg.get_finish ());
3597 return result;
3600 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3602 static bool
3603 char_type_p (tree type)
3605 return (type == char_type_node
3606 || type == unsigned_char_type_node
3607 || type == signed_char_type_node
3608 || type == char16_type_node
3609 || type == char32_type_node);
3612 /* This is the entry point used by the parser to build binary operators
3613 in the input. CODE, a tree_code, specifies the binary operator, and
3614 ARG1 and ARG2 are the operands. In addition to constructing the
3615 expression, we check for operands that were written with other binary
3616 operators in a way that is likely to confuse the user.
3618 LOCATION is the location of the binary operator. */
3620 struct c_expr
3621 parser_build_binary_op (location_t location, enum tree_code code,
3622 struct c_expr arg1, struct c_expr arg2)
3624 struct c_expr result;
3626 enum tree_code code1 = arg1.original_code;
3627 enum tree_code code2 = arg2.original_code;
3628 tree type1 = (arg1.original_type
3629 ? arg1.original_type
3630 : TREE_TYPE (arg1.value));
3631 tree type2 = (arg2.original_type
3632 ? arg2.original_type
3633 : TREE_TYPE (arg2.value));
3635 result.value = build_binary_op (location, code,
3636 arg1.value, arg2.value, true);
3637 result.original_code = code;
3638 result.original_type = NULL;
3640 if (TREE_CODE (result.value) == ERROR_MARK)
3642 set_c_expr_source_range (&result,
3643 arg1.get_start (),
3644 arg2.get_finish ());
3645 return result;
3648 if (location != UNKNOWN_LOCATION)
3649 protected_set_expr_location (result.value, location);
3651 set_c_expr_source_range (&result,
3652 arg1.get_start (),
3653 arg2.get_finish ());
3655 /* Check for cases such as x+y<<z which users are likely
3656 to misinterpret. */
3657 if (warn_parentheses)
3658 warn_about_parentheses (location, code, code1, arg1.value, code2,
3659 arg2.value);
3661 if (warn_logical_op)
3662 warn_logical_operator (location, code, TREE_TYPE (result.value),
3663 code1, arg1.value, code2, arg2.value);
3665 if (warn_tautological_compare)
3667 tree lhs = arg1.value;
3668 tree rhs = arg2.value;
3669 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3671 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3672 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3673 lhs = NULL_TREE;
3674 else
3675 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3677 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3679 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3680 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3681 rhs = NULL_TREE;
3682 else
3683 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3685 if (lhs != NULL_TREE && rhs != NULL_TREE)
3686 warn_tautological_cmp (location, code, lhs, rhs);
3689 if (warn_logical_not_paren
3690 && TREE_CODE_CLASS (code) == tcc_comparison
3691 && code1 == TRUTH_NOT_EXPR
3692 && code2 != TRUTH_NOT_EXPR
3693 /* Avoid warning for !!x == y. */
3694 && (TREE_CODE (arg1.value) != NE_EXPR
3695 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3697 /* Avoid warning for !b == y where b has _Bool type. */
3698 tree t = integer_zero_node;
3699 if (TREE_CODE (arg1.value) == EQ_EXPR
3700 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3701 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3703 t = TREE_OPERAND (arg1.value, 0);
3706 if (TREE_TYPE (t) != integer_type_node)
3707 break;
3708 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3709 t = C_MAYBE_CONST_EXPR_EXPR (t);
3710 else if (CONVERT_EXPR_P (t))
3711 t = TREE_OPERAND (t, 0);
3712 else
3713 break;
3715 while (1);
3717 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3718 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3721 /* Warn about comparisons against string literals, with the exception
3722 of testing for equality or inequality of a string literal with NULL. */
3723 if (code == EQ_EXPR || code == NE_EXPR)
3725 if ((code1 == STRING_CST
3726 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3727 || (code2 == STRING_CST
3728 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3729 warning_at (location, OPT_Waddress,
3730 "comparison with string literal results in unspecified behavior");
3731 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3732 if (POINTER_TYPE_P (type1)
3733 && null_pointer_constant_p (arg2.value)
3734 && char_type_p (type2)
3735 && warning_at (location, OPT_Wpointer_compare,
3736 "comparison between pointer and zero character "
3737 "constant"))
3738 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3739 else if (POINTER_TYPE_P (type2)
3740 && null_pointer_constant_p (arg1.value)
3741 && char_type_p (type1)
3742 && warning_at (location, OPT_Wpointer_compare,
3743 "comparison between pointer and zero character "
3744 "constant"))
3745 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3747 else if (TREE_CODE_CLASS (code) == tcc_comparison
3748 && (code1 == STRING_CST || code2 == STRING_CST))
3749 warning_at (location, OPT_Waddress,
3750 "comparison with string literal results in unspecified behavior");
3752 if (TREE_OVERFLOW_P (result.value)
3753 && !TREE_OVERFLOW_P (arg1.value)
3754 && !TREE_OVERFLOW_P (arg2.value))
3755 overflow_warning (location, result.value);
3757 /* Warn about comparisons of different enum types. */
3758 if (warn_enum_compare
3759 && TREE_CODE_CLASS (code) == tcc_comparison
3760 && TREE_CODE (type1) == ENUMERAL_TYPE
3761 && TREE_CODE (type2) == ENUMERAL_TYPE
3762 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3763 warning_at (location, OPT_Wenum_compare,
3764 "comparison between %qT and %qT",
3765 type1, type2);
3767 return result;
3770 /* Return a tree for the difference of pointers OP0 and OP1.
3771 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3772 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3774 static tree
3775 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3777 tree restype = ptrdiff_type_node;
3778 tree result, inttype;
3780 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3781 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3782 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3783 tree orig_op1 = op1;
3785 /* If the operands point into different address spaces, we need to
3786 explicitly convert them to pointers into the common address space
3787 before we can subtract the numerical address values. */
3788 if (as0 != as1)
3790 addr_space_t as_common;
3791 tree common_type;
3793 /* Determine the common superset address space. This is guaranteed
3794 to exist because the caller verified that comp_target_types
3795 returned non-zero. */
3796 if (!addr_space_superset (as0, as1, &as_common))
3797 gcc_unreachable ();
3799 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3800 op0 = convert (common_type, op0);
3801 op1 = convert (common_type, op1);
3804 /* Determine integer type result of the subtraction. This will usually
3805 be the same as the result type (ptrdiff_t), but may need to be a wider
3806 type if pointers for the address space are wider than ptrdiff_t. */
3807 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3808 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3809 else
3810 inttype = restype;
3812 if (TREE_CODE (target_type) == VOID_TYPE)
3813 pedwarn (loc, OPT_Wpointer_arith,
3814 "pointer of type %<void *%> used in subtraction");
3815 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3816 pedwarn (loc, OPT_Wpointer_arith,
3817 "pointer to a function used in subtraction");
3819 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3821 gcc_assert (current_function_decl != NULL_TREE);
3823 op0 = save_expr (op0);
3824 op1 = save_expr (op1);
3826 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3827 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3830 /* First do the subtraction, then build the divide operator
3831 and only convert at the very end.
3832 Do not do default conversions in case restype is a short type. */
3834 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3835 pointers. If some platform cannot provide that, or has a larger
3836 ptrdiff_type to support differences larger than half the address
3837 space, cast the pointers to some larger integer type and do the
3838 computations in that type. */
3839 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3840 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3841 convert (inttype, op1), false);
3842 else
3843 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3845 /* This generates an error if op1 is pointer to incomplete type. */
3846 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3847 error_at (loc, "arithmetic on pointer to an incomplete type");
3849 op1 = c_size_in_bytes (target_type);
3851 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3852 error_at (loc, "arithmetic on pointer to an empty aggregate");
3854 /* Divide by the size, in easiest possible way. */
3855 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3856 op0, convert (inttype, op1));
3858 /* Convert to final result type if necessary. */
3859 return convert (restype, result);
3862 /* Expand atomic compound assignments into an appropriate sequence as
3863 specified by the C11 standard section 6.5.16.2.
3865 _Atomic T1 E1
3866 T2 E2
3867 E1 op= E2
3869 This sequence is used for all types for which these operations are
3870 supported.
3872 In addition, built-in versions of the 'fe' prefixed routines may
3873 need to be invoked for floating point (real, complex or vector) when
3874 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3876 T1 newval;
3877 T1 old;
3878 T1 *addr
3879 T2 val
3880 fenv_t fenv
3882 addr = &E1;
3883 val = (E2);
3884 __atomic_load (addr, &old, SEQ_CST);
3885 feholdexcept (&fenv);
3886 loop:
3887 newval = old op val;
3888 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3889 SEQ_CST))
3890 goto done;
3891 feclearexcept (FE_ALL_EXCEPT);
3892 goto loop:
3893 done:
3894 feupdateenv (&fenv);
3896 The compiler will issue the __atomic_fetch_* built-in when possible,
3897 otherwise it will generate the generic form of the atomic operations.
3898 This requires temp(s) and has their address taken. The atomic processing
3899 is smart enough to figure out when the size of an object can utilize
3900 a lock-free version, and convert the built-in call to the appropriate
3901 lock-free routine. The optimizers will then dispose of any temps that
3902 are no longer required, and lock-free implementations are utilized as
3903 long as there is target support for the required size.
3905 If the operator is NOP_EXPR, then this is a simple assignment, and
3906 an __atomic_store is issued to perform the assignment rather than
3907 the above loop. */
3909 /* Build an atomic assignment at LOC, expanding into the proper
3910 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3911 the result of the operation, unless RETURN_OLD_P, in which case
3912 return the old value of LHS (this is only for postincrement and
3913 postdecrement). */
3915 static tree
3916 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3917 tree rhs, bool return_old_p)
3919 tree fndecl, func_call;
3920 vec<tree, va_gc> *params;
3921 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3922 tree old, old_addr;
3923 tree compound_stmt;
3924 tree stmt, goto_stmt;
3925 tree loop_label, loop_decl, done_label, done_decl;
3927 tree lhs_type = TREE_TYPE (lhs);
3928 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3929 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3930 tree rhs_semantic_type = TREE_TYPE (rhs);
3931 tree nonatomic_rhs_semantic_type;
3932 tree rhs_type;
3934 gcc_assert (TYPE_ATOMIC (lhs_type));
3936 if (return_old_p)
3937 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3939 /* Allocate enough vector items for a compare_exchange. */
3940 vec_alloc (params, 6);
3942 /* Create a compound statement to hold the sequence of statements
3943 with a loop. */
3944 compound_stmt = c_begin_compound_stmt (false);
3946 /* Remove any excess precision (which is only present here in the
3947 case of compound assignments). */
3948 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3950 gcc_assert (modifycode != NOP_EXPR);
3951 rhs = TREE_OPERAND (rhs, 0);
3953 rhs_type = TREE_TYPE (rhs);
3955 /* Fold the RHS if it hasn't already been folded. */
3956 if (modifycode != NOP_EXPR)
3957 rhs = c_fully_fold (rhs, false, NULL);
3959 /* Remove the qualifiers for the rest of the expressions and create
3960 the VAL temp variable to hold the RHS. */
3961 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3962 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3963 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3964 TYPE_UNQUALIFIED);
3965 val = create_tmp_var_raw (nonatomic_rhs_type);
3966 TREE_ADDRESSABLE (val) = 1;
3967 TREE_NO_WARNING (val) = 1;
3968 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3969 NULL_TREE);
3970 SET_EXPR_LOCATION (rhs, loc);
3971 add_stmt (rhs);
3973 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3974 an atomic_store. */
3975 if (modifycode == NOP_EXPR)
3977 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3978 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3979 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3980 params->quick_push (lhs_addr);
3981 params->quick_push (rhs);
3982 params->quick_push (seq_cst);
3983 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3984 add_stmt (func_call);
3986 /* Finish the compound statement. */
3987 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3989 /* VAL is the value which was stored, return a COMPOUND_STMT of
3990 the statement and that value. */
3991 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3994 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3995 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3996 isn't applicable for such builtins. ??? Do we want to handle enums? */
3997 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3998 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4000 built_in_function fncode;
4001 switch (modifycode)
4003 case PLUS_EXPR:
4004 case POINTER_PLUS_EXPR:
4005 fncode = (return_old_p
4006 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4007 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4008 break;
4009 case MINUS_EXPR:
4010 fncode = (return_old_p
4011 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4012 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4013 break;
4014 case BIT_AND_EXPR:
4015 fncode = (return_old_p
4016 ? BUILT_IN_ATOMIC_FETCH_AND_N
4017 : BUILT_IN_ATOMIC_AND_FETCH_N);
4018 break;
4019 case BIT_IOR_EXPR:
4020 fncode = (return_old_p
4021 ? BUILT_IN_ATOMIC_FETCH_OR_N
4022 : BUILT_IN_ATOMIC_OR_FETCH_N);
4023 break;
4024 case BIT_XOR_EXPR:
4025 fncode = (return_old_p
4026 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4027 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4028 break;
4029 default:
4030 goto cas_loop;
4033 /* We can only use "_1" through "_16" variants of the atomic fetch
4034 built-ins. */
4035 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4036 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4037 goto cas_loop;
4039 /* If this is a pointer type, we need to multiply by the size of
4040 the pointer target type. */
4041 if (POINTER_TYPE_P (lhs_type))
4043 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4044 /* ??? This would introduce -Wdiscarded-qualifiers
4045 warning: __atomic_fetch_* expect volatile void *
4046 type as the first argument. (Assignments between
4047 atomic and non-atomic objects are OK.) */
4048 || TYPE_RESTRICT (lhs_type))
4049 goto cas_loop;
4050 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4051 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4052 convert (ptrdiff_type_node, rhs),
4053 convert (ptrdiff_type_node, sz));
4056 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4057 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4058 fndecl = builtin_decl_explicit (fncode);
4059 params->quick_push (lhs_addr);
4060 params->quick_push (rhs);
4061 params->quick_push (seq_cst);
4062 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4064 newval = create_tmp_var_raw (nonatomic_lhs_type);
4065 TREE_ADDRESSABLE (newval) = 1;
4066 TREE_NO_WARNING (newval) = 1;
4067 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4068 NULL_TREE, NULL_TREE);
4069 SET_EXPR_LOCATION (rhs, loc);
4070 add_stmt (rhs);
4072 /* Finish the compound statement. */
4073 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4075 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4076 the statement and that value. */
4077 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4080 cas_loop:
4081 /* Create the variables and labels required for the op= form. */
4082 old = create_tmp_var_raw (nonatomic_lhs_type);
4083 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4084 TREE_ADDRESSABLE (old) = 1;
4085 TREE_NO_WARNING (old) = 1;
4087 newval = create_tmp_var_raw (nonatomic_lhs_type);
4088 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4089 TREE_ADDRESSABLE (newval) = 1;
4090 TREE_NO_WARNING (newval) = 1;
4092 loop_decl = create_artificial_label (loc);
4093 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4095 done_decl = create_artificial_label (loc);
4096 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4098 /* __atomic_load (addr, &old, SEQ_CST). */
4099 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4100 params->quick_push (lhs_addr);
4101 params->quick_push (old_addr);
4102 params->quick_push (seq_cst);
4103 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4104 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4105 NULL_TREE);
4106 add_stmt (old);
4107 params->truncate (0);
4109 /* Create the expressions for floating-point environment
4110 manipulation, if required. */
4111 bool need_fenv = (flag_trapping_math
4112 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4113 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4114 if (need_fenv)
4115 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4117 if (hold_call)
4118 add_stmt (hold_call);
4120 /* loop: */
4121 add_stmt (loop_label);
4123 /* newval = old + val; */
4124 if (rhs_type != rhs_semantic_type)
4125 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4126 rhs = build_binary_op (loc, modifycode, old, val, true);
4127 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4129 tree eptype = TREE_TYPE (rhs);
4130 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4131 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4133 else
4134 rhs = c_fully_fold (rhs, false, NULL);
4135 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4136 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4137 NULL_TREE, 0);
4138 if (rhs != error_mark_node)
4140 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4141 NULL_TREE);
4142 SET_EXPR_LOCATION (rhs, loc);
4143 add_stmt (rhs);
4146 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4147 goto done; */
4148 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4149 params->quick_push (lhs_addr);
4150 params->quick_push (old_addr);
4151 params->quick_push (newval_addr);
4152 params->quick_push (integer_zero_node);
4153 params->quick_push (seq_cst);
4154 params->quick_push (seq_cst);
4155 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4157 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4158 SET_EXPR_LOCATION (goto_stmt, loc);
4160 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4161 SET_EXPR_LOCATION (stmt, loc);
4162 add_stmt (stmt);
4164 if (clear_call)
4165 add_stmt (clear_call);
4167 /* goto loop; */
4168 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4169 SET_EXPR_LOCATION (goto_stmt, loc);
4170 add_stmt (goto_stmt);
4172 /* done: */
4173 add_stmt (done_label);
4175 if (update_call)
4176 add_stmt (update_call);
4178 /* Finish the compound statement. */
4179 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4181 /* NEWVAL is the value that was successfully stored, return a
4182 COMPOUND_EXPR of the statement and the appropriate value. */
4183 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4184 return_old_p ? old : newval);
4187 /* Construct and perhaps optimize a tree representation
4188 for a unary operation. CODE, a tree_code, specifies the operation
4189 and XARG is the operand.
4190 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4191 promotions (such as from short to int).
4192 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4193 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4194 to pointers in C99.
4196 LOCATION is the location of the operator. */
4198 tree
4199 build_unary_op (location_t location, enum tree_code code, tree xarg,
4200 bool noconvert)
4202 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4203 tree arg = xarg;
4204 tree argtype = NULL_TREE;
4205 enum tree_code typecode;
4206 tree val;
4207 tree ret = error_mark_node;
4208 tree eptype = NULL_TREE;
4209 const char *invalid_op_diag;
4210 bool int_operands;
4212 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4213 if (int_operands)
4214 arg = remove_c_maybe_const_expr (arg);
4216 if (code != ADDR_EXPR)
4217 arg = require_complete_type (location, arg);
4219 typecode = TREE_CODE (TREE_TYPE (arg));
4220 if (typecode == ERROR_MARK)
4221 return error_mark_node;
4222 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4223 typecode = INTEGER_TYPE;
4225 if ((invalid_op_diag
4226 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4228 error_at (location, invalid_op_diag);
4229 return error_mark_node;
4232 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4234 eptype = TREE_TYPE (arg);
4235 arg = TREE_OPERAND (arg, 0);
4238 switch (code)
4240 case CONVERT_EXPR:
4241 /* This is used for unary plus, because a CONVERT_EXPR
4242 is enough to prevent anybody from looking inside for
4243 associativity, but won't generate any code. */
4244 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4245 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4246 || typecode == VECTOR_TYPE))
4248 error_at (location, "wrong type argument to unary plus");
4249 return error_mark_node;
4251 else if (!noconvert)
4252 arg = default_conversion (arg);
4253 arg = non_lvalue_loc (location, arg);
4254 break;
4256 case NEGATE_EXPR:
4257 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4258 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4259 || typecode == VECTOR_TYPE))
4261 error_at (location, "wrong type argument to unary minus");
4262 return error_mark_node;
4264 else if (!noconvert)
4265 arg = default_conversion (arg);
4266 break;
4268 case BIT_NOT_EXPR:
4269 /* ~ works on integer types and non float vectors. */
4270 if (typecode == INTEGER_TYPE
4271 || (typecode == VECTOR_TYPE
4272 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4274 tree e = arg;
4276 /* Warn if the expression has boolean value. */
4277 while (TREE_CODE (e) == COMPOUND_EXPR)
4278 e = TREE_OPERAND (e, 1);
4280 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4281 || truth_value_p (TREE_CODE (e)))
4282 && warning_at (location, OPT_Wbool_operation,
4283 "%<~%> on a boolean expression"))
4285 gcc_rich_location richloc (location);
4286 richloc.add_fixit_insert_before (location, "!");
4287 inform (&richloc, "did you mean to use logical not?");
4289 if (!noconvert)
4290 arg = default_conversion (arg);
4292 else if (typecode == COMPLEX_TYPE)
4294 code = CONJ_EXPR;
4295 pedwarn (location, OPT_Wpedantic,
4296 "ISO C does not support %<~%> for complex conjugation");
4297 if (!noconvert)
4298 arg = default_conversion (arg);
4300 else
4302 error_at (location, "wrong type argument to bit-complement");
4303 return error_mark_node;
4305 break;
4307 case ABS_EXPR:
4308 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4310 error_at (location, "wrong type argument to abs");
4311 return error_mark_node;
4313 else if (!noconvert)
4314 arg = default_conversion (arg);
4315 break;
4317 case CONJ_EXPR:
4318 /* Conjugating a real value is a no-op, but allow it anyway. */
4319 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4320 || typecode == COMPLEX_TYPE))
4322 error_at (location, "wrong type argument to conjugation");
4323 return error_mark_node;
4325 else if (!noconvert)
4326 arg = default_conversion (arg);
4327 break;
4329 case TRUTH_NOT_EXPR:
4330 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4331 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4332 && typecode != COMPLEX_TYPE)
4334 error_at (location,
4335 "wrong type argument to unary exclamation mark");
4336 return error_mark_node;
4338 if (int_operands)
4340 arg = c_objc_common_truthvalue_conversion (location, xarg);
4341 arg = remove_c_maybe_const_expr (arg);
4343 else
4344 arg = c_objc_common_truthvalue_conversion (location, arg);
4345 ret = invert_truthvalue_loc (location, arg);
4346 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4347 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4348 location = EXPR_LOCATION (ret);
4349 goto return_build_unary_op;
4351 case REALPART_EXPR:
4352 case IMAGPART_EXPR:
4353 ret = build_real_imag_expr (location, code, arg);
4354 if (ret == error_mark_node)
4355 return error_mark_node;
4356 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4357 eptype = TREE_TYPE (eptype);
4358 goto return_build_unary_op;
4360 case PREINCREMENT_EXPR:
4361 case POSTINCREMENT_EXPR:
4362 case PREDECREMENT_EXPR:
4363 case POSTDECREMENT_EXPR:
4365 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4367 tree inner = build_unary_op (location, code,
4368 C_MAYBE_CONST_EXPR_EXPR (arg),
4369 noconvert);
4370 if (inner == error_mark_node)
4371 return error_mark_node;
4372 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4373 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4374 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4375 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4376 goto return_build_unary_op;
4379 /* Complain about anything that is not a true lvalue. In
4380 Objective-C, skip this check for property_refs. */
4381 if (!objc_is_property_ref (arg)
4382 && !lvalue_or_else (location,
4383 arg, ((code == PREINCREMENT_EXPR
4384 || code == POSTINCREMENT_EXPR)
4385 ? lv_increment
4386 : lv_decrement)))
4387 return error_mark_node;
4389 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4391 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4392 warning_at (location, OPT_Wc___compat,
4393 "increment of enumeration value is invalid in C++");
4394 else
4395 warning_at (location, OPT_Wc___compat,
4396 "decrement of enumeration value is invalid in C++");
4399 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4401 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4402 warning_at (location, OPT_Wbool_operation,
4403 "increment of a boolean expression");
4404 else
4405 warning_at (location, OPT_Wbool_operation,
4406 "decrement of a boolean expression");
4409 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4410 arg = c_fully_fold (arg, false, NULL, true);
4412 bool atomic_op;
4413 atomic_op = really_atomic_lvalue (arg);
4415 /* Increment or decrement the real part of the value,
4416 and don't change the imaginary part. */
4417 if (typecode == COMPLEX_TYPE)
4419 tree real, imag;
4421 pedwarn (location, OPT_Wpedantic,
4422 "ISO C does not support %<++%> and %<--%> on complex types");
4424 if (!atomic_op)
4426 arg = stabilize_reference (arg);
4427 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4428 true);
4429 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4430 true);
4431 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4432 if (real == error_mark_node || imag == error_mark_node)
4433 return error_mark_node;
4434 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4435 real, imag);
4436 goto return_build_unary_op;
4440 /* Report invalid types. */
4442 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4443 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4444 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4446 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4447 error_at (location, "wrong type argument to increment");
4448 else
4449 error_at (location, "wrong type argument to decrement");
4451 return error_mark_node;
4455 tree inc;
4457 argtype = TREE_TYPE (arg);
4459 /* Compute the increment. */
4461 if (typecode == POINTER_TYPE)
4463 /* If pointer target is an incomplete type,
4464 we just cannot know how to do the arithmetic. */
4465 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4467 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4468 error_at (location,
4469 "increment of pointer to an incomplete type %qT",
4470 TREE_TYPE (argtype));
4471 else
4472 error_at (location,
4473 "decrement of pointer to an incomplete type %qT",
4474 TREE_TYPE (argtype));
4476 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4477 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4479 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4480 pedwarn (location, OPT_Wpointer_arith,
4481 "wrong type argument to increment");
4482 else
4483 pedwarn (location, OPT_Wpointer_arith,
4484 "wrong type argument to decrement");
4487 inc = c_size_in_bytes (TREE_TYPE (argtype));
4488 inc = convert_to_ptrofftype_loc (location, inc);
4490 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4492 /* For signed fract types, we invert ++ to -- or
4493 -- to ++, and change inc from 1 to -1, because
4494 it is not possible to represent 1 in signed fract constants.
4495 For unsigned fract types, the result always overflows and
4496 we get an undefined (original) or the maximum value. */
4497 if (code == PREINCREMENT_EXPR)
4498 code = PREDECREMENT_EXPR;
4499 else if (code == PREDECREMENT_EXPR)
4500 code = PREINCREMENT_EXPR;
4501 else if (code == POSTINCREMENT_EXPR)
4502 code = POSTDECREMENT_EXPR;
4503 else /* code == POSTDECREMENT_EXPR */
4504 code = POSTINCREMENT_EXPR;
4506 inc = integer_minus_one_node;
4507 inc = convert (argtype, inc);
4509 else
4511 inc = VECTOR_TYPE_P (argtype)
4512 ? build_one_cst (argtype)
4513 : integer_one_node;
4514 inc = convert (argtype, inc);
4517 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4518 need to ask Objective-C to build the increment or decrement
4519 expression for it. */
4520 if (objc_is_property_ref (arg))
4521 return objc_build_incr_expr_for_property_ref (location, code,
4522 arg, inc);
4524 /* Report a read-only lvalue. */
4525 if (TYPE_READONLY (argtype))
4527 readonly_error (location, arg,
4528 ((code == PREINCREMENT_EXPR
4529 || code == POSTINCREMENT_EXPR)
4530 ? lv_increment : lv_decrement));
4531 return error_mark_node;
4533 else if (TREE_READONLY (arg))
4534 readonly_warning (arg,
4535 ((code == PREINCREMENT_EXPR
4536 || code == POSTINCREMENT_EXPR)
4537 ? lv_increment : lv_decrement));
4539 /* If the argument is atomic, use the special code sequences for
4540 atomic compound assignment. */
4541 if (atomic_op)
4543 arg = stabilize_reference (arg);
4544 ret = build_atomic_assign (location, arg,
4545 ((code == PREINCREMENT_EXPR
4546 || code == POSTINCREMENT_EXPR)
4547 ? PLUS_EXPR
4548 : MINUS_EXPR),
4549 (FRACT_MODE_P (TYPE_MODE (argtype))
4550 ? inc
4551 : integer_one_node),
4552 (code == POSTINCREMENT_EXPR
4553 || code == POSTDECREMENT_EXPR));
4554 goto return_build_unary_op;
4557 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4558 val = boolean_increment (code, arg);
4559 else
4560 val = build2 (code, TREE_TYPE (arg), arg, inc);
4561 TREE_SIDE_EFFECTS (val) = 1;
4562 if (TREE_CODE (val) != code)
4563 TREE_NO_WARNING (val) = 1;
4564 ret = val;
4565 goto return_build_unary_op;
4568 case ADDR_EXPR:
4569 /* Note that this operation never does default_conversion. */
4571 /* The operand of unary '&' must be an lvalue (which excludes
4572 expressions of type void), or, in C99, the result of a [] or
4573 unary '*' operator. */
4574 if (VOID_TYPE_P (TREE_TYPE (arg))
4575 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4576 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4577 pedwarn (location, 0, "taking address of expression of type %<void%>");
4579 /* Let &* cancel out to simplify resulting code. */
4580 if (INDIRECT_REF_P (arg))
4582 /* Don't let this be an lvalue. */
4583 if (lvalue_p (TREE_OPERAND (arg, 0)))
4584 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4585 ret = TREE_OPERAND (arg, 0);
4586 goto return_build_unary_op;
4589 /* Anything not already handled and not a true memory reference
4590 or a non-lvalue array is an error. */
4591 if (typecode != FUNCTION_TYPE && !noconvert
4592 && !lvalue_or_else (location, arg, lv_addressof))
4593 return error_mark_node;
4595 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4596 folding later. */
4597 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4599 tree inner = build_unary_op (location, code,
4600 C_MAYBE_CONST_EXPR_EXPR (arg),
4601 noconvert);
4602 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4603 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4604 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4605 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4606 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4607 goto return_build_unary_op;
4610 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4611 argtype = TREE_TYPE (arg);
4613 /* If the lvalue is const or volatile, merge that into the type
4614 to which the address will point. This is only needed
4615 for function types. */
4616 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4617 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4618 && TREE_CODE (argtype) == FUNCTION_TYPE)
4620 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4621 int quals = orig_quals;
4623 if (TREE_READONLY (arg))
4624 quals |= TYPE_QUAL_CONST;
4625 if (TREE_THIS_VOLATILE (arg))
4626 quals |= TYPE_QUAL_VOLATILE;
4628 argtype = c_build_qualified_type (argtype, quals);
4631 switch (TREE_CODE (arg))
4633 case COMPONENT_REF:
4634 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4636 error_at (location, "cannot take address of bit-field %qD",
4637 TREE_OPERAND (arg, 1));
4638 return error_mark_node;
4641 /* fall through */
4643 case ARRAY_REF:
4644 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4646 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4647 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4649 error_at (location, "cannot take address of scalar with "
4650 "reverse storage order");
4651 return error_mark_node;
4654 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4655 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4656 warning_at (location, OPT_Wscalar_storage_order,
4657 "address of array with reverse scalar storage "
4658 "order requested");
4661 default:
4662 break;
4665 if (!c_mark_addressable (arg))
4666 return error_mark_node;
4668 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4669 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4671 argtype = build_pointer_type (argtype);
4673 /* ??? Cope with user tricks that amount to offsetof. Delete this
4674 when we have proper support for integer constant expressions. */
4675 val = get_base_address (arg);
4676 if (val && INDIRECT_REF_P (val)
4677 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4679 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4680 goto return_build_unary_op;
4683 val = build1 (ADDR_EXPR, argtype, arg);
4685 ret = val;
4686 goto return_build_unary_op;
4688 default:
4689 gcc_unreachable ();
4692 if (argtype == NULL_TREE)
4693 argtype = TREE_TYPE (arg);
4694 if (TREE_CODE (arg) == INTEGER_CST)
4695 ret = (require_constant_value
4696 ? fold_build1_initializer_loc (location, code, argtype, arg)
4697 : fold_build1_loc (location, code, argtype, arg));
4698 else
4699 ret = build1 (code, argtype, arg);
4700 return_build_unary_op:
4701 gcc_assert (ret != error_mark_node);
4702 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4703 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4704 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4705 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4706 ret = note_integer_operands (ret);
4707 if (eptype)
4708 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4709 protected_set_expr_location (ret, location);
4710 return ret;
4713 /* Return nonzero if REF is an lvalue valid for this language.
4714 Lvalues can be assigned, unless their type has TYPE_READONLY.
4715 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4717 bool
4718 lvalue_p (const_tree ref)
4720 const enum tree_code code = TREE_CODE (ref);
4722 switch (code)
4724 case REALPART_EXPR:
4725 case IMAGPART_EXPR:
4726 case COMPONENT_REF:
4727 return lvalue_p (TREE_OPERAND (ref, 0));
4729 case C_MAYBE_CONST_EXPR:
4730 return lvalue_p (TREE_OPERAND (ref, 1));
4732 case COMPOUND_LITERAL_EXPR:
4733 case STRING_CST:
4734 return true;
4736 case INDIRECT_REF:
4737 case ARRAY_REF:
4738 case VAR_DECL:
4739 case PARM_DECL:
4740 case RESULT_DECL:
4741 case ERROR_MARK:
4742 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4743 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4745 case BIND_EXPR:
4746 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4748 default:
4749 return false;
4753 /* Give a warning for storing in something that is read-only in GCC
4754 terms but not const in ISO C terms. */
4756 static void
4757 readonly_warning (tree arg, enum lvalue_use use)
4759 switch (use)
4761 case lv_assign:
4762 warning (0, "assignment of read-only location %qE", arg);
4763 break;
4764 case lv_increment:
4765 warning (0, "increment of read-only location %qE", arg);
4766 break;
4767 case lv_decrement:
4768 warning (0, "decrement of read-only location %qE", arg);
4769 break;
4770 default:
4771 gcc_unreachable ();
4773 return;
4777 /* Return nonzero if REF is an lvalue valid for this language;
4778 otherwise, print an error message and return zero. USE says
4779 how the lvalue is being used and so selects the error message.
4780 LOCATION is the location at which any error should be reported. */
4782 static int
4783 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4785 int win = lvalue_p (ref);
4787 if (!win)
4788 lvalue_error (loc, use);
4790 return win;
4793 /* Mark EXP saying that we need to be able to take the
4794 address of it; it should not be allocated in a register.
4795 Returns true if successful. ARRAY_REF_P is true if this
4796 is for ARRAY_REF construction - in that case we don't want
4797 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4798 it is fine to use ARRAY_REFs for vector subscripts on vector
4799 register variables. */
4801 bool
4802 c_mark_addressable (tree exp, bool array_ref_p)
4804 tree x = exp;
4806 while (1)
4807 switch (TREE_CODE (x))
4809 case VIEW_CONVERT_EXPR:
4810 if (array_ref_p
4811 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4812 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4813 return true;
4814 /* FALLTHRU */
4815 case COMPONENT_REF:
4816 case ADDR_EXPR:
4817 case ARRAY_REF:
4818 case REALPART_EXPR:
4819 case IMAGPART_EXPR:
4820 x = TREE_OPERAND (x, 0);
4821 break;
4823 case COMPOUND_LITERAL_EXPR:
4824 case CONSTRUCTOR:
4825 TREE_ADDRESSABLE (x) = 1;
4826 return true;
4828 case VAR_DECL:
4829 case CONST_DECL:
4830 case PARM_DECL:
4831 case RESULT_DECL:
4832 if (C_DECL_REGISTER (x)
4833 && DECL_NONLOCAL (x))
4835 if (TREE_PUBLIC (x) || is_global_var (x))
4837 error
4838 ("global register variable %qD used in nested function", x);
4839 return false;
4841 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4843 else if (C_DECL_REGISTER (x))
4845 if (TREE_PUBLIC (x) || is_global_var (x))
4846 error ("address of global register variable %qD requested", x);
4847 else
4848 error ("address of register variable %qD requested", x);
4849 return false;
4852 /* FALLTHRU */
4853 case FUNCTION_DECL:
4854 TREE_ADDRESSABLE (x) = 1;
4855 /* FALLTHRU */
4856 default:
4857 return true;
4861 /* Convert EXPR to TYPE, warning about conversion problems with
4862 constants. SEMANTIC_TYPE is the type this conversion would use
4863 without excess precision. If SEMANTIC_TYPE is NULL, this function
4864 is equivalent to convert_and_check. This function is a wrapper that
4865 handles conversions that may be different than
4866 the usual ones because of excess precision. */
4868 static tree
4869 ep_convert_and_check (location_t loc, tree type, tree expr,
4870 tree semantic_type)
4872 if (TREE_TYPE (expr) == type)
4873 return expr;
4875 /* For C11, integer conversions may have results with excess
4876 precision. */
4877 if (flag_isoc11 || !semantic_type)
4878 return convert_and_check (loc, type, expr);
4880 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4881 && TREE_TYPE (expr) != semantic_type)
4883 /* For integers, we need to check the real conversion, not
4884 the conversion to the excess precision type. */
4885 expr = convert_and_check (loc, semantic_type, expr);
4887 /* Result type is the excess precision type, which should be
4888 large enough, so do not check. */
4889 return convert (type, expr);
4892 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4893 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4894 if folded to an integer constant then the unselected half may
4895 contain arbitrary operations not normally permitted in constant
4896 expressions. Set the location of the expression to LOC. */
4898 tree
4899 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4900 tree op1, tree op1_original_type, location_t op1_loc,
4901 tree op2, tree op2_original_type, location_t op2_loc)
4903 tree type1;
4904 tree type2;
4905 enum tree_code code1;
4906 enum tree_code code2;
4907 tree result_type = NULL;
4908 tree semantic_result_type = NULL;
4909 tree orig_op1 = op1, orig_op2 = op2;
4910 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4911 bool ifexp_int_operands;
4912 tree ret;
4914 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4915 if (op1_int_operands)
4916 op1 = remove_c_maybe_const_expr (op1);
4917 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4918 if (op2_int_operands)
4919 op2 = remove_c_maybe_const_expr (op2);
4920 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4921 if (ifexp_int_operands)
4922 ifexp = remove_c_maybe_const_expr (ifexp);
4924 /* Promote both alternatives. */
4926 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4927 op1 = default_conversion (op1);
4928 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4929 op2 = default_conversion (op2);
4931 if (TREE_CODE (ifexp) == ERROR_MARK
4932 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4933 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4934 return error_mark_node;
4936 type1 = TREE_TYPE (op1);
4937 code1 = TREE_CODE (type1);
4938 type2 = TREE_TYPE (op2);
4939 code2 = TREE_CODE (type2);
4941 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4942 return error_mark_node;
4944 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4945 return error_mark_node;
4947 /* C90 does not permit non-lvalue arrays in conditional expressions.
4948 In C99 they will be pointers by now. */
4949 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4951 error_at (colon_loc, "non-lvalue array in conditional expression");
4952 return error_mark_node;
4955 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4956 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4957 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4958 || code1 == COMPLEX_TYPE)
4959 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4960 || code2 == COMPLEX_TYPE))
4962 semantic_result_type = c_common_type (type1, type2);
4963 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4965 op1 = TREE_OPERAND (op1, 0);
4966 type1 = TREE_TYPE (op1);
4967 gcc_assert (TREE_CODE (type1) == code1);
4969 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4971 op2 = TREE_OPERAND (op2, 0);
4972 type2 = TREE_TYPE (op2);
4973 gcc_assert (TREE_CODE (type2) == code2);
4977 if (warn_cxx_compat)
4979 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4980 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4982 if (TREE_CODE (t1) == ENUMERAL_TYPE
4983 && TREE_CODE (t2) == ENUMERAL_TYPE
4984 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4985 warning_at (colon_loc, OPT_Wc___compat,
4986 ("different enum types in conditional is "
4987 "invalid in C++: %qT vs %qT"),
4988 t1, t2);
4991 /* Quickly detect the usual case where op1 and op2 have the same type
4992 after promotion. */
4993 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4995 if (type1 == type2)
4996 result_type = type1;
4997 else
4998 result_type = TYPE_MAIN_VARIANT (type1);
5000 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5001 || code1 == COMPLEX_TYPE)
5002 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5003 || code2 == COMPLEX_TYPE))
5005 /* In C11, a conditional expression between a floating-point
5006 type and an integer type should convert the integer type to
5007 the evaluation format of the floating-point type, with
5008 possible excess precision. */
5009 tree eptype1 = type1;
5010 tree eptype2 = type2;
5011 if (flag_isoc11)
5013 tree eptype;
5014 if (ANY_INTEGRAL_TYPE_P (type1)
5015 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5017 eptype2 = eptype;
5018 if (!semantic_result_type)
5019 semantic_result_type = c_common_type (type1, type2);
5021 else if (ANY_INTEGRAL_TYPE_P (type2)
5022 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5024 eptype1 = eptype;
5025 if (!semantic_result_type)
5026 semantic_result_type = c_common_type (type1, type2);
5029 result_type = c_common_type (eptype1, eptype2);
5030 if (result_type == error_mark_node)
5031 return error_mark_node;
5032 do_warn_double_promotion (result_type, type1, type2,
5033 "implicit conversion from %qT to %qT to "
5034 "match other result of conditional",
5035 colon_loc);
5037 /* If -Wsign-compare, warn here if type1 and type2 have
5038 different signedness. We'll promote the signed to unsigned
5039 and later code won't know it used to be different.
5040 Do this check on the original types, so that explicit casts
5041 will be considered, but default promotions won't. */
5042 if (c_inhibit_evaluation_warnings == 0)
5044 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5045 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5047 if (unsigned_op1 ^ unsigned_op2)
5049 bool ovf;
5051 /* Do not warn if the result type is signed, since the
5052 signed type will only be chosen if it can represent
5053 all the values of the unsigned type. */
5054 if (!TYPE_UNSIGNED (result_type))
5055 /* OK */;
5056 else
5058 bool op1_maybe_const = true;
5059 bool op2_maybe_const = true;
5061 /* Do not warn if the signed quantity is an
5062 unsuffixed integer literal (or some static
5063 constant expression involving such literals) and
5064 it is non-negative. This warning requires the
5065 operands to be folded for best results, so do
5066 that folding in this case even without
5067 warn_sign_compare to avoid warning options
5068 possibly affecting code generation. */
5069 c_inhibit_evaluation_warnings
5070 += (ifexp == truthvalue_false_node);
5071 op1 = c_fully_fold (op1, require_constant_value,
5072 &op1_maybe_const);
5073 c_inhibit_evaluation_warnings
5074 -= (ifexp == truthvalue_false_node);
5076 c_inhibit_evaluation_warnings
5077 += (ifexp == truthvalue_true_node);
5078 op2 = c_fully_fold (op2, require_constant_value,
5079 &op2_maybe_const);
5080 c_inhibit_evaluation_warnings
5081 -= (ifexp == truthvalue_true_node);
5083 if (warn_sign_compare)
5085 if ((unsigned_op2
5086 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5087 || (unsigned_op1
5088 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5089 /* OK */;
5090 else if (unsigned_op2)
5091 warning_at (op1_loc, OPT_Wsign_compare,
5092 "operand of ?: changes signedness from "
5093 "%qT to %qT due to unsignedness of other "
5094 "operand", TREE_TYPE (orig_op1),
5095 TREE_TYPE (orig_op2));
5096 else
5097 warning_at (op2_loc, OPT_Wsign_compare,
5098 "operand of ?: changes signedness from "
5099 "%qT to %qT due to unsignedness of other "
5100 "operand", TREE_TYPE (orig_op2),
5101 TREE_TYPE (orig_op1));
5103 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5104 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5105 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5106 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5111 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5113 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5114 pedwarn (colon_loc, OPT_Wpedantic,
5115 "ISO C forbids conditional expr with only one void side");
5116 result_type = void_type_node;
5118 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5120 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5121 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5122 addr_space_t as_common;
5124 if (comp_target_types (colon_loc, type1, type2))
5125 result_type = common_pointer_type (type1, type2);
5126 else if (null_pointer_constant_p (orig_op1))
5127 result_type = type2;
5128 else if (null_pointer_constant_p (orig_op2))
5129 result_type = type1;
5130 else if (!addr_space_superset (as1, as2, &as_common))
5132 error_at (colon_loc, "pointers to disjoint address spaces "
5133 "used in conditional expression");
5134 return error_mark_node;
5136 else if (VOID_TYPE_P (TREE_TYPE (type1))
5137 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5139 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5140 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5141 & ~TYPE_QUALS (TREE_TYPE (type1))))
5142 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5143 "pointer to array loses qualifier "
5144 "in conditional expression");
5146 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5147 pedwarn (colon_loc, OPT_Wpedantic,
5148 "ISO C forbids conditional expr between "
5149 "%<void *%> and function pointer");
5150 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5151 TREE_TYPE (type2)));
5153 else if (VOID_TYPE_P (TREE_TYPE (type2))
5154 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5156 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5157 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5158 & ~TYPE_QUALS (TREE_TYPE (type2))))
5159 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5160 "pointer to array loses qualifier "
5161 "in conditional expression");
5163 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5164 pedwarn (colon_loc, OPT_Wpedantic,
5165 "ISO C forbids conditional expr between "
5166 "%<void *%> and function pointer");
5167 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5168 TREE_TYPE (type1)));
5170 /* Objective-C pointer comparisons are a bit more lenient. */
5171 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5172 result_type = objc_common_type (type1, type2);
5173 else
5175 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5177 pedwarn (colon_loc, 0,
5178 "pointer type mismatch in conditional expression");
5179 result_type = build_pointer_type
5180 (build_qualified_type (void_type_node, qual));
5183 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5185 if (!null_pointer_constant_p (orig_op2))
5186 pedwarn (colon_loc, 0,
5187 "pointer/integer type mismatch in conditional expression");
5188 else
5190 op2 = null_pointer_node;
5192 result_type = type1;
5194 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5196 if (!null_pointer_constant_p (orig_op1))
5197 pedwarn (colon_loc, 0,
5198 "pointer/integer type mismatch in conditional expression");
5199 else
5201 op1 = null_pointer_node;
5203 result_type = type2;
5206 if (!result_type)
5208 if (flag_cond_mismatch)
5209 result_type = void_type_node;
5210 else
5212 error_at (colon_loc, "type mismatch in conditional expression");
5213 return error_mark_node;
5217 /* Merge const and volatile flags of the incoming types. */
5218 result_type
5219 = build_type_variant (result_type,
5220 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5221 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5223 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5224 semantic_result_type);
5225 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5226 semantic_result_type);
5228 if (ifexp_bcp && ifexp == truthvalue_true_node)
5230 op2_int_operands = true;
5231 op1 = c_fully_fold (op1, require_constant_value, NULL);
5233 if (ifexp_bcp && ifexp == truthvalue_false_node)
5235 op1_int_operands = true;
5236 op2 = c_fully_fold (op2, require_constant_value, NULL);
5238 int_const = int_operands = (ifexp_int_operands
5239 && op1_int_operands
5240 && op2_int_operands);
5241 if (int_operands)
5243 int_const = ((ifexp == truthvalue_true_node
5244 && TREE_CODE (orig_op1) == INTEGER_CST
5245 && !TREE_OVERFLOW (orig_op1))
5246 || (ifexp == truthvalue_false_node
5247 && TREE_CODE (orig_op2) == INTEGER_CST
5248 && !TREE_OVERFLOW (orig_op2)));
5251 /* Need to convert condition operand into a vector mask. */
5252 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5254 tree vectype = TREE_TYPE (ifexp);
5255 tree elem_type = TREE_TYPE (vectype);
5256 tree zero = build_int_cst (elem_type, 0);
5257 tree zero_vec = build_vector_from_val (vectype, zero);
5258 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5259 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5262 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5263 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5264 else
5266 if (int_operands)
5268 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5269 nested inside of the expression. */
5270 op1 = c_fully_fold (op1, false, NULL);
5271 op2 = c_fully_fold (op2, false, NULL);
5273 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5274 if (int_operands)
5275 ret = note_integer_operands (ret);
5277 if (semantic_result_type)
5278 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5280 protected_set_expr_location (ret, colon_loc);
5282 /* If the OP1 and OP2 are the same and don't have side-effects,
5283 warn here, because the COND_EXPR will be turned into OP1. */
5284 if (warn_duplicated_branches
5285 && TREE_CODE (ret) == COND_EXPR
5286 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5287 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5288 "this condition has identical branches");
5290 return ret;
5293 /* Return a compound expression that performs two expressions and
5294 returns the value of the second of them.
5296 LOC is the location of the COMPOUND_EXPR. */
5298 tree
5299 build_compound_expr (location_t loc, tree expr1, tree expr2)
5301 bool expr1_int_operands, expr2_int_operands;
5302 tree eptype = NULL_TREE;
5303 tree ret;
5305 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5306 if (expr1_int_operands)
5307 expr1 = remove_c_maybe_const_expr (expr1);
5308 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5309 if (expr2_int_operands)
5310 expr2 = remove_c_maybe_const_expr (expr2);
5312 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5313 expr1 = TREE_OPERAND (expr1, 0);
5314 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5316 eptype = TREE_TYPE (expr2);
5317 expr2 = TREE_OPERAND (expr2, 0);
5320 if (!TREE_SIDE_EFFECTS (expr1))
5322 /* The left-hand operand of a comma expression is like an expression
5323 statement: with -Wunused, we should warn if it doesn't have
5324 any side-effects, unless it was explicitly cast to (void). */
5325 if (warn_unused_value)
5327 if (VOID_TYPE_P (TREE_TYPE (expr1))
5328 && CONVERT_EXPR_P (expr1))
5329 ; /* (void) a, b */
5330 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5331 && TREE_CODE (expr1) == COMPOUND_EXPR
5332 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5333 ; /* (void) a, (void) b, c */
5334 else
5335 warning_at (loc, OPT_Wunused_value,
5336 "left-hand operand of comma expression has no effect");
5339 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5340 && warn_unused_value)
5342 tree r = expr1;
5343 location_t cloc = loc;
5344 while (TREE_CODE (r) == COMPOUND_EXPR)
5346 if (EXPR_HAS_LOCATION (r))
5347 cloc = EXPR_LOCATION (r);
5348 r = TREE_OPERAND (r, 1);
5350 if (!TREE_SIDE_EFFECTS (r)
5351 && !VOID_TYPE_P (TREE_TYPE (r))
5352 && !CONVERT_EXPR_P (r))
5353 warning_at (cloc, OPT_Wunused_value,
5354 "right-hand operand of comma expression has no effect");
5357 /* With -Wunused, we should also warn if the left-hand operand does have
5358 side-effects, but computes a value which is not used. For example, in
5359 `foo() + bar(), baz()' the result of the `+' operator is not used,
5360 so we should issue a warning. */
5361 else if (warn_unused_value)
5362 warn_if_unused_value (expr1, loc);
5364 if (expr2 == error_mark_node)
5365 return error_mark_node;
5367 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5369 if (flag_isoc99
5370 && expr1_int_operands
5371 && expr2_int_operands)
5372 ret = note_integer_operands (ret);
5374 if (eptype)
5375 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5377 protected_set_expr_location (ret, loc);
5378 return ret;
5381 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5382 which we are casting. OTYPE is the type of the expression being
5383 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5384 of the cast. -Wcast-qual appeared on the command line. Named
5385 address space qualifiers are not handled here, because they result
5386 in different warnings. */
5388 static void
5389 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5391 tree in_type = type;
5392 tree in_otype = otype;
5393 int added = 0;
5394 int discarded = 0;
5395 bool is_const;
5397 /* Check that the qualifiers on IN_TYPE are a superset of the
5398 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5399 nodes is uninteresting and we stop as soon as we hit a
5400 non-POINTER_TYPE node on either type. */
5403 in_otype = TREE_TYPE (in_otype);
5404 in_type = TREE_TYPE (in_type);
5406 /* GNU C allows cv-qualified function types. 'const' means the
5407 function is very pure, 'volatile' means it can't return. We
5408 need to warn when such qualifiers are added, not when they're
5409 taken away. */
5410 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5411 && TREE_CODE (in_type) == FUNCTION_TYPE)
5412 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5413 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5414 else
5415 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5416 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5418 while (TREE_CODE (in_type) == POINTER_TYPE
5419 && TREE_CODE (in_otype) == POINTER_TYPE);
5421 if (added)
5422 warning_at (loc, OPT_Wcast_qual,
5423 "cast adds %q#v qualifier to function type", added);
5425 if (discarded)
5426 /* There are qualifiers present in IN_OTYPE that are not present
5427 in IN_TYPE. */
5428 warning_at (loc, OPT_Wcast_qual,
5429 "cast discards %qv qualifier from pointer target type",
5430 discarded);
5432 if (added || discarded)
5433 return;
5435 /* A cast from **T to const **T is unsafe, because it can cause a
5436 const value to be changed with no additional warning. We only
5437 issue this warning if T is the same on both sides, and we only
5438 issue the warning if there are the same number of pointers on
5439 both sides, as otherwise the cast is clearly unsafe anyhow. A
5440 cast is unsafe when a qualifier is added at one level and const
5441 is not present at all outer levels.
5443 To issue this warning, we check at each level whether the cast
5444 adds new qualifiers not already seen. We don't need to special
5445 case function types, as they won't have the same
5446 TYPE_MAIN_VARIANT. */
5448 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5449 return;
5450 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5451 return;
5453 in_type = type;
5454 in_otype = otype;
5455 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5458 in_type = TREE_TYPE (in_type);
5459 in_otype = TREE_TYPE (in_otype);
5460 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5461 && !is_const)
5463 warning_at (loc, OPT_Wcast_qual,
5464 "to be safe all intermediate pointers in cast from "
5465 "%qT to %qT must be %<const%> qualified",
5466 otype, type);
5467 break;
5469 if (is_const)
5470 is_const = TYPE_READONLY (in_type);
5472 while (TREE_CODE (in_type) == POINTER_TYPE);
5475 /* Build an expression representing a cast to type TYPE of expression EXPR.
5476 LOC is the location of the cast-- typically the open paren of the cast. */
5478 tree
5479 build_c_cast (location_t loc, tree type, tree expr)
5481 tree value;
5483 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5484 expr = TREE_OPERAND (expr, 0);
5486 value = expr;
5488 if (type == error_mark_node || expr == error_mark_node)
5489 return error_mark_node;
5491 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5492 only in <protocol> qualifications. But when constructing cast expressions,
5493 the protocols do matter and must be kept around. */
5494 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5495 return build1 (NOP_EXPR, type, expr);
5497 type = TYPE_MAIN_VARIANT (type);
5499 if (TREE_CODE (type) == ARRAY_TYPE)
5501 error_at (loc, "cast specifies array type");
5502 return error_mark_node;
5505 if (TREE_CODE (type) == FUNCTION_TYPE)
5507 error_at (loc, "cast specifies function type");
5508 return error_mark_node;
5511 if (!VOID_TYPE_P (type))
5513 value = require_complete_type (loc, value);
5514 if (value == error_mark_node)
5515 return error_mark_node;
5518 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5520 if (RECORD_OR_UNION_TYPE_P (type))
5521 pedwarn (loc, OPT_Wpedantic,
5522 "ISO C forbids casting nonscalar to the same type");
5524 /* Convert to remove any qualifiers from VALUE's type. */
5525 value = convert (type, value);
5527 else if (TREE_CODE (type) == UNION_TYPE)
5529 tree field;
5531 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5532 if (TREE_TYPE (field) != error_mark_node
5533 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5534 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5535 break;
5537 if (field)
5539 tree t;
5540 bool maybe_const = true;
5542 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5543 t = c_fully_fold (value, false, &maybe_const);
5544 t = build_constructor_single (type, field, t);
5545 if (!maybe_const)
5546 t = c_wrap_maybe_const (t, true);
5547 t = digest_init (loc, type, t,
5548 NULL_TREE, false, true, 0);
5549 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5550 return t;
5552 error_at (loc, "cast to union type from type not present in union");
5553 return error_mark_node;
5555 else
5557 tree otype, ovalue;
5559 if (type == void_type_node)
5561 tree t = build1 (CONVERT_EXPR, type, value);
5562 SET_EXPR_LOCATION (t, loc);
5563 return t;
5566 otype = TREE_TYPE (value);
5568 /* Optionally warn about potentially worrisome casts. */
5569 if (warn_cast_qual
5570 && TREE_CODE (type) == POINTER_TYPE
5571 && TREE_CODE (otype) == POINTER_TYPE)
5572 handle_warn_cast_qual (loc, type, otype);
5574 /* Warn about conversions between pointers to disjoint
5575 address spaces. */
5576 if (TREE_CODE (type) == POINTER_TYPE
5577 && TREE_CODE (otype) == POINTER_TYPE
5578 && !null_pointer_constant_p (value))
5580 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5581 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5582 addr_space_t as_common;
5584 if (!addr_space_superset (as_to, as_from, &as_common))
5586 if (ADDR_SPACE_GENERIC_P (as_from))
5587 warning_at (loc, 0, "cast to %s address space pointer "
5588 "from disjoint generic address space pointer",
5589 c_addr_space_name (as_to));
5591 else if (ADDR_SPACE_GENERIC_P (as_to))
5592 warning_at (loc, 0, "cast to generic address space pointer "
5593 "from disjoint %s address space pointer",
5594 c_addr_space_name (as_from));
5596 else
5597 warning_at (loc, 0, "cast to %s address space pointer "
5598 "from disjoint %s address space pointer",
5599 c_addr_space_name (as_to),
5600 c_addr_space_name (as_from));
5604 /* Warn about possible alignment problems. */
5605 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5606 && TREE_CODE (type) == POINTER_TYPE
5607 && TREE_CODE (otype) == POINTER_TYPE
5608 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5609 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5610 /* Don't warn about opaque types, where the actual alignment
5611 restriction is unknown. */
5612 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5613 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5614 && min_align_of_type (TREE_TYPE (type))
5615 > min_align_of_type (TREE_TYPE (otype)))
5616 warning_at (loc, OPT_Wcast_align,
5617 "cast increases required alignment of target type");
5619 if (TREE_CODE (type) == INTEGER_TYPE
5620 && TREE_CODE (otype) == POINTER_TYPE
5621 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5622 /* Unlike conversion of integers to pointers, where the
5623 warning is disabled for converting constants because
5624 of cases such as SIG_*, warn about converting constant
5625 pointers to integers. In some cases it may cause unwanted
5626 sign extension, and a warning is appropriate. */
5627 warning_at (loc, OPT_Wpointer_to_int_cast,
5628 "cast from pointer to integer of different size");
5630 if (TREE_CODE (value) == CALL_EXPR
5631 && TREE_CODE (type) != TREE_CODE (otype))
5632 warning_at (loc, OPT_Wbad_function_cast,
5633 "cast from function call of type %qT "
5634 "to non-matching type %qT", otype, type);
5636 if (TREE_CODE (type) == POINTER_TYPE
5637 && TREE_CODE (otype) == INTEGER_TYPE
5638 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5639 /* Don't warn about converting any constant. */
5640 && !TREE_CONSTANT (value))
5641 warning_at (loc,
5642 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5643 "of different size");
5645 if (warn_strict_aliasing <= 2)
5646 strict_aliasing_warning (otype, type, expr);
5648 /* If pedantic, warn for conversions between function and object
5649 pointer types, except for converting a null pointer constant
5650 to function pointer type. */
5651 if (pedantic
5652 && TREE_CODE (type) == POINTER_TYPE
5653 && TREE_CODE (otype) == POINTER_TYPE
5654 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5655 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5656 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5657 "conversion of function pointer to object pointer type");
5659 if (pedantic
5660 && TREE_CODE (type) == POINTER_TYPE
5661 && TREE_CODE (otype) == POINTER_TYPE
5662 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5663 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5664 && !null_pointer_constant_p (value))
5665 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5666 "conversion of object pointer to function pointer type");
5668 ovalue = value;
5669 value = convert (type, value);
5671 /* Ignore any integer overflow caused by the cast. */
5672 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5674 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5676 if (!TREE_OVERFLOW (value))
5678 /* Avoid clobbering a shared constant. */
5679 value = copy_node (value);
5680 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5683 else if (TREE_OVERFLOW (value))
5684 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5685 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5689 /* Don't let a cast be an lvalue. */
5690 if (lvalue_p (value))
5691 value = non_lvalue_loc (loc, value);
5693 /* Don't allow the results of casting to floating-point or complex
5694 types be confused with actual constants, or casts involving
5695 integer and pointer types other than direct integer-to-integer
5696 and integer-to-pointer be confused with integer constant
5697 expressions and null pointer constants. */
5698 if (TREE_CODE (value) == REAL_CST
5699 || TREE_CODE (value) == COMPLEX_CST
5700 || (TREE_CODE (value) == INTEGER_CST
5701 && !((TREE_CODE (expr) == INTEGER_CST
5702 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5703 || TREE_CODE (expr) == REAL_CST
5704 || TREE_CODE (expr) == COMPLEX_CST)))
5705 value = build1 (NOP_EXPR, type, value);
5707 protected_set_expr_location (value, loc);
5708 return value;
5711 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5712 location of the open paren of the cast, or the position of the cast
5713 expr. */
5714 tree
5715 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5717 tree type;
5718 tree type_expr = NULL_TREE;
5719 bool type_expr_const = true;
5720 tree ret;
5721 int saved_wsp = warn_strict_prototypes;
5723 /* This avoids warnings about unprototyped casts on
5724 integers. E.g. "#define SIG_DFL (void(*)())0". */
5725 if (TREE_CODE (expr) == INTEGER_CST)
5726 warn_strict_prototypes = 0;
5727 type = groktypename (type_name, &type_expr, &type_expr_const);
5728 warn_strict_prototypes = saved_wsp;
5730 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5731 && reject_gcc_builtin (expr))
5732 return error_mark_node;
5734 ret = build_c_cast (loc, type, expr);
5735 if (type_expr)
5737 bool inner_expr_const = true;
5738 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5739 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5740 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5741 && inner_expr_const);
5742 SET_EXPR_LOCATION (ret, loc);
5745 if (!EXPR_HAS_LOCATION (ret))
5746 protected_set_expr_location (ret, loc);
5748 /* C++ does not permits types to be defined in a cast, but it
5749 allows references to incomplete types. */
5750 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5751 warning_at (loc, OPT_Wc___compat,
5752 "defining a type in a cast is invalid in C++");
5754 return ret;
5757 /* Build an assignment expression of lvalue LHS from value RHS.
5758 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5759 may differ from TREE_TYPE (LHS) for an enum bitfield.
5760 MODIFYCODE is the code for a binary operator that we use
5761 to combine the old value of LHS with RHS to get the new value.
5762 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5763 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5764 which may differ from TREE_TYPE (RHS) for an enum value.
5766 LOCATION is the location of the MODIFYCODE operator.
5767 RHS_LOC is the location of the RHS. */
5769 tree
5770 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5771 enum tree_code modifycode,
5772 location_t rhs_loc, tree rhs, tree rhs_origtype)
5774 tree result;
5775 tree newrhs;
5776 tree rhseval = NULL_TREE;
5777 tree lhstype = TREE_TYPE (lhs);
5778 tree olhstype = lhstype;
5779 bool npc;
5780 bool is_atomic_op;
5782 /* Types that aren't fully specified cannot be used in assignments. */
5783 lhs = require_complete_type (location, lhs);
5785 /* Avoid duplicate error messages from operands that had errors. */
5786 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5787 return error_mark_node;
5789 /* Ensure an error for assigning a non-lvalue array to an array in
5790 C90. */
5791 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5793 error_at (location, "assignment to expression with array type");
5794 return error_mark_node;
5797 /* For ObjC properties, defer this check. */
5798 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5799 return error_mark_node;
5801 is_atomic_op = really_atomic_lvalue (lhs);
5803 newrhs = rhs;
5805 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5807 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5808 lhs_origtype, modifycode, rhs_loc, rhs,
5809 rhs_origtype);
5810 if (inner == error_mark_node)
5811 return error_mark_node;
5812 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5813 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5814 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5815 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5816 protected_set_expr_location (result, location);
5817 return result;
5820 /* If a binary op has been requested, combine the old LHS value with the RHS
5821 producing the value we should actually store into the LHS. */
5823 if (modifycode != NOP_EXPR)
5825 lhs = c_fully_fold (lhs, false, NULL, true);
5826 lhs = stabilize_reference (lhs);
5828 /* Construct the RHS for any non-atomic compound assignemnt. */
5829 if (!is_atomic_op)
5831 /* If in LHS op= RHS the RHS has side-effects, ensure they
5832 are preevaluated before the rest of the assignment expression's
5833 side-effects, because RHS could contain e.g. function calls
5834 that modify LHS. */
5835 if (TREE_SIDE_EFFECTS (rhs))
5837 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5838 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5839 else
5840 newrhs = save_expr (rhs);
5841 rhseval = newrhs;
5842 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5843 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5844 newrhs);
5846 newrhs = build_binary_op (location,
5847 modifycode, lhs, newrhs, true);
5849 /* The original type of the right hand side is no longer
5850 meaningful. */
5851 rhs_origtype = NULL_TREE;
5855 if (c_dialect_objc ())
5857 /* Check if we are modifying an Objective-C property reference;
5858 if so, we need to generate setter calls. */
5859 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5860 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5861 else
5862 result = objc_maybe_build_modify_expr (lhs, newrhs);
5863 if (result)
5864 goto return_result;
5866 /* Else, do the check that we postponed for Objective-C. */
5867 if (!lvalue_or_else (location, lhs, lv_assign))
5868 return error_mark_node;
5871 /* Give an error for storing in something that is 'const'. */
5873 if (TYPE_READONLY (lhstype)
5874 || (RECORD_OR_UNION_TYPE_P (lhstype)
5875 && C_TYPE_FIELDS_READONLY (lhstype)))
5877 readonly_error (location, lhs, lv_assign);
5878 return error_mark_node;
5880 else if (TREE_READONLY (lhs))
5881 readonly_warning (lhs, lv_assign);
5883 /* If storing into a structure or union member,
5884 it has probably been given type `int'.
5885 Compute the type that would go with
5886 the actual amount of storage the member occupies. */
5888 if (TREE_CODE (lhs) == COMPONENT_REF
5889 && (TREE_CODE (lhstype) == INTEGER_TYPE
5890 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5891 || TREE_CODE (lhstype) == REAL_TYPE
5892 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5893 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5895 /* If storing in a field that is in actuality a short or narrower than one,
5896 we must store in the field in its actual type. */
5898 if (lhstype != TREE_TYPE (lhs))
5900 lhs = copy_node (lhs);
5901 TREE_TYPE (lhs) = lhstype;
5904 /* Issue -Wc++-compat warnings about an assignment to an enum type
5905 when LHS does not have its original type. This happens for,
5906 e.g., an enum bitfield in a struct. */
5907 if (warn_cxx_compat
5908 && lhs_origtype != NULL_TREE
5909 && lhs_origtype != lhstype
5910 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5912 tree checktype = (rhs_origtype != NULL_TREE
5913 ? rhs_origtype
5914 : TREE_TYPE (rhs));
5915 if (checktype != error_mark_node
5916 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5917 || (is_atomic_op && modifycode != NOP_EXPR)))
5918 warning_at (location, OPT_Wc___compat,
5919 "enum conversion in assignment is invalid in C++");
5922 /* If the lhs is atomic, remove that qualifier. */
5923 if (is_atomic_op)
5925 lhstype = build_qualified_type (lhstype,
5926 (TYPE_QUALS (lhstype)
5927 & ~TYPE_QUAL_ATOMIC));
5928 olhstype = build_qualified_type (olhstype,
5929 (TYPE_QUALS (lhstype)
5930 & ~TYPE_QUAL_ATOMIC));
5933 /* Convert new value to destination type. Fold it first, then
5934 restore any excess precision information, for the sake of
5935 conversion warnings. */
5937 if (!(is_atomic_op && modifycode != NOP_EXPR))
5939 tree rhs_semantic_type = NULL_TREE;
5940 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5942 rhs_semantic_type = TREE_TYPE (newrhs);
5943 newrhs = TREE_OPERAND (newrhs, 0);
5945 npc = null_pointer_constant_p (newrhs);
5946 newrhs = c_fully_fold (newrhs, false, NULL);
5947 if (rhs_semantic_type)
5948 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5949 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5950 rhs_origtype, ic_assign, npc,
5951 NULL_TREE, NULL_TREE, 0);
5952 if (TREE_CODE (newrhs) == ERROR_MARK)
5953 return error_mark_node;
5956 /* Emit ObjC write barrier, if necessary. */
5957 if (c_dialect_objc () && flag_objc_gc)
5959 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5960 if (result)
5962 protected_set_expr_location (result, location);
5963 goto return_result;
5967 /* Scan operands. */
5969 if (is_atomic_op)
5970 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5971 else
5973 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5974 TREE_SIDE_EFFECTS (result) = 1;
5975 protected_set_expr_location (result, location);
5978 /* If we got the LHS in a different type for storing in,
5979 convert the result back to the nominal type of LHS
5980 so that the value we return always has the same type
5981 as the LHS argument. */
5983 if (olhstype == TREE_TYPE (result))
5984 goto return_result;
5986 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5987 rhs_origtype, ic_assign, false, NULL_TREE,
5988 NULL_TREE, 0);
5989 protected_set_expr_location (result, location);
5991 return_result:
5992 if (rhseval)
5993 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5994 return result;
5997 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5998 This is used to implement -fplan9-extensions. */
6000 static bool
6001 find_anonymous_field_with_type (tree struct_type, tree type)
6003 tree field;
6004 bool found;
6006 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6007 found = false;
6008 for (field = TYPE_FIELDS (struct_type);
6009 field != NULL_TREE;
6010 field = TREE_CHAIN (field))
6012 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6013 ? c_build_qualified_type (TREE_TYPE (field),
6014 TYPE_QUAL_ATOMIC)
6015 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6016 if (DECL_NAME (field) == NULL
6017 && comptypes (type, fieldtype))
6019 if (found)
6020 return false;
6021 found = true;
6023 else if (DECL_NAME (field) == NULL
6024 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6025 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6027 if (found)
6028 return false;
6029 found = true;
6032 return found;
6035 /* RHS is an expression whose type is pointer to struct. If there is
6036 an anonymous field in RHS with type TYPE, then return a pointer to
6037 that field in RHS. This is used with -fplan9-extensions. This
6038 returns NULL if no conversion could be found. */
6040 static tree
6041 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6043 tree rhs_struct_type, lhs_main_type;
6044 tree field, found_field;
6045 bool found_sub_field;
6046 tree ret;
6048 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6049 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6050 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6052 gcc_assert (POINTER_TYPE_P (type));
6053 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6054 ? c_build_qualified_type (TREE_TYPE (type),
6055 TYPE_QUAL_ATOMIC)
6056 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6058 found_field = NULL_TREE;
6059 found_sub_field = false;
6060 for (field = TYPE_FIELDS (rhs_struct_type);
6061 field != NULL_TREE;
6062 field = TREE_CHAIN (field))
6064 if (DECL_NAME (field) != NULL_TREE
6065 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6066 continue;
6067 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6068 ? c_build_qualified_type (TREE_TYPE (field),
6069 TYPE_QUAL_ATOMIC)
6070 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6071 if (comptypes (lhs_main_type, fieldtype))
6073 if (found_field != NULL_TREE)
6074 return NULL_TREE;
6075 found_field = field;
6077 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6078 lhs_main_type))
6080 if (found_field != NULL_TREE)
6081 return NULL_TREE;
6082 found_field = field;
6083 found_sub_field = true;
6087 if (found_field == NULL_TREE)
6088 return NULL_TREE;
6090 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6091 build_fold_indirect_ref (rhs), found_field,
6092 NULL_TREE);
6093 ret = build_fold_addr_expr_loc (location, ret);
6095 if (found_sub_field)
6097 ret = convert_to_anonymous_field (location, type, ret);
6098 gcc_assert (ret != NULL_TREE);
6101 return ret;
6104 /* Issue an error message for a bad initializer component.
6105 GMSGID identifies the message.
6106 The component name is taken from the spelling stack. */
6108 static void
6109 error_init (location_t loc, const char *gmsgid)
6111 char *ofwhat;
6113 /* The gmsgid may be a format string with %< and %>. */
6114 error_at (loc, gmsgid);
6115 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6116 if (*ofwhat)
6117 inform (loc, "(near initialization for %qs)", ofwhat);
6120 /* Issue a pedantic warning for a bad initializer component. OPT is
6121 the option OPT_* (from options.h) controlling this warning or 0 if
6122 it is unconditionally given. GMSGID identifies the message. The
6123 component name is taken from the spelling stack. */
6125 static void ATTRIBUTE_GCC_DIAG (3,0)
6126 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6128 /* Use the location where a macro was expanded rather than where
6129 it was defined to make sure macros defined in system headers
6130 but used incorrectly elsewhere are diagnosed. */
6131 source_location exploc = expansion_point_location_if_in_system_header (loc);
6133 va_list ap;
6134 va_start (ap, gmsgid);
6135 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6136 va_end (ap);
6137 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6138 if (*ofwhat && warned)
6139 inform (exploc, "(near initialization for %qs)", ofwhat);
6142 /* Issue a warning for a bad initializer component.
6144 OPT is the OPT_W* value corresponding to the warning option that
6145 controls this warning. GMSGID identifies the message. The
6146 component name is taken from the spelling stack. */
6148 static void
6149 warning_init (location_t loc, int opt, const char *gmsgid)
6151 char *ofwhat;
6152 bool warned;
6154 /* Use the location where a macro was expanded rather than where
6155 it was defined to make sure macros defined in system headers
6156 but used incorrectly elsewhere are diagnosed. */
6157 source_location exploc = expansion_point_location_if_in_system_header (loc);
6159 /* The gmsgid may be a format string with %< and %>. */
6160 warned = warning_at (exploc, opt, gmsgid);
6161 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6162 if (*ofwhat && warned)
6163 inform (exploc, "(near initialization for %qs)", ofwhat);
6166 /* If TYPE is an array type and EXPR is a parenthesized string
6167 constant, warn if pedantic that EXPR is being used to initialize an
6168 object of type TYPE. */
6170 void
6171 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6173 if (pedantic
6174 && TREE_CODE (type) == ARRAY_TYPE
6175 && TREE_CODE (expr.value) == STRING_CST
6176 && expr.original_code != STRING_CST)
6177 pedwarn_init (loc, OPT_Wpedantic,
6178 "array initialized from parenthesized string constant");
6181 /* Attempt to locate the parameter with the given index within FNDECL,
6182 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6184 static location_t
6185 get_fndecl_argument_location (tree fndecl, int argnum)
6187 int i;
6188 tree param;
6190 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6191 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6192 i < argnum && param;
6193 i++, param = TREE_CHAIN (param))
6196 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6197 return DECL_SOURCE_LOCATION (FNDECL). */
6198 if (param == NULL)
6199 return DECL_SOURCE_LOCATION (fndecl);
6201 return DECL_SOURCE_LOCATION (param);
6204 /* Issue a note about a mismatching argument for parameter PARMNUM
6205 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6206 Attempt to issue the note at the pertinent parameter of the decl;
6207 failing that issue it at the location of FUNDECL; failing that
6208 issue it at PLOC. */
6210 static void
6211 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6212 tree expected_type, tree actual_type)
6214 location_t loc;
6215 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6216 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6217 else
6218 loc = ploc;
6220 inform (loc,
6221 "expected %qT but argument is of type %qT",
6222 expected_type, actual_type);
6225 /* Convert value RHS to type TYPE as preparation for an assignment to
6226 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6227 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6228 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6229 constant before any folding.
6230 The real work of conversion is done by `convert'.
6231 The purpose of this function is to generate error messages
6232 for assignments that are not allowed in C.
6233 ERRTYPE says whether it is argument passing, assignment,
6234 initialization or return.
6236 In the following example, '~' denotes where EXPR_LOC and '^' where
6237 LOCATION point to:
6239 f (var); [ic_argpass]
6240 ^ ~~~
6241 x = var; [ic_assign]
6242 ^ ~~~;
6243 int x = var; [ic_init]
6245 return x; [ic_return]
6248 FUNCTION is a tree for the function being called.
6249 PARMNUM is the number of the argument, for printing in error messages. */
6251 static tree
6252 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6253 tree rhs, tree origtype, enum impl_conv errtype,
6254 bool null_pointer_constant, tree fundecl,
6255 tree function, int parmnum)
6257 enum tree_code codel = TREE_CODE (type);
6258 tree orig_rhs = rhs;
6259 tree rhstype;
6260 enum tree_code coder;
6261 tree rname = NULL_TREE;
6262 bool objc_ok = false;
6264 /* Use the expansion point location to handle cases such as user's
6265 function returning a wrong-type macro defined in a system header. */
6266 location = expansion_point_location_if_in_system_header (location);
6268 if (errtype == ic_argpass)
6270 tree selector;
6271 /* Change pointer to function to the function itself for
6272 diagnostics. */
6273 if (TREE_CODE (function) == ADDR_EXPR
6274 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6275 function = TREE_OPERAND (function, 0);
6277 /* Handle an ObjC selector specially for diagnostics. */
6278 selector = objc_message_selector ();
6279 rname = function;
6280 if (selector && parmnum > 2)
6282 rname = selector;
6283 parmnum -= 2;
6287 /* This macro is used to emit diagnostics to ensure that all format
6288 strings are complete sentences, visible to gettext and checked at
6289 compile time. */
6290 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6291 do { \
6292 switch (errtype) \
6294 case ic_argpass: \
6295 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6296 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6297 break; \
6298 case ic_assign: \
6299 pedwarn (LOCATION, OPT, AS); \
6300 break; \
6301 case ic_init: \
6302 pedwarn_init (LOCATION, OPT, IN); \
6303 break; \
6304 case ic_return: \
6305 pedwarn (LOCATION, OPT, RE); \
6306 break; \
6307 default: \
6308 gcc_unreachable (); \
6310 } while (0)
6312 /* This macro is used to emit diagnostics to ensure that all format
6313 strings are complete sentences, visible to gettext and checked at
6314 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6315 extra parameter to enumerate qualifiers. */
6316 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6317 do { \
6318 switch (errtype) \
6320 case ic_argpass: \
6321 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6322 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6323 break; \
6324 case ic_assign: \
6325 pedwarn (LOCATION, OPT, AS, QUALS); \
6326 break; \
6327 case ic_init: \
6328 pedwarn (LOCATION, OPT, IN, QUALS); \
6329 break; \
6330 case ic_return: \
6331 pedwarn (LOCATION, OPT, RE, QUALS); \
6332 break; \
6333 default: \
6334 gcc_unreachable (); \
6336 } while (0)
6338 /* This macro is used to emit diagnostics to ensure that all format
6339 strings are complete sentences, visible to gettext and checked at
6340 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6341 warning_at instead of pedwarn. */
6342 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6343 do { \
6344 switch (errtype) \
6346 case ic_argpass: \
6347 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6348 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6349 break; \
6350 case ic_assign: \
6351 warning_at (LOCATION, OPT, AS, QUALS); \
6352 break; \
6353 case ic_init: \
6354 warning_at (LOCATION, OPT, IN, QUALS); \
6355 break; \
6356 case ic_return: \
6357 warning_at (LOCATION, OPT, RE, QUALS); \
6358 break; \
6359 default: \
6360 gcc_unreachable (); \
6362 } while (0)
6364 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6365 rhs = TREE_OPERAND (rhs, 0);
6367 rhstype = TREE_TYPE (rhs);
6368 coder = TREE_CODE (rhstype);
6370 if (coder == ERROR_MARK)
6371 return error_mark_node;
6373 if (c_dialect_objc ())
6375 int parmno;
6377 switch (errtype)
6379 case ic_return:
6380 parmno = 0;
6381 break;
6383 case ic_assign:
6384 parmno = -1;
6385 break;
6387 case ic_init:
6388 parmno = -2;
6389 break;
6391 default:
6392 parmno = parmnum;
6393 break;
6396 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6399 if (warn_cxx_compat)
6401 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6402 if (checktype != error_mark_node
6403 && TREE_CODE (type) == ENUMERAL_TYPE
6404 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6405 switch (errtype)
6407 case ic_argpass:
6408 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6409 "passing argument %d of %qE is invalid in C++",
6410 parmnum, rname))
6411 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6412 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6413 "expected %qT but argument is of type %qT",
6414 type, rhstype);
6415 break;
6416 case ic_assign:
6417 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6418 "%qT in assignment is invalid in C++", rhstype, type);
6419 break;
6420 case ic_init:
6421 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6422 "%qT to %qT in initialization is invalid in C++",
6423 rhstype, type);
6424 break;
6425 case ic_return:
6426 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6427 "%qT in return is invalid in C++", rhstype, type);
6428 break;
6429 default:
6430 gcc_unreachable ();
6434 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6435 return rhs;
6437 if (coder == VOID_TYPE)
6439 /* Except for passing an argument to an unprototyped function,
6440 this is a constraint violation. When passing an argument to
6441 an unprototyped function, it is compile-time undefined;
6442 making it a constraint in that case was rejected in
6443 DR#252. */
6444 error_at (location, "void value not ignored as it ought to be");
6445 return error_mark_node;
6447 rhs = require_complete_type (location, rhs);
6448 if (rhs == error_mark_node)
6449 return error_mark_node;
6451 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6452 return error_mark_node;
6454 /* A non-reference type can convert to a reference. This handles
6455 va_start, va_copy and possibly port built-ins. */
6456 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6458 if (!lvalue_p (rhs))
6460 error_at (location, "cannot pass rvalue to reference parameter");
6461 return error_mark_node;
6463 if (!c_mark_addressable (rhs))
6464 return error_mark_node;
6465 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6466 SET_EXPR_LOCATION (rhs, location);
6468 rhs = convert_for_assignment (location, expr_loc,
6469 build_pointer_type (TREE_TYPE (type)),
6470 rhs, origtype, errtype,
6471 null_pointer_constant, fundecl, function,
6472 parmnum);
6473 if (rhs == error_mark_node)
6474 return error_mark_node;
6476 rhs = build1 (NOP_EXPR, type, rhs);
6477 SET_EXPR_LOCATION (rhs, location);
6478 return rhs;
6480 /* Some types can interconvert without explicit casts. */
6481 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6482 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6483 return convert (type, rhs);
6484 /* Arithmetic types all interconvert, and enum is treated like int. */
6485 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6486 || codel == FIXED_POINT_TYPE
6487 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6488 || codel == BOOLEAN_TYPE)
6489 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6490 || coder == FIXED_POINT_TYPE
6491 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6492 || coder == BOOLEAN_TYPE))
6494 tree ret;
6495 bool save = in_late_binary_op;
6496 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6497 || (coder == REAL_TYPE
6498 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6499 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6500 in_late_binary_op = true;
6501 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6502 ? expr_loc : location, type, orig_rhs);
6503 in_late_binary_op = save;
6504 return ret;
6507 /* Aggregates in different TUs might need conversion. */
6508 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6509 && codel == coder
6510 && comptypes (type, rhstype))
6511 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6512 ? expr_loc : location, type, rhs);
6514 /* Conversion to a transparent union or record from its member types.
6515 This applies only to function arguments. */
6516 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6517 && TYPE_TRANSPARENT_AGGR (type))
6518 && errtype == ic_argpass)
6520 tree memb, marginal_memb = NULL_TREE;
6522 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6524 tree memb_type = TREE_TYPE (memb);
6526 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6527 TYPE_MAIN_VARIANT (rhstype)))
6528 break;
6530 if (TREE_CODE (memb_type) != POINTER_TYPE)
6531 continue;
6533 if (coder == POINTER_TYPE)
6535 tree ttl = TREE_TYPE (memb_type);
6536 tree ttr = TREE_TYPE (rhstype);
6538 /* Any non-function converts to a [const][volatile] void *
6539 and vice versa; otherwise, targets must be the same.
6540 Meanwhile, the lhs target must have all the qualifiers of
6541 the rhs. */
6542 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6543 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6544 || comp_target_types (location, memb_type, rhstype))
6546 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6547 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6548 /* If this type won't generate any warnings, use it. */
6549 if (lquals == rquals
6550 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6551 && TREE_CODE (ttl) == FUNCTION_TYPE)
6552 ? ((lquals | rquals) == rquals)
6553 : ((lquals | rquals) == lquals)))
6554 break;
6556 /* Keep looking for a better type, but remember this one. */
6557 if (!marginal_memb)
6558 marginal_memb = memb;
6562 /* Can convert integer zero to any pointer type. */
6563 if (null_pointer_constant)
6565 rhs = null_pointer_node;
6566 break;
6570 if (memb || marginal_memb)
6572 if (!memb)
6574 /* We have only a marginally acceptable member type;
6575 it needs a warning. */
6576 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6577 tree ttr = TREE_TYPE (rhstype);
6579 /* Const and volatile mean something different for function
6580 types, so the usual warnings are not appropriate. */
6581 if (TREE_CODE (ttr) == FUNCTION_TYPE
6582 && TREE_CODE (ttl) == FUNCTION_TYPE)
6584 /* Because const and volatile on functions are
6585 restrictions that say the function will not do
6586 certain things, it is okay to use a const or volatile
6587 function where an ordinary one is wanted, but not
6588 vice-versa. */
6589 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6590 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6591 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6592 OPT_Wdiscarded_qualifiers,
6593 G_("passing argument %d of %qE "
6594 "makes %q#v qualified function "
6595 "pointer from unqualified"),
6596 G_("assignment makes %q#v qualified "
6597 "function pointer from "
6598 "unqualified"),
6599 G_("initialization makes %q#v qualified "
6600 "function pointer from "
6601 "unqualified"),
6602 G_("return makes %q#v qualified function "
6603 "pointer from unqualified"),
6604 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6606 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6607 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6608 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6609 OPT_Wdiscarded_qualifiers,
6610 G_("passing argument %d of %qE discards "
6611 "%qv qualifier from pointer target type"),
6612 G_("assignment discards %qv qualifier "
6613 "from pointer target type"),
6614 G_("initialization discards %qv qualifier "
6615 "from pointer target type"),
6616 G_("return discards %qv qualifier from "
6617 "pointer target type"),
6618 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6620 memb = marginal_memb;
6623 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6624 pedwarn (location, OPT_Wpedantic,
6625 "ISO C prohibits argument conversion to union type");
6627 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6628 return build_constructor_single (type, memb, rhs);
6632 /* Conversions among pointers */
6633 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6634 && (coder == codel))
6636 tree ttl = TREE_TYPE (type);
6637 tree ttr = TREE_TYPE (rhstype);
6638 tree mvl = ttl;
6639 tree mvr = ttr;
6640 bool is_opaque_pointer;
6641 int target_cmp = 0; /* Cache comp_target_types () result. */
6642 addr_space_t asl;
6643 addr_space_t asr;
6645 if (TREE_CODE (mvl) != ARRAY_TYPE)
6646 mvl = (TYPE_ATOMIC (mvl)
6647 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6648 TYPE_QUAL_ATOMIC)
6649 : TYPE_MAIN_VARIANT (mvl));
6650 if (TREE_CODE (mvr) != ARRAY_TYPE)
6651 mvr = (TYPE_ATOMIC (mvr)
6652 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6653 TYPE_QUAL_ATOMIC)
6654 : TYPE_MAIN_VARIANT (mvr));
6655 /* Opaque pointers are treated like void pointers. */
6656 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6658 /* The Plan 9 compiler permits a pointer to a struct to be
6659 automatically converted into a pointer to an anonymous field
6660 within the struct. */
6661 if (flag_plan9_extensions
6662 && RECORD_OR_UNION_TYPE_P (mvl)
6663 && RECORD_OR_UNION_TYPE_P (mvr)
6664 && mvl != mvr)
6666 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6667 if (new_rhs != NULL_TREE)
6669 rhs = new_rhs;
6670 rhstype = TREE_TYPE (rhs);
6671 coder = TREE_CODE (rhstype);
6672 ttr = TREE_TYPE (rhstype);
6673 mvr = TYPE_MAIN_VARIANT (ttr);
6677 /* C++ does not allow the implicit conversion void* -> T*. However,
6678 for the purpose of reducing the number of false positives, we
6679 tolerate the special case of
6681 int *p = NULL;
6683 where NULL is typically defined in C to be '(void *) 0'. */
6684 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6685 warning_at (errtype == ic_argpass ? expr_loc : location,
6686 OPT_Wc___compat,
6687 "request for implicit conversion "
6688 "from %qT to %qT not permitted in C++", rhstype, type);
6690 /* See if the pointers point to incompatible address spaces. */
6691 asl = TYPE_ADDR_SPACE (ttl);
6692 asr = TYPE_ADDR_SPACE (ttr);
6693 if (!null_pointer_constant_p (rhs)
6694 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6696 switch (errtype)
6698 case ic_argpass:
6699 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6700 "non-enclosed address space", parmnum, rname);
6701 break;
6702 case ic_assign:
6703 error_at (location, "assignment from pointer to "
6704 "non-enclosed address space");
6705 break;
6706 case ic_init:
6707 error_at (location, "initialization from pointer to "
6708 "non-enclosed address space");
6709 break;
6710 case ic_return:
6711 error_at (location, "return from pointer to "
6712 "non-enclosed address space");
6713 break;
6714 default:
6715 gcc_unreachable ();
6717 return error_mark_node;
6720 /* Check if the right-hand side has a format attribute but the
6721 left-hand side doesn't. */
6722 if (warn_suggest_attribute_format
6723 && check_missing_format_attribute (type, rhstype))
6725 switch (errtype)
6727 case ic_argpass:
6728 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6729 "argument %d of %qE might be "
6730 "a candidate for a format attribute",
6731 parmnum, rname);
6732 break;
6733 case ic_assign:
6734 warning_at (location, OPT_Wsuggest_attribute_format,
6735 "assignment left-hand side might be "
6736 "a candidate for a format attribute");
6737 break;
6738 case ic_init:
6739 warning_at (location, OPT_Wsuggest_attribute_format,
6740 "initialization left-hand side might be "
6741 "a candidate for a format attribute");
6742 break;
6743 case ic_return:
6744 warning_at (location, OPT_Wsuggest_attribute_format,
6745 "return type might be "
6746 "a candidate for a format attribute");
6747 break;
6748 default:
6749 gcc_unreachable ();
6753 /* Any non-function converts to a [const][volatile] void *
6754 and vice versa; otherwise, targets must be the same.
6755 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6756 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6757 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6758 || (target_cmp = comp_target_types (location, type, rhstype))
6759 || is_opaque_pointer
6760 || ((c_common_unsigned_type (mvl)
6761 == c_common_unsigned_type (mvr))
6762 && (c_common_signed_type (mvl)
6763 == c_common_signed_type (mvr))
6764 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6766 /* Warn about loss of qualifers from pointers to arrays with
6767 qualifiers on the element type. */
6768 if (TREE_CODE (ttr) == ARRAY_TYPE)
6770 ttr = strip_array_types (ttr);
6771 ttl = strip_array_types (ttl);
6773 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6774 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6775 WARNING_FOR_QUALIFIERS (location, expr_loc,
6776 OPT_Wdiscarded_array_qualifiers,
6777 G_("passing argument %d of %qE discards "
6778 "%qv qualifier from pointer target type"),
6779 G_("assignment discards %qv qualifier "
6780 "from pointer target type"),
6781 G_("initialization discards %qv qualifier "
6782 "from pointer target type"),
6783 G_("return discards %qv qualifier from "
6784 "pointer target type"),
6785 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6787 else if (pedantic
6788 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6790 (VOID_TYPE_P (ttr)
6791 && !null_pointer_constant
6792 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6793 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6794 G_("ISO C forbids passing argument %d of "
6795 "%qE between function pointer "
6796 "and %<void *%>"),
6797 G_("ISO C forbids assignment between "
6798 "function pointer and %<void *%>"),
6799 G_("ISO C forbids initialization between "
6800 "function pointer and %<void *%>"),
6801 G_("ISO C forbids return between function "
6802 "pointer and %<void *%>"));
6803 /* Const and volatile mean something different for function types,
6804 so the usual warnings are not appropriate. */
6805 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6806 && TREE_CODE (ttl) != FUNCTION_TYPE)
6808 /* Don't warn about loss of qualifier for conversions from
6809 qualified void* to pointers to arrays with corresponding
6810 qualifier on the element type. */
6811 if (!pedantic)
6812 ttl = strip_array_types (ttl);
6814 /* Assignments between atomic and non-atomic objects are OK. */
6815 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6816 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6818 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6819 OPT_Wdiscarded_qualifiers,
6820 G_("passing argument %d of %qE discards "
6821 "%qv qualifier from pointer target type"),
6822 G_("assignment discards %qv qualifier "
6823 "from pointer target type"),
6824 G_("initialization discards %qv qualifier "
6825 "from pointer target type"),
6826 G_("return discards %qv qualifier from "
6827 "pointer target type"),
6828 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6830 /* If this is not a case of ignoring a mismatch in signedness,
6831 no warning. */
6832 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6833 || target_cmp)
6835 /* If there is a mismatch, do warn. */
6836 else if (warn_pointer_sign)
6837 switch (errtype)
6839 case ic_argpass:
6840 if (pedwarn (expr_loc, OPT_Wpointer_sign,
6841 "pointer targets in passing argument %d of "
6842 "%qE differ in signedness", parmnum, rname))
6843 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6844 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6845 "expected %qT but argument is of type %qT",
6846 type, rhstype);
6847 break;
6848 case ic_assign:
6849 pedwarn (location, OPT_Wpointer_sign,
6850 "pointer targets in assignment from %qT to %qT "
6851 "differ in signedness", rhstype, type);
6852 break;
6853 case ic_init:
6854 pedwarn_init (location, OPT_Wpointer_sign,
6855 "pointer targets in initialization of %qT "
6856 "from %qT differ in signedness", type,
6857 rhstype);
6858 break;
6859 case ic_return:
6860 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6861 "returning %qT from a function with return type "
6862 "%qT differ in signedness", rhstype, type);
6863 break;
6864 default:
6865 gcc_unreachable ();
6868 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6869 && TREE_CODE (ttr) == FUNCTION_TYPE)
6871 /* Because const and volatile on functions are restrictions
6872 that say the function will not do certain things,
6873 it is okay to use a const or volatile function
6874 where an ordinary one is wanted, but not vice-versa. */
6875 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6876 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6877 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6878 OPT_Wdiscarded_qualifiers,
6879 G_("passing argument %d of %qE makes "
6880 "%q#v qualified function pointer "
6881 "from unqualified"),
6882 G_("assignment makes %q#v qualified function "
6883 "pointer from unqualified"),
6884 G_("initialization makes %q#v qualified "
6885 "function pointer from unqualified"),
6886 G_("return makes %q#v qualified function "
6887 "pointer from unqualified"),
6888 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6891 /* Avoid warning about the volatile ObjC EH puts on decls. */
6892 else if (!objc_ok)
6894 switch (errtype)
6896 case ic_argpass:
6897 if (pedwarn (expr_loc, OPT_Wincompatible_pointer_types,
6898 "passing argument %d of %qE from incompatible "
6899 "pointer type", parmnum, rname))
6900 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
6901 break;
6902 case ic_assign:
6903 pedwarn (location, OPT_Wincompatible_pointer_types,
6904 "assignment to %qT from incompatible pointer type %qT",
6905 type, rhstype);
6906 break;
6907 case ic_init:
6908 pedwarn_init (location, OPT_Wincompatible_pointer_types,
6909 "initialization of %qT from incompatible pointer "
6910 "type %qT", type, rhstype);
6911 break;
6912 case ic_return:
6913 pedwarn (location, OPT_Wincompatible_pointer_types,
6914 "returning %qT from a function with incompatible "
6915 "return type %qT", rhstype, type);
6916 break;
6917 default:
6918 gcc_unreachable ();
6922 return convert (type, rhs);
6924 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6926 /* ??? This should not be an error when inlining calls to
6927 unprototyped functions. */
6928 error_at (location, "invalid use of non-lvalue array");
6929 return error_mark_node;
6931 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6933 /* An explicit constant 0 can convert to a pointer,
6934 or one that results from arithmetic, even including
6935 a cast to integer type. */
6936 if (!null_pointer_constant)
6937 switch (errtype)
6939 case ic_argpass:
6940 if (pedwarn (expr_loc, OPT_Wint_conversion,
6941 "passing argument %d of %qE makes pointer from "
6942 "integer without a cast", parmnum, rname))
6943 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
6944 break;
6945 case ic_assign:
6946 pedwarn (location, OPT_Wint_conversion,
6947 "assignment to %qT from %qT makes pointer from integer "
6948 "without a cast", type, rhstype);
6949 break;
6950 case ic_init:
6951 pedwarn_init (location, OPT_Wint_conversion,
6952 "initialization of %qT from %qT makes pointer from "
6953 "integer without a cast", type, rhstype);
6954 break;
6955 case ic_return:
6956 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6957 "function with return type %qT makes pointer from "
6958 "integer without a cast", rhstype, type);
6959 break;
6960 default:
6961 gcc_unreachable ();
6964 return convert (type, rhs);
6966 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6968 switch (errtype)
6970 case ic_argpass:
6971 if (pedwarn (expr_loc, OPT_Wint_conversion,
6972 "passing argument %d of %qE makes integer from "
6973 "pointer without a cast", parmnum, rname))
6974 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
6975 break;
6976 case ic_assign:
6977 pedwarn (location, OPT_Wint_conversion,
6978 "assignment to %qT from %qT makes integer from pointer "
6979 "without a cast", type, rhstype);
6980 break;
6981 case ic_init:
6982 pedwarn_init (location, OPT_Wint_conversion,
6983 "initialization of %qT from %qT makes integer from "
6984 "pointer without a cast", type, rhstype);
6985 break;
6986 case ic_return:
6987 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6988 "function with return type %qT makes integer from "
6989 "pointer without a cast", rhstype, type);
6990 break;
6991 default:
6992 gcc_unreachable ();
6995 return convert (type, rhs);
6997 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6999 tree ret;
7000 bool save = in_late_binary_op;
7001 in_late_binary_op = true;
7002 ret = convert (type, rhs);
7003 in_late_binary_op = save;
7004 return ret;
7007 switch (errtype)
7009 case ic_argpass:
7010 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
7011 rname);
7012 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7013 break;
7014 case ic_assign:
7015 error_at (location, "incompatible types when assigning to type %qT from "
7016 "type %qT", type, rhstype);
7017 break;
7018 case ic_init:
7019 error_at (location,
7020 "incompatible types when initializing type %qT using type %qT",
7021 type, rhstype);
7022 break;
7023 case ic_return:
7024 error_at (location,
7025 "incompatible types when returning type %qT but %qT was "
7026 "expected", rhstype, type);
7027 break;
7028 default:
7029 gcc_unreachable ();
7032 return error_mark_node;
7035 /* If VALUE is a compound expr all of whose expressions are constant, then
7036 return its value. Otherwise, return error_mark_node.
7038 This is for handling COMPOUND_EXPRs as initializer elements
7039 which is allowed with a warning when -pedantic is specified. */
7041 static tree
7042 valid_compound_expr_initializer (tree value, tree endtype)
7044 if (TREE_CODE (value) == COMPOUND_EXPR)
7046 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7047 == error_mark_node)
7048 return error_mark_node;
7049 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7050 endtype);
7052 else if (!initializer_constant_valid_p (value, endtype))
7053 return error_mark_node;
7054 else
7055 return value;
7058 /* Perform appropriate conversions on the initial value of a variable,
7059 store it in the declaration DECL,
7060 and print any error messages that are appropriate.
7061 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7062 If the init is invalid, store an ERROR_MARK.
7064 INIT_LOC is the location of the initial value. */
7066 void
7067 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7069 tree value, type;
7070 bool npc = false;
7072 /* If variable's type was invalidly declared, just ignore it. */
7074 type = TREE_TYPE (decl);
7075 if (TREE_CODE (type) == ERROR_MARK)
7076 return;
7078 /* Digest the specified initializer into an expression. */
7080 if (init)
7081 npc = null_pointer_constant_p (init);
7082 value = digest_init (init_loc, type, init, origtype, npc,
7083 true, TREE_STATIC (decl));
7085 /* Store the expression if valid; else report error. */
7087 if (!in_system_header_at (input_location)
7088 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7089 warning (OPT_Wtraditional, "traditional C rejects automatic "
7090 "aggregate initialization");
7092 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7093 DECL_INITIAL (decl) = value;
7095 /* ANSI wants warnings about out-of-range constant initializers. */
7096 STRIP_TYPE_NOPS (value);
7097 if (TREE_STATIC (decl))
7098 constant_expression_warning (value);
7100 /* Check if we need to set array size from compound literal size. */
7101 if (TREE_CODE (type) == ARRAY_TYPE
7102 && TYPE_DOMAIN (type) == NULL_TREE
7103 && value != error_mark_node)
7105 tree inside_init = init;
7107 STRIP_TYPE_NOPS (inside_init);
7108 inside_init = fold (inside_init);
7110 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7112 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7114 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7116 /* For int foo[] = (int [3]){1}; we need to set array size
7117 now since later on array initializer will be just the
7118 brace enclosed list of the compound literal. */
7119 tree etype = strip_array_types (TREE_TYPE (decl));
7120 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7121 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7122 layout_type (type);
7123 layout_decl (cldecl, 0);
7124 TREE_TYPE (decl)
7125 = c_build_qualified_type (type, TYPE_QUALS (etype));
7131 /* Methods for storing and printing names for error messages. */
7133 /* Implement a spelling stack that allows components of a name to be pushed
7134 and popped. Each element on the stack is this structure. */
7136 struct spelling
7138 int kind;
7139 union
7141 unsigned HOST_WIDE_INT i;
7142 const char *s;
7143 } u;
7146 #define SPELLING_STRING 1
7147 #define SPELLING_MEMBER 2
7148 #define SPELLING_BOUNDS 3
7150 static struct spelling *spelling; /* Next stack element (unused). */
7151 static struct spelling *spelling_base; /* Spelling stack base. */
7152 static int spelling_size; /* Size of the spelling stack. */
7154 /* Macros to save and restore the spelling stack around push_... functions.
7155 Alternative to SAVE_SPELLING_STACK. */
7157 #define SPELLING_DEPTH() (spelling - spelling_base)
7158 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7160 /* Push an element on the spelling stack with type KIND and assign VALUE
7161 to MEMBER. */
7163 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7165 int depth = SPELLING_DEPTH (); \
7167 if (depth >= spelling_size) \
7169 spelling_size += 10; \
7170 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7171 spelling_size); \
7172 RESTORE_SPELLING_DEPTH (depth); \
7175 spelling->kind = (KIND); \
7176 spelling->MEMBER = (VALUE); \
7177 spelling++; \
7180 /* Push STRING on the stack. Printed literally. */
7182 static void
7183 push_string (const char *string)
7185 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7188 /* Push a member name on the stack. Printed as '.' STRING. */
7190 static void
7191 push_member_name (tree decl)
7193 const char *const string
7194 = (DECL_NAME (decl)
7195 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7196 : _("<anonymous>"));
7197 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7200 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7202 static void
7203 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7205 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7208 /* Compute the maximum size in bytes of the printed spelling. */
7210 static int
7211 spelling_length (void)
7213 int size = 0;
7214 struct spelling *p;
7216 for (p = spelling_base; p < spelling; p++)
7218 if (p->kind == SPELLING_BOUNDS)
7219 size += 25;
7220 else
7221 size += strlen (p->u.s) + 1;
7224 return size;
7227 /* Print the spelling to BUFFER and return it. */
7229 static char *
7230 print_spelling (char *buffer)
7232 char *d = buffer;
7233 struct spelling *p;
7235 for (p = spelling_base; p < spelling; p++)
7236 if (p->kind == SPELLING_BOUNDS)
7238 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7239 d += strlen (d);
7241 else
7243 const char *s;
7244 if (p->kind == SPELLING_MEMBER)
7245 *d++ = '.';
7246 for (s = p->u.s; (*d = *s++); d++)
7249 *d++ = '\0';
7250 return buffer;
7253 /* Digest the parser output INIT as an initializer for type TYPE.
7254 Return a C expression of type TYPE to represent the initial value.
7256 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7258 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7260 If INIT is a string constant, STRICT_STRING is true if it is
7261 unparenthesized or we should not warn here for it being parenthesized.
7262 For other types of INIT, STRICT_STRING is not used.
7264 INIT_LOC is the location of the INIT.
7266 REQUIRE_CONSTANT requests an error if non-constant initializers or
7267 elements are seen. */
7269 static tree
7270 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7271 bool null_pointer_constant, bool strict_string,
7272 int require_constant)
7274 enum tree_code code = TREE_CODE (type);
7275 tree inside_init = init;
7276 tree semantic_type = NULL_TREE;
7277 bool maybe_const = true;
7279 if (type == error_mark_node
7280 || !init
7281 || error_operand_p (init))
7282 return error_mark_node;
7284 STRIP_TYPE_NOPS (inside_init);
7286 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7288 semantic_type = TREE_TYPE (inside_init);
7289 inside_init = TREE_OPERAND (inside_init, 0);
7291 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7293 /* Initialization of an array of chars from a string constant
7294 optionally enclosed in braces. */
7296 if (code == ARRAY_TYPE && inside_init
7297 && TREE_CODE (inside_init) == STRING_CST)
7299 tree typ1
7300 = (TYPE_ATOMIC (TREE_TYPE (type))
7301 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7302 TYPE_QUAL_ATOMIC)
7303 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7304 /* Note that an array could be both an array of character type
7305 and an array of wchar_t if wchar_t is signed char or unsigned
7306 char. */
7307 bool char_array = (typ1 == char_type_node
7308 || typ1 == signed_char_type_node
7309 || typ1 == unsigned_char_type_node);
7310 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7311 bool char16_array = !!comptypes (typ1, char16_type_node);
7312 bool char32_array = !!comptypes (typ1, char32_type_node);
7314 if (char_array || wchar_array || char16_array || char32_array)
7316 struct c_expr expr;
7317 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7318 expr.value = inside_init;
7319 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7320 expr.original_type = NULL;
7321 maybe_warn_string_init (init_loc, type, expr);
7323 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7324 pedwarn_init (init_loc, OPT_Wpedantic,
7325 "initialization of a flexible array member");
7327 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7328 TYPE_MAIN_VARIANT (type)))
7329 return inside_init;
7331 if (char_array)
7333 if (typ2 != char_type_node)
7335 error_init (init_loc, "char-array initialized from wide "
7336 "string");
7337 return error_mark_node;
7340 else
7342 if (typ2 == char_type_node)
7344 error_init (init_loc, "wide character array initialized "
7345 "from non-wide string");
7346 return error_mark_node;
7348 else if (!comptypes(typ1, typ2))
7350 error_init (init_loc, "wide character array initialized "
7351 "from incompatible wide string");
7352 return error_mark_node;
7356 TREE_TYPE (inside_init) = type;
7357 if (TYPE_DOMAIN (type) != NULL_TREE
7358 && TYPE_SIZE (type) != NULL_TREE
7359 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7361 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7363 /* Subtract the size of a single (possibly wide) character
7364 because it's ok to ignore the terminating null char
7365 that is counted in the length of the constant. */
7366 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7367 (len
7368 - (TYPE_PRECISION (typ1)
7369 / BITS_PER_UNIT))))
7370 pedwarn_init (init_loc, 0,
7371 ("initializer-string for array of chars "
7372 "is too long"));
7373 else if (warn_cxx_compat
7374 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7375 warning_at (init_loc, OPT_Wc___compat,
7376 ("initializer-string for array chars "
7377 "is too long for C++"));
7380 return inside_init;
7382 else if (INTEGRAL_TYPE_P (typ1))
7384 error_init (init_loc, "array of inappropriate type initialized "
7385 "from string constant");
7386 return error_mark_node;
7390 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7391 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7392 below and handle as a constructor. */
7393 if (code == VECTOR_TYPE
7394 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7395 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7396 && TREE_CONSTANT (inside_init))
7398 if (TREE_CODE (inside_init) == VECTOR_CST
7399 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7400 TYPE_MAIN_VARIANT (type)))
7401 return inside_init;
7403 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7405 unsigned HOST_WIDE_INT ix;
7406 tree value;
7407 bool constant_p = true;
7409 /* Iterate through elements and check if all constructor
7410 elements are *_CSTs. */
7411 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7412 if (!CONSTANT_CLASS_P (value))
7414 constant_p = false;
7415 break;
7418 if (constant_p)
7419 return build_vector_from_ctor (type,
7420 CONSTRUCTOR_ELTS (inside_init));
7424 if (warn_sequence_point)
7425 verify_sequence_points (inside_init);
7427 /* Any type can be initialized
7428 from an expression of the same type, optionally with braces. */
7430 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7431 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7432 TYPE_MAIN_VARIANT (type))
7433 || (code == ARRAY_TYPE
7434 && comptypes (TREE_TYPE (inside_init), type))
7435 || (code == VECTOR_TYPE
7436 && comptypes (TREE_TYPE (inside_init), type))
7437 || (code == POINTER_TYPE
7438 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7439 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7440 TREE_TYPE (type)))))
7442 if (code == POINTER_TYPE)
7444 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7446 if (TREE_CODE (inside_init) == STRING_CST
7447 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7448 inside_init = array_to_pointer_conversion
7449 (init_loc, inside_init);
7450 else
7452 error_init (init_loc, "invalid use of non-lvalue array");
7453 return error_mark_node;
7458 if (code == VECTOR_TYPE)
7459 /* Although the types are compatible, we may require a
7460 conversion. */
7461 inside_init = convert (type, inside_init);
7463 if (require_constant
7464 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7466 /* As an extension, allow initializing objects with static storage
7467 duration with compound literals (which are then treated just as
7468 the brace enclosed list they contain). Also allow this for
7469 vectors, as we can only assign them with compound literals. */
7470 if (flag_isoc99 && code != VECTOR_TYPE)
7471 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7472 "is not constant");
7473 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7474 inside_init = DECL_INITIAL (decl);
7477 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7478 && TREE_CODE (inside_init) != CONSTRUCTOR)
7480 error_init (init_loc, "array initialized from non-constant array "
7481 "expression");
7482 return error_mark_node;
7485 /* Compound expressions can only occur here if -Wpedantic or
7486 -pedantic-errors is specified. In the later case, we always want
7487 an error. In the former case, we simply want a warning. */
7488 if (require_constant && pedantic
7489 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7491 inside_init
7492 = valid_compound_expr_initializer (inside_init,
7493 TREE_TYPE (inside_init));
7494 if (inside_init == error_mark_node)
7495 error_init (init_loc, "initializer element is not constant");
7496 else
7497 pedwarn_init (init_loc, OPT_Wpedantic,
7498 "initializer element is not constant");
7499 if (flag_pedantic_errors)
7500 inside_init = error_mark_node;
7502 else if (require_constant
7503 && !initializer_constant_valid_p (inside_init,
7504 TREE_TYPE (inside_init)))
7506 error_init (init_loc, "initializer element is not constant");
7507 inside_init = error_mark_node;
7509 else if (require_constant && !maybe_const)
7510 pedwarn_init (init_loc, OPT_Wpedantic,
7511 "initializer element is not a constant expression");
7513 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7514 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7515 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7516 type, inside_init, origtype,
7517 ic_init, null_pointer_constant,
7518 NULL_TREE, NULL_TREE, 0);
7519 return inside_init;
7522 /* Handle scalar types, including conversions. */
7524 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7525 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7526 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7528 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7529 && (TREE_CODE (init) == STRING_CST
7530 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7531 inside_init = init = array_to_pointer_conversion (init_loc, init);
7532 if (semantic_type)
7533 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7534 inside_init);
7535 inside_init
7536 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7537 inside_init, origtype, ic_init,
7538 null_pointer_constant, NULL_TREE, NULL_TREE,
7541 /* Check to see if we have already given an error message. */
7542 if (inside_init == error_mark_node)
7544 else if (require_constant && !TREE_CONSTANT (inside_init))
7546 error_init (init_loc, "initializer element is not constant");
7547 inside_init = error_mark_node;
7549 else if (require_constant
7550 && !initializer_constant_valid_p (inside_init,
7551 TREE_TYPE (inside_init)))
7553 error_init (init_loc, "initializer element is not computable at "
7554 "load time");
7555 inside_init = error_mark_node;
7557 else if (require_constant && !maybe_const)
7558 pedwarn_init (init_loc, OPT_Wpedantic,
7559 "initializer element is not a constant expression");
7561 return inside_init;
7564 /* Come here only for records and arrays. */
7566 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7568 error_init (init_loc, "variable-sized object may not be initialized");
7569 return error_mark_node;
7572 error_init (init_loc, "invalid initializer");
7573 return error_mark_node;
7576 /* Handle initializers that use braces. */
7578 /* Type of object we are accumulating a constructor for.
7579 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7580 static tree constructor_type;
7582 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7583 left to fill. */
7584 static tree constructor_fields;
7586 /* For an ARRAY_TYPE, this is the specified index
7587 at which to store the next element we get. */
7588 static tree constructor_index;
7590 /* For an ARRAY_TYPE, this is the maximum index. */
7591 static tree constructor_max_index;
7593 /* For a RECORD_TYPE, this is the first field not yet written out. */
7594 static tree constructor_unfilled_fields;
7596 /* For an ARRAY_TYPE, this is the index of the first element
7597 not yet written out. */
7598 static tree constructor_unfilled_index;
7600 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7601 This is so we can generate gaps between fields, when appropriate. */
7602 static tree constructor_bit_index;
7604 /* If we are saving up the elements rather than allocating them,
7605 this is the list of elements so far (in reverse order,
7606 most recent first). */
7607 static vec<constructor_elt, va_gc> *constructor_elements;
7609 /* 1 if constructor should be incrementally stored into a constructor chain,
7610 0 if all the elements should be kept in AVL tree. */
7611 static int constructor_incremental;
7613 /* 1 if so far this constructor's elements are all compile-time constants. */
7614 static int constructor_constant;
7616 /* 1 if so far this constructor's elements are all valid address constants. */
7617 static int constructor_simple;
7619 /* 1 if this constructor has an element that cannot be part of a
7620 constant expression. */
7621 static int constructor_nonconst;
7623 /* 1 if this constructor is erroneous so far. */
7624 static int constructor_erroneous;
7626 /* 1 if this constructor is the universal zero initializer { 0 }. */
7627 static int constructor_zeroinit;
7629 /* Structure for managing pending initializer elements, organized as an
7630 AVL tree. */
7632 struct init_node
7634 struct init_node *left, *right;
7635 struct init_node *parent;
7636 int balance;
7637 tree purpose;
7638 tree value;
7639 tree origtype;
7642 /* Tree of pending elements at this constructor level.
7643 These are elements encountered out of order
7644 which belong at places we haven't reached yet in actually
7645 writing the output.
7646 Will never hold tree nodes across GC runs. */
7647 static struct init_node *constructor_pending_elts;
7649 /* The SPELLING_DEPTH of this constructor. */
7650 static int constructor_depth;
7652 /* DECL node for which an initializer is being read.
7653 0 means we are reading a constructor expression
7654 such as (struct foo) {...}. */
7655 static tree constructor_decl;
7657 /* Nonzero if this is an initializer for a top-level decl. */
7658 static int constructor_top_level;
7660 /* Nonzero if there were any member designators in this initializer. */
7661 static int constructor_designated;
7663 /* Nesting depth of designator list. */
7664 static int designator_depth;
7666 /* Nonzero if there were diagnosed errors in this designator list. */
7667 static int designator_erroneous;
7670 /* This stack has a level for each implicit or explicit level of
7671 structuring in the initializer, including the outermost one. It
7672 saves the values of most of the variables above. */
7674 struct constructor_range_stack;
7676 struct constructor_stack
7678 struct constructor_stack *next;
7679 tree type;
7680 tree fields;
7681 tree index;
7682 tree max_index;
7683 tree unfilled_index;
7684 tree unfilled_fields;
7685 tree bit_index;
7686 vec<constructor_elt, va_gc> *elements;
7687 struct init_node *pending_elts;
7688 int offset;
7689 int depth;
7690 /* If value nonzero, this value should replace the entire
7691 constructor at this level. */
7692 struct c_expr replacement_value;
7693 struct constructor_range_stack *range_stack;
7694 char constant;
7695 char simple;
7696 char nonconst;
7697 char implicit;
7698 char erroneous;
7699 char outer;
7700 char incremental;
7701 char designated;
7702 int designator_depth;
7705 static struct constructor_stack *constructor_stack;
7707 /* This stack represents designators from some range designator up to
7708 the last designator in the list. */
7710 struct constructor_range_stack
7712 struct constructor_range_stack *next, *prev;
7713 struct constructor_stack *stack;
7714 tree range_start;
7715 tree index;
7716 tree range_end;
7717 tree fields;
7720 static struct constructor_range_stack *constructor_range_stack;
7722 /* This stack records separate initializers that are nested.
7723 Nested initializers can't happen in ANSI C, but GNU C allows them
7724 in cases like { ... (struct foo) { ... } ... }. */
7726 struct initializer_stack
7728 struct initializer_stack *next;
7729 tree decl;
7730 struct constructor_stack *constructor_stack;
7731 struct constructor_range_stack *constructor_range_stack;
7732 vec<constructor_elt, va_gc> *elements;
7733 struct spelling *spelling;
7734 struct spelling *spelling_base;
7735 int spelling_size;
7736 char top_level;
7737 char require_constant_value;
7738 char require_constant_elements;
7739 rich_location *missing_brace_richloc;
7742 static struct initializer_stack *initializer_stack;
7744 /* Prepare to parse and output the initializer for variable DECL. */
7746 void
7747 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7748 rich_location *richloc)
7750 const char *locus;
7751 struct initializer_stack *p = XNEW (struct initializer_stack);
7753 p->decl = constructor_decl;
7754 p->require_constant_value = require_constant_value;
7755 p->require_constant_elements = require_constant_elements;
7756 p->constructor_stack = constructor_stack;
7757 p->constructor_range_stack = constructor_range_stack;
7758 p->elements = constructor_elements;
7759 p->spelling = spelling;
7760 p->spelling_base = spelling_base;
7761 p->spelling_size = spelling_size;
7762 p->top_level = constructor_top_level;
7763 p->next = initializer_stack;
7764 p->missing_brace_richloc = richloc;
7765 initializer_stack = p;
7767 constructor_decl = decl;
7768 constructor_designated = 0;
7769 constructor_top_level = top_level;
7771 if (decl != NULL_TREE && decl != error_mark_node)
7773 require_constant_value = TREE_STATIC (decl);
7774 require_constant_elements
7775 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7776 /* For a scalar, you can always use any value to initialize,
7777 even within braces. */
7778 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7779 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7781 else
7783 require_constant_value = 0;
7784 require_constant_elements = 0;
7785 locus = _("(anonymous)");
7788 constructor_stack = 0;
7789 constructor_range_stack = 0;
7791 found_missing_braces = 0;
7793 spelling_base = 0;
7794 spelling_size = 0;
7795 RESTORE_SPELLING_DEPTH (0);
7797 if (locus)
7798 push_string (locus);
7801 void
7802 finish_init (void)
7804 struct initializer_stack *p = initializer_stack;
7806 /* Free the whole constructor stack of this initializer. */
7807 while (constructor_stack)
7809 struct constructor_stack *q = constructor_stack;
7810 constructor_stack = q->next;
7811 free (q);
7814 gcc_assert (!constructor_range_stack);
7816 /* Pop back to the data of the outer initializer (if any). */
7817 free (spelling_base);
7819 constructor_decl = p->decl;
7820 require_constant_value = p->require_constant_value;
7821 require_constant_elements = p->require_constant_elements;
7822 constructor_stack = p->constructor_stack;
7823 constructor_range_stack = p->constructor_range_stack;
7824 constructor_elements = p->elements;
7825 spelling = p->spelling;
7826 spelling_base = p->spelling_base;
7827 spelling_size = p->spelling_size;
7828 constructor_top_level = p->top_level;
7829 initializer_stack = p->next;
7830 free (p);
7833 /* Call here when we see the initializer is surrounded by braces.
7834 This is instead of a call to push_init_level;
7835 it is matched by a call to pop_init_level.
7837 TYPE is the type to initialize, for a constructor expression.
7838 For an initializer for a decl, TYPE is zero. */
7840 void
7841 really_start_incremental_init (tree type)
7843 struct constructor_stack *p = XNEW (struct constructor_stack);
7845 if (type == NULL_TREE)
7846 type = TREE_TYPE (constructor_decl);
7848 if (VECTOR_TYPE_P (type)
7849 && TYPE_VECTOR_OPAQUE (type))
7850 error ("opaque vector types cannot be initialized");
7852 p->type = constructor_type;
7853 p->fields = constructor_fields;
7854 p->index = constructor_index;
7855 p->max_index = constructor_max_index;
7856 p->unfilled_index = constructor_unfilled_index;
7857 p->unfilled_fields = constructor_unfilled_fields;
7858 p->bit_index = constructor_bit_index;
7859 p->elements = constructor_elements;
7860 p->constant = constructor_constant;
7861 p->simple = constructor_simple;
7862 p->nonconst = constructor_nonconst;
7863 p->erroneous = constructor_erroneous;
7864 p->pending_elts = constructor_pending_elts;
7865 p->depth = constructor_depth;
7866 p->replacement_value.value = 0;
7867 p->replacement_value.original_code = ERROR_MARK;
7868 p->replacement_value.original_type = NULL;
7869 p->implicit = 0;
7870 p->range_stack = 0;
7871 p->outer = 0;
7872 p->incremental = constructor_incremental;
7873 p->designated = constructor_designated;
7874 p->designator_depth = designator_depth;
7875 p->next = 0;
7876 constructor_stack = p;
7878 constructor_constant = 1;
7879 constructor_simple = 1;
7880 constructor_nonconst = 0;
7881 constructor_depth = SPELLING_DEPTH ();
7882 constructor_elements = NULL;
7883 constructor_pending_elts = 0;
7884 constructor_type = type;
7885 constructor_incremental = 1;
7886 constructor_designated = 0;
7887 constructor_zeroinit = 1;
7888 designator_depth = 0;
7889 designator_erroneous = 0;
7891 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7893 constructor_fields = TYPE_FIELDS (constructor_type);
7894 /* Skip any nameless bit fields at the beginning. */
7895 while (constructor_fields != NULL_TREE
7896 && DECL_C_BIT_FIELD (constructor_fields)
7897 && DECL_NAME (constructor_fields) == NULL_TREE)
7898 constructor_fields = DECL_CHAIN (constructor_fields);
7900 constructor_unfilled_fields = constructor_fields;
7901 constructor_bit_index = bitsize_zero_node;
7903 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7905 if (TYPE_DOMAIN (constructor_type))
7907 constructor_max_index
7908 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7910 /* Detect non-empty initializations of zero-length arrays. */
7911 if (constructor_max_index == NULL_TREE
7912 && TYPE_SIZE (constructor_type))
7913 constructor_max_index = integer_minus_one_node;
7915 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7916 to initialize VLAs will cause a proper error; avoid tree
7917 checking errors as well by setting a safe value. */
7918 if (constructor_max_index
7919 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7920 constructor_max_index = integer_minus_one_node;
7922 constructor_index
7923 = convert (bitsizetype,
7924 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7926 else
7928 constructor_index = bitsize_zero_node;
7929 constructor_max_index = NULL_TREE;
7932 constructor_unfilled_index = constructor_index;
7934 else if (VECTOR_TYPE_P (constructor_type))
7936 /* Vectors are like simple fixed-size arrays. */
7937 constructor_max_index =
7938 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7939 constructor_index = bitsize_zero_node;
7940 constructor_unfilled_index = constructor_index;
7942 else
7944 /* Handle the case of int x = {5}; */
7945 constructor_fields = constructor_type;
7946 constructor_unfilled_fields = constructor_type;
7950 extern location_t last_init_list_comma;
7952 /* Called when we see an open brace for a nested initializer. Finish
7953 off any pending levels with implicit braces. */
7954 void
7955 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7957 while (constructor_stack->implicit)
7959 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7960 && constructor_fields == NULL_TREE)
7961 process_init_element (input_location,
7962 pop_init_level (loc, 1, braced_init_obstack,
7963 last_init_list_comma),
7964 true, braced_init_obstack);
7965 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7966 && constructor_max_index
7967 && tree_int_cst_lt (constructor_max_index,
7968 constructor_index))
7969 process_init_element (input_location,
7970 pop_init_level (loc, 1, braced_init_obstack,
7971 last_init_list_comma),
7972 true, braced_init_obstack);
7973 else
7974 break;
7978 /* Push down into a subobject, for initialization.
7979 If this is for an explicit set of braces, IMPLICIT is 0.
7980 If it is because the next element belongs at a lower level,
7981 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7983 void
7984 push_init_level (location_t loc, int implicit,
7985 struct obstack *braced_init_obstack)
7987 struct constructor_stack *p;
7988 tree value = NULL_TREE;
7990 /* Unless this is an explicit brace, we need to preserve previous
7991 content if any. */
7992 if (implicit)
7994 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7995 value = find_init_member (constructor_fields, braced_init_obstack);
7996 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7997 value = find_init_member (constructor_index, braced_init_obstack);
8000 p = XNEW (struct constructor_stack);
8001 p->type = constructor_type;
8002 p->fields = constructor_fields;
8003 p->index = constructor_index;
8004 p->max_index = constructor_max_index;
8005 p->unfilled_index = constructor_unfilled_index;
8006 p->unfilled_fields = constructor_unfilled_fields;
8007 p->bit_index = constructor_bit_index;
8008 p->elements = constructor_elements;
8009 p->constant = constructor_constant;
8010 p->simple = constructor_simple;
8011 p->nonconst = constructor_nonconst;
8012 p->erroneous = constructor_erroneous;
8013 p->pending_elts = constructor_pending_elts;
8014 p->depth = constructor_depth;
8015 p->replacement_value.value = NULL_TREE;
8016 p->replacement_value.original_code = ERROR_MARK;
8017 p->replacement_value.original_type = NULL;
8018 p->implicit = implicit;
8019 p->outer = 0;
8020 p->incremental = constructor_incremental;
8021 p->designated = constructor_designated;
8022 p->designator_depth = designator_depth;
8023 p->next = constructor_stack;
8024 p->range_stack = 0;
8025 constructor_stack = p;
8027 constructor_constant = 1;
8028 constructor_simple = 1;
8029 constructor_nonconst = 0;
8030 constructor_depth = SPELLING_DEPTH ();
8031 constructor_elements = NULL;
8032 constructor_incremental = 1;
8033 constructor_designated = 0;
8034 constructor_pending_elts = 0;
8035 if (!implicit)
8037 p->range_stack = constructor_range_stack;
8038 constructor_range_stack = 0;
8039 designator_depth = 0;
8040 designator_erroneous = 0;
8043 /* Don't die if an entire brace-pair level is superfluous
8044 in the containing level. */
8045 if (constructor_type == NULL_TREE)
8047 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8049 /* Don't die if there are extra init elts at the end. */
8050 if (constructor_fields == NULL_TREE)
8051 constructor_type = NULL_TREE;
8052 else
8054 constructor_type = TREE_TYPE (constructor_fields);
8055 push_member_name (constructor_fields);
8056 constructor_depth++;
8058 /* If upper initializer is designated, then mark this as
8059 designated too to prevent bogus warnings. */
8060 constructor_designated = p->designated;
8062 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8064 constructor_type = TREE_TYPE (constructor_type);
8065 push_array_bounds (tree_to_uhwi (constructor_index));
8066 constructor_depth++;
8069 if (constructor_type == NULL_TREE)
8071 error_init (loc, "extra brace group at end of initializer");
8072 constructor_fields = NULL_TREE;
8073 constructor_unfilled_fields = NULL_TREE;
8074 return;
8077 if (value && TREE_CODE (value) == CONSTRUCTOR)
8079 constructor_constant = TREE_CONSTANT (value);
8080 constructor_simple = TREE_STATIC (value);
8081 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8082 constructor_elements = CONSTRUCTOR_ELTS (value);
8083 if (!vec_safe_is_empty (constructor_elements)
8084 && (TREE_CODE (constructor_type) == RECORD_TYPE
8085 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8086 set_nonincremental_init (braced_init_obstack);
8089 if (implicit == 1)
8091 found_missing_braces = 1;
8092 if (initializer_stack->missing_brace_richloc)
8093 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8094 (loc, "{");
8097 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8099 constructor_fields = TYPE_FIELDS (constructor_type);
8100 /* Skip any nameless bit fields at the beginning. */
8101 while (constructor_fields != NULL_TREE
8102 && DECL_C_BIT_FIELD (constructor_fields)
8103 && DECL_NAME (constructor_fields) == NULL_TREE)
8104 constructor_fields = DECL_CHAIN (constructor_fields);
8106 constructor_unfilled_fields = constructor_fields;
8107 constructor_bit_index = bitsize_zero_node;
8109 else if (VECTOR_TYPE_P (constructor_type))
8111 /* Vectors are like simple fixed-size arrays. */
8112 constructor_max_index =
8113 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8114 constructor_index = bitsize_int (0);
8115 constructor_unfilled_index = constructor_index;
8117 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8119 if (TYPE_DOMAIN (constructor_type))
8121 constructor_max_index
8122 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8124 /* Detect non-empty initializations of zero-length arrays. */
8125 if (constructor_max_index == NULL_TREE
8126 && TYPE_SIZE (constructor_type))
8127 constructor_max_index = integer_minus_one_node;
8129 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8130 to initialize VLAs will cause a proper error; avoid tree
8131 checking errors as well by setting a safe value. */
8132 if (constructor_max_index
8133 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8134 constructor_max_index = integer_minus_one_node;
8136 constructor_index
8137 = convert (bitsizetype,
8138 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8140 else
8141 constructor_index = bitsize_zero_node;
8143 constructor_unfilled_index = constructor_index;
8144 if (value && TREE_CODE (value) == STRING_CST)
8146 /* We need to split the char/wchar array into individual
8147 characters, so that we don't have to special case it
8148 everywhere. */
8149 set_nonincremental_init_from_string (value, braced_init_obstack);
8152 else
8154 if (constructor_type != error_mark_node)
8155 warning_init (input_location, 0, "braces around scalar initializer");
8156 constructor_fields = constructor_type;
8157 constructor_unfilled_fields = constructor_type;
8161 /* At the end of an implicit or explicit brace level,
8162 finish up that level of constructor. If a single expression
8163 with redundant braces initialized that level, return the
8164 c_expr structure for that expression. Otherwise, the original_code
8165 element is set to ERROR_MARK.
8166 If we were outputting the elements as they are read, return 0 as the value
8167 from inner levels (process_init_element ignores that),
8168 but return error_mark_node as the value from the outermost level
8169 (that's what we want to put in DECL_INITIAL).
8170 Otherwise, return a CONSTRUCTOR expression as the value. */
8172 struct c_expr
8173 pop_init_level (location_t loc, int implicit,
8174 struct obstack *braced_init_obstack,
8175 location_t insert_before)
8177 struct constructor_stack *p;
8178 struct c_expr ret;
8179 ret.value = NULL_TREE;
8180 ret.original_code = ERROR_MARK;
8181 ret.original_type = NULL;
8183 if (implicit == 0)
8185 /* When we come to an explicit close brace,
8186 pop any inner levels that didn't have explicit braces. */
8187 while (constructor_stack->implicit)
8188 process_init_element (input_location,
8189 pop_init_level (loc, 1, braced_init_obstack,
8190 insert_before),
8191 true, braced_init_obstack);
8192 gcc_assert (!constructor_range_stack);
8194 else
8195 if (initializer_stack->missing_brace_richloc)
8196 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8197 (insert_before, "}");
8199 /* Now output all pending elements. */
8200 constructor_incremental = 1;
8201 output_pending_init_elements (1, braced_init_obstack);
8203 p = constructor_stack;
8205 /* Error for initializing a flexible array member, or a zero-length
8206 array member in an inappropriate context. */
8207 if (constructor_type && constructor_fields
8208 && TREE_CODE (constructor_type) == ARRAY_TYPE
8209 && TYPE_DOMAIN (constructor_type)
8210 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8212 /* Silently discard empty initializations. The parser will
8213 already have pedwarned for empty brackets. */
8214 if (integer_zerop (constructor_unfilled_index))
8215 constructor_type = NULL_TREE;
8216 else
8218 gcc_assert (!TYPE_SIZE (constructor_type));
8220 if (constructor_depth > 2)
8221 error_init (loc, "initialization of flexible array member in a nested context");
8222 else
8223 pedwarn_init (loc, OPT_Wpedantic,
8224 "initialization of a flexible array member");
8226 /* We have already issued an error message for the existence
8227 of a flexible array member not at the end of the structure.
8228 Discard the initializer so that we do not die later. */
8229 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8230 constructor_type = NULL_TREE;
8234 switch (vec_safe_length (constructor_elements))
8236 case 0:
8237 /* Initialization with { } counts as zeroinit. */
8238 constructor_zeroinit = 1;
8239 break;
8240 case 1:
8241 /* This might be zeroinit as well. */
8242 if (integer_zerop ((*constructor_elements)[0].value))
8243 constructor_zeroinit = 1;
8244 break;
8245 default:
8246 /* If the constructor has more than one element, it can't be { 0 }. */
8247 constructor_zeroinit = 0;
8248 break;
8251 /* Warn when some structs are initialized with direct aggregation. */
8252 if (!implicit && found_missing_braces && warn_missing_braces
8253 && !constructor_zeroinit)
8255 gcc_assert (initializer_stack->missing_brace_richloc);
8256 warning_at (initializer_stack->missing_brace_richloc,
8257 OPT_Wmissing_braces,
8258 "missing braces around initializer");
8261 /* Warn when some struct elements are implicitly initialized to zero. */
8262 if (warn_missing_field_initializers
8263 && constructor_type
8264 && TREE_CODE (constructor_type) == RECORD_TYPE
8265 && constructor_unfilled_fields)
8267 /* Do not warn for flexible array members or zero-length arrays. */
8268 while (constructor_unfilled_fields
8269 && (!DECL_SIZE (constructor_unfilled_fields)
8270 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8271 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8273 if (constructor_unfilled_fields
8274 /* Do not warn if this level of the initializer uses member
8275 designators; it is likely to be deliberate. */
8276 && !constructor_designated
8277 /* Do not warn about initializing with { 0 } or with { }. */
8278 && !constructor_zeroinit)
8280 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8281 "missing initializer for field %qD of %qT",
8282 constructor_unfilled_fields,
8283 constructor_type))
8284 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8285 "%qD declared here", constructor_unfilled_fields);
8289 /* Pad out the end of the structure. */
8290 if (p->replacement_value.value)
8291 /* If this closes a superfluous brace pair,
8292 just pass out the element between them. */
8293 ret = p->replacement_value;
8294 else if (constructor_type == NULL_TREE)
8296 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8297 && TREE_CODE (constructor_type) != ARRAY_TYPE
8298 && !VECTOR_TYPE_P (constructor_type))
8300 /* A nonincremental scalar initializer--just return
8301 the element, after verifying there is just one. */
8302 if (vec_safe_is_empty (constructor_elements))
8304 if (!constructor_erroneous)
8305 error_init (loc, "empty scalar initializer");
8306 ret.value = error_mark_node;
8308 else if (vec_safe_length (constructor_elements) != 1)
8310 error_init (loc, "extra elements in scalar initializer");
8311 ret.value = (*constructor_elements)[0].value;
8313 else
8314 ret.value = (*constructor_elements)[0].value;
8316 else
8318 if (constructor_erroneous)
8319 ret.value = error_mark_node;
8320 else
8322 ret.value = build_constructor (constructor_type,
8323 constructor_elements);
8324 if (constructor_constant)
8325 TREE_CONSTANT (ret.value) = 1;
8326 if (constructor_constant && constructor_simple)
8327 TREE_STATIC (ret.value) = 1;
8328 if (constructor_nonconst)
8329 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8333 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8335 if (constructor_nonconst)
8336 ret.original_code = C_MAYBE_CONST_EXPR;
8337 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8338 ret.original_code = ERROR_MARK;
8341 constructor_type = p->type;
8342 constructor_fields = p->fields;
8343 constructor_index = p->index;
8344 constructor_max_index = p->max_index;
8345 constructor_unfilled_index = p->unfilled_index;
8346 constructor_unfilled_fields = p->unfilled_fields;
8347 constructor_bit_index = p->bit_index;
8348 constructor_elements = p->elements;
8349 constructor_constant = p->constant;
8350 constructor_simple = p->simple;
8351 constructor_nonconst = p->nonconst;
8352 constructor_erroneous = p->erroneous;
8353 constructor_incremental = p->incremental;
8354 constructor_designated = p->designated;
8355 designator_depth = p->designator_depth;
8356 constructor_pending_elts = p->pending_elts;
8357 constructor_depth = p->depth;
8358 if (!p->implicit)
8359 constructor_range_stack = p->range_stack;
8360 RESTORE_SPELLING_DEPTH (constructor_depth);
8362 constructor_stack = p->next;
8363 free (p);
8365 if (ret.value == NULL_TREE && constructor_stack == 0)
8366 ret.value = error_mark_node;
8367 return ret;
8370 /* Common handling for both array range and field name designators.
8371 ARRAY argument is nonzero for array ranges. Returns false for success. */
8373 static bool
8374 set_designator (location_t loc, bool array,
8375 struct obstack *braced_init_obstack)
8377 tree subtype;
8378 enum tree_code subcode;
8380 /* Don't die if an entire brace-pair level is superfluous
8381 in the containing level. */
8382 if (constructor_type == NULL_TREE)
8383 return true;
8385 /* If there were errors in this designator list already, bail out
8386 silently. */
8387 if (designator_erroneous)
8388 return true;
8390 if (!designator_depth)
8392 gcc_assert (!constructor_range_stack);
8394 /* Designator list starts at the level of closest explicit
8395 braces. */
8396 while (constructor_stack->implicit)
8397 process_init_element (input_location,
8398 pop_init_level (loc, 1, braced_init_obstack,
8399 last_init_list_comma),
8400 true, braced_init_obstack);
8401 constructor_designated = 1;
8402 return false;
8405 switch (TREE_CODE (constructor_type))
8407 case RECORD_TYPE:
8408 case UNION_TYPE:
8409 subtype = TREE_TYPE (constructor_fields);
8410 if (subtype != error_mark_node)
8411 subtype = TYPE_MAIN_VARIANT (subtype);
8412 break;
8413 case ARRAY_TYPE:
8414 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8415 break;
8416 default:
8417 gcc_unreachable ();
8420 subcode = TREE_CODE (subtype);
8421 if (array && subcode != ARRAY_TYPE)
8423 error_init (loc, "array index in non-array initializer");
8424 return true;
8426 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8428 error_init (loc, "field name not in record or union initializer");
8429 return true;
8432 constructor_designated = 1;
8433 finish_implicit_inits (loc, braced_init_obstack);
8434 push_init_level (loc, 2, braced_init_obstack);
8435 return false;
8438 /* If there are range designators in designator list, push a new designator
8439 to constructor_range_stack. RANGE_END is end of such stack range or
8440 NULL_TREE if there is no range designator at this level. */
8442 static void
8443 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8445 struct constructor_range_stack *p;
8447 p = (struct constructor_range_stack *)
8448 obstack_alloc (braced_init_obstack,
8449 sizeof (struct constructor_range_stack));
8450 p->prev = constructor_range_stack;
8451 p->next = 0;
8452 p->fields = constructor_fields;
8453 p->range_start = constructor_index;
8454 p->index = constructor_index;
8455 p->stack = constructor_stack;
8456 p->range_end = range_end;
8457 if (constructor_range_stack)
8458 constructor_range_stack->next = p;
8459 constructor_range_stack = p;
8462 /* Within an array initializer, specify the next index to be initialized.
8463 FIRST is that index. If LAST is nonzero, then initialize a range
8464 of indices, running from FIRST through LAST. */
8466 void
8467 set_init_index (location_t loc, tree first, tree last,
8468 struct obstack *braced_init_obstack)
8470 if (set_designator (loc, true, braced_init_obstack))
8471 return;
8473 designator_erroneous = 1;
8475 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8476 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8478 error_init (loc, "array index in initializer not of integer type");
8479 return;
8482 if (TREE_CODE (first) != INTEGER_CST)
8484 first = c_fully_fold (first, false, NULL);
8485 if (TREE_CODE (first) == INTEGER_CST)
8486 pedwarn_init (loc, OPT_Wpedantic,
8487 "array index in initializer is not "
8488 "an integer constant expression");
8491 if (last && TREE_CODE (last) != INTEGER_CST)
8493 last = c_fully_fold (last, false, NULL);
8494 if (TREE_CODE (last) == INTEGER_CST)
8495 pedwarn_init (loc, OPT_Wpedantic,
8496 "array index in initializer is not "
8497 "an integer constant expression");
8500 if (TREE_CODE (first) != INTEGER_CST)
8501 error_init (loc, "nonconstant array index in initializer");
8502 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8503 error_init (loc, "nonconstant array index in initializer");
8504 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8505 error_init (loc, "array index in non-array initializer");
8506 else if (tree_int_cst_sgn (first) == -1)
8507 error_init (loc, "array index in initializer exceeds array bounds");
8508 else if (constructor_max_index
8509 && tree_int_cst_lt (constructor_max_index, first))
8510 error_init (loc, "array index in initializer exceeds array bounds");
8511 else
8513 constant_expression_warning (first);
8514 if (last)
8515 constant_expression_warning (last);
8516 constructor_index = convert (bitsizetype, first);
8517 if (tree_int_cst_lt (constructor_index, first))
8519 constructor_index = copy_node (constructor_index);
8520 TREE_OVERFLOW (constructor_index) = 1;
8523 if (last)
8525 if (tree_int_cst_equal (first, last))
8526 last = NULL_TREE;
8527 else if (tree_int_cst_lt (last, first))
8529 error_init (loc, "empty index range in initializer");
8530 last = NULL_TREE;
8532 else
8534 last = convert (bitsizetype, last);
8535 if (constructor_max_index != NULL_TREE
8536 && tree_int_cst_lt (constructor_max_index, last))
8538 error_init (loc, "array index range in initializer exceeds "
8539 "array bounds");
8540 last = NULL_TREE;
8545 designator_depth++;
8546 designator_erroneous = 0;
8547 if (constructor_range_stack || last)
8548 push_range_stack (last, braced_init_obstack);
8552 /* Within a struct initializer, specify the next field to be initialized. */
8554 void
8555 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8556 struct obstack *braced_init_obstack)
8558 tree field;
8560 if (set_designator (loc, false, braced_init_obstack))
8561 return;
8563 designator_erroneous = 1;
8565 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8567 error_init (loc, "field name not in record or union initializer");
8568 return;
8571 field = lookup_field (constructor_type, fieldname);
8573 if (field == NULL_TREE)
8575 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8576 if (guessed_id)
8578 gcc_rich_location rich_loc (fieldname_loc);
8579 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8580 error_at (&rich_loc,
8581 "%qT has no member named %qE; did you mean %qE?",
8582 constructor_type, fieldname, guessed_id);
8584 else
8585 error_at (fieldname_loc, "%qT has no member named %qE",
8586 constructor_type, fieldname);
8588 else
8591 constructor_fields = TREE_VALUE (field);
8592 designator_depth++;
8593 designator_erroneous = 0;
8594 if (constructor_range_stack)
8595 push_range_stack (NULL_TREE, braced_init_obstack);
8596 field = TREE_CHAIN (field);
8597 if (field)
8599 if (set_designator (loc, false, braced_init_obstack))
8600 return;
8603 while (field != NULL_TREE);
8606 /* Add a new initializer to the tree of pending initializers. PURPOSE
8607 identifies the initializer, either array index or field in a structure.
8608 VALUE is the value of that index or field. If ORIGTYPE is not
8609 NULL_TREE, it is the original type of VALUE.
8611 IMPLICIT is true if value comes from pop_init_level (1),
8612 the new initializer has been merged with the existing one
8613 and thus no warnings should be emitted about overriding an
8614 existing initializer. */
8616 static void
8617 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8618 bool implicit, struct obstack *braced_init_obstack)
8620 struct init_node *p, **q, *r;
8622 q = &constructor_pending_elts;
8623 p = 0;
8625 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8627 while (*q != 0)
8629 p = *q;
8630 if (tree_int_cst_lt (purpose, p->purpose))
8631 q = &p->left;
8632 else if (tree_int_cst_lt (p->purpose, purpose))
8633 q = &p->right;
8634 else
8636 if (!implicit)
8638 if (TREE_SIDE_EFFECTS (p->value))
8639 warning_init (loc, OPT_Woverride_init_side_effects,
8640 "initialized field with side-effects "
8641 "overwritten");
8642 else if (warn_override_init)
8643 warning_init (loc, OPT_Woverride_init,
8644 "initialized field overwritten");
8646 p->value = value;
8647 p->origtype = origtype;
8648 return;
8652 else
8654 tree bitpos;
8656 bitpos = bit_position (purpose);
8657 while (*q != NULL)
8659 p = *q;
8660 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8661 q = &p->left;
8662 else if (p->purpose != purpose)
8663 q = &p->right;
8664 else
8666 if (!implicit)
8668 if (TREE_SIDE_EFFECTS (p->value))
8669 warning_init (loc, OPT_Woverride_init_side_effects,
8670 "initialized field with side-effects "
8671 "overwritten");
8672 else if (warn_override_init)
8673 warning_init (loc, OPT_Woverride_init,
8674 "initialized field overwritten");
8676 p->value = value;
8677 p->origtype = origtype;
8678 return;
8683 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8684 sizeof (struct init_node));
8685 r->purpose = purpose;
8686 r->value = value;
8687 r->origtype = origtype;
8689 *q = r;
8690 r->parent = p;
8691 r->left = 0;
8692 r->right = 0;
8693 r->balance = 0;
8695 while (p)
8697 struct init_node *s;
8699 if (r == p->left)
8701 if (p->balance == 0)
8702 p->balance = -1;
8703 else if (p->balance < 0)
8705 if (r->balance < 0)
8707 /* L rotation. */
8708 p->left = r->right;
8709 if (p->left)
8710 p->left->parent = p;
8711 r->right = p;
8713 p->balance = 0;
8714 r->balance = 0;
8716 s = p->parent;
8717 p->parent = r;
8718 r->parent = s;
8719 if (s)
8721 if (s->left == p)
8722 s->left = r;
8723 else
8724 s->right = r;
8726 else
8727 constructor_pending_elts = r;
8729 else
8731 /* LR rotation. */
8732 struct init_node *t = r->right;
8734 r->right = t->left;
8735 if (r->right)
8736 r->right->parent = r;
8737 t->left = r;
8739 p->left = t->right;
8740 if (p->left)
8741 p->left->parent = p;
8742 t->right = p;
8744 p->balance = t->balance < 0;
8745 r->balance = -(t->balance > 0);
8746 t->balance = 0;
8748 s = p->parent;
8749 p->parent = t;
8750 r->parent = t;
8751 t->parent = s;
8752 if (s)
8754 if (s->left == p)
8755 s->left = t;
8756 else
8757 s->right = t;
8759 else
8760 constructor_pending_elts = t;
8762 break;
8764 else
8766 /* p->balance == +1; growth of left side balances the node. */
8767 p->balance = 0;
8768 break;
8771 else /* r == p->right */
8773 if (p->balance == 0)
8774 /* Growth propagation from right side. */
8775 p->balance++;
8776 else if (p->balance > 0)
8778 if (r->balance > 0)
8780 /* R rotation. */
8781 p->right = r->left;
8782 if (p->right)
8783 p->right->parent = p;
8784 r->left = p;
8786 p->balance = 0;
8787 r->balance = 0;
8789 s = p->parent;
8790 p->parent = r;
8791 r->parent = s;
8792 if (s)
8794 if (s->left == p)
8795 s->left = r;
8796 else
8797 s->right = r;
8799 else
8800 constructor_pending_elts = r;
8802 else /* r->balance == -1 */
8804 /* RL rotation */
8805 struct init_node *t = r->left;
8807 r->left = t->right;
8808 if (r->left)
8809 r->left->parent = r;
8810 t->right = r;
8812 p->right = t->left;
8813 if (p->right)
8814 p->right->parent = p;
8815 t->left = p;
8817 r->balance = (t->balance < 0);
8818 p->balance = -(t->balance > 0);
8819 t->balance = 0;
8821 s = p->parent;
8822 p->parent = t;
8823 r->parent = t;
8824 t->parent = s;
8825 if (s)
8827 if (s->left == p)
8828 s->left = t;
8829 else
8830 s->right = t;
8832 else
8833 constructor_pending_elts = t;
8835 break;
8837 else
8839 /* p->balance == -1; growth of right side balances the node. */
8840 p->balance = 0;
8841 break;
8845 r = p;
8846 p = p->parent;
8850 /* Build AVL tree from a sorted chain. */
8852 static void
8853 set_nonincremental_init (struct obstack * braced_init_obstack)
8855 unsigned HOST_WIDE_INT ix;
8856 tree index, value;
8858 if (TREE_CODE (constructor_type) != RECORD_TYPE
8859 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8860 return;
8862 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8863 add_pending_init (input_location, index, value, NULL_TREE, true,
8864 braced_init_obstack);
8865 constructor_elements = NULL;
8866 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8868 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8869 /* Skip any nameless bit fields at the beginning. */
8870 while (constructor_unfilled_fields != NULL_TREE
8871 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8872 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
8873 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8876 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8878 if (TYPE_DOMAIN (constructor_type))
8879 constructor_unfilled_index
8880 = convert (bitsizetype,
8881 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8882 else
8883 constructor_unfilled_index = bitsize_zero_node;
8885 constructor_incremental = 0;
8888 /* Build AVL tree from a string constant. */
8890 static void
8891 set_nonincremental_init_from_string (tree str,
8892 struct obstack * braced_init_obstack)
8894 tree value, purpose, type;
8895 HOST_WIDE_INT val[2];
8896 const char *p, *end;
8897 int byte, wchar_bytes, charwidth, bitpos;
8899 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8901 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8902 charwidth = TYPE_PRECISION (char_type_node);
8903 gcc_assert ((size_t) wchar_bytes * charwidth
8904 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8905 type = TREE_TYPE (constructor_type);
8906 p = TREE_STRING_POINTER (str);
8907 end = p + TREE_STRING_LENGTH (str);
8909 for (purpose = bitsize_zero_node;
8910 p < end
8911 && !(constructor_max_index
8912 && tree_int_cst_lt (constructor_max_index, purpose));
8913 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8915 if (wchar_bytes == 1)
8917 val[0] = (unsigned char) *p++;
8918 val[1] = 0;
8920 else
8922 val[1] = 0;
8923 val[0] = 0;
8924 for (byte = 0; byte < wchar_bytes; byte++)
8926 if (BYTES_BIG_ENDIAN)
8927 bitpos = (wchar_bytes - byte - 1) * charwidth;
8928 else
8929 bitpos = byte * charwidth;
8930 val[bitpos / HOST_BITS_PER_WIDE_INT]
8931 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8932 << (bitpos % HOST_BITS_PER_WIDE_INT);
8936 if (!TYPE_UNSIGNED (type))
8938 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8939 if (bitpos < HOST_BITS_PER_WIDE_INT)
8941 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8943 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8944 val[1] = -1;
8947 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8949 if (val[0] < 0)
8950 val[1] = -1;
8952 else if (val[1] & (HOST_WIDE_INT_1
8953 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8954 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8957 value = wide_int_to_tree (type,
8958 wide_int::from_array (val, 2,
8959 HOST_BITS_PER_WIDE_INT * 2));
8960 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8961 braced_init_obstack);
8964 constructor_incremental = 0;
8967 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
8968 not initialized yet. */
8970 static tree
8971 find_init_member (tree field, struct obstack * braced_init_obstack)
8973 struct init_node *p;
8975 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8977 if (constructor_incremental
8978 && tree_int_cst_lt (field, constructor_unfilled_index))
8979 set_nonincremental_init (braced_init_obstack);
8981 p = constructor_pending_elts;
8982 while (p)
8984 if (tree_int_cst_lt (field, p->purpose))
8985 p = p->left;
8986 else if (tree_int_cst_lt (p->purpose, field))
8987 p = p->right;
8988 else
8989 return p->value;
8992 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8994 tree bitpos = bit_position (field);
8996 if (constructor_incremental
8997 && (!constructor_unfilled_fields
8998 || tree_int_cst_lt (bitpos,
8999 bit_position (constructor_unfilled_fields))))
9000 set_nonincremental_init (braced_init_obstack);
9002 p = constructor_pending_elts;
9003 while (p)
9005 if (field == p->purpose)
9006 return p->value;
9007 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9008 p = p->left;
9009 else
9010 p = p->right;
9013 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9015 if (!vec_safe_is_empty (constructor_elements)
9016 && (constructor_elements->last ().index == field))
9017 return constructor_elements->last ().value;
9019 return NULL_TREE;
9022 /* "Output" the next constructor element.
9023 At top level, really output it to assembler code now.
9024 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9025 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9026 TYPE is the data type that the containing data type wants here.
9027 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9028 If VALUE is a string constant, STRICT_STRING is true if it is
9029 unparenthesized or we should not warn here for it being parenthesized.
9030 For other types of VALUE, STRICT_STRING is not used.
9032 PENDING if true means output pending elements that belong
9033 right after this element. (PENDING is normally true;
9034 it is false while outputting pending elements, to avoid recursion.)
9036 IMPLICIT is true if value comes from pop_init_level (1),
9037 the new initializer has been merged with the existing one
9038 and thus no warnings should be emitted about overriding an
9039 existing initializer. */
9041 static void
9042 output_init_element (location_t loc, tree value, tree origtype,
9043 bool strict_string, tree type, tree field, bool pending,
9044 bool implicit, struct obstack * braced_init_obstack)
9046 tree semantic_type = NULL_TREE;
9047 bool maybe_const = true;
9048 bool npc;
9050 if (type == error_mark_node || value == error_mark_node)
9052 constructor_erroneous = 1;
9053 return;
9055 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9056 && (TREE_CODE (value) == STRING_CST
9057 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9058 && !(TREE_CODE (value) == STRING_CST
9059 && TREE_CODE (type) == ARRAY_TYPE
9060 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9061 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9062 TYPE_MAIN_VARIANT (type)))
9063 value = array_to_pointer_conversion (input_location, value);
9065 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9066 && require_constant_value && pending)
9068 /* As an extension, allow initializing objects with static storage
9069 duration with compound literals (which are then treated just as
9070 the brace enclosed list they contain). */
9071 if (flag_isoc99)
9072 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9073 "constant");
9074 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9075 value = DECL_INITIAL (decl);
9078 npc = null_pointer_constant_p (value);
9079 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9081 semantic_type = TREE_TYPE (value);
9082 value = TREE_OPERAND (value, 0);
9084 value = c_fully_fold (value, require_constant_value, &maybe_const);
9086 if (value == error_mark_node)
9087 constructor_erroneous = 1;
9088 else if (!TREE_CONSTANT (value))
9089 constructor_constant = 0;
9090 else if (!initializer_constant_valid_p (value,
9091 TREE_TYPE (value),
9092 AGGREGATE_TYPE_P (constructor_type)
9093 && TYPE_REVERSE_STORAGE_ORDER
9094 (constructor_type))
9095 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9096 && DECL_C_BIT_FIELD (field)
9097 && TREE_CODE (value) != INTEGER_CST))
9098 constructor_simple = 0;
9099 if (!maybe_const)
9100 constructor_nonconst = 1;
9102 /* Digest the initializer and issue any errors about incompatible
9103 types before issuing errors about non-constant initializers. */
9104 tree new_value = value;
9105 if (semantic_type)
9106 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9107 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9108 require_constant_value);
9109 if (new_value == error_mark_node)
9111 constructor_erroneous = 1;
9112 return;
9114 if (require_constant_value || require_constant_elements)
9115 constant_expression_warning (new_value);
9117 /* Proceed to check the constness of the original initializer. */
9118 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9120 if (require_constant_value)
9122 error_init (loc, "initializer element is not constant");
9123 value = error_mark_node;
9125 else if (require_constant_elements)
9126 pedwarn (loc, OPT_Wpedantic,
9127 "initializer element is not computable at load time");
9129 else if (!maybe_const
9130 && (require_constant_value || require_constant_elements))
9131 pedwarn_init (loc, OPT_Wpedantic,
9132 "initializer element is not a constant expression");
9134 /* Issue -Wc++-compat warnings about initializing a bitfield with
9135 enum type. */
9136 if (warn_cxx_compat
9137 && field != NULL_TREE
9138 && TREE_CODE (field) == FIELD_DECL
9139 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9140 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9141 != TYPE_MAIN_VARIANT (type))
9142 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9144 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9145 if (checktype != error_mark_node
9146 && (TYPE_MAIN_VARIANT (checktype)
9147 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9148 warning_init (loc, OPT_Wc___compat,
9149 "enum conversion in initialization is invalid in C++");
9152 /* If this field is empty (and not at the end of structure),
9153 don't do anything other than checking the initializer. */
9154 if (field
9155 && (TREE_TYPE (field) == error_mark_node
9156 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9157 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9158 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9159 || DECL_CHAIN (field)))))
9160 return;
9162 /* Finally, set VALUE to the initializer value digested above. */
9163 value = new_value;
9165 /* If this element doesn't come next in sequence,
9166 put it on constructor_pending_elts. */
9167 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9168 && (!constructor_incremental
9169 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9171 if (constructor_incremental
9172 && tree_int_cst_lt (field, constructor_unfilled_index))
9173 set_nonincremental_init (braced_init_obstack);
9175 add_pending_init (loc, field, value, origtype, implicit,
9176 braced_init_obstack);
9177 return;
9179 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9180 && (!constructor_incremental
9181 || field != constructor_unfilled_fields))
9183 /* We do this for records but not for unions. In a union,
9184 no matter which field is specified, it can be initialized
9185 right away since it starts at the beginning of the union. */
9186 if (constructor_incremental)
9188 if (!constructor_unfilled_fields)
9189 set_nonincremental_init (braced_init_obstack);
9190 else
9192 tree bitpos, unfillpos;
9194 bitpos = bit_position (field);
9195 unfillpos = bit_position (constructor_unfilled_fields);
9197 if (tree_int_cst_lt (bitpos, unfillpos))
9198 set_nonincremental_init (braced_init_obstack);
9202 add_pending_init (loc, field, value, origtype, implicit,
9203 braced_init_obstack);
9204 return;
9206 else if (TREE_CODE (constructor_type) == UNION_TYPE
9207 && !vec_safe_is_empty (constructor_elements))
9209 if (!implicit)
9211 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9212 warning_init (loc, OPT_Woverride_init_side_effects,
9213 "initialized field with side-effects overwritten");
9214 else if (warn_override_init)
9215 warning_init (loc, OPT_Woverride_init,
9216 "initialized field overwritten");
9219 /* We can have just one union field set. */
9220 constructor_elements = NULL;
9223 /* Otherwise, output this element either to
9224 constructor_elements or to the assembler file. */
9226 constructor_elt celt = {field, value};
9227 vec_safe_push (constructor_elements, celt);
9229 /* Advance the variable that indicates sequential elements output. */
9230 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9231 constructor_unfilled_index
9232 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9233 bitsize_one_node);
9234 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9236 constructor_unfilled_fields
9237 = DECL_CHAIN (constructor_unfilled_fields);
9239 /* Skip any nameless bit fields. */
9240 while (constructor_unfilled_fields != NULL_TREE
9241 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9242 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
9243 constructor_unfilled_fields =
9244 DECL_CHAIN (constructor_unfilled_fields);
9246 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9247 constructor_unfilled_fields = NULL_TREE;
9249 /* Now output any pending elements which have become next. */
9250 if (pending)
9251 output_pending_init_elements (0, braced_init_obstack);
9254 /* Output any pending elements which have become next.
9255 As we output elements, constructor_unfilled_{fields,index}
9256 advances, which may cause other elements to become next;
9257 if so, they too are output.
9259 If ALL is 0, we return when there are
9260 no more pending elements to output now.
9262 If ALL is 1, we output space as necessary so that
9263 we can output all the pending elements. */
9264 static void
9265 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9267 struct init_node *elt = constructor_pending_elts;
9268 tree next;
9270 retry:
9272 /* Look through the whole pending tree.
9273 If we find an element that should be output now,
9274 output it. Otherwise, set NEXT to the element
9275 that comes first among those still pending. */
9277 next = NULL_TREE;
9278 while (elt)
9280 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9282 if (tree_int_cst_equal (elt->purpose,
9283 constructor_unfilled_index))
9284 output_init_element (input_location, elt->value, elt->origtype,
9285 true, TREE_TYPE (constructor_type),
9286 constructor_unfilled_index, false, false,
9287 braced_init_obstack);
9288 else if (tree_int_cst_lt (constructor_unfilled_index,
9289 elt->purpose))
9291 /* Advance to the next smaller node. */
9292 if (elt->left)
9293 elt = elt->left;
9294 else
9296 /* We have reached the smallest node bigger than the
9297 current unfilled index. Fill the space first. */
9298 next = elt->purpose;
9299 break;
9302 else
9304 /* Advance to the next bigger node. */
9305 if (elt->right)
9306 elt = elt->right;
9307 else
9309 /* We have reached the biggest node in a subtree. Find
9310 the parent of it, which is the next bigger node. */
9311 while (elt->parent && elt->parent->right == elt)
9312 elt = elt->parent;
9313 elt = elt->parent;
9314 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9315 elt->purpose))
9317 next = elt->purpose;
9318 break;
9323 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9325 tree ctor_unfilled_bitpos, elt_bitpos;
9327 /* If the current record is complete we are done. */
9328 if (constructor_unfilled_fields == NULL_TREE)
9329 break;
9331 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9332 elt_bitpos = bit_position (elt->purpose);
9333 /* We can't compare fields here because there might be empty
9334 fields in between. */
9335 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9337 constructor_unfilled_fields = elt->purpose;
9338 output_init_element (input_location, elt->value, elt->origtype,
9339 true, TREE_TYPE (elt->purpose),
9340 elt->purpose, false, false,
9341 braced_init_obstack);
9343 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9345 /* Advance to the next smaller node. */
9346 if (elt->left)
9347 elt = elt->left;
9348 else
9350 /* We have reached the smallest node bigger than the
9351 current unfilled field. Fill the space first. */
9352 next = elt->purpose;
9353 break;
9356 else
9358 /* Advance to the next bigger node. */
9359 if (elt->right)
9360 elt = elt->right;
9361 else
9363 /* We have reached the biggest node in a subtree. Find
9364 the parent of it, which is the next bigger node. */
9365 while (elt->parent && elt->parent->right == elt)
9366 elt = elt->parent;
9367 elt = elt->parent;
9368 if (elt
9369 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9370 bit_position (elt->purpose))))
9372 next = elt->purpose;
9373 break;
9380 /* Ordinarily return, but not if we want to output all
9381 and there are elements left. */
9382 if (!(all && next != NULL_TREE))
9383 return;
9385 /* If it's not incremental, just skip over the gap, so that after
9386 jumping to retry we will output the next successive element. */
9387 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9388 constructor_unfilled_fields = next;
9389 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9390 constructor_unfilled_index = next;
9392 /* ELT now points to the node in the pending tree with the next
9393 initializer to output. */
9394 goto retry;
9397 /* Add one non-braced element to the current constructor level.
9398 This adjusts the current position within the constructor's type.
9399 This may also start or terminate implicit levels
9400 to handle a partly-braced initializer.
9402 Once this has found the correct level for the new element,
9403 it calls output_init_element.
9405 IMPLICIT is true if value comes from pop_init_level (1),
9406 the new initializer has been merged with the existing one
9407 and thus no warnings should be emitted about overriding an
9408 existing initializer. */
9410 void
9411 process_init_element (location_t loc, struct c_expr value, bool implicit,
9412 struct obstack * braced_init_obstack)
9414 tree orig_value = value.value;
9415 int string_flag
9416 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9417 bool strict_string = value.original_code == STRING_CST;
9418 bool was_designated = designator_depth != 0;
9420 designator_depth = 0;
9421 designator_erroneous = 0;
9423 if (!implicit && value.value && !integer_zerop (value.value))
9424 constructor_zeroinit = 0;
9426 /* Handle superfluous braces around string cst as in
9427 char x[] = {"foo"}; */
9428 if (string_flag
9429 && constructor_type
9430 && !was_designated
9431 && TREE_CODE (constructor_type) == ARRAY_TYPE
9432 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9433 && integer_zerop (constructor_unfilled_index))
9435 if (constructor_stack->replacement_value.value)
9436 error_init (loc, "excess elements in char array initializer");
9437 constructor_stack->replacement_value = value;
9438 return;
9441 if (constructor_stack->replacement_value.value != NULL_TREE)
9443 error_init (loc, "excess elements in struct initializer");
9444 return;
9447 /* Ignore elements of a brace group if it is entirely superfluous
9448 and has already been diagnosed. */
9449 if (constructor_type == NULL_TREE)
9450 return;
9452 if (!implicit && warn_designated_init && !was_designated
9453 && TREE_CODE (constructor_type) == RECORD_TYPE
9454 && lookup_attribute ("designated_init",
9455 TYPE_ATTRIBUTES (constructor_type)))
9456 warning_init (loc,
9457 OPT_Wdesignated_init,
9458 "positional initialization of field "
9459 "in %<struct%> declared with %<designated_init%> attribute");
9461 /* If we've exhausted any levels that didn't have braces,
9462 pop them now. */
9463 while (constructor_stack->implicit)
9465 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9466 && constructor_fields == NULL_TREE)
9467 process_init_element (loc,
9468 pop_init_level (loc, 1, braced_init_obstack,
9469 last_init_list_comma),
9470 true, braced_init_obstack);
9471 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9472 || VECTOR_TYPE_P (constructor_type))
9473 && constructor_max_index
9474 && tree_int_cst_lt (constructor_max_index,
9475 constructor_index))
9476 process_init_element (loc,
9477 pop_init_level (loc, 1, braced_init_obstack,
9478 last_init_list_comma),
9479 true, braced_init_obstack);
9480 else
9481 break;
9484 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9485 if (constructor_range_stack)
9487 /* If value is a compound literal and we'll be just using its
9488 content, don't put it into a SAVE_EXPR. */
9489 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9490 || !require_constant_value)
9492 tree semantic_type = NULL_TREE;
9493 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9495 semantic_type = TREE_TYPE (value.value);
9496 value.value = TREE_OPERAND (value.value, 0);
9498 value.value = save_expr (value.value);
9499 if (semantic_type)
9500 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9501 value.value);
9505 while (1)
9507 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9509 tree fieldtype;
9510 enum tree_code fieldcode;
9512 if (constructor_fields == NULL_TREE)
9514 pedwarn_init (loc, 0, "excess elements in struct initializer");
9515 break;
9518 fieldtype = TREE_TYPE (constructor_fields);
9519 if (fieldtype != error_mark_node)
9520 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9521 fieldcode = TREE_CODE (fieldtype);
9523 /* Error for non-static initialization of a flexible array member. */
9524 if (fieldcode == ARRAY_TYPE
9525 && !require_constant_value
9526 && TYPE_SIZE (fieldtype) == NULL_TREE
9527 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9529 error_init (loc, "non-static initialization of a flexible "
9530 "array member");
9531 break;
9534 /* Error for initialization of a flexible array member with
9535 a string constant if the structure is in an array. E.g.:
9536 struct S { int x; char y[]; };
9537 struct S s[] = { { 1, "foo" } };
9538 is invalid. */
9539 if (string_flag
9540 && fieldcode == ARRAY_TYPE
9541 && constructor_depth > 1
9542 && TYPE_SIZE (fieldtype) == NULL_TREE
9543 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9545 bool in_array_p = false;
9546 for (struct constructor_stack *p = constructor_stack;
9547 p && p->type; p = p->next)
9548 if (TREE_CODE (p->type) == ARRAY_TYPE)
9550 in_array_p = true;
9551 break;
9553 if (in_array_p)
9555 error_init (loc, "initialization of flexible array "
9556 "member in a nested context");
9557 break;
9561 /* Accept a string constant to initialize a subarray. */
9562 if (value.value != NULL_TREE
9563 && fieldcode == ARRAY_TYPE
9564 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9565 && string_flag)
9566 value.value = orig_value;
9567 /* Otherwise, if we have come to a subaggregate,
9568 and we don't have an element of its type, push into it. */
9569 else if (value.value != NULL_TREE
9570 && value.value != error_mark_node
9571 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9572 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9573 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9575 push_init_level (loc, 1, braced_init_obstack);
9576 continue;
9579 if (value.value)
9581 push_member_name (constructor_fields);
9582 output_init_element (loc, value.value, value.original_type,
9583 strict_string, fieldtype,
9584 constructor_fields, true, implicit,
9585 braced_init_obstack);
9586 RESTORE_SPELLING_DEPTH (constructor_depth);
9588 else
9589 /* Do the bookkeeping for an element that was
9590 directly output as a constructor. */
9592 /* For a record, keep track of end position of last field. */
9593 if (DECL_SIZE (constructor_fields))
9594 constructor_bit_index
9595 = size_binop_loc (input_location, PLUS_EXPR,
9596 bit_position (constructor_fields),
9597 DECL_SIZE (constructor_fields));
9599 /* If the current field was the first one not yet written out,
9600 it isn't now, so update. */
9601 if (constructor_unfilled_fields == constructor_fields)
9603 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9604 /* Skip any nameless bit fields. */
9605 while (constructor_unfilled_fields != 0
9606 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9607 && DECL_NAME (constructor_unfilled_fields) == 0)
9608 constructor_unfilled_fields =
9609 DECL_CHAIN (constructor_unfilled_fields);
9613 constructor_fields = DECL_CHAIN (constructor_fields);
9614 /* Skip any nameless bit fields at the beginning. */
9615 while (constructor_fields != NULL_TREE
9616 && DECL_C_BIT_FIELD (constructor_fields)
9617 && DECL_NAME (constructor_fields) == NULL_TREE)
9618 constructor_fields = DECL_CHAIN (constructor_fields);
9620 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9622 tree fieldtype;
9623 enum tree_code fieldcode;
9625 if (constructor_fields == NULL_TREE)
9627 pedwarn_init (loc, 0,
9628 "excess elements in union initializer");
9629 break;
9632 fieldtype = TREE_TYPE (constructor_fields);
9633 if (fieldtype != error_mark_node)
9634 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9635 fieldcode = TREE_CODE (fieldtype);
9637 /* Warn that traditional C rejects initialization of unions.
9638 We skip the warning if the value is zero. This is done
9639 under the assumption that the zero initializer in user
9640 code appears conditioned on e.g. __STDC__ to avoid
9641 "missing initializer" warnings and relies on default
9642 initialization to zero in the traditional C case.
9643 We also skip the warning if the initializer is designated,
9644 again on the assumption that this must be conditional on
9645 __STDC__ anyway (and we've already complained about the
9646 member-designator already). */
9647 if (!in_system_header_at (input_location) && !constructor_designated
9648 && !(value.value && (integer_zerop (value.value)
9649 || real_zerop (value.value))))
9650 warning (OPT_Wtraditional, "traditional C rejects initialization "
9651 "of unions");
9653 /* Accept a string constant to initialize a subarray. */
9654 if (value.value != NULL_TREE
9655 && fieldcode == ARRAY_TYPE
9656 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9657 && string_flag)
9658 value.value = orig_value;
9659 /* Otherwise, if we have come to a subaggregate,
9660 and we don't have an element of its type, push into it. */
9661 else if (value.value != NULL_TREE
9662 && value.value != error_mark_node
9663 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9664 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9665 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9667 push_init_level (loc, 1, braced_init_obstack);
9668 continue;
9671 if (value.value)
9673 push_member_name (constructor_fields);
9674 output_init_element (loc, value.value, value.original_type,
9675 strict_string, fieldtype,
9676 constructor_fields, true, implicit,
9677 braced_init_obstack);
9678 RESTORE_SPELLING_DEPTH (constructor_depth);
9680 else
9681 /* Do the bookkeeping for an element that was
9682 directly output as a constructor. */
9684 constructor_bit_index = DECL_SIZE (constructor_fields);
9685 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9688 constructor_fields = NULL_TREE;
9690 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9692 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9693 enum tree_code eltcode = TREE_CODE (elttype);
9695 /* Accept a string constant to initialize a subarray. */
9696 if (value.value != NULL_TREE
9697 && eltcode == ARRAY_TYPE
9698 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9699 && string_flag)
9700 value.value = orig_value;
9701 /* Otherwise, if we have come to a subaggregate,
9702 and we don't have an element of its type, push into it. */
9703 else if (value.value != NULL_TREE
9704 && value.value != error_mark_node
9705 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9706 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9707 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9709 push_init_level (loc, 1, braced_init_obstack);
9710 continue;
9713 if (constructor_max_index != NULL_TREE
9714 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9715 || integer_all_onesp (constructor_max_index)))
9717 pedwarn_init (loc, 0,
9718 "excess elements in array initializer");
9719 break;
9722 /* Now output the actual element. */
9723 if (value.value)
9725 push_array_bounds (tree_to_uhwi (constructor_index));
9726 output_init_element (loc, value.value, value.original_type,
9727 strict_string, elttype,
9728 constructor_index, true, implicit,
9729 braced_init_obstack);
9730 RESTORE_SPELLING_DEPTH (constructor_depth);
9733 constructor_index
9734 = size_binop_loc (input_location, PLUS_EXPR,
9735 constructor_index, bitsize_one_node);
9737 if (!value.value)
9738 /* If we are doing the bookkeeping for an element that was
9739 directly output as a constructor, we must update
9740 constructor_unfilled_index. */
9741 constructor_unfilled_index = constructor_index;
9743 else if (VECTOR_TYPE_P (constructor_type))
9745 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9747 /* Do a basic check of initializer size. Note that vectors
9748 always have a fixed size derived from their type. */
9749 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9751 pedwarn_init (loc, 0,
9752 "excess elements in vector initializer");
9753 break;
9756 /* Now output the actual element. */
9757 if (value.value)
9759 if (TREE_CODE (value.value) == VECTOR_CST)
9760 elttype = TYPE_MAIN_VARIANT (constructor_type);
9761 output_init_element (loc, value.value, value.original_type,
9762 strict_string, elttype,
9763 constructor_index, true, implicit,
9764 braced_init_obstack);
9767 constructor_index
9768 = size_binop_loc (input_location,
9769 PLUS_EXPR, constructor_index, bitsize_one_node);
9771 if (!value.value)
9772 /* If we are doing the bookkeeping for an element that was
9773 directly output as a constructor, we must update
9774 constructor_unfilled_index. */
9775 constructor_unfilled_index = constructor_index;
9778 /* Handle the sole element allowed in a braced initializer
9779 for a scalar variable. */
9780 else if (constructor_type != error_mark_node
9781 && constructor_fields == NULL_TREE)
9783 pedwarn_init (loc, 0,
9784 "excess elements in scalar initializer");
9785 break;
9787 else
9789 if (value.value)
9790 output_init_element (loc, value.value, value.original_type,
9791 strict_string, constructor_type,
9792 NULL_TREE, true, implicit,
9793 braced_init_obstack);
9794 constructor_fields = NULL_TREE;
9797 /* Handle range initializers either at this level or anywhere higher
9798 in the designator stack. */
9799 if (constructor_range_stack)
9801 struct constructor_range_stack *p, *range_stack;
9802 int finish = 0;
9804 range_stack = constructor_range_stack;
9805 constructor_range_stack = 0;
9806 while (constructor_stack != range_stack->stack)
9808 gcc_assert (constructor_stack->implicit);
9809 process_init_element (loc,
9810 pop_init_level (loc, 1,
9811 braced_init_obstack,
9812 last_init_list_comma),
9813 true, braced_init_obstack);
9815 for (p = range_stack;
9816 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9817 p = p->prev)
9819 gcc_assert (constructor_stack->implicit);
9820 process_init_element (loc,
9821 pop_init_level (loc, 1,
9822 braced_init_obstack,
9823 last_init_list_comma),
9824 true, braced_init_obstack);
9827 p->index = size_binop_loc (input_location,
9828 PLUS_EXPR, p->index, bitsize_one_node);
9829 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9830 finish = 1;
9832 while (1)
9834 constructor_index = p->index;
9835 constructor_fields = p->fields;
9836 if (finish && p->range_end && p->index == p->range_start)
9838 finish = 0;
9839 p->prev = 0;
9841 p = p->next;
9842 if (!p)
9843 break;
9844 finish_implicit_inits (loc, braced_init_obstack);
9845 push_init_level (loc, 2, braced_init_obstack);
9846 p->stack = constructor_stack;
9847 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9848 p->index = p->range_start;
9851 if (!finish)
9852 constructor_range_stack = range_stack;
9853 continue;
9856 break;
9859 constructor_range_stack = 0;
9862 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9863 (guaranteed to be 'volatile' or null) and ARGS (represented using
9864 an ASM_EXPR node). */
9865 tree
9866 build_asm_stmt (tree cv_qualifier, tree args)
9868 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9869 ASM_VOLATILE_P (args) = 1;
9870 return add_stmt (args);
9873 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9874 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9875 SIMPLE indicates whether there was anything at all after the
9876 string in the asm expression -- asm("blah") and asm("blah" : )
9877 are subtly different. We use a ASM_EXPR node to represent this. */
9878 tree
9879 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9880 tree clobbers, tree labels, bool simple)
9882 tree tail;
9883 tree args;
9884 int i;
9885 const char *constraint;
9886 const char **oconstraints;
9887 bool allows_mem, allows_reg, is_inout;
9888 int ninputs, noutputs;
9890 ninputs = list_length (inputs);
9891 noutputs = list_length (outputs);
9892 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9894 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9896 /* Remove output conversions that change the type but not the mode. */
9897 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9899 tree output = TREE_VALUE (tail);
9901 output = c_fully_fold (output, false, NULL, true);
9903 /* ??? Really, this should not be here. Users should be using a
9904 proper lvalue, dammit. But there's a long history of using casts
9905 in the output operands. In cases like longlong.h, this becomes a
9906 primitive form of typechecking -- if the cast can be removed, then
9907 the output operand had a type of the proper width; otherwise we'll
9908 get an error. Gross, but ... */
9909 STRIP_NOPS (output);
9911 if (!lvalue_or_else (loc, output, lv_asm))
9912 output = error_mark_node;
9914 if (output != error_mark_node
9915 && (TREE_READONLY (output)
9916 || TYPE_READONLY (TREE_TYPE (output))
9917 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9918 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9919 readonly_error (loc, output, lv_asm);
9921 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9922 oconstraints[i] = constraint;
9924 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9925 &allows_mem, &allows_reg, &is_inout))
9927 /* If the operand is going to end up in memory,
9928 mark it addressable. */
9929 if (!allows_reg && !c_mark_addressable (output))
9930 output = error_mark_node;
9931 if (!(!allows_reg && allows_mem)
9932 && output != error_mark_node
9933 && VOID_TYPE_P (TREE_TYPE (output)))
9935 error_at (loc, "invalid use of void expression");
9936 output = error_mark_node;
9939 else
9940 output = error_mark_node;
9942 TREE_VALUE (tail) = output;
9945 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9947 tree input;
9949 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9950 input = TREE_VALUE (tail);
9952 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9953 oconstraints, &allows_mem, &allows_reg))
9955 /* If the operand is going to end up in memory,
9956 mark it addressable. */
9957 if (!allows_reg && allows_mem)
9959 input = c_fully_fold (input, false, NULL, true);
9961 /* Strip the nops as we allow this case. FIXME, this really
9962 should be rejected or made deprecated. */
9963 STRIP_NOPS (input);
9964 if (!c_mark_addressable (input))
9965 input = error_mark_node;
9967 else
9969 struct c_expr expr;
9970 memset (&expr, 0, sizeof (expr));
9971 expr.value = input;
9972 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9973 input = c_fully_fold (expr.value, false, NULL);
9975 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9977 error_at (loc, "invalid use of void expression");
9978 input = error_mark_node;
9982 else
9983 input = error_mark_node;
9985 TREE_VALUE (tail) = input;
9988 /* ASMs with labels cannot have outputs. This should have been
9989 enforced by the parser. */
9990 gcc_assert (outputs == NULL || labels == NULL);
9992 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9994 /* asm statements without outputs, including simple ones, are treated
9995 as volatile. */
9996 ASM_INPUT_P (args) = simple;
9997 ASM_VOLATILE_P (args) = (noutputs == 0);
9999 return args;
10002 /* Generate a goto statement to LABEL. LOC is the location of the
10003 GOTO. */
10005 tree
10006 c_finish_goto_label (location_t loc, tree label)
10008 tree decl = lookup_label_for_goto (loc, label);
10009 if (!decl)
10010 return NULL_TREE;
10011 TREE_USED (decl) = 1;
10013 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10014 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10015 SET_EXPR_LOCATION (t, loc);
10016 return add_stmt (t);
10020 /* Generate a computed goto statement to EXPR. LOC is the location of
10021 the GOTO. */
10023 tree
10024 c_finish_goto_ptr (location_t loc, tree expr)
10026 tree t;
10027 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10028 expr = c_fully_fold (expr, false, NULL);
10029 expr = convert (ptr_type_node, expr);
10030 t = build1 (GOTO_EXPR, void_type_node, expr);
10031 SET_EXPR_LOCATION (t, loc);
10032 return add_stmt (t);
10035 /* Generate a C `return' statement. RETVAL is the expression for what
10036 to return, or a null pointer for `return;' with no value. LOC is
10037 the location of the return statement, or the location of the expression,
10038 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10039 is the original type of RETVAL. */
10041 tree
10042 c_finish_return (location_t loc, tree retval, tree origtype)
10044 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10045 bool no_warning = false;
10046 bool npc = false;
10048 /* Use the expansion point to handle cases such as returning NULL
10049 in a function returning void. */
10050 source_location xloc = expansion_point_location_if_in_system_header (loc);
10052 if (TREE_THIS_VOLATILE (current_function_decl))
10053 warning_at (xloc, 0,
10054 "function declared %<noreturn%> has a %<return%> statement");
10056 if (retval)
10058 tree semantic_type = NULL_TREE;
10059 npc = null_pointer_constant_p (retval);
10060 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10062 semantic_type = TREE_TYPE (retval);
10063 retval = TREE_OPERAND (retval, 0);
10065 retval = c_fully_fold (retval, false, NULL);
10066 if (semantic_type)
10067 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10070 if (!retval)
10072 current_function_returns_null = 1;
10073 if ((warn_return_type || flag_isoc99)
10074 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10076 bool warned_here;
10077 if (flag_isoc99)
10078 warned_here = pedwarn
10079 (loc, 0,
10080 "%<return%> with no value, in function returning non-void");
10081 else
10082 warned_here = warning_at
10083 (loc, OPT_Wreturn_type,
10084 "%<return%> with no value, in function returning non-void");
10085 no_warning = true;
10086 if (warned_here)
10087 inform (DECL_SOURCE_LOCATION (current_function_decl),
10088 "declared here");
10091 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10093 current_function_returns_null = 1;
10094 bool warned_here;
10095 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10096 warned_here = pedwarn
10097 (xloc, 0,
10098 "%<return%> with a value, in function returning void");
10099 else
10100 warned_here = pedwarn
10101 (xloc, OPT_Wpedantic, "ISO C forbids "
10102 "%<return%> with expression, in function returning void");
10103 if (warned_here)
10104 inform (DECL_SOURCE_LOCATION (current_function_decl),
10105 "declared here");
10107 else
10109 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10110 retval, origtype, ic_return,
10111 npc, NULL_TREE, NULL_TREE, 0);
10112 tree res = DECL_RESULT (current_function_decl);
10113 tree inner;
10114 bool save;
10116 current_function_returns_value = 1;
10117 if (t == error_mark_node)
10118 return NULL_TREE;
10120 save = in_late_binary_op;
10121 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10122 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10123 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10124 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10125 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10126 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10127 in_late_binary_op = true;
10128 inner = t = convert (TREE_TYPE (res), t);
10129 in_late_binary_op = save;
10131 /* Strip any conversions, additions, and subtractions, and see if
10132 we are returning the address of a local variable. Warn if so. */
10133 while (1)
10135 switch (TREE_CODE (inner))
10137 CASE_CONVERT:
10138 case NON_LVALUE_EXPR:
10139 case PLUS_EXPR:
10140 case POINTER_PLUS_EXPR:
10141 inner = TREE_OPERAND (inner, 0);
10142 continue;
10144 case MINUS_EXPR:
10145 /* If the second operand of the MINUS_EXPR has a pointer
10146 type (or is converted from it), this may be valid, so
10147 don't give a warning. */
10149 tree op1 = TREE_OPERAND (inner, 1);
10151 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10152 && (CONVERT_EXPR_P (op1)
10153 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10154 op1 = TREE_OPERAND (op1, 0);
10156 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10157 break;
10159 inner = TREE_OPERAND (inner, 0);
10160 continue;
10163 case ADDR_EXPR:
10164 inner = TREE_OPERAND (inner, 0);
10166 while (REFERENCE_CLASS_P (inner)
10167 && !INDIRECT_REF_P (inner))
10168 inner = TREE_OPERAND (inner, 0);
10170 if (DECL_P (inner)
10171 && !DECL_EXTERNAL (inner)
10172 && !TREE_STATIC (inner)
10173 && DECL_CONTEXT (inner) == current_function_decl)
10175 if (TREE_CODE (inner) == LABEL_DECL)
10176 warning_at (loc, OPT_Wreturn_local_addr,
10177 "function returns address of label");
10178 else
10180 warning_at (loc, OPT_Wreturn_local_addr,
10181 "function returns address of local variable");
10182 tree zero = build_zero_cst (TREE_TYPE (res));
10183 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10186 break;
10188 default:
10189 break;
10192 break;
10195 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10196 SET_EXPR_LOCATION (retval, loc);
10198 if (warn_sequence_point)
10199 verify_sequence_points (retval);
10202 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10203 TREE_NO_WARNING (ret_stmt) |= no_warning;
10204 return add_stmt (ret_stmt);
10207 struct c_switch {
10208 /* The SWITCH_EXPR being built. */
10209 tree switch_expr;
10211 /* The original type of the testing expression, i.e. before the
10212 default conversion is applied. */
10213 tree orig_type;
10215 /* A splay-tree mapping the low element of a case range to the high
10216 element, or NULL_TREE if there is no high element. Used to
10217 determine whether or not a new case label duplicates an old case
10218 label. We need a tree, rather than simply a hash table, because
10219 of the GNU case range extension. */
10220 splay_tree cases;
10222 /* The bindings at the point of the switch. This is used for
10223 warnings crossing decls when branching to a case label. */
10224 struct c_spot_bindings *bindings;
10226 /* The next node on the stack. */
10227 struct c_switch *next;
10229 /* Remember whether the controlling expression had boolean type
10230 before integer promotions for the sake of -Wswitch-bool. */
10231 bool bool_cond_p;
10233 /* Remember whether there was a case value that is outside the
10234 range of the ORIG_TYPE. */
10235 bool outside_range_p;
10238 /* A stack of the currently active switch statements. The innermost
10239 switch statement is on the top of the stack. There is no need to
10240 mark the stack for garbage collection because it is only active
10241 during the processing of the body of a function, and we never
10242 collect at that point. */
10244 struct c_switch *c_switch_stack;
10246 /* Start a C switch statement, testing expression EXP. Return the new
10247 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10248 SWITCH_COND_LOC is the location of the switch's condition.
10249 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10251 tree
10252 c_start_case (location_t switch_loc,
10253 location_t switch_cond_loc,
10254 tree exp, bool explicit_cast_p)
10256 tree orig_type = error_mark_node;
10257 bool bool_cond_p = false;
10258 struct c_switch *cs;
10260 if (exp != error_mark_node)
10262 orig_type = TREE_TYPE (exp);
10264 if (!INTEGRAL_TYPE_P (orig_type))
10266 if (orig_type != error_mark_node)
10268 error_at (switch_cond_loc, "switch quantity not an integer");
10269 orig_type = error_mark_node;
10271 exp = integer_zero_node;
10273 else
10275 tree type = TYPE_MAIN_VARIANT (orig_type);
10276 tree e = exp;
10278 /* Warn if the condition has boolean value. */
10279 while (TREE_CODE (e) == COMPOUND_EXPR)
10280 e = TREE_OPERAND (e, 1);
10282 if ((TREE_CODE (type) == BOOLEAN_TYPE
10283 || truth_value_p (TREE_CODE (e)))
10284 /* Explicit cast to int suppresses this warning. */
10285 && !(TREE_CODE (type) == INTEGER_TYPE
10286 && explicit_cast_p))
10287 bool_cond_p = true;
10289 if (!in_system_header_at (input_location)
10290 && (type == long_integer_type_node
10291 || type == long_unsigned_type_node))
10292 warning_at (switch_cond_loc,
10293 OPT_Wtraditional, "%<long%> switch expression not "
10294 "converted to %<int%> in ISO C");
10296 exp = c_fully_fold (exp, false, NULL);
10297 exp = default_conversion (exp);
10299 if (warn_sequence_point)
10300 verify_sequence_points (exp);
10304 /* Add this new SWITCH_EXPR to the stack. */
10305 cs = XNEW (struct c_switch);
10306 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10307 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10308 cs->orig_type = orig_type;
10309 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10310 cs->bindings = c_get_switch_bindings ();
10311 cs->bool_cond_p = bool_cond_p;
10312 cs->outside_range_p = false;
10313 cs->next = c_switch_stack;
10314 c_switch_stack = cs;
10316 return add_stmt (cs->switch_expr);
10319 /* Process a case label at location LOC. */
10321 tree
10322 do_case (location_t loc, tree low_value, tree high_value)
10324 tree label = NULL_TREE;
10326 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10328 low_value = c_fully_fold (low_value, false, NULL);
10329 if (TREE_CODE (low_value) == INTEGER_CST)
10330 pedwarn (loc, OPT_Wpedantic,
10331 "case label is not an integer constant expression");
10334 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10336 high_value = c_fully_fold (high_value, false, NULL);
10337 if (TREE_CODE (high_value) == INTEGER_CST)
10338 pedwarn (input_location, OPT_Wpedantic,
10339 "case label is not an integer constant expression");
10342 if (c_switch_stack == NULL)
10344 if (low_value)
10345 error_at (loc, "case label not within a switch statement");
10346 else
10347 error_at (loc, "%<default%> label not within a switch statement");
10348 return NULL_TREE;
10351 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10352 EXPR_LOCATION (c_switch_stack->switch_expr),
10353 loc))
10354 return NULL_TREE;
10356 label = c_add_case_label (loc, c_switch_stack->cases,
10357 SWITCH_COND (c_switch_stack->switch_expr),
10358 c_switch_stack->orig_type,
10359 low_value, high_value,
10360 &c_switch_stack->outside_range_p);
10361 if (label == error_mark_node)
10362 label = NULL_TREE;
10363 return label;
10366 /* Finish the switch statement. TYPE is the original type of the
10367 controlling expression of the switch, or NULL_TREE. */
10369 void
10370 c_finish_case (tree body, tree type)
10372 struct c_switch *cs = c_switch_stack;
10373 location_t switch_location;
10375 SWITCH_BODY (cs->switch_expr) = body;
10377 /* Emit warnings as needed. */
10378 switch_location = EXPR_LOCATION (cs->switch_expr);
10379 c_do_switch_warnings (cs->cases, switch_location,
10380 type ? type : TREE_TYPE (cs->switch_expr),
10381 SWITCH_COND (cs->switch_expr),
10382 cs->bool_cond_p, cs->outside_range_p);
10383 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10384 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10386 /* Pop the stack. */
10387 c_switch_stack = cs->next;
10388 splay_tree_delete (cs->cases);
10389 c_release_switch_bindings (cs->bindings);
10390 XDELETE (cs);
10393 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10394 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10395 may be null. */
10397 void
10398 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10399 tree else_block)
10401 tree stmt;
10403 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10404 SET_EXPR_LOCATION (stmt, if_locus);
10405 add_stmt (stmt);
10408 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10409 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10410 is false for DO loops. INCR is the FOR increment expression. BODY is
10411 the statement controlled by the loop. BLAB is the break label. CLAB is
10412 the continue label. Everything is allowed to be NULL. */
10414 void
10415 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10416 tree blab, tree clab, bool cond_is_first)
10418 tree entry = NULL, exit = NULL, t;
10420 /* If the condition is zero don't generate a loop construct. */
10421 if (cond && integer_zerop (cond))
10423 if (cond_is_first)
10425 t = build_and_jump (&blab);
10426 SET_EXPR_LOCATION (t, start_locus);
10427 add_stmt (t);
10430 else
10432 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10434 /* If we have an exit condition, then we build an IF with gotos either
10435 out of the loop, or to the top of it. If there's no exit condition,
10436 then we just build a jump back to the top. */
10437 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10439 if (cond && !integer_nonzerop (cond))
10441 /* Canonicalize the loop condition to the end. This means
10442 generating a branch to the loop condition. Reuse the
10443 continue label, if possible. */
10444 if (cond_is_first)
10446 if (incr || !clab)
10448 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10449 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10451 else
10452 t = build1 (GOTO_EXPR, void_type_node, clab);
10453 SET_EXPR_LOCATION (t, start_locus);
10454 add_stmt (t);
10457 t = build_and_jump (&blab);
10458 if (cond_is_first)
10459 exit = fold_build3_loc (start_locus,
10460 COND_EXPR, void_type_node, cond, exit, t);
10461 else
10462 exit = fold_build3_loc (input_location,
10463 COND_EXPR, void_type_node, cond, exit, t);
10465 else
10467 /* For the backward-goto's location of an unconditional loop
10468 use the beginning of the body, or, if there is none, the
10469 top of the loop. */
10470 location_t loc = EXPR_LOCATION (expr_first (body));
10471 if (loc == UNKNOWN_LOCATION)
10472 loc = start_locus;
10473 SET_EXPR_LOCATION (exit, loc);
10476 add_stmt (top);
10479 if (body)
10480 add_stmt (body);
10481 if (clab)
10482 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10483 if (incr)
10484 add_stmt (incr);
10485 if (entry)
10486 add_stmt (entry);
10487 if (exit)
10488 add_stmt (exit);
10489 if (blab)
10490 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10493 tree
10494 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10496 bool skip;
10497 tree label = *label_p;
10499 /* In switch statements break is sometimes stylistically used after
10500 a return statement. This can lead to spurious warnings about
10501 control reaching the end of a non-void function when it is
10502 inlined. Note that we are calling block_may_fallthru with
10503 language specific tree nodes; this works because
10504 block_may_fallthru returns true when given something it does not
10505 understand. */
10506 skip = !block_may_fallthru (cur_stmt_list);
10508 if (!label)
10510 if (!skip)
10511 *label_p = label = create_artificial_label (loc);
10513 else if (TREE_CODE (label) == LABEL_DECL)
10515 else switch (TREE_INT_CST_LOW (label))
10517 case 0:
10518 if (is_break)
10519 error_at (loc, "break statement not within loop or switch");
10520 else
10521 error_at (loc, "continue statement not within a loop");
10522 return NULL_TREE;
10524 case 1:
10525 gcc_assert (is_break);
10526 error_at (loc, "break statement used with OpenMP for loop");
10527 return NULL_TREE;
10529 case 2:
10530 if (is_break)
10531 error ("break statement within %<#pragma simd%> loop body");
10532 else
10533 error ("continue statement within %<#pragma simd%> loop body");
10534 return NULL_TREE;
10536 default:
10537 gcc_unreachable ();
10540 if (skip)
10541 return NULL_TREE;
10543 if (!is_break)
10544 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10546 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10549 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10551 static void
10552 emit_side_effect_warnings (location_t loc, tree expr)
10554 if (expr == error_mark_node)
10556 else if (!TREE_SIDE_EFFECTS (expr))
10558 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10559 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10561 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10563 tree r = expr;
10564 location_t cloc = loc;
10565 while (TREE_CODE (r) == COMPOUND_EXPR)
10567 if (EXPR_HAS_LOCATION (r))
10568 cloc = EXPR_LOCATION (r);
10569 r = TREE_OPERAND (r, 1);
10571 if (!TREE_SIDE_EFFECTS (r)
10572 && !VOID_TYPE_P (TREE_TYPE (r))
10573 && !CONVERT_EXPR_P (r)
10574 && !TREE_NO_WARNING (r)
10575 && !TREE_NO_WARNING (expr))
10576 warning_at (cloc, OPT_Wunused_value,
10577 "right-hand operand of comma expression has no effect");
10579 else
10580 warn_if_unused_value (expr, loc);
10583 /* Process an expression as if it were a complete statement. Emit
10584 diagnostics, but do not call ADD_STMT. LOC is the location of the
10585 statement. */
10587 tree
10588 c_process_expr_stmt (location_t loc, tree expr)
10590 tree exprv;
10592 if (!expr)
10593 return NULL_TREE;
10595 expr = c_fully_fold (expr, false, NULL);
10597 if (warn_sequence_point)
10598 verify_sequence_points (expr);
10600 if (TREE_TYPE (expr) != error_mark_node
10601 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10602 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10603 error_at (loc, "expression statement has incomplete type");
10605 /* If we're not processing a statement expression, warn about unused values.
10606 Warnings for statement expressions will be emitted later, once we figure
10607 out which is the result. */
10608 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10609 && warn_unused_value)
10610 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10612 exprv = expr;
10613 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10614 exprv = TREE_OPERAND (exprv, 1);
10615 while (CONVERT_EXPR_P (exprv))
10616 exprv = TREE_OPERAND (exprv, 0);
10617 if (DECL_P (exprv)
10618 || handled_component_p (exprv)
10619 || TREE_CODE (exprv) == ADDR_EXPR)
10620 mark_exp_read (exprv);
10622 /* If the expression is not of a type to which we cannot assign a line
10623 number, wrap the thing in a no-op NOP_EXPR. */
10624 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10626 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10627 SET_EXPR_LOCATION (expr, loc);
10630 return expr;
10633 /* Emit an expression as a statement. LOC is the location of the
10634 expression. */
10636 tree
10637 c_finish_expr_stmt (location_t loc, tree expr)
10639 if (expr)
10640 return add_stmt (c_process_expr_stmt (loc, expr));
10641 else
10642 return NULL;
10645 /* Do the opposite and emit a statement as an expression. To begin,
10646 create a new binding level and return it. */
10648 tree
10649 c_begin_stmt_expr (void)
10651 tree ret;
10653 /* We must force a BLOCK for this level so that, if it is not expanded
10654 later, there is a way to turn off the entire subtree of blocks that
10655 are contained in it. */
10656 keep_next_level ();
10657 ret = c_begin_compound_stmt (true);
10659 c_bindings_start_stmt_expr (c_switch_stack == NULL
10660 ? NULL
10661 : c_switch_stack->bindings);
10663 /* Mark the current statement list as belonging to a statement list. */
10664 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10666 return ret;
10669 /* LOC is the location of the compound statement to which this body
10670 belongs. */
10672 tree
10673 c_finish_stmt_expr (location_t loc, tree body)
10675 tree last, type, tmp, val;
10676 tree *last_p;
10678 body = c_end_compound_stmt (loc, body, true);
10680 c_bindings_end_stmt_expr (c_switch_stack == NULL
10681 ? NULL
10682 : c_switch_stack->bindings);
10684 /* Locate the last statement in BODY. See c_end_compound_stmt
10685 about always returning a BIND_EXPR. */
10686 last_p = &BIND_EXPR_BODY (body);
10687 last = BIND_EXPR_BODY (body);
10689 continue_searching:
10690 if (TREE_CODE (last) == STATEMENT_LIST)
10692 tree_stmt_iterator i;
10694 /* This can happen with degenerate cases like ({ }). No value. */
10695 if (!TREE_SIDE_EFFECTS (last))
10696 return body;
10698 /* If we're supposed to generate side effects warnings, process
10699 all of the statements except the last. */
10700 if (warn_unused_value)
10702 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10704 location_t tloc;
10705 tree t = tsi_stmt (i);
10707 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10708 emit_side_effect_warnings (tloc, t);
10711 else
10712 i = tsi_last (last);
10713 if (TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
10715 tsi_prev (&i);
10716 while (TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT);
10717 last_p = tsi_stmt_ptr (i);
10718 last = *last_p;
10721 /* If the end of the list is exception related, then the list was split
10722 by a call to push_cleanup. Continue searching. */
10723 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10724 || TREE_CODE (last) == TRY_CATCH_EXPR)
10726 last_p = &TREE_OPERAND (last, 0);
10727 last = *last_p;
10728 goto continue_searching;
10731 if (last == error_mark_node)
10732 return last;
10734 /* In the case that the BIND_EXPR is not necessary, return the
10735 expression out from inside it. */
10736 if ((last == BIND_EXPR_BODY (body)
10737 /* Skip nested debug stmts. */
10738 || last == expr_first (BIND_EXPR_BODY (body)))
10739 && BIND_EXPR_VARS (body) == NULL)
10741 /* Even if this looks constant, do not allow it in a constant
10742 expression. */
10743 last = c_wrap_maybe_const (last, true);
10744 /* Do not warn if the return value of a statement expression is
10745 unused. */
10746 TREE_NO_WARNING (last) = 1;
10747 return last;
10750 /* Extract the type of said expression. */
10751 type = TREE_TYPE (last);
10753 /* If we're not returning a value at all, then the BIND_EXPR that
10754 we already have is a fine expression to return. */
10755 if (!type || VOID_TYPE_P (type))
10756 return body;
10758 /* Now that we've located the expression containing the value, it seems
10759 silly to make voidify_wrapper_expr repeat the process. Create a
10760 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10761 tmp = create_tmp_var_raw (type);
10763 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10764 tree_expr_nonnegative_p giving up immediately. */
10765 val = last;
10766 if (TREE_CODE (val) == NOP_EXPR
10767 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10768 val = TREE_OPERAND (val, 0);
10770 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10771 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10774 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10775 SET_EXPR_LOCATION (t, loc);
10776 return t;
10780 /* Begin and end compound statements. This is as simple as pushing
10781 and popping new statement lists from the tree. */
10783 tree
10784 c_begin_compound_stmt (bool do_scope)
10786 tree stmt = push_stmt_list ();
10787 if (do_scope)
10788 push_scope ();
10789 return stmt;
10792 /* End a compound statement. STMT is the statement. LOC is the
10793 location of the compound statement-- this is usually the location
10794 of the opening brace. */
10796 tree
10797 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10799 tree block = NULL;
10801 if (do_scope)
10803 if (c_dialect_objc ())
10804 objc_clear_super_receiver ();
10805 block = pop_scope ();
10808 stmt = pop_stmt_list (stmt);
10809 stmt = c_build_bind_expr (loc, block, stmt);
10811 /* If this compound statement is nested immediately inside a statement
10812 expression, then force a BIND_EXPR to be created. Otherwise we'll
10813 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10814 STATEMENT_LISTs merge, and thus we can lose track of what statement
10815 was really last. */
10816 if (building_stmt_list_p ()
10817 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10818 && TREE_CODE (stmt) != BIND_EXPR)
10820 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10821 TREE_SIDE_EFFECTS (stmt) = 1;
10822 SET_EXPR_LOCATION (stmt, loc);
10825 return stmt;
10828 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10829 when the current scope is exited. EH_ONLY is true when this is not
10830 meant to apply to normal control flow transfer. */
10832 void
10833 push_cleanup (tree decl, tree cleanup, bool eh_only)
10835 enum tree_code code;
10836 tree stmt, list;
10837 bool stmt_expr;
10839 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10840 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10841 add_stmt (stmt);
10842 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10843 list = push_stmt_list ();
10844 TREE_OPERAND (stmt, 0) = list;
10845 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10848 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10849 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10851 static tree
10852 build_vec_cmp (tree_code code, tree type,
10853 tree arg0, tree arg1)
10855 tree zero_vec = build_zero_cst (type);
10856 tree minus_one_vec = build_minus_one_cst (type);
10857 tree cmp_type = build_same_sized_truth_vector_type (type);
10858 tree cmp = build2 (code, cmp_type, arg0, arg1);
10859 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10862 /* Build a binary-operation expression without default conversions.
10863 CODE is the kind of expression to build.
10864 LOCATION is the operator's location.
10865 This function differs from `build' in several ways:
10866 the data type of the result is computed and recorded in it,
10867 warnings are generated if arg data types are invalid,
10868 special handling for addition and subtraction of pointers is known,
10869 and some optimization is done (operations on narrow ints
10870 are done in the narrower type when that gives the same result).
10871 Constant folding is also done before the result is returned.
10873 Note that the operands will never have enumeral types, or function
10874 or array types, because either they will have the default conversions
10875 performed or they have both just been converted to some other type in which
10876 the arithmetic is to be done. */
10878 tree
10879 build_binary_op (location_t location, enum tree_code code,
10880 tree orig_op0, tree orig_op1, bool convert_p)
10882 tree type0, type1, orig_type0, orig_type1;
10883 tree eptype;
10884 enum tree_code code0, code1;
10885 tree op0, op1;
10886 tree ret = error_mark_node;
10887 const char *invalid_op_diag;
10888 bool op0_int_operands, op1_int_operands;
10889 bool int_const, int_const_or_overflow, int_operands;
10891 /* Expression code to give to the expression when it is built.
10892 Normally this is CODE, which is what the caller asked for,
10893 but in some special cases we change it. */
10894 enum tree_code resultcode = code;
10896 /* Data type in which the computation is to be performed.
10897 In the simplest cases this is the common type of the arguments. */
10898 tree result_type = NULL;
10900 /* When the computation is in excess precision, the type of the
10901 final EXCESS_PRECISION_EXPR. */
10902 tree semantic_result_type = NULL;
10904 /* Nonzero means operands have already been type-converted
10905 in whatever way is necessary.
10906 Zero means they need to be converted to RESULT_TYPE. */
10907 int converted = 0;
10909 /* Nonzero means create the expression with this type, rather than
10910 RESULT_TYPE. */
10911 tree build_type = NULL_TREE;
10913 /* Nonzero means after finally constructing the expression
10914 convert it to this type. */
10915 tree final_type = NULL_TREE;
10917 /* Nonzero if this is an operation like MIN or MAX which can
10918 safely be computed in short if both args are promoted shorts.
10919 Also implies COMMON.
10920 -1 indicates a bitwise operation; this makes a difference
10921 in the exact conditions for when it is safe to do the operation
10922 in a narrower mode. */
10923 int shorten = 0;
10925 /* Nonzero if this is a comparison operation;
10926 if both args are promoted shorts, compare the original shorts.
10927 Also implies COMMON. */
10928 int short_compare = 0;
10930 /* Nonzero if this is a right-shift operation, which can be computed on the
10931 original short and then promoted if the operand is a promoted short. */
10932 int short_shift = 0;
10934 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10935 int common = 0;
10937 /* True means types are compatible as far as ObjC is concerned. */
10938 bool objc_ok;
10940 /* True means this is an arithmetic operation that may need excess
10941 precision. */
10942 bool may_need_excess_precision;
10944 /* True means this is a boolean operation that converts both its
10945 operands to truth-values. */
10946 bool boolean_op = false;
10948 /* Remember whether we're doing / or %. */
10949 bool doing_div_or_mod = false;
10951 /* Remember whether we're doing << or >>. */
10952 bool doing_shift = false;
10954 /* Tree holding instrumentation expression. */
10955 tree instrument_expr = NULL;
10957 if (location == UNKNOWN_LOCATION)
10958 location = input_location;
10960 op0 = orig_op0;
10961 op1 = orig_op1;
10963 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10964 if (op0_int_operands)
10965 op0 = remove_c_maybe_const_expr (op0);
10966 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10967 if (op1_int_operands)
10968 op1 = remove_c_maybe_const_expr (op1);
10969 int_operands = (op0_int_operands && op1_int_operands);
10970 if (int_operands)
10972 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10973 && TREE_CODE (orig_op1) == INTEGER_CST);
10974 int_const = (int_const_or_overflow
10975 && !TREE_OVERFLOW (orig_op0)
10976 && !TREE_OVERFLOW (orig_op1));
10978 else
10979 int_const = int_const_or_overflow = false;
10981 /* Do not apply default conversion in mixed vector/scalar expression. */
10982 if (convert_p
10983 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10985 op0 = default_conversion (op0);
10986 op1 = default_conversion (op1);
10989 orig_type0 = type0 = TREE_TYPE (op0);
10991 orig_type1 = type1 = TREE_TYPE (op1);
10993 /* The expression codes of the data types of the arguments tell us
10994 whether the arguments are integers, floating, pointers, etc. */
10995 code0 = TREE_CODE (type0);
10996 code1 = TREE_CODE (type1);
10998 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10999 STRIP_TYPE_NOPS (op0);
11000 STRIP_TYPE_NOPS (op1);
11002 /* If an error was already reported for one of the arguments,
11003 avoid reporting another error. */
11005 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11006 return error_mark_node;
11008 if (code0 == POINTER_TYPE
11009 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11010 return error_mark_node;
11012 if (code1 == POINTER_TYPE
11013 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11014 return error_mark_node;
11016 if ((invalid_op_diag
11017 = targetm.invalid_binary_op (code, type0, type1)))
11019 error_at (location, invalid_op_diag);
11020 return error_mark_node;
11023 switch (code)
11025 case PLUS_EXPR:
11026 case MINUS_EXPR:
11027 case MULT_EXPR:
11028 case TRUNC_DIV_EXPR:
11029 case CEIL_DIV_EXPR:
11030 case FLOOR_DIV_EXPR:
11031 case ROUND_DIV_EXPR:
11032 case EXACT_DIV_EXPR:
11033 may_need_excess_precision = true;
11034 break;
11035 default:
11036 may_need_excess_precision = false;
11037 break;
11039 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11041 op0 = TREE_OPERAND (op0, 0);
11042 type0 = TREE_TYPE (op0);
11044 else if (may_need_excess_precision
11045 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11047 type0 = eptype;
11048 op0 = convert (eptype, op0);
11050 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11052 op1 = TREE_OPERAND (op1, 0);
11053 type1 = TREE_TYPE (op1);
11055 else if (may_need_excess_precision
11056 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11058 type1 = eptype;
11059 op1 = convert (eptype, op1);
11062 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11064 /* In case when one of the operands of the binary operation is
11065 a vector and another is a scalar -- convert scalar to vector. */
11066 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11068 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11069 true);
11071 switch (convert_flag)
11073 case stv_error:
11074 return error_mark_node;
11075 case stv_firstarg:
11077 bool maybe_const = true;
11078 tree sc;
11079 sc = c_fully_fold (op0, false, &maybe_const);
11080 sc = save_expr (sc);
11081 sc = convert (TREE_TYPE (type1), sc);
11082 op0 = build_vector_from_val (type1, sc);
11083 if (!maybe_const)
11084 op0 = c_wrap_maybe_const (op0, true);
11085 orig_type0 = type0 = TREE_TYPE (op0);
11086 code0 = TREE_CODE (type0);
11087 converted = 1;
11088 break;
11090 case stv_secondarg:
11092 bool maybe_const = true;
11093 tree sc;
11094 sc = c_fully_fold (op1, false, &maybe_const);
11095 sc = save_expr (sc);
11096 sc = convert (TREE_TYPE (type0), sc);
11097 op1 = build_vector_from_val (type0, sc);
11098 if (!maybe_const)
11099 op1 = c_wrap_maybe_const (op1, true);
11100 orig_type1 = type1 = TREE_TYPE (op1);
11101 code1 = TREE_CODE (type1);
11102 converted = 1;
11103 break;
11105 default:
11106 break;
11110 switch (code)
11112 case PLUS_EXPR:
11113 /* Handle the pointer + int case. */
11114 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11116 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11117 goto return_build_binary_op;
11119 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11121 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11122 goto return_build_binary_op;
11124 else
11125 common = 1;
11126 break;
11128 case MINUS_EXPR:
11129 /* Subtraction of two similar pointers.
11130 We must subtract them as integers, then divide by object size. */
11131 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11132 && comp_target_types (location, type0, type1))
11134 ret = pointer_diff (location, op0, op1, &instrument_expr);
11135 goto return_build_binary_op;
11137 /* Handle pointer minus int. Just like pointer plus int. */
11138 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11140 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11141 goto return_build_binary_op;
11143 else
11144 common = 1;
11145 break;
11147 case MULT_EXPR:
11148 common = 1;
11149 break;
11151 case TRUNC_DIV_EXPR:
11152 case CEIL_DIV_EXPR:
11153 case FLOOR_DIV_EXPR:
11154 case ROUND_DIV_EXPR:
11155 case EXACT_DIV_EXPR:
11156 doing_div_or_mod = true;
11157 warn_for_div_by_zero (location, op1);
11159 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11160 || code0 == FIXED_POINT_TYPE
11161 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11162 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11163 || code1 == FIXED_POINT_TYPE
11164 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11166 enum tree_code tcode0 = code0, tcode1 = code1;
11168 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11169 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11170 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11171 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11173 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11174 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11175 resultcode = RDIV_EXPR;
11176 else
11177 /* Although it would be tempting to shorten always here, that
11178 loses on some targets, since the modulo instruction is
11179 undefined if the quotient can't be represented in the
11180 computation mode. We shorten only if unsigned or if
11181 dividing by something we know != -1. */
11182 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11183 || (TREE_CODE (op1) == INTEGER_CST
11184 && !integer_all_onesp (op1)));
11185 common = 1;
11187 break;
11189 case BIT_AND_EXPR:
11190 case BIT_IOR_EXPR:
11191 case BIT_XOR_EXPR:
11192 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11193 shorten = -1;
11194 /* Allow vector types which are not floating point types. */
11195 else if (code0 == VECTOR_TYPE
11196 && code1 == VECTOR_TYPE
11197 && !VECTOR_FLOAT_TYPE_P (type0)
11198 && !VECTOR_FLOAT_TYPE_P (type1))
11199 common = 1;
11200 break;
11202 case TRUNC_MOD_EXPR:
11203 case FLOOR_MOD_EXPR:
11204 doing_div_or_mod = true;
11205 warn_for_div_by_zero (location, op1);
11207 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11208 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11209 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11210 common = 1;
11211 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11213 /* Although it would be tempting to shorten always here, that loses
11214 on some targets, since the modulo instruction is undefined if the
11215 quotient can't be represented in the computation mode. We shorten
11216 only if unsigned or if dividing by something we know != -1. */
11217 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11218 || (TREE_CODE (op1) == INTEGER_CST
11219 && !integer_all_onesp (op1)));
11220 common = 1;
11222 break;
11224 case TRUTH_ANDIF_EXPR:
11225 case TRUTH_ORIF_EXPR:
11226 case TRUTH_AND_EXPR:
11227 case TRUTH_OR_EXPR:
11228 case TRUTH_XOR_EXPR:
11229 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11230 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11231 || code0 == FIXED_POINT_TYPE)
11232 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11233 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11234 || code1 == FIXED_POINT_TYPE))
11236 /* Result of these operations is always an int,
11237 but that does not mean the operands should be
11238 converted to ints! */
11239 result_type = integer_type_node;
11240 if (op0_int_operands)
11242 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11243 op0 = remove_c_maybe_const_expr (op0);
11245 else
11246 op0 = c_objc_common_truthvalue_conversion (location, op0);
11247 if (op1_int_operands)
11249 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11250 op1 = remove_c_maybe_const_expr (op1);
11252 else
11253 op1 = c_objc_common_truthvalue_conversion (location, op1);
11254 converted = 1;
11255 boolean_op = true;
11257 if (code == TRUTH_ANDIF_EXPR)
11259 int_const_or_overflow = (int_operands
11260 && TREE_CODE (orig_op0) == INTEGER_CST
11261 && (op0 == truthvalue_false_node
11262 || TREE_CODE (orig_op1) == INTEGER_CST));
11263 int_const = (int_const_or_overflow
11264 && !TREE_OVERFLOW (orig_op0)
11265 && (op0 == truthvalue_false_node
11266 || !TREE_OVERFLOW (orig_op1)));
11268 else if (code == TRUTH_ORIF_EXPR)
11270 int_const_or_overflow = (int_operands
11271 && TREE_CODE (orig_op0) == INTEGER_CST
11272 && (op0 == truthvalue_true_node
11273 || TREE_CODE (orig_op1) == INTEGER_CST));
11274 int_const = (int_const_or_overflow
11275 && !TREE_OVERFLOW (orig_op0)
11276 && (op0 == truthvalue_true_node
11277 || !TREE_OVERFLOW (orig_op1)));
11279 break;
11281 /* Shift operations: result has same type as first operand;
11282 always convert second operand to int.
11283 Also set SHORT_SHIFT if shifting rightward. */
11285 case RSHIFT_EXPR:
11286 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11287 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11288 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11289 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11291 result_type = type0;
11292 converted = 1;
11294 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11295 || code0 == VECTOR_TYPE)
11296 && code1 == INTEGER_TYPE)
11298 doing_shift = true;
11299 if (TREE_CODE (op1) == INTEGER_CST)
11301 if (tree_int_cst_sgn (op1) < 0)
11303 int_const = false;
11304 if (c_inhibit_evaluation_warnings == 0)
11305 warning_at (location, OPT_Wshift_count_negative,
11306 "right shift count is negative");
11308 else if (code0 == VECTOR_TYPE)
11310 if (compare_tree_int (op1,
11311 TYPE_PRECISION (TREE_TYPE (type0)))
11312 >= 0)
11314 int_const = false;
11315 if (c_inhibit_evaluation_warnings == 0)
11316 warning_at (location, OPT_Wshift_count_overflow,
11317 "right shift count >= width of vector element");
11320 else
11322 if (!integer_zerop (op1))
11323 short_shift = 1;
11325 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11327 int_const = false;
11328 if (c_inhibit_evaluation_warnings == 0)
11329 warning_at (location, OPT_Wshift_count_overflow,
11330 "right shift count >= width of type");
11335 /* Use the type of the value to be shifted. */
11336 result_type = type0;
11337 /* Avoid converting op1 to result_type later. */
11338 converted = 1;
11340 break;
11342 case LSHIFT_EXPR:
11343 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11344 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11345 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11346 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11348 result_type = type0;
11349 converted = 1;
11351 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11352 || code0 == VECTOR_TYPE)
11353 && code1 == INTEGER_TYPE)
11355 doing_shift = true;
11356 if (TREE_CODE (op0) == INTEGER_CST
11357 && tree_int_cst_sgn (op0) < 0)
11359 /* Don't reject a left shift of a negative value in a context
11360 where a constant expression is needed in C90. */
11361 if (flag_isoc99)
11362 int_const = false;
11363 if (c_inhibit_evaluation_warnings == 0)
11364 warning_at (location, OPT_Wshift_negative_value,
11365 "left shift of negative value");
11367 if (TREE_CODE (op1) == INTEGER_CST)
11369 if (tree_int_cst_sgn (op1) < 0)
11371 int_const = false;
11372 if (c_inhibit_evaluation_warnings == 0)
11373 warning_at (location, OPT_Wshift_count_negative,
11374 "left shift count is negative");
11376 else if (code0 == VECTOR_TYPE)
11378 if (compare_tree_int (op1,
11379 TYPE_PRECISION (TREE_TYPE (type0)))
11380 >= 0)
11382 int_const = false;
11383 if (c_inhibit_evaluation_warnings == 0)
11384 warning_at (location, OPT_Wshift_count_overflow,
11385 "left shift count >= width of vector element");
11388 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11390 int_const = false;
11391 if (c_inhibit_evaluation_warnings == 0)
11392 warning_at (location, OPT_Wshift_count_overflow,
11393 "left shift count >= width of type");
11395 else if (TREE_CODE (op0) == INTEGER_CST
11396 && maybe_warn_shift_overflow (location, op0, op1)
11397 && flag_isoc99)
11398 int_const = false;
11401 /* Use the type of the value to be shifted. */
11402 result_type = type0;
11403 /* Avoid converting op1 to result_type later. */
11404 converted = 1;
11406 break;
11408 case EQ_EXPR:
11409 case NE_EXPR:
11410 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11412 tree intt;
11413 if (!vector_types_compatible_elements_p (type0, type1))
11415 error_at (location, "comparing vectors with different "
11416 "element types");
11417 return error_mark_node;
11420 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11422 error_at (location, "comparing vectors with different "
11423 "number of elements");
11424 return error_mark_node;
11427 /* It's not precisely specified how the usual arithmetic
11428 conversions apply to the vector types. Here, we use
11429 the unsigned type if one of the operands is signed and
11430 the other one is unsigned. */
11431 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11433 if (!TYPE_UNSIGNED (type0))
11434 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11435 else
11436 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11437 warning_at (location, OPT_Wsign_compare, "comparison between "
11438 "types %qT and %qT", type0, type1);
11441 /* Always construct signed integer vector type. */
11442 intt = c_common_type_for_size (GET_MODE_BITSIZE
11443 (SCALAR_TYPE_MODE
11444 (TREE_TYPE (type0))), 0);
11445 result_type = build_opaque_vector_type (intt,
11446 TYPE_VECTOR_SUBPARTS (type0));
11447 converted = 1;
11448 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11449 goto return_build_binary_op;
11451 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11452 warning_at (location,
11453 OPT_Wfloat_equal,
11454 "comparing floating point with == or != is unsafe");
11455 /* Result of comparison is always int,
11456 but don't convert the args to int! */
11457 build_type = integer_type_node;
11458 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11459 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11460 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11461 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11462 short_compare = 1;
11463 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11465 if (TREE_CODE (op0) == ADDR_EXPR
11466 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11467 && !from_macro_expansion_at (location))
11469 if (code == EQ_EXPR)
11470 warning_at (location,
11471 OPT_Waddress,
11472 "the comparison will always evaluate as %<false%> "
11473 "for the address of %qD will never be NULL",
11474 TREE_OPERAND (op0, 0));
11475 else
11476 warning_at (location,
11477 OPT_Waddress,
11478 "the comparison will always evaluate as %<true%> "
11479 "for the address of %qD will never be NULL",
11480 TREE_OPERAND (op0, 0));
11482 result_type = type0;
11484 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11486 if (TREE_CODE (op1) == ADDR_EXPR
11487 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11488 && !from_macro_expansion_at (location))
11490 if (code == EQ_EXPR)
11491 warning_at (location,
11492 OPT_Waddress,
11493 "the comparison will always evaluate as %<false%> "
11494 "for the address of %qD will never be NULL",
11495 TREE_OPERAND (op1, 0));
11496 else
11497 warning_at (location,
11498 OPT_Waddress,
11499 "the comparison will always evaluate as %<true%> "
11500 "for the address of %qD will never be NULL",
11501 TREE_OPERAND (op1, 0));
11503 result_type = type1;
11505 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11507 tree tt0 = TREE_TYPE (type0);
11508 tree tt1 = TREE_TYPE (type1);
11509 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11510 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11511 addr_space_t as_common = ADDR_SPACE_GENERIC;
11513 /* Anything compares with void *. void * compares with anything.
11514 Otherwise, the targets must be compatible
11515 and both must be object or both incomplete. */
11516 if (comp_target_types (location, type0, type1))
11517 result_type = common_pointer_type (type0, type1);
11518 else if (!addr_space_superset (as0, as1, &as_common))
11520 error_at (location, "comparison of pointers to "
11521 "disjoint address spaces");
11522 return error_mark_node;
11524 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11526 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11527 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11528 "comparison of %<void *%> with function pointer");
11530 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11532 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11533 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11534 "comparison of %<void *%> with function pointer");
11536 else
11537 /* Avoid warning about the volatile ObjC EH puts on decls. */
11538 if (!objc_ok)
11539 pedwarn (location, 0,
11540 "comparison of distinct pointer types lacks a cast");
11542 if (result_type == NULL_TREE)
11544 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11545 result_type = build_pointer_type
11546 (build_qualified_type (void_type_node, qual));
11549 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11551 result_type = type0;
11552 pedwarn (location, 0, "comparison between pointer and integer");
11554 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11556 result_type = type1;
11557 pedwarn (location, 0, "comparison between pointer and integer");
11559 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11560 || truth_value_p (TREE_CODE (orig_op0)))
11561 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11562 || truth_value_p (TREE_CODE (orig_op1))))
11563 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11564 break;
11566 case LE_EXPR:
11567 case GE_EXPR:
11568 case LT_EXPR:
11569 case GT_EXPR:
11570 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11572 tree intt;
11573 if (!vector_types_compatible_elements_p (type0, type1))
11575 error_at (location, "comparing vectors with different "
11576 "element types");
11577 return error_mark_node;
11580 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11582 error_at (location, "comparing vectors with different "
11583 "number of elements");
11584 return error_mark_node;
11587 /* It's not precisely specified how the usual arithmetic
11588 conversions apply to the vector types. Here, we use
11589 the unsigned type if one of the operands is signed and
11590 the other one is unsigned. */
11591 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11593 if (!TYPE_UNSIGNED (type0))
11594 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11595 else
11596 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11597 warning_at (location, OPT_Wsign_compare, "comparison between "
11598 "types %qT and %qT", type0, type1);
11601 /* Always construct signed integer vector type. */
11602 intt = c_common_type_for_size (GET_MODE_BITSIZE
11603 (SCALAR_TYPE_MODE
11604 (TREE_TYPE (type0))), 0);
11605 result_type = build_opaque_vector_type (intt,
11606 TYPE_VECTOR_SUBPARTS (type0));
11607 converted = 1;
11608 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11609 goto return_build_binary_op;
11611 build_type = integer_type_node;
11612 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11613 || code0 == FIXED_POINT_TYPE)
11614 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11615 || code1 == FIXED_POINT_TYPE))
11616 short_compare = 1;
11617 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11619 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11620 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11621 addr_space_t as_common;
11623 if (comp_target_types (location, type0, type1))
11625 result_type = common_pointer_type (type0, type1);
11626 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11627 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11628 pedwarn (location, 0,
11629 "comparison of complete and incomplete pointers");
11630 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11631 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11632 "ordered comparisons of pointers to functions");
11633 else if (null_pointer_constant_p (orig_op0)
11634 || null_pointer_constant_p (orig_op1))
11635 warning_at (location, OPT_Wextra,
11636 "ordered comparison of pointer with null pointer");
11639 else if (!addr_space_superset (as0, as1, &as_common))
11641 error_at (location, "comparison of pointers to "
11642 "disjoint address spaces");
11643 return error_mark_node;
11645 else
11647 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11648 result_type = build_pointer_type
11649 (build_qualified_type (void_type_node, qual));
11650 pedwarn (location, 0,
11651 "comparison of distinct pointer types lacks a cast");
11654 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11656 result_type = type0;
11657 if (pedantic)
11658 pedwarn (location, OPT_Wpedantic,
11659 "ordered comparison of pointer with integer zero");
11660 else if (extra_warnings)
11661 warning_at (location, OPT_Wextra,
11662 "ordered comparison of pointer with integer zero");
11664 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11666 result_type = type1;
11667 if (pedantic)
11668 pedwarn (location, OPT_Wpedantic,
11669 "ordered comparison of pointer with integer zero");
11670 else if (extra_warnings)
11671 warning_at (location, OPT_Wextra,
11672 "ordered comparison of pointer with integer zero");
11674 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11676 result_type = type0;
11677 pedwarn (location, 0, "comparison between pointer and integer");
11679 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11681 result_type = type1;
11682 pedwarn (location, 0, "comparison between pointer and integer");
11685 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
11686 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
11688 op0 = save_expr (op0);
11689 op1 = save_expr (op1);
11691 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
11692 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
11695 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11696 || truth_value_p (TREE_CODE (orig_op0)))
11697 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11698 || truth_value_p (TREE_CODE (orig_op1))))
11699 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11700 break;
11702 default:
11703 gcc_unreachable ();
11706 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11707 return error_mark_node;
11709 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11710 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11711 || !vector_types_compatible_elements_p (type0, type1)))
11713 gcc_rich_location richloc (location);
11714 richloc.maybe_add_expr (orig_op0);
11715 richloc.maybe_add_expr (orig_op1);
11716 binary_op_error (&richloc, code, type0, type1);
11717 return error_mark_node;
11720 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11721 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11723 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11724 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11726 bool first_complex = (code0 == COMPLEX_TYPE);
11727 bool second_complex = (code1 == COMPLEX_TYPE);
11728 int none_complex = (!first_complex && !second_complex);
11730 if (shorten || common || short_compare)
11732 result_type = c_common_type (type0, type1);
11733 do_warn_double_promotion (result_type, type0, type1,
11734 "implicit conversion from %qT to %qT "
11735 "to match other operand of binary "
11736 "expression",
11737 location);
11738 if (result_type == error_mark_node)
11739 return error_mark_node;
11742 if (first_complex != second_complex
11743 && (code == PLUS_EXPR
11744 || code == MINUS_EXPR
11745 || code == MULT_EXPR
11746 || (code == TRUNC_DIV_EXPR && first_complex))
11747 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11748 && flag_signed_zeros)
11750 /* An operation on mixed real/complex operands must be
11751 handled specially, but the language-independent code can
11752 more easily optimize the plain complex arithmetic if
11753 -fno-signed-zeros. */
11754 tree real_type = TREE_TYPE (result_type);
11755 tree real, imag;
11756 if (type0 != orig_type0 || type1 != orig_type1)
11758 gcc_assert (may_need_excess_precision && common);
11759 semantic_result_type = c_common_type (orig_type0, orig_type1);
11761 if (first_complex)
11763 if (TREE_TYPE (op0) != result_type)
11764 op0 = convert_and_check (location, result_type, op0);
11765 if (TREE_TYPE (op1) != real_type)
11766 op1 = convert_and_check (location, real_type, op1);
11768 else
11770 if (TREE_TYPE (op0) != real_type)
11771 op0 = convert_and_check (location, real_type, op0);
11772 if (TREE_TYPE (op1) != result_type)
11773 op1 = convert_and_check (location, result_type, op1);
11775 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11776 return error_mark_node;
11777 if (first_complex)
11779 op0 = save_expr (op0);
11780 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11781 op0, true);
11782 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11783 op0, true);
11784 switch (code)
11786 case MULT_EXPR:
11787 case TRUNC_DIV_EXPR:
11788 op1 = save_expr (op1);
11789 imag = build2 (resultcode, real_type, imag, op1);
11790 /* Fall through. */
11791 case PLUS_EXPR:
11792 case MINUS_EXPR:
11793 real = build2 (resultcode, real_type, real, op1);
11794 break;
11795 default:
11796 gcc_unreachable();
11799 else
11801 op1 = save_expr (op1);
11802 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11803 op1, true);
11804 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11805 op1, true);
11806 switch (code)
11808 case MULT_EXPR:
11809 op0 = save_expr (op0);
11810 imag = build2 (resultcode, real_type, op0, imag);
11811 /* Fall through. */
11812 case PLUS_EXPR:
11813 real = build2 (resultcode, real_type, op0, real);
11814 break;
11815 case MINUS_EXPR:
11816 real = build2 (resultcode, real_type, op0, real);
11817 imag = build1 (NEGATE_EXPR, real_type, imag);
11818 break;
11819 default:
11820 gcc_unreachable();
11823 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11824 goto return_build_binary_op;
11827 /* For certain operations (which identify themselves by shorten != 0)
11828 if both args were extended from the same smaller type,
11829 do the arithmetic in that type and then extend.
11831 shorten !=0 and !=1 indicates a bitwise operation.
11832 For them, this optimization is safe only if
11833 both args are zero-extended or both are sign-extended.
11834 Otherwise, we might change the result.
11835 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11836 but calculated in (unsigned short) it would be (unsigned short)-1. */
11838 if (shorten && none_complex)
11840 final_type = result_type;
11841 result_type = shorten_binary_op (result_type, op0, op1,
11842 shorten == -1);
11845 /* Shifts can be shortened if shifting right. */
11847 if (short_shift)
11849 int unsigned_arg;
11850 tree arg0 = get_narrower (op0, &unsigned_arg);
11852 final_type = result_type;
11854 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11855 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11857 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11858 && tree_int_cst_sgn (op1) > 0
11859 /* We can shorten only if the shift count is less than the
11860 number of bits in the smaller type size. */
11861 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11862 /* We cannot drop an unsigned shift after sign-extension. */
11863 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11865 /* Do an unsigned shift if the operand was zero-extended. */
11866 result_type
11867 = c_common_signed_or_unsigned_type (unsigned_arg,
11868 TREE_TYPE (arg0));
11869 /* Convert value-to-be-shifted to that type. */
11870 if (TREE_TYPE (op0) != result_type)
11871 op0 = convert (result_type, op0);
11872 converted = 1;
11876 /* Comparison operations are shortened too but differently.
11877 They identify themselves by setting short_compare = 1. */
11879 if (short_compare)
11881 /* Don't write &op0, etc., because that would prevent op0
11882 from being kept in a register.
11883 Instead, make copies of the our local variables and
11884 pass the copies by reference, then copy them back afterward. */
11885 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11886 enum tree_code xresultcode = resultcode;
11887 tree val
11888 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11889 &xresultcode);
11891 if (val != NULL_TREE)
11893 ret = val;
11894 goto return_build_binary_op;
11897 op0 = xop0, op1 = xop1;
11898 converted = 1;
11899 resultcode = xresultcode;
11901 if (c_inhibit_evaluation_warnings == 0)
11903 bool op0_maybe_const = true;
11904 bool op1_maybe_const = true;
11905 tree orig_op0_folded, orig_op1_folded;
11907 if (in_late_binary_op)
11909 orig_op0_folded = orig_op0;
11910 orig_op1_folded = orig_op1;
11912 else
11914 /* Fold for the sake of possible warnings, as in
11915 build_conditional_expr. This requires the
11916 "original" values to be folded, not just op0 and
11917 op1. */
11918 c_inhibit_evaluation_warnings++;
11919 op0 = c_fully_fold (op0, require_constant_value,
11920 &op0_maybe_const);
11921 op1 = c_fully_fold (op1, require_constant_value,
11922 &op1_maybe_const);
11923 c_inhibit_evaluation_warnings--;
11924 orig_op0_folded = c_fully_fold (orig_op0,
11925 require_constant_value,
11926 NULL);
11927 orig_op1_folded = c_fully_fold (orig_op1,
11928 require_constant_value,
11929 NULL);
11932 if (warn_sign_compare)
11933 warn_for_sign_compare (location, orig_op0_folded,
11934 orig_op1_folded, op0, op1,
11935 result_type, resultcode);
11936 if (!in_late_binary_op && !int_operands)
11938 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11939 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11940 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11941 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11947 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11948 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11949 Then the expression will be built.
11950 It will be given type FINAL_TYPE if that is nonzero;
11951 otherwise, it will be given type RESULT_TYPE. */
11953 if (!result_type)
11955 gcc_rich_location richloc (location);
11956 richloc.maybe_add_expr (orig_op0);
11957 richloc.maybe_add_expr (orig_op1);
11958 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11959 return error_mark_node;
11962 if (build_type == NULL_TREE)
11964 build_type = result_type;
11965 if ((type0 != orig_type0 || type1 != orig_type1)
11966 && !boolean_op)
11968 gcc_assert (may_need_excess_precision && common);
11969 semantic_result_type = c_common_type (orig_type0, orig_type1);
11973 if (!converted)
11975 op0 = ep_convert_and_check (location, result_type, op0,
11976 semantic_result_type);
11977 op1 = ep_convert_and_check (location, result_type, op1,
11978 semantic_result_type);
11980 /* This can happen if one operand has a vector type, and the other
11981 has a different type. */
11982 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11983 return error_mark_node;
11986 if (sanitize_flags_p ((SANITIZE_SHIFT
11987 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
11988 && current_function_decl != NULL_TREE
11989 && (doing_div_or_mod || doing_shift)
11990 && !require_constant_value)
11992 /* OP0 and/or OP1 might have side-effects. */
11993 op0 = save_expr (op0);
11994 op1 = save_expr (op1);
11995 op0 = c_fully_fold (op0, false, NULL);
11996 op1 = c_fully_fold (op1, false, NULL);
11997 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
11998 | SANITIZE_FLOAT_DIVIDE))))
11999 instrument_expr = ubsan_instrument_division (location, op0, op1);
12000 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12001 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12004 /* Treat expressions in initializers specially as they can't trap. */
12005 if (int_const_or_overflow)
12006 ret = (require_constant_value
12007 ? fold_build2_initializer_loc (location, resultcode, build_type,
12008 op0, op1)
12009 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12010 else
12011 ret = build2 (resultcode, build_type, op0, op1);
12012 if (final_type != NULL_TREE)
12013 ret = convert (final_type, ret);
12015 return_build_binary_op:
12016 gcc_assert (ret != error_mark_node);
12017 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12018 ret = (int_operands
12019 ? note_integer_operands (ret)
12020 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12021 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12022 && !in_late_binary_op)
12023 ret = note_integer_operands (ret);
12024 protected_set_expr_location (ret, location);
12026 if (instrument_expr != NULL)
12027 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12028 instrument_expr, ret);
12030 if (semantic_result_type)
12031 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12032 semantic_result_type, ret);
12034 return ret;
12038 /* Convert EXPR to be a truth-value, validating its type for this
12039 purpose. LOCATION is the source location for the expression. */
12041 tree
12042 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12044 bool int_const, int_operands;
12046 switch (TREE_CODE (TREE_TYPE (expr)))
12048 case ARRAY_TYPE:
12049 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12050 return error_mark_node;
12052 case RECORD_TYPE:
12053 error_at (location, "used struct type value where scalar is required");
12054 return error_mark_node;
12056 case UNION_TYPE:
12057 error_at (location, "used union type value where scalar is required");
12058 return error_mark_node;
12060 case VOID_TYPE:
12061 error_at (location, "void value not ignored as it ought to be");
12062 return error_mark_node;
12064 case POINTER_TYPE:
12065 if (reject_gcc_builtin (expr))
12066 return error_mark_node;
12067 break;
12069 case FUNCTION_TYPE:
12070 gcc_unreachable ();
12072 case VECTOR_TYPE:
12073 error_at (location, "used vector type where scalar is required");
12074 return error_mark_node;
12076 default:
12077 break;
12080 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12081 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12082 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12084 expr = remove_c_maybe_const_expr (expr);
12085 expr = build2 (NE_EXPR, integer_type_node, expr,
12086 convert (TREE_TYPE (expr), integer_zero_node));
12087 expr = note_integer_operands (expr);
12089 else
12090 /* ??? Should we also give an error for vectors rather than leaving
12091 those to give errors later? */
12092 expr = c_common_truthvalue_conversion (location, expr);
12094 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12096 if (TREE_OVERFLOW (expr))
12097 return expr;
12098 else
12099 return note_integer_operands (expr);
12101 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12102 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12103 return expr;
12107 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12108 required. */
12110 tree
12111 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12113 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12115 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12116 /* Executing a compound literal inside a function reinitializes
12117 it. */
12118 if (!TREE_STATIC (decl))
12119 *se = true;
12120 return decl;
12122 else
12123 return expr;
12126 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12127 statement. LOC is the location of the construct. */
12129 tree
12130 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12131 tree clauses)
12133 body = c_end_compound_stmt (loc, body, true);
12135 tree stmt = make_node (code);
12136 TREE_TYPE (stmt) = void_type_node;
12137 OMP_BODY (stmt) = body;
12138 OMP_CLAUSES (stmt) = clauses;
12139 SET_EXPR_LOCATION (stmt, loc);
12141 return add_stmt (stmt);
12144 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12145 statement. LOC is the location of the OACC_DATA. */
12147 tree
12148 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12150 tree stmt;
12152 block = c_end_compound_stmt (loc, block, true);
12154 stmt = make_node (OACC_DATA);
12155 TREE_TYPE (stmt) = void_type_node;
12156 OACC_DATA_CLAUSES (stmt) = clauses;
12157 OACC_DATA_BODY (stmt) = block;
12158 SET_EXPR_LOCATION (stmt, loc);
12160 return add_stmt (stmt);
12163 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12164 statement. LOC is the location of the OACC_HOST_DATA. */
12166 tree
12167 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12169 tree stmt;
12171 block = c_end_compound_stmt (loc, block, true);
12173 stmt = make_node (OACC_HOST_DATA);
12174 TREE_TYPE (stmt) = void_type_node;
12175 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12176 OACC_HOST_DATA_BODY (stmt) = block;
12177 SET_EXPR_LOCATION (stmt, loc);
12179 return add_stmt (stmt);
12182 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12184 tree
12185 c_begin_omp_parallel (void)
12187 tree block;
12189 keep_next_level ();
12190 block = c_begin_compound_stmt (true);
12192 return block;
12195 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12196 statement. LOC is the location of the OMP_PARALLEL. */
12198 tree
12199 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12201 tree stmt;
12203 block = c_end_compound_stmt (loc, block, true);
12205 stmt = make_node (OMP_PARALLEL);
12206 TREE_TYPE (stmt) = void_type_node;
12207 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12208 OMP_PARALLEL_BODY (stmt) = block;
12209 SET_EXPR_LOCATION (stmt, loc);
12211 return add_stmt (stmt);
12214 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12216 tree
12217 c_begin_omp_task (void)
12219 tree block;
12221 keep_next_level ();
12222 block = c_begin_compound_stmt (true);
12224 return block;
12227 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12228 statement. LOC is the location of the #pragma. */
12230 tree
12231 c_finish_omp_task (location_t loc, tree clauses, tree block)
12233 tree stmt;
12235 block = c_end_compound_stmt (loc, block, true);
12237 stmt = make_node (OMP_TASK);
12238 TREE_TYPE (stmt) = void_type_node;
12239 OMP_TASK_CLAUSES (stmt) = clauses;
12240 OMP_TASK_BODY (stmt) = block;
12241 SET_EXPR_LOCATION (stmt, loc);
12243 return add_stmt (stmt);
12246 /* Generate GOMP_cancel call for #pragma omp cancel. */
12248 void
12249 c_finish_omp_cancel (location_t loc, tree clauses)
12251 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12252 int mask = 0;
12253 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12254 mask = 1;
12255 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12256 mask = 2;
12257 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12258 mask = 4;
12259 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12260 mask = 8;
12261 else
12263 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12264 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12265 "clauses");
12266 return;
12268 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12269 if (ifc != NULL_TREE)
12271 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12272 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12273 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12274 build_zero_cst (type));
12276 else
12277 ifc = boolean_true_node;
12278 tree stmt = build_call_expr_loc (loc, fn, 2,
12279 build_int_cst (integer_type_node, mask),
12280 ifc);
12281 add_stmt (stmt);
12284 /* Generate GOMP_cancellation_point call for
12285 #pragma omp cancellation point. */
12287 void
12288 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12290 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12291 int mask = 0;
12292 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12293 mask = 1;
12294 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12295 mask = 2;
12296 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12297 mask = 4;
12298 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12299 mask = 8;
12300 else
12302 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12303 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12304 "clauses");
12305 return;
12307 tree stmt = build_call_expr_loc (loc, fn, 1,
12308 build_int_cst (integer_type_node, mask));
12309 add_stmt (stmt);
12312 /* Helper function for handle_omp_array_sections. Called recursively
12313 to handle multiple array-section-subscripts. C is the clause,
12314 T current expression (initially OMP_CLAUSE_DECL), which is either
12315 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12316 expression if specified, TREE_VALUE length expression if specified,
12317 TREE_CHAIN is what it has been specified after, or some decl.
12318 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12319 set to true if any of the array-section-subscript could have length
12320 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12321 first array-section-subscript which is known not to have length
12322 of one. Given say:
12323 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12324 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12325 all are or may have length of 1, array-section-subscript [:2] is the
12326 first one known not to have length 1. For array-section-subscript
12327 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12328 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12329 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12330 case though, as some lengths could be zero. */
12332 static tree
12333 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12334 bool &maybe_zero_len, unsigned int &first_non_one,
12335 enum c_omp_region_type ort)
12337 tree ret, low_bound, length, type;
12338 if (TREE_CODE (t) != TREE_LIST)
12340 if (error_operand_p (t))
12341 return error_mark_node;
12342 ret = t;
12343 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12344 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12346 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12347 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12348 return error_mark_node;
12350 if (TREE_CODE (t) == COMPONENT_REF
12351 && ort == C_ORT_OMP
12352 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12353 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12354 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12356 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12358 error_at (OMP_CLAUSE_LOCATION (c),
12359 "bit-field %qE in %qs clause",
12360 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12361 return error_mark_node;
12363 while (TREE_CODE (t) == COMPONENT_REF)
12365 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12367 error_at (OMP_CLAUSE_LOCATION (c),
12368 "%qE is a member of a union", t);
12369 return error_mark_node;
12371 t = TREE_OPERAND (t, 0);
12374 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12376 if (DECL_P (t))
12377 error_at (OMP_CLAUSE_LOCATION (c),
12378 "%qD is not a variable in %qs clause", t,
12379 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12380 else
12381 error_at (OMP_CLAUSE_LOCATION (c),
12382 "%qE is not a variable in %qs clause", t,
12383 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12384 return error_mark_node;
12386 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12387 && TYPE_ATOMIC (TREE_TYPE (t)))
12389 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12390 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12391 return error_mark_node;
12393 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12394 && VAR_P (t)
12395 && DECL_THREAD_LOCAL_P (t))
12397 error_at (OMP_CLAUSE_LOCATION (c),
12398 "%qD is threadprivate variable in %qs clause", t,
12399 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12400 return error_mark_node;
12402 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12403 && TYPE_ATOMIC (TREE_TYPE (t))
12404 && POINTER_TYPE_P (TREE_TYPE (t)))
12406 /* If the array section is pointer based and the pointer
12407 itself is _Atomic qualified, we need to atomically load
12408 the pointer. */
12409 c_expr expr;
12410 memset (&expr, 0, sizeof (expr));
12411 expr.value = ret;
12412 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12413 expr, false, false);
12414 ret = expr.value;
12416 return ret;
12419 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12420 maybe_zero_len, first_non_one, ort);
12421 if (ret == error_mark_node || ret == NULL_TREE)
12422 return ret;
12424 type = TREE_TYPE (ret);
12425 low_bound = TREE_PURPOSE (t);
12426 length = TREE_VALUE (t);
12428 if (low_bound == error_mark_node || length == error_mark_node)
12429 return error_mark_node;
12431 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12433 error_at (OMP_CLAUSE_LOCATION (c),
12434 "low bound %qE of array section does not have integral type",
12435 low_bound);
12436 return error_mark_node;
12438 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12440 error_at (OMP_CLAUSE_LOCATION (c),
12441 "length %qE of array section does not have integral type",
12442 length);
12443 return error_mark_node;
12445 if (low_bound
12446 && TREE_CODE (low_bound) == INTEGER_CST
12447 && TYPE_PRECISION (TREE_TYPE (low_bound))
12448 > TYPE_PRECISION (sizetype))
12449 low_bound = fold_convert (sizetype, low_bound);
12450 if (length
12451 && TREE_CODE (length) == INTEGER_CST
12452 && TYPE_PRECISION (TREE_TYPE (length))
12453 > TYPE_PRECISION (sizetype))
12454 length = fold_convert (sizetype, length);
12455 if (low_bound == NULL_TREE)
12456 low_bound = integer_zero_node;
12458 if (length != NULL_TREE)
12460 if (!integer_nonzerop (length))
12462 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12463 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12465 if (integer_zerop (length))
12467 error_at (OMP_CLAUSE_LOCATION (c),
12468 "zero length array section in %qs clause",
12469 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12470 return error_mark_node;
12473 else
12474 maybe_zero_len = true;
12476 if (first_non_one == types.length ()
12477 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12478 first_non_one++;
12480 if (TREE_CODE (type) == ARRAY_TYPE)
12482 if (length == NULL_TREE
12483 && (TYPE_DOMAIN (type) == NULL_TREE
12484 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12486 error_at (OMP_CLAUSE_LOCATION (c),
12487 "for unknown bound array type length expression must "
12488 "be specified");
12489 return error_mark_node;
12491 if (TREE_CODE (low_bound) == INTEGER_CST
12492 && tree_int_cst_sgn (low_bound) == -1)
12494 error_at (OMP_CLAUSE_LOCATION (c),
12495 "negative low bound in array section in %qs clause",
12496 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12497 return error_mark_node;
12499 if (length != NULL_TREE
12500 && TREE_CODE (length) == INTEGER_CST
12501 && tree_int_cst_sgn (length) == -1)
12503 error_at (OMP_CLAUSE_LOCATION (c),
12504 "negative length in array section in %qs clause",
12505 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12506 return error_mark_node;
12508 if (TYPE_DOMAIN (type)
12509 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12510 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12511 == INTEGER_CST)
12513 tree size
12514 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12515 size = size_binop (PLUS_EXPR, size, size_one_node);
12516 if (TREE_CODE (low_bound) == INTEGER_CST)
12518 if (tree_int_cst_lt (size, low_bound))
12520 error_at (OMP_CLAUSE_LOCATION (c),
12521 "low bound %qE above array section size "
12522 "in %qs clause", low_bound,
12523 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12524 return error_mark_node;
12526 if (tree_int_cst_equal (size, low_bound))
12528 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12529 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12531 error_at (OMP_CLAUSE_LOCATION (c),
12532 "zero length array section in %qs clause",
12533 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12534 return error_mark_node;
12536 maybe_zero_len = true;
12538 else if (length == NULL_TREE
12539 && first_non_one == types.length ()
12540 && tree_int_cst_equal
12541 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12542 low_bound))
12543 first_non_one++;
12545 else if (length == NULL_TREE)
12547 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12548 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12549 maybe_zero_len = true;
12550 if (first_non_one == types.length ())
12551 first_non_one++;
12553 if (length && TREE_CODE (length) == INTEGER_CST)
12555 if (tree_int_cst_lt (size, length))
12557 error_at (OMP_CLAUSE_LOCATION (c),
12558 "length %qE above array section size "
12559 "in %qs clause", length,
12560 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12561 return error_mark_node;
12563 if (TREE_CODE (low_bound) == INTEGER_CST)
12565 tree lbpluslen
12566 = size_binop (PLUS_EXPR,
12567 fold_convert (sizetype, low_bound),
12568 fold_convert (sizetype, length));
12569 if (TREE_CODE (lbpluslen) == INTEGER_CST
12570 && tree_int_cst_lt (size, lbpluslen))
12572 error_at (OMP_CLAUSE_LOCATION (c),
12573 "high bound %qE above array section size "
12574 "in %qs clause", lbpluslen,
12575 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12576 return error_mark_node;
12581 else if (length == NULL_TREE)
12583 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12584 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12585 maybe_zero_len = true;
12586 if (first_non_one == types.length ())
12587 first_non_one++;
12590 /* For [lb:] we will need to evaluate lb more than once. */
12591 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12593 tree lb = save_expr (low_bound);
12594 if (lb != low_bound)
12596 TREE_PURPOSE (t) = lb;
12597 low_bound = lb;
12601 else if (TREE_CODE (type) == POINTER_TYPE)
12603 if (length == NULL_TREE)
12605 error_at (OMP_CLAUSE_LOCATION (c),
12606 "for pointer type length expression must be specified");
12607 return error_mark_node;
12609 if (length != NULL_TREE
12610 && TREE_CODE (length) == INTEGER_CST
12611 && tree_int_cst_sgn (length) == -1)
12613 error_at (OMP_CLAUSE_LOCATION (c),
12614 "negative length in array section in %qs clause",
12615 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12616 return error_mark_node;
12618 /* If there is a pointer type anywhere but in the very first
12619 array-section-subscript, the array section can't be contiguous. */
12620 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12621 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12623 error_at (OMP_CLAUSE_LOCATION (c),
12624 "array section is not contiguous in %qs clause",
12625 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12626 return error_mark_node;
12629 else
12631 error_at (OMP_CLAUSE_LOCATION (c),
12632 "%qE does not have pointer or array type", ret);
12633 return error_mark_node;
12635 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12636 types.safe_push (TREE_TYPE (ret));
12637 /* We will need to evaluate lb more than once. */
12638 tree lb = save_expr (low_bound);
12639 if (lb != low_bound)
12641 TREE_PURPOSE (t) = lb;
12642 low_bound = lb;
12644 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12645 return ret;
12648 /* Handle array sections for clause C. */
12650 static bool
12651 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12653 bool maybe_zero_len = false;
12654 unsigned int first_non_one = 0;
12655 auto_vec<tree, 10> types;
12656 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12657 maybe_zero_len, first_non_one,
12658 ort);
12659 if (first == error_mark_node)
12660 return true;
12661 if (first == NULL_TREE)
12662 return false;
12663 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12665 tree t = OMP_CLAUSE_DECL (c);
12666 tree tem = NULL_TREE;
12667 /* Need to evaluate side effects in the length expressions
12668 if any. */
12669 while (TREE_CODE (t) == TREE_LIST)
12671 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12673 if (tem == NULL_TREE)
12674 tem = TREE_VALUE (t);
12675 else
12676 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12677 TREE_VALUE (t), tem);
12679 t = TREE_CHAIN (t);
12681 if (tem)
12682 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12683 first = c_fully_fold (first, false, NULL, true);
12684 OMP_CLAUSE_DECL (c) = first;
12686 else
12688 unsigned int num = types.length (), i;
12689 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12690 tree condition = NULL_TREE;
12692 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12693 maybe_zero_len = true;
12695 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12696 t = TREE_CHAIN (t))
12698 tree low_bound = TREE_PURPOSE (t);
12699 tree length = TREE_VALUE (t);
12701 i--;
12702 if (low_bound
12703 && TREE_CODE (low_bound) == INTEGER_CST
12704 && TYPE_PRECISION (TREE_TYPE (low_bound))
12705 > TYPE_PRECISION (sizetype))
12706 low_bound = fold_convert (sizetype, low_bound);
12707 if (length
12708 && TREE_CODE (length) == INTEGER_CST
12709 && TYPE_PRECISION (TREE_TYPE (length))
12710 > TYPE_PRECISION (sizetype))
12711 length = fold_convert (sizetype, length);
12712 if (low_bound == NULL_TREE)
12713 low_bound = integer_zero_node;
12714 if (!maybe_zero_len && i > first_non_one)
12716 if (integer_nonzerop (low_bound))
12717 goto do_warn_noncontiguous;
12718 if (length != NULL_TREE
12719 && TREE_CODE (length) == INTEGER_CST
12720 && TYPE_DOMAIN (types[i])
12721 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12722 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12723 == INTEGER_CST)
12725 tree size;
12726 size = size_binop (PLUS_EXPR,
12727 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12728 size_one_node);
12729 if (!tree_int_cst_equal (length, size))
12731 do_warn_noncontiguous:
12732 error_at (OMP_CLAUSE_LOCATION (c),
12733 "array section is not contiguous in %qs "
12734 "clause",
12735 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12736 return true;
12739 if (length != NULL_TREE
12740 && TREE_SIDE_EFFECTS (length))
12742 if (side_effects == NULL_TREE)
12743 side_effects = length;
12744 else
12745 side_effects = build2 (COMPOUND_EXPR,
12746 TREE_TYPE (side_effects),
12747 length, side_effects);
12750 else
12752 tree l;
12754 if (i > first_non_one
12755 && ((length && integer_nonzerop (length))
12756 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12757 continue;
12758 if (length)
12759 l = fold_convert (sizetype, length);
12760 else
12762 l = size_binop (PLUS_EXPR,
12763 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12764 size_one_node);
12765 l = size_binop (MINUS_EXPR, l,
12766 fold_convert (sizetype, low_bound));
12768 if (i > first_non_one)
12770 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12771 size_zero_node);
12772 if (condition == NULL_TREE)
12773 condition = l;
12774 else
12775 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12776 l, condition);
12778 else if (size == NULL_TREE)
12780 size = size_in_bytes (TREE_TYPE (types[i]));
12781 tree eltype = TREE_TYPE (types[num - 1]);
12782 while (TREE_CODE (eltype) == ARRAY_TYPE)
12783 eltype = TREE_TYPE (eltype);
12784 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12786 if (integer_zerop (size)
12787 || integer_zerop (size_in_bytes (eltype)))
12789 error_at (OMP_CLAUSE_LOCATION (c),
12790 "zero length array section in %qs clause",
12791 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12792 return error_mark_node;
12794 size = size_binop (EXACT_DIV_EXPR, size,
12795 size_in_bytes (eltype));
12797 size = size_binop (MULT_EXPR, size, l);
12798 if (condition)
12799 size = fold_build3 (COND_EXPR, sizetype, condition,
12800 size, size_zero_node);
12802 else
12803 size = size_binop (MULT_EXPR, size, l);
12806 if (side_effects)
12807 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12808 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12810 size = size_binop (MINUS_EXPR, size, size_one_node);
12811 size = c_fully_fold (size, false, NULL);
12812 tree index_type = build_index_type (size);
12813 tree eltype = TREE_TYPE (first);
12814 while (TREE_CODE (eltype) == ARRAY_TYPE)
12815 eltype = TREE_TYPE (eltype);
12816 tree type = build_array_type (eltype, index_type);
12817 tree ptype = build_pointer_type (eltype);
12818 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12819 t = build_fold_addr_expr (t);
12820 tree t2 = build_fold_addr_expr (first);
12821 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12822 ptrdiff_type_node, t2);
12823 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12824 ptrdiff_type_node, t2,
12825 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12826 ptrdiff_type_node, t));
12827 t2 = c_fully_fold (t2, false, NULL);
12828 if (tree_fits_shwi_p (t2))
12829 t = build2 (MEM_REF, type, t,
12830 build_int_cst (ptype, tree_to_shwi (t2)));
12831 else
12833 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12834 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12835 TREE_TYPE (t), t, t2);
12836 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12838 OMP_CLAUSE_DECL (c) = t;
12839 return false;
12841 first = c_fully_fold (first, false, NULL);
12842 OMP_CLAUSE_DECL (c) = first;
12843 if (size)
12844 size = c_fully_fold (size, false, NULL);
12845 OMP_CLAUSE_SIZE (c) = size;
12846 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12847 || (TREE_CODE (t) == COMPONENT_REF
12848 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12849 return false;
12850 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12851 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12852 switch (OMP_CLAUSE_MAP_KIND (c))
12854 case GOMP_MAP_ALLOC:
12855 case GOMP_MAP_TO:
12856 case GOMP_MAP_FROM:
12857 case GOMP_MAP_TOFROM:
12858 case GOMP_MAP_ALWAYS_TO:
12859 case GOMP_MAP_ALWAYS_FROM:
12860 case GOMP_MAP_ALWAYS_TOFROM:
12861 case GOMP_MAP_RELEASE:
12862 case GOMP_MAP_DELETE:
12863 case GOMP_MAP_FORCE_TO:
12864 case GOMP_MAP_FORCE_FROM:
12865 case GOMP_MAP_FORCE_TOFROM:
12866 case GOMP_MAP_FORCE_PRESENT:
12867 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12868 break;
12869 default:
12870 break;
12872 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12873 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12874 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12875 else if (TREE_CODE (t) == COMPONENT_REF)
12876 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12877 else
12878 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12879 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12880 && !c_mark_addressable (t))
12881 return false;
12882 OMP_CLAUSE_DECL (c2) = t;
12883 t = build_fold_addr_expr (first);
12884 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12885 tree ptr = OMP_CLAUSE_DECL (c2);
12886 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12887 ptr = build_fold_addr_expr (ptr);
12888 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12889 ptrdiff_type_node, t,
12890 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12891 ptrdiff_type_node, ptr));
12892 t = c_fully_fold (t, false, NULL);
12893 OMP_CLAUSE_SIZE (c2) = t;
12894 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12895 OMP_CLAUSE_CHAIN (c) = c2;
12897 return false;
12900 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12901 an inline call. But, remap
12902 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12903 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12905 static tree
12906 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12907 tree decl, tree placeholder)
12909 copy_body_data id;
12910 hash_map<tree, tree> decl_map;
12912 decl_map.put (omp_decl1, placeholder);
12913 decl_map.put (omp_decl2, decl);
12914 memset (&id, 0, sizeof (id));
12915 id.src_fn = DECL_CONTEXT (omp_decl1);
12916 id.dst_fn = current_function_decl;
12917 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12918 id.decl_map = &decl_map;
12920 id.copy_decl = copy_decl_no_change;
12921 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12922 id.transform_new_cfg = true;
12923 id.transform_return_to_modify = false;
12924 id.transform_lang_insert_block = NULL;
12925 id.eh_lp_nr = 0;
12926 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12927 return stmt;
12930 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12931 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12933 static tree
12934 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12936 if (*tp == (tree) data)
12937 return *tp;
12938 return NULL_TREE;
12941 /* For all elements of CLAUSES, validate them against their constraints.
12942 Remove any elements from the list that are invalid. */
12944 tree
12945 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12947 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12948 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12949 tree c, t, type, *pc;
12950 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12951 bool branch_seen = false;
12952 bool copyprivate_seen = false;
12953 bool linear_variable_step_check = false;
12954 tree *nowait_clause = NULL;
12955 bool ordered_seen = false;
12956 tree schedule_clause = NULL_TREE;
12957 bool oacc_async = false;
12959 bitmap_obstack_initialize (NULL);
12960 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12961 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12962 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12963 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12964 bitmap_initialize (&map_head, &bitmap_default_obstack);
12965 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12966 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12968 if (ort & C_ORT_ACC)
12969 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12970 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12972 oacc_async = true;
12973 break;
12976 for (pc = &clauses, c = clauses; c ; c = *pc)
12978 bool remove = false;
12979 bool need_complete = false;
12980 bool need_implicitly_determined = false;
12982 switch (OMP_CLAUSE_CODE (c))
12984 case OMP_CLAUSE_SHARED:
12985 need_implicitly_determined = true;
12986 goto check_dup_generic;
12988 case OMP_CLAUSE_PRIVATE:
12989 need_complete = true;
12990 need_implicitly_determined = true;
12991 goto check_dup_generic;
12993 case OMP_CLAUSE_REDUCTION:
12994 need_implicitly_determined = true;
12995 t = OMP_CLAUSE_DECL (c);
12996 if (TREE_CODE (t) == TREE_LIST)
12998 if (handle_omp_array_sections (c, ort))
13000 remove = true;
13001 break;
13004 t = OMP_CLAUSE_DECL (c);
13006 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13007 if (t == error_mark_node)
13009 remove = true;
13010 break;
13012 if (oacc_async)
13013 c_mark_addressable (t);
13014 type = TREE_TYPE (t);
13015 if (TREE_CODE (t) == MEM_REF)
13016 type = TREE_TYPE (type);
13017 if (TREE_CODE (type) == ARRAY_TYPE)
13019 tree oatype = type;
13020 gcc_assert (TREE_CODE (t) != MEM_REF);
13021 while (TREE_CODE (type) == ARRAY_TYPE)
13022 type = TREE_TYPE (type);
13023 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13025 error_at (OMP_CLAUSE_LOCATION (c),
13026 "%qD in %<reduction%> clause is a zero size array",
13028 remove = true;
13029 break;
13031 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13032 TYPE_SIZE_UNIT (type));
13033 if (integer_zerop (size))
13035 error_at (OMP_CLAUSE_LOCATION (c),
13036 "%qD in %<reduction%> clause is a zero size array",
13038 remove = true;
13039 break;
13041 size = size_binop (MINUS_EXPR, size, size_one_node);
13042 tree index_type = build_index_type (size);
13043 tree atype = build_array_type (type, index_type);
13044 tree ptype = build_pointer_type (type);
13045 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13046 t = build_fold_addr_expr (t);
13047 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13048 OMP_CLAUSE_DECL (c) = t;
13050 if (TYPE_ATOMIC (type))
13052 error_at (OMP_CLAUSE_LOCATION (c),
13053 "%<_Atomic%> %qE in %<reduction%> clause", t);
13054 remove = true;
13055 break;
13057 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13058 && (FLOAT_TYPE_P (type)
13059 || TREE_CODE (type) == COMPLEX_TYPE))
13061 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13062 const char *r_name = NULL;
13064 switch (r_code)
13066 case PLUS_EXPR:
13067 case MULT_EXPR:
13068 case MINUS_EXPR:
13069 break;
13070 case MIN_EXPR:
13071 if (TREE_CODE (type) == COMPLEX_TYPE)
13072 r_name = "min";
13073 break;
13074 case MAX_EXPR:
13075 if (TREE_CODE (type) == COMPLEX_TYPE)
13076 r_name = "max";
13077 break;
13078 case BIT_AND_EXPR:
13079 r_name = "&";
13080 break;
13081 case BIT_XOR_EXPR:
13082 r_name = "^";
13083 break;
13084 case BIT_IOR_EXPR:
13085 r_name = "|";
13086 break;
13087 case TRUTH_ANDIF_EXPR:
13088 if (FLOAT_TYPE_P (type))
13089 r_name = "&&";
13090 break;
13091 case TRUTH_ORIF_EXPR:
13092 if (FLOAT_TYPE_P (type))
13093 r_name = "||";
13094 break;
13095 default:
13096 gcc_unreachable ();
13098 if (r_name)
13100 error_at (OMP_CLAUSE_LOCATION (c),
13101 "%qE has invalid type for %<reduction(%s)%>",
13102 t, r_name);
13103 remove = true;
13104 break;
13107 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13109 error_at (OMP_CLAUSE_LOCATION (c),
13110 "user defined reduction not found for %qE", t);
13111 remove = true;
13112 break;
13114 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13116 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13117 type = TYPE_MAIN_VARIANT (type);
13118 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13119 VAR_DECL, NULL_TREE, type);
13120 tree decl_placeholder = NULL_TREE;
13121 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13122 DECL_ARTIFICIAL (placeholder) = 1;
13123 DECL_IGNORED_P (placeholder) = 1;
13124 if (TREE_CODE (t) == MEM_REF)
13126 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13127 VAR_DECL, NULL_TREE, type);
13128 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13129 DECL_ARTIFICIAL (decl_placeholder) = 1;
13130 DECL_IGNORED_P (decl_placeholder) = 1;
13132 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13133 c_mark_addressable (placeholder);
13134 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13135 c_mark_addressable (decl_placeholder ? decl_placeholder
13136 : OMP_CLAUSE_DECL (c));
13137 OMP_CLAUSE_REDUCTION_MERGE (c)
13138 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13139 TREE_VEC_ELT (list, 0),
13140 TREE_VEC_ELT (list, 1),
13141 decl_placeholder ? decl_placeholder
13142 : OMP_CLAUSE_DECL (c), placeholder);
13143 OMP_CLAUSE_REDUCTION_MERGE (c)
13144 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13145 void_type_node, NULL_TREE,
13146 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13147 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13148 if (TREE_VEC_LENGTH (list) == 6)
13150 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13151 c_mark_addressable (decl_placeholder ? decl_placeholder
13152 : OMP_CLAUSE_DECL (c));
13153 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13154 c_mark_addressable (placeholder);
13155 tree init = TREE_VEC_ELT (list, 5);
13156 if (init == error_mark_node)
13157 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13158 OMP_CLAUSE_REDUCTION_INIT (c)
13159 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13160 TREE_VEC_ELT (list, 3),
13161 decl_placeholder ? decl_placeholder
13162 : OMP_CLAUSE_DECL (c), placeholder);
13163 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13165 tree v = decl_placeholder ? decl_placeholder : t;
13166 OMP_CLAUSE_REDUCTION_INIT (c)
13167 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13168 OMP_CLAUSE_REDUCTION_INIT (c));
13170 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13171 c_find_omp_placeholder_r,
13172 placeholder, NULL))
13173 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13175 else
13177 tree init;
13178 tree v = decl_placeholder ? decl_placeholder : t;
13179 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13180 init = build_constructor (TREE_TYPE (v), NULL);
13181 else
13182 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13183 OMP_CLAUSE_REDUCTION_INIT (c)
13184 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13186 OMP_CLAUSE_REDUCTION_INIT (c)
13187 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13188 void_type_node, NULL_TREE,
13189 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13190 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13192 if (TREE_CODE (t) == MEM_REF)
13194 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13195 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13196 != INTEGER_CST)
13198 sorry ("variable length element type in array "
13199 "%<reduction%> clause");
13200 remove = true;
13201 break;
13203 t = TREE_OPERAND (t, 0);
13204 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13205 t = TREE_OPERAND (t, 0);
13206 if (TREE_CODE (t) == ADDR_EXPR)
13207 t = TREE_OPERAND (t, 0);
13209 goto check_dup_generic_t;
13211 case OMP_CLAUSE_COPYPRIVATE:
13212 copyprivate_seen = true;
13213 if (nowait_clause)
13215 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13216 "%<nowait%> clause must not be used together "
13217 "with %<copyprivate%>");
13218 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13219 nowait_clause = NULL;
13221 goto check_dup_generic;
13223 case OMP_CLAUSE_COPYIN:
13224 t = OMP_CLAUSE_DECL (c);
13225 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13227 error_at (OMP_CLAUSE_LOCATION (c),
13228 "%qE must be %<threadprivate%> for %<copyin%>", t);
13229 remove = true;
13230 break;
13232 goto check_dup_generic;
13234 case OMP_CLAUSE_LINEAR:
13235 if (ort != C_ORT_OMP_DECLARE_SIMD)
13236 need_implicitly_determined = true;
13237 t = OMP_CLAUSE_DECL (c);
13238 if (ort != C_ORT_OMP_DECLARE_SIMD
13239 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13241 error_at (OMP_CLAUSE_LOCATION (c),
13242 "modifier should not be specified in %<linear%> "
13243 "clause on %<simd%> or %<for%> constructs");
13244 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13246 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13247 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13249 error_at (OMP_CLAUSE_LOCATION (c),
13250 "linear clause applied to non-integral non-pointer "
13251 "variable with type %qT", TREE_TYPE (t));
13252 remove = true;
13253 break;
13255 if (TYPE_ATOMIC (TREE_TYPE (t)))
13257 error_at (OMP_CLAUSE_LOCATION (c),
13258 "%<_Atomic%> %qD in %<linear%> clause", t);
13259 remove = true;
13260 break;
13262 if (ort == C_ORT_OMP_DECLARE_SIMD)
13264 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13265 if (TREE_CODE (s) == PARM_DECL)
13267 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13268 /* map_head bitmap is used as uniform_head if
13269 declare_simd. */
13270 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13271 linear_variable_step_check = true;
13272 goto check_dup_generic;
13274 if (TREE_CODE (s) != INTEGER_CST)
13276 error_at (OMP_CLAUSE_LOCATION (c),
13277 "%<linear%> clause step %qE is neither constant "
13278 "nor a parameter", s);
13279 remove = true;
13280 break;
13283 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13285 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13286 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13287 OMP_CLAUSE_DECL (c), s);
13288 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13289 sizetype, fold_convert (sizetype, s),
13290 fold_convert
13291 (sizetype, OMP_CLAUSE_DECL (c)));
13292 if (s == error_mark_node)
13293 s = size_one_node;
13294 OMP_CLAUSE_LINEAR_STEP (c) = s;
13296 else
13297 OMP_CLAUSE_LINEAR_STEP (c)
13298 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13299 goto check_dup_generic;
13301 check_dup_generic:
13302 t = OMP_CLAUSE_DECL (c);
13303 check_dup_generic_t:
13304 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13306 error_at (OMP_CLAUSE_LOCATION (c),
13307 "%qE is not a variable in clause %qs", t,
13308 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13309 remove = true;
13311 else if (ort == C_ORT_ACC
13312 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13314 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13316 error ("%qD appears more than once in reduction clauses", t);
13317 remove = true;
13319 else
13320 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13322 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13323 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13324 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13326 error_at (OMP_CLAUSE_LOCATION (c),
13327 "%qE appears more than once in data clauses", t);
13328 remove = true;
13330 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13331 && bitmap_bit_p (&map_head, DECL_UID (t)))
13333 if (ort == C_ORT_ACC)
13334 error ("%qD appears more than once in data clauses", t);
13335 else
13336 error ("%qD appears both in data and map clauses", t);
13337 remove = true;
13339 else
13340 bitmap_set_bit (&generic_head, DECL_UID (t));
13341 break;
13343 case OMP_CLAUSE_FIRSTPRIVATE:
13344 t = OMP_CLAUSE_DECL (c);
13345 need_complete = true;
13346 need_implicitly_determined = true;
13347 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13349 error_at (OMP_CLAUSE_LOCATION (c),
13350 "%qE is not a variable in clause %<firstprivate%>", t);
13351 remove = true;
13353 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13354 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13356 error_at (OMP_CLAUSE_LOCATION (c),
13357 "%qE appears more than once in data clauses", t);
13358 remove = true;
13360 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13362 if (ort == C_ORT_ACC)
13363 error ("%qD appears more than once in data clauses", t);
13364 else
13365 error ("%qD appears both in data and map clauses", t);
13366 remove = true;
13368 else
13369 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13370 break;
13372 case OMP_CLAUSE_LASTPRIVATE:
13373 t = OMP_CLAUSE_DECL (c);
13374 need_complete = true;
13375 need_implicitly_determined = true;
13376 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13378 error_at (OMP_CLAUSE_LOCATION (c),
13379 "%qE is not a variable in clause %<lastprivate%>", t);
13380 remove = true;
13382 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13383 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13385 error_at (OMP_CLAUSE_LOCATION (c),
13386 "%qE appears more than once in data clauses", t);
13387 remove = true;
13389 else
13390 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13391 break;
13393 case OMP_CLAUSE_ALIGNED:
13394 t = OMP_CLAUSE_DECL (c);
13395 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13397 error_at (OMP_CLAUSE_LOCATION (c),
13398 "%qE is not a variable in %<aligned%> clause", t);
13399 remove = true;
13401 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13402 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13404 error_at (OMP_CLAUSE_LOCATION (c),
13405 "%qE in %<aligned%> clause is neither a pointer nor "
13406 "an array", t);
13407 remove = true;
13409 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13411 error_at (OMP_CLAUSE_LOCATION (c),
13412 "%<_Atomic%> %qD in %<aligned%> clause", t);
13413 remove = true;
13414 break;
13416 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13418 error_at (OMP_CLAUSE_LOCATION (c),
13419 "%qE appears more than once in %<aligned%> clauses",
13421 remove = true;
13423 else
13424 bitmap_set_bit (&aligned_head, DECL_UID (t));
13425 break;
13427 case OMP_CLAUSE_DEPEND:
13428 t = OMP_CLAUSE_DECL (c);
13429 if (t == NULL_TREE)
13431 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13432 == OMP_CLAUSE_DEPEND_SOURCE);
13433 break;
13435 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13437 gcc_assert (TREE_CODE (t) == TREE_LIST);
13438 for (; t; t = TREE_CHAIN (t))
13440 tree decl = TREE_VALUE (t);
13441 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13443 tree offset = TREE_PURPOSE (t);
13444 bool neg = wi::neg_p (wi::to_wide (offset));
13445 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13446 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13447 neg ? MINUS_EXPR : PLUS_EXPR,
13448 decl, offset);
13449 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13450 sizetype,
13451 fold_convert (sizetype, t2),
13452 fold_convert (sizetype, decl));
13453 if (t2 == error_mark_node)
13455 remove = true;
13456 break;
13458 TREE_PURPOSE (t) = t2;
13461 break;
13463 if (TREE_CODE (t) == TREE_LIST)
13465 if (handle_omp_array_sections (c, ort))
13466 remove = true;
13467 break;
13469 if (t == error_mark_node)
13470 remove = true;
13471 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13473 error_at (OMP_CLAUSE_LOCATION (c),
13474 "%qE is not a variable in %<depend%> clause", t);
13475 remove = true;
13477 else if (!c_mark_addressable (t))
13478 remove = true;
13479 break;
13481 case OMP_CLAUSE_MAP:
13482 case OMP_CLAUSE_TO:
13483 case OMP_CLAUSE_FROM:
13484 case OMP_CLAUSE__CACHE_:
13485 t = OMP_CLAUSE_DECL (c);
13486 if (TREE_CODE (t) == TREE_LIST)
13488 if (handle_omp_array_sections (c, ort))
13489 remove = true;
13490 else
13492 t = OMP_CLAUSE_DECL (c);
13493 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13495 error_at (OMP_CLAUSE_LOCATION (c),
13496 "array section does not have mappable type "
13497 "in %qs clause",
13498 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13499 remove = true;
13501 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13503 error_at (OMP_CLAUSE_LOCATION (c),
13504 "%<_Atomic%> %qE in %qs clause", t,
13505 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13506 remove = true;
13508 while (TREE_CODE (t) == ARRAY_REF)
13509 t = TREE_OPERAND (t, 0);
13510 if (TREE_CODE (t) == COMPONENT_REF
13511 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13513 while (TREE_CODE (t) == COMPONENT_REF)
13514 t = TREE_OPERAND (t, 0);
13515 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13516 break;
13517 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13519 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13520 error ("%qD appears more than once in motion"
13521 " clauses", t);
13522 else if (ort == C_ORT_ACC)
13523 error ("%qD appears more than once in data"
13524 " clauses", t);
13525 else
13526 error ("%qD appears more than once in map"
13527 " clauses", t);
13528 remove = true;
13530 else
13532 bitmap_set_bit (&map_head, DECL_UID (t));
13533 bitmap_set_bit (&map_field_head, DECL_UID (t));
13537 break;
13539 if (t == error_mark_node)
13541 remove = true;
13542 break;
13544 if (TREE_CODE (t) == COMPONENT_REF
13545 && (ort & C_ORT_OMP)
13546 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13548 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13550 error_at (OMP_CLAUSE_LOCATION (c),
13551 "bit-field %qE in %qs clause",
13552 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13553 remove = true;
13555 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13557 error_at (OMP_CLAUSE_LOCATION (c),
13558 "%qE does not have a mappable type in %qs clause",
13559 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13560 remove = true;
13562 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13564 error_at (OMP_CLAUSE_LOCATION (c),
13565 "%<_Atomic%> %qE in %qs clause", t,
13566 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13567 remove = true;
13569 while (TREE_CODE (t) == COMPONENT_REF)
13571 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13572 == UNION_TYPE)
13574 error_at (OMP_CLAUSE_LOCATION (c),
13575 "%qE is a member of a union", t);
13576 remove = true;
13577 break;
13579 t = TREE_OPERAND (t, 0);
13581 if (remove)
13582 break;
13583 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13585 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13586 break;
13589 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13591 error_at (OMP_CLAUSE_LOCATION (c),
13592 "%qE is not a variable in %qs clause", t,
13593 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13594 remove = true;
13596 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13598 error_at (OMP_CLAUSE_LOCATION (c),
13599 "%qD is threadprivate variable in %qs clause", t,
13600 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13601 remove = true;
13603 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13604 || (OMP_CLAUSE_MAP_KIND (c)
13605 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13606 && !c_mark_addressable (t))
13607 remove = true;
13608 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13609 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13610 || (OMP_CLAUSE_MAP_KIND (c)
13611 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13612 || (OMP_CLAUSE_MAP_KIND (c)
13613 == GOMP_MAP_FORCE_DEVICEPTR)))
13614 && t == OMP_CLAUSE_DECL (c)
13615 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13617 error_at (OMP_CLAUSE_LOCATION (c),
13618 "%qD does not have a mappable type in %qs clause", t,
13619 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13620 remove = true;
13622 else if (TREE_TYPE (t) == error_mark_node)
13623 remove = true;
13624 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13626 error_at (OMP_CLAUSE_LOCATION (c),
13627 "%<_Atomic%> %qE in %qs clause", t,
13628 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13629 remove = true;
13631 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13632 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13634 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13635 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13637 error ("%qD appears more than once in data clauses", t);
13638 remove = true;
13640 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13642 if (ort == C_ORT_ACC)
13643 error ("%qD appears more than once in data clauses", t);
13644 else
13645 error ("%qD appears both in data and map clauses", t);
13646 remove = true;
13648 else
13649 bitmap_set_bit (&generic_head, DECL_UID (t));
13651 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13653 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13654 error ("%qD appears more than once in motion clauses", t);
13655 else if (ort == C_ORT_ACC)
13656 error ("%qD appears more than once in data clauses", t);
13657 else
13658 error ("%qD appears more than once in map clauses", t);
13659 remove = true;
13661 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13662 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13664 if (ort == C_ORT_ACC)
13665 error ("%qD appears more than once in data clauses", t);
13666 else
13667 error ("%qD appears both in data and map clauses", t);
13668 remove = true;
13670 else
13672 bitmap_set_bit (&map_head, DECL_UID (t));
13673 if (t != OMP_CLAUSE_DECL (c)
13674 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13675 bitmap_set_bit (&map_field_head, DECL_UID (t));
13677 break;
13679 case OMP_CLAUSE_TO_DECLARE:
13680 case OMP_CLAUSE_LINK:
13681 t = OMP_CLAUSE_DECL (c);
13682 if (TREE_CODE (t) == FUNCTION_DECL
13683 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13685 else if (!VAR_P (t))
13687 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13688 error_at (OMP_CLAUSE_LOCATION (c),
13689 "%qE is neither a variable nor a function name in "
13690 "clause %qs", t,
13691 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13692 else
13693 error_at (OMP_CLAUSE_LOCATION (c),
13694 "%qE is not a variable in clause %qs", t,
13695 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13696 remove = true;
13698 else if (DECL_THREAD_LOCAL_P (t))
13700 error_at (OMP_CLAUSE_LOCATION (c),
13701 "%qD is threadprivate variable in %qs clause", t,
13702 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13703 remove = true;
13705 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13707 error_at (OMP_CLAUSE_LOCATION (c),
13708 "%qD does not have a mappable type in %qs clause", t,
13709 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13710 remove = true;
13712 if (remove)
13713 break;
13714 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13716 error_at (OMP_CLAUSE_LOCATION (c),
13717 "%qE appears more than once on the same "
13718 "%<declare target%> directive", t);
13719 remove = true;
13721 else
13722 bitmap_set_bit (&generic_head, DECL_UID (t));
13723 break;
13725 case OMP_CLAUSE_UNIFORM:
13726 t = OMP_CLAUSE_DECL (c);
13727 if (TREE_CODE (t) != PARM_DECL)
13729 if (DECL_P (t))
13730 error_at (OMP_CLAUSE_LOCATION (c),
13731 "%qD is not an argument in %<uniform%> clause", t);
13732 else
13733 error_at (OMP_CLAUSE_LOCATION (c),
13734 "%qE is not an argument in %<uniform%> clause", t);
13735 remove = true;
13736 break;
13738 /* map_head bitmap is used as uniform_head if declare_simd. */
13739 bitmap_set_bit (&map_head, DECL_UID (t));
13740 goto check_dup_generic;
13742 case OMP_CLAUSE_IS_DEVICE_PTR:
13743 case OMP_CLAUSE_USE_DEVICE_PTR:
13744 t = OMP_CLAUSE_DECL (c);
13745 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13746 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13748 error_at (OMP_CLAUSE_LOCATION (c),
13749 "%qs variable is neither a pointer nor an array",
13750 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13751 remove = true;
13753 goto check_dup_generic;
13755 case OMP_CLAUSE_NOWAIT:
13756 if (copyprivate_seen)
13758 error_at (OMP_CLAUSE_LOCATION (c),
13759 "%<nowait%> clause must not be used together "
13760 "with %<copyprivate%>");
13761 remove = true;
13762 break;
13764 nowait_clause = pc;
13765 pc = &OMP_CLAUSE_CHAIN (c);
13766 continue;
13768 case OMP_CLAUSE_IF:
13769 case OMP_CLAUSE_NUM_THREADS:
13770 case OMP_CLAUSE_NUM_TEAMS:
13771 case OMP_CLAUSE_THREAD_LIMIT:
13772 case OMP_CLAUSE_DEFAULT:
13773 case OMP_CLAUSE_UNTIED:
13774 case OMP_CLAUSE_COLLAPSE:
13775 case OMP_CLAUSE_FINAL:
13776 case OMP_CLAUSE_MERGEABLE:
13777 case OMP_CLAUSE_DEVICE:
13778 case OMP_CLAUSE_DIST_SCHEDULE:
13779 case OMP_CLAUSE_PARALLEL:
13780 case OMP_CLAUSE_FOR:
13781 case OMP_CLAUSE_SECTIONS:
13782 case OMP_CLAUSE_TASKGROUP:
13783 case OMP_CLAUSE_PROC_BIND:
13784 case OMP_CLAUSE_PRIORITY:
13785 case OMP_CLAUSE_GRAINSIZE:
13786 case OMP_CLAUSE_NUM_TASKS:
13787 case OMP_CLAUSE_NOGROUP:
13788 case OMP_CLAUSE_THREADS:
13789 case OMP_CLAUSE_SIMD:
13790 case OMP_CLAUSE_HINT:
13791 case OMP_CLAUSE_DEFAULTMAP:
13792 case OMP_CLAUSE_NUM_GANGS:
13793 case OMP_CLAUSE_NUM_WORKERS:
13794 case OMP_CLAUSE_VECTOR_LENGTH:
13795 case OMP_CLAUSE_ASYNC:
13796 case OMP_CLAUSE_WAIT:
13797 case OMP_CLAUSE_AUTO:
13798 case OMP_CLAUSE_INDEPENDENT:
13799 case OMP_CLAUSE_SEQ:
13800 case OMP_CLAUSE_GANG:
13801 case OMP_CLAUSE_WORKER:
13802 case OMP_CLAUSE_VECTOR:
13803 case OMP_CLAUSE_TILE:
13804 pc = &OMP_CLAUSE_CHAIN (c);
13805 continue;
13807 case OMP_CLAUSE_SCHEDULE:
13808 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13810 const char *p = NULL;
13811 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13813 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13814 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13815 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13816 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13817 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13818 default: gcc_unreachable ();
13820 if (p)
13822 error_at (OMP_CLAUSE_LOCATION (c),
13823 "%<nonmonotonic%> modifier specified for %qs "
13824 "schedule kind", p);
13825 OMP_CLAUSE_SCHEDULE_KIND (c)
13826 = (enum omp_clause_schedule_kind)
13827 (OMP_CLAUSE_SCHEDULE_KIND (c)
13828 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13831 schedule_clause = c;
13832 pc = &OMP_CLAUSE_CHAIN (c);
13833 continue;
13835 case OMP_CLAUSE_ORDERED:
13836 ordered_seen = true;
13837 pc = &OMP_CLAUSE_CHAIN (c);
13838 continue;
13840 case OMP_CLAUSE_SAFELEN:
13841 safelen = c;
13842 pc = &OMP_CLAUSE_CHAIN (c);
13843 continue;
13844 case OMP_CLAUSE_SIMDLEN:
13845 simdlen = c;
13846 pc = &OMP_CLAUSE_CHAIN (c);
13847 continue;
13849 case OMP_CLAUSE_INBRANCH:
13850 case OMP_CLAUSE_NOTINBRANCH:
13851 if (branch_seen)
13853 error_at (OMP_CLAUSE_LOCATION (c),
13854 "%<inbranch%> clause is incompatible with "
13855 "%<notinbranch%>");
13856 remove = true;
13857 break;
13859 branch_seen = true;
13860 pc = &OMP_CLAUSE_CHAIN (c);
13861 continue;
13863 default:
13864 gcc_unreachable ();
13867 if (!remove)
13869 t = OMP_CLAUSE_DECL (c);
13871 if (need_complete)
13873 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13874 if (t == error_mark_node)
13875 remove = true;
13878 if (need_implicitly_determined)
13880 const char *share_name = NULL;
13882 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13883 share_name = "threadprivate";
13884 else switch (c_omp_predetermined_sharing (t))
13886 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13887 break;
13888 case OMP_CLAUSE_DEFAULT_SHARED:
13889 /* const vars may be specified in firstprivate clause. */
13890 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13891 && TREE_READONLY (t))
13892 break;
13893 share_name = "shared";
13894 break;
13895 case OMP_CLAUSE_DEFAULT_PRIVATE:
13896 share_name = "private";
13897 break;
13898 default:
13899 gcc_unreachable ();
13901 if (share_name)
13903 error_at (OMP_CLAUSE_LOCATION (c),
13904 "%qE is predetermined %qs for %qs",
13905 t, share_name,
13906 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13907 remove = true;
13912 if (remove)
13913 *pc = OMP_CLAUSE_CHAIN (c);
13914 else
13915 pc = &OMP_CLAUSE_CHAIN (c);
13918 if (simdlen
13919 && safelen
13920 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13921 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13923 error_at (OMP_CLAUSE_LOCATION (simdlen),
13924 "%<simdlen%> clause value is bigger than "
13925 "%<safelen%> clause value");
13926 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13927 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13930 if (ordered_seen
13931 && schedule_clause
13932 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13933 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13935 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13936 "%<nonmonotonic%> schedule modifier specified together "
13937 "with %<ordered%> clause");
13938 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13939 = (enum omp_clause_schedule_kind)
13940 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13941 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13944 if (linear_variable_step_check)
13945 for (pc = &clauses, c = clauses; c ; c = *pc)
13947 bool remove = false;
13948 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13949 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13950 && !bitmap_bit_p (&map_head,
13951 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13953 error_at (OMP_CLAUSE_LOCATION (c),
13954 "%<linear%> clause step is a parameter %qD not "
13955 "specified in %<uniform%> clause",
13956 OMP_CLAUSE_LINEAR_STEP (c));
13957 remove = true;
13960 if (remove)
13961 *pc = OMP_CLAUSE_CHAIN (c);
13962 else
13963 pc = &OMP_CLAUSE_CHAIN (c);
13966 bitmap_obstack_release (NULL);
13967 return clauses;
13970 /* Return code to initialize DST with a copy constructor from SRC.
13971 C doesn't have copy constructors nor assignment operators, only for
13972 _Atomic vars we need to perform __atomic_load from src into a temporary
13973 followed by __atomic_store of the temporary to dst. */
13975 tree
13976 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13978 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13979 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13981 location_t loc = OMP_CLAUSE_LOCATION (clause);
13982 tree type = TREE_TYPE (dst);
13983 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13984 tree tmp = create_tmp_var (nonatomic_type);
13985 tree tmp_addr = build_fold_addr_expr (tmp);
13986 TREE_ADDRESSABLE (tmp) = 1;
13987 TREE_NO_WARNING (tmp) = 1;
13988 tree src_addr = build_fold_addr_expr (src);
13989 tree dst_addr = build_fold_addr_expr (dst);
13990 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13991 vec<tree, va_gc> *params;
13992 /* Expansion of a generic atomic load may require an addition
13993 element, so allocate enough to prevent a resize. */
13994 vec_alloc (params, 4);
13996 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13997 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13998 params->quick_push (src_addr);
13999 params->quick_push (tmp_addr);
14000 params->quick_push (seq_cst);
14001 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14003 vec_alloc (params, 4);
14005 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14006 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14007 params->quick_push (dst_addr);
14008 params->quick_push (tmp_addr);
14009 params->quick_push (seq_cst);
14010 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14011 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14014 /* Create a transaction node. */
14016 tree
14017 c_finish_transaction (location_t loc, tree block, int flags)
14019 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14020 if (flags & TM_STMT_ATTR_OUTER)
14021 TRANSACTION_EXPR_OUTER (stmt) = 1;
14022 if (flags & TM_STMT_ATTR_RELAXED)
14023 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14024 return add_stmt (stmt);
14027 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14028 down to the element type of an array. If ORIG_QUAL_TYPE is not
14029 NULL, then it should be used as the qualified type
14030 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14031 preserve information about the typedef name from which an array
14032 type was derived). */
14034 tree
14035 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14036 size_t orig_qual_indirect)
14038 if (type == error_mark_node)
14039 return type;
14041 if (TREE_CODE (type) == ARRAY_TYPE)
14043 tree t;
14044 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14045 type_quals, orig_qual_type,
14046 orig_qual_indirect - 1);
14048 /* See if we already have an identically qualified type. */
14049 if (orig_qual_type && orig_qual_indirect == 0)
14050 t = orig_qual_type;
14051 else
14052 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14054 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14055 && TYPE_NAME (t) == TYPE_NAME (type)
14056 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14057 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14058 TYPE_ATTRIBUTES (type)))
14059 break;
14061 if (!t)
14063 tree domain = TYPE_DOMAIN (type);
14065 t = build_variant_type_copy (type);
14066 TREE_TYPE (t) = element_type;
14068 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14069 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14070 SET_TYPE_STRUCTURAL_EQUALITY (t);
14071 else if (TYPE_CANONICAL (element_type) != element_type
14072 || (domain && TYPE_CANONICAL (domain) != domain))
14074 tree unqualified_canon
14075 = build_array_type (TYPE_CANONICAL (element_type),
14076 domain? TYPE_CANONICAL (domain)
14077 : NULL_TREE);
14078 if (TYPE_REVERSE_STORAGE_ORDER (type))
14080 unqualified_canon
14081 = build_distinct_type_copy (unqualified_canon);
14082 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14084 TYPE_CANONICAL (t)
14085 = c_build_qualified_type (unqualified_canon, type_quals);
14087 else
14088 TYPE_CANONICAL (t) = t;
14090 return t;
14093 /* A restrict-qualified pointer type must be a pointer to object or
14094 incomplete type. Note that the use of POINTER_TYPE_P also allows
14095 REFERENCE_TYPEs, which is appropriate for C++. */
14096 if ((type_quals & TYPE_QUAL_RESTRICT)
14097 && (!POINTER_TYPE_P (type)
14098 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14100 error ("invalid use of %<restrict%>");
14101 type_quals &= ~TYPE_QUAL_RESTRICT;
14104 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14105 ? orig_qual_type
14106 : build_qualified_type (type, type_quals));
14107 /* A variant type does not inherit the list of incomplete vars from the
14108 type main variant. */
14109 if (RECORD_OR_UNION_TYPE_P (var_type)
14110 && TYPE_MAIN_VARIANT (var_type) != var_type)
14111 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14112 return var_type;
14115 /* Build a VA_ARG_EXPR for the C parser. */
14117 tree
14118 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14120 if (error_operand_p (type))
14121 return error_mark_node;
14122 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14123 order because it takes the address of the expression. */
14124 else if (handled_component_p (expr)
14125 && reverse_storage_order_for_component_p (expr))
14127 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14128 return error_mark_node;
14130 else if (!COMPLETE_TYPE_P (type))
14132 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14133 "type %qT", type);
14134 return error_mark_node;
14136 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14137 warning_at (loc2, OPT_Wc___compat,
14138 "C++ requires promoted type, not enum type, in %<va_arg%>");
14139 return build_va_arg (loc2, expr, type);
14142 /* Return truthvalue of whether T1 is the same tree structure as T2.
14143 Return 1 if they are the same. Return false if they are different. */
14145 bool
14146 c_tree_equal (tree t1, tree t2)
14148 enum tree_code code1, code2;
14150 if (t1 == t2)
14151 return true;
14152 if (!t1 || !t2)
14153 return false;
14155 for (code1 = TREE_CODE (t1);
14156 CONVERT_EXPR_CODE_P (code1)
14157 || code1 == NON_LVALUE_EXPR;
14158 code1 = TREE_CODE (t1))
14159 t1 = TREE_OPERAND (t1, 0);
14160 for (code2 = TREE_CODE (t2);
14161 CONVERT_EXPR_CODE_P (code2)
14162 || code2 == NON_LVALUE_EXPR;
14163 code2 = TREE_CODE (t2))
14164 t2 = TREE_OPERAND (t2, 0);
14166 /* They might have become equal now. */
14167 if (t1 == t2)
14168 return true;
14170 if (code1 != code2)
14171 return false;
14173 switch (code1)
14175 case INTEGER_CST:
14176 return wi::to_wide (t1) == wi::to_wide (t2);
14178 case REAL_CST:
14179 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14181 case STRING_CST:
14182 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14183 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14184 TREE_STRING_LENGTH (t1));
14186 case FIXED_CST:
14187 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14188 TREE_FIXED_CST (t2));
14190 case COMPLEX_CST:
14191 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14192 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14194 case VECTOR_CST:
14195 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14197 case CONSTRUCTOR:
14198 /* We need to do this when determining whether or not two
14199 non-type pointer to member function template arguments
14200 are the same. */
14201 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14202 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14203 return false;
14205 tree field, value;
14206 unsigned int i;
14207 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14209 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14210 if (!c_tree_equal (field, elt2->index)
14211 || !c_tree_equal (value, elt2->value))
14212 return false;
14215 return true;
14217 case TREE_LIST:
14218 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14219 return false;
14220 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14221 return false;
14222 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14224 case SAVE_EXPR:
14225 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14227 case CALL_EXPR:
14229 tree arg1, arg2;
14230 call_expr_arg_iterator iter1, iter2;
14231 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14232 return false;
14233 for (arg1 = first_call_expr_arg (t1, &iter1),
14234 arg2 = first_call_expr_arg (t2, &iter2);
14235 arg1 && arg2;
14236 arg1 = next_call_expr_arg (&iter1),
14237 arg2 = next_call_expr_arg (&iter2))
14238 if (!c_tree_equal (arg1, arg2))
14239 return false;
14240 if (arg1 || arg2)
14241 return false;
14242 return true;
14245 case TARGET_EXPR:
14247 tree o1 = TREE_OPERAND (t1, 0);
14248 tree o2 = TREE_OPERAND (t2, 0);
14250 /* Special case: if either target is an unallocated VAR_DECL,
14251 it means that it's going to be unified with whatever the
14252 TARGET_EXPR is really supposed to initialize, so treat it
14253 as being equivalent to anything. */
14254 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14255 && !DECL_RTL_SET_P (o1))
14256 /*Nop*/;
14257 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14258 && !DECL_RTL_SET_P (o2))
14259 /*Nop*/;
14260 else if (!c_tree_equal (o1, o2))
14261 return false;
14263 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14266 case COMPONENT_REF:
14267 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14268 return false;
14269 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14271 case PARM_DECL:
14272 case VAR_DECL:
14273 case CONST_DECL:
14274 case FIELD_DECL:
14275 case FUNCTION_DECL:
14276 case IDENTIFIER_NODE:
14277 case SSA_NAME:
14278 return false;
14280 case TREE_VEC:
14282 unsigned ix;
14283 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14284 return false;
14285 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14286 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14287 TREE_VEC_ELT (t2, ix)))
14288 return false;
14289 return true;
14292 default:
14293 break;
14296 switch (TREE_CODE_CLASS (code1))
14298 case tcc_unary:
14299 case tcc_binary:
14300 case tcc_comparison:
14301 case tcc_expression:
14302 case tcc_vl_exp:
14303 case tcc_reference:
14304 case tcc_statement:
14306 int i, n = TREE_OPERAND_LENGTH (t1);
14308 switch (code1)
14310 case PREINCREMENT_EXPR:
14311 case PREDECREMENT_EXPR:
14312 case POSTINCREMENT_EXPR:
14313 case POSTDECREMENT_EXPR:
14314 n = 1;
14315 break;
14316 case ARRAY_REF:
14317 n = 2;
14318 break;
14319 default:
14320 break;
14323 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14324 && n != TREE_OPERAND_LENGTH (t2))
14325 return false;
14327 for (i = 0; i < n; ++i)
14328 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14329 return false;
14331 return true;
14334 case tcc_type:
14335 return comptypes (t1, t2);
14336 default:
14337 gcc_unreachable ();
14339 /* We can get here with --disable-checking. */
14340 return false;
14343 /* Returns true when the function declaration FNDECL is implicit,
14344 introduced as a result of a call to an otherwise undeclared
14345 function, and false otherwise. */
14347 bool
14348 c_decl_implicit (const_tree fndecl)
14350 return C_DECL_IMPLICIT (fndecl);