PR c/82167
[official-gcc.git] / gcc / c / c-typeck.c
blobf45fd3cfbbf0942aa6f048f0cef0f8dbd9197ff7
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 "cilk.h"
50 #include "gomp-constants.h"
51 #include "spellcheck-tree.h"
52 #include "gcc-rich-location.h"
53 #include "stringpool.h"
54 #include "attribs.h"
55 #include "asan.h"
57 /* Possible cases of implicit bad conversions. Used to select
58 diagnostic messages in convert_for_assignment. */
59 enum impl_conv {
60 ic_argpass,
61 ic_assign,
62 ic_init,
63 ic_return
66 /* The level of nesting inside "__alignof__". */
67 int in_alignof;
69 /* The level of nesting inside "sizeof". */
70 int in_sizeof;
72 /* The level of nesting inside "typeof". */
73 int in_typeof;
75 /* The argument of last parsed sizeof expression, only to be tested
76 if expr.original_code == SIZEOF_EXPR. */
77 tree c_last_sizeof_arg;
78 location_t c_last_sizeof_loc;
80 /* Nonzero if we might need to print a "missing braces around
81 initializer" message within this initializer. */
82 static int found_missing_braces;
84 static int require_constant_value;
85 static int require_constant_elements;
87 static bool null_pointer_constant_p (const_tree);
88 static tree qualify_type (tree, tree);
89 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
90 bool *);
91 static int comp_target_types (location_t, tree, tree);
92 static int function_types_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
95 static tree lookup_field (tree, tree);
96 static int convert_arguments (location_t, vec<location_t>, tree,
97 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
98 tree);
99 static tree pointer_diff (location_t, tree, tree);
100 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
101 enum impl_conv, bool, tree, tree, int);
102 static tree valid_compound_expr_initializer (tree, tree);
103 static void push_string (const char *);
104 static void push_member_name (tree);
105 static int spelling_length (void);
106 static char *print_spelling (char *);
107 static void warning_init (location_t, int, const char *);
108 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
109 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
110 bool, struct obstack *);
111 static void output_pending_init_elements (int, struct obstack *);
112 static bool set_designator (location_t, bool, struct obstack *);
113 static void push_range_stack (tree, struct obstack *);
114 static void add_pending_init (location_t, tree, tree, tree, bool,
115 struct obstack *);
116 static void set_nonincremental_init (struct obstack *);
117 static void set_nonincremental_init_from_string (tree, struct obstack *);
118 static tree find_init_member (tree, struct obstack *);
119 static void readonly_warning (tree, enum lvalue_use);
120 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
121 static void record_maybe_used_decl (tree);
122 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
124 /* Return true if EXP is a null pointer constant, false otherwise. */
126 static bool
127 null_pointer_constant_p (const_tree expr)
129 /* This should really operate on c_expr structures, but they aren't
130 yet available everywhere required. */
131 tree type = TREE_TYPE (expr);
132 return (TREE_CODE (expr) == INTEGER_CST
133 && !TREE_OVERFLOW (expr)
134 && integer_zerop (expr)
135 && (INTEGRAL_TYPE_P (type)
136 || (TREE_CODE (type) == POINTER_TYPE
137 && VOID_TYPE_P (TREE_TYPE (type))
138 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
141 /* EXPR may appear in an unevaluated part of an integer constant
142 expression, but not in an evaluated part. Wrap it in a
143 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
144 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
146 static tree
147 note_integer_operands (tree expr)
149 tree ret;
150 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
152 ret = copy_node (expr);
153 TREE_OVERFLOW (ret) = 1;
155 else
157 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
158 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
160 return ret;
163 /* Having checked whether EXPR may appear in an unevaluated part of an
164 integer constant expression and found that it may, remove any
165 C_MAYBE_CONST_EXPR noting this fact and return the resulting
166 expression. */
168 static inline tree
169 remove_c_maybe_const_expr (tree expr)
171 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
172 return C_MAYBE_CONST_EXPR_EXPR (expr);
173 else
174 return expr;
177 \f/* This is a cache to hold if two types are compatible or not. */
179 struct tagged_tu_seen_cache {
180 const struct tagged_tu_seen_cache * next;
181 const_tree t1;
182 const_tree t2;
183 /* The return value of tagged_types_tu_compatible_p if we had seen
184 these two types already. */
185 int val;
188 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
189 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
191 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
192 does not have an incomplete type. (That includes void types.)
193 LOC is the location of the use. */
195 tree
196 require_complete_type (location_t loc, tree value)
198 tree type = TREE_TYPE (value);
200 if (error_operand_p (value))
201 return error_mark_node;
203 /* First, detect a valid value with a complete type. */
204 if (COMPLETE_TYPE_P (type))
205 return value;
207 c_incomplete_type_error (loc, value, type);
208 return error_mark_node;
211 /* Print an error message for invalid use of an incomplete type.
212 VALUE is the expression that was used (or 0 if that isn't known)
213 and TYPE is the type that was invalid. LOC is the location for
214 the error. */
216 void
217 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
219 /* Avoid duplicate error message. */
220 if (TREE_CODE (type) == ERROR_MARK)
221 return;
223 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
224 error_at (loc, "%qD has an incomplete type %qT", value, type);
225 else
227 retry:
228 /* We must print an error message. Be clever about what it says. */
230 switch (TREE_CODE (type))
232 case RECORD_TYPE:
233 case UNION_TYPE:
234 case ENUMERAL_TYPE:
235 break;
237 case VOID_TYPE:
238 error_at (loc, "invalid use of void expression");
239 return;
241 case ARRAY_TYPE:
242 if (TYPE_DOMAIN (type))
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
246 error_at (loc, "invalid use of flexible array member");
247 return;
249 type = TREE_TYPE (type);
250 goto retry;
252 error_at (loc, "invalid use of array with unspecified bounds");
253 return;
255 default:
256 gcc_unreachable ();
259 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
260 error_at (loc, "invalid use of undefined type %qT", type);
261 else
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error_at (loc, "invalid use of incomplete typedef %qT", type);
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
270 tree
271 c_type_promotes_to (tree type)
273 tree ret = NULL_TREE;
275 if (TYPE_MAIN_VARIANT (type) == float_type_node)
276 ret = double_type_node;
277 else if (c_promoting_integer_type_p (type))
279 /* Preserve unsignedness if not really getting any wider. */
280 if (TYPE_UNSIGNED (type)
281 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
282 ret = unsigned_type_node;
283 else
284 ret = integer_type_node;
287 if (ret != NULL_TREE)
288 return (TYPE_ATOMIC (type)
289 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
290 : ret);
292 return type;
295 /* Return true if between two named address spaces, whether there is a superset
296 named address space that encompasses both address spaces. If there is a
297 superset, return which address space is the superset. */
299 static bool
300 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
302 if (as1 == as2)
304 *common = as1;
305 return true;
307 else if (targetm.addr_space.subset_p (as1, as2))
309 *common = as2;
310 return true;
312 else if (targetm.addr_space.subset_p (as2, as1))
314 *common = as1;
315 return true;
317 else
318 return false;
321 /* Return a variant of TYPE which has all the type qualifiers of LIKE
322 as well as those of TYPE. */
324 static tree
325 qualify_type (tree type, tree like)
327 addr_space_t as_type = TYPE_ADDR_SPACE (type);
328 addr_space_t as_like = TYPE_ADDR_SPACE (like);
329 addr_space_t as_common;
331 /* If the two named address spaces are different, determine the common
332 superset address space. If there isn't one, raise an error. */
333 if (!addr_space_superset (as_type, as_like, &as_common))
335 as_common = as_type;
336 error ("%qT and %qT are in disjoint named address spaces",
337 type, like);
340 return c_build_qualified_type (type,
341 TYPE_QUALS_NO_ADDR_SPACE (type)
342 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
343 | ENCODE_QUAL_ADDR_SPACE (as_common));
346 /* Return true iff the given tree T is a variable length array. */
348 bool
349 c_vla_type_p (const_tree t)
351 if (TREE_CODE (t) == ARRAY_TYPE
352 && C_TYPE_VARIABLE_SIZE (t))
353 return true;
354 return false;
357 /* Return the composite type of two compatible types.
359 We assume that comptypes has already been done and returned
360 nonzero; if that isn't so, this may crash. In particular, we
361 assume that qualifiers match. */
363 tree
364 composite_type (tree t1, tree t2)
366 enum tree_code code1;
367 enum tree_code code2;
368 tree attributes;
370 /* Save time if the two types are the same. */
372 if (t1 == t2) return t1;
374 /* If one type is nonsense, use the other. */
375 if (t1 == error_mark_node)
376 return t2;
377 if (t2 == error_mark_node)
378 return t1;
380 code1 = TREE_CODE (t1);
381 code2 = TREE_CODE (t2);
383 /* Merge the attributes. */
384 attributes = targetm.merge_type_attributes (t1, t2);
386 /* If one is an enumerated type and the other is the compatible
387 integer type, the composite type might be either of the two
388 (DR#013 question 3). For consistency, use the enumerated type as
389 the composite type. */
391 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
392 return t1;
393 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
394 return t2;
396 gcc_assert (code1 == code2);
398 switch (code1)
400 case POINTER_TYPE:
401 /* For two pointers, do this recursively on the target type. */
403 tree pointed_to_1 = TREE_TYPE (t1);
404 tree pointed_to_2 = TREE_TYPE (t2);
405 tree target = composite_type (pointed_to_1, pointed_to_2);
406 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
407 t1 = build_type_attribute_variant (t1, attributes);
408 return qualify_type (t1, t2);
411 case ARRAY_TYPE:
413 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
414 int quals;
415 tree unqual_elt;
416 tree d1 = TYPE_DOMAIN (t1);
417 tree d2 = TYPE_DOMAIN (t2);
418 bool d1_variable, d2_variable;
419 bool d1_zero, d2_zero;
420 bool t1_complete, t2_complete;
422 /* We should not have any type quals on arrays at all. */
423 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
424 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
426 t1_complete = COMPLETE_TYPE_P (t1);
427 t2_complete = COMPLETE_TYPE_P (t2);
429 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
430 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
432 d1_variable = (!d1_zero
433 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
434 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
435 d2_variable = (!d2_zero
436 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
437 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
438 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
439 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
441 /* Save space: see if the result is identical to one of the args. */
442 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
443 && (d2_variable || d2_zero || !d1_variable))
444 return build_type_attribute_variant (t1, attributes);
445 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
446 && (d1_variable || d1_zero || !d2_variable))
447 return build_type_attribute_variant (t2, attributes);
449 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
450 return build_type_attribute_variant (t1, attributes);
451 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
452 return build_type_attribute_variant (t2, attributes);
454 /* Merge the element types, and have a size if either arg has
455 one. We may have qualifiers on the element types. To set
456 up TYPE_MAIN_VARIANT correctly, we need to form the
457 composite of the unqualified types and add the qualifiers
458 back at the end. */
459 quals = TYPE_QUALS (strip_array_types (elt));
460 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
461 t1 = build_array_type (unqual_elt,
462 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
463 && (d2_variable
464 || d2_zero
465 || !d1_variable))
466 ? t1
467 : t2));
468 /* Ensure a composite type involving a zero-length array type
469 is a zero-length type not an incomplete type. */
470 if (d1_zero && d2_zero
471 && (t1_complete || t2_complete)
472 && !COMPLETE_TYPE_P (t1))
474 TYPE_SIZE (t1) = bitsize_zero_node;
475 TYPE_SIZE_UNIT (t1) = size_zero_node;
477 t1 = c_build_qualified_type (t1, quals);
478 return build_type_attribute_variant (t1, attributes);
481 case ENUMERAL_TYPE:
482 case RECORD_TYPE:
483 case UNION_TYPE:
484 if (attributes != NULL)
486 /* Try harder not to create a new aggregate type. */
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
488 return t1;
489 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
490 return t2;
492 return build_type_attribute_variant (t1, attributes);
494 case FUNCTION_TYPE:
495 /* Function types: prefer the one that specified arg types.
496 If both do, merge the arg types. Also merge the return types. */
498 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
499 tree p1 = TYPE_ARG_TYPES (t1);
500 tree p2 = TYPE_ARG_TYPES (t2);
501 int len;
502 tree newargs, n;
503 int i;
505 /* Save space: see if the result is identical to one of the args. */
506 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
507 return build_type_attribute_variant (t1, attributes);
508 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
509 return build_type_attribute_variant (t2, attributes);
511 /* Simple way if one arg fails to specify argument types. */
512 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
514 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
515 t1 = build_type_attribute_variant (t1, attributes);
516 return qualify_type (t1, t2);
518 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
520 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
521 t1 = build_type_attribute_variant (t1, attributes);
522 return qualify_type (t1, t2);
525 /* If both args specify argument types, we must merge the two
526 lists, argument by argument. */
528 for (len = 0, newargs = p1;
529 newargs && newargs != void_list_node;
530 len++, newargs = TREE_CHAIN (newargs))
533 for (i = 0; i < len; i++)
534 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
536 n = newargs;
538 for (; p1 && p1 != void_list_node;
539 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
541 /* A null type means arg type is not specified.
542 Take whatever the other function type has. */
543 if (TREE_VALUE (p1) == NULL_TREE)
545 TREE_VALUE (n) = TREE_VALUE (p2);
546 goto parm_done;
548 if (TREE_VALUE (p2) == NULL_TREE)
550 TREE_VALUE (n) = TREE_VALUE (p1);
551 goto parm_done;
554 /* Given wait (union {union wait *u; int *i} *)
555 and wait (union wait *),
556 prefer union wait * as type of parm. */
557 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
558 && TREE_VALUE (p1) != TREE_VALUE (p2))
560 tree memb;
561 tree mv2 = TREE_VALUE (p2);
562 if (mv2 && mv2 != error_mark_node
563 && TREE_CODE (mv2) != ARRAY_TYPE)
564 mv2 = TYPE_MAIN_VARIANT (mv2);
565 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
566 memb; memb = DECL_CHAIN (memb))
568 tree mv3 = TREE_TYPE (memb);
569 if (mv3 && mv3 != error_mark_node
570 && TREE_CODE (mv3) != ARRAY_TYPE)
571 mv3 = TYPE_MAIN_VARIANT (mv3);
572 if (comptypes (mv3, mv2))
574 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
575 TREE_VALUE (p2));
576 pedwarn (input_location, OPT_Wpedantic,
577 "function types not truly compatible in ISO C");
578 goto parm_done;
582 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
583 && TREE_VALUE (p2) != TREE_VALUE (p1))
585 tree memb;
586 tree mv1 = TREE_VALUE (p1);
587 if (mv1 && mv1 != error_mark_node
588 && TREE_CODE (mv1) != ARRAY_TYPE)
589 mv1 = TYPE_MAIN_VARIANT (mv1);
590 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
591 memb; memb = DECL_CHAIN (memb))
593 tree mv3 = TREE_TYPE (memb);
594 if (mv3 && mv3 != error_mark_node
595 && TREE_CODE (mv3) != ARRAY_TYPE)
596 mv3 = TYPE_MAIN_VARIANT (mv3);
597 if (comptypes (mv3, mv1))
599 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
600 TREE_VALUE (p1));
601 pedwarn (input_location, OPT_Wpedantic,
602 "function types not truly compatible in ISO C");
603 goto parm_done;
607 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
608 parm_done: ;
611 t1 = build_function_type (valtype, newargs);
612 t1 = qualify_type (t1, t2);
614 /* FALLTHRU */
616 default:
617 return build_type_attribute_variant (t1, attributes);
622 /* Return the type of a conditional expression between pointers to
623 possibly differently qualified versions of compatible types.
625 We assume that comp_target_types has already been done and returned
626 nonzero; if that isn't so, this may crash. */
628 static tree
629 common_pointer_type (tree t1, tree t2)
631 tree attributes;
632 tree pointed_to_1, mv1;
633 tree pointed_to_2, mv2;
634 tree target;
635 unsigned target_quals;
636 addr_space_t as1, as2, as_common;
637 int quals1, quals2;
639 /* Save time if the two types are the same. */
641 if (t1 == t2) return t1;
643 /* If one type is nonsense, use the other. */
644 if (t1 == error_mark_node)
645 return t2;
646 if (t2 == error_mark_node)
647 return t1;
649 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
650 && TREE_CODE (t2) == POINTER_TYPE);
652 /* Merge the attributes. */
653 attributes = targetm.merge_type_attributes (t1, t2);
655 /* Find the composite type of the target types, and combine the
656 qualifiers of the two types' targets. Do not lose qualifiers on
657 array element types by taking the TYPE_MAIN_VARIANT. */
658 mv1 = pointed_to_1 = TREE_TYPE (t1);
659 mv2 = pointed_to_2 = TREE_TYPE (t2);
660 if (TREE_CODE (mv1) != ARRAY_TYPE)
661 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
662 if (TREE_CODE (mv2) != ARRAY_TYPE)
663 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
664 target = composite_type (mv1, mv2);
666 /* Strip array types to get correct qualifier for pointers to arrays */
667 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
668 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
670 /* For function types do not merge const qualifiers, but drop them
671 if used inconsistently. The middle-end uses these to mark const
672 and noreturn functions. */
673 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
674 target_quals = (quals1 & quals2);
675 else
676 target_quals = (quals1 | quals2);
678 /* If the two named address spaces are different, determine the common
679 superset address space. This is guaranteed to exist due to the
680 assumption that comp_target_type returned non-zero. */
681 as1 = TYPE_ADDR_SPACE (pointed_to_1);
682 as2 = TYPE_ADDR_SPACE (pointed_to_2);
683 if (!addr_space_superset (as1, as2, &as_common))
684 gcc_unreachable ();
686 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
688 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
689 return build_type_attribute_variant (t1, attributes);
692 /* Return the common type for two arithmetic types under the usual
693 arithmetic conversions. The default conversions have already been
694 applied, and enumerated types converted to their compatible integer
695 types. The resulting type is unqualified and has no attributes.
697 This is the type for the result of most arithmetic operations
698 if the operands have the given two types. */
700 static tree
701 c_common_type (tree t1, tree t2)
703 enum tree_code code1;
704 enum tree_code code2;
706 /* If one type is nonsense, use the other. */
707 if (t1 == error_mark_node)
708 return t2;
709 if (t2 == error_mark_node)
710 return t1;
712 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
713 t1 = TYPE_MAIN_VARIANT (t1);
715 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
716 t2 = TYPE_MAIN_VARIANT (t2);
718 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
719 t1 = build_type_attribute_variant (t1, NULL_TREE);
721 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
722 t2 = build_type_attribute_variant (t2, NULL_TREE);
724 /* Save time if the two types are the same. */
726 if (t1 == t2) return t1;
728 code1 = TREE_CODE (t1);
729 code2 = TREE_CODE (t2);
731 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
732 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
733 || code1 == INTEGER_TYPE);
734 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
735 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
736 || code2 == INTEGER_TYPE);
738 /* When one operand is a decimal float type, the other operand cannot be
739 a generic float type or a complex type. We also disallow vector types
740 here. */
741 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
742 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
744 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
746 error ("can%'t mix operands of decimal float and vector types");
747 return error_mark_node;
749 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
751 error ("can%'t mix operands of decimal float and complex types");
752 return error_mark_node;
754 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
756 error ("can%'t mix operands of decimal float and other float types");
757 return error_mark_node;
761 /* If one type is a vector type, return that type. (How the usual
762 arithmetic conversions apply to the vector types extension is not
763 precisely specified.) */
764 if (code1 == VECTOR_TYPE)
765 return t1;
767 if (code2 == VECTOR_TYPE)
768 return t2;
770 /* If one type is complex, form the common type of the non-complex
771 components, then make that complex. Use T1 or T2 if it is the
772 required type. */
773 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
775 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
776 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
777 tree subtype = c_common_type (subtype1, subtype2);
779 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
780 return t1;
781 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
782 return t2;
783 else
784 return build_complex_type (subtype);
787 /* If only one is real, use it as the result. */
789 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
790 return t1;
792 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
793 return t2;
795 /* If both are real and either are decimal floating point types, use
796 the decimal floating point type with the greater precision. */
798 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
800 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
801 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
802 return dfloat128_type_node;
803 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
804 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
805 return dfloat64_type_node;
806 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
807 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
808 return dfloat32_type_node;
811 /* Deal with fixed-point types. */
812 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
814 unsigned int unsignedp = 0, satp = 0;
815 scalar_mode m1, m2;
816 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
818 m1 = SCALAR_TYPE_MODE (t1);
819 m2 = SCALAR_TYPE_MODE (t2);
821 /* If one input type is saturating, the result type is saturating. */
822 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
823 satp = 1;
825 /* If both fixed-point types are unsigned, the result type is unsigned.
826 When mixing fixed-point and integer types, follow the sign of the
827 fixed-point type.
828 Otherwise, the result type is signed. */
829 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
830 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
831 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t1))
833 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t2)))
835 unsignedp = 1;
837 /* The result type is signed. */
838 if (unsignedp == 0)
840 /* If the input type is unsigned, we need to convert to the
841 signed type. */
842 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
844 enum mode_class mclass = (enum mode_class) 0;
845 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
846 mclass = MODE_FRACT;
847 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
848 mclass = MODE_ACCUM;
849 else
850 gcc_unreachable ();
851 m1 = as_a <scalar_mode>
852 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
854 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
856 enum mode_class mclass = (enum mode_class) 0;
857 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
858 mclass = MODE_FRACT;
859 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
860 mclass = MODE_ACCUM;
861 else
862 gcc_unreachable ();
863 m2 = as_a <scalar_mode>
864 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
868 if (code1 == FIXED_POINT_TYPE)
870 fbit1 = GET_MODE_FBIT (m1);
871 ibit1 = GET_MODE_IBIT (m1);
873 else
875 fbit1 = 0;
876 /* Signed integers need to subtract one sign bit. */
877 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
880 if (code2 == FIXED_POINT_TYPE)
882 fbit2 = GET_MODE_FBIT (m2);
883 ibit2 = GET_MODE_IBIT (m2);
885 else
887 fbit2 = 0;
888 /* Signed integers need to subtract one sign bit. */
889 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
892 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
893 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
894 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
895 satp);
898 /* Both real or both integers; use the one with greater precision. */
900 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
901 return t1;
902 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
903 return t2;
905 /* Same precision. Prefer long longs to longs to ints when the
906 same precision, following the C99 rules on integer type rank
907 (which are equivalent to the C90 rules for C90 types). */
909 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
911 return long_long_unsigned_type_node;
913 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
914 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
916 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
917 return long_long_unsigned_type_node;
918 else
919 return long_long_integer_type_node;
922 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
923 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
924 return long_unsigned_type_node;
926 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
927 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
929 /* But preserve unsignedness from the other type,
930 since long cannot hold all the values of an unsigned int. */
931 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
932 return long_unsigned_type_node;
933 else
934 return long_integer_type_node;
937 /* For floating types of the same TYPE_PRECISION (which we here
938 assume means either the same set of values, or sets of values
939 neither a subset of the other, with behavior being undefined in
940 the latter case), follow the rules from TS 18661-3: prefer
941 interchange types _FloatN, then standard types long double,
942 double, float, then extended types _FloatNx. For extended types,
943 check them starting with _Float128x as that seems most consistent
944 in spirit with preferring long double to double; for interchange
945 types, also check in that order for consistency although it's not
946 possible for more than one of them to have the same
947 precision. */
948 tree mv1 = TYPE_MAIN_VARIANT (t1);
949 tree mv2 = TYPE_MAIN_VARIANT (t2);
951 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
952 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
953 return FLOATN_TYPE_NODE (i);
955 /* Likewise, prefer long double to double even if same size. */
956 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
957 return long_double_type_node;
959 /* Likewise, prefer double to float even if same size.
960 We got a couple of embedded targets with 32 bit doubles, and the
961 pdp11 might have 64 bit floats. */
962 if (mv1 == double_type_node || mv2 == double_type_node)
963 return double_type_node;
965 if (mv1 == float_type_node || mv2 == float_type_node)
966 return float_type_node;
968 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
969 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
970 return FLOATNX_TYPE_NODE (i);
972 /* Otherwise prefer the unsigned one. */
974 if (TYPE_UNSIGNED (t1))
975 return t1;
976 else
977 return t2;
980 /* Wrapper around c_common_type that is used by c-common.c and other
981 front end optimizations that remove promotions. ENUMERAL_TYPEs
982 are allowed here and are converted to their compatible integer types.
983 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
984 preferably a non-Boolean type as the common type. */
985 tree
986 common_type (tree t1, tree t2)
988 if (TREE_CODE (t1) == ENUMERAL_TYPE)
989 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
990 if (TREE_CODE (t2) == ENUMERAL_TYPE)
991 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
993 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
994 if (TREE_CODE (t1) == BOOLEAN_TYPE
995 && TREE_CODE (t2) == BOOLEAN_TYPE)
996 return boolean_type_node;
998 /* If either type is BOOLEAN_TYPE, then return the other. */
999 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1000 return t2;
1001 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1002 return t1;
1004 return c_common_type (t1, t2);
1007 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1008 or various other operations. Return 2 if they are compatible
1009 but a warning may be needed if you use them together. */
1012 comptypes (tree type1, tree type2)
1014 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1015 int val;
1017 val = comptypes_internal (type1, type2, NULL, NULL);
1018 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1020 return val;
1023 /* Like comptypes, but if it returns non-zero because enum and int are
1024 compatible, it sets *ENUM_AND_INT_P to true. */
1026 static int
1027 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1029 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1030 int val;
1032 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1033 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1035 return val;
1038 /* Like comptypes, but if it returns nonzero for different types, it
1039 sets *DIFFERENT_TYPES_P to true. */
1042 comptypes_check_different_types (tree type1, tree type2,
1043 bool *different_types_p)
1045 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1046 int val;
1048 val = comptypes_internal (type1, type2, NULL, different_types_p);
1049 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1051 return val;
1054 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1055 or various other operations. Return 2 if they are compatible
1056 but a warning may be needed if you use them together. If
1057 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1058 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1059 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1060 NULL, and the types are compatible but different enough not to be
1061 permitted in C11 typedef redeclarations, then this sets
1062 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1063 false, but may or may not be set if the types are incompatible.
1064 This differs from comptypes, in that we don't free the seen
1065 types. */
1067 static int
1068 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1069 bool *different_types_p)
1071 const_tree t1 = type1;
1072 const_tree t2 = type2;
1073 int attrval, val;
1075 /* Suppress errors caused by previously reported errors. */
1077 if (t1 == t2 || !t1 || !t2
1078 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1079 return 1;
1081 /* Enumerated types are compatible with integer types, but this is
1082 not transitive: two enumerated types in the same translation unit
1083 are compatible with each other only if they are the same type. */
1085 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1087 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1088 if (TREE_CODE (t2) != VOID_TYPE)
1090 if (enum_and_int_p != NULL)
1091 *enum_and_int_p = true;
1092 if (different_types_p != NULL)
1093 *different_types_p = true;
1096 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1098 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1099 if (TREE_CODE (t1) != VOID_TYPE)
1101 if (enum_and_int_p != NULL)
1102 *enum_and_int_p = true;
1103 if (different_types_p != NULL)
1104 *different_types_p = true;
1108 if (t1 == t2)
1109 return 1;
1111 /* Different classes of types can't be compatible. */
1113 if (TREE_CODE (t1) != TREE_CODE (t2))
1114 return 0;
1116 /* Qualifiers must match. C99 6.7.3p9 */
1118 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1119 return 0;
1121 /* Allow for two different type nodes which have essentially the same
1122 definition. Note that we already checked for equality of the type
1123 qualifiers (just above). */
1125 if (TREE_CODE (t1) != ARRAY_TYPE
1126 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1127 return 1;
1129 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1130 if (!(attrval = comp_type_attributes (t1, t2)))
1131 return 0;
1133 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1134 val = 0;
1136 switch (TREE_CODE (t1))
1138 case INTEGER_TYPE:
1139 case FIXED_POINT_TYPE:
1140 case REAL_TYPE:
1141 /* With these nodes, we can't determine type equivalence by
1142 looking at what is stored in the nodes themselves, because
1143 two nodes might have different TYPE_MAIN_VARIANTs but still
1144 represent the same type. For example, wchar_t and int could
1145 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1146 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1147 and are distinct types. On the other hand, int and the
1148 following typedef
1150 typedef int INT __attribute((may_alias));
1152 have identical properties, different TYPE_MAIN_VARIANTs, but
1153 represent the same type. The canonical type system keeps
1154 track of equivalence in this case, so we fall back on it. */
1155 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1157 case POINTER_TYPE:
1158 /* Do not remove mode information. */
1159 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1160 break;
1161 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1162 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1163 enum_and_int_p, different_types_p));
1164 break;
1166 case FUNCTION_TYPE:
1167 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1168 different_types_p);
1169 break;
1171 case ARRAY_TYPE:
1173 tree d1 = TYPE_DOMAIN (t1);
1174 tree d2 = TYPE_DOMAIN (t2);
1175 bool d1_variable, d2_variable;
1176 bool d1_zero, d2_zero;
1177 val = 1;
1179 /* Target types must match incl. qualifiers. */
1180 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1181 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1182 enum_and_int_p,
1183 different_types_p)))
1184 return 0;
1186 if (different_types_p != NULL
1187 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1188 *different_types_p = true;
1189 /* Sizes must match unless one is missing or variable. */
1190 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1191 break;
1193 d1_zero = !TYPE_MAX_VALUE (d1);
1194 d2_zero = !TYPE_MAX_VALUE (d2);
1196 d1_variable = (!d1_zero
1197 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1198 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1199 d2_variable = (!d2_zero
1200 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1201 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1202 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1203 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1205 if (different_types_p != NULL
1206 && d1_variable != d2_variable)
1207 *different_types_p = true;
1208 if (d1_variable || d2_variable)
1209 break;
1210 if (d1_zero && d2_zero)
1211 break;
1212 if (d1_zero || d2_zero
1213 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1214 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1215 val = 0;
1217 break;
1220 case ENUMERAL_TYPE:
1221 case RECORD_TYPE:
1222 case UNION_TYPE:
1223 if (val != 1 && !same_translation_unit_p (t1, t2))
1225 tree a1 = TYPE_ATTRIBUTES (t1);
1226 tree a2 = TYPE_ATTRIBUTES (t2);
1228 if (! attribute_list_contained (a1, a2)
1229 && ! attribute_list_contained (a2, a1))
1230 break;
1232 if (attrval != 2)
1233 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1234 different_types_p);
1235 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1236 different_types_p);
1238 break;
1240 case VECTOR_TYPE:
1241 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1242 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1243 enum_and_int_p, different_types_p));
1244 break;
1246 default:
1247 break;
1249 return attrval == 2 && val == 1 ? 2 : val;
1252 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1253 their qualifiers, except for named address spaces. If the pointers point to
1254 different named addresses, then we must determine if one address space is a
1255 subset of the other. */
1257 static int
1258 comp_target_types (location_t location, tree ttl, tree ttr)
1260 int val;
1261 int val_ped;
1262 tree mvl = TREE_TYPE (ttl);
1263 tree mvr = TREE_TYPE (ttr);
1264 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1265 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1266 addr_space_t as_common;
1267 bool enum_and_int_p;
1269 /* Fail if pointers point to incompatible address spaces. */
1270 if (!addr_space_superset (asl, asr, &as_common))
1271 return 0;
1273 /* For pedantic record result of comptypes on arrays before losing
1274 qualifiers on the element type below. */
1275 val_ped = 1;
1277 if (TREE_CODE (mvl) == ARRAY_TYPE
1278 && TREE_CODE (mvr) == ARRAY_TYPE)
1279 val_ped = comptypes (mvl, mvr);
1281 /* Qualifiers on element types of array types that are
1282 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1284 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1285 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1286 : TYPE_MAIN_VARIANT (mvl));
1288 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1289 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1290 : TYPE_MAIN_VARIANT (mvr));
1292 enum_and_int_p = false;
1293 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1295 if (val == 1 && val_ped != 1)
1296 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1297 "are incompatible in ISO C");
1299 if (val == 2)
1300 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1302 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1303 warning_at (location, OPT_Wc___compat,
1304 "pointer target types incompatible in C++");
1306 return val;
1309 /* Subroutines of `comptypes'. */
1311 /* Determine whether two trees derive from the same translation unit.
1312 If the CONTEXT chain ends in a null, that tree's context is still
1313 being parsed, so if two trees have context chains ending in null,
1314 they're in the same translation unit. */
1316 bool
1317 same_translation_unit_p (const_tree t1, const_tree t2)
1319 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1320 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1322 case tcc_declaration:
1323 t1 = DECL_CONTEXT (t1); break;
1324 case tcc_type:
1325 t1 = TYPE_CONTEXT (t1); break;
1326 case tcc_exceptional:
1327 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1328 default: gcc_unreachable ();
1331 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1332 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1334 case tcc_declaration:
1335 t2 = DECL_CONTEXT (t2); break;
1336 case tcc_type:
1337 t2 = TYPE_CONTEXT (t2); break;
1338 case tcc_exceptional:
1339 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1340 default: gcc_unreachable ();
1343 return t1 == t2;
1346 /* Allocate the seen two types, assuming that they are compatible. */
1348 static struct tagged_tu_seen_cache *
1349 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1351 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1352 tu->next = tagged_tu_seen_base;
1353 tu->t1 = t1;
1354 tu->t2 = t2;
1356 tagged_tu_seen_base = tu;
1358 /* The C standard says that two structures in different translation
1359 units are compatible with each other only if the types of their
1360 fields are compatible (among other things). We assume that they
1361 are compatible until proven otherwise when building the cache.
1362 An example where this can occur is:
1363 struct a
1365 struct a *next;
1367 If we are comparing this against a similar struct in another TU,
1368 and did not assume they were compatible, we end up with an infinite
1369 loop. */
1370 tu->val = 1;
1371 return tu;
1374 /* Free the seen types until we get to TU_TIL. */
1376 static void
1377 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1379 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1380 while (tu != tu_til)
1382 const struct tagged_tu_seen_cache *const tu1
1383 = (const struct tagged_tu_seen_cache *) tu;
1384 tu = tu1->next;
1385 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1387 tagged_tu_seen_base = tu_til;
1390 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1391 compatible. If the two types are not the same (which has been
1392 checked earlier), this can only happen when multiple translation
1393 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1394 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1395 comptypes_internal. */
1397 static int
1398 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1399 bool *enum_and_int_p, bool *different_types_p)
1401 tree s1, s2;
1402 bool needs_warning = false;
1404 /* We have to verify that the tags of the types are the same. This
1405 is harder than it looks because this may be a typedef, so we have
1406 to go look at the original type. It may even be a typedef of a
1407 typedef...
1408 In the case of compiler-created builtin structs the TYPE_DECL
1409 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1410 while (TYPE_NAME (t1)
1411 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1412 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1413 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1415 while (TYPE_NAME (t2)
1416 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1417 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1418 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1420 /* C90 didn't have the requirement that the two tags be the same. */
1421 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1422 return 0;
1424 /* C90 didn't say what happened if one or both of the types were
1425 incomplete; we choose to follow C99 rules here, which is that they
1426 are compatible. */
1427 if (TYPE_SIZE (t1) == NULL
1428 || TYPE_SIZE (t2) == NULL)
1429 return 1;
1432 const struct tagged_tu_seen_cache * tts_i;
1433 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1434 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1435 return tts_i->val;
1438 switch (TREE_CODE (t1))
1440 case ENUMERAL_TYPE:
1442 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1443 /* Speed up the case where the type values are in the same order. */
1444 tree tv1 = TYPE_VALUES (t1);
1445 tree tv2 = TYPE_VALUES (t2);
1447 if (tv1 == tv2)
1449 return 1;
1452 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1454 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1455 break;
1456 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1458 tu->val = 0;
1459 return 0;
1463 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1465 return 1;
1467 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1469 tu->val = 0;
1470 return 0;
1473 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1475 tu->val = 0;
1476 return 0;
1479 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1481 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1482 if (s2 == NULL
1483 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1485 tu->val = 0;
1486 return 0;
1489 return 1;
1492 case UNION_TYPE:
1494 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1495 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1497 tu->val = 0;
1498 return 0;
1501 /* Speed up the common case where the fields are in the same order. */
1502 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1503 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1505 int result;
1507 if (DECL_NAME (s1) != DECL_NAME (s2))
1508 break;
1509 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1510 enum_and_int_p, different_types_p);
1512 if (result != 1 && !DECL_NAME (s1))
1513 break;
1514 if (result == 0)
1516 tu->val = 0;
1517 return 0;
1519 if (result == 2)
1520 needs_warning = true;
1522 if (TREE_CODE (s1) == FIELD_DECL
1523 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1524 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1526 tu->val = 0;
1527 return 0;
1530 if (!s1 && !s2)
1532 tu->val = needs_warning ? 2 : 1;
1533 return tu->val;
1536 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1538 bool ok = false;
1540 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1541 if (DECL_NAME (s1) == DECL_NAME (s2))
1543 int result;
1545 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1546 enum_and_int_p,
1547 different_types_p);
1549 if (result != 1 && !DECL_NAME (s1))
1550 continue;
1551 if (result == 0)
1553 tu->val = 0;
1554 return 0;
1556 if (result == 2)
1557 needs_warning = true;
1559 if (TREE_CODE (s1) == FIELD_DECL
1560 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1561 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1562 break;
1564 ok = true;
1565 break;
1567 if (!ok)
1569 tu->val = 0;
1570 return 0;
1573 tu->val = needs_warning ? 2 : 10;
1574 return tu->val;
1577 case RECORD_TYPE:
1579 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1581 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1582 s1 && s2;
1583 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1585 int result;
1586 if (TREE_CODE (s1) != TREE_CODE (s2)
1587 || DECL_NAME (s1) != DECL_NAME (s2))
1588 break;
1589 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1590 enum_and_int_p, different_types_p);
1591 if (result == 0)
1592 break;
1593 if (result == 2)
1594 needs_warning = true;
1596 if (TREE_CODE (s1) == FIELD_DECL
1597 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1598 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1599 break;
1601 if (s1 && s2)
1602 tu->val = 0;
1603 else
1604 tu->val = needs_warning ? 2 : 1;
1605 return tu->val;
1608 default:
1609 gcc_unreachable ();
1613 /* Return 1 if two function types F1 and F2 are compatible.
1614 If either type specifies no argument types,
1615 the other must specify a fixed number of self-promoting arg types.
1616 Otherwise, if one type specifies only the number of arguments,
1617 the other must specify that number of self-promoting arg types.
1618 Otherwise, the argument types must match.
1619 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1621 static int
1622 function_types_compatible_p (const_tree f1, const_tree f2,
1623 bool *enum_and_int_p, bool *different_types_p)
1625 tree args1, args2;
1626 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1627 int val = 1;
1628 int val1;
1629 tree ret1, ret2;
1631 ret1 = TREE_TYPE (f1);
1632 ret2 = TREE_TYPE (f2);
1634 /* 'volatile' qualifiers on a function's return type used to mean
1635 the function is noreturn. */
1636 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1637 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1638 if (TYPE_VOLATILE (ret1))
1639 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1640 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1641 if (TYPE_VOLATILE (ret2))
1642 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1643 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1644 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1645 if (val == 0)
1646 return 0;
1648 args1 = TYPE_ARG_TYPES (f1);
1649 args2 = TYPE_ARG_TYPES (f2);
1651 if (different_types_p != NULL
1652 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1653 *different_types_p = true;
1655 /* An unspecified parmlist matches any specified parmlist
1656 whose argument types don't need default promotions. */
1658 if (args1 == NULL_TREE)
1660 if (!self_promoting_args_p (args2))
1661 return 0;
1662 /* If one of these types comes from a non-prototype fn definition,
1663 compare that with the other type's arglist.
1664 If they don't match, ask for a warning (but no error). */
1665 if (TYPE_ACTUAL_ARG_TYPES (f1)
1666 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1667 enum_and_int_p, different_types_p))
1668 val = 2;
1669 return val;
1671 if (args2 == NULL_TREE)
1673 if (!self_promoting_args_p (args1))
1674 return 0;
1675 if (TYPE_ACTUAL_ARG_TYPES (f2)
1676 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1677 enum_and_int_p, different_types_p))
1678 val = 2;
1679 return val;
1682 /* Both types have argument lists: compare them and propagate results. */
1683 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1684 different_types_p);
1685 return val1 != 1 ? val1 : val;
1688 /* Check two lists of types for compatibility, returning 0 for
1689 incompatible, 1 for compatible, or 2 for compatible with
1690 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1691 comptypes_internal. */
1693 static int
1694 type_lists_compatible_p (const_tree args1, const_tree args2,
1695 bool *enum_and_int_p, bool *different_types_p)
1697 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1698 int val = 1;
1699 int newval = 0;
1701 while (1)
1703 tree a1, mv1, a2, mv2;
1704 if (args1 == NULL_TREE && args2 == NULL_TREE)
1705 return val;
1706 /* If one list is shorter than the other,
1707 they fail to match. */
1708 if (args1 == NULL_TREE || args2 == NULL_TREE)
1709 return 0;
1710 mv1 = a1 = TREE_VALUE (args1);
1711 mv2 = a2 = TREE_VALUE (args2);
1712 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1713 mv1 = (TYPE_ATOMIC (mv1)
1714 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1715 TYPE_QUAL_ATOMIC)
1716 : TYPE_MAIN_VARIANT (mv1));
1717 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1718 mv2 = (TYPE_ATOMIC (mv2)
1719 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1720 TYPE_QUAL_ATOMIC)
1721 : TYPE_MAIN_VARIANT (mv2));
1722 /* A null pointer instead of a type
1723 means there is supposed to be an argument
1724 but nothing is specified about what type it has.
1725 So match anything that self-promotes. */
1726 if (different_types_p != NULL
1727 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1728 *different_types_p = true;
1729 if (a1 == NULL_TREE)
1731 if (c_type_promotes_to (a2) != a2)
1732 return 0;
1734 else if (a2 == NULL_TREE)
1736 if (c_type_promotes_to (a1) != a1)
1737 return 0;
1739 /* If one of the lists has an error marker, ignore this arg. */
1740 else if (TREE_CODE (a1) == ERROR_MARK
1741 || TREE_CODE (a2) == ERROR_MARK)
1743 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1744 different_types_p)))
1746 if (different_types_p != NULL)
1747 *different_types_p = true;
1748 /* Allow wait (union {union wait *u; int *i} *)
1749 and wait (union wait *) to be compatible. */
1750 if (TREE_CODE (a1) == UNION_TYPE
1751 && (TYPE_NAME (a1) == NULL_TREE
1752 || TYPE_TRANSPARENT_AGGR (a1))
1753 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1754 && tree_int_cst_equal (TYPE_SIZE (a1),
1755 TYPE_SIZE (a2)))
1757 tree memb;
1758 for (memb = TYPE_FIELDS (a1);
1759 memb; memb = DECL_CHAIN (memb))
1761 tree mv3 = TREE_TYPE (memb);
1762 if (mv3 && mv3 != error_mark_node
1763 && TREE_CODE (mv3) != ARRAY_TYPE)
1764 mv3 = (TYPE_ATOMIC (mv3)
1765 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1766 TYPE_QUAL_ATOMIC)
1767 : TYPE_MAIN_VARIANT (mv3));
1768 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1769 different_types_p))
1770 break;
1772 if (memb == NULL_TREE)
1773 return 0;
1775 else if (TREE_CODE (a2) == UNION_TYPE
1776 && (TYPE_NAME (a2) == NULL_TREE
1777 || TYPE_TRANSPARENT_AGGR (a2))
1778 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1779 && tree_int_cst_equal (TYPE_SIZE (a2),
1780 TYPE_SIZE (a1)))
1782 tree memb;
1783 for (memb = TYPE_FIELDS (a2);
1784 memb; memb = DECL_CHAIN (memb))
1786 tree mv3 = TREE_TYPE (memb);
1787 if (mv3 && mv3 != error_mark_node
1788 && TREE_CODE (mv3) != ARRAY_TYPE)
1789 mv3 = (TYPE_ATOMIC (mv3)
1790 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1791 TYPE_QUAL_ATOMIC)
1792 : TYPE_MAIN_VARIANT (mv3));
1793 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1794 different_types_p))
1795 break;
1797 if (memb == NULL_TREE)
1798 return 0;
1800 else
1801 return 0;
1804 /* comptypes said ok, but record if it said to warn. */
1805 if (newval > val)
1806 val = newval;
1808 args1 = TREE_CHAIN (args1);
1809 args2 = TREE_CHAIN (args2);
1813 /* Compute the size to increment a pointer by. When a function type or void
1814 type or incomplete type is passed, size_one_node is returned.
1815 This function does not emit any diagnostics; the caller is responsible
1816 for that. */
1818 static tree
1819 c_size_in_bytes (const_tree type)
1821 enum tree_code code = TREE_CODE (type);
1823 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1824 || !COMPLETE_TYPE_P (type))
1825 return size_one_node;
1827 /* Convert in case a char is more than one unit. */
1828 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1829 size_int (TYPE_PRECISION (char_type_node)
1830 / BITS_PER_UNIT));
1833 /* Return either DECL or its known constant value (if it has one). */
1835 tree
1836 decl_constant_value (tree decl)
1838 if (/* Don't change a variable array bound or initial value to a constant
1839 in a place where a variable is invalid. Note that DECL_INITIAL
1840 isn't valid for a PARM_DECL. */
1841 current_function_decl != NULL_TREE
1842 && TREE_CODE (decl) != PARM_DECL
1843 && !TREE_THIS_VOLATILE (decl)
1844 && TREE_READONLY (decl)
1845 && DECL_INITIAL (decl) != NULL_TREE
1846 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1847 /* This is invalid if initial value is not constant.
1848 If it has either a function call, a memory reference,
1849 or a variable, then re-evaluating it could give different results. */
1850 && TREE_CONSTANT (DECL_INITIAL (decl))
1851 /* Check for cases where this is sub-optimal, even though valid. */
1852 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1853 return DECL_INITIAL (decl);
1854 return decl;
1857 /* Convert the array expression EXP to a pointer. */
1858 static tree
1859 array_to_pointer_conversion (location_t loc, tree exp)
1861 tree orig_exp = exp;
1862 tree type = TREE_TYPE (exp);
1863 tree adr;
1864 tree restype = TREE_TYPE (type);
1865 tree ptrtype;
1867 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1869 STRIP_TYPE_NOPS (exp);
1871 if (TREE_NO_WARNING (orig_exp))
1872 TREE_NO_WARNING (exp) = 1;
1874 ptrtype = build_pointer_type (restype);
1876 if (INDIRECT_REF_P (exp))
1877 return convert (ptrtype, TREE_OPERAND (exp, 0));
1879 /* In C++ array compound literals are temporary objects unless they are
1880 const or appear in namespace scope, so they are destroyed too soon
1881 to use them for much of anything (c++/53220). */
1882 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1884 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1885 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1886 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1887 "converting an array compound literal to a pointer "
1888 "is ill-formed in C++");
1891 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1892 return convert (ptrtype, adr);
1895 /* Convert the function expression EXP to a pointer. */
1896 static tree
1897 function_to_pointer_conversion (location_t loc, tree exp)
1899 tree orig_exp = exp;
1901 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1903 STRIP_TYPE_NOPS (exp);
1905 if (TREE_NO_WARNING (orig_exp))
1906 TREE_NO_WARNING (exp) = 1;
1908 return build_unary_op (loc, ADDR_EXPR, exp, false);
1911 /* Mark EXP as read, not just set, for set but not used -Wunused
1912 warning purposes. */
1914 void
1915 mark_exp_read (tree exp)
1917 switch (TREE_CODE (exp))
1919 case VAR_DECL:
1920 case PARM_DECL:
1921 DECL_READ_P (exp) = 1;
1922 break;
1923 case ARRAY_REF:
1924 case COMPONENT_REF:
1925 case MODIFY_EXPR:
1926 case REALPART_EXPR:
1927 case IMAGPART_EXPR:
1928 CASE_CONVERT:
1929 case ADDR_EXPR:
1930 case VIEW_CONVERT_EXPR:
1931 mark_exp_read (TREE_OPERAND (exp, 0));
1932 break;
1933 case COMPOUND_EXPR:
1934 case C_MAYBE_CONST_EXPR:
1935 mark_exp_read (TREE_OPERAND (exp, 1));
1936 break;
1937 default:
1938 break;
1942 /* Perform the default conversion of arrays and functions to pointers.
1943 Return the result of converting EXP. For any other expression, just
1944 return EXP.
1946 LOC is the location of the expression. */
1948 struct c_expr
1949 default_function_array_conversion (location_t loc, struct c_expr exp)
1951 tree orig_exp = exp.value;
1952 tree type = TREE_TYPE (exp.value);
1953 enum tree_code code = TREE_CODE (type);
1955 switch (code)
1957 case ARRAY_TYPE:
1959 bool not_lvalue = false;
1960 bool lvalue_array_p;
1962 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1963 || CONVERT_EXPR_P (exp.value))
1964 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1966 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1967 not_lvalue = true;
1968 exp.value = TREE_OPERAND (exp.value, 0);
1971 if (TREE_NO_WARNING (orig_exp))
1972 TREE_NO_WARNING (exp.value) = 1;
1974 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1975 if (!flag_isoc99 && !lvalue_array_p)
1977 /* Before C99, non-lvalue arrays do not decay to pointers.
1978 Normally, using such an array would be invalid; but it can
1979 be used correctly inside sizeof or as a statement expression.
1980 Thus, do not give an error here; an error will result later. */
1981 return exp;
1984 exp.value = array_to_pointer_conversion (loc, exp.value);
1986 break;
1987 case FUNCTION_TYPE:
1988 exp.value = function_to_pointer_conversion (loc, exp.value);
1989 break;
1990 default:
1991 break;
1994 return exp;
1997 struct c_expr
1998 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2000 mark_exp_read (exp.value);
2001 return default_function_array_conversion (loc, exp);
2004 /* Return whether EXPR should be treated as an atomic lvalue for the
2005 purposes of load and store handling. */
2007 static bool
2008 really_atomic_lvalue (tree expr)
2010 if (error_operand_p (expr))
2011 return false;
2012 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2013 return false;
2014 if (!lvalue_p (expr))
2015 return false;
2017 /* Ignore _Atomic on register variables, since their addresses can't
2018 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2019 sequences wouldn't work. Ignore _Atomic on structures containing
2020 bit-fields, since accessing elements of atomic structures or
2021 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2022 it's undefined at translation time or execution time, and the
2023 normal atomic sequences again wouldn't work. */
2024 while (handled_component_p (expr))
2026 if (TREE_CODE (expr) == COMPONENT_REF
2027 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2028 return false;
2029 expr = TREE_OPERAND (expr, 0);
2031 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2032 return false;
2033 return true;
2036 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2037 including converting functions and arrays to pointers if CONVERT_P.
2038 If READ_P, also mark the expression as having been read. */
2040 struct c_expr
2041 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2042 bool convert_p, bool read_p)
2044 if (read_p)
2045 mark_exp_read (exp.value);
2046 if (convert_p)
2047 exp = default_function_array_conversion (loc, exp);
2048 if (really_atomic_lvalue (exp.value))
2050 vec<tree, va_gc> *params;
2051 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2052 tree expr_type = TREE_TYPE (exp.value);
2053 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2054 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2056 gcc_assert (TYPE_ATOMIC (expr_type));
2058 /* Expansion of a generic atomic load may require an addition
2059 element, so allocate enough to prevent a resize. */
2060 vec_alloc (params, 4);
2062 /* Remove the qualifiers for the rest of the expressions and
2063 create the VAL temp variable to hold the RHS. */
2064 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2065 tmp = create_tmp_var_raw (nonatomic_type);
2066 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2067 TREE_ADDRESSABLE (tmp) = 1;
2068 TREE_NO_WARNING (tmp) = 1;
2070 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2071 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2072 params->quick_push (expr_addr);
2073 params->quick_push (tmp_addr);
2074 params->quick_push (seq_cst);
2075 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2077 /* EXPR is always read. */
2078 mark_exp_read (exp.value);
2080 /* Return tmp which contains the value loaded. */
2081 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2082 NULL_TREE, NULL_TREE);
2084 return exp;
2087 /* EXP is an expression of integer type. Apply the integer promotions
2088 to it and return the promoted value. */
2090 tree
2091 perform_integral_promotions (tree exp)
2093 tree type = TREE_TYPE (exp);
2094 enum tree_code code = TREE_CODE (type);
2096 gcc_assert (INTEGRAL_TYPE_P (type));
2098 /* Normally convert enums to int,
2099 but convert wide enums to something wider. */
2100 if (code == ENUMERAL_TYPE)
2102 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2103 TYPE_PRECISION (integer_type_node)),
2104 ((TYPE_PRECISION (type)
2105 >= TYPE_PRECISION (integer_type_node))
2106 && TYPE_UNSIGNED (type)));
2108 return convert (type, exp);
2111 /* ??? This should no longer be needed now bit-fields have their
2112 proper types. */
2113 if (TREE_CODE (exp) == COMPONENT_REF
2114 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2115 /* If it's thinner than an int, promote it like a
2116 c_promoting_integer_type_p, otherwise leave it alone. */
2117 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2118 TYPE_PRECISION (integer_type_node)))
2119 return convert (integer_type_node, exp);
2121 if (c_promoting_integer_type_p (type))
2123 /* Preserve unsignedness if not really getting any wider. */
2124 if (TYPE_UNSIGNED (type)
2125 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2126 return convert (unsigned_type_node, exp);
2128 return convert (integer_type_node, exp);
2131 return exp;
2135 /* Perform default promotions for C data used in expressions.
2136 Enumeral types or short or char are converted to int.
2137 In addition, manifest constants symbols are replaced by their values. */
2139 tree
2140 default_conversion (tree exp)
2142 tree orig_exp;
2143 tree type = TREE_TYPE (exp);
2144 enum tree_code code = TREE_CODE (type);
2145 tree promoted_type;
2147 mark_exp_read (exp);
2149 /* Functions and arrays have been converted during parsing. */
2150 gcc_assert (code != FUNCTION_TYPE);
2151 if (code == ARRAY_TYPE)
2152 return exp;
2154 /* Constants can be used directly unless they're not loadable. */
2155 if (TREE_CODE (exp) == CONST_DECL)
2156 exp = DECL_INITIAL (exp);
2158 /* Strip no-op conversions. */
2159 orig_exp = exp;
2160 STRIP_TYPE_NOPS (exp);
2162 if (TREE_NO_WARNING (orig_exp))
2163 TREE_NO_WARNING (exp) = 1;
2165 if (code == VOID_TYPE)
2167 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2168 "void value not ignored as it ought to be");
2169 return error_mark_node;
2172 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2173 if (exp == error_mark_node)
2174 return error_mark_node;
2176 promoted_type = targetm.promoted_type (type);
2177 if (promoted_type)
2178 return convert (promoted_type, exp);
2180 if (INTEGRAL_TYPE_P (type))
2181 return perform_integral_promotions (exp);
2183 return exp;
2186 /* Look up COMPONENT in a structure or union TYPE.
2188 If the component name is not found, returns NULL_TREE. Otherwise,
2189 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2190 stepping down the chain to the component, which is in the last
2191 TREE_VALUE of the list. Normally the list is of length one, but if
2192 the component is embedded within (nested) anonymous structures or
2193 unions, the list steps down the chain to the component. */
2195 static tree
2196 lookup_field (tree type, tree component)
2198 tree field;
2200 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2201 to the field elements. Use a binary search on this array to quickly
2202 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2203 will always be set for structures which have many elements. */
2205 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2207 int bot, top, half;
2208 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2210 field = TYPE_FIELDS (type);
2211 bot = 0;
2212 top = TYPE_LANG_SPECIFIC (type)->s->len;
2213 while (top - bot > 1)
2215 half = (top - bot + 1) >> 1;
2216 field = field_array[bot+half];
2218 if (DECL_NAME (field) == NULL_TREE)
2220 /* Step through all anon unions in linear fashion. */
2221 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2223 field = field_array[bot++];
2224 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2226 tree anon = lookup_field (TREE_TYPE (field), component);
2228 if (anon)
2229 return tree_cons (NULL_TREE, field, anon);
2231 /* The Plan 9 compiler permits referring
2232 directly to an anonymous struct/union field
2233 using a typedef name. */
2234 if (flag_plan9_extensions
2235 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2236 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2237 == TYPE_DECL)
2238 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2239 == component))
2240 break;
2244 /* Entire record is only anon unions. */
2245 if (bot > top)
2246 return NULL_TREE;
2248 /* Restart the binary search, with new lower bound. */
2249 continue;
2252 if (DECL_NAME (field) == component)
2253 break;
2254 if (DECL_NAME (field) < component)
2255 bot += half;
2256 else
2257 top = bot + half;
2260 if (DECL_NAME (field_array[bot]) == component)
2261 field = field_array[bot];
2262 else if (DECL_NAME (field) != component)
2263 return NULL_TREE;
2265 else
2267 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2269 if (DECL_NAME (field) == NULL_TREE
2270 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2272 tree anon = lookup_field (TREE_TYPE (field), component);
2274 if (anon)
2275 return tree_cons (NULL_TREE, field, anon);
2277 /* The Plan 9 compiler permits referring directly to an
2278 anonymous struct/union field using a typedef
2279 name. */
2280 if (flag_plan9_extensions
2281 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2282 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2283 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2284 == component))
2285 break;
2288 if (DECL_NAME (field) == component)
2289 break;
2292 if (field == NULL_TREE)
2293 return NULL_TREE;
2296 return tree_cons (NULL_TREE, field, NULL_TREE);
2299 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2301 static void
2302 lookup_field_fuzzy_find_candidates (tree type, tree component,
2303 vec<tree> *candidates)
2305 tree field;
2306 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2308 if (DECL_NAME (field) == NULL_TREE
2309 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2310 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2311 candidates);
2313 if (DECL_NAME (field))
2314 candidates->safe_push (DECL_NAME (field));
2318 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2319 rather than returning a TREE_LIST for an exact match. */
2321 static tree
2322 lookup_field_fuzzy (tree type, tree component)
2324 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2326 /* First, gather a list of candidates. */
2327 auto_vec <tree> candidates;
2329 lookup_field_fuzzy_find_candidates (type, component,
2330 &candidates);
2332 return find_closest_identifier (component, &candidates);
2335 /* Support function for build_component_ref's error-handling.
2337 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2338 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2340 static bool
2341 should_suggest_deref_p (tree datum_type)
2343 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2344 allows "." for ptrs; we could be handling a failed attempt
2345 to access a property. */
2346 if (c_dialect_objc ())
2347 return false;
2349 /* Only suggest it for pointers... */
2350 if (TREE_CODE (datum_type) != POINTER_TYPE)
2351 return false;
2353 /* ...to structs/unions. */
2354 tree underlying_type = TREE_TYPE (datum_type);
2355 enum tree_code code = TREE_CODE (underlying_type);
2356 if (code == RECORD_TYPE || code == UNION_TYPE)
2357 return true;
2358 else
2359 return false;
2362 /* Make an expression to refer to the COMPONENT field of structure or
2363 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2364 location of the COMPONENT_REF. COMPONENT_LOC is the location
2365 of COMPONENT. */
2367 tree
2368 build_component_ref (location_t loc, tree datum, tree component,
2369 location_t component_loc)
2371 tree type = TREE_TYPE (datum);
2372 enum tree_code code = TREE_CODE (type);
2373 tree field = NULL;
2374 tree ref;
2375 bool datum_lvalue = lvalue_p (datum);
2377 if (!objc_is_public (datum, component))
2378 return error_mark_node;
2380 /* Detect Objective-C property syntax object.property. */
2381 if (c_dialect_objc ()
2382 && (ref = objc_maybe_build_component_ref (datum, component)))
2383 return ref;
2385 /* See if there is a field or component with name COMPONENT. */
2387 if (code == RECORD_TYPE || code == UNION_TYPE)
2389 if (!COMPLETE_TYPE_P (type))
2391 c_incomplete_type_error (loc, NULL_TREE, type);
2392 return error_mark_node;
2395 field = lookup_field (type, component);
2397 if (!field)
2399 tree guessed_id = lookup_field_fuzzy (type, component);
2400 if (guessed_id)
2402 /* Attempt to provide a fixit replacement hint, if
2403 we have a valid range for the component. */
2404 location_t reported_loc
2405 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2406 gcc_rich_location rich_loc (reported_loc);
2407 if (component_loc != UNKNOWN_LOCATION)
2408 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2409 error_at_rich_loc
2410 (&rich_loc,
2411 "%qT has no member named %qE; did you mean %qE?",
2412 type, component, guessed_id);
2414 else
2415 error_at (loc, "%qT has no member named %qE", type, component);
2416 return error_mark_node;
2419 /* Accessing elements of atomic structures or unions is undefined
2420 behavior (C11 6.5.2.3#5). */
2421 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2423 if (code == RECORD_TYPE)
2424 warning_at (loc, 0, "accessing a member %qE of an atomic "
2425 "structure %qE", component, datum);
2426 else
2427 warning_at (loc, 0, "accessing a member %qE of an atomic "
2428 "union %qE", component, datum);
2431 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2432 This might be better solved in future the way the C++ front
2433 end does it - by giving the anonymous entities each a
2434 separate name and type, and then have build_component_ref
2435 recursively call itself. We can't do that here. */
2438 tree subdatum = TREE_VALUE (field);
2439 int quals;
2440 tree subtype;
2441 bool use_datum_quals;
2443 if (TREE_TYPE (subdatum) == error_mark_node)
2444 return error_mark_node;
2446 /* If this is an rvalue, it does not have qualifiers in C
2447 standard terms and we must avoid propagating such
2448 qualifiers down to a non-lvalue array that is then
2449 converted to a pointer. */
2450 use_datum_quals = (datum_lvalue
2451 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2453 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2454 if (use_datum_quals)
2455 quals |= TYPE_QUALS (TREE_TYPE (datum));
2456 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2458 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2459 NULL_TREE);
2460 SET_EXPR_LOCATION (ref, loc);
2461 if (TREE_READONLY (subdatum)
2462 || (use_datum_quals && TREE_READONLY (datum)))
2463 TREE_READONLY (ref) = 1;
2464 if (TREE_THIS_VOLATILE (subdatum)
2465 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2466 TREE_THIS_VOLATILE (ref) = 1;
2468 if (TREE_DEPRECATED (subdatum))
2469 warn_deprecated_use (subdatum, NULL_TREE);
2471 datum = ref;
2473 field = TREE_CHAIN (field);
2475 while (field);
2477 return ref;
2479 else if (should_suggest_deref_p (type))
2481 /* Special-case the error message for "ptr.field" for the case
2482 where the user has confused "." vs "->". */
2483 rich_location richloc (line_table, loc);
2484 /* "loc" should be the "." token. */
2485 richloc.add_fixit_replace ("->");
2486 error_at_rich_loc (&richloc,
2487 "%qE is a pointer; did you mean to use %<->%>?",
2488 datum);
2489 return error_mark_node;
2491 else if (code != ERROR_MARK)
2492 error_at (loc,
2493 "request for member %qE in something not a structure or union",
2494 component);
2496 return error_mark_node;
2499 /* Given an expression PTR for a pointer, return an expression
2500 for the value pointed to.
2501 ERRORSTRING is the name of the operator to appear in error messages.
2503 LOC is the location to use for the generated tree. */
2505 tree
2506 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2508 tree pointer = default_conversion (ptr);
2509 tree type = TREE_TYPE (pointer);
2510 tree ref;
2512 if (TREE_CODE (type) == POINTER_TYPE)
2514 if (CONVERT_EXPR_P (pointer)
2515 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2517 /* If a warning is issued, mark it to avoid duplicates from
2518 the backend. This only needs to be done at
2519 warn_strict_aliasing > 2. */
2520 if (warn_strict_aliasing > 2)
2521 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2522 type, TREE_OPERAND (pointer, 0)))
2523 TREE_NO_WARNING (pointer) = 1;
2526 if (TREE_CODE (pointer) == ADDR_EXPR
2527 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2528 == TREE_TYPE (type)))
2530 ref = TREE_OPERAND (pointer, 0);
2531 protected_set_expr_location (ref, loc);
2532 return ref;
2534 else
2536 tree t = TREE_TYPE (type);
2538 ref = build1 (INDIRECT_REF, t, pointer);
2540 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2542 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2544 error_at (loc, "dereferencing pointer to incomplete type "
2545 "%qT", t);
2546 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2548 return error_mark_node;
2550 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2551 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2553 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2554 so that we get the proper error message if the result is used
2555 to assign to. Also, &* is supposed to be a no-op.
2556 And ANSI C seems to specify that the type of the result
2557 should be the const type. */
2558 /* A de-reference of a pointer to const is not a const. It is valid
2559 to change it via some other pointer. */
2560 TREE_READONLY (ref) = TYPE_READONLY (t);
2561 TREE_SIDE_EFFECTS (ref)
2562 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2563 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2564 protected_set_expr_location (ref, loc);
2565 return ref;
2568 else if (TREE_CODE (pointer) != ERROR_MARK)
2569 invalid_indirection_error (loc, type, errstring);
2571 return error_mark_node;
2574 /* This handles expressions of the form "a[i]", which denotes
2575 an array reference.
2577 This is logically equivalent in C to *(a+i), but we may do it differently.
2578 If A is a variable or a member, we generate a primitive ARRAY_REF.
2579 This avoids forcing the array out of registers, and can work on
2580 arrays that are not lvalues (for example, members of structures returned
2581 by functions).
2583 For vector types, allow vector[i] but not i[vector], and create
2584 *(((type*)&vectortype) + i) for the expression.
2586 LOC is the location to use for the returned expression. */
2588 tree
2589 build_array_ref (location_t loc, tree array, tree index)
2591 tree ret;
2592 bool swapped = false;
2593 if (TREE_TYPE (array) == error_mark_node
2594 || TREE_TYPE (index) == error_mark_node)
2595 return error_mark_node;
2597 if (flag_cilkplus && contains_array_notation_expr (index))
2599 size_t rank = 0;
2600 if (!find_rank (loc, index, index, true, &rank))
2601 return error_mark_node;
2602 if (rank > 1)
2604 error_at (loc, "rank of the array's index is greater than 1");
2605 return error_mark_node;
2608 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2609 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2610 /* Allow vector[index] but not index[vector]. */
2611 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2613 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2614 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2616 error_at (loc,
2617 "subscripted value is neither array nor pointer nor vector");
2619 return error_mark_node;
2621 std::swap (array, index);
2622 swapped = true;
2625 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2627 error_at (loc, "array subscript is not an integer");
2628 return error_mark_node;
2631 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2633 error_at (loc, "subscripted value is pointer to function");
2634 return error_mark_node;
2637 /* ??? Existing practice has been to warn only when the char
2638 index is syntactically the index, not for char[array]. */
2639 if (!swapped)
2640 warn_array_subscript_with_type_char (loc, index);
2642 /* Apply default promotions *after* noticing character types. */
2643 index = default_conversion (index);
2644 if (index == error_mark_node)
2645 return error_mark_node;
2647 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2649 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2650 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2652 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2654 tree rval, type;
2656 /* An array that is indexed by a non-constant
2657 cannot be stored in a register; we must be able to do
2658 address arithmetic on its address.
2659 Likewise an array of elements of variable size. */
2660 if (TREE_CODE (index) != INTEGER_CST
2661 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2662 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2664 if (!c_mark_addressable (array, true))
2665 return error_mark_node;
2667 /* An array that is indexed by a constant value which is not within
2668 the array bounds cannot be stored in a register either; because we
2669 would get a crash in store_bit_field/extract_bit_field when trying
2670 to access a non-existent part of the register. */
2671 if (TREE_CODE (index) == INTEGER_CST
2672 && TYPE_DOMAIN (TREE_TYPE (array))
2673 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2675 if (!c_mark_addressable (array))
2676 return error_mark_node;
2679 if ((pedantic || warn_c90_c99_compat)
2680 && ! was_vector)
2682 tree foo = array;
2683 while (TREE_CODE (foo) == COMPONENT_REF)
2684 foo = TREE_OPERAND (foo, 0);
2685 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2686 pedwarn (loc, OPT_Wpedantic,
2687 "ISO C forbids subscripting %<register%> array");
2688 else if (!lvalue_p (foo))
2689 pedwarn_c90 (loc, OPT_Wpedantic,
2690 "ISO C90 forbids subscripting non-lvalue "
2691 "array");
2694 type = TREE_TYPE (TREE_TYPE (array));
2695 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2696 /* Array ref is const/volatile if the array elements are
2697 or if the array is. */
2698 TREE_READONLY (rval)
2699 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2700 | TREE_READONLY (array));
2701 TREE_SIDE_EFFECTS (rval)
2702 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2703 | TREE_SIDE_EFFECTS (array));
2704 TREE_THIS_VOLATILE (rval)
2705 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2706 /* This was added by rms on 16 Nov 91.
2707 It fixes vol struct foo *a; a->elts[1]
2708 in an inline function.
2709 Hope it doesn't break something else. */
2710 | TREE_THIS_VOLATILE (array));
2711 ret = require_complete_type (loc, rval);
2712 protected_set_expr_location (ret, loc);
2713 if (non_lvalue)
2714 ret = non_lvalue_loc (loc, ret);
2715 return ret;
2717 else
2719 tree ar = default_conversion (array);
2721 if (ar == error_mark_node)
2722 return ar;
2724 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2725 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2727 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2728 index, false),
2729 RO_ARRAY_INDEXING);
2730 if (non_lvalue)
2731 ret = non_lvalue_loc (loc, ret);
2732 return ret;
2736 /* Build an external reference to identifier ID. FUN indicates
2737 whether this will be used for a function call. LOC is the source
2738 location of the identifier. This sets *TYPE to the type of the
2739 identifier, which is not the same as the type of the returned value
2740 for CONST_DECLs defined as enum constants. If the type of the
2741 identifier is not available, *TYPE is set to NULL. */
2742 tree
2743 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2745 tree ref;
2746 tree decl = lookup_name (id);
2748 /* In Objective-C, an instance variable (ivar) may be preferred to
2749 whatever lookup_name() found. */
2750 decl = objc_lookup_ivar (decl, id);
2752 *type = NULL;
2753 if (decl && decl != error_mark_node)
2755 ref = decl;
2756 *type = TREE_TYPE (ref);
2758 else if (fun)
2759 /* Implicit function declaration. */
2760 ref = implicitly_declare (loc, id);
2761 else if (decl == error_mark_node)
2762 /* Don't complain about something that's already been
2763 complained about. */
2764 return error_mark_node;
2765 else
2767 undeclared_variable (loc, id);
2768 return error_mark_node;
2771 if (TREE_TYPE (ref) == error_mark_node)
2772 return error_mark_node;
2774 if (TREE_DEPRECATED (ref))
2775 warn_deprecated_use (ref, NULL_TREE);
2777 /* Recursive call does not count as usage. */
2778 if (ref != current_function_decl)
2780 TREE_USED (ref) = 1;
2783 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2785 if (!in_sizeof && !in_typeof)
2786 C_DECL_USED (ref) = 1;
2787 else if (DECL_INITIAL (ref) == NULL_TREE
2788 && DECL_EXTERNAL (ref)
2789 && !TREE_PUBLIC (ref))
2790 record_maybe_used_decl (ref);
2793 if (TREE_CODE (ref) == CONST_DECL)
2795 used_types_insert (TREE_TYPE (ref));
2797 if (warn_cxx_compat
2798 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2799 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2801 warning_at (loc, OPT_Wc___compat,
2802 ("enum constant defined in struct or union "
2803 "is not visible in C++"));
2804 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2807 ref = DECL_INITIAL (ref);
2808 TREE_CONSTANT (ref) = 1;
2810 else if (current_function_decl != NULL_TREE
2811 && !DECL_FILE_SCOPE_P (current_function_decl)
2812 && (VAR_OR_FUNCTION_DECL_P (ref)
2813 || TREE_CODE (ref) == PARM_DECL))
2815 tree context = decl_function_context (ref);
2817 if (context != NULL_TREE && context != current_function_decl)
2818 DECL_NONLOCAL (ref) = 1;
2820 /* C99 6.7.4p3: An inline definition of a function with external
2821 linkage ... shall not contain a reference to an identifier with
2822 internal linkage. */
2823 else if (current_function_decl != NULL_TREE
2824 && DECL_DECLARED_INLINE_P (current_function_decl)
2825 && DECL_EXTERNAL (current_function_decl)
2826 && VAR_OR_FUNCTION_DECL_P (ref)
2827 && (!VAR_P (ref) || TREE_STATIC (ref))
2828 && ! TREE_PUBLIC (ref)
2829 && DECL_CONTEXT (ref) != current_function_decl)
2830 record_inline_static (loc, current_function_decl, ref,
2831 csi_internal);
2833 return ref;
2836 /* Record details of decls possibly used inside sizeof or typeof. */
2837 struct maybe_used_decl
2839 /* The decl. */
2840 tree decl;
2841 /* The level seen at (in_sizeof + in_typeof). */
2842 int level;
2843 /* The next one at this level or above, or NULL. */
2844 struct maybe_used_decl *next;
2847 static struct maybe_used_decl *maybe_used_decls;
2849 /* Record that DECL, an undefined static function reference seen
2850 inside sizeof or typeof, might be used if the operand of sizeof is
2851 a VLA type or the operand of typeof is a variably modified
2852 type. */
2854 static void
2855 record_maybe_used_decl (tree decl)
2857 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2858 t->decl = decl;
2859 t->level = in_sizeof + in_typeof;
2860 t->next = maybe_used_decls;
2861 maybe_used_decls = t;
2864 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2865 USED is false, just discard them. If it is true, mark them used
2866 (if no longer inside sizeof or typeof) or move them to the next
2867 level up (if still inside sizeof or typeof). */
2869 void
2870 pop_maybe_used (bool used)
2872 struct maybe_used_decl *p = maybe_used_decls;
2873 int cur_level = in_sizeof + in_typeof;
2874 while (p && p->level > cur_level)
2876 if (used)
2878 if (cur_level == 0)
2879 C_DECL_USED (p->decl) = 1;
2880 else
2881 p->level = cur_level;
2883 p = p->next;
2885 if (!used || cur_level == 0)
2886 maybe_used_decls = p;
2889 /* Return the result of sizeof applied to EXPR. */
2891 struct c_expr
2892 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2894 struct c_expr ret;
2895 if (expr.value == error_mark_node)
2897 ret.value = error_mark_node;
2898 ret.original_code = ERROR_MARK;
2899 ret.original_type = NULL;
2900 pop_maybe_used (false);
2902 else
2904 bool expr_const_operands = true;
2906 if (TREE_CODE (expr.value) == PARM_DECL
2907 && C_ARRAY_PARAMETER (expr.value))
2909 if (warning_at (loc, OPT_Wsizeof_array_argument,
2910 "%<sizeof%> on array function parameter %qE will "
2911 "return size of %qT", expr.value,
2912 TREE_TYPE (expr.value)))
2913 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2915 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2916 &expr_const_operands);
2917 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2918 c_last_sizeof_arg = expr.value;
2919 c_last_sizeof_loc = loc;
2920 ret.original_code = SIZEOF_EXPR;
2921 ret.original_type = NULL;
2922 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2924 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2925 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2926 folded_expr, ret.value);
2927 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2928 SET_EXPR_LOCATION (ret.value, loc);
2930 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2932 return ret;
2935 /* Return the result of sizeof applied to T, a structure for the type
2936 name passed to sizeof (rather than the type itself). LOC is the
2937 location of the original expression. */
2939 struct c_expr
2940 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2942 tree type;
2943 struct c_expr ret;
2944 tree type_expr = NULL_TREE;
2945 bool type_expr_const = true;
2946 type = groktypename (t, &type_expr, &type_expr_const);
2947 ret.value = c_sizeof (loc, type);
2948 c_last_sizeof_arg = type;
2949 c_last_sizeof_loc = loc;
2950 ret.original_code = SIZEOF_EXPR;
2951 ret.original_type = NULL;
2952 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2953 && c_vla_type_p (type))
2955 /* If the type is a [*] array, it is a VLA but is represented as
2956 having a size of zero. In such a case we must ensure that
2957 the result of sizeof does not get folded to a constant by
2958 c_fully_fold, because if the size is evaluated the result is
2959 not constant and so constraints on zero or negative size
2960 arrays must not be applied when this sizeof call is inside
2961 another array declarator. */
2962 if (!type_expr)
2963 type_expr = integer_zero_node;
2964 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2965 type_expr, ret.value);
2966 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2968 pop_maybe_used (type != error_mark_node
2969 ? C_TYPE_VARIABLE_SIZE (type) : false);
2970 return ret;
2973 /* Build a function call to function FUNCTION with parameters PARAMS.
2974 The function call is at LOC.
2975 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2976 TREE_VALUE of each node is a parameter-expression.
2977 FUNCTION's data type may be a function type or a pointer-to-function. */
2979 tree
2980 build_function_call (location_t loc, tree function, tree params)
2982 vec<tree, va_gc> *v;
2983 tree ret;
2985 vec_alloc (v, list_length (params));
2986 for (; params; params = TREE_CHAIN (params))
2987 v->quick_push (TREE_VALUE (params));
2988 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2989 vec_free (v);
2990 return ret;
2993 /* Give a note about the location of the declaration of DECL. */
2995 static void
2996 inform_declaration (tree decl)
2998 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2999 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3002 /* Build a function call to function FUNCTION with parameters PARAMS.
3003 ORIGTYPES, if not NULL, is a vector of types; each element is
3004 either NULL or the original type of the corresponding element in
3005 PARAMS. The original type may differ from TREE_TYPE of the
3006 parameter for enums. FUNCTION's data type may be a function type
3007 or pointer-to-function. This function changes the elements of
3008 PARAMS. */
3010 tree
3011 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3012 tree function, vec<tree, va_gc> *params,
3013 vec<tree, va_gc> *origtypes)
3015 tree fntype, fundecl = NULL_TREE;
3016 tree name = NULL_TREE, result;
3017 tree tem;
3018 int nargs;
3019 tree *argarray;
3022 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3023 STRIP_TYPE_NOPS (function);
3025 /* Convert anything with function type to a pointer-to-function. */
3026 if (TREE_CODE (function) == FUNCTION_DECL)
3028 name = DECL_NAME (function);
3030 if (flag_tm)
3031 tm_malloc_replacement (function);
3032 fundecl = function;
3033 /* Atomic functions have type checking/casting already done. They are
3034 often rewritten and don't match the original parameter list. */
3035 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3036 origtypes = NULL;
3038 if (flag_cilkplus
3039 && is_cilkplus_reduce_builtin (function))
3040 origtypes = NULL;
3042 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3043 function = function_to_pointer_conversion (loc, function);
3045 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3046 expressions, like those used for ObjC messenger dispatches. */
3047 if (params && !params->is_empty ())
3048 function = objc_rewrite_function_call (function, (*params)[0]);
3050 function = c_fully_fold (function, false, NULL);
3052 fntype = TREE_TYPE (function);
3054 if (TREE_CODE (fntype) == ERROR_MARK)
3055 return error_mark_node;
3057 if (!(TREE_CODE (fntype) == POINTER_TYPE
3058 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3060 if (!flag_diagnostics_show_caret)
3061 error_at (loc,
3062 "called object %qE is not a function or function pointer",
3063 function);
3064 else if (DECL_P (function))
3066 error_at (loc,
3067 "called object %qD is not a function or function pointer",
3068 function);
3069 inform_declaration (function);
3071 else
3072 error_at (loc,
3073 "called object is not a function or function pointer");
3074 return error_mark_node;
3077 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3078 current_function_returns_abnormally = 1;
3080 /* fntype now gets the type of function pointed to. */
3081 fntype = TREE_TYPE (fntype);
3083 /* Convert the parameters to the types declared in the
3084 function prototype, or apply default promotions. */
3086 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3087 origtypes, function, fundecl);
3088 if (nargs < 0)
3089 return error_mark_node;
3091 /* Check that the function is called through a compatible prototype.
3092 If it is not, warn. */
3093 if (CONVERT_EXPR_P (function)
3094 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3095 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3096 && !comptypes (fntype, TREE_TYPE (tem)))
3098 tree return_type = TREE_TYPE (fntype);
3100 /* This situation leads to run-time undefined behavior. We can't,
3101 therefore, simply error unless we can prove that all possible
3102 executions of the program must execute the code. */
3103 warning_at (loc, 0, "function called through a non-compatible type");
3105 if (VOID_TYPE_P (return_type)
3106 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3107 pedwarn (loc, 0,
3108 "function with qualified void return type called");
3111 argarray = vec_safe_address (params);
3113 /* Check that arguments to builtin functions match the expectations. */
3114 if (fundecl
3115 && DECL_BUILT_IN (fundecl)
3116 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3117 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3118 argarray))
3119 return error_mark_node;
3121 /* Check that the arguments to the function are valid. */
3122 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3123 nargs, argarray, &arg_loc);
3125 if (name != NULL_TREE
3126 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3128 if (require_constant_value)
3129 result
3130 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3131 function, nargs, argarray);
3132 else
3133 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3134 function, nargs, argarray);
3135 if (TREE_CODE (result) == NOP_EXPR
3136 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3137 STRIP_TYPE_NOPS (result);
3139 else
3140 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3141 function, nargs, argarray);
3142 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3143 later. */
3144 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3145 TREE_NO_WARNING (result) = 1;
3147 /* In this improbable scenario, a nested function returns a VM type.
3148 Create a TARGET_EXPR so that the call always has a LHS, much as
3149 what the C++ FE does for functions returning non-PODs. */
3150 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3152 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3153 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3154 NULL_TREE, NULL_TREE);
3157 if (VOID_TYPE_P (TREE_TYPE (result)))
3159 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3160 pedwarn (loc, 0,
3161 "function with qualified void return type called");
3162 return result;
3164 return require_complete_type (loc, result);
3167 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3169 tree
3170 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3171 tree function, vec<tree, va_gc> *params,
3172 vec<tree, va_gc> *origtypes)
3174 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3175 STRIP_TYPE_NOPS (function);
3177 /* Convert anything with function type to a pointer-to-function. */
3178 if (TREE_CODE (function) == FUNCTION_DECL)
3180 /* Implement type-directed function overloading for builtins.
3181 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3182 handle all the type checking. The result is a complete expression
3183 that implements this function call. */
3184 tree tem = resolve_overloaded_builtin (loc, function, params);
3185 if (tem)
3186 return tem;
3188 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3191 /* Convert the argument expressions in the vector VALUES
3192 to the types in the list TYPELIST.
3194 If TYPELIST is exhausted, or when an element has NULL as its type,
3195 perform the default conversions.
3197 ORIGTYPES is the original types of the expressions in VALUES. This
3198 holds the type of enum values which have been converted to integral
3199 types. It may be NULL.
3201 FUNCTION is a tree for the called function. It is used only for
3202 error messages, where it is formatted with %qE.
3204 This is also where warnings about wrong number of args are generated.
3206 ARG_LOC are locations of function arguments (if any).
3208 Returns the actual number of arguments processed (which may be less
3209 than the length of VALUES in some error situations), or -1 on
3210 failure. */
3212 static int
3213 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3214 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3215 tree function, tree fundecl)
3217 tree typetail, val;
3218 unsigned int parmnum;
3219 bool error_args = false;
3220 const bool type_generic = fundecl
3221 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3222 bool type_generic_remove_excess_precision = false;
3223 bool type_generic_overflow_p = false;
3224 tree selector;
3226 /* Change pointer to function to the function itself for
3227 diagnostics. */
3228 if (TREE_CODE (function) == ADDR_EXPR
3229 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3230 function = TREE_OPERAND (function, 0);
3232 /* Handle an ObjC selector specially for diagnostics. */
3233 selector = objc_message_selector ();
3235 /* For type-generic built-in functions, determine whether excess
3236 precision should be removed (classification) or not
3237 (comparison). */
3238 if (type_generic
3239 && DECL_BUILT_IN (fundecl)
3240 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3242 switch (DECL_FUNCTION_CODE (fundecl))
3244 case BUILT_IN_ISFINITE:
3245 case BUILT_IN_ISINF:
3246 case BUILT_IN_ISINF_SIGN:
3247 case BUILT_IN_ISNAN:
3248 case BUILT_IN_ISNORMAL:
3249 case BUILT_IN_FPCLASSIFY:
3250 type_generic_remove_excess_precision = true;
3251 break;
3253 case BUILT_IN_ADD_OVERFLOW_P:
3254 case BUILT_IN_SUB_OVERFLOW_P:
3255 case BUILT_IN_MUL_OVERFLOW_P:
3256 /* The last argument of these type-generic builtins
3257 should not be promoted. */
3258 type_generic_overflow_p = true;
3259 break;
3261 default:
3262 break;
3265 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3266 return vec_safe_length (values);
3268 /* Scan the given expressions and types, producing individual
3269 converted arguments. */
3271 for (typetail = typelist, parmnum = 0;
3272 values && values->iterate (parmnum, &val);
3273 ++parmnum)
3275 tree type = typetail ? TREE_VALUE (typetail) : 0;
3276 tree valtype = TREE_TYPE (val);
3277 tree rname = function;
3278 int argnum = parmnum + 1;
3279 const char *invalid_func_diag;
3280 bool excess_precision = false;
3281 bool npc;
3282 tree parmval;
3283 /* Some __atomic_* builtins have additional hidden argument at
3284 position 0. */
3285 location_t ploc
3286 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3287 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3288 : input_location;
3290 if (type == void_type_node)
3292 if (selector)
3293 error_at (loc, "too many arguments to method %qE", selector);
3294 else
3295 error_at (loc, "too many arguments to function %qE", function);
3296 inform_declaration (fundecl);
3297 return error_args ? -1 : (int) parmnum;
3300 if (selector && argnum > 2)
3302 rname = selector;
3303 argnum -= 2;
3306 npc = null_pointer_constant_p (val);
3308 /* If there is excess precision and a prototype, convert once to
3309 the required type rather than converting via the semantic
3310 type. Likewise without a prototype a float value represented
3311 as long double should be converted once to double. But for
3312 type-generic classification functions excess precision must
3313 be removed here. */
3314 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3315 && (type || !type_generic || !type_generic_remove_excess_precision))
3317 val = TREE_OPERAND (val, 0);
3318 excess_precision = true;
3320 val = c_fully_fold (val, false, NULL);
3321 STRIP_TYPE_NOPS (val);
3323 val = require_complete_type (ploc, val);
3325 /* Some floating-point arguments must be promoted to double when
3326 no type is specified by a prototype. This applies to
3327 arguments of type float, and to architecture-specific types
3328 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3329 bool promote_float_arg = false;
3330 if (type == NULL_TREE
3331 && TREE_CODE (valtype) == REAL_TYPE
3332 && (TYPE_PRECISION (valtype)
3333 <= TYPE_PRECISION (double_type_node))
3334 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3335 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3336 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3338 /* Promote this argument, unless it has a _FloatN or
3339 _FloatNx type. */
3340 promote_float_arg = true;
3341 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3342 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3344 promote_float_arg = false;
3345 break;
3349 if (type != NULL_TREE)
3351 /* Formal parm type is specified by a function prototype. */
3353 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3355 error_at (ploc, "type of formal parameter %d is incomplete",
3356 parmnum + 1);
3357 parmval = val;
3359 else
3361 tree origtype;
3363 /* Optionally warn about conversions that
3364 differ from the default conversions. */
3365 if (warn_traditional_conversion || warn_traditional)
3367 unsigned int formal_prec = TYPE_PRECISION (type);
3369 if (INTEGRAL_TYPE_P (type)
3370 && TREE_CODE (valtype) == REAL_TYPE)
3371 warning_at (ploc, OPT_Wtraditional_conversion,
3372 "passing argument %d of %qE as integer rather "
3373 "than floating due to prototype",
3374 argnum, rname);
3375 if (INTEGRAL_TYPE_P (type)
3376 && TREE_CODE (valtype) == COMPLEX_TYPE)
3377 warning_at (ploc, OPT_Wtraditional_conversion,
3378 "passing argument %d of %qE as integer rather "
3379 "than complex due to prototype",
3380 argnum, rname);
3381 else if (TREE_CODE (type) == COMPLEX_TYPE
3382 && TREE_CODE (valtype) == REAL_TYPE)
3383 warning_at (ploc, OPT_Wtraditional_conversion,
3384 "passing argument %d of %qE as complex rather "
3385 "than floating due to prototype",
3386 argnum, rname);
3387 else if (TREE_CODE (type) == REAL_TYPE
3388 && INTEGRAL_TYPE_P (valtype))
3389 warning_at (ploc, OPT_Wtraditional_conversion,
3390 "passing argument %d of %qE as floating rather "
3391 "than integer due to prototype",
3392 argnum, rname);
3393 else if (TREE_CODE (type) == COMPLEX_TYPE
3394 && INTEGRAL_TYPE_P (valtype))
3395 warning_at (ploc, OPT_Wtraditional_conversion,
3396 "passing argument %d of %qE as complex rather "
3397 "than integer due to prototype",
3398 argnum, rname);
3399 else if (TREE_CODE (type) == REAL_TYPE
3400 && TREE_CODE (valtype) == COMPLEX_TYPE)
3401 warning_at (ploc, OPT_Wtraditional_conversion,
3402 "passing argument %d of %qE as floating rather "
3403 "than complex due to prototype",
3404 argnum, rname);
3405 /* ??? At some point, messages should be written about
3406 conversions between complex types, but that's too messy
3407 to do now. */
3408 else if (TREE_CODE (type) == REAL_TYPE
3409 && TREE_CODE (valtype) == REAL_TYPE)
3411 /* Warn if any argument is passed as `float',
3412 since without a prototype it would be `double'. */
3413 if (formal_prec == TYPE_PRECISION (float_type_node)
3414 && type != dfloat32_type_node)
3415 warning_at (ploc, 0,
3416 "passing argument %d of %qE as %<float%> "
3417 "rather than %<double%> due to prototype",
3418 argnum, rname);
3420 /* Warn if mismatch between argument and prototype
3421 for decimal float types. Warn of conversions with
3422 binary float types and of precision narrowing due to
3423 prototype. */
3424 else if (type != valtype
3425 && (type == dfloat32_type_node
3426 || type == dfloat64_type_node
3427 || type == dfloat128_type_node
3428 || valtype == dfloat32_type_node
3429 || valtype == dfloat64_type_node
3430 || valtype == dfloat128_type_node)
3431 && (formal_prec
3432 <= TYPE_PRECISION (valtype)
3433 || (type == dfloat128_type_node
3434 && (valtype
3435 != dfloat64_type_node
3436 && (valtype
3437 != dfloat32_type_node)))
3438 || (type == dfloat64_type_node
3439 && (valtype
3440 != dfloat32_type_node))))
3441 warning_at (ploc, 0,
3442 "passing argument %d of %qE as %qT "
3443 "rather than %qT due to prototype",
3444 argnum, rname, type, valtype);
3447 /* Detect integer changing in width or signedness.
3448 These warnings are only activated with
3449 -Wtraditional-conversion, not with -Wtraditional. */
3450 else if (warn_traditional_conversion
3451 && INTEGRAL_TYPE_P (type)
3452 && INTEGRAL_TYPE_P (valtype))
3454 tree would_have_been = default_conversion (val);
3455 tree type1 = TREE_TYPE (would_have_been);
3457 if (val == error_mark_node)
3458 /* VAL could have been of incomplete type. */;
3459 else if (TREE_CODE (type) == ENUMERAL_TYPE
3460 && (TYPE_MAIN_VARIANT (type)
3461 == TYPE_MAIN_VARIANT (valtype)))
3462 /* No warning if function asks for enum
3463 and the actual arg is that enum type. */
3465 else if (formal_prec != TYPE_PRECISION (type1))
3466 warning_at (ploc, OPT_Wtraditional_conversion,
3467 "passing argument %d of %qE "
3468 "with different width due to prototype",
3469 argnum, rname);
3470 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3472 /* Don't complain if the formal parameter type
3473 is an enum, because we can't tell now whether
3474 the value was an enum--even the same enum. */
3475 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3477 else if (TREE_CODE (val) == INTEGER_CST
3478 && int_fits_type_p (val, type))
3479 /* Change in signedness doesn't matter
3480 if a constant value is unaffected. */
3482 /* If the value is extended from a narrower
3483 unsigned type, it doesn't matter whether we
3484 pass it as signed or unsigned; the value
3485 certainly is the same either way. */
3486 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3487 && TYPE_UNSIGNED (valtype))
3489 else if (TYPE_UNSIGNED (type))
3490 warning_at (ploc, OPT_Wtraditional_conversion,
3491 "passing argument %d of %qE "
3492 "as unsigned due to prototype",
3493 argnum, rname);
3494 else
3495 warning_at (ploc, OPT_Wtraditional_conversion,
3496 "passing argument %d of %qE "
3497 "as signed due to prototype",
3498 argnum, rname);
3502 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3503 sake of better warnings from convert_and_check. */
3504 if (excess_precision)
3505 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3506 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3507 parmval = convert_for_assignment (loc, ploc, type,
3508 val, origtype, ic_argpass,
3509 npc, fundecl, function,
3510 parmnum + 1);
3512 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3513 && INTEGRAL_TYPE_P (type)
3514 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3515 parmval = default_conversion (parmval);
3518 else if (promote_float_arg)
3520 if (type_generic)
3521 parmval = val;
3522 else
3524 /* Convert `float' to `double'. */
3525 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3526 warning_at (ploc, OPT_Wdouble_promotion,
3527 "implicit conversion from %qT to %qT when passing "
3528 "argument to function",
3529 valtype, double_type_node);
3530 parmval = convert (double_type_node, val);
3533 else if ((excess_precision && !type_generic)
3534 || (type_generic_overflow_p && parmnum == 2))
3535 /* A "double" argument with excess precision being passed
3536 without a prototype or in variable arguments.
3537 The last argument of __builtin_*_overflow_p should not be
3538 promoted. */
3539 parmval = convert (valtype, val);
3540 else if ((invalid_func_diag =
3541 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3543 error (invalid_func_diag);
3544 return -1;
3546 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3548 return -1;
3550 else
3551 /* Convert `short' and `char' to full-size `int'. */
3552 parmval = default_conversion (val);
3554 (*values)[parmnum] = parmval;
3555 if (parmval == error_mark_node)
3556 error_args = true;
3558 if (typetail)
3559 typetail = TREE_CHAIN (typetail);
3562 gcc_assert (parmnum == vec_safe_length (values));
3564 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3566 error_at (loc, "too few arguments to function %qE", function);
3567 inform_declaration (fundecl);
3568 return -1;
3571 return error_args ? -1 : (int) parmnum;
3574 /* This is the entry point used by the parser to build unary operators
3575 in the input. CODE, a tree_code, specifies the unary operator, and
3576 ARG is the operand. For unary plus, the C parser currently uses
3577 CONVERT_EXPR for code.
3579 LOC is the location to use for the tree generated.
3582 struct c_expr
3583 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3585 struct c_expr result;
3587 result.original_code = code;
3588 result.original_type = NULL;
3590 if (reject_gcc_builtin (arg.value))
3592 result.value = error_mark_node;
3594 else
3596 result.value = build_unary_op (loc, code, arg.value, false);
3598 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3599 overflow_warning (loc, result.value, arg.value);
3602 /* We are typically called when parsing a prefix token at LOC acting on
3603 ARG. Reflect this by updating the source range of the result to
3604 start at LOC and end at the end of ARG. */
3605 set_c_expr_source_range (&result,
3606 loc, arg.get_finish ());
3608 return result;
3611 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3613 static bool
3614 char_type_p (tree type)
3616 return (type == char_type_node
3617 || type == unsigned_char_type_node
3618 || type == signed_char_type_node
3619 || type == char16_type_node
3620 || type == char32_type_node);
3623 /* This is the entry point used by the parser to build binary operators
3624 in the input. CODE, a tree_code, specifies the binary operator, and
3625 ARG1 and ARG2 are the operands. In addition to constructing the
3626 expression, we check for operands that were written with other binary
3627 operators in a way that is likely to confuse the user.
3629 LOCATION is the location of the binary operator. */
3631 struct c_expr
3632 parser_build_binary_op (location_t location, enum tree_code code,
3633 struct c_expr arg1, struct c_expr arg2)
3635 struct c_expr result;
3637 enum tree_code code1 = arg1.original_code;
3638 enum tree_code code2 = arg2.original_code;
3639 tree type1 = (arg1.original_type
3640 ? arg1.original_type
3641 : TREE_TYPE (arg1.value));
3642 tree type2 = (arg2.original_type
3643 ? arg2.original_type
3644 : TREE_TYPE (arg2.value));
3646 result.value = build_binary_op (location, code,
3647 arg1.value, arg2.value, true);
3648 result.original_code = code;
3649 result.original_type = NULL;
3651 if (TREE_CODE (result.value) == ERROR_MARK)
3653 set_c_expr_source_range (&result,
3654 arg1.get_start (),
3655 arg2.get_finish ());
3656 return result;
3659 if (location != UNKNOWN_LOCATION)
3660 protected_set_expr_location (result.value, location);
3662 set_c_expr_source_range (&result,
3663 arg1.get_start (),
3664 arg2.get_finish ());
3666 /* Check for cases such as x+y<<z which users are likely
3667 to misinterpret. */
3668 if (warn_parentheses)
3669 warn_about_parentheses (location, code, code1, arg1.value, code2,
3670 arg2.value);
3672 if (warn_logical_op)
3673 warn_logical_operator (location, code, TREE_TYPE (result.value),
3674 code1, arg1.value, code2, arg2.value);
3676 if (warn_tautological_compare)
3678 tree lhs = arg1.value;
3679 tree rhs = arg2.value;
3680 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3682 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3683 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3684 lhs = NULL_TREE;
3685 else
3686 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3688 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3690 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3691 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3692 rhs = NULL_TREE;
3693 else
3694 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3696 if (lhs != NULL_TREE && rhs != NULL_TREE)
3697 warn_tautological_cmp (location, code, lhs, rhs);
3700 if (warn_logical_not_paren
3701 && TREE_CODE_CLASS (code) == tcc_comparison
3702 && code1 == TRUTH_NOT_EXPR
3703 && code2 != TRUTH_NOT_EXPR
3704 /* Avoid warning for !!x == y. */
3705 && (TREE_CODE (arg1.value) != NE_EXPR
3706 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3708 /* Avoid warning for !b == y where b has _Bool type. */
3709 tree t = integer_zero_node;
3710 if (TREE_CODE (arg1.value) == EQ_EXPR
3711 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3712 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3714 t = TREE_OPERAND (arg1.value, 0);
3717 if (TREE_TYPE (t) != integer_type_node)
3718 break;
3719 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3720 t = C_MAYBE_CONST_EXPR_EXPR (t);
3721 else if (CONVERT_EXPR_P (t))
3722 t = TREE_OPERAND (t, 0);
3723 else
3724 break;
3726 while (1);
3728 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3729 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3732 /* Warn about comparisons against string literals, with the exception
3733 of testing for equality or inequality of a string literal with NULL. */
3734 if (code == EQ_EXPR || code == NE_EXPR)
3736 if ((code1 == STRING_CST
3737 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3738 || (code2 == STRING_CST
3739 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3740 warning_at (location, OPT_Waddress,
3741 "comparison with string literal results in unspecified behavior");
3742 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3743 if (POINTER_TYPE_P (type1)
3744 && null_pointer_constant_p (arg2.value)
3745 && char_type_p (type2)
3746 && warning_at (location, OPT_Wpointer_compare,
3747 "comparison between pointer and zero character "
3748 "constant"))
3749 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3750 else if (POINTER_TYPE_P (type2)
3751 && null_pointer_constant_p (arg1.value)
3752 && char_type_p (type1)
3753 && warning_at (location, OPT_Wpointer_compare,
3754 "comparison between pointer and zero character "
3755 "constant"))
3756 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3758 else if (TREE_CODE_CLASS (code) == tcc_comparison
3759 && (code1 == STRING_CST || code2 == STRING_CST))
3760 warning_at (location, OPT_Waddress,
3761 "comparison with string literal results in unspecified behavior");
3763 if (TREE_OVERFLOW_P (result.value)
3764 && !TREE_OVERFLOW_P (arg1.value)
3765 && !TREE_OVERFLOW_P (arg2.value))
3766 overflow_warning (location, result.value);
3768 /* Warn about comparisons of different enum types. */
3769 if (warn_enum_compare
3770 && TREE_CODE_CLASS (code) == tcc_comparison
3771 && TREE_CODE (type1) == ENUMERAL_TYPE
3772 && TREE_CODE (type2) == ENUMERAL_TYPE
3773 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3774 warning_at (location, OPT_Wenum_compare,
3775 "comparison between %qT and %qT",
3776 type1, type2);
3778 return result;
3781 /* Return a tree for the difference of pointers OP0 and OP1.
3782 The resulting tree has type int. */
3784 static tree
3785 pointer_diff (location_t loc, tree op0, tree op1)
3787 tree restype = ptrdiff_type_node;
3788 tree result, inttype;
3790 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3791 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3792 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3793 tree orig_op1 = op1;
3795 /* If the operands point into different address spaces, we need to
3796 explicitly convert them to pointers into the common address space
3797 before we can subtract the numerical address values. */
3798 if (as0 != as1)
3800 addr_space_t as_common;
3801 tree common_type;
3803 /* Determine the common superset address space. This is guaranteed
3804 to exist because the caller verified that comp_target_types
3805 returned non-zero. */
3806 if (!addr_space_superset (as0, as1, &as_common))
3807 gcc_unreachable ();
3809 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3810 op0 = convert (common_type, op0);
3811 op1 = convert (common_type, op1);
3814 /* Determine integer type to perform computations in. This will usually
3815 be the same as the result type (ptrdiff_t), but may need to be a wider
3816 type if pointers for the address space are wider than ptrdiff_t. */
3817 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3818 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3819 else
3820 inttype = restype;
3822 if (TREE_CODE (target_type) == VOID_TYPE)
3823 pedwarn (loc, OPT_Wpointer_arith,
3824 "pointer of type %<void *%> used in subtraction");
3825 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3826 pedwarn (loc, OPT_Wpointer_arith,
3827 "pointer to a function used in subtraction");
3829 /* First do the subtraction as integers;
3830 then drop through to build the divide operator.
3831 Do not do default conversions on the minus operator
3832 in case restype is a short type. */
3834 op0 = build_binary_op (loc,
3835 MINUS_EXPR, convert (inttype, op0),
3836 convert (inttype, op1), false);
3837 /* This generates an error if op1 is pointer to incomplete type. */
3838 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3839 error_at (loc, "arithmetic on pointer to an incomplete type");
3841 op1 = c_size_in_bytes (target_type);
3843 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3844 error_at (loc, "arithmetic on pointer to an empty aggregate");
3846 /* Divide by the size, in easiest possible way. */
3847 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3848 op0, convert (inttype, op1));
3850 /* Convert to final result type if necessary. */
3851 return convert (restype, result);
3854 /* Expand atomic compound assignments into an appropriate sequence as
3855 specified by the C11 standard section 6.5.16.2.
3857 _Atomic T1 E1
3858 T2 E2
3859 E1 op= E2
3861 This sequence is used for all types for which these operations are
3862 supported.
3864 In addition, built-in versions of the 'fe' prefixed routines may
3865 need to be invoked for floating point (real, complex or vector) when
3866 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3868 T1 newval;
3869 T1 old;
3870 T1 *addr
3871 T2 val
3872 fenv_t fenv
3874 addr = &E1;
3875 val = (E2);
3876 __atomic_load (addr, &old, SEQ_CST);
3877 feholdexcept (&fenv);
3878 loop:
3879 newval = old op val;
3880 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3881 SEQ_CST))
3882 goto done;
3883 feclearexcept (FE_ALL_EXCEPT);
3884 goto loop:
3885 done:
3886 feupdateenv (&fenv);
3888 The compiler will issue the __atomic_fetch_* built-in when possible,
3889 otherwise it will generate the generic form of the atomic operations.
3890 This requires temp(s) and has their address taken. The atomic processing
3891 is smart enough to figure out when the size of an object can utilize
3892 a lock-free version, and convert the built-in call to the appropriate
3893 lock-free routine. The optimizers will then dispose of any temps that
3894 are no longer required, and lock-free implementations are utilized as
3895 long as there is target support for the required size.
3897 If the operator is NOP_EXPR, then this is a simple assignment, and
3898 an __atomic_store is issued to perform the assignment rather than
3899 the above loop. */
3901 /* Build an atomic assignment at LOC, expanding into the proper
3902 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3903 the result of the operation, unless RETURN_OLD_P, in which case
3904 return the old value of LHS (this is only for postincrement and
3905 postdecrement). */
3907 static tree
3908 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3909 tree rhs, bool return_old_p)
3911 tree fndecl, func_call;
3912 vec<tree, va_gc> *params;
3913 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3914 tree old, old_addr;
3915 tree compound_stmt;
3916 tree stmt, goto_stmt;
3917 tree loop_label, loop_decl, done_label, done_decl;
3919 tree lhs_type = TREE_TYPE (lhs);
3920 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3921 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3922 tree rhs_semantic_type = TREE_TYPE (rhs);
3923 tree nonatomic_rhs_semantic_type;
3924 tree rhs_type;
3926 gcc_assert (TYPE_ATOMIC (lhs_type));
3928 if (return_old_p)
3929 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3931 /* Allocate enough vector items for a compare_exchange. */
3932 vec_alloc (params, 6);
3934 /* Create a compound statement to hold the sequence of statements
3935 with a loop. */
3936 compound_stmt = c_begin_compound_stmt (false);
3938 /* Remove any excess precision (which is only present here in the
3939 case of compound assignments). */
3940 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3942 gcc_assert (modifycode != NOP_EXPR);
3943 rhs = TREE_OPERAND (rhs, 0);
3945 rhs_type = TREE_TYPE (rhs);
3947 /* Fold the RHS if it hasn't already been folded. */
3948 if (modifycode != NOP_EXPR)
3949 rhs = c_fully_fold (rhs, false, NULL);
3951 /* Remove the qualifiers for the rest of the expressions and create
3952 the VAL temp variable to hold the RHS. */
3953 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3954 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3955 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3956 TYPE_UNQUALIFIED);
3957 val = create_tmp_var_raw (nonatomic_rhs_type);
3958 TREE_ADDRESSABLE (val) = 1;
3959 TREE_NO_WARNING (val) = 1;
3960 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3961 NULL_TREE);
3962 SET_EXPR_LOCATION (rhs, loc);
3963 add_stmt (rhs);
3965 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3966 an atomic_store. */
3967 if (modifycode == NOP_EXPR)
3969 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3970 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3971 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3972 params->quick_push (lhs_addr);
3973 params->quick_push (rhs);
3974 params->quick_push (seq_cst);
3975 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3976 add_stmt (func_call);
3978 /* Finish the compound statement. */
3979 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3981 /* VAL is the value which was stored, return a COMPOUND_STMT of
3982 the statement and that value. */
3983 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3986 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3987 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3988 isn't applicable for such builtins. ??? Do we want to handle enums? */
3989 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3990 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3992 built_in_function fncode;
3993 switch (modifycode)
3995 case PLUS_EXPR:
3996 case POINTER_PLUS_EXPR:
3997 fncode = (return_old_p
3998 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3999 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4000 break;
4001 case MINUS_EXPR:
4002 fncode = (return_old_p
4003 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4004 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4005 break;
4006 case BIT_AND_EXPR:
4007 fncode = (return_old_p
4008 ? BUILT_IN_ATOMIC_FETCH_AND_N
4009 : BUILT_IN_ATOMIC_AND_FETCH_N);
4010 break;
4011 case BIT_IOR_EXPR:
4012 fncode = (return_old_p
4013 ? BUILT_IN_ATOMIC_FETCH_OR_N
4014 : BUILT_IN_ATOMIC_OR_FETCH_N);
4015 break;
4016 case BIT_XOR_EXPR:
4017 fncode = (return_old_p
4018 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4019 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4020 break;
4021 default:
4022 goto cas_loop;
4025 /* We can only use "_1" through "_16" variants of the atomic fetch
4026 built-ins. */
4027 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4028 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4029 goto cas_loop;
4031 /* If this is a pointer type, we need to multiply by the size of
4032 the pointer target type. */
4033 if (POINTER_TYPE_P (lhs_type))
4035 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4036 /* ??? This would introduce -Wdiscarded-qualifiers
4037 warning: __atomic_fetch_* expect volatile void *
4038 type as the first argument. (Assignments between
4039 atomic and non-atomic objects are OK.) */
4040 || TYPE_RESTRICT (lhs_type))
4041 goto cas_loop;
4042 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4043 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4044 convert (ptrdiff_type_node, rhs),
4045 convert (ptrdiff_type_node, sz));
4048 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4049 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4050 fndecl = builtin_decl_explicit (fncode);
4051 params->quick_push (lhs_addr);
4052 params->quick_push (rhs);
4053 params->quick_push (seq_cst);
4054 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4056 newval = create_tmp_var_raw (nonatomic_lhs_type);
4057 TREE_ADDRESSABLE (newval) = 1;
4058 TREE_NO_WARNING (newval) = 1;
4059 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4060 NULL_TREE, NULL_TREE);
4061 SET_EXPR_LOCATION (rhs, loc);
4062 add_stmt (rhs);
4064 /* Finish the compound statement. */
4065 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4067 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4068 the statement and that value. */
4069 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4072 cas_loop:
4073 /* Create the variables and labels required for the op= form. */
4074 old = create_tmp_var_raw (nonatomic_lhs_type);
4075 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4076 TREE_ADDRESSABLE (old) = 1;
4077 TREE_NO_WARNING (old) = 1;
4079 newval = create_tmp_var_raw (nonatomic_lhs_type);
4080 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4081 TREE_ADDRESSABLE (newval) = 1;
4082 TREE_NO_WARNING (newval) = 1;
4084 loop_decl = create_artificial_label (loc);
4085 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4087 done_decl = create_artificial_label (loc);
4088 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4090 /* __atomic_load (addr, &old, SEQ_CST). */
4091 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4092 params->quick_push (lhs_addr);
4093 params->quick_push (old_addr);
4094 params->quick_push (seq_cst);
4095 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4096 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4097 NULL_TREE);
4098 add_stmt (old);
4099 params->truncate (0);
4101 /* Create the expressions for floating-point environment
4102 manipulation, if required. */
4103 bool need_fenv = (flag_trapping_math
4104 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4105 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4106 if (need_fenv)
4107 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4109 if (hold_call)
4110 add_stmt (hold_call);
4112 /* loop: */
4113 add_stmt (loop_label);
4115 /* newval = old + val; */
4116 if (rhs_type != rhs_semantic_type)
4117 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4118 rhs = build_binary_op (loc, modifycode, old, val, true);
4119 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4121 tree eptype = TREE_TYPE (rhs);
4122 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4123 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4125 else
4126 rhs = c_fully_fold (rhs, false, NULL);
4127 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4128 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4129 NULL_TREE, 0);
4130 if (rhs != error_mark_node)
4132 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4133 NULL_TREE);
4134 SET_EXPR_LOCATION (rhs, loc);
4135 add_stmt (rhs);
4138 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4139 goto done; */
4140 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4141 params->quick_push (lhs_addr);
4142 params->quick_push (old_addr);
4143 params->quick_push (newval_addr);
4144 params->quick_push (integer_zero_node);
4145 params->quick_push (seq_cst);
4146 params->quick_push (seq_cst);
4147 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4149 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4150 SET_EXPR_LOCATION (goto_stmt, loc);
4152 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4153 SET_EXPR_LOCATION (stmt, loc);
4154 add_stmt (stmt);
4156 if (clear_call)
4157 add_stmt (clear_call);
4159 /* goto loop; */
4160 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4161 SET_EXPR_LOCATION (goto_stmt, loc);
4162 add_stmt (goto_stmt);
4164 /* done: */
4165 add_stmt (done_label);
4167 if (update_call)
4168 add_stmt (update_call);
4170 /* Finish the compound statement. */
4171 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4173 /* NEWVAL is the value that was successfully stored, return a
4174 COMPOUND_EXPR of the statement and the appropriate value. */
4175 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4176 return_old_p ? old : newval);
4179 /* Construct and perhaps optimize a tree representation
4180 for a unary operation. CODE, a tree_code, specifies the operation
4181 and XARG is the operand.
4182 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4183 promotions (such as from short to int).
4184 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4185 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4186 to pointers in C99.
4188 LOCATION is the location of the operator. */
4190 tree
4191 build_unary_op (location_t location, enum tree_code code, tree xarg,
4192 bool noconvert)
4194 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4195 tree arg = xarg;
4196 tree argtype = NULL_TREE;
4197 enum tree_code typecode;
4198 tree val;
4199 tree ret = error_mark_node;
4200 tree eptype = NULL_TREE;
4201 const char *invalid_op_diag;
4202 bool int_operands;
4204 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4205 if (int_operands)
4206 arg = remove_c_maybe_const_expr (arg);
4208 if (code != ADDR_EXPR)
4209 arg = require_complete_type (location, arg);
4211 typecode = TREE_CODE (TREE_TYPE (arg));
4212 if (typecode == ERROR_MARK)
4213 return error_mark_node;
4214 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4215 typecode = INTEGER_TYPE;
4217 if ((invalid_op_diag
4218 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4220 error_at (location, invalid_op_diag);
4221 return error_mark_node;
4224 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4226 eptype = TREE_TYPE (arg);
4227 arg = TREE_OPERAND (arg, 0);
4230 switch (code)
4232 case CONVERT_EXPR:
4233 /* This is used for unary plus, because a CONVERT_EXPR
4234 is enough to prevent anybody from looking inside for
4235 associativity, but won't generate any code. */
4236 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4237 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4238 || typecode == VECTOR_TYPE))
4240 error_at (location, "wrong type argument to unary plus");
4241 return error_mark_node;
4243 else if (!noconvert)
4244 arg = default_conversion (arg);
4245 arg = non_lvalue_loc (location, arg);
4246 break;
4248 case NEGATE_EXPR:
4249 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4250 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4251 || typecode == VECTOR_TYPE))
4253 error_at (location, "wrong type argument to unary minus");
4254 return error_mark_node;
4256 else if (!noconvert)
4257 arg = default_conversion (arg);
4258 break;
4260 case BIT_NOT_EXPR:
4261 /* ~ works on integer types and non float vectors. */
4262 if (typecode == INTEGER_TYPE
4263 || (typecode == VECTOR_TYPE
4264 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4266 tree e = arg;
4268 /* Warn if the expression has boolean value. */
4269 while (TREE_CODE (e) == COMPOUND_EXPR)
4270 e = TREE_OPERAND (e, 1);
4272 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4273 || truth_value_p (TREE_CODE (e)))
4274 && warning_at (location, OPT_Wbool_operation,
4275 "%<~%> on a boolean expression"))
4277 gcc_rich_location richloc (location);
4278 richloc.add_fixit_insert_before (location, "!");
4279 inform_at_rich_loc (&richloc, "did you mean to use logical "
4280 "not?");
4282 if (!noconvert)
4283 arg = default_conversion (arg);
4285 else if (typecode == COMPLEX_TYPE)
4287 code = CONJ_EXPR;
4288 pedwarn (location, OPT_Wpedantic,
4289 "ISO C does not support %<~%> for complex conjugation");
4290 if (!noconvert)
4291 arg = default_conversion (arg);
4293 else
4295 error_at (location, "wrong type argument to bit-complement");
4296 return error_mark_node;
4298 break;
4300 case ABS_EXPR:
4301 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4303 error_at (location, "wrong type argument to abs");
4304 return error_mark_node;
4306 else if (!noconvert)
4307 arg = default_conversion (arg);
4308 break;
4310 case CONJ_EXPR:
4311 /* Conjugating a real value is a no-op, but allow it anyway. */
4312 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4313 || typecode == COMPLEX_TYPE))
4315 error_at (location, "wrong type argument to conjugation");
4316 return error_mark_node;
4318 else if (!noconvert)
4319 arg = default_conversion (arg);
4320 break;
4322 case TRUTH_NOT_EXPR:
4323 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4324 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4325 && typecode != COMPLEX_TYPE)
4327 error_at (location,
4328 "wrong type argument to unary exclamation mark");
4329 return error_mark_node;
4331 if (int_operands)
4333 arg = c_objc_common_truthvalue_conversion (location, xarg);
4334 arg = remove_c_maybe_const_expr (arg);
4336 else
4337 arg = c_objc_common_truthvalue_conversion (location, arg);
4338 ret = invert_truthvalue_loc (location, arg);
4339 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4340 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4341 location = EXPR_LOCATION (ret);
4342 goto return_build_unary_op;
4344 case REALPART_EXPR:
4345 case IMAGPART_EXPR:
4346 ret = build_real_imag_expr (location, code, arg);
4347 if (ret == error_mark_node)
4348 return error_mark_node;
4349 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4350 eptype = TREE_TYPE (eptype);
4351 goto return_build_unary_op;
4353 case PREINCREMENT_EXPR:
4354 case POSTINCREMENT_EXPR:
4355 case PREDECREMENT_EXPR:
4356 case POSTDECREMENT_EXPR:
4358 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4360 tree inner = build_unary_op (location, code,
4361 C_MAYBE_CONST_EXPR_EXPR (arg),
4362 noconvert);
4363 if (inner == error_mark_node)
4364 return error_mark_node;
4365 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4366 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4367 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4368 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4369 goto return_build_unary_op;
4372 /* Complain about anything that is not a true lvalue. In
4373 Objective-C, skip this check for property_refs. */
4374 if (!objc_is_property_ref (arg)
4375 && !lvalue_or_else (location,
4376 arg, ((code == PREINCREMENT_EXPR
4377 || code == POSTINCREMENT_EXPR)
4378 ? lv_increment
4379 : lv_decrement)))
4380 return error_mark_node;
4382 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4384 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4385 warning_at (location, OPT_Wc___compat,
4386 "increment of enumeration value is invalid in C++");
4387 else
4388 warning_at (location, OPT_Wc___compat,
4389 "decrement of enumeration value is invalid in C++");
4392 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4394 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4395 warning_at (location, OPT_Wbool_operation,
4396 "increment of a boolean expression");
4397 else
4398 warning_at (location, OPT_Wbool_operation,
4399 "decrement of a boolean expression");
4402 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4403 arg = c_fully_fold (arg, false, NULL);
4405 bool atomic_op;
4406 atomic_op = really_atomic_lvalue (arg);
4408 /* Increment or decrement the real part of the value,
4409 and don't change the imaginary part. */
4410 if (typecode == COMPLEX_TYPE)
4412 tree real, imag;
4414 pedwarn (location, OPT_Wpedantic,
4415 "ISO C does not support %<++%> and %<--%> on complex types");
4417 if (!atomic_op)
4419 arg = stabilize_reference (arg);
4420 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4421 true);
4422 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4423 true);
4424 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4425 if (real == error_mark_node || imag == error_mark_node)
4426 return error_mark_node;
4427 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4428 real, imag);
4429 goto return_build_unary_op;
4433 /* Report invalid types. */
4435 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4436 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4437 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4439 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4440 error_at (location, "wrong type argument to increment");
4441 else
4442 error_at (location, "wrong type argument to decrement");
4444 return error_mark_node;
4448 tree inc;
4450 argtype = TREE_TYPE (arg);
4452 /* Compute the increment. */
4454 if (typecode == POINTER_TYPE)
4456 /* If pointer target is an incomplete type,
4457 we just cannot know how to do the arithmetic. */
4458 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4460 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4461 error_at (location,
4462 "increment of pointer to an incomplete type %qT",
4463 TREE_TYPE (argtype));
4464 else
4465 error_at (location,
4466 "decrement of pointer to an incomplete type %qT",
4467 TREE_TYPE (argtype));
4469 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4470 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4472 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4473 pedwarn (location, OPT_Wpointer_arith,
4474 "wrong type argument to increment");
4475 else
4476 pedwarn (location, OPT_Wpointer_arith,
4477 "wrong type argument to decrement");
4480 inc = c_size_in_bytes (TREE_TYPE (argtype));
4481 inc = convert_to_ptrofftype_loc (location, inc);
4483 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4485 /* For signed fract types, we invert ++ to -- or
4486 -- to ++, and change inc from 1 to -1, because
4487 it is not possible to represent 1 in signed fract constants.
4488 For unsigned fract types, the result always overflows and
4489 we get an undefined (original) or the maximum value. */
4490 if (code == PREINCREMENT_EXPR)
4491 code = PREDECREMENT_EXPR;
4492 else if (code == PREDECREMENT_EXPR)
4493 code = PREINCREMENT_EXPR;
4494 else if (code == POSTINCREMENT_EXPR)
4495 code = POSTDECREMENT_EXPR;
4496 else /* code == POSTDECREMENT_EXPR */
4497 code = POSTINCREMENT_EXPR;
4499 inc = integer_minus_one_node;
4500 inc = convert (argtype, inc);
4502 else
4504 inc = VECTOR_TYPE_P (argtype)
4505 ? build_one_cst (argtype)
4506 : integer_one_node;
4507 inc = convert (argtype, inc);
4510 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4511 need to ask Objective-C to build the increment or decrement
4512 expression for it. */
4513 if (objc_is_property_ref (arg))
4514 return objc_build_incr_expr_for_property_ref (location, code,
4515 arg, inc);
4517 /* Report a read-only lvalue. */
4518 if (TYPE_READONLY (argtype))
4520 readonly_error (location, arg,
4521 ((code == PREINCREMENT_EXPR
4522 || code == POSTINCREMENT_EXPR)
4523 ? lv_increment : lv_decrement));
4524 return error_mark_node;
4526 else if (TREE_READONLY (arg))
4527 readonly_warning (arg,
4528 ((code == PREINCREMENT_EXPR
4529 || code == POSTINCREMENT_EXPR)
4530 ? lv_increment : lv_decrement));
4532 /* If the argument is atomic, use the special code sequences for
4533 atomic compound assignment. */
4534 if (atomic_op)
4536 arg = stabilize_reference (arg);
4537 ret = build_atomic_assign (location, arg,
4538 ((code == PREINCREMENT_EXPR
4539 || code == POSTINCREMENT_EXPR)
4540 ? PLUS_EXPR
4541 : MINUS_EXPR),
4542 (FRACT_MODE_P (TYPE_MODE (argtype))
4543 ? inc
4544 : integer_one_node),
4545 (code == POSTINCREMENT_EXPR
4546 || code == POSTDECREMENT_EXPR));
4547 goto return_build_unary_op;
4550 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4551 val = boolean_increment (code, arg);
4552 else
4553 val = build2 (code, TREE_TYPE (arg), arg, inc);
4554 TREE_SIDE_EFFECTS (val) = 1;
4555 if (TREE_CODE (val) != code)
4556 TREE_NO_WARNING (val) = 1;
4557 ret = val;
4558 goto return_build_unary_op;
4561 case ADDR_EXPR:
4562 /* Note that this operation never does default_conversion. */
4564 /* The operand of unary '&' must be an lvalue (which excludes
4565 expressions of type void), or, in C99, the result of a [] or
4566 unary '*' operator. */
4567 if (VOID_TYPE_P (TREE_TYPE (arg))
4568 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4569 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4570 pedwarn (location, 0, "taking address of expression of type %<void%>");
4572 /* Let &* cancel out to simplify resulting code. */
4573 if (INDIRECT_REF_P (arg))
4575 /* Don't let this be an lvalue. */
4576 if (lvalue_p (TREE_OPERAND (arg, 0)))
4577 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4578 ret = TREE_OPERAND (arg, 0);
4579 goto return_build_unary_op;
4582 /* Anything not already handled and not a true memory reference
4583 or a non-lvalue array is an error. */
4584 if (typecode != FUNCTION_TYPE && !noconvert
4585 && !lvalue_or_else (location, arg, lv_addressof))
4586 return error_mark_node;
4588 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4589 folding later. */
4590 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4592 tree inner = build_unary_op (location, code,
4593 C_MAYBE_CONST_EXPR_EXPR (arg),
4594 noconvert);
4595 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4596 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4597 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4598 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4599 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4600 goto return_build_unary_op;
4603 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4604 argtype = TREE_TYPE (arg);
4606 /* If the lvalue is const or volatile, merge that into the type
4607 to which the address will point. This is only needed
4608 for function types. */
4609 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4610 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4611 && TREE_CODE (argtype) == FUNCTION_TYPE)
4613 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4614 int quals = orig_quals;
4616 if (TREE_READONLY (arg))
4617 quals |= TYPE_QUAL_CONST;
4618 if (TREE_THIS_VOLATILE (arg))
4619 quals |= TYPE_QUAL_VOLATILE;
4621 argtype = c_build_qualified_type (argtype, quals);
4624 switch (TREE_CODE (arg))
4626 case COMPONENT_REF:
4627 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4629 error_at (location, "cannot take address of bit-field %qD",
4630 TREE_OPERAND (arg, 1));
4631 return error_mark_node;
4634 /* fall through */
4636 case ARRAY_REF:
4637 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4639 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4640 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4642 error_at (location, "cannot take address of scalar with "
4643 "reverse storage order");
4644 return error_mark_node;
4647 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4648 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4649 warning_at (location, OPT_Wscalar_storage_order,
4650 "address of array with reverse scalar storage "
4651 "order requested");
4654 default:
4655 break;
4658 if (!c_mark_addressable (arg))
4659 return error_mark_node;
4661 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4662 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4664 argtype = build_pointer_type (argtype);
4666 /* ??? Cope with user tricks that amount to offsetof. Delete this
4667 when we have proper support for integer constant expressions. */
4668 val = get_base_address (arg);
4669 if (val && INDIRECT_REF_P (val)
4670 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4672 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4673 goto return_build_unary_op;
4676 val = build1 (ADDR_EXPR, argtype, arg);
4678 ret = val;
4679 goto return_build_unary_op;
4681 default:
4682 gcc_unreachable ();
4685 if (argtype == NULL_TREE)
4686 argtype = TREE_TYPE (arg);
4687 if (TREE_CODE (arg) == INTEGER_CST)
4688 ret = (require_constant_value
4689 ? fold_build1_initializer_loc (location, code, argtype, arg)
4690 : fold_build1_loc (location, code, argtype, arg));
4691 else
4692 ret = build1 (code, argtype, arg);
4693 return_build_unary_op:
4694 gcc_assert (ret != error_mark_node);
4695 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4696 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4697 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4698 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4699 ret = note_integer_operands (ret);
4700 if (eptype)
4701 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4702 protected_set_expr_location (ret, location);
4703 return ret;
4706 /* Return nonzero if REF is an lvalue valid for this language.
4707 Lvalues can be assigned, unless their type has TYPE_READONLY.
4708 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4710 bool
4711 lvalue_p (const_tree ref)
4713 const enum tree_code code = TREE_CODE (ref);
4715 switch (code)
4717 case REALPART_EXPR:
4718 case IMAGPART_EXPR:
4719 case COMPONENT_REF:
4720 return lvalue_p (TREE_OPERAND (ref, 0));
4722 case C_MAYBE_CONST_EXPR:
4723 return lvalue_p (TREE_OPERAND (ref, 1));
4725 case COMPOUND_LITERAL_EXPR:
4726 case STRING_CST:
4727 return true;
4729 case INDIRECT_REF:
4730 case ARRAY_REF:
4731 case ARRAY_NOTATION_REF:
4732 case VAR_DECL:
4733 case PARM_DECL:
4734 case RESULT_DECL:
4735 case ERROR_MARK:
4736 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4737 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4739 case BIND_EXPR:
4740 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4742 default:
4743 return false;
4747 /* Give a warning for storing in something that is read-only in GCC
4748 terms but not const in ISO C terms. */
4750 static void
4751 readonly_warning (tree arg, enum lvalue_use use)
4753 switch (use)
4755 case lv_assign:
4756 warning (0, "assignment of read-only location %qE", arg);
4757 break;
4758 case lv_increment:
4759 warning (0, "increment of read-only location %qE", arg);
4760 break;
4761 case lv_decrement:
4762 warning (0, "decrement of read-only location %qE", arg);
4763 break;
4764 default:
4765 gcc_unreachable ();
4767 return;
4771 /* Return nonzero if REF is an lvalue valid for this language;
4772 otherwise, print an error message and return zero. USE says
4773 how the lvalue is being used and so selects the error message.
4774 LOCATION is the location at which any error should be reported. */
4776 static int
4777 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4779 int win = lvalue_p (ref);
4781 if (!win)
4782 lvalue_error (loc, use);
4784 return win;
4787 /* Mark EXP saying that we need to be able to take the
4788 address of it; it should not be allocated in a register.
4789 Returns true if successful. ARRAY_REF_P is true if this
4790 is for ARRAY_REF construction - in that case we don't want
4791 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4792 it is fine to use ARRAY_REFs for vector subscripts on vector
4793 register variables. */
4795 bool
4796 c_mark_addressable (tree exp, bool array_ref_p)
4798 tree x = exp;
4800 while (1)
4801 switch (TREE_CODE (x))
4803 case VIEW_CONVERT_EXPR:
4804 if (array_ref_p
4805 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4806 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4807 return true;
4808 /* FALLTHRU */
4809 case COMPONENT_REF:
4810 case ADDR_EXPR:
4811 case ARRAY_REF:
4812 case REALPART_EXPR:
4813 case IMAGPART_EXPR:
4814 x = TREE_OPERAND (x, 0);
4815 break;
4817 case COMPOUND_LITERAL_EXPR:
4818 case CONSTRUCTOR:
4819 TREE_ADDRESSABLE (x) = 1;
4820 return true;
4822 case VAR_DECL:
4823 case CONST_DECL:
4824 case PARM_DECL:
4825 case RESULT_DECL:
4826 if (C_DECL_REGISTER (x)
4827 && DECL_NONLOCAL (x))
4829 if (TREE_PUBLIC (x) || is_global_var (x))
4831 error
4832 ("global register variable %qD used in nested function", x);
4833 return false;
4835 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4837 else if (C_DECL_REGISTER (x))
4839 if (TREE_PUBLIC (x) || is_global_var (x))
4840 error ("address of global register variable %qD requested", x);
4841 else
4842 error ("address of register variable %qD requested", x);
4843 return false;
4846 /* FALLTHRU */
4847 case FUNCTION_DECL:
4848 TREE_ADDRESSABLE (x) = 1;
4849 /* FALLTHRU */
4850 default:
4851 return true;
4855 /* Convert EXPR to TYPE, warning about conversion problems with
4856 constants. SEMANTIC_TYPE is the type this conversion would use
4857 without excess precision. If SEMANTIC_TYPE is NULL, this function
4858 is equivalent to convert_and_check. This function is a wrapper that
4859 handles conversions that may be different than
4860 the usual ones because of excess precision. */
4862 static tree
4863 ep_convert_and_check (location_t loc, tree type, tree expr,
4864 tree semantic_type)
4866 if (TREE_TYPE (expr) == type)
4867 return expr;
4869 if (!semantic_type)
4870 return convert_and_check (loc, type, expr);
4872 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4873 && TREE_TYPE (expr) != semantic_type)
4875 /* For integers, we need to check the real conversion, not
4876 the conversion to the excess precision type. */
4877 expr = convert_and_check (loc, semantic_type, expr);
4879 /* Result type is the excess precision type, which should be
4880 large enough, so do not check. */
4881 return convert (type, expr);
4884 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4885 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4886 if folded to an integer constant then the unselected half may
4887 contain arbitrary operations not normally permitted in constant
4888 expressions. Set the location of the expression to LOC. */
4890 tree
4891 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4892 tree op1, tree op1_original_type, location_t op1_loc,
4893 tree op2, tree op2_original_type, location_t op2_loc)
4895 tree type1;
4896 tree type2;
4897 enum tree_code code1;
4898 enum tree_code code2;
4899 tree result_type = NULL;
4900 tree semantic_result_type = NULL;
4901 tree orig_op1 = op1, orig_op2 = op2;
4902 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4903 bool ifexp_int_operands;
4904 tree ret;
4906 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4907 if (op1_int_operands)
4908 op1 = remove_c_maybe_const_expr (op1);
4909 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4910 if (op2_int_operands)
4911 op2 = remove_c_maybe_const_expr (op2);
4912 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4913 if (ifexp_int_operands)
4914 ifexp = remove_c_maybe_const_expr (ifexp);
4916 /* Promote both alternatives. */
4918 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4919 op1 = default_conversion (op1);
4920 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4921 op2 = default_conversion (op2);
4923 if (TREE_CODE (ifexp) == ERROR_MARK
4924 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4925 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4926 return error_mark_node;
4928 type1 = TREE_TYPE (op1);
4929 code1 = TREE_CODE (type1);
4930 type2 = TREE_TYPE (op2);
4931 code2 = TREE_CODE (type2);
4933 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4934 return error_mark_node;
4936 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4937 return error_mark_node;
4939 /* C90 does not permit non-lvalue arrays in conditional expressions.
4940 In C99 they will be pointers by now. */
4941 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4943 error_at (colon_loc, "non-lvalue array in conditional expression");
4944 return error_mark_node;
4947 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4948 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4949 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4950 || code1 == COMPLEX_TYPE)
4951 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4952 || code2 == COMPLEX_TYPE))
4954 semantic_result_type = c_common_type (type1, type2);
4955 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4957 op1 = TREE_OPERAND (op1, 0);
4958 type1 = TREE_TYPE (op1);
4959 gcc_assert (TREE_CODE (type1) == code1);
4961 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4963 op2 = TREE_OPERAND (op2, 0);
4964 type2 = TREE_TYPE (op2);
4965 gcc_assert (TREE_CODE (type2) == code2);
4969 if (warn_cxx_compat)
4971 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4972 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4974 if (TREE_CODE (t1) == ENUMERAL_TYPE
4975 && TREE_CODE (t2) == ENUMERAL_TYPE
4976 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4977 warning_at (colon_loc, OPT_Wc___compat,
4978 ("different enum types in conditional is "
4979 "invalid in C++: %qT vs %qT"),
4980 t1, t2);
4983 /* Quickly detect the usual case where op1 and op2 have the same type
4984 after promotion. */
4985 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4987 if (type1 == type2)
4988 result_type = type1;
4989 else
4990 result_type = TYPE_MAIN_VARIANT (type1);
4992 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4993 || code1 == COMPLEX_TYPE)
4994 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4995 || code2 == COMPLEX_TYPE))
4997 result_type = c_common_type (type1, type2);
4998 if (result_type == error_mark_node)
4999 return error_mark_node;
5000 do_warn_double_promotion (result_type, type1, type2,
5001 "implicit conversion from %qT to %qT to "
5002 "match other result of conditional",
5003 colon_loc);
5005 /* If -Wsign-compare, warn here if type1 and type2 have
5006 different signedness. We'll promote the signed to unsigned
5007 and later code won't know it used to be different.
5008 Do this check on the original types, so that explicit casts
5009 will be considered, but default promotions won't. */
5010 if (c_inhibit_evaluation_warnings == 0)
5012 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5013 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5015 if (unsigned_op1 ^ unsigned_op2)
5017 bool ovf;
5019 /* Do not warn if the result type is signed, since the
5020 signed type will only be chosen if it can represent
5021 all the values of the unsigned type. */
5022 if (!TYPE_UNSIGNED (result_type))
5023 /* OK */;
5024 else
5026 bool op1_maybe_const = true;
5027 bool op2_maybe_const = true;
5029 /* Do not warn if the signed quantity is an
5030 unsuffixed integer literal (or some static
5031 constant expression involving such literals) and
5032 it is non-negative. This warning requires the
5033 operands to be folded for best results, so do
5034 that folding in this case even without
5035 warn_sign_compare to avoid warning options
5036 possibly affecting code generation. */
5037 c_inhibit_evaluation_warnings
5038 += (ifexp == truthvalue_false_node);
5039 op1 = c_fully_fold (op1, require_constant_value,
5040 &op1_maybe_const);
5041 c_inhibit_evaluation_warnings
5042 -= (ifexp == truthvalue_false_node);
5044 c_inhibit_evaluation_warnings
5045 += (ifexp == truthvalue_true_node);
5046 op2 = c_fully_fold (op2, require_constant_value,
5047 &op2_maybe_const);
5048 c_inhibit_evaluation_warnings
5049 -= (ifexp == truthvalue_true_node);
5051 if (warn_sign_compare)
5053 if ((unsigned_op2
5054 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5055 || (unsigned_op1
5056 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5057 /* OK */;
5058 else if (unsigned_op2)
5059 warning_at (op1_loc, OPT_Wsign_compare,
5060 "operand of ?: changes signedness from "
5061 "%qT to %qT due to unsignedness of other "
5062 "operand", TREE_TYPE (orig_op1),
5063 TREE_TYPE (orig_op2));
5064 else
5065 warning_at (op2_loc, OPT_Wsign_compare,
5066 "operand of ?: changes signedness from "
5067 "%qT to %qT due to unsignedness of other "
5068 "operand", TREE_TYPE (orig_op2),
5069 TREE_TYPE (orig_op1));
5071 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5072 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5073 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5074 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5079 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5081 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5082 pedwarn (colon_loc, OPT_Wpedantic,
5083 "ISO C forbids conditional expr with only one void side");
5084 result_type = void_type_node;
5086 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5088 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5089 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5090 addr_space_t as_common;
5092 if (comp_target_types (colon_loc, type1, type2))
5093 result_type = common_pointer_type (type1, type2);
5094 else if (null_pointer_constant_p (orig_op1))
5095 result_type = type2;
5096 else if (null_pointer_constant_p (orig_op2))
5097 result_type = type1;
5098 else if (!addr_space_superset (as1, as2, &as_common))
5100 error_at (colon_loc, "pointers to disjoint address spaces "
5101 "used in conditional expression");
5102 return error_mark_node;
5104 else if (VOID_TYPE_P (TREE_TYPE (type1))
5105 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5107 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5108 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5109 & ~TYPE_QUALS (TREE_TYPE (type1))))
5110 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5111 "pointer to array loses qualifier "
5112 "in conditional expression");
5114 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5115 pedwarn (colon_loc, OPT_Wpedantic,
5116 "ISO C forbids conditional expr between "
5117 "%<void *%> and function pointer");
5118 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5119 TREE_TYPE (type2)));
5121 else if (VOID_TYPE_P (TREE_TYPE (type2))
5122 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5124 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5125 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5126 & ~TYPE_QUALS (TREE_TYPE (type2))))
5127 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5128 "pointer to array loses qualifier "
5129 "in conditional expression");
5131 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5132 pedwarn (colon_loc, OPT_Wpedantic,
5133 "ISO C forbids conditional expr between "
5134 "%<void *%> and function pointer");
5135 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5136 TREE_TYPE (type1)));
5138 /* Objective-C pointer comparisons are a bit more lenient. */
5139 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5140 result_type = objc_common_type (type1, type2);
5141 else
5143 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5145 pedwarn (colon_loc, 0,
5146 "pointer type mismatch in conditional expression");
5147 result_type = build_pointer_type
5148 (build_qualified_type (void_type_node, qual));
5151 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5153 if (!null_pointer_constant_p (orig_op2))
5154 pedwarn (colon_loc, 0,
5155 "pointer/integer type mismatch in conditional expression");
5156 else
5158 op2 = null_pointer_node;
5160 result_type = type1;
5162 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5164 if (!null_pointer_constant_p (orig_op1))
5165 pedwarn (colon_loc, 0,
5166 "pointer/integer type mismatch in conditional expression");
5167 else
5169 op1 = null_pointer_node;
5171 result_type = type2;
5174 if (!result_type)
5176 if (flag_cond_mismatch)
5177 result_type = void_type_node;
5178 else
5180 error_at (colon_loc, "type mismatch in conditional expression");
5181 return error_mark_node;
5185 /* Merge const and volatile flags of the incoming types. */
5186 result_type
5187 = build_type_variant (result_type,
5188 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5189 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5191 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5192 semantic_result_type);
5193 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5194 semantic_result_type);
5196 if (ifexp_bcp && ifexp == truthvalue_true_node)
5198 op2_int_operands = true;
5199 op1 = c_fully_fold (op1, require_constant_value, NULL);
5201 if (ifexp_bcp && ifexp == truthvalue_false_node)
5203 op1_int_operands = true;
5204 op2 = c_fully_fold (op2, require_constant_value, NULL);
5206 int_const = int_operands = (ifexp_int_operands
5207 && op1_int_operands
5208 && op2_int_operands);
5209 if (int_operands)
5211 int_const = ((ifexp == truthvalue_true_node
5212 && TREE_CODE (orig_op1) == INTEGER_CST
5213 && !TREE_OVERFLOW (orig_op1))
5214 || (ifexp == truthvalue_false_node
5215 && TREE_CODE (orig_op2) == INTEGER_CST
5216 && !TREE_OVERFLOW (orig_op2)));
5219 /* Need to convert condition operand into a vector mask. */
5220 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5222 tree vectype = TREE_TYPE (ifexp);
5223 tree elem_type = TREE_TYPE (vectype);
5224 tree zero = build_int_cst (elem_type, 0);
5225 tree zero_vec = build_vector_from_val (vectype, zero);
5226 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5227 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5230 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5231 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5232 else
5234 if (int_operands)
5236 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5237 nested inside of the expression. */
5238 op1 = c_fully_fold (op1, false, NULL);
5239 op2 = c_fully_fold (op2, false, NULL);
5241 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5242 if (int_operands)
5243 ret = note_integer_operands (ret);
5245 if (semantic_result_type)
5246 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5248 protected_set_expr_location (ret, colon_loc);
5250 /* If the OP1 and OP2 are the same and don't have side-effects,
5251 warn here, because the COND_EXPR will be turned into OP1. */
5252 if (warn_duplicated_branches
5253 && TREE_CODE (ret) == COND_EXPR
5254 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5255 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5256 "this condition has identical branches");
5258 return ret;
5261 /* Return a compound expression that performs two expressions and
5262 returns the value of the second of them.
5264 LOC is the location of the COMPOUND_EXPR. */
5266 tree
5267 build_compound_expr (location_t loc, tree expr1, tree expr2)
5269 bool expr1_int_operands, expr2_int_operands;
5270 tree eptype = NULL_TREE;
5271 tree ret;
5273 if (flag_cilkplus
5274 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5275 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5277 error_at (loc,
5278 "spawned function call cannot be part of a comma expression");
5279 return error_mark_node;
5281 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5282 if (expr1_int_operands)
5283 expr1 = remove_c_maybe_const_expr (expr1);
5284 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5285 if (expr2_int_operands)
5286 expr2 = remove_c_maybe_const_expr (expr2);
5288 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5289 expr1 = TREE_OPERAND (expr1, 0);
5290 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5292 eptype = TREE_TYPE (expr2);
5293 expr2 = TREE_OPERAND (expr2, 0);
5296 if (!TREE_SIDE_EFFECTS (expr1))
5298 /* The left-hand operand of a comma expression is like an expression
5299 statement: with -Wunused, we should warn if it doesn't have
5300 any side-effects, unless it was explicitly cast to (void). */
5301 if (warn_unused_value)
5303 if (VOID_TYPE_P (TREE_TYPE (expr1))
5304 && CONVERT_EXPR_P (expr1))
5305 ; /* (void) a, b */
5306 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5307 && TREE_CODE (expr1) == COMPOUND_EXPR
5308 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5309 ; /* (void) a, (void) b, c */
5310 else
5311 warning_at (loc, OPT_Wunused_value,
5312 "left-hand operand of comma expression has no effect");
5315 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5316 && warn_unused_value)
5318 tree r = expr1;
5319 location_t cloc = loc;
5320 while (TREE_CODE (r) == COMPOUND_EXPR)
5322 if (EXPR_HAS_LOCATION (r))
5323 cloc = EXPR_LOCATION (r);
5324 r = TREE_OPERAND (r, 1);
5326 if (!TREE_SIDE_EFFECTS (r)
5327 && !VOID_TYPE_P (TREE_TYPE (r))
5328 && !CONVERT_EXPR_P (r))
5329 warning_at (cloc, OPT_Wunused_value,
5330 "right-hand operand of comma expression has no effect");
5333 /* With -Wunused, we should also warn if the left-hand operand does have
5334 side-effects, but computes a value which is not used. For example, in
5335 `foo() + bar(), baz()' the result of the `+' operator is not used,
5336 so we should issue a warning. */
5337 else if (warn_unused_value)
5338 warn_if_unused_value (expr1, loc);
5340 if (expr2 == error_mark_node)
5341 return error_mark_node;
5343 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5345 if (flag_isoc99
5346 && expr1_int_operands
5347 && expr2_int_operands)
5348 ret = note_integer_operands (ret);
5350 if (eptype)
5351 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5353 protected_set_expr_location (ret, loc);
5354 return ret;
5357 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5358 which we are casting. OTYPE is the type of the expression being
5359 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5360 of the cast. -Wcast-qual appeared on the command line. Named
5361 address space qualifiers are not handled here, because they result
5362 in different warnings. */
5364 static void
5365 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5367 tree in_type = type;
5368 tree in_otype = otype;
5369 int added = 0;
5370 int discarded = 0;
5371 bool is_const;
5373 /* Check that the qualifiers on IN_TYPE are a superset of the
5374 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5375 nodes is uninteresting and we stop as soon as we hit a
5376 non-POINTER_TYPE node on either type. */
5379 in_otype = TREE_TYPE (in_otype);
5380 in_type = TREE_TYPE (in_type);
5382 /* GNU C allows cv-qualified function types. 'const' means the
5383 function is very pure, 'volatile' means it can't return. We
5384 need to warn when such qualifiers are added, not when they're
5385 taken away. */
5386 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5387 && TREE_CODE (in_type) == FUNCTION_TYPE)
5388 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5389 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5390 else
5391 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5392 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5394 while (TREE_CODE (in_type) == POINTER_TYPE
5395 && TREE_CODE (in_otype) == POINTER_TYPE);
5397 if (added)
5398 warning_at (loc, OPT_Wcast_qual,
5399 "cast adds %q#v qualifier to function type", added);
5401 if (discarded)
5402 /* There are qualifiers present in IN_OTYPE that are not present
5403 in IN_TYPE. */
5404 warning_at (loc, OPT_Wcast_qual,
5405 "cast discards %qv qualifier from pointer target type",
5406 discarded);
5408 if (added || discarded)
5409 return;
5411 /* A cast from **T to const **T is unsafe, because it can cause a
5412 const value to be changed with no additional warning. We only
5413 issue this warning if T is the same on both sides, and we only
5414 issue the warning if there are the same number of pointers on
5415 both sides, as otherwise the cast is clearly unsafe anyhow. A
5416 cast is unsafe when a qualifier is added at one level and const
5417 is not present at all outer levels.
5419 To issue this warning, we check at each level whether the cast
5420 adds new qualifiers not already seen. We don't need to special
5421 case function types, as they won't have the same
5422 TYPE_MAIN_VARIANT. */
5424 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5425 return;
5426 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5427 return;
5429 in_type = type;
5430 in_otype = otype;
5431 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5434 in_type = TREE_TYPE (in_type);
5435 in_otype = TREE_TYPE (in_otype);
5436 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5437 && !is_const)
5439 warning_at (loc, OPT_Wcast_qual,
5440 "to be safe all intermediate pointers in cast from "
5441 "%qT to %qT must be %<const%> qualified",
5442 otype, type);
5443 break;
5445 if (is_const)
5446 is_const = TYPE_READONLY (in_type);
5448 while (TREE_CODE (in_type) == POINTER_TYPE);
5451 /* Build an expression representing a cast to type TYPE of expression EXPR.
5452 LOC is the location of the cast-- typically the open paren of the cast. */
5454 tree
5455 build_c_cast (location_t loc, tree type, tree expr)
5457 tree value;
5459 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5460 expr = TREE_OPERAND (expr, 0);
5462 value = expr;
5464 if (type == error_mark_node || expr == error_mark_node)
5465 return error_mark_node;
5467 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5468 only in <protocol> qualifications. But when constructing cast expressions,
5469 the protocols do matter and must be kept around. */
5470 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5471 return build1 (NOP_EXPR, type, expr);
5473 type = TYPE_MAIN_VARIANT (type);
5475 if (TREE_CODE (type) == ARRAY_TYPE)
5477 error_at (loc, "cast specifies array type");
5478 return error_mark_node;
5481 if (TREE_CODE (type) == FUNCTION_TYPE)
5483 error_at (loc, "cast specifies function type");
5484 return error_mark_node;
5487 if (!VOID_TYPE_P (type))
5489 value = require_complete_type (loc, value);
5490 if (value == error_mark_node)
5491 return error_mark_node;
5494 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5496 if (RECORD_OR_UNION_TYPE_P (type))
5497 pedwarn (loc, OPT_Wpedantic,
5498 "ISO C forbids casting nonscalar to the same type");
5500 /* Convert to remove any qualifiers from VALUE's type. */
5501 value = convert (type, value);
5503 else if (TREE_CODE (type) == UNION_TYPE)
5505 tree field;
5507 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5508 if (TREE_TYPE (field) != error_mark_node
5509 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5510 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5511 break;
5513 if (field)
5515 tree t;
5516 bool maybe_const = true;
5518 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5519 t = c_fully_fold (value, false, &maybe_const);
5520 t = build_constructor_single (type, field, t);
5521 if (!maybe_const)
5522 t = c_wrap_maybe_const (t, true);
5523 t = digest_init (loc, type, t,
5524 NULL_TREE, false, true, 0);
5525 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5526 return t;
5528 error_at (loc, "cast to union type from type not present in union");
5529 return error_mark_node;
5531 else
5533 tree otype, ovalue;
5535 if (type == void_type_node)
5537 tree t = build1 (CONVERT_EXPR, type, value);
5538 SET_EXPR_LOCATION (t, loc);
5539 return t;
5542 otype = TREE_TYPE (value);
5544 /* Optionally warn about potentially worrisome casts. */
5545 if (warn_cast_qual
5546 && TREE_CODE (type) == POINTER_TYPE
5547 && TREE_CODE (otype) == POINTER_TYPE)
5548 handle_warn_cast_qual (loc, type, otype);
5550 /* Warn about conversions between pointers to disjoint
5551 address spaces. */
5552 if (TREE_CODE (type) == POINTER_TYPE
5553 && TREE_CODE (otype) == POINTER_TYPE
5554 && !null_pointer_constant_p (value))
5556 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5557 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5558 addr_space_t as_common;
5560 if (!addr_space_superset (as_to, as_from, &as_common))
5562 if (ADDR_SPACE_GENERIC_P (as_from))
5563 warning_at (loc, 0, "cast to %s address space pointer "
5564 "from disjoint generic address space pointer",
5565 c_addr_space_name (as_to));
5567 else if (ADDR_SPACE_GENERIC_P (as_to))
5568 warning_at (loc, 0, "cast to generic address space pointer "
5569 "from disjoint %s address space pointer",
5570 c_addr_space_name (as_from));
5572 else
5573 warning_at (loc, 0, "cast to %s address space pointer "
5574 "from disjoint %s address space pointer",
5575 c_addr_space_name (as_to),
5576 c_addr_space_name (as_from));
5580 /* Warn about possible alignment problems. */
5581 if (STRICT_ALIGNMENT
5582 && TREE_CODE (type) == POINTER_TYPE
5583 && TREE_CODE (otype) == POINTER_TYPE
5584 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5585 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5586 /* Don't warn about opaque types, where the actual alignment
5587 restriction is unknown. */
5588 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5589 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5590 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5591 warning_at (loc, OPT_Wcast_align,
5592 "cast increases required alignment of target type");
5594 if (TREE_CODE (type) == INTEGER_TYPE
5595 && TREE_CODE (otype) == POINTER_TYPE
5596 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5597 /* Unlike conversion of integers to pointers, where the
5598 warning is disabled for converting constants because
5599 of cases such as SIG_*, warn about converting constant
5600 pointers to integers. In some cases it may cause unwanted
5601 sign extension, and a warning is appropriate. */
5602 warning_at (loc, OPT_Wpointer_to_int_cast,
5603 "cast from pointer to integer of different size");
5605 if (TREE_CODE (value) == CALL_EXPR
5606 && TREE_CODE (type) != TREE_CODE (otype))
5607 warning_at (loc, OPT_Wbad_function_cast,
5608 "cast from function call of type %qT "
5609 "to non-matching type %qT", otype, type);
5611 if (TREE_CODE (type) == POINTER_TYPE
5612 && TREE_CODE (otype) == INTEGER_TYPE
5613 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5614 /* Don't warn about converting any constant. */
5615 && !TREE_CONSTANT (value))
5616 warning_at (loc,
5617 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5618 "of different size");
5620 if (warn_strict_aliasing <= 2)
5621 strict_aliasing_warning (otype, type, expr);
5623 /* If pedantic, warn for conversions between function and object
5624 pointer types, except for converting a null pointer constant
5625 to function pointer type. */
5626 if (pedantic
5627 && TREE_CODE (type) == POINTER_TYPE
5628 && TREE_CODE (otype) == POINTER_TYPE
5629 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5630 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5631 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5632 "conversion of function pointer to object pointer type");
5634 if (pedantic
5635 && TREE_CODE (type) == POINTER_TYPE
5636 && TREE_CODE (otype) == POINTER_TYPE
5637 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5638 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5639 && !null_pointer_constant_p (value))
5640 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5641 "conversion of object pointer to function pointer type");
5643 ovalue = value;
5644 value = convert (type, value);
5646 /* Ignore any integer overflow caused by the cast. */
5647 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5649 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5651 if (!TREE_OVERFLOW (value))
5653 /* Avoid clobbering a shared constant. */
5654 value = copy_node (value);
5655 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5658 else if (TREE_OVERFLOW (value))
5659 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5660 value = wide_int_to_tree (TREE_TYPE (value), value);
5664 /* Don't let a cast be an lvalue. */
5665 if (lvalue_p (value))
5666 value = non_lvalue_loc (loc, value);
5668 /* Don't allow the results of casting to floating-point or complex
5669 types be confused with actual constants, or casts involving
5670 integer and pointer types other than direct integer-to-integer
5671 and integer-to-pointer be confused with integer constant
5672 expressions and null pointer constants. */
5673 if (TREE_CODE (value) == REAL_CST
5674 || TREE_CODE (value) == COMPLEX_CST
5675 || (TREE_CODE (value) == INTEGER_CST
5676 && !((TREE_CODE (expr) == INTEGER_CST
5677 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5678 || TREE_CODE (expr) == REAL_CST
5679 || TREE_CODE (expr) == COMPLEX_CST)))
5680 value = build1 (NOP_EXPR, type, value);
5682 protected_set_expr_location (value, loc);
5683 return value;
5686 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5687 location of the open paren of the cast, or the position of the cast
5688 expr. */
5689 tree
5690 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5692 tree type;
5693 tree type_expr = NULL_TREE;
5694 bool type_expr_const = true;
5695 tree ret;
5696 int saved_wsp = warn_strict_prototypes;
5698 /* This avoids warnings about unprototyped casts on
5699 integers. E.g. "#define SIG_DFL (void(*)())0". */
5700 if (TREE_CODE (expr) == INTEGER_CST)
5701 warn_strict_prototypes = 0;
5702 type = groktypename (type_name, &type_expr, &type_expr_const);
5703 warn_strict_prototypes = saved_wsp;
5705 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5706 && reject_gcc_builtin (expr))
5707 return error_mark_node;
5709 ret = build_c_cast (loc, type, expr);
5710 if (type_expr)
5712 bool inner_expr_const = true;
5713 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5714 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5715 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5716 && inner_expr_const);
5717 SET_EXPR_LOCATION (ret, loc);
5720 if (!EXPR_HAS_LOCATION (ret))
5721 protected_set_expr_location (ret, loc);
5723 /* C++ does not permits types to be defined in a cast, but it
5724 allows references to incomplete types. */
5725 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5726 warning_at (loc, OPT_Wc___compat,
5727 "defining a type in a cast is invalid in C++");
5729 return ret;
5732 /* Build an assignment expression of lvalue LHS from value RHS.
5733 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5734 may differ from TREE_TYPE (LHS) for an enum bitfield.
5735 MODIFYCODE is the code for a binary operator that we use
5736 to combine the old value of LHS with RHS to get the new value.
5737 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5738 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5739 which may differ from TREE_TYPE (RHS) for an enum value.
5741 LOCATION is the location of the MODIFYCODE operator.
5742 RHS_LOC is the location of the RHS. */
5744 tree
5745 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5746 enum tree_code modifycode,
5747 location_t rhs_loc, tree rhs, tree rhs_origtype)
5749 tree result;
5750 tree newrhs;
5751 tree rhseval = NULL_TREE;
5752 tree lhstype = TREE_TYPE (lhs);
5753 tree olhstype = lhstype;
5754 bool npc;
5755 bool is_atomic_op;
5757 /* Types that aren't fully specified cannot be used in assignments. */
5758 lhs = require_complete_type (location, lhs);
5760 /* Avoid duplicate error messages from operands that had errors. */
5761 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5762 return error_mark_node;
5764 /* Ensure an error for assigning a non-lvalue array to an array in
5765 C90. */
5766 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5768 error_at (location, "assignment to expression with array type");
5769 return error_mark_node;
5772 /* For ObjC properties, defer this check. */
5773 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5774 return error_mark_node;
5776 is_atomic_op = really_atomic_lvalue (lhs);
5778 newrhs = rhs;
5780 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5782 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5783 lhs_origtype, modifycode, rhs_loc, rhs,
5784 rhs_origtype);
5785 if (inner == error_mark_node)
5786 return error_mark_node;
5787 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5788 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5789 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5790 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5791 protected_set_expr_location (result, location);
5792 return result;
5795 /* If a binary op has been requested, combine the old LHS value with the RHS
5796 producing the value we should actually store into the LHS. */
5798 if (modifycode != NOP_EXPR)
5800 lhs = c_fully_fold (lhs, false, NULL);
5801 lhs = stabilize_reference (lhs);
5803 /* Construct the RHS for any non-atomic compound assignemnt. */
5804 if (!is_atomic_op)
5806 /* If in LHS op= RHS the RHS has side-effects, ensure they
5807 are preevaluated before the rest of the assignment expression's
5808 side-effects, because RHS could contain e.g. function calls
5809 that modify LHS. */
5810 if (TREE_SIDE_EFFECTS (rhs))
5812 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5813 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5814 else
5815 newrhs = save_expr (rhs);
5816 rhseval = newrhs;
5817 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5818 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5819 newrhs);
5821 newrhs = build_binary_op (location,
5822 modifycode, lhs, newrhs, true);
5824 /* The original type of the right hand side is no longer
5825 meaningful. */
5826 rhs_origtype = NULL_TREE;
5830 if (c_dialect_objc ())
5832 /* Check if we are modifying an Objective-C property reference;
5833 if so, we need to generate setter calls. */
5834 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5835 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5836 else
5837 result = objc_maybe_build_modify_expr (lhs, newrhs);
5838 if (result)
5839 goto return_result;
5841 /* Else, do the check that we postponed for Objective-C. */
5842 if (!lvalue_or_else (location, lhs, lv_assign))
5843 return error_mark_node;
5846 /* Give an error for storing in something that is 'const'. */
5848 if (TYPE_READONLY (lhstype)
5849 || (RECORD_OR_UNION_TYPE_P (lhstype)
5850 && C_TYPE_FIELDS_READONLY (lhstype)))
5852 readonly_error (location, lhs, lv_assign);
5853 return error_mark_node;
5855 else if (TREE_READONLY (lhs))
5856 readonly_warning (lhs, lv_assign);
5858 /* If storing into a structure or union member,
5859 it has probably been given type `int'.
5860 Compute the type that would go with
5861 the actual amount of storage the member occupies. */
5863 if (TREE_CODE (lhs) == COMPONENT_REF
5864 && (TREE_CODE (lhstype) == INTEGER_TYPE
5865 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5866 || TREE_CODE (lhstype) == REAL_TYPE
5867 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5868 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5870 /* If storing in a field that is in actuality a short or narrower than one,
5871 we must store in the field in its actual type. */
5873 if (lhstype != TREE_TYPE (lhs))
5875 lhs = copy_node (lhs);
5876 TREE_TYPE (lhs) = lhstype;
5879 /* Issue -Wc++-compat warnings about an assignment to an enum type
5880 when LHS does not have its original type. This happens for,
5881 e.g., an enum bitfield in a struct. */
5882 if (warn_cxx_compat
5883 && lhs_origtype != NULL_TREE
5884 && lhs_origtype != lhstype
5885 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5887 tree checktype = (rhs_origtype != NULL_TREE
5888 ? rhs_origtype
5889 : TREE_TYPE (rhs));
5890 if (checktype != error_mark_node
5891 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5892 || (is_atomic_op && modifycode != NOP_EXPR)))
5893 warning_at (location, OPT_Wc___compat,
5894 "enum conversion in assignment is invalid in C++");
5897 /* If the lhs is atomic, remove that qualifier. */
5898 if (is_atomic_op)
5900 lhstype = build_qualified_type (lhstype,
5901 (TYPE_QUALS (lhstype)
5902 & ~TYPE_QUAL_ATOMIC));
5903 olhstype = build_qualified_type (olhstype,
5904 (TYPE_QUALS (lhstype)
5905 & ~TYPE_QUAL_ATOMIC));
5908 /* Convert new value to destination type. Fold it first, then
5909 restore any excess precision information, for the sake of
5910 conversion warnings. */
5912 if (!(is_atomic_op && modifycode != NOP_EXPR))
5914 tree rhs_semantic_type = NULL_TREE;
5915 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5917 rhs_semantic_type = TREE_TYPE (newrhs);
5918 newrhs = TREE_OPERAND (newrhs, 0);
5920 npc = null_pointer_constant_p (newrhs);
5921 newrhs = c_fully_fold (newrhs, false, NULL);
5922 if (rhs_semantic_type)
5923 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5924 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5925 rhs_origtype, ic_assign, npc,
5926 NULL_TREE, NULL_TREE, 0);
5927 if (TREE_CODE (newrhs) == ERROR_MARK)
5928 return error_mark_node;
5931 /* Emit ObjC write barrier, if necessary. */
5932 if (c_dialect_objc () && flag_objc_gc)
5934 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5935 if (result)
5937 protected_set_expr_location (result, location);
5938 goto return_result;
5942 /* Scan operands. */
5944 if (is_atomic_op)
5945 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5946 else
5948 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5949 TREE_SIDE_EFFECTS (result) = 1;
5950 protected_set_expr_location (result, location);
5953 /* If we got the LHS in a different type for storing in,
5954 convert the result back to the nominal type of LHS
5955 so that the value we return always has the same type
5956 as the LHS argument. */
5958 if (olhstype == TREE_TYPE (result))
5959 goto return_result;
5961 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5962 rhs_origtype, ic_assign, false, NULL_TREE,
5963 NULL_TREE, 0);
5964 protected_set_expr_location (result, location);
5966 return_result:
5967 if (rhseval)
5968 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5969 return result;
5972 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5973 This is used to implement -fplan9-extensions. */
5975 static bool
5976 find_anonymous_field_with_type (tree struct_type, tree type)
5978 tree field;
5979 bool found;
5981 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5982 found = false;
5983 for (field = TYPE_FIELDS (struct_type);
5984 field != NULL_TREE;
5985 field = TREE_CHAIN (field))
5987 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5988 ? c_build_qualified_type (TREE_TYPE (field),
5989 TYPE_QUAL_ATOMIC)
5990 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5991 if (DECL_NAME (field) == NULL
5992 && comptypes (type, fieldtype))
5994 if (found)
5995 return false;
5996 found = true;
5998 else if (DECL_NAME (field) == NULL
5999 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6000 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6002 if (found)
6003 return false;
6004 found = true;
6007 return found;
6010 /* RHS is an expression whose type is pointer to struct. If there is
6011 an anonymous field in RHS with type TYPE, then return a pointer to
6012 that field in RHS. This is used with -fplan9-extensions. This
6013 returns NULL if no conversion could be found. */
6015 static tree
6016 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6018 tree rhs_struct_type, lhs_main_type;
6019 tree field, found_field;
6020 bool found_sub_field;
6021 tree ret;
6023 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6024 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6025 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6027 gcc_assert (POINTER_TYPE_P (type));
6028 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6029 ? c_build_qualified_type (TREE_TYPE (type),
6030 TYPE_QUAL_ATOMIC)
6031 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6033 found_field = NULL_TREE;
6034 found_sub_field = false;
6035 for (field = TYPE_FIELDS (rhs_struct_type);
6036 field != NULL_TREE;
6037 field = TREE_CHAIN (field))
6039 if (DECL_NAME (field) != NULL_TREE
6040 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6041 continue;
6042 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6043 ? c_build_qualified_type (TREE_TYPE (field),
6044 TYPE_QUAL_ATOMIC)
6045 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6046 if (comptypes (lhs_main_type, fieldtype))
6048 if (found_field != NULL_TREE)
6049 return NULL_TREE;
6050 found_field = field;
6052 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6053 lhs_main_type))
6055 if (found_field != NULL_TREE)
6056 return NULL_TREE;
6057 found_field = field;
6058 found_sub_field = true;
6062 if (found_field == NULL_TREE)
6063 return NULL_TREE;
6065 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6066 build_fold_indirect_ref (rhs), found_field,
6067 NULL_TREE);
6068 ret = build_fold_addr_expr_loc (location, ret);
6070 if (found_sub_field)
6072 ret = convert_to_anonymous_field (location, type, ret);
6073 gcc_assert (ret != NULL_TREE);
6076 return ret;
6079 /* Issue an error message for a bad initializer component.
6080 GMSGID identifies the message.
6081 The component name is taken from the spelling stack. */
6083 static void
6084 error_init (location_t loc, const char *gmsgid)
6086 char *ofwhat;
6088 /* The gmsgid may be a format string with %< and %>. */
6089 error_at (loc, gmsgid);
6090 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6091 if (*ofwhat)
6092 inform (loc, "(near initialization for %qs)", ofwhat);
6095 /* Issue a pedantic warning for a bad initializer component. OPT is
6096 the option OPT_* (from options.h) controlling this warning or 0 if
6097 it is unconditionally given. GMSGID identifies the message. The
6098 component name is taken from the spelling stack. */
6100 static void ATTRIBUTE_GCC_DIAG (3,0)
6101 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6103 /* Use the location where a macro was expanded rather than where
6104 it was defined to make sure macros defined in system headers
6105 but used incorrectly elsewhere are diagnosed. */
6106 source_location exploc = expansion_point_location_if_in_system_header (loc);
6108 va_list ap;
6109 va_start (ap, gmsgid);
6110 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6111 va_end (ap);
6112 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6113 if (*ofwhat && warned)
6114 inform (exploc, "(near initialization for %qs)", ofwhat);
6117 /* Issue a warning for a bad initializer component.
6119 OPT is the OPT_W* value corresponding to the warning option that
6120 controls this warning. GMSGID identifies the message. The
6121 component name is taken from the spelling stack. */
6123 static void
6124 warning_init (location_t loc, int opt, const char *gmsgid)
6126 char *ofwhat;
6127 bool warned;
6129 /* Use the location where a macro was expanded rather than where
6130 it was defined to make sure macros defined in system headers
6131 but used incorrectly elsewhere are diagnosed. */
6132 source_location exploc = expansion_point_location_if_in_system_header (loc);
6134 /* The gmsgid may be a format string with %< and %>. */
6135 warned = warning_at (exploc, opt, gmsgid);
6136 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6137 if (*ofwhat && warned)
6138 inform (exploc, "(near initialization for %qs)", ofwhat);
6141 /* If TYPE is an array type and EXPR is a parenthesized string
6142 constant, warn if pedantic that EXPR is being used to initialize an
6143 object of type TYPE. */
6145 void
6146 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6148 if (pedantic
6149 && TREE_CODE (type) == ARRAY_TYPE
6150 && TREE_CODE (expr.value) == STRING_CST
6151 && expr.original_code != STRING_CST)
6152 pedwarn_init (loc, OPT_Wpedantic,
6153 "array initialized from parenthesized string constant");
6156 /* Convert value RHS to type TYPE as preparation for an assignment to
6157 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6158 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6159 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6160 constant before any folding.
6161 The real work of conversion is done by `convert'.
6162 The purpose of this function is to generate error messages
6163 for assignments that are not allowed in C.
6164 ERRTYPE says whether it is argument passing, assignment,
6165 initialization or return.
6167 In the following example, '~' denotes where EXPR_LOC and '^' where
6168 LOCATION point to:
6170 f (var); [ic_argpass]
6171 ^ ~~~
6172 x = var; [ic_assign]
6173 ^ ~~~;
6174 int x = var; [ic_init]
6176 return x; [ic_return]
6179 FUNCTION is a tree for the function being called.
6180 PARMNUM is the number of the argument, for printing in error messages. */
6182 static tree
6183 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6184 tree rhs, tree origtype, enum impl_conv errtype,
6185 bool null_pointer_constant, tree fundecl,
6186 tree function, int parmnum)
6188 enum tree_code codel = TREE_CODE (type);
6189 tree orig_rhs = rhs;
6190 tree rhstype;
6191 enum tree_code coder;
6192 tree rname = NULL_TREE;
6193 bool objc_ok = false;
6195 /* Use the expansion point location to handle cases such as user's
6196 function returning a wrong-type macro defined in a system header. */
6197 location = expansion_point_location_if_in_system_header (location);
6199 if (errtype == ic_argpass)
6201 tree selector;
6202 /* Change pointer to function to the function itself for
6203 diagnostics. */
6204 if (TREE_CODE (function) == ADDR_EXPR
6205 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6206 function = TREE_OPERAND (function, 0);
6208 /* Handle an ObjC selector specially for diagnostics. */
6209 selector = objc_message_selector ();
6210 rname = function;
6211 if (selector && parmnum > 2)
6213 rname = selector;
6214 parmnum -= 2;
6218 /* This macro is used to emit diagnostics to ensure that all format
6219 strings are complete sentences, visible to gettext and checked at
6220 compile time. */
6221 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6222 do { \
6223 switch (errtype) \
6225 case ic_argpass: \
6226 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6227 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6228 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6229 "expected %qT but argument is of type %qT", \
6230 type, rhstype); \
6231 break; \
6232 case ic_assign: \
6233 pedwarn (LOCATION, OPT, AS); \
6234 break; \
6235 case ic_init: \
6236 pedwarn_init (LOCATION, OPT, IN); \
6237 break; \
6238 case ic_return: \
6239 pedwarn (LOCATION, OPT, RE); \
6240 break; \
6241 default: \
6242 gcc_unreachable (); \
6244 } while (0)
6246 /* This macro is used to emit diagnostics to ensure that all format
6247 strings are complete sentences, visible to gettext and checked at
6248 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6249 extra parameter to enumerate qualifiers. */
6250 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6251 do { \
6252 switch (errtype) \
6254 case ic_argpass: \
6255 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6256 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6257 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6258 "expected %qT but argument is of type %qT", \
6259 type, rhstype); \
6260 break; \
6261 case ic_assign: \
6262 pedwarn (LOCATION, OPT, AS, QUALS); \
6263 break; \
6264 case ic_init: \
6265 pedwarn (LOCATION, OPT, IN, QUALS); \
6266 break; \
6267 case ic_return: \
6268 pedwarn (LOCATION, OPT, RE, QUALS); \
6269 break; \
6270 default: \
6271 gcc_unreachable (); \
6273 } while (0)
6275 /* This macro is used to emit diagnostics to ensure that all format
6276 strings are complete sentences, visible to gettext and checked at
6277 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6278 warning_at instead of pedwarn. */
6279 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6280 do { \
6281 switch (errtype) \
6283 case ic_argpass: \
6284 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6285 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6286 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6287 "expected %qT but argument is of type %qT", \
6288 type, rhstype); \
6289 break; \
6290 case ic_assign: \
6291 warning_at (LOCATION, OPT, AS, QUALS); \
6292 break; \
6293 case ic_init: \
6294 warning_at (LOCATION, OPT, IN, QUALS); \
6295 break; \
6296 case ic_return: \
6297 warning_at (LOCATION, OPT, RE, QUALS); \
6298 break; \
6299 default: \
6300 gcc_unreachable (); \
6302 } while (0)
6304 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6305 rhs = TREE_OPERAND (rhs, 0);
6307 rhstype = TREE_TYPE (rhs);
6308 coder = TREE_CODE (rhstype);
6310 if (coder == ERROR_MARK)
6311 return error_mark_node;
6313 if (c_dialect_objc ())
6315 int parmno;
6317 switch (errtype)
6319 case ic_return:
6320 parmno = 0;
6321 break;
6323 case ic_assign:
6324 parmno = -1;
6325 break;
6327 case ic_init:
6328 parmno = -2;
6329 break;
6331 default:
6332 parmno = parmnum;
6333 break;
6336 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6339 if (warn_cxx_compat)
6341 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6342 if (checktype != error_mark_node
6343 && TREE_CODE (type) == ENUMERAL_TYPE
6344 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6345 switch (errtype)
6347 case ic_argpass:
6348 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6349 "passing argument %d of %qE is invalid in C++",
6350 parmnum, rname))
6351 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6352 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6353 "expected %qT but argument is of type %qT",
6354 type, rhstype);
6355 break;
6356 case ic_assign:
6357 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6358 "%qT in assignment is invalid in C++", rhstype, type);
6359 break;
6360 case ic_init:
6361 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6362 "%qT to %qT in initialization is invalid in C++",
6363 rhstype, type);
6364 break;
6365 case ic_return:
6366 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6367 "%qT in return is invalid in C++", rhstype, type);
6368 break;
6369 default:
6370 gcc_unreachable ();
6374 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6375 return rhs;
6377 if (coder == VOID_TYPE)
6379 /* Except for passing an argument to an unprototyped function,
6380 this is a constraint violation. When passing an argument to
6381 an unprototyped function, it is compile-time undefined;
6382 making it a constraint in that case was rejected in
6383 DR#252. */
6384 error_at (location, "void value not ignored as it ought to be");
6385 return error_mark_node;
6387 rhs = require_complete_type (location, rhs);
6388 if (rhs == error_mark_node)
6389 return error_mark_node;
6391 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6392 return error_mark_node;
6394 /* A non-reference type can convert to a reference. This handles
6395 va_start, va_copy and possibly port built-ins. */
6396 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6398 if (!lvalue_p (rhs))
6400 error_at (location, "cannot pass rvalue to reference parameter");
6401 return error_mark_node;
6403 if (!c_mark_addressable (rhs))
6404 return error_mark_node;
6405 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6406 SET_EXPR_LOCATION (rhs, location);
6408 rhs = convert_for_assignment (location, expr_loc,
6409 build_pointer_type (TREE_TYPE (type)),
6410 rhs, origtype, errtype,
6411 null_pointer_constant, fundecl, function,
6412 parmnum);
6413 if (rhs == error_mark_node)
6414 return error_mark_node;
6416 rhs = build1 (NOP_EXPR, type, rhs);
6417 SET_EXPR_LOCATION (rhs, location);
6418 return rhs;
6420 /* Some types can interconvert without explicit casts. */
6421 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6422 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6423 return convert (type, rhs);
6424 /* Arithmetic types all interconvert, and enum is treated like int. */
6425 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6426 || codel == FIXED_POINT_TYPE
6427 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6428 || codel == BOOLEAN_TYPE)
6429 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6430 || coder == FIXED_POINT_TYPE
6431 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6432 || coder == BOOLEAN_TYPE))
6434 tree ret;
6435 bool save = in_late_binary_op;
6436 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6437 || (coder == REAL_TYPE
6438 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6439 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6440 in_late_binary_op = true;
6441 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6442 ? expr_loc : location, type, orig_rhs);
6443 in_late_binary_op = save;
6444 return ret;
6447 /* Aggregates in different TUs might need conversion. */
6448 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6449 && codel == coder
6450 && comptypes (type, rhstype))
6451 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6452 ? expr_loc : location, type, rhs);
6454 /* Conversion to a transparent union or record from its member types.
6455 This applies only to function arguments. */
6456 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6457 && TYPE_TRANSPARENT_AGGR (type))
6458 && errtype == ic_argpass)
6460 tree memb, marginal_memb = NULL_TREE;
6462 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6464 tree memb_type = TREE_TYPE (memb);
6466 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6467 TYPE_MAIN_VARIANT (rhstype)))
6468 break;
6470 if (TREE_CODE (memb_type) != POINTER_TYPE)
6471 continue;
6473 if (coder == POINTER_TYPE)
6475 tree ttl = TREE_TYPE (memb_type);
6476 tree ttr = TREE_TYPE (rhstype);
6478 /* Any non-function converts to a [const][volatile] void *
6479 and vice versa; otherwise, targets must be the same.
6480 Meanwhile, the lhs target must have all the qualifiers of
6481 the rhs. */
6482 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6483 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6484 || comp_target_types (location, memb_type, rhstype))
6486 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6487 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6488 /* If this type won't generate any warnings, use it. */
6489 if (lquals == rquals
6490 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6491 && TREE_CODE (ttl) == FUNCTION_TYPE)
6492 ? ((lquals | rquals) == rquals)
6493 : ((lquals | rquals) == lquals)))
6494 break;
6496 /* Keep looking for a better type, but remember this one. */
6497 if (!marginal_memb)
6498 marginal_memb = memb;
6502 /* Can convert integer zero to any pointer type. */
6503 if (null_pointer_constant)
6505 rhs = null_pointer_node;
6506 break;
6510 if (memb || marginal_memb)
6512 if (!memb)
6514 /* We have only a marginally acceptable member type;
6515 it needs a warning. */
6516 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6517 tree ttr = TREE_TYPE (rhstype);
6519 /* Const and volatile mean something different for function
6520 types, so the usual warnings are not appropriate. */
6521 if (TREE_CODE (ttr) == FUNCTION_TYPE
6522 && TREE_CODE (ttl) == FUNCTION_TYPE)
6524 /* Because const and volatile on functions are
6525 restrictions that say the function will not do
6526 certain things, it is okay to use a const or volatile
6527 function where an ordinary one is wanted, but not
6528 vice-versa. */
6529 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6530 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6531 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6532 OPT_Wdiscarded_qualifiers,
6533 G_("passing argument %d of %qE "
6534 "makes %q#v qualified function "
6535 "pointer from unqualified"),
6536 G_("assignment makes %q#v qualified "
6537 "function pointer from "
6538 "unqualified"),
6539 G_("initialization makes %q#v qualified "
6540 "function pointer from "
6541 "unqualified"),
6542 G_("return makes %q#v qualified function "
6543 "pointer from unqualified"),
6544 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6546 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6547 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6548 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6549 OPT_Wdiscarded_qualifiers,
6550 G_("passing argument %d of %qE discards "
6551 "%qv qualifier from pointer target type"),
6552 G_("assignment discards %qv qualifier "
6553 "from pointer target type"),
6554 G_("initialization discards %qv qualifier "
6555 "from pointer target type"),
6556 G_("return discards %qv qualifier from "
6557 "pointer target type"),
6558 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6560 memb = marginal_memb;
6563 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6564 pedwarn (location, OPT_Wpedantic,
6565 "ISO C prohibits argument conversion to union type");
6567 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6568 return build_constructor_single (type, memb, rhs);
6572 /* Conversions among pointers */
6573 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6574 && (coder == codel))
6576 tree ttl = TREE_TYPE (type);
6577 tree ttr = TREE_TYPE (rhstype);
6578 tree mvl = ttl;
6579 tree mvr = ttr;
6580 bool is_opaque_pointer;
6581 int target_cmp = 0; /* Cache comp_target_types () result. */
6582 addr_space_t asl;
6583 addr_space_t asr;
6585 if (TREE_CODE (mvl) != ARRAY_TYPE)
6586 mvl = (TYPE_ATOMIC (mvl)
6587 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6588 TYPE_QUAL_ATOMIC)
6589 : TYPE_MAIN_VARIANT (mvl));
6590 if (TREE_CODE (mvr) != ARRAY_TYPE)
6591 mvr = (TYPE_ATOMIC (mvr)
6592 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6593 TYPE_QUAL_ATOMIC)
6594 : TYPE_MAIN_VARIANT (mvr));
6595 /* Opaque pointers are treated like void pointers. */
6596 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6598 /* The Plan 9 compiler permits a pointer to a struct to be
6599 automatically converted into a pointer to an anonymous field
6600 within the struct. */
6601 if (flag_plan9_extensions
6602 && RECORD_OR_UNION_TYPE_P (mvl)
6603 && RECORD_OR_UNION_TYPE_P (mvr)
6604 && mvl != mvr)
6606 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6607 if (new_rhs != NULL_TREE)
6609 rhs = new_rhs;
6610 rhstype = TREE_TYPE (rhs);
6611 coder = TREE_CODE (rhstype);
6612 ttr = TREE_TYPE (rhstype);
6613 mvr = TYPE_MAIN_VARIANT (ttr);
6617 /* C++ does not allow the implicit conversion void* -> T*. However,
6618 for the purpose of reducing the number of false positives, we
6619 tolerate the special case of
6621 int *p = NULL;
6623 where NULL is typically defined in C to be '(void *) 0'. */
6624 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6625 warning_at (errtype == ic_argpass ? expr_loc : location,
6626 OPT_Wc___compat,
6627 "request for implicit conversion "
6628 "from %qT to %qT not permitted in C++", rhstype, type);
6630 /* See if the pointers point to incompatible address spaces. */
6631 asl = TYPE_ADDR_SPACE (ttl);
6632 asr = TYPE_ADDR_SPACE (ttr);
6633 if (!null_pointer_constant_p (rhs)
6634 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6636 switch (errtype)
6638 case ic_argpass:
6639 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6640 "non-enclosed address space", parmnum, rname);
6641 break;
6642 case ic_assign:
6643 error_at (location, "assignment from pointer to "
6644 "non-enclosed address space");
6645 break;
6646 case ic_init:
6647 error_at (location, "initialization from pointer to "
6648 "non-enclosed address space");
6649 break;
6650 case ic_return:
6651 error_at (location, "return from pointer to "
6652 "non-enclosed address space");
6653 break;
6654 default:
6655 gcc_unreachable ();
6657 return error_mark_node;
6660 /* Check if the right-hand side has a format attribute but the
6661 left-hand side doesn't. */
6662 if (warn_suggest_attribute_format
6663 && check_missing_format_attribute (type, rhstype))
6665 switch (errtype)
6667 case ic_argpass:
6668 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6669 "argument %d of %qE might be "
6670 "a candidate for a format attribute",
6671 parmnum, rname);
6672 break;
6673 case ic_assign:
6674 warning_at (location, OPT_Wsuggest_attribute_format,
6675 "assignment left-hand side might be "
6676 "a candidate for a format attribute");
6677 break;
6678 case ic_init:
6679 warning_at (location, OPT_Wsuggest_attribute_format,
6680 "initialization left-hand side might be "
6681 "a candidate for a format attribute");
6682 break;
6683 case ic_return:
6684 warning_at (location, OPT_Wsuggest_attribute_format,
6685 "return type might be "
6686 "a candidate for a format attribute");
6687 break;
6688 default:
6689 gcc_unreachable ();
6693 /* Any non-function converts to a [const][volatile] void *
6694 and vice versa; otherwise, targets must be the same.
6695 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6696 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6697 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6698 || (target_cmp = comp_target_types (location, type, rhstype))
6699 || is_opaque_pointer
6700 || ((c_common_unsigned_type (mvl)
6701 == c_common_unsigned_type (mvr))
6702 && (c_common_signed_type (mvl)
6703 == c_common_signed_type (mvr))
6704 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6706 /* Warn about loss of qualifers from pointers to arrays with
6707 qualifiers on the element type. */
6708 if (TREE_CODE (ttr) == ARRAY_TYPE)
6710 ttr = strip_array_types (ttr);
6711 ttl = strip_array_types (ttl);
6713 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6714 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6715 WARNING_FOR_QUALIFIERS (location, expr_loc,
6716 OPT_Wdiscarded_array_qualifiers,
6717 G_("passing argument %d of %qE discards "
6718 "%qv qualifier from pointer target type"),
6719 G_("assignment discards %qv qualifier "
6720 "from pointer target type"),
6721 G_("initialization discards %qv qualifier "
6722 "from pointer target type"),
6723 G_("return discards %qv qualifier from "
6724 "pointer target type"),
6725 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6727 else if (pedantic
6728 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6730 (VOID_TYPE_P (ttr)
6731 && !null_pointer_constant
6732 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6733 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6734 G_("ISO C forbids passing argument %d of "
6735 "%qE between function pointer "
6736 "and %<void *%>"),
6737 G_("ISO C forbids assignment between "
6738 "function pointer and %<void *%>"),
6739 G_("ISO C forbids initialization between "
6740 "function pointer and %<void *%>"),
6741 G_("ISO C forbids return between function "
6742 "pointer and %<void *%>"));
6743 /* Const and volatile mean something different for function types,
6744 so the usual warnings are not appropriate. */
6745 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6746 && TREE_CODE (ttl) != FUNCTION_TYPE)
6748 /* Don't warn about loss of qualifier for conversions from
6749 qualified void* to pointers to arrays with corresponding
6750 qualifier on the element type. */
6751 if (!pedantic)
6752 ttl = strip_array_types (ttl);
6754 /* Assignments between atomic and non-atomic objects are OK. */
6755 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6756 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6758 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6759 OPT_Wdiscarded_qualifiers,
6760 G_("passing argument %d of %qE discards "
6761 "%qv qualifier from pointer target type"),
6762 G_("assignment discards %qv qualifier "
6763 "from pointer target type"),
6764 G_("initialization discards %qv qualifier "
6765 "from pointer target type"),
6766 G_("return discards %qv qualifier from "
6767 "pointer target type"),
6768 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6770 /* If this is not a case of ignoring a mismatch in signedness,
6771 no warning. */
6772 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6773 || target_cmp)
6775 /* If there is a mismatch, do warn. */
6776 else if (warn_pointer_sign)
6777 switch (errtype)
6779 case ic_argpass:
6780 if (pedwarn (expr_loc, OPT_Wpointer_sign,
6781 "pointer targets in passing argument %d of "
6782 "%qE differ in signedness", parmnum, rname))
6783 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6784 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6785 "expected %qT but argument is of type %qT",
6786 type, rhstype);
6787 break;
6788 case ic_assign:
6789 pedwarn (location, OPT_Wpointer_sign,
6790 "pointer targets in assignment from %qT to %qT "
6791 "differ in signedness", rhstype, type);
6792 break;
6793 case ic_init:
6794 pedwarn_init (location, OPT_Wpointer_sign,
6795 "pointer targets in initialization of %qT "
6796 "from %qT differ in signedness", type,
6797 rhstype);
6798 break;
6799 case ic_return:
6800 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6801 "returning %qT from a function with return type "
6802 "%qT differ in signedness", rhstype, type);
6803 break;
6804 default:
6805 gcc_unreachable ();
6808 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6809 && TREE_CODE (ttr) == FUNCTION_TYPE)
6811 /* Because const and volatile on functions are restrictions
6812 that say the function will not do certain things,
6813 it is okay to use a const or volatile function
6814 where an ordinary one is wanted, but not vice-versa. */
6815 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6816 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6817 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6818 OPT_Wdiscarded_qualifiers,
6819 G_("passing argument %d of %qE makes "
6820 "%q#v qualified function pointer "
6821 "from unqualified"),
6822 G_("assignment makes %q#v qualified function "
6823 "pointer from unqualified"),
6824 G_("initialization makes %q#v qualified "
6825 "function pointer from unqualified"),
6826 G_("return makes %q#v qualified function "
6827 "pointer from unqualified"),
6828 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6831 /* Avoid warning about the volatile ObjC EH puts on decls. */
6832 else if (!objc_ok)
6834 switch (errtype)
6836 case ic_argpass:
6837 if (pedwarn (expr_loc, OPT_Wincompatible_pointer_types,
6838 "passing argument %d of %qE from incompatible "
6839 "pointer type", parmnum, rname))
6840 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6841 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6842 "expected %qT but argument is of type %qT",
6843 type, rhstype);
6844 break;
6845 case ic_assign:
6846 pedwarn (location, OPT_Wincompatible_pointer_types,
6847 "assignment to %qT from incompatible pointer type %qT",
6848 type, rhstype);
6849 break;
6850 case ic_init:
6851 pedwarn_init (location, OPT_Wincompatible_pointer_types,
6852 "initialization of %qT from incompatible pointer "
6853 "type %qT", type, rhstype);
6854 break;
6855 case ic_return:
6856 pedwarn (location, OPT_Wincompatible_pointer_types,
6857 "returning %qT from a function with incompatible "
6858 "return type %qT", rhstype, type);
6859 break;
6860 default:
6861 gcc_unreachable ();
6865 return convert (type, rhs);
6867 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6869 /* ??? This should not be an error when inlining calls to
6870 unprototyped functions. */
6871 error_at (location, "invalid use of non-lvalue array");
6872 return error_mark_node;
6874 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6876 /* An explicit constant 0 can convert to a pointer,
6877 or one that results from arithmetic, even including
6878 a cast to integer type. */
6879 if (!null_pointer_constant)
6880 switch (errtype)
6882 case ic_argpass:
6883 if (pedwarn (expr_loc, OPT_Wint_conversion,
6884 "passing argument %d of %qE makes pointer from "
6885 "integer without a cast", parmnum, rname))
6886 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6887 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6888 "expected %qT but argument is of type %qT",
6889 type, rhstype);
6890 break;
6891 case ic_assign:
6892 pedwarn (location, OPT_Wint_conversion,
6893 "assignment to %qT from %qT makes pointer from integer "
6894 "without a cast", type, rhstype);
6895 break;
6896 case ic_init:
6897 pedwarn_init (location, OPT_Wint_conversion,
6898 "initialization of %qT from %qT makes pointer from "
6899 "integer without a cast", type, rhstype);
6900 break;
6901 case ic_return:
6902 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6903 "function with return type %qT makes pointer from "
6904 "integer without a cast", rhstype, type);
6905 break;
6906 default:
6907 gcc_unreachable ();
6910 return convert (type, rhs);
6912 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6914 switch (errtype)
6916 case ic_argpass:
6917 if (pedwarn (expr_loc, OPT_Wint_conversion,
6918 "passing argument %d of %qE makes integer from "
6919 "pointer without a cast", parmnum, rname))
6920 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6921 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6922 "expected %qT but argument is of type %qT",
6923 type, rhstype);
6924 break;
6925 case ic_assign:
6926 pedwarn (location, OPT_Wint_conversion,
6927 "assignment to %qT from %qT makes integer from pointer "
6928 "without a cast", type, rhstype);
6929 break;
6930 case ic_init:
6931 pedwarn_init (location, OPT_Wint_conversion,
6932 "initialization of %qT from %qT makes integer from "
6933 "pointer without a cast", type, rhstype);
6934 break;
6935 case ic_return:
6936 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6937 "function with return type %qT makes integer from "
6938 "pointer without a cast", rhstype, type);
6939 break;
6940 default:
6941 gcc_unreachable ();
6944 return convert (type, rhs);
6946 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6948 tree ret;
6949 bool save = in_late_binary_op;
6950 in_late_binary_op = true;
6951 ret = convert (type, rhs);
6952 in_late_binary_op = save;
6953 return ret;
6956 switch (errtype)
6958 case ic_argpass:
6959 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6960 rname);
6961 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6962 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6963 "expected %qT but argument is of type %qT", type, rhstype);
6964 break;
6965 case ic_assign:
6966 error_at (location, "incompatible types when assigning to type %qT from "
6967 "type %qT", type, rhstype);
6968 break;
6969 case ic_init:
6970 error_at (location,
6971 "incompatible types when initializing type %qT using type %qT",
6972 type, rhstype);
6973 break;
6974 case ic_return:
6975 error_at (location,
6976 "incompatible types when returning type %qT but %qT was "
6977 "expected", rhstype, type);
6978 break;
6979 default:
6980 gcc_unreachable ();
6983 return error_mark_node;
6986 /* If VALUE is a compound expr all of whose expressions are constant, then
6987 return its value. Otherwise, return error_mark_node.
6989 This is for handling COMPOUND_EXPRs as initializer elements
6990 which is allowed with a warning when -pedantic is specified. */
6992 static tree
6993 valid_compound_expr_initializer (tree value, tree endtype)
6995 if (TREE_CODE (value) == COMPOUND_EXPR)
6997 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6998 == error_mark_node)
6999 return error_mark_node;
7000 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7001 endtype);
7003 else if (!initializer_constant_valid_p (value, endtype))
7004 return error_mark_node;
7005 else
7006 return value;
7009 /* Perform appropriate conversions on the initial value of a variable,
7010 store it in the declaration DECL,
7011 and print any error messages that are appropriate.
7012 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7013 If the init is invalid, store an ERROR_MARK.
7015 INIT_LOC is the location of the initial value. */
7017 void
7018 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7020 tree value, type;
7021 bool npc = false;
7023 /* If variable's type was invalidly declared, just ignore it. */
7025 type = TREE_TYPE (decl);
7026 if (TREE_CODE (type) == ERROR_MARK)
7027 return;
7029 /* Digest the specified initializer into an expression. */
7031 if (init)
7032 npc = null_pointer_constant_p (init);
7033 value = digest_init (init_loc, type, init, origtype, npc,
7034 true, TREE_STATIC (decl));
7036 /* Store the expression if valid; else report error. */
7038 if (!in_system_header_at (input_location)
7039 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7040 warning (OPT_Wtraditional, "traditional C rejects automatic "
7041 "aggregate initialization");
7043 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7044 DECL_INITIAL (decl) = value;
7046 /* ANSI wants warnings about out-of-range constant initializers. */
7047 STRIP_TYPE_NOPS (value);
7048 if (TREE_STATIC (decl))
7049 constant_expression_warning (value);
7051 /* Check if we need to set array size from compound literal size. */
7052 if (TREE_CODE (type) == ARRAY_TYPE
7053 && TYPE_DOMAIN (type) == NULL_TREE
7054 && value != error_mark_node)
7056 tree inside_init = init;
7058 STRIP_TYPE_NOPS (inside_init);
7059 inside_init = fold (inside_init);
7061 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7063 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7065 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7067 /* For int foo[] = (int [3]){1}; we need to set array size
7068 now since later on array initializer will be just the
7069 brace enclosed list of the compound literal. */
7070 tree etype = strip_array_types (TREE_TYPE (decl));
7071 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7072 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7073 layout_type (type);
7074 layout_decl (cldecl, 0);
7075 TREE_TYPE (decl)
7076 = c_build_qualified_type (type, TYPE_QUALS (etype));
7082 /* Methods for storing and printing names for error messages. */
7084 /* Implement a spelling stack that allows components of a name to be pushed
7085 and popped. Each element on the stack is this structure. */
7087 struct spelling
7089 int kind;
7090 union
7092 unsigned HOST_WIDE_INT i;
7093 const char *s;
7094 } u;
7097 #define SPELLING_STRING 1
7098 #define SPELLING_MEMBER 2
7099 #define SPELLING_BOUNDS 3
7101 static struct spelling *spelling; /* Next stack element (unused). */
7102 static struct spelling *spelling_base; /* Spelling stack base. */
7103 static int spelling_size; /* Size of the spelling stack. */
7105 /* Macros to save and restore the spelling stack around push_... functions.
7106 Alternative to SAVE_SPELLING_STACK. */
7108 #define SPELLING_DEPTH() (spelling - spelling_base)
7109 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7111 /* Push an element on the spelling stack with type KIND and assign VALUE
7112 to MEMBER. */
7114 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7116 int depth = SPELLING_DEPTH (); \
7118 if (depth >= spelling_size) \
7120 spelling_size += 10; \
7121 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7122 spelling_size); \
7123 RESTORE_SPELLING_DEPTH (depth); \
7126 spelling->kind = (KIND); \
7127 spelling->MEMBER = (VALUE); \
7128 spelling++; \
7131 /* Push STRING on the stack. Printed literally. */
7133 static void
7134 push_string (const char *string)
7136 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7139 /* Push a member name on the stack. Printed as '.' STRING. */
7141 static void
7142 push_member_name (tree decl)
7144 const char *const string
7145 = (DECL_NAME (decl)
7146 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7147 : _("<anonymous>"));
7148 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7151 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7153 static void
7154 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7156 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7159 /* Compute the maximum size in bytes of the printed spelling. */
7161 static int
7162 spelling_length (void)
7164 int size = 0;
7165 struct spelling *p;
7167 for (p = spelling_base; p < spelling; p++)
7169 if (p->kind == SPELLING_BOUNDS)
7170 size += 25;
7171 else
7172 size += strlen (p->u.s) + 1;
7175 return size;
7178 /* Print the spelling to BUFFER and return it. */
7180 static char *
7181 print_spelling (char *buffer)
7183 char *d = buffer;
7184 struct spelling *p;
7186 for (p = spelling_base; p < spelling; p++)
7187 if (p->kind == SPELLING_BOUNDS)
7189 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7190 d += strlen (d);
7192 else
7194 const char *s;
7195 if (p->kind == SPELLING_MEMBER)
7196 *d++ = '.';
7197 for (s = p->u.s; (*d = *s++); d++)
7200 *d++ = '\0';
7201 return buffer;
7204 /* Digest the parser output INIT as an initializer for type TYPE.
7205 Return a C expression of type TYPE to represent the initial value.
7207 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7209 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7211 If INIT is a string constant, STRICT_STRING is true if it is
7212 unparenthesized or we should not warn here for it being parenthesized.
7213 For other types of INIT, STRICT_STRING is not used.
7215 INIT_LOC is the location of the INIT.
7217 REQUIRE_CONSTANT requests an error if non-constant initializers or
7218 elements are seen. */
7220 static tree
7221 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7222 bool null_pointer_constant, bool strict_string,
7223 int require_constant)
7225 enum tree_code code = TREE_CODE (type);
7226 tree inside_init = init;
7227 tree semantic_type = NULL_TREE;
7228 bool maybe_const = true;
7230 if (type == error_mark_node
7231 || !init
7232 || error_operand_p (init))
7233 return error_mark_node;
7235 STRIP_TYPE_NOPS (inside_init);
7237 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7239 semantic_type = TREE_TYPE (inside_init);
7240 inside_init = TREE_OPERAND (inside_init, 0);
7242 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7243 inside_init = decl_constant_value_for_optimization (inside_init);
7245 /* Initialization of an array of chars from a string constant
7246 optionally enclosed in braces. */
7248 if (code == ARRAY_TYPE && inside_init
7249 && TREE_CODE (inside_init) == STRING_CST)
7251 tree typ1
7252 = (TYPE_ATOMIC (TREE_TYPE (type))
7253 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7254 TYPE_QUAL_ATOMIC)
7255 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7256 /* Note that an array could be both an array of character type
7257 and an array of wchar_t if wchar_t is signed char or unsigned
7258 char. */
7259 bool char_array = (typ1 == char_type_node
7260 || typ1 == signed_char_type_node
7261 || typ1 == unsigned_char_type_node);
7262 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7263 bool char16_array = !!comptypes (typ1, char16_type_node);
7264 bool char32_array = !!comptypes (typ1, char32_type_node);
7266 if (char_array || wchar_array || char16_array || char32_array)
7268 struct c_expr expr;
7269 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7270 expr.value = inside_init;
7271 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7272 expr.original_type = NULL;
7273 maybe_warn_string_init (init_loc, type, expr);
7275 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7276 pedwarn_init (init_loc, OPT_Wpedantic,
7277 "initialization of a flexible array member");
7279 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7280 TYPE_MAIN_VARIANT (type)))
7281 return inside_init;
7283 if (char_array)
7285 if (typ2 != char_type_node)
7287 error_init (init_loc, "char-array initialized from wide "
7288 "string");
7289 return error_mark_node;
7292 else
7294 if (typ2 == char_type_node)
7296 error_init (init_loc, "wide character array initialized "
7297 "from non-wide string");
7298 return error_mark_node;
7300 else if (!comptypes(typ1, typ2))
7302 error_init (init_loc, "wide character array initialized "
7303 "from incompatible wide string");
7304 return error_mark_node;
7308 TREE_TYPE (inside_init) = type;
7309 if (TYPE_DOMAIN (type) != NULL_TREE
7310 && TYPE_SIZE (type) != NULL_TREE
7311 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7313 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7315 /* Subtract the size of a single (possibly wide) character
7316 because it's ok to ignore the terminating null char
7317 that is counted in the length of the constant. */
7318 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7319 (len
7320 - (TYPE_PRECISION (typ1)
7321 / BITS_PER_UNIT))))
7322 pedwarn_init (init_loc, 0,
7323 ("initializer-string for array of chars "
7324 "is too long"));
7325 else if (warn_cxx_compat
7326 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7327 warning_at (init_loc, OPT_Wc___compat,
7328 ("initializer-string for array chars "
7329 "is too long for C++"));
7332 return inside_init;
7334 else if (INTEGRAL_TYPE_P (typ1))
7336 error_init (init_loc, "array of inappropriate type initialized "
7337 "from string constant");
7338 return error_mark_node;
7342 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7343 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7344 below and handle as a constructor. */
7345 if (code == VECTOR_TYPE
7346 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7347 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7348 && TREE_CONSTANT (inside_init))
7350 if (TREE_CODE (inside_init) == VECTOR_CST
7351 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7352 TYPE_MAIN_VARIANT (type)))
7353 return inside_init;
7355 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7357 unsigned HOST_WIDE_INT ix;
7358 tree value;
7359 bool constant_p = true;
7361 /* Iterate through elements and check if all constructor
7362 elements are *_CSTs. */
7363 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7364 if (!CONSTANT_CLASS_P (value))
7366 constant_p = false;
7367 break;
7370 if (constant_p)
7371 return build_vector_from_ctor (type,
7372 CONSTRUCTOR_ELTS (inside_init));
7376 if (warn_sequence_point)
7377 verify_sequence_points (inside_init);
7379 /* Any type can be initialized
7380 from an expression of the same type, optionally with braces. */
7382 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7383 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7384 TYPE_MAIN_VARIANT (type))
7385 || (code == ARRAY_TYPE
7386 && comptypes (TREE_TYPE (inside_init), type))
7387 || (code == VECTOR_TYPE
7388 && comptypes (TREE_TYPE (inside_init), type))
7389 || (code == POINTER_TYPE
7390 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7391 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7392 TREE_TYPE (type)))))
7394 if (code == POINTER_TYPE)
7396 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7398 if (TREE_CODE (inside_init) == STRING_CST
7399 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7400 inside_init = array_to_pointer_conversion
7401 (init_loc, inside_init);
7402 else
7404 error_init (init_loc, "invalid use of non-lvalue array");
7405 return error_mark_node;
7410 if (code == VECTOR_TYPE)
7411 /* Although the types are compatible, we may require a
7412 conversion. */
7413 inside_init = convert (type, inside_init);
7415 if (require_constant
7416 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7418 /* As an extension, allow initializing objects with static storage
7419 duration with compound literals (which are then treated just as
7420 the brace enclosed list they contain). Also allow this for
7421 vectors, as we can only assign them with compound literals. */
7422 if (flag_isoc99 && code != VECTOR_TYPE)
7423 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7424 "is not constant");
7425 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7426 inside_init = DECL_INITIAL (decl);
7429 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7430 && TREE_CODE (inside_init) != CONSTRUCTOR)
7432 error_init (init_loc, "array initialized from non-constant array "
7433 "expression");
7434 return error_mark_node;
7437 /* Compound expressions can only occur here if -Wpedantic or
7438 -pedantic-errors is specified. In the later case, we always want
7439 an error. In the former case, we simply want a warning. */
7440 if (require_constant && pedantic
7441 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7443 inside_init
7444 = valid_compound_expr_initializer (inside_init,
7445 TREE_TYPE (inside_init));
7446 if (inside_init == error_mark_node)
7447 error_init (init_loc, "initializer element is not constant");
7448 else
7449 pedwarn_init (init_loc, OPT_Wpedantic,
7450 "initializer element is not constant");
7451 if (flag_pedantic_errors)
7452 inside_init = error_mark_node;
7454 else if (require_constant
7455 && !initializer_constant_valid_p (inside_init,
7456 TREE_TYPE (inside_init)))
7458 error_init (init_loc, "initializer element is not constant");
7459 inside_init = error_mark_node;
7461 else if (require_constant && !maybe_const)
7462 pedwarn_init (init_loc, OPT_Wpedantic,
7463 "initializer element is not a constant expression");
7465 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7466 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7467 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7468 type, inside_init, origtype,
7469 ic_init, null_pointer_constant,
7470 NULL_TREE, NULL_TREE, 0);
7471 return inside_init;
7474 /* Handle scalar types, including conversions. */
7476 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7477 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7478 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7480 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7481 && (TREE_CODE (init) == STRING_CST
7482 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7483 inside_init = init = array_to_pointer_conversion (init_loc, init);
7484 if (semantic_type)
7485 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7486 inside_init);
7487 inside_init
7488 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7489 inside_init, origtype, ic_init,
7490 null_pointer_constant, NULL_TREE, NULL_TREE,
7493 /* Check to see if we have already given an error message. */
7494 if (inside_init == error_mark_node)
7496 else if (require_constant && !TREE_CONSTANT (inside_init))
7498 error_init (init_loc, "initializer element is not constant");
7499 inside_init = error_mark_node;
7501 else if (require_constant
7502 && !initializer_constant_valid_p (inside_init,
7503 TREE_TYPE (inside_init)))
7505 error_init (init_loc, "initializer element is not computable at "
7506 "load time");
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 return inside_init;
7516 /* Come here only for records and arrays. */
7518 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7520 error_init (init_loc, "variable-sized object may not be initialized");
7521 return error_mark_node;
7524 error_init (init_loc, "invalid initializer");
7525 return error_mark_node;
7528 /* Handle initializers that use braces. */
7530 /* Type of object we are accumulating a constructor for.
7531 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7532 static tree constructor_type;
7534 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7535 left to fill. */
7536 static tree constructor_fields;
7538 /* For an ARRAY_TYPE, this is the specified index
7539 at which to store the next element we get. */
7540 static tree constructor_index;
7542 /* For an ARRAY_TYPE, this is the maximum index. */
7543 static tree constructor_max_index;
7545 /* For a RECORD_TYPE, this is the first field not yet written out. */
7546 static tree constructor_unfilled_fields;
7548 /* For an ARRAY_TYPE, this is the index of the first element
7549 not yet written out. */
7550 static tree constructor_unfilled_index;
7552 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7553 This is so we can generate gaps between fields, when appropriate. */
7554 static tree constructor_bit_index;
7556 /* If we are saving up the elements rather than allocating them,
7557 this is the list of elements so far (in reverse order,
7558 most recent first). */
7559 static vec<constructor_elt, va_gc> *constructor_elements;
7561 /* 1 if constructor should be incrementally stored into a constructor chain,
7562 0 if all the elements should be kept in AVL tree. */
7563 static int constructor_incremental;
7565 /* 1 if so far this constructor's elements are all compile-time constants. */
7566 static int constructor_constant;
7568 /* 1 if so far this constructor's elements are all valid address constants. */
7569 static int constructor_simple;
7571 /* 1 if this constructor has an element that cannot be part of a
7572 constant expression. */
7573 static int constructor_nonconst;
7575 /* 1 if this constructor is erroneous so far. */
7576 static int constructor_erroneous;
7578 /* 1 if this constructor is the universal zero initializer { 0 }. */
7579 static int constructor_zeroinit;
7581 /* Structure for managing pending initializer elements, organized as an
7582 AVL tree. */
7584 struct init_node
7586 struct init_node *left, *right;
7587 struct init_node *parent;
7588 int balance;
7589 tree purpose;
7590 tree value;
7591 tree origtype;
7594 /* Tree of pending elements at this constructor level.
7595 These are elements encountered out of order
7596 which belong at places we haven't reached yet in actually
7597 writing the output.
7598 Will never hold tree nodes across GC runs. */
7599 static struct init_node *constructor_pending_elts;
7601 /* The SPELLING_DEPTH of this constructor. */
7602 static int constructor_depth;
7604 /* DECL node for which an initializer is being read.
7605 0 means we are reading a constructor expression
7606 such as (struct foo) {...}. */
7607 static tree constructor_decl;
7609 /* Nonzero if this is an initializer for a top-level decl. */
7610 static int constructor_top_level;
7612 /* Nonzero if there were any member designators in this initializer. */
7613 static int constructor_designated;
7615 /* Nesting depth of designator list. */
7616 static int designator_depth;
7618 /* Nonzero if there were diagnosed errors in this designator list. */
7619 static int designator_erroneous;
7622 /* This stack has a level for each implicit or explicit level of
7623 structuring in the initializer, including the outermost one. It
7624 saves the values of most of the variables above. */
7626 struct constructor_range_stack;
7628 struct constructor_stack
7630 struct constructor_stack *next;
7631 tree type;
7632 tree fields;
7633 tree index;
7634 tree max_index;
7635 tree unfilled_index;
7636 tree unfilled_fields;
7637 tree bit_index;
7638 vec<constructor_elt, va_gc> *elements;
7639 struct init_node *pending_elts;
7640 int offset;
7641 int depth;
7642 /* If value nonzero, this value should replace the entire
7643 constructor at this level. */
7644 struct c_expr replacement_value;
7645 struct constructor_range_stack *range_stack;
7646 char constant;
7647 char simple;
7648 char nonconst;
7649 char implicit;
7650 char erroneous;
7651 char outer;
7652 char incremental;
7653 char designated;
7654 int designator_depth;
7657 static struct constructor_stack *constructor_stack;
7659 /* This stack represents designators from some range designator up to
7660 the last designator in the list. */
7662 struct constructor_range_stack
7664 struct constructor_range_stack *next, *prev;
7665 struct constructor_stack *stack;
7666 tree range_start;
7667 tree index;
7668 tree range_end;
7669 tree fields;
7672 static struct constructor_range_stack *constructor_range_stack;
7674 /* This stack records separate initializers that are nested.
7675 Nested initializers can't happen in ANSI C, but GNU C allows them
7676 in cases like { ... (struct foo) { ... } ... }. */
7678 struct initializer_stack
7680 struct initializer_stack *next;
7681 tree decl;
7682 struct constructor_stack *constructor_stack;
7683 struct constructor_range_stack *constructor_range_stack;
7684 vec<constructor_elt, va_gc> *elements;
7685 struct spelling *spelling;
7686 struct spelling *spelling_base;
7687 int spelling_size;
7688 char top_level;
7689 char require_constant_value;
7690 char require_constant_elements;
7691 rich_location *missing_brace_richloc;
7694 static struct initializer_stack *initializer_stack;
7696 /* Prepare to parse and output the initializer for variable DECL. */
7698 void
7699 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7700 rich_location *richloc)
7702 const char *locus;
7703 struct initializer_stack *p = XNEW (struct initializer_stack);
7705 p->decl = constructor_decl;
7706 p->require_constant_value = require_constant_value;
7707 p->require_constant_elements = require_constant_elements;
7708 p->constructor_stack = constructor_stack;
7709 p->constructor_range_stack = constructor_range_stack;
7710 p->elements = constructor_elements;
7711 p->spelling = spelling;
7712 p->spelling_base = spelling_base;
7713 p->spelling_size = spelling_size;
7714 p->top_level = constructor_top_level;
7715 p->next = initializer_stack;
7716 p->missing_brace_richloc = richloc;
7717 initializer_stack = p;
7719 constructor_decl = decl;
7720 constructor_designated = 0;
7721 constructor_top_level = top_level;
7723 if (decl != NULL_TREE && decl != error_mark_node)
7725 require_constant_value = TREE_STATIC (decl);
7726 require_constant_elements
7727 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7728 /* For a scalar, you can always use any value to initialize,
7729 even within braces. */
7730 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7731 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7733 else
7735 require_constant_value = 0;
7736 require_constant_elements = 0;
7737 locus = _("(anonymous)");
7740 constructor_stack = 0;
7741 constructor_range_stack = 0;
7743 found_missing_braces = 0;
7745 spelling_base = 0;
7746 spelling_size = 0;
7747 RESTORE_SPELLING_DEPTH (0);
7749 if (locus)
7750 push_string (locus);
7753 void
7754 finish_init (void)
7756 struct initializer_stack *p = initializer_stack;
7758 /* Free the whole constructor stack of this initializer. */
7759 while (constructor_stack)
7761 struct constructor_stack *q = constructor_stack;
7762 constructor_stack = q->next;
7763 free (q);
7766 gcc_assert (!constructor_range_stack);
7768 /* Pop back to the data of the outer initializer (if any). */
7769 free (spelling_base);
7771 constructor_decl = p->decl;
7772 require_constant_value = p->require_constant_value;
7773 require_constant_elements = p->require_constant_elements;
7774 constructor_stack = p->constructor_stack;
7775 constructor_range_stack = p->constructor_range_stack;
7776 constructor_elements = p->elements;
7777 spelling = p->spelling;
7778 spelling_base = p->spelling_base;
7779 spelling_size = p->spelling_size;
7780 constructor_top_level = p->top_level;
7781 initializer_stack = p->next;
7782 free (p);
7785 /* Call here when we see the initializer is surrounded by braces.
7786 This is instead of a call to push_init_level;
7787 it is matched by a call to pop_init_level.
7789 TYPE is the type to initialize, for a constructor expression.
7790 For an initializer for a decl, TYPE is zero. */
7792 void
7793 really_start_incremental_init (tree type)
7795 struct constructor_stack *p = XNEW (struct constructor_stack);
7797 if (type == NULL_TREE)
7798 type = TREE_TYPE (constructor_decl);
7800 if (VECTOR_TYPE_P (type)
7801 && TYPE_VECTOR_OPAQUE (type))
7802 error ("opaque vector types cannot be initialized");
7804 p->type = constructor_type;
7805 p->fields = constructor_fields;
7806 p->index = constructor_index;
7807 p->max_index = constructor_max_index;
7808 p->unfilled_index = constructor_unfilled_index;
7809 p->unfilled_fields = constructor_unfilled_fields;
7810 p->bit_index = constructor_bit_index;
7811 p->elements = constructor_elements;
7812 p->constant = constructor_constant;
7813 p->simple = constructor_simple;
7814 p->nonconst = constructor_nonconst;
7815 p->erroneous = constructor_erroneous;
7816 p->pending_elts = constructor_pending_elts;
7817 p->depth = constructor_depth;
7818 p->replacement_value.value = 0;
7819 p->replacement_value.original_code = ERROR_MARK;
7820 p->replacement_value.original_type = NULL;
7821 p->implicit = 0;
7822 p->range_stack = 0;
7823 p->outer = 0;
7824 p->incremental = constructor_incremental;
7825 p->designated = constructor_designated;
7826 p->designator_depth = designator_depth;
7827 p->next = 0;
7828 constructor_stack = p;
7830 constructor_constant = 1;
7831 constructor_simple = 1;
7832 constructor_nonconst = 0;
7833 constructor_depth = SPELLING_DEPTH ();
7834 constructor_elements = NULL;
7835 constructor_pending_elts = 0;
7836 constructor_type = type;
7837 constructor_incremental = 1;
7838 constructor_designated = 0;
7839 constructor_zeroinit = 1;
7840 designator_depth = 0;
7841 designator_erroneous = 0;
7843 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7845 constructor_fields = TYPE_FIELDS (constructor_type);
7846 /* Skip any nameless bit fields at the beginning. */
7847 while (constructor_fields != NULL_TREE
7848 && DECL_C_BIT_FIELD (constructor_fields)
7849 && DECL_NAME (constructor_fields) == NULL_TREE)
7850 constructor_fields = DECL_CHAIN (constructor_fields);
7852 constructor_unfilled_fields = constructor_fields;
7853 constructor_bit_index = bitsize_zero_node;
7855 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7857 if (TYPE_DOMAIN (constructor_type))
7859 constructor_max_index
7860 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7862 /* Detect non-empty initializations of zero-length arrays. */
7863 if (constructor_max_index == NULL_TREE
7864 && TYPE_SIZE (constructor_type))
7865 constructor_max_index = integer_minus_one_node;
7867 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7868 to initialize VLAs will cause a proper error; avoid tree
7869 checking errors as well by setting a safe value. */
7870 if (constructor_max_index
7871 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7872 constructor_max_index = integer_minus_one_node;
7874 constructor_index
7875 = convert (bitsizetype,
7876 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7878 else
7880 constructor_index = bitsize_zero_node;
7881 constructor_max_index = NULL_TREE;
7884 constructor_unfilled_index = constructor_index;
7886 else if (VECTOR_TYPE_P (constructor_type))
7888 /* Vectors are like simple fixed-size arrays. */
7889 constructor_max_index =
7890 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7891 constructor_index = bitsize_zero_node;
7892 constructor_unfilled_index = constructor_index;
7894 else
7896 /* Handle the case of int x = {5}; */
7897 constructor_fields = constructor_type;
7898 constructor_unfilled_fields = constructor_type;
7902 extern location_t last_init_list_comma;
7904 /* Called when we see an open brace for a nested initializer. Finish
7905 off any pending levels with implicit braces. */
7906 void
7907 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7909 while (constructor_stack->implicit)
7911 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7912 && constructor_fields == NULL_TREE)
7913 process_init_element (input_location,
7914 pop_init_level (loc, 1, braced_init_obstack,
7915 last_init_list_comma),
7916 true, braced_init_obstack);
7917 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7918 && constructor_max_index
7919 && tree_int_cst_lt (constructor_max_index,
7920 constructor_index))
7921 process_init_element (input_location,
7922 pop_init_level (loc, 1, braced_init_obstack,
7923 last_init_list_comma),
7924 true, braced_init_obstack);
7925 else
7926 break;
7930 /* Push down into a subobject, for initialization.
7931 If this is for an explicit set of braces, IMPLICIT is 0.
7932 If it is because the next element belongs at a lower level,
7933 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7935 void
7936 push_init_level (location_t loc, int implicit,
7937 struct obstack *braced_init_obstack)
7939 struct constructor_stack *p;
7940 tree value = NULL_TREE;
7942 /* Unless this is an explicit brace, we need to preserve previous
7943 content if any. */
7944 if (implicit)
7946 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7947 value = find_init_member (constructor_fields, braced_init_obstack);
7948 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7949 value = find_init_member (constructor_index, braced_init_obstack);
7952 p = XNEW (struct constructor_stack);
7953 p->type = constructor_type;
7954 p->fields = constructor_fields;
7955 p->index = constructor_index;
7956 p->max_index = constructor_max_index;
7957 p->unfilled_index = constructor_unfilled_index;
7958 p->unfilled_fields = constructor_unfilled_fields;
7959 p->bit_index = constructor_bit_index;
7960 p->elements = constructor_elements;
7961 p->constant = constructor_constant;
7962 p->simple = constructor_simple;
7963 p->nonconst = constructor_nonconst;
7964 p->erroneous = constructor_erroneous;
7965 p->pending_elts = constructor_pending_elts;
7966 p->depth = constructor_depth;
7967 p->replacement_value.value = NULL_TREE;
7968 p->replacement_value.original_code = ERROR_MARK;
7969 p->replacement_value.original_type = NULL;
7970 p->implicit = implicit;
7971 p->outer = 0;
7972 p->incremental = constructor_incremental;
7973 p->designated = constructor_designated;
7974 p->designator_depth = designator_depth;
7975 p->next = constructor_stack;
7976 p->range_stack = 0;
7977 constructor_stack = p;
7979 constructor_constant = 1;
7980 constructor_simple = 1;
7981 constructor_nonconst = 0;
7982 constructor_depth = SPELLING_DEPTH ();
7983 constructor_elements = NULL;
7984 constructor_incremental = 1;
7985 constructor_designated = 0;
7986 constructor_pending_elts = 0;
7987 if (!implicit)
7989 p->range_stack = constructor_range_stack;
7990 constructor_range_stack = 0;
7991 designator_depth = 0;
7992 designator_erroneous = 0;
7995 /* Don't die if an entire brace-pair level is superfluous
7996 in the containing level. */
7997 if (constructor_type == NULL_TREE)
7999 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8001 /* Don't die if there are extra init elts at the end. */
8002 if (constructor_fields == NULL_TREE)
8003 constructor_type = NULL_TREE;
8004 else
8006 constructor_type = TREE_TYPE (constructor_fields);
8007 push_member_name (constructor_fields);
8008 constructor_depth++;
8010 /* If upper initializer is designated, then mark this as
8011 designated too to prevent bogus warnings. */
8012 constructor_designated = p->designated;
8014 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8016 constructor_type = TREE_TYPE (constructor_type);
8017 push_array_bounds (tree_to_uhwi (constructor_index));
8018 constructor_depth++;
8021 if (constructor_type == NULL_TREE)
8023 error_init (loc, "extra brace group at end of initializer");
8024 constructor_fields = NULL_TREE;
8025 constructor_unfilled_fields = NULL_TREE;
8026 return;
8029 if (value && TREE_CODE (value) == CONSTRUCTOR)
8031 constructor_constant = TREE_CONSTANT (value);
8032 constructor_simple = TREE_STATIC (value);
8033 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8034 constructor_elements = CONSTRUCTOR_ELTS (value);
8035 if (!vec_safe_is_empty (constructor_elements)
8036 && (TREE_CODE (constructor_type) == RECORD_TYPE
8037 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8038 set_nonincremental_init (braced_init_obstack);
8041 if (implicit == 1)
8043 found_missing_braces = 1;
8044 if (initializer_stack->missing_brace_richloc)
8045 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8046 (loc, "{");
8049 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8051 constructor_fields = TYPE_FIELDS (constructor_type);
8052 /* Skip any nameless bit fields at the beginning. */
8053 while (constructor_fields != NULL_TREE
8054 && DECL_C_BIT_FIELD (constructor_fields)
8055 && DECL_NAME (constructor_fields) == NULL_TREE)
8056 constructor_fields = DECL_CHAIN (constructor_fields);
8058 constructor_unfilled_fields = constructor_fields;
8059 constructor_bit_index = bitsize_zero_node;
8061 else if (VECTOR_TYPE_P (constructor_type))
8063 /* Vectors are like simple fixed-size arrays. */
8064 constructor_max_index =
8065 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8066 constructor_index = bitsize_int (0);
8067 constructor_unfilled_index = constructor_index;
8069 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8071 if (TYPE_DOMAIN (constructor_type))
8073 constructor_max_index
8074 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8076 /* Detect non-empty initializations of zero-length arrays. */
8077 if (constructor_max_index == NULL_TREE
8078 && TYPE_SIZE (constructor_type))
8079 constructor_max_index = integer_minus_one_node;
8081 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8082 to initialize VLAs will cause a proper error; avoid tree
8083 checking errors as well by setting a safe value. */
8084 if (constructor_max_index
8085 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8086 constructor_max_index = integer_minus_one_node;
8088 constructor_index
8089 = convert (bitsizetype,
8090 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8092 else
8093 constructor_index = bitsize_zero_node;
8095 constructor_unfilled_index = constructor_index;
8096 if (value && TREE_CODE (value) == STRING_CST)
8098 /* We need to split the char/wchar array into individual
8099 characters, so that we don't have to special case it
8100 everywhere. */
8101 set_nonincremental_init_from_string (value, braced_init_obstack);
8104 else
8106 if (constructor_type != error_mark_node)
8107 warning_init (input_location, 0, "braces around scalar initializer");
8108 constructor_fields = constructor_type;
8109 constructor_unfilled_fields = constructor_type;
8113 /* At the end of an implicit or explicit brace level,
8114 finish up that level of constructor. If a single expression
8115 with redundant braces initialized that level, return the
8116 c_expr structure for that expression. Otherwise, the original_code
8117 element is set to ERROR_MARK.
8118 If we were outputting the elements as they are read, return 0 as the value
8119 from inner levels (process_init_element ignores that),
8120 but return error_mark_node as the value from the outermost level
8121 (that's what we want to put in DECL_INITIAL).
8122 Otherwise, return a CONSTRUCTOR expression as the value. */
8124 struct c_expr
8125 pop_init_level (location_t loc, int implicit,
8126 struct obstack *braced_init_obstack,
8127 location_t insert_before)
8129 struct constructor_stack *p;
8130 struct c_expr ret;
8131 ret.value = NULL_TREE;
8132 ret.original_code = ERROR_MARK;
8133 ret.original_type = NULL;
8135 if (implicit == 0)
8137 /* When we come to an explicit close brace,
8138 pop any inner levels that didn't have explicit braces. */
8139 while (constructor_stack->implicit)
8140 process_init_element (input_location,
8141 pop_init_level (loc, 1, braced_init_obstack,
8142 insert_before),
8143 true, braced_init_obstack);
8144 gcc_assert (!constructor_range_stack);
8146 else
8147 if (initializer_stack->missing_brace_richloc)
8148 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8149 (insert_before, "}");
8151 /* Now output all pending elements. */
8152 constructor_incremental = 1;
8153 output_pending_init_elements (1, braced_init_obstack);
8155 p = constructor_stack;
8157 /* Error for initializing a flexible array member, or a zero-length
8158 array member in an inappropriate context. */
8159 if (constructor_type && constructor_fields
8160 && TREE_CODE (constructor_type) == ARRAY_TYPE
8161 && TYPE_DOMAIN (constructor_type)
8162 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8164 /* Silently discard empty initializations. The parser will
8165 already have pedwarned for empty brackets. */
8166 if (integer_zerop (constructor_unfilled_index))
8167 constructor_type = NULL_TREE;
8168 else
8170 gcc_assert (!TYPE_SIZE (constructor_type));
8172 if (constructor_depth > 2)
8173 error_init (loc, "initialization of flexible array member in a nested context");
8174 else
8175 pedwarn_init (loc, OPT_Wpedantic,
8176 "initialization of a flexible array member");
8178 /* We have already issued an error message for the existence
8179 of a flexible array member not at the end of the structure.
8180 Discard the initializer so that we do not die later. */
8181 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8182 constructor_type = NULL_TREE;
8186 switch (vec_safe_length (constructor_elements))
8188 case 0:
8189 /* Initialization with { } counts as zeroinit. */
8190 constructor_zeroinit = 1;
8191 break;
8192 case 1:
8193 /* This might be zeroinit as well. */
8194 if (integer_zerop ((*constructor_elements)[0].value))
8195 constructor_zeroinit = 1;
8196 break;
8197 default:
8198 /* If the constructor has more than one element, it can't be { 0 }. */
8199 constructor_zeroinit = 0;
8200 break;
8203 /* Warn when some structs are initialized with direct aggregation. */
8204 if (!implicit && found_missing_braces && warn_missing_braces
8205 && !constructor_zeroinit)
8207 gcc_assert (initializer_stack->missing_brace_richloc);
8208 warning_at_rich_loc (initializer_stack->missing_brace_richloc,
8209 OPT_Wmissing_braces,
8210 "missing braces around initializer");
8213 /* Warn when some struct elements are implicitly initialized to zero. */
8214 if (warn_missing_field_initializers
8215 && constructor_type
8216 && TREE_CODE (constructor_type) == RECORD_TYPE
8217 && constructor_unfilled_fields)
8219 /* Do not warn for flexible array members or zero-length arrays. */
8220 while (constructor_unfilled_fields
8221 && (!DECL_SIZE (constructor_unfilled_fields)
8222 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8223 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8225 if (constructor_unfilled_fields
8226 /* Do not warn if this level of the initializer uses member
8227 designators; it is likely to be deliberate. */
8228 && !constructor_designated
8229 /* Do not warn about initializing with { 0 } or with { }. */
8230 && !constructor_zeroinit)
8232 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8233 "missing initializer for field %qD of %qT",
8234 constructor_unfilled_fields,
8235 constructor_type))
8236 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8237 "%qD declared here", constructor_unfilled_fields);
8241 /* Pad out the end of the structure. */
8242 if (p->replacement_value.value)
8243 /* If this closes a superfluous brace pair,
8244 just pass out the element between them. */
8245 ret = p->replacement_value;
8246 else if (constructor_type == NULL_TREE)
8248 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8249 && TREE_CODE (constructor_type) != ARRAY_TYPE
8250 && !VECTOR_TYPE_P (constructor_type))
8252 /* A nonincremental scalar initializer--just return
8253 the element, after verifying there is just one. */
8254 if (vec_safe_is_empty (constructor_elements))
8256 if (!constructor_erroneous)
8257 error_init (loc, "empty scalar initializer");
8258 ret.value = error_mark_node;
8260 else if (vec_safe_length (constructor_elements) != 1)
8262 error_init (loc, "extra elements in scalar initializer");
8263 ret.value = (*constructor_elements)[0].value;
8265 else
8266 ret.value = (*constructor_elements)[0].value;
8268 else
8270 if (constructor_erroneous)
8271 ret.value = error_mark_node;
8272 else
8274 ret.value = build_constructor (constructor_type,
8275 constructor_elements);
8276 if (constructor_constant)
8277 TREE_CONSTANT (ret.value) = 1;
8278 if (constructor_constant && constructor_simple)
8279 TREE_STATIC (ret.value) = 1;
8280 if (constructor_nonconst)
8281 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8285 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8287 if (constructor_nonconst)
8288 ret.original_code = C_MAYBE_CONST_EXPR;
8289 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8290 ret.original_code = ERROR_MARK;
8293 constructor_type = p->type;
8294 constructor_fields = p->fields;
8295 constructor_index = p->index;
8296 constructor_max_index = p->max_index;
8297 constructor_unfilled_index = p->unfilled_index;
8298 constructor_unfilled_fields = p->unfilled_fields;
8299 constructor_bit_index = p->bit_index;
8300 constructor_elements = p->elements;
8301 constructor_constant = p->constant;
8302 constructor_simple = p->simple;
8303 constructor_nonconst = p->nonconst;
8304 constructor_erroneous = p->erroneous;
8305 constructor_incremental = p->incremental;
8306 constructor_designated = p->designated;
8307 designator_depth = p->designator_depth;
8308 constructor_pending_elts = p->pending_elts;
8309 constructor_depth = p->depth;
8310 if (!p->implicit)
8311 constructor_range_stack = p->range_stack;
8312 RESTORE_SPELLING_DEPTH (constructor_depth);
8314 constructor_stack = p->next;
8315 free (p);
8317 if (ret.value == NULL_TREE && constructor_stack == 0)
8318 ret.value = error_mark_node;
8319 return ret;
8322 /* Common handling for both array range and field name designators.
8323 ARRAY argument is nonzero for array ranges. Returns false for success. */
8325 static bool
8326 set_designator (location_t loc, bool array,
8327 struct obstack *braced_init_obstack)
8329 tree subtype;
8330 enum tree_code subcode;
8332 /* Don't die if an entire brace-pair level is superfluous
8333 in the containing level. */
8334 if (constructor_type == NULL_TREE)
8335 return true;
8337 /* If there were errors in this designator list already, bail out
8338 silently. */
8339 if (designator_erroneous)
8340 return true;
8342 if (!designator_depth)
8344 gcc_assert (!constructor_range_stack);
8346 /* Designator list starts at the level of closest explicit
8347 braces. */
8348 while (constructor_stack->implicit)
8349 process_init_element (input_location,
8350 pop_init_level (loc, 1, braced_init_obstack,
8351 last_init_list_comma),
8352 true, braced_init_obstack);
8353 constructor_designated = 1;
8354 return false;
8357 switch (TREE_CODE (constructor_type))
8359 case RECORD_TYPE:
8360 case UNION_TYPE:
8361 subtype = TREE_TYPE (constructor_fields);
8362 if (subtype != error_mark_node)
8363 subtype = TYPE_MAIN_VARIANT (subtype);
8364 break;
8365 case ARRAY_TYPE:
8366 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8367 break;
8368 default:
8369 gcc_unreachable ();
8372 subcode = TREE_CODE (subtype);
8373 if (array && subcode != ARRAY_TYPE)
8375 error_init (loc, "array index in non-array initializer");
8376 return true;
8378 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8380 error_init (loc, "field name not in record or union initializer");
8381 return true;
8384 constructor_designated = 1;
8385 finish_implicit_inits (loc, braced_init_obstack);
8386 push_init_level (loc, 2, braced_init_obstack);
8387 return false;
8390 /* If there are range designators in designator list, push a new designator
8391 to constructor_range_stack. RANGE_END is end of such stack range or
8392 NULL_TREE if there is no range designator at this level. */
8394 static void
8395 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8397 struct constructor_range_stack *p;
8399 p = (struct constructor_range_stack *)
8400 obstack_alloc (braced_init_obstack,
8401 sizeof (struct constructor_range_stack));
8402 p->prev = constructor_range_stack;
8403 p->next = 0;
8404 p->fields = constructor_fields;
8405 p->range_start = constructor_index;
8406 p->index = constructor_index;
8407 p->stack = constructor_stack;
8408 p->range_end = range_end;
8409 if (constructor_range_stack)
8410 constructor_range_stack->next = p;
8411 constructor_range_stack = p;
8414 /* Within an array initializer, specify the next index to be initialized.
8415 FIRST is that index. If LAST is nonzero, then initialize a range
8416 of indices, running from FIRST through LAST. */
8418 void
8419 set_init_index (location_t loc, tree first, tree last,
8420 struct obstack *braced_init_obstack)
8422 if (set_designator (loc, true, braced_init_obstack))
8423 return;
8425 designator_erroneous = 1;
8427 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8428 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8430 error_init (loc, "array index in initializer not of integer type");
8431 return;
8434 if (TREE_CODE (first) != INTEGER_CST)
8436 first = c_fully_fold (first, false, NULL);
8437 if (TREE_CODE (first) == INTEGER_CST)
8438 pedwarn_init (loc, OPT_Wpedantic,
8439 "array index in initializer is not "
8440 "an integer constant expression");
8443 if (last && TREE_CODE (last) != INTEGER_CST)
8445 last = c_fully_fold (last, false, NULL);
8446 if (TREE_CODE (last) == INTEGER_CST)
8447 pedwarn_init (loc, OPT_Wpedantic,
8448 "array index in initializer is not "
8449 "an integer constant expression");
8452 if (TREE_CODE (first) != INTEGER_CST)
8453 error_init (loc, "nonconstant array index in initializer");
8454 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8455 error_init (loc, "nonconstant array index in initializer");
8456 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8457 error_init (loc, "array index in non-array initializer");
8458 else if (tree_int_cst_sgn (first) == -1)
8459 error_init (loc, "array index in initializer exceeds array bounds");
8460 else if (constructor_max_index
8461 && tree_int_cst_lt (constructor_max_index, first))
8462 error_init (loc, "array index in initializer exceeds array bounds");
8463 else
8465 constant_expression_warning (first);
8466 if (last)
8467 constant_expression_warning (last);
8468 constructor_index = convert (bitsizetype, first);
8469 if (tree_int_cst_lt (constructor_index, first))
8471 constructor_index = copy_node (constructor_index);
8472 TREE_OVERFLOW (constructor_index) = 1;
8475 if (last)
8477 if (tree_int_cst_equal (first, last))
8478 last = NULL_TREE;
8479 else if (tree_int_cst_lt (last, first))
8481 error_init (loc, "empty index range in initializer");
8482 last = NULL_TREE;
8484 else
8486 last = convert (bitsizetype, last);
8487 if (constructor_max_index != NULL_TREE
8488 && tree_int_cst_lt (constructor_max_index, last))
8490 error_init (loc, "array index range in initializer exceeds "
8491 "array bounds");
8492 last = NULL_TREE;
8497 designator_depth++;
8498 designator_erroneous = 0;
8499 if (constructor_range_stack || last)
8500 push_range_stack (last, braced_init_obstack);
8504 /* Within a struct initializer, specify the next field to be initialized. */
8506 void
8507 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8508 struct obstack *braced_init_obstack)
8510 tree field;
8512 if (set_designator (loc, false, braced_init_obstack))
8513 return;
8515 designator_erroneous = 1;
8517 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8519 error_init (loc, "field name not in record or union initializer");
8520 return;
8523 field = lookup_field (constructor_type, fieldname);
8525 if (field == NULL_TREE)
8527 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8528 if (guessed_id)
8530 gcc_rich_location rich_loc (fieldname_loc);
8531 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8532 error_at_rich_loc
8533 (&rich_loc,
8534 "%qT has no member named %qE; did you mean %qE?",
8535 constructor_type, fieldname, guessed_id);
8537 else
8538 error_at (fieldname_loc, "%qT has no member named %qE",
8539 constructor_type, fieldname);
8541 else
8544 constructor_fields = TREE_VALUE (field);
8545 designator_depth++;
8546 designator_erroneous = 0;
8547 if (constructor_range_stack)
8548 push_range_stack (NULL_TREE, braced_init_obstack);
8549 field = TREE_CHAIN (field);
8550 if (field)
8552 if (set_designator (loc, false, braced_init_obstack))
8553 return;
8556 while (field != NULL_TREE);
8559 /* Add a new initializer to the tree of pending initializers. PURPOSE
8560 identifies the initializer, either array index or field in a structure.
8561 VALUE is the value of that index or field. If ORIGTYPE is not
8562 NULL_TREE, it is the original type of VALUE.
8564 IMPLICIT is true if value comes from pop_init_level (1),
8565 the new initializer has been merged with the existing one
8566 and thus no warnings should be emitted about overriding an
8567 existing initializer. */
8569 static void
8570 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8571 bool implicit, struct obstack *braced_init_obstack)
8573 struct init_node *p, **q, *r;
8575 q = &constructor_pending_elts;
8576 p = 0;
8578 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8580 while (*q != 0)
8582 p = *q;
8583 if (tree_int_cst_lt (purpose, p->purpose))
8584 q = &p->left;
8585 else if (tree_int_cst_lt (p->purpose, purpose))
8586 q = &p->right;
8587 else
8589 if (!implicit)
8591 if (TREE_SIDE_EFFECTS (p->value))
8592 warning_init (loc, OPT_Woverride_init_side_effects,
8593 "initialized field with side-effects "
8594 "overwritten");
8595 else if (warn_override_init)
8596 warning_init (loc, OPT_Woverride_init,
8597 "initialized field overwritten");
8599 p->value = value;
8600 p->origtype = origtype;
8601 return;
8605 else
8607 tree bitpos;
8609 bitpos = bit_position (purpose);
8610 while (*q != NULL)
8612 p = *q;
8613 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8614 q = &p->left;
8615 else if (p->purpose != purpose)
8616 q = &p->right;
8617 else
8619 if (!implicit)
8621 if (TREE_SIDE_EFFECTS (p->value))
8622 warning_init (loc, OPT_Woverride_init_side_effects,
8623 "initialized field with side-effects "
8624 "overwritten");
8625 else if (warn_override_init)
8626 warning_init (loc, OPT_Woverride_init,
8627 "initialized field overwritten");
8629 p->value = value;
8630 p->origtype = origtype;
8631 return;
8636 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8637 sizeof (struct init_node));
8638 r->purpose = purpose;
8639 r->value = value;
8640 r->origtype = origtype;
8642 *q = r;
8643 r->parent = p;
8644 r->left = 0;
8645 r->right = 0;
8646 r->balance = 0;
8648 while (p)
8650 struct init_node *s;
8652 if (r == p->left)
8654 if (p->balance == 0)
8655 p->balance = -1;
8656 else if (p->balance < 0)
8658 if (r->balance < 0)
8660 /* L rotation. */
8661 p->left = r->right;
8662 if (p->left)
8663 p->left->parent = p;
8664 r->right = p;
8666 p->balance = 0;
8667 r->balance = 0;
8669 s = p->parent;
8670 p->parent = r;
8671 r->parent = s;
8672 if (s)
8674 if (s->left == p)
8675 s->left = r;
8676 else
8677 s->right = r;
8679 else
8680 constructor_pending_elts = r;
8682 else
8684 /* LR rotation. */
8685 struct init_node *t = r->right;
8687 r->right = t->left;
8688 if (r->right)
8689 r->right->parent = r;
8690 t->left = r;
8692 p->left = t->right;
8693 if (p->left)
8694 p->left->parent = p;
8695 t->right = p;
8697 p->balance = t->balance < 0;
8698 r->balance = -(t->balance > 0);
8699 t->balance = 0;
8701 s = p->parent;
8702 p->parent = t;
8703 r->parent = t;
8704 t->parent = s;
8705 if (s)
8707 if (s->left == p)
8708 s->left = t;
8709 else
8710 s->right = t;
8712 else
8713 constructor_pending_elts = t;
8715 break;
8717 else
8719 /* p->balance == +1; growth of left side balances the node. */
8720 p->balance = 0;
8721 break;
8724 else /* r == p->right */
8726 if (p->balance == 0)
8727 /* Growth propagation from right side. */
8728 p->balance++;
8729 else if (p->balance > 0)
8731 if (r->balance > 0)
8733 /* R rotation. */
8734 p->right = r->left;
8735 if (p->right)
8736 p->right->parent = p;
8737 r->left = p;
8739 p->balance = 0;
8740 r->balance = 0;
8742 s = p->parent;
8743 p->parent = r;
8744 r->parent = s;
8745 if (s)
8747 if (s->left == p)
8748 s->left = r;
8749 else
8750 s->right = r;
8752 else
8753 constructor_pending_elts = r;
8755 else /* r->balance == -1 */
8757 /* RL rotation */
8758 struct init_node *t = r->left;
8760 r->left = t->right;
8761 if (r->left)
8762 r->left->parent = r;
8763 t->right = r;
8765 p->right = t->left;
8766 if (p->right)
8767 p->right->parent = p;
8768 t->left = p;
8770 r->balance = (t->balance < 0);
8771 p->balance = -(t->balance > 0);
8772 t->balance = 0;
8774 s = p->parent;
8775 p->parent = t;
8776 r->parent = t;
8777 t->parent = s;
8778 if (s)
8780 if (s->left == p)
8781 s->left = t;
8782 else
8783 s->right = t;
8785 else
8786 constructor_pending_elts = t;
8788 break;
8790 else
8792 /* p->balance == -1; growth of right side balances the node. */
8793 p->balance = 0;
8794 break;
8798 r = p;
8799 p = p->parent;
8803 /* Build AVL tree from a sorted chain. */
8805 static void
8806 set_nonincremental_init (struct obstack * braced_init_obstack)
8808 unsigned HOST_WIDE_INT ix;
8809 tree index, value;
8811 if (TREE_CODE (constructor_type) != RECORD_TYPE
8812 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8813 return;
8815 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8816 add_pending_init (input_location, index, value, NULL_TREE, true,
8817 braced_init_obstack);
8818 constructor_elements = NULL;
8819 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8821 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8822 /* Skip any nameless bit fields at the beginning. */
8823 while (constructor_unfilled_fields != NULL_TREE
8824 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8825 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
8826 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8829 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8831 if (TYPE_DOMAIN (constructor_type))
8832 constructor_unfilled_index
8833 = convert (bitsizetype,
8834 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8835 else
8836 constructor_unfilled_index = bitsize_zero_node;
8838 constructor_incremental = 0;
8841 /* Build AVL tree from a string constant. */
8843 static void
8844 set_nonincremental_init_from_string (tree str,
8845 struct obstack * braced_init_obstack)
8847 tree value, purpose, type;
8848 HOST_WIDE_INT val[2];
8849 const char *p, *end;
8850 int byte, wchar_bytes, charwidth, bitpos;
8852 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8854 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8855 charwidth = TYPE_PRECISION (char_type_node);
8856 gcc_assert ((size_t) wchar_bytes * charwidth
8857 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8858 type = TREE_TYPE (constructor_type);
8859 p = TREE_STRING_POINTER (str);
8860 end = p + TREE_STRING_LENGTH (str);
8862 for (purpose = bitsize_zero_node;
8863 p < end
8864 && !(constructor_max_index
8865 && tree_int_cst_lt (constructor_max_index, purpose));
8866 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8868 if (wchar_bytes == 1)
8870 val[0] = (unsigned char) *p++;
8871 val[1] = 0;
8873 else
8875 val[1] = 0;
8876 val[0] = 0;
8877 for (byte = 0; byte < wchar_bytes; byte++)
8879 if (BYTES_BIG_ENDIAN)
8880 bitpos = (wchar_bytes - byte - 1) * charwidth;
8881 else
8882 bitpos = byte * charwidth;
8883 val[bitpos / HOST_BITS_PER_WIDE_INT]
8884 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8885 << (bitpos % HOST_BITS_PER_WIDE_INT);
8889 if (!TYPE_UNSIGNED (type))
8891 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8892 if (bitpos < HOST_BITS_PER_WIDE_INT)
8894 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8896 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8897 val[1] = -1;
8900 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8902 if (val[0] < 0)
8903 val[1] = -1;
8905 else if (val[1] & (HOST_WIDE_INT_1
8906 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8907 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8910 value = wide_int_to_tree (type,
8911 wide_int::from_array (val, 2,
8912 HOST_BITS_PER_WIDE_INT * 2));
8913 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8914 braced_init_obstack);
8917 constructor_incremental = 0;
8920 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
8921 not initialized yet. */
8923 static tree
8924 find_init_member (tree field, struct obstack * braced_init_obstack)
8926 struct init_node *p;
8928 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8930 if (constructor_incremental
8931 && tree_int_cst_lt (field, constructor_unfilled_index))
8932 set_nonincremental_init (braced_init_obstack);
8934 p = constructor_pending_elts;
8935 while (p)
8937 if (tree_int_cst_lt (field, p->purpose))
8938 p = p->left;
8939 else if (tree_int_cst_lt (p->purpose, field))
8940 p = p->right;
8941 else
8942 return p->value;
8945 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8947 tree bitpos = bit_position (field);
8949 if (constructor_incremental
8950 && (!constructor_unfilled_fields
8951 || tree_int_cst_lt (bitpos,
8952 bit_position (constructor_unfilled_fields))))
8953 set_nonincremental_init (braced_init_obstack);
8955 p = constructor_pending_elts;
8956 while (p)
8958 if (field == p->purpose)
8959 return p->value;
8960 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8961 p = p->left;
8962 else
8963 p = p->right;
8966 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8968 if (!vec_safe_is_empty (constructor_elements)
8969 && (constructor_elements->last ().index == field))
8970 return constructor_elements->last ().value;
8972 return NULL_TREE;
8975 /* "Output" the next constructor element.
8976 At top level, really output it to assembler code now.
8977 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8978 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8979 TYPE is the data type that the containing data type wants here.
8980 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8981 If VALUE is a string constant, STRICT_STRING is true if it is
8982 unparenthesized or we should not warn here for it being parenthesized.
8983 For other types of VALUE, STRICT_STRING is not used.
8985 PENDING if true means output pending elements that belong
8986 right after this element. (PENDING is normally true;
8987 it is false while outputting pending elements, to avoid recursion.)
8989 IMPLICIT is true if value comes from pop_init_level (1),
8990 the new initializer has been merged with the existing one
8991 and thus no warnings should be emitted about overriding an
8992 existing initializer. */
8994 static void
8995 output_init_element (location_t loc, tree value, tree origtype,
8996 bool strict_string, tree type, tree field, bool pending,
8997 bool implicit, struct obstack * braced_init_obstack)
8999 tree semantic_type = NULL_TREE;
9000 bool maybe_const = true;
9001 bool npc;
9003 if (type == error_mark_node || value == error_mark_node)
9005 constructor_erroneous = 1;
9006 return;
9008 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9009 && (TREE_CODE (value) == STRING_CST
9010 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9011 && !(TREE_CODE (value) == STRING_CST
9012 && TREE_CODE (type) == ARRAY_TYPE
9013 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9014 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9015 TYPE_MAIN_VARIANT (type)))
9016 value = array_to_pointer_conversion (input_location, value);
9018 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9019 && require_constant_value && pending)
9021 /* As an extension, allow initializing objects with static storage
9022 duration with compound literals (which are then treated just as
9023 the brace enclosed list they contain). */
9024 if (flag_isoc99)
9025 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9026 "constant");
9027 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9028 value = DECL_INITIAL (decl);
9031 npc = null_pointer_constant_p (value);
9032 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9034 semantic_type = TREE_TYPE (value);
9035 value = TREE_OPERAND (value, 0);
9037 value = c_fully_fold (value, require_constant_value, &maybe_const);
9039 if (value == error_mark_node)
9040 constructor_erroneous = 1;
9041 else if (!TREE_CONSTANT (value))
9042 constructor_constant = 0;
9043 else if (!initializer_constant_valid_p (value,
9044 TREE_TYPE (value),
9045 AGGREGATE_TYPE_P (constructor_type)
9046 && TYPE_REVERSE_STORAGE_ORDER
9047 (constructor_type))
9048 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9049 && DECL_C_BIT_FIELD (field)
9050 && TREE_CODE (value) != INTEGER_CST))
9051 constructor_simple = 0;
9052 if (!maybe_const)
9053 constructor_nonconst = 1;
9055 /* Digest the initializer and issue any errors about incompatible
9056 types before issuing errors about non-constant initializers. */
9057 tree new_value = value;
9058 if (semantic_type)
9059 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9060 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9061 require_constant_value);
9062 if (new_value == error_mark_node)
9064 constructor_erroneous = 1;
9065 return;
9067 if (require_constant_value || require_constant_elements)
9068 constant_expression_warning (new_value);
9070 /* Proceed to check the constness of the original initializer. */
9071 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9073 if (require_constant_value)
9075 error_init (loc, "initializer element is not constant");
9076 value = error_mark_node;
9078 else if (require_constant_elements)
9079 pedwarn (loc, OPT_Wpedantic,
9080 "initializer element is not computable at load time");
9082 else if (!maybe_const
9083 && (require_constant_value || require_constant_elements))
9084 pedwarn_init (loc, OPT_Wpedantic,
9085 "initializer element is not a constant expression");
9087 /* Issue -Wc++-compat warnings about initializing a bitfield with
9088 enum type. */
9089 if (warn_cxx_compat
9090 && field != NULL_TREE
9091 && TREE_CODE (field) == FIELD_DECL
9092 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9093 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9094 != TYPE_MAIN_VARIANT (type))
9095 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9097 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9098 if (checktype != error_mark_node
9099 && (TYPE_MAIN_VARIANT (checktype)
9100 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9101 warning_init (loc, OPT_Wc___compat,
9102 "enum conversion in initialization is invalid in C++");
9105 /* If this field is empty (and not at the end of structure),
9106 don't do anything other than checking the initializer. */
9107 if (field
9108 && (TREE_TYPE (field) == error_mark_node
9109 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9110 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9111 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9112 || DECL_CHAIN (field)))))
9113 return;
9115 /* Finally, set VALUE to the initializer value digested above. */
9116 value = new_value;
9118 /* If this element doesn't come next in sequence,
9119 put it on constructor_pending_elts. */
9120 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9121 && (!constructor_incremental
9122 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9124 if (constructor_incremental
9125 && tree_int_cst_lt (field, constructor_unfilled_index))
9126 set_nonincremental_init (braced_init_obstack);
9128 add_pending_init (loc, field, value, origtype, implicit,
9129 braced_init_obstack);
9130 return;
9132 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9133 && (!constructor_incremental
9134 || field != constructor_unfilled_fields))
9136 /* We do this for records but not for unions. In a union,
9137 no matter which field is specified, it can be initialized
9138 right away since it starts at the beginning of the union. */
9139 if (constructor_incremental)
9141 if (!constructor_unfilled_fields)
9142 set_nonincremental_init (braced_init_obstack);
9143 else
9145 tree bitpos, unfillpos;
9147 bitpos = bit_position (field);
9148 unfillpos = bit_position (constructor_unfilled_fields);
9150 if (tree_int_cst_lt (bitpos, unfillpos))
9151 set_nonincremental_init (braced_init_obstack);
9155 add_pending_init (loc, field, value, origtype, implicit,
9156 braced_init_obstack);
9157 return;
9159 else if (TREE_CODE (constructor_type) == UNION_TYPE
9160 && !vec_safe_is_empty (constructor_elements))
9162 if (!implicit)
9164 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9165 warning_init (loc, OPT_Woverride_init_side_effects,
9166 "initialized field with side-effects overwritten");
9167 else if (warn_override_init)
9168 warning_init (loc, OPT_Woverride_init,
9169 "initialized field overwritten");
9172 /* We can have just one union field set. */
9173 constructor_elements = NULL;
9176 /* Otherwise, output this element either to
9177 constructor_elements or to the assembler file. */
9179 constructor_elt celt = {field, value};
9180 vec_safe_push (constructor_elements, celt);
9182 /* Advance the variable that indicates sequential elements output. */
9183 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9184 constructor_unfilled_index
9185 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9186 bitsize_one_node);
9187 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9189 constructor_unfilled_fields
9190 = DECL_CHAIN (constructor_unfilled_fields);
9192 /* Skip any nameless bit fields. */
9193 while (constructor_unfilled_fields != NULL_TREE
9194 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9195 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
9196 constructor_unfilled_fields =
9197 DECL_CHAIN (constructor_unfilled_fields);
9199 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9200 constructor_unfilled_fields = NULL_TREE;
9202 /* Now output any pending elements which have become next. */
9203 if (pending)
9204 output_pending_init_elements (0, braced_init_obstack);
9207 /* Output any pending elements which have become next.
9208 As we output elements, constructor_unfilled_{fields,index}
9209 advances, which may cause other elements to become next;
9210 if so, they too are output.
9212 If ALL is 0, we return when there are
9213 no more pending elements to output now.
9215 If ALL is 1, we output space as necessary so that
9216 we can output all the pending elements. */
9217 static void
9218 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9220 struct init_node *elt = constructor_pending_elts;
9221 tree next;
9223 retry:
9225 /* Look through the whole pending tree.
9226 If we find an element that should be output now,
9227 output it. Otherwise, set NEXT to the element
9228 that comes first among those still pending. */
9230 next = NULL_TREE;
9231 while (elt)
9233 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9235 if (tree_int_cst_equal (elt->purpose,
9236 constructor_unfilled_index))
9237 output_init_element (input_location, elt->value, elt->origtype,
9238 true, TREE_TYPE (constructor_type),
9239 constructor_unfilled_index, false, false,
9240 braced_init_obstack);
9241 else if (tree_int_cst_lt (constructor_unfilled_index,
9242 elt->purpose))
9244 /* Advance to the next smaller node. */
9245 if (elt->left)
9246 elt = elt->left;
9247 else
9249 /* We have reached the smallest node bigger than the
9250 current unfilled index. Fill the space first. */
9251 next = elt->purpose;
9252 break;
9255 else
9257 /* Advance to the next bigger node. */
9258 if (elt->right)
9259 elt = elt->right;
9260 else
9262 /* We have reached the biggest node in a subtree. Find
9263 the parent of it, which is the next bigger node. */
9264 while (elt->parent && elt->parent->right == elt)
9265 elt = elt->parent;
9266 elt = elt->parent;
9267 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9268 elt->purpose))
9270 next = elt->purpose;
9271 break;
9276 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9278 tree ctor_unfilled_bitpos, elt_bitpos;
9280 /* If the current record is complete we are done. */
9281 if (constructor_unfilled_fields == NULL_TREE)
9282 break;
9284 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9285 elt_bitpos = bit_position (elt->purpose);
9286 /* We can't compare fields here because there might be empty
9287 fields in between. */
9288 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9290 constructor_unfilled_fields = elt->purpose;
9291 output_init_element (input_location, elt->value, elt->origtype,
9292 true, TREE_TYPE (elt->purpose),
9293 elt->purpose, false, false,
9294 braced_init_obstack);
9296 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9298 /* Advance to the next smaller node. */
9299 if (elt->left)
9300 elt = elt->left;
9301 else
9303 /* We have reached the smallest node bigger than the
9304 current unfilled field. Fill the space first. */
9305 next = elt->purpose;
9306 break;
9309 else
9311 /* Advance to the next bigger node. */
9312 if (elt->right)
9313 elt = elt->right;
9314 else
9316 /* We have reached the biggest node in a subtree. Find
9317 the parent of it, which is the next bigger node. */
9318 while (elt->parent && elt->parent->right == elt)
9319 elt = elt->parent;
9320 elt = elt->parent;
9321 if (elt
9322 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9323 bit_position (elt->purpose))))
9325 next = elt->purpose;
9326 break;
9333 /* Ordinarily return, but not if we want to output all
9334 and there are elements left. */
9335 if (!(all && next != NULL_TREE))
9336 return;
9338 /* If it's not incremental, just skip over the gap, so that after
9339 jumping to retry we will output the next successive element. */
9340 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9341 constructor_unfilled_fields = next;
9342 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9343 constructor_unfilled_index = next;
9345 /* ELT now points to the node in the pending tree with the next
9346 initializer to output. */
9347 goto retry;
9350 /* Add one non-braced element to the current constructor level.
9351 This adjusts the current position within the constructor's type.
9352 This may also start or terminate implicit levels
9353 to handle a partly-braced initializer.
9355 Once this has found the correct level for the new element,
9356 it calls output_init_element.
9358 IMPLICIT is true if value comes from pop_init_level (1),
9359 the new initializer has been merged with the existing one
9360 and thus no warnings should be emitted about overriding an
9361 existing initializer. */
9363 void
9364 process_init_element (location_t loc, struct c_expr value, bool implicit,
9365 struct obstack * braced_init_obstack)
9367 tree orig_value = value.value;
9368 int string_flag
9369 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9370 bool strict_string = value.original_code == STRING_CST;
9371 bool was_designated = designator_depth != 0;
9373 designator_depth = 0;
9374 designator_erroneous = 0;
9376 if (!implicit && value.value && !integer_zerop (value.value))
9377 constructor_zeroinit = 0;
9379 /* Handle superfluous braces around string cst as in
9380 char x[] = {"foo"}; */
9381 if (string_flag
9382 && constructor_type
9383 && !was_designated
9384 && TREE_CODE (constructor_type) == ARRAY_TYPE
9385 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9386 && integer_zerop (constructor_unfilled_index))
9388 if (constructor_stack->replacement_value.value)
9389 error_init (loc, "excess elements in char array initializer");
9390 constructor_stack->replacement_value = value;
9391 return;
9394 if (constructor_stack->replacement_value.value != NULL_TREE)
9396 error_init (loc, "excess elements in struct initializer");
9397 return;
9400 /* Ignore elements of a brace group if it is entirely superfluous
9401 and has already been diagnosed. */
9402 if (constructor_type == NULL_TREE)
9403 return;
9405 if (!implicit && warn_designated_init && !was_designated
9406 && TREE_CODE (constructor_type) == RECORD_TYPE
9407 && lookup_attribute ("designated_init",
9408 TYPE_ATTRIBUTES (constructor_type)))
9409 warning_init (loc,
9410 OPT_Wdesignated_init,
9411 "positional initialization of field "
9412 "in %<struct%> declared with %<designated_init%> attribute");
9414 /* If we've exhausted any levels that didn't have braces,
9415 pop them now. */
9416 while (constructor_stack->implicit)
9418 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9419 && constructor_fields == NULL_TREE)
9420 process_init_element (loc,
9421 pop_init_level (loc, 1, braced_init_obstack,
9422 last_init_list_comma),
9423 true, braced_init_obstack);
9424 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9425 || VECTOR_TYPE_P (constructor_type))
9426 && constructor_max_index
9427 && tree_int_cst_lt (constructor_max_index,
9428 constructor_index))
9429 process_init_element (loc,
9430 pop_init_level (loc, 1, braced_init_obstack,
9431 last_init_list_comma),
9432 true, braced_init_obstack);
9433 else
9434 break;
9437 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9438 if (constructor_range_stack)
9440 /* If value is a compound literal and we'll be just using its
9441 content, don't put it into a SAVE_EXPR. */
9442 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9443 || !require_constant_value)
9445 tree semantic_type = NULL_TREE;
9446 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9448 semantic_type = TREE_TYPE (value.value);
9449 value.value = TREE_OPERAND (value.value, 0);
9451 value.value = save_expr (value.value);
9452 if (semantic_type)
9453 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9454 value.value);
9458 while (1)
9460 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9462 tree fieldtype;
9463 enum tree_code fieldcode;
9465 if (constructor_fields == NULL_TREE)
9467 pedwarn_init (loc, 0, "excess elements in struct initializer");
9468 break;
9471 fieldtype = TREE_TYPE (constructor_fields);
9472 if (fieldtype != error_mark_node)
9473 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9474 fieldcode = TREE_CODE (fieldtype);
9476 /* Error for non-static initialization of a flexible array member. */
9477 if (fieldcode == ARRAY_TYPE
9478 && !require_constant_value
9479 && TYPE_SIZE (fieldtype) == NULL_TREE
9480 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9482 error_init (loc, "non-static initialization of a flexible "
9483 "array member");
9484 break;
9487 /* Error for initialization of a flexible array member with
9488 a string constant if the structure is in an array. E.g.:
9489 struct S { int x; char y[]; };
9490 struct S s[] = { { 1, "foo" } };
9491 is invalid. */
9492 if (string_flag
9493 && fieldcode == ARRAY_TYPE
9494 && constructor_depth > 1
9495 && TYPE_SIZE (fieldtype) == NULL_TREE
9496 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9498 bool in_array_p = false;
9499 for (struct constructor_stack *p = constructor_stack;
9500 p && p->type; p = p->next)
9501 if (TREE_CODE (p->type) == ARRAY_TYPE)
9503 in_array_p = true;
9504 break;
9506 if (in_array_p)
9508 error_init (loc, "initialization of flexible array "
9509 "member in a nested context");
9510 break;
9514 /* Accept a string constant to initialize a subarray. */
9515 if (value.value != NULL_TREE
9516 && fieldcode == ARRAY_TYPE
9517 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9518 && string_flag)
9519 value.value = orig_value;
9520 /* Otherwise, if we have come to a subaggregate,
9521 and we don't have an element of its type, push into it. */
9522 else if (value.value != NULL_TREE
9523 && value.value != error_mark_node
9524 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9525 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9526 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9528 push_init_level (loc, 1, braced_init_obstack);
9529 continue;
9532 if (value.value)
9534 push_member_name (constructor_fields);
9535 output_init_element (loc, value.value, value.original_type,
9536 strict_string, fieldtype,
9537 constructor_fields, true, implicit,
9538 braced_init_obstack);
9539 RESTORE_SPELLING_DEPTH (constructor_depth);
9541 else
9542 /* Do the bookkeeping for an element that was
9543 directly output as a constructor. */
9545 /* For a record, keep track of end position of last field. */
9546 if (DECL_SIZE (constructor_fields))
9547 constructor_bit_index
9548 = size_binop_loc (input_location, PLUS_EXPR,
9549 bit_position (constructor_fields),
9550 DECL_SIZE (constructor_fields));
9552 /* If the current field was the first one not yet written out,
9553 it isn't now, so update. */
9554 if (constructor_unfilled_fields == constructor_fields)
9556 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9557 /* Skip any nameless bit fields. */
9558 while (constructor_unfilled_fields != 0
9559 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9560 && DECL_NAME (constructor_unfilled_fields) == 0)
9561 constructor_unfilled_fields =
9562 DECL_CHAIN (constructor_unfilled_fields);
9566 constructor_fields = DECL_CHAIN (constructor_fields);
9567 /* Skip any nameless bit fields at the beginning. */
9568 while (constructor_fields != NULL_TREE
9569 && DECL_C_BIT_FIELD (constructor_fields)
9570 && DECL_NAME (constructor_fields) == NULL_TREE)
9571 constructor_fields = DECL_CHAIN (constructor_fields);
9573 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9575 tree fieldtype;
9576 enum tree_code fieldcode;
9578 if (constructor_fields == NULL_TREE)
9580 pedwarn_init (loc, 0,
9581 "excess elements in union initializer");
9582 break;
9585 fieldtype = TREE_TYPE (constructor_fields);
9586 if (fieldtype != error_mark_node)
9587 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9588 fieldcode = TREE_CODE (fieldtype);
9590 /* Warn that traditional C rejects initialization of unions.
9591 We skip the warning if the value is zero. This is done
9592 under the assumption that the zero initializer in user
9593 code appears conditioned on e.g. __STDC__ to avoid
9594 "missing initializer" warnings and relies on default
9595 initialization to zero in the traditional C case.
9596 We also skip the warning if the initializer is designated,
9597 again on the assumption that this must be conditional on
9598 __STDC__ anyway (and we've already complained about the
9599 member-designator already). */
9600 if (!in_system_header_at (input_location) && !constructor_designated
9601 && !(value.value && (integer_zerop (value.value)
9602 || real_zerop (value.value))))
9603 warning (OPT_Wtraditional, "traditional C rejects initialization "
9604 "of unions");
9606 /* Accept a string constant to initialize a subarray. */
9607 if (value.value != NULL_TREE
9608 && fieldcode == ARRAY_TYPE
9609 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9610 && string_flag)
9611 value.value = orig_value;
9612 /* Otherwise, if we have come to a subaggregate,
9613 and we don't have an element of its type, push into it. */
9614 else if (value.value != NULL_TREE
9615 && value.value != error_mark_node
9616 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9617 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9618 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9620 push_init_level (loc, 1, braced_init_obstack);
9621 continue;
9624 if (value.value)
9626 push_member_name (constructor_fields);
9627 output_init_element (loc, value.value, value.original_type,
9628 strict_string, fieldtype,
9629 constructor_fields, true, implicit,
9630 braced_init_obstack);
9631 RESTORE_SPELLING_DEPTH (constructor_depth);
9633 else
9634 /* Do the bookkeeping for an element that was
9635 directly output as a constructor. */
9637 constructor_bit_index = DECL_SIZE (constructor_fields);
9638 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9641 constructor_fields = NULL_TREE;
9643 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9645 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9646 enum tree_code eltcode = TREE_CODE (elttype);
9648 /* Accept a string constant to initialize a subarray. */
9649 if (value.value != NULL_TREE
9650 && eltcode == ARRAY_TYPE
9651 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9652 && string_flag)
9653 value.value = orig_value;
9654 /* Otherwise, if we have come to a subaggregate,
9655 and we don't have an element of its type, push into it. */
9656 else if (value.value != NULL_TREE
9657 && value.value != error_mark_node
9658 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9659 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9660 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9662 push_init_level (loc, 1, braced_init_obstack);
9663 continue;
9666 if (constructor_max_index != NULL_TREE
9667 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9668 || integer_all_onesp (constructor_max_index)))
9670 pedwarn_init (loc, 0,
9671 "excess elements in array initializer");
9672 break;
9675 /* Now output the actual element. */
9676 if (value.value)
9678 push_array_bounds (tree_to_uhwi (constructor_index));
9679 output_init_element (loc, value.value, value.original_type,
9680 strict_string, elttype,
9681 constructor_index, true, implicit,
9682 braced_init_obstack);
9683 RESTORE_SPELLING_DEPTH (constructor_depth);
9686 constructor_index
9687 = size_binop_loc (input_location, PLUS_EXPR,
9688 constructor_index, bitsize_one_node);
9690 if (!value.value)
9691 /* If we are doing the bookkeeping for an element that was
9692 directly output as a constructor, we must update
9693 constructor_unfilled_index. */
9694 constructor_unfilled_index = constructor_index;
9696 else if (VECTOR_TYPE_P (constructor_type))
9698 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9700 /* Do a basic check of initializer size. Note that vectors
9701 always have a fixed size derived from their type. */
9702 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9704 pedwarn_init (loc, 0,
9705 "excess elements in vector initializer");
9706 break;
9709 /* Now output the actual element. */
9710 if (value.value)
9712 if (TREE_CODE (value.value) == VECTOR_CST)
9713 elttype = TYPE_MAIN_VARIANT (constructor_type);
9714 output_init_element (loc, value.value, value.original_type,
9715 strict_string, elttype,
9716 constructor_index, true, implicit,
9717 braced_init_obstack);
9720 constructor_index
9721 = size_binop_loc (input_location,
9722 PLUS_EXPR, constructor_index, bitsize_one_node);
9724 if (!value.value)
9725 /* If we are doing the bookkeeping for an element that was
9726 directly output as a constructor, we must update
9727 constructor_unfilled_index. */
9728 constructor_unfilled_index = constructor_index;
9731 /* Handle the sole element allowed in a braced initializer
9732 for a scalar variable. */
9733 else if (constructor_type != error_mark_node
9734 && constructor_fields == NULL_TREE)
9736 pedwarn_init (loc, 0,
9737 "excess elements in scalar initializer");
9738 break;
9740 else
9742 if (value.value)
9743 output_init_element (loc, value.value, value.original_type,
9744 strict_string, constructor_type,
9745 NULL_TREE, true, implicit,
9746 braced_init_obstack);
9747 constructor_fields = NULL_TREE;
9750 /* Handle range initializers either at this level or anywhere higher
9751 in the designator stack. */
9752 if (constructor_range_stack)
9754 struct constructor_range_stack *p, *range_stack;
9755 int finish = 0;
9757 range_stack = constructor_range_stack;
9758 constructor_range_stack = 0;
9759 while (constructor_stack != range_stack->stack)
9761 gcc_assert (constructor_stack->implicit);
9762 process_init_element (loc,
9763 pop_init_level (loc, 1,
9764 braced_init_obstack,
9765 last_init_list_comma),
9766 true, braced_init_obstack);
9768 for (p = range_stack;
9769 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9770 p = p->prev)
9772 gcc_assert (constructor_stack->implicit);
9773 process_init_element (loc,
9774 pop_init_level (loc, 1,
9775 braced_init_obstack,
9776 last_init_list_comma),
9777 true, braced_init_obstack);
9780 p->index = size_binop_loc (input_location,
9781 PLUS_EXPR, p->index, bitsize_one_node);
9782 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9783 finish = 1;
9785 while (1)
9787 constructor_index = p->index;
9788 constructor_fields = p->fields;
9789 if (finish && p->range_end && p->index == p->range_start)
9791 finish = 0;
9792 p->prev = 0;
9794 p = p->next;
9795 if (!p)
9796 break;
9797 finish_implicit_inits (loc, braced_init_obstack);
9798 push_init_level (loc, 2, braced_init_obstack);
9799 p->stack = constructor_stack;
9800 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9801 p->index = p->range_start;
9804 if (!finish)
9805 constructor_range_stack = range_stack;
9806 continue;
9809 break;
9812 constructor_range_stack = 0;
9815 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9816 (guaranteed to be 'volatile' or null) and ARGS (represented using
9817 an ASM_EXPR node). */
9818 tree
9819 build_asm_stmt (tree cv_qualifier, tree args)
9821 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9822 ASM_VOLATILE_P (args) = 1;
9823 return add_stmt (args);
9826 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9827 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9828 SIMPLE indicates whether there was anything at all after the
9829 string in the asm expression -- asm("blah") and asm("blah" : )
9830 are subtly different. We use a ASM_EXPR node to represent this. */
9831 tree
9832 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9833 tree clobbers, tree labels, bool simple)
9835 tree tail;
9836 tree args;
9837 int i;
9838 const char *constraint;
9839 const char **oconstraints;
9840 bool allows_mem, allows_reg, is_inout;
9841 int ninputs, noutputs;
9843 ninputs = list_length (inputs);
9844 noutputs = list_length (outputs);
9845 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9847 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9849 /* Remove output conversions that change the type but not the mode. */
9850 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9852 tree output = TREE_VALUE (tail);
9854 output = c_fully_fold (output, false, NULL);
9856 /* ??? Really, this should not be here. Users should be using a
9857 proper lvalue, dammit. But there's a long history of using casts
9858 in the output operands. In cases like longlong.h, this becomes a
9859 primitive form of typechecking -- if the cast can be removed, then
9860 the output operand had a type of the proper width; otherwise we'll
9861 get an error. Gross, but ... */
9862 STRIP_NOPS (output);
9864 if (!lvalue_or_else (loc, output, lv_asm))
9865 output = error_mark_node;
9867 if (output != error_mark_node
9868 && (TREE_READONLY (output)
9869 || TYPE_READONLY (TREE_TYPE (output))
9870 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9871 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9872 readonly_error (loc, output, lv_asm);
9874 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9875 oconstraints[i] = constraint;
9877 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9878 &allows_mem, &allows_reg, &is_inout))
9880 /* If the operand is going to end up in memory,
9881 mark it addressable. */
9882 if (!allows_reg && !c_mark_addressable (output))
9883 output = error_mark_node;
9884 if (!(!allows_reg && allows_mem)
9885 && output != error_mark_node
9886 && VOID_TYPE_P (TREE_TYPE (output)))
9888 error_at (loc, "invalid use of void expression");
9889 output = error_mark_node;
9892 else
9893 output = error_mark_node;
9895 TREE_VALUE (tail) = output;
9898 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9900 tree input;
9902 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9903 input = TREE_VALUE (tail);
9905 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9906 oconstraints, &allows_mem, &allows_reg))
9908 /* If the operand is going to end up in memory,
9909 mark it addressable. */
9910 if (!allows_reg && allows_mem)
9912 input = c_fully_fold (input, false, NULL);
9914 /* Strip the nops as we allow this case. FIXME, this really
9915 should be rejected or made deprecated. */
9916 STRIP_NOPS (input);
9917 if (!c_mark_addressable (input))
9918 input = error_mark_node;
9920 else
9922 struct c_expr expr;
9923 memset (&expr, 0, sizeof (expr));
9924 expr.value = input;
9925 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9926 input = c_fully_fold (expr.value, false, NULL);
9928 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9930 error_at (loc, "invalid use of void expression");
9931 input = error_mark_node;
9935 else
9936 input = error_mark_node;
9938 TREE_VALUE (tail) = input;
9941 /* ASMs with labels cannot have outputs. This should have been
9942 enforced by the parser. */
9943 gcc_assert (outputs == NULL || labels == NULL);
9945 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9947 /* asm statements without outputs, including simple ones, are treated
9948 as volatile. */
9949 ASM_INPUT_P (args) = simple;
9950 ASM_VOLATILE_P (args) = (noutputs == 0);
9952 return args;
9955 /* Generate a goto statement to LABEL. LOC is the location of the
9956 GOTO. */
9958 tree
9959 c_finish_goto_label (location_t loc, tree label)
9961 tree decl = lookup_label_for_goto (loc, label);
9962 if (!decl)
9963 return NULL_TREE;
9964 TREE_USED (decl) = 1;
9966 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
9967 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9968 SET_EXPR_LOCATION (t, loc);
9969 return add_stmt (t);
9973 /* Generate a computed goto statement to EXPR. LOC is the location of
9974 the GOTO. */
9976 tree
9977 c_finish_goto_ptr (location_t loc, tree expr)
9979 tree t;
9980 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9981 expr = c_fully_fold (expr, false, NULL);
9982 expr = convert (ptr_type_node, expr);
9983 t = build1 (GOTO_EXPR, void_type_node, expr);
9984 SET_EXPR_LOCATION (t, loc);
9985 return add_stmt (t);
9988 /* Generate a C `return' statement. RETVAL is the expression for what
9989 to return, or a null pointer for `return;' with no value. LOC is
9990 the location of the return statement, or the location of the expression,
9991 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9992 is the original type of RETVAL. */
9994 tree
9995 c_finish_return (location_t loc, tree retval, tree origtype)
9997 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9998 bool no_warning = false;
9999 bool npc = false;
10000 size_t rank = 0;
10002 /* Use the expansion point to handle cases such as returning NULL
10003 in a function returning void. */
10004 source_location xloc = expansion_point_location_if_in_system_header (loc);
10006 if (TREE_THIS_VOLATILE (current_function_decl))
10007 warning_at (xloc, 0,
10008 "function declared %<noreturn%> has a %<return%> statement");
10010 if (flag_cilkplus && contains_array_notation_expr (retval))
10012 /* Array notations are allowed in a return statement if it is inside a
10013 built-in array notation reduction function. */
10014 if (!find_rank (loc, retval, retval, false, &rank))
10015 return error_mark_node;
10016 if (rank >= 1)
10018 error_at (loc, "array notation expression cannot be used as a "
10019 "return value");
10020 return error_mark_node;
10023 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
10025 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
10026 "allowed");
10027 return error_mark_node;
10029 if (retval)
10031 tree semantic_type = NULL_TREE;
10032 npc = null_pointer_constant_p (retval);
10033 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10035 semantic_type = TREE_TYPE (retval);
10036 retval = TREE_OPERAND (retval, 0);
10038 retval = c_fully_fold (retval, false, NULL);
10039 if (semantic_type)
10040 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10043 if (!retval)
10045 current_function_returns_null = 1;
10046 if ((warn_return_type || flag_isoc99)
10047 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10049 bool warned_here;
10050 if (flag_isoc99)
10051 warned_here = pedwarn
10052 (loc, 0,
10053 "%<return%> with no value, in function returning non-void");
10054 else
10055 warned_here = warning_at
10056 (loc, OPT_Wreturn_type,
10057 "%<return%> with no value, in function returning non-void");
10058 no_warning = true;
10059 if (warned_here)
10060 inform (DECL_SOURCE_LOCATION (current_function_decl),
10061 "declared here");
10064 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10066 current_function_returns_null = 1;
10067 bool warned_here;
10068 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10069 warned_here = pedwarn
10070 (xloc, 0,
10071 "%<return%> with a value, in function returning void");
10072 else
10073 warned_here = pedwarn
10074 (xloc, OPT_Wpedantic, "ISO C forbids "
10075 "%<return%> with expression, in function returning void");
10076 if (warned_here)
10077 inform (DECL_SOURCE_LOCATION (current_function_decl),
10078 "declared here");
10080 else
10082 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10083 retval, origtype, ic_return,
10084 npc, NULL_TREE, NULL_TREE, 0);
10085 tree res = DECL_RESULT (current_function_decl);
10086 tree inner;
10087 bool save;
10089 current_function_returns_value = 1;
10090 if (t == error_mark_node)
10091 return NULL_TREE;
10093 save = in_late_binary_op;
10094 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10095 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10096 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10097 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10098 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10099 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10100 in_late_binary_op = true;
10101 inner = t = convert (TREE_TYPE (res), t);
10102 in_late_binary_op = save;
10104 /* Strip any conversions, additions, and subtractions, and see if
10105 we are returning the address of a local variable. Warn if so. */
10106 while (1)
10108 switch (TREE_CODE (inner))
10110 CASE_CONVERT:
10111 case NON_LVALUE_EXPR:
10112 case PLUS_EXPR:
10113 case POINTER_PLUS_EXPR:
10114 inner = TREE_OPERAND (inner, 0);
10115 continue;
10117 case MINUS_EXPR:
10118 /* If the second operand of the MINUS_EXPR has a pointer
10119 type (or is converted from it), this may be valid, so
10120 don't give a warning. */
10122 tree op1 = TREE_OPERAND (inner, 1);
10124 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10125 && (CONVERT_EXPR_P (op1)
10126 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10127 op1 = TREE_OPERAND (op1, 0);
10129 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10130 break;
10132 inner = TREE_OPERAND (inner, 0);
10133 continue;
10136 case ADDR_EXPR:
10137 inner = TREE_OPERAND (inner, 0);
10139 while (REFERENCE_CLASS_P (inner)
10140 && !INDIRECT_REF_P (inner))
10141 inner = TREE_OPERAND (inner, 0);
10143 if (DECL_P (inner)
10144 && !DECL_EXTERNAL (inner)
10145 && !TREE_STATIC (inner)
10146 && DECL_CONTEXT (inner) == current_function_decl)
10148 if (TREE_CODE (inner) == LABEL_DECL)
10149 warning_at (loc, OPT_Wreturn_local_addr,
10150 "function returns address of label");
10151 else
10153 warning_at (loc, OPT_Wreturn_local_addr,
10154 "function returns address of local variable");
10155 tree zero = build_zero_cst (TREE_TYPE (res));
10156 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10159 break;
10161 default:
10162 break;
10165 break;
10168 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10169 SET_EXPR_LOCATION (retval, loc);
10171 if (warn_sequence_point)
10172 verify_sequence_points (retval);
10175 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10176 TREE_NO_WARNING (ret_stmt) |= no_warning;
10177 return add_stmt (ret_stmt);
10180 struct c_switch {
10181 /* The SWITCH_EXPR being built. */
10182 tree switch_expr;
10184 /* The original type of the testing expression, i.e. before the
10185 default conversion is applied. */
10186 tree orig_type;
10188 /* A splay-tree mapping the low element of a case range to the high
10189 element, or NULL_TREE if there is no high element. Used to
10190 determine whether or not a new case label duplicates an old case
10191 label. We need a tree, rather than simply a hash table, because
10192 of the GNU case range extension. */
10193 splay_tree cases;
10195 /* The bindings at the point of the switch. This is used for
10196 warnings crossing decls when branching to a case label. */
10197 struct c_spot_bindings *bindings;
10199 /* The next node on the stack. */
10200 struct c_switch *next;
10202 /* Remember whether the controlling expression had boolean type
10203 before integer promotions for the sake of -Wswitch-bool. */
10204 bool bool_cond_p;
10206 /* Remember whether there was a case value that is outside the
10207 range of the ORIG_TYPE. */
10208 bool outside_range_p;
10211 /* A stack of the currently active switch statements. The innermost
10212 switch statement is on the top of the stack. There is no need to
10213 mark the stack for garbage collection because it is only active
10214 during the processing of the body of a function, and we never
10215 collect at that point. */
10217 struct c_switch *c_switch_stack;
10219 /* Start a C switch statement, testing expression EXP. Return the new
10220 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10221 SWITCH_COND_LOC is the location of the switch's condition.
10222 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10224 tree
10225 c_start_case (location_t switch_loc,
10226 location_t switch_cond_loc,
10227 tree exp, bool explicit_cast_p)
10229 tree orig_type = error_mark_node;
10230 bool bool_cond_p = false;
10231 struct c_switch *cs;
10233 if (exp != error_mark_node)
10235 orig_type = TREE_TYPE (exp);
10237 if (!INTEGRAL_TYPE_P (orig_type))
10239 if (orig_type != error_mark_node)
10241 error_at (switch_cond_loc, "switch quantity not an integer");
10242 orig_type = error_mark_node;
10244 exp = integer_zero_node;
10246 else
10248 tree type = TYPE_MAIN_VARIANT (orig_type);
10249 tree e = exp;
10251 /* Warn if the condition has boolean value. */
10252 while (TREE_CODE (e) == COMPOUND_EXPR)
10253 e = TREE_OPERAND (e, 1);
10255 if ((TREE_CODE (type) == BOOLEAN_TYPE
10256 || truth_value_p (TREE_CODE (e)))
10257 /* Explicit cast to int suppresses this warning. */
10258 && !(TREE_CODE (type) == INTEGER_TYPE
10259 && explicit_cast_p))
10260 bool_cond_p = true;
10262 if (!in_system_header_at (input_location)
10263 && (type == long_integer_type_node
10264 || type == long_unsigned_type_node))
10265 warning_at (switch_cond_loc,
10266 OPT_Wtraditional, "%<long%> switch expression not "
10267 "converted to %<int%> in ISO C");
10269 exp = c_fully_fold (exp, false, NULL);
10270 exp = default_conversion (exp);
10272 if (warn_sequence_point)
10273 verify_sequence_points (exp);
10277 /* Add this new SWITCH_EXPR to the stack. */
10278 cs = XNEW (struct c_switch);
10279 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10280 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10281 cs->orig_type = orig_type;
10282 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10283 cs->bindings = c_get_switch_bindings ();
10284 cs->bool_cond_p = bool_cond_p;
10285 cs->outside_range_p = false;
10286 cs->next = c_switch_stack;
10287 c_switch_stack = cs;
10289 return add_stmt (cs->switch_expr);
10292 /* Process a case label at location LOC. */
10294 tree
10295 do_case (location_t loc, tree low_value, tree high_value)
10297 tree label = NULL_TREE;
10299 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10301 low_value = c_fully_fold (low_value, false, NULL);
10302 if (TREE_CODE (low_value) == INTEGER_CST)
10303 pedwarn (loc, OPT_Wpedantic,
10304 "case label is not an integer constant expression");
10307 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10309 high_value = c_fully_fold (high_value, false, NULL);
10310 if (TREE_CODE (high_value) == INTEGER_CST)
10311 pedwarn (input_location, OPT_Wpedantic,
10312 "case label is not an integer constant expression");
10315 if (c_switch_stack == NULL)
10317 if (low_value)
10318 error_at (loc, "case label not within a switch statement");
10319 else
10320 error_at (loc, "%<default%> label not within a switch statement");
10321 return NULL_TREE;
10324 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10325 EXPR_LOCATION (c_switch_stack->switch_expr),
10326 loc))
10327 return NULL_TREE;
10329 label = c_add_case_label (loc, c_switch_stack->cases,
10330 SWITCH_COND (c_switch_stack->switch_expr),
10331 c_switch_stack->orig_type,
10332 low_value, high_value,
10333 &c_switch_stack->outside_range_p);
10334 if (label == error_mark_node)
10335 label = NULL_TREE;
10336 return label;
10339 /* Finish the switch statement. TYPE is the original type of the
10340 controlling expression of the switch, or NULL_TREE. */
10342 void
10343 c_finish_case (tree body, tree type)
10345 struct c_switch *cs = c_switch_stack;
10346 location_t switch_location;
10348 SWITCH_BODY (cs->switch_expr) = body;
10350 /* Emit warnings as needed. */
10351 switch_location = EXPR_LOCATION (cs->switch_expr);
10352 c_do_switch_warnings (cs->cases, switch_location,
10353 type ? type : TREE_TYPE (cs->switch_expr),
10354 SWITCH_COND (cs->switch_expr),
10355 cs->bool_cond_p, cs->outside_range_p);
10357 /* Pop the stack. */
10358 c_switch_stack = cs->next;
10359 splay_tree_delete (cs->cases);
10360 c_release_switch_bindings (cs->bindings);
10361 XDELETE (cs);
10364 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10365 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10366 may be null. */
10368 void
10369 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10370 tree else_block)
10372 tree stmt;
10374 /* If the condition has array notations, then the rank of the then_block and
10375 else_block must be either 0 or be equal to the rank of the condition. If
10376 the condition does not have array notations then break them up as it is
10377 broken up in a normal expression. */
10378 if (flag_cilkplus && contains_array_notation_expr (cond))
10380 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10381 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10382 return;
10383 if (then_block
10384 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10385 return;
10386 if (else_block
10387 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10388 return;
10389 if (cond_rank != then_rank && then_rank != 0)
10391 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10392 " and the then-block");
10393 return;
10395 else if (cond_rank != else_rank && else_rank != 0)
10397 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10398 " and the else-block");
10399 return;
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 /* In theory could forbid cilk spawn for loop increment expression,
10421 but it should work just fine. */
10423 /* If the condition is zero don't generate a loop construct. */
10424 if (cond && integer_zerop (cond))
10426 if (cond_is_first)
10428 t = build_and_jump (&blab);
10429 SET_EXPR_LOCATION (t, start_locus);
10430 add_stmt (t);
10433 else
10435 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10437 /* If we have an exit condition, then we build an IF with gotos either
10438 out of the loop, or to the top of it. If there's no exit condition,
10439 then we just build a jump back to the top. */
10440 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10442 if (cond && !integer_nonzerop (cond))
10444 /* Canonicalize the loop condition to the end. This means
10445 generating a branch to the loop condition. Reuse the
10446 continue label, if possible. */
10447 if (cond_is_first)
10449 if (incr || !clab)
10451 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10452 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10454 else
10455 t = build1 (GOTO_EXPR, void_type_node, clab);
10456 SET_EXPR_LOCATION (t, start_locus);
10457 add_stmt (t);
10460 t = build_and_jump (&blab);
10461 if (cond_is_first)
10462 exit = fold_build3_loc (start_locus,
10463 COND_EXPR, void_type_node, cond, exit, t);
10464 else
10465 exit = fold_build3_loc (input_location,
10466 COND_EXPR, void_type_node, cond, exit, t);
10468 else
10470 /* For the backward-goto's location of an unconditional loop
10471 use the beginning of the body, or, if there is none, the
10472 top of the loop. */
10473 location_t loc = EXPR_LOCATION (expr_first (body));
10474 if (loc == UNKNOWN_LOCATION)
10475 loc = start_locus;
10476 SET_EXPR_LOCATION (exit, loc);
10479 add_stmt (top);
10482 if (body)
10483 add_stmt (body);
10484 if (clab)
10485 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10486 if (incr)
10487 add_stmt (incr);
10488 if (entry)
10489 add_stmt (entry);
10490 if (exit)
10491 add_stmt (exit);
10492 if (blab)
10493 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10496 tree
10497 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10499 bool skip;
10500 tree label = *label_p;
10502 /* In switch statements break is sometimes stylistically used after
10503 a return statement. This can lead to spurious warnings about
10504 control reaching the end of a non-void function when it is
10505 inlined. Note that we are calling block_may_fallthru with
10506 language specific tree nodes; this works because
10507 block_may_fallthru returns true when given something it does not
10508 understand. */
10509 skip = !block_may_fallthru (cur_stmt_list);
10511 if (!label)
10513 if (!skip)
10514 *label_p = label = create_artificial_label (loc);
10516 else if (TREE_CODE (label) == LABEL_DECL)
10518 else switch (TREE_INT_CST_LOW (label))
10520 case 0:
10521 if (is_break)
10522 error_at (loc, "break statement not within loop or switch");
10523 else
10524 error_at (loc, "continue statement not within a loop");
10525 return NULL_TREE;
10527 case 1:
10528 gcc_assert (is_break);
10529 error_at (loc, "break statement used with OpenMP for loop");
10530 return NULL_TREE;
10532 case 2:
10533 if (is_break)
10534 error ("break statement within %<#pragma simd%> loop body");
10535 else
10536 error ("continue statement within %<#pragma simd%> loop body");
10537 return NULL_TREE;
10539 default:
10540 gcc_unreachable ();
10543 if (skip)
10544 return NULL_TREE;
10546 if (!is_break)
10547 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10549 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10552 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10554 static void
10555 emit_side_effect_warnings (location_t loc, tree expr)
10557 if (expr == error_mark_node)
10559 else if (!TREE_SIDE_EFFECTS (expr))
10561 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10562 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10564 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10566 tree r = expr;
10567 location_t cloc = loc;
10568 while (TREE_CODE (r) == COMPOUND_EXPR)
10570 if (EXPR_HAS_LOCATION (r))
10571 cloc = EXPR_LOCATION (r);
10572 r = TREE_OPERAND (r, 1);
10574 if (!TREE_SIDE_EFFECTS (r)
10575 && !VOID_TYPE_P (TREE_TYPE (r))
10576 && !CONVERT_EXPR_P (r)
10577 && !TREE_NO_WARNING (r)
10578 && !TREE_NO_WARNING (expr))
10579 warning_at (cloc, OPT_Wunused_value,
10580 "right-hand operand of comma expression has no effect");
10582 else
10583 warn_if_unused_value (expr, loc);
10586 /* Process an expression as if it were a complete statement. Emit
10587 diagnostics, but do not call ADD_STMT. LOC is the location of the
10588 statement. */
10590 tree
10591 c_process_expr_stmt (location_t loc, tree expr)
10593 tree exprv;
10595 if (!expr)
10596 return NULL_TREE;
10598 expr = c_fully_fold (expr, false, NULL);
10600 if (warn_sequence_point)
10601 verify_sequence_points (expr);
10603 if (TREE_TYPE (expr) != error_mark_node
10604 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10605 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10606 error_at (loc, "expression statement has incomplete type");
10608 /* If we're not processing a statement expression, warn about unused values.
10609 Warnings for statement expressions will be emitted later, once we figure
10610 out which is the result. */
10611 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10612 && warn_unused_value)
10613 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10615 exprv = expr;
10616 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10617 exprv = TREE_OPERAND (exprv, 1);
10618 while (CONVERT_EXPR_P (exprv))
10619 exprv = TREE_OPERAND (exprv, 0);
10620 if (DECL_P (exprv)
10621 || handled_component_p (exprv)
10622 || TREE_CODE (exprv) == ADDR_EXPR)
10623 mark_exp_read (exprv);
10625 /* If the expression is not of a type to which we cannot assign a line
10626 number, wrap the thing in a no-op NOP_EXPR. */
10627 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10629 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10630 SET_EXPR_LOCATION (expr, loc);
10633 return expr;
10636 /* Emit an expression as a statement. LOC is the location of the
10637 expression. */
10639 tree
10640 c_finish_expr_stmt (location_t loc, tree expr)
10642 if (expr)
10643 return add_stmt (c_process_expr_stmt (loc, expr));
10644 else
10645 return NULL;
10648 /* Do the opposite and emit a statement as an expression. To begin,
10649 create a new binding level and return it. */
10651 tree
10652 c_begin_stmt_expr (void)
10654 tree ret;
10656 /* We must force a BLOCK for this level so that, if it is not expanded
10657 later, there is a way to turn off the entire subtree of blocks that
10658 are contained in it. */
10659 keep_next_level ();
10660 ret = c_begin_compound_stmt (true);
10662 c_bindings_start_stmt_expr (c_switch_stack == NULL
10663 ? NULL
10664 : c_switch_stack->bindings);
10666 /* Mark the current statement list as belonging to a statement list. */
10667 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10669 return ret;
10672 /* LOC is the location of the compound statement to which this body
10673 belongs. */
10675 tree
10676 c_finish_stmt_expr (location_t loc, tree body)
10678 tree last, type, tmp, val;
10679 tree *last_p;
10681 body = c_end_compound_stmt (loc, body, true);
10683 c_bindings_end_stmt_expr (c_switch_stack == NULL
10684 ? NULL
10685 : c_switch_stack->bindings);
10687 /* Locate the last statement in BODY. See c_end_compound_stmt
10688 about always returning a BIND_EXPR. */
10689 last_p = &BIND_EXPR_BODY (body);
10690 last = BIND_EXPR_BODY (body);
10692 continue_searching:
10693 if (TREE_CODE (last) == STATEMENT_LIST)
10695 tree_stmt_iterator i;
10697 /* This can happen with degenerate cases like ({ }). No value. */
10698 if (!TREE_SIDE_EFFECTS (last))
10699 return body;
10701 /* If we're supposed to generate side effects warnings, process
10702 all of the statements except the last. */
10703 if (warn_unused_value)
10705 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10707 location_t tloc;
10708 tree t = tsi_stmt (i);
10710 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10711 emit_side_effect_warnings (tloc, t);
10714 else
10715 i = tsi_last (last);
10716 last_p = tsi_stmt_ptr (i);
10717 last = *last_p;
10720 /* If the end of the list is exception related, then the list was split
10721 by a call to push_cleanup. Continue searching. */
10722 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10723 || TREE_CODE (last) == TRY_CATCH_EXPR)
10725 last_p = &TREE_OPERAND (last, 0);
10726 last = *last_p;
10727 goto continue_searching;
10730 if (last == error_mark_node)
10731 return last;
10733 /* In the case that the BIND_EXPR is not necessary, return the
10734 expression out from inside it. */
10735 if (last == BIND_EXPR_BODY (body)
10736 && BIND_EXPR_VARS (body) == NULL)
10738 /* Even if this looks constant, do not allow it in a constant
10739 expression. */
10740 last = c_wrap_maybe_const (last, true);
10741 /* Do not warn if the return value of a statement expression is
10742 unused. */
10743 TREE_NO_WARNING (last) = 1;
10744 return last;
10747 /* Extract the type of said expression. */
10748 type = TREE_TYPE (last);
10750 /* If we're not returning a value at all, then the BIND_EXPR that
10751 we already have is a fine expression to return. */
10752 if (!type || VOID_TYPE_P (type))
10753 return body;
10755 /* Now that we've located the expression containing the value, it seems
10756 silly to make voidify_wrapper_expr repeat the process. Create a
10757 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10758 tmp = create_tmp_var_raw (type);
10760 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10761 tree_expr_nonnegative_p giving up immediately. */
10762 val = last;
10763 if (TREE_CODE (val) == NOP_EXPR
10764 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10765 val = TREE_OPERAND (val, 0);
10767 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10768 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10771 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10772 SET_EXPR_LOCATION (t, loc);
10773 return t;
10777 /* Begin and end compound statements. This is as simple as pushing
10778 and popping new statement lists from the tree. */
10780 tree
10781 c_begin_compound_stmt (bool do_scope)
10783 tree stmt = push_stmt_list ();
10784 if (do_scope)
10785 push_scope ();
10786 return stmt;
10789 /* End a compound statement. STMT is the statement. LOC is the
10790 location of the compound statement-- this is usually the location
10791 of the opening brace. */
10793 tree
10794 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10796 tree block = NULL;
10798 if (do_scope)
10800 if (c_dialect_objc ())
10801 objc_clear_super_receiver ();
10802 block = pop_scope ();
10805 stmt = pop_stmt_list (stmt);
10806 stmt = c_build_bind_expr (loc, block, stmt);
10808 /* If this compound statement is nested immediately inside a statement
10809 expression, then force a BIND_EXPR to be created. Otherwise we'll
10810 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10811 STATEMENT_LISTs merge, and thus we can lose track of what statement
10812 was really last. */
10813 if (building_stmt_list_p ()
10814 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10815 && TREE_CODE (stmt) != BIND_EXPR)
10817 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10818 TREE_SIDE_EFFECTS (stmt) = 1;
10819 SET_EXPR_LOCATION (stmt, loc);
10822 return stmt;
10825 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10826 when the current scope is exited. EH_ONLY is true when this is not
10827 meant to apply to normal control flow transfer. */
10829 void
10830 push_cleanup (tree decl, tree cleanup, bool eh_only)
10832 enum tree_code code;
10833 tree stmt, list;
10834 bool stmt_expr;
10836 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10837 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10838 add_stmt (stmt);
10839 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10840 list = push_stmt_list ();
10841 TREE_OPERAND (stmt, 0) = list;
10842 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10845 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10846 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10848 static tree
10849 build_vec_cmp (tree_code code, tree type,
10850 tree arg0, tree arg1)
10852 tree zero_vec = build_zero_cst (type);
10853 tree minus_one_vec = build_minus_one_cst (type);
10854 tree cmp_type = build_same_sized_truth_vector_type (type);
10855 tree cmp = build2 (code, cmp_type, arg0, arg1);
10856 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10859 /* Build a binary-operation expression without default conversions.
10860 CODE is the kind of expression to build.
10861 LOCATION is the operator's location.
10862 This function differs from `build' in several ways:
10863 the data type of the result is computed and recorded in it,
10864 warnings are generated if arg data types are invalid,
10865 special handling for addition and subtraction of pointers is known,
10866 and some optimization is done (operations on narrow ints
10867 are done in the narrower type when that gives the same result).
10868 Constant folding is also done before the result is returned.
10870 Note that the operands will never have enumeral types, or function
10871 or array types, because either they will have the default conversions
10872 performed or they have both just been converted to some other type in which
10873 the arithmetic is to be done. */
10875 tree
10876 build_binary_op (location_t location, enum tree_code code,
10877 tree orig_op0, tree orig_op1, bool convert_p)
10879 tree type0, type1, orig_type0, orig_type1;
10880 tree eptype;
10881 enum tree_code code0, code1;
10882 tree op0, op1;
10883 tree ret = error_mark_node;
10884 const char *invalid_op_diag;
10885 bool op0_int_operands, op1_int_operands;
10886 bool int_const, int_const_or_overflow, int_operands;
10888 /* Expression code to give to the expression when it is built.
10889 Normally this is CODE, which is what the caller asked for,
10890 but in some special cases we change it. */
10891 enum tree_code resultcode = code;
10893 /* Data type in which the computation is to be performed.
10894 In the simplest cases this is the common type of the arguments. */
10895 tree result_type = NULL;
10897 /* When the computation is in excess precision, the type of the
10898 final EXCESS_PRECISION_EXPR. */
10899 tree semantic_result_type = NULL;
10901 /* Nonzero means operands have already been type-converted
10902 in whatever way is necessary.
10903 Zero means they need to be converted to RESULT_TYPE. */
10904 int converted = 0;
10906 /* Nonzero means create the expression with this type, rather than
10907 RESULT_TYPE. */
10908 tree build_type = NULL_TREE;
10910 /* Nonzero means after finally constructing the expression
10911 convert it to this type. */
10912 tree final_type = NULL_TREE;
10914 /* Nonzero if this is an operation like MIN or MAX which can
10915 safely be computed in short if both args are promoted shorts.
10916 Also implies COMMON.
10917 -1 indicates a bitwise operation; this makes a difference
10918 in the exact conditions for when it is safe to do the operation
10919 in a narrower mode. */
10920 int shorten = 0;
10922 /* Nonzero if this is a comparison operation;
10923 if both args are promoted shorts, compare the original shorts.
10924 Also implies COMMON. */
10925 int short_compare = 0;
10927 /* Nonzero if this is a right-shift operation, which can be computed on the
10928 original short and then promoted if the operand is a promoted short. */
10929 int short_shift = 0;
10931 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10932 int common = 0;
10934 /* True means types are compatible as far as ObjC is concerned. */
10935 bool objc_ok;
10937 /* True means this is an arithmetic operation that may need excess
10938 precision. */
10939 bool may_need_excess_precision;
10941 /* True means this is a boolean operation that converts both its
10942 operands to truth-values. */
10943 bool boolean_op = false;
10945 /* Remember whether we're doing / or %. */
10946 bool doing_div_or_mod = false;
10948 /* Remember whether we're doing << or >>. */
10949 bool doing_shift = false;
10951 /* Tree holding instrumentation expression. */
10952 tree instrument_expr = NULL;
10954 if (location == UNKNOWN_LOCATION)
10955 location = input_location;
10957 op0 = orig_op0;
10958 op1 = orig_op1;
10960 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10961 if (op0_int_operands)
10962 op0 = remove_c_maybe_const_expr (op0);
10963 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10964 if (op1_int_operands)
10965 op1 = remove_c_maybe_const_expr (op1);
10966 int_operands = (op0_int_operands && op1_int_operands);
10967 if (int_operands)
10969 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10970 && TREE_CODE (orig_op1) == INTEGER_CST);
10971 int_const = (int_const_or_overflow
10972 && !TREE_OVERFLOW (orig_op0)
10973 && !TREE_OVERFLOW (orig_op1));
10975 else
10976 int_const = int_const_or_overflow = false;
10978 /* Do not apply default conversion in mixed vector/scalar expression. */
10979 if (convert_p
10980 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10982 op0 = default_conversion (op0);
10983 op1 = default_conversion (op1);
10986 /* When Cilk Plus is enabled and there are array notations inside op0, then
10987 we check to see if there are builtin array notation functions. If
10988 so, then we take on the type of the array notation inside it. */
10989 if (flag_cilkplus && contains_array_notation_expr (op0))
10990 orig_type0 = type0 = find_correct_array_notation_type (op0);
10991 else
10992 orig_type0 = type0 = TREE_TYPE (op0);
10994 if (flag_cilkplus && contains_array_notation_expr (op1))
10995 orig_type1 = type1 = find_correct_array_notation_type (op1);
10996 else
10997 orig_type1 = type1 = TREE_TYPE (op1);
10999 /* The expression codes of the data types of the arguments tell us
11000 whether the arguments are integers, floating, pointers, etc. */
11001 code0 = TREE_CODE (type0);
11002 code1 = TREE_CODE (type1);
11004 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11005 STRIP_TYPE_NOPS (op0);
11006 STRIP_TYPE_NOPS (op1);
11008 /* If an error was already reported for one of the arguments,
11009 avoid reporting another error. */
11011 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11012 return error_mark_node;
11014 if (code0 == POINTER_TYPE
11015 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11016 return error_mark_node;
11018 if (code1 == POINTER_TYPE
11019 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11020 return error_mark_node;
11022 if ((invalid_op_diag
11023 = targetm.invalid_binary_op (code, type0, type1)))
11025 error_at (location, invalid_op_diag);
11026 return error_mark_node;
11029 switch (code)
11031 case PLUS_EXPR:
11032 case MINUS_EXPR:
11033 case MULT_EXPR:
11034 case TRUNC_DIV_EXPR:
11035 case CEIL_DIV_EXPR:
11036 case FLOOR_DIV_EXPR:
11037 case ROUND_DIV_EXPR:
11038 case EXACT_DIV_EXPR:
11039 may_need_excess_precision = true;
11040 break;
11041 default:
11042 may_need_excess_precision = false;
11043 break;
11045 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11047 op0 = TREE_OPERAND (op0, 0);
11048 type0 = TREE_TYPE (op0);
11050 else if (may_need_excess_precision
11051 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11053 type0 = eptype;
11054 op0 = convert (eptype, op0);
11056 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11058 op1 = TREE_OPERAND (op1, 0);
11059 type1 = TREE_TYPE (op1);
11061 else if (may_need_excess_precision
11062 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11064 type1 = eptype;
11065 op1 = convert (eptype, op1);
11068 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11070 /* In case when one of the operands of the binary operation is
11071 a vector and another is a scalar -- convert scalar to vector. */
11072 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11074 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11075 true);
11077 switch (convert_flag)
11079 case stv_error:
11080 return error_mark_node;
11081 case stv_firstarg:
11083 bool maybe_const = true;
11084 tree sc;
11085 sc = c_fully_fold (op0, false, &maybe_const);
11086 sc = save_expr (sc);
11087 sc = convert (TREE_TYPE (type1), sc);
11088 op0 = build_vector_from_val (type1, sc);
11089 if (!maybe_const)
11090 op0 = c_wrap_maybe_const (op0, true);
11091 orig_type0 = type0 = TREE_TYPE (op0);
11092 code0 = TREE_CODE (type0);
11093 converted = 1;
11094 break;
11096 case stv_secondarg:
11098 bool maybe_const = true;
11099 tree sc;
11100 sc = c_fully_fold (op1, false, &maybe_const);
11101 sc = save_expr (sc);
11102 sc = convert (TREE_TYPE (type0), sc);
11103 op1 = build_vector_from_val (type0, sc);
11104 if (!maybe_const)
11105 op1 = c_wrap_maybe_const (op1, true);
11106 orig_type1 = type1 = TREE_TYPE (op1);
11107 code1 = TREE_CODE (type1);
11108 converted = 1;
11109 break;
11111 default:
11112 break;
11116 switch (code)
11118 case PLUS_EXPR:
11119 /* Handle the pointer + int case. */
11120 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11122 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11123 goto return_build_binary_op;
11125 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11127 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11128 goto return_build_binary_op;
11130 else
11131 common = 1;
11132 break;
11134 case MINUS_EXPR:
11135 /* Subtraction of two similar pointers.
11136 We must subtract them as integers, then divide by object size. */
11137 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11138 && comp_target_types (location, type0, type1))
11140 ret = pointer_diff (location, op0, op1);
11141 goto return_build_binary_op;
11143 /* Handle pointer minus int. Just like pointer plus int. */
11144 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11146 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11147 goto return_build_binary_op;
11149 else
11150 common = 1;
11151 break;
11153 case MULT_EXPR:
11154 common = 1;
11155 break;
11157 case TRUNC_DIV_EXPR:
11158 case CEIL_DIV_EXPR:
11159 case FLOOR_DIV_EXPR:
11160 case ROUND_DIV_EXPR:
11161 case EXACT_DIV_EXPR:
11162 doing_div_or_mod = true;
11163 warn_for_div_by_zero (location, op1);
11165 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11166 || code0 == FIXED_POINT_TYPE
11167 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11168 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11169 || code1 == FIXED_POINT_TYPE
11170 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11172 enum tree_code tcode0 = code0, tcode1 = code1;
11174 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11175 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11176 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11177 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11179 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11180 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11181 resultcode = RDIV_EXPR;
11182 else
11183 /* Although it would be tempting to shorten always here, that
11184 loses on some targets, since the modulo instruction is
11185 undefined if the quotient can't be represented in the
11186 computation mode. We shorten only if unsigned or if
11187 dividing by something we know != -1. */
11188 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11189 || (TREE_CODE (op1) == INTEGER_CST
11190 && !integer_all_onesp (op1)));
11191 common = 1;
11193 break;
11195 case BIT_AND_EXPR:
11196 case BIT_IOR_EXPR:
11197 case BIT_XOR_EXPR:
11198 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11199 shorten = -1;
11200 /* Allow vector types which are not floating point types. */
11201 else if (code0 == VECTOR_TYPE
11202 && code1 == VECTOR_TYPE
11203 && !VECTOR_FLOAT_TYPE_P (type0)
11204 && !VECTOR_FLOAT_TYPE_P (type1))
11205 common = 1;
11206 break;
11208 case TRUNC_MOD_EXPR:
11209 case FLOOR_MOD_EXPR:
11210 doing_div_or_mod = true;
11211 warn_for_div_by_zero (location, op1);
11213 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11214 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11215 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11216 common = 1;
11217 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11219 /* Although it would be tempting to shorten always here, that loses
11220 on some targets, since the modulo instruction is undefined if the
11221 quotient can't be represented in the computation mode. We shorten
11222 only if unsigned or if dividing by something we know != -1. */
11223 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11224 || (TREE_CODE (op1) == INTEGER_CST
11225 && !integer_all_onesp (op1)));
11226 common = 1;
11228 break;
11230 case TRUTH_ANDIF_EXPR:
11231 case TRUTH_ORIF_EXPR:
11232 case TRUTH_AND_EXPR:
11233 case TRUTH_OR_EXPR:
11234 case TRUTH_XOR_EXPR:
11235 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11236 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11237 || code0 == FIXED_POINT_TYPE)
11238 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11239 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11240 || code1 == FIXED_POINT_TYPE))
11242 /* Result of these operations is always an int,
11243 but that does not mean the operands should be
11244 converted to ints! */
11245 result_type = integer_type_node;
11246 if (op0_int_operands)
11248 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11249 op0 = remove_c_maybe_const_expr (op0);
11251 else
11252 op0 = c_objc_common_truthvalue_conversion (location, op0);
11253 if (op1_int_operands)
11255 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11256 op1 = remove_c_maybe_const_expr (op1);
11258 else
11259 op1 = c_objc_common_truthvalue_conversion (location, op1);
11260 converted = 1;
11261 boolean_op = true;
11263 if (code == TRUTH_ANDIF_EXPR)
11265 int_const_or_overflow = (int_operands
11266 && TREE_CODE (orig_op0) == INTEGER_CST
11267 && (op0 == truthvalue_false_node
11268 || TREE_CODE (orig_op1) == INTEGER_CST));
11269 int_const = (int_const_or_overflow
11270 && !TREE_OVERFLOW (orig_op0)
11271 && (op0 == truthvalue_false_node
11272 || !TREE_OVERFLOW (orig_op1)));
11274 else if (code == TRUTH_ORIF_EXPR)
11276 int_const_or_overflow = (int_operands
11277 && TREE_CODE (orig_op0) == INTEGER_CST
11278 && (op0 == truthvalue_true_node
11279 || TREE_CODE (orig_op1) == INTEGER_CST));
11280 int_const = (int_const_or_overflow
11281 && !TREE_OVERFLOW (orig_op0)
11282 && (op0 == truthvalue_true_node
11283 || !TREE_OVERFLOW (orig_op1)));
11285 break;
11287 /* Shift operations: result has same type as first operand;
11288 always convert second operand to int.
11289 Also set SHORT_SHIFT if shifting rightward. */
11291 case RSHIFT_EXPR:
11292 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11293 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11294 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11295 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11297 result_type = type0;
11298 converted = 1;
11300 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11301 || code0 == VECTOR_TYPE)
11302 && code1 == INTEGER_TYPE)
11304 doing_shift = true;
11305 if (TREE_CODE (op1) == INTEGER_CST)
11307 if (tree_int_cst_sgn (op1) < 0)
11309 int_const = false;
11310 if (c_inhibit_evaluation_warnings == 0)
11311 warning_at (location, OPT_Wshift_count_negative,
11312 "right shift count is negative");
11314 else if (code0 == VECTOR_TYPE)
11316 if (compare_tree_int (op1,
11317 TYPE_PRECISION (TREE_TYPE (type0)))
11318 >= 0)
11320 int_const = false;
11321 if (c_inhibit_evaluation_warnings == 0)
11322 warning_at (location, OPT_Wshift_count_overflow,
11323 "right shift count >= width of vector element");
11326 else
11328 if (!integer_zerop (op1))
11329 short_shift = 1;
11331 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11333 int_const = false;
11334 if (c_inhibit_evaluation_warnings == 0)
11335 warning_at (location, OPT_Wshift_count_overflow,
11336 "right shift count >= width of type");
11341 /* Use the type of the value to be shifted. */
11342 result_type = type0;
11343 /* Avoid converting op1 to result_type later. */
11344 converted = 1;
11346 break;
11348 case LSHIFT_EXPR:
11349 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11350 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11351 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11352 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11354 result_type = type0;
11355 converted = 1;
11357 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11358 || code0 == VECTOR_TYPE)
11359 && code1 == INTEGER_TYPE)
11361 doing_shift = true;
11362 if (TREE_CODE (op0) == INTEGER_CST
11363 && tree_int_cst_sgn (op0) < 0)
11365 /* Don't reject a left shift of a negative value in a context
11366 where a constant expression is needed in C90. */
11367 if (flag_isoc99)
11368 int_const = false;
11369 if (c_inhibit_evaluation_warnings == 0)
11370 warning_at (location, OPT_Wshift_negative_value,
11371 "left shift of negative value");
11373 if (TREE_CODE (op1) == INTEGER_CST)
11375 if (tree_int_cst_sgn (op1) < 0)
11377 int_const = false;
11378 if (c_inhibit_evaluation_warnings == 0)
11379 warning_at (location, OPT_Wshift_count_negative,
11380 "left shift count is negative");
11382 else if (code0 == VECTOR_TYPE)
11384 if (compare_tree_int (op1,
11385 TYPE_PRECISION (TREE_TYPE (type0)))
11386 >= 0)
11388 int_const = false;
11389 if (c_inhibit_evaluation_warnings == 0)
11390 warning_at (location, OPT_Wshift_count_overflow,
11391 "left shift count >= width of vector element");
11394 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11396 int_const = false;
11397 if (c_inhibit_evaluation_warnings == 0)
11398 warning_at (location, OPT_Wshift_count_overflow,
11399 "left shift count >= width of type");
11401 else if (TREE_CODE (op0) == INTEGER_CST
11402 && maybe_warn_shift_overflow (location, op0, op1)
11403 && flag_isoc99)
11404 int_const = false;
11407 /* Use the type of the value to be shifted. */
11408 result_type = type0;
11409 /* Avoid converting op1 to result_type later. */
11410 converted = 1;
11412 break;
11414 case EQ_EXPR:
11415 case NE_EXPR:
11416 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11418 tree intt;
11419 if (!vector_types_compatible_elements_p (type0, type1))
11421 error_at (location, "comparing vectors with different "
11422 "element types");
11423 return error_mark_node;
11426 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11428 error_at (location, "comparing vectors with different "
11429 "number of elements");
11430 return error_mark_node;
11433 /* It's not precisely specified how the usual arithmetic
11434 conversions apply to the vector types. Here, we use
11435 the unsigned type if one of the operands is signed and
11436 the other one is unsigned. */
11437 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11439 if (!TYPE_UNSIGNED (type0))
11440 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11441 else
11442 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11443 warning_at (location, OPT_Wsign_compare, "comparison between "
11444 "types %qT and %qT", type0, type1);
11447 /* Always construct signed integer vector type. */
11448 intt = c_common_type_for_size (GET_MODE_BITSIZE
11449 (SCALAR_TYPE_MODE
11450 (TREE_TYPE (type0))), 0);
11451 result_type = build_opaque_vector_type (intt,
11452 TYPE_VECTOR_SUBPARTS (type0));
11453 converted = 1;
11454 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11455 goto return_build_binary_op;
11457 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11458 warning_at (location,
11459 OPT_Wfloat_equal,
11460 "comparing floating point with == or != is unsafe");
11461 /* Result of comparison is always int,
11462 but don't convert the args to int! */
11463 build_type = integer_type_node;
11464 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11465 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11466 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11467 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11468 short_compare = 1;
11469 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11471 if (TREE_CODE (op0) == ADDR_EXPR
11472 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11473 && !from_macro_expansion_at (location))
11475 if (code == EQ_EXPR)
11476 warning_at (location,
11477 OPT_Waddress,
11478 "the comparison will always evaluate as %<false%> "
11479 "for the address of %qD will never be NULL",
11480 TREE_OPERAND (op0, 0));
11481 else
11482 warning_at (location,
11483 OPT_Waddress,
11484 "the comparison will always evaluate as %<true%> "
11485 "for the address of %qD will never be NULL",
11486 TREE_OPERAND (op0, 0));
11488 result_type = type0;
11490 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11492 if (TREE_CODE (op1) == ADDR_EXPR
11493 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11494 && !from_macro_expansion_at (location))
11496 if (code == EQ_EXPR)
11497 warning_at (location,
11498 OPT_Waddress,
11499 "the comparison will always evaluate as %<false%> "
11500 "for the address of %qD will never be NULL",
11501 TREE_OPERAND (op1, 0));
11502 else
11503 warning_at (location,
11504 OPT_Waddress,
11505 "the comparison will always evaluate as %<true%> "
11506 "for the address of %qD will never be NULL",
11507 TREE_OPERAND (op1, 0));
11509 result_type = type1;
11511 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11513 tree tt0 = TREE_TYPE (type0);
11514 tree tt1 = TREE_TYPE (type1);
11515 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11516 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11517 addr_space_t as_common = ADDR_SPACE_GENERIC;
11519 /* Anything compares with void *. void * compares with anything.
11520 Otherwise, the targets must be compatible
11521 and both must be object or both incomplete. */
11522 if (comp_target_types (location, type0, type1))
11523 result_type = common_pointer_type (type0, type1);
11524 else if (!addr_space_superset (as0, as1, &as_common))
11526 error_at (location, "comparison of pointers to "
11527 "disjoint address spaces");
11528 return error_mark_node;
11530 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11532 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11533 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11534 "comparison of %<void *%> with function pointer");
11536 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11538 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11539 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11540 "comparison of %<void *%> with function pointer");
11542 else
11543 /* Avoid warning about the volatile ObjC EH puts on decls. */
11544 if (!objc_ok)
11545 pedwarn (location, 0,
11546 "comparison of distinct pointer types lacks a cast");
11548 if (result_type == NULL_TREE)
11550 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11551 result_type = build_pointer_type
11552 (build_qualified_type (void_type_node, qual));
11555 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11557 result_type = type0;
11558 pedwarn (location, 0, "comparison between pointer and integer");
11560 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11562 result_type = type1;
11563 pedwarn (location, 0, "comparison between pointer and integer");
11565 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11566 || truth_value_p (TREE_CODE (orig_op0)))
11567 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11568 || truth_value_p (TREE_CODE (orig_op1))))
11569 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11570 break;
11572 case LE_EXPR:
11573 case GE_EXPR:
11574 case LT_EXPR:
11575 case GT_EXPR:
11576 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11578 tree intt;
11579 if (!vector_types_compatible_elements_p (type0, type1))
11581 error_at (location, "comparing vectors with different "
11582 "element types");
11583 return error_mark_node;
11586 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11588 error_at (location, "comparing vectors with different "
11589 "number of elements");
11590 return error_mark_node;
11593 /* It's not precisely specified how the usual arithmetic
11594 conversions apply to the vector types. Here, we use
11595 the unsigned type if one of the operands is signed and
11596 the other one is unsigned. */
11597 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11599 if (!TYPE_UNSIGNED (type0))
11600 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11601 else
11602 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11603 warning_at (location, OPT_Wsign_compare, "comparison between "
11604 "types %qT and %qT", type0, type1);
11607 /* Always construct signed integer vector type. */
11608 intt = c_common_type_for_size (GET_MODE_BITSIZE
11609 (SCALAR_TYPE_MODE
11610 (TREE_TYPE (type0))), 0);
11611 result_type = build_opaque_vector_type (intt,
11612 TYPE_VECTOR_SUBPARTS (type0));
11613 converted = 1;
11614 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11615 goto return_build_binary_op;
11617 build_type = integer_type_node;
11618 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11619 || code0 == FIXED_POINT_TYPE)
11620 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11621 || code1 == FIXED_POINT_TYPE))
11622 short_compare = 1;
11623 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11625 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11626 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11627 addr_space_t as_common;
11629 if (comp_target_types (location, type0, type1))
11631 result_type = common_pointer_type (type0, type1);
11632 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11633 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11634 pedwarn (location, 0,
11635 "comparison of complete and incomplete pointers");
11636 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11637 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11638 "ordered comparisons of pointers to functions");
11639 else if (null_pointer_constant_p (orig_op0)
11640 || null_pointer_constant_p (orig_op1))
11641 warning_at (location, OPT_Wextra,
11642 "ordered comparison of pointer with null pointer");
11645 else if (!addr_space_superset (as0, as1, &as_common))
11647 error_at (location, "comparison of pointers to "
11648 "disjoint address spaces");
11649 return error_mark_node;
11651 else
11653 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11654 result_type = build_pointer_type
11655 (build_qualified_type (void_type_node, qual));
11656 pedwarn (location, 0,
11657 "comparison of distinct pointer types lacks a cast");
11660 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11662 result_type = type0;
11663 if (pedantic)
11664 pedwarn (location, OPT_Wpedantic,
11665 "ordered comparison of pointer with integer zero");
11666 else if (extra_warnings)
11667 warning_at (location, OPT_Wextra,
11668 "ordered comparison of pointer with integer zero");
11670 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11672 result_type = type1;
11673 if (pedantic)
11674 pedwarn (location, OPT_Wpedantic,
11675 "ordered comparison of pointer with integer zero");
11676 else if (extra_warnings)
11677 warning_at (location, OPT_Wextra,
11678 "ordered comparison of pointer with integer zero");
11680 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11682 result_type = type0;
11683 pedwarn (location, 0, "comparison between pointer and integer");
11685 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11687 result_type = type1;
11688 pedwarn (location, 0, "comparison between pointer and integer");
11690 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11691 || truth_value_p (TREE_CODE (orig_op0)))
11692 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11693 || truth_value_p (TREE_CODE (orig_op1))))
11694 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11695 break;
11697 default:
11698 gcc_unreachable ();
11701 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11702 return error_mark_node;
11704 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11705 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11706 || !vector_types_compatible_elements_p (type0, type1)))
11708 gcc_rich_location richloc (location);
11709 richloc.maybe_add_expr (orig_op0);
11710 richloc.maybe_add_expr (orig_op1);
11711 binary_op_error (&richloc, code, type0, type1);
11712 return error_mark_node;
11715 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11716 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11718 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11719 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11721 bool first_complex = (code0 == COMPLEX_TYPE);
11722 bool second_complex = (code1 == COMPLEX_TYPE);
11723 int none_complex = (!first_complex && !second_complex);
11725 if (shorten || common || short_compare)
11727 result_type = c_common_type (type0, type1);
11728 do_warn_double_promotion (result_type, type0, type1,
11729 "implicit conversion from %qT to %qT "
11730 "to match other operand of binary "
11731 "expression",
11732 location);
11733 if (result_type == error_mark_node)
11734 return error_mark_node;
11737 if (first_complex != second_complex
11738 && (code == PLUS_EXPR
11739 || code == MINUS_EXPR
11740 || code == MULT_EXPR
11741 || (code == TRUNC_DIV_EXPR && first_complex))
11742 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11743 && flag_signed_zeros)
11745 /* An operation on mixed real/complex operands must be
11746 handled specially, but the language-independent code can
11747 more easily optimize the plain complex arithmetic if
11748 -fno-signed-zeros. */
11749 tree real_type = TREE_TYPE (result_type);
11750 tree real, imag;
11751 if (type0 != orig_type0 || type1 != orig_type1)
11753 gcc_assert (may_need_excess_precision && common);
11754 semantic_result_type = c_common_type (orig_type0, orig_type1);
11756 if (first_complex)
11758 if (TREE_TYPE (op0) != result_type)
11759 op0 = convert_and_check (location, result_type, op0);
11760 if (TREE_TYPE (op1) != real_type)
11761 op1 = convert_and_check (location, real_type, op1);
11763 else
11765 if (TREE_TYPE (op0) != real_type)
11766 op0 = convert_and_check (location, real_type, op0);
11767 if (TREE_TYPE (op1) != result_type)
11768 op1 = convert_and_check (location, result_type, op1);
11770 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11771 return error_mark_node;
11772 if (first_complex)
11774 op0 = save_expr (op0);
11775 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11776 op0, true);
11777 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11778 op0, true);
11779 switch (code)
11781 case MULT_EXPR:
11782 case TRUNC_DIV_EXPR:
11783 op1 = save_expr (op1);
11784 imag = build2 (resultcode, real_type, imag, op1);
11785 /* Fall through. */
11786 case PLUS_EXPR:
11787 case MINUS_EXPR:
11788 real = build2 (resultcode, real_type, real, op1);
11789 break;
11790 default:
11791 gcc_unreachable();
11794 else
11796 op1 = save_expr (op1);
11797 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11798 op1, true);
11799 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11800 op1, true);
11801 switch (code)
11803 case MULT_EXPR:
11804 op0 = save_expr (op0);
11805 imag = build2 (resultcode, real_type, op0, imag);
11806 /* Fall through. */
11807 case PLUS_EXPR:
11808 real = build2 (resultcode, real_type, op0, real);
11809 break;
11810 case MINUS_EXPR:
11811 real = build2 (resultcode, real_type, op0, real);
11812 imag = build1 (NEGATE_EXPR, real_type, imag);
11813 break;
11814 default:
11815 gcc_unreachable();
11818 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11819 goto return_build_binary_op;
11822 /* For certain operations (which identify themselves by shorten != 0)
11823 if both args were extended from the same smaller type,
11824 do the arithmetic in that type and then extend.
11826 shorten !=0 and !=1 indicates a bitwise operation.
11827 For them, this optimization is safe only if
11828 both args are zero-extended or both are sign-extended.
11829 Otherwise, we might change the result.
11830 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11831 but calculated in (unsigned short) it would be (unsigned short)-1. */
11833 if (shorten && none_complex)
11835 final_type = result_type;
11836 result_type = shorten_binary_op (result_type, op0, op1,
11837 shorten == -1);
11840 /* Shifts can be shortened if shifting right. */
11842 if (short_shift)
11844 int unsigned_arg;
11845 tree arg0 = get_narrower (op0, &unsigned_arg);
11847 final_type = result_type;
11849 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11850 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11852 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11853 && tree_int_cst_sgn (op1) > 0
11854 /* We can shorten only if the shift count is less than the
11855 number of bits in the smaller type size. */
11856 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11857 /* We cannot drop an unsigned shift after sign-extension. */
11858 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11860 /* Do an unsigned shift if the operand was zero-extended. */
11861 result_type
11862 = c_common_signed_or_unsigned_type (unsigned_arg,
11863 TREE_TYPE (arg0));
11864 /* Convert value-to-be-shifted to that type. */
11865 if (TREE_TYPE (op0) != result_type)
11866 op0 = convert (result_type, op0);
11867 converted = 1;
11871 /* Comparison operations are shortened too but differently.
11872 They identify themselves by setting short_compare = 1. */
11874 if (short_compare)
11876 /* Don't write &op0, etc., because that would prevent op0
11877 from being kept in a register.
11878 Instead, make copies of the our local variables and
11879 pass the copies by reference, then copy them back afterward. */
11880 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11881 enum tree_code xresultcode = resultcode;
11882 tree val
11883 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11884 &xresultcode);
11886 if (val != NULL_TREE)
11888 ret = val;
11889 goto return_build_binary_op;
11892 op0 = xop0, op1 = xop1;
11893 converted = 1;
11894 resultcode = xresultcode;
11896 if (c_inhibit_evaluation_warnings == 0)
11898 bool op0_maybe_const = true;
11899 bool op1_maybe_const = true;
11900 tree orig_op0_folded, orig_op1_folded;
11902 if (in_late_binary_op)
11904 orig_op0_folded = orig_op0;
11905 orig_op1_folded = orig_op1;
11907 else
11909 /* Fold for the sake of possible warnings, as in
11910 build_conditional_expr. This requires the
11911 "original" values to be folded, not just op0 and
11912 op1. */
11913 c_inhibit_evaluation_warnings++;
11914 op0 = c_fully_fold (op0, require_constant_value,
11915 &op0_maybe_const);
11916 op1 = c_fully_fold (op1, require_constant_value,
11917 &op1_maybe_const);
11918 c_inhibit_evaluation_warnings--;
11919 orig_op0_folded = c_fully_fold (orig_op0,
11920 require_constant_value,
11921 NULL);
11922 orig_op1_folded = c_fully_fold (orig_op1,
11923 require_constant_value,
11924 NULL);
11927 if (warn_sign_compare)
11928 warn_for_sign_compare (location, orig_op0_folded,
11929 orig_op1_folded, op0, op1,
11930 result_type, resultcode);
11931 if (!in_late_binary_op && !int_operands)
11933 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11934 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11935 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11936 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11942 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11943 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11944 Then the expression will be built.
11945 It will be given type FINAL_TYPE if that is nonzero;
11946 otherwise, it will be given type RESULT_TYPE. */
11948 if (!result_type)
11950 gcc_rich_location richloc (location);
11951 richloc.maybe_add_expr (orig_op0);
11952 richloc.maybe_add_expr (orig_op1);
11953 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11954 return error_mark_node;
11957 if (build_type == NULL_TREE)
11959 build_type = result_type;
11960 if ((type0 != orig_type0 || type1 != orig_type1)
11961 && !boolean_op)
11963 gcc_assert (may_need_excess_precision && common);
11964 semantic_result_type = c_common_type (orig_type0, orig_type1);
11968 if (!converted)
11970 op0 = ep_convert_and_check (location, result_type, op0,
11971 semantic_result_type);
11972 op1 = ep_convert_and_check (location, result_type, op1,
11973 semantic_result_type);
11975 /* This can happen if one operand has a vector type, and the other
11976 has a different type. */
11977 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11978 return error_mark_node;
11981 if (sanitize_flags_p ((SANITIZE_SHIFT
11982 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
11983 && current_function_decl != NULL_TREE
11984 && (doing_div_or_mod || doing_shift)
11985 && !require_constant_value)
11987 /* OP0 and/or OP1 might have side-effects. */
11988 op0 = save_expr (op0);
11989 op1 = save_expr (op1);
11990 op0 = c_fully_fold (op0, false, NULL);
11991 op1 = c_fully_fold (op1, false, NULL);
11992 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
11993 | SANITIZE_FLOAT_DIVIDE))))
11994 instrument_expr = ubsan_instrument_division (location, op0, op1);
11995 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
11996 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11999 /* Treat expressions in initializers specially as they can't trap. */
12000 if (int_const_or_overflow)
12001 ret = (require_constant_value
12002 ? fold_build2_initializer_loc (location, resultcode, build_type,
12003 op0, op1)
12004 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12005 else
12006 ret = build2 (resultcode, build_type, op0, op1);
12007 if (final_type != NULL_TREE)
12008 ret = convert (final_type, ret);
12010 return_build_binary_op:
12011 gcc_assert (ret != error_mark_node);
12012 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12013 ret = (int_operands
12014 ? note_integer_operands (ret)
12015 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12016 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12017 && !in_late_binary_op)
12018 ret = note_integer_operands (ret);
12019 protected_set_expr_location (ret, location);
12021 if (instrument_expr != NULL)
12022 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12023 instrument_expr, ret);
12025 if (semantic_result_type)
12026 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12027 semantic_result_type, ret);
12029 return ret;
12033 /* Convert EXPR to be a truth-value, validating its type for this
12034 purpose. LOCATION is the source location for the expression. */
12036 tree
12037 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12039 bool int_const, int_operands;
12041 switch (TREE_CODE (TREE_TYPE (expr)))
12043 case ARRAY_TYPE:
12044 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12045 return error_mark_node;
12047 case RECORD_TYPE:
12048 error_at (location, "used struct type value where scalar is required");
12049 return error_mark_node;
12051 case UNION_TYPE:
12052 error_at (location, "used union type value where scalar is required");
12053 return error_mark_node;
12055 case VOID_TYPE:
12056 error_at (location, "void value not ignored as it ought to be");
12057 return error_mark_node;
12059 case POINTER_TYPE:
12060 if (reject_gcc_builtin (expr))
12061 return error_mark_node;
12062 break;
12064 case FUNCTION_TYPE:
12065 gcc_unreachable ();
12067 case VECTOR_TYPE:
12068 error_at (location, "used vector type where scalar is required");
12069 return error_mark_node;
12071 default:
12072 break;
12075 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12076 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12077 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12079 expr = remove_c_maybe_const_expr (expr);
12080 expr = build2 (NE_EXPR, integer_type_node, expr,
12081 convert (TREE_TYPE (expr), integer_zero_node));
12082 expr = note_integer_operands (expr);
12084 else
12085 /* ??? Should we also give an error for vectors rather than leaving
12086 those to give errors later? */
12087 expr = c_common_truthvalue_conversion (location, expr);
12089 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12091 if (TREE_OVERFLOW (expr))
12092 return expr;
12093 else
12094 return note_integer_operands (expr);
12096 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12097 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12098 return expr;
12102 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12103 required. */
12105 tree
12106 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12108 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12110 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12111 /* Executing a compound literal inside a function reinitializes
12112 it. */
12113 if (!TREE_STATIC (decl))
12114 *se = true;
12115 return decl;
12117 else
12118 return expr;
12121 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12122 statement. LOC is the location of the construct. */
12124 tree
12125 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12126 tree clauses)
12128 body = c_end_compound_stmt (loc, body, true);
12130 tree stmt = make_node (code);
12131 TREE_TYPE (stmt) = void_type_node;
12132 OMP_BODY (stmt) = body;
12133 OMP_CLAUSES (stmt) = clauses;
12134 SET_EXPR_LOCATION (stmt, loc);
12136 return add_stmt (stmt);
12139 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12140 statement. LOC is the location of the OACC_DATA. */
12142 tree
12143 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12145 tree stmt;
12147 block = c_end_compound_stmt (loc, block, true);
12149 stmt = make_node (OACC_DATA);
12150 TREE_TYPE (stmt) = void_type_node;
12151 OACC_DATA_CLAUSES (stmt) = clauses;
12152 OACC_DATA_BODY (stmt) = block;
12153 SET_EXPR_LOCATION (stmt, loc);
12155 return add_stmt (stmt);
12158 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12159 statement. LOC is the location of the OACC_HOST_DATA. */
12161 tree
12162 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12164 tree stmt;
12166 block = c_end_compound_stmt (loc, block, true);
12168 stmt = make_node (OACC_HOST_DATA);
12169 TREE_TYPE (stmt) = void_type_node;
12170 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12171 OACC_HOST_DATA_BODY (stmt) = block;
12172 SET_EXPR_LOCATION (stmt, loc);
12174 return add_stmt (stmt);
12177 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12179 tree
12180 c_begin_omp_parallel (void)
12182 tree block;
12184 keep_next_level ();
12185 block = c_begin_compound_stmt (true);
12187 return block;
12190 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12191 statement. LOC is the location of the OMP_PARALLEL. */
12193 tree
12194 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12196 tree stmt;
12198 block = c_end_compound_stmt (loc, block, true);
12200 stmt = make_node (OMP_PARALLEL);
12201 TREE_TYPE (stmt) = void_type_node;
12202 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12203 OMP_PARALLEL_BODY (stmt) = block;
12204 SET_EXPR_LOCATION (stmt, loc);
12206 return add_stmt (stmt);
12209 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12211 tree
12212 c_begin_omp_task (void)
12214 tree block;
12216 keep_next_level ();
12217 block = c_begin_compound_stmt (true);
12219 return block;
12222 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12223 statement. LOC is the location of the #pragma. */
12225 tree
12226 c_finish_omp_task (location_t loc, tree clauses, tree block)
12228 tree stmt;
12230 block = c_end_compound_stmt (loc, block, true);
12232 stmt = make_node (OMP_TASK);
12233 TREE_TYPE (stmt) = void_type_node;
12234 OMP_TASK_CLAUSES (stmt) = clauses;
12235 OMP_TASK_BODY (stmt) = block;
12236 SET_EXPR_LOCATION (stmt, loc);
12238 return add_stmt (stmt);
12241 /* Generate GOMP_cancel call for #pragma omp cancel. */
12243 void
12244 c_finish_omp_cancel (location_t loc, tree clauses)
12246 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12247 int mask = 0;
12248 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12249 mask = 1;
12250 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12251 mask = 2;
12252 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12253 mask = 4;
12254 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12255 mask = 8;
12256 else
12258 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12259 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12260 "clauses");
12261 return;
12263 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12264 if (ifc != NULL_TREE)
12266 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12267 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12268 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12269 build_zero_cst (type));
12271 else
12272 ifc = boolean_true_node;
12273 tree stmt = build_call_expr_loc (loc, fn, 2,
12274 build_int_cst (integer_type_node, mask),
12275 ifc);
12276 add_stmt (stmt);
12279 /* Generate GOMP_cancellation_point call for
12280 #pragma omp cancellation point. */
12282 void
12283 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12285 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12286 int mask = 0;
12287 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12288 mask = 1;
12289 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12290 mask = 2;
12291 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12292 mask = 4;
12293 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12294 mask = 8;
12295 else
12297 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12298 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12299 "clauses");
12300 return;
12302 tree stmt = build_call_expr_loc (loc, fn, 1,
12303 build_int_cst (integer_type_node, mask));
12304 add_stmt (stmt);
12307 /* Helper function for handle_omp_array_sections. Called recursively
12308 to handle multiple array-section-subscripts. C is the clause,
12309 T current expression (initially OMP_CLAUSE_DECL), which is either
12310 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12311 expression if specified, TREE_VALUE length expression if specified,
12312 TREE_CHAIN is what it has been specified after, or some decl.
12313 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12314 set to true if any of the array-section-subscript could have length
12315 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12316 first array-section-subscript which is known not to have length
12317 of one. Given say:
12318 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12319 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12320 all are or may have length of 1, array-section-subscript [:2] is the
12321 first one known not to have length 1. For array-section-subscript
12322 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12323 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12324 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12325 case though, as some lengths could be zero. */
12327 static tree
12328 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12329 bool &maybe_zero_len, unsigned int &first_non_one,
12330 enum c_omp_region_type ort)
12332 tree ret, low_bound, length, type;
12333 if (TREE_CODE (t) != TREE_LIST)
12335 if (error_operand_p (t))
12336 return error_mark_node;
12337 ret = t;
12338 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12339 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12341 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12342 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12343 return error_mark_node;
12345 if (TREE_CODE (t) == COMPONENT_REF
12346 && ort == C_ORT_OMP
12347 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12348 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12349 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12351 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12353 error_at (OMP_CLAUSE_LOCATION (c),
12354 "bit-field %qE in %qs clause",
12355 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12356 return error_mark_node;
12358 while (TREE_CODE (t) == COMPONENT_REF)
12360 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12362 error_at (OMP_CLAUSE_LOCATION (c),
12363 "%qE is a member of a union", t);
12364 return error_mark_node;
12366 t = TREE_OPERAND (t, 0);
12369 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12371 if (DECL_P (t))
12372 error_at (OMP_CLAUSE_LOCATION (c),
12373 "%qD is not a variable in %qs clause", t,
12374 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12375 else
12376 error_at (OMP_CLAUSE_LOCATION (c),
12377 "%qE is not a variable in %qs clause", t,
12378 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12379 return error_mark_node;
12381 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12382 && TYPE_ATOMIC (TREE_TYPE (t)))
12384 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12385 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12386 return error_mark_node;
12388 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12389 && VAR_P (t)
12390 && DECL_THREAD_LOCAL_P (t))
12392 error_at (OMP_CLAUSE_LOCATION (c),
12393 "%qD is threadprivate variable in %qs clause", t,
12394 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12395 return error_mark_node;
12397 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12398 && TYPE_ATOMIC (TREE_TYPE (t))
12399 && POINTER_TYPE_P (TREE_TYPE (t)))
12401 /* If the array section is pointer based and the pointer
12402 itself is _Atomic qualified, we need to atomically load
12403 the pointer. */
12404 c_expr expr;
12405 memset (&expr, 0, sizeof (expr));
12406 expr.value = ret;
12407 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12408 expr, false, false);
12409 ret = expr.value;
12411 return ret;
12414 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12415 maybe_zero_len, first_non_one, ort);
12416 if (ret == error_mark_node || ret == NULL_TREE)
12417 return ret;
12419 type = TREE_TYPE (ret);
12420 low_bound = TREE_PURPOSE (t);
12421 length = TREE_VALUE (t);
12423 if (low_bound == error_mark_node || length == error_mark_node)
12424 return error_mark_node;
12426 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12428 error_at (OMP_CLAUSE_LOCATION (c),
12429 "low bound %qE of array section does not have integral type",
12430 low_bound);
12431 return error_mark_node;
12433 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12435 error_at (OMP_CLAUSE_LOCATION (c),
12436 "length %qE of array section does not have integral type",
12437 length);
12438 return error_mark_node;
12440 if (low_bound
12441 && TREE_CODE (low_bound) == INTEGER_CST
12442 && TYPE_PRECISION (TREE_TYPE (low_bound))
12443 > TYPE_PRECISION (sizetype))
12444 low_bound = fold_convert (sizetype, low_bound);
12445 if (length
12446 && TREE_CODE (length) == INTEGER_CST
12447 && TYPE_PRECISION (TREE_TYPE (length))
12448 > TYPE_PRECISION (sizetype))
12449 length = fold_convert (sizetype, length);
12450 if (low_bound == NULL_TREE)
12451 low_bound = integer_zero_node;
12453 if (length != NULL_TREE)
12455 if (!integer_nonzerop (length))
12457 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12458 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12460 if (integer_zerop (length))
12462 error_at (OMP_CLAUSE_LOCATION (c),
12463 "zero length array section in %qs clause",
12464 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12465 return error_mark_node;
12468 else
12469 maybe_zero_len = true;
12471 if (first_non_one == types.length ()
12472 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12473 first_non_one++;
12475 if (TREE_CODE (type) == ARRAY_TYPE)
12477 if (length == NULL_TREE
12478 && (TYPE_DOMAIN (type) == NULL_TREE
12479 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12481 error_at (OMP_CLAUSE_LOCATION (c),
12482 "for unknown bound array type length expression must "
12483 "be specified");
12484 return error_mark_node;
12486 if (TREE_CODE (low_bound) == INTEGER_CST
12487 && tree_int_cst_sgn (low_bound) == -1)
12489 error_at (OMP_CLAUSE_LOCATION (c),
12490 "negative low bound in array section in %qs clause",
12491 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12492 return error_mark_node;
12494 if (length != NULL_TREE
12495 && TREE_CODE (length) == INTEGER_CST
12496 && tree_int_cst_sgn (length) == -1)
12498 error_at (OMP_CLAUSE_LOCATION (c),
12499 "negative length in array section in %qs clause",
12500 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12501 return error_mark_node;
12503 if (TYPE_DOMAIN (type)
12504 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12505 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12506 == INTEGER_CST)
12508 tree size
12509 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12510 size = size_binop (PLUS_EXPR, size, size_one_node);
12511 if (TREE_CODE (low_bound) == INTEGER_CST)
12513 if (tree_int_cst_lt (size, low_bound))
12515 error_at (OMP_CLAUSE_LOCATION (c),
12516 "low bound %qE above array section size "
12517 "in %qs clause", low_bound,
12518 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12519 return error_mark_node;
12521 if (tree_int_cst_equal (size, low_bound))
12523 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12524 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12526 error_at (OMP_CLAUSE_LOCATION (c),
12527 "zero length array section in %qs clause",
12528 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12529 return error_mark_node;
12531 maybe_zero_len = true;
12533 else if (length == NULL_TREE
12534 && first_non_one == types.length ()
12535 && tree_int_cst_equal
12536 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12537 low_bound))
12538 first_non_one++;
12540 else if (length == NULL_TREE)
12542 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12543 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12544 maybe_zero_len = true;
12545 if (first_non_one == types.length ())
12546 first_non_one++;
12548 if (length && TREE_CODE (length) == INTEGER_CST)
12550 if (tree_int_cst_lt (size, length))
12552 error_at (OMP_CLAUSE_LOCATION (c),
12553 "length %qE above array section size "
12554 "in %qs clause", length,
12555 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12556 return error_mark_node;
12558 if (TREE_CODE (low_bound) == INTEGER_CST)
12560 tree lbpluslen
12561 = size_binop (PLUS_EXPR,
12562 fold_convert (sizetype, low_bound),
12563 fold_convert (sizetype, length));
12564 if (TREE_CODE (lbpluslen) == INTEGER_CST
12565 && tree_int_cst_lt (size, lbpluslen))
12567 error_at (OMP_CLAUSE_LOCATION (c),
12568 "high bound %qE above array section size "
12569 "in %qs clause", lbpluslen,
12570 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12571 return error_mark_node;
12576 else if (length == NULL_TREE)
12578 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12579 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12580 maybe_zero_len = true;
12581 if (first_non_one == types.length ())
12582 first_non_one++;
12585 /* For [lb:] we will need to evaluate lb more than once. */
12586 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12588 tree lb = save_expr (low_bound);
12589 if (lb != low_bound)
12591 TREE_PURPOSE (t) = lb;
12592 low_bound = lb;
12596 else if (TREE_CODE (type) == POINTER_TYPE)
12598 if (length == NULL_TREE)
12600 error_at (OMP_CLAUSE_LOCATION (c),
12601 "for pointer type length expression must be specified");
12602 return error_mark_node;
12604 if (length != NULL_TREE
12605 && TREE_CODE (length) == INTEGER_CST
12606 && tree_int_cst_sgn (length) == -1)
12608 error_at (OMP_CLAUSE_LOCATION (c),
12609 "negative length in array section in %qs clause",
12610 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12611 return error_mark_node;
12613 /* If there is a pointer type anywhere but in the very first
12614 array-section-subscript, the array section can't be contiguous. */
12615 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12616 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12618 error_at (OMP_CLAUSE_LOCATION (c),
12619 "array section is not contiguous in %qs clause",
12620 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12621 return error_mark_node;
12624 else
12626 error_at (OMP_CLAUSE_LOCATION (c),
12627 "%qE does not have pointer or array type", ret);
12628 return error_mark_node;
12630 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12631 types.safe_push (TREE_TYPE (ret));
12632 /* We will need to evaluate lb more than once. */
12633 tree lb = save_expr (low_bound);
12634 if (lb != low_bound)
12636 TREE_PURPOSE (t) = lb;
12637 low_bound = lb;
12639 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12640 return ret;
12643 /* Handle array sections for clause C. */
12645 static bool
12646 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12648 bool maybe_zero_len = false;
12649 unsigned int first_non_one = 0;
12650 auto_vec<tree, 10> types;
12651 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12652 maybe_zero_len, first_non_one,
12653 ort);
12654 if (first == error_mark_node)
12655 return true;
12656 if (first == NULL_TREE)
12657 return false;
12658 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12660 tree t = OMP_CLAUSE_DECL (c);
12661 tree tem = NULL_TREE;
12662 /* Need to evaluate side effects in the length expressions
12663 if any. */
12664 while (TREE_CODE (t) == TREE_LIST)
12666 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12668 if (tem == NULL_TREE)
12669 tem = TREE_VALUE (t);
12670 else
12671 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12672 TREE_VALUE (t), tem);
12674 t = TREE_CHAIN (t);
12676 if (tem)
12677 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12678 first = c_fully_fold (first, false, NULL);
12679 OMP_CLAUSE_DECL (c) = first;
12681 else
12683 unsigned int num = types.length (), i;
12684 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12685 tree condition = NULL_TREE;
12687 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12688 maybe_zero_len = true;
12690 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12691 t = TREE_CHAIN (t))
12693 tree low_bound = TREE_PURPOSE (t);
12694 tree length = TREE_VALUE (t);
12696 i--;
12697 if (low_bound
12698 && TREE_CODE (low_bound) == INTEGER_CST
12699 && TYPE_PRECISION (TREE_TYPE (low_bound))
12700 > TYPE_PRECISION (sizetype))
12701 low_bound = fold_convert (sizetype, low_bound);
12702 if (length
12703 && TREE_CODE (length) == INTEGER_CST
12704 && TYPE_PRECISION (TREE_TYPE (length))
12705 > TYPE_PRECISION (sizetype))
12706 length = fold_convert (sizetype, length);
12707 if (low_bound == NULL_TREE)
12708 low_bound = integer_zero_node;
12709 if (!maybe_zero_len && i > first_non_one)
12711 if (integer_nonzerop (low_bound))
12712 goto do_warn_noncontiguous;
12713 if (length != NULL_TREE
12714 && TREE_CODE (length) == INTEGER_CST
12715 && TYPE_DOMAIN (types[i])
12716 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12717 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12718 == INTEGER_CST)
12720 tree size;
12721 size = size_binop (PLUS_EXPR,
12722 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12723 size_one_node);
12724 if (!tree_int_cst_equal (length, size))
12726 do_warn_noncontiguous:
12727 error_at (OMP_CLAUSE_LOCATION (c),
12728 "array section is not contiguous in %qs "
12729 "clause",
12730 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12731 return true;
12734 if (length != NULL_TREE
12735 && TREE_SIDE_EFFECTS (length))
12737 if (side_effects == NULL_TREE)
12738 side_effects = length;
12739 else
12740 side_effects = build2 (COMPOUND_EXPR,
12741 TREE_TYPE (side_effects),
12742 length, side_effects);
12745 else
12747 tree l;
12749 if (i > first_non_one
12750 && ((length && integer_nonzerop (length))
12751 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12752 continue;
12753 if (length)
12754 l = fold_convert (sizetype, length);
12755 else
12757 l = size_binop (PLUS_EXPR,
12758 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12759 size_one_node);
12760 l = size_binop (MINUS_EXPR, l,
12761 fold_convert (sizetype, low_bound));
12763 if (i > first_non_one)
12765 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12766 size_zero_node);
12767 if (condition == NULL_TREE)
12768 condition = l;
12769 else
12770 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12771 l, condition);
12773 else if (size == NULL_TREE)
12775 size = size_in_bytes (TREE_TYPE (types[i]));
12776 tree eltype = TREE_TYPE (types[num - 1]);
12777 while (TREE_CODE (eltype) == ARRAY_TYPE)
12778 eltype = TREE_TYPE (eltype);
12779 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12781 if (integer_zerop (size)
12782 || integer_zerop (size_in_bytes (eltype)))
12784 error_at (OMP_CLAUSE_LOCATION (c),
12785 "zero length array section in %qs clause",
12786 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12787 return error_mark_node;
12789 size = size_binop (EXACT_DIV_EXPR, size,
12790 size_in_bytes (eltype));
12792 size = size_binop (MULT_EXPR, size, l);
12793 if (condition)
12794 size = fold_build3 (COND_EXPR, sizetype, condition,
12795 size, size_zero_node);
12797 else
12798 size = size_binop (MULT_EXPR, size, l);
12801 if (side_effects)
12802 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12803 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12805 size = size_binop (MINUS_EXPR, size, size_one_node);
12806 size = c_fully_fold (size, false, NULL);
12807 tree index_type = build_index_type (size);
12808 tree eltype = TREE_TYPE (first);
12809 while (TREE_CODE (eltype) == ARRAY_TYPE)
12810 eltype = TREE_TYPE (eltype);
12811 tree type = build_array_type (eltype, index_type);
12812 tree ptype = build_pointer_type (eltype);
12813 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12814 t = build_fold_addr_expr (t);
12815 tree t2 = build_fold_addr_expr (first);
12816 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12817 ptrdiff_type_node, t2);
12818 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12819 ptrdiff_type_node, t2,
12820 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12821 ptrdiff_type_node, t));
12822 t2 = c_fully_fold (t2, false, NULL);
12823 if (tree_fits_shwi_p (t2))
12824 t = build2 (MEM_REF, type, t,
12825 build_int_cst (ptype, tree_to_shwi (t2)));
12826 else
12828 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12829 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12830 TREE_TYPE (t), t, t2);
12831 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12833 OMP_CLAUSE_DECL (c) = t;
12834 return false;
12836 first = c_fully_fold (first, false, NULL);
12837 OMP_CLAUSE_DECL (c) = first;
12838 if (size)
12839 size = c_fully_fold (size, false, NULL);
12840 OMP_CLAUSE_SIZE (c) = size;
12841 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12842 || (TREE_CODE (t) == COMPONENT_REF
12843 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12844 return false;
12845 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12846 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12847 switch (OMP_CLAUSE_MAP_KIND (c))
12849 case GOMP_MAP_ALLOC:
12850 case GOMP_MAP_TO:
12851 case GOMP_MAP_FROM:
12852 case GOMP_MAP_TOFROM:
12853 case GOMP_MAP_ALWAYS_TO:
12854 case GOMP_MAP_ALWAYS_FROM:
12855 case GOMP_MAP_ALWAYS_TOFROM:
12856 case GOMP_MAP_RELEASE:
12857 case GOMP_MAP_DELETE:
12858 case GOMP_MAP_FORCE_TO:
12859 case GOMP_MAP_FORCE_FROM:
12860 case GOMP_MAP_FORCE_TOFROM:
12861 case GOMP_MAP_FORCE_PRESENT:
12862 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12863 break;
12864 default:
12865 break;
12867 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12868 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12869 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12870 else if (TREE_CODE (t) == COMPONENT_REF)
12871 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12872 else
12873 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12874 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12875 && !c_mark_addressable (t))
12876 return false;
12877 OMP_CLAUSE_DECL (c2) = t;
12878 t = build_fold_addr_expr (first);
12879 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12880 tree ptr = OMP_CLAUSE_DECL (c2);
12881 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12882 ptr = build_fold_addr_expr (ptr);
12883 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12884 ptrdiff_type_node, t,
12885 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12886 ptrdiff_type_node, ptr));
12887 t = c_fully_fold (t, false, NULL);
12888 OMP_CLAUSE_SIZE (c2) = t;
12889 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12890 OMP_CLAUSE_CHAIN (c) = c2;
12892 return false;
12895 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12896 an inline call. But, remap
12897 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12898 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12900 static tree
12901 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12902 tree decl, tree placeholder)
12904 copy_body_data id;
12905 hash_map<tree, tree> decl_map;
12907 decl_map.put (omp_decl1, placeholder);
12908 decl_map.put (omp_decl2, decl);
12909 memset (&id, 0, sizeof (id));
12910 id.src_fn = DECL_CONTEXT (omp_decl1);
12911 id.dst_fn = current_function_decl;
12912 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12913 id.decl_map = &decl_map;
12915 id.copy_decl = copy_decl_no_change;
12916 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12917 id.transform_new_cfg = true;
12918 id.transform_return_to_modify = false;
12919 id.transform_lang_insert_block = NULL;
12920 id.eh_lp_nr = 0;
12921 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12922 return stmt;
12925 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12926 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12928 static tree
12929 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12931 if (*tp == (tree) data)
12932 return *tp;
12933 return NULL_TREE;
12936 /* For all elements of CLAUSES, validate them against their constraints.
12937 Remove any elements from the list that are invalid. */
12939 tree
12940 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12942 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12943 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12944 tree c, t, type, *pc;
12945 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12946 bool branch_seen = false;
12947 bool copyprivate_seen = false;
12948 bool linear_variable_step_check = false;
12949 tree *nowait_clause = NULL;
12950 bool ordered_seen = false;
12951 tree schedule_clause = NULL_TREE;
12952 bool oacc_async = false;
12954 bitmap_obstack_initialize (NULL);
12955 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12956 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12957 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12958 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12959 bitmap_initialize (&map_head, &bitmap_default_obstack);
12960 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12961 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12963 if (ort & C_ORT_ACC)
12964 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12965 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12967 oacc_async = true;
12968 break;
12971 for (pc = &clauses, c = clauses; c ; c = *pc)
12973 bool remove = false;
12974 bool need_complete = false;
12975 bool need_implicitly_determined = false;
12977 switch (OMP_CLAUSE_CODE (c))
12979 case OMP_CLAUSE_SHARED:
12980 need_implicitly_determined = true;
12981 goto check_dup_generic;
12983 case OMP_CLAUSE_PRIVATE:
12984 need_complete = true;
12985 need_implicitly_determined = true;
12986 goto check_dup_generic;
12988 case OMP_CLAUSE_REDUCTION:
12989 need_implicitly_determined = true;
12990 t = OMP_CLAUSE_DECL (c);
12991 if (TREE_CODE (t) == TREE_LIST)
12993 if (handle_omp_array_sections (c, ort))
12995 remove = true;
12996 break;
12999 t = OMP_CLAUSE_DECL (c);
13001 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13002 if (t == error_mark_node)
13004 remove = true;
13005 break;
13007 if (oacc_async)
13008 c_mark_addressable (t);
13009 type = TREE_TYPE (t);
13010 if (TREE_CODE (t) == MEM_REF)
13011 type = TREE_TYPE (type);
13012 if (TREE_CODE (type) == ARRAY_TYPE)
13014 tree oatype = type;
13015 gcc_assert (TREE_CODE (t) != MEM_REF);
13016 while (TREE_CODE (type) == ARRAY_TYPE)
13017 type = TREE_TYPE (type);
13018 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13020 error_at (OMP_CLAUSE_LOCATION (c),
13021 "%qD in %<reduction%> clause is a zero size array",
13023 remove = true;
13024 break;
13026 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13027 TYPE_SIZE_UNIT (type));
13028 if (integer_zerop (size))
13030 error_at (OMP_CLAUSE_LOCATION (c),
13031 "%qD in %<reduction%> clause is a zero size array",
13033 remove = true;
13034 break;
13036 size = size_binop (MINUS_EXPR, size, size_one_node);
13037 tree index_type = build_index_type (size);
13038 tree atype = build_array_type (type, index_type);
13039 tree ptype = build_pointer_type (type);
13040 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13041 t = build_fold_addr_expr (t);
13042 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13043 OMP_CLAUSE_DECL (c) = t;
13045 if (TYPE_ATOMIC (type))
13047 error_at (OMP_CLAUSE_LOCATION (c),
13048 "%<_Atomic%> %qE in %<reduction%> clause", t);
13049 remove = true;
13050 break;
13052 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13053 && (FLOAT_TYPE_P (type)
13054 || TREE_CODE (type) == COMPLEX_TYPE))
13056 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13057 const char *r_name = NULL;
13059 switch (r_code)
13061 case PLUS_EXPR:
13062 case MULT_EXPR:
13063 case MINUS_EXPR:
13064 break;
13065 case MIN_EXPR:
13066 if (TREE_CODE (type) == COMPLEX_TYPE)
13067 r_name = "min";
13068 break;
13069 case MAX_EXPR:
13070 if (TREE_CODE (type) == COMPLEX_TYPE)
13071 r_name = "max";
13072 break;
13073 case BIT_AND_EXPR:
13074 r_name = "&";
13075 break;
13076 case BIT_XOR_EXPR:
13077 r_name = "^";
13078 break;
13079 case BIT_IOR_EXPR:
13080 r_name = "|";
13081 break;
13082 case TRUTH_ANDIF_EXPR:
13083 if (FLOAT_TYPE_P (type))
13084 r_name = "&&";
13085 break;
13086 case TRUTH_ORIF_EXPR:
13087 if (FLOAT_TYPE_P (type))
13088 r_name = "||";
13089 break;
13090 default:
13091 gcc_unreachable ();
13093 if (r_name)
13095 error_at (OMP_CLAUSE_LOCATION (c),
13096 "%qE has invalid type for %<reduction(%s)%>",
13097 t, r_name);
13098 remove = true;
13099 break;
13102 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13104 error_at (OMP_CLAUSE_LOCATION (c),
13105 "user defined reduction not found for %qE", t);
13106 remove = true;
13107 break;
13109 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13111 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13112 type = TYPE_MAIN_VARIANT (type);
13113 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13114 VAR_DECL, NULL_TREE, type);
13115 tree decl_placeholder = NULL_TREE;
13116 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13117 DECL_ARTIFICIAL (placeholder) = 1;
13118 DECL_IGNORED_P (placeholder) = 1;
13119 if (TREE_CODE (t) == MEM_REF)
13121 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13122 VAR_DECL, NULL_TREE, type);
13123 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13124 DECL_ARTIFICIAL (decl_placeholder) = 1;
13125 DECL_IGNORED_P (decl_placeholder) = 1;
13127 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13128 c_mark_addressable (placeholder);
13129 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13130 c_mark_addressable (decl_placeholder ? decl_placeholder
13131 : OMP_CLAUSE_DECL (c));
13132 OMP_CLAUSE_REDUCTION_MERGE (c)
13133 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13134 TREE_VEC_ELT (list, 0),
13135 TREE_VEC_ELT (list, 1),
13136 decl_placeholder ? decl_placeholder
13137 : OMP_CLAUSE_DECL (c), placeholder);
13138 OMP_CLAUSE_REDUCTION_MERGE (c)
13139 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13140 void_type_node, NULL_TREE,
13141 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13142 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13143 if (TREE_VEC_LENGTH (list) == 6)
13145 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13146 c_mark_addressable (decl_placeholder ? decl_placeholder
13147 : OMP_CLAUSE_DECL (c));
13148 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13149 c_mark_addressable (placeholder);
13150 tree init = TREE_VEC_ELT (list, 5);
13151 if (init == error_mark_node)
13152 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13153 OMP_CLAUSE_REDUCTION_INIT (c)
13154 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13155 TREE_VEC_ELT (list, 3),
13156 decl_placeholder ? decl_placeholder
13157 : OMP_CLAUSE_DECL (c), placeholder);
13158 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13160 tree v = decl_placeholder ? decl_placeholder : t;
13161 OMP_CLAUSE_REDUCTION_INIT (c)
13162 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13163 OMP_CLAUSE_REDUCTION_INIT (c));
13165 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13166 c_find_omp_placeholder_r,
13167 placeholder, NULL))
13168 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13170 else
13172 tree init;
13173 tree v = decl_placeholder ? decl_placeholder : t;
13174 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13175 init = build_constructor (TREE_TYPE (v), NULL);
13176 else
13177 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13178 OMP_CLAUSE_REDUCTION_INIT (c)
13179 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13181 OMP_CLAUSE_REDUCTION_INIT (c)
13182 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13183 void_type_node, NULL_TREE,
13184 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13185 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13187 if (TREE_CODE (t) == MEM_REF)
13189 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13190 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13191 != INTEGER_CST)
13193 sorry ("variable length element type in array "
13194 "%<reduction%> clause");
13195 remove = true;
13196 break;
13198 t = TREE_OPERAND (t, 0);
13199 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13200 t = TREE_OPERAND (t, 0);
13201 if (TREE_CODE (t) == ADDR_EXPR)
13202 t = TREE_OPERAND (t, 0);
13204 goto check_dup_generic_t;
13206 case OMP_CLAUSE_COPYPRIVATE:
13207 copyprivate_seen = true;
13208 if (nowait_clause)
13210 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13211 "%<nowait%> clause must not be used together "
13212 "with %<copyprivate%>");
13213 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13214 nowait_clause = NULL;
13216 goto check_dup_generic;
13218 case OMP_CLAUSE_COPYIN:
13219 t = OMP_CLAUSE_DECL (c);
13220 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13222 error_at (OMP_CLAUSE_LOCATION (c),
13223 "%qE must be %<threadprivate%> for %<copyin%>", t);
13224 remove = true;
13225 break;
13227 goto check_dup_generic;
13229 case OMP_CLAUSE_LINEAR:
13230 if (ort != C_ORT_OMP_DECLARE_SIMD)
13231 need_implicitly_determined = true;
13232 t = OMP_CLAUSE_DECL (c);
13233 if (ort != C_ORT_OMP_DECLARE_SIMD
13234 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13236 error_at (OMP_CLAUSE_LOCATION (c),
13237 "modifier should not be specified in %<linear%> "
13238 "clause on %<simd%> or %<for%> constructs");
13239 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13241 if (ort & C_ORT_CILK)
13243 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13244 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13245 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13247 error_at (OMP_CLAUSE_LOCATION (c),
13248 "linear clause applied to non-integral, "
13249 "non-floating, non-pointer variable with type %qT",
13250 TREE_TYPE (t));
13251 remove = true;
13252 break;
13255 else
13257 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13258 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13260 error_at (OMP_CLAUSE_LOCATION (c),
13261 "linear clause applied to non-integral non-pointer "
13262 "variable with type %qT", TREE_TYPE (t));
13263 remove = true;
13264 break;
13266 if (TYPE_ATOMIC (TREE_TYPE (t)))
13268 error_at (OMP_CLAUSE_LOCATION (c),
13269 "%<_Atomic%> %qD in %<linear%> clause", t);
13270 remove = true;
13271 break;
13274 if (ort == C_ORT_OMP_DECLARE_SIMD)
13276 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13277 if (TREE_CODE (s) == PARM_DECL)
13279 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13280 /* map_head bitmap is used as uniform_head if
13281 declare_simd. */
13282 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13283 linear_variable_step_check = true;
13284 goto check_dup_generic;
13286 if (TREE_CODE (s) != INTEGER_CST)
13288 error_at (OMP_CLAUSE_LOCATION (c),
13289 "%<linear%> clause step %qE is neither constant "
13290 "nor a parameter", s);
13291 remove = true;
13292 break;
13295 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13297 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13298 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13299 OMP_CLAUSE_DECL (c), s);
13300 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13301 sizetype, fold_convert (sizetype, s),
13302 fold_convert
13303 (sizetype, OMP_CLAUSE_DECL (c)));
13304 if (s == error_mark_node)
13305 s = size_one_node;
13306 OMP_CLAUSE_LINEAR_STEP (c) = s;
13308 else
13309 OMP_CLAUSE_LINEAR_STEP (c)
13310 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13311 goto check_dup_generic;
13313 check_dup_generic:
13314 t = OMP_CLAUSE_DECL (c);
13315 check_dup_generic_t:
13316 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13318 error_at (OMP_CLAUSE_LOCATION (c),
13319 "%qE is not a variable in clause %qs", t,
13320 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13321 remove = true;
13323 else if (ort == C_ORT_ACC
13324 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13326 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13328 error ("%qD appears more than once in reduction clauses", t);
13329 remove = true;
13331 else
13332 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13334 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13335 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13336 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13338 error_at (OMP_CLAUSE_LOCATION (c),
13339 "%qE appears more than once in data clauses", t);
13340 remove = true;
13342 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13343 && bitmap_bit_p (&map_head, DECL_UID (t)))
13345 if (ort == C_ORT_ACC)
13346 error ("%qD appears more than once in data clauses", t);
13347 else
13348 error ("%qD appears both in data and map clauses", t);
13349 remove = true;
13351 else
13352 bitmap_set_bit (&generic_head, DECL_UID (t));
13353 break;
13355 case OMP_CLAUSE_FIRSTPRIVATE:
13356 t = OMP_CLAUSE_DECL (c);
13357 need_complete = true;
13358 need_implicitly_determined = true;
13359 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13361 error_at (OMP_CLAUSE_LOCATION (c),
13362 "%qE is not a variable in clause %<firstprivate%>", t);
13363 remove = true;
13365 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13366 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13368 error_at (OMP_CLAUSE_LOCATION (c),
13369 "%qE appears more than once in data clauses", t);
13370 remove = true;
13372 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13374 if (ort == C_ORT_ACC)
13375 error ("%qD appears more than once in data clauses", t);
13376 else
13377 error ("%qD appears both in data and map clauses", t);
13378 remove = true;
13380 else
13381 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13382 break;
13384 case OMP_CLAUSE_LASTPRIVATE:
13385 t = OMP_CLAUSE_DECL (c);
13386 need_complete = true;
13387 need_implicitly_determined = true;
13388 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13390 error_at (OMP_CLAUSE_LOCATION (c),
13391 "%qE is not a variable in clause %<lastprivate%>", t);
13392 remove = true;
13394 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13395 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13397 error_at (OMP_CLAUSE_LOCATION (c),
13398 "%qE appears more than once in data clauses", t);
13399 remove = true;
13401 else
13402 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13403 break;
13405 case OMP_CLAUSE_ALIGNED:
13406 t = OMP_CLAUSE_DECL (c);
13407 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13409 error_at (OMP_CLAUSE_LOCATION (c),
13410 "%qE is not a variable in %<aligned%> clause", t);
13411 remove = true;
13413 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13414 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13416 error_at (OMP_CLAUSE_LOCATION (c),
13417 "%qE in %<aligned%> clause is neither a pointer nor "
13418 "an array", t);
13419 remove = true;
13421 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13423 error_at (OMP_CLAUSE_LOCATION (c),
13424 "%<_Atomic%> %qD in %<aligned%> clause", t);
13425 remove = true;
13426 break;
13428 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13430 error_at (OMP_CLAUSE_LOCATION (c),
13431 "%qE appears more than once in %<aligned%> clauses",
13433 remove = true;
13435 else
13436 bitmap_set_bit (&aligned_head, DECL_UID (t));
13437 break;
13439 case OMP_CLAUSE_DEPEND:
13440 t = OMP_CLAUSE_DECL (c);
13441 if (t == NULL_TREE)
13443 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13444 == OMP_CLAUSE_DEPEND_SOURCE);
13445 break;
13447 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13449 gcc_assert (TREE_CODE (t) == TREE_LIST);
13450 for (; t; t = TREE_CHAIN (t))
13452 tree decl = TREE_VALUE (t);
13453 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13455 tree offset = TREE_PURPOSE (t);
13456 bool neg = wi::neg_p ((wide_int) offset);
13457 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13458 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13459 neg ? MINUS_EXPR : PLUS_EXPR,
13460 decl, offset);
13461 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13462 sizetype,
13463 fold_convert (sizetype, t2),
13464 fold_convert (sizetype, decl));
13465 if (t2 == error_mark_node)
13467 remove = true;
13468 break;
13470 TREE_PURPOSE (t) = t2;
13473 break;
13475 if (TREE_CODE (t) == TREE_LIST)
13477 if (handle_omp_array_sections (c, ort))
13478 remove = true;
13479 break;
13481 if (t == error_mark_node)
13482 remove = true;
13483 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13485 error_at (OMP_CLAUSE_LOCATION (c),
13486 "%qE is not a variable in %<depend%> clause", t);
13487 remove = true;
13489 else if (!c_mark_addressable (t))
13490 remove = true;
13491 break;
13493 case OMP_CLAUSE_MAP:
13494 case OMP_CLAUSE_TO:
13495 case OMP_CLAUSE_FROM:
13496 case OMP_CLAUSE__CACHE_:
13497 t = OMP_CLAUSE_DECL (c);
13498 if (TREE_CODE (t) == TREE_LIST)
13500 if (handle_omp_array_sections (c, ort))
13501 remove = true;
13502 else
13504 t = OMP_CLAUSE_DECL (c);
13505 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13507 error_at (OMP_CLAUSE_LOCATION (c),
13508 "array section does not have mappable type "
13509 "in %qs clause",
13510 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13511 remove = true;
13513 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13515 error_at (OMP_CLAUSE_LOCATION (c),
13516 "%<_Atomic%> %qE in %qs clause", t,
13517 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13518 remove = true;
13520 while (TREE_CODE (t) == ARRAY_REF)
13521 t = TREE_OPERAND (t, 0);
13522 if (TREE_CODE (t) == COMPONENT_REF
13523 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13525 while (TREE_CODE (t) == COMPONENT_REF)
13526 t = TREE_OPERAND (t, 0);
13527 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13528 break;
13529 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13531 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13532 error ("%qD appears more than once in motion"
13533 " clauses", t);
13534 else if (ort == C_ORT_ACC)
13535 error ("%qD appears more than once in data"
13536 " clauses", t);
13537 else
13538 error ("%qD appears more than once in map"
13539 " clauses", t);
13540 remove = true;
13542 else
13544 bitmap_set_bit (&map_head, DECL_UID (t));
13545 bitmap_set_bit (&map_field_head, DECL_UID (t));
13549 break;
13551 if (t == error_mark_node)
13553 remove = true;
13554 break;
13556 if (TREE_CODE (t) == COMPONENT_REF
13557 && (ort & C_ORT_OMP)
13558 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13560 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13562 error_at (OMP_CLAUSE_LOCATION (c),
13563 "bit-field %qE in %qs clause",
13564 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13565 remove = true;
13567 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13569 error_at (OMP_CLAUSE_LOCATION (c),
13570 "%qE does not have a mappable type in %qs clause",
13571 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13572 remove = true;
13574 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13576 error_at (OMP_CLAUSE_LOCATION (c),
13577 "%<_Atomic%> %qE in %qs clause", t,
13578 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13579 remove = true;
13581 while (TREE_CODE (t) == COMPONENT_REF)
13583 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13584 == UNION_TYPE)
13586 error_at (OMP_CLAUSE_LOCATION (c),
13587 "%qE is a member of a union", t);
13588 remove = true;
13589 break;
13591 t = TREE_OPERAND (t, 0);
13593 if (remove)
13594 break;
13595 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13597 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13598 break;
13601 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13603 error_at (OMP_CLAUSE_LOCATION (c),
13604 "%qE is not a variable in %qs clause", t,
13605 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13606 remove = true;
13608 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13610 error_at (OMP_CLAUSE_LOCATION (c),
13611 "%qD is threadprivate variable in %qs clause", t,
13612 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13613 remove = true;
13615 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13616 || (OMP_CLAUSE_MAP_KIND (c)
13617 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13618 && !c_mark_addressable (t))
13619 remove = true;
13620 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13621 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13622 || (OMP_CLAUSE_MAP_KIND (c)
13623 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13624 || (OMP_CLAUSE_MAP_KIND (c)
13625 == GOMP_MAP_FORCE_DEVICEPTR)))
13626 && t == OMP_CLAUSE_DECL (c)
13627 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13629 error_at (OMP_CLAUSE_LOCATION (c),
13630 "%qD does not have a mappable type in %qs clause", t,
13631 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13632 remove = true;
13634 else if (TREE_TYPE (t) == error_mark_node)
13635 remove = true;
13636 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13638 error_at (OMP_CLAUSE_LOCATION (c),
13639 "%<_Atomic%> %qE in %qs clause", t,
13640 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13641 remove = true;
13643 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13644 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13646 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13647 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13649 error ("%qD appears more than once in data clauses", t);
13650 remove = true;
13652 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13654 if (ort == C_ORT_ACC)
13655 error ("%qD appears more than once in data clauses", t);
13656 else
13657 error ("%qD appears both in data and map clauses", t);
13658 remove = true;
13660 else
13661 bitmap_set_bit (&generic_head, DECL_UID (t));
13663 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13665 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13666 error ("%qD appears more than once in motion clauses", t);
13667 else if (ort == C_ORT_ACC)
13668 error ("%qD appears more than once in data clauses", t);
13669 else
13670 error ("%qD appears more than once in map clauses", t);
13671 remove = true;
13673 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13674 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13676 if (ort == C_ORT_ACC)
13677 error ("%qD appears more than once in data clauses", t);
13678 else
13679 error ("%qD appears both in data and map clauses", t);
13680 remove = true;
13682 else
13684 bitmap_set_bit (&map_head, DECL_UID (t));
13685 if (t != OMP_CLAUSE_DECL (c)
13686 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13687 bitmap_set_bit (&map_field_head, DECL_UID (t));
13689 break;
13691 case OMP_CLAUSE_TO_DECLARE:
13692 case OMP_CLAUSE_LINK:
13693 t = OMP_CLAUSE_DECL (c);
13694 if (TREE_CODE (t) == FUNCTION_DECL
13695 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13697 else if (!VAR_P (t))
13699 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13700 error_at (OMP_CLAUSE_LOCATION (c),
13701 "%qE is neither a variable nor a function name in "
13702 "clause %qs", t,
13703 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13704 else
13705 error_at (OMP_CLAUSE_LOCATION (c),
13706 "%qE is not a variable in clause %qs", t,
13707 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13708 remove = true;
13710 else if (DECL_THREAD_LOCAL_P (t))
13712 error_at (OMP_CLAUSE_LOCATION (c),
13713 "%qD is threadprivate variable in %qs clause", t,
13714 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13715 remove = true;
13717 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13719 error_at (OMP_CLAUSE_LOCATION (c),
13720 "%qD does not have a mappable type in %qs clause", t,
13721 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13722 remove = true;
13724 if (remove)
13725 break;
13726 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13728 error_at (OMP_CLAUSE_LOCATION (c),
13729 "%qE appears more than once on the same "
13730 "%<declare target%> directive", t);
13731 remove = true;
13733 else
13734 bitmap_set_bit (&generic_head, DECL_UID (t));
13735 break;
13737 case OMP_CLAUSE_UNIFORM:
13738 t = OMP_CLAUSE_DECL (c);
13739 if (TREE_CODE (t) != PARM_DECL)
13741 if (DECL_P (t))
13742 error_at (OMP_CLAUSE_LOCATION (c),
13743 "%qD is not an argument in %<uniform%> clause", t);
13744 else
13745 error_at (OMP_CLAUSE_LOCATION (c),
13746 "%qE is not an argument in %<uniform%> clause", t);
13747 remove = true;
13748 break;
13750 /* map_head bitmap is used as uniform_head if declare_simd. */
13751 bitmap_set_bit (&map_head, DECL_UID (t));
13752 goto check_dup_generic;
13754 case OMP_CLAUSE_IS_DEVICE_PTR:
13755 case OMP_CLAUSE_USE_DEVICE_PTR:
13756 t = OMP_CLAUSE_DECL (c);
13757 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13758 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13760 error_at (OMP_CLAUSE_LOCATION (c),
13761 "%qs variable is neither a pointer nor an array",
13762 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13763 remove = true;
13765 goto check_dup_generic;
13767 case OMP_CLAUSE_NOWAIT:
13768 if (copyprivate_seen)
13770 error_at (OMP_CLAUSE_LOCATION (c),
13771 "%<nowait%> clause must not be used together "
13772 "with %<copyprivate%>");
13773 remove = true;
13774 break;
13776 nowait_clause = pc;
13777 pc = &OMP_CLAUSE_CHAIN (c);
13778 continue;
13780 case OMP_CLAUSE_IF:
13781 case OMP_CLAUSE_NUM_THREADS:
13782 case OMP_CLAUSE_NUM_TEAMS:
13783 case OMP_CLAUSE_THREAD_LIMIT:
13784 case OMP_CLAUSE_DEFAULT:
13785 case OMP_CLAUSE_UNTIED:
13786 case OMP_CLAUSE_COLLAPSE:
13787 case OMP_CLAUSE_FINAL:
13788 case OMP_CLAUSE_MERGEABLE:
13789 case OMP_CLAUSE_DEVICE:
13790 case OMP_CLAUSE_DIST_SCHEDULE:
13791 case OMP_CLAUSE_PARALLEL:
13792 case OMP_CLAUSE_FOR:
13793 case OMP_CLAUSE_SECTIONS:
13794 case OMP_CLAUSE_TASKGROUP:
13795 case OMP_CLAUSE_PROC_BIND:
13796 case OMP_CLAUSE_PRIORITY:
13797 case OMP_CLAUSE_GRAINSIZE:
13798 case OMP_CLAUSE_NUM_TASKS:
13799 case OMP_CLAUSE_NOGROUP:
13800 case OMP_CLAUSE_THREADS:
13801 case OMP_CLAUSE_SIMD:
13802 case OMP_CLAUSE_HINT:
13803 case OMP_CLAUSE_DEFAULTMAP:
13804 case OMP_CLAUSE__CILK_FOR_COUNT_:
13805 case OMP_CLAUSE_NUM_GANGS:
13806 case OMP_CLAUSE_NUM_WORKERS:
13807 case OMP_CLAUSE_VECTOR_LENGTH:
13808 case OMP_CLAUSE_ASYNC:
13809 case OMP_CLAUSE_WAIT:
13810 case OMP_CLAUSE_AUTO:
13811 case OMP_CLAUSE_INDEPENDENT:
13812 case OMP_CLAUSE_SEQ:
13813 case OMP_CLAUSE_GANG:
13814 case OMP_CLAUSE_WORKER:
13815 case OMP_CLAUSE_VECTOR:
13816 case OMP_CLAUSE_TILE:
13817 pc = &OMP_CLAUSE_CHAIN (c);
13818 continue;
13820 case OMP_CLAUSE_SCHEDULE:
13821 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13823 const char *p = NULL;
13824 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13826 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13827 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13828 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13829 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13830 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13831 default: gcc_unreachable ();
13833 if (p)
13835 error_at (OMP_CLAUSE_LOCATION (c),
13836 "%<nonmonotonic%> modifier specified for %qs "
13837 "schedule kind", p);
13838 OMP_CLAUSE_SCHEDULE_KIND (c)
13839 = (enum omp_clause_schedule_kind)
13840 (OMP_CLAUSE_SCHEDULE_KIND (c)
13841 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13844 schedule_clause = c;
13845 pc = &OMP_CLAUSE_CHAIN (c);
13846 continue;
13848 case OMP_CLAUSE_ORDERED:
13849 ordered_seen = true;
13850 pc = &OMP_CLAUSE_CHAIN (c);
13851 continue;
13853 case OMP_CLAUSE_SAFELEN:
13854 safelen = c;
13855 pc = &OMP_CLAUSE_CHAIN (c);
13856 continue;
13857 case OMP_CLAUSE_SIMDLEN:
13858 simdlen = c;
13859 pc = &OMP_CLAUSE_CHAIN (c);
13860 continue;
13862 case OMP_CLAUSE_INBRANCH:
13863 case OMP_CLAUSE_NOTINBRANCH:
13864 if (branch_seen)
13866 error_at (OMP_CLAUSE_LOCATION (c),
13867 "%<inbranch%> clause is incompatible with "
13868 "%<notinbranch%>");
13869 remove = true;
13870 break;
13872 branch_seen = true;
13873 pc = &OMP_CLAUSE_CHAIN (c);
13874 continue;
13876 default:
13877 gcc_unreachable ();
13880 if (!remove)
13882 t = OMP_CLAUSE_DECL (c);
13884 if (need_complete)
13886 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13887 if (t == error_mark_node)
13888 remove = true;
13891 if (need_implicitly_determined)
13893 const char *share_name = NULL;
13895 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13896 share_name = "threadprivate";
13897 else switch (c_omp_predetermined_sharing (t))
13899 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13900 break;
13901 case OMP_CLAUSE_DEFAULT_SHARED:
13902 /* const vars may be specified in firstprivate clause. */
13903 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13904 && TREE_READONLY (t))
13905 break;
13906 share_name = "shared";
13907 break;
13908 case OMP_CLAUSE_DEFAULT_PRIVATE:
13909 share_name = "private";
13910 break;
13911 default:
13912 gcc_unreachable ();
13914 if (share_name)
13916 error_at (OMP_CLAUSE_LOCATION (c),
13917 "%qE is predetermined %qs for %qs",
13918 t, share_name,
13919 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13920 remove = true;
13925 if (remove)
13926 *pc = OMP_CLAUSE_CHAIN (c);
13927 else
13928 pc = &OMP_CLAUSE_CHAIN (c);
13931 if (simdlen
13932 && safelen
13933 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13934 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13936 error_at (OMP_CLAUSE_LOCATION (simdlen),
13937 "%<simdlen%> clause value is bigger than "
13938 "%<safelen%> clause value");
13939 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13940 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13943 if (ordered_seen
13944 && schedule_clause
13945 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13946 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13948 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13949 "%<nonmonotonic%> schedule modifier specified together "
13950 "with %<ordered%> clause");
13951 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13952 = (enum omp_clause_schedule_kind)
13953 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13954 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13957 if (linear_variable_step_check)
13958 for (pc = &clauses, c = clauses; c ; c = *pc)
13960 bool remove = false;
13961 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13962 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13963 && !bitmap_bit_p (&map_head,
13964 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13966 error_at (OMP_CLAUSE_LOCATION (c),
13967 "%<linear%> clause step is a parameter %qD not "
13968 "specified in %<uniform%> clause",
13969 OMP_CLAUSE_LINEAR_STEP (c));
13970 remove = true;
13973 if (remove)
13974 *pc = OMP_CLAUSE_CHAIN (c);
13975 else
13976 pc = &OMP_CLAUSE_CHAIN (c);
13979 bitmap_obstack_release (NULL);
13980 return clauses;
13983 /* Return code to initialize DST with a copy constructor from SRC.
13984 C doesn't have copy constructors nor assignment operators, only for
13985 _Atomic vars we need to perform __atomic_load from src into a temporary
13986 followed by __atomic_store of the temporary to dst. */
13988 tree
13989 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13991 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13992 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13994 location_t loc = OMP_CLAUSE_LOCATION (clause);
13995 tree type = TREE_TYPE (dst);
13996 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13997 tree tmp = create_tmp_var (nonatomic_type);
13998 tree tmp_addr = build_fold_addr_expr (tmp);
13999 TREE_ADDRESSABLE (tmp) = 1;
14000 TREE_NO_WARNING (tmp) = 1;
14001 tree src_addr = build_fold_addr_expr (src);
14002 tree dst_addr = build_fold_addr_expr (dst);
14003 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14004 vec<tree, va_gc> *params;
14005 /* Expansion of a generic atomic load may require an addition
14006 element, so allocate enough to prevent a resize. */
14007 vec_alloc (params, 4);
14009 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14010 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14011 params->quick_push (src_addr);
14012 params->quick_push (tmp_addr);
14013 params->quick_push (seq_cst);
14014 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14016 vec_alloc (params, 4);
14018 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14019 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14020 params->quick_push (dst_addr);
14021 params->quick_push (tmp_addr);
14022 params->quick_push (seq_cst);
14023 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14024 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14027 /* Create a transaction node. */
14029 tree
14030 c_finish_transaction (location_t loc, tree block, int flags)
14032 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14033 if (flags & TM_STMT_ATTR_OUTER)
14034 TRANSACTION_EXPR_OUTER (stmt) = 1;
14035 if (flags & TM_STMT_ATTR_RELAXED)
14036 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14037 return add_stmt (stmt);
14040 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14041 down to the element type of an array. If ORIG_QUAL_TYPE is not
14042 NULL, then it should be used as the qualified type
14043 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14044 preserve information about the typedef name from which an array
14045 type was derived). */
14047 tree
14048 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14049 size_t orig_qual_indirect)
14051 if (type == error_mark_node)
14052 return type;
14054 if (TREE_CODE (type) == ARRAY_TYPE)
14056 tree t;
14057 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14058 type_quals, orig_qual_type,
14059 orig_qual_indirect - 1);
14061 /* See if we already have an identically qualified type. */
14062 if (orig_qual_type && orig_qual_indirect == 0)
14063 t = orig_qual_type;
14064 else
14065 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14067 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14068 && TYPE_NAME (t) == TYPE_NAME (type)
14069 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14070 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14071 TYPE_ATTRIBUTES (type)))
14072 break;
14074 if (!t)
14076 tree domain = TYPE_DOMAIN (type);
14078 t = build_variant_type_copy (type);
14079 TREE_TYPE (t) = element_type;
14081 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14082 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14083 SET_TYPE_STRUCTURAL_EQUALITY (t);
14084 else if (TYPE_CANONICAL (element_type) != element_type
14085 || (domain && TYPE_CANONICAL (domain) != domain))
14087 tree unqualified_canon
14088 = build_array_type (TYPE_CANONICAL (element_type),
14089 domain? TYPE_CANONICAL (domain)
14090 : NULL_TREE);
14091 if (TYPE_REVERSE_STORAGE_ORDER (type))
14093 unqualified_canon
14094 = build_distinct_type_copy (unqualified_canon);
14095 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14097 TYPE_CANONICAL (t)
14098 = c_build_qualified_type (unqualified_canon, type_quals);
14100 else
14101 TYPE_CANONICAL (t) = t;
14103 return t;
14106 /* A restrict-qualified pointer type must be a pointer to object or
14107 incomplete type. Note that the use of POINTER_TYPE_P also allows
14108 REFERENCE_TYPEs, which is appropriate for C++. */
14109 if ((type_quals & TYPE_QUAL_RESTRICT)
14110 && (!POINTER_TYPE_P (type)
14111 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14113 error ("invalid use of %<restrict%>");
14114 type_quals &= ~TYPE_QUAL_RESTRICT;
14117 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14118 ? orig_qual_type
14119 : build_qualified_type (type, type_quals));
14120 /* A variant type does not inherit the list of incomplete vars from the
14121 type main variant. */
14122 if (RECORD_OR_UNION_TYPE_P (var_type)
14123 && TYPE_MAIN_VARIANT (var_type) != var_type)
14124 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14125 return var_type;
14128 /* Build a VA_ARG_EXPR for the C parser. */
14130 tree
14131 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14133 if (error_operand_p (type))
14134 return error_mark_node;
14135 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14136 order because it takes the address of the expression. */
14137 else if (handled_component_p (expr)
14138 && reverse_storage_order_for_component_p (expr))
14140 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14141 return error_mark_node;
14143 else if (!COMPLETE_TYPE_P (type))
14145 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14146 "type %qT", type);
14147 return error_mark_node;
14149 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14150 warning_at (loc2, OPT_Wc___compat,
14151 "C++ requires promoted type, not enum type, in %<va_arg%>");
14152 return build_va_arg (loc2, expr, type);
14155 /* Return truthvalue of whether T1 is the same tree structure as T2.
14156 Return 1 if they are the same. Return false if they are different. */
14158 bool
14159 c_tree_equal (tree t1, tree t2)
14161 enum tree_code code1, code2;
14163 if (t1 == t2)
14164 return true;
14165 if (!t1 || !t2)
14166 return false;
14168 for (code1 = TREE_CODE (t1);
14169 CONVERT_EXPR_CODE_P (code1)
14170 || code1 == NON_LVALUE_EXPR;
14171 code1 = TREE_CODE (t1))
14172 t1 = TREE_OPERAND (t1, 0);
14173 for (code2 = TREE_CODE (t2);
14174 CONVERT_EXPR_CODE_P (code2)
14175 || code2 == NON_LVALUE_EXPR;
14176 code2 = TREE_CODE (t2))
14177 t2 = TREE_OPERAND (t2, 0);
14179 /* They might have become equal now. */
14180 if (t1 == t2)
14181 return true;
14183 if (code1 != code2)
14184 return false;
14186 switch (code1)
14188 case INTEGER_CST:
14189 return wi::eq_p (t1, t2);
14191 case REAL_CST:
14192 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14194 case STRING_CST:
14195 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14196 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14197 TREE_STRING_LENGTH (t1));
14199 case FIXED_CST:
14200 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14201 TREE_FIXED_CST (t2));
14203 case COMPLEX_CST:
14204 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14205 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14207 case VECTOR_CST:
14208 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14210 case CONSTRUCTOR:
14211 /* We need to do this when determining whether or not two
14212 non-type pointer to member function template arguments
14213 are the same. */
14214 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14215 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14216 return false;
14218 tree field, value;
14219 unsigned int i;
14220 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14222 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14223 if (!c_tree_equal (field, elt2->index)
14224 || !c_tree_equal (value, elt2->value))
14225 return false;
14228 return true;
14230 case TREE_LIST:
14231 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14232 return false;
14233 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14234 return false;
14235 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14237 case SAVE_EXPR:
14238 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14240 case CALL_EXPR:
14242 tree arg1, arg2;
14243 call_expr_arg_iterator iter1, iter2;
14244 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14245 return false;
14246 for (arg1 = first_call_expr_arg (t1, &iter1),
14247 arg2 = first_call_expr_arg (t2, &iter2);
14248 arg1 && arg2;
14249 arg1 = next_call_expr_arg (&iter1),
14250 arg2 = next_call_expr_arg (&iter2))
14251 if (!c_tree_equal (arg1, arg2))
14252 return false;
14253 if (arg1 || arg2)
14254 return false;
14255 return true;
14258 case TARGET_EXPR:
14260 tree o1 = TREE_OPERAND (t1, 0);
14261 tree o2 = TREE_OPERAND (t2, 0);
14263 /* Special case: if either target is an unallocated VAR_DECL,
14264 it means that it's going to be unified with whatever the
14265 TARGET_EXPR is really supposed to initialize, so treat it
14266 as being equivalent to anything. */
14267 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14268 && !DECL_RTL_SET_P (o1))
14269 /*Nop*/;
14270 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14271 && !DECL_RTL_SET_P (o2))
14272 /*Nop*/;
14273 else if (!c_tree_equal (o1, o2))
14274 return false;
14276 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14279 case COMPONENT_REF:
14280 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14281 return false;
14282 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14284 case PARM_DECL:
14285 case VAR_DECL:
14286 case CONST_DECL:
14287 case FIELD_DECL:
14288 case FUNCTION_DECL:
14289 case IDENTIFIER_NODE:
14290 case SSA_NAME:
14291 return false;
14293 case TREE_VEC:
14295 unsigned ix;
14296 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14297 return false;
14298 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14299 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14300 TREE_VEC_ELT (t2, ix)))
14301 return false;
14302 return true;
14305 default:
14306 break;
14309 switch (TREE_CODE_CLASS (code1))
14311 case tcc_unary:
14312 case tcc_binary:
14313 case tcc_comparison:
14314 case tcc_expression:
14315 case tcc_vl_exp:
14316 case tcc_reference:
14317 case tcc_statement:
14319 int i, n = TREE_OPERAND_LENGTH (t1);
14321 switch (code1)
14323 case PREINCREMENT_EXPR:
14324 case PREDECREMENT_EXPR:
14325 case POSTINCREMENT_EXPR:
14326 case POSTDECREMENT_EXPR:
14327 n = 1;
14328 break;
14329 case ARRAY_REF:
14330 n = 2;
14331 break;
14332 default:
14333 break;
14336 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14337 && n != TREE_OPERAND_LENGTH (t2))
14338 return false;
14340 for (i = 0; i < n; ++i)
14341 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14342 return false;
14344 return true;
14347 case tcc_type:
14348 return comptypes (t1, t2);
14349 default:
14350 gcc_unreachable ();
14352 /* We can get here with --disable-checking. */
14353 return false;
14356 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14357 spawn-helper and BODY is the newly created body for FNDECL. */
14359 void
14360 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14362 tree list = alloc_stmt_list ();
14363 tree frame = make_cilk_frame (fndecl);
14364 tree dtor = create_cilk_function_exit (frame, false, true);
14365 add_local_decl (cfun, frame);
14367 DECL_SAVED_TREE (fndecl) = list;
14369 tree body_list = alloc_stmt_list ();
14370 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14371 body = fold_build_cleanup_point_expr (void_type_node, body);
14373 append_to_statement_list (body, &body_list);
14374 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14375 body_list, dtor), &list);
14378 /* Returns true when the function declaration FNDECL is implicit,
14379 introduced as a result of a call to an otherwise undeclared
14380 function, and false otherwise. */
14382 bool
14383 c_decl_implicit (const_tree fndecl)
14385 return C_DECL_IMPLICIT (fndecl);