PR c/81417
[official-gcc.git] / gcc / c / c-typeck.c
blob8033a8190f3938b6e0a603160e90ac4c87d3004b
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, int,
110 bool, struct obstack *);
111 static void output_pending_init_elements (int, struct obstack *);
112 static bool set_designator (location_t, int, 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 machine_mode m1, m2;
816 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
818 m1 = TYPE_MODE (t1);
819 m2 = 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 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
853 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
855 enum mode_class mclass = (enum mode_class) 0;
856 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
857 mclass = MODE_FRACT;
858 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
859 mclass = MODE_ACCUM;
860 else
861 gcc_unreachable ();
862 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
866 if (code1 == FIXED_POINT_TYPE)
868 fbit1 = GET_MODE_FBIT (m1);
869 ibit1 = GET_MODE_IBIT (m1);
871 else
873 fbit1 = 0;
874 /* Signed integers need to subtract one sign bit. */
875 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
878 if (code2 == FIXED_POINT_TYPE)
880 fbit2 = GET_MODE_FBIT (m2);
881 ibit2 = GET_MODE_IBIT (m2);
883 else
885 fbit2 = 0;
886 /* Signed integers need to subtract one sign bit. */
887 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
890 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
891 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
892 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
893 satp);
896 /* Both real or both integers; use the one with greater precision. */
898 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
899 return t1;
900 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
901 return t2;
903 /* Same precision. Prefer long longs to longs to ints when the
904 same precision, following the C99 rules on integer type rank
905 (which are equivalent to the C90 rules for C90 types). */
907 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
908 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
909 return long_long_unsigned_type_node;
911 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
912 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
914 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
915 return long_long_unsigned_type_node;
916 else
917 return long_long_integer_type_node;
920 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
922 return long_unsigned_type_node;
924 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
925 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
927 /* But preserve unsignedness from the other type,
928 since long cannot hold all the values of an unsigned int. */
929 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
930 return long_unsigned_type_node;
931 else
932 return long_integer_type_node;
935 /* For floating types of the same TYPE_PRECISION (which we here
936 assume means either the same set of values, or sets of values
937 neither a subset of the other, with behavior being undefined in
938 the latter case), follow the rules from TS 18661-3: prefer
939 interchange types _FloatN, then standard types long double,
940 double, float, then extended types _FloatNx. For extended types,
941 check them starting with _Float128x as that seems most consistent
942 in spirit with preferring long double to double; for interchange
943 types, also check in that order for consistency although it's not
944 possible for more than one of them to have the same
945 precision. */
946 tree mv1 = TYPE_MAIN_VARIANT (t1);
947 tree mv2 = TYPE_MAIN_VARIANT (t2);
949 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
950 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
951 return FLOATN_TYPE_NODE (i);
953 /* Likewise, prefer long double to double even if same size. */
954 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
955 return long_double_type_node;
957 /* Likewise, prefer double to float even if same size.
958 We got a couple of embedded targets with 32 bit doubles, and the
959 pdp11 might have 64 bit floats. */
960 if (mv1 == double_type_node || mv2 == double_type_node)
961 return double_type_node;
963 if (mv1 == float_type_node || mv2 == float_type_node)
964 return float_type_node;
966 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
967 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
968 return FLOATNX_TYPE_NODE (i);
970 /* Otherwise prefer the unsigned one. */
972 if (TYPE_UNSIGNED (t1))
973 return t1;
974 else
975 return t2;
978 /* Wrapper around c_common_type that is used by c-common.c and other
979 front end optimizations that remove promotions. ENUMERAL_TYPEs
980 are allowed here and are converted to their compatible integer types.
981 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
982 preferably a non-Boolean type as the common type. */
983 tree
984 common_type (tree t1, tree t2)
986 if (TREE_CODE (t1) == ENUMERAL_TYPE)
987 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
988 if (TREE_CODE (t2) == ENUMERAL_TYPE)
989 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
991 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
992 if (TREE_CODE (t1) == BOOLEAN_TYPE
993 && TREE_CODE (t2) == BOOLEAN_TYPE)
994 return boolean_type_node;
996 /* If either type is BOOLEAN_TYPE, then return the other. */
997 if (TREE_CODE (t1) == BOOLEAN_TYPE)
998 return t2;
999 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1000 return t1;
1002 return c_common_type (t1, t2);
1005 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1006 or various other operations. Return 2 if they are compatible
1007 but a warning may be needed if you use them together. */
1010 comptypes (tree type1, tree type2)
1012 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1013 int val;
1015 val = comptypes_internal (type1, type2, NULL, NULL);
1016 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1018 return val;
1021 /* Like comptypes, but if it returns non-zero because enum and int are
1022 compatible, it sets *ENUM_AND_INT_P to true. */
1024 static int
1025 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1027 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1028 int val;
1030 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1031 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1033 return val;
1036 /* Like comptypes, but if it returns nonzero for different types, it
1037 sets *DIFFERENT_TYPES_P to true. */
1040 comptypes_check_different_types (tree type1, tree type2,
1041 bool *different_types_p)
1043 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1044 int val;
1046 val = comptypes_internal (type1, type2, NULL, different_types_p);
1047 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1049 return val;
1052 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1053 or various other operations. Return 2 if they are compatible
1054 but a warning may be needed if you use them together. If
1055 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1056 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1057 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1058 NULL, and the types are compatible but different enough not to be
1059 permitted in C11 typedef redeclarations, then this sets
1060 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1061 false, but may or may not be set if the types are incompatible.
1062 This differs from comptypes, in that we don't free the seen
1063 types. */
1065 static int
1066 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1067 bool *different_types_p)
1069 const_tree t1 = type1;
1070 const_tree t2 = type2;
1071 int attrval, val;
1073 /* Suppress errors caused by previously reported errors. */
1075 if (t1 == t2 || !t1 || !t2
1076 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1077 return 1;
1079 /* Enumerated types are compatible with integer types, but this is
1080 not transitive: two enumerated types in the same translation unit
1081 are compatible with each other only if they are the same type. */
1083 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1085 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1086 if (TREE_CODE (t2) != VOID_TYPE)
1088 if (enum_and_int_p != NULL)
1089 *enum_and_int_p = true;
1090 if (different_types_p != NULL)
1091 *different_types_p = true;
1094 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1096 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1097 if (TREE_CODE (t1) != VOID_TYPE)
1099 if (enum_and_int_p != NULL)
1100 *enum_and_int_p = true;
1101 if (different_types_p != NULL)
1102 *different_types_p = true;
1106 if (t1 == t2)
1107 return 1;
1109 /* Different classes of types can't be compatible. */
1111 if (TREE_CODE (t1) != TREE_CODE (t2))
1112 return 0;
1114 /* Qualifiers must match. C99 6.7.3p9 */
1116 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1117 return 0;
1119 /* Allow for two different type nodes which have essentially the same
1120 definition. Note that we already checked for equality of the type
1121 qualifiers (just above). */
1123 if (TREE_CODE (t1) != ARRAY_TYPE
1124 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1125 return 1;
1127 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1128 if (!(attrval = comp_type_attributes (t1, t2)))
1129 return 0;
1131 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1132 val = 0;
1134 switch (TREE_CODE (t1))
1136 case INTEGER_TYPE:
1137 case FIXED_POINT_TYPE:
1138 case REAL_TYPE:
1139 /* With these nodes, we can't determine type equivalence by
1140 looking at what is stored in the nodes themselves, because
1141 two nodes might have different TYPE_MAIN_VARIANTs but still
1142 represent the same type. For example, wchar_t and int could
1143 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1144 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1145 and are distinct types. On the other hand, int and the
1146 following typedef
1148 typedef int INT __attribute((may_alias));
1150 have identical properties, different TYPE_MAIN_VARIANTs, but
1151 represent the same type. The canonical type system keeps
1152 track of equivalence in this case, so we fall back on it. */
1153 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1155 case POINTER_TYPE:
1156 /* Do not remove mode information. */
1157 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1158 break;
1159 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1160 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1161 enum_and_int_p, different_types_p));
1162 break;
1164 case FUNCTION_TYPE:
1165 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1166 different_types_p);
1167 break;
1169 case ARRAY_TYPE:
1171 tree d1 = TYPE_DOMAIN (t1);
1172 tree d2 = TYPE_DOMAIN (t2);
1173 bool d1_variable, d2_variable;
1174 bool d1_zero, d2_zero;
1175 val = 1;
1177 /* Target types must match incl. qualifiers. */
1178 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1179 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1180 enum_and_int_p,
1181 different_types_p)))
1182 return 0;
1184 if (different_types_p != NULL
1185 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1186 *different_types_p = true;
1187 /* Sizes must match unless one is missing or variable. */
1188 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1189 break;
1191 d1_zero = !TYPE_MAX_VALUE (d1);
1192 d2_zero = !TYPE_MAX_VALUE (d2);
1194 d1_variable = (!d1_zero
1195 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1196 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1197 d2_variable = (!d2_zero
1198 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1199 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1200 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1201 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1203 if (different_types_p != NULL
1204 && d1_variable != d2_variable)
1205 *different_types_p = true;
1206 if (d1_variable || d2_variable)
1207 break;
1208 if (d1_zero && d2_zero)
1209 break;
1210 if (d1_zero || d2_zero
1211 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1212 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1213 val = 0;
1215 break;
1218 case ENUMERAL_TYPE:
1219 case RECORD_TYPE:
1220 case UNION_TYPE:
1221 if (val != 1 && !same_translation_unit_p (t1, t2))
1223 tree a1 = TYPE_ATTRIBUTES (t1);
1224 tree a2 = TYPE_ATTRIBUTES (t2);
1226 if (! attribute_list_contained (a1, a2)
1227 && ! attribute_list_contained (a2, a1))
1228 break;
1230 if (attrval != 2)
1231 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1232 different_types_p);
1233 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1234 different_types_p);
1236 break;
1238 case VECTOR_TYPE:
1239 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1240 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1241 enum_and_int_p, different_types_p));
1242 break;
1244 default:
1245 break;
1247 return attrval == 2 && val == 1 ? 2 : val;
1250 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1251 their qualifiers, except for named address spaces. If the pointers point to
1252 different named addresses, then we must determine if one address space is a
1253 subset of the other. */
1255 static int
1256 comp_target_types (location_t location, tree ttl, tree ttr)
1258 int val;
1259 int val_ped;
1260 tree mvl = TREE_TYPE (ttl);
1261 tree mvr = TREE_TYPE (ttr);
1262 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1263 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1264 addr_space_t as_common;
1265 bool enum_and_int_p;
1267 /* Fail if pointers point to incompatible address spaces. */
1268 if (!addr_space_superset (asl, asr, &as_common))
1269 return 0;
1271 /* For pedantic record result of comptypes on arrays before losing
1272 qualifiers on the element type below. */
1273 val_ped = 1;
1275 if (TREE_CODE (mvl) == ARRAY_TYPE
1276 && TREE_CODE (mvr) == ARRAY_TYPE)
1277 val_ped = comptypes (mvl, mvr);
1279 /* Qualifiers on element types of array types that are
1280 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1282 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1283 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1284 : TYPE_MAIN_VARIANT (mvl));
1286 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1287 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1288 : TYPE_MAIN_VARIANT (mvr));
1290 enum_and_int_p = false;
1291 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1293 if (val == 1 && val_ped != 1)
1294 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1295 "are incompatible in ISO C");
1297 if (val == 2)
1298 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1300 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1301 warning_at (location, OPT_Wc___compat,
1302 "pointer target types incompatible in C++");
1304 return val;
1307 /* Subroutines of `comptypes'. */
1309 /* Determine whether two trees derive from the same translation unit.
1310 If the CONTEXT chain ends in a null, that tree's context is still
1311 being parsed, so if two trees have context chains ending in null,
1312 they're in the same translation unit. */
1314 bool
1315 same_translation_unit_p (const_tree t1, const_tree t2)
1317 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1318 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1320 case tcc_declaration:
1321 t1 = DECL_CONTEXT (t1); break;
1322 case tcc_type:
1323 t1 = TYPE_CONTEXT (t1); break;
1324 case tcc_exceptional:
1325 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1326 default: gcc_unreachable ();
1329 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1330 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1332 case tcc_declaration:
1333 t2 = DECL_CONTEXT (t2); break;
1334 case tcc_type:
1335 t2 = TYPE_CONTEXT (t2); break;
1336 case tcc_exceptional:
1337 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1338 default: gcc_unreachable ();
1341 return t1 == t2;
1344 /* Allocate the seen two types, assuming that they are compatible. */
1346 static struct tagged_tu_seen_cache *
1347 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1349 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1350 tu->next = tagged_tu_seen_base;
1351 tu->t1 = t1;
1352 tu->t2 = t2;
1354 tagged_tu_seen_base = tu;
1356 /* The C standard says that two structures in different translation
1357 units are compatible with each other only if the types of their
1358 fields are compatible (among other things). We assume that they
1359 are compatible until proven otherwise when building the cache.
1360 An example where this can occur is:
1361 struct a
1363 struct a *next;
1365 If we are comparing this against a similar struct in another TU,
1366 and did not assume they were compatible, we end up with an infinite
1367 loop. */
1368 tu->val = 1;
1369 return tu;
1372 /* Free the seen types until we get to TU_TIL. */
1374 static void
1375 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1377 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1378 while (tu != tu_til)
1380 const struct tagged_tu_seen_cache *const tu1
1381 = (const struct tagged_tu_seen_cache *) tu;
1382 tu = tu1->next;
1383 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1385 tagged_tu_seen_base = tu_til;
1388 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1389 compatible. If the two types are not the same (which has been
1390 checked earlier), this can only happen when multiple translation
1391 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1392 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1393 comptypes_internal. */
1395 static int
1396 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1397 bool *enum_and_int_p, bool *different_types_p)
1399 tree s1, s2;
1400 bool needs_warning = false;
1402 /* We have to verify that the tags of the types are the same. This
1403 is harder than it looks because this may be a typedef, so we have
1404 to go look at the original type. It may even be a typedef of a
1405 typedef...
1406 In the case of compiler-created builtin structs the TYPE_DECL
1407 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1408 while (TYPE_NAME (t1)
1409 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1410 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1411 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1413 while (TYPE_NAME (t2)
1414 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1415 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1416 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1418 /* C90 didn't have the requirement that the two tags be the same. */
1419 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1420 return 0;
1422 /* C90 didn't say what happened if one or both of the types were
1423 incomplete; we choose to follow C99 rules here, which is that they
1424 are compatible. */
1425 if (TYPE_SIZE (t1) == NULL
1426 || TYPE_SIZE (t2) == NULL)
1427 return 1;
1430 const struct tagged_tu_seen_cache * tts_i;
1431 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1432 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1433 return tts_i->val;
1436 switch (TREE_CODE (t1))
1438 case ENUMERAL_TYPE:
1440 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1441 /* Speed up the case where the type values are in the same order. */
1442 tree tv1 = TYPE_VALUES (t1);
1443 tree tv2 = TYPE_VALUES (t2);
1445 if (tv1 == tv2)
1447 return 1;
1450 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1452 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1453 break;
1454 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1456 tu->val = 0;
1457 return 0;
1461 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1463 return 1;
1465 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1467 tu->val = 0;
1468 return 0;
1471 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1473 tu->val = 0;
1474 return 0;
1477 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1479 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1480 if (s2 == NULL
1481 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1483 tu->val = 0;
1484 return 0;
1487 return 1;
1490 case UNION_TYPE:
1492 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1493 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1495 tu->val = 0;
1496 return 0;
1499 /* Speed up the common case where the fields are in the same order. */
1500 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1501 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1503 int result;
1505 if (DECL_NAME (s1) != DECL_NAME (s2))
1506 break;
1507 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1508 enum_and_int_p, different_types_p);
1510 if (result != 1 && !DECL_NAME (s1))
1511 break;
1512 if (result == 0)
1514 tu->val = 0;
1515 return 0;
1517 if (result == 2)
1518 needs_warning = true;
1520 if (TREE_CODE (s1) == FIELD_DECL
1521 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1522 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1524 tu->val = 0;
1525 return 0;
1528 if (!s1 && !s2)
1530 tu->val = needs_warning ? 2 : 1;
1531 return tu->val;
1534 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1536 bool ok = false;
1538 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1539 if (DECL_NAME (s1) == DECL_NAME (s2))
1541 int result;
1543 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1544 enum_and_int_p,
1545 different_types_p);
1547 if (result != 1 && !DECL_NAME (s1))
1548 continue;
1549 if (result == 0)
1551 tu->val = 0;
1552 return 0;
1554 if (result == 2)
1555 needs_warning = true;
1557 if (TREE_CODE (s1) == FIELD_DECL
1558 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1559 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1560 break;
1562 ok = true;
1563 break;
1565 if (!ok)
1567 tu->val = 0;
1568 return 0;
1571 tu->val = needs_warning ? 2 : 10;
1572 return tu->val;
1575 case RECORD_TYPE:
1577 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1579 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1580 s1 && s2;
1581 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1583 int result;
1584 if (TREE_CODE (s1) != TREE_CODE (s2)
1585 || DECL_NAME (s1) != DECL_NAME (s2))
1586 break;
1587 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1588 enum_and_int_p, different_types_p);
1589 if (result == 0)
1590 break;
1591 if (result == 2)
1592 needs_warning = true;
1594 if (TREE_CODE (s1) == FIELD_DECL
1595 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1596 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1597 break;
1599 if (s1 && s2)
1600 tu->val = 0;
1601 else
1602 tu->val = needs_warning ? 2 : 1;
1603 return tu->val;
1606 default:
1607 gcc_unreachable ();
1611 /* Return 1 if two function types F1 and F2 are compatible.
1612 If either type specifies no argument types,
1613 the other must specify a fixed number of self-promoting arg types.
1614 Otherwise, if one type specifies only the number of arguments,
1615 the other must specify that number of self-promoting arg types.
1616 Otherwise, the argument types must match.
1617 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1619 static int
1620 function_types_compatible_p (const_tree f1, const_tree f2,
1621 bool *enum_and_int_p, bool *different_types_p)
1623 tree args1, args2;
1624 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1625 int val = 1;
1626 int val1;
1627 tree ret1, ret2;
1629 ret1 = TREE_TYPE (f1);
1630 ret2 = TREE_TYPE (f2);
1632 /* 'volatile' qualifiers on a function's return type used to mean
1633 the function is noreturn. */
1634 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1635 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1636 if (TYPE_VOLATILE (ret1))
1637 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1638 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1639 if (TYPE_VOLATILE (ret2))
1640 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1641 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1642 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1643 if (val == 0)
1644 return 0;
1646 args1 = TYPE_ARG_TYPES (f1);
1647 args2 = TYPE_ARG_TYPES (f2);
1649 if (different_types_p != NULL
1650 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1651 *different_types_p = true;
1653 /* An unspecified parmlist matches any specified parmlist
1654 whose argument types don't need default promotions. */
1656 if (args1 == NULL_TREE)
1658 if (!self_promoting_args_p (args2))
1659 return 0;
1660 /* If one of these types comes from a non-prototype fn definition,
1661 compare that with the other type's arglist.
1662 If they don't match, ask for a warning (but no error). */
1663 if (TYPE_ACTUAL_ARG_TYPES (f1)
1664 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1665 enum_and_int_p, different_types_p))
1666 val = 2;
1667 return val;
1669 if (args2 == NULL_TREE)
1671 if (!self_promoting_args_p (args1))
1672 return 0;
1673 if (TYPE_ACTUAL_ARG_TYPES (f2)
1674 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1675 enum_and_int_p, different_types_p))
1676 val = 2;
1677 return val;
1680 /* Both types have argument lists: compare them and propagate results. */
1681 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1682 different_types_p);
1683 return val1 != 1 ? val1 : val;
1686 /* Check two lists of types for compatibility, returning 0 for
1687 incompatible, 1 for compatible, or 2 for compatible with
1688 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1689 comptypes_internal. */
1691 static int
1692 type_lists_compatible_p (const_tree args1, const_tree args2,
1693 bool *enum_and_int_p, bool *different_types_p)
1695 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1696 int val = 1;
1697 int newval = 0;
1699 while (1)
1701 tree a1, mv1, a2, mv2;
1702 if (args1 == NULL_TREE && args2 == NULL_TREE)
1703 return val;
1704 /* If one list is shorter than the other,
1705 they fail to match. */
1706 if (args1 == NULL_TREE || args2 == NULL_TREE)
1707 return 0;
1708 mv1 = a1 = TREE_VALUE (args1);
1709 mv2 = a2 = TREE_VALUE (args2);
1710 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1711 mv1 = (TYPE_ATOMIC (mv1)
1712 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1713 TYPE_QUAL_ATOMIC)
1714 : TYPE_MAIN_VARIANT (mv1));
1715 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1716 mv2 = (TYPE_ATOMIC (mv2)
1717 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1718 TYPE_QUAL_ATOMIC)
1719 : TYPE_MAIN_VARIANT (mv2));
1720 /* A null pointer instead of a type
1721 means there is supposed to be an argument
1722 but nothing is specified about what type it has.
1723 So match anything that self-promotes. */
1724 if (different_types_p != NULL
1725 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1726 *different_types_p = true;
1727 if (a1 == NULL_TREE)
1729 if (c_type_promotes_to (a2) != a2)
1730 return 0;
1732 else if (a2 == NULL_TREE)
1734 if (c_type_promotes_to (a1) != a1)
1735 return 0;
1737 /* If one of the lists has an error marker, ignore this arg. */
1738 else if (TREE_CODE (a1) == ERROR_MARK
1739 || TREE_CODE (a2) == ERROR_MARK)
1741 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1742 different_types_p)))
1744 if (different_types_p != NULL)
1745 *different_types_p = true;
1746 /* Allow wait (union {union wait *u; int *i} *)
1747 and wait (union wait *) to be compatible. */
1748 if (TREE_CODE (a1) == UNION_TYPE
1749 && (TYPE_NAME (a1) == NULL_TREE
1750 || TYPE_TRANSPARENT_AGGR (a1))
1751 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1752 && tree_int_cst_equal (TYPE_SIZE (a1),
1753 TYPE_SIZE (a2)))
1755 tree memb;
1756 for (memb = TYPE_FIELDS (a1);
1757 memb; memb = DECL_CHAIN (memb))
1759 tree mv3 = TREE_TYPE (memb);
1760 if (mv3 && mv3 != error_mark_node
1761 && TREE_CODE (mv3) != ARRAY_TYPE)
1762 mv3 = (TYPE_ATOMIC (mv3)
1763 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1764 TYPE_QUAL_ATOMIC)
1765 : TYPE_MAIN_VARIANT (mv3));
1766 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1767 different_types_p))
1768 break;
1770 if (memb == NULL_TREE)
1771 return 0;
1773 else if (TREE_CODE (a2) == UNION_TYPE
1774 && (TYPE_NAME (a2) == NULL_TREE
1775 || TYPE_TRANSPARENT_AGGR (a2))
1776 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1777 && tree_int_cst_equal (TYPE_SIZE (a2),
1778 TYPE_SIZE (a1)))
1780 tree memb;
1781 for (memb = TYPE_FIELDS (a2);
1782 memb; memb = DECL_CHAIN (memb))
1784 tree mv3 = TREE_TYPE (memb);
1785 if (mv3 && mv3 != error_mark_node
1786 && TREE_CODE (mv3) != ARRAY_TYPE)
1787 mv3 = (TYPE_ATOMIC (mv3)
1788 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1789 TYPE_QUAL_ATOMIC)
1790 : TYPE_MAIN_VARIANT (mv3));
1791 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1792 different_types_p))
1793 break;
1795 if (memb == NULL_TREE)
1796 return 0;
1798 else
1799 return 0;
1802 /* comptypes said ok, but record if it said to warn. */
1803 if (newval > val)
1804 val = newval;
1806 args1 = TREE_CHAIN (args1);
1807 args2 = TREE_CHAIN (args2);
1811 /* Compute the size to increment a pointer by. When a function type or void
1812 type or incomplete type is passed, size_one_node is returned.
1813 This function does not emit any diagnostics; the caller is responsible
1814 for that. */
1816 static tree
1817 c_size_in_bytes (const_tree type)
1819 enum tree_code code = TREE_CODE (type);
1821 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1822 || !COMPLETE_TYPE_P (type))
1823 return size_one_node;
1825 /* Convert in case a char is more than one unit. */
1826 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1827 size_int (TYPE_PRECISION (char_type_node)
1828 / BITS_PER_UNIT));
1831 /* Return either DECL or its known constant value (if it has one). */
1833 tree
1834 decl_constant_value (tree decl)
1836 if (/* Don't change a variable array bound or initial value to a constant
1837 in a place where a variable is invalid. Note that DECL_INITIAL
1838 isn't valid for a PARM_DECL. */
1839 current_function_decl != NULL_TREE
1840 && TREE_CODE (decl) != PARM_DECL
1841 && !TREE_THIS_VOLATILE (decl)
1842 && TREE_READONLY (decl)
1843 && DECL_INITIAL (decl) != NULL_TREE
1844 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1845 /* This is invalid if initial value is not constant.
1846 If it has either a function call, a memory reference,
1847 or a variable, then re-evaluating it could give different results. */
1848 && TREE_CONSTANT (DECL_INITIAL (decl))
1849 /* Check for cases where this is sub-optimal, even though valid. */
1850 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1851 return DECL_INITIAL (decl);
1852 return decl;
1855 /* Convert the array expression EXP to a pointer. */
1856 static tree
1857 array_to_pointer_conversion (location_t loc, tree exp)
1859 tree orig_exp = exp;
1860 tree type = TREE_TYPE (exp);
1861 tree adr;
1862 tree restype = TREE_TYPE (type);
1863 tree ptrtype;
1865 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1867 STRIP_TYPE_NOPS (exp);
1869 if (TREE_NO_WARNING (orig_exp))
1870 TREE_NO_WARNING (exp) = 1;
1872 ptrtype = build_pointer_type (restype);
1874 if (INDIRECT_REF_P (exp))
1875 return convert (ptrtype, TREE_OPERAND (exp, 0));
1877 /* In C++ array compound literals are temporary objects unless they are
1878 const or appear in namespace scope, so they are destroyed too soon
1879 to use them for much of anything (c++/53220). */
1880 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1882 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1883 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1884 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1885 "converting an array compound literal to a pointer "
1886 "is ill-formed in C++");
1889 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1890 return convert (ptrtype, adr);
1893 /* Convert the function expression EXP to a pointer. */
1894 static tree
1895 function_to_pointer_conversion (location_t loc, tree exp)
1897 tree orig_exp = exp;
1899 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1901 STRIP_TYPE_NOPS (exp);
1903 if (TREE_NO_WARNING (orig_exp))
1904 TREE_NO_WARNING (exp) = 1;
1906 return build_unary_op (loc, ADDR_EXPR, exp, false);
1909 /* Mark EXP as read, not just set, for set but not used -Wunused
1910 warning purposes. */
1912 void
1913 mark_exp_read (tree exp)
1915 switch (TREE_CODE (exp))
1917 case VAR_DECL:
1918 case PARM_DECL:
1919 DECL_READ_P (exp) = 1;
1920 break;
1921 case ARRAY_REF:
1922 case COMPONENT_REF:
1923 case MODIFY_EXPR:
1924 case REALPART_EXPR:
1925 case IMAGPART_EXPR:
1926 CASE_CONVERT:
1927 case ADDR_EXPR:
1928 case VIEW_CONVERT_EXPR:
1929 mark_exp_read (TREE_OPERAND (exp, 0));
1930 break;
1931 case COMPOUND_EXPR:
1932 case C_MAYBE_CONST_EXPR:
1933 mark_exp_read (TREE_OPERAND (exp, 1));
1934 break;
1935 default:
1936 break;
1940 /* Perform the default conversion of arrays and functions to pointers.
1941 Return the result of converting EXP. For any other expression, just
1942 return EXP.
1944 LOC is the location of the expression. */
1946 struct c_expr
1947 default_function_array_conversion (location_t loc, struct c_expr exp)
1949 tree orig_exp = exp.value;
1950 tree type = TREE_TYPE (exp.value);
1951 enum tree_code code = TREE_CODE (type);
1953 switch (code)
1955 case ARRAY_TYPE:
1957 bool not_lvalue = false;
1958 bool lvalue_array_p;
1960 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1961 || CONVERT_EXPR_P (exp.value))
1962 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1964 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1965 not_lvalue = true;
1966 exp.value = TREE_OPERAND (exp.value, 0);
1969 if (TREE_NO_WARNING (orig_exp))
1970 TREE_NO_WARNING (exp.value) = 1;
1972 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1973 if (!flag_isoc99 && !lvalue_array_p)
1975 /* Before C99, non-lvalue arrays do not decay to pointers.
1976 Normally, using such an array would be invalid; but it can
1977 be used correctly inside sizeof or as a statement expression.
1978 Thus, do not give an error here; an error will result later. */
1979 return exp;
1982 exp.value = array_to_pointer_conversion (loc, exp.value);
1984 break;
1985 case FUNCTION_TYPE:
1986 exp.value = function_to_pointer_conversion (loc, exp.value);
1987 break;
1988 default:
1989 break;
1992 return exp;
1995 struct c_expr
1996 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1998 mark_exp_read (exp.value);
1999 return default_function_array_conversion (loc, exp);
2002 /* Return whether EXPR should be treated as an atomic lvalue for the
2003 purposes of load and store handling. */
2005 static bool
2006 really_atomic_lvalue (tree expr)
2008 if (error_operand_p (expr))
2009 return false;
2010 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2011 return false;
2012 if (!lvalue_p (expr))
2013 return false;
2015 /* Ignore _Atomic on register variables, since their addresses can't
2016 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2017 sequences wouldn't work. Ignore _Atomic on structures containing
2018 bit-fields, since accessing elements of atomic structures or
2019 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2020 it's undefined at translation time or execution time, and the
2021 normal atomic sequences again wouldn't work. */
2022 while (handled_component_p (expr))
2024 if (TREE_CODE (expr) == COMPONENT_REF
2025 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2026 return false;
2027 expr = TREE_OPERAND (expr, 0);
2029 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2030 return false;
2031 return true;
2034 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2035 including converting functions and arrays to pointers if CONVERT_P.
2036 If READ_P, also mark the expression as having been read. */
2038 struct c_expr
2039 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2040 bool convert_p, bool read_p)
2042 if (read_p)
2043 mark_exp_read (exp.value);
2044 if (convert_p)
2045 exp = default_function_array_conversion (loc, exp);
2046 if (really_atomic_lvalue (exp.value))
2048 vec<tree, va_gc> *params;
2049 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2050 tree expr_type = TREE_TYPE (exp.value);
2051 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2052 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2054 gcc_assert (TYPE_ATOMIC (expr_type));
2056 /* Expansion of a generic atomic load may require an addition
2057 element, so allocate enough to prevent a resize. */
2058 vec_alloc (params, 4);
2060 /* Remove the qualifiers for the rest of the expressions and
2061 create the VAL temp variable to hold the RHS. */
2062 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2063 tmp = create_tmp_var_raw (nonatomic_type);
2064 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2065 TREE_ADDRESSABLE (tmp) = 1;
2066 TREE_NO_WARNING (tmp) = 1;
2068 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2069 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2070 params->quick_push (expr_addr);
2071 params->quick_push (tmp_addr);
2072 params->quick_push (seq_cst);
2073 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2075 /* EXPR is always read. */
2076 mark_exp_read (exp.value);
2078 /* Return tmp which contains the value loaded. */
2079 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2080 NULL_TREE, NULL_TREE);
2082 return exp;
2085 /* EXP is an expression of integer type. Apply the integer promotions
2086 to it and return the promoted value. */
2088 tree
2089 perform_integral_promotions (tree exp)
2091 tree type = TREE_TYPE (exp);
2092 enum tree_code code = TREE_CODE (type);
2094 gcc_assert (INTEGRAL_TYPE_P (type));
2096 /* Normally convert enums to int,
2097 but convert wide enums to something wider. */
2098 if (code == ENUMERAL_TYPE)
2100 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2101 TYPE_PRECISION (integer_type_node)),
2102 ((TYPE_PRECISION (type)
2103 >= TYPE_PRECISION (integer_type_node))
2104 && TYPE_UNSIGNED (type)));
2106 return convert (type, exp);
2109 /* ??? This should no longer be needed now bit-fields have their
2110 proper types. */
2111 if (TREE_CODE (exp) == COMPONENT_REF
2112 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2113 /* If it's thinner than an int, promote it like a
2114 c_promoting_integer_type_p, otherwise leave it alone. */
2115 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2116 TYPE_PRECISION (integer_type_node)))
2117 return convert (integer_type_node, exp);
2119 if (c_promoting_integer_type_p (type))
2121 /* Preserve unsignedness if not really getting any wider. */
2122 if (TYPE_UNSIGNED (type)
2123 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2124 return convert (unsigned_type_node, exp);
2126 return convert (integer_type_node, exp);
2129 return exp;
2133 /* Perform default promotions for C data used in expressions.
2134 Enumeral types or short or char are converted to int.
2135 In addition, manifest constants symbols are replaced by their values. */
2137 tree
2138 default_conversion (tree exp)
2140 tree orig_exp;
2141 tree type = TREE_TYPE (exp);
2142 enum tree_code code = TREE_CODE (type);
2143 tree promoted_type;
2145 mark_exp_read (exp);
2147 /* Functions and arrays have been converted during parsing. */
2148 gcc_assert (code != FUNCTION_TYPE);
2149 if (code == ARRAY_TYPE)
2150 return exp;
2152 /* Constants can be used directly unless they're not loadable. */
2153 if (TREE_CODE (exp) == CONST_DECL)
2154 exp = DECL_INITIAL (exp);
2156 /* Strip no-op conversions. */
2157 orig_exp = exp;
2158 STRIP_TYPE_NOPS (exp);
2160 if (TREE_NO_WARNING (orig_exp))
2161 TREE_NO_WARNING (exp) = 1;
2163 if (code == VOID_TYPE)
2165 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2166 "void value not ignored as it ought to be");
2167 return error_mark_node;
2170 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2171 if (exp == error_mark_node)
2172 return error_mark_node;
2174 promoted_type = targetm.promoted_type (type);
2175 if (promoted_type)
2176 return convert (promoted_type, exp);
2178 if (INTEGRAL_TYPE_P (type))
2179 return perform_integral_promotions (exp);
2181 return exp;
2184 /* Look up COMPONENT in a structure or union TYPE.
2186 If the component name is not found, returns NULL_TREE. Otherwise,
2187 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2188 stepping down the chain to the component, which is in the last
2189 TREE_VALUE of the list. Normally the list is of length one, but if
2190 the component is embedded within (nested) anonymous structures or
2191 unions, the list steps down the chain to the component. */
2193 static tree
2194 lookup_field (tree type, tree component)
2196 tree field;
2198 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2199 to the field elements. Use a binary search on this array to quickly
2200 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2201 will always be set for structures which have many elements. */
2203 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2205 int bot, top, half;
2206 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2208 field = TYPE_FIELDS (type);
2209 bot = 0;
2210 top = TYPE_LANG_SPECIFIC (type)->s->len;
2211 while (top - bot > 1)
2213 half = (top - bot + 1) >> 1;
2214 field = field_array[bot+half];
2216 if (DECL_NAME (field) == NULL_TREE)
2218 /* Step through all anon unions in linear fashion. */
2219 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2221 field = field_array[bot++];
2222 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2224 tree anon = lookup_field (TREE_TYPE (field), component);
2226 if (anon)
2227 return tree_cons (NULL_TREE, field, anon);
2229 /* The Plan 9 compiler permits referring
2230 directly to an anonymous struct/union field
2231 using a typedef name. */
2232 if (flag_plan9_extensions
2233 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2234 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2235 == TYPE_DECL)
2236 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2237 == component))
2238 break;
2242 /* Entire record is only anon unions. */
2243 if (bot > top)
2244 return NULL_TREE;
2246 /* Restart the binary search, with new lower bound. */
2247 continue;
2250 if (DECL_NAME (field) == component)
2251 break;
2252 if (DECL_NAME (field) < component)
2253 bot += half;
2254 else
2255 top = bot + half;
2258 if (DECL_NAME (field_array[bot]) == component)
2259 field = field_array[bot];
2260 else if (DECL_NAME (field) != component)
2261 return NULL_TREE;
2263 else
2265 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2267 if (DECL_NAME (field) == NULL_TREE
2268 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2270 tree anon = lookup_field (TREE_TYPE (field), component);
2272 if (anon)
2273 return tree_cons (NULL_TREE, field, anon);
2275 /* The Plan 9 compiler permits referring directly to an
2276 anonymous struct/union field using a typedef
2277 name. */
2278 if (flag_plan9_extensions
2279 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2280 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2281 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2282 == component))
2283 break;
2286 if (DECL_NAME (field) == component)
2287 break;
2290 if (field == NULL_TREE)
2291 return NULL_TREE;
2294 return tree_cons (NULL_TREE, field, NULL_TREE);
2297 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2299 static void
2300 lookup_field_fuzzy_find_candidates (tree type, tree component,
2301 vec<tree> *candidates)
2303 tree field;
2304 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2306 if (DECL_NAME (field) == NULL_TREE
2307 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2308 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2309 candidates);
2311 if (DECL_NAME (field))
2312 candidates->safe_push (DECL_NAME (field));
2316 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2317 rather than returning a TREE_LIST for an exact match. */
2319 static tree
2320 lookup_field_fuzzy (tree type, tree component)
2322 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2324 /* First, gather a list of candidates. */
2325 auto_vec <tree> candidates;
2327 lookup_field_fuzzy_find_candidates (type, component,
2328 &candidates);
2330 return find_closest_identifier (component, &candidates);
2333 /* Support function for build_component_ref's error-handling.
2335 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2336 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2338 static bool
2339 should_suggest_deref_p (tree datum_type)
2341 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2342 allows "." for ptrs; we could be handling a failed attempt
2343 to access a property. */
2344 if (c_dialect_objc ())
2345 return false;
2347 /* Only suggest it for pointers... */
2348 if (TREE_CODE (datum_type) != POINTER_TYPE)
2349 return false;
2351 /* ...to structs/unions. */
2352 tree underlying_type = TREE_TYPE (datum_type);
2353 enum tree_code code = TREE_CODE (underlying_type);
2354 if (code == RECORD_TYPE || code == UNION_TYPE)
2355 return true;
2356 else
2357 return false;
2360 /* Make an expression to refer to the COMPONENT field of structure or
2361 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2362 location of the COMPONENT_REF. COMPONENT_LOC is the location
2363 of COMPONENT. */
2365 tree
2366 build_component_ref (location_t loc, tree datum, tree component,
2367 location_t component_loc)
2369 tree type = TREE_TYPE (datum);
2370 enum tree_code code = TREE_CODE (type);
2371 tree field = NULL;
2372 tree ref;
2373 bool datum_lvalue = lvalue_p (datum);
2375 if (!objc_is_public (datum, component))
2376 return error_mark_node;
2378 /* Detect Objective-C property syntax object.property. */
2379 if (c_dialect_objc ()
2380 && (ref = objc_maybe_build_component_ref (datum, component)))
2381 return ref;
2383 /* See if there is a field or component with name COMPONENT. */
2385 if (code == RECORD_TYPE || code == UNION_TYPE)
2387 if (!COMPLETE_TYPE_P (type))
2389 c_incomplete_type_error (loc, NULL_TREE, type);
2390 return error_mark_node;
2393 field = lookup_field (type, component);
2395 if (!field)
2397 tree guessed_id = lookup_field_fuzzy (type, component);
2398 if (guessed_id)
2400 /* Attempt to provide a fixit replacement hint, if
2401 we have a valid range for the component. */
2402 location_t reported_loc
2403 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2404 gcc_rich_location rich_loc (reported_loc);
2405 if (component_loc != UNKNOWN_LOCATION)
2406 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2407 error_at_rich_loc
2408 (&rich_loc,
2409 "%qT has no member named %qE; did you mean %qE?",
2410 type, component, guessed_id);
2412 else
2413 error_at (loc, "%qT has no member named %qE", type, component);
2414 return error_mark_node;
2417 /* Accessing elements of atomic structures or unions is undefined
2418 behavior (C11 6.5.2.3#5). */
2419 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2421 if (code == RECORD_TYPE)
2422 warning_at (loc, 0, "accessing a member %qE of an atomic "
2423 "structure %qE", component, datum);
2424 else
2425 warning_at (loc, 0, "accessing a member %qE of an atomic "
2426 "union %qE", component, datum);
2429 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2430 This might be better solved in future the way the C++ front
2431 end does it - by giving the anonymous entities each a
2432 separate name and type, and then have build_component_ref
2433 recursively call itself. We can't do that here. */
2436 tree subdatum = TREE_VALUE (field);
2437 int quals;
2438 tree subtype;
2439 bool use_datum_quals;
2441 if (TREE_TYPE (subdatum) == error_mark_node)
2442 return error_mark_node;
2444 /* If this is an rvalue, it does not have qualifiers in C
2445 standard terms and we must avoid propagating such
2446 qualifiers down to a non-lvalue array that is then
2447 converted to a pointer. */
2448 use_datum_quals = (datum_lvalue
2449 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2451 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2452 if (use_datum_quals)
2453 quals |= TYPE_QUALS (TREE_TYPE (datum));
2454 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2456 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2457 NULL_TREE);
2458 SET_EXPR_LOCATION (ref, loc);
2459 if (TREE_READONLY (subdatum)
2460 || (use_datum_quals && TREE_READONLY (datum)))
2461 TREE_READONLY (ref) = 1;
2462 if (TREE_THIS_VOLATILE (subdatum)
2463 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2464 TREE_THIS_VOLATILE (ref) = 1;
2466 if (TREE_DEPRECATED (subdatum))
2467 warn_deprecated_use (subdatum, NULL_TREE);
2469 datum = ref;
2471 field = TREE_CHAIN (field);
2473 while (field);
2475 return ref;
2477 else if (should_suggest_deref_p (type))
2479 /* Special-case the error message for "ptr.field" for the case
2480 where the user has confused "." vs "->". */
2481 rich_location richloc (line_table, loc);
2482 /* "loc" should be the "." token. */
2483 richloc.add_fixit_replace ("->");
2484 error_at_rich_loc (&richloc,
2485 "%qE is a pointer; did you mean to use %<->%>?",
2486 datum);
2487 return error_mark_node;
2489 else if (code != ERROR_MARK)
2490 error_at (loc,
2491 "request for member %qE in something not a structure or union",
2492 component);
2494 return error_mark_node;
2497 /* Given an expression PTR for a pointer, return an expression
2498 for the value pointed to.
2499 ERRORSTRING is the name of the operator to appear in error messages.
2501 LOC is the location to use for the generated tree. */
2503 tree
2504 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2506 tree pointer = default_conversion (ptr);
2507 tree type = TREE_TYPE (pointer);
2508 tree ref;
2510 if (TREE_CODE (type) == POINTER_TYPE)
2512 if (CONVERT_EXPR_P (pointer)
2513 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2515 /* If a warning is issued, mark it to avoid duplicates from
2516 the backend. This only needs to be done at
2517 warn_strict_aliasing > 2. */
2518 if (warn_strict_aliasing > 2)
2519 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2520 type, TREE_OPERAND (pointer, 0)))
2521 TREE_NO_WARNING (pointer) = 1;
2524 if (TREE_CODE (pointer) == ADDR_EXPR
2525 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2526 == TREE_TYPE (type)))
2528 ref = TREE_OPERAND (pointer, 0);
2529 protected_set_expr_location (ref, loc);
2530 return ref;
2532 else
2534 tree t = TREE_TYPE (type);
2536 ref = build1 (INDIRECT_REF, t, pointer);
2538 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2540 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2542 error_at (loc, "dereferencing pointer to incomplete type "
2543 "%qT", t);
2544 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2546 return error_mark_node;
2548 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2549 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2551 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2552 so that we get the proper error message if the result is used
2553 to assign to. Also, &* is supposed to be a no-op.
2554 And ANSI C seems to specify that the type of the result
2555 should be the const type. */
2556 /* A de-reference of a pointer to const is not a const. It is valid
2557 to change it via some other pointer. */
2558 TREE_READONLY (ref) = TYPE_READONLY (t);
2559 TREE_SIDE_EFFECTS (ref)
2560 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2561 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2562 protected_set_expr_location (ref, loc);
2563 return ref;
2566 else if (TREE_CODE (pointer) != ERROR_MARK)
2567 invalid_indirection_error (loc, type, errstring);
2569 return error_mark_node;
2572 /* This handles expressions of the form "a[i]", which denotes
2573 an array reference.
2575 This is logically equivalent in C to *(a+i), but we may do it differently.
2576 If A is a variable or a member, we generate a primitive ARRAY_REF.
2577 This avoids forcing the array out of registers, and can work on
2578 arrays that are not lvalues (for example, members of structures returned
2579 by functions).
2581 For vector types, allow vector[i] but not i[vector], and create
2582 *(((type*)&vectortype) + i) for the expression.
2584 LOC is the location to use for the returned expression. */
2586 tree
2587 build_array_ref (location_t loc, tree array, tree index)
2589 tree ret;
2590 bool swapped = false;
2591 if (TREE_TYPE (array) == error_mark_node
2592 || TREE_TYPE (index) == error_mark_node)
2593 return error_mark_node;
2595 if (flag_cilkplus && contains_array_notation_expr (index))
2597 size_t rank = 0;
2598 if (!find_rank (loc, index, index, true, &rank))
2599 return error_mark_node;
2600 if (rank > 1)
2602 error_at (loc, "rank of the array's index is greater than 1");
2603 return error_mark_node;
2606 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2607 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2608 /* Allow vector[index] but not index[vector]. */
2609 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2611 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2612 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2614 error_at (loc,
2615 "subscripted value is neither array nor pointer nor vector");
2617 return error_mark_node;
2619 std::swap (array, index);
2620 swapped = true;
2623 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2625 error_at (loc, "array subscript is not an integer");
2626 return error_mark_node;
2629 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2631 error_at (loc, "subscripted value is pointer to function");
2632 return error_mark_node;
2635 /* ??? Existing practice has been to warn only when the char
2636 index is syntactically the index, not for char[array]. */
2637 if (!swapped)
2638 warn_array_subscript_with_type_char (loc, index);
2640 /* Apply default promotions *after* noticing character types. */
2641 index = default_conversion (index);
2642 if (index == error_mark_node)
2643 return error_mark_node;
2645 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2647 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2648 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2650 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2652 tree rval, type;
2654 /* An array that is indexed by a non-constant
2655 cannot be stored in a register; we must be able to do
2656 address arithmetic on its address.
2657 Likewise an array of elements of variable size. */
2658 if (TREE_CODE (index) != INTEGER_CST
2659 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2660 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2662 if (!c_mark_addressable (array, true))
2663 return error_mark_node;
2665 /* An array that is indexed by a constant value which is not within
2666 the array bounds cannot be stored in a register either; because we
2667 would get a crash in store_bit_field/extract_bit_field when trying
2668 to access a non-existent part of the register. */
2669 if (TREE_CODE (index) == INTEGER_CST
2670 && TYPE_DOMAIN (TREE_TYPE (array))
2671 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2673 if (!c_mark_addressable (array))
2674 return error_mark_node;
2677 if ((pedantic || warn_c90_c99_compat)
2678 && ! was_vector)
2680 tree foo = array;
2681 while (TREE_CODE (foo) == COMPONENT_REF)
2682 foo = TREE_OPERAND (foo, 0);
2683 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2684 pedwarn (loc, OPT_Wpedantic,
2685 "ISO C forbids subscripting %<register%> array");
2686 else if (!lvalue_p (foo))
2687 pedwarn_c90 (loc, OPT_Wpedantic,
2688 "ISO C90 forbids subscripting non-lvalue "
2689 "array");
2692 type = TREE_TYPE (TREE_TYPE (array));
2693 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2694 /* Array ref is const/volatile if the array elements are
2695 or if the array is. */
2696 TREE_READONLY (rval)
2697 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2698 | TREE_READONLY (array));
2699 TREE_SIDE_EFFECTS (rval)
2700 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2701 | TREE_SIDE_EFFECTS (array));
2702 TREE_THIS_VOLATILE (rval)
2703 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2704 /* This was added by rms on 16 Nov 91.
2705 It fixes vol struct foo *a; a->elts[1]
2706 in an inline function.
2707 Hope it doesn't break something else. */
2708 | TREE_THIS_VOLATILE (array));
2709 ret = require_complete_type (loc, rval);
2710 protected_set_expr_location (ret, loc);
2711 if (non_lvalue)
2712 ret = non_lvalue_loc (loc, ret);
2713 return ret;
2715 else
2717 tree ar = default_conversion (array);
2719 if (ar == error_mark_node)
2720 return ar;
2722 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2723 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2725 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2726 index, 0),
2727 RO_ARRAY_INDEXING);
2728 if (non_lvalue)
2729 ret = non_lvalue_loc (loc, ret);
2730 return ret;
2734 /* Build an external reference to identifier ID. FUN indicates
2735 whether this will be used for a function call. LOC is the source
2736 location of the identifier. This sets *TYPE to the type of the
2737 identifier, which is not the same as the type of the returned value
2738 for CONST_DECLs defined as enum constants. If the type of the
2739 identifier is not available, *TYPE is set to NULL. */
2740 tree
2741 build_external_ref (location_t loc, tree id, int fun, tree *type)
2743 tree ref;
2744 tree decl = lookup_name (id);
2746 /* In Objective-C, an instance variable (ivar) may be preferred to
2747 whatever lookup_name() found. */
2748 decl = objc_lookup_ivar (decl, id);
2750 *type = NULL;
2751 if (decl && decl != error_mark_node)
2753 ref = decl;
2754 *type = TREE_TYPE (ref);
2756 else if (fun)
2757 /* Implicit function declaration. */
2758 ref = implicitly_declare (loc, id);
2759 else if (decl == error_mark_node)
2760 /* Don't complain about something that's already been
2761 complained about. */
2762 return error_mark_node;
2763 else
2765 undeclared_variable (loc, id);
2766 return error_mark_node;
2769 if (TREE_TYPE (ref) == error_mark_node)
2770 return error_mark_node;
2772 if (TREE_DEPRECATED (ref))
2773 warn_deprecated_use (ref, NULL_TREE);
2775 /* Recursive call does not count as usage. */
2776 if (ref != current_function_decl)
2778 TREE_USED (ref) = 1;
2781 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2783 if (!in_sizeof && !in_typeof)
2784 C_DECL_USED (ref) = 1;
2785 else if (DECL_INITIAL (ref) == NULL_TREE
2786 && DECL_EXTERNAL (ref)
2787 && !TREE_PUBLIC (ref))
2788 record_maybe_used_decl (ref);
2791 if (TREE_CODE (ref) == CONST_DECL)
2793 used_types_insert (TREE_TYPE (ref));
2795 if (warn_cxx_compat
2796 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2797 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2799 warning_at (loc, OPT_Wc___compat,
2800 ("enum constant defined in struct or union "
2801 "is not visible in C++"));
2802 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2805 ref = DECL_INITIAL (ref);
2806 TREE_CONSTANT (ref) = 1;
2808 else if (current_function_decl != NULL_TREE
2809 && !DECL_FILE_SCOPE_P (current_function_decl)
2810 && (VAR_OR_FUNCTION_DECL_P (ref)
2811 || TREE_CODE (ref) == PARM_DECL))
2813 tree context = decl_function_context (ref);
2815 if (context != NULL_TREE && context != current_function_decl)
2816 DECL_NONLOCAL (ref) = 1;
2818 /* C99 6.7.4p3: An inline definition of a function with external
2819 linkage ... shall not contain a reference to an identifier with
2820 internal linkage. */
2821 else if (current_function_decl != NULL_TREE
2822 && DECL_DECLARED_INLINE_P (current_function_decl)
2823 && DECL_EXTERNAL (current_function_decl)
2824 && VAR_OR_FUNCTION_DECL_P (ref)
2825 && (!VAR_P (ref) || TREE_STATIC (ref))
2826 && ! TREE_PUBLIC (ref)
2827 && DECL_CONTEXT (ref) != current_function_decl)
2828 record_inline_static (loc, current_function_decl, ref,
2829 csi_internal);
2831 return ref;
2834 /* Record details of decls possibly used inside sizeof or typeof. */
2835 struct maybe_used_decl
2837 /* The decl. */
2838 tree decl;
2839 /* The level seen at (in_sizeof + in_typeof). */
2840 int level;
2841 /* The next one at this level or above, or NULL. */
2842 struct maybe_used_decl *next;
2845 static struct maybe_used_decl *maybe_used_decls;
2847 /* Record that DECL, an undefined static function reference seen
2848 inside sizeof or typeof, might be used if the operand of sizeof is
2849 a VLA type or the operand of typeof is a variably modified
2850 type. */
2852 static void
2853 record_maybe_used_decl (tree decl)
2855 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2856 t->decl = decl;
2857 t->level = in_sizeof + in_typeof;
2858 t->next = maybe_used_decls;
2859 maybe_used_decls = t;
2862 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2863 USED is false, just discard them. If it is true, mark them used
2864 (if no longer inside sizeof or typeof) or move them to the next
2865 level up (if still inside sizeof or typeof). */
2867 void
2868 pop_maybe_used (bool used)
2870 struct maybe_used_decl *p = maybe_used_decls;
2871 int cur_level = in_sizeof + in_typeof;
2872 while (p && p->level > cur_level)
2874 if (used)
2876 if (cur_level == 0)
2877 C_DECL_USED (p->decl) = 1;
2878 else
2879 p->level = cur_level;
2881 p = p->next;
2883 if (!used || cur_level == 0)
2884 maybe_used_decls = p;
2887 /* Return the result of sizeof applied to EXPR. */
2889 struct c_expr
2890 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2892 struct c_expr ret;
2893 if (expr.value == error_mark_node)
2895 ret.value = error_mark_node;
2896 ret.original_code = ERROR_MARK;
2897 ret.original_type = NULL;
2898 pop_maybe_used (false);
2900 else
2902 bool expr_const_operands = true;
2904 if (TREE_CODE (expr.value) == PARM_DECL
2905 && C_ARRAY_PARAMETER (expr.value))
2907 if (warning_at (loc, OPT_Wsizeof_array_argument,
2908 "%<sizeof%> on array function parameter %qE will "
2909 "return size of %qT", expr.value,
2910 expr.original_type))
2911 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2913 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2914 &expr_const_operands);
2915 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2916 c_last_sizeof_arg = expr.value;
2917 c_last_sizeof_loc = loc;
2918 ret.original_code = SIZEOF_EXPR;
2919 ret.original_type = NULL;
2920 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2922 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2923 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2924 folded_expr, ret.value);
2925 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2926 SET_EXPR_LOCATION (ret.value, loc);
2928 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2930 return ret;
2933 /* Return the result of sizeof applied to T, a structure for the type
2934 name passed to sizeof (rather than the type itself). LOC is the
2935 location of the original expression. */
2937 struct c_expr
2938 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2940 tree type;
2941 struct c_expr ret;
2942 tree type_expr = NULL_TREE;
2943 bool type_expr_const = true;
2944 type = groktypename (t, &type_expr, &type_expr_const);
2945 ret.value = c_sizeof (loc, type);
2946 c_last_sizeof_arg = type;
2947 c_last_sizeof_loc = loc;
2948 ret.original_code = SIZEOF_EXPR;
2949 ret.original_type = NULL;
2950 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2951 && c_vla_type_p (type))
2953 /* If the type is a [*] array, it is a VLA but is represented as
2954 having a size of zero. In such a case we must ensure that
2955 the result of sizeof does not get folded to a constant by
2956 c_fully_fold, because if the size is evaluated the result is
2957 not constant and so constraints on zero or negative size
2958 arrays must not be applied when this sizeof call is inside
2959 another array declarator. */
2960 if (!type_expr)
2961 type_expr = integer_zero_node;
2962 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2963 type_expr, ret.value);
2964 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2966 pop_maybe_used (type != error_mark_node
2967 ? C_TYPE_VARIABLE_SIZE (type) : false);
2968 return ret;
2971 /* Build a function call to function FUNCTION with parameters PARAMS.
2972 The function call is at LOC.
2973 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2974 TREE_VALUE of each node is a parameter-expression.
2975 FUNCTION's data type may be a function type or a pointer-to-function. */
2977 tree
2978 build_function_call (location_t loc, tree function, tree params)
2980 vec<tree, va_gc> *v;
2981 tree ret;
2983 vec_alloc (v, list_length (params));
2984 for (; params; params = TREE_CHAIN (params))
2985 v->quick_push (TREE_VALUE (params));
2986 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2987 vec_free (v);
2988 return ret;
2991 /* Give a note about the location of the declaration of DECL. */
2993 static void
2994 inform_declaration (tree decl)
2996 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2997 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3000 /* Build a function call to function FUNCTION with parameters PARAMS.
3001 ORIGTYPES, if not NULL, is a vector of types; each element is
3002 either NULL or the original type of the corresponding element in
3003 PARAMS. The original type may differ from TREE_TYPE of the
3004 parameter for enums. FUNCTION's data type may be a function type
3005 or pointer-to-function. This function changes the elements of
3006 PARAMS. */
3008 tree
3009 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3010 tree function, vec<tree, va_gc> *params,
3011 vec<tree, va_gc> *origtypes)
3013 tree fntype, fundecl = NULL_TREE;
3014 tree name = NULL_TREE, result;
3015 tree tem;
3016 int nargs;
3017 tree *argarray;
3020 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3021 STRIP_TYPE_NOPS (function);
3023 /* Convert anything with function type to a pointer-to-function. */
3024 if (TREE_CODE (function) == FUNCTION_DECL)
3026 name = DECL_NAME (function);
3028 if (flag_tm)
3029 tm_malloc_replacement (function);
3030 fundecl = function;
3031 /* Atomic functions have type checking/casting already done. They are
3032 often rewritten and don't match the original parameter list. */
3033 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3034 origtypes = NULL;
3036 if (flag_cilkplus
3037 && is_cilkplus_reduce_builtin (function))
3038 origtypes = NULL;
3040 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3041 function = function_to_pointer_conversion (loc, function);
3043 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3044 expressions, like those used for ObjC messenger dispatches. */
3045 if (params && !params->is_empty ())
3046 function = objc_rewrite_function_call (function, (*params)[0]);
3048 function = c_fully_fold (function, false, NULL);
3050 fntype = TREE_TYPE (function);
3052 if (TREE_CODE (fntype) == ERROR_MARK)
3053 return error_mark_node;
3055 if (!(TREE_CODE (fntype) == POINTER_TYPE
3056 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3058 if (!flag_diagnostics_show_caret)
3059 error_at (loc,
3060 "called object %qE is not a function or function pointer",
3061 function);
3062 else if (DECL_P (function))
3064 error_at (loc,
3065 "called object %qD is not a function or function pointer",
3066 function);
3067 inform_declaration (function);
3069 else
3070 error_at (loc,
3071 "called object is not a function or function pointer");
3072 return error_mark_node;
3075 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3076 current_function_returns_abnormally = 1;
3078 /* fntype now gets the type of function pointed to. */
3079 fntype = TREE_TYPE (fntype);
3081 /* Convert the parameters to the types declared in the
3082 function prototype, or apply default promotions. */
3084 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3085 origtypes, function, fundecl);
3086 if (nargs < 0)
3087 return error_mark_node;
3089 /* Check that the function is called through a compatible prototype.
3090 If it is not, warn. */
3091 if (CONVERT_EXPR_P (function)
3092 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3093 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3094 && !comptypes (fntype, TREE_TYPE (tem)))
3096 tree return_type = TREE_TYPE (fntype);
3098 /* This situation leads to run-time undefined behavior. We can't,
3099 therefore, simply error unless we can prove that all possible
3100 executions of the program must execute the code. */
3101 warning_at (loc, 0, "function called through a non-compatible type");
3103 if (VOID_TYPE_P (return_type)
3104 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3105 pedwarn (loc, 0,
3106 "function with qualified void return type called");
3109 argarray = vec_safe_address (params);
3111 /* Check that arguments to builtin functions match the expectations. */
3112 if (fundecl
3113 && DECL_BUILT_IN (fundecl)
3114 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3115 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3116 argarray))
3117 return error_mark_node;
3119 /* Check that the arguments to the function are valid. */
3120 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3121 nargs, argarray);
3123 if (name != NULL_TREE
3124 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3126 if (require_constant_value)
3127 result
3128 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3129 function, nargs, argarray);
3130 else
3131 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3132 function, nargs, argarray);
3133 if (TREE_CODE (result) == NOP_EXPR
3134 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3135 STRIP_TYPE_NOPS (result);
3137 else
3138 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3139 function, nargs, argarray);
3140 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3141 later. */
3142 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3143 TREE_NO_WARNING (result) = 1;
3145 /* In this improbable scenario, a nested function returns a VM type.
3146 Create a TARGET_EXPR so that the call always has a LHS, much as
3147 what the C++ FE does for functions returning non-PODs. */
3148 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3150 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3151 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3152 NULL_TREE, NULL_TREE);
3155 if (VOID_TYPE_P (TREE_TYPE (result)))
3157 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3158 pedwarn (loc, 0,
3159 "function with qualified void return type called");
3160 return result;
3162 return require_complete_type (loc, result);
3165 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3167 tree
3168 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3169 tree function, vec<tree, va_gc> *params,
3170 vec<tree, va_gc> *origtypes)
3172 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3173 STRIP_TYPE_NOPS (function);
3175 /* Convert anything with function type to a pointer-to-function. */
3176 if (TREE_CODE (function) == FUNCTION_DECL)
3178 /* Implement type-directed function overloading for builtins.
3179 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3180 handle all the type checking. The result is a complete expression
3181 that implements this function call. */
3182 tree tem = resolve_overloaded_builtin (loc, function, params);
3183 if (tem)
3184 return tem;
3186 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3189 /* Convert the argument expressions in the vector VALUES
3190 to the types in the list TYPELIST.
3192 If TYPELIST is exhausted, or when an element has NULL as its type,
3193 perform the default conversions.
3195 ORIGTYPES is the original types of the expressions in VALUES. This
3196 holds the type of enum values which have been converted to integral
3197 types. It may be NULL.
3199 FUNCTION is a tree for the called function. It is used only for
3200 error messages, where it is formatted with %qE.
3202 This is also where warnings about wrong number of args are generated.
3204 ARG_LOC are locations of function arguments (if any).
3206 Returns the actual number of arguments processed (which may be less
3207 than the length of VALUES in some error situations), or -1 on
3208 failure. */
3210 static int
3211 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3212 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3213 tree function, tree fundecl)
3215 tree typetail, val;
3216 unsigned int parmnum;
3217 bool error_args = false;
3218 const bool type_generic = fundecl
3219 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3220 bool type_generic_remove_excess_precision = false;
3221 bool type_generic_overflow_p = false;
3222 tree selector;
3224 /* Change pointer to function to the function itself for
3225 diagnostics. */
3226 if (TREE_CODE (function) == ADDR_EXPR
3227 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3228 function = TREE_OPERAND (function, 0);
3230 /* Handle an ObjC selector specially for diagnostics. */
3231 selector = objc_message_selector ();
3233 /* For type-generic built-in functions, determine whether excess
3234 precision should be removed (classification) or not
3235 (comparison). */
3236 if (type_generic
3237 && DECL_BUILT_IN (fundecl)
3238 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3240 switch (DECL_FUNCTION_CODE (fundecl))
3242 case BUILT_IN_ISFINITE:
3243 case BUILT_IN_ISINF:
3244 case BUILT_IN_ISINF_SIGN:
3245 case BUILT_IN_ISNAN:
3246 case BUILT_IN_ISNORMAL:
3247 case BUILT_IN_FPCLASSIFY:
3248 type_generic_remove_excess_precision = true;
3249 break;
3251 case BUILT_IN_ADD_OVERFLOW_P:
3252 case BUILT_IN_SUB_OVERFLOW_P:
3253 case BUILT_IN_MUL_OVERFLOW_P:
3254 /* The last argument of these type-generic builtins
3255 should not be promoted. */
3256 type_generic_overflow_p = true;
3257 break;
3259 default:
3260 break;
3263 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3264 return vec_safe_length (values);
3266 /* Scan the given expressions and types, producing individual
3267 converted arguments. */
3269 for (typetail = typelist, parmnum = 0;
3270 values && values->iterate (parmnum, &val);
3271 ++parmnum)
3273 tree type = typetail ? TREE_VALUE (typetail) : 0;
3274 tree valtype = TREE_TYPE (val);
3275 tree rname = function;
3276 int argnum = parmnum + 1;
3277 const char *invalid_func_diag;
3278 bool excess_precision = false;
3279 bool npc;
3280 tree parmval;
3281 /* Some __atomic_* builtins have additional hidden argument at
3282 position 0. */
3283 location_t ploc
3284 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3285 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3286 : input_location;
3288 if (type == void_type_node)
3290 if (selector)
3291 error_at (loc, "too many arguments to method %qE", selector);
3292 else
3293 error_at (loc, "too many arguments to function %qE", function);
3294 inform_declaration (fundecl);
3295 return error_args ? -1 : (int) parmnum;
3298 if (selector && argnum > 2)
3300 rname = selector;
3301 argnum -= 2;
3304 npc = null_pointer_constant_p (val);
3306 /* If there is excess precision and a prototype, convert once to
3307 the required type rather than converting via the semantic
3308 type. Likewise without a prototype a float value represented
3309 as long double should be converted once to double. But for
3310 type-generic classification functions excess precision must
3311 be removed here. */
3312 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3313 && (type || !type_generic || !type_generic_remove_excess_precision))
3315 val = TREE_OPERAND (val, 0);
3316 excess_precision = true;
3318 val = c_fully_fold (val, false, NULL);
3319 STRIP_TYPE_NOPS (val);
3321 val = require_complete_type (ploc, val);
3323 /* Some floating-point arguments must be promoted to double when
3324 no type is specified by a prototype. This applies to
3325 arguments of type float, and to architecture-specific types
3326 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3327 bool promote_float_arg = false;
3328 if (type == NULL_TREE
3329 && TREE_CODE (valtype) == REAL_TYPE
3330 && (TYPE_PRECISION (valtype)
3331 <= TYPE_PRECISION (double_type_node))
3332 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3333 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3334 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3336 /* Promote this argument, unless it has a _FloatN or
3337 _FloatNx type. */
3338 promote_float_arg = true;
3339 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3340 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3342 promote_float_arg = false;
3343 break;
3347 if (type != NULL_TREE)
3349 /* Formal parm type is specified by a function prototype. */
3351 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3353 error_at (ploc, "type of formal parameter %d is incomplete",
3354 parmnum + 1);
3355 parmval = val;
3357 else
3359 tree origtype;
3361 /* Optionally warn about conversions that
3362 differ from the default conversions. */
3363 if (warn_traditional_conversion || warn_traditional)
3365 unsigned int formal_prec = TYPE_PRECISION (type);
3367 if (INTEGRAL_TYPE_P (type)
3368 && TREE_CODE (valtype) == REAL_TYPE)
3369 warning_at (ploc, OPT_Wtraditional_conversion,
3370 "passing argument %d of %qE as integer rather "
3371 "than floating due to prototype",
3372 argnum, rname);
3373 if (INTEGRAL_TYPE_P (type)
3374 && TREE_CODE (valtype) == COMPLEX_TYPE)
3375 warning_at (ploc, OPT_Wtraditional_conversion,
3376 "passing argument %d of %qE as integer rather "
3377 "than complex due to prototype",
3378 argnum, rname);
3379 else if (TREE_CODE (type) == COMPLEX_TYPE
3380 && TREE_CODE (valtype) == REAL_TYPE)
3381 warning_at (ploc, OPT_Wtraditional_conversion,
3382 "passing argument %d of %qE as complex rather "
3383 "than floating due to prototype",
3384 argnum, rname);
3385 else if (TREE_CODE (type) == REAL_TYPE
3386 && INTEGRAL_TYPE_P (valtype))
3387 warning_at (ploc, OPT_Wtraditional_conversion,
3388 "passing argument %d of %qE as floating rather "
3389 "than integer due to prototype",
3390 argnum, rname);
3391 else if (TREE_CODE (type) == COMPLEX_TYPE
3392 && INTEGRAL_TYPE_P (valtype))
3393 warning_at (ploc, OPT_Wtraditional_conversion,
3394 "passing argument %d of %qE as complex rather "
3395 "than integer due to prototype",
3396 argnum, rname);
3397 else if (TREE_CODE (type) == REAL_TYPE
3398 && TREE_CODE (valtype) == COMPLEX_TYPE)
3399 warning_at (ploc, OPT_Wtraditional_conversion,
3400 "passing argument %d of %qE as floating rather "
3401 "than complex due to prototype",
3402 argnum, rname);
3403 /* ??? At some point, messages should be written about
3404 conversions between complex types, but that's too messy
3405 to do now. */
3406 else if (TREE_CODE (type) == REAL_TYPE
3407 && TREE_CODE (valtype) == REAL_TYPE)
3409 /* Warn if any argument is passed as `float',
3410 since without a prototype it would be `double'. */
3411 if (formal_prec == TYPE_PRECISION (float_type_node)
3412 && type != dfloat32_type_node)
3413 warning_at (ploc, 0,
3414 "passing argument %d of %qE as %<float%> "
3415 "rather than %<double%> due to prototype",
3416 argnum, rname);
3418 /* Warn if mismatch between argument and prototype
3419 for decimal float types. Warn of conversions with
3420 binary float types and of precision narrowing due to
3421 prototype. */
3422 else if (type != valtype
3423 && (type == dfloat32_type_node
3424 || type == dfloat64_type_node
3425 || type == dfloat128_type_node
3426 || valtype == dfloat32_type_node
3427 || valtype == dfloat64_type_node
3428 || valtype == dfloat128_type_node)
3429 && (formal_prec
3430 <= TYPE_PRECISION (valtype)
3431 || (type == dfloat128_type_node
3432 && (valtype
3433 != dfloat64_type_node
3434 && (valtype
3435 != dfloat32_type_node)))
3436 || (type == dfloat64_type_node
3437 && (valtype
3438 != dfloat32_type_node))))
3439 warning_at (ploc, 0,
3440 "passing argument %d of %qE as %qT "
3441 "rather than %qT due to prototype",
3442 argnum, rname, type, valtype);
3445 /* Detect integer changing in width or signedness.
3446 These warnings are only activated with
3447 -Wtraditional-conversion, not with -Wtraditional. */
3448 else if (warn_traditional_conversion
3449 && INTEGRAL_TYPE_P (type)
3450 && INTEGRAL_TYPE_P (valtype))
3452 tree would_have_been = default_conversion (val);
3453 tree type1 = TREE_TYPE (would_have_been);
3455 if (val == error_mark_node)
3456 /* VAL could have been of incomplete type. */;
3457 else if (TREE_CODE (type) == ENUMERAL_TYPE
3458 && (TYPE_MAIN_VARIANT (type)
3459 == TYPE_MAIN_VARIANT (valtype)))
3460 /* No warning if function asks for enum
3461 and the actual arg is that enum type. */
3463 else if (formal_prec != TYPE_PRECISION (type1))
3464 warning_at (ploc, OPT_Wtraditional_conversion,
3465 "passing argument %d of %qE "
3466 "with different width due to prototype",
3467 argnum, rname);
3468 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3470 /* Don't complain if the formal parameter type
3471 is an enum, because we can't tell now whether
3472 the value was an enum--even the same enum. */
3473 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3475 else if (TREE_CODE (val) == INTEGER_CST
3476 && int_fits_type_p (val, type))
3477 /* Change in signedness doesn't matter
3478 if a constant value is unaffected. */
3480 /* If the value is extended from a narrower
3481 unsigned type, it doesn't matter whether we
3482 pass it as signed or unsigned; the value
3483 certainly is the same either way. */
3484 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3485 && TYPE_UNSIGNED (valtype))
3487 else if (TYPE_UNSIGNED (type))
3488 warning_at (ploc, OPT_Wtraditional_conversion,
3489 "passing argument %d of %qE "
3490 "as unsigned due to prototype",
3491 argnum, rname);
3492 else
3493 warning_at (ploc, OPT_Wtraditional_conversion,
3494 "passing argument %d of %qE "
3495 "as signed due to prototype",
3496 argnum, rname);
3500 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3501 sake of better warnings from convert_and_check. */
3502 if (excess_precision)
3503 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3504 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3505 parmval = convert_for_assignment (loc, ploc, type,
3506 val, origtype, ic_argpass,
3507 npc, fundecl, function,
3508 parmnum + 1);
3510 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3511 && INTEGRAL_TYPE_P (type)
3512 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3513 parmval = default_conversion (parmval);
3516 else if (promote_float_arg)
3518 if (type_generic)
3519 parmval = val;
3520 else
3522 /* Convert `float' to `double'. */
3523 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3524 warning_at (ploc, OPT_Wdouble_promotion,
3525 "implicit conversion from %qT to %qT when passing "
3526 "argument to function",
3527 valtype, double_type_node);
3528 parmval = convert (double_type_node, val);
3531 else if ((excess_precision && !type_generic)
3532 || (type_generic_overflow_p && parmnum == 2))
3533 /* A "double" argument with excess precision being passed
3534 without a prototype or in variable arguments.
3535 The last argument of __builtin_*_overflow_p should not be
3536 promoted. */
3537 parmval = convert (valtype, val);
3538 else if ((invalid_func_diag =
3539 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3541 error (invalid_func_diag);
3542 return -1;
3544 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3546 return -1;
3548 else
3549 /* Convert `short' and `char' to full-size `int'. */
3550 parmval = default_conversion (val);
3552 (*values)[parmnum] = parmval;
3553 if (parmval == error_mark_node)
3554 error_args = true;
3556 if (typetail)
3557 typetail = TREE_CHAIN (typetail);
3560 gcc_assert (parmnum == vec_safe_length (values));
3562 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3564 error_at (loc, "too few arguments to function %qE", function);
3565 inform_declaration (fundecl);
3566 return -1;
3569 return error_args ? -1 : (int) parmnum;
3572 /* This is the entry point used by the parser to build unary operators
3573 in the input. CODE, a tree_code, specifies the unary operator, and
3574 ARG is the operand. For unary plus, the C parser currently uses
3575 CONVERT_EXPR for code.
3577 LOC is the location to use for the tree generated.
3580 struct c_expr
3581 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3583 struct c_expr result;
3585 result.original_code = code;
3586 result.original_type = NULL;
3588 if (reject_gcc_builtin (arg.value))
3590 result.value = error_mark_node;
3592 else
3594 result.value = build_unary_op (loc, code, arg.value, false);
3596 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3597 overflow_warning (loc, result.value, arg.value);
3600 /* We are typically called when parsing a prefix token at LOC acting on
3601 ARG. Reflect this by updating the source range of the result to
3602 start at LOC and end at the end of ARG. */
3603 set_c_expr_source_range (&result,
3604 loc, arg.get_finish ());
3606 return result;
3609 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3611 static bool
3612 char_type_p (tree type)
3614 return (type == char_type_node
3615 || type == unsigned_char_type_node
3616 || type == signed_char_type_node
3617 || type == char16_type_node
3618 || type == char32_type_node);
3621 /* This is the entry point used by the parser to build binary operators
3622 in the input. CODE, a tree_code, specifies the binary operator, and
3623 ARG1 and ARG2 are the operands. In addition to constructing the
3624 expression, we check for operands that were written with other binary
3625 operators in a way that is likely to confuse the user.
3627 LOCATION is the location of the binary operator. */
3629 struct c_expr
3630 parser_build_binary_op (location_t location, enum tree_code code,
3631 struct c_expr arg1, struct c_expr arg2)
3633 struct c_expr result;
3635 enum tree_code code1 = arg1.original_code;
3636 enum tree_code code2 = arg2.original_code;
3637 tree type1 = (arg1.original_type
3638 ? arg1.original_type
3639 : TREE_TYPE (arg1.value));
3640 tree type2 = (arg2.original_type
3641 ? arg2.original_type
3642 : TREE_TYPE (arg2.value));
3644 result.value = build_binary_op (location, code,
3645 arg1.value, arg2.value, 1);
3646 result.original_code = code;
3647 result.original_type = NULL;
3649 if (TREE_CODE (result.value) == ERROR_MARK)
3651 set_c_expr_source_range (&result,
3652 arg1.get_start (),
3653 arg2.get_finish ());
3654 return result;
3657 if (location != UNKNOWN_LOCATION)
3658 protected_set_expr_location (result.value, location);
3660 set_c_expr_source_range (&result,
3661 arg1.get_start (),
3662 arg2.get_finish ());
3664 /* Check for cases such as x+y<<z which users are likely
3665 to misinterpret. */
3666 if (warn_parentheses)
3667 warn_about_parentheses (location, code, code1, arg1.value, code2,
3668 arg2.value);
3670 if (warn_logical_op)
3671 warn_logical_operator (location, code, TREE_TYPE (result.value),
3672 code1, arg1.value, code2, arg2.value);
3674 if (warn_tautological_compare)
3676 tree lhs = arg1.value;
3677 tree rhs = arg2.value;
3678 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3680 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3681 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3682 lhs = NULL_TREE;
3683 else
3684 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3686 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3688 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3689 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3690 rhs = NULL_TREE;
3691 else
3692 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3694 if (lhs != NULL_TREE && rhs != NULL_TREE)
3695 warn_tautological_cmp (location, code, lhs, rhs);
3698 if (warn_logical_not_paren
3699 && TREE_CODE_CLASS (code) == tcc_comparison
3700 && code1 == TRUTH_NOT_EXPR
3701 && code2 != TRUTH_NOT_EXPR
3702 /* Avoid warning for !!x == y. */
3703 && (TREE_CODE (arg1.value) != NE_EXPR
3704 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3706 /* Avoid warning for !b == y where b has _Bool type. */
3707 tree t = integer_zero_node;
3708 if (TREE_CODE (arg1.value) == EQ_EXPR
3709 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3710 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3712 t = TREE_OPERAND (arg1.value, 0);
3715 if (TREE_TYPE (t) != integer_type_node)
3716 break;
3717 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3718 t = C_MAYBE_CONST_EXPR_EXPR (t);
3719 else if (CONVERT_EXPR_P (t))
3720 t = TREE_OPERAND (t, 0);
3721 else
3722 break;
3724 while (1);
3726 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3727 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3730 /* Warn about comparisons against string literals, with the exception
3731 of testing for equality or inequality of a string literal with NULL. */
3732 if (code == EQ_EXPR || code == NE_EXPR)
3734 if ((code1 == STRING_CST
3735 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3736 || (code2 == STRING_CST
3737 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3738 warning_at (location, OPT_Waddress,
3739 "comparison with string literal results in unspecified behavior");
3740 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3741 if (POINTER_TYPE_P (type1)
3742 && null_pointer_constant_p (arg2.value)
3743 && char_type_p (type2)
3744 && warning_at (location, OPT_Wpointer_compare,
3745 "comparison between pointer and zero character "
3746 "constant"))
3747 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3748 else if (POINTER_TYPE_P (type2)
3749 && null_pointer_constant_p (arg1.value)
3750 && char_type_p (type1)
3751 && warning_at (location, OPT_Wpointer_compare,
3752 "comparison between pointer and zero character "
3753 "constant"))
3754 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3756 else if (TREE_CODE_CLASS (code) == tcc_comparison
3757 && (code1 == STRING_CST || code2 == STRING_CST))
3758 warning_at (location, OPT_Waddress,
3759 "comparison with string literal results in unspecified behavior");
3761 if (TREE_OVERFLOW_P (result.value)
3762 && !TREE_OVERFLOW_P (arg1.value)
3763 && !TREE_OVERFLOW_P (arg2.value))
3764 overflow_warning (location, result.value);
3766 /* Warn about comparisons of different enum types. */
3767 if (warn_enum_compare
3768 && TREE_CODE_CLASS (code) == tcc_comparison
3769 && TREE_CODE (type1) == ENUMERAL_TYPE
3770 && TREE_CODE (type2) == ENUMERAL_TYPE
3771 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3772 warning_at (location, OPT_Wenum_compare,
3773 "comparison between %qT and %qT",
3774 type1, type2);
3776 return result;
3779 /* Return a tree for the difference of pointers OP0 and OP1.
3780 The resulting tree has type int. */
3782 static tree
3783 pointer_diff (location_t loc, tree op0, tree op1)
3785 tree restype = ptrdiff_type_node;
3786 tree result, inttype;
3788 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3789 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3790 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3791 tree orig_op1 = op1;
3793 /* If the operands point into different address spaces, we need to
3794 explicitly convert them to pointers into the common address space
3795 before we can subtract the numerical address values. */
3796 if (as0 != as1)
3798 addr_space_t as_common;
3799 tree common_type;
3801 /* Determine the common superset address space. This is guaranteed
3802 to exist because the caller verified that comp_target_types
3803 returned non-zero. */
3804 if (!addr_space_superset (as0, as1, &as_common))
3805 gcc_unreachable ();
3807 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3808 op0 = convert (common_type, op0);
3809 op1 = convert (common_type, op1);
3812 /* Determine integer type to perform computations in. This will usually
3813 be the same as the result type (ptrdiff_t), but may need to be a wider
3814 type if pointers for the address space are wider than ptrdiff_t. */
3815 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3816 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3817 else
3818 inttype = restype;
3820 if (TREE_CODE (target_type) == VOID_TYPE)
3821 pedwarn (loc, OPT_Wpointer_arith,
3822 "pointer of type %<void *%> used in subtraction");
3823 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3824 pedwarn (loc, OPT_Wpointer_arith,
3825 "pointer to a function used in subtraction");
3827 /* First do the subtraction as integers;
3828 then drop through to build the divide operator.
3829 Do not do default conversions on the minus operator
3830 in case restype is a short type. */
3832 op0 = build_binary_op (loc,
3833 MINUS_EXPR, convert (inttype, op0),
3834 convert (inttype, op1), 0);
3835 /* This generates an error if op1 is pointer to incomplete type. */
3836 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3837 error_at (loc, "arithmetic on pointer to an incomplete type");
3839 op1 = c_size_in_bytes (target_type);
3841 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3842 error_at (loc, "arithmetic on pointer to an empty aggregate");
3844 /* Divide by the size, in easiest possible way. */
3845 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3846 op0, convert (inttype, op1));
3848 /* Convert to final result type if necessary. */
3849 return convert (restype, result);
3852 /* Expand atomic compound assignments into an appropriate sequence as
3853 specified by the C11 standard section 6.5.16.2.
3855 _Atomic T1 E1
3856 T2 E2
3857 E1 op= E2
3859 This sequence is used for all types for which these operations are
3860 supported.
3862 In addition, built-in versions of the 'fe' prefixed routines may
3863 need to be invoked for floating point (real, complex or vector) when
3864 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3866 T1 newval;
3867 T1 old;
3868 T1 *addr
3869 T2 val
3870 fenv_t fenv
3872 addr = &E1;
3873 val = (E2);
3874 __atomic_load (addr, &old, SEQ_CST);
3875 feholdexcept (&fenv);
3876 loop:
3877 newval = old op val;
3878 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3879 SEQ_CST))
3880 goto done;
3881 feclearexcept (FE_ALL_EXCEPT);
3882 goto loop:
3883 done:
3884 feupdateenv (&fenv);
3886 The compiler will issue the __atomic_fetch_* built-in when possible,
3887 otherwise it will generate the generic form of the atomic operations.
3888 This requires temp(s) and has their address taken. The atomic processing
3889 is smart enough to figure out when the size of an object can utilize
3890 a lock-free version, and convert the built-in call to the appropriate
3891 lock-free routine. The optimizers will then dispose of any temps that
3892 are no longer required, and lock-free implementations are utilized as
3893 long as there is target support for the required size.
3895 If the operator is NOP_EXPR, then this is a simple assignment, and
3896 an __atomic_store is issued to perform the assignment rather than
3897 the above loop. */
3899 /* Build an atomic assignment at LOC, expanding into the proper
3900 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3901 the result of the operation, unless RETURN_OLD_P, in which case
3902 return the old value of LHS (this is only for postincrement and
3903 postdecrement). */
3905 static tree
3906 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3907 tree rhs, bool return_old_p)
3909 tree fndecl, func_call;
3910 vec<tree, va_gc> *params;
3911 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3912 tree old, old_addr;
3913 tree compound_stmt;
3914 tree stmt, goto_stmt;
3915 tree loop_label, loop_decl, done_label, done_decl;
3917 tree lhs_type = TREE_TYPE (lhs);
3918 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3919 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3920 tree rhs_type = TREE_TYPE (rhs);
3922 gcc_assert (TYPE_ATOMIC (lhs_type));
3924 if (return_old_p)
3925 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3927 /* Allocate enough vector items for a compare_exchange. */
3928 vec_alloc (params, 6);
3930 /* Create a compound statement to hold the sequence of statements
3931 with a loop. */
3932 compound_stmt = c_begin_compound_stmt (false);
3934 /* Fold the RHS if it hasn't already been folded. */
3935 if (modifycode != NOP_EXPR)
3936 rhs = c_fully_fold (rhs, false, NULL);
3938 /* Remove the qualifiers for the rest of the expressions and create
3939 the VAL temp variable to hold the RHS. */
3940 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3941 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3942 val = create_tmp_var_raw (nonatomic_rhs_type);
3943 TREE_ADDRESSABLE (val) = 1;
3944 TREE_NO_WARNING (val) = 1;
3945 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3946 NULL_TREE);
3947 SET_EXPR_LOCATION (rhs, loc);
3948 add_stmt (rhs);
3950 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3951 an atomic_store. */
3952 if (modifycode == NOP_EXPR)
3954 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3955 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3956 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3957 params->quick_push (lhs_addr);
3958 params->quick_push (rhs);
3959 params->quick_push (seq_cst);
3960 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3961 add_stmt (func_call);
3963 /* Finish the compound statement. */
3964 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3966 /* VAL is the value which was stored, return a COMPOUND_STMT of
3967 the statement and that value. */
3968 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3971 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3972 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3973 isn't applicable for such builtins. ??? Do we want to handle enums? */
3974 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3975 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3977 built_in_function fncode;
3978 switch (modifycode)
3980 case PLUS_EXPR:
3981 case POINTER_PLUS_EXPR:
3982 fncode = (return_old_p
3983 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3984 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3985 break;
3986 case MINUS_EXPR:
3987 fncode = (return_old_p
3988 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3989 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3990 break;
3991 case BIT_AND_EXPR:
3992 fncode = (return_old_p
3993 ? BUILT_IN_ATOMIC_FETCH_AND_N
3994 : BUILT_IN_ATOMIC_AND_FETCH_N);
3995 break;
3996 case BIT_IOR_EXPR:
3997 fncode = (return_old_p
3998 ? BUILT_IN_ATOMIC_FETCH_OR_N
3999 : BUILT_IN_ATOMIC_OR_FETCH_N);
4000 break;
4001 case BIT_XOR_EXPR:
4002 fncode = (return_old_p
4003 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4004 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4005 break;
4006 default:
4007 goto cas_loop;
4010 /* We can only use "_1" through "_16" variants of the atomic fetch
4011 built-ins. */
4012 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4013 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4014 goto cas_loop;
4016 /* If this is a pointer type, we need to multiply by the size of
4017 the pointer target type. */
4018 if (POINTER_TYPE_P (lhs_type))
4020 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4021 /* ??? This would introduce -Wdiscarded-qualifiers
4022 warning: __atomic_fetch_* expect volatile void *
4023 type as the first argument. (Assignments between
4024 atomic and non-atomic objects are OK.) */
4025 || TYPE_RESTRICT (lhs_type))
4026 goto cas_loop;
4027 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4028 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4029 convert (ptrdiff_type_node, rhs),
4030 convert (ptrdiff_type_node, sz));
4033 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4034 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4035 fndecl = builtin_decl_explicit (fncode);
4036 params->quick_push (lhs_addr);
4037 params->quick_push (rhs);
4038 params->quick_push (seq_cst);
4039 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4041 newval = create_tmp_var_raw (nonatomic_lhs_type);
4042 TREE_ADDRESSABLE (newval) = 1;
4043 TREE_NO_WARNING (newval) = 1;
4044 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4045 NULL_TREE, NULL_TREE);
4046 SET_EXPR_LOCATION (rhs, loc);
4047 add_stmt (rhs);
4049 /* Finish the compound statement. */
4050 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4052 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4053 the statement and that value. */
4054 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4057 cas_loop:
4058 /* Create the variables and labels required for the op= form. */
4059 old = create_tmp_var_raw (nonatomic_lhs_type);
4060 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4061 TREE_ADDRESSABLE (old) = 1;
4062 TREE_NO_WARNING (old) = 1;
4064 newval = create_tmp_var_raw (nonatomic_lhs_type);
4065 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4066 TREE_ADDRESSABLE (newval) = 1;
4067 TREE_NO_WARNING (newval) = 1;
4069 loop_decl = create_artificial_label (loc);
4070 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4072 done_decl = create_artificial_label (loc);
4073 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4075 /* __atomic_load (addr, &old, SEQ_CST). */
4076 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4077 params->quick_push (lhs_addr);
4078 params->quick_push (old_addr);
4079 params->quick_push (seq_cst);
4080 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4081 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4082 NULL_TREE);
4083 add_stmt (old);
4084 params->truncate (0);
4086 /* Create the expressions for floating-point environment
4087 manipulation, if required. */
4088 bool need_fenv = (flag_trapping_math
4089 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4090 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4091 if (need_fenv)
4092 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4094 if (hold_call)
4095 add_stmt (hold_call);
4097 /* loop: */
4098 add_stmt (loop_label);
4100 /* newval = old + val; */
4101 rhs = build_binary_op (loc, modifycode, old, val, 1);
4102 rhs = c_fully_fold (rhs, false, NULL);
4103 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4104 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4105 NULL_TREE, 0);
4106 if (rhs != error_mark_node)
4108 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4109 NULL_TREE);
4110 SET_EXPR_LOCATION (rhs, loc);
4111 add_stmt (rhs);
4114 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4115 goto done; */
4116 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4117 params->quick_push (lhs_addr);
4118 params->quick_push (old_addr);
4119 params->quick_push (newval_addr);
4120 params->quick_push (integer_zero_node);
4121 params->quick_push (seq_cst);
4122 params->quick_push (seq_cst);
4123 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4125 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4126 SET_EXPR_LOCATION (goto_stmt, loc);
4128 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4129 SET_EXPR_LOCATION (stmt, loc);
4130 add_stmt (stmt);
4132 if (clear_call)
4133 add_stmt (clear_call);
4135 /* goto loop; */
4136 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4137 SET_EXPR_LOCATION (goto_stmt, loc);
4138 add_stmt (goto_stmt);
4140 /* done: */
4141 add_stmt (done_label);
4143 if (update_call)
4144 add_stmt (update_call);
4146 /* Finish the compound statement. */
4147 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4149 /* NEWVAL is the value that was successfully stored, return a
4150 COMPOUND_EXPR of the statement and the appropriate value. */
4151 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4152 return_old_p ? old : newval);
4155 /* Construct and perhaps optimize a tree representation
4156 for a unary operation. CODE, a tree_code, specifies the operation
4157 and XARG is the operand.
4158 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4159 promotions (such as from short to int).
4160 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4161 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4162 to pointers in C99.
4164 LOCATION is the location of the operator. */
4166 tree
4167 build_unary_op (location_t location, enum tree_code code, tree xarg,
4168 bool noconvert)
4170 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4171 tree arg = xarg;
4172 tree argtype = NULL_TREE;
4173 enum tree_code typecode;
4174 tree val;
4175 tree ret = error_mark_node;
4176 tree eptype = NULL_TREE;
4177 const char *invalid_op_diag;
4178 bool int_operands;
4180 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4181 if (int_operands)
4182 arg = remove_c_maybe_const_expr (arg);
4184 if (code != ADDR_EXPR)
4185 arg = require_complete_type (location, arg);
4187 typecode = TREE_CODE (TREE_TYPE (arg));
4188 if (typecode == ERROR_MARK)
4189 return error_mark_node;
4190 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4191 typecode = INTEGER_TYPE;
4193 if ((invalid_op_diag
4194 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4196 error_at (location, invalid_op_diag);
4197 return error_mark_node;
4200 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4202 eptype = TREE_TYPE (arg);
4203 arg = TREE_OPERAND (arg, 0);
4206 switch (code)
4208 case CONVERT_EXPR:
4209 /* This is used for unary plus, because a CONVERT_EXPR
4210 is enough to prevent anybody from looking inside for
4211 associativity, but won't generate any code. */
4212 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4213 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4214 || typecode == VECTOR_TYPE))
4216 error_at (location, "wrong type argument to unary plus");
4217 return error_mark_node;
4219 else if (!noconvert)
4220 arg = default_conversion (arg);
4221 arg = non_lvalue_loc (location, arg);
4222 break;
4224 case NEGATE_EXPR:
4225 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4226 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4227 || typecode == VECTOR_TYPE))
4229 error_at (location, "wrong type argument to unary minus");
4230 return error_mark_node;
4232 else if (!noconvert)
4233 arg = default_conversion (arg);
4234 break;
4236 case BIT_NOT_EXPR:
4237 /* ~ works on integer types and non float vectors. */
4238 if (typecode == INTEGER_TYPE
4239 || (typecode == VECTOR_TYPE
4240 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4242 tree e = arg;
4244 /* Warn if the expression has boolean value. */
4245 while (TREE_CODE (e) == COMPOUND_EXPR)
4246 e = TREE_OPERAND (e, 1);
4248 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4249 || truth_value_p (TREE_CODE (e)))
4250 && warning_at (location, OPT_Wbool_operation,
4251 "%<~%> on a boolean expression"))
4253 gcc_rich_location richloc (location);
4254 richloc.add_fixit_insert_before (location, "!");
4255 inform_at_rich_loc (&richloc, "did you mean to use logical "
4256 "not?");
4258 if (!noconvert)
4259 arg = default_conversion (arg);
4261 else if (typecode == COMPLEX_TYPE)
4263 code = CONJ_EXPR;
4264 pedwarn (location, OPT_Wpedantic,
4265 "ISO C does not support %<~%> for complex conjugation");
4266 if (!noconvert)
4267 arg = default_conversion (arg);
4269 else
4271 error_at (location, "wrong type argument to bit-complement");
4272 return error_mark_node;
4274 break;
4276 case ABS_EXPR:
4277 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4279 error_at (location, "wrong type argument to abs");
4280 return error_mark_node;
4282 else if (!noconvert)
4283 arg = default_conversion (arg);
4284 break;
4286 case CONJ_EXPR:
4287 /* Conjugating a real value is a no-op, but allow it anyway. */
4288 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4289 || typecode == COMPLEX_TYPE))
4291 error_at (location, "wrong type argument to conjugation");
4292 return error_mark_node;
4294 else if (!noconvert)
4295 arg = default_conversion (arg);
4296 break;
4298 case TRUTH_NOT_EXPR:
4299 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4300 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4301 && typecode != COMPLEX_TYPE)
4303 error_at (location,
4304 "wrong type argument to unary exclamation mark");
4305 return error_mark_node;
4307 if (int_operands)
4309 arg = c_objc_common_truthvalue_conversion (location, xarg);
4310 arg = remove_c_maybe_const_expr (arg);
4312 else
4313 arg = c_objc_common_truthvalue_conversion (location, arg);
4314 ret = invert_truthvalue_loc (location, arg);
4315 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4316 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4317 location = EXPR_LOCATION (ret);
4318 goto return_build_unary_op;
4320 case REALPART_EXPR:
4321 case IMAGPART_EXPR:
4322 ret = build_real_imag_expr (location, code, arg);
4323 if (ret == error_mark_node)
4324 return error_mark_node;
4325 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4326 eptype = TREE_TYPE (eptype);
4327 goto return_build_unary_op;
4329 case PREINCREMENT_EXPR:
4330 case POSTINCREMENT_EXPR:
4331 case PREDECREMENT_EXPR:
4332 case POSTDECREMENT_EXPR:
4334 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4336 tree inner = build_unary_op (location, code,
4337 C_MAYBE_CONST_EXPR_EXPR (arg),
4338 noconvert);
4339 if (inner == error_mark_node)
4340 return error_mark_node;
4341 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4342 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4343 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4344 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4345 goto return_build_unary_op;
4348 /* Complain about anything that is not a true lvalue. In
4349 Objective-C, skip this check for property_refs. */
4350 if (!objc_is_property_ref (arg)
4351 && !lvalue_or_else (location,
4352 arg, ((code == PREINCREMENT_EXPR
4353 || code == POSTINCREMENT_EXPR)
4354 ? lv_increment
4355 : lv_decrement)))
4356 return error_mark_node;
4358 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4360 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4361 warning_at (location, OPT_Wc___compat,
4362 "increment of enumeration value is invalid in C++");
4363 else
4364 warning_at (location, OPT_Wc___compat,
4365 "decrement of enumeration value is invalid in C++");
4368 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4370 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4371 warning_at (location, OPT_Wbool_operation,
4372 "increment of a boolean expression");
4373 else
4374 warning_at (location, OPT_Wbool_operation,
4375 "decrement of a boolean expression");
4378 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4379 arg = c_fully_fold (arg, false, NULL);
4381 bool atomic_op;
4382 atomic_op = really_atomic_lvalue (arg);
4384 /* Increment or decrement the real part of the value,
4385 and don't change the imaginary part. */
4386 if (typecode == COMPLEX_TYPE)
4388 tree real, imag;
4390 pedwarn (location, OPT_Wpedantic,
4391 "ISO C does not support %<++%> and %<--%> on complex types");
4393 if (!atomic_op)
4395 arg = stabilize_reference (arg);
4396 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4397 true);
4398 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4399 true);
4400 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4401 if (real == error_mark_node || imag == error_mark_node)
4402 return error_mark_node;
4403 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4404 real, imag);
4405 goto return_build_unary_op;
4409 /* Report invalid types. */
4411 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4412 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4413 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4415 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4416 error_at (location, "wrong type argument to increment");
4417 else
4418 error_at (location, "wrong type argument to decrement");
4420 return error_mark_node;
4424 tree inc;
4426 argtype = TREE_TYPE (arg);
4428 /* Compute the increment. */
4430 if (typecode == POINTER_TYPE)
4432 /* If pointer target is an incomplete type,
4433 we just cannot know how to do the arithmetic. */
4434 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4436 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4437 error_at (location,
4438 "increment of pointer to an incomplete type %qT",
4439 TREE_TYPE (argtype));
4440 else
4441 error_at (location,
4442 "decrement of pointer to an incomplete type %qT",
4443 TREE_TYPE (argtype));
4445 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4446 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4448 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4449 pedwarn (location, OPT_Wpointer_arith,
4450 "wrong type argument to increment");
4451 else
4452 pedwarn (location, OPT_Wpointer_arith,
4453 "wrong type argument to decrement");
4456 inc = c_size_in_bytes (TREE_TYPE (argtype));
4457 inc = convert_to_ptrofftype_loc (location, inc);
4459 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4461 /* For signed fract types, we invert ++ to -- or
4462 -- to ++, and change inc from 1 to -1, because
4463 it is not possible to represent 1 in signed fract constants.
4464 For unsigned fract types, the result always overflows and
4465 we get an undefined (original) or the maximum value. */
4466 if (code == PREINCREMENT_EXPR)
4467 code = PREDECREMENT_EXPR;
4468 else if (code == PREDECREMENT_EXPR)
4469 code = PREINCREMENT_EXPR;
4470 else if (code == POSTINCREMENT_EXPR)
4471 code = POSTDECREMENT_EXPR;
4472 else /* code == POSTDECREMENT_EXPR */
4473 code = POSTINCREMENT_EXPR;
4475 inc = integer_minus_one_node;
4476 inc = convert (argtype, inc);
4478 else
4480 inc = VECTOR_TYPE_P (argtype)
4481 ? build_one_cst (argtype)
4482 : integer_one_node;
4483 inc = convert (argtype, inc);
4486 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4487 need to ask Objective-C to build the increment or decrement
4488 expression for it. */
4489 if (objc_is_property_ref (arg))
4490 return objc_build_incr_expr_for_property_ref (location, code,
4491 arg, inc);
4493 /* Report a read-only lvalue. */
4494 if (TYPE_READONLY (argtype))
4496 readonly_error (location, arg,
4497 ((code == PREINCREMENT_EXPR
4498 || code == POSTINCREMENT_EXPR)
4499 ? lv_increment : lv_decrement));
4500 return error_mark_node;
4502 else if (TREE_READONLY (arg))
4503 readonly_warning (arg,
4504 ((code == PREINCREMENT_EXPR
4505 || code == POSTINCREMENT_EXPR)
4506 ? lv_increment : lv_decrement));
4508 /* If the argument is atomic, use the special code sequences for
4509 atomic compound assignment. */
4510 if (atomic_op)
4512 arg = stabilize_reference (arg);
4513 ret = build_atomic_assign (location, arg,
4514 ((code == PREINCREMENT_EXPR
4515 || code == POSTINCREMENT_EXPR)
4516 ? PLUS_EXPR
4517 : MINUS_EXPR),
4518 (FRACT_MODE_P (TYPE_MODE (argtype))
4519 ? inc
4520 : integer_one_node),
4521 (code == POSTINCREMENT_EXPR
4522 || code == POSTDECREMENT_EXPR));
4523 goto return_build_unary_op;
4526 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4527 val = boolean_increment (code, arg);
4528 else
4529 val = build2 (code, TREE_TYPE (arg), arg, inc);
4530 TREE_SIDE_EFFECTS (val) = 1;
4531 if (TREE_CODE (val) != code)
4532 TREE_NO_WARNING (val) = 1;
4533 ret = val;
4534 goto return_build_unary_op;
4537 case ADDR_EXPR:
4538 /* Note that this operation never does default_conversion. */
4540 /* The operand of unary '&' must be an lvalue (which excludes
4541 expressions of type void), or, in C99, the result of a [] or
4542 unary '*' operator. */
4543 if (VOID_TYPE_P (TREE_TYPE (arg))
4544 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4545 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4546 pedwarn (location, 0, "taking address of expression of type %<void%>");
4548 /* Let &* cancel out to simplify resulting code. */
4549 if (INDIRECT_REF_P (arg))
4551 /* Don't let this be an lvalue. */
4552 if (lvalue_p (TREE_OPERAND (arg, 0)))
4553 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4554 ret = TREE_OPERAND (arg, 0);
4555 goto return_build_unary_op;
4558 /* Anything not already handled and not a true memory reference
4559 or a non-lvalue array is an error. */
4560 if (typecode != FUNCTION_TYPE && !noconvert
4561 && !lvalue_or_else (location, arg, lv_addressof))
4562 return error_mark_node;
4564 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4565 folding later. */
4566 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4568 tree inner = build_unary_op (location, code,
4569 C_MAYBE_CONST_EXPR_EXPR (arg),
4570 noconvert);
4571 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4572 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4573 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4574 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4575 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4576 goto return_build_unary_op;
4579 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4580 argtype = TREE_TYPE (arg);
4582 /* If the lvalue is const or volatile, merge that into the type
4583 to which the address will point. This is only needed
4584 for function types. */
4585 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4586 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4587 && TREE_CODE (argtype) == FUNCTION_TYPE)
4589 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4590 int quals = orig_quals;
4592 if (TREE_READONLY (arg))
4593 quals |= TYPE_QUAL_CONST;
4594 if (TREE_THIS_VOLATILE (arg))
4595 quals |= TYPE_QUAL_VOLATILE;
4597 argtype = c_build_qualified_type (argtype, quals);
4600 switch (TREE_CODE (arg))
4602 case COMPONENT_REF:
4603 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4605 error_at (location, "cannot take address of bit-field %qD",
4606 TREE_OPERAND (arg, 1));
4607 return error_mark_node;
4610 /* fall through */
4612 case ARRAY_REF:
4613 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4615 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4616 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4618 error_at (location, "cannot take address of scalar with "
4619 "reverse storage order");
4620 return error_mark_node;
4623 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4624 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4625 warning_at (location, OPT_Wscalar_storage_order,
4626 "address of array with reverse scalar storage "
4627 "order requested");
4630 default:
4631 break;
4634 if (!c_mark_addressable (arg))
4635 return error_mark_node;
4637 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4638 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4640 argtype = build_pointer_type (argtype);
4642 /* ??? Cope with user tricks that amount to offsetof. Delete this
4643 when we have proper support for integer constant expressions. */
4644 val = get_base_address (arg);
4645 if (val && INDIRECT_REF_P (val)
4646 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4648 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4649 goto return_build_unary_op;
4652 val = build1 (ADDR_EXPR, argtype, arg);
4654 ret = val;
4655 goto return_build_unary_op;
4657 default:
4658 gcc_unreachable ();
4661 if (argtype == NULL_TREE)
4662 argtype = TREE_TYPE (arg);
4663 if (TREE_CODE (arg) == INTEGER_CST)
4664 ret = (require_constant_value
4665 ? fold_build1_initializer_loc (location, code, argtype, arg)
4666 : fold_build1_loc (location, code, argtype, arg));
4667 else
4668 ret = build1 (code, argtype, arg);
4669 return_build_unary_op:
4670 gcc_assert (ret != error_mark_node);
4671 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4672 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4673 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4674 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4675 ret = note_integer_operands (ret);
4676 if (eptype)
4677 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4678 protected_set_expr_location (ret, location);
4679 return ret;
4682 /* Return nonzero if REF is an lvalue valid for this language.
4683 Lvalues can be assigned, unless their type has TYPE_READONLY.
4684 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4686 bool
4687 lvalue_p (const_tree ref)
4689 const enum tree_code code = TREE_CODE (ref);
4691 switch (code)
4693 case REALPART_EXPR:
4694 case IMAGPART_EXPR:
4695 case COMPONENT_REF:
4696 return lvalue_p (TREE_OPERAND (ref, 0));
4698 case C_MAYBE_CONST_EXPR:
4699 return lvalue_p (TREE_OPERAND (ref, 1));
4701 case COMPOUND_LITERAL_EXPR:
4702 case STRING_CST:
4703 return true;
4705 case INDIRECT_REF:
4706 case ARRAY_REF:
4707 case ARRAY_NOTATION_REF:
4708 case VAR_DECL:
4709 case PARM_DECL:
4710 case RESULT_DECL:
4711 case ERROR_MARK:
4712 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4713 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4715 case BIND_EXPR:
4716 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4718 default:
4719 return false;
4723 /* Give a warning for storing in something that is read-only in GCC
4724 terms but not const in ISO C terms. */
4726 static void
4727 readonly_warning (tree arg, enum lvalue_use use)
4729 switch (use)
4731 case lv_assign:
4732 warning (0, "assignment of read-only location %qE", arg);
4733 break;
4734 case lv_increment:
4735 warning (0, "increment of read-only location %qE", arg);
4736 break;
4737 case lv_decrement:
4738 warning (0, "decrement of read-only location %qE", arg);
4739 break;
4740 default:
4741 gcc_unreachable ();
4743 return;
4747 /* Return nonzero if REF is an lvalue valid for this language;
4748 otherwise, print an error message and return zero. USE says
4749 how the lvalue is being used and so selects the error message.
4750 LOCATION is the location at which any error should be reported. */
4752 static int
4753 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4755 int win = lvalue_p (ref);
4757 if (!win)
4758 lvalue_error (loc, use);
4760 return win;
4763 /* Mark EXP saying that we need to be able to take the
4764 address of it; it should not be allocated in a register.
4765 Returns true if successful. ARRAY_REF_P is true if this
4766 is for ARRAY_REF construction - in that case we don't want
4767 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4768 it is fine to use ARRAY_REFs for vector subscripts on vector
4769 register variables. */
4771 bool
4772 c_mark_addressable (tree exp, bool array_ref_p)
4774 tree x = exp;
4776 while (1)
4777 switch (TREE_CODE (x))
4779 case VIEW_CONVERT_EXPR:
4780 if (array_ref_p
4781 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4782 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4783 return true;
4784 /* FALLTHRU */
4785 case COMPONENT_REF:
4786 case ADDR_EXPR:
4787 case ARRAY_REF:
4788 case REALPART_EXPR:
4789 case IMAGPART_EXPR:
4790 x = TREE_OPERAND (x, 0);
4791 break;
4793 case COMPOUND_LITERAL_EXPR:
4794 case CONSTRUCTOR:
4795 TREE_ADDRESSABLE (x) = 1;
4796 return true;
4798 case VAR_DECL:
4799 case CONST_DECL:
4800 case PARM_DECL:
4801 case RESULT_DECL:
4802 if (C_DECL_REGISTER (x)
4803 && DECL_NONLOCAL (x))
4805 if (TREE_PUBLIC (x) || is_global_var (x))
4807 error
4808 ("global register variable %qD used in nested function", x);
4809 return false;
4811 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4813 else if (C_DECL_REGISTER (x))
4815 if (TREE_PUBLIC (x) || is_global_var (x))
4816 error ("address of global register variable %qD requested", x);
4817 else
4818 error ("address of register variable %qD requested", x);
4819 return false;
4822 /* FALLTHRU */
4823 case FUNCTION_DECL:
4824 TREE_ADDRESSABLE (x) = 1;
4825 /* FALLTHRU */
4826 default:
4827 return true;
4831 /* Convert EXPR to TYPE, warning about conversion problems with
4832 constants. SEMANTIC_TYPE is the type this conversion would use
4833 without excess precision. If SEMANTIC_TYPE is NULL, this function
4834 is equivalent to convert_and_check. This function is a wrapper that
4835 handles conversions that may be different than
4836 the usual ones because of excess precision. */
4838 static tree
4839 ep_convert_and_check (location_t loc, tree type, tree expr,
4840 tree semantic_type)
4842 if (TREE_TYPE (expr) == type)
4843 return expr;
4845 if (!semantic_type)
4846 return convert_and_check (loc, type, expr);
4848 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4849 && TREE_TYPE (expr) != semantic_type)
4851 /* For integers, we need to check the real conversion, not
4852 the conversion to the excess precision type. */
4853 expr = convert_and_check (loc, semantic_type, expr);
4855 /* Result type is the excess precision type, which should be
4856 large enough, so do not check. */
4857 return convert (type, expr);
4860 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4861 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4862 if folded to an integer constant then the unselected half may
4863 contain arbitrary operations not normally permitted in constant
4864 expressions. Set the location of the expression to LOC. */
4866 tree
4867 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4868 tree op1, tree op1_original_type, location_t op1_loc,
4869 tree op2, tree op2_original_type, location_t op2_loc)
4871 tree type1;
4872 tree type2;
4873 enum tree_code code1;
4874 enum tree_code code2;
4875 tree result_type = NULL;
4876 tree semantic_result_type = NULL;
4877 tree orig_op1 = op1, orig_op2 = op2;
4878 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4879 bool ifexp_int_operands;
4880 tree ret;
4882 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4883 if (op1_int_operands)
4884 op1 = remove_c_maybe_const_expr (op1);
4885 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4886 if (op2_int_operands)
4887 op2 = remove_c_maybe_const_expr (op2);
4888 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4889 if (ifexp_int_operands)
4890 ifexp = remove_c_maybe_const_expr (ifexp);
4892 /* Promote both alternatives. */
4894 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4895 op1 = default_conversion (op1);
4896 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4897 op2 = default_conversion (op2);
4899 if (TREE_CODE (ifexp) == ERROR_MARK
4900 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4901 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4902 return error_mark_node;
4904 type1 = TREE_TYPE (op1);
4905 code1 = TREE_CODE (type1);
4906 type2 = TREE_TYPE (op2);
4907 code2 = TREE_CODE (type2);
4909 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4910 return error_mark_node;
4912 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4913 return error_mark_node;
4915 /* C90 does not permit non-lvalue arrays in conditional expressions.
4916 In C99 they will be pointers by now. */
4917 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4919 error_at (colon_loc, "non-lvalue array in conditional expression");
4920 return error_mark_node;
4923 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4924 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4925 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4926 || code1 == COMPLEX_TYPE)
4927 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4928 || code2 == COMPLEX_TYPE))
4930 semantic_result_type = c_common_type (type1, type2);
4931 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4933 op1 = TREE_OPERAND (op1, 0);
4934 type1 = TREE_TYPE (op1);
4935 gcc_assert (TREE_CODE (type1) == code1);
4937 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4939 op2 = TREE_OPERAND (op2, 0);
4940 type2 = TREE_TYPE (op2);
4941 gcc_assert (TREE_CODE (type2) == code2);
4945 if (warn_cxx_compat)
4947 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4948 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4950 if (TREE_CODE (t1) == ENUMERAL_TYPE
4951 && TREE_CODE (t2) == ENUMERAL_TYPE
4952 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4953 warning_at (colon_loc, OPT_Wc___compat,
4954 ("different enum types in conditional is "
4955 "invalid in C++: %qT vs %qT"),
4956 t1, t2);
4959 /* Quickly detect the usual case where op1 and op2 have the same type
4960 after promotion. */
4961 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4963 if (type1 == type2)
4964 result_type = type1;
4965 else
4966 result_type = TYPE_MAIN_VARIANT (type1);
4968 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4969 || code1 == COMPLEX_TYPE)
4970 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4971 || code2 == COMPLEX_TYPE))
4973 result_type = c_common_type (type1, type2);
4974 if (result_type == error_mark_node)
4975 return error_mark_node;
4976 do_warn_double_promotion (result_type, type1, type2,
4977 "implicit conversion from %qT to %qT to "
4978 "match other result of conditional",
4979 colon_loc);
4981 /* If -Wsign-compare, warn here if type1 and type2 have
4982 different signedness. We'll promote the signed to unsigned
4983 and later code won't know it used to be different.
4984 Do this check on the original types, so that explicit casts
4985 will be considered, but default promotions won't. */
4986 if (c_inhibit_evaluation_warnings == 0)
4988 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4989 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4991 if (unsigned_op1 ^ unsigned_op2)
4993 bool ovf;
4995 /* Do not warn if the result type is signed, since the
4996 signed type will only be chosen if it can represent
4997 all the values of the unsigned type. */
4998 if (!TYPE_UNSIGNED (result_type))
4999 /* OK */;
5000 else
5002 bool op1_maybe_const = true;
5003 bool op2_maybe_const = true;
5005 /* Do not warn if the signed quantity is an
5006 unsuffixed integer literal (or some static
5007 constant expression involving such literals) and
5008 it is non-negative. This warning requires the
5009 operands to be folded for best results, so do
5010 that folding in this case even without
5011 warn_sign_compare to avoid warning options
5012 possibly affecting code generation. */
5013 c_inhibit_evaluation_warnings
5014 += (ifexp == truthvalue_false_node);
5015 op1 = c_fully_fold (op1, require_constant_value,
5016 &op1_maybe_const);
5017 c_inhibit_evaluation_warnings
5018 -= (ifexp == truthvalue_false_node);
5020 c_inhibit_evaluation_warnings
5021 += (ifexp == truthvalue_true_node);
5022 op2 = c_fully_fold (op2, require_constant_value,
5023 &op2_maybe_const);
5024 c_inhibit_evaluation_warnings
5025 -= (ifexp == truthvalue_true_node);
5027 if (warn_sign_compare)
5029 if ((unsigned_op2
5030 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5031 || (unsigned_op1
5032 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5033 /* OK */;
5034 else if (unsigned_op2)
5035 warning_at (op1_loc, OPT_Wsign_compare,
5036 "operand of ?: changes signedness from "
5037 "%qT to %qT due to unsignedness of other "
5038 "operand", TREE_TYPE (orig_op1),
5039 TREE_TYPE (orig_op2));
5040 else
5041 warning_at (op2_loc, OPT_Wsign_compare,
5042 "operand of ?: changes signedness from "
5043 "%qT to %qT due to unsignedness of other "
5044 "operand", TREE_TYPE (orig_op2),
5045 TREE_TYPE (orig_op1));
5047 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5048 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5049 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5050 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5055 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5057 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5058 pedwarn (colon_loc, OPT_Wpedantic,
5059 "ISO C forbids conditional expr with only one void side");
5060 result_type = void_type_node;
5062 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5064 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5065 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5066 addr_space_t as_common;
5068 if (comp_target_types (colon_loc, type1, type2))
5069 result_type = common_pointer_type (type1, type2);
5070 else if (null_pointer_constant_p (orig_op1))
5071 result_type = type2;
5072 else if (null_pointer_constant_p (orig_op2))
5073 result_type = type1;
5074 else if (!addr_space_superset (as1, as2, &as_common))
5076 error_at (colon_loc, "pointers to disjoint address spaces "
5077 "used in conditional expression");
5078 return error_mark_node;
5080 else if (VOID_TYPE_P (TREE_TYPE (type1))
5081 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5083 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5084 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5085 & ~TYPE_QUALS (TREE_TYPE (type1))))
5086 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5087 "pointer to array loses qualifier "
5088 "in conditional expression");
5090 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5091 pedwarn (colon_loc, OPT_Wpedantic,
5092 "ISO C forbids conditional expr between "
5093 "%<void *%> and function pointer");
5094 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5095 TREE_TYPE (type2)));
5097 else if (VOID_TYPE_P (TREE_TYPE (type2))
5098 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5100 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5101 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5102 & ~TYPE_QUALS (TREE_TYPE (type2))))
5103 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5104 "pointer to array loses qualifier "
5105 "in conditional expression");
5107 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5108 pedwarn (colon_loc, OPT_Wpedantic,
5109 "ISO C forbids conditional expr between "
5110 "%<void *%> and function pointer");
5111 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5112 TREE_TYPE (type1)));
5114 /* Objective-C pointer comparisons are a bit more lenient. */
5115 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5116 result_type = objc_common_type (type1, type2);
5117 else
5119 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5121 pedwarn (colon_loc, 0,
5122 "pointer type mismatch in conditional expression");
5123 result_type = build_pointer_type
5124 (build_qualified_type (void_type_node, qual));
5127 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5129 if (!null_pointer_constant_p (orig_op2))
5130 pedwarn (colon_loc, 0,
5131 "pointer/integer type mismatch in conditional expression");
5132 else
5134 op2 = null_pointer_node;
5136 result_type = type1;
5138 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5140 if (!null_pointer_constant_p (orig_op1))
5141 pedwarn (colon_loc, 0,
5142 "pointer/integer type mismatch in conditional expression");
5143 else
5145 op1 = null_pointer_node;
5147 result_type = type2;
5150 if (!result_type)
5152 if (flag_cond_mismatch)
5153 result_type = void_type_node;
5154 else
5156 error_at (colon_loc, "type mismatch in conditional expression");
5157 return error_mark_node;
5161 /* Merge const and volatile flags of the incoming types. */
5162 result_type
5163 = build_type_variant (result_type,
5164 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5165 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5167 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5168 semantic_result_type);
5169 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5170 semantic_result_type);
5172 if (ifexp_bcp && ifexp == truthvalue_true_node)
5174 op2_int_operands = true;
5175 op1 = c_fully_fold (op1, require_constant_value, NULL);
5177 if (ifexp_bcp && ifexp == truthvalue_false_node)
5179 op1_int_operands = true;
5180 op2 = c_fully_fold (op2, require_constant_value, NULL);
5182 int_const = int_operands = (ifexp_int_operands
5183 && op1_int_operands
5184 && op2_int_operands);
5185 if (int_operands)
5187 int_const = ((ifexp == truthvalue_true_node
5188 && TREE_CODE (orig_op1) == INTEGER_CST
5189 && !TREE_OVERFLOW (orig_op1))
5190 || (ifexp == truthvalue_false_node
5191 && TREE_CODE (orig_op2) == INTEGER_CST
5192 && !TREE_OVERFLOW (orig_op2)));
5195 /* Need to convert condition operand into a vector mask. */
5196 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5198 tree vectype = TREE_TYPE (ifexp);
5199 tree elem_type = TREE_TYPE (vectype);
5200 tree zero = build_int_cst (elem_type, 0);
5201 tree zero_vec = build_vector_from_val (vectype, zero);
5202 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5203 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5206 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5207 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5208 else
5210 if (int_operands)
5212 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5213 nested inside of the expression. */
5214 op1 = c_fully_fold (op1, false, NULL);
5215 op2 = c_fully_fold (op2, false, NULL);
5217 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5218 if (int_operands)
5219 ret = note_integer_operands (ret);
5221 if (semantic_result_type)
5222 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5224 protected_set_expr_location (ret, colon_loc);
5226 /* If the OP1 and OP2 are the same and don't have side-effects,
5227 warn here, because the COND_EXPR will be turned into OP1. */
5228 if (warn_duplicated_branches
5229 && TREE_CODE (ret) == COND_EXPR
5230 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5231 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5232 "this condition has identical branches");
5234 return ret;
5237 /* Return a compound expression that performs two expressions and
5238 returns the value of the second of them.
5240 LOC is the location of the COMPOUND_EXPR. */
5242 tree
5243 build_compound_expr (location_t loc, tree expr1, tree expr2)
5245 bool expr1_int_operands, expr2_int_operands;
5246 tree eptype = NULL_TREE;
5247 tree ret;
5249 if (flag_cilkplus
5250 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5251 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5253 error_at (loc,
5254 "spawned function call cannot be part of a comma expression");
5255 return error_mark_node;
5257 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5258 if (expr1_int_operands)
5259 expr1 = remove_c_maybe_const_expr (expr1);
5260 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5261 if (expr2_int_operands)
5262 expr2 = remove_c_maybe_const_expr (expr2);
5264 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5265 expr1 = TREE_OPERAND (expr1, 0);
5266 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5268 eptype = TREE_TYPE (expr2);
5269 expr2 = TREE_OPERAND (expr2, 0);
5272 if (!TREE_SIDE_EFFECTS (expr1))
5274 /* The left-hand operand of a comma expression is like an expression
5275 statement: with -Wunused, we should warn if it doesn't have
5276 any side-effects, unless it was explicitly cast to (void). */
5277 if (warn_unused_value)
5279 if (VOID_TYPE_P (TREE_TYPE (expr1))
5280 && CONVERT_EXPR_P (expr1))
5281 ; /* (void) a, b */
5282 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5283 && TREE_CODE (expr1) == COMPOUND_EXPR
5284 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5285 ; /* (void) a, (void) b, c */
5286 else
5287 warning_at (loc, OPT_Wunused_value,
5288 "left-hand operand of comma expression has no effect");
5291 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5292 && warn_unused_value)
5294 tree r = expr1;
5295 location_t cloc = loc;
5296 while (TREE_CODE (r) == COMPOUND_EXPR)
5298 if (EXPR_HAS_LOCATION (r))
5299 cloc = EXPR_LOCATION (r);
5300 r = TREE_OPERAND (r, 1);
5302 if (!TREE_SIDE_EFFECTS (r)
5303 && !VOID_TYPE_P (TREE_TYPE (r))
5304 && !CONVERT_EXPR_P (r))
5305 warning_at (cloc, OPT_Wunused_value,
5306 "right-hand operand of comma expression has no effect");
5309 /* With -Wunused, we should also warn if the left-hand operand does have
5310 side-effects, but computes a value which is not used. For example, in
5311 `foo() + bar(), baz()' the result of the `+' operator is not used,
5312 so we should issue a warning. */
5313 else if (warn_unused_value)
5314 warn_if_unused_value (expr1, loc);
5316 if (expr2 == error_mark_node)
5317 return error_mark_node;
5319 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5321 if (flag_isoc99
5322 && expr1_int_operands
5323 && expr2_int_operands)
5324 ret = note_integer_operands (ret);
5326 if (eptype)
5327 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5329 protected_set_expr_location (ret, loc);
5330 return ret;
5333 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5334 which we are casting. OTYPE is the type of the expression being
5335 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5336 of the cast. -Wcast-qual appeared on the command line. Named
5337 address space qualifiers are not handled here, because they result
5338 in different warnings. */
5340 static void
5341 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5343 tree in_type = type;
5344 tree in_otype = otype;
5345 int added = 0;
5346 int discarded = 0;
5347 bool is_const;
5349 /* Check that the qualifiers on IN_TYPE are a superset of the
5350 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5351 nodes is uninteresting and we stop as soon as we hit a
5352 non-POINTER_TYPE node on either type. */
5355 in_otype = TREE_TYPE (in_otype);
5356 in_type = TREE_TYPE (in_type);
5358 /* GNU C allows cv-qualified function types. 'const' means the
5359 function is very pure, 'volatile' means it can't return. We
5360 need to warn when such qualifiers are added, not when they're
5361 taken away. */
5362 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5363 && TREE_CODE (in_type) == FUNCTION_TYPE)
5364 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5365 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5366 else
5367 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5368 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5370 while (TREE_CODE (in_type) == POINTER_TYPE
5371 && TREE_CODE (in_otype) == POINTER_TYPE);
5373 if (added)
5374 warning_at (loc, OPT_Wcast_qual,
5375 "cast adds %q#v qualifier to function type", added);
5377 if (discarded)
5378 /* There are qualifiers present in IN_OTYPE that are not present
5379 in IN_TYPE. */
5380 warning_at (loc, OPT_Wcast_qual,
5381 "cast discards %qv qualifier from pointer target type",
5382 discarded);
5384 if (added || discarded)
5385 return;
5387 /* A cast from **T to const **T is unsafe, because it can cause a
5388 const value to be changed with no additional warning. We only
5389 issue this warning if T is the same on both sides, and we only
5390 issue the warning if there are the same number of pointers on
5391 both sides, as otherwise the cast is clearly unsafe anyhow. A
5392 cast is unsafe when a qualifier is added at one level and const
5393 is not present at all outer levels.
5395 To issue this warning, we check at each level whether the cast
5396 adds new qualifiers not already seen. We don't need to special
5397 case function types, as they won't have the same
5398 TYPE_MAIN_VARIANT. */
5400 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5401 return;
5402 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5403 return;
5405 in_type = type;
5406 in_otype = otype;
5407 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5410 in_type = TREE_TYPE (in_type);
5411 in_otype = TREE_TYPE (in_otype);
5412 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5413 && !is_const)
5415 warning_at (loc, OPT_Wcast_qual,
5416 "to be safe all intermediate pointers in cast from "
5417 "%qT to %qT must be %<const%> qualified",
5418 otype, type);
5419 break;
5421 if (is_const)
5422 is_const = TYPE_READONLY (in_type);
5424 while (TREE_CODE (in_type) == POINTER_TYPE);
5427 /* Build an expression representing a cast to type TYPE of expression EXPR.
5428 LOC is the location of the cast-- typically the open paren of the cast. */
5430 tree
5431 build_c_cast (location_t loc, tree type, tree expr)
5433 tree value;
5435 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5436 expr = TREE_OPERAND (expr, 0);
5438 value = expr;
5440 if (type == error_mark_node || expr == error_mark_node)
5441 return error_mark_node;
5443 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5444 only in <protocol> qualifications. But when constructing cast expressions,
5445 the protocols do matter and must be kept around. */
5446 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5447 return build1 (NOP_EXPR, type, expr);
5449 type = TYPE_MAIN_VARIANT (type);
5451 if (TREE_CODE (type) == ARRAY_TYPE)
5453 error_at (loc, "cast specifies array type");
5454 return error_mark_node;
5457 if (TREE_CODE (type) == FUNCTION_TYPE)
5459 error_at (loc, "cast specifies function type");
5460 return error_mark_node;
5463 if (!VOID_TYPE_P (type))
5465 value = require_complete_type (loc, value);
5466 if (value == error_mark_node)
5467 return error_mark_node;
5470 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5472 if (RECORD_OR_UNION_TYPE_P (type))
5473 pedwarn (loc, OPT_Wpedantic,
5474 "ISO C forbids casting nonscalar to the same type");
5476 /* Convert to remove any qualifiers from VALUE's type. */
5477 value = convert (type, value);
5479 else if (TREE_CODE (type) == UNION_TYPE)
5481 tree field;
5483 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5484 if (TREE_TYPE (field) != error_mark_node
5485 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5486 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5487 break;
5489 if (field)
5491 tree t;
5492 bool maybe_const = true;
5494 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5495 t = c_fully_fold (value, false, &maybe_const);
5496 t = build_constructor_single (type, field, t);
5497 if (!maybe_const)
5498 t = c_wrap_maybe_const (t, true);
5499 t = digest_init (loc, type, t,
5500 NULL_TREE, false, true, 0);
5501 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5502 return t;
5504 error_at (loc, "cast to union type from type not present in union");
5505 return error_mark_node;
5507 else
5509 tree otype, ovalue;
5511 if (type == void_type_node)
5513 tree t = build1 (CONVERT_EXPR, type, value);
5514 SET_EXPR_LOCATION (t, loc);
5515 return t;
5518 otype = TREE_TYPE (value);
5520 /* Optionally warn about potentially worrisome casts. */
5521 if (warn_cast_qual
5522 && TREE_CODE (type) == POINTER_TYPE
5523 && TREE_CODE (otype) == POINTER_TYPE)
5524 handle_warn_cast_qual (loc, type, otype);
5526 /* Warn about conversions between pointers to disjoint
5527 address spaces. */
5528 if (TREE_CODE (type) == POINTER_TYPE
5529 && TREE_CODE (otype) == POINTER_TYPE
5530 && !null_pointer_constant_p (value))
5532 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5533 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5534 addr_space_t as_common;
5536 if (!addr_space_superset (as_to, as_from, &as_common))
5538 if (ADDR_SPACE_GENERIC_P (as_from))
5539 warning_at (loc, 0, "cast to %s address space pointer "
5540 "from disjoint generic address space pointer",
5541 c_addr_space_name (as_to));
5543 else if (ADDR_SPACE_GENERIC_P (as_to))
5544 warning_at (loc, 0, "cast to generic address space pointer "
5545 "from disjoint %s address space pointer",
5546 c_addr_space_name (as_from));
5548 else
5549 warning_at (loc, 0, "cast to %s address space pointer "
5550 "from disjoint %s address space pointer",
5551 c_addr_space_name (as_to),
5552 c_addr_space_name (as_from));
5556 /* Warn about possible alignment problems. */
5557 if (STRICT_ALIGNMENT
5558 && TREE_CODE (type) == POINTER_TYPE
5559 && TREE_CODE (otype) == POINTER_TYPE
5560 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5561 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5562 /* Don't warn about opaque types, where the actual alignment
5563 restriction is unknown. */
5564 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5565 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5566 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5567 warning_at (loc, OPT_Wcast_align,
5568 "cast increases required alignment of target type");
5570 if (TREE_CODE (type) == INTEGER_TYPE
5571 && TREE_CODE (otype) == POINTER_TYPE
5572 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5573 /* Unlike conversion of integers to pointers, where the
5574 warning is disabled for converting constants because
5575 of cases such as SIG_*, warn about converting constant
5576 pointers to integers. In some cases it may cause unwanted
5577 sign extension, and a warning is appropriate. */
5578 warning_at (loc, OPT_Wpointer_to_int_cast,
5579 "cast from pointer to integer of different size");
5581 if (TREE_CODE (value) == CALL_EXPR
5582 && TREE_CODE (type) != TREE_CODE (otype))
5583 warning_at (loc, OPT_Wbad_function_cast,
5584 "cast from function call of type %qT "
5585 "to non-matching type %qT", otype, type);
5587 if (TREE_CODE (type) == POINTER_TYPE
5588 && TREE_CODE (otype) == INTEGER_TYPE
5589 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5590 /* Don't warn about converting any constant. */
5591 && !TREE_CONSTANT (value))
5592 warning_at (loc,
5593 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5594 "of different size");
5596 if (warn_strict_aliasing <= 2)
5597 strict_aliasing_warning (otype, type, expr);
5599 /* If pedantic, warn for conversions between function and object
5600 pointer types, except for converting a null pointer constant
5601 to function pointer type. */
5602 if (pedantic
5603 && TREE_CODE (type) == POINTER_TYPE
5604 && TREE_CODE (otype) == POINTER_TYPE
5605 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5606 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5607 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5608 "conversion of function pointer to object pointer type");
5610 if (pedantic
5611 && TREE_CODE (type) == POINTER_TYPE
5612 && TREE_CODE (otype) == POINTER_TYPE
5613 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5614 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5615 && !null_pointer_constant_p (value))
5616 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5617 "conversion of object pointer to function pointer type");
5619 ovalue = value;
5620 value = convert (type, value);
5622 /* Ignore any integer overflow caused by the cast. */
5623 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5625 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5627 if (!TREE_OVERFLOW (value))
5629 /* Avoid clobbering a shared constant. */
5630 value = copy_node (value);
5631 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5634 else if (TREE_OVERFLOW (value))
5635 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5636 value = wide_int_to_tree (TREE_TYPE (value), value);
5640 /* Don't let a cast be an lvalue. */
5641 if (lvalue_p (value))
5642 value = non_lvalue_loc (loc, value);
5644 /* Don't allow the results of casting to floating-point or complex
5645 types be confused with actual constants, or casts involving
5646 integer and pointer types other than direct integer-to-integer
5647 and integer-to-pointer be confused with integer constant
5648 expressions and null pointer constants. */
5649 if (TREE_CODE (value) == REAL_CST
5650 || TREE_CODE (value) == COMPLEX_CST
5651 || (TREE_CODE (value) == INTEGER_CST
5652 && !((TREE_CODE (expr) == INTEGER_CST
5653 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5654 || TREE_CODE (expr) == REAL_CST
5655 || TREE_CODE (expr) == COMPLEX_CST)))
5656 value = build1 (NOP_EXPR, type, value);
5658 protected_set_expr_location (value, loc);
5659 return value;
5662 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5663 location of the open paren of the cast, or the position of the cast
5664 expr. */
5665 tree
5666 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5668 tree type;
5669 tree type_expr = NULL_TREE;
5670 bool type_expr_const = true;
5671 tree ret;
5672 int saved_wsp = warn_strict_prototypes;
5674 /* This avoids warnings about unprototyped casts on
5675 integers. E.g. "#define SIG_DFL (void(*)())0". */
5676 if (TREE_CODE (expr) == INTEGER_CST)
5677 warn_strict_prototypes = 0;
5678 type = groktypename (type_name, &type_expr, &type_expr_const);
5679 warn_strict_prototypes = saved_wsp;
5681 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5682 && reject_gcc_builtin (expr))
5683 return error_mark_node;
5685 ret = build_c_cast (loc, type, expr);
5686 if (type_expr)
5688 bool inner_expr_const = true;
5689 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5690 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5691 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5692 && inner_expr_const);
5693 SET_EXPR_LOCATION (ret, loc);
5696 if (!EXPR_HAS_LOCATION (ret))
5697 protected_set_expr_location (ret, loc);
5699 /* C++ does not permits types to be defined in a cast, but it
5700 allows references to incomplete types. */
5701 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5702 warning_at (loc, OPT_Wc___compat,
5703 "defining a type in a cast is invalid in C++");
5705 return ret;
5708 /* Build an assignment expression of lvalue LHS from value RHS.
5709 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5710 may differ from TREE_TYPE (LHS) for an enum bitfield.
5711 MODIFYCODE is the code for a binary operator that we use
5712 to combine the old value of LHS with RHS to get the new value.
5713 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5714 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5715 which may differ from TREE_TYPE (RHS) for an enum value.
5717 LOCATION is the location of the MODIFYCODE operator.
5718 RHS_LOC is the location of the RHS. */
5720 tree
5721 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5722 enum tree_code modifycode,
5723 location_t rhs_loc, tree rhs, tree rhs_origtype)
5725 tree result;
5726 tree newrhs;
5727 tree rhseval = NULL_TREE;
5728 tree rhs_semantic_type = NULL_TREE;
5729 tree lhstype = TREE_TYPE (lhs);
5730 tree olhstype = lhstype;
5731 bool npc;
5732 bool is_atomic_op;
5734 /* Types that aren't fully specified cannot be used in assignments. */
5735 lhs = require_complete_type (location, lhs);
5737 /* Avoid duplicate error messages from operands that had errors. */
5738 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5739 return error_mark_node;
5741 /* Ensure an error for assigning a non-lvalue array to an array in
5742 C90. */
5743 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5745 error_at (location, "assignment to expression with array type");
5746 return error_mark_node;
5749 /* For ObjC properties, defer this check. */
5750 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5751 return error_mark_node;
5753 is_atomic_op = really_atomic_lvalue (lhs);
5755 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5757 rhs_semantic_type = TREE_TYPE (rhs);
5758 rhs = TREE_OPERAND (rhs, 0);
5761 newrhs = rhs;
5763 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5765 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5766 lhs_origtype, modifycode, rhs_loc, rhs,
5767 rhs_origtype);
5768 if (inner == error_mark_node)
5769 return error_mark_node;
5770 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5771 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5772 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5773 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5774 protected_set_expr_location (result, location);
5775 return result;
5778 /* If a binary op has been requested, combine the old LHS value with the RHS
5779 producing the value we should actually store into the LHS. */
5781 if (modifycode != NOP_EXPR)
5783 lhs = c_fully_fold (lhs, false, NULL);
5784 lhs = stabilize_reference (lhs);
5786 /* Construct the RHS for any non-atomic compound assignemnt. */
5787 if (!is_atomic_op)
5789 /* If in LHS op= RHS the RHS has side-effects, ensure they
5790 are preevaluated before the rest of the assignment expression's
5791 side-effects, because RHS could contain e.g. function calls
5792 that modify LHS. */
5793 if (TREE_SIDE_EFFECTS (rhs))
5795 newrhs = save_expr (rhs);
5796 rhseval = newrhs;
5798 newrhs = build_binary_op (location,
5799 modifycode, lhs, newrhs, 1);
5801 /* The original type of the right hand side is no longer
5802 meaningful. */
5803 rhs_origtype = NULL_TREE;
5807 if (c_dialect_objc ())
5809 /* Check if we are modifying an Objective-C property reference;
5810 if so, we need to generate setter calls. */
5811 result = objc_maybe_build_modify_expr (lhs, newrhs);
5812 if (result)
5813 goto return_result;
5815 /* Else, do the check that we postponed for Objective-C. */
5816 if (!lvalue_or_else (location, lhs, lv_assign))
5817 return error_mark_node;
5820 /* Give an error for storing in something that is 'const'. */
5822 if (TYPE_READONLY (lhstype)
5823 || (RECORD_OR_UNION_TYPE_P (lhstype)
5824 && C_TYPE_FIELDS_READONLY (lhstype)))
5826 readonly_error (location, lhs, lv_assign);
5827 return error_mark_node;
5829 else if (TREE_READONLY (lhs))
5830 readonly_warning (lhs, lv_assign);
5832 /* If storing into a structure or union member,
5833 it has probably been given type `int'.
5834 Compute the type that would go with
5835 the actual amount of storage the member occupies. */
5837 if (TREE_CODE (lhs) == COMPONENT_REF
5838 && (TREE_CODE (lhstype) == INTEGER_TYPE
5839 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5840 || TREE_CODE (lhstype) == REAL_TYPE
5841 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5842 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5844 /* If storing in a field that is in actuality a short or narrower than one,
5845 we must store in the field in its actual type. */
5847 if (lhstype != TREE_TYPE (lhs))
5849 lhs = copy_node (lhs);
5850 TREE_TYPE (lhs) = lhstype;
5853 /* Issue -Wc++-compat warnings about an assignment to an enum type
5854 when LHS does not have its original type. This happens for,
5855 e.g., an enum bitfield in a struct. */
5856 if (warn_cxx_compat
5857 && lhs_origtype != NULL_TREE
5858 && lhs_origtype != lhstype
5859 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5861 tree checktype = (rhs_origtype != NULL_TREE
5862 ? rhs_origtype
5863 : TREE_TYPE (rhs));
5864 if (checktype != error_mark_node
5865 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5866 || (is_atomic_op && modifycode != NOP_EXPR)))
5867 warning_at (location, OPT_Wc___compat,
5868 "enum conversion in assignment is invalid in C++");
5871 /* If the lhs is atomic, remove that qualifier. */
5872 if (is_atomic_op)
5874 lhstype = build_qualified_type (lhstype,
5875 (TYPE_QUALS (lhstype)
5876 & ~TYPE_QUAL_ATOMIC));
5877 olhstype = build_qualified_type (olhstype,
5878 (TYPE_QUALS (lhstype)
5879 & ~TYPE_QUAL_ATOMIC));
5882 /* Convert new value to destination type. Fold it first, then
5883 restore any excess precision information, for the sake of
5884 conversion warnings. */
5886 if (!(is_atomic_op && modifycode != NOP_EXPR))
5888 npc = null_pointer_constant_p (newrhs);
5889 newrhs = c_fully_fold (newrhs, false, NULL);
5890 if (rhs_semantic_type)
5891 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5892 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5893 rhs_origtype, ic_assign, npc,
5894 NULL_TREE, NULL_TREE, 0);
5895 if (TREE_CODE (newrhs) == ERROR_MARK)
5896 return error_mark_node;
5899 /* Emit ObjC write barrier, if necessary. */
5900 if (c_dialect_objc () && flag_objc_gc)
5902 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5903 if (result)
5905 protected_set_expr_location (result, location);
5906 goto return_result;
5910 /* Scan operands. */
5912 if (is_atomic_op)
5913 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5914 else
5916 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5917 TREE_SIDE_EFFECTS (result) = 1;
5918 protected_set_expr_location (result, location);
5921 /* If we got the LHS in a different type for storing in,
5922 convert the result back to the nominal type of LHS
5923 so that the value we return always has the same type
5924 as the LHS argument. */
5926 if (olhstype == TREE_TYPE (result))
5927 goto return_result;
5929 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5930 rhs_origtype, ic_assign, false, NULL_TREE,
5931 NULL_TREE, 0);
5932 protected_set_expr_location (result, location);
5934 return_result:
5935 if (rhseval)
5936 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5937 return result;
5940 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5941 This is used to implement -fplan9-extensions. */
5943 static bool
5944 find_anonymous_field_with_type (tree struct_type, tree type)
5946 tree field;
5947 bool found;
5949 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5950 found = false;
5951 for (field = TYPE_FIELDS (struct_type);
5952 field != NULL_TREE;
5953 field = TREE_CHAIN (field))
5955 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5956 ? c_build_qualified_type (TREE_TYPE (field),
5957 TYPE_QUAL_ATOMIC)
5958 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5959 if (DECL_NAME (field) == NULL
5960 && comptypes (type, fieldtype))
5962 if (found)
5963 return false;
5964 found = true;
5966 else if (DECL_NAME (field) == NULL
5967 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5968 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5970 if (found)
5971 return false;
5972 found = true;
5975 return found;
5978 /* RHS is an expression whose type is pointer to struct. If there is
5979 an anonymous field in RHS with type TYPE, then return a pointer to
5980 that field in RHS. This is used with -fplan9-extensions. This
5981 returns NULL if no conversion could be found. */
5983 static tree
5984 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5986 tree rhs_struct_type, lhs_main_type;
5987 tree field, found_field;
5988 bool found_sub_field;
5989 tree ret;
5991 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5992 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5993 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5995 gcc_assert (POINTER_TYPE_P (type));
5996 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5997 ? c_build_qualified_type (TREE_TYPE (type),
5998 TYPE_QUAL_ATOMIC)
5999 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6001 found_field = NULL_TREE;
6002 found_sub_field = false;
6003 for (field = TYPE_FIELDS (rhs_struct_type);
6004 field != NULL_TREE;
6005 field = TREE_CHAIN (field))
6007 if (DECL_NAME (field) != NULL_TREE
6008 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6009 continue;
6010 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6011 ? c_build_qualified_type (TREE_TYPE (field),
6012 TYPE_QUAL_ATOMIC)
6013 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6014 if (comptypes (lhs_main_type, fieldtype))
6016 if (found_field != NULL_TREE)
6017 return NULL_TREE;
6018 found_field = field;
6020 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6021 lhs_main_type))
6023 if (found_field != NULL_TREE)
6024 return NULL_TREE;
6025 found_field = field;
6026 found_sub_field = true;
6030 if (found_field == NULL_TREE)
6031 return NULL_TREE;
6033 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6034 build_fold_indirect_ref (rhs), found_field,
6035 NULL_TREE);
6036 ret = build_fold_addr_expr_loc (location, ret);
6038 if (found_sub_field)
6040 ret = convert_to_anonymous_field (location, type, ret);
6041 gcc_assert (ret != NULL_TREE);
6044 return ret;
6047 /* Issue an error message for a bad initializer component.
6048 GMSGID identifies the message.
6049 The component name is taken from the spelling stack. */
6051 static void
6052 error_init (location_t loc, const char *gmsgid)
6054 char *ofwhat;
6056 /* The gmsgid may be a format string with %< and %>. */
6057 error_at (loc, gmsgid);
6058 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6059 if (*ofwhat)
6060 inform (loc, "(near initialization for %qs)", ofwhat);
6063 /* Issue a pedantic warning for a bad initializer component. OPT is
6064 the option OPT_* (from options.h) controlling this warning or 0 if
6065 it is unconditionally given. GMSGID identifies the message. The
6066 component name is taken from the spelling stack. */
6068 static void
6069 pedwarn_init (location_t loc, int opt, const char *gmsgid)
6071 char *ofwhat;
6072 bool warned;
6074 /* Use the location where a macro was expanded rather than where
6075 it was defined to make sure macros defined in system headers
6076 but used incorrectly elsewhere are diagnosed. */
6077 source_location exploc = expansion_point_location_if_in_system_header (loc);
6079 /* The gmsgid may be a format string with %< and %>. */
6080 warned = pedwarn (exploc, opt, gmsgid);
6081 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6082 if (*ofwhat && warned)
6083 inform (exploc, "(near initialization for %qs)", ofwhat);
6086 /* Issue a warning for a bad initializer component.
6088 OPT is the OPT_W* value corresponding to the warning option that
6089 controls this warning. GMSGID identifies the message. The
6090 component name is taken from the spelling stack. */
6092 static void
6093 warning_init (location_t loc, int opt, const char *gmsgid)
6095 char *ofwhat;
6096 bool warned;
6098 /* Use the location where a macro was expanded rather than where
6099 it was defined to make sure macros defined in system headers
6100 but used incorrectly elsewhere are diagnosed. */
6101 source_location exploc = expansion_point_location_if_in_system_header (loc);
6103 /* The gmsgid may be a format string with %< and %>. */
6104 warned = warning_at (exploc, opt, gmsgid);
6105 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6106 if (*ofwhat && warned)
6107 inform (exploc, "(near initialization for %qs)", ofwhat);
6110 /* If TYPE is an array type and EXPR is a parenthesized string
6111 constant, warn if pedantic that EXPR is being used to initialize an
6112 object of type TYPE. */
6114 void
6115 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6117 if (pedantic
6118 && TREE_CODE (type) == ARRAY_TYPE
6119 && TREE_CODE (expr.value) == STRING_CST
6120 && expr.original_code != STRING_CST)
6121 pedwarn_init (loc, OPT_Wpedantic,
6122 "array initialized from parenthesized string constant");
6125 /* Convert value RHS to type TYPE as preparation for an assignment to
6126 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6127 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6128 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6129 constant before any folding.
6130 The real work of conversion is done by `convert'.
6131 The purpose of this function is to generate error messages
6132 for assignments that are not allowed in C.
6133 ERRTYPE says whether it is argument passing, assignment,
6134 initialization or return.
6136 In the following example, '~' denotes where EXPR_LOC and '^' where
6137 LOCATION point to:
6139 f (var); [ic_argpass]
6140 ^ ~~~
6141 x = var; [ic_assign]
6142 ^ ~~~;
6143 int x = var; [ic_init]
6145 return x; [ic_return]
6148 FUNCTION is a tree for the function being called.
6149 PARMNUM is the number of the argument, for printing in error messages. */
6151 static tree
6152 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6153 tree rhs, tree origtype, enum impl_conv errtype,
6154 bool null_pointer_constant, tree fundecl,
6155 tree function, int parmnum)
6157 enum tree_code codel = TREE_CODE (type);
6158 tree orig_rhs = rhs;
6159 tree rhstype;
6160 enum tree_code coder;
6161 tree rname = NULL_TREE;
6162 bool objc_ok = false;
6164 /* Use the expansion point location to handle cases such as user's
6165 function returning a wrong-type macro defined in a system header. */
6166 location = expansion_point_location_if_in_system_header (location);
6168 if (errtype == ic_argpass)
6170 tree selector;
6171 /* Change pointer to function to the function itself for
6172 diagnostics. */
6173 if (TREE_CODE (function) == ADDR_EXPR
6174 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6175 function = TREE_OPERAND (function, 0);
6177 /* Handle an ObjC selector specially for diagnostics. */
6178 selector = objc_message_selector ();
6179 rname = function;
6180 if (selector && parmnum > 2)
6182 rname = selector;
6183 parmnum -= 2;
6187 /* This macro is used to emit diagnostics to ensure that all format
6188 strings are complete sentences, visible to gettext and checked at
6189 compile time. */
6190 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6191 do { \
6192 switch (errtype) \
6194 case ic_argpass: \
6195 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6196 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6197 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6198 "expected %qT but argument is of type %qT", \
6199 type, rhstype); \
6200 break; \
6201 case ic_assign: \
6202 pedwarn (LOCATION, OPT, AS); \
6203 break; \
6204 case ic_init: \
6205 pedwarn_init (LOCATION, OPT, IN); \
6206 break; \
6207 case ic_return: \
6208 pedwarn (LOCATION, OPT, RE); \
6209 break; \
6210 default: \
6211 gcc_unreachable (); \
6213 } while (0)
6215 /* This macro is used to emit diagnostics to ensure that all format
6216 strings are complete sentences, visible to gettext and checked at
6217 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6218 extra parameter to enumerate qualifiers. */
6219 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6220 do { \
6221 switch (errtype) \
6223 case ic_argpass: \
6224 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6225 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6226 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6227 "expected %qT but argument is of type %qT", \
6228 type, rhstype); \
6229 break; \
6230 case ic_assign: \
6231 pedwarn (LOCATION, OPT, AS, QUALS); \
6232 break; \
6233 case ic_init: \
6234 pedwarn (LOCATION, OPT, IN, QUALS); \
6235 break; \
6236 case ic_return: \
6237 pedwarn (LOCATION, OPT, RE, QUALS); \
6238 break; \
6239 default: \
6240 gcc_unreachable (); \
6242 } while (0)
6244 /* This macro is used to emit diagnostics to ensure that all format
6245 strings are complete sentences, visible to gettext and checked at
6246 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6247 warning_at instead of pedwarn. */
6248 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6249 do { \
6250 switch (errtype) \
6252 case ic_argpass: \
6253 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6254 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6255 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6256 "expected %qT but argument is of type %qT", \
6257 type, rhstype); \
6258 break; \
6259 case ic_assign: \
6260 warning_at (LOCATION, OPT, AS, QUALS); \
6261 break; \
6262 case ic_init: \
6263 warning_at (LOCATION, OPT, IN, QUALS); \
6264 break; \
6265 case ic_return: \
6266 warning_at (LOCATION, OPT, RE, QUALS); \
6267 break; \
6268 default: \
6269 gcc_unreachable (); \
6271 } while (0)
6273 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6274 rhs = TREE_OPERAND (rhs, 0);
6276 rhstype = TREE_TYPE (rhs);
6277 coder = TREE_CODE (rhstype);
6279 if (coder == ERROR_MARK)
6280 return error_mark_node;
6282 if (c_dialect_objc ())
6284 int parmno;
6286 switch (errtype)
6288 case ic_return:
6289 parmno = 0;
6290 break;
6292 case ic_assign:
6293 parmno = -1;
6294 break;
6296 case ic_init:
6297 parmno = -2;
6298 break;
6300 default:
6301 parmno = parmnum;
6302 break;
6305 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6308 if (warn_cxx_compat)
6310 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6311 if (checktype != error_mark_node
6312 && TREE_CODE (type) == ENUMERAL_TYPE
6313 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6315 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6316 G_("enum conversion when passing argument "
6317 "%d of %qE is invalid in C++"),
6318 G_("enum conversion in assignment is "
6319 "invalid in C++"),
6320 G_("enum conversion in initialization is "
6321 "invalid in C++"),
6322 G_("enum conversion in return is "
6323 "invalid in C++"));
6327 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6328 return rhs;
6330 if (coder == VOID_TYPE)
6332 /* Except for passing an argument to an unprototyped function,
6333 this is a constraint violation. When passing an argument to
6334 an unprototyped function, it is compile-time undefined;
6335 making it a constraint in that case was rejected in
6336 DR#252. */
6337 error_at (location, "void value not ignored as it ought to be");
6338 return error_mark_node;
6340 rhs = require_complete_type (location, rhs);
6341 if (rhs == error_mark_node)
6342 return error_mark_node;
6344 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6345 return error_mark_node;
6347 /* A non-reference type can convert to a reference. This handles
6348 va_start, va_copy and possibly port built-ins. */
6349 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6351 if (!lvalue_p (rhs))
6353 error_at (location, "cannot pass rvalue to reference parameter");
6354 return error_mark_node;
6356 if (!c_mark_addressable (rhs))
6357 return error_mark_node;
6358 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6359 SET_EXPR_LOCATION (rhs, location);
6361 rhs = convert_for_assignment (location, expr_loc,
6362 build_pointer_type (TREE_TYPE (type)),
6363 rhs, origtype, errtype,
6364 null_pointer_constant, fundecl, function,
6365 parmnum);
6366 if (rhs == error_mark_node)
6367 return error_mark_node;
6369 rhs = build1 (NOP_EXPR, type, rhs);
6370 SET_EXPR_LOCATION (rhs, location);
6371 return rhs;
6373 /* Some types can interconvert without explicit casts. */
6374 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6375 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6376 return convert (type, rhs);
6377 /* Arithmetic types all interconvert, and enum is treated like int. */
6378 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6379 || codel == FIXED_POINT_TYPE
6380 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6381 || codel == BOOLEAN_TYPE)
6382 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6383 || coder == FIXED_POINT_TYPE
6384 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6385 || coder == BOOLEAN_TYPE))
6387 tree ret;
6388 bool save = in_late_binary_op;
6389 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6390 || (coder == REAL_TYPE
6391 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6392 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6393 in_late_binary_op = true;
6394 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6395 ? expr_loc : location, type, orig_rhs);
6396 in_late_binary_op = save;
6397 return ret;
6400 /* Aggregates in different TUs might need conversion. */
6401 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6402 && codel == coder
6403 && comptypes (type, rhstype))
6404 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6405 ? expr_loc : location, type, rhs);
6407 /* Conversion to a transparent union or record from its member types.
6408 This applies only to function arguments. */
6409 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6410 && TYPE_TRANSPARENT_AGGR (type))
6411 && errtype == ic_argpass)
6413 tree memb, marginal_memb = NULL_TREE;
6415 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6417 tree memb_type = TREE_TYPE (memb);
6419 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6420 TYPE_MAIN_VARIANT (rhstype)))
6421 break;
6423 if (TREE_CODE (memb_type) != POINTER_TYPE)
6424 continue;
6426 if (coder == POINTER_TYPE)
6428 tree ttl = TREE_TYPE (memb_type);
6429 tree ttr = TREE_TYPE (rhstype);
6431 /* Any non-function converts to a [const][volatile] void *
6432 and vice versa; otherwise, targets must be the same.
6433 Meanwhile, the lhs target must have all the qualifiers of
6434 the rhs. */
6435 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6436 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6437 || comp_target_types (location, memb_type, rhstype))
6439 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6440 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6441 /* If this type won't generate any warnings, use it. */
6442 if (lquals == rquals
6443 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6444 && TREE_CODE (ttl) == FUNCTION_TYPE)
6445 ? ((lquals | rquals) == rquals)
6446 : ((lquals | rquals) == lquals)))
6447 break;
6449 /* Keep looking for a better type, but remember this one. */
6450 if (!marginal_memb)
6451 marginal_memb = memb;
6455 /* Can convert integer zero to any pointer type. */
6456 if (null_pointer_constant)
6458 rhs = null_pointer_node;
6459 break;
6463 if (memb || marginal_memb)
6465 if (!memb)
6467 /* We have only a marginally acceptable member type;
6468 it needs a warning. */
6469 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6470 tree ttr = TREE_TYPE (rhstype);
6472 /* Const and volatile mean something different for function
6473 types, so the usual warnings are not appropriate. */
6474 if (TREE_CODE (ttr) == FUNCTION_TYPE
6475 && TREE_CODE (ttl) == FUNCTION_TYPE)
6477 /* Because const and volatile on functions are
6478 restrictions that say the function will not do
6479 certain things, it is okay to use a const or volatile
6480 function where an ordinary one is wanted, but not
6481 vice-versa. */
6482 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6483 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6484 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6485 OPT_Wdiscarded_qualifiers,
6486 G_("passing argument %d of %qE "
6487 "makes %q#v qualified function "
6488 "pointer from unqualified"),
6489 G_("assignment makes %q#v qualified "
6490 "function pointer from "
6491 "unqualified"),
6492 G_("initialization makes %q#v qualified "
6493 "function pointer from "
6494 "unqualified"),
6495 G_("return makes %q#v qualified function "
6496 "pointer from unqualified"),
6497 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6499 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6500 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6501 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6502 OPT_Wdiscarded_qualifiers,
6503 G_("passing argument %d of %qE discards "
6504 "%qv qualifier from pointer target type"),
6505 G_("assignment discards %qv qualifier "
6506 "from pointer target type"),
6507 G_("initialization discards %qv qualifier "
6508 "from pointer target type"),
6509 G_("return discards %qv qualifier from "
6510 "pointer target type"),
6511 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6513 memb = marginal_memb;
6516 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6517 pedwarn (location, OPT_Wpedantic,
6518 "ISO C prohibits argument conversion to union type");
6520 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6521 return build_constructor_single (type, memb, rhs);
6525 /* Conversions among pointers */
6526 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6527 && (coder == codel))
6529 tree ttl = TREE_TYPE (type);
6530 tree ttr = TREE_TYPE (rhstype);
6531 tree mvl = ttl;
6532 tree mvr = ttr;
6533 bool is_opaque_pointer;
6534 int target_cmp = 0; /* Cache comp_target_types () result. */
6535 addr_space_t asl;
6536 addr_space_t asr;
6538 if (TREE_CODE (mvl) != ARRAY_TYPE)
6539 mvl = (TYPE_ATOMIC (mvl)
6540 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6541 TYPE_QUAL_ATOMIC)
6542 : TYPE_MAIN_VARIANT (mvl));
6543 if (TREE_CODE (mvr) != ARRAY_TYPE)
6544 mvr = (TYPE_ATOMIC (mvr)
6545 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6546 TYPE_QUAL_ATOMIC)
6547 : TYPE_MAIN_VARIANT (mvr));
6548 /* Opaque pointers are treated like void pointers. */
6549 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6551 /* The Plan 9 compiler permits a pointer to a struct to be
6552 automatically converted into a pointer to an anonymous field
6553 within the struct. */
6554 if (flag_plan9_extensions
6555 && RECORD_OR_UNION_TYPE_P (mvl)
6556 && RECORD_OR_UNION_TYPE_P (mvr)
6557 && mvl != mvr)
6559 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6560 if (new_rhs != NULL_TREE)
6562 rhs = new_rhs;
6563 rhstype = TREE_TYPE (rhs);
6564 coder = TREE_CODE (rhstype);
6565 ttr = TREE_TYPE (rhstype);
6566 mvr = TYPE_MAIN_VARIANT (ttr);
6570 /* C++ does not allow the implicit conversion void* -> T*. However,
6571 for the purpose of reducing the number of false positives, we
6572 tolerate the special case of
6574 int *p = NULL;
6576 where NULL is typically defined in C to be '(void *) 0'. */
6577 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6578 warning_at (errtype == ic_argpass ? expr_loc : location,
6579 OPT_Wc___compat,
6580 "request for implicit conversion "
6581 "from %qT to %qT not permitted in C++", rhstype, type);
6583 /* See if the pointers point to incompatible address spaces. */
6584 asl = TYPE_ADDR_SPACE (ttl);
6585 asr = TYPE_ADDR_SPACE (ttr);
6586 if (!null_pointer_constant_p (rhs)
6587 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6589 switch (errtype)
6591 case ic_argpass:
6592 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6593 "non-enclosed address space", parmnum, rname);
6594 break;
6595 case ic_assign:
6596 error_at (location, "assignment from pointer to "
6597 "non-enclosed address space");
6598 break;
6599 case ic_init:
6600 error_at (location, "initialization from pointer to "
6601 "non-enclosed address space");
6602 break;
6603 case ic_return:
6604 error_at (location, "return from pointer to "
6605 "non-enclosed address space");
6606 break;
6607 default:
6608 gcc_unreachable ();
6610 return error_mark_node;
6613 /* Check if the right-hand side has a format attribute but the
6614 left-hand side doesn't. */
6615 if (warn_suggest_attribute_format
6616 && check_missing_format_attribute (type, rhstype))
6618 switch (errtype)
6620 case ic_argpass:
6621 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6622 "argument %d of %qE might be "
6623 "a candidate for a format attribute",
6624 parmnum, rname);
6625 break;
6626 case ic_assign:
6627 warning_at (location, OPT_Wsuggest_attribute_format,
6628 "assignment left-hand side might be "
6629 "a candidate for a format attribute");
6630 break;
6631 case ic_init:
6632 warning_at (location, OPT_Wsuggest_attribute_format,
6633 "initialization left-hand side might be "
6634 "a candidate for a format attribute");
6635 break;
6636 case ic_return:
6637 warning_at (location, OPT_Wsuggest_attribute_format,
6638 "return type might be "
6639 "a candidate for a format attribute");
6640 break;
6641 default:
6642 gcc_unreachable ();
6646 /* Any non-function converts to a [const][volatile] void *
6647 and vice versa; otherwise, targets must be the same.
6648 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6649 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6650 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6651 || (target_cmp = comp_target_types (location, type, rhstype))
6652 || is_opaque_pointer
6653 || ((c_common_unsigned_type (mvl)
6654 == c_common_unsigned_type (mvr))
6655 && (c_common_signed_type (mvl)
6656 == c_common_signed_type (mvr))
6657 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6659 /* Warn about loss of qualifers from pointers to arrays with
6660 qualifiers on the element type. */
6661 if (TREE_CODE (ttr) == ARRAY_TYPE)
6663 ttr = strip_array_types (ttr);
6664 ttl = strip_array_types (ttl);
6666 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6667 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6668 WARNING_FOR_QUALIFIERS (location, expr_loc,
6669 OPT_Wdiscarded_array_qualifiers,
6670 G_("passing argument %d of %qE discards "
6671 "%qv qualifier from pointer target type"),
6672 G_("assignment discards %qv qualifier "
6673 "from pointer target type"),
6674 G_("initialization discards %qv qualifier "
6675 "from pointer target type"),
6676 G_("return discards %qv qualifier from "
6677 "pointer target type"),
6678 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6680 else if (pedantic
6681 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6683 (VOID_TYPE_P (ttr)
6684 && !null_pointer_constant
6685 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6686 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6687 G_("ISO C forbids passing argument %d of "
6688 "%qE between function pointer "
6689 "and %<void *%>"),
6690 G_("ISO C forbids assignment between "
6691 "function pointer and %<void *%>"),
6692 G_("ISO C forbids initialization between "
6693 "function pointer and %<void *%>"),
6694 G_("ISO C forbids return between function "
6695 "pointer and %<void *%>"));
6696 /* Const and volatile mean something different for function types,
6697 so the usual warnings are not appropriate. */
6698 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6699 && TREE_CODE (ttl) != FUNCTION_TYPE)
6701 /* Don't warn about loss of qualifier for conversions from
6702 qualified void* to pointers to arrays with corresponding
6703 qualifier on the element type. */
6704 if (!pedantic)
6705 ttl = strip_array_types (ttl);
6707 /* Assignments between atomic and non-atomic objects are OK. */
6708 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6709 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6711 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6712 OPT_Wdiscarded_qualifiers,
6713 G_("passing argument %d of %qE discards "
6714 "%qv qualifier from pointer target type"),
6715 G_("assignment discards %qv qualifier "
6716 "from pointer target type"),
6717 G_("initialization discards %qv qualifier "
6718 "from pointer target type"),
6719 G_("return discards %qv qualifier from "
6720 "pointer target type"),
6721 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6723 /* If this is not a case of ignoring a mismatch in signedness,
6724 no warning. */
6725 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6726 || target_cmp)
6728 /* If there is a mismatch, do warn. */
6729 else if (warn_pointer_sign)
6730 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6731 G_("pointer targets in passing argument "
6732 "%d of %qE differ in signedness"),
6733 G_("pointer targets in assignment "
6734 "differ in signedness"),
6735 G_("pointer targets in initialization "
6736 "differ in signedness"),
6737 G_("pointer targets in return differ "
6738 "in signedness"));
6740 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6741 && TREE_CODE (ttr) == FUNCTION_TYPE)
6743 /* Because const and volatile on functions are restrictions
6744 that say the function will not do certain things,
6745 it is okay to use a const or volatile function
6746 where an ordinary one is wanted, but not vice-versa. */
6747 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6748 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6749 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6750 OPT_Wdiscarded_qualifiers,
6751 G_("passing argument %d of %qE makes "
6752 "%q#v qualified function pointer "
6753 "from unqualified"),
6754 G_("assignment makes %q#v qualified function "
6755 "pointer from unqualified"),
6756 G_("initialization makes %q#v qualified "
6757 "function pointer from unqualified"),
6758 G_("return makes %q#v qualified function "
6759 "pointer from unqualified"),
6760 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6763 else
6764 /* Avoid warning about the volatile ObjC EH puts on decls. */
6765 if (!objc_ok)
6766 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6767 OPT_Wincompatible_pointer_types,
6768 G_("passing argument %d of %qE from "
6769 "incompatible pointer type"),
6770 G_("assignment from incompatible pointer type"),
6771 G_("initialization from incompatible "
6772 "pointer type"),
6773 G_("return from incompatible pointer type"));
6775 return convert (type, rhs);
6777 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6779 /* ??? This should not be an error when inlining calls to
6780 unprototyped functions. */
6781 error_at (location, "invalid use of non-lvalue array");
6782 return error_mark_node;
6784 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6786 /* An explicit constant 0 can convert to a pointer,
6787 or one that results from arithmetic, even including
6788 a cast to integer type. */
6789 if (!null_pointer_constant)
6790 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6791 OPT_Wint_conversion,
6792 G_("passing argument %d of %qE makes "
6793 "pointer from integer without a cast"),
6794 G_("assignment makes pointer from integer "
6795 "without a cast"),
6796 G_("initialization makes pointer from "
6797 "integer without a cast"),
6798 G_("return makes pointer from integer "
6799 "without a cast"));
6801 return convert (type, rhs);
6803 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6805 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6806 OPT_Wint_conversion,
6807 G_("passing argument %d of %qE makes integer "
6808 "from pointer without a cast"),
6809 G_("assignment makes integer from pointer "
6810 "without a cast"),
6811 G_("initialization makes integer from pointer "
6812 "without a cast"),
6813 G_("return makes integer from pointer "
6814 "without a cast"));
6815 return convert (type, rhs);
6817 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6819 tree ret;
6820 bool save = in_late_binary_op;
6821 in_late_binary_op = true;
6822 ret = convert (type, rhs);
6823 in_late_binary_op = save;
6824 return ret;
6827 switch (errtype)
6829 case ic_argpass:
6830 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6831 rname);
6832 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6833 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6834 "expected %qT but argument is of type %qT", type, rhstype);
6835 break;
6836 case ic_assign:
6837 error_at (location, "incompatible types when assigning to type %qT from "
6838 "type %qT", type, rhstype);
6839 break;
6840 case ic_init:
6841 error_at (location,
6842 "incompatible types when initializing type %qT using type %qT",
6843 type, rhstype);
6844 break;
6845 case ic_return:
6846 error_at (location,
6847 "incompatible types when returning type %qT but %qT was "
6848 "expected", rhstype, type);
6849 break;
6850 default:
6851 gcc_unreachable ();
6854 return error_mark_node;
6857 /* If VALUE is a compound expr all of whose expressions are constant, then
6858 return its value. Otherwise, return error_mark_node.
6860 This is for handling COMPOUND_EXPRs as initializer elements
6861 which is allowed with a warning when -pedantic is specified. */
6863 static tree
6864 valid_compound_expr_initializer (tree value, tree endtype)
6866 if (TREE_CODE (value) == COMPOUND_EXPR)
6868 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6869 == error_mark_node)
6870 return error_mark_node;
6871 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6872 endtype);
6874 else if (!initializer_constant_valid_p (value, endtype))
6875 return error_mark_node;
6876 else
6877 return value;
6880 /* Perform appropriate conversions on the initial value of a variable,
6881 store it in the declaration DECL,
6882 and print any error messages that are appropriate.
6883 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6884 If the init is invalid, store an ERROR_MARK.
6886 INIT_LOC is the location of the initial value. */
6888 void
6889 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6891 tree value, type;
6892 bool npc = false;
6894 /* If variable's type was invalidly declared, just ignore it. */
6896 type = TREE_TYPE (decl);
6897 if (TREE_CODE (type) == ERROR_MARK)
6898 return;
6900 /* Digest the specified initializer into an expression. */
6902 if (init)
6903 npc = null_pointer_constant_p (init);
6904 value = digest_init (init_loc, type, init, origtype, npc,
6905 true, TREE_STATIC (decl));
6907 /* Store the expression if valid; else report error. */
6909 if (!in_system_header_at (input_location)
6910 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6911 warning (OPT_Wtraditional, "traditional C rejects automatic "
6912 "aggregate initialization");
6914 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6915 DECL_INITIAL (decl) = value;
6917 /* ANSI wants warnings about out-of-range constant initializers. */
6918 STRIP_TYPE_NOPS (value);
6919 if (TREE_STATIC (decl))
6920 constant_expression_warning (value);
6922 /* Check if we need to set array size from compound literal size. */
6923 if (TREE_CODE (type) == ARRAY_TYPE
6924 && TYPE_DOMAIN (type) == NULL_TREE
6925 && value != error_mark_node)
6927 tree inside_init = init;
6929 STRIP_TYPE_NOPS (inside_init);
6930 inside_init = fold (inside_init);
6932 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6934 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6936 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6938 /* For int foo[] = (int [3]){1}; we need to set array size
6939 now since later on array initializer will be just the
6940 brace enclosed list of the compound literal. */
6941 tree etype = strip_array_types (TREE_TYPE (decl));
6942 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6943 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6944 layout_type (type);
6945 layout_decl (cldecl, 0);
6946 TREE_TYPE (decl)
6947 = c_build_qualified_type (type, TYPE_QUALS (etype));
6953 /* Methods for storing and printing names for error messages. */
6955 /* Implement a spelling stack that allows components of a name to be pushed
6956 and popped. Each element on the stack is this structure. */
6958 struct spelling
6960 int kind;
6961 union
6963 unsigned HOST_WIDE_INT i;
6964 const char *s;
6965 } u;
6968 #define SPELLING_STRING 1
6969 #define SPELLING_MEMBER 2
6970 #define SPELLING_BOUNDS 3
6972 static struct spelling *spelling; /* Next stack element (unused). */
6973 static struct spelling *spelling_base; /* Spelling stack base. */
6974 static int spelling_size; /* Size of the spelling stack. */
6976 /* Macros to save and restore the spelling stack around push_... functions.
6977 Alternative to SAVE_SPELLING_STACK. */
6979 #define SPELLING_DEPTH() (spelling - spelling_base)
6980 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6982 /* Push an element on the spelling stack with type KIND and assign VALUE
6983 to MEMBER. */
6985 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6987 int depth = SPELLING_DEPTH (); \
6989 if (depth >= spelling_size) \
6991 spelling_size += 10; \
6992 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6993 spelling_size); \
6994 RESTORE_SPELLING_DEPTH (depth); \
6997 spelling->kind = (KIND); \
6998 spelling->MEMBER = (VALUE); \
6999 spelling++; \
7002 /* Push STRING on the stack. Printed literally. */
7004 static void
7005 push_string (const char *string)
7007 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7010 /* Push a member name on the stack. Printed as '.' STRING. */
7012 static void
7013 push_member_name (tree decl)
7015 const char *const string
7016 = (DECL_NAME (decl)
7017 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7018 : _("<anonymous>"));
7019 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7022 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7024 static void
7025 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7027 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7030 /* Compute the maximum size in bytes of the printed spelling. */
7032 static int
7033 spelling_length (void)
7035 int size = 0;
7036 struct spelling *p;
7038 for (p = spelling_base; p < spelling; p++)
7040 if (p->kind == SPELLING_BOUNDS)
7041 size += 25;
7042 else
7043 size += strlen (p->u.s) + 1;
7046 return size;
7049 /* Print the spelling to BUFFER and return it. */
7051 static char *
7052 print_spelling (char *buffer)
7054 char *d = buffer;
7055 struct spelling *p;
7057 for (p = spelling_base; p < spelling; p++)
7058 if (p->kind == SPELLING_BOUNDS)
7060 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7061 d += strlen (d);
7063 else
7065 const char *s;
7066 if (p->kind == SPELLING_MEMBER)
7067 *d++ = '.';
7068 for (s = p->u.s; (*d = *s++); d++)
7071 *d++ = '\0';
7072 return buffer;
7075 /* Digest the parser output INIT as an initializer for type TYPE.
7076 Return a C expression of type TYPE to represent the initial value.
7078 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7080 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7082 If INIT is a string constant, STRICT_STRING is true if it is
7083 unparenthesized or we should not warn here for it being parenthesized.
7084 For other types of INIT, STRICT_STRING is not used.
7086 INIT_LOC is the location of the INIT.
7088 REQUIRE_CONSTANT requests an error if non-constant initializers or
7089 elements are seen. */
7091 static tree
7092 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7093 bool null_pointer_constant, bool strict_string,
7094 int require_constant)
7096 enum tree_code code = TREE_CODE (type);
7097 tree inside_init = init;
7098 tree semantic_type = NULL_TREE;
7099 bool maybe_const = true;
7101 if (type == error_mark_node
7102 || !init
7103 || error_operand_p (init))
7104 return error_mark_node;
7106 STRIP_TYPE_NOPS (inside_init);
7108 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7110 semantic_type = TREE_TYPE (inside_init);
7111 inside_init = TREE_OPERAND (inside_init, 0);
7113 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7114 inside_init = decl_constant_value_for_optimization (inside_init);
7116 /* Initialization of an array of chars from a string constant
7117 optionally enclosed in braces. */
7119 if (code == ARRAY_TYPE && inside_init
7120 && TREE_CODE (inside_init) == STRING_CST)
7122 tree typ1
7123 = (TYPE_ATOMIC (TREE_TYPE (type))
7124 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7125 TYPE_QUAL_ATOMIC)
7126 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7127 /* Note that an array could be both an array of character type
7128 and an array of wchar_t if wchar_t is signed char or unsigned
7129 char. */
7130 bool char_array = (typ1 == char_type_node
7131 || typ1 == signed_char_type_node
7132 || typ1 == unsigned_char_type_node);
7133 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7134 bool char16_array = !!comptypes (typ1, char16_type_node);
7135 bool char32_array = !!comptypes (typ1, char32_type_node);
7137 if (char_array || wchar_array || char16_array || char32_array)
7139 struct c_expr expr;
7140 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7141 expr.value = inside_init;
7142 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7143 expr.original_type = NULL;
7144 maybe_warn_string_init (init_loc, type, expr);
7146 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7147 pedwarn_init (init_loc, OPT_Wpedantic,
7148 "initialization of a flexible array member");
7150 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7151 TYPE_MAIN_VARIANT (type)))
7152 return inside_init;
7154 if (char_array)
7156 if (typ2 != char_type_node)
7158 error_init (init_loc, "char-array initialized from wide "
7159 "string");
7160 return error_mark_node;
7163 else
7165 if (typ2 == char_type_node)
7167 error_init (init_loc, "wide character array initialized "
7168 "from non-wide string");
7169 return error_mark_node;
7171 else if (!comptypes(typ1, typ2))
7173 error_init (init_loc, "wide character array initialized "
7174 "from incompatible wide string");
7175 return error_mark_node;
7179 TREE_TYPE (inside_init) = type;
7180 if (TYPE_DOMAIN (type) != NULL_TREE
7181 && TYPE_SIZE (type) != NULL_TREE
7182 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7184 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7186 /* Subtract the size of a single (possibly wide) character
7187 because it's ok to ignore the terminating null char
7188 that is counted in the length of the constant. */
7189 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7190 (len
7191 - (TYPE_PRECISION (typ1)
7192 / BITS_PER_UNIT))))
7193 pedwarn_init (init_loc, 0,
7194 ("initializer-string for array of chars "
7195 "is too long"));
7196 else if (warn_cxx_compat
7197 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7198 warning_at (init_loc, OPT_Wc___compat,
7199 ("initializer-string for array chars "
7200 "is too long for C++"));
7203 return inside_init;
7205 else if (INTEGRAL_TYPE_P (typ1))
7207 error_init (init_loc, "array of inappropriate type initialized "
7208 "from string constant");
7209 return error_mark_node;
7213 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7214 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7215 below and handle as a constructor. */
7216 if (code == VECTOR_TYPE
7217 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7218 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7219 && TREE_CONSTANT (inside_init))
7221 if (TREE_CODE (inside_init) == VECTOR_CST
7222 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7223 TYPE_MAIN_VARIANT (type)))
7224 return inside_init;
7226 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7228 unsigned HOST_WIDE_INT ix;
7229 tree value;
7230 bool constant_p = true;
7232 /* Iterate through elements and check if all constructor
7233 elements are *_CSTs. */
7234 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7235 if (!CONSTANT_CLASS_P (value))
7237 constant_p = false;
7238 break;
7241 if (constant_p)
7242 return build_vector_from_ctor (type,
7243 CONSTRUCTOR_ELTS (inside_init));
7247 if (warn_sequence_point)
7248 verify_sequence_points (inside_init);
7250 /* Any type can be initialized
7251 from an expression of the same type, optionally with braces. */
7253 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7254 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7255 TYPE_MAIN_VARIANT (type))
7256 || (code == ARRAY_TYPE
7257 && comptypes (TREE_TYPE (inside_init), type))
7258 || (code == VECTOR_TYPE
7259 && comptypes (TREE_TYPE (inside_init), type))
7260 || (code == POINTER_TYPE
7261 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7262 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7263 TREE_TYPE (type)))))
7265 if (code == POINTER_TYPE)
7267 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7269 if (TREE_CODE (inside_init) == STRING_CST
7270 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7271 inside_init = array_to_pointer_conversion
7272 (init_loc, inside_init);
7273 else
7275 error_init (init_loc, "invalid use of non-lvalue array");
7276 return error_mark_node;
7281 if (code == VECTOR_TYPE)
7282 /* Although the types are compatible, we may require a
7283 conversion. */
7284 inside_init = convert (type, inside_init);
7286 if (require_constant
7287 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7289 /* As an extension, allow initializing objects with static storage
7290 duration with compound literals (which are then treated just as
7291 the brace enclosed list they contain). Also allow this for
7292 vectors, as we can only assign them with compound literals. */
7293 if (flag_isoc99 && code != VECTOR_TYPE)
7294 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7295 "is not constant");
7296 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7297 inside_init = DECL_INITIAL (decl);
7300 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7301 && TREE_CODE (inside_init) != CONSTRUCTOR)
7303 error_init (init_loc, "array initialized from non-constant array "
7304 "expression");
7305 return error_mark_node;
7308 /* Compound expressions can only occur here if -Wpedantic or
7309 -pedantic-errors is specified. In the later case, we always want
7310 an error. In the former case, we simply want a warning. */
7311 if (require_constant && pedantic
7312 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7314 inside_init
7315 = valid_compound_expr_initializer (inside_init,
7316 TREE_TYPE (inside_init));
7317 if (inside_init == error_mark_node)
7318 error_init (init_loc, "initializer element is not constant");
7319 else
7320 pedwarn_init (init_loc, OPT_Wpedantic,
7321 "initializer element is not constant");
7322 if (flag_pedantic_errors)
7323 inside_init = error_mark_node;
7325 else if (require_constant
7326 && !initializer_constant_valid_p (inside_init,
7327 TREE_TYPE (inside_init)))
7329 error_init (init_loc, "initializer element is not constant");
7330 inside_init = error_mark_node;
7332 else if (require_constant && !maybe_const)
7333 pedwarn_init (init_loc, OPT_Wpedantic,
7334 "initializer element is not a constant expression");
7336 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7337 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7338 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7339 type, inside_init, origtype,
7340 ic_init, null_pointer_constant,
7341 NULL_TREE, NULL_TREE, 0);
7342 return inside_init;
7345 /* Handle scalar types, including conversions. */
7347 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7348 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7349 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7351 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7352 && (TREE_CODE (init) == STRING_CST
7353 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7354 inside_init = init = array_to_pointer_conversion (init_loc, init);
7355 if (semantic_type)
7356 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7357 inside_init);
7358 inside_init
7359 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7360 inside_init, origtype, ic_init,
7361 null_pointer_constant, NULL_TREE, NULL_TREE,
7364 /* Check to see if we have already given an error message. */
7365 if (inside_init == error_mark_node)
7367 else if (require_constant && !TREE_CONSTANT (inside_init))
7369 error_init (init_loc, "initializer element is not constant");
7370 inside_init = error_mark_node;
7372 else if (require_constant
7373 && !initializer_constant_valid_p (inside_init,
7374 TREE_TYPE (inside_init)))
7376 error_init (init_loc, "initializer element is not computable at "
7377 "load time");
7378 inside_init = error_mark_node;
7380 else if (require_constant && !maybe_const)
7381 pedwarn_init (init_loc, OPT_Wpedantic,
7382 "initializer element is not a constant expression");
7384 return inside_init;
7387 /* Come here only for records and arrays. */
7389 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7391 error_init (init_loc, "variable-sized object may not be initialized");
7392 return error_mark_node;
7395 error_init (init_loc, "invalid initializer");
7396 return error_mark_node;
7399 /* Handle initializers that use braces. */
7401 /* Type of object we are accumulating a constructor for.
7402 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7403 static tree constructor_type;
7405 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7406 left to fill. */
7407 static tree constructor_fields;
7409 /* For an ARRAY_TYPE, this is the specified index
7410 at which to store the next element we get. */
7411 static tree constructor_index;
7413 /* For an ARRAY_TYPE, this is the maximum index. */
7414 static tree constructor_max_index;
7416 /* For a RECORD_TYPE, this is the first field not yet written out. */
7417 static tree constructor_unfilled_fields;
7419 /* For an ARRAY_TYPE, this is the index of the first element
7420 not yet written out. */
7421 static tree constructor_unfilled_index;
7423 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7424 This is so we can generate gaps between fields, when appropriate. */
7425 static tree constructor_bit_index;
7427 /* If we are saving up the elements rather than allocating them,
7428 this is the list of elements so far (in reverse order,
7429 most recent first). */
7430 static vec<constructor_elt, va_gc> *constructor_elements;
7432 /* 1 if constructor should be incrementally stored into a constructor chain,
7433 0 if all the elements should be kept in AVL tree. */
7434 static int constructor_incremental;
7436 /* 1 if so far this constructor's elements are all compile-time constants. */
7437 static int constructor_constant;
7439 /* 1 if so far this constructor's elements are all valid address constants. */
7440 static int constructor_simple;
7442 /* 1 if this constructor has an element that cannot be part of a
7443 constant expression. */
7444 static int constructor_nonconst;
7446 /* 1 if this constructor is erroneous so far. */
7447 static int constructor_erroneous;
7449 /* 1 if this constructor is the universal zero initializer { 0 }. */
7450 static int constructor_zeroinit;
7452 /* Structure for managing pending initializer elements, organized as an
7453 AVL tree. */
7455 struct init_node
7457 struct init_node *left, *right;
7458 struct init_node *parent;
7459 int balance;
7460 tree purpose;
7461 tree value;
7462 tree origtype;
7465 /* Tree of pending elements at this constructor level.
7466 These are elements encountered out of order
7467 which belong at places we haven't reached yet in actually
7468 writing the output.
7469 Will never hold tree nodes across GC runs. */
7470 static struct init_node *constructor_pending_elts;
7472 /* The SPELLING_DEPTH of this constructor. */
7473 static int constructor_depth;
7475 /* DECL node for which an initializer is being read.
7476 0 means we are reading a constructor expression
7477 such as (struct foo) {...}. */
7478 static tree constructor_decl;
7480 /* Nonzero if this is an initializer for a top-level decl. */
7481 static int constructor_top_level;
7483 /* Nonzero if there were any member designators in this initializer. */
7484 static int constructor_designated;
7486 /* Nesting depth of designator list. */
7487 static int designator_depth;
7489 /* Nonzero if there were diagnosed errors in this designator list. */
7490 static int designator_erroneous;
7493 /* This stack has a level for each implicit or explicit level of
7494 structuring in the initializer, including the outermost one. It
7495 saves the values of most of the variables above. */
7497 struct constructor_range_stack;
7499 struct constructor_stack
7501 struct constructor_stack *next;
7502 tree type;
7503 tree fields;
7504 tree index;
7505 tree max_index;
7506 tree unfilled_index;
7507 tree unfilled_fields;
7508 tree bit_index;
7509 vec<constructor_elt, va_gc> *elements;
7510 struct init_node *pending_elts;
7511 int offset;
7512 int depth;
7513 /* If value nonzero, this value should replace the entire
7514 constructor at this level. */
7515 struct c_expr replacement_value;
7516 struct constructor_range_stack *range_stack;
7517 char constant;
7518 char simple;
7519 char nonconst;
7520 char implicit;
7521 char erroneous;
7522 char outer;
7523 char incremental;
7524 char designated;
7525 int designator_depth;
7528 static struct constructor_stack *constructor_stack;
7530 /* This stack represents designators from some range designator up to
7531 the last designator in the list. */
7533 struct constructor_range_stack
7535 struct constructor_range_stack *next, *prev;
7536 struct constructor_stack *stack;
7537 tree range_start;
7538 tree index;
7539 tree range_end;
7540 tree fields;
7543 static struct constructor_range_stack *constructor_range_stack;
7545 /* This stack records separate initializers that are nested.
7546 Nested initializers can't happen in ANSI C, but GNU C allows them
7547 in cases like { ... (struct foo) { ... } ... }. */
7549 struct initializer_stack
7551 struct initializer_stack *next;
7552 tree decl;
7553 struct constructor_stack *constructor_stack;
7554 struct constructor_range_stack *constructor_range_stack;
7555 vec<constructor_elt, va_gc> *elements;
7556 struct spelling *spelling;
7557 struct spelling *spelling_base;
7558 int spelling_size;
7559 char top_level;
7560 char require_constant_value;
7561 char require_constant_elements;
7562 rich_location *missing_brace_richloc;
7565 static struct initializer_stack *initializer_stack;
7567 /* Prepare to parse and output the initializer for variable DECL. */
7569 void
7570 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7571 rich_location *richloc)
7573 const char *locus;
7574 struct initializer_stack *p = XNEW (struct initializer_stack);
7576 p->decl = constructor_decl;
7577 p->require_constant_value = require_constant_value;
7578 p->require_constant_elements = require_constant_elements;
7579 p->constructor_stack = constructor_stack;
7580 p->constructor_range_stack = constructor_range_stack;
7581 p->elements = constructor_elements;
7582 p->spelling = spelling;
7583 p->spelling_base = spelling_base;
7584 p->spelling_size = spelling_size;
7585 p->top_level = constructor_top_level;
7586 p->next = initializer_stack;
7587 p->missing_brace_richloc = richloc;
7588 initializer_stack = p;
7590 constructor_decl = decl;
7591 constructor_designated = 0;
7592 constructor_top_level = top_level;
7594 if (decl != NULL_TREE && decl != error_mark_node)
7596 require_constant_value = TREE_STATIC (decl);
7597 require_constant_elements
7598 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7599 /* For a scalar, you can always use any value to initialize,
7600 even within braces. */
7601 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7602 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7604 else
7606 require_constant_value = 0;
7607 require_constant_elements = 0;
7608 locus = _("(anonymous)");
7611 constructor_stack = 0;
7612 constructor_range_stack = 0;
7614 found_missing_braces = 0;
7616 spelling_base = 0;
7617 spelling_size = 0;
7618 RESTORE_SPELLING_DEPTH (0);
7620 if (locus)
7621 push_string (locus);
7624 void
7625 finish_init (void)
7627 struct initializer_stack *p = initializer_stack;
7629 /* Free the whole constructor stack of this initializer. */
7630 while (constructor_stack)
7632 struct constructor_stack *q = constructor_stack;
7633 constructor_stack = q->next;
7634 free (q);
7637 gcc_assert (!constructor_range_stack);
7639 /* Pop back to the data of the outer initializer (if any). */
7640 free (spelling_base);
7642 constructor_decl = p->decl;
7643 require_constant_value = p->require_constant_value;
7644 require_constant_elements = p->require_constant_elements;
7645 constructor_stack = p->constructor_stack;
7646 constructor_range_stack = p->constructor_range_stack;
7647 constructor_elements = p->elements;
7648 spelling = p->spelling;
7649 spelling_base = p->spelling_base;
7650 spelling_size = p->spelling_size;
7651 constructor_top_level = p->top_level;
7652 initializer_stack = p->next;
7653 free (p);
7656 /* Call here when we see the initializer is surrounded by braces.
7657 This is instead of a call to push_init_level;
7658 it is matched by a call to pop_init_level.
7660 TYPE is the type to initialize, for a constructor expression.
7661 For an initializer for a decl, TYPE is zero. */
7663 void
7664 really_start_incremental_init (tree type)
7666 struct constructor_stack *p = XNEW (struct constructor_stack);
7668 if (type == NULL_TREE)
7669 type = TREE_TYPE (constructor_decl);
7671 if (VECTOR_TYPE_P (type)
7672 && TYPE_VECTOR_OPAQUE (type))
7673 error ("opaque vector types cannot be initialized");
7675 p->type = constructor_type;
7676 p->fields = constructor_fields;
7677 p->index = constructor_index;
7678 p->max_index = constructor_max_index;
7679 p->unfilled_index = constructor_unfilled_index;
7680 p->unfilled_fields = constructor_unfilled_fields;
7681 p->bit_index = constructor_bit_index;
7682 p->elements = constructor_elements;
7683 p->constant = constructor_constant;
7684 p->simple = constructor_simple;
7685 p->nonconst = constructor_nonconst;
7686 p->erroneous = constructor_erroneous;
7687 p->pending_elts = constructor_pending_elts;
7688 p->depth = constructor_depth;
7689 p->replacement_value.value = 0;
7690 p->replacement_value.original_code = ERROR_MARK;
7691 p->replacement_value.original_type = NULL;
7692 p->implicit = 0;
7693 p->range_stack = 0;
7694 p->outer = 0;
7695 p->incremental = constructor_incremental;
7696 p->designated = constructor_designated;
7697 p->designator_depth = designator_depth;
7698 p->next = 0;
7699 constructor_stack = p;
7701 constructor_constant = 1;
7702 constructor_simple = 1;
7703 constructor_nonconst = 0;
7704 constructor_depth = SPELLING_DEPTH ();
7705 constructor_elements = NULL;
7706 constructor_pending_elts = 0;
7707 constructor_type = type;
7708 constructor_incremental = 1;
7709 constructor_designated = 0;
7710 constructor_zeroinit = 1;
7711 designator_depth = 0;
7712 designator_erroneous = 0;
7714 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7716 constructor_fields = TYPE_FIELDS (constructor_type);
7717 /* Skip any nameless bit fields at the beginning. */
7718 while (constructor_fields != NULL_TREE
7719 && DECL_C_BIT_FIELD (constructor_fields)
7720 && DECL_NAME (constructor_fields) == NULL_TREE)
7721 constructor_fields = DECL_CHAIN (constructor_fields);
7723 constructor_unfilled_fields = constructor_fields;
7724 constructor_bit_index = bitsize_zero_node;
7726 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7728 if (TYPE_DOMAIN (constructor_type))
7730 constructor_max_index
7731 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7733 /* Detect non-empty initializations of zero-length arrays. */
7734 if (constructor_max_index == NULL_TREE
7735 && TYPE_SIZE (constructor_type))
7736 constructor_max_index = integer_minus_one_node;
7738 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7739 to initialize VLAs will cause a proper error; avoid tree
7740 checking errors as well by setting a safe value. */
7741 if (constructor_max_index
7742 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7743 constructor_max_index = integer_minus_one_node;
7745 constructor_index
7746 = convert (bitsizetype,
7747 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7749 else
7751 constructor_index = bitsize_zero_node;
7752 constructor_max_index = NULL_TREE;
7755 constructor_unfilled_index = constructor_index;
7757 else if (VECTOR_TYPE_P (constructor_type))
7759 /* Vectors are like simple fixed-size arrays. */
7760 constructor_max_index =
7761 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7762 constructor_index = bitsize_zero_node;
7763 constructor_unfilled_index = constructor_index;
7765 else
7767 /* Handle the case of int x = {5}; */
7768 constructor_fields = constructor_type;
7769 constructor_unfilled_fields = constructor_type;
7773 extern location_t last_init_list_comma;
7775 /* Called when we see an open brace for a nested initializer. Finish
7776 off any pending levels with implicit braces. */
7777 void
7778 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7780 while (constructor_stack->implicit)
7782 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7783 && constructor_fields == NULL_TREE)
7784 process_init_element (input_location,
7785 pop_init_level (loc, 1, braced_init_obstack,
7786 last_init_list_comma),
7787 true, braced_init_obstack);
7788 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7789 && constructor_max_index
7790 && tree_int_cst_lt (constructor_max_index,
7791 constructor_index))
7792 process_init_element (input_location,
7793 pop_init_level (loc, 1, braced_init_obstack,
7794 last_init_list_comma),
7795 true, braced_init_obstack);
7796 else
7797 break;
7801 /* Push down into a subobject, for initialization.
7802 If this is for an explicit set of braces, IMPLICIT is 0.
7803 If it is because the next element belongs at a lower level,
7804 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7806 void
7807 push_init_level (location_t loc, int implicit,
7808 struct obstack *braced_init_obstack)
7810 struct constructor_stack *p;
7811 tree value = NULL_TREE;
7813 /* Unless this is an explicit brace, we need to preserve previous
7814 content if any. */
7815 if (implicit)
7817 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7818 value = find_init_member (constructor_fields, braced_init_obstack);
7819 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7820 value = find_init_member (constructor_index, braced_init_obstack);
7823 p = XNEW (struct constructor_stack);
7824 p->type = constructor_type;
7825 p->fields = constructor_fields;
7826 p->index = constructor_index;
7827 p->max_index = constructor_max_index;
7828 p->unfilled_index = constructor_unfilled_index;
7829 p->unfilled_fields = constructor_unfilled_fields;
7830 p->bit_index = constructor_bit_index;
7831 p->elements = constructor_elements;
7832 p->constant = constructor_constant;
7833 p->simple = constructor_simple;
7834 p->nonconst = constructor_nonconst;
7835 p->erroneous = constructor_erroneous;
7836 p->pending_elts = constructor_pending_elts;
7837 p->depth = constructor_depth;
7838 p->replacement_value.value = NULL_TREE;
7839 p->replacement_value.original_code = ERROR_MARK;
7840 p->replacement_value.original_type = NULL;
7841 p->implicit = implicit;
7842 p->outer = 0;
7843 p->incremental = constructor_incremental;
7844 p->designated = constructor_designated;
7845 p->designator_depth = designator_depth;
7846 p->next = constructor_stack;
7847 p->range_stack = 0;
7848 constructor_stack = p;
7850 constructor_constant = 1;
7851 constructor_simple = 1;
7852 constructor_nonconst = 0;
7853 constructor_depth = SPELLING_DEPTH ();
7854 constructor_elements = NULL;
7855 constructor_incremental = 1;
7856 constructor_designated = 0;
7857 constructor_pending_elts = 0;
7858 if (!implicit)
7860 p->range_stack = constructor_range_stack;
7861 constructor_range_stack = 0;
7862 designator_depth = 0;
7863 designator_erroneous = 0;
7866 /* Don't die if an entire brace-pair level is superfluous
7867 in the containing level. */
7868 if (constructor_type == NULL_TREE)
7870 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7872 /* Don't die if there are extra init elts at the end. */
7873 if (constructor_fields == NULL_TREE)
7874 constructor_type = NULL_TREE;
7875 else
7877 constructor_type = TREE_TYPE (constructor_fields);
7878 push_member_name (constructor_fields);
7879 constructor_depth++;
7881 /* If upper initializer is designated, then mark this as
7882 designated too to prevent bogus warnings. */
7883 constructor_designated = p->designated;
7885 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7887 constructor_type = TREE_TYPE (constructor_type);
7888 push_array_bounds (tree_to_uhwi (constructor_index));
7889 constructor_depth++;
7892 if (constructor_type == NULL_TREE)
7894 error_init (loc, "extra brace group at end of initializer");
7895 constructor_fields = NULL_TREE;
7896 constructor_unfilled_fields = NULL_TREE;
7897 return;
7900 if (value && TREE_CODE (value) == CONSTRUCTOR)
7902 constructor_constant = TREE_CONSTANT (value);
7903 constructor_simple = TREE_STATIC (value);
7904 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7905 constructor_elements = CONSTRUCTOR_ELTS (value);
7906 if (!vec_safe_is_empty (constructor_elements)
7907 && (TREE_CODE (constructor_type) == RECORD_TYPE
7908 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7909 set_nonincremental_init (braced_init_obstack);
7912 if (implicit == 1)
7914 found_missing_braces = 1;
7915 if (initializer_stack->missing_brace_richloc)
7916 initializer_stack->missing_brace_richloc->add_fixit_insert_before
7917 (loc, "{");
7920 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7922 constructor_fields = TYPE_FIELDS (constructor_type);
7923 /* Skip any nameless bit fields at the beginning. */
7924 while (constructor_fields != NULL_TREE
7925 && DECL_C_BIT_FIELD (constructor_fields)
7926 && DECL_NAME (constructor_fields) == NULL_TREE)
7927 constructor_fields = DECL_CHAIN (constructor_fields);
7929 constructor_unfilled_fields = constructor_fields;
7930 constructor_bit_index = bitsize_zero_node;
7932 else if (VECTOR_TYPE_P (constructor_type))
7934 /* Vectors are like simple fixed-size arrays. */
7935 constructor_max_index =
7936 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7937 constructor_index = bitsize_int (0);
7938 constructor_unfilled_index = constructor_index;
7940 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7942 if (TYPE_DOMAIN (constructor_type))
7944 constructor_max_index
7945 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7947 /* Detect non-empty initializations of zero-length arrays. */
7948 if (constructor_max_index == NULL_TREE
7949 && TYPE_SIZE (constructor_type))
7950 constructor_max_index = integer_minus_one_node;
7952 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7953 to initialize VLAs will cause a proper error; avoid tree
7954 checking errors as well by setting a safe value. */
7955 if (constructor_max_index
7956 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7957 constructor_max_index = integer_minus_one_node;
7959 constructor_index
7960 = convert (bitsizetype,
7961 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7963 else
7964 constructor_index = bitsize_zero_node;
7966 constructor_unfilled_index = constructor_index;
7967 if (value && TREE_CODE (value) == STRING_CST)
7969 /* We need to split the char/wchar array into individual
7970 characters, so that we don't have to special case it
7971 everywhere. */
7972 set_nonincremental_init_from_string (value, braced_init_obstack);
7975 else
7977 if (constructor_type != error_mark_node)
7978 warning_init (input_location, 0, "braces around scalar initializer");
7979 constructor_fields = constructor_type;
7980 constructor_unfilled_fields = constructor_type;
7984 /* At the end of an implicit or explicit brace level,
7985 finish up that level of constructor. If a single expression
7986 with redundant braces initialized that level, return the
7987 c_expr structure for that expression. Otherwise, the original_code
7988 element is set to ERROR_MARK.
7989 If we were outputting the elements as they are read, return 0 as the value
7990 from inner levels (process_init_element ignores that),
7991 but return error_mark_node as the value from the outermost level
7992 (that's what we want to put in DECL_INITIAL).
7993 Otherwise, return a CONSTRUCTOR expression as the value. */
7995 struct c_expr
7996 pop_init_level (location_t loc, int implicit,
7997 struct obstack *braced_init_obstack,
7998 location_t insert_before)
8000 struct constructor_stack *p;
8001 struct c_expr ret;
8002 ret.value = NULL_TREE;
8003 ret.original_code = ERROR_MARK;
8004 ret.original_type = NULL;
8006 if (implicit == 0)
8008 /* When we come to an explicit close brace,
8009 pop any inner levels that didn't have explicit braces. */
8010 while (constructor_stack->implicit)
8011 process_init_element (input_location,
8012 pop_init_level (loc, 1, braced_init_obstack,
8013 insert_before),
8014 true, braced_init_obstack);
8015 gcc_assert (!constructor_range_stack);
8017 else
8018 if (initializer_stack->missing_brace_richloc)
8019 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8020 (insert_before, "}");
8022 /* Now output all pending elements. */
8023 constructor_incremental = 1;
8024 output_pending_init_elements (1, braced_init_obstack);
8026 p = constructor_stack;
8028 /* Error for initializing a flexible array member, or a zero-length
8029 array member in an inappropriate context. */
8030 if (constructor_type && constructor_fields
8031 && TREE_CODE (constructor_type) == ARRAY_TYPE
8032 && TYPE_DOMAIN (constructor_type)
8033 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8035 /* Silently discard empty initializations. The parser will
8036 already have pedwarned for empty brackets. */
8037 if (integer_zerop (constructor_unfilled_index))
8038 constructor_type = NULL_TREE;
8039 else
8041 gcc_assert (!TYPE_SIZE (constructor_type));
8043 if (constructor_depth > 2)
8044 error_init (loc, "initialization of flexible array member in a nested context");
8045 else
8046 pedwarn_init (loc, OPT_Wpedantic,
8047 "initialization of a flexible array member");
8049 /* We have already issued an error message for the existence
8050 of a flexible array member not at the end of the structure.
8051 Discard the initializer so that we do not die later. */
8052 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8053 constructor_type = NULL_TREE;
8057 switch (vec_safe_length (constructor_elements))
8059 case 0:
8060 /* Initialization with { } counts as zeroinit. */
8061 constructor_zeroinit = 1;
8062 break;
8063 case 1:
8064 /* This might be zeroinit as well. */
8065 if (integer_zerop ((*constructor_elements)[0].value))
8066 constructor_zeroinit = 1;
8067 break;
8068 default:
8069 /* If the constructor has more than one element, it can't be { 0 }. */
8070 constructor_zeroinit = 0;
8071 break;
8074 /* Warn when some structs are initialized with direct aggregation. */
8075 if (!implicit && found_missing_braces && warn_missing_braces
8076 && !constructor_zeroinit)
8078 gcc_assert (initializer_stack->missing_brace_richloc);
8079 warning_at_rich_loc (initializer_stack->missing_brace_richloc,
8080 OPT_Wmissing_braces,
8081 "missing braces around initializer");
8084 /* Warn when some struct elements are implicitly initialized to zero. */
8085 if (warn_missing_field_initializers
8086 && constructor_type
8087 && TREE_CODE (constructor_type) == RECORD_TYPE
8088 && constructor_unfilled_fields)
8090 /* Do not warn for flexible array members or zero-length arrays. */
8091 while (constructor_unfilled_fields
8092 && (!DECL_SIZE (constructor_unfilled_fields)
8093 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8094 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8096 if (constructor_unfilled_fields
8097 /* Do not warn if this level of the initializer uses member
8098 designators; it is likely to be deliberate. */
8099 && !constructor_designated
8100 /* Do not warn about initializing with { 0 } or with { }. */
8101 && !constructor_zeroinit)
8103 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8104 "missing initializer for field %qD of %qT",
8105 constructor_unfilled_fields,
8106 constructor_type))
8107 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8108 "%qD declared here", constructor_unfilled_fields);
8112 /* Pad out the end of the structure. */
8113 if (p->replacement_value.value)
8114 /* If this closes a superfluous brace pair,
8115 just pass out the element between them. */
8116 ret = p->replacement_value;
8117 else if (constructor_type == NULL_TREE)
8119 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8120 && TREE_CODE (constructor_type) != ARRAY_TYPE
8121 && !VECTOR_TYPE_P (constructor_type))
8123 /* A nonincremental scalar initializer--just return
8124 the element, after verifying there is just one. */
8125 if (vec_safe_is_empty (constructor_elements))
8127 if (!constructor_erroneous)
8128 error_init (loc, "empty scalar initializer");
8129 ret.value = error_mark_node;
8131 else if (vec_safe_length (constructor_elements) != 1)
8133 error_init (loc, "extra elements in scalar initializer");
8134 ret.value = (*constructor_elements)[0].value;
8136 else
8137 ret.value = (*constructor_elements)[0].value;
8139 else
8141 if (constructor_erroneous)
8142 ret.value = error_mark_node;
8143 else
8145 ret.value = build_constructor (constructor_type,
8146 constructor_elements);
8147 if (constructor_constant)
8148 TREE_CONSTANT (ret.value) = 1;
8149 if (constructor_constant && constructor_simple)
8150 TREE_STATIC (ret.value) = 1;
8151 if (constructor_nonconst)
8152 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8156 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8158 if (constructor_nonconst)
8159 ret.original_code = C_MAYBE_CONST_EXPR;
8160 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8161 ret.original_code = ERROR_MARK;
8164 constructor_type = p->type;
8165 constructor_fields = p->fields;
8166 constructor_index = p->index;
8167 constructor_max_index = p->max_index;
8168 constructor_unfilled_index = p->unfilled_index;
8169 constructor_unfilled_fields = p->unfilled_fields;
8170 constructor_bit_index = p->bit_index;
8171 constructor_elements = p->elements;
8172 constructor_constant = p->constant;
8173 constructor_simple = p->simple;
8174 constructor_nonconst = p->nonconst;
8175 constructor_erroneous = p->erroneous;
8176 constructor_incremental = p->incremental;
8177 constructor_designated = p->designated;
8178 designator_depth = p->designator_depth;
8179 constructor_pending_elts = p->pending_elts;
8180 constructor_depth = p->depth;
8181 if (!p->implicit)
8182 constructor_range_stack = p->range_stack;
8183 RESTORE_SPELLING_DEPTH (constructor_depth);
8185 constructor_stack = p->next;
8186 free (p);
8188 if (ret.value == NULL_TREE && constructor_stack == 0)
8189 ret.value = error_mark_node;
8190 return ret;
8193 /* Common handling for both array range and field name designators.
8194 ARRAY argument is nonzero for array ranges. Returns false for success. */
8196 static bool
8197 set_designator (location_t loc, int array,
8198 struct obstack *braced_init_obstack)
8200 tree subtype;
8201 enum tree_code subcode;
8203 /* Don't die if an entire brace-pair level is superfluous
8204 in the containing level. */
8205 if (constructor_type == NULL_TREE)
8206 return true;
8208 /* If there were errors in this designator list already, bail out
8209 silently. */
8210 if (designator_erroneous)
8211 return true;
8213 if (!designator_depth)
8215 gcc_assert (!constructor_range_stack);
8217 /* Designator list starts at the level of closest explicit
8218 braces. */
8219 while (constructor_stack->implicit)
8220 process_init_element (input_location,
8221 pop_init_level (loc, 1, braced_init_obstack,
8222 last_init_list_comma),
8223 true, braced_init_obstack);
8224 constructor_designated = 1;
8225 return false;
8228 switch (TREE_CODE (constructor_type))
8230 case RECORD_TYPE:
8231 case UNION_TYPE:
8232 subtype = TREE_TYPE (constructor_fields);
8233 if (subtype != error_mark_node)
8234 subtype = TYPE_MAIN_VARIANT (subtype);
8235 break;
8236 case ARRAY_TYPE:
8237 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8238 break;
8239 default:
8240 gcc_unreachable ();
8243 subcode = TREE_CODE (subtype);
8244 if (array && subcode != ARRAY_TYPE)
8246 error_init (loc, "array index in non-array initializer");
8247 return true;
8249 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8251 error_init (loc, "field name not in record or union initializer");
8252 return true;
8255 constructor_designated = 1;
8256 finish_implicit_inits (loc, braced_init_obstack);
8257 push_init_level (loc, 2, braced_init_obstack);
8258 return false;
8261 /* If there are range designators in designator list, push a new designator
8262 to constructor_range_stack. RANGE_END is end of such stack range or
8263 NULL_TREE if there is no range designator at this level. */
8265 static void
8266 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8268 struct constructor_range_stack *p;
8270 p = (struct constructor_range_stack *)
8271 obstack_alloc (braced_init_obstack,
8272 sizeof (struct constructor_range_stack));
8273 p->prev = constructor_range_stack;
8274 p->next = 0;
8275 p->fields = constructor_fields;
8276 p->range_start = constructor_index;
8277 p->index = constructor_index;
8278 p->stack = constructor_stack;
8279 p->range_end = range_end;
8280 if (constructor_range_stack)
8281 constructor_range_stack->next = p;
8282 constructor_range_stack = p;
8285 /* Within an array initializer, specify the next index to be initialized.
8286 FIRST is that index. If LAST is nonzero, then initialize a range
8287 of indices, running from FIRST through LAST. */
8289 void
8290 set_init_index (location_t loc, tree first, tree last,
8291 struct obstack *braced_init_obstack)
8293 if (set_designator (loc, 1, braced_init_obstack))
8294 return;
8296 designator_erroneous = 1;
8298 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8299 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8301 error_init (loc, "array index in initializer not of integer type");
8302 return;
8305 if (TREE_CODE (first) != INTEGER_CST)
8307 first = c_fully_fold (first, false, NULL);
8308 if (TREE_CODE (first) == INTEGER_CST)
8309 pedwarn_init (loc, OPT_Wpedantic,
8310 "array index in initializer is not "
8311 "an integer constant expression");
8314 if (last && TREE_CODE (last) != INTEGER_CST)
8316 last = c_fully_fold (last, false, NULL);
8317 if (TREE_CODE (last) == INTEGER_CST)
8318 pedwarn_init (loc, OPT_Wpedantic,
8319 "array index in initializer is not "
8320 "an integer constant expression");
8323 if (TREE_CODE (first) != INTEGER_CST)
8324 error_init (loc, "nonconstant array index in initializer");
8325 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8326 error_init (loc, "nonconstant array index in initializer");
8327 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8328 error_init (loc, "array index in non-array initializer");
8329 else if (tree_int_cst_sgn (first) == -1)
8330 error_init (loc, "array index in initializer exceeds array bounds");
8331 else if (constructor_max_index
8332 && tree_int_cst_lt (constructor_max_index, first))
8333 error_init (loc, "array index in initializer exceeds array bounds");
8334 else
8336 constant_expression_warning (first);
8337 if (last)
8338 constant_expression_warning (last);
8339 constructor_index = convert (bitsizetype, first);
8340 if (tree_int_cst_lt (constructor_index, first))
8342 constructor_index = copy_node (constructor_index);
8343 TREE_OVERFLOW (constructor_index) = 1;
8346 if (last)
8348 if (tree_int_cst_equal (first, last))
8349 last = NULL_TREE;
8350 else if (tree_int_cst_lt (last, first))
8352 error_init (loc, "empty index range in initializer");
8353 last = NULL_TREE;
8355 else
8357 last = convert (bitsizetype, last);
8358 if (constructor_max_index != NULL_TREE
8359 && tree_int_cst_lt (constructor_max_index, last))
8361 error_init (loc, "array index range in initializer exceeds "
8362 "array bounds");
8363 last = NULL_TREE;
8368 designator_depth++;
8369 designator_erroneous = 0;
8370 if (constructor_range_stack || last)
8371 push_range_stack (last, braced_init_obstack);
8375 /* Within a struct initializer, specify the next field to be initialized. */
8377 void
8378 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8379 struct obstack *braced_init_obstack)
8381 tree field;
8383 if (set_designator (loc, 0, braced_init_obstack))
8384 return;
8386 designator_erroneous = 1;
8388 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8390 error_init (loc, "field name not in record or union initializer");
8391 return;
8394 field = lookup_field (constructor_type, fieldname);
8396 if (field == NULL_TREE)
8398 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8399 if (guessed_id)
8401 gcc_rich_location rich_loc (fieldname_loc);
8402 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8403 error_at_rich_loc
8404 (&rich_loc,
8405 "%qT has no member named %qE; did you mean %qE?",
8406 constructor_type, fieldname, guessed_id);
8408 else
8409 error_at (fieldname_loc, "%qT has no member named %qE",
8410 constructor_type, fieldname);
8412 else
8415 constructor_fields = TREE_VALUE (field);
8416 designator_depth++;
8417 designator_erroneous = 0;
8418 if (constructor_range_stack)
8419 push_range_stack (NULL_TREE, braced_init_obstack);
8420 field = TREE_CHAIN (field);
8421 if (field)
8423 if (set_designator (loc, 0, braced_init_obstack))
8424 return;
8427 while (field != NULL_TREE);
8430 /* Add a new initializer to the tree of pending initializers. PURPOSE
8431 identifies the initializer, either array index or field in a structure.
8432 VALUE is the value of that index or field. If ORIGTYPE is not
8433 NULL_TREE, it is the original type of VALUE.
8435 IMPLICIT is true if value comes from pop_init_level (1),
8436 the new initializer has been merged with the existing one
8437 and thus no warnings should be emitted about overriding an
8438 existing initializer. */
8440 static void
8441 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8442 bool implicit, struct obstack *braced_init_obstack)
8444 struct init_node *p, **q, *r;
8446 q = &constructor_pending_elts;
8447 p = 0;
8449 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8451 while (*q != 0)
8453 p = *q;
8454 if (tree_int_cst_lt (purpose, p->purpose))
8455 q = &p->left;
8456 else if (tree_int_cst_lt (p->purpose, purpose))
8457 q = &p->right;
8458 else
8460 if (!implicit)
8462 if (TREE_SIDE_EFFECTS (p->value))
8463 warning_init (loc, OPT_Woverride_init_side_effects,
8464 "initialized field with side-effects "
8465 "overwritten");
8466 else if (warn_override_init)
8467 warning_init (loc, OPT_Woverride_init,
8468 "initialized field overwritten");
8470 p->value = value;
8471 p->origtype = origtype;
8472 return;
8476 else
8478 tree bitpos;
8480 bitpos = bit_position (purpose);
8481 while (*q != NULL)
8483 p = *q;
8484 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8485 q = &p->left;
8486 else if (p->purpose != purpose)
8487 q = &p->right;
8488 else
8490 if (!implicit)
8492 if (TREE_SIDE_EFFECTS (p->value))
8493 warning_init (loc, OPT_Woverride_init_side_effects,
8494 "initialized field with side-effects "
8495 "overwritten");
8496 else if (warn_override_init)
8497 warning_init (loc, OPT_Woverride_init,
8498 "initialized field overwritten");
8500 p->value = value;
8501 p->origtype = origtype;
8502 return;
8507 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8508 sizeof (struct init_node));
8509 r->purpose = purpose;
8510 r->value = value;
8511 r->origtype = origtype;
8513 *q = r;
8514 r->parent = p;
8515 r->left = 0;
8516 r->right = 0;
8517 r->balance = 0;
8519 while (p)
8521 struct init_node *s;
8523 if (r == p->left)
8525 if (p->balance == 0)
8526 p->balance = -1;
8527 else if (p->balance < 0)
8529 if (r->balance < 0)
8531 /* L rotation. */
8532 p->left = r->right;
8533 if (p->left)
8534 p->left->parent = p;
8535 r->right = p;
8537 p->balance = 0;
8538 r->balance = 0;
8540 s = p->parent;
8541 p->parent = r;
8542 r->parent = s;
8543 if (s)
8545 if (s->left == p)
8546 s->left = r;
8547 else
8548 s->right = r;
8550 else
8551 constructor_pending_elts = r;
8553 else
8555 /* LR rotation. */
8556 struct init_node *t = r->right;
8558 r->right = t->left;
8559 if (r->right)
8560 r->right->parent = r;
8561 t->left = r;
8563 p->left = t->right;
8564 if (p->left)
8565 p->left->parent = p;
8566 t->right = p;
8568 p->balance = t->balance < 0;
8569 r->balance = -(t->balance > 0);
8570 t->balance = 0;
8572 s = p->parent;
8573 p->parent = t;
8574 r->parent = t;
8575 t->parent = s;
8576 if (s)
8578 if (s->left == p)
8579 s->left = t;
8580 else
8581 s->right = t;
8583 else
8584 constructor_pending_elts = t;
8586 break;
8588 else
8590 /* p->balance == +1; growth of left side balances the node. */
8591 p->balance = 0;
8592 break;
8595 else /* r == p->right */
8597 if (p->balance == 0)
8598 /* Growth propagation from right side. */
8599 p->balance++;
8600 else if (p->balance > 0)
8602 if (r->balance > 0)
8604 /* R rotation. */
8605 p->right = r->left;
8606 if (p->right)
8607 p->right->parent = p;
8608 r->left = p;
8610 p->balance = 0;
8611 r->balance = 0;
8613 s = p->parent;
8614 p->parent = r;
8615 r->parent = s;
8616 if (s)
8618 if (s->left == p)
8619 s->left = r;
8620 else
8621 s->right = r;
8623 else
8624 constructor_pending_elts = r;
8626 else /* r->balance == -1 */
8628 /* RL rotation */
8629 struct init_node *t = r->left;
8631 r->left = t->right;
8632 if (r->left)
8633 r->left->parent = r;
8634 t->right = r;
8636 p->right = t->left;
8637 if (p->right)
8638 p->right->parent = p;
8639 t->left = p;
8641 r->balance = (t->balance < 0);
8642 p->balance = -(t->balance > 0);
8643 t->balance = 0;
8645 s = p->parent;
8646 p->parent = t;
8647 r->parent = t;
8648 t->parent = s;
8649 if (s)
8651 if (s->left == p)
8652 s->left = t;
8653 else
8654 s->right = t;
8656 else
8657 constructor_pending_elts = t;
8659 break;
8661 else
8663 /* p->balance == -1; growth of right side balances the node. */
8664 p->balance = 0;
8665 break;
8669 r = p;
8670 p = p->parent;
8674 /* Build AVL tree from a sorted chain. */
8676 static void
8677 set_nonincremental_init (struct obstack * braced_init_obstack)
8679 unsigned HOST_WIDE_INT ix;
8680 tree index, value;
8682 if (TREE_CODE (constructor_type) != RECORD_TYPE
8683 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8684 return;
8686 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8687 add_pending_init (input_location, index, value, NULL_TREE, true,
8688 braced_init_obstack);
8689 constructor_elements = NULL;
8690 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8692 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8693 /* Skip any nameless bit fields at the beginning. */
8694 while (constructor_unfilled_fields != NULL_TREE
8695 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8696 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
8697 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8700 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8702 if (TYPE_DOMAIN (constructor_type))
8703 constructor_unfilled_index
8704 = convert (bitsizetype,
8705 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8706 else
8707 constructor_unfilled_index = bitsize_zero_node;
8709 constructor_incremental = 0;
8712 /* Build AVL tree from a string constant. */
8714 static void
8715 set_nonincremental_init_from_string (tree str,
8716 struct obstack * braced_init_obstack)
8718 tree value, purpose, type;
8719 HOST_WIDE_INT val[2];
8720 const char *p, *end;
8721 int byte, wchar_bytes, charwidth, bitpos;
8723 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8725 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8726 charwidth = TYPE_PRECISION (char_type_node);
8727 gcc_assert ((size_t) wchar_bytes * charwidth
8728 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8729 type = TREE_TYPE (constructor_type);
8730 p = TREE_STRING_POINTER (str);
8731 end = p + TREE_STRING_LENGTH (str);
8733 for (purpose = bitsize_zero_node;
8734 p < end
8735 && !(constructor_max_index
8736 && tree_int_cst_lt (constructor_max_index, purpose));
8737 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8739 if (wchar_bytes == 1)
8741 val[0] = (unsigned char) *p++;
8742 val[1] = 0;
8744 else
8746 val[1] = 0;
8747 val[0] = 0;
8748 for (byte = 0; byte < wchar_bytes; byte++)
8750 if (BYTES_BIG_ENDIAN)
8751 bitpos = (wchar_bytes - byte - 1) * charwidth;
8752 else
8753 bitpos = byte * charwidth;
8754 val[bitpos / HOST_BITS_PER_WIDE_INT]
8755 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8756 << (bitpos % HOST_BITS_PER_WIDE_INT);
8760 if (!TYPE_UNSIGNED (type))
8762 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8763 if (bitpos < HOST_BITS_PER_WIDE_INT)
8765 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8767 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8768 val[1] = -1;
8771 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8773 if (val[0] < 0)
8774 val[1] = -1;
8776 else if (val[1] & (HOST_WIDE_INT_1
8777 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8778 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8781 value = wide_int_to_tree (type,
8782 wide_int::from_array (val, 2,
8783 HOST_BITS_PER_WIDE_INT * 2));
8784 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8785 braced_init_obstack);
8788 constructor_incremental = 0;
8791 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
8792 not initialized yet. */
8794 static tree
8795 find_init_member (tree field, struct obstack * braced_init_obstack)
8797 struct init_node *p;
8799 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8801 if (constructor_incremental
8802 && tree_int_cst_lt (field, constructor_unfilled_index))
8803 set_nonincremental_init (braced_init_obstack);
8805 p = constructor_pending_elts;
8806 while (p)
8808 if (tree_int_cst_lt (field, p->purpose))
8809 p = p->left;
8810 else if (tree_int_cst_lt (p->purpose, field))
8811 p = p->right;
8812 else
8813 return p->value;
8816 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8818 tree bitpos = bit_position (field);
8820 if (constructor_incremental
8821 && (!constructor_unfilled_fields
8822 || tree_int_cst_lt (bitpos,
8823 bit_position (constructor_unfilled_fields))))
8824 set_nonincremental_init (braced_init_obstack);
8826 p = constructor_pending_elts;
8827 while (p)
8829 if (field == p->purpose)
8830 return p->value;
8831 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8832 p = p->left;
8833 else
8834 p = p->right;
8837 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8839 if (!vec_safe_is_empty (constructor_elements)
8840 && (constructor_elements->last ().index == field))
8841 return constructor_elements->last ().value;
8843 return NULL_TREE;
8846 /* "Output" the next constructor element.
8847 At top level, really output it to assembler code now.
8848 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8849 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8850 TYPE is the data type that the containing data type wants here.
8851 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8852 If VALUE is a string constant, STRICT_STRING is true if it is
8853 unparenthesized or we should not warn here for it being parenthesized.
8854 For other types of VALUE, STRICT_STRING is not used.
8856 PENDING if non-nil means output pending elements that belong
8857 right after this element. (PENDING is normally 1;
8858 it is 0 while outputting pending elements, to avoid recursion.)
8860 IMPLICIT is true if value comes from pop_init_level (1),
8861 the new initializer has been merged with the existing one
8862 and thus no warnings should be emitted about overriding an
8863 existing initializer. */
8865 static void
8866 output_init_element (location_t loc, tree value, tree origtype,
8867 bool strict_string, tree type, tree field, int pending,
8868 bool implicit, struct obstack * braced_init_obstack)
8870 tree semantic_type = NULL_TREE;
8871 bool maybe_const = true;
8872 bool npc;
8874 if (type == error_mark_node || value == error_mark_node)
8876 constructor_erroneous = 1;
8877 return;
8879 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8880 && (TREE_CODE (value) == STRING_CST
8881 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8882 && !(TREE_CODE (value) == STRING_CST
8883 && TREE_CODE (type) == ARRAY_TYPE
8884 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8885 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8886 TYPE_MAIN_VARIANT (type)))
8887 value = array_to_pointer_conversion (input_location, value);
8889 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8890 && require_constant_value && pending)
8892 /* As an extension, allow initializing objects with static storage
8893 duration with compound literals (which are then treated just as
8894 the brace enclosed list they contain). */
8895 if (flag_isoc99)
8896 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8897 "constant");
8898 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8899 value = DECL_INITIAL (decl);
8902 npc = null_pointer_constant_p (value);
8903 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8905 semantic_type = TREE_TYPE (value);
8906 value = TREE_OPERAND (value, 0);
8908 value = c_fully_fold (value, require_constant_value, &maybe_const);
8910 if (value == error_mark_node)
8911 constructor_erroneous = 1;
8912 else if (!TREE_CONSTANT (value))
8913 constructor_constant = 0;
8914 else if (!initializer_constant_valid_p (value,
8915 TREE_TYPE (value),
8916 AGGREGATE_TYPE_P (constructor_type)
8917 && TYPE_REVERSE_STORAGE_ORDER
8918 (constructor_type))
8919 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8920 && DECL_C_BIT_FIELD (field)
8921 && TREE_CODE (value) != INTEGER_CST))
8922 constructor_simple = 0;
8923 if (!maybe_const)
8924 constructor_nonconst = 1;
8926 /* Digest the initializer and issue any errors about incompatible
8927 types before issuing errors about non-constant initializers. */
8928 tree new_value = value;
8929 if (semantic_type)
8930 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8931 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8932 require_constant_value);
8933 if (new_value == error_mark_node)
8935 constructor_erroneous = 1;
8936 return;
8938 if (require_constant_value || require_constant_elements)
8939 constant_expression_warning (new_value);
8941 /* Proceed to check the constness of the original initializer. */
8942 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8944 if (require_constant_value)
8946 error_init (loc, "initializer element is not constant");
8947 value = error_mark_node;
8949 else if (require_constant_elements)
8950 pedwarn (loc, OPT_Wpedantic,
8951 "initializer element is not computable at load time");
8953 else if (!maybe_const
8954 && (require_constant_value || require_constant_elements))
8955 pedwarn_init (loc, OPT_Wpedantic,
8956 "initializer element is not a constant expression");
8958 /* Issue -Wc++-compat warnings about initializing a bitfield with
8959 enum type. */
8960 if (warn_cxx_compat
8961 && field != NULL_TREE
8962 && TREE_CODE (field) == FIELD_DECL
8963 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8964 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8965 != TYPE_MAIN_VARIANT (type))
8966 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8968 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8969 if (checktype != error_mark_node
8970 && (TYPE_MAIN_VARIANT (checktype)
8971 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8972 warning_init (loc, OPT_Wc___compat,
8973 "enum conversion in initialization is invalid in C++");
8976 /* If this field is empty (and not at the end of structure),
8977 don't do anything other than checking the initializer. */
8978 if (field
8979 && (TREE_TYPE (field) == error_mark_node
8980 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8981 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8982 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8983 || DECL_CHAIN (field)))))
8984 return;
8986 /* Finally, set VALUE to the initializer value digested above. */
8987 value = new_value;
8989 /* If this element doesn't come next in sequence,
8990 put it on constructor_pending_elts. */
8991 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8992 && (!constructor_incremental
8993 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8995 if (constructor_incremental
8996 && tree_int_cst_lt (field, constructor_unfilled_index))
8997 set_nonincremental_init (braced_init_obstack);
8999 add_pending_init (loc, field, value, origtype, implicit,
9000 braced_init_obstack);
9001 return;
9003 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9004 && (!constructor_incremental
9005 || field != constructor_unfilled_fields))
9007 /* We do this for records but not for unions. In a union,
9008 no matter which field is specified, it can be initialized
9009 right away since it starts at the beginning of the union. */
9010 if (constructor_incremental)
9012 if (!constructor_unfilled_fields)
9013 set_nonincremental_init (braced_init_obstack);
9014 else
9016 tree bitpos, unfillpos;
9018 bitpos = bit_position (field);
9019 unfillpos = bit_position (constructor_unfilled_fields);
9021 if (tree_int_cst_lt (bitpos, unfillpos))
9022 set_nonincremental_init (braced_init_obstack);
9026 add_pending_init (loc, field, value, origtype, implicit,
9027 braced_init_obstack);
9028 return;
9030 else if (TREE_CODE (constructor_type) == UNION_TYPE
9031 && !vec_safe_is_empty (constructor_elements))
9033 if (!implicit)
9035 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9036 warning_init (loc, OPT_Woverride_init_side_effects,
9037 "initialized field with side-effects overwritten");
9038 else if (warn_override_init)
9039 warning_init (loc, OPT_Woverride_init,
9040 "initialized field overwritten");
9043 /* We can have just one union field set. */
9044 constructor_elements = NULL;
9047 /* Otherwise, output this element either to
9048 constructor_elements or to the assembler file. */
9050 constructor_elt celt = {field, value};
9051 vec_safe_push (constructor_elements, celt);
9053 /* Advance the variable that indicates sequential elements output. */
9054 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9055 constructor_unfilled_index
9056 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9057 bitsize_one_node);
9058 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9060 constructor_unfilled_fields
9061 = DECL_CHAIN (constructor_unfilled_fields);
9063 /* Skip any nameless bit fields. */
9064 while (constructor_unfilled_fields != NULL_TREE
9065 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9066 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
9067 constructor_unfilled_fields =
9068 DECL_CHAIN (constructor_unfilled_fields);
9070 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9071 constructor_unfilled_fields = NULL_TREE;
9073 /* Now output any pending elements which have become next. */
9074 if (pending)
9075 output_pending_init_elements (0, braced_init_obstack);
9078 /* Output any pending elements which have become next.
9079 As we output elements, constructor_unfilled_{fields,index}
9080 advances, which may cause other elements to become next;
9081 if so, they too are output.
9083 If ALL is 0, we return when there are
9084 no more pending elements to output now.
9086 If ALL is 1, we output space as necessary so that
9087 we can output all the pending elements. */
9088 static void
9089 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9091 struct init_node *elt = constructor_pending_elts;
9092 tree next;
9094 retry:
9096 /* Look through the whole pending tree.
9097 If we find an element that should be output now,
9098 output it. Otherwise, set NEXT to the element
9099 that comes first among those still pending. */
9101 next = NULL_TREE;
9102 while (elt)
9104 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9106 if (tree_int_cst_equal (elt->purpose,
9107 constructor_unfilled_index))
9108 output_init_element (input_location, elt->value, elt->origtype,
9109 true, TREE_TYPE (constructor_type),
9110 constructor_unfilled_index, 0, false,
9111 braced_init_obstack);
9112 else if (tree_int_cst_lt (constructor_unfilled_index,
9113 elt->purpose))
9115 /* Advance to the next smaller node. */
9116 if (elt->left)
9117 elt = elt->left;
9118 else
9120 /* We have reached the smallest node bigger than the
9121 current unfilled index. Fill the space first. */
9122 next = elt->purpose;
9123 break;
9126 else
9128 /* Advance to the next bigger node. */
9129 if (elt->right)
9130 elt = elt->right;
9131 else
9133 /* We have reached the biggest node in a subtree. Find
9134 the parent of it, which is the next bigger node. */
9135 while (elt->parent && elt->parent->right == elt)
9136 elt = elt->parent;
9137 elt = elt->parent;
9138 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9139 elt->purpose))
9141 next = elt->purpose;
9142 break;
9147 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9149 tree ctor_unfilled_bitpos, elt_bitpos;
9151 /* If the current record is complete we are done. */
9152 if (constructor_unfilled_fields == NULL_TREE)
9153 break;
9155 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9156 elt_bitpos = bit_position (elt->purpose);
9157 /* We can't compare fields here because there might be empty
9158 fields in between. */
9159 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9161 constructor_unfilled_fields = elt->purpose;
9162 output_init_element (input_location, elt->value, elt->origtype,
9163 true, TREE_TYPE (elt->purpose),
9164 elt->purpose, 0, false,
9165 braced_init_obstack);
9167 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9169 /* Advance to the next smaller node. */
9170 if (elt->left)
9171 elt = elt->left;
9172 else
9174 /* We have reached the smallest node bigger than the
9175 current unfilled field. Fill the space first. */
9176 next = elt->purpose;
9177 break;
9180 else
9182 /* Advance to the next bigger node. */
9183 if (elt->right)
9184 elt = elt->right;
9185 else
9187 /* We have reached the biggest node in a subtree. Find
9188 the parent of it, which is the next bigger node. */
9189 while (elt->parent && elt->parent->right == elt)
9190 elt = elt->parent;
9191 elt = elt->parent;
9192 if (elt
9193 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9194 bit_position (elt->purpose))))
9196 next = elt->purpose;
9197 break;
9204 /* Ordinarily return, but not if we want to output all
9205 and there are elements left. */
9206 if (!(all && next != NULL_TREE))
9207 return;
9209 /* If it's not incremental, just skip over the gap, so that after
9210 jumping to retry we will output the next successive element. */
9211 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9212 constructor_unfilled_fields = next;
9213 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9214 constructor_unfilled_index = next;
9216 /* ELT now points to the node in the pending tree with the next
9217 initializer to output. */
9218 goto retry;
9221 /* Add one non-braced element to the current constructor level.
9222 This adjusts the current position within the constructor's type.
9223 This may also start or terminate implicit levels
9224 to handle a partly-braced initializer.
9226 Once this has found the correct level for the new element,
9227 it calls output_init_element.
9229 IMPLICIT is true if value comes from pop_init_level (1),
9230 the new initializer has been merged with the existing one
9231 and thus no warnings should be emitted about overriding an
9232 existing initializer. */
9234 void
9235 process_init_element (location_t loc, struct c_expr value, bool implicit,
9236 struct obstack * braced_init_obstack)
9238 tree orig_value = value.value;
9239 int string_flag
9240 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9241 bool strict_string = value.original_code == STRING_CST;
9242 bool was_designated = designator_depth != 0;
9244 designator_depth = 0;
9245 designator_erroneous = 0;
9247 if (!implicit && value.value && !integer_zerop (value.value))
9248 constructor_zeroinit = 0;
9250 /* Handle superfluous braces around string cst as in
9251 char x[] = {"foo"}; */
9252 if (string_flag
9253 && constructor_type
9254 && !was_designated
9255 && TREE_CODE (constructor_type) == ARRAY_TYPE
9256 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9257 && integer_zerop (constructor_unfilled_index))
9259 if (constructor_stack->replacement_value.value)
9260 error_init (loc, "excess elements in char array initializer");
9261 constructor_stack->replacement_value = value;
9262 return;
9265 if (constructor_stack->replacement_value.value != NULL_TREE)
9267 error_init (loc, "excess elements in struct initializer");
9268 return;
9271 /* Ignore elements of a brace group if it is entirely superfluous
9272 and has already been diagnosed. */
9273 if (constructor_type == NULL_TREE)
9274 return;
9276 if (!implicit && warn_designated_init && !was_designated
9277 && TREE_CODE (constructor_type) == RECORD_TYPE
9278 && lookup_attribute ("designated_init",
9279 TYPE_ATTRIBUTES (constructor_type)))
9280 warning_init (loc,
9281 OPT_Wdesignated_init,
9282 "positional initialization of field "
9283 "in %<struct%> declared with %<designated_init%> attribute");
9285 /* If we've exhausted any levels that didn't have braces,
9286 pop them now. */
9287 while (constructor_stack->implicit)
9289 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9290 && constructor_fields == NULL_TREE)
9291 process_init_element (loc,
9292 pop_init_level (loc, 1, braced_init_obstack,
9293 last_init_list_comma),
9294 true, braced_init_obstack);
9295 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9296 || VECTOR_TYPE_P (constructor_type))
9297 && constructor_max_index
9298 && tree_int_cst_lt (constructor_max_index,
9299 constructor_index))
9300 process_init_element (loc,
9301 pop_init_level (loc, 1, braced_init_obstack,
9302 last_init_list_comma),
9303 true, braced_init_obstack);
9304 else
9305 break;
9308 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9309 if (constructor_range_stack)
9311 /* If value is a compound literal and we'll be just using its
9312 content, don't put it into a SAVE_EXPR. */
9313 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9314 || !require_constant_value)
9316 tree semantic_type = NULL_TREE;
9317 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9319 semantic_type = TREE_TYPE (value.value);
9320 value.value = TREE_OPERAND (value.value, 0);
9322 value.value = save_expr (value.value);
9323 if (semantic_type)
9324 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9325 value.value);
9329 while (1)
9331 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9333 tree fieldtype;
9334 enum tree_code fieldcode;
9336 if (constructor_fields == NULL_TREE)
9338 pedwarn_init (loc, 0, "excess elements in struct initializer");
9339 break;
9342 fieldtype = TREE_TYPE (constructor_fields);
9343 if (fieldtype != error_mark_node)
9344 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9345 fieldcode = TREE_CODE (fieldtype);
9347 /* Error for non-static initialization of a flexible array member. */
9348 if (fieldcode == ARRAY_TYPE
9349 && !require_constant_value
9350 && TYPE_SIZE (fieldtype) == NULL_TREE
9351 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9353 error_init (loc, "non-static initialization of a flexible "
9354 "array member");
9355 break;
9358 /* Error for initialization of a flexible array member with
9359 a string constant if the structure is in an array. E.g.:
9360 struct S { int x; char y[]; };
9361 struct S s[] = { { 1, "foo" } };
9362 is invalid. */
9363 if (string_flag
9364 && fieldcode == ARRAY_TYPE
9365 && constructor_depth > 1
9366 && TYPE_SIZE (fieldtype) == NULL_TREE
9367 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9369 bool in_array_p = false;
9370 for (struct constructor_stack *p = constructor_stack;
9371 p && p->type; p = p->next)
9372 if (TREE_CODE (p->type) == ARRAY_TYPE)
9374 in_array_p = true;
9375 break;
9377 if (in_array_p)
9379 error_init (loc, "initialization of flexible array "
9380 "member in a nested context");
9381 break;
9385 /* Accept a string constant to initialize a subarray. */
9386 if (value.value != NULL_TREE
9387 && fieldcode == ARRAY_TYPE
9388 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9389 && string_flag)
9390 value.value = orig_value;
9391 /* Otherwise, if we have come to a subaggregate,
9392 and we don't have an element of its type, push into it. */
9393 else if (value.value != NULL_TREE
9394 && value.value != error_mark_node
9395 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9396 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9397 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9399 push_init_level (loc, 1, braced_init_obstack);
9400 continue;
9403 if (value.value)
9405 push_member_name (constructor_fields);
9406 output_init_element (loc, value.value, value.original_type,
9407 strict_string, fieldtype,
9408 constructor_fields, 1, implicit,
9409 braced_init_obstack);
9410 RESTORE_SPELLING_DEPTH (constructor_depth);
9412 else
9413 /* Do the bookkeeping for an element that was
9414 directly output as a constructor. */
9416 /* For a record, keep track of end position of last field. */
9417 if (DECL_SIZE (constructor_fields))
9418 constructor_bit_index
9419 = size_binop_loc (input_location, PLUS_EXPR,
9420 bit_position (constructor_fields),
9421 DECL_SIZE (constructor_fields));
9423 /* If the current field was the first one not yet written out,
9424 it isn't now, so update. */
9425 if (constructor_unfilled_fields == constructor_fields)
9427 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9428 /* Skip any nameless bit fields. */
9429 while (constructor_unfilled_fields != 0
9430 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9431 && DECL_NAME (constructor_unfilled_fields) == 0)
9432 constructor_unfilled_fields =
9433 DECL_CHAIN (constructor_unfilled_fields);
9437 constructor_fields = DECL_CHAIN (constructor_fields);
9438 /* Skip any nameless bit fields at the beginning. */
9439 while (constructor_fields != NULL_TREE
9440 && DECL_C_BIT_FIELD (constructor_fields)
9441 && DECL_NAME (constructor_fields) == NULL_TREE)
9442 constructor_fields = DECL_CHAIN (constructor_fields);
9444 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9446 tree fieldtype;
9447 enum tree_code fieldcode;
9449 if (constructor_fields == NULL_TREE)
9451 pedwarn_init (loc, 0,
9452 "excess elements in union initializer");
9453 break;
9456 fieldtype = TREE_TYPE (constructor_fields);
9457 if (fieldtype != error_mark_node)
9458 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9459 fieldcode = TREE_CODE (fieldtype);
9461 /* Warn that traditional C rejects initialization of unions.
9462 We skip the warning if the value is zero. This is done
9463 under the assumption that the zero initializer in user
9464 code appears conditioned on e.g. __STDC__ to avoid
9465 "missing initializer" warnings and relies on default
9466 initialization to zero in the traditional C case.
9467 We also skip the warning if the initializer is designated,
9468 again on the assumption that this must be conditional on
9469 __STDC__ anyway (and we've already complained about the
9470 member-designator already). */
9471 if (!in_system_header_at (input_location) && !constructor_designated
9472 && !(value.value && (integer_zerop (value.value)
9473 || real_zerop (value.value))))
9474 warning (OPT_Wtraditional, "traditional C rejects initialization "
9475 "of unions");
9477 /* Accept a string constant to initialize a subarray. */
9478 if (value.value != NULL_TREE
9479 && fieldcode == ARRAY_TYPE
9480 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9481 && string_flag)
9482 value.value = orig_value;
9483 /* Otherwise, if we have come to a subaggregate,
9484 and we don't have an element of its type, push into it. */
9485 else if (value.value != NULL_TREE
9486 && value.value != error_mark_node
9487 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9488 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9489 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9491 push_init_level (loc, 1, braced_init_obstack);
9492 continue;
9495 if (value.value)
9497 push_member_name (constructor_fields);
9498 output_init_element (loc, value.value, value.original_type,
9499 strict_string, fieldtype,
9500 constructor_fields, 1, implicit,
9501 braced_init_obstack);
9502 RESTORE_SPELLING_DEPTH (constructor_depth);
9504 else
9505 /* Do the bookkeeping for an element that was
9506 directly output as a constructor. */
9508 constructor_bit_index = DECL_SIZE (constructor_fields);
9509 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9512 constructor_fields = NULL_TREE;
9514 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9516 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9517 enum tree_code eltcode = TREE_CODE (elttype);
9519 /* Accept a string constant to initialize a subarray. */
9520 if (value.value != NULL_TREE
9521 && eltcode == ARRAY_TYPE
9522 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9523 && string_flag)
9524 value.value = orig_value;
9525 /* Otherwise, if we have come to a subaggregate,
9526 and we don't have an element of its type, push into it. */
9527 else if (value.value != NULL_TREE
9528 && value.value != error_mark_node
9529 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9530 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9531 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9533 push_init_level (loc, 1, braced_init_obstack);
9534 continue;
9537 if (constructor_max_index != NULL_TREE
9538 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9539 || integer_all_onesp (constructor_max_index)))
9541 pedwarn_init (loc, 0,
9542 "excess elements in array initializer");
9543 break;
9546 /* Now output the actual element. */
9547 if (value.value)
9549 push_array_bounds (tree_to_uhwi (constructor_index));
9550 output_init_element (loc, value.value, value.original_type,
9551 strict_string, elttype,
9552 constructor_index, 1, implicit,
9553 braced_init_obstack);
9554 RESTORE_SPELLING_DEPTH (constructor_depth);
9557 constructor_index
9558 = size_binop_loc (input_location, PLUS_EXPR,
9559 constructor_index, bitsize_one_node);
9561 if (!value.value)
9562 /* If we are doing the bookkeeping for an element that was
9563 directly output as a constructor, we must update
9564 constructor_unfilled_index. */
9565 constructor_unfilled_index = constructor_index;
9567 else if (VECTOR_TYPE_P (constructor_type))
9569 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9571 /* Do a basic check of initializer size. Note that vectors
9572 always have a fixed size derived from their type. */
9573 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9575 pedwarn_init (loc, 0,
9576 "excess elements in vector initializer");
9577 break;
9580 /* Now output the actual element. */
9581 if (value.value)
9583 if (TREE_CODE (value.value) == VECTOR_CST)
9584 elttype = TYPE_MAIN_VARIANT (constructor_type);
9585 output_init_element (loc, value.value, value.original_type,
9586 strict_string, elttype,
9587 constructor_index, 1, implicit,
9588 braced_init_obstack);
9591 constructor_index
9592 = size_binop_loc (input_location,
9593 PLUS_EXPR, constructor_index, bitsize_one_node);
9595 if (!value.value)
9596 /* If we are doing the bookkeeping for an element that was
9597 directly output as a constructor, we must update
9598 constructor_unfilled_index. */
9599 constructor_unfilled_index = constructor_index;
9602 /* Handle the sole element allowed in a braced initializer
9603 for a scalar variable. */
9604 else if (constructor_type != error_mark_node
9605 && constructor_fields == NULL_TREE)
9607 pedwarn_init (loc, 0,
9608 "excess elements in scalar initializer");
9609 break;
9611 else
9613 if (value.value)
9614 output_init_element (loc, value.value, value.original_type,
9615 strict_string, constructor_type,
9616 NULL_TREE, 1, implicit,
9617 braced_init_obstack);
9618 constructor_fields = NULL_TREE;
9621 /* Handle range initializers either at this level or anywhere higher
9622 in the designator stack. */
9623 if (constructor_range_stack)
9625 struct constructor_range_stack *p, *range_stack;
9626 int finish = 0;
9628 range_stack = constructor_range_stack;
9629 constructor_range_stack = 0;
9630 while (constructor_stack != range_stack->stack)
9632 gcc_assert (constructor_stack->implicit);
9633 process_init_element (loc,
9634 pop_init_level (loc, 1,
9635 braced_init_obstack,
9636 last_init_list_comma),
9637 true, braced_init_obstack);
9639 for (p = range_stack;
9640 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9641 p = p->prev)
9643 gcc_assert (constructor_stack->implicit);
9644 process_init_element (loc,
9645 pop_init_level (loc, 1,
9646 braced_init_obstack,
9647 last_init_list_comma),
9648 true, braced_init_obstack);
9651 p->index = size_binop_loc (input_location,
9652 PLUS_EXPR, p->index, bitsize_one_node);
9653 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9654 finish = 1;
9656 while (1)
9658 constructor_index = p->index;
9659 constructor_fields = p->fields;
9660 if (finish && p->range_end && p->index == p->range_start)
9662 finish = 0;
9663 p->prev = 0;
9665 p = p->next;
9666 if (!p)
9667 break;
9668 finish_implicit_inits (loc, braced_init_obstack);
9669 push_init_level (loc, 2, braced_init_obstack);
9670 p->stack = constructor_stack;
9671 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9672 p->index = p->range_start;
9675 if (!finish)
9676 constructor_range_stack = range_stack;
9677 continue;
9680 break;
9683 constructor_range_stack = 0;
9686 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9687 (guaranteed to be 'volatile' or null) and ARGS (represented using
9688 an ASM_EXPR node). */
9689 tree
9690 build_asm_stmt (tree cv_qualifier, tree args)
9692 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9693 ASM_VOLATILE_P (args) = 1;
9694 return add_stmt (args);
9697 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9698 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9699 SIMPLE indicates whether there was anything at all after the
9700 string in the asm expression -- asm("blah") and asm("blah" : )
9701 are subtly different. We use a ASM_EXPR node to represent this. */
9702 tree
9703 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9704 tree clobbers, tree labels, bool simple)
9706 tree tail;
9707 tree args;
9708 int i;
9709 const char *constraint;
9710 const char **oconstraints;
9711 bool allows_mem, allows_reg, is_inout;
9712 int ninputs, noutputs;
9714 ninputs = list_length (inputs);
9715 noutputs = list_length (outputs);
9716 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9718 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9720 /* Remove output conversions that change the type but not the mode. */
9721 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9723 tree output = TREE_VALUE (tail);
9725 output = c_fully_fold (output, false, NULL);
9727 /* ??? Really, this should not be here. Users should be using a
9728 proper lvalue, dammit. But there's a long history of using casts
9729 in the output operands. In cases like longlong.h, this becomes a
9730 primitive form of typechecking -- if the cast can be removed, then
9731 the output operand had a type of the proper width; otherwise we'll
9732 get an error. Gross, but ... */
9733 STRIP_NOPS (output);
9735 if (!lvalue_or_else (loc, output, lv_asm))
9736 output = error_mark_node;
9738 if (output != error_mark_node
9739 && (TREE_READONLY (output)
9740 || TYPE_READONLY (TREE_TYPE (output))
9741 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9742 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9743 readonly_error (loc, output, lv_asm);
9745 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9746 oconstraints[i] = constraint;
9748 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9749 &allows_mem, &allows_reg, &is_inout))
9751 /* If the operand is going to end up in memory,
9752 mark it addressable. */
9753 if (!allows_reg && !c_mark_addressable (output))
9754 output = error_mark_node;
9755 if (!(!allows_reg && allows_mem)
9756 && output != error_mark_node
9757 && VOID_TYPE_P (TREE_TYPE (output)))
9759 error_at (loc, "invalid use of void expression");
9760 output = error_mark_node;
9763 else
9764 output = error_mark_node;
9766 TREE_VALUE (tail) = output;
9769 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9771 tree input;
9773 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9774 input = TREE_VALUE (tail);
9776 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9777 oconstraints, &allows_mem, &allows_reg))
9779 /* If the operand is going to end up in memory,
9780 mark it addressable. */
9781 if (!allows_reg && allows_mem)
9783 input = c_fully_fold (input, false, NULL);
9785 /* Strip the nops as we allow this case. FIXME, this really
9786 should be rejected or made deprecated. */
9787 STRIP_NOPS (input);
9788 if (!c_mark_addressable (input))
9789 input = error_mark_node;
9791 else
9793 struct c_expr expr;
9794 memset (&expr, 0, sizeof (expr));
9795 expr.value = input;
9796 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9797 input = c_fully_fold (expr.value, false, NULL);
9799 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9801 error_at (loc, "invalid use of void expression");
9802 input = error_mark_node;
9806 else
9807 input = error_mark_node;
9809 TREE_VALUE (tail) = input;
9812 /* ASMs with labels cannot have outputs. This should have been
9813 enforced by the parser. */
9814 gcc_assert (outputs == NULL || labels == NULL);
9816 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9818 /* asm statements without outputs, including simple ones, are treated
9819 as volatile. */
9820 ASM_INPUT_P (args) = simple;
9821 ASM_VOLATILE_P (args) = (noutputs == 0);
9823 return args;
9826 /* Generate a goto statement to LABEL. LOC is the location of the
9827 GOTO. */
9829 tree
9830 c_finish_goto_label (location_t loc, tree label)
9832 tree decl = lookup_label_for_goto (loc, label);
9833 if (!decl)
9834 return NULL_TREE;
9835 TREE_USED (decl) = 1;
9837 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
9838 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9839 SET_EXPR_LOCATION (t, loc);
9840 return add_stmt (t);
9844 /* Generate a computed goto statement to EXPR. LOC is the location of
9845 the GOTO. */
9847 tree
9848 c_finish_goto_ptr (location_t loc, tree expr)
9850 tree t;
9851 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9852 expr = c_fully_fold (expr, false, NULL);
9853 expr = convert (ptr_type_node, expr);
9854 t = build1 (GOTO_EXPR, void_type_node, expr);
9855 SET_EXPR_LOCATION (t, loc);
9856 return add_stmt (t);
9859 /* Generate a C `return' statement. RETVAL is the expression for what
9860 to return, or a null pointer for `return;' with no value. LOC is
9861 the location of the return statement, or the location of the expression,
9862 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9863 is the original type of RETVAL. */
9865 tree
9866 c_finish_return (location_t loc, tree retval, tree origtype)
9868 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9869 bool no_warning = false;
9870 bool npc = false;
9871 size_t rank = 0;
9873 /* Use the expansion point to handle cases such as returning NULL
9874 in a function returning void. */
9875 source_location xloc = expansion_point_location_if_in_system_header (loc);
9877 if (TREE_THIS_VOLATILE (current_function_decl))
9878 warning_at (xloc, 0,
9879 "function declared %<noreturn%> has a %<return%> statement");
9881 if (flag_cilkplus && contains_array_notation_expr (retval))
9883 /* Array notations are allowed in a return statement if it is inside a
9884 built-in array notation reduction function. */
9885 if (!find_rank (loc, retval, retval, false, &rank))
9886 return error_mark_node;
9887 if (rank >= 1)
9889 error_at (loc, "array notation expression cannot be used as a "
9890 "return value");
9891 return error_mark_node;
9894 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9896 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9897 "allowed");
9898 return error_mark_node;
9900 if (retval)
9902 tree semantic_type = NULL_TREE;
9903 npc = null_pointer_constant_p (retval);
9904 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9906 semantic_type = TREE_TYPE (retval);
9907 retval = TREE_OPERAND (retval, 0);
9909 retval = c_fully_fold (retval, false, NULL);
9910 if (semantic_type)
9911 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9914 if (!retval)
9916 current_function_returns_null = 1;
9917 if ((warn_return_type || flag_isoc99)
9918 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
9920 bool warned_here;
9921 if (flag_isoc99)
9922 warned_here = pedwarn
9923 (loc, 0,
9924 "%<return%> with no value, in function returning non-void");
9925 else
9926 warned_here = warning_at
9927 (loc, OPT_Wreturn_type,
9928 "%<return%> with no value, in function returning non-void");
9929 no_warning = true;
9930 if (warned_here)
9931 inform (DECL_SOURCE_LOCATION (current_function_decl),
9932 "declared here");
9935 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
9937 current_function_returns_null = 1;
9938 bool warned_here;
9939 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9940 warned_here = pedwarn
9941 (xloc, 0,
9942 "%<return%> with a value, in function returning void");
9943 else
9944 warned_here = pedwarn
9945 (xloc, OPT_Wpedantic, "ISO C forbids "
9946 "%<return%> with expression, in function returning void");
9947 if (warned_here)
9948 inform (DECL_SOURCE_LOCATION (current_function_decl),
9949 "declared here");
9951 else
9953 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9954 retval, origtype, ic_return,
9955 npc, NULL_TREE, NULL_TREE, 0);
9956 tree res = DECL_RESULT (current_function_decl);
9957 tree inner;
9958 bool save;
9960 current_function_returns_value = 1;
9961 if (t == error_mark_node)
9962 return NULL_TREE;
9964 save = in_late_binary_op;
9965 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9966 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9967 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9968 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9969 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9970 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
9971 in_late_binary_op = true;
9972 inner = t = convert (TREE_TYPE (res), t);
9973 in_late_binary_op = save;
9975 /* Strip any conversions, additions, and subtractions, and see if
9976 we are returning the address of a local variable. Warn if so. */
9977 while (1)
9979 switch (TREE_CODE (inner))
9981 CASE_CONVERT:
9982 case NON_LVALUE_EXPR:
9983 case PLUS_EXPR:
9984 case POINTER_PLUS_EXPR:
9985 inner = TREE_OPERAND (inner, 0);
9986 continue;
9988 case MINUS_EXPR:
9989 /* If the second operand of the MINUS_EXPR has a pointer
9990 type (or is converted from it), this may be valid, so
9991 don't give a warning. */
9993 tree op1 = TREE_OPERAND (inner, 1);
9995 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9996 && (CONVERT_EXPR_P (op1)
9997 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9998 op1 = TREE_OPERAND (op1, 0);
10000 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10001 break;
10003 inner = TREE_OPERAND (inner, 0);
10004 continue;
10007 case ADDR_EXPR:
10008 inner = TREE_OPERAND (inner, 0);
10010 while (REFERENCE_CLASS_P (inner)
10011 && !INDIRECT_REF_P (inner))
10012 inner = TREE_OPERAND (inner, 0);
10014 if (DECL_P (inner)
10015 && !DECL_EXTERNAL (inner)
10016 && !TREE_STATIC (inner)
10017 && DECL_CONTEXT (inner) == current_function_decl)
10019 if (TREE_CODE (inner) == LABEL_DECL)
10020 warning_at (loc, OPT_Wreturn_local_addr,
10021 "function returns address of label");
10022 else
10024 warning_at (loc, OPT_Wreturn_local_addr,
10025 "function returns address of local variable");
10026 tree zero = build_zero_cst (TREE_TYPE (res));
10027 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10030 break;
10032 default:
10033 break;
10036 break;
10039 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10040 SET_EXPR_LOCATION (retval, loc);
10042 if (warn_sequence_point)
10043 verify_sequence_points (retval);
10046 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10047 TREE_NO_WARNING (ret_stmt) |= no_warning;
10048 return add_stmt (ret_stmt);
10051 struct c_switch {
10052 /* The SWITCH_EXPR being built. */
10053 tree switch_expr;
10055 /* The original type of the testing expression, i.e. before the
10056 default conversion is applied. */
10057 tree orig_type;
10059 /* A splay-tree mapping the low element of a case range to the high
10060 element, or NULL_TREE if there is no high element. Used to
10061 determine whether or not a new case label duplicates an old case
10062 label. We need a tree, rather than simply a hash table, because
10063 of the GNU case range extension. */
10064 splay_tree cases;
10066 /* The bindings at the point of the switch. This is used for
10067 warnings crossing decls when branching to a case label. */
10068 struct c_spot_bindings *bindings;
10070 /* The next node on the stack. */
10071 struct c_switch *next;
10073 /* Remember whether the controlling expression had boolean type
10074 before integer promotions for the sake of -Wswitch-bool. */
10075 bool bool_cond_p;
10077 /* Remember whether there was a case value that is outside the
10078 range of the ORIG_TYPE. */
10079 bool outside_range_p;
10082 /* A stack of the currently active switch statements. The innermost
10083 switch statement is on the top of the stack. There is no need to
10084 mark the stack for garbage collection because it is only active
10085 during the processing of the body of a function, and we never
10086 collect at that point. */
10088 struct c_switch *c_switch_stack;
10090 /* Start a C switch statement, testing expression EXP. Return the new
10091 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10092 SWITCH_COND_LOC is the location of the switch's condition.
10093 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10095 tree
10096 c_start_case (location_t switch_loc,
10097 location_t switch_cond_loc,
10098 tree exp, bool explicit_cast_p)
10100 tree orig_type = error_mark_node;
10101 bool bool_cond_p = false;
10102 struct c_switch *cs;
10104 if (exp != error_mark_node)
10106 orig_type = TREE_TYPE (exp);
10108 if (!INTEGRAL_TYPE_P (orig_type))
10110 if (orig_type != error_mark_node)
10112 error_at (switch_cond_loc, "switch quantity not an integer");
10113 orig_type = error_mark_node;
10115 exp = integer_zero_node;
10117 else
10119 tree type = TYPE_MAIN_VARIANT (orig_type);
10120 tree e = exp;
10122 /* Warn if the condition has boolean value. */
10123 while (TREE_CODE (e) == COMPOUND_EXPR)
10124 e = TREE_OPERAND (e, 1);
10126 if ((TREE_CODE (type) == BOOLEAN_TYPE
10127 || truth_value_p (TREE_CODE (e)))
10128 /* Explicit cast to int suppresses this warning. */
10129 && !(TREE_CODE (type) == INTEGER_TYPE
10130 && explicit_cast_p))
10131 bool_cond_p = true;
10133 if (!in_system_header_at (input_location)
10134 && (type == long_integer_type_node
10135 || type == long_unsigned_type_node))
10136 warning_at (switch_cond_loc,
10137 OPT_Wtraditional, "%<long%> switch expression not "
10138 "converted to %<int%> in ISO C");
10140 exp = c_fully_fold (exp, false, NULL);
10141 exp = default_conversion (exp);
10143 if (warn_sequence_point)
10144 verify_sequence_points (exp);
10148 /* Add this new SWITCH_EXPR to the stack. */
10149 cs = XNEW (struct c_switch);
10150 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10151 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10152 cs->orig_type = orig_type;
10153 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10154 cs->bindings = c_get_switch_bindings ();
10155 cs->bool_cond_p = bool_cond_p;
10156 cs->outside_range_p = false;
10157 cs->next = c_switch_stack;
10158 c_switch_stack = cs;
10160 return add_stmt (cs->switch_expr);
10163 /* Process a case label at location LOC. */
10165 tree
10166 do_case (location_t loc, tree low_value, tree high_value)
10168 tree label = NULL_TREE;
10170 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10172 low_value = c_fully_fold (low_value, false, NULL);
10173 if (TREE_CODE (low_value) == INTEGER_CST)
10174 pedwarn (loc, OPT_Wpedantic,
10175 "case label is not an integer constant expression");
10178 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10180 high_value = c_fully_fold (high_value, false, NULL);
10181 if (TREE_CODE (high_value) == INTEGER_CST)
10182 pedwarn (input_location, OPT_Wpedantic,
10183 "case label is not an integer constant expression");
10186 if (c_switch_stack == NULL)
10188 if (low_value)
10189 error_at (loc, "case label not within a switch statement");
10190 else
10191 error_at (loc, "%<default%> label not within a switch statement");
10192 return NULL_TREE;
10195 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10196 EXPR_LOCATION (c_switch_stack->switch_expr),
10197 loc))
10198 return NULL_TREE;
10200 label = c_add_case_label (loc, c_switch_stack->cases,
10201 SWITCH_COND (c_switch_stack->switch_expr),
10202 c_switch_stack->orig_type,
10203 low_value, high_value,
10204 &c_switch_stack->outside_range_p);
10205 if (label == error_mark_node)
10206 label = NULL_TREE;
10207 return label;
10210 /* Finish the switch statement. TYPE is the original type of the
10211 controlling expression of the switch, or NULL_TREE. */
10213 void
10214 c_finish_case (tree body, tree type)
10216 struct c_switch *cs = c_switch_stack;
10217 location_t switch_location;
10219 SWITCH_BODY (cs->switch_expr) = body;
10221 /* Emit warnings as needed. */
10222 switch_location = EXPR_LOCATION (cs->switch_expr);
10223 c_do_switch_warnings (cs->cases, switch_location,
10224 type ? type : TREE_TYPE (cs->switch_expr),
10225 SWITCH_COND (cs->switch_expr),
10226 cs->bool_cond_p, cs->outside_range_p);
10228 /* Pop the stack. */
10229 c_switch_stack = cs->next;
10230 splay_tree_delete (cs->cases);
10231 c_release_switch_bindings (cs->bindings);
10232 XDELETE (cs);
10235 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10236 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10237 may be null. */
10239 void
10240 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10241 tree else_block)
10243 tree stmt;
10245 /* If the condition has array notations, then the rank of the then_block and
10246 else_block must be either 0 or be equal to the rank of the condition. If
10247 the condition does not have array notations then break them up as it is
10248 broken up in a normal expression. */
10249 if (flag_cilkplus && contains_array_notation_expr (cond))
10251 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10252 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10253 return;
10254 if (then_block
10255 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10256 return;
10257 if (else_block
10258 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10259 return;
10260 if (cond_rank != then_rank && then_rank != 0)
10262 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10263 " and the then-block");
10264 return;
10266 else if (cond_rank != else_rank && else_rank != 0)
10268 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10269 " and the else-block");
10270 return;
10274 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10275 SET_EXPR_LOCATION (stmt, if_locus);
10276 add_stmt (stmt);
10279 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10280 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10281 is false for DO loops. INCR is the FOR increment expression. BODY is
10282 the statement controlled by the loop. BLAB is the break label. CLAB is
10283 the continue label. Everything is allowed to be NULL. */
10285 void
10286 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10287 tree blab, tree clab, bool cond_is_first)
10289 tree entry = NULL, exit = NULL, t;
10291 /* In theory could forbid cilk spawn for loop increment expression,
10292 but it should work just fine. */
10294 /* If the condition is zero don't generate a loop construct. */
10295 if (cond && integer_zerop (cond))
10297 if (cond_is_first)
10299 t = build_and_jump (&blab);
10300 SET_EXPR_LOCATION (t, start_locus);
10301 add_stmt (t);
10304 else
10306 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10308 /* If we have an exit condition, then we build an IF with gotos either
10309 out of the loop, or to the top of it. If there's no exit condition,
10310 then we just build a jump back to the top. */
10311 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10313 if (cond && !integer_nonzerop (cond))
10315 /* Canonicalize the loop condition to the end. This means
10316 generating a branch to the loop condition. Reuse the
10317 continue label, if possible. */
10318 if (cond_is_first)
10320 if (incr || !clab)
10322 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10323 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10325 else
10326 t = build1 (GOTO_EXPR, void_type_node, clab);
10327 SET_EXPR_LOCATION (t, start_locus);
10328 add_stmt (t);
10331 t = build_and_jump (&blab);
10332 if (cond_is_first)
10333 exit = fold_build3_loc (start_locus,
10334 COND_EXPR, void_type_node, cond, exit, t);
10335 else
10336 exit = fold_build3_loc (input_location,
10337 COND_EXPR, void_type_node, cond, exit, t);
10339 else
10341 /* For the backward-goto's location of an unconditional loop
10342 use the beginning of the body, or, if there is none, the
10343 top of the loop. */
10344 location_t loc = EXPR_LOCATION (expr_first (body));
10345 if (loc == UNKNOWN_LOCATION)
10346 loc = start_locus;
10347 SET_EXPR_LOCATION (exit, loc);
10350 add_stmt (top);
10353 if (body)
10354 add_stmt (body);
10355 if (clab)
10356 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10357 if (incr)
10358 add_stmt (incr);
10359 if (entry)
10360 add_stmt (entry);
10361 if (exit)
10362 add_stmt (exit);
10363 if (blab)
10364 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10367 tree
10368 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10370 bool skip;
10371 tree label = *label_p;
10373 /* In switch statements break is sometimes stylistically used after
10374 a return statement. This can lead to spurious warnings about
10375 control reaching the end of a non-void function when it is
10376 inlined. Note that we are calling block_may_fallthru with
10377 language specific tree nodes; this works because
10378 block_may_fallthru returns true when given something it does not
10379 understand. */
10380 skip = !block_may_fallthru (cur_stmt_list);
10382 if (!label)
10384 if (!skip)
10385 *label_p = label = create_artificial_label (loc);
10387 else if (TREE_CODE (label) == LABEL_DECL)
10389 else switch (TREE_INT_CST_LOW (label))
10391 case 0:
10392 if (is_break)
10393 error_at (loc, "break statement not within loop or switch");
10394 else
10395 error_at (loc, "continue statement not within a loop");
10396 return NULL_TREE;
10398 case 1:
10399 gcc_assert (is_break);
10400 error_at (loc, "break statement used with OpenMP for loop");
10401 return NULL_TREE;
10403 case 2:
10404 if (is_break)
10405 error ("break statement within %<#pragma simd%> loop body");
10406 else
10407 error ("continue statement within %<#pragma simd%> loop body");
10408 return NULL_TREE;
10410 default:
10411 gcc_unreachable ();
10414 if (skip)
10415 return NULL_TREE;
10417 if (!is_break)
10418 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10420 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10423 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10425 static void
10426 emit_side_effect_warnings (location_t loc, tree expr)
10428 if (expr == error_mark_node)
10430 else if (!TREE_SIDE_EFFECTS (expr))
10432 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10433 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10435 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10437 tree r = expr;
10438 location_t cloc = loc;
10439 while (TREE_CODE (r) == COMPOUND_EXPR)
10441 if (EXPR_HAS_LOCATION (r))
10442 cloc = EXPR_LOCATION (r);
10443 r = TREE_OPERAND (r, 1);
10445 if (!TREE_SIDE_EFFECTS (r)
10446 && !VOID_TYPE_P (TREE_TYPE (r))
10447 && !CONVERT_EXPR_P (r)
10448 && !TREE_NO_WARNING (r)
10449 && !TREE_NO_WARNING (expr))
10450 warning_at (cloc, OPT_Wunused_value,
10451 "right-hand operand of comma expression has no effect");
10453 else
10454 warn_if_unused_value (expr, loc);
10457 /* Process an expression as if it were a complete statement. Emit
10458 diagnostics, but do not call ADD_STMT. LOC is the location of the
10459 statement. */
10461 tree
10462 c_process_expr_stmt (location_t loc, tree expr)
10464 tree exprv;
10466 if (!expr)
10467 return NULL_TREE;
10469 expr = c_fully_fold (expr, false, NULL);
10471 if (warn_sequence_point)
10472 verify_sequence_points (expr);
10474 if (TREE_TYPE (expr) != error_mark_node
10475 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10476 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10477 error_at (loc, "expression statement has incomplete type");
10479 /* If we're not processing a statement expression, warn about unused values.
10480 Warnings for statement expressions will be emitted later, once we figure
10481 out which is the result. */
10482 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10483 && warn_unused_value)
10484 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10486 exprv = expr;
10487 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10488 exprv = TREE_OPERAND (exprv, 1);
10489 while (CONVERT_EXPR_P (exprv))
10490 exprv = TREE_OPERAND (exprv, 0);
10491 if (DECL_P (exprv)
10492 || handled_component_p (exprv)
10493 || TREE_CODE (exprv) == ADDR_EXPR)
10494 mark_exp_read (exprv);
10496 /* If the expression is not of a type to which we cannot assign a line
10497 number, wrap the thing in a no-op NOP_EXPR. */
10498 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10500 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10501 SET_EXPR_LOCATION (expr, loc);
10504 return expr;
10507 /* Emit an expression as a statement. LOC is the location of the
10508 expression. */
10510 tree
10511 c_finish_expr_stmt (location_t loc, tree expr)
10513 if (expr)
10514 return add_stmt (c_process_expr_stmt (loc, expr));
10515 else
10516 return NULL;
10519 /* Do the opposite and emit a statement as an expression. To begin,
10520 create a new binding level and return it. */
10522 tree
10523 c_begin_stmt_expr (void)
10525 tree ret;
10527 /* We must force a BLOCK for this level so that, if it is not expanded
10528 later, there is a way to turn off the entire subtree of blocks that
10529 are contained in it. */
10530 keep_next_level ();
10531 ret = c_begin_compound_stmt (true);
10533 c_bindings_start_stmt_expr (c_switch_stack == NULL
10534 ? NULL
10535 : c_switch_stack->bindings);
10537 /* Mark the current statement list as belonging to a statement list. */
10538 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10540 return ret;
10543 /* LOC is the location of the compound statement to which this body
10544 belongs. */
10546 tree
10547 c_finish_stmt_expr (location_t loc, tree body)
10549 tree last, type, tmp, val;
10550 tree *last_p;
10552 body = c_end_compound_stmt (loc, body, true);
10554 c_bindings_end_stmt_expr (c_switch_stack == NULL
10555 ? NULL
10556 : c_switch_stack->bindings);
10558 /* Locate the last statement in BODY. See c_end_compound_stmt
10559 about always returning a BIND_EXPR. */
10560 last_p = &BIND_EXPR_BODY (body);
10561 last = BIND_EXPR_BODY (body);
10563 continue_searching:
10564 if (TREE_CODE (last) == STATEMENT_LIST)
10566 tree_stmt_iterator i;
10568 /* This can happen with degenerate cases like ({ }). No value. */
10569 if (!TREE_SIDE_EFFECTS (last))
10570 return body;
10572 /* If we're supposed to generate side effects warnings, process
10573 all of the statements except the last. */
10574 if (warn_unused_value)
10576 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10578 location_t tloc;
10579 tree t = tsi_stmt (i);
10581 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10582 emit_side_effect_warnings (tloc, t);
10585 else
10586 i = tsi_last (last);
10587 last_p = tsi_stmt_ptr (i);
10588 last = *last_p;
10591 /* If the end of the list is exception related, then the list was split
10592 by a call to push_cleanup. Continue searching. */
10593 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10594 || TREE_CODE (last) == TRY_CATCH_EXPR)
10596 last_p = &TREE_OPERAND (last, 0);
10597 last = *last_p;
10598 goto continue_searching;
10601 if (last == error_mark_node)
10602 return last;
10604 /* In the case that the BIND_EXPR is not necessary, return the
10605 expression out from inside it. */
10606 if (last == BIND_EXPR_BODY (body)
10607 && BIND_EXPR_VARS (body) == NULL)
10609 /* Even if this looks constant, do not allow it in a constant
10610 expression. */
10611 last = c_wrap_maybe_const (last, true);
10612 /* Do not warn if the return value of a statement expression is
10613 unused. */
10614 TREE_NO_WARNING (last) = 1;
10615 return last;
10618 /* Extract the type of said expression. */
10619 type = TREE_TYPE (last);
10621 /* If we're not returning a value at all, then the BIND_EXPR that
10622 we already have is a fine expression to return. */
10623 if (!type || VOID_TYPE_P (type))
10624 return body;
10626 /* Now that we've located the expression containing the value, it seems
10627 silly to make voidify_wrapper_expr repeat the process. Create a
10628 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10629 tmp = create_tmp_var_raw (type);
10631 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10632 tree_expr_nonnegative_p giving up immediately. */
10633 val = last;
10634 if (TREE_CODE (val) == NOP_EXPR
10635 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10636 val = TREE_OPERAND (val, 0);
10638 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10639 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10642 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10643 SET_EXPR_LOCATION (t, loc);
10644 return t;
10648 /* Begin and end compound statements. This is as simple as pushing
10649 and popping new statement lists from the tree. */
10651 tree
10652 c_begin_compound_stmt (bool do_scope)
10654 tree stmt = push_stmt_list ();
10655 if (do_scope)
10656 push_scope ();
10657 return stmt;
10660 /* End a compound statement. STMT is the statement. LOC is the
10661 location of the compound statement-- this is usually the location
10662 of the opening brace. */
10664 tree
10665 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10667 tree block = NULL;
10669 if (do_scope)
10671 if (c_dialect_objc ())
10672 objc_clear_super_receiver ();
10673 block = pop_scope ();
10676 stmt = pop_stmt_list (stmt);
10677 stmt = c_build_bind_expr (loc, block, stmt);
10679 /* If this compound statement is nested immediately inside a statement
10680 expression, then force a BIND_EXPR to be created. Otherwise we'll
10681 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10682 STATEMENT_LISTs merge, and thus we can lose track of what statement
10683 was really last. */
10684 if (building_stmt_list_p ()
10685 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10686 && TREE_CODE (stmt) != BIND_EXPR)
10688 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10689 TREE_SIDE_EFFECTS (stmt) = 1;
10690 SET_EXPR_LOCATION (stmt, loc);
10693 return stmt;
10696 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10697 when the current scope is exited. EH_ONLY is true when this is not
10698 meant to apply to normal control flow transfer. */
10700 void
10701 push_cleanup (tree decl, tree cleanup, bool eh_only)
10703 enum tree_code code;
10704 tree stmt, list;
10705 bool stmt_expr;
10707 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10708 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10709 add_stmt (stmt);
10710 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10711 list = push_stmt_list ();
10712 TREE_OPERAND (stmt, 0) = list;
10713 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10716 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10717 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10719 static tree
10720 build_vec_cmp (tree_code code, tree type,
10721 tree arg0, tree arg1)
10723 tree zero_vec = build_zero_cst (type);
10724 tree minus_one_vec = build_minus_one_cst (type);
10725 tree cmp_type = build_same_sized_truth_vector_type (type);
10726 tree cmp = build2 (code, cmp_type, arg0, arg1);
10727 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10730 /* Build a binary-operation expression without default conversions.
10731 CODE is the kind of expression to build.
10732 LOCATION is the operator's location.
10733 This function differs from `build' in several ways:
10734 the data type of the result is computed and recorded in it,
10735 warnings are generated if arg data types are invalid,
10736 special handling for addition and subtraction of pointers is known,
10737 and some optimization is done (operations on narrow ints
10738 are done in the narrower type when that gives the same result).
10739 Constant folding is also done before the result is returned.
10741 Note that the operands will never have enumeral types, or function
10742 or array types, because either they will have the default conversions
10743 performed or they have both just been converted to some other type in which
10744 the arithmetic is to be done. */
10746 tree
10747 build_binary_op (location_t location, enum tree_code code,
10748 tree orig_op0, tree orig_op1, int convert_p)
10750 tree type0, type1, orig_type0, orig_type1;
10751 tree eptype;
10752 enum tree_code code0, code1;
10753 tree op0, op1;
10754 tree ret = error_mark_node;
10755 const char *invalid_op_diag;
10756 bool op0_int_operands, op1_int_operands;
10757 bool int_const, int_const_or_overflow, int_operands;
10759 /* Expression code to give to the expression when it is built.
10760 Normally this is CODE, which is what the caller asked for,
10761 but in some special cases we change it. */
10762 enum tree_code resultcode = code;
10764 /* Data type in which the computation is to be performed.
10765 In the simplest cases this is the common type of the arguments. */
10766 tree result_type = NULL;
10768 /* When the computation is in excess precision, the type of the
10769 final EXCESS_PRECISION_EXPR. */
10770 tree semantic_result_type = NULL;
10772 /* Nonzero means operands have already been type-converted
10773 in whatever way is necessary.
10774 Zero means they need to be converted to RESULT_TYPE. */
10775 int converted = 0;
10777 /* Nonzero means create the expression with this type, rather than
10778 RESULT_TYPE. */
10779 tree build_type = NULL_TREE;
10781 /* Nonzero means after finally constructing the expression
10782 convert it to this type. */
10783 tree final_type = NULL_TREE;
10785 /* Nonzero if this is an operation like MIN or MAX which can
10786 safely be computed in short if both args are promoted shorts.
10787 Also implies COMMON.
10788 -1 indicates a bitwise operation; this makes a difference
10789 in the exact conditions for when it is safe to do the operation
10790 in a narrower mode. */
10791 int shorten = 0;
10793 /* Nonzero if this is a comparison operation;
10794 if both args are promoted shorts, compare the original shorts.
10795 Also implies COMMON. */
10796 int short_compare = 0;
10798 /* Nonzero if this is a right-shift operation, which can be computed on the
10799 original short and then promoted if the operand is a promoted short. */
10800 int short_shift = 0;
10802 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10803 int common = 0;
10805 /* True means types are compatible as far as ObjC is concerned. */
10806 bool objc_ok;
10808 /* True means this is an arithmetic operation that may need excess
10809 precision. */
10810 bool may_need_excess_precision;
10812 /* True means this is a boolean operation that converts both its
10813 operands to truth-values. */
10814 bool boolean_op = false;
10816 /* Remember whether we're doing / or %. */
10817 bool doing_div_or_mod = false;
10819 /* Remember whether we're doing << or >>. */
10820 bool doing_shift = false;
10822 /* Tree holding instrumentation expression. */
10823 tree instrument_expr = NULL;
10825 if (location == UNKNOWN_LOCATION)
10826 location = input_location;
10828 op0 = orig_op0;
10829 op1 = orig_op1;
10831 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10832 if (op0_int_operands)
10833 op0 = remove_c_maybe_const_expr (op0);
10834 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10835 if (op1_int_operands)
10836 op1 = remove_c_maybe_const_expr (op1);
10837 int_operands = (op0_int_operands && op1_int_operands);
10838 if (int_operands)
10840 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10841 && TREE_CODE (orig_op1) == INTEGER_CST);
10842 int_const = (int_const_or_overflow
10843 && !TREE_OVERFLOW (orig_op0)
10844 && !TREE_OVERFLOW (orig_op1));
10846 else
10847 int_const = int_const_or_overflow = false;
10849 /* Do not apply default conversion in mixed vector/scalar expression. */
10850 if (convert_p
10851 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10853 op0 = default_conversion (op0);
10854 op1 = default_conversion (op1);
10857 /* When Cilk Plus is enabled and there are array notations inside op0, then
10858 we check to see if there are builtin array notation functions. If
10859 so, then we take on the type of the array notation inside it. */
10860 if (flag_cilkplus && contains_array_notation_expr (op0))
10861 orig_type0 = type0 = find_correct_array_notation_type (op0);
10862 else
10863 orig_type0 = type0 = TREE_TYPE (op0);
10865 if (flag_cilkplus && contains_array_notation_expr (op1))
10866 orig_type1 = type1 = find_correct_array_notation_type (op1);
10867 else
10868 orig_type1 = type1 = TREE_TYPE (op1);
10870 /* The expression codes of the data types of the arguments tell us
10871 whether the arguments are integers, floating, pointers, etc. */
10872 code0 = TREE_CODE (type0);
10873 code1 = TREE_CODE (type1);
10875 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10876 STRIP_TYPE_NOPS (op0);
10877 STRIP_TYPE_NOPS (op1);
10879 /* If an error was already reported for one of the arguments,
10880 avoid reporting another error. */
10882 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10883 return error_mark_node;
10885 if (code0 == POINTER_TYPE
10886 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10887 return error_mark_node;
10889 if (code1 == POINTER_TYPE
10890 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10891 return error_mark_node;
10893 if ((invalid_op_diag
10894 = targetm.invalid_binary_op (code, type0, type1)))
10896 error_at (location, invalid_op_diag);
10897 return error_mark_node;
10900 switch (code)
10902 case PLUS_EXPR:
10903 case MINUS_EXPR:
10904 case MULT_EXPR:
10905 case TRUNC_DIV_EXPR:
10906 case CEIL_DIV_EXPR:
10907 case FLOOR_DIV_EXPR:
10908 case ROUND_DIV_EXPR:
10909 case EXACT_DIV_EXPR:
10910 may_need_excess_precision = true;
10911 break;
10912 default:
10913 may_need_excess_precision = false;
10914 break;
10916 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10918 op0 = TREE_OPERAND (op0, 0);
10919 type0 = TREE_TYPE (op0);
10921 else if (may_need_excess_precision
10922 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10924 type0 = eptype;
10925 op0 = convert (eptype, op0);
10927 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10929 op1 = TREE_OPERAND (op1, 0);
10930 type1 = TREE_TYPE (op1);
10932 else if (may_need_excess_precision
10933 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10935 type1 = eptype;
10936 op1 = convert (eptype, op1);
10939 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10941 /* In case when one of the operands of the binary operation is
10942 a vector and another is a scalar -- convert scalar to vector. */
10943 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10945 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10946 true);
10948 switch (convert_flag)
10950 case stv_error:
10951 return error_mark_node;
10952 case stv_firstarg:
10954 bool maybe_const = true;
10955 tree sc;
10956 sc = c_fully_fold (op0, false, &maybe_const);
10957 sc = save_expr (sc);
10958 sc = convert (TREE_TYPE (type1), sc);
10959 op0 = build_vector_from_val (type1, sc);
10960 if (!maybe_const)
10961 op0 = c_wrap_maybe_const (op0, true);
10962 orig_type0 = type0 = TREE_TYPE (op0);
10963 code0 = TREE_CODE (type0);
10964 converted = 1;
10965 break;
10967 case stv_secondarg:
10969 bool maybe_const = true;
10970 tree sc;
10971 sc = c_fully_fold (op1, false, &maybe_const);
10972 sc = save_expr (sc);
10973 sc = convert (TREE_TYPE (type0), sc);
10974 op1 = build_vector_from_val (type0, sc);
10975 if (!maybe_const)
10976 op1 = c_wrap_maybe_const (op1, true);
10977 orig_type1 = type1 = TREE_TYPE (op1);
10978 code1 = TREE_CODE (type1);
10979 converted = 1;
10980 break;
10982 default:
10983 break;
10987 switch (code)
10989 case PLUS_EXPR:
10990 /* Handle the pointer + int case. */
10991 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10993 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10994 goto return_build_binary_op;
10996 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10998 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10999 goto return_build_binary_op;
11001 else
11002 common = 1;
11003 break;
11005 case MINUS_EXPR:
11006 /* Subtraction of two similar pointers.
11007 We must subtract them as integers, then divide by object size. */
11008 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11009 && comp_target_types (location, type0, type1))
11011 ret = pointer_diff (location, op0, op1);
11012 goto return_build_binary_op;
11014 /* Handle pointer minus int. Just like pointer plus int. */
11015 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11017 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11018 goto return_build_binary_op;
11020 else
11021 common = 1;
11022 break;
11024 case MULT_EXPR:
11025 common = 1;
11026 break;
11028 case TRUNC_DIV_EXPR:
11029 case CEIL_DIV_EXPR:
11030 case FLOOR_DIV_EXPR:
11031 case ROUND_DIV_EXPR:
11032 case EXACT_DIV_EXPR:
11033 doing_div_or_mod = true;
11034 warn_for_div_by_zero (location, op1);
11036 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11037 || code0 == FIXED_POINT_TYPE
11038 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11039 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11040 || code1 == FIXED_POINT_TYPE
11041 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11043 enum tree_code tcode0 = code0, tcode1 = code1;
11045 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11046 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11047 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11048 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11050 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11051 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11052 resultcode = RDIV_EXPR;
11053 else
11054 /* Although it would be tempting to shorten always here, that
11055 loses on some targets, since the modulo instruction is
11056 undefined if the quotient can't be represented in the
11057 computation mode. We shorten only if unsigned or if
11058 dividing by something we know != -1. */
11059 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11060 || (TREE_CODE (op1) == INTEGER_CST
11061 && !integer_all_onesp (op1)));
11062 common = 1;
11064 break;
11066 case BIT_AND_EXPR:
11067 case BIT_IOR_EXPR:
11068 case BIT_XOR_EXPR:
11069 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11070 shorten = -1;
11071 /* Allow vector types which are not floating point types. */
11072 else if (code0 == VECTOR_TYPE
11073 && code1 == VECTOR_TYPE
11074 && !VECTOR_FLOAT_TYPE_P (type0)
11075 && !VECTOR_FLOAT_TYPE_P (type1))
11076 common = 1;
11077 break;
11079 case TRUNC_MOD_EXPR:
11080 case FLOOR_MOD_EXPR:
11081 doing_div_or_mod = true;
11082 warn_for_div_by_zero (location, op1);
11084 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11085 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11086 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11087 common = 1;
11088 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11090 /* Although it would be tempting to shorten always here, that loses
11091 on some targets, since the modulo instruction is undefined if the
11092 quotient can't be represented in the computation mode. We shorten
11093 only if unsigned or if dividing by something we know != -1. */
11094 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11095 || (TREE_CODE (op1) == INTEGER_CST
11096 && !integer_all_onesp (op1)));
11097 common = 1;
11099 break;
11101 case TRUTH_ANDIF_EXPR:
11102 case TRUTH_ORIF_EXPR:
11103 case TRUTH_AND_EXPR:
11104 case TRUTH_OR_EXPR:
11105 case TRUTH_XOR_EXPR:
11106 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11107 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11108 || code0 == FIXED_POINT_TYPE)
11109 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11110 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11111 || code1 == FIXED_POINT_TYPE))
11113 /* Result of these operations is always an int,
11114 but that does not mean the operands should be
11115 converted to ints! */
11116 result_type = integer_type_node;
11117 if (op0_int_operands)
11119 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11120 op0 = remove_c_maybe_const_expr (op0);
11122 else
11123 op0 = c_objc_common_truthvalue_conversion (location, op0);
11124 if (op1_int_operands)
11126 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11127 op1 = remove_c_maybe_const_expr (op1);
11129 else
11130 op1 = c_objc_common_truthvalue_conversion (location, op1);
11131 converted = 1;
11132 boolean_op = true;
11134 if (code == TRUTH_ANDIF_EXPR)
11136 int_const_or_overflow = (int_operands
11137 && TREE_CODE (orig_op0) == INTEGER_CST
11138 && (op0 == truthvalue_false_node
11139 || TREE_CODE (orig_op1) == INTEGER_CST));
11140 int_const = (int_const_or_overflow
11141 && !TREE_OVERFLOW (orig_op0)
11142 && (op0 == truthvalue_false_node
11143 || !TREE_OVERFLOW (orig_op1)));
11145 else if (code == TRUTH_ORIF_EXPR)
11147 int_const_or_overflow = (int_operands
11148 && TREE_CODE (orig_op0) == INTEGER_CST
11149 && (op0 == truthvalue_true_node
11150 || TREE_CODE (orig_op1) == INTEGER_CST));
11151 int_const = (int_const_or_overflow
11152 && !TREE_OVERFLOW (orig_op0)
11153 && (op0 == truthvalue_true_node
11154 || !TREE_OVERFLOW (orig_op1)));
11156 break;
11158 /* Shift operations: result has same type as first operand;
11159 always convert second operand to int.
11160 Also set SHORT_SHIFT if shifting rightward. */
11162 case RSHIFT_EXPR:
11163 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11164 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11165 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11166 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11168 result_type = type0;
11169 converted = 1;
11171 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11172 || code0 == VECTOR_TYPE)
11173 && code1 == INTEGER_TYPE)
11175 doing_shift = true;
11176 if (TREE_CODE (op1) == INTEGER_CST)
11178 if (tree_int_cst_sgn (op1) < 0)
11180 int_const = false;
11181 if (c_inhibit_evaluation_warnings == 0)
11182 warning_at (location, OPT_Wshift_count_negative,
11183 "right shift count is negative");
11185 else if (code0 == VECTOR_TYPE)
11187 if (compare_tree_int (op1,
11188 TYPE_PRECISION (TREE_TYPE (type0)))
11189 >= 0)
11191 int_const = false;
11192 if (c_inhibit_evaluation_warnings == 0)
11193 warning_at (location, OPT_Wshift_count_overflow,
11194 "right shift count >= width of vector element");
11197 else
11199 if (!integer_zerop (op1))
11200 short_shift = 1;
11202 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11204 int_const = false;
11205 if (c_inhibit_evaluation_warnings == 0)
11206 warning_at (location, OPT_Wshift_count_overflow,
11207 "right shift count >= width of type");
11212 /* Use the type of the value to be shifted. */
11213 result_type = type0;
11214 /* Avoid converting op1 to result_type later. */
11215 converted = 1;
11217 break;
11219 case LSHIFT_EXPR:
11220 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11221 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11222 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11223 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11225 result_type = type0;
11226 converted = 1;
11228 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11229 || code0 == VECTOR_TYPE)
11230 && code1 == INTEGER_TYPE)
11232 doing_shift = true;
11233 if (TREE_CODE (op0) == INTEGER_CST
11234 && tree_int_cst_sgn (op0) < 0)
11236 /* Don't reject a left shift of a negative value in a context
11237 where a constant expression is needed in C90. */
11238 if (flag_isoc99)
11239 int_const = false;
11240 if (c_inhibit_evaluation_warnings == 0)
11241 warning_at (location, OPT_Wshift_negative_value,
11242 "left shift of negative value");
11244 if (TREE_CODE (op1) == INTEGER_CST)
11246 if (tree_int_cst_sgn (op1) < 0)
11248 int_const = false;
11249 if (c_inhibit_evaluation_warnings == 0)
11250 warning_at (location, OPT_Wshift_count_negative,
11251 "left shift count is negative");
11253 else if (code0 == VECTOR_TYPE)
11255 if (compare_tree_int (op1,
11256 TYPE_PRECISION (TREE_TYPE (type0)))
11257 >= 0)
11259 int_const = false;
11260 if (c_inhibit_evaluation_warnings == 0)
11261 warning_at (location, OPT_Wshift_count_overflow,
11262 "left shift count >= width of vector element");
11265 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11267 int_const = false;
11268 if (c_inhibit_evaluation_warnings == 0)
11269 warning_at (location, OPT_Wshift_count_overflow,
11270 "left shift count >= width of type");
11272 else if (TREE_CODE (op0) == INTEGER_CST
11273 && maybe_warn_shift_overflow (location, op0, op1)
11274 && flag_isoc99)
11275 int_const = false;
11278 /* Use the type of the value to be shifted. */
11279 result_type = type0;
11280 /* Avoid converting op1 to result_type later. */
11281 converted = 1;
11283 break;
11285 case EQ_EXPR:
11286 case NE_EXPR:
11287 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11289 tree intt;
11290 if (!vector_types_compatible_elements_p (type0, type1))
11292 error_at (location, "comparing vectors with different "
11293 "element types");
11294 return error_mark_node;
11297 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11299 error_at (location, "comparing vectors with different "
11300 "number of elements");
11301 return error_mark_node;
11304 /* It's not precisely specified how the usual arithmetic
11305 conversions apply to the vector types. Here, we use
11306 the unsigned type if one of the operands is signed and
11307 the other one is unsigned. */
11308 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11310 if (!TYPE_UNSIGNED (type0))
11311 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11312 else
11313 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11314 warning_at (location, OPT_Wsign_compare, "comparison between "
11315 "types %qT and %qT", type0, type1);
11318 /* Always construct signed integer vector type. */
11319 intt = c_common_type_for_size (GET_MODE_BITSIZE
11320 (TYPE_MODE (TREE_TYPE (type0))), 0);
11321 result_type = build_opaque_vector_type (intt,
11322 TYPE_VECTOR_SUBPARTS (type0));
11323 converted = 1;
11324 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11325 goto return_build_binary_op;
11327 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11328 warning_at (location,
11329 OPT_Wfloat_equal,
11330 "comparing floating point with == or != is unsafe");
11331 /* Result of comparison is always int,
11332 but don't convert the args to int! */
11333 build_type = integer_type_node;
11334 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11335 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11336 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11337 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11338 short_compare = 1;
11339 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11341 if (TREE_CODE (op0) == ADDR_EXPR
11342 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11343 && !from_macro_expansion_at (location))
11345 if (code == EQ_EXPR)
11346 warning_at (location,
11347 OPT_Waddress,
11348 "the comparison will always evaluate as %<false%> "
11349 "for the address of %qD will never be NULL",
11350 TREE_OPERAND (op0, 0));
11351 else
11352 warning_at (location,
11353 OPT_Waddress,
11354 "the comparison will always evaluate as %<true%> "
11355 "for the address of %qD will never be NULL",
11356 TREE_OPERAND (op0, 0));
11358 result_type = type0;
11360 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11362 if (TREE_CODE (op1) == ADDR_EXPR
11363 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11364 && !from_macro_expansion_at (location))
11366 if (code == EQ_EXPR)
11367 warning_at (location,
11368 OPT_Waddress,
11369 "the comparison will always evaluate as %<false%> "
11370 "for the address of %qD will never be NULL",
11371 TREE_OPERAND (op1, 0));
11372 else
11373 warning_at (location,
11374 OPT_Waddress,
11375 "the comparison will always evaluate as %<true%> "
11376 "for the address of %qD will never be NULL",
11377 TREE_OPERAND (op1, 0));
11379 result_type = type1;
11381 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11383 tree tt0 = TREE_TYPE (type0);
11384 tree tt1 = TREE_TYPE (type1);
11385 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11386 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11387 addr_space_t as_common = ADDR_SPACE_GENERIC;
11389 /* Anything compares with void *. void * compares with anything.
11390 Otherwise, the targets must be compatible
11391 and both must be object or both incomplete. */
11392 if (comp_target_types (location, type0, type1))
11393 result_type = common_pointer_type (type0, type1);
11394 else if (!addr_space_superset (as0, as1, &as_common))
11396 error_at (location, "comparison of pointers to "
11397 "disjoint address spaces");
11398 return error_mark_node;
11400 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11402 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11403 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11404 "comparison of %<void *%> with function pointer");
11406 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11408 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11409 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11410 "comparison of %<void *%> with function pointer");
11412 else
11413 /* Avoid warning about the volatile ObjC EH puts on decls. */
11414 if (!objc_ok)
11415 pedwarn (location, 0,
11416 "comparison of distinct pointer types lacks a cast");
11418 if (result_type == NULL_TREE)
11420 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11421 result_type = build_pointer_type
11422 (build_qualified_type (void_type_node, qual));
11425 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11427 result_type = type0;
11428 pedwarn (location, 0, "comparison between pointer and integer");
11430 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11432 result_type = type1;
11433 pedwarn (location, 0, "comparison between pointer and integer");
11435 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11436 || truth_value_p (TREE_CODE (orig_op0)))
11437 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11438 || truth_value_p (TREE_CODE (orig_op1))))
11439 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11440 break;
11442 case LE_EXPR:
11443 case GE_EXPR:
11444 case LT_EXPR:
11445 case GT_EXPR:
11446 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11448 tree intt;
11449 if (!vector_types_compatible_elements_p (type0, type1))
11451 error_at (location, "comparing vectors with different "
11452 "element types");
11453 return error_mark_node;
11456 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11458 error_at (location, "comparing vectors with different "
11459 "number of elements");
11460 return error_mark_node;
11463 /* It's not precisely specified how the usual arithmetic
11464 conversions apply to the vector types. Here, we use
11465 the unsigned type if one of the operands is signed and
11466 the other one is unsigned. */
11467 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11469 if (!TYPE_UNSIGNED (type0))
11470 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11471 else
11472 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11473 warning_at (location, OPT_Wsign_compare, "comparison between "
11474 "types %qT and %qT", type0, type1);
11477 /* Always construct signed integer vector type. */
11478 intt = c_common_type_for_size (GET_MODE_BITSIZE
11479 (TYPE_MODE (TREE_TYPE (type0))), 0);
11480 result_type = build_opaque_vector_type (intt,
11481 TYPE_VECTOR_SUBPARTS (type0));
11482 converted = 1;
11483 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11484 goto return_build_binary_op;
11486 build_type = integer_type_node;
11487 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11488 || code0 == FIXED_POINT_TYPE)
11489 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11490 || code1 == FIXED_POINT_TYPE))
11491 short_compare = 1;
11492 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11494 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11495 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11496 addr_space_t as_common;
11498 if (comp_target_types (location, type0, type1))
11500 result_type = common_pointer_type (type0, type1);
11501 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11502 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11503 pedwarn (location, 0,
11504 "comparison of complete and incomplete pointers");
11505 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11506 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11507 "ordered comparisons of pointers to functions");
11508 else if (null_pointer_constant_p (orig_op0)
11509 || null_pointer_constant_p (orig_op1))
11510 warning_at (location, OPT_Wextra,
11511 "ordered comparison of pointer with null pointer");
11514 else if (!addr_space_superset (as0, as1, &as_common))
11516 error_at (location, "comparison of pointers to "
11517 "disjoint address spaces");
11518 return error_mark_node;
11520 else
11522 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11523 result_type = build_pointer_type
11524 (build_qualified_type (void_type_node, qual));
11525 pedwarn (location, 0,
11526 "comparison of distinct pointer types lacks a cast");
11529 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11531 result_type = type0;
11532 if (pedantic)
11533 pedwarn (location, OPT_Wpedantic,
11534 "ordered comparison of pointer with integer zero");
11535 else if (extra_warnings)
11536 warning_at (location, OPT_Wextra,
11537 "ordered comparison of pointer with integer zero");
11539 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11541 result_type = type1;
11542 if (pedantic)
11543 pedwarn (location, OPT_Wpedantic,
11544 "ordered comparison of pointer with integer zero");
11545 else if (extra_warnings)
11546 warning_at (location, OPT_Wextra,
11547 "ordered comparison of pointer with integer zero");
11549 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11551 result_type = type0;
11552 pedwarn (location, 0, "comparison between pointer and integer");
11554 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11556 result_type = type1;
11557 pedwarn (location, 0, "comparison between pointer and integer");
11559 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11560 || truth_value_p (TREE_CODE (orig_op0)))
11561 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11562 || truth_value_p (TREE_CODE (orig_op1))))
11563 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11564 break;
11566 default:
11567 gcc_unreachable ();
11570 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11571 return error_mark_node;
11573 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11574 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11575 || !vector_types_compatible_elements_p (type0, type1)))
11577 gcc_rich_location richloc (location);
11578 richloc.maybe_add_expr (orig_op0);
11579 richloc.maybe_add_expr (orig_op1);
11580 binary_op_error (&richloc, code, type0, type1);
11581 return error_mark_node;
11584 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11585 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11587 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11588 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11590 bool first_complex = (code0 == COMPLEX_TYPE);
11591 bool second_complex = (code1 == COMPLEX_TYPE);
11592 int none_complex = (!first_complex && !second_complex);
11594 if (shorten || common || short_compare)
11596 result_type = c_common_type (type0, type1);
11597 do_warn_double_promotion (result_type, type0, type1,
11598 "implicit conversion from %qT to %qT "
11599 "to match other operand of binary "
11600 "expression",
11601 location);
11602 if (result_type == error_mark_node)
11603 return error_mark_node;
11606 if (first_complex != second_complex
11607 && (code == PLUS_EXPR
11608 || code == MINUS_EXPR
11609 || code == MULT_EXPR
11610 || (code == TRUNC_DIV_EXPR && first_complex))
11611 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11612 && flag_signed_zeros)
11614 /* An operation on mixed real/complex operands must be
11615 handled specially, but the language-independent code can
11616 more easily optimize the plain complex arithmetic if
11617 -fno-signed-zeros. */
11618 tree real_type = TREE_TYPE (result_type);
11619 tree real, imag;
11620 if (type0 != orig_type0 || type1 != orig_type1)
11622 gcc_assert (may_need_excess_precision && common);
11623 semantic_result_type = c_common_type (orig_type0, orig_type1);
11625 if (first_complex)
11627 if (TREE_TYPE (op0) != result_type)
11628 op0 = convert_and_check (location, result_type, op0);
11629 if (TREE_TYPE (op1) != real_type)
11630 op1 = convert_and_check (location, real_type, op1);
11632 else
11634 if (TREE_TYPE (op0) != real_type)
11635 op0 = convert_and_check (location, real_type, op0);
11636 if (TREE_TYPE (op1) != result_type)
11637 op1 = convert_and_check (location, result_type, op1);
11639 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11640 return error_mark_node;
11641 if (first_complex)
11643 op0 = save_expr (op0);
11644 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11645 op0, true);
11646 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11647 op0, true);
11648 switch (code)
11650 case MULT_EXPR:
11651 case TRUNC_DIV_EXPR:
11652 op1 = save_expr (op1);
11653 imag = build2 (resultcode, real_type, imag, op1);
11654 /* Fall through. */
11655 case PLUS_EXPR:
11656 case MINUS_EXPR:
11657 real = build2 (resultcode, real_type, real, op1);
11658 break;
11659 default:
11660 gcc_unreachable();
11663 else
11665 op1 = save_expr (op1);
11666 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11667 op1, true);
11668 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11669 op1, true);
11670 switch (code)
11672 case MULT_EXPR:
11673 op0 = save_expr (op0);
11674 imag = build2 (resultcode, real_type, op0, imag);
11675 /* Fall through. */
11676 case PLUS_EXPR:
11677 real = build2 (resultcode, real_type, op0, real);
11678 break;
11679 case MINUS_EXPR:
11680 real = build2 (resultcode, real_type, op0, real);
11681 imag = build1 (NEGATE_EXPR, real_type, imag);
11682 break;
11683 default:
11684 gcc_unreachable();
11687 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11688 goto return_build_binary_op;
11691 /* For certain operations (which identify themselves by shorten != 0)
11692 if both args were extended from the same smaller type,
11693 do the arithmetic in that type and then extend.
11695 shorten !=0 and !=1 indicates a bitwise operation.
11696 For them, this optimization is safe only if
11697 both args are zero-extended or both are sign-extended.
11698 Otherwise, we might change the result.
11699 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11700 but calculated in (unsigned short) it would be (unsigned short)-1. */
11702 if (shorten && none_complex)
11704 final_type = result_type;
11705 result_type = shorten_binary_op (result_type, op0, op1,
11706 shorten == -1);
11709 /* Shifts can be shortened if shifting right. */
11711 if (short_shift)
11713 int unsigned_arg;
11714 tree arg0 = get_narrower (op0, &unsigned_arg);
11716 final_type = result_type;
11718 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11719 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11721 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11722 && tree_int_cst_sgn (op1) > 0
11723 /* We can shorten only if the shift count is less than the
11724 number of bits in the smaller type size. */
11725 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11726 /* We cannot drop an unsigned shift after sign-extension. */
11727 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11729 /* Do an unsigned shift if the operand was zero-extended. */
11730 result_type
11731 = c_common_signed_or_unsigned_type (unsigned_arg,
11732 TREE_TYPE (arg0));
11733 /* Convert value-to-be-shifted to that type. */
11734 if (TREE_TYPE (op0) != result_type)
11735 op0 = convert (result_type, op0);
11736 converted = 1;
11740 /* Comparison operations are shortened too but differently.
11741 They identify themselves by setting short_compare = 1. */
11743 if (short_compare)
11745 /* Don't write &op0, etc., because that would prevent op0
11746 from being kept in a register.
11747 Instead, make copies of the our local variables and
11748 pass the copies by reference, then copy them back afterward. */
11749 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11750 enum tree_code xresultcode = resultcode;
11751 tree val
11752 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11753 &xresultcode);
11755 if (val != NULL_TREE)
11757 ret = val;
11758 goto return_build_binary_op;
11761 op0 = xop0, op1 = xop1;
11762 converted = 1;
11763 resultcode = xresultcode;
11765 if (c_inhibit_evaluation_warnings == 0)
11767 bool op0_maybe_const = true;
11768 bool op1_maybe_const = true;
11769 tree orig_op0_folded, orig_op1_folded;
11771 if (in_late_binary_op)
11773 orig_op0_folded = orig_op0;
11774 orig_op1_folded = orig_op1;
11776 else
11778 /* Fold for the sake of possible warnings, as in
11779 build_conditional_expr. This requires the
11780 "original" values to be folded, not just op0 and
11781 op1. */
11782 c_inhibit_evaluation_warnings++;
11783 op0 = c_fully_fold (op0, require_constant_value,
11784 &op0_maybe_const);
11785 op1 = c_fully_fold (op1, require_constant_value,
11786 &op1_maybe_const);
11787 c_inhibit_evaluation_warnings--;
11788 orig_op0_folded = c_fully_fold (orig_op0,
11789 require_constant_value,
11790 NULL);
11791 orig_op1_folded = c_fully_fold (orig_op1,
11792 require_constant_value,
11793 NULL);
11796 if (warn_sign_compare)
11797 warn_for_sign_compare (location, orig_op0_folded,
11798 orig_op1_folded, op0, op1,
11799 result_type, resultcode);
11800 if (!in_late_binary_op && !int_operands)
11802 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11803 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11804 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11805 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11811 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11812 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11813 Then the expression will be built.
11814 It will be given type FINAL_TYPE if that is nonzero;
11815 otherwise, it will be given type RESULT_TYPE. */
11817 if (!result_type)
11819 gcc_rich_location richloc (location);
11820 richloc.maybe_add_expr (orig_op0);
11821 richloc.maybe_add_expr (orig_op1);
11822 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11823 return error_mark_node;
11826 if (build_type == NULL_TREE)
11828 build_type = result_type;
11829 if ((type0 != orig_type0 || type1 != orig_type1)
11830 && !boolean_op)
11832 gcc_assert (may_need_excess_precision && common);
11833 semantic_result_type = c_common_type (orig_type0, orig_type1);
11837 if (!converted)
11839 op0 = ep_convert_and_check (location, result_type, op0,
11840 semantic_result_type);
11841 op1 = ep_convert_and_check (location, result_type, op1,
11842 semantic_result_type);
11844 /* This can happen if one operand has a vector type, and the other
11845 has a different type. */
11846 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11847 return error_mark_node;
11850 if (sanitize_flags_p ((SANITIZE_SHIFT
11851 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
11852 && current_function_decl != NULL_TREE
11853 && (doing_div_or_mod || doing_shift)
11854 && !require_constant_value)
11856 /* OP0 and/or OP1 might have side-effects. */
11857 op0 = save_expr (op0);
11858 op1 = save_expr (op1);
11859 op0 = c_fully_fold (op0, false, NULL);
11860 op1 = c_fully_fold (op1, false, NULL);
11861 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
11862 | SANITIZE_FLOAT_DIVIDE))))
11863 instrument_expr = ubsan_instrument_division (location, op0, op1);
11864 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
11865 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11868 /* Treat expressions in initializers specially as they can't trap. */
11869 if (int_const_or_overflow)
11870 ret = (require_constant_value
11871 ? fold_build2_initializer_loc (location, resultcode, build_type,
11872 op0, op1)
11873 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11874 else
11875 ret = build2 (resultcode, build_type, op0, op1);
11876 if (final_type != NULL_TREE)
11877 ret = convert (final_type, ret);
11879 return_build_binary_op:
11880 gcc_assert (ret != error_mark_node);
11881 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11882 ret = (int_operands
11883 ? note_integer_operands (ret)
11884 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11885 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11886 && !in_late_binary_op)
11887 ret = note_integer_operands (ret);
11888 protected_set_expr_location (ret, location);
11890 if (instrument_expr != NULL)
11891 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11892 instrument_expr, ret);
11894 if (semantic_result_type)
11895 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
11896 semantic_result_type, ret);
11898 return ret;
11902 /* Convert EXPR to be a truth-value, validating its type for this
11903 purpose. LOCATION is the source location for the expression. */
11905 tree
11906 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11908 bool int_const, int_operands;
11910 switch (TREE_CODE (TREE_TYPE (expr)))
11912 case ARRAY_TYPE:
11913 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11914 return error_mark_node;
11916 case RECORD_TYPE:
11917 error_at (location, "used struct type value where scalar is required");
11918 return error_mark_node;
11920 case UNION_TYPE:
11921 error_at (location, "used union type value where scalar is required");
11922 return error_mark_node;
11924 case VOID_TYPE:
11925 error_at (location, "void value not ignored as it ought to be");
11926 return error_mark_node;
11928 case POINTER_TYPE:
11929 if (reject_gcc_builtin (expr))
11930 return error_mark_node;
11931 break;
11933 case FUNCTION_TYPE:
11934 gcc_unreachable ();
11936 case VECTOR_TYPE:
11937 error_at (location, "used vector type where scalar is required");
11938 return error_mark_node;
11940 default:
11941 break;
11944 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11945 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11946 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11948 expr = remove_c_maybe_const_expr (expr);
11949 expr = build2 (NE_EXPR, integer_type_node, expr,
11950 convert (TREE_TYPE (expr), integer_zero_node));
11951 expr = note_integer_operands (expr);
11953 else
11954 /* ??? Should we also give an error for vectors rather than leaving
11955 those to give errors later? */
11956 expr = c_common_truthvalue_conversion (location, expr);
11958 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11960 if (TREE_OVERFLOW (expr))
11961 return expr;
11962 else
11963 return note_integer_operands (expr);
11965 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11966 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11967 return expr;
11971 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11972 required. */
11974 tree
11975 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11977 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11979 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11980 /* Executing a compound literal inside a function reinitializes
11981 it. */
11982 if (!TREE_STATIC (decl))
11983 *se = true;
11984 return decl;
11986 else
11987 return expr;
11990 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11991 statement. LOC is the location of the construct. */
11993 tree
11994 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11995 tree clauses)
11997 body = c_end_compound_stmt (loc, body, true);
11999 tree stmt = make_node (code);
12000 TREE_TYPE (stmt) = void_type_node;
12001 OMP_BODY (stmt) = body;
12002 OMP_CLAUSES (stmt) = clauses;
12003 SET_EXPR_LOCATION (stmt, loc);
12005 return add_stmt (stmt);
12008 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12009 statement. LOC is the location of the OACC_DATA. */
12011 tree
12012 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12014 tree stmt;
12016 block = c_end_compound_stmt (loc, block, true);
12018 stmt = make_node (OACC_DATA);
12019 TREE_TYPE (stmt) = void_type_node;
12020 OACC_DATA_CLAUSES (stmt) = clauses;
12021 OACC_DATA_BODY (stmt) = block;
12022 SET_EXPR_LOCATION (stmt, loc);
12024 return add_stmt (stmt);
12027 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12028 statement. LOC is the location of the OACC_HOST_DATA. */
12030 tree
12031 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12033 tree stmt;
12035 block = c_end_compound_stmt (loc, block, true);
12037 stmt = make_node (OACC_HOST_DATA);
12038 TREE_TYPE (stmt) = void_type_node;
12039 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12040 OACC_HOST_DATA_BODY (stmt) = block;
12041 SET_EXPR_LOCATION (stmt, loc);
12043 return add_stmt (stmt);
12046 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12048 tree
12049 c_begin_omp_parallel (void)
12051 tree block;
12053 keep_next_level ();
12054 block = c_begin_compound_stmt (true);
12056 return block;
12059 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12060 statement. LOC is the location of the OMP_PARALLEL. */
12062 tree
12063 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12065 tree stmt;
12067 block = c_end_compound_stmt (loc, block, true);
12069 stmt = make_node (OMP_PARALLEL);
12070 TREE_TYPE (stmt) = void_type_node;
12071 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12072 OMP_PARALLEL_BODY (stmt) = block;
12073 SET_EXPR_LOCATION (stmt, loc);
12075 return add_stmt (stmt);
12078 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12080 tree
12081 c_begin_omp_task (void)
12083 tree block;
12085 keep_next_level ();
12086 block = c_begin_compound_stmt (true);
12088 return block;
12091 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12092 statement. LOC is the location of the #pragma. */
12094 tree
12095 c_finish_omp_task (location_t loc, tree clauses, tree block)
12097 tree stmt;
12099 block = c_end_compound_stmt (loc, block, true);
12101 stmt = make_node (OMP_TASK);
12102 TREE_TYPE (stmt) = void_type_node;
12103 OMP_TASK_CLAUSES (stmt) = clauses;
12104 OMP_TASK_BODY (stmt) = block;
12105 SET_EXPR_LOCATION (stmt, loc);
12107 return add_stmt (stmt);
12110 /* Generate GOMP_cancel call for #pragma omp cancel. */
12112 void
12113 c_finish_omp_cancel (location_t loc, tree clauses)
12115 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12116 int mask = 0;
12117 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12118 mask = 1;
12119 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12120 mask = 2;
12121 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12122 mask = 4;
12123 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12124 mask = 8;
12125 else
12127 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12128 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12129 "clauses");
12130 return;
12132 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12133 if (ifc != NULL_TREE)
12135 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12136 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12137 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12138 build_zero_cst (type));
12140 else
12141 ifc = boolean_true_node;
12142 tree stmt = build_call_expr_loc (loc, fn, 2,
12143 build_int_cst (integer_type_node, mask),
12144 ifc);
12145 add_stmt (stmt);
12148 /* Generate GOMP_cancellation_point call for
12149 #pragma omp cancellation point. */
12151 void
12152 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12154 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12155 int mask = 0;
12156 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12157 mask = 1;
12158 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12159 mask = 2;
12160 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12161 mask = 4;
12162 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12163 mask = 8;
12164 else
12166 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12167 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12168 "clauses");
12169 return;
12171 tree stmt = build_call_expr_loc (loc, fn, 1,
12172 build_int_cst (integer_type_node, mask));
12173 add_stmt (stmt);
12176 /* Helper function for handle_omp_array_sections. Called recursively
12177 to handle multiple array-section-subscripts. C is the clause,
12178 T current expression (initially OMP_CLAUSE_DECL), which is either
12179 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12180 expression if specified, TREE_VALUE length expression if specified,
12181 TREE_CHAIN is what it has been specified after, or some decl.
12182 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12183 set to true if any of the array-section-subscript could have length
12184 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12185 first array-section-subscript which is known not to have length
12186 of one. Given say:
12187 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12188 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12189 all are or may have length of 1, array-section-subscript [:2] is the
12190 first one known not to have length 1. For array-section-subscript
12191 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12192 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12193 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12194 case though, as some lengths could be zero. */
12196 static tree
12197 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12198 bool &maybe_zero_len, unsigned int &first_non_one,
12199 enum c_omp_region_type ort)
12201 tree ret, low_bound, length, type;
12202 if (TREE_CODE (t) != TREE_LIST)
12204 if (error_operand_p (t))
12205 return error_mark_node;
12206 ret = t;
12207 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12208 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12210 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12211 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12212 return error_mark_node;
12214 if (TREE_CODE (t) == COMPONENT_REF
12215 && ort == C_ORT_OMP
12216 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12217 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12218 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12220 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12222 error_at (OMP_CLAUSE_LOCATION (c),
12223 "bit-field %qE in %qs clause",
12224 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12225 return error_mark_node;
12227 while (TREE_CODE (t) == COMPONENT_REF)
12229 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12231 error_at (OMP_CLAUSE_LOCATION (c),
12232 "%qE is a member of a union", t);
12233 return error_mark_node;
12235 t = TREE_OPERAND (t, 0);
12238 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12240 if (DECL_P (t))
12241 error_at (OMP_CLAUSE_LOCATION (c),
12242 "%qD is not a variable in %qs clause", t,
12243 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12244 else
12245 error_at (OMP_CLAUSE_LOCATION (c),
12246 "%qE is not a variable in %qs clause", t,
12247 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12248 return error_mark_node;
12250 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12251 && TYPE_ATOMIC (TREE_TYPE (t)))
12253 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12254 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12255 return error_mark_node;
12257 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12258 && VAR_P (t)
12259 && DECL_THREAD_LOCAL_P (t))
12261 error_at (OMP_CLAUSE_LOCATION (c),
12262 "%qD is threadprivate variable in %qs clause", t,
12263 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12264 return error_mark_node;
12266 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12267 && TYPE_ATOMIC (TREE_TYPE (t))
12268 && POINTER_TYPE_P (TREE_TYPE (t)))
12270 /* If the array section is pointer based and the pointer
12271 itself is _Atomic qualified, we need to atomically load
12272 the pointer. */
12273 c_expr expr;
12274 memset (&expr, 0, sizeof (expr));
12275 expr.value = ret;
12276 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12277 expr, false, false);
12278 ret = expr.value;
12280 return ret;
12283 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12284 maybe_zero_len, first_non_one, ort);
12285 if (ret == error_mark_node || ret == NULL_TREE)
12286 return ret;
12288 type = TREE_TYPE (ret);
12289 low_bound = TREE_PURPOSE (t);
12290 length = TREE_VALUE (t);
12292 if (low_bound == error_mark_node || length == error_mark_node)
12293 return error_mark_node;
12295 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12297 error_at (OMP_CLAUSE_LOCATION (c),
12298 "low bound %qE of array section does not have integral type",
12299 low_bound);
12300 return error_mark_node;
12302 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12304 error_at (OMP_CLAUSE_LOCATION (c),
12305 "length %qE of array section does not have integral type",
12306 length);
12307 return error_mark_node;
12309 if (low_bound
12310 && TREE_CODE (low_bound) == INTEGER_CST
12311 && TYPE_PRECISION (TREE_TYPE (low_bound))
12312 > TYPE_PRECISION (sizetype))
12313 low_bound = fold_convert (sizetype, low_bound);
12314 if (length
12315 && TREE_CODE (length) == INTEGER_CST
12316 && TYPE_PRECISION (TREE_TYPE (length))
12317 > TYPE_PRECISION (sizetype))
12318 length = fold_convert (sizetype, length);
12319 if (low_bound == NULL_TREE)
12320 low_bound = integer_zero_node;
12322 if (length != NULL_TREE)
12324 if (!integer_nonzerop (length))
12326 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12327 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12329 if (integer_zerop (length))
12331 error_at (OMP_CLAUSE_LOCATION (c),
12332 "zero length array section in %qs clause",
12333 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12334 return error_mark_node;
12337 else
12338 maybe_zero_len = true;
12340 if (first_non_one == types.length ()
12341 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12342 first_non_one++;
12344 if (TREE_CODE (type) == ARRAY_TYPE)
12346 if (length == NULL_TREE
12347 && (TYPE_DOMAIN (type) == NULL_TREE
12348 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12350 error_at (OMP_CLAUSE_LOCATION (c),
12351 "for unknown bound array type length expression must "
12352 "be specified");
12353 return error_mark_node;
12355 if (TREE_CODE (low_bound) == INTEGER_CST
12356 && tree_int_cst_sgn (low_bound) == -1)
12358 error_at (OMP_CLAUSE_LOCATION (c),
12359 "negative low bound in array section in %qs clause",
12360 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12361 return error_mark_node;
12363 if (length != NULL_TREE
12364 && TREE_CODE (length) == INTEGER_CST
12365 && tree_int_cst_sgn (length) == -1)
12367 error_at (OMP_CLAUSE_LOCATION (c),
12368 "negative length in array section in %qs clause",
12369 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12370 return error_mark_node;
12372 if (TYPE_DOMAIN (type)
12373 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12374 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12375 == INTEGER_CST)
12377 tree size
12378 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12379 size = size_binop (PLUS_EXPR, size, size_one_node);
12380 if (TREE_CODE (low_bound) == INTEGER_CST)
12382 if (tree_int_cst_lt (size, low_bound))
12384 error_at (OMP_CLAUSE_LOCATION (c),
12385 "low bound %qE above array section size "
12386 "in %qs clause", low_bound,
12387 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12388 return error_mark_node;
12390 if (tree_int_cst_equal (size, low_bound))
12392 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12393 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12395 error_at (OMP_CLAUSE_LOCATION (c),
12396 "zero length array section in %qs clause",
12397 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12398 return error_mark_node;
12400 maybe_zero_len = true;
12402 else if (length == NULL_TREE
12403 && first_non_one == types.length ()
12404 && tree_int_cst_equal
12405 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12406 low_bound))
12407 first_non_one++;
12409 else if (length == NULL_TREE)
12411 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12412 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12413 maybe_zero_len = true;
12414 if (first_non_one == types.length ())
12415 first_non_one++;
12417 if (length && TREE_CODE (length) == INTEGER_CST)
12419 if (tree_int_cst_lt (size, length))
12421 error_at (OMP_CLAUSE_LOCATION (c),
12422 "length %qE above array section size "
12423 "in %qs clause", length,
12424 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12425 return error_mark_node;
12427 if (TREE_CODE (low_bound) == INTEGER_CST)
12429 tree lbpluslen
12430 = size_binop (PLUS_EXPR,
12431 fold_convert (sizetype, low_bound),
12432 fold_convert (sizetype, length));
12433 if (TREE_CODE (lbpluslen) == INTEGER_CST
12434 && tree_int_cst_lt (size, lbpluslen))
12436 error_at (OMP_CLAUSE_LOCATION (c),
12437 "high bound %qE above array section size "
12438 "in %qs clause", lbpluslen,
12439 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12440 return error_mark_node;
12445 else if (length == NULL_TREE)
12447 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12448 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12449 maybe_zero_len = true;
12450 if (first_non_one == types.length ())
12451 first_non_one++;
12454 /* For [lb:] we will need to evaluate lb more than once. */
12455 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12457 tree lb = save_expr (low_bound);
12458 if (lb != low_bound)
12460 TREE_PURPOSE (t) = lb;
12461 low_bound = lb;
12465 else if (TREE_CODE (type) == POINTER_TYPE)
12467 if (length == NULL_TREE)
12469 error_at (OMP_CLAUSE_LOCATION (c),
12470 "for pointer type length expression must be specified");
12471 return error_mark_node;
12473 if (length != NULL_TREE
12474 && TREE_CODE (length) == INTEGER_CST
12475 && tree_int_cst_sgn (length) == -1)
12477 error_at (OMP_CLAUSE_LOCATION (c),
12478 "negative length in array section in %qs clause",
12479 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12480 return error_mark_node;
12482 /* If there is a pointer type anywhere but in the very first
12483 array-section-subscript, the array section can't be contiguous. */
12484 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12485 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12487 error_at (OMP_CLAUSE_LOCATION (c),
12488 "array section is not contiguous in %qs clause",
12489 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12490 return error_mark_node;
12493 else
12495 error_at (OMP_CLAUSE_LOCATION (c),
12496 "%qE does not have pointer or array type", ret);
12497 return error_mark_node;
12499 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12500 types.safe_push (TREE_TYPE (ret));
12501 /* We will need to evaluate lb more than once. */
12502 tree lb = save_expr (low_bound);
12503 if (lb != low_bound)
12505 TREE_PURPOSE (t) = lb;
12506 low_bound = lb;
12508 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12509 return ret;
12512 /* Handle array sections for clause C. */
12514 static bool
12515 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12517 bool maybe_zero_len = false;
12518 unsigned int first_non_one = 0;
12519 auto_vec<tree, 10> types;
12520 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12521 maybe_zero_len, first_non_one,
12522 ort);
12523 if (first == error_mark_node)
12524 return true;
12525 if (first == NULL_TREE)
12526 return false;
12527 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12529 tree t = OMP_CLAUSE_DECL (c);
12530 tree tem = NULL_TREE;
12531 /* Need to evaluate side effects in the length expressions
12532 if any. */
12533 while (TREE_CODE (t) == TREE_LIST)
12535 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12537 if (tem == NULL_TREE)
12538 tem = TREE_VALUE (t);
12539 else
12540 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12541 TREE_VALUE (t), tem);
12543 t = TREE_CHAIN (t);
12545 if (tem)
12546 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12547 first = c_fully_fold (first, false, NULL);
12548 OMP_CLAUSE_DECL (c) = first;
12550 else
12552 unsigned int num = types.length (), i;
12553 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12554 tree condition = NULL_TREE;
12556 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12557 maybe_zero_len = true;
12559 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12560 t = TREE_CHAIN (t))
12562 tree low_bound = TREE_PURPOSE (t);
12563 tree length = TREE_VALUE (t);
12565 i--;
12566 if (low_bound
12567 && TREE_CODE (low_bound) == INTEGER_CST
12568 && TYPE_PRECISION (TREE_TYPE (low_bound))
12569 > TYPE_PRECISION (sizetype))
12570 low_bound = fold_convert (sizetype, low_bound);
12571 if (length
12572 && TREE_CODE (length) == INTEGER_CST
12573 && TYPE_PRECISION (TREE_TYPE (length))
12574 > TYPE_PRECISION (sizetype))
12575 length = fold_convert (sizetype, length);
12576 if (low_bound == NULL_TREE)
12577 low_bound = integer_zero_node;
12578 if (!maybe_zero_len && i > first_non_one)
12580 if (integer_nonzerop (low_bound))
12581 goto do_warn_noncontiguous;
12582 if (length != NULL_TREE
12583 && TREE_CODE (length) == INTEGER_CST
12584 && TYPE_DOMAIN (types[i])
12585 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12586 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12587 == INTEGER_CST)
12589 tree size;
12590 size = size_binop (PLUS_EXPR,
12591 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12592 size_one_node);
12593 if (!tree_int_cst_equal (length, size))
12595 do_warn_noncontiguous:
12596 error_at (OMP_CLAUSE_LOCATION (c),
12597 "array section is not contiguous in %qs "
12598 "clause",
12599 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12600 return true;
12603 if (length != NULL_TREE
12604 && TREE_SIDE_EFFECTS (length))
12606 if (side_effects == NULL_TREE)
12607 side_effects = length;
12608 else
12609 side_effects = build2 (COMPOUND_EXPR,
12610 TREE_TYPE (side_effects),
12611 length, side_effects);
12614 else
12616 tree l;
12618 if (i > first_non_one
12619 && ((length && integer_nonzerop (length))
12620 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12621 continue;
12622 if (length)
12623 l = fold_convert (sizetype, length);
12624 else
12626 l = size_binop (PLUS_EXPR,
12627 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12628 size_one_node);
12629 l = size_binop (MINUS_EXPR, l,
12630 fold_convert (sizetype, low_bound));
12632 if (i > first_non_one)
12634 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12635 size_zero_node);
12636 if (condition == NULL_TREE)
12637 condition = l;
12638 else
12639 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12640 l, condition);
12642 else if (size == NULL_TREE)
12644 size = size_in_bytes (TREE_TYPE (types[i]));
12645 tree eltype = TREE_TYPE (types[num - 1]);
12646 while (TREE_CODE (eltype) == ARRAY_TYPE)
12647 eltype = TREE_TYPE (eltype);
12648 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12650 if (integer_zerop (size)
12651 || integer_zerop (size_in_bytes (eltype)))
12653 error_at (OMP_CLAUSE_LOCATION (c),
12654 "zero length array section in %qs clause",
12655 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12656 return error_mark_node;
12658 size = size_binop (EXACT_DIV_EXPR, size,
12659 size_in_bytes (eltype));
12661 size = size_binop (MULT_EXPR, size, l);
12662 if (condition)
12663 size = fold_build3 (COND_EXPR, sizetype, condition,
12664 size, size_zero_node);
12666 else
12667 size = size_binop (MULT_EXPR, size, l);
12670 if (side_effects)
12671 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12672 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12674 size = size_binop (MINUS_EXPR, size, size_one_node);
12675 size = c_fully_fold (size, false, NULL);
12676 tree index_type = build_index_type (size);
12677 tree eltype = TREE_TYPE (first);
12678 while (TREE_CODE (eltype) == ARRAY_TYPE)
12679 eltype = TREE_TYPE (eltype);
12680 tree type = build_array_type (eltype, index_type);
12681 tree ptype = build_pointer_type (eltype);
12682 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12683 t = build_fold_addr_expr (t);
12684 tree t2 = build_fold_addr_expr (first);
12685 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12686 ptrdiff_type_node, t2);
12687 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12688 ptrdiff_type_node, t2,
12689 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12690 ptrdiff_type_node, t));
12691 t2 = c_fully_fold (t2, false, NULL);
12692 if (tree_fits_shwi_p (t2))
12693 t = build2 (MEM_REF, type, t,
12694 build_int_cst (ptype, tree_to_shwi (t2)));
12695 else
12697 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12698 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12699 TREE_TYPE (t), t, t2);
12700 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12702 OMP_CLAUSE_DECL (c) = t;
12703 return false;
12705 first = c_fully_fold (first, false, NULL);
12706 OMP_CLAUSE_DECL (c) = first;
12707 if (size)
12708 size = c_fully_fold (size, false, NULL);
12709 OMP_CLAUSE_SIZE (c) = size;
12710 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12711 || (TREE_CODE (t) == COMPONENT_REF
12712 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12713 return false;
12714 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12715 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12716 switch (OMP_CLAUSE_MAP_KIND (c))
12718 case GOMP_MAP_ALLOC:
12719 case GOMP_MAP_TO:
12720 case GOMP_MAP_FROM:
12721 case GOMP_MAP_TOFROM:
12722 case GOMP_MAP_ALWAYS_TO:
12723 case GOMP_MAP_ALWAYS_FROM:
12724 case GOMP_MAP_ALWAYS_TOFROM:
12725 case GOMP_MAP_RELEASE:
12726 case GOMP_MAP_DELETE:
12727 case GOMP_MAP_FORCE_TO:
12728 case GOMP_MAP_FORCE_FROM:
12729 case GOMP_MAP_FORCE_TOFROM:
12730 case GOMP_MAP_FORCE_PRESENT:
12731 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12732 break;
12733 default:
12734 break;
12736 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12737 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12738 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12739 else if (TREE_CODE (t) == COMPONENT_REF)
12740 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12741 else
12742 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12743 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12744 && !c_mark_addressable (t))
12745 return false;
12746 OMP_CLAUSE_DECL (c2) = t;
12747 t = build_fold_addr_expr (first);
12748 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12749 tree ptr = OMP_CLAUSE_DECL (c2);
12750 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12751 ptr = build_fold_addr_expr (ptr);
12752 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12753 ptrdiff_type_node, t,
12754 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12755 ptrdiff_type_node, ptr));
12756 t = c_fully_fold (t, false, NULL);
12757 OMP_CLAUSE_SIZE (c2) = t;
12758 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12759 OMP_CLAUSE_CHAIN (c) = c2;
12761 return false;
12764 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12765 an inline call. But, remap
12766 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12767 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12769 static tree
12770 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12771 tree decl, tree placeholder)
12773 copy_body_data id;
12774 hash_map<tree, tree> decl_map;
12776 decl_map.put (omp_decl1, placeholder);
12777 decl_map.put (omp_decl2, decl);
12778 memset (&id, 0, sizeof (id));
12779 id.src_fn = DECL_CONTEXT (omp_decl1);
12780 id.dst_fn = current_function_decl;
12781 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12782 id.decl_map = &decl_map;
12784 id.copy_decl = copy_decl_no_change;
12785 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12786 id.transform_new_cfg = true;
12787 id.transform_return_to_modify = false;
12788 id.transform_lang_insert_block = NULL;
12789 id.eh_lp_nr = 0;
12790 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12791 return stmt;
12794 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12795 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12797 static tree
12798 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12800 if (*tp == (tree) data)
12801 return *tp;
12802 return NULL_TREE;
12805 /* For all elements of CLAUSES, validate them against their constraints.
12806 Remove any elements from the list that are invalid. */
12808 tree
12809 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12811 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12812 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12813 tree c, t, type, *pc;
12814 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12815 bool branch_seen = false;
12816 bool copyprivate_seen = false;
12817 bool linear_variable_step_check = false;
12818 tree *nowait_clause = NULL;
12819 bool ordered_seen = false;
12820 tree schedule_clause = NULL_TREE;
12821 bool oacc_async = false;
12823 bitmap_obstack_initialize (NULL);
12824 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12825 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12826 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12827 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12828 bitmap_initialize (&map_head, &bitmap_default_obstack);
12829 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12830 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12832 if (ort & C_ORT_ACC)
12833 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12834 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12836 oacc_async = true;
12837 break;
12840 for (pc = &clauses, c = clauses; c ; c = *pc)
12842 bool remove = false;
12843 bool need_complete = false;
12844 bool need_implicitly_determined = false;
12846 switch (OMP_CLAUSE_CODE (c))
12848 case OMP_CLAUSE_SHARED:
12849 need_implicitly_determined = true;
12850 goto check_dup_generic;
12852 case OMP_CLAUSE_PRIVATE:
12853 need_complete = true;
12854 need_implicitly_determined = true;
12855 goto check_dup_generic;
12857 case OMP_CLAUSE_REDUCTION:
12858 need_implicitly_determined = true;
12859 t = OMP_CLAUSE_DECL (c);
12860 if (TREE_CODE (t) == TREE_LIST)
12862 if (handle_omp_array_sections (c, ort))
12864 remove = true;
12865 break;
12868 t = OMP_CLAUSE_DECL (c);
12870 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12871 if (t == error_mark_node)
12873 remove = true;
12874 break;
12876 if (oacc_async)
12877 c_mark_addressable (t);
12878 type = TREE_TYPE (t);
12879 if (TREE_CODE (t) == MEM_REF)
12880 type = TREE_TYPE (type);
12881 if (TREE_CODE (type) == ARRAY_TYPE)
12883 tree oatype = type;
12884 gcc_assert (TREE_CODE (t) != MEM_REF);
12885 while (TREE_CODE (type) == ARRAY_TYPE)
12886 type = TREE_TYPE (type);
12887 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12889 error_at (OMP_CLAUSE_LOCATION (c),
12890 "%qD in %<reduction%> clause is a zero size array",
12892 remove = true;
12893 break;
12895 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12896 TYPE_SIZE_UNIT (type));
12897 if (integer_zerop (size))
12899 error_at (OMP_CLAUSE_LOCATION (c),
12900 "%qD in %<reduction%> clause is a zero size array",
12902 remove = true;
12903 break;
12905 size = size_binop (MINUS_EXPR, size, size_one_node);
12906 tree index_type = build_index_type (size);
12907 tree atype = build_array_type (type, index_type);
12908 tree ptype = build_pointer_type (type);
12909 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12910 t = build_fold_addr_expr (t);
12911 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12912 OMP_CLAUSE_DECL (c) = t;
12914 if (TYPE_ATOMIC (type))
12916 error_at (OMP_CLAUSE_LOCATION (c),
12917 "%<_Atomic%> %qE in %<reduction%> clause", t);
12918 remove = true;
12919 break;
12921 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12922 && (FLOAT_TYPE_P (type)
12923 || TREE_CODE (type) == COMPLEX_TYPE))
12925 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12926 const char *r_name = NULL;
12928 switch (r_code)
12930 case PLUS_EXPR:
12931 case MULT_EXPR:
12932 case MINUS_EXPR:
12933 break;
12934 case MIN_EXPR:
12935 if (TREE_CODE (type) == COMPLEX_TYPE)
12936 r_name = "min";
12937 break;
12938 case MAX_EXPR:
12939 if (TREE_CODE (type) == COMPLEX_TYPE)
12940 r_name = "max";
12941 break;
12942 case BIT_AND_EXPR:
12943 r_name = "&";
12944 break;
12945 case BIT_XOR_EXPR:
12946 r_name = "^";
12947 break;
12948 case BIT_IOR_EXPR:
12949 r_name = "|";
12950 break;
12951 case TRUTH_ANDIF_EXPR:
12952 if (FLOAT_TYPE_P (type))
12953 r_name = "&&";
12954 break;
12955 case TRUTH_ORIF_EXPR:
12956 if (FLOAT_TYPE_P (type))
12957 r_name = "||";
12958 break;
12959 default:
12960 gcc_unreachable ();
12962 if (r_name)
12964 error_at (OMP_CLAUSE_LOCATION (c),
12965 "%qE has invalid type for %<reduction(%s)%>",
12966 t, r_name);
12967 remove = true;
12968 break;
12971 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12973 error_at (OMP_CLAUSE_LOCATION (c),
12974 "user defined reduction not found for %qE", t);
12975 remove = true;
12976 break;
12978 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12980 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12981 type = TYPE_MAIN_VARIANT (type);
12982 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12983 VAR_DECL, NULL_TREE, type);
12984 tree decl_placeholder = NULL_TREE;
12985 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12986 DECL_ARTIFICIAL (placeholder) = 1;
12987 DECL_IGNORED_P (placeholder) = 1;
12988 if (TREE_CODE (t) == MEM_REF)
12990 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12991 VAR_DECL, NULL_TREE, type);
12992 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12993 DECL_ARTIFICIAL (decl_placeholder) = 1;
12994 DECL_IGNORED_P (decl_placeholder) = 1;
12996 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12997 c_mark_addressable (placeholder);
12998 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12999 c_mark_addressable (decl_placeholder ? decl_placeholder
13000 : OMP_CLAUSE_DECL (c));
13001 OMP_CLAUSE_REDUCTION_MERGE (c)
13002 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13003 TREE_VEC_ELT (list, 0),
13004 TREE_VEC_ELT (list, 1),
13005 decl_placeholder ? decl_placeholder
13006 : OMP_CLAUSE_DECL (c), placeholder);
13007 OMP_CLAUSE_REDUCTION_MERGE (c)
13008 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13009 void_type_node, NULL_TREE,
13010 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13011 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13012 if (TREE_VEC_LENGTH (list) == 6)
13014 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13015 c_mark_addressable (decl_placeholder ? decl_placeholder
13016 : OMP_CLAUSE_DECL (c));
13017 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13018 c_mark_addressable (placeholder);
13019 tree init = TREE_VEC_ELT (list, 5);
13020 if (init == error_mark_node)
13021 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13022 OMP_CLAUSE_REDUCTION_INIT (c)
13023 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13024 TREE_VEC_ELT (list, 3),
13025 decl_placeholder ? decl_placeholder
13026 : OMP_CLAUSE_DECL (c), placeholder);
13027 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13029 tree v = decl_placeholder ? decl_placeholder : t;
13030 OMP_CLAUSE_REDUCTION_INIT (c)
13031 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13032 OMP_CLAUSE_REDUCTION_INIT (c));
13034 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13035 c_find_omp_placeholder_r,
13036 placeholder, NULL))
13037 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13039 else
13041 tree init;
13042 tree v = decl_placeholder ? decl_placeholder : t;
13043 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13044 init = build_constructor (TREE_TYPE (v), NULL);
13045 else
13046 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13047 OMP_CLAUSE_REDUCTION_INIT (c)
13048 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13050 OMP_CLAUSE_REDUCTION_INIT (c)
13051 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13052 void_type_node, NULL_TREE,
13053 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13054 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13056 if (TREE_CODE (t) == MEM_REF)
13058 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13059 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13060 != INTEGER_CST)
13062 sorry ("variable length element type in array "
13063 "%<reduction%> clause");
13064 remove = true;
13065 break;
13067 t = TREE_OPERAND (t, 0);
13068 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13069 t = TREE_OPERAND (t, 0);
13070 if (TREE_CODE (t) == ADDR_EXPR)
13071 t = TREE_OPERAND (t, 0);
13073 goto check_dup_generic_t;
13075 case OMP_CLAUSE_COPYPRIVATE:
13076 copyprivate_seen = true;
13077 if (nowait_clause)
13079 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13080 "%<nowait%> clause must not be used together "
13081 "with %<copyprivate%>");
13082 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13083 nowait_clause = NULL;
13085 goto check_dup_generic;
13087 case OMP_CLAUSE_COPYIN:
13088 t = OMP_CLAUSE_DECL (c);
13089 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13091 error_at (OMP_CLAUSE_LOCATION (c),
13092 "%qE must be %<threadprivate%> for %<copyin%>", t);
13093 remove = true;
13094 break;
13096 goto check_dup_generic;
13098 case OMP_CLAUSE_LINEAR:
13099 if (ort != C_ORT_OMP_DECLARE_SIMD)
13100 need_implicitly_determined = true;
13101 t = OMP_CLAUSE_DECL (c);
13102 if (ort != C_ORT_OMP_DECLARE_SIMD
13103 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13105 error_at (OMP_CLAUSE_LOCATION (c),
13106 "modifier should not be specified in %<linear%> "
13107 "clause on %<simd%> or %<for%> constructs");
13108 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13110 if (ort & C_ORT_CILK)
13112 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13113 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13114 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13116 error_at (OMP_CLAUSE_LOCATION (c),
13117 "linear clause applied to non-integral, "
13118 "non-floating, non-pointer variable with type %qT",
13119 TREE_TYPE (t));
13120 remove = true;
13121 break;
13124 else
13126 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13127 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13129 error_at (OMP_CLAUSE_LOCATION (c),
13130 "linear clause applied to non-integral non-pointer "
13131 "variable with type %qT", TREE_TYPE (t));
13132 remove = true;
13133 break;
13135 if (TYPE_ATOMIC (TREE_TYPE (t)))
13137 error_at (OMP_CLAUSE_LOCATION (c),
13138 "%<_Atomic%> %qD in %<linear%> clause", t);
13139 remove = true;
13140 break;
13143 if (ort == C_ORT_OMP_DECLARE_SIMD)
13145 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13146 if (TREE_CODE (s) == PARM_DECL)
13148 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13149 /* map_head bitmap is used as uniform_head if
13150 declare_simd. */
13151 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13152 linear_variable_step_check = true;
13153 goto check_dup_generic;
13155 if (TREE_CODE (s) != INTEGER_CST)
13157 error_at (OMP_CLAUSE_LOCATION (c),
13158 "%<linear%> clause step %qE is neither constant "
13159 "nor a parameter", s);
13160 remove = true;
13161 break;
13164 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13166 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13167 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13168 OMP_CLAUSE_DECL (c), s);
13169 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13170 sizetype, fold_convert (sizetype, s),
13171 fold_convert
13172 (sizetype, OMP_CLAUSE_DECL (c)));
13173 if (s == error_mark_node)
13174 s = size_one_node;
13175 OMP_CLAUSE_LINEAR_STEP (c) = s;
13177 else
13178 OMP_CLAUSE_LINEAR_STEP (c)
13179 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13180 goto check_dup_generic;
13182 check_dup_generic:
13183 t = OMP_CLAUSE_DECL (c);
13184 check_dup_generic_t:
13185 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13187 error_at (OMP_CLAUSE_LOCATION (c),
13188 "%qE is not a variable in clause %qs", t,
13189 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13190 remove = true;
13192 else if (ort == C_ORT_ACC
13193 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13195 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13197 error ("%qD appears more than once in reduction clauses", t);
13198 remove = true;
13200 else
13201 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13203 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13204 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13205 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13207 error_at (OMP_CLAUSE_LOCATION (c),
13208 "%qE appears more than once in data clauses", t);
13209 remove = true;
13211 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13212 && bitmap_bit_p (&map_head, DECL_UID (t)))
13214 if (ort == C_ORT_ACC)
13215 error ("%qD appears more than once in data clauses", t);
13216 else
13217 error ("%qD appears both in data and map clauses", t);
13218 remove = true;
13220 else
13221 bitmap_set_bit (&generic_head, DECL_UID (t));
13222 break;
13224 case OMP_CLAUSE_FIRSTPRIVATE:
13225 t = OMP_CLAUSE_DECL (c);
13226 need_complete = true;
13227 need_implicitly_determined = true;
13228 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13230 error_at (OMP_CLAUSE_LOCATION (c),
13231 "%qE is not a variable in clause %<firstprivate%>", t);
13232 remove = true;
13234 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13235 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13237 error_at (OMP_CLAUSE_LOCATION (c),
13238 "%qE appears more than once in data clauses", t);
13239 remove = true;
13241 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13243 if (ort == C_ORT_ACC)
13244 error ("%qD appears more than once in data clauses", t);
13245 else
13246 error ("%qD appears both in data and map clauses", t);
13247 remove = true;
13249 else
13250 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13251 break;
13253 case OMP_CLAUSE_LASTPRIVATE:
13254 t = OMP_CLAUSE_DECL (c);
13255 need_complete = true;
13256 need_implicitly_determined = true;
13257 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13259 error_at (OMP_CLAUSE_LOCATION (c),
13260 "%qE is not a variable in clause %<lastprivate%>", t);
13261 remove = true;
13263 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13264 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13266 error_at (OMP_CLAUSE_LOCATION (c),
13267 "%qE appears more than once in data clauses", t);
13268 remove = true;
13270 else
13271 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13272 break;
13274 case OMP_CLAUSE_ALIGNED:
13275 t = OMP_CLAUSE_DECL (c);
13276 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13278 error_at (OMP_CLAUSE_LOCATION (c),
13279 "%qE is not a variable in %<aligned%> clause", t);
13280 remove = true;
13282 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13283 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13285 error_at (OMP_CLAUSE_LOCATION (c),
13286 "%qE in %<aligned%> clause is neither a pointer nor "
13287 "an array", t);
13288 remove = true;
13290 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13292 error_at (OMP_CLAUSE_LOCATION (c),
13293 "%<_Atomic%> %qD in %<aligned%> clause", t);
13294 remove = true;
13295 break;
13297 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13299 error_at (OMP_CLAUSE_LOCATION (c),
13300 "%qE appears more than once in %<aligned%> clauses",
13302 remove = true;
13304 else
13305 bitmap_set_bit (&aligned_head, DECL_UID (t));
13306 break;
13308 case OMP_CLAUSE_DEPEND:
13309 t = OMP_CLAUSE_DECL (c);
13310 if (t == NULL_TREE)
13312 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13313 == OMP_CLAUSE_DEPEND_SOURCE);
13314 break;
13316 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13318 gcc_assert (TREE_CODE (t) == TREE_LIST);
13319 for (; t; t = TREE_CHAIN (t))
13321 tree decl = TREE_VALUE (t);
13322 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13324 tree offset = TREE_PURPOSE (t);
13325 bool neg = wi::neg_p ((wide_int) offset);
13326 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13327 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13328 neg ? MINUS_EXPR : PLUS_EXPR,
13329 decl, offset);
13330 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13331 sizetype,
13332 fold_convert (sizetype, t2),
13333 fold_convert (sizetype, decl));
13334 if (t2 == error_mark_node)
13336 remove = true;
13337 break;
13339 TREE_PURPOSE (t) = t2;
13342 break;
13344 if (TREE_CODE (t) == TREE_LIST)
13346 if (handle_omp_array_sections (c, ort))
13347 remove = true;
13348 break;
13350 if (t == error_mark_node)
13351 remove = true;
13352 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13354 error_at (OMP_CLAUSE_LOCATION (c),
13355 "%qE is not a variable in %<depend%> clause", t);
13356 remove = true;
13358 else if (!c_mark_addressable (t))
13359 remove = true;
13360 break;
13362 case OMP_CLAUSE_MAP:
13363 case OMP_CLAUSE_TO:
13364 case OMP_CLAUSE_FROM:
13365 case OMP_CLAUSE__CACHE_:
13366 t = OMP_CLAUSE_DECL (c);
13367 if (TREE_CODE (t) == TREE_LIST)
13369 if (handle_omp_array_sections (c, ort))
13370 remove = true;
13371 else
13373 t = OMP_CLAUSE_DECL (c);
13374 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13376 error_at (OMP_CLAUSE_LOCATION (c),
13377 "array section does not have mappable type "
13378 "in %qs clause",
13379 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13380 remove = true;
13382 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13384 error_at (OMP_CLAUSE_LOCATION (c),
13385 "%<_Atomic%> %qE in %qs clause", t,
13386 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13387 remove = true;
13389 while (TREE_CODE (t) == ARRAY_REF)
13390 t = TREE_OPERAND (t, 0);
13391 if (TREE_CODE (t) == COMPONENT_REF
13392 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13394 while (TREE_CODE (t) == COMPONENT_REF)
13395 t = TREE_OPERAND (t, 0);
13396 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13397 break;
13398 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13400 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13401 error ("%qD appears more than once in motion"
13402 " clauses", t);
13403 else if (ort == C_ORT_ACC)
13404 error ("%qD appears more than once in data"
13405 " clauses", t);
13406 else
13407 error ("%qD appears more than once in map"
13408 " clauses", t);
13409 remove = true;
13411 else
13413 bitmap_set_bit (&map_head, DECL_UID (t));
13414 bitmap_set_bit (&map_field_head, DECL_UID (t));
13418 break;
13420 if (t == error_mark_node)
13422 remove = true;
13423 break;
13425 if (TREE_CODE (t) == COMPONENT_REF
13426 && (ort & C_ORT_OMP)
13427 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13429 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13431 error_at (OMP_CLAUSE_LOCATION (c),
13432 "bit-field %qE in %qs clause",
13433 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13434 remove = true;
13436 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13438 error_at (OMP_CLAUSE_LOCATION (c),
13439 "%qE does not have a mappable type in %qs clause",
13440 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13441 remove = true;
13443 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13445 error_at (OMP_CLAUSE_LOCATION (c),
13446 "%<_Atomic%> %qE in %qs clause", t,
13447 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13448 remove = true;
13450 while (TREE_CODE (t) == COMPONENT_REF)
13452 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13453 == UNION_TYPE)
13455 error_at (OMP_CLAUSE_LOCATION (c),
13456 "%qE is a member of a union", t);
13457 remove = true;
13458 break;
13460 t = TREE_OPERAND (t, 0);
13462 if (remove)
13463 break;
13464 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13466 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13467 break;
13470 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13472 error_at (OMP_CLAUSE_LOCATION (c),
13473 "%qE is not a variable in %qs clause", t,
13474 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13475 remove = true;
13477 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13479 error_at (OMP_CLAUSE_LOCATION (c),
13480 "%qD is threadprivate variable in %qs clause", t,
13481 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13482 remove = true;
13484 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13485 || (OMP_CLAUSE_MAP_KIND (c)
13486 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13487 && !c_mark_addressable (t))
13488 remove = true;
13489 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13490 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13491 || (OMP_CLAUSE_MAP_KIND (c)
13492 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13493 || (OMP_CLAUSE_MAP_KIND (c)
13494 == GOMP_MAP_FORCE_DEVICEPTR)))
13495 && t == OMP_CLAUSE_DECL (c)
13496 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13498 error_at (OMP_CLAUSE_LOCATION (c),
13499 "%qD does not have a mappable type in %qs clause", t,
13500 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13501 remove = true;
13503 else if (TREE_TYPE (t) == error_mark_node)
13504 remove = true;
13505 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13507 error_at (OMP_CLAUSE_LOCATION (c),
13508 "%<_Atomic%> %qE in %qs clause", t,
13509 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13510 remove = true;
13512 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13513 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13515 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13516 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13518 error ("%qD appears more than once in data clauses", t);
13519 remove = true;
13521 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13523 if (ort == C_ORT_ACC)
13524 error ("%qD appears more than once in data clauses", t);
13525 else
13526 error ("%qD appears both in data and map clauses", t);
13527 remove = true;
13529 else
13530 bitmap_set_bit (&generic_head, DECL_UID (t));
13532 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13534 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13535 error ("%qD appears more than once in motion clauses", t);
13536 else if (ort == C_ORT_ACC)
13537 error ("%qD appears more than once in data clauses", t);
13538 else
13539 error ("%qD appears more than once in map clauses", t);
13540 remove = true;
13542 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13543 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13545 if (ort == C_ORT_ACC)
13546 error ("%qD appears more than once in data clauses", t);
13547 else
13548 error ("%qD appears both in data and map clauses", t);
13549 remove = true;
13551 else
13553 bitmap_set_bit (&map_head, DECL_UID (t));
13554 if (t != OMP_CLAUSE_DECL (c)
13555 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13556 bitmap_set_bit (&map_field_head, DECL_UID (t));
13558 break;
13560 case OMP_CLAUSE_TO_DECLARE:
13561 case OMP_CLAUSE_LINK:
13562 t = OMP_CLAUSE_DECL (c);
13563 if (TREE_CODE (t) == FUNCTION_DECL
13564 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13566 else if (!VAR_P (t))
13568 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13569 error_at (OMP_CLAUSE_LOCATION (c),
13570 "%qE is neither a variable nor a function name in "
13571 "clause %qs", t,
13572 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13573 else
13574 error_at (OMP_CLAUSE_LOCATION (c),
13575 "%qE is not a variable in clause %qs", t,
13576 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13577 remove = true;
13579 else if (DECL_THREAD_LOCAL_P (t))
13581 error_at (OMP_CLAUSE_LOCATION (c),
13582 "%qD is threadprivate variable in %qs clause", t,
13583 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13584 remove = true;
13586 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13588 error_at (OMP_CLAUSE_LOCATION (c),
13589 "%qD does not have a mappable type in %qs clause", t,
13590 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13591 remove = true;
13593 if (remove)
13594 break;
13595 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13597 error_at (OMP_CLAUSE_LOCATION (c),
13598 "%qE appears more than once on the same "
13599 "%<declare target%> directive", t);
13600 remove = true;
13602 else
13603 bitmap_set_bit (&generic_head, DECL_UID (t));
13604 break;
13606 case OMP_CLAUSE_UNIFORM:
13607 t = OMP_CLAUSE_DECL (c);
13608 if (TREE_CODE (t) != PARM_DECL)
13610 if (DECL_P (t))
13611 error_at (OMP_CLAUSE_LOCATION (c),
13612 "%qD is not an argument in %<uniform%> clause", t);
13613 else
13614 error_at (OMP_CLAUSE_LOCATION (c),
13615 "%qE is not an argument in %<uniform%> clause", t);
13616 remove = true;
13617 break;
13619 /* map_head bitmap is used as uniform_head if declare_simd. */
13620 bitmap_set_bit (&map_head, DECL_UID (t));
13621 goto check_dup_generic;
13623 case OMP_CLAUSE_IS_DEVICE_PTR:
13624 case OMP_CLAUSE_USE_DEVICE_PTR:
13625 t = OMP_CLAUSE_DECL (c);
13626 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13627 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13629 error_at (OMP_CLAUSE_LOCATION (c),
13630 "%qs variable is neither a pointer nor an array",
13631 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13632 remove = true;
13634 goto check_dup_generic;
13636 case OMP_CLAUSE_NOWAIT:
13637 if (copyprivate_seen)
13639 error_at (OMP_CLAUSE_LOCATION (c),
13640 "%<nowait%> clause must not be used together "
13641 "with %<copyprivate%>");
13642 remove = true;
13643 break;
13645 nowait_clause = pc;
13646 pc = &OMP_CLAUSE_CHAIN (c);
13647 continue;
13649 case OMP_CLAUSE_IF:
13650 case OMP_CLAUSE_NUM_THREADS:
13651 case OMP_CLAUSE_NUM_TEAMS:
13652 case OMP_CLAUSE_THREAD_LIMIT:
13653 case OMP_CLAUSE_DEFAULT:
13654 case OMP_CLAUSE_UNTIED:
13655 case OMP_CLAUSE_COLLAPSE:
13656 case OMP_CLAUSE_FINAL:
13657 case OMP_CLAUSE_MERGEABLE:
13658 case OMP_CLAUSE_DEVICE:
13659 case OMP_CLAUSE_DIST_SCHEDULE:
13660 case OMP_CLAUSE_PARALLEL:
13661 case OMP_CLAUSE_FOR:
13662 case OMP_CLAUSE_SECTIONS:
13663 case OMP_CLAUSE_TASKGROUP:
13664 case OMP_CLAUSE_PROC_BIND:
13665 case OMP_CLAUSE_PRIORITY:
13666 case OMP_CLAUSE_GRAINSIZE:
13667 case OMP_CLAUSE_NUM_TASKS:
13668 case OMP_CLAUSE_NOGROUP:
13669 case OMP_CLAUSE_THREADS:
13670 case OMP_CLAUSE_SIMD:
13671 case OMP_CLAUSE_HINT:
13672 case OMP_CLAUSE_DEFAULTMAP:
13673 case OMP_CLAUSE__CILK_FOR_COUNT_:
13674 case OMP_CLAUSE_NUM_GANGS:
13675 case OMP_CLAUSE_NUM_WORKERS:
13676 case OMP_CLAUSE_VECTOR_LENGTH:
13677 case OMP_CLAUSE_ASYNC:
13678 case OMP_CLAUSE_WAIT:
13679 case OMP_CLAUSE_AUTO:
13680 case OMP_CLAUSE_INDEPENDENT:
13681 case OMP_CLAUSE_SEQ:
13682 case OMP_CLAUSE_GANG:
13683 case OMP_CLAUSE_WORKER:
13684 case OMP_CLAUSE_VECTOR:
13685 case OMP_CLAUSE_TILE:
13686 pc = &OMP_CLAUSE_CHAIN (c);
13687 continue;
13689 case OMP_CLAUSE_SCHEDULE:
13690 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13692 const char *p = NULL;
13693 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13695 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13696 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13697 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13698 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13699 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13700 default: gcc_unreachable ();
13702 if (p)
13704 error_at (OMP_CLAUSE_LOCATION (c),
13705 "%<nonmonotonic%> modifier specified for %qs "
13706 "schedule kind", p);
13707 OMP_CLAUSE_SCHEDULE_KIND (c)
13708 = (enum omp_clause_schedule_kind)
13709 (OMP_CLAUSE_SCHEDULE_KIND (c)
13710 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13713 schedule_clause = c;
13714 pc = &OMP_CLAUSE_CHAIN (c);
13715 continue;
13717 case OMP_CLAUSE_ORDERED:
13718 ordered_seen = true;
13719 pc = &OMP_CLAUSE_CHAIN (c);
13720 continue;
13722 case OMP_CLAUSE_SAFELEN:
13723 safelen = c;
13724 pc = &OMP_CLAUSE_CHAIN (c);
13725 continue;
13726 case OMP_CLAUSE_SIMDLEN:
13727 simdlen = c;
13728 pc = &OMP_CLAUSE_CHAIN (c);
13729 continue;
13731 case OMP_CLAUSE_INBRANCH:
13732 case OMP_CLAUSE_NOTINBRANCH:
13733 if (branch_seen)
13735 error_at (OMP_CLAUSE_LOCATION (c),
13736 "%<inbranch%> clause is incompatible with "
13737 "%<notinbranch%>");
13738 remove = true;
13739 break;
13741 branch_seen = true;
13742 pc = &OMP_CLAUSE_CHAIN (c);
13743 continue;
13745 default:
13746 gcc_unreachable ();
13749 if (!remove)
13751 t = OMP_CLAUSE_DECL (c);
13753 if (need_complete)
13755 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13756 if (t == error_mark_node)
13757 remove = true;
13760 if (need_implicitly_determined)
13762 const char *share_name = NULL;
13764 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13765 share_name = "threadprivate";
13766 else switch (c_omp_predetermined_sharing (t))
13768 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13769 break;
13770 case OMP_CLAUSE_DEFAULT_SHARED:
13771 /* const vars may be specified in firstprivate clause. */
13772 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13773 && TREE_READONLY (t))
13774 break;
13775 share_name = "shared";
13776 break;
13777 case OMP_CLAUSE_DEFAULT_PRIVATE:
13778 share_name = "private";
13779 break;
13780 default:
13781 gcc_unreachable ();
13783 if (share_name)
13785 error_at (OMP_CLAUSE_LOCATION (c),
13786 "%qE is predetermined %qs for %qs",
13787 t, share_name,
13788 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13789 remove = true;
13794 if (remove)
13795 *pc = OMP_CLAUSE_CHAIN (c);
13796 else
13797 pc = &OMP_CLAUSE_CHAIN (c);
13800 if (simdlen
13801 && safelen
13802 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13803 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13805 error_at (OMP_CLAUSE_LOCATION (simdlen),
13806 "%<simdlen%> clause value is bigger than "
13807 "%<safelen%> clause value");
13808 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13809 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13812 if (ordered_seen
13813 && schedule_clause
13814 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13815 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13817 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13818 "%<nonmonotonic%> schedule modifier specified together "
13819 "with %<ordered%> clause");
13820 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13821 = (enum omp_clause_schedule_kind)
13822 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13823 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13826 if (linear_variable_step_check)
13827 for (pc = &clauses, c = clauses; c ; c = *pc)
13829 bool remove = false;
13830 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13831 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13832 && !bitmap_bit_p (&map_head,
13833 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13835 error_at (OMP_CLAUSE_LOCATION (c),
13836 "%<linear%> clause step is a parameter %qD not "
13837 "specified in %<uniform%> clause",
13838 OMP_CLAUSE_LINEAR_STEP (c));
13839 remove = true;
13842 if (remove)
13843 *pc = OMP_CLAUSE_CHAIN (c);
13844 else
13845 pc = &OMP_CLAUSE_CHAIN (c);
13848 bitmap_obstack_release (NULL);
13849 return clauses;
13852 /* Return code to initialize DST with a copy constructor from SRC.
13853 C doesn't have copy constructors nor assignment operators, only for
13854 _Atomic vars we need to perform __atomic_load from src into a temporary
13855 followed by __atomic_store of the temporary to dst. */
13857 tree
13858 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13860 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13861 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13863 location_t loc = OMP_CLAUSE_LOCATION (clause);
13864 tree type = TREE_TYPE (dst);
13865 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13866 tree tmp = create_tmp_var (nonatomic_type);
13867 tree tmp_addr = build_fold_addr_expr (tmp);
13868 TREE_ADDRESSABLE (tmp) = 1;
13869 TREE_NO_WARNING (tmp) = 1;
13870 tree src_addr = build_fold_addr_expr (src);
13871 tree dst_addr = build_fold_addr_expr (dst);
13872 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13873 vec<tree, va_gc> *params;
13874 /* Expansion of a generic atomic load may require an addition
13875 element, so allocate enough to prevent a resize. */
13876 vec_alloc (params, 4);
13878 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13879 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13880 params->quick_push (src_addr);
13881 params->quick_push (tmp_addr);
13882 params->quick_push (seq_cst);
13883 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13885 vec_alloc (params, 4);
13887 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13888 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13889 params->quick_push (dst_addr);
13890 params->quick_push (tmp_addr);
13891 params->quick_push (seq_cst);
13892 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13893 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13896 /* Create a transaction node. */
13898 tree
13899 c_finish_transaction (location_t loc, tree block, int flags)
13901 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13902 if (flags & TM_STMT_ATTR_OUTER)
13903 TRANSACTION_EXPR_OUTER (stmt) = 1;
13904 if (flags & TM_STMT_ATTR_RELAXED)
13905 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13906 return add_stmt (stmt);
13909 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13910 down to the element type of an array. If ORIG_QUAL_TYPE is not
13911 NULL, then it should be used as the qualified type
13912 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13913 preserve information about the typedef name from which an array
13914 type was derived). */
13916 tree
13917 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13918 size_t orig_qual_indirect)
13920 if (type == error_mark_node)
13921 return type;
13923 if (TREE_CODE (type) == ARRAY_TYPE)
13925 tree t;
13926 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13927 type_quals, orig_qual_type,
13928 orig_qual_indirect - 1);
13930 /* See if we already have an identically qualified type. */
13931 if (orig_qual_type && orig_qual_indirect == 0)
13932 t = orig_qual_type;
13933 else
13934 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13936 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13937 && TYPE_NAME (t) == TYPE_NAME (type)
13938 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13939 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13940 TYPE_ATTRIBUTES (type)))
13941 break;
13943 if (!t)
13945 tree domain = TYPE_DOMAIN (type);
13947 t = build_variant_type_copy (type);
13948 TREE_TYPE (t) = element_type;
13950 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13951 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13952 SET_TYPE_STRUCTURAL_EQUALITY (t);
13953 else if (TYPE_CANONICAL (element_type) != element_type
13954 || (domain && TYPE_CANONICAL (domain) != domain))
13956 tree unqualified_canon
13957 = build_array_type (TYPE_CANONICAL (element_type),
13958 domain? TYPE_CANONICAL (domain)
13959 : NULL_TREE);
13960 if (TYPE_REVERSE_STORAGE_ORDER (type))
13962 unqualified_canon
13963 = build_distinct_type_copy (unqualified_canon);
13964 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13966 TYPE_CANONICAL (t)
13967 = c_build_qualified_type (unqualified_canon, type_quals);
13969 else
13970 TYPE_CANONICAL (t) = t;
13972 return t;
13975 /* A restrict-qualified pointer type must be a pointer to object or
13976 incomplete type. Note that the use of POINTER_TYPE_P also allows
13977 REFERENCE_TYPEs, which is appropriate for C++. */
13978 if ((type_quals & TYPE_QUAL_RESTRICT)
13979 && (!POINTER_TYPE_P (type)
13980 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13982 error ("invalid use of %<restrict%>");
13983 type_quals &= ~TYPE_QUAL_RESTRICT;
13986 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13987 ? orig_qual_type
13988 : build_qualified_type (type, type_quals));
13989 /* A variant type does not inherit the list of incomplete vars from the
13990 type main variant. */
13991 if (RECORD_OR_UNION_TYPE_P (var_type)
13992 && TYPE_MAIN_VARIANT (var_type) != var_type)
13993 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13994 return var_type;
13997 /* Build a VA_ARG_EXPR for the C parser. */
13999 tree
14000 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14002 if (error_operand_p (type))
14003 return error_mark_node;
14004 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14005 order because it takes the address of the expression. */
14006 else if (handled_component_p (expr)
14007 && reverse_storage_order_for_component_p (expr))
14009 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14010 return error_mark_node;
14012 else if (!COMPLETE_TYPE_P (type))
14014 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14015 "type %qT", type);
14016 return error_mark_node;
14018 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14019 warning_at (loc2, OPT_Wc___compat,
14020 "C++ requires promoted type, not enum type, in %<va_arg%>");
14021 return build_va_arg (loc2, expr, type);
14024 /* Return truthvalue of whether T1 is the same tree structure as T2.
14025 Return 1 if they are the same. Return false if they are different. */
14027 bool
14028 c_tree_equal (tree t1, tree t2)
14030 enum tree_code code1, code2;
14032 if (t1 == t2)
14033 return true;
14034 if (!t1 || !t2)
14035 return false;
14037 for (code1 = TREE_CODE (t1);
14038 CONVERT_EXPR_CODE_P (code1)
14039 || code1 == NON_LVALUE_EXPR;
14040 code1 = TREE_CODE (t1))
14041 t1 = TREE_OPERAND (t1, 0);
14042 for (code2 = TREE_CODE (t2);
14043 CONVERT_EXPR_CODE_P (code2)
14044 || code2 == NON_LVALUE_EXPR;
14045 code2 = TREE_CODE (t2))
14046 t2 = TREE_OPERAND (t2, 0);
14048 /* They might have become equal now. */
14049 if (t1 == t2)
14050 return true;
14052 if (code1 != code2)
14053 return false;
14055 switch (code1)
14057 case INTEGER_CST:
14058 return wi::eq_p (t1, t2);
14060 case REAL_CST:
14061 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14063 case STRING_CST:
14064 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14065 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14066 TREE_STRING_LENGTH (t1));
14068 case FIXED_CST:
14069 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14070 TREE_FIXED_CST (t2));
14072 case COMPLEX_CST:
14073 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14074 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14076 case VECTOR_CST:
14077 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14079 case CONSTRUCTOR:
14080 /* We need to do this when determining whether or not two
14081 non-type pointer to member function template arguments
14082 are the same. */
14083 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14084 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14085 return false;
14087 tree field, value;
14088 unsigned int i;
14089 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14091 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14092 if (!c_tree_equal (field, elt2->index)
14093 || !c_tree_equal (value, elt2->value))
14094 return false;
14097 return true;
14099 case TREE_LIST:
14100 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14101 return false;
14102 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14103 return false;
14104 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14106 case SAVE_EXPR:
14107 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14109 case CALL_EXPR:
14111 tree arg1, arg2;
14112 call_expr_arg_iterator iter1, iter2;
14113 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14114 return false;
14115 for (arg1 = first_call_expr_arg (t1, &iter1),
14116 arg2 = first_call_expr_arg (t2, &iter2);
14117 arg1 && arg2;
14118 arg1 = next_call_expr_arg (&iter1),
14119 arg2 = next_call_expr_arg (&iter2))
14120 if (!c_tree_equal (arg1, arg2))
14121 return false;
14122 if (arg1 || arg2)
14123 return false;
14124 return true;
14127 case TARGET_EXPR:
14129 tree o1 = TREE_OPERAND (t1, 0);
14130 tree o2 = TREE_OPERAND (t2, 0);
14132 /* Special case: if either target is an unallocated VAR_DECL,
14133 it means that it's going to be unified with whatever the
14134 TARGET_EXPR is really supposed to initialize, so treat it
14135 as being equivalent to anything. */
14136 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14137 && !DECL_RTL_SET_P (o1))
14138 /*Nop*/;
14139 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14140 && !DECL_RTL_SET_P (o2))
14141 /*Nop*/;
14142 else if (!c_tree_equal (o1, o2))
14143 return false;
14145 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14148 case COMPONENT_REF:
14149 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14150 return false;
14151 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14153 case PARM_DECL:
14154 case VAR_DECL:
14155 case CONST_DECL:
14156 case FIELD_DECL:
14157 case FUNCTION_DECL:
14158 case IDENTIFIER_NODE:
14159 case SSA_NAME:
14160 return false;
14162 case TREE_VEC:
14164 unsigned ix;
14165 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14166 return false;
14167 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14168 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14169 TREE_VEC_ELT (t2, ix)))
14170 return false;
14171 return true;
14174 default:
14175 break;
14178 switch (TREE_CODE_CLASS (code1))
14180 case tcc_unary:
14181 case tcc_binary:
14182 case tcc_comparison:
14183 case tcc_expression:
14184 case tcc_vl_exp:
14185 case tcc_reference:
14186 case tcc_statement:
14188 int i, n = TREE_OPERAND_LENGTH (t1);
14190 switch (code1)
14192 case PREINCREMENT_EXPR:
14193 case PREDECREMENT_EXPR:
14194 case POSTINCREMENT_EXPR:
14195 case POSTDECREMENT_EXPR:
14196 n = 1;
14197 break;
14198 case ARRAY_REF:
14199 n = 2;
14200 break;
14201 default:
14202 break;
14205 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14206 && n != TREE_OPERAND_LENGTH (t2))
14207 return false;
14209 for (i = 0; i < n; ++i)
14210 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14211 return false;
14213 return true;
14216 case tcc_type:
14217 return comptypes (t1, t2);
14218 default:
14219 gcc_unreachable ();
14221 /* We can get here with --disable-checking. */
14222 return false;
14225 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14226 spawn-helper and BODY is the newly created body for FNDECL. */
14228 void
14229 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14231 tree list = alloc_stmt_list ();
14232 tree frame = make_cilk_frame (fndecl);
14233 tree dtor = create_cilk_function_exit (frame, false, true);
14234 add_local_decl (cfun, frame);
14236 DECL_SAVED_TREE (fndecl) = list;
14238 tree body_list = alloc_stmt_list ();
14239 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14240 body = fold_build_cleanup_point_expr (void_type_node, body);
14242 append_to_statement_list (body, &body_list);
14243 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14244 body_list, dtor), &list);
14247 /* Returns true when the function declaration FNDECL is implicit,
14248 introduced as a result of a call to an otherwise undeclared
14249 function, and false otherwise. */
14251 bool
14252 c_decl_implicit (const_tree fndecl)
14254 return C_DECL_IMPLICIT (fndecl);