PR c++/64767
[official-gcc.git] / gcc / c / c-typeck.c
blob96e7351bf4580788ed2383e26d0aec0d2d6c4fae
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"
54 /* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56 enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
63 /* The level of nesting inside "__alignof__". */
64 int in_alignof;
66 /* The level of nesting inside "sizeof". */
67 int in_sizeof;
69 /* The level of nesting inside "typeof". */
70 int in_typeof;
72 /* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74 tree c_last_sizeof_arg;
76 /* Nonzero if we might need to print a "missing braces around
77 initializer" message within this initializer. */
78 static int found_missing_braces;
80 static int require_constant_value;
81 static int require_constant_elements;
83 static bool null_pointer_constant_p (const_tree);
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
87 static int comp_target_types (location_t, tree, tree);
88 static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
91 static tree lookup_field (tree, tree);
92 static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
95 static tree pointer_diff (location_t, tree, tree);
96 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
97 enum impl_conv, bool, tree, tree, int);
98 static tree valid_compound_expr_initializer (tree, tree);
99 static void push_string (const char *);
100 static void push_member_name (tree);
101 static int spelling_length (void);
102 static char *print_spelling (char *);
103 static void warning_init (location_t, int, const char *);
104 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
105 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
106 bool, struct obstack *);
107 static void output_pending_init_elements (int, struct obstack *);
108 static int set_designator (location_t, int, struct obstack *);
109 static void push_range_stack (tree, struct obstack *);
110 static void add_pending_init (location_t, tree, tree, tree, bool,
111 struct obstack *);
112 static void set_nonincremental_init (struct obstack *);
113 static void set_nonincremental_init_from_string (tree, struct obstack *);
114 static tree find_init_member (tree, struct obstack *);
115 static void readonly_warning (tree, enum lvalue_use);
116 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
117 static void record_maybe_used_decl (tree);
118 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
120 /* Return true if EXP is a null pointer constant, false otherwise. */
122 static bool
123 null_pointer_constant_p (const_tree expr)
125 /* This should really operate on c_expr structures, but they aren't
126 yet available everywhere required. */
127 tree type = TREE_TYPE (expr);
128 return (TREE_CODE (expr) == INTEGER_CST
129 && !TREE_OVERFLOW (expr)
130 && integer_zerop (expr)
131 && (INTEGRAL_TYPE_P (type)
132 || (TREE_CODE (type) == POINTER_TYPE
133 && VOID_TYPE_P (TREE_TYPE (type))
134 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
137 /* EXPR may appear in an unevaluated part of an integer constant
138 expression, but not in an evaluated part. Wrap it in a
139 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
140 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
142 static tree
143 note_integer_operands (tree expr)
145 tree ret;
146 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
148 ret = copy_node (expr);
149 TREE_OVERFLOW (ret) = 1;
151 else
153 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
154 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
156 return ret;
159 /* Having checked whether EXPR may appear in an unevaluated part of an
160 integer constant expression and found that it may, remove any
161 C_MAYBE_CONST_EXPR noting this fact and return the resulting
162 expression. */
164 static inline tree
165 remove_c_maybe_const_expr (tree expr)
167 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
168 return C_MAYBE_CONST_EXPR_EXPR (expr);
169 else
170 return expr;
173 \f/* This is a cache to hold if two types are compatible or not. */
175 struct tagged_tu_seen_cache {
176 const struct tagged_tu_seen_cache * next;
177 const_tree t1;
178 const_tree t2;
179 /* The return value of tagged_types_tu_compatible_p if we had seen
180 these two types already. */
181 int val;
184 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
185 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
187 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
188 does not have an incomplete type. (That includes void types.)
189 LOC is the location of the use. */
191 tree
192 require_complete_type (location_t loc, tree value)
194 tree type = TREE_TYPE (value);
196 if (error_operand_p (value))
197 return error_mark_node;
199 /* First, detect a valid value with a complete type. */
200 if (COMPLETE_TYPE_P (type))
201 return value;
203 c_incomplete_type_error (loc, value, type);
204 return error_mark_node;
207 /* Print an error message for invalid use of an incomplete type.
208 VALUE is the expression that was used (or 0 if that isn't known)
209 and TYPE is the type that was invalid. LOC is the location for
210 the error. */
212 void
213 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
215 /* Avoid duplicate error message. */
216 if (TREE_CODE (type) == ERROR_MARK)
217 return;
219 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
220 error_at (loc, "%qD has an incomplete type %qT", value, type);
221 else
223 retry:
224 /* We must print an error message. Be clever about what it says. */
226 switch (TREE_CODE (type))
228 case RECORD_TYPE:
229 case UNION_TYPE:
230 case ENUMERAL_TYPE:
231 break;
233 case VOID_TYPE:
234 error_at (loc, "invalid use of void expression");
235 return;
237 case ARRAY_TYPE:
238 if (TYPE_DOMAIN (type))
240 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
242 error_at (loc, "invalid use of flexible array member");
243 return;
245 type = TREE_TYPE (type);
246 goto retry;
248 error_at (loc, "invalid use of array with unspecified bounds");
249 return;
251 default:
252 gcc_unreachable ();
255 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
256 error_at (loc, "invalid use of undefined type %qT", type);
257 else
258 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
259 error_at (loc, "invalid use of incomplete typedef %qT", type);
263 /* Given a type, apply default promotions wrt unnamed function
264 arguments and return the new type. */
266 tree
267 c_type_promotes_to (tree type)
269 tree ret = NULL_TREE;
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 ret = double_type_node;
273 else if (c_promoting_integer_type_p (type))
275 /* Preserve unsignedness if not really getting any wider. */
276 if (TYPE_UNSIGNED (type)
277 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
278 ret = unsigned_type_node;
279 else
280 ret = integer_type_node;
283 if (ret != NULL_TREE)
284 return (TYPE_ATOMIC (type)
285 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
286 : ret);
288 return type;
291 /* Return true if between two named address spaces, whether there is a superset
292 named address space that encompasses both address spaces. If there is a
293 superset, return which address space is the superset. */
295 static bool
296 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
298 if (as1 == as2)
300 *common = as1;
301 return true;
303 else if (targetm.addr_space.subset_p (as1, as2))
305 *common = as2;
306 return true;
308 else if (targetm.addr_space.subset_p (as2, as1))
310 *common = as1;
311 return true;
313 else
314 return false;
317 /* Return a variant of TYPE which has all the type qualifiers of LIKE
318 as well as those of TYPE. */
320 static tree
321 qualify_type (tree type, tree like)
323 addr_space_t as_type = TYPE_ADDR_SPACE (type);
324 addr_space_t as_like = TYPE_ADDR_SPACE (like);
325 addr_space_t as_common;
327 /* If the two named address spaces are different, determine the common
328 superset address space. If there isn't one, raise an error. */
329 if (!addr_space_superset (as_type, as_like, &as_common))
331 as_common = as_type;
332 error ("%qT and %qT are in disjoint named address spaces",
333 type, like);
336 return c_build_qualified_type (type,
337 TYPE_QUALS_NO_ADDR_SPACE (type)
338 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
339 | ENCODE_QUAL_ADDR_SPACE (as_common));
342 /* Return true iff the given tree T is a variable length array. */
344 bool
345 c_vla_type_p (const_tree t)
347 if (TREE_CODE (t) == ARRAY_TYPE
348 && C_TYPE_VARIABLE_SIZE (t))
349 return true;
350 return false;
353 /* Return the composite type of two compatible types.
355 We assume that comptypes has already been done and returned
356 nonzero; if that isn't so, this may crash. In particular, we
357 assume that qualifiers match. */
359 tree
360 composite_type (tree t1, tree t2)
362 enum tree_code code1;
363 enum tree_code code2;
364 tree attributes;
366 /* Save time if the two types are the same. */
368 if (t1 == t2) return t1;
370 /* If one type is nonsense, use the other. */
371 if (t1 == error_mark_node)
372 return t2;
373 if (t2 == error_mark_node)
374 return t1;
376 code1 = TREE_CODE (t1);
377 code2 = TREE_CODE (t2);
379 /* Merge the attributes. */
380 attributes = targetm.merge_type_attributes (t1, t2);
382 /* If one is an enumerated type and the other is the compatible
383 integer type, the composite type might be either of the two
384 (DR#013 question 3). For consistency, use the enumerated type as
385 the composite type. */
387 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
388 return t1;
389 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
390 return t2;
392 gcc_assert (code1 == code2);
394 switch (code1)
396 case POINTER_TYPE:
397 /* For two pointers, do this recursively on the target type. */
399 tree pointed_to_1 = TREE_TYPE (t1);
400 tree pointed_to_2 = TREE_TYPE (t2);
401 tree target = composite_type (pointed_to_1, pointed_to_2);
402 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
403 t1 = build_type_attribute_variant (t1, attributes);
404 return qualify_type (t1, t2);
407 case ARRAY_TYPE:
409 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
410 int quals;
411 tree unqual_elt;
412 tree d1 = TYPE_DOMAIN (t1);
413 tree d2 = TYPE_DOMAIN (t2);
414 bool d1_variable, d2_variable;
415 bool d1_zero, d2_zero;
416 bool t1_complete, t2_complete;
418 /* We should not have any type quals on arrays at all. */
419 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
420 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
422 t1_complete = COMPLETE_TYPE_P (t1);
423 t2_complete = COMPLETE_TYPE_P (t2);
425 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
426 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
428 d1_variable = (!d1_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
431 d2_variable = (!d2_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
434 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
435 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
437 /* Save space: see if the result is identical to one of the args. */
438 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
439 && (d2_variable || d2_zero || !d1_variable))
440 return build_type_attribute_variant (t1, attributes);
441 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
442 && (d1_variable || d1_zero || !d2_variable))
443 return build_type_attribute_variant (t2, attributes);
445 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
446 return build_type_attribute_variant (t1, attributes);
447 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
448 return build_type_attribute_variant (t2, attributes);
450 /* Merge the element types, and have a size if either arg has
451 one. We may have qualifiers on the element types. To set
452 up TYPE_MAIN_VARIANT correctly, we need to form the
453 composite of the unqualified types and add the qualifiers
454 back at the end. */
455 quals = TYPE_QUALS (strip_array_types (elt));
456 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
457 t1 = build_array_type (unqual_elt,
458 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
459 && (d2_variable
460 || d2_zero
461 || !d1_variable))
462 ? t1
463 : t2));
464 /* Ensure a composite type involving a zero-length array type
465 is a zero-length type not an incomplete type. */
466 if (d1_zero && d2_zero
467 && (t1_complete || t2_complete)
468 && !COMPLETE_TYPE_P (t1))
470 TYPE_SIZE (t1) = bitsize_zero_node;
471 TYPE_SIZE_UNIT (t1) = size_zero_node;
473 t1 = c_build_qualified_type (t1, quals);
474 return build_type_attribute_variant (t1, attributes);
477 case ENUMERAL_TYPE:
478 case RECORD_TYPE:
479 case UNION_TYPE:
480 if (attributes != NULL)
482 /* Try harder not to create a new aggregate type. */
483 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
484 return t1;
485 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
486 return t2;
488 return build_type_attribute_variant (t1, attributes);
490 case FUNCTION_TYPE:
491 /* Function types: prefer the one that specified arg types.
492 If both do, merge the arg types. Also merge the return types. */
494 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
495 tree p1 = TYPE_ARG_TYPES (t1);
496 tree p2 = TYPE_ARG_TYPES (t2);
497 int len;
498 tree newargs, n;
499 int i;
501 /* Save space: see if the result is identical to one of the args. */
502 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
503 return build_type_attribute_variant (t1, attributes);
504 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
505 return build_type_attribute_variant (t2, attributes);
507 /* Simple way if one arg fails to specify argument types. */
508 if (TYPE_ARG_TYPES (t1) == 0)
510 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
511 t1 = build_type_attribute_variant (t1, attributes);
512 return qualify_type (t1, t2);
514 if (TYPE_ARG_TYPES (t2) == 0)
516 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
517 t1 = build_type_attribute_variant (t1, attributes);
518 return qualify_type (t1, t2);
521 /* If both args specify argument types, we must merge the two
522 lists, argument by argument. */
524 for (len = 0, newargs = p1;
525 newargs && newargs != void_list_node;
526 len++, newargs = TREE_CHAIN (newargs))
529 for (i = 0; i < len; i++)
530 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
532 n = newargs;
534 for (; p1 && p1 != void_list_node;
535 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
537 /* A null type means arg type is not specified.
538 Take whatever the other function type has. */
539 if (TREE_VALUE (p1) == 0)
541 TREE_VALUE (n) = TREE_VALUE (p2);
542 goto parm_done;
544 if (TREE_VALUE (p2) == 0)
546 TREE_VALUE (n) = TREE_VALUE (p1);
547 goto parm_done;
550 /* Given wait (union {union wait *u; int *i} *)
551 and wait (union wait *),
552 prefer union wait * as type of parm. */
553 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
554 && TREE_VALUE (p1) != TREE_VALUE (p2))
556 tree memb;
557 tree mv2 = TREE_VALUE (p2);
558 if (mv2 && mv2 != error_mark_node
559 && TREE_CODE (mv2) != ARRAY_TYPE)
560 mv2 = TYPE_MAIN_VARIANT (mv2);
561 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
562 memb; memb = DECL_CHAIN (memb))
564 tree mv3 = TREE_TYPE (memb);
565 if (mv3 && mv3 != error_mark_node
566 && TREE_CODE (mv3) != ARRAY_TYPE)
567 mv3 = TYPE_MAIN_VARIANT (mv3);
568 if (comptypes (mv3, mv2))
570 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
571 TREE_VALUE (p2));
572 pedwarn (input_location, OPT_Wpedantic,
573 "function types not truly compatible in ISO C");
574 goto parm_done;
578 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
579 && TREE_VALUE (p2) != TREE_VALUE (p1))
581 tree memb;
582 tree mv1 = TREE_VALUE (p1);
583 if (mv1 && mv1 != error_mark_node
584 && TREE_CODE (mv1) != ARRAY_TYPE)
585 mv1 = TYPE_MAIN_VARIANT (mv1);
586 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
587 memb; memb = DECL_CHAIN (memb))
589 tree mv3 = TREE_TYPE (memb);
590 if (mv3 && mv3 != error_mark_node
591 && TREE_CODE (mv3) != ARRAY_TYPE)
592 mv3 = TYPE_MAIN_VARIANT (mv3);
593 if (comptypes (mv3, mv1))
595 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
596 TREE_VALUE (p1));
597 pedwarn (input_location, OPT_Wpedantic,
598 "function types not truly compatible in ISO C");
599 goto parm_done;
603 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
604 parm_done: ;
607 t1 = build_function_type (valtype, newargs);
608 t1 = qualify_type (t1, t2);
610 /* FALLTHRU */
612 default:
613 return build_type_attribute_variant (t1, attributes);
618 /* Return the type of a conditional expression between pointers to
619 possibly differently qualified versions of compatible types.
621 We assume that comp_target_types has already been done and returned
622 nonzero; if that isn't so, this may crash. */
624 static tree
625 common_pointer_type (tree t1, tree t2)
627 tree attributes;
628 tree pointed_to_1, mv1;
629 tree pointed_to_2, mv2;
630 tree target;
631 unsigned target_quals;
632 addr_space_t as1, as2, as_common;
633 int quals1, quals2;
635 /* Save time if the two types are the same. */
637 if (t1 == t2) return t1;
639 /* If one type is nonsense, use the other. */
640 if (t1 == error_mark_node)
641 return t2;
642 if (t2 == error_mark_node)
643 return t1;
645 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
646 && TREE_CODE (t2) == POINTER_TYPE);
648 /* Merge the attributes. */
649 attributes = targetm.merge_type_attributes (t1, t2);
651 /* Find the composite type of the target types, and combine the
652 qualifiers of the two types' targets. Do not lose qualifiers on
653 array element types by taking the TYPE_MAIN_VARIANT. */
654 mv1 = pointed_to_1 = TREE_TYPE (t1);
655 mv2 = pointed_to_2 = TREE_TYPE (t2);
656 if (TREE_CODE (mv1) != ARRAY_TYPE)
657 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
658 if (TREE_CODE (mv2) != ARRAY_TYPE)
659 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
660 target = composite_type (mv1, mv2);
662 /* Strip array types to get correct qualifier for pointers to arrays */
663 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
664 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
666 /* For function types do not merge const qualifiers, but drop them
667 if used inconsistently. The middle-end uses these to mark const
668 and noreturn functions. */
669 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
670 target_quals = (quals1 & quals2);
671 else
672 target_quals = (quals1 | quals2);
674 /* If the two named address spaces are different, determine the common
675 superset address space. This is guaranteed to exist due to the
676 assumption that comp_target_type returned non-zero. */
677 as1 = TYPE_ADDR_SPACE (pointed_to_1);
678 as2 = TYPE_ADDR_SPACE (pointed_to_2);
679 if (!addr_space_superset (as1, as2, &as_common))
680 gcc_unreachable ();
682 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
684 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
685 return build_type_attribute_variant (t1, attributes);
688 /* Return the common type for two arithmetic types under the usual
689 arithmetic conversions. The default conversions have already been
690 applied, and enumerated types converted to their compatible integer
691 types. The resulting type is unqualified and has no attributes.
693 This is the type for the result of most arithmetic operations
694 if the operands have the given two types. */
696 static tree
697 c_common_type (tree t1, tree t2)
699 enum tree_code code1;
700 enum tree_code code2;
702 /* If one type is nonsense, use the other. */
703 if (t1 == error_mark_node)
704 return t2;
705 if (t2 == error_mark_node)
706 return t1;
708 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
709 t1 = TYPE_MAIN_VARIANT (t1);
711 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
712 t2 = TYPE_MAIN_VARIANT (t2);
714 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
715 t1 = build_type_attribute_variant (t1, NULL_TREE);
717 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
718 t2 = build_type_attribute_variant (t2, NULL_TREE);
720 /* Save time if the two types are the same. */
722 if (t1 == t2) return t1;
724 code1 = TREE_CODE (t1);
725 code2 = TREE_CODE (t2);
727 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
728 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
729 || code1 == INTEGER_TYPE);
730 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
731 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
732 || code2 == INTEGER_TYPE);
734 /* When one operand is a decimal float type, the other operand cannot be
735 a generic float type or a complex type. We also disallow vector types
736 here. */
737 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
738 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
740 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
742 error ("can%'t mix operands of decimal float and vector types");
743 return error_mark_node;
745 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
747 error ("can%'t mix operands of decimal float and complex types");
748 return error_mark_node;
750 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
752 error ("can%'t mix operands of decimal float and other float types");
753 return error_mark_node;
757 /* If one type is a vector type, return that type. (How the usual
758 arithmetic conversions apply to the vector types extension is not
759 precisely specified.) */
760 if (code1 == VECTOR_TYPE)
761 return t1;
763 if (code2 == VECTOR_TYPE)
764 return t2;
766 /* If one type is complex, form the common type of the non-complex
767 components, then make that complex. Use T1 or T2 if it is the
768 required type. */
769 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
771 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
772 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
773 tree subtype = c_common_type (subtype1, subtype2);
775 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
776 return t1;
777 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
778 return t2;
779 else
780 return build_complex_type (subtype);
783 /* If only one is real, use it as the result. */
785 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
786 return t1;
788 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
789 return t2;
791 /* If both are real and either are decimal floating point types, use
792 the decimal floating point type with the greater precision. */
794 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
796 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
798 return dfloat128_type_node;
799 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
801 return dfloat64_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
804 return dfloat32_type_node;
807 /* Deal with fixed-point types. */
808 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
810 unsigned int unsignedp = 0, satp = 0;
811 machine_mode m1, m2;
812 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
814 m1 = TYPE_MODE (t1);
815 m2 = TYPE_MODE (t2);
817 /* If one input type is saturating, the result type is saturating. */
818 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
819 satp = 1;
821 /* If both fixed-point types are unsigned, the result type is unsigned.
822 When mixing fixed-point and integer types, follow the sign of the
823 fixed-point type.
824 Otherwise, the result type is signed. */
825 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
826 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
827 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
828 && TYPE_UNSIGNED (t1))
829 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
830 && TYPE_UNSIGNED (t2)))
831 unsignedp = 1;
833 /* The result type is signed. */
834 if (unsignedp == 0)
836 /* If the input type is unsigned, we need to convert to the
837 signed type. */
838 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
840 enum mode_class mclass = (enum mode_class) 0;
841 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
842 mclass = MODE_FRACT;
843 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
844 mclass = MODE_ACCUM;
845 else
846 gcc_unreachable ();
847 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
849 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
851 enum mode_class mclass = (enum mode_class) 0;
852 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
853 mclass = MODE_FRACT;
854 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
855 mclass = MODE_ACCUM;
856 else
857 gcc_unreachable ();
858 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
862 if (code1 == FIXED_POINT_TYPE)
864 fbit1 = GET_MODE_FBIT (m1);
865 ibit1 = GET_MODE_IBIT (m1);
867 else
869 fbit1 = 0;
870 /* Signed integers need to subtract one sign bit. */
871 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
874 if (code2 == FIXED_POINT_TYPE)
876 fbit2 = GET_MODE_FBIT (m2);
877 ibit2 = GET_MODE_IBIT (m2);
879 else
881 fbit2 = 0;
882 /* Signed integers need to subtract one sign bit. */
883 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
886 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
887 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
888 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
889 satp);
892 /* Both real or both integers; use the one with greater precision. */
894 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
895 return t1;
896 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
897 return t2;
899 /* Same precision. Prefer long longs to longs to ints when the
900 same precision, following the C99 rules on integer type rank
901 (which are equivalent to the C90 rules for C90 types). */
903 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
905 return long_long_unsigned_type_node;
907 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
908 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
910 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
911 return long_long_unsigned_type_node;
912 else
913 return long_long_integer_type_node;
916 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
918 return long_unsigned_type_node;
920 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
923 /* But preserve unsignedness from the other type,
924 since long cannot hold all the values of an unsigned int. */
925 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
926 return long_unsigned_type_node;
927 else
928 return long_integer_type_node;
931 /* For floating types of the same TYPE_PRECISION (which we here
932 assume means either the same set of values, or sets of values
933 neither a subset of the other, with behavior being undefined in
934 the latter case), follow the rules from TS 18661-3: prefer
935 interchange types _FloatN, then standard types long double,
936 double, float, then extended types _FloatNx. For extended types,
937 check them starting with _Float128x as that seems most consistent
938 in spirit with preferring long double to double; for interchange
939 types, also check in that order for consistency although it's not
940 possible for more than one of them to have the same
941 precision. */
942 tree mv1 = TYPE_MAIN_VARIANT (t1);
943 tree mv2 = TYPE_MAIN_VARIANT (t2);
945 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
946 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
947 return FLOATN_TYPE_NODE (i);
949 /* Likewise, prefer long double to double even if same size. */
950 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
951 return long_double_type_node;
953 /* Likewise, prefer double to float even if same size.
954 We got a couple of embedded targets with 32 bit doubles, and the
955 pdp11 might have 64 bit floats. */
956 if (mv1 == double_type_node || mv2 == double_type_node)
957 return double_type_node;
959 if (mv1 == float_type_node || mv2 == float_type_node)
960 return float_type_node;
962 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
963 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
964 return FLOATNX_TYPE_NODE (i);
966 /* Otherwise prefer the unsigned one. */
968 if (TYPE_UNSIGNED (t1))
969 return t1;
970 else
971 return t2;
974 /* Wrapper around c_common_type that is used by c-common.c and other
975 front end optimizations that remove promotions. ENUMERAL_TYPEs
976 are allowed here and are converted to their compatible integer types.
977 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
978 preferably a non-Boolean type as the common type. */
979 tree
980 common_type (tree t1, tree t2)
982 if (TREE_CODE (t1) == ENUMERAL_TYPE)
983 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
984 if (TREE_CODE (t2) == ENUMERAL_TYPE)
985 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
987 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
988 if (TREE_CODE (t1) == BOOLEAN_TYPE
989 && TREE_CODE (t2) == BOOLEAN_TYPE)
990 return boolean_type_node;
992 /* If either type is BOOLEAN_TYPE, then return the other. */
993 if (TREE_CODE (t1) == BOOLEAN_TYPE)
994 return t2;
995 if (TREE_CODE (t2) == BOOLEAN_TYPE)
996 return t1;
998 return c_common_type (t1, t2);
1001 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1002 or various other operations. Return 2 if they are compatible
1003 but a warning may be needed if you use them together. */
1006 comptypes (tree type1, tree type2)
1008 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1009 int val;
1011 val = comptypes_internal (type1, type2, NULL, NULL);
1012 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1014 return val;
1017 /* Like comptypes, but if it returns non-zero because enum and int are
1018 compatible, it sets *ENUM_AND_INT_P to true. */
1020 static int
1021 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1023 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1024 int val;
1026 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1027 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1029 return val;
1032 /* Like comptypes, but if it returns nonzero for different types, it
1033 sets *DIFFERENT_TYPES_P to true. */
1036 comptypes_check_different_types (tree type1, tree type2,
1037 bool *different_types_p)
1039 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1040 int val;
1042 val = comptypes_internal (type1, type2, NULL, different_types_p);
1043 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1045 return val;
1048 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1049 or various other operations. Return 2 if they are compatible
1050 but a warning may be needed if you use them together. If
1051 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1052 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1053 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1054 NULL, and the types are compatible but different enough not to be
1055 permitted in C11 typedef redeclarations, then this sets
1056 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1057 false, but may or may not be set if the types are incompatible.
1058 This differs from comptypes, in that we don't free the seen
1059 types. */
1061 static int
1062 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1063 bool *different_types_p)
1065 const_tree t1 = type1;
1066 const_tree t2 = type2;
1067 int attrval, val;
1069 /* Suppress errors caused by previously reported errors. */
1071 if (t1 == t2 || !t1 || !t2
1072 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1073 return 1;
1075 /* Enumerated types are compatible with integer types, but this is
1076 not transitive: two enumerated types in the same translation unit
1077 are compatible with each other only if they are the same type. */
1079 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1081 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1082 if (TREE_CODE (t2) != VOID_TYPE)
1084 if (enum_and_int_p != NULL)
1085 *enum_and_int_p = true;
1086 if (different_types_p != NULL)
1087 *different_types_p = true;
1090 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1092 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1093 if (TREE_CODE (t1) != VOID_TYPE)
1095 if (enum_and_int_p != NULL)
1096 *enum_and_int_p = true;
1097 if (different_types_p != NULL)
1098 *different_types_p = true;
1102 if (t1 == t2)
1103 return 1;
1105 /* Different classes of types can't be compatible. */
1107 if (TREE_CODE (t1) != TREE_CODE (t2))
1108 return 0;
1110 /* Qualifiers must match. C99 6.7.3p9 */
1112 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1113 return 0;
1115 /* Allow for two different type nodes which have essentially the same
1116 definition. Note that we already checked for equality of the type
1117 qualifiers (just above). */
1119 if (TREE_CODE (t1) != ARRAY_TYPE
1120 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1121 return 1;
1123 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1124 if (!(attrval = comp_type_attributes (t1, t2)))
1125 return 0;
1127 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1128 val = 0;
1130 switch (TREE_CODE (t1))
1132 case INTEGER_TYPE:
1133 case FIXED_POINT_TYPE:
1134 case REAL_TYPE:
1135 /* With these nodes, we can't determine type equivalence by
1136 looking at what is stored in the nodes themselves, because
1137 two nodes might have different TYPE_MAIN_VARIANTs but still
1138 represent the same type. For example, wchar_t and int could
1139 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1140 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1141 and are distinct types. On the other hand, int and the
1142 following typedef
1144 typedef int INT __attribute((may_alias));
1146 have identical properties, different TYPE_MAIN_VARIANTs, but
1147 represent the same type. The canonical type system keeps
1148 track of equivalence in this case, so we fall back on it. */
1149 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1151 case POINTER_TYPE:
1152 /* Do not remove mode information. */
1153 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1154 break;
1155 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1156 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1157 enum_and_int_p, different_types_p));
1158 break;
1160 case FUNCTION_TYPE:
1161 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1162 different_types_p);
1163 break;
1165 case ARRAY_TYPE:
1167 tree d1 = TYPE_DOMAIN (t1);
1168 tree d2 = TYPE_DOMAIN (t2);
1169 bool d1_variable, d2_variable;
1170 bool d1_zero, d2_zero;
1171 val = 1;
1173 /* Target types must match incl. qualifiers. */
1174 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1175 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1176 enum_and_int_p,
1177 different_types_p)))
1178 return 0;
1180 if (different_types_p != NULL
1181 && (d1 == 0) != (d2 == 0))
1182 *different_types_p = true;
1183 /* Sizes must match unless one is missing or variable. */
1184 if (d1 == 0 || d2 == 0 || d1 == d2)
1185 break;
1187 d1_zero = !TYPE_MAX_VALUE (d1);
1188 d2_zero = !TYPE_MAX_VALUE (d2);
1190 d1_variable = (!d1_zero
1191 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1192 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1193 d2_variable = (!d2_zero
1194 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1195 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1196 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1197 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1199 if (different_types_p != NULL
1200 && d1_variable != d2_variable)
1201 *different_types_p = true;
1202 if (d1_variable || d2_variable)
1203 break;
1204 if (d1_zero && d2_zero)
1205 break;
1206 if (d1_zero || d2_zero
1207 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1208 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1209 val = 0;
1211 break;
1214 case ENUMERAL_TYPE:
1215 case RECORD_TYPE:
1216 case UNION_TYPE:
1217 if (val != 1 && !same_translation_unit_p (t1, t2))
1219 tree a1 = TYPE_ATTRIBUTES (t1);
1220 tree a2 = TYPE_ATTRIBUTES (t2);
1222 if (! attribute_list_contained (a1, a2)
1223 && ! attribute_list_contained (a2, a1))
1224 break;
1226 if (attrval != 2)
1227 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1228 different_types_p);
1229 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1230 different_types_p);
1232 break;
1234 case VECTOR_TYPE:
1235 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1236 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1237 enum_and_int_p, different_types_p));
1238 break;
1240 default:
1241 break;
1243 return attrval == 2 && val == 1 ? 2 : val;
1246 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1247 their qualifiers, except for named address spaces. If the pointers point to
1248 different named addresses, then we must determine if one address space is a
1249 subset of the other. */
1251 static int
1252 comp_target_types (location_t location, tree ttl, tree ttr)
1254 int val;
1255 int val_ped;
1256 tree mvl = TREE_TYPE (ttl);
1257 tree mvr = TREE_TYPE (ttr);
1258 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1259 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1260 addr_space_t as_common;
1261 bool enum_and_int_p;
1263 /* Fail if pointers point to incompatible address spaces. */
1264 if (!addr_space_superset (asl, asr, &as_common))
1265 return 0;
1267 /* For pedantic record result of comptypes on arrays before losing
1268 qualifiers on the element type below. */
1269 val_ped = 1;
1271 if (TREE_CODE (mvl) == ARRAY_TYPE
1272 && TREE_CODE (mvr) == ARRAY_TYPE)
1273 val_ped = comptypes (mvl, mvr);
1275 /* Qualifiers on element types of array types that are
1276 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1278 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1279 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1280 : TYPE_MAIN_VARIANT (mvl));
1282 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1283 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1284 : TYPE_MAIN_VARIANT (mvr));
1286 enum_and_int_p = false;
1287 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1289 if (val == 1 && val_ped != 1)
1290 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1291 "are incompatible in ISO C");
1293 if (val == 2)
1294 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1296 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1297 warning_at (location, OPT_Wc___compat,
1298 "pointer target types incompatible in C++");
1300 return val;
1303 /* Subroutines of `comptypes'. */
1305 /* Determine whether two trees derive from the same translation unit.
1306 If the CONTEXT chain ends in a null, that tree's context is still
1307 being parsed, so if two trees have context chains ending in null,
1308 they're in the same translation unit. */
1310 same_translation_unit_p (const_tree t1, const_tree t2)
1312 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1313 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1315 case tcc_declaration:
1316 t1 = DECL_CONTEXT (t1); break;
1317 case tcc_type:
1318 t1 = TYPE_CONTEXT (t1); break;
1319 case tcc_exceptional:
1320 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1321 default: gcc_unreachable ();
1324 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1325 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1327 case tcc_declaration:
1328 t2 = DECL_CONTEXT (t2); break;
1329 case tcc_type:
1330 t2 = TYPE_CONTEXT (t2); break;
1331 case tcc_exceptional:
1332 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1333 default: gcc_unreachable ();
1336 return t1 == t2;
1339 /* Allocate the seen two types, assuming that they are compatible. */
1341 static struct tagged_tu_seen_cache *
1342 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1344 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1345 tu->next = tagged_tu_seen_base;
1346 tu->t1 = t1;
1347 tu->t2 = t2;
1349 tagged_tu_seen_base = tu;
1351 /* The C standard says that two structures in different translation
1352 units are compatible with each other only if the types of their
1353 fields are compatible (among other things). We assume that they
1354 are compatible until proven otherwise when building the cache.
1355 An example where this can occur is:
1356 struct a
1358 struct a *next;
1360 If we are comparing this against a similar struct in another TU,
1361 and did not assume they were compatible, we end up with an infinite
1362 loop. */
1363 tu->val = 1;
1364 return tu;
1367 /* Free the seen types until we get to TU_TIL. */
1369 static void
1370 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1372 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1373 while (tu != tu_til)
1375 const struct tagged_tu_seen_cache *const tu1
1376 = (const struct tagged_tu_seen_cache *) tu;
1377 tu = tu1->next;
1378 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1380 tagged_tu_seen_base = tu_til;
1383 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1384 compatible. If the two types are not the same (which has been
1385 checked earlier), this can only happen when multiple translation
1386 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1387 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1388 comptypes_internal. */
1390 static int
1391 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1392 bool *enum_and_int_p, bool *different_types_p)
1394 tree s1, s2;
1395 bool needs_warning = false;
1397 /* We have to verify that the tags of the types are the same. This
1398 is harder than it looks because this may be a typedef, so we have
1399 to go look at the original type. It may even be a typedef of a
1400 typedef...
1401 In the case of compiler-created builtin structs the TYPE_DECL
1402 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1403 while (TYPE_NAME (t1)
1404 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1405 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1406 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1408 while (TYPE_NAME (t2)
1409 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1410 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1411 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1413 /* C90 didn't have the requirement that the two tags be the same. */
1414 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1415 return 0;
1417 /* C90 didn't say what happened if one or both of the types were
1418 incomplete; we choose to follow C99 rules here, which is that they
1419 are compatible. */
1420 if (TYPE_SIZE (t1) == NULL
1421 || TYPE_SIZE (t2) == NULL)
1422 return 1;
1425 const struct tagged_tu_seen_cache * tts_i;
1426 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1427 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1428 return tts_i->val;
1431 switch (TREE_CODE (t1))
1433 case ENUMERAL_TYPE:
1435 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1436 /* Speed up the case where the type values are in the same order. */
1437 tree tv1 = TYPE_VALUES (t1);
1438 tree tv2 = TYPE_VALUES (t2);
1440 if (tv1 == tv2)
1442 return 1;
1445 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1447 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1448 break;
1449 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1451 tu->val = 0;
1452 return 0;
1456 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1458 return 1;
1460 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1462 tu->val = 0;
1463 return 0;
1466 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1468 tu->val = 0;
1469 return 0;
1472 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1474 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1475 if (s2 == NULL
1476 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1478 tu->val = 0;
1479 return 0;
1482 return 1;
1485 case UNION_TYPE:
1487 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1488 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1490 tu->val = 0;
1491 return 0;
1494 /* Speed up the common case where the fields are in the same order. */
1495 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1496 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1498 int result;
1500 if (DECL_NAME (s1) != DECL_NAME (s2))
1501 break;
1502 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1503 enum_and_int_p, different_types_p);
1505 if (result != 1 && !DECL_NAME (s1))
1506 break;
1507 if (result == 0)
1509 tu->val = 0;
1510 return 0;
1512 if (result == 2)
1513 needs_warning = true;
1515 if (TREE_CODE (s1) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1517 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1519 tu->val = 0;
1520 return 0;
1523 if (!s1 && !s2)
1525 tu->val = needs_warning ? 2 : 1;
1526 return tu->val;
1529 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1531 bool ok = false;
1533 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1534 if (DECL_NAME (s1) == DECL_NAME (s2))
1536 int result;
1538 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1539 enum_and_int_p,
1540 different_types_p);
1542 if (result != 1 && !DECL_NAME (s1))
1543 continue;
1544 if (result == 0)
1546 tu->val = 0;
1547 return 0;
1549 if (result == 2)
1550 needs_warning = true;
1552 if (TREE_CODE (s1) == FIELD_DECL
1553 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1554 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1555 break;
1557 ok = true;
1558 break;
1560 if (!ok)
1562 tu->val = 0;
1563 return 0;
1566 tu->val = needs_warning ? 2 : 10;
1567 return tu->val;
1570 case RECORD_TYPE:
1572 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1574 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1575 s1 && s2;
1576 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1578 int result;
1579 if (TREE_CODE (s1) != TREE_CODE (s2)
1580 || DECL_NAME (s1) != DECL_NAME (s2))
1581 break;
1582 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1583 enum_and_int_p, different_types_p);
1584 if (result == 0)
1585 break;
1586 if (result == 2)
1587 needs_warning = true;
1589 if (TREE_CODE (s1) == FIELD_DECL
1590 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1591 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1592 break;
1594 if (s1 && s2)
1595 tu->val = 0;
1596 else
1597 tu->val = needs_warning ? 2 : 1;
1598 return tu->val;
1601 default:
1602 gcc_unreachable ();
1606 /* Return 1 if two function types F1 and F2 are compatible.
1607 If either type specifies no argument types,
1608 the other must specify a fixed number of self-promoting arg types.
1609 Otherwise, if one type specifies only the number of arguments,
1610 the other must specify that number of self-promoting arg types.
1611 Otherwise, the argument types must match.
1612 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1614 static int
1615 function_types_compatible_p (const_tree f1, const_tree f2,
1616 bool *enum_and_int_p, bool *different_types_p)
1618 tree args1, args2;
1619 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1620 int val = 1;
1621 int val1;
1622 tree ret1, ret2;
1624 ret1 = TREE_TYPE (f1);
1625 ret2 = TREE_TYPE (f2);
1627 /* 'volatile' qualifiers on a function's return type used to mean
1628 the function is noreturn. */
1629 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1630 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1631 if (TYPE_VOLATILE (ret1))
1632 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1633 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1634 if (TYPE_VOLATILE (ret2))
1635 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1636 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1637 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1638 if (val == 0)
1639 return 0;
1641 args1 = TYPE_ARG_TYPES (f1);
1642 args2 = TYPE_ARG_TYPES (f2);
1644 if (different_types_p != NULL
1645 && (args1 == 0) != (args2 == 0))
1646 *different_types_p = true;
1648 /* An unspecified parmlist matches any specified parmlist
1649 whose argument types don't need default promotions. */
1651 if (args1 == 0)
1653 if (!self_promoting_args_p (args2))
1654 return 0;
1655 /* If one of these types comes from a non-prototype fn definition,
1656 compare that with the other type's arglist.
1657 If they don't match, ask for a warning (but no error). */
1658 if (TYPE_ACTUAL_ARG_TYPES (f1)
1659 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1660 enum_and_int_p, different_types_p))
1661 val = 2;
1662 return val;
1664 if (args2 == 0)
1666 if (!self_promoting_args_p (args1))
1667 return 0;
1668 if (TYPE_ACTUAL_ARG_TYPES (f2)
1669 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1670 enum_and_int_p, different_types_p))
1671 val = 2;
1672 return val;
1675 /* Both types have argument lists: compare them and propagate results. */
1676 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1677 different_types_p);
1678 return val1 != 1 ? val1 : val;
1681 /* Check two lists of types for compatibility, returning 0 for
1682 incompatible, 1 for compatible, or 2 for compatible with
1683 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1684 comptypes_internal. */
1686 static int
1687 type_lists_compatible_p (const_tree args1, const_tree args2,
1688 bool *enum_and_int_p, bool *different_types_p)
1690 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1691 int val = 1;
1692 int newval = 0;
1694 while (1)
1696 tree a1, mv1, a2, mv2;
1697 if (args1 == 0 && args2 == 0)
1698 return val;
1699 /* If one list is shorter than the other,
1700 they fail to match. */
1701 if (args1 == 0 || args2 == 0)
1702 return 0;
1703 mv1 = a1 = TREE_VALUE (args1);
1704 mv2 = a2 = TREE_VALUE (args2);
1705 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1706 mv1 = (TYPE_ATOMIC (mv1)
1707 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1708 TYPE_QUAL_ATOMIC)
1709 : TYPE_MAIN_VARIANT (mv1));
1710 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1711 mv2 = (TYPE_ATOMIC (mv2)
1712 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1713 TYPE_QUAL_ATOMIC)
1714 : TYPE_MAIN_VARIANT (mv2));
1715 /* A null pointer instead of a type
1716 means there is supposed to be an argument
1717 but nothing is specified about what type it has.
1718 So match anything that self-promotes. */
1719 if (different_types_p != NULL
1720 && (a1 == 0) != (a2 == 0))
1721 *different_types_p = true;
1722 if (a1 == 0)
1724 if (c_type_promotes_to (a2) != a2)
1725 return 0;
1727 else if (a2 == 0)
1729 if (c_type_promotes_to (a1) != a1)
1730 return 0;
1732 /* If one of the lists has an error marker, ignore this arg. */
1733 else if (TREE_CODE (a1) == ERROR_MARK
1734 || TREE_CODE (a2) == ERROR_MARK)
1736 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1737 different_types_p)))
1739 if (different_types_p != NULL)
1740 *different_types_p = true;
1741 /* Allow wait (union {union wait *u; int *i} *)
1742 and wait (union wait *) to be compatible. */
1743 if (TREE_CODE (a1) == UNION_TYPE
1744 && (TYPE_NAME (a1) == 0
1745 || TYPE_TRANSPARENT_AGGR (a1))
1746 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1747 && tree_int_cst_equal (TYPE_SIZE (a1),
1748 TYPE_SIZE (a2)))
1750 tree memb;
1751 for (memb = TYPE_FIELDS (a1);
1752 memb; memb = DECL_CHAIN (memb))
1754 tree mv3 = TREE_TYPE (memb);
1755 if (mv3 && mv3 != error_mark_node
1756 && TREE_CODE (mv3) != ARRAY_TYPE)
1757 mv3 = (TYPE_ATOMIC (mv3)
1758 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1759 TYPE_QUAL_ATOMIC)
1760 : TYPE_MAIN_VARIANT (mv3));
1761 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1762 different_types_p))
1763 break;
1765 if (memb == 0)
1766 return 0;
1768 else if (TREE_CODE (a2) == UNION_TYPE
1769 && (TYPE_NAME (a2) == 0
1770 || TYPE_TRANSPARENT_AGGR (a2))
1771 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1772 && tree_int_cst_equal (TYPE_SIZE (a2),
1773 TYPE_SIZE (a1)))
1775 tree memb;
1776 for (memb = TYPE_FIELDS (a2);
1777 memb; memb = DECL_CHAIN (memb))
1779 tree mv3 = TREE_TYPE (memb);
1780 if (mv3 && mv3 != error_mark_node
1781 && TREE_CODE (mv3) != ARRAY_TYPE)
1782 mv3 = (TYPE_ATOMIC (mv3)
1783 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1784 TYPE_QUAL_ATOMIC)
1785 : TYPE_MAIN_VARIANT (mv3));
1786 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1787 different_types_p))
1788 break;
1790 if (memb == 0)
1791 return 0;
1793 else
1794 return 0;
1797 /* comptypes said ok, but record if it said to warn. */
1798 if (newval > val)
1799 val = newval;
1801 args1 = TREE_CHAIN (args1);
1802 args2 = TREE_CHAIN (args2);
1806 /* Compute the size to increment a pointer by. When a function type or void
1807 type or incomplete type is passed, size_one_node is returned.
1808 This function does not emit any diagnostics; the caller is responsible
1809 for that. */
1811 static tree
1812 c_size_in_bytes (const_tree type)
1814 enum tree_code code = TREE_CODE (type);
1816 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1817 || !COMPLETE_TYPE_P (type))
1818 return size_one_node;
1820 /* Convert in case a char is more than one unit. */
1821 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1822 size_int (TYPE_PRECISION (char_type_node)
1823 / BITS_PER_UNIT));
1826 /* Return either DECL or its known constant value (if it has one). */
1828 tree
1829 decl_constant_value (tree decl)
1831 if (/* Don't change a variable array bound or initial value to a constant
1832 in a place where a variable is invalid. Note that DECL_INITIAL
1833 isn't valid for a PARM_DECL. */
1834 current_function_decl != 0
1835 && TREE_CODE (decl) != PARM_DECL
1836 && !TREE_THIS_VOLATILE (decl)
1837 && TREE_READONLY (decl)
1838 && DECL_INITIAL (decl) != 0
1839 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1840 /* This is invalid if initial value is not constant.
1841 If it has either a function call, a memory reference,
1842 or a variable, then re-evaluating it could give different results. */
1843 && TREE_CONSTANT (DECL_INITIAL (decl))
1844 /* Check for cases where this is sub-optimal, even though valid. */
1845 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1846 return DECL_INITIAL (decl);
1847 return decl;
1850 /* Convert the array expression EXP to a pointer. */
1851 static tree
1852 array_to_pointer_conversion (location_t loc, tree exp)
1854 tree orig_exp = exp;
1855 tree type = TREE_TYPE (exp);
1856 tree adr;
1857 tree restype = TREE_TYPE (type);
1858 tree ptrtype;
1860 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1862 STRIP_TYPE_NOPS (exp);
1864 if (TREE_NO_WARNING (orig_exp))
1865 TREE_NO_WARNING (exp) = 1;
1867 ptrtype = build_pointer_type (restype);
1869 if (INDIRECT_REF_P (exp))
1870 return convert (ptrtype, TREE_OPERAND (exp, 0));
1872 /* In C++ array compound literals are temporary objects unless they are
1873 const or appear in namespace scope, so they are destroyed too soon
1874 to use them for much of anything (c++/53220). */
1875 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1877 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1878 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1879 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1880 "converting an array compound literal to a pointer "
1881 "is ill-formed in C++");
1884 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1885 return convert (ptrtype, adr);
1888 /* Convert the function expression EXP to a pointer. */
1889 static tree
1890 function_to_pointer_conversion (location_t loc, tree exp)
1892 tree orig_exp = exp;
1894 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1896 STRIP_TYPE_NOPS (exp);
1898 if (TREE_NO_WARNING (orig_exp))
1899 TREE_NO_WARNING (exp) = 1;
1901 return build_unary_op (loc, ADDR_EXPR, exp, false);
1904 /* Mark EXP as read, not just set, for set but not used -Wunused
1905 warning purposes. */
1907 void
1908 mark_exp_read (tree exp)
1910 switch (TREE_CODE (exp))
1912 case VAR_DECL:
1913 case PARM_DECL:
1914 DECL_READ_P (exp) = 1;
1915 break;
1916 case ARRAY_REF:
1917 case COMPONENT_REF:
1918 case MODIFY_EXPR:
1919 case REALPART_EXPR:
1920 case IMAGPART_EXPR:
1921 CASE_CONVERT:
1922 case ADDR_EXPR:
1923 case VIEW_CONVERT_EXPR:
1924 mark_exp_read (TREE_OPERAND (exp, 0));
1925 break;
1926 case COMPOUND_EXPR:
1927 case C_MAYBE_CONST_EXPR:
1928 mark_exp_read (TREE_OPERAND (exp, 1));
1929 break;
1930 default:
1931 break;
1935 /* Perform the default conversion of arrays and functions to pointers.
1936 Return the result of converting EXP. For any other expression, just
1937 return EXP.
1939 LOC is the location of the expression. */
1941 struct c_expr
1942 default_function_array_conversion (location_t loc, struct c_expr exp)
1944 tree orig_exp = exp.value;
1945 tree type = TREE_TYPE (exp.value);
1946 enum tree_code code = TREE_CODE (type);
1948 switch (code)
1950 case ARRAY_TYPE:
1952 bool not_lvalue = false;
1953 bool lvalue_array_p;
1955 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1956 || CONVERT_EXPR_P (exp.value))
1957 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1959 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1960 not_lvalue = true;
1961 exp.value = TREE_OPERAND (exp.value, 0);
1964 if (TREE_NO_WARNING (orig_exp))
1965 TREE_NO_WARNING (exp.value) = 1;
1967 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1968 if (!flag_isoc99 && !lvalue_array_p)
1970 /* Before C99, non-lvalue arrays do not decay to pointers.
1971 Normally, using such an array would be invalid; but it can
1972 be used correctly inside sizeof or as a statement expression.
1973 Thus, do not give an error here; an error will result later. */
1974 return exp;
1977 exp.value = array_to_pointer_conversion (loc, exp.value);
1979 break;
1980 case FUNCTION_TYPE:
1981 exp.value = function_to_pointer_conversion (loc, exp.value);
1982 break;
1983 default:
1984 break;
1987 return exp;
1990 struct c_expr
1991 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1993 mark_exp_read (exp.value);
1994 return default_function_array_conversion (loc, exp);
1997 /* Return whether EXPR should be treated as an atomic lvalue for the
1998 purposes of load and store handling. */
2000 static bool
2001 really_atomic_lvalue (tree expr)
2003 if (error_operand_p (expr))
2004 return false;
2005 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2006 return false;
2007 if (!lvalue_p (expr))
2008 return false;
2010 /* Ignore _Atomic on register variables, since their addresses can't
2011 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2012 sequences wouldn't work. Ignore _Atomic on structures containing
2013 bit-fields, since accessing elements of atomic structures or
2014 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2015 it's undefined at translation time or execution time, and the
2016 normal atomic sequences again wouldn't work. */
2017 while (handled_component_p (expr))
2019 if (TREE_CODE (expr) == COMPONENT_REF
2020 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2021 return false;
2022 expr = TREE_OPERAND (expr, 0);
2024 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2025 return false;
2026 return true;
2029 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2030 including converting functions and arrays to pointers if CONVERT_P.
2031 If READ_P, also mark the expression as having been read. */
2033 struct c_expr
2034 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2035 bool convert_p, bool read_p)
2037 if (read_p)
2038 mark_exp_read (exp.value);
2039 if (convert_p)
2040 exp = default_function_array_conversion (loc, exp);
2041 if (really_atomic_lvalue (exp.value))
2043 vec<tree, va_gc> *params;
2044 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2045 tree expr_type = TREE_TYPE (exp.value);
2046 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2047 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2049 gcc_assert (TYPE_ATOMIC (expr_type));
2051 /* Expansion of a generic atomic load may require an addition
2052 element, so allocate enough to prevent a resize. */
2053 vec_alloc (params, 4);
2055 /* Remove the qualifiers for the rest of the expressions and
2056 create the VAL temp variable to hold the RHS. */
2057 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2058 tmp = create_tmp_var_raw (nonatomic_type);
2059 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2060 TREE_ADDRESSABLE (tmp) = 1;
2061 TREE_NO_WARNING (tmp) = 1;
2063 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2064 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2065 params->quick_push (expr_addr);
2066 params->quick_push (tmp_addr);
2067 params->quick_push (seq_cst);
2068 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2070 /* EXPR is always read. */
2071 mark_exp_read (exp.value);
2073 /* Return tmp which contains the value loaded. */
2074 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2075 NULL_TREE, NULL_TREE);
2077 return exp;
2080 /* EXP is an expression of integer type. Apply the integer promotions
2081 to it and return the promoted value. */
2083 tree
2084 perform_integral_promotions (tree exp)
2086 tree type = TREE_TYPE (exp);
2087 enum tree_code code = TREE_CODE (type);
2089 gcc_assert (INTEGRAL_TYPE_P (type));
2091 /* Normally convert enums to int,
2092 but convert wide enums to something wider. */
2093 if (code == ENUMERAL_TYPE)
2095 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2096 TYPE_PRECISION (integer_type_node)),
2097 ((TYPE_PRECISION (type)
2098 >= TYPE_PRECISION (integer_type_node))
2099 && TYPE_UNSIGNED (type)));
2101 return convert (type, exp);
2104 /* ??? This should no longer be needed now bit-fields have their
2105 proper types. */
2106 if (TREE_CODE (exp) == COMPONENT_REF
2107 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2108 /* If it's thinner than an int, promote it like a
2109 c_promoting_integer_type_p, otherwise leave it alone. */
2110 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2111 TYPE_PRECISION (integer_type_node)))
2112 return convert (integer_type_node, exp);
2114 if (c_promoting_integer_type_p (type))
2116 /* Preserve unsignedness if not really getting any wider. */
2117 if (TYPE_UNSIGNED (type)
2118 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2119 return convert (unsigned_type_node, exp);
2121 return convert (integer_type_node, exp);
2124 return exp;
2128 /* Perform default promotions for C data used in expressions.
2129 Enumeral types or short or char are converted to int.
2130 In addition, manifest constants symbols are replaced by their values. */
2132 tree
2133 default_conversion (tree exp)
2135 tree orig_exp;
2136 tree type = TREE_TYPE (exp);
2137 enum tree_code code = TREE_CODE (type);
2138 tree promoted_type;
2140 mark_exp_read (exp);
2142 /* Functions and arrays have been converted during parsing. */
2143 gcc_assert (code != FUNCTION_TYPE);
2144 if (code == ARRAY_TYPE)
2145 return exp;
2147 /* Constants can be used directly unless they're not loadable. */
2148 if (TREE_CODE (exp) == CONST_DECL)
2149 exp = DECL_INITIAL (exp);
2151 /* Strip no-op conversions. */
2152 orig_exp = exp;
2153 STRIP_TYPE_NOPS (exp);
2155 if (TREE_NO_WARNING (orig_exp))
2156 TREE_NO_WARNING (exp) = 1;
2158 if (code == VOID_TYPE)
2160 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2161 "void value not ignored as it ought to be");
2162 return error_mark_node;
2165 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2166 if (exp == error_mark_node)
2167 return error_mark_node;
2169 promoted_type = targetm.promoted_type (type);
2170 if (promoted_type)
2171 return convert (promoted_type, exp);
2173 if (INTEGRAL_TYPE_P (type))
2174 return perform_integral_promotions (exp);
2176 return exp;
2179 /* Look up COMPONENT in a structure or union TYPE.
2181 If the component name is not found, returns NULL_TREE. Otherwise,
2182 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2183 stepping down the chain to the component, which is in the last
2184 TREE_VALUE of the list. Normally the list is of length one, but if
2185 the component is embedded within (nested) anonymous structures or
2186 unions, the list steps down the chain to the component. */
2188 static tree
2189 lookup_field (tree type, tree component)
2191 tree field;
2193 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2194 to the field elements. Use a binary search on this array to quickly
2195 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2196 will always be set for structures which have many elements. */
2198 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2200 int bot, top, half;
2201 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2203 field = TYPE_FIELDS (type);
2204 bot = 0;
2205 top = TYPE_LANG_SPECIFIC (type)->s->len;
2206 while (top - bot > 1)
2208 half = (top - bot + 1) >> 1;
2209 field = field_array[bot+half];
2211 if (DECL_NAME (field) == NULL_TREE)
2213 /* Step through all anon unions in linear fashion. */
2214 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2216 field = field_array[bot++];
2217 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2219 tree anon = lookup_field (TREE_TYPE (field), component);
2221 if (anon)
2222 return tree_cons (NULL_TREE, field, anon);
2224 /* The Plan 9 compiler permits referring
2225 directly to an anonymous struct/union field
2226 using a typedef name. */
2227 if (flag_plan9_extensions
2228 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2229 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2230 == TYPE_DECL)
2231 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2232 == component))
2233 break;
2237 /* Entire record is only anon unions. */
2238 if (bot > top)
2239 return NULL_TREE;
2241 /* Restart the binary search, with new lower bound. */
2242 continue;
2245 if (DECL_NAME (field) == component)
2246 break;
2247 if (DECL_NAME (field) < component)
2248 bot += half;
2249 else
2250 top = bot + half;
2253 if (DECL_NAME (field_array[bot]) == component)
2254 field = field_array[bot];
2255 else if (DECL_NAME (field) != component)
2256 return NULL_TREE;
2258 else
2260 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2262 if (DECL_NAME (field) == NULL_TREE
2263 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2265 tree anon = lookup_field (TREE_TYPE (field), component);
2267 if (anon)
2268 return tree_cons (NULL_TREE, field, anon);
2270 /* The Plan 9 compiler permits referring directly to an
2271 anonymous struct/union field using a typedef
2272 name. */
2273 if (flag_plan9_extensions
2274 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2275 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2276 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2277 == component))
2278 break;
2281 if (DECL_NAME (field) == component)
2282 break;
2285 if (field == NULL_TREE)
2286 return NULL_TREE;
2289 return tree_cons (NULL_TREE, field, NULL_TREE);
2292 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2294 static void
2295 lookup_field_fuzzy_find_candidates (tree type, tree component,
2296 vec<tree> *candidates)
2298 tree field;
2299 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2301 if (DECL_NAME (field) == NULL_TREE
2302 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2303 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2304 candidates);
2306 if (DECL_NAME (field))
2307 candidates->safe_push (DECL_NAME (field));
2311 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2312 rather than returning a TREE_LIST for an exact match. */
2314 static tree
2315 lookup_field_fuzzy (tree type, tree component)
2317 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2319 /* First, gather a list of candidates. */
2320 auto_vec <tree> candidates;
2322 lookup_field_fuzzy_find_candidates (type, component,
2323 &candidates);
2325 return find_closest_identifier (component, &candidates);
2328 /* Support function for build_component_ref's error-handling.
2330 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2331 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2333 static bool
2334 should_suggest_deref_p (tree datum_type)
2336 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2337 allows "." for ptrs; we could be handling a failed attempt
2338 to access a property. */
2339 if (c_dialect_objc ())
2340 return false;
2342 /* Only suggest it for pointers... */
2343 if (TREE_CODE (datum_type) != POINTER_TYPE)
2344 return false;
2346 /* ...to structs/unions. */
2347 tree underlying_type = TREE_TYPE (datum_type);
2348 enum tree_code code = TREE_CODE (underlying_type);
2349 if (code == RECORD_TYPE || code == UNION_TYPE)
2350 return true;
2351 else
2352 return false;
2355 /* Make an expression to refer to the COMPONENT field of structure or
2356 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2357 location of the COMPONENT_REF. COMPONENT_LOC is the location
2358 of COMPONENT. */
2360 tree
2361 build_component_ref (location_t loc, tree datum, tree component,
2362 location_t component_loc)
2364 tree type = TREE_TYPE (datum);
2365 enum tree_code code = TREE_CODE (type);
2366 tree field = NULL;
2367 tree ref;
2368 bool datum_lvalue = lvalue_p (datum);
2370 if (!objc_is_public (datum, component))
2371 return error_mark_node;
2373 /* Detect Objective-C property syntax object.property. */
2374 if (c_dialect_objc ()
2375 && (ref = objc_maybe_build_component_ref (datum, component)))
2376 return ref;
2378 /* See if there is a field or component with name COMPONENT. */
2380 if (code == RECORD_TYPE || code == UNION_TYPE)
2382 if (!COMPLETE_TYPE_P (type))
2384 c_incomplete_type_error (loc, NULL_TREE, type);
2385 return error_mark_node;
2388 field = lookup_field (type, component);
2390 if (!field)
2392 tree guessed_id = lookup_field_fuzzy (type, component);
2393 if (guessed_id)
2395 /* Attempt to provide a fixit replacement hint, if
2396 we have a valid range for the component. */
2397 location_t reported_loc
2398 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2399 gcc_rich_location rich_loc (reported_loc);
2400 if (component_loc != UNKNOWN_LOCATION)
2401 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2402 error_at_rich_loc
2403 (&rich_loc,
2404 "%qT has no member named %qE; did you mean %qE?",
2405 type, component, guessed_id);
2407 else
2408 error_at (loc, "%qT has no member named %qE", type, component);
2409 return error_mark_node;
2412 /* Accessing elements of atomic structures or unions is undefined
2413 behavior (C11 6.5.2.3#5). */
2414 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2416 if (code == RECORD_TYPE)
2417 warning_at (loc, 0, "accessing a member %qE of an atomic "
2418 "structure %qE", component, datum);
2419 else
2420 warning_at (loc, 0, "accessing a member %qE of an atomic "
2421 "union %qE", component, datum);
2424 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2425 This might be better solved in future the way the C++ front
2426 end does it - by giving the anonymous entities each a
2427 separate name and type, and then have build_component_ref
2428 recursively call itself. We can't do that here. */
2431 tree subdatum = TREE_VALUE (field);
2432 int quals;
2433 tree subtype;
2434 bool use_datum_quals;
2436 if (TREE_TYPE (subdatum) == error_mark_node)
2437 return error_mark_node;
2439 /* If this is an rvalue, it does not have qualifiers in C
2440 standard terms and we must avoid propagating such
2441 qualifiers down to a non-lvalue array that is then
2442 converted to a pointer. */
2443 use_datum_quals = (datum_lvalue
2444 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2446 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2447 if (use_datum_quals)
2448 quals |= TYPE_QUALS (TREE_TYPE (datum));
2449 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2451 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2452 NULL_TREE);
2453 SET_EXPR_LOCATION (ref, loc);
2454 if (TREE_READONLY (subdatum)
2455 || (use_datum_quals && TREE_READONLY (datum)))
2456 TREE_READONLY (ref) = 1;
2457 if (TREE_THIS_VOLATILE (subdatum)
2458 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2459 TREE_THIS_VOLATILE (ref) = 1;
2461 if (TREE_DEPRECATED (subdatum))
2462 warn_deprecated_use (subdatum, NULL_TREE);
2464 datum = ref;
2466 field = TREE_CHAIN (field);
2468 while (field);
2470 return ref;
2472 else if (should_suggest_deref_p (type))
2474 /* Special-case the error message for "ptr.field" for the case
2475 where the user has confused "." vs "->". */
2476 rich_location richloc (line_table, loc);
2477 /* "loc" should be the "." token. */
2478 richloc.add_fixit_replace ("->");
2479 error_at_rich_loc (&richloc,
2480 "%qE is a pointer; did you mean to use %<->%>?",
2481 datum);
2482 return error_mark_node;
2484 else if (code != ERROR_MARK)
2485 error_at (loc,
2486 "request for member %qE in something not a structure or union",
2487 component);
2489 return error_mark_node;
2492 /* Given an expression PTR for a pointer, return an expression
2493 for the value pointed to.
2494 ERRORSTRING is the name of the operator to appear in error messages.
2496 LOC is the location to use for the generated tree. */
2498 tree
2499 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2501 tree pointer = default_conversion (ptr);
2502 tree type = TREE_TYPE (pointer);
2503 tree ref;
2505 if (TREE_CODE (type) == POINTER_TYPE)
2507 if (CONVERT_EXPR_P (pointer)
2508 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2510 /* If a warning is issued, mark it to avoid duplicates from
2511 the backend. This only needs to be done at
2512 warn_strict_aliasing > 2. */
2513 if (warn_strict_aliasing > 2)
2514 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2515 type, TREE_OPERAND (pointer, 0)))
2516 TREE_NO_WARNING (pointer) = 1;
2519 if (TREE_CODE (pointer) == ADDR_EXPR
2520 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2521 == TREE_TYPE (type)))
2523 ref = TREE_OPERAND (pointer, 0);
2524 protected_set_expr_location (ref, loc);
2525 return ref;
2527 else
2529 tree t = TREE_TYPE (type);
2531 ref = build1 (INDIRECT_REF, t, pointer);
2533 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2535 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2537 error_at (loc, "dereferencing pointer to incomplete type "
2538 "%qT", t);
2539 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2541 return error_mark_node;
2543 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2544 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2546 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2547 so that we get the proper error message if the result is used
2548 to assign to. Also, &* is supposed to be a no-op.
2549 And ANSI C seems to specify that the type of the result
2550 should be the const type. */
2551 /* A de-reference of a pointer to const is not a const. It is valid
2552 to change it via some other pointer. */
2553 TREE_READONLY (ref) = TYPE_READONLY (t);
2554 TREE_SIDE_EFFECTS (ref)
2555 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2556 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2557 protected_set_expr_location (ref, loc);
2558 return ref;
2561 else if (TREE_CODE (pointer) != ERROR_MARK)
2562 invalid_indirection_error (loc, type, errstring);
2564 return error_mark_node;
2567 /* This handles expressions of the form "a[i]", which denotes
2568 an array reference.
2570 This is logically equivalent in C to *(a+i), but we may do it differently.
2571 If A is a variable or a member, we generate a primitive ARRAY_REF.
2572 This avoids forcing the array out of registers, and can work on
2573 arrays that are not lvalues (for example, members of structures returned
2574 by functions).
2576 For vector types, allow vector[i] but not i[vector], and create
2577 *(((type*)&vectortype) + i) for the expression.
2579 LOC is the location to use for the returned expression. */
2581 tree
2582 build_array_ref (location_t loc, tree array, tree index)
2584 tree ret;
2585 bool swapped = false;
2586 if (TREE_TYPE (array) == error_mark_node
2587 || TREE_TYPE (index) == error_mark_node)
2588 return error_mark_node;
2590 if (flag_cilkplus && contains_array_notation_expr (index))
2592 size_t rank = 0;
2593 if (!find_rank (loc, index, index, true, &rank))
2594 return error_mark_node;
2595 if (rank > 1)
2597 error_at (loc, "rank of the array's index is greater than 1");
2598 return error_mark_node;
2601 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2602 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2603 /* Allow vector[index] but not index[vector]. */
2604 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2606 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2607 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2609 error_at (loc,
2610 "subscripted value is neither array nor pointer nor vector");
2612 return error_mark_node;
2614 std::swap (array, index);
2615 swapped = true;
2618 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2620 error_at (loc, "array subscript is not an integer");
2621 return error_mark_node;
2624 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2626 error_at (loc, "subscripted value is pointer to function");
2627 return error_mark_node;
2630 /* ??? Existing practice has been to warn only when the char
2631 index is syntactically the index, not for char[array]. */
2632 if (!swapped)
2633 warn_array_subscript_with_type_char (loc, index);
2635 /* Apply default promotions *after* noticing character types. */
2636 index = default_conversion (index);
2637 if (index == error_mark_node)
2638 return error_mark_node;
2640 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2642 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2643 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2645 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2647 tree rval, type;
2649 /* An array that is indexed by a non-constant
2650 cannot be stored in a register; we must be able to do
2651 address arithmetic on its address.
2652 Likewise an array of elements of variable size. */
2653 if (TREE_CODE (index) != INTEGER_CST
2654 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2655 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2657 if (!c_mark_addressable (array))
2658 return error_mark_node;
2660 /* An array that is indexed by a constant value which is not within
2661 the array bounds cannot be stored in a register either; because we
2662 would get a crash in store_bit_field/extract_bit_field when trying
2663 to access a non-existent part of the register. */
2664 if (TREE_CODE (index) == INTEGER_CST
2665 && TYPE_DOMAIN (TREE_TYPE (array))
2666 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2668 if (!c_mark_addressable (array))
2669 return error_mark_node;
2672 if ((pedantic || warn_c90_c99_compat)
2673 && ! was_vector)
2675 tree foo = array;
2676 while (TREE_CODE (foo) == COMPONENT_REF)
2677 foo = TREE_OPERAND (foo, 0);
2678 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2679 pedwarn (loc, OPT_Wpedantic,
2680 "ISO C forbids subscripting %<register%> array");
2681 else if (!lvalue_p (foo))
2682 pedwarn_c90 (loc, OPT_Wpedantic,
2683 "ISO C90 forbids subscripting non-lvalue "
2684 "array");
2687 type = TREE_TYPE (TREE_TYPE (array));
2688 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2689 /* Array ref is const/volatile if the array elements are
2690 or if the array is. */
2691 TREE_READONLY (rval)
2692 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2693 | TREE_READONLY (array));
2694 TREE_SIDE_EFFECTS (rval)
2695 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2696 | TREE_SIDE_EFFECTS (array));
2697 TREE_THIS_VOLATILE (rval)
2698 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2699 /* This was added by rms on 16 Nov 91.
2700 It fixes vol struct foo *a; a->elts[1]
2701 in an inline function.
2702 Hope it doesn't break something else. */
2703 | TREE_THIS_VOLATILE (array));
2704 ret = require_complete_type (loc, rval);
2705 protected_set_expr_location (ret, loc);
2706 if (non_lvalue)
2707 ret = non_lvalue_loc (loc, ret);
2708 return ret;
2710 else
2712 tree ar = default_conversion (array);
2714 if (ar == error_mark_node)
2715 return ar;
2717 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2718 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2720 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2721 index, 0),
2722 RO_ARRAY_INDEXING);
2723 if (non_lvalue)
2724 ret = non_lvalue_loc (loc, ret);
2725 return ret;
2729 /* Build an external reference to identifier ID. FUN indicates
2730 whether this will be used for a function call. LOC is the source
2731 location of the identifier. This sets *TYPE to the type of the
2732 identifier, which is not the same as the type of the returned value
2733 for CONST_DECLs defined as enum constants. If the type of the
2734 identifier is not available, *TYPE is set to NULL. */
2735 tree
2736 build_external_ref (location_t loc, tree id, int fun, tree *type)
2738 tree ref;
2739 tree decl = lookup_name (id);
2741 /* In Objective-C, an instance variable (ivar) may be preferred to
2742 whatever lookup_name() found. */
2743 decl = objc_lookup_ivar (decl, id);
2745 *type = NULL;
2746 if (decl && decl != error_mark_node)
2748 ref = decl;
2749 *type = TREE_TYPE (ref);
2751 else if (fun)
2752 /* Implicit function declaration. */
2753 ref = implicitly_declare (loc, id);
2754 else if (decl == error_mark_node)
2755 /* Don't complain about something that's already been
2756 complained about. */
2757 return error_mark_node;
2758 else
2760 undeclared_variable (loc, id);
2761 return error_mark_node;
2764 if (TREE_TYPE (ref) == error_mark_node)
2765 return error_mark_node;
2767 if (TREE_DEPRECATED (ref))
2768 warn_deprecated_use (ref, NULL_TREE);
2770 /* Recursive call does not count as usage. */
2771 if (ref != current_function_decl)
2773 TREE_USED (ref) = 1;
2776 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2778 if (!in_sizeof && !in_typeof)
2779 C_DECL_USED (ref) = 1;
2780 else if (DECL_INITIAL (ref) == 0
2781 && DECL_EXTERNAL (ref)
2782 && !TREE_PUBLIC (ref))
2783 record_maybe_used_decl (ref);
2786 if (TREE_CODE (ref) == CONST_DECL)
2788 used_types_insert (TREE_TYPE (ref));
2790 if (warn_cxx_compat
2791 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2792 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2794 warning_at (loc, OPT_Wc___compat,
2795 ("enum constant defined in struct or union "
2796 "is not visible in C++"));
2797 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2800 ref = DECL_INITIAL (ref);
2801 TREE_CONSTANT (ref) = 1;
2803 else if (current_function_decl != 0
2804 && !DECL_FILE_SCOPE_P (current_function_decl)
2805 && (VAR_OR_FUNCTION_DECL_P (ref)
2806 || TREE_CODE (ref) == PARM_DECL))
2808 tree context = decl_function_context (ref);
2810 if (context != 0 && context != current_function_decl)
2811 DECL_NONLOCAL (ref) = 1;
2813 /* C99 6.7.4p3: An inline definition of a function with external
2814 linkage ... shall not contain a reference to an identifier with
2815 internal linkage. */
2816 else if (current_function_decl != 0
2817 && DECL_DECLARED_INLINE_P (current_function_decl)
2818 && DECL_EXTERNAL (current_function_decl)
2819 && VAR_OR_FUNCTION_DECL_P (ref)
2820 && (!VAR_P (ref) || TREE_STATIC (ref))
2821 && ! TREE_PUBLIC (ref)
2822 && DECL_CONTEXT (ref) != current_function_decl)
2823 record_inline_static (loc, current_function_decl, ref,
2824 csi_internal);
2826 return ref;
2829 /* Record details of decls possibly used inside sizeof or typeof. */
2830 struct maybe_used_decl
2832 /* The decl. */
2833 tree decl;
2834 /* The level seen at (in_sizeof + in_typeof). */
2835 int level;
2836 /* The next one at this level or above, or NULL. */
2837 struct maybe_used_decl *next;
2840 static struct maybe_used_decl *maybe_used_decls;
2842 /* Record that DECL, an undefined static function reference seen
2843 inside sizeof or typeof, might be used if the operand of sizeof is
2844 a VLA type or the operand of typeof is a variably modified
2845 type. */
2847 static void
2848 record_maybe_used_decl (tree decl)
2850 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2851 t->decl = decl;
2852 t->level = in_sizeof + in_typeof;
2853 t->next = maybe_used_decls;
2854 maybe_used_decls = t;
2857 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2858 USED is false, just discard them. If it is true, mark them used
2859 (if no longer inside sizeof or typeof) or move them to the next
2860 level up (if still inside sizeof or typeof). */
2862 void
2863 pop_maybe_used (bool used)
2865 struct maybe_used_decl *p = maybe_used_decls;
2866 int cur_level = in_sizeof + in_typeof;
2867 while (p && p->level > cur_level)
2869 if (used)
2871 if (cur_level == 0)
2872 C_DECL_USED (p->decl) = 1;
2873 else
2874 p->level = cur_level;
2876 p = p->next;
2878 if (!used || cur_level == 0)
2879 maybe_used_decls = p;
2882 /* Return the result of sizeof applied to EXPR. */
2884 struct c_expr
2885 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2887 struct c_expr ret;
2888 if (expr.value == error_mark_node)
2890 ret.value = error_mark_node;
2891 ret.original_code = ERROR_MARK;
2892 ret.original_type = NULL;
2893 pop_maybe_used (false);
2895 else
2897 bool expr_const_operands = true;
2899 if (TREE_CODE (expr.value) == PARM_DECL
2900 && C_ARRAY_PARAMETER (expr.value))
2902 if (warning_at (loc, OPT_Wsizeof_array_argument,
2903 "%<sizeof%> on array function parameter %qE will "
2904 "return size of %qT", expr.value,
2905 expr.original_type))
2906 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2908 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2909 &expr_const_operands);
2910 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2911 c_last_sizeof_arg = expr.value;
2912 ret.original_code = SIZEOF_EXPR;
2913 ret.original_type = NULL;
2914 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2916 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2917 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2918 folded_expr, ret.value);
2919 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2920 SET_EXPR_LOCATION (ret.value, loc);
2922 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2924 return ret;
2927 /* Return the result of sizeof applied to T, a structure for the type
2928 name passed to sizeof (rather than the type itself). LOC is the
2929 location of the original expression. */
2931 struct c_expr
2932 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2934 tree type;
2935 struct c_expr ret;
2936 tree type_expr = NULL_TREE;
2937 bool type_expr_const = true;
2938 type = groktypename (t, &type_expr, &type_expr_const);
2939 ret.value = c_sizeof (loc, type);
2940 c_last_sizeof_arg = type;
2941 ret.original_code = SIZEOF_EXPR;
2942 ret.original_type = NULL;
2943 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2944 && c_vla_type_p (type))
2946 /* If the type is a [*] array, it is a VLA but is represented as
2947 having a size of zero. In such a case we must ensure that
2948 the result of sizeof does not get folded to a constant by
2949 c_fully_fold, because if the size is evaluated the result is
2950 not constant and so constraints on zero or negative size
2951 arrays must not be applied when this sizeof call is inside
2952 another array declarator. */
2953 if (!type_expr)
2954 type_expr = integer_zero_node;
2955 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2956 type_expr, ret.value);
2957 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2959 pop_maybe_used (type != error_mark_node
2960 ? C_TYPE_VARIABLE_SIZE (type) : false);
2961 return ret;
2964 /* Build a function call to function FUNCTION with parameters PARAMS.
2965 The function call is at LOC.
2966 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2967 TREE_VALUE of each node is a parameter-expression.
2968 FUNCTION's data type may be a function type or a pointer-to-function. */
2970 tree
2971 build_function_call (location_t loc, tree function, tree params)
2973 vec<tree, va_gc> *v;
2974 tree ret;
2976 vec_alloc (v, list_length (params));
2977 for (; params; params = TREE_CHAIN (params))
2978 v->quick_push (TREE_VALUE (params));
2979 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2980 vec_free (v);
2981 return ret;
2984 /* Give a note about the location of the declaration of DECL. */
2986 static void
2987 inform_declaration (tree decl)
2989 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2990 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2993 /* Build a function call to function FUNCTION with parameters PARAMS.
2994 ORIGTYPES, if not NULL, is a vector of types; each element is
2995 either NULL or the original type of the corresponding element in
2996 PARAMS. The original type may differ from TREE_TYPE of the
2997 parameter for enums. FUNCTION's data type may be a function type
2998 or pointer-to-function. This function changes the elements of
2999 PARAMS. */
3001 tree
3002 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3003 tree function, vec<tree, va_gc> *params,
3004 vec<tree, va_gc> *origtypes)
3006 tree fntype, fundecl = 0;
3007 tree name = NULL_TREE, result;
3008 tree tem;
3009 int nargs;
3010 tree *argarray;
3013 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3014 STRIP_TYPE_NOPS (function);
3016 /* Convert anything with function type to a pointer-to-function. */
3017 if (TREE_CODE (function) == FUNCTION_DECL)
3019 name = DECL_NAME (function);
3021 if (flag_tm)
3022 tm_malloc_replacement (function);
3023 fundecl = function;
3024 /* Atomic functions have type checking/casting already done. They are
3025 often rewritten and don't match the original parameter list. */
3026 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3027 origtypes = NULL;
3029 if (flag_cilkplus
3030 && is_cilkplus_reduce_builtin (function))
3031 origtypes = NULL;
3033 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3034 function = function_to_pointer_conversion (loc, function);
3036 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3037 expressions, like those used for ObjC messenger dispatches. */
3038 if (params && !params->is_empty ())
3039 function = objc_rewrite_function_call (function, (*params)[0]);
3041 function = c_fully_fold (function, false, NULL);
3043 fntype = TREE_TYPE (function);
3045 if (TREE_CODE (fntype) == ERROR_MARK)
3046 return error_mark_node;
3048 if (!(TREE_CODE (fntype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3051 if (!flag_diagnostics_show_caret)
3052 error_at (loc,
3053 "called object %qE is not a function or function pointer",
3054 function);
3055 else if (DECL_P (function))
3057 error_at (loc,
3058 "called object %qD is not a function or function pointer",
3059 function);
3060 inform_declaration (function);
3062 else
3063 error_at (loc,
3064 "called object is not a function or function pointer");
3065 return error_mark_node;
3068 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3069 current_function_returns_abnormally = 1;
3071 /* fntype now gets the type of function pointed to. */
3072 fntype = TREE_TYPE (fntype);
3074 /* Convert the parameters to the types declared in the
3075 function prototype, or apply default promotions. */
3077 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3078 origtypes, function, fundecl);
3079 if (nargs < 0)
3080 return error_mark_node;
3082 /* Check that the function is called through a compatible prototype.
3083 If it is not, warn. */
3084 if (CONVERT_EXPR_P (function)
3085 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3086 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3087 && !comptypes (fntype, TREE_TYPE (tem)))
3089 tree return_type = TREE_TYPE (fntype);
3091 /* This situation leads to run-time undefined behavior. We can't,
3092 therefore, simply error unless we can prove that all possible
3093 executions of the program must execute the code. */
3094 warning_at (loc, 0, "function called through a non-compatible type");
3096 if (VOID_TYPE_P (return_type)
3097 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3098 pedwarn (loc, 0,
3099 "function with qualified void return type called");
3102 argarray = vec_safe_address (params);
3104 /* Check that arguments to builtin functions match the expectations. */
3105 if (fundecl
3106 && DECL_BUILT_IN (fundecl)
3107 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3108 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3109 argarray))
3110 return error_mark_node;
3112 /* Check that the arguments to the function are valid. */
3113 bool warned_p = check_function_arguments (loc, fntype, nargs, argarray);
3115 if (name != NULL_TREE
3116 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3118 if (require_constant_value)
3119 result
3120 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3121 function, nargs, argarray);
3122 else
3123 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3124 function, nargs, argarray);
3125 if (TREE_CODE (result) == NOP_EXPR
3126 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3127 STRIP_TYPE_NOPS (result);
3129 else
3130 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3131 function, nargs, argarray);
3132 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3133 later. */
3134 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3135 TREE_NO_WARNING (result) = 1;
3137 /* In this improbable scenario, a nested function returns a VM type.
3138 Create a TARGET_EXPR so that the call always has a LHS, much as
3139 what the C++ FE does for functions returning non-PODs. */
3140 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3142 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3143 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3144 NULL_TREE, NULL_TREE);
3147 if (VOID_TYPE_P (TREE_TYPE (result)))
3149 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3150 pedwarn (loc, 0,
3151 "function with qualified void return type called");
3152 return result;
3154 return require_complete_type (loc, result);
3157 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3159 tree
3160 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3161 tree function, vec<tree, va_gc> *params,
3162 vec<tree, va_gc> *origtypes)
3164 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3165 STRIP_TYPE_NOPS (function);
3167 /* Convert anything with function type to a pointer-to-function. */
3168 if (TREE_CODE (function) == FUNCTION_DECL)
3170 /* Implement type-directed function overloading for builtins.
3171 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3172 handle all the type checking. The result is a complete expression
3173 that implements this function call. */
3174 tree tem = resolve_overloaded_builtin (loc, function, params);
3175 if (tem)
3176 return tem;
3178 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3181 /* Convert the argument expressions in the vector VALUES
3182 to the types in the list TYPELIST.
3184 If TYPELIST is exhausted, or when an element has NULL as its type,
3185 perform the default conversions.
3187 ORIGTYPES is the original types of the expressions in VALUES. This
3188 holds the type of enum values which have been converted to integral
3189 types. It may be NULL.
3191 FUNCTION is a tree for the called function. It is used only for
3192 error messages, where it is formatted with %qE.
3194 This is also where warnings about wrong number of args are generated.
3196 ARG_LOC are locations of function arguments (if any).
3198 Returns the actual number of arguments processed (which may be less
3199 than the length of VALUES in some error situations), or -1 on
3200 failure. */
3202 static int
3203 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3204 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3205 tree function, tree fundecl)
3207 tree typetail, val;
3208 unsigned int parmnum;
3209 bool error_args = false;
3210 const bool type_generic = fundecl
3211 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3212 bool type_generic_remove_excess_precision = false;
3213 bool type_generic_overflow_p = false;
3214 tree selector;
3216 /* Change pointer to function to the function itself for
3217 diagnostics. */
3218 if (TREE_CODE (function) == ADDR_EXPR
3219 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3220 function = TREE_OPERAND (function, 0);
3222 /* Handle an ObjC selector specially for diagnostics. */
3223 selector = objc_message_selector ();
3225 /* For type-generic built-in functions, determine whether excess
3226 precision should be removed (classification) or not
3227 (comparison). */
3228 if (type_generic
3229 && DECL_BUILT_IN (fundecl)
3230 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3232 switch (DECL_FUNCTION_CODE (fundecl))
3234 case BUILT_IN_ISFINITE:
3235 case BUILT_IN_ISINF:
3236 case BUILT_IN_ISINF_SIGN:
3237 case BUILT_IN_ISNAN:
3238 case BUILT_IN_ISNORMAL:
3239 case BUILT_IN_FPCLASSIFY:
3240 type_generic_remove_excess_precision = true;
3241 break;
3243 case BUILT_IN_ADD_OVERFLOW_P:
3244 case BUILT_IN_SUB_OVERFLOW_P:
3245 case BUILT_IN_MUL_OVERFLOW_P:
3246 /* The last argument of these type-generic builtins
3247 should not be promoted. */
3248 type_generic_overflow_p = true;
3249 break;
3251 default:
3252 break;
3255 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3256 return vec_safe_length (values);
3258 /* Scan the given expressions and types, producing individual
3259 converted arguments. */
3261 for (typetail = typelist, parmnum = 0;
3262 values && values->iterate (parmnum, &val);
3263 ++parmnum)
3265 tree type = typetail ? TREE_VALUE (typetail) : 0;
3266 tree valtype = TREE_TYPE (val);
3267 tree rname = function;
3268 int argnum = parmnum + 1;
3269 const char *invalid_func_diag;
3270 bool excess_precision = false;
3271 bool npc;
3272 tree parmval;
3273 /* Some __atomic_* builtins have additional hidden argument at
3274 position 0. */
3275 location_t ploc
3276 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3277 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3278 : input_location;
3280 if (type == void_type_node)
3282 if (selector)
3283 error_at (loc, "too many arguments to method %qE", selector);
3284 else
3285 error_at (loc, "too many arguments to function %qE", function);
3286 inform_declaration (fundecl);
3287 return error_args ? -1 : (int) parmnum;
3290 if (selector && argnum > 2)
3292 rname = selector;
3293 argnum -= 2;
3296 npc = null_pointer_constant_p (val);
3298 /* If there is excess precision and a prototype, convert once to
3299 the required type rather than converting via the semantic
3300 type. Likewise without a prototype a float value represented
3301 as long double should be converted once to double. But for
3302 type-generic classification functions excess precision must
3303 be removed here. */
3304 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3305 && (type || !type_generic || !type_generic_remove_excess_precision))
3307 val = TREE_OPERAND (val, 0);
3308 excess_precision = true;
3310 val = c_fully_fold (val, false, NULL);
3311 STRIP_TYPE_NOPS (val);
3313 val = require_complete_type (ploc, val);
3315 /* Some floating-point arguments must be promoted to double when
3316 no type is specified by a prototype. This applies to
3317 arguments of type float, and to architecture-specific types
3318 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3319 bool promote_float_arg = false;
3320 if (type == NULL_TREE
3321 && TREE_CODE (valtype) == REAL_TYPE
3322 && (TYPE_PRECISION (valtype)
3323 <= TYPE_PRECISION (double_type_node))
3324 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3325 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3326 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3328 /* Promote this argument, unless it has a _FloatN or
3329 _FloatNx type. */
3330 promote_float_arg = true;
3331 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3332 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3334 promote_float_arg = false;
3335 break;
3339 if (type != 0)
3341 /* Formal parm type is specified by a function prototype. */
3343 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3345 error_at (ploc, "type of formal parameter %d is incomplete",
3346 parmnum + 1);
3347 parmval = val;
3349 else
3351 tree origtype;
3353 /* Optionally warn about conversions that
3354 differ from the default conversions. */
3355 if (warn_traditional_conversion || warn_traditional)
3357 unsigned int formal_prec = TYPE_PRECISION (type);
3359 if (INTEGRAL_TYPE_P (type)
3360 && TREE_CODE (valtype) == REAL_TYPE)
3361 warning_at (ploc, OPT_Wtraditional_conversion,
3362 "passing argument %d of %qE as integer rather "
3363 "than floating due to prototype",
3364 argnum, rname);
3365 if (INTEGRAL_TYPE_P (type)
3366 && TREE_CODE (valtype) == COMPLEX_TYPE)
3367 warning_at (ploc, OPT_Wtraditional_conversion,
3368 "passing argument %d of %qE as integer rather "
3369 "than complex due to prototype",
3370 argnum, rname);
3371 else if (TREE_CODE (type) == COMPLEX_TYPE
3372 && TREE_CODE (valtype) == REAL_TYPE)
3373 warning_at (ploc, OPT_Wtraditional_conversion,
3374 "passing argument %d of %qE as complex rather "
3375 "than floating due to prototype",
3376 argnum, rname);
3377 else if (TREE_CODE (type) == REAL_TYPE
3378 && INTEGRAL_TYPE_P (valtype))
3379 warning_at (ploc, OPT_Wtraditional_conversion,
3380 "passing argument %d of %qE as floating rather "
3381 "than integer due to prototype",
3382 argnum, rname);
3383 else if (TREE_CODE (type) == COMPLEX_TYPE
3384 && INTEGRAL_TYPE_P (valtype))
3385 warning_at (ploc, OPT_Wtraditional_conversion,
3386 "passing argument %d of %qE as complex rather "
3387 "than integer due to prototype",
3388 argnum, rname);
3389 else if (TREE_CODE (type) == REAL_TYPE
3390 && TREE_CODE (valtype) == COMPLEX_TYPE)
3391 warning_at (ploc, OPT_Wtraditional_conversion,
3392 "passing argument %d of %qE as floating rather "
3393 "than complex due to prototype",
3394 argnum, rname);
3395 /* ??? At some point, messages should be written about
3396 conversions between complex types, but that's too messy
3397 to do now. */
3398 else if (TREE_CODE (type) == REAL_TYPE
3399 && TREE_CODE (valtype) == REAL_TYPE)
3401 /* Warn if any argument is passed as `float',
3402 since without a prototype it would be `double'. */
3403 if (formal_prec == TYPE_PRECISION (float_type_node)
3404 && type != dfloat32_type_node)
3405 warning_at (ploc, 0,
3406 "passing argument %d of %qE as %<float%> "
3407 "rather than %<double%> due to prototype",
3408 argnum, rname);
3410 /* Warn if mismatch between argument and prototype
3411 for decimal float types. Warn of conversions with
3412 binary float types and of precision narrowing due to
3413 prototype. */
3414 else if (type != valtype
3415 && (type == dfloat32_type_node
3416 || type == dfloat64_type_node
3417 || type == dfloat128_type_node
3418 || valtype == dfloat32_type_node
3419 || valtype == dfloat64_type_node
3420 || valtype == dfloat128_type_node)
3421 && (formal_prec
3422 <= TYPE_PRECISION (valtype)
3423 || (type == dfloat128_type_node
3424 && (valtype
3425 != dfloat64_type_node
3426 && (valtype
3427 != dfloat32_type_node)))
3428 || (type == dfloat64_type_node
3429 && (valtype
3430 != dfloat32_type_node))))
3431 warning_at (ploc, 0,
3432 "passing argument %d of %qE as %qT "
3433 "rather than %qT due to prototype",
3434 argnum, rname, type, valtype);
3437 /* Detect integer changing in width or signedness.
3438 These warnings are only activated with
3439 -Wtraditional-conversion, not with -Wtraditional. */
3440 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3441 && INTEGRAL_TYPE_P (valtype))
3443 tree would_have_been = default_conversion (val);
3444 tree type1 = TREE_TYPE (would_have_been);
3446 if (TREE_CODE (type) == ENUMERAL_TYPE
3447 && (TYPE_MAIN_VARIANT (type)
3448 == TYPE_MAIN_VARIANT (valtype)))
3449 /* No warning if function asks for enum
3450 and the actual arg is that enum type. */
3452 else if (formal_prec != TYPE_PRECISION (type1))
3453 warning_at (ploc, OPT_Wtraditional_conversion,
3454 "passing argument %d of %qE "
3455 "with different width due to prototype",
3456 argnum, rname);
3457 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3459 /* Don't complain if the formal parameter type
3460 is an enum, because we can't tell now whether
3461 the value was an enum--even the same enum. */
3462 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3464 else if (TREE_CODE (val) == INTEGER_CST
3465 && int_fits_type_p (val, type))
3466 /* Change in signedness doesn't matter
3467 if a constant value is unaffected. */
3469 /* If the value is extended from a narrower
3470 unsigned type, it doesn't matter whether we
3471 pass it as signed or unsigned; the value
3472 certainly is the same either way. */
3473 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3474 && TYPE_UNSIGNED (valtype))
3476 else if (TYPE_UNSIGNED (type))
3477 warning_at (ploc, OPT_Wtraditional_conversion,
3478 "passing argument %d of %qE "
3479 "as unsigned due to prototype",
3480 argnum, rname);
3481 else
3482 warning_at (ploc, OPT_Wtraditional_conversion,
3483 "passing argument %d of %qE "
3484 "as signed due to prototype",
3485 argnum, rname);
3489 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3490 sake of better warnings from convert_and_check. */
3491 if (excess_precision)
3492 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3493 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3494 parmval = convert_for_assignment (loc, ploc, type,
3495 val, origtype, ic_argpass,
3496 npc, fundecl, function,
3497 parmnum + 1);
3499 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3500 && INTEGRAL_TYPE_P (type)
3501 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3502 parmval = default_conversion (parmval);
3505 else if (promote_float_arg)
3507 if (type_generic)
3508 parmval = val;
3509 else
3511 /* Convert `float' to `double'. */
3512 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3513 warning_at (ploc, OPT_Wdouble_promotion,
3514 "implicit conversion from %qT to %qT when passing "
3515 "argument to function",
3516 valtype, double_type_node);
3517 parmval = convert (double_type_node, val);
3520 else if ((excess_precision && !type_generic)
3521 || (type_generic_overflow_p && parmnum == 2))
3522 /* A "double" argument with excess precision being passed
3523 without a prototype or in variable arguments.
3524 The last argument of __builtin_*_overflow_p should not be
3525 promoted. */
3526 parmval = convert (valtype, val);
3527 else if ((invalid_func_diag =
3528 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3530 error (invalid_func_diag);
3531 return -1;
3533 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3535 return -1;
3537 else
3538 /* Convert `short' and `char' to full-size `int'. */
3539 parmval = default_conversion (val);
3541 (*values)[parmnum] = parmval;
3542 if (parmval == error_mark_node)
3543 error_args = true;
3545 if (typetail)
3546 typetail = TREE_CHAIN (typetail);
3549 gcc_assert (parmnum == vec_safe_length (values));
3551 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3553 error_at (loc, "too few arguments to function %qE", function);
3554 inform_declaration (fundecl);
3555 return -1;
3558 return error_args ? -1 : (int) parmnum;
3561 /* This is the entry point used by the parser to build unary operators
3562 in the input. CODE, a tree_code, specifies the unary operator, and
3563 ARG is the operand. For unary plus, the C parser currently uses
3564 CONVERT_EXPR for code.
3566 LOC is the location to use for the tree generated.
3569 struct c_expr
3570 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3572 struct c_expr result;
3574 result.original_code = code;
3575 result.original_type = NULL;
3577 if (reject_gcc_builtin (arg.value))
3579 result.value = error_mark_node;
3581 else
3583 result.value = build_unary_op (loc, code, arg.value, false);
3585 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3586 overflow_warning (loc, result.value);
3589 /* We are typically called when parsing a prefix token at LOC acting on
3590 ARG. Reflect this by updating the source range of the result to
3591 start at LOC and end at the end of ARG. */
3592 set_c_expr_source_range (&result,
3593 loc, arg.get_finish ());
3595 return result;
3598 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3600 static bool
3601 char_type_p (tree type)
3603 return (type == char_type_node
3604 || type == unsigned_char_type_node
3605 || type == signed_char_type_node
3606 || type == char16_type_node
3607 || type == char32_type_node);
3610 /* This is the entry point used by the parser to build binary operators
3611 in the input. CODE, a tree_code, specifies the binary operator, and
3612 ARG1 and ARG2 are the operands. In addition to constructing the
3613 expression, we check for operands that were written with other binary
3614 operators in a way that is likely to confuse the user.
3616 LOCATION is the location of the binary operator. */
3618 struct c_expr
3619 parser_build_binary_op (location_t location, enum tree_code code,
3620 struct c_expr arg1, struct c_expr arg2)
3622 struct c_expr result;
3624 enum tree_code code1 = arg1.original_code;
3625 enum tree_code code2 = arg2.original_code;
3626 tree type1 = (arg1.original_type
3627 ? arg1.original_type
3628 : TREE_TYPE (arg1.value));
3629 tree type2 = (arg2.original_type
3630 ? arg2.original_type
3631 : TREE_TYPE (arg2.value));
3633 result.value = build_binary_op (location, code,
3634 arg1.value, arg2.value, 1);
3635 result.original_code = code;
3636 result.original_type = NULL;
3638 if (TREE_CODE (result.value) == ERROR_MARK)
3640 set_c_expr_source_range (&result,
3641 arg1.get_start (),
3642 arg2.get_finish ());
3643 return result;
3646 if (location != UNKNOWN_LOCATION)
3647 protected_set_expr_location (result.value, location);
3649 set_c_expr_source_range (&result,
3650 arg1.get_start (),
3651 arg2.get_finish ());
3653 /* Check for cases such as x+y<<z which users are likely
3654 to misinterpret. */
3655 if (warn_parentheses)
3656 warn_about_parentheses (location, code, code1, arg1.value, code2,
3657 arg2.value);
3659 if (warn_logical_op)
3660 warn_logical_operator (location, code, TREE_TYPE (result.value),
3661 code1, arg1.value, code2, arg2.value);
3663 if (warn_tautological_compare)
3665 tree lhs = arg1.value;
3666 tree rhs = arg2.value;
3667 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3669 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3670 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3671 lhs = NULL_TREE;
3672 else
3673 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3675 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3677 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3678 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3679 rhs = NULL_TREE;
3680 else
3681 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3683 if (lhs != NULL_TREE && rhs != NULL_TREE)
3684 warn_tautological_cmp (location, code, lhs, rhs);
3687 if (warn_logical_not_paren
3688 && TREE_CODE_CLASS (code) == tcc_comparison
3689 && code1 == TRUTH_NOT_EXPR
3690 && code2 != TRUTH_NOT_EXPR
3691 /* Avoid warning for !!x == y. */
3692 && (TREE_CODE (arg1.value) != NE_EXPR
3693 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3695 /* Avoid warning for !b == y where b has _Bool type. */
3696 tree t = integer_zero_node;
3697 if (TREE_CODE (arg1.value) == EQ_EXPR
3698 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3699 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3701 t = TREE_OPERAND (arg1.value, 0);
3704 if (TREE_TYPE (t) != integer_type_node)
3705 break;
3706 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3707 t = C_MAYBE_CONST_EXPR_EXPR (t);
3708 else if (CONVERT_EXPR_P (t))
3709 t = TREE_OPERAND (t, 0);
3710 else
3711 break;
3713 while (1);
3715 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3716 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3719 /* Warn about comparisons against string literals, with the exception
3720 of testing for equality or inequality of a string literal with NULL. */
3721 if (code == EQ_EXPR || code == NE_EXPR)
3723 if ((code1 == STRING_CST
3724 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3725 || (code2 == STRING_CST
3726 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3727 warning_at (location, OPT_Waddress,
3728 "comparison with string literal results in unspecified behavior");
3729 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3730 if (POINTER_TYPE_P (type1)
3731 && null_pointer_constant_p (arg2.value)
3732 && char_type_p (type2)
3733 && warning_at (location, OPT_Wpointer_compare,
3734 "comparison between pointer and zero character "
3735 "constant"))
3736 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3737 else if (POINTER_TYPE_P (type2)
3738 && null_pointer_constant_p (arg1.value)
3739 && char_type_p (type1)
3740 && warning_at (location, OPT_Wpointer_compare,
3741 "comparison between pointer and zero character "
3742 "constant"))
3743 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3745 else if (TREE_CODE_CLASS (code) == tcc_comparison
3746 && (code1 == STRING_CST || code2 == STRING_CST))
3747 warning_at (location, OPT_Waddress,
3748 "comparison with string literal results in unspecified behavior");
3750 if (TREE_OVERFLOW_P (result.value)
3751 && !TREE_OVERFLOW_P (arg1.value)
3752 && !TREE_OVERFLOW_P (arg2.value))
3753 overflow_warning (location, result.value);
3755 /* Warn about comparisons of different enum types. */
3756 if (warn_enum_compare
3757 && TREE_CODE_CLASS (code) == tcc_comparison
3758 && TREE_CODE (type1) == ENUMERAL_TYPE
3759 && TREE_CODE (type2) == ENUMERAL_TYPE
3760 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3761 warning_at (location, OPT_Wenum_compare,
3762 "comparison between %qT and %qT",
3763 type1, type2);
3765 return result;
3768 /* Return a tree for the difference of pointers OP0 and OP1.
3769 The resulting tree has type int. */
3771 static tree
3772 pointer_diff (location_t loc, tree op0, tree op1)
3774 tree restype = ptrdiff_type_node;
3775 tree result, inttype;
3777 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3778 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3779 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3780 tree orig_op1 = op1;
3782 /* If the operands point into different address spaces, we need to
3783 explicitly convert them to pointers into the common address space
3784 before we can subtract the numerical address values. */
3785 if (as0 != as1)
3787 addr_space_t as_common;
3788 tree common_type;
3790 /* Determine the common superset address space. This is guaranteed
3791 to exist because the caller verified that comp_target_types
3792 returned non-zero. */
3793 if (!addr_space_superset (as0, as1, &as_common))
3794 gcc_unreachable ();
3796 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3797 op0 = convert (common_type, op0);
3798 op1 = convert (common_type, op1);
3801 /* Determine integer type to perform computations in. This will usually
3802 be the same as the result type (ptrdiff_t), but may need to be a wider
3803 type if pointers for the address space are wider than ptrdiff_t. */
3804 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3805 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3806 else
3807 inttype = restype;
3809 if (TREE_CODE (target_type) == VOID_TYPE)
3810 pedwarn (loc, OPT_Wpointer_arith,
3811 "pointer of type %<void *%> used in subtraction");
3812 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3813 pedwarn (loc, OPT_Wpointer_arith,
3814 "pointer to a function used in subtraction");
3816 /* First do the subtraction as integers;
3817 then drop through to build the divide operator.
3818 Do not do default conversions on the minus operator
3819 in case restype is a short type. */
3821 op0 = build_binary_op (loc,
3822 MINUS_EXPR, convert (inttype, op0),
3823 convert (inttype, op1), 0);
3824 /* This generates an error if op1 is pointer to incomplete type. */
3825 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3826 error_at (loc, "arithmetic on pointer to an incomplete type");
3828 op1 = c_size_in_bytes (target_type);
3830 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3831 error_at (loc, "arithmetic on pointer to an empty aggregate");
3833 /* Divide by the size, in easiest possible way. */
3834 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3835 op0, convert (inttype, op1));
3837 /* Convert to final result type if necessary. */
3838 return convert (restype, result);
3841 /* Expand atomic compound assignments into an appropriate sequence as
3842 specified by the C11 standard section 6.5.16.2.
3844 _Atomic T1 E1
3845 T2 E2
3846 E1 op= E2
3848 This sequence is used for all types for which these operations are
3849 supported.
3851 In addition, built-in versions of the 'fe' prefixed routines may
3852 need to be invoked for floating point (real, complex or vector) when
3853 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3855 T1 newval;
3856 T1 old;
3857 T1 *addr
3858 T2 val
3859 fenv_t fenv
3861 addr = &E1;
3862 val = (E2);
3863 __atomic_load (addr, &old, SEQ_CST);
3864 feholdexcept (&fenv);
3865 loop:
3866 newval = old op val;
3867 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3868 SEQ_CST))
3869 goto done;
3870 feclearexcept (FE_ALL_EXCEPT);
3871 goto loop:
3872 done:
3873 feupdateenv (&fenv);
3875 The compiler will issue the __atomic_fetch_* built-in when possible,
3876 otherwise it will generate the generic form of the atomic operations.
3877 This requires temp(s) and has their address taken. The atomic processing
3878 is smart enough to figure out when the size of an object can utilize
3879 a lock-free version, and convert the built-in call to the appropriate
3880 lock-free routine. The optimizers will then dispose of any temps that
3881 are no longer required, and lock-free implementations are utilized as
3882 long as there is target support for the required size.
3884 If the operator is NOP_EXPR, then this is a simple assignment, and
3885 an __atomic_store is issued to perform the assignment rather than
3886 the above loop. */
3888 /* Build an atomic assignment at LOC, expanding into the proper
3889 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3890 the result of the operation, unless RETURN_OLD_P, in which case
3891 return the old value of LHS (this is only for postincrement and
3892 postdecrement). */
3894 static tree
3895 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3896 tree rhs, bool return_old_p)
3898 tree fndecl, func_call;
3899 vec<tree, va_gc> *params;
3900 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3901 tree old, old_addr;
3902 tree compound_stmt;
3903 tree stmt, goto_stmt;
3904 tree loop_label, loop_decl, done_label, done_decl;
3906 tree lhs_type = TREE_TYPE (lhs);
3907 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3908 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3909 tree rhs_type = TREE_TYPE (rhs);
3911 gcc_assert (TYPE_ATOMIC (lhs_type));
3913 if (return_old_p)
3914 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3916 /* Allocate enough vector items for a compare_exchange. */
3917 vec_alloc (params, 6);
3919 /* Create a compound statement to hold the sequence of statements
3920 with a loop. */
3921 compound_stmt = c_begin_compound_stmt (false);
3923 /* Fold the RHS if it hasn't already been folded. */
3924 if (modifycode != NOP_EXPR)
3925 rhs = c_fully_fold (rhs, false, NULL);
3927 /* Remove the qualifiers for the rest of the expressions and create
3928 the VAL temp variable to hold the RHS. */
3929 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3930 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3931 val = create_tmp_var_raw (nonatomic_rhs_type);
3932 TREE_ADDRESSABLE (val) = 1;
3933 TREE_NO_WARNING (val) = 1;
3934 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3935 NULL_TREE);
3936 SET_EXPR_LOCATION (rhs, loc);
3937 add_stmt (rhs);
3939 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3940 an atomic_store. */
3941 if (modifycode == NOP_EXPR)
3943 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3944 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3945 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3946 params->quick_push (lhs_addr);
3947 params->quick_push (rhs);
3948 params->quick_push (seq_cst);
3949 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3950 add_stmt (func_call);
3952 /* Finish the compound statement. */
3953 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3955 /* VAL is the value which was stored, return a COMPOUND_STMT of
3956 the statement and that value. */
3957 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3960 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3961 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3962 isn't applicable for such builtins. ??? Do we want to handle enums? */
3963 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3964 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3966 built_in_function fncode;
3967 switch (modifycode)
3969 case PLUS_EXPR:
3970 case POINTER_PLUS_EXPR:
3971 fncode = (return_old_p
3972 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3973 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3974 break;
3975 case MINUS_EXPR:
3976 fncode = (return_old_p
3977 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3978 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3979 break;
3980 case BIT_AND_EXPR:
3981 fncode = (return_old_p
3982 ? BUILT_IN_ATOMIC_FETCH_AND_N
3983 : BUILT_IN_ATOMIC_AND_FETCH_N);
3984 break;
3985 case BIT_IOR_EXPR:
3986 fncode = (return_old_p
3987 ? BUILT_IN_ATOMIC_FETCH_OR_N
3988 : BUILT_IN_ATOMIC_OR_FETCH_N);
3989 break;
3990 case BIT_XOR_EXPR:
3991 fncode = (return_old_p
3992 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3993 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3994 break;
3995 default:
3996 goto cas_loop;
3999 /* We can only use "_1" through "_16" variants of the atomic fetch
4000 built-ins. */
4001 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4002 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4003 goto cas_loop;
4005 /* If this is a pointer type, we need to multiply by the size of
4006 the pointer target type. */
4007 if (POINTER_TYPE_P (lhs_type))
4009 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4010 /* ??? This would introduce -Wdiscarded-qualifiers
4011 warning: __atomic_fetch_* expect volatile void *
4012 type as the first argument. (Assignments between
4013 atomic and non-atomic objects are OK.) */
4014 || TYPE_RESTRICT (lhs_type))
4015 goto cas_loop;
4016 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4017 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4018 convert (ptrdiff_type_node, rhs),
4019 convert (ptrdiff_type_node, sz));
4022 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4023 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4024 fndecl = builtin_decl_explicit (fncode);
4025 params->quick_push (lhs_addr);
4026 params->quick_push (rhs);
4027 params->quick_push (seq_cst);
4028 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4030 newval = create_tmp_var_raw (nonatomic_lhs_type);
4031 TREE_ADDRESSABLE (newval) = 1;
4032 TREE_NO_WARNING (newval) = 1;
4033 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4034 NULL_TREE, NULL_TREE);
4035 SET_EXPR_LOCATION (rhs, loc);
4036 add_stmt (rhs);
4038 /* Finish the compound statement. */
4039 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4041 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4042 the statement and that value. */
4043 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4046 cas_loop:
4047 /* Create the variables and labels required for the op= form. */
4048 old = create_tmp_var_raw (nonatomic_lhs_type);
4049 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4050 TREE_ADDRESSABLE (old) = 1;
4051 TREE_NO_WARNING (old) = 1;
4053 newval = create_tmp_var_raw (nonatomic_lhs_type);
4054 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4055 TREE_ADDRESSABLE (newval) = 1;
4056 TREE_NO_WARNING (newval) = 1;
4058 loop_decl = create_artificial_label (loc);
4059 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4061 done_decl = create_artificial_label (loc);
4062 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4064 /* __atomic_load (addr, &old, SEQ_CST). */
4065 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4066 params->quick_push (lhs_addr);
4067 params->quick_push (old_addr);
4068 params->quick_push (seq_cst);
4069 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4070 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4071 NULL_TREE);
4072 add_stmt (old);
4073 params->truncate (0);
4075 /* Create the expressions for floating-point environment
4076 manipulation, if required. */
4077 bool need_fenv = (flag_trapping_math
4078 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4079 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4080 if (need_fenv)
4081 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4083 if (hold_call)
4084 add_stmt (hold_call);
4086 /* loop: */
4087 add_stmt (loop_label);
4089 /* newval = old + val; */
4090 rhs = build_binary_op (loc, modifycode, old, val, 1);
4091 rhs = c_fully_fold (rhs, false, NULL);
4092 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4093 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4094 NULL_TREE, 0);
4095 if (rhs != error_mark_node)
4097 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4098 NULL_TREE);
4099 SET_EXPR_LOCATION (rhs, loc);
4100 add_stmt (rhs);
4103 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4104 goto done; */
4105 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4106 params->quick_push (lhs_addr);
4107 params->quick_push (old_addr);
4108 params->quick_push (newval_addr);
4109 params->quick_push (integer_zero_node);
4110 params->quick_push (seq_cst);
4111 params->quick_push (seq_cst);
4112 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4114 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4115 SET_EXPR_LOCATION (goto_stmt, loc);
4117 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4118 SET_EXPR_LOCATION (stmt, loc);
4119 add_stmt (stmt);
4121 if (clear_call)
4122 add_stmt (clear_call);
4124 /* goto loop; */
4125 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4126 SET_EXPR_LOCATION (goto_stmt, loc);
4127 add_stmt (goto_stmt);
4129 /* done: */
4130 add_stmt (done_label);
4132 if (update_call)
4133 add_stmt (update_call);
4135 /* Finish the compound statement. */
4136 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4138 /* NEWVAL is the value that was successfully stored, return a
4139 COMPOUND_EXPR of the statement and the appropriate value. */
4140 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4141 return_old_p ? old : newval);
4144 /* Construct and perhaps optimize a tree representation
4145 for a unary operation. CODE, a tree_code, specifies the operation
4146 and XARG is the operand.
4147 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4148 promotions (such as from short to int).
4149 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4150 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4151 to pointers in C99.
4153 LOCATION is the location of the operator. */
4155 tree
4156 build_unary_op (location_t location, enum tree_code code, tree xarg,
4157 bool noconvert)
4159 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4160 tree arg = xarg;
4161 tree argtype = 0;
4162 enum tree_code typecode;
4163 tree val;
4164 tree ret = error_mark_node;
4165 tree eptype = NULL_TREE;
4166 const char *invalid_op_diag;
4167 bool int_operands;
4169 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4170 if (int_operands)
4171 arg = remove_c_maybe_const_expr (arg);
4173 if (code != ADDR_EXPR)
4174 arg = require_complete_type (location, arg);
4176 typecode = TREE_CODE (TREE_TYPE (arg));
4177 if (typecode == ERROR_MARK)
4178 return error_mark_node;
4179 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4180 typecode = INTEGER_TYPE;
4182 if ((invalid_op_diag
4183 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4185 error_at (location, invalid_op_diag);
4186 return error_mark_node;
4189 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4191 eptype = TREE_TYPE (arg);
4192 arg = TREE_OPERAND (arg, 0);
4195 switch (code)
4197 case CONVERT_EXPR:
4198 /* This is used for unary plus, because a CONVERT_EXPR
4199 is enough to prevent anybody from looking inside for
4200 associativity, but won't generate any code. */
4201 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4202 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4203 || typecode == VECTOR_TYPE))
4205 error_at (location, "wrong type argument to unary plus");
4206 return error_mark_node;
4208 else if (!noconvert)
4209 arg = default_conversion (arg);
4210 arg = non_lvalue_loc (location, arg);
4211 break;
4213 case NEGATE_EXPR:
4214 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4215 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4216 || typecode == VECTOR_TYPE))
4218 error_at (location, "wrong type argument to unary minus");
4219 return error_mark_node;
4221 else if (!noconvert)
4222 arg = default_conversion (arg);
4223 break;
4225 case BIT_NOT_EXPR:
4226 /* ~ works on integer types and non float vectors. */
4227 if (typecode == INTEGER_TYPE
4228 || (typecode == VECTOR_TYPE
4229 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4231 tree e = arg;
4233 /* Warn if the expression has boolean value. */
4234 while (TREE_CODE (e) == COMPOUND_EXPR)
4235 e = TREE_OPERAND (e, 1);
4237 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4238 || truth_value_p (TREE_CODE (e)))
4239 && warning_at (location, OPT_Wbool_operation,
4240 "%<~%> on a boolean expression"))
4242 gcc_rich_location richloc (location);
4243 richloc.add_fixit_insert_before (location, "!");
4244 inform_at_rich_loc (&richloc, "did you mean to use logical "
4245 "not?");
4247 if (!noconvert)
4248 arg = default_conversion (arg);
4250 else if (typecode == COMPLEX_TYPE)
4252 code = CONJ_EXPR;
4253 pedwarn (location, OPT_Wpedantic,
4254 "ISO C does not support %<~%> for complex conjugation");
4255 if (!noconvert)
4256 arg = default_conversion (arg);
4258 else
4260 error_at (location, "wrong type argument to bit-complement");
4261 return error_mark_node;
4263 break;
4265 case ABS_EXPR:
4266 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4268 error_at (location, "wrong type argument to abs");
4269 return error_mark_node;
4271 else if (!noconvert)
4272 arg = default_conversion (arg);
4273 break;
4275 case CONJ_EXPR:
4276 /* Conjugating a real value is a no-op, but allow it anyway. */
4277 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4278 || typecode == COMPLEX_TYPE))
4280 error_at (location, "wrong type argument to conjugation");
4281 return error_mark_node;
4283 else if (!noconvert)
4284 arg = default_conversion (arg);
4285 break;
4287 case TRUTH_NOT_EXPR:
4288 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4289 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4290 && typecode != COMPLEX_TYPE)
4292 error_at (location,
4293 "wrong type argument to unary exclamation mark");
4294 return error_mark_node;
4296 if (int_operands)
4298 arg = c_objc_common_truthvalue_conversion (location, xarg);
4299 arg = remove_c_maybe_const_expr (arg);
4301 else
4302 arg = c_objc_common_truthvalue_conversion (location, arg);
4303 ret = invert_truthvalue_loc (location, arg);
4304 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4305 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4306 location = EXPR_LOCATION (ret);
4307 goto return_build_unary_op;
4309 case REALPART_EXPR:
4310 case IMAGPART_EXPR:
4311 ret = build_real_imag_expr (location, code, arg);
4312 if (ret == error_mark_node)
4313 return error_mark_node;
4314 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4315 eptype = TREE_TYPE (eptype);
4316 goto return_build_unary_op;
4318 case PREINCREMENT_EXPR:
4319 case POSTINCREMENT_EXPR:
4320 case PREDECREMENT_EXPR:
4321 case POSTDECREMENT_EXPR:
4323 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4325 tree inner = build_unary_op (location, code,
4326 C_MAYBE_CONST_EXPR_EXPR (arg),
4327 noconvert);
4328 if (inner == error_mark_node)
4329 return error_mark_node;
4330 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4331 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4332 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4333 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4334 goto return_build_unary_op;
4337 /* Complain about anything that is not a true lvalue. In
4338 Objective-C, skip this check for property_refs. */
4339 if (!objc_is_property_ref (arg)
4340 && !lvalue_or_else (location,
4341 arg, ((code == PREINCREMENT_EXPR
4342 || code == POSTINCREMENT_EXPR)
4343 ? lv_increment
4344 : lv_decrement)))
4345 return error_mark_node;
4347 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4349 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4350 warning_at (location, OPT_Wc___compat,
4351 "increment of enumeration value is invalid in C++");
4352 else
4353 warning_at (location, OPT_Wc___compat,
4354 "decrement of enumeration value is invalid in C++");
4357 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4359 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4360 warning_at (location, OPT_Wbool_operation,
4361 "increment of a boolean expression");
4362 else
4363 warning_at (location, OPT_Wbool_operation,
4364 "decrement of a boolean expression");
4367 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4368 arg = c_fully_fold (arg, false, NULL);
4370 bool atomic_op;
4371 atomic_op = really_atomic_lvalue (arg);
4373 /* Increment or decrement the real part of the value,
4374 and don't change the imaginary part. */
4375 if (typecode == COMPLEX_TYPE)
4377 tree real, imag;
4379 pedwarn (location, OPT_Wpedantic,
4380 "ISO C does not support %<++%> and %<--%> on complex types");
4382 if (!atomic_op)
4384 arg = stabilize_reference (arg);
4385 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4386 true);
4387 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4388 true);
4389 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4390 if (real == error_mark_node || imag == error_mark_node)
4391 return error_mark_node;
4392 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4393 real, imag);
4394 goto return_build_unary_op;
4398 /* Report invalid types. */
4400 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4401 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4402 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4404 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4405 error_at (location, "wrong type argument to increment");
4406 else
4407 error_at (location, "wrong type argument to decrement");
4409 return error_mark_node;
4413 tree inc;
4415 argtype = TREE_TYPE (arg);
4417 /* Compute the increment. */
4419 if (typecode == POINTER_TYPE)
4421 /* If pointer target is an incomplete type,
4422 we just cannot know how to do the arithmetic. */
4423 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4425 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4426 error_at (location,
4427 "increment of pointer to an incomplete type %qT",
4428 TREE_TYPE (argtype));
4429 else
4430 error_at (location,
4431 "decrement of pointer to an incomplete type %qT",
4432 TREE_TYPE (argtype));
4434 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4435 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4437 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4438 pedwarn (location, OPT_Wpointer_arith,
4439 "wrong type argument to increment");
4440 else
4441 pedwarn (location, OPT_Wpointer_arith,
4442 "wrong type argument to decrement");
4445 inc = c_size_in_bytes (TREE_TYPE (argtype));
4446 inc = convert_to_ptrofftype_loc (location, inc);
4448 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4450 /* For signed fract types, we invert ++ to -- or
4451 -- to ++, and change inc from 1 to -1, because
4452 it is not possible to represent 1 in signed fract constants.
4453 For unsigned fract types, the result always overflows and
4454 we get an undefined (original) or the maximum value. */
4455 if (code == PREINCREMENT_EXPR)
4456 code = PREDECREMENT_EXPR;
4457 else if (code == PREDECREMENT_EXPR)
4458 code = PREINCREMENT_EXPR;
4459 else if (code == POSTINCREMENT_EXPR)
4460 code = POSTDECREMENT_EXPR;
4461 else /* code == POSTDECREMENT_EXPR */
4462 code = POSTINCREMENT_EXPR;
4464 inc = integer_minus_one_node;
4465 inc = convert (argtype, inc);
4467 else
4469 inc = VECTOR_TYPE_P (argtype)
4470 ? build_one_cst (argtype)
4471 : integer_one_node;
4472 inc = convert (argtype, inc);
4475 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4476 need to ask Objective-C to build the increment or decrement
4477 expression for it. */
4478 if (objc_is_property_ref (arg))
4479 return objc_build_incr_expr_for_property_ref (location, code,
4480 arg, inc);
4482 /* Report a read-only lvalue. */
4483 if (TYPE_READONLY (argtype))
4485 readonly_error (location, arg,
4486 ((code == PREINCREMENT_EXPR
4487 || code == POSTINCREMENT_EXPR)
4488 ? lv_increment : lv_decrement));
4489 return error_mark_node;
4491 else if (TREE_READONLY (arg))
4492 readonly_warning (arg,
4493 ((code == PREINCREMENT_EXPR
4494 || code == POSTINCREMENT_EXPR)
4495 ? lv_increment : lv_decrement));
4497 /* If the argument is atomic, use the special code sequences for
4498 atomic compound assignment. */
4499 if (atomic_op)
4501 arg = stabilize_reference (arg);
4502 ret = build_atomic_assign (location, arg,
4503 ((code == PREINCREMENT_EXPR
4504 || code == POSTINCREMENT_EXPR)
4505 ? PLUS_EXPR
4506 : MINUS_EXPR),
4507 (FRACT_MODE_P (TYPE_MODE (argtype))
4508 ? inc
4509 : integer_one_node),
4510 (code == POSTINCREMENT_EXPR
4511 || code == POSTDECREMENT_EXPR));
4512 goto return_build_unary_op;
4515 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4516 val = boolean_increment (code, arg);
4517 else
4518 val = build2 (code, TREE_TYPE (arg), arg, inc);
4519 TREE_SIDE_EFFECTS (val) = 1;
4520 if (TREE_CODE (val) != code)
4521 TREE_NO_WARNING (val) = 1;
4522 ret = val;
4523 goto return_build_unary_op;
4526 case ADDR_EXPR:
4527 /* Note that this operation never does default_conversion. */
4529 /* The operand of unary '&' must be an lvalue (which excludes
4530 expressions of type void), or, in C99, the result of a [] or
4531 unary '*' operator. */
4532 if (VOID_TYPE_P (TREE_TYPE (arg))
4533 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4534 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4535 pedwarn (location, 0, "taking address of expression of type %<void%>");
4537 /* Let &* cancel out to simplify resulting code. */
4538 if (INDIRECT_REF_P (arg))
4540 /* Don't let this be an lvalue. */
4541 if (lvalue_p (TREE_OPERAND (arg, 0)))
4542 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4543 ret = TREE_OPERAND (arg, 0);
4544 goto return_build_unary_op;
4547 /* Anything not already handled and not a true memory reference
4548 or a non-lvalue array is an error. */
4549 if (typecode != FUNCTION_TYPE && !noconvert
4550 && !lvalue_or_else (location, arg, lv_addressof))
4551 return error_mark_node;
4553 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4554 folding later. */
4555 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4557 tree inner = build_unary_op (location, code,
4558 C_MAYBE_CONST_EXPR_EXPR (arg),
4559 noconvert);
4560 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4561 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4562 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4563 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4564 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4565 goto return_build_unary_op;
4568 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4569 argtype = TREE_TYPE (arg);
4571 /* If the lvalue is const or volatile, merge that into the type
4572 to which the address will point. This is only needed
4573 for function types. */
4574 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4575 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4576 && TREE_CODE (argtype) == FUNCTION_TYPE)
4578 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4579 int quals = orig_quals;
4581 if (TREE_READONLY (arg))
4582 quals |= TYPE_QUAL_CONST;
4583 if (TREE_THIS_VOLATILE (arg))
4584 quals |= TYPE_QUAL_VOLATILE;
4586 argtype = c_build_qualified_type (argtype, quals);
4589 switch (TREE_CODE (arg))
4591 case COMPONENT_REF:
4592 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4594 error_at (location, "cannot take address of bit-field %qD",
4595 TREE_OPERAND (arg, 1));
4596 return error_mark_node;
4599 /* fall through */
4601 case ARRAY_REF:
4602 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4604 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4605 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4607 error_at (location, "cannot take address of scalar with "
4608 "reverse storage order");
4609 return error_mark_node;
4612 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4613 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4614 warning_at (location, OPT_Wscalar_storage_order,
4615 "address of array with reverse scalar storage "
4616 "order requested");
4619 default:
4620 break;
4623 if (!c_mark_addressable (arg))
4624 return error_mark_node;
4626 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4627 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4629 argtype = build_pointer_type (argtype);
4631 /* ??? Cope with user tricks that amount to offsetof. Delete this
4632 when we have proper support for integer constant expressions. */
4633 val = get_base_address (arg);
4634 if (val && INDIRECT_REF_P (val)
4635 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4637 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4638 goto return_build_unary_op;
4641 val = build1 (ADDR_EXPR, argtype, arg);
4643 ret = val;
4644 goto return_build_unary_op;
4646 default:
4647 gcc_unreachable ();
4650 if (argtype == 0)
4651 argtype = TREE_TYPE (arg);
4652 if (TREE_CODE (arg) == INTEGER_CST)
4653 ret = (require_constant_value
4654 ? fold_build1_initializer_loc (location, code, argtype, arg)
4655 : fold_build1_loc (location, code, argtype, arg));
4656 else
4657 ret = build1 (code, argtype, arg);
4658 return_build_unary_op:
4659 gcc_assert (ret != error_mark_node);
4660 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4661 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4662 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4663 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4664 ret = note_integer_operands (ret);
4665 if (eptype)
4666 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4667 protected_set_expr_location (ret, location);
4668 return ret;
4671 /* Return nonzero if REF is an lvalue valid for this language.
4672 Lvalues can be assigned, unless their type has TYPE_READONLY.
4673 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4675 bool
4676 lvalue_p (const_tree ref)
4678 const enum tree_code code = TREE_CODE (ref);
4680 switch (code)
4682 case REALPART_EXPR:
4683 case IMAGPART_EXPR:
4684 case COMPONENT_REF:
4685 return lvalue_p (TREE_OPERAND (ref, 0));
4687 case C_MAYBE_CONST_EXPR:
4688 return lvalue_p (TREE_OPERAND (ref, 1));
4690 case COMPOUND_LITERAL_EXPR:
4691 case STRING_CST:
4692 return true;
4694 case INDIRECT_REF:
4695 case ARRAY_REF:
4696 case ARRAY_NOTATION_REF:
4697 case VAR_DECL:
4698 case PARM_DECL:
4699 case RESULT_DECL:
4700 case ERROR_MARK:
4701 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4702 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4704 case BIND_EXPR:
4705 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4707 default:
4708 return false;
4712 /* Give a warning for storing in something that is read-only in GCC
4713 terms but not const in ISO C terms. */
4715 static void
4716 readonly_warning (tree arg, enum lvalue_use use)
4718 switch (use)
4720 case lv_assign:
4721 warning (0, "assignment of read-only location %qE", arg);
4722 break;
4723 case lv_increment:
4724 warning (0, "increment of read-only location %qE", arg);
4725 break;
4726 case lv_decrement:
4727 warning (0, "decrement of read-only location %qE", arg);
4728 break;
4729 default:
4730 gcc_unreachable ();
4732 return;
4736 /* Return nonzero if REF is an lvalue valid for this language;
4737 otherwise, print an error message and return zero. USE says
4738 how the lvalue is being used and so selects the error message.
4739 LOCATION is the location at which any error should be reported. */
4741 static int
4742 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4744 int win = lvalue_p (ref);
4746 if (!win)
4747 lvalue_error (loc, use);
4749 return win;
4752 /* Mark EXP saying that we need to be able to take the
4753 address of it; it should not be allocated in a register.
4754 Returns true if successful. */
4756 bool
4757 c_mark_addressable (tree exp)
4759 tree x = exp;
4761 while (1)
4762 switch (TREE_CODE (x))
4764 case COMPONENT_REF:
4765 case ADDR_EXPR:
4766 case ARRAY_REF:
4767 case REALPART_EXPR:
4768 case IMAGPART_EXPR:
4769 x = TREE_OPERAND (x, 0);
4770 break;
4772 case COMPOUND_LITERAL_EXPR:
4773 case CONSTRUCTOR:
4774 TREE_ADDRESSABLE (x) = 1;
4775 return true;
4777 case VAR_DECL:
4778 case CONST_DECL:
4779 case PARM_DECL:
4780 case RESULT_DECL:
4781 if (C_DECL_REGISTER (x)
4782 && DECL_NONLOCAL (x))
4784 if (TREE_PUBLIC (x) || is_global_var (x))
4786 error
4787 ("global register variable %qD used in nested function", x);
4788 return false;
4790 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4792 else if (C_DECL_REGISTER (x))
4794 if (TREE_PUBLIC (x) || is_global_var (x))
4795 error ("address of global register variable %qD requested", x);
4796 else
4797 error ("address of register variable %qD requested", x);
4798 return false;
4801 /* FALLTHRU */
4802 case FUNCTION_DECL:
4803 TREE_ADDRESSABLE (x) = 1;
4804 /* FALLTHRU */
4805 default:
4806 return true;
4810 /* Convert EXPR to TYPE, warning about conversion problems with
4811 constants. SEMANTIC_TYPE is the type this conversion would use
4812 without excess precision. If SEMANTIC_TYPE is NULL, this function
4813 is equivalent to convert_and_check. This function is a wrapper that
4814 handles conversions that may be different than
4815 the usual ones because of excess precision. */
4817 static tree
4818 ep_convert_and_check (location_t loc, tree type, tree expr,
4819 tree semantic_type)
4821 if (TREE_TYPE (expr) == type)
4822 return expr;
4824 if (!semantic_type)
4825 return convert_and_check (loc, type, expr);
4827 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4828 && TREE_TYPE (expr) != semantic_type)
4830 /* For integers, we need to check the real conversion, not
4831 the conversion to the excess precision type. */
4832 expr = convert_and_check (loc, semantic_type, expr);
4834 /* Result type is the excess precision type, which should be
4835 large enough, so do not check. */
4836 return convert (type, expr);
4839 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4840 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4841 if folded to an integer constant then the unselected half may
4842 contain arbitrary operations not normally permitted in constant
4843 expressions. Set the location of the expression to LOC. */
4845 tree
4846 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4847 tree op1, tree op1_original_type, tree op2,
4848 tree op2_original_type)
4850 tree type1;
4851 tree type2;
4852 enum tree_code code1;
4853 enum tree_code code2;
4854 tree result_type = NULL;
4855 tree semantic_result_type = NULL;
4856 tree orig_op1 = op1, orig_op2 = op2;
4857 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4858 bool ifexp_int_operands;
4859 tree ret;
4861 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4862 if (op1_int_operands)
4863 op1 = remove_c_maybe_const_expr (op1);
4864 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4865 if (op2_int_operands)
4866 op2 = remove_c_maybe_const_expr (op2);
4867 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4868 if (ifexp_int_operands)
4869 ifexp = remove_c_maybe_const_expr (ifexp);
4871 /* Promote both alternatives. */
4873 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4874 op1 = default_conversion (op1);
4875 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4876 op2 = default_conversion (op2);
4878 if (TREE_CODE (ifexp) == ERROR_MARK
4879 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4880 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4881 return error_mark_node;
4883 type1 = TREE_TYPE (op1);
4884 code1 = TREE_CODE (type1);
4885 type2 = TREE_TYPE (op2);
4886 code2 = TREE_CODE (type2);
4888 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4889 return error_mark_node;
4891 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4892 return error_mark_node;
4894 /* C90 does not permit non-lvalue arrays in conditional expressions.
4895 In C99 they will be pointers by now. */
4896 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4898 error_at (colon_loc, "non-lvalue array in conditional expression");
4899 return error_mark_node;
4902 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4903 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4904 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4905 || code1 == COMPLEX_TYPE)
4906 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4907 || code2 == COMPLEX_TYPE))
4909 semantic_result_type = c_common_type (type1, type2);
4910 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4912 op1 = TREE_OPERAND (op1, 0);
4913 type1 = TREE_TYPE (op1);
4914 gcc_assert (TREE_CODE (type1) == code1);
4916 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4918 op2 = TREE_OPERAND (op2, 0);
4919 type2 = TREE_TYPE (op2);
4920 gcc_assert (TREE_CODE (type2) == code2);
4924 if (warn_cxx_compat)
4926 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4927 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4929 if (TREE_CODE (t1) == ENUMERAL_TYPE
4930 && TREE_CODE (t2) == ENUMERAL_TYPE
4931 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4932 warning_at (colon_loc, OPT_Wc___compat,
4933 ("different enum types in conditional is "
4934 "invalid in C++: %qT vs %qT"),
4935 t1, t2);
4938 /* Quickly detect the usual case where op1 and op2 have the same type
4939 after promotion. */
4940 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4942 if (type1 == type2)
4943 result_type = type1;
4944 else
4945 result_type = TYPE_MAIN_VARIANT (type1);
4947 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4948 || code1 == COMPLEX_TYPE)
4949 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4950 || code2 == COMPLEX_TYPE))
4952 result_type = c_common_type (type1, type2);
4953 if (result_type == error_mark_node)
4954 return error_mark_node;
4955 do_warn_double_promotion (result_type, type1, type2,
4956 "implicit conversion from %qT to %qT to "
4957 "match other result of conditional",
4958 colon_loc);
4960 /* If -Wsign-compare, warn here if type1 and type2 have
4961 different signedness. We'll promote the signed to unsigned
4962 and later code won't know it used to be different.
4963 Do this check on the original types, so that explicit casts
4964 will be considered, but default promotions won't. */
4965 if (c_inhibit_evaluation_warnings == 0)
4967 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4968 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4970 if (unsigned_op1 ^ unsigned_op2)
4972 bool ovf;
4974 /* Do not warn if the result type is signed, since the
4975 signed type will only be chosen if it can represent
4976 all the values of the unsigned type. */
4977 if (!TYPE_UNSIGNED (result_type))
4978 /* OK */;
4979 else
4981 bool op1_maybe_const = true;
4982 bool op2_maybe_const = true;
4984 /* Do not warn if the signed quantity is an
4985 unsuffixed integer literal (or some static
4986 constant expression involving such literals) and
4987 it is non-negative. This warning requires the
4988 operands to be folded for best results, so do
4989 that folding in this case even without
4990 warn_sign_compare to avoid warning options
4991 possibly affecting code generation. */
4992 c_inhibit_evaluation_warnings
4993 += (ifexp == truthvalue_false_node);
4994 op1 = c_fully_fold (op1, require_constant_value,
4995 &op1_maybe_const);
4996 c_inhibit_evaluation_warnings
4997 -= (ifexp == truthvalue_false_node);
4999 c_inhibit_evaluation_warnings
5000 += (ifexp == truthvalue_true_node);
5001 op2 = c_fully_fold (op2, require_constant_value,
5002 &op2_maybe_const);
5003 c_inhibit_evaluation_warnings
5004 -= (ifexp == truthvalue_true_node);
5006 if (warn_sign_compare)
5008 if ((unsigned_op2
5009 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5010 || (unsigned_op1
5011 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5012 /* OK */;
5013 else
5014 warning_at (colon_loc, OPT_Wsign_compare,
5015 ("signed and unsigned type in "
5016 "conditional expression"));
5018 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5019 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5020 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5021 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5026 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5028 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5029 pedwarn (colon_loc, OPT_Wpedantic,
5030 "ISO C forbids conditional expr with only one void side");
5031 result_type = void_type_node;
5033 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5035 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5036 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5037 addr_space_t as_common;
5039 if (comp_target_types (colon_loc, type1, type2))
5040 result_type = common_pointer_type (type1, type2);
5041 else if (null_pointer_constant_p (orig_op1))
5042 result_type = type2;
5043 else if (null_pointer_constant_p (orig_op2))
5044 result_type = type1;
5045 else if (!addr_space_superset (as1, as2, &as_common))
5047 error_at (colon_loc, "pointers to disjoint address spaces "
5048 "used in conditional expression");
5049 return error_mark_node;
5051 else if (VOID_TYPE_P (TREE_TYPE (type1))
5052 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5054 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5055 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5056 & ~TYPE_QUALS (TREE_TYPE (type1))))
5057 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5058 "pointer to array loses qualifier "
5059 "in conditional expression");
5061 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5062 pedwarn (colon_loc, OPT_Wpedantic,
5063 "ISO C forbids conditional expr between "
5064 "%<void *%> and function pointer");
5065 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5066 TREE_TYPE (type2)));
5068 else if (VOID_TYPE_P (TREE_TYPE (type2))
5069 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5071 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5072 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5073 & ~TYPE_QUALS (TREE_TYPE (type2))))
5074 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5075 "pointer to array loses qualifier "
5076 "in conditional expression");
5078 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5079 pedwarn (colon_loc, OPT_Wpedantic,
5080 "ISO C forbids conditional expr between "
5081 "%<void *%> and function pointer");
5082 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5083 TREE_TYPE (type1)));
5085 /* Objective-C pointer comparisons are a bit more lenient. */
5086 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5087 result_type = objc_common_type (type1, type2);
5088 else
5090 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5092 pedwarn (colon_loc, 0,
5093 "pointer type mismatch in conditional expression");
5094 result_type = build_pointer_type
5095 (build_qualified_type (void_type_node, qual));
5098 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5100 if (!null_pointer_constant_p (orig_op2))
5101 pedwarn (colon_loc, 0,
5102 "pointer/integer type mismatch in conditional expression");
5103 else
5105 op2 = null_pointer_node;
5107 result_type = type1;
5109 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5111 if (!null_pointer_constant_p (orig_op1))
5112 pedwarn (colon_loc, 0,
5113 "pointer/integer type mismatch in conditional expression");
5114 else
5116 op1 = null_pointer_node;
5118 result_type = type2;
5121 if (!result_type)
5123 if (flag_cond_mismatch)
5124 result_type = void_type_node;
5125 else
5127 error_at (colon_loc, "type mismatch in conditional expression");
5128 return error_mark_node;
5132 /* Merge const and volatile flags of the incoming types. */
5133 result_type
5134 = build_type_variant (result_type,
5135 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5136 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5138 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5139 semantic_result_type);
5140 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5141 semantic_result_type);
5143 if (ifexp_bcp && ifexp == truthvalue_true_node)
5145 op2_int_operands = true;
5146 op1 = c_fully_fold (op1, require_constant_value, NULL);
5148 if (ifexp_bcp && ifexp == truthvalue_false_node)
5150 op1_int_operands = true;
5151 op2 = c_fully_fold (op2, require_constant_value, NULL);
5153 int_const = int_operands = (ifexp_int_operands
5154 && op1_int_operands
5155 && op2_int_operands);
5156 if (int_operands)
5158 int_const = ((ifexp == truthvalue_true_node
5159 && TREE_CODE (orig_op1) == INTEGER_CST
5160 && !TREE_OVERFLOW (orig_op1))
5161 || (ifexp == truthvalue_false_node
5162 && TREE_CODE (orig_op2) == INTEGER_CST
5163 && !TREE_OVERFLOW (orig_op2)));
5166 /* Need to convert condition operand into a vector mask. */
5167 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5169 tree vectype = TREE_TYPE (ifexp);
5170 tree elem_type = TREE_TYPE (vectype);
5171 tree zero = build_int_cst (elem_type, 0);
5172 tree zero_vec = build_vector_from_val (vectype, zero);
5173 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5174 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5177 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5178 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5179 else
5181 if (int_operands)
5183 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5184 nested inside of the expression. */
5185 op1 = c_fully_fold (op1, false, NULL);
5186 op2 = c_fully_fold (op2, false, NULL);
5188 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5189 if (int_operands)
5190 ret = note_integer_operands (ret);
5192 if (semantic_result_type)
5193 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5195 protected_set_expr_location (ret, colon_loc);
5196 return ret;
5199 /* Return a compound expression that performs two expressions and
5200 returns the value of the second of them.
5202 LOC is the location of the COMPOUND_EXPR. */
5204 tree
5205 build_compound_expr (location_t loc, tree expr1, tree expr2)
5207 bool expr1_int_operands, expr2_int_operands;
5208 tree eptype = NULL_TREE;
5209 tree ret;
5211 if (flag_cilkplus
5212 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5213 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5215 error_at (loc,
5216 "spawned function call cannot be part of a comma expression");
5217 return error_mark_node;
5219 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5220 if (expr1_int_operands)
5221 expr1 = remove_c_maybe_const_expr (expr1);
5222 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5223 if (expr2_int_operands)
5224 expr2 = remove_c_maybe_const_expr (expr2);
5226 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5227 expr1 = TREE_OPERAND (expr1, 0);
5228 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5230 eptype = TREE_TYPE (expr2);
5231 expr2 = TREE_OPERAND (expr2, 0);
5234 if (!TREE_SIDE_EFFECTS (expr1))
5236 /* The left-hand operand of a comma expression is like an expression
5237 statement: with -Wunused, we should warn if it doesn't have
5238 any side-effects, unless it was explicitly cast to (void). */
5239 if (warn_unused_value)
5241 if (VOID_TYPE_P (TREE_TYPE (expr1))
5242 && CONVERT_EXPR_P (expr1))
5243 ; /* (void) a, b */
5244 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5245 && TREE_CODE (expr1) == COMPOUND_EXPR
5246 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5247 ; /* (void) a, (void) b, c */
5248 else
5249 warning_at (loc, OPT_Wunused_value,
5250 "left-hand operand of comma expression has no effect");
5253 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5254 && warn_unused_value)
5256 tree r = expr1;
5257 location_t cloc = loc;
5258 while (TREE_CODE (r) == COMPOUND_EXPR)
5260 if (EXPR_HAS_LOCATION (r))
5261 cloc = EXPR_LOCATION (r);
5262 r = TREE_OPERAND (r, 1);
5264 if (!TREE_SIDE_EFFECTS (r)
5265 && !VOID_TYPE_P (TREE_TYPE (r))
5266 && !CONVERT_EXPR_P (r))
5267 warning_at (cloc, OPT_Wunused_value,
5268 "right-hand operand of comma expression has no effect");
5271 /* With -Wunused, we should also warn if the left-hand operand does have
5272 side-effects, but computes a value which is not used. For example, in
5273 `foo() + bar(), baz()' the result of the `+' operator is not used,
5274 so we should issue a warning. */
5275 else if (warn_unused_value)
5276 warn_if_unused_value (expr1, loc);
5278 if (expr2 == error_mark_node)
5279 return error_mark_node;
5281 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5283 if (flag_isoc99
5284 && expr1_int_operands
5285 && expr2_int_operands)
5286 ret = note_integer_operands (ret);
5288 if (eptype)
5289 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5291 protected_set_expr_location (ret, loc);
5292 return ret;
5295 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5296 which we are casting. OTYPE is the type of the expression being
5297 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5298 of the cast. -Wcast-qual appeared on the command line. Named
5299 address space qualifiers are not handled here, because they result
5300 in different warnings. */
5302 static void
5303 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5305 tree in_type = type;
5306 tree in_otype = otype;
5307 int added = 0;
5308 int discarded = 0;
5309 bool is_const;
5311 /* Check that the qualifiers on IN_TYPE are a superset of the
5312 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5313 nodes is uninteresting and we stop as soon as we hit a
5314 non-POINTER_TYPE node on either type. */
5317 in_otype = TREE_TYPE (in_otype);
5318 in_type = TREE_TYPE (in_type);
5320 /* GNU C allows cv-qualified function types. 'const' means the
5321 function is very pure, 'volatile' means it can't return. We
5322 need to warn when such qualifiers are added, not when they're
5323 taken away. */
5324 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5325 && TREE_CODE (in_type) == FUNCTION_TYPE)
5326 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5327 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5328 else
5329 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5330 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5332 while (TREE_CODE (in_type) == POINTER_TYPE
5333 && TREE_CODE (in_otype) == POINTER_TYPE);
5335 if (added)
5336 warning_at (loc, OPT_Wcast_qual,
5337 "cast adds %q#v qualifier to function type", added);
5339 if (discarded)
5340 /* There are qualifiers present in IN_OTYPE that are not present
5341 in IN_TYPE. */
5342 warning_at (loc, OPT_Wcast_qual,
5343 "cast discards %qv qualifier from pointer target type",
5344 discarded);
5346 if (added || discarded)
5347 return;
5349 /* A cast from **T to const **T is unsafe, because it can cause a
5350 const value to be changed with no additional warning. We only
5351 issue this warning if T is the same on both sides, and we only
5352 issue the warning if there are the same number of pointers on
5353 both sides, as otherwise the cast is clearly unsafe anyhow. A
5354 cast is unsafe when a qualifier is added at one level and const
5355 is not present at all outer levels.
5357 To issue this warning, we check at each level whether the cast
5358 adds new qualifiers not already seen. We don't need to special
5359 case function types, as they won't have the same
5360 TYPE_MAIN_VARIANT. */
5362 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5363 return;
5364 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5365 return;
5367 in_type = type;
5368 in_otype = otype;
5369 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5372 in_type = TREE_TYPE (in_type);
5373 in_otype = TREE_TYPE (in_otype);
5374 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5375 && !is_const)
5377 warning_at (loc, OPT_Wcast_qual,
5378 "to be safe all intermediate pointers in cast from "
5379 "%qT to %qT must be %<const%> qualified",
5380 otype, type);
5381 break;
5383 if (is_const)
5384 is_const = TYPE_READONLY (in_type);
5386 while (TREE_CODE (in_type) == POINTER_TYPE);
5389 /* Build an expression representing a cast to type TYPE of expression EXPR.
5390 LOC is the location of the cast-- typically the open paren of the cast. */
5392 tree
5393 build_c_cast (location_t loc, tree type, tree expr)
5395 tree value;
5397 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5398 expr = TREE_OPERAND (expr, 0);
5400 value = expr;
5402 if (type == error_mark_node || expr == error_mark_node)
5403 return error_mark_node;
5405 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5406 only in <protocol> qualifications. But when constructing cast expressions,
5407 the protocols do matter and must be kept around. */
5408 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5409 return build1 (NOP_EXPR, type, expr);
5411 type = TYPE_MAIN_VARIANT (type);
5413 if (TREE_CODE (type) == ARRAY_TYPE)
5415 error_at (loc, "cast specifies array type");
5416 return error_mark_node;
5419 if (TREE_CODE (type) == FUNCTION_TYPE)
5421 error_at (loc, "cast specifies function type");
5422 return error_mark_node;
5425 if (!VOID_TYPE_P (type))
5427 value = require_complete_type (loc, value);
5428 if (value == error_mark_node)
5429 return error_mark_node;
5432 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5434 if (RECORD_OR_UNION_TYPE_P (type))
5435 pedwarn (loc, OPT_Wpedantic,
5436 "ISO C forbids casting nonscalar to the same type");
5438 /* Convert to remove any qualifiers from VALUE's type. */
5439 value = convert (type, value);
5441 else if (TREE_CODE (type) == UNION_TYPE)
5443 tree field;
5445 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5446 if (TREE_TYPE (field) != error_mark_node
5447 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5448 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5449 break;
5451 if (field)
5453 tree t;
5454 bool maybe_const = true;
5456 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5457 t = c_fully_fold (value, false, &maybe_const);
5458 t = build_constructor_single (type, field, t);
5459 if (!maybe_const)
5460 t = c_wrap_maybe_const (t, true);
5461 t = digest_init (loc, type, t,
5462 NULL_TREE, false, true, 0);
5463 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5464 return t;
5466 error_at (loc, "cast to union type from type not present in union");
5467 return error_mark_node;
5469 else
5471 tree otype, ovalue;
5473 if (type == void_type_node)
5475 tree t = build1 (CONVERT_EXPR, type, value);
5476 SET_EXPR_LOCATION (t, loc);
5477 return t;
5480 otype = TREE_TYPE (value);
5482 /* Optionally warn about potentially worrisome casts. */
5483 if (warn_cast_qual
5484 && TREE_CODE (type) == POINTER_TYPE
5485 && TREE_CODE (otype) == POINTER_TYPE)
5486 handle_warn_cast_qual (loc, type, otype);
5488 /* Warn about conversions between pointers to disjoint
5489 address spaces. */
5490 if (TREE_CODE (type) == POINTER_TYPE
5491 && TREE_CODE (otype) == POINTER_TYPE
5492 && !null_pointer_constant_p (value))
5494 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5495 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5496 addr_space_t as_common;
5498 if (!addr_space_superset (as_to, as_from, &as_common))
5500 if (ADDR_SPACE_GENERIC_P (as_from))
5501 warning_at (loc, 0, "cast to %s address space pointer "
5502 "from disjoint generic address space pointer",
5503 c_addr_space_name (as_to));
5505 else if (ADDR_SPACE_GENERIC_P (as_to))
5506 warning_at (loc, 0, "cast to generic address space pointer "
5507 "from disjoint %s address space pointer",
5508 c_addr_space_name (as_from));
5510 else
5511 warning_at (loc, 0, "cast to %s address space pointer "
5512 "from disjoint %s address space pointer",
5513 c_addr_space_name (as_to),
5514 c_addr_space_name (as_from));
5518 /* Warn about possible alignment problems. */
5519 if (STRICT_ALIGNMENT
5520 && TREE_CODE (type) == POINTER_TYPE
5521 && TREE_CODE (otype) == POINTER_TYPE
5522 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5523 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5524 /* Don't warn about opaque types, where the actual alignment
5525 restriction is unknown. */
5526 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5527 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5528 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5529 warning_at (loc, OPT_Wcast_align,
5530 "cast increases required alignment of target type");
5532 if (TREE_CODE (type) == INTEGER_TYPE
5533 && TREE_CODE (otype) == POINTER_TYPE
5534 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5535 /* Unlike conversion of integers to pointers, where the
5536 warning is disabled for converting constants because
5537 of cases such as SIG_*, warn about converting constant
5538 pointers to integers. In some cases it may cause unwanted
5539 sign extension, and a warning is appropriate. */
5540 warning_at (loc, OPT_Wpointer_to_int_cast,
5541 "cast from pointer to integer of different size");
5543 if (TREE_CODE (value) == CALL_EXPR
5544 && TREE_CODE (type) != TREE_CODE (otype))
5545 warning_at (loc, OPT_Wbad_function_cast,
5546 "cast from function call of type %qT "
5547 "to non-matching type %qT", otype, type);
5549 if (TREE_CODE (type) == POINTER_TYPE
5550 && TREE_CODE (otype) == INTEGER_TYPE
5551 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5552 /* Don't warn about converting any constant. */
5553 && !TREE_CONSTANT (value))
5554 warning_at (loc,
5555 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5556 "of different size");
5558 if (warn_strict_aliasing <= 2)
5559 strict_aliasing_warning (otype, type, expr);
5561 /* If pedantic, warn for conversions between function and object
5562 pointer types, except for converting a null pointer constant
5563 to function pointer type. */
5564 if (pedantic
5565 && TREE_CODE (type) == POINTER_TYPE
5566 && TREE_CODE (otype) == POINTER_TYPE
5567 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5568 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5569 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5570 "conversion of function pointer to object pointer type");
5572 if (pedantic
5573 && TREE_CODE (type) == POINTER_TYPE
5574 && TREE_CODE (otype) == POINTER_TYPE
5575 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5576 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5577 && !null_pointer_constant_p (value))
5578 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5579 "conversion of object pointer to function pointer type");
5581 ovalue = value;
5582 value = convert (type, value);
5584 /* Ignore any integer overflow caused by the cast. */
5585 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5587 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5589 if (!TREE_OVERFLOW (value))
5591 /* Avoid clobbering a shared constant. */
5592 value = copy_node (value);
5593 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5596 else if (TREE_OVERFLOW (value))
5597 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5598 value = wide_int_to_tree (TREE_TYPE (value), value);
5602 /* Don't let a cast be an lvalue. */
5603 if (lvalue_p (value))
5604 value = non_lvalue_loc (loc, value);
5606 /* Don't allow the results of casting to floating-point or complex
5607 types be confused with actual constants, or casts involving
5608 integer and pointer types other than direct integer-to-integer
5609 and integer-to-pointer be confused with integer constant
5610 expressions and null pointer constants. */
5611 if (TREE_CODE (value) == REAL_CST
5612 || TREE_CODE (value) == COMPLEX_CST
5613 || (TREE_CODE (value) == INTEGER_CST
5614 && !((TREE_CODE (expr) == INTEGER_CST
5615 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5616 || TREE_CODE (expr) == REAL_CST
5617 || TREE_CODE (expr) == COMPLEX_CST)))
5618 value = build1 (NOP_EXPR, type, value);
5620 protected_set_expr_location (value, loc);
5621 return value;
5624 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5625 location of the open paren of the cast, or the position of the cast
5626 expr. */
5627 tree
5628 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5630 tree type;
5631 tree type_expr = NULL_TREE;
5632 bool type_expr_const = true;
5633 tree ret;
5634 int saved_wsp = warn_strict_prototypes;
5636 /* This avoids warnings about unprototyped casts on
5637 integers. E.g. "#define SIG_DFL (void(*)())0". */
5638 if (TREE_CODE (expr) == INTEGER_CST)
5639 warn_strict_prototypes = 0;
5640 type = groktypename (type_name, &type_expr, &type_expr_const);
5641 warn_strict_prototypes = saved_wsp;
5643 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5644 && reject_gcc_builtin (expr))
5645 return error_mark_node;
5647 ret = build_c_cast (loc, type, expr);
5648 if (type_expr)
5650 bool inner_expr_const = true;
5651 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5652 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5653 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5654 && inner_expr_const);
5655 SET_EXPR_LOCATION (ret, loc);
5658 if (!EXPR_HAS_LOCATION (ret))
5659 protected_set_expr_location (ret, loc);
5661 /* C++ does not permits types to be defined in a cast, but it
5662 allows references to incomplete types. */
5663 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5664 warning_at (loc, OPT_Wc___compat,
5665 "defining a type in a cast is invalid in C++");
5667 return ret;
5670 /* Build an assignment expression of lvalue LHS from value RHS.
5671 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5672 may differ from TREE_TYPE (LHS) for an enum bitfield.
5673 MODIFYCODE is the code for a binary operator that we use
5674 to combine the old value of LHS with RHS to get the new value.
5675 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5676 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5677 which may differ from TREE_TYPE (RHS) for an enum value.
5679 LOCATION is the location of the MODIFYCODE operator.
5680 RHS_LOC is the location of the RHS. */
5682 tree
5683 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5684 enum tree_code modifycode,
5685 location_t rhs_loc, tree rhs, tree rhs_origtype)
5687 tree result;
5688 tree newrhs;
5689 tree rhseval = NULL_TREE;
5690 tree rhs_semantic_type = NULL_TREE;
5691 tree lhstype = TREE_TYPE (lhs);
5692 tree olhstype = lhstype;
5693 bool npc;
5694 bool is_atomic_op;
5696 /* Types that aren't fully specified cannot be used in assignments. */
5697 lhs = require_complete_type (location, lhs);
5699 /* Avoid duplicate error messages from operands that had errors. */
5700 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5701 return error_mark_node;
5703 /* Ensure an error for assigning a non-lvalue array to an array in
5704 C90. */
5705 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5707 error_at (location, "assignment to expression with array type");
5708 return error_mark_node;
5711 /* For ObjC properties, defer this check. */
5712 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5713 return error_mark_node;
5715 is_atomic_op = really_atomic_lvalue (lhs);
5717 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5719 rhs_semantic_type = TREE_TYPE (rhs);
5720 rhs = TREE_OPERAND (rhs, 0);
5723 newrhs = rhs;
5725 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5727 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5728 lhs_origtype, modifycode, rhs_loc, rhs,
5729 rhs_origtype);
5730 if (inner == error_mark_node)
5731 return error_mark_node;
5732 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5733 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5734 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5735 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5736 protected_set_expr_location (result, location);
5737 return result;
5740 /* If a binary op has been requested, combine the old LHS value with the RHS
5741 producing the value we should actually store into the LHS. */
5743 if (modifycode != NOP_EXPR)
5745 lhs = c_fully_fold (lhs, false, NULL);
5746 lhs = stabilize_reference (lhs);
5748 /* Construct the RHS for any non-atomic compound assignemnt. */
5749 if (!is_atomic_op)
5751 /* If in LHS op= RHS the RHS has side-effects, ensure they
5752 are preevaluated before the rest of the assignment expression's
5753 side-effects, because RHS could contain e.g. function calls
5754 that modify LHS. */
5755 if (TREE_SIDE_EFFECTS (rhs))
5757 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5758 rhseval = newrhs;
5760 newrhs = build_binary_op (location,
5761 modifycode, lhs, newrhs, 1);
5763 /* The original type of the right hand side is no longer
5764 meaningful. */
5765 rhs_origtype = NULL_TREE;
5769 if (c_dialect_objc ())
5771 /* Check if we are modifying an Objective-C property reference;
5772 if so, we need to generate setter calls. */
5773 result = objc_maybe_build_modify_expr (lhs, newrhs);
5774 if (result)
5775 goto return_result;
5777 /* Else, do the check that we postponed for Objective-C. */
5778 if (!lvalue_or_else (location, lhs, lv_assign))
5779 return error_mark_node;
5782 /* Give an error for storing in something that is 'const'. */
5784 if (TYPE_READONLY (lhstype)
5785 || (RECORD_OR_UNION_TYPE_P (lhstype)
5786 && C_TYPE_FIELDS_READONLY (lhstype)))
5788 readonly_error (location, lhs, lv_assign);
5789 return error_mark_node;
5791 else if (TREE_READONLY (lhs))
5792 readonly_warning (lhs, lv_assign);
5794 /* If storing into a structure or union member,
5795 it has probably been given type `int'.
5796 Compute the type that would go with
5797 the actual amount of storage the member occupies. */
5799 if (TREE_CODE (lhs) == COMPONENT_REF
5800 && (TREE_CODE (lhstype) == INTEGER_TYPE
5801 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5802 || TREE_CODE (lhstype) == REAL_TYPE
5803 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5804 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5806 /* If storing in a field that is in actuality a short or narrower than one,
5807 we must store in the field in its actual type. */
5809 if (lhstype != TREE_TYPE (lhs))
5811 lhs = copy_node (lhs);
5812 TREE_TYPE (lhs) = lhstype;
5815 /* Issue -Wc++-compat warnings about an assignment to an enum type
5816 when LHS does not have its original type. This happens for,
5817 e.g., an enum bitfield in a struct. */
5818 if (warn_cxx_compat
5819 && lhs_origtype != NULL_TREE
5820 && lhs_origtype != lhstype
5821 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5823 tree checktype = (rhs_origtype != NULL_TREE
5824 ? rhs_origtype
5825 : TREE_TYPE (rhs));
5826 if (checktype != error_mark_node
5827 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5828 || (is_atomic_op && modifycode != NOP_EXPR)))
5829 warning_at (location, OPT_Wc___compat,
5830 "enum conversion in assignment is invalid in C++");
5833 /* If the lhs is atomic, remove that qualifier. */
5834 if (is_atomic_op)
5836 lhstype = build_qualified_type (lhstype,
5837 (TYPE_QUALS (lhstype)
5838 & ~TYPE_QUAL_ATOMIC));
5839 olhstype = build_qualified_type (olhstype,
5840 (TYPE_QUALS (lhstype)
5841 & ~TYPE_QUAL_ATOMIC));
5844 /* Convert new value to destination type. Fold it first, then
5845 restore any excess precision information, for the sake of
5846 conversion warnings. */
5848 if (!(is_atomic_op && modifycode != NOP_EXPR))
5850 npc = null_pointer_constant_p (newrhs);
5851 newrhs = c_fully_fold (newrhs, false, NULL);
5852 if (rhs_semantic_type)
5853 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5854 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5855 rhs_origtype, ic_assign, npc,
5856 NULL_TREE, NULL_TREE, 0);
5857 if (TREE_CODE (newrhs) == ERROR_MARK)
5858 return error_mark_node;
5861 /* Emit ObjC write barrier, if necessary. */
5862 if (c_dialect_objc () && flag_objc_gc)
5864 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5865 if (result)
5867 protected_set_expr_location (result, location);
5868 goto return_result;
5872 /* Scan operands. */
5874 if (is_atomic_op)
5875 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5876 else
5878 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5879 TREE_SIDE_EFFECTS (result) = 1;
5880 protected_set_expr_location (result, location);
5883 /* If we got the LHS in a different type for storing in,
5884 convert the result back to the nominal type of LHS
5885 so that the value we return always has the same type
5886 as the LHS argument. */
5888 if (olhstype == TREE_TYPE (result))
5889 goto return_result;
5891 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5892 rhs_origtype, ic_assign, false, NULL_TREE,
5893 NULL_TREE, 0);
5894 protected_set_expr_location (result, location);
5896 return_result:
5897 if (rhseval)
5898 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5899 return result;
5902 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5903 This is used to implement -fplan9-extensions. */
5905 static bool
5906 find_anonymous_field_with_type (tree struct_type, tree type)
5908 tree field;
5909 bool found;
5911 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5912 found = false;
5913 for (field = TYPE_FIELDS (struct_type);
5914 field != NULL_TREE;
5915 field = TREE_CHAIN (field))
5917 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5918 ? c_build_qualified_type (TREE_TYPE (field),
5919 TYPE_QUAL_ATOMIC)
5920 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5921 if (DECL_NAME (field) == NULL
5922 && comptypes (type, fieldtype))
5924 if (found)
5925 return false;
5926 found = true;
5928 else if (DECL_NAME (field) == NULL
5929 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5930 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5932 if (found)
5933 return false;
5934 found = true;
5937 return found;
5940 /* RHS is an expression whose type is pointer to struct. If there is
5941 an anonymous field in RHS with type TYPE, then return a pointer to
5942 that field in RHS. This is used with -fplan9-extensions. This
5943 returns NULL if no conversion could be found. */
5945 static tree
5946 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5948 tree rhs_struct_type, lhs_main_type;
5949 tree field, found_field;
5950 bool found_sub_field;
5951 tree ret;
5953 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5954 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5955 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5957 gcc_assert (POINTER_TYPE_P (type));
5958 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5959 ? c_build_qualified_type (TREE_TYPE (type),
5960 TYPE_QUAL_ATOMIC)
5961 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5963 found_field = NULL_TREE;
5964 found_sub_field = false;
5965 for (field = TYPE_FIELDS (rhs_struct_type);
5966 field != NULL_TREE;
5967 field = TREE_CHAIN (field))
5969 if (DECL_NAME (field) != NULL_TREE
5970 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
5971 continue;
5972 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5973 ? c_build_qualified_type (TREE_TYPE (field),
5974 TYPE_QUAL_ATOMIC)
5975 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5976 if (comptypes (lhs_main_type, fieldtype))
5978 if (found_field != NULL_TREE)
5979 return NULL_TREE;
5980 found_field = field;
5982 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5983 lhs_main_type))
5985 if (found_field != NULL_TREE)
5986 return NULL_TREE;
5987 found_field = field;
5988 found_sub_field = true;
5992 if (found_field == NULL_TREE)
5993 return NULL_TREE;
5995 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5996 build_fold_indirect_ref (rhs), found_field,
5997 NULL_TREE);
5998 ret = build_fold_addr_expr_loc (location, ret);
6000 if (found_sub_field)
6002 ret = convert_to_anonymous_field (location, type, ret);
6003 gcc_assert (ret != NULL_TREE);
6006 return ret;
6009 /* Issue an error message for a bad initializer component.
6010 GMSGID identifies the message.
6011 The component name is taken from the spelling stack. */
6013 static void
6014 error_init (location_t loc, const char *gmsgid)
6016 char *ofwhat;
6018 /* The gmsgid may be a format string with %< and %>. */
6019 error_at (loc, gmsgid);
6020 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6021 if (*ofwhat)
6022 inform (loc, "(near initialization for %qs)", ofwhat);
6025 /* Issue a pedantic warning for a bad initializer component. OPT is
6026 the option OPT_* (from options.h) controlling this warning or 0 if
6027 it is unconditionally given. GMSGID identifies the message. The
6028 component name is taken from the spelling stack. */
6030 static void
6031 pedwarn_init (location_t loc, int opt, const char *gmsgid)
6033 char *ofwhat;
6034 bool warned;
6036 /* Use the location where a macro was expanded rather than where
6037 it was defined to make sure macros defined in system headers
6038 but used incorrectly elsewhere are diagnosed. */
6039 source_location exploc = expansion_point_location_if_in_system_header (loc);
6041 /* The gmsgid may be a format string with %< and %>. */
6042 warned = pedwarn (exploc, opt, gmsgid);
6043 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6044 if (*ofwhat && warned)
6045 inform (exploc, "(near initialization for %qs)", ofwhat);
6048 /* Issue a warning for a bad initializer component.
6050 OPT is the OPT_W* value corresponding to the warning option that
6051 controls this warning. GMSGID identifies the message. The
6052 component name is taken from the spelling stack. */
6054 static void
6055 warning_init (location_t loc, int opt, const char *gmsgid)
6057 char *ofwhat;
6058 bool warned;
6060 /* Use the location where a macro was expanded rather than where
6061 it was defined to make sure macros defined in system headers
6062 but used incorrectly elsewhere are diagnosed. */
6063 source_location exploc = expansion_point_location_if_in_system_header (loc);
6065 /* The gmsgid may be a format string with %< and %>. */
6066 warned = warning_at (exploc, opt, gmsgid);
6067 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6068 if (*ofwhat && warned)
6069 inform (exploc, "(near initialization for %qs)", ofwhat);
6072 /* If TYPE is an array type and EXPR is a parenthesized string
6073 constant, warn if pedantic that EXPR is being used to initialize an
6074 object of type TYPE. */
6076 void
6077 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6079 if (pedantic
6080 && TREE_CODE (type) == ARRAY_TYPE
6081 && TREE_CODE (expr.value) == STRING_CST
6082 && expr.original_code != STRING_CST)
6083 pedwarn_init (loc, OPT_Wpedantic,
6084 "array initialized from parenthesized string constant");
6087 /* Convert value RHS to type TYPE as preparation for an assignment to
6088 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6089 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6090 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6091 constant before any folding.
6092 The real work of conversion is done by `convert'.
6093 The purpose of this function is to generate error messages
6094 for assignments that are not allowed in C.
6095 ERRTYPE says whether it is argument passing, assignment,
6096 initialization or return.
6098 In the following example, '~' denotes where EXPR_LOC and '^' where
6099 LOCATION point to:
6101 f (var); [ic_argpass]
6102 ^ ~~~
6103 x = var; [ic_assign]
6104 ^ ~~~;
6105 int x = var; [ic_init]
6107 return x; [ic_return]
6110 FUNCTION is a tree for the function being called.
6111 PARMNUM is the number of the argument, for printing in error messages. */
6113 static tree
6114 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6115 tree rhs, tree origtype, enum impl_conv errtype,
6116 bool null_pointer_constant, tree fundecl,
6117 tree function, int parmnum)
6119 enum tree_code codel = TREE_CODE (type);
6120 tree orig_rhs = rhs;
6121 tree rhstype;
6122 enum tree_code coder;
6123 tree rname = NULL_TREE;
6124 bool objc_ok = false;
6126 /* Use the expansion point location to handle cases such as user's
6127 function returning a wrong-type macro defined in a system header. */
6128 location = expansion_point_location_if_in_system_header (location);
6130 if (errtype == ic_argpass)
6132 tree selector;
6133 /* Change pointer to function to the function itself for
6134 diagnostics. */
6135 if (TREE_CODE (function) == ADDR_EXPR
6136 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6137 function = TREE_OPERAND (function, 0);
6139 /* Handle an ObjC selector specially for diagnostics. */
6140 selector = objc_message_selector ();
6141 rname = function;
6142 if (selector && parmnum > 2)
6144 rname = selector;
6145 parmnum -= 2;
6149 /* This macro is used to emit diagnostics to ensure that all format
6150 strings are complete sentences, visible to gettext and checked at
6151 compile time. */
6152 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6153 do { \
6154 switch (errtype) \
6156 case ic_argpass: \
6157 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6158 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6159 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6160 "expected %qT but argument is of type %qT", \
6161 type, rhstype); \
6162 break; \
6163 case ic_assign: \
6164 pedwarn (LOCATION, OPT, AS); \
6165 break; \
6166 case ic_init: \
6167 pedwarn_init (LOCATION, OPT, IN); \
6168 break; \
6169 case ic_return: \
6170 pedwarn (LOCATION, OPT, RE); \
6171 break; \
6172 default: \
6173 gcc_unreachable (); \
6175 } while (0)
6177 /* This macro is used to emit diagnostics to ensure that all format
6178 strings are complete sentences, visible to gettext and checked at
6179 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6180 extra parameter to enumerate qualifiers. */
6181 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6182 do { \
6183 switch (errtype) \
6185 case ic_argpass: \
6186 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6187 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6188 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6189 "expected %qT but argument is of type %qT", \
6190 type, rhstype); \
6191 break; \
6192 case ic_assign: \
6193 pedwarn (LOCATION, OPT, AS, QUALS); \
6194 break; \
6195 case ic_init: \
6196 pedwarn (LOCATION, OPT, IN, QUALS); \
6197 break; \
6198 case ic_return: \
6199 pedwarn (LOCATION, OPT, RE, QUALS); \
6200 break; \
6201 default: \
6202 gcc_unreachable (); \
6204 } while (0)
6206 /* This macro is used to emit diagnostics to ensure that all format
6207 strings are complete sentences, visible to gettext and checked at
6208 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6209 warning_at instead of pedwarn. */
6210 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6211 do { \
6212 switch (errtype) \
6214 case ic_argpass: \
6215 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6216 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6217 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6218 "expected %qT but argument is of type %qT", \
6219 type, rhstype); \
6220 break; \
6221 case ic_assign: \
6222 warning_at (LOCATION, OPT, AS, QUALS); \
6223 break; \
6224 case ic_init: \
6225 warning_at (LOCATION, OPT, IN, QUALS); \
6226 break; \
6227 case ic_return: \
6228 warning_at (LOCATION, OPT, RE, QUALS); \
6229 break; \
6230 default: \
6231 gcc_unreachable (); \
6233 } while (0)
6235 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6236 rhs = TREE_OPERAND (rhs, 0);
6238 rhstype = TREE_TYPE (rhs);
6239 coder = TREE_CODE (rhstype);
6241 if (coder == ERROR_MARK)
6242 return error_mark_node;
6244 if (c_dialect_objc ())
6246 int parmno;
6248 switch (errtype)
6250 case ic_return:
6251 parmno = 0;
6252 break;
6254 case ic_assign:
6255 parmno = -1;
6256 break;
6258 case ic_init:
6259 parmno = -2;
6260 break;
6262 default:
6263 parmno = parmnum;
6264 break;
6267 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6270 if (warn_cxx_compat)
6272 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6273 if (checktype != error_mark_node
6274 && TREE_CODE (type) == ENUMERAL_TYPE
6275 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6277 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6278 G_("enum conversion when passing argument "
6279 "%d of %qE is invalid in C++"),
6280 G_("enum conversion in assignment is "
6281 "invalid in C++"),
6282 G_("enum conversion in initialization is "
6283 "invalid in C++"),
6284 G_("enum conversion in return is "
6285 "invalid in C++"));
6289 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6290 return rhs;
6292 if (coder == VOID_TYPE)
6294 /* Except for passing an argument to an unprototyped function,
6295 this is a constraint violation. When passing an argument to
6296 an unprototyped function, it is compile-time undefined;
6297 making it a constraint in that case was rejected in
6298 DR#252. */
6299 error_at (location, "void value not ignored as it ought to be");
6300 return error_mark_node;
6302 rhs = require_complete_type (location, rhs);
6303 if (rhs == error_mark_node)
6304 return error_mark_node;
6306 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6307 return error_mark_node;
6309 /* A non-reference type can convert to a reference. This handles
6310 va_start, va_copy and possibly port built-ins. */
6311 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6313 if (!lvalue_p (rhs))
6315 error_at (location, "cannot pass rvalue to reference parameter");
6316 return error_mark_node;
6318 if (!c_mark_addressable (rhs))
6319 return error_mark_node;
6320 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6321 SET_EXPR_LOCATION (rhs, location);
6323 rhs = convert_for_assignment (location, expr_loc,
6324 build_pointer_type (TREE_TYPE (type)),
6325 rhs, origtype, errtype,
6326 null_pointer_constant, fundecl, function,
6327 parmnum);
6328 if (rhs == error_mark_node)
6329 return error_mark_node;
6331 rhs = build1 (NOP_EXPR, type, rhs);
6332 SET_EXPR_LOCATION (rhs, location);
6333 return rhs;
6335 /* Some types can interconvert without explicit casts. */
6336 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6337 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6338 return convert (type, rhs);
6339 /* Arithmetic types all interconvert, and enum is treated like int. */
6340 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6341 || codel == FIXED_POINT_TYPE
6342 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6343 || codel == BOOLEAN_TYPE)
6344 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6345 || coder == FIXED_POINT_TYPE
6346 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6347 || coder == BOOLEAN_TYPE))
6349 tree ret;
6350 bool save = in_late_binary_op;
6351 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6352 || (coder == REAL_TYPE
6353 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6354 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
6355 in_late_binary_op = true;
6356 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6357 ? expr_loc : location, type, orig_rhs);
6358 in_late_binary_op = save;
6359 return ret;
6362 /* Aggregates in different TUs might need conversion. */
6363 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6364 && codel == coder
6365 && comptypes (type, rhstype))
6366 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6367 ? expr_loc : location, type, rhs);
6369 /* Conversion to a transparent union or record from its member types.
6370 This applies only to function arguments. */
6371 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6372 && TYPE_TRANSPARENT_AGGR (type))
6373 && errtype == ic_argpass)
6375 tree memb, marginal_memb = NULL_TREE;
6377 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6379 tree memb_type = TREE_TYPE (memb);
6381 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6382 TYPE_MAIN_VARIANT (rhstype)))
6383 break;
6385 if (TREE_CODE (memb_type) != POINTER_TYPE)
6386 continue;
6388 if (coder == POINTER_TYPE)
6390 tree ttl = TREE_TYPE (memb_type);
6391 tree ttr = TREE_TYPE (rhstype);
6393 /* Any non-function converts to a [const][volatile] void *
6394 and vice versa; otherwise, targets must be the same.
6395 Meanwhile, the lhs target must have all the qualifiers of
6396 the rhs. */
6397 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6398 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6399 || comp_target_types (location, memb_type, rhstype))
6401 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6402 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6403 /* If this type won't generate any warnings, use it. */
6404 if (lquals == rquals
6405 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6406 && TREE_CODE (ttl) == FUNCTION_TYPE)
6407 ? ((lquals | rquals) == rquals)
6408 : ((lquals | rquals) == lquals)))
6409 break;
6411 /* Keep looking for a better type, but remember this one. */
6412 if (!marginal_memb)
6413 marginal_memb = memb;
6417 /* Can convert integer zero to any pointer type. */
6418 if (null_pointer_constant)
6420 rhs = null_pointer_node;
6421 break;
6425 if (memb || marginal_memb)
6427 if (!memb)
6429 /* We have only a marginally acceptable member type;
6430 it needs a warning. */
6431 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6432 tree ttr = TREE_TYPE (rhstype);
6434 /* Const and volatile mean something different for function
6435 types, so the usual warnings are not appropriate. */
6436 if (TREE_CODE (ttr) == FUNCTION_TYPE
6437 && TREE_CODE (ttl) == FUNCTION_TYPE)
6439 /* Because const and volatile on functions are
6440 restrictions that say the function will not do
6441 certain things, it is okay to use a const or volatile
6442 function where an ordinary one is wanted, but not
6443 vice-versa. */
6444 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6445 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6446 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6447 OPT_Wdiscarded_qualifiers,
6448 G_("passing argument %d of %qE "
6449 "makes %q#v qualified function "
6450 "pointer from unqualified"),
6451 G_("assignment makes %q#v qualified "
6452 "function pointer from "
6453 "unqualified"),
6454 G_("initialization makes %q#v qualified "
6455 "function pointer from "
6456 "unqualified"),
6457 G_("return makes %q#v qualified function "
6458 "pointer from unqualified"),
6459 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6461 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6462 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6463 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6464 OPT_Wdiscarded_qualifiers,
6465 G_("passing argument %d of %qE discards "
6466 "%qv qualifier from pointer target type"),
6467 G_("assignment discards %qv qualifier "
6468 "from pointer target type"),
6469 G_("initialization discards %qv qualifier "
6470 "from pointer target type"),
6471 G_("return discards %qv qualifier from "
6472 "pointer target type"),
6473 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6475 memb = marginal_memb;
6478 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6479 pedwarn (location, OPT_Wpedantic,
6480 "ISO C prohibits argument conversion to union type");
6482 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6483 return build_constructor_single (type, memb, rhs);
6487 /* Conversions among pointers */
6488 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6489 && (coder == codel))
6491 tree ttl = TREE_TYPE (type);
6492 tree ttr = TREE_TYPE (rhstype);
6493 tree mvl = ttl;
6494 tree mvr = ttr;
6495 bool is_opaque_pointer;
6496 int target_cmp = 0; /* Cache comp_target_types () result. */
6497 addr_space_t asl;
6498 addr_space_t asr;
6500 if (TREE_CODE (mvl) != ARRAY_TYPE)
6501 mvl = (TYPE_ATOMIC (mvl)
6502 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6503 TYPE_QUAL_ATOMIC)
6504 : TYPE_MAIN_VARIANT (mvl));
6505 if (TREE_CODE (mvr) != ARRAY_TYPE)
6506 mvr = (TYPE_ATOMIC (mvr)
6507 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6508 TYPE_QUAL_ATOMIC)
6509 : TYPE_MAIN_VARIANT (mvr));
6510 /* Opaque pointers are treated like void pointers. */
6511 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6513 /* The Plan 9 compiler permits a pointer to a struct to be
6514 automatically converted into a pointer to an anonymous field
6515 within the struct. */
6516 if (flag_plan9_extensions
6517 && RECORD_OR_UNION_TYPE_P (mvl)
6518 && RECORD_OR_UNION_TYPE_P (mvr)
6519 && mvl != mvr)
6521 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6522 if (new_rhs != NULL_TREE)
6524 rhs = new_rhs;
6525 rhstype = TREE_TYPE (rhs);
6526 coder = TREE_CODE (rhstype);
6527 ttr = TREE_TYPE (rhstype);
6528 mvr = TYPE_MAIN_VARIANT (ttr);
6532 /* C++ does not allow the implicit conversion void* -> T*. However,
6533 for the purpose of reducing the number of false positives, we
6534 tolerate the special case of
6536 int *p = NULL;
6538 where NULL is typically defined in C to be '(void *) 0'. */
6539 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6540 warning_at (errtype == ic_argpass ? expr_loc : location,
6541 OPT_Wc___compat,
6542 "request for implicit conversion "
6543 "from %qT to %qT not permitted in C++", rhstype, type);
6545 /* See if the pointers point to incompatible address spaces. */
6546 asl = TYPE_ADDR_SPACE (ttl);
6547 asr = TYPE_ADDR_SPACE (ttr);
6548 if (!null_pointer_constant_p (rhs)
6549 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6551 switch (errtype)
6553 case ic_argpass:
6554 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6555 "non-enclosed address space", parmnum, rname);
6556 break;
6557 case ic_assign:
6558 error_at (location, "assignment from pointer to "
6559 "non-enclosed address space");
6560 break;
6561 case ic_init:
6562 error_at (location, "initialization from pointer to "
6563 "non-enclosed address space");
6564 break;
6565 case ic_return:
6566 error_at (location, "return from pointer to "
6567 "non-enclosed address space");
6568 break;
6569 default:
6570 gcc_unreachable ();
6572 return error_mark_node;
6575 /* Check if the right-hand side has a format attribute but the
6576 left-hand side doesn't. */
6577 if (warn_suggest_attribute_format
6578 && check_missing_format_attribute (type, rhstype))
6580 switch (errtype)
6582 case ic_argpass:
6583 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6584 "argument %d of %qE might be "
6585 "a candidate for a format attribute",
6586 parmnum, rname);
6587 break;
6588 case ic_assign:
6589 warning_at (location, OPT_Wsuggest_attribute_format,
6590 "assignment left-hand side might be "
6591 "a candidate for a format attribute");
6592 break;
6593 case ic_init:
6594 warning_at (location, OPT_Wsuggest_attribute_format,
6595 "initialization left-hand side might be "
6596 "a candidate for a format attribute");
6597 break;
6598 case ic_return:
6599 warning_at (location, OPT_Wsuggest_attribute_format,
6600 "return type might be "
6601 "a candidate for a format attribute");
6602 break;
6603 default:
6604 gcc_unreachable ();
6608 /* Any non-function converts to a [const][volatile] void *
6609 and vice versa; otherwise, targets must be the same.
6610 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6611 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6612 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6613 || (target_cmp = comp_target_types (location, type, rhstype))
6614 || is_opaque_pointer
6615 || ((c_common_unsigned_type (mvl)
6616 == c_common_unsigned_type (mvr))
6617 && (c_common_signed_type (mvl)
6618 == c_common_signed_type (mvr))
6619 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6621 /* Warn about loss of qualifers from pointers to arrays with
6622 qualifiers on the element type. */
6623 if (TREE_CODE (ttr) == ARRAY_TYPE)
6625 ttr = strip_array_types (ttr);
6626 ttl = strip_array_types (ttl);
6628 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6629 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6630 WARNING_FOR_QUALIFIERS (location, expr_loc,
6631 OPT_Wdiscarded_array_qualifiers,
6632 G_("passing argument %d of %qE discards "
6633 "%qv qualifier from pointer target type"),
6634 G_("assignment discards %qv qualifier "
6635 "from pointer target type"),
6636 G_("initialization discards %qv qualifier "
6637 "from pointer target type"),
6638 G_("return discards %qv qualifier from "
6639 "pointer target type"),
6640 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6642 else if (pedantic
6643 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6645 (VOID_TYPE_P (ttr)
6646 && !null_pointer_constant
6647 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6648 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6649 G_("ISO C forbids passing argument %d of "
6650 "%qE between function pointer "
6651 "and %<void *%>"),
6652 G_("ISO C forbids assignment between "
6653 "function pointer and %<void *%>"),
6654 G_("ISO C forbids initialization between "
6655 "function pointer and %<void *%>"),
6656 G_("ISO C forbids return between function "
6657 "pointer and %<void *%>"));
6658 /* Const and volatile mean something different for function types,
6659 so the usual warnings are not appropriate. */
6660 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6661 && TREE_CODE (ttl) != FUNCTION_TYPE)
6663 /* Don't warn about loss of qualifier for conversions from
6664 qualified void* to pointers to arrays with corresponding
6665 qualifier on the element type. */
6666 if (!pedantic)
6667 ttl = strip_array_types (ttl);
6669 /* Assignments between atomic and non-atomic objects are OK. */
6670 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6671 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6673 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6674 OPT_Wdiscarded_qualifiers,
6675 G_("passing argument %d of %qE discards "
6676 "%qv qualifier from pointer target type"),
6677 G_("assignment discards %qv qualifier "
6678 "from pointer target type"),
6679 G_("initialization discards %qv qualifier "
6680 "from pointer target type"),
6681 G_("return discards %qv qualifier from "
6682 "pointer target type"),
6683 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6685 /* If this is not a case of ignoring a mismatch in signedness,
6686 no warning. */
6687 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6688 || target_cmp)
6690 /* If there is a mismatch, do warn. */
6691 else if (warn_pointer_sign)
6692 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6693 G_("pointer targets in passing argument "
6694 "%d of %qE differ in signedness"),
6695 G_("pointer targets in assignment "
6696 "differ in signedness"),
6697 G_("pointer targets in initialization "
6698 "differ in signedness"),
6699 G_("pointer targets in return differ "
6700 "in signedness"));
6702 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6703 && TREE_CODE (ttr) == FUNCTION_TYPE)
6705 /* Because const and volatile on functions are restrictions
6706 that say the function will not do certain things,
6707 it is okay to use a const or volatile function
6708 where an ordinary one is wanted, but not vice-versa. */
6709 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6710 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6711 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6712 OPT_Wdiscarded_qualifiers,
6713 G_("passing argument %d of %qE makes "
6714 "%q#v qualified function pointer "
6715 "from unqualified"),
6716 G_("assignment makes %q#v qualified function "
6717 "pointer from unqualified"),
6718 G_("initialization makes %q#v qualified "
6719 "function pointer from unqualified"),
6720 G_("return makes %q#v qualified function "
6721 "pointer from unqualified"),
6722 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6725 else
6726 /* Avoid warning about the volatile ObjC EH puts on decls. */
6727 if (!objc_ok)
6728 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6729 OPT_Wincompatible_pointer_types,
6730 G_("passing argument %d of %qE from "
6731 "incompatible pointer type"),
6732 G_("assignment from incompatible pointer type"),
6733 G_("initialization from incompatible "
6734 "pointer type"),
6735 G_("return from incompatible pointer type"));
6737 return convert (type, rhs);
6739 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6741 /* ??? This should not be an error when inlining calls to
6742 unprototyped functions. */
6743 error_at (location, "invalid use of non-lvalue array");
6744 return error_mark_node;
6746 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6748 /* An explicit constant 0 can convert to a pointer,
6749 or one that results from arithmetic, even including
6750 a cast to integer type. */
6751 if (!null_pointer_constant)
6752 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6753 OPT_Wint_conversion,
6754 G_("passing argument %d of %qE makes "
6755 "pointer from integer without a cast"),
6756 G_("assignment makes pointer from integer "
6757 "without a cast"),
6758 G_("initialization makes pointer from "
6759 "integer without a cast"),
6760 G_("return makes pointer from integer "
6761 "without a cast"));
6763 return convert (type, rhs);
6765 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6767 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6768 OPT_Wint_conversion,
6769 G_("passing argument %d of %qE makes integer "
6770 "from pointer without a cast"),
6771 G_("assignment makes integer from pointer "
6772 "without a cast"),
6773 G_("initialization makes integer from pointer "
6774 "without a cast"),
6775 G_("return makes integer from pointer "
6776 "without a cast"));
6777 return convert (type, rhs);
6779 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6781 tree ret;
6782 bool save = in_late_binary_op;
6783 in_late_binary_op = true;
6784 ret = convert (type, rhs);
6785 in_late_binary_op = save;
6786 return ret;
6789 switch (errtype)
6791 case ic_argpass:
6792 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6793 rname);
6794 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6795 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6796 "expected %qT but argument is of type %qT", type, rhstype);
6797 break;
6798 case ic_assign:
6799 error_at (location, "incompatible types when assigning to type %qT from "
6800 "type %qT", type, rhstype);
6801 break;
6802 case ic_init:
6803 error_at (location,
6804 "incompatible types when initializing type %qT using type %qT",
6805 type, rhstype);
6806 break;
6807 case ic_return:
6808 error_at (location,
6809 "incompatible types when returning type %qT but %qT was "
6810 "expected", rhstype, type);
6811 break;
6812 default:
6813 gcc_unreachable ();
6816 return error_mark_node;
6819 /* If VALUE is a compound expr all of whose expressions are constant, then
6820 return its value. Otherwise, return error_mark_node.
6822 This is for handling COMPOUND_EXPRs as initializer elements
6823 which is allowed with a warning when -pedantic is specified. */
6825 static tree
6826 valid_compound_expr_initializer (tree value, tree endtype)
6828 if (TREE_CODE (value) == COMPOUND_EXPR)
6830 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6831 == error_mark_node)
6832 return error_mark_node;
6833 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6834 endtype);
6836 else if (!initializer_constant_valid_p (value, endtype))
6837 return error_mark_node;
6838 else
6839 return value;
6842 /* Perform appropriate conversions on the initial value of a variable,
6843 store it in the declaration DECL,
6844 and print any error messages that are appropriate.
6845 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6846 If the init is invalid, store an ERROR_MARK.
6848 INIT_LOC is the location of the initial value. */
6850 void
6851 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6853 tree value, type;
6854 bool npc = false;
6856 /* If variable's type was invalidly declared, just ignore it. */
6858 type = TREE_TYPE (decl);
6859 if (TREE_CODE (type) == ERROR_MARK)
6860 return;
6862 /* Digest the specified initializer into an expression. */
6864 if (init)
6865 npc = null_pointer_constant_p (init);
6866 value = digest_init (init_loc, type, init, origtype, npc,
6867 true, TREE_STATIC (decl));
6869 /* Store the expression if valid; else report error. */
6871 if (!in_system_header_at (input_location)
6872 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6873 warning (OPT_Wtraditional, "traditional C rejects automatic "
6874 "aggregate initialization");
6876 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6877 DECL_INITIAL (decl) = value;
6879 /* ANSI wants warnings about out-of-range constant initializers. */
6880 STRIP_TYPE_NOPS (value);
6881 if (TREE_STATIC (decl))
6882 constant_expression_warning (value);
6884 /* Check if we need to set array size from compound literal size. */
6885 if (TREE_CODE (type) == ARRAY_TYPE
6886 && TYPE_DOMAIN (type) == 0
6887 && value != error_mark_node)
6889 tree inside_init = init;
6891 STRIP_TYPE_NOPS (inside_init);
6892 inside_init = fold (inside_init);
6894 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6896 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6898 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6900 /* For int foo[] = (int [3]){1}; we need to set array size
6901 now since later on array initializer will be just the
6902 brace enclosed list of the compound literal. */
6903 tree etype = strip_array_types (TREE_TYPE (decl));
6904 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6905 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6906 layout_type (type);
6907 layout_decl (cldecl, 0);
6908 TREE_TYPE (decl)
6909 = c_build_qualified_type (type, TYPE_QUALS (etype));
6915 /* Methods for storing and printing names for error messages. */
6917 /* Implement a spelling stack that allows components of a name to be pushed
6918 and popped. Each element on the stack is this structure. */
6920 struct spelling
6922 int kind;
6923 union
6925 unsigned HOST_WIDE_INT i;
6926 const char *s;
6927 } u;
6930 #define SPELLING_STRING 1
6931 #define SPELLING_MEMBER 2
6932 #define SPELLING_BOUNDS 3
6934 static struct spelling *spelling; /* Next stack element (unused). */
6935 static struct spelling *spelling_base; /* Spelling stack base. */
6936 static int spelling_size; /* Size of the spelling stack. */
6938 /* Macros to save and restore the spelling stack around push_... functions.
6939 Alternative to SAVE_SPELLING_STACK. */
6941 #define SPELLING_DEPTH() (spelling - spelling_base)
6942 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6944 /* Push an element on the spelling stack with type KIND and assign VALUE
6945 to MEMBER. */
6947 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6949 int depth = SPELLING_DEPTH (); \
6951 if (depth >= spelling_size) \
6953 spelling_size += 10; \
6954 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6955 spelling_size); \
6956 RESTORE_SPELLING_DEPTH (depth); \
6959 spelling->kind = (KIND); \
6960 spelling->MEMBER = (VALUE); \
6961 spelling++; \
6964 /* Push STRING on the stack. Printed literally. */
6966 static void
6967 push_string (const char *string)
6969 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6972 /* Push a member name on the stack. Printed as '.' STRING. */
6974 static void
6975 push_member_name (tree decl)
6977 const char *const string
6978 = (DECL_NAME (decl)
6979 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6980 : _("<anonymous>"));
6981 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6984 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6986 static void
6987 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6989 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6992 /* Compute the maximum size in bytes of the printed spelling. */
6994 static int
6995 spelling_length (void)
6997 int size = 0;
6998 struct spelling *p;
7000 for (p = spelling_base; p < spelling; p++)
7002 if (p->kind == SPELLING_BOUNDS)
7003 size += 25;
7004 else
7005 size += strlen (p->u.s) + 1;
7008 return size;
7011 /* Print the spelling to BUFFER and return it. */
7013 static char *
7014 print_spelling (char *buffer)
7016 char *d = buffer;
7017 struct spelling *p;
7019 for (p = spelling_base; p < spelling; p++)
7020 if (p->kind == SPELLING_BOUNDS)
7022 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7023 d += strlen (d);
7025 else
7027 const char *s;
7028 if (p->kind == SPELLING_MEMBER)
7029 *d++ = '.';
7030 for (s = p->u.s; (*d = *s++); d++)
7033 *d++ = '\0';
7034 return buffer;
7037 /* Digest the parser output INIT as an initializer for type TYPE.
7038 Return a C expression of type TYPE to represent the initial value.
7040 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7042 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7044 If INIT is a string constant, STRICT_STRING is true if it is
7045 unparenthesized or we should not warn here for it being parenthesized.
7046 For other types of INIT, STRICT_STRING is not used.
7048 INIT_LOC is the location of the INIT.
7050 REQUIRE_CONSTANT requests an error if non-constant initializers or
7051 elements are seen. */
7053 static tree
7054 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7055 bool null_pointer_constant, bool strict_string,
7056 int require_constant)
7058 enum tree_code code = TREE_CODE (type);
7059 tree inside_init = init;
7060 tree semantic_type = NULL_TREE;
7061 bool maybe_const = true;
7063 if (type == error_mark_node
7064 || !init
7065 || error_operand_p (init))
7066 return error_mark_node;
7068 STRIP_TYPE_NOPS (inside_init);
7070 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7072 semantic_type = TREE_TYPE (inside_init);
7073 inside_init = TREE_OPERAND (inside_init, 0);
7075 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7076 inside_init = decl_constant_value_for_optimization (inside_init);
7078 /* Initialization of an array of chars from a string constant
7079 optionally enclosed in braces. */
7081 if (code == ARRAY_TYPE && inside_init
7082 && TREE_CODE (inside_init) == STRING_CST)
7084 tree typ1
7085 = (TYPE_ATOMIC (TREE_TYPE (type))
7086 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7087 TYPE_QUAL_ATOMIC)
7088 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7089 /* Note that an array could be both an array of character type
7090 and an array of wchar_t if wchar_t is signed char or unsigned
7091 char. */
7092 bool char_array = (typ1 == char_type_node
7093 || typ1 == signed_char_type_node
7094 || typ1 == unsigned_char_type_node);
7095 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7096 bool char16_array = !!comptypes (typ1, char16_type_node);
7097 bool char32_array = !!comptypes (typ1, char32_type_node);
7099 if (char_array || wchar_array || char16_array || char32_array)
7101 struct c_expr expr;
7102 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7103 expr.value = inside_init;
7104 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7105 expr.original_type = NULL;
7106 maybe_warn_string_init (init_loc, type, expr);
7108 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7109 pedwarn_init (init_loc, OPT_Wpedantic,
7110 "initialization of a flexible array member");
7112 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7113 TYPE_MAIN_VARIANT (type)))
7114 return inside_init;
7116 if (char_array)
7118 if (typ2 != char_type_node)
7120 error_init (init_loc, "char-array initialized from wide "
7121 "string");
7122 return error_mark_node;
7125 else
7127 if (typ2 == char_type_node)
7129 error_init (init_loc, "wide character array initialized "
7130 "from non-wide string");
7131 return error_mark_node;
7133 else if (!comptypes(typ1, typ2))
7135 error_init (init_loc, "wide character array initialized "
7136 "from incompatible wide string");
7137 return error_mark_node;
7141 TREE_TYPE (inside_init) = type;
7142 if (TYPE_DOMAIN (type) != 0
7143 && TYPE_SIZE (type) != 0
7144 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7146 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7148 /* Subtract the size of a single (possibly wide) character
7149 because it's ok to ignore the terminating null char
7150 that is counted in the length of the constant. */
7151 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7152 (len
7153 - (TYPE_PRECISION (typ1)
7154 / BITS_PER_UNIT))))
7155 pedwarn_init (init_loc, 0,
7156 ("initializer-string for array of chars "
7157 "is too long"));
7158 else if (warn_cxx_compat
7159 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7160 warning_at (init_loc, OPT_Wc___compat,
7161 ("initializer-string for array chars "
7162 "is too long for C++"));
7165 return inside_init;
7167 else if (INTEGRAL_TYPE_P (typ1))
7169 error_init (init_loc, "array of inappropriate type initialized "
7170 "from string constant");
7171 return error_mark_node;
7175 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7176 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7177 below and handle as a constructor. */
7178 if (code == VECTOR_TYPE
7179 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7180 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7181 && TREE_CONSTANT (inside_init))
7183 if (TREE_CODE (inside_init) == VECTOR_CST
7184 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7185 TYPE_MAIN_VARIANT (type)))
7186 return inside_init;
7188 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7190 unsigned HOST_WIDE_INT ix;
7191 tree value;
7192 bool constant_p = true;
7194 /* Iterate through elements and check if all constructor
7195 elements are *_CSTs. */
7196 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7197 if (!CONSTANT_CLASS_P (value))
7199 constant_p = false;
7200 break;
7203 if (constant_p)
7204 return build_vector_from_ctor (type,
7205 CONSTRUCTOR_ELTS (inside_init));
7209 if (warn_sequence_point)
7210 verify_sequence_points (inside_init);
7212 /* Any type can be initialized
7213 from an expression of the same type, optionally with braces. */
7215 if (inside_init && TREE_TYPE (inside_init) != 0
7216 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7217 TYPE_MAIN_VARIANT (type))
7218 || (code == ARRAY_TYPE
7219 && comptypes (TREE_TYPE (inside_init), type))
7220 || (code == VECTOR_TYPE
7221 && comptypes (TREE_TYPE (inside_init), type))
7222 || (code == POINTER_TYPE
7223 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7224 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7225 TREE_TYPE (type)))))
7227 if (code == POINTER_TYPE)
7229 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7231 if (TREE_CODE (inside_init) == STRING_CST
7232 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7233 inside_init = array_to_pointer_conversion
7234 (init_loc, inside_init);
7235 else
7237 error_init (init_loc, "invalid use of non-lvalue array");
7238 return error_mark_node;
7243 if (code == VECTOR_TYPE)
7244 /* Although the types are compatible, we may require a
7245 conversion. */
7246 inside_init = convert (type, inside_init);
7248 if (require_constant
7249 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7251 /* As an extension, allow initializing objects with static storage
7252 duration with compound literals (which are then treated just as
7253 the brace enclosed list they contain). Also allow this for
7254 vectors, as we can only assign them with compound literals. */
7255 if (flag_isoc99 && code != VECTOR_TYPE)
7256 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7257 "is not constant");
7258 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7259 inside_init = DECL_INITIAL (decl);
7262 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7263 && TREE_CODE (inside_init) != CONSTRUCTOR)
7265 error_init (init_loc, "array initialized from non-constant array "
7266 "expression");
7267 return error_mark_node;
7270 /* Compound expressions can only occur here if -Wpedantic or
7271 -pedantic-errors is specified. In the later case, we always want
7272 an error. In the former case, we simply want a warning. */
7273 if (require_constant && pedantic
7274 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7276 inside_init
7277 = valid_compound_expr_initializer (inside_init,
7278 TREE_TYPE (inside_init));
7279 if (inside_init == error_mark_node)
7280 error_init (init_loc, "initializer element is not constant");
7281 else
7282 pedwarn_init (init_loc, OPT_Wpedantic,
7283 "initializer element is not constant");
7284 if (flag_pedantic_errors)
7285 inside_init = error_mark_node;
7287 else if (require_constant
7288 && !initializer_constant_valid_p (inside_init,
7289 TREE_TYPE (inside_init)))
7291 error_init (init_loc, "initializer element is not constant");
7292 inside_init = error_mark_node;
7294 else if (require_constant && !maybe_const)
7295 pedwarn_init (init_loc, OPT_Wpedantic,
7296 "initializer element is not a constant expression");
7298 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7299 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7300 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7301 type, inside_init, origtype,
7302 ic_init, null_pointer_constant,
7303 NULL_TREE, NULL_TREE, 0);
7304 return inside_init;
7307 /* Handle scalar types, including conversions. */
7309 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7310 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7311 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7313 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7314 && (TREE_CODE (init) == STRING_CST
7315 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7316 inside_init = init = array_to_pointer_conversion (init_loc, init);
7317 if (semantic_type)
7318 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7319 inside_init);
7320 inside_init
7321 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7322 inside_init, origtype, ic_init,
7323 null_pointer_constant, NULL_TREE, NULL_TREE,
7326 /* Check to see if we have already given an error message. */
7327 if (inside_init == error_mark_node)
7329 else if (require_constant && !TREE_CONSTANT (inside_init))
7331 error_init (init_loc, "initializer element is not constant");
7332 inside_init = error_mark_node;
7334 else if (require_constant
7335 && !initializer_constant_valid_p (inside_init,
7336 TREE_TYPE (inside_init)))
7338 error_init (init_loc, "initializer element is not computable at "
7339 "load time");
7340 inside_init = error_mark_node;
7342 else if (require_constant && !maybe_const)
7343 pedwarn_init (init_loc, OPT_Wpedantic,
7344 "initializer element is not a constant expression");
7346 return inside_init;
7349 /* Come here only for records and arrays. */
7351 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7353 error_init (init_loc, "variable-sized object may not be initialized");
7354 return error_mark_node;
7357 error_init (init_loc, "invalid initializer");
7358 return error_mark_node;
7361 /* Handle initializers that use braces. */
7363 /* Type of object we are accumulating a constructor for.
7364 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7365 static tree constructor_type;
7367 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7368 left to fill. */
7369 static tree constructor_fields;
7371 /* For an ARRAY_TYPE, this is the specified index
7372 at which to store the next element we get. */
7373 static tree constructor_index;
7375 /* For an ARRAY_TYPE, this is the maximum index. */
7376 static tree constructor_max_index;
7378 /* For a RECORD_TYPE, this is the first field not yet written out. */
7379 static tree constructor_unfilled_fields;
7381 /* For an ARRAY_TYPE, this is the index of the first element
7382 not yet written out. */
7383 static tree constructor_unfilled_index;
7385 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7386 This is so we can generate gaps between fields, when appropriate. */
7387 static tree constructor_bit_index;
7389 /* If we are saving up the elements rather than allocating them,
7390 this is the list of elements so far (in reverse order,
7391 most recent first). */
7392 static vec<constructor_elt, va_gc> *constructor_elements;
7394 /* 1 if constructor should be incrementally stored into a constructor chain,
7395 0 if all the elements should be kept in AVL tree. */
7396 static int constructor_incremental;
7398 /* 1 if so far this constructor's elements are all compile-time constants. */
7399 static int constructor_constant;
7401 /* 1 if so far this constructor's elements are all valid address constants. */
7402 static int constructor_simple;
7404 /* 1 if this constructor has an element that cannot be part of a
7405 constant expression. */
7406 static int constructor_nonconst;
7408 /* 1 if this constructor is erroneous so far. */
7409 static int constructor_erroneous;
7411 /* 1 if this constructor is the universal zero initializer { 0 }. */
7412 static int constructor_zeroinit;
7414 /* Structure for managing pending initializer elements, organized as an
7415 AVL tree. */
7417 struct init_node
7419 struct init_node *left, *right;
7420 struct init_node *parent;
7421 int balance;
7422 tree purpose;
7423 tree value;
7424 tree origtype;
7427 /* Tree of pending elements at this constructor level.
7428 These are elements encountered out of order
7429 which belong at places we haven't reached yet in actually
7430 writing the output.
7431 Will never hold tree nodes across GC runs. */
7432 static struct init_node *constructor_pending_elts;
7434 /* The SPELLING_DEPTH of this constructor. */
7435 static int constructor_depth;
7437 /* DECL node for which an initializer is being read.
7438 0 means we are reading a constructor expression
7439 such as (struct foo) {...}. */
7440 static tree constructor_decl;
7442 /* Nonzero if this is an initializer for a top-level decl. */
7443 static int constructor_top_level;
7445 /* Nonzero if there were any member designators in this initializer. */
7446 static int constructor_designated;
7448 /* Nesting depth of designator list. */
7449 static int designator_depth;
7451 /* Nonzero if there were diagnosed errors in this designator list. */
7452 static int designator_erroneous;
7455 /* This stack has a level for each implicit or explicit level of
7456 structuring in the initializer, including the outermost one. It
7457 saves the values of most of the variables above. */
7459 struct constructor_range_stack;
7461 struct constructor_stack
7463 struct constructor_stack *next;
7464 tree type;
7465 tree fields;
7466 tree index;
7467 tree max_index;
7468 tree unfilled_index;
7469 tree unfilled_fields;
7470 tree bit_index;
7471 vec<constructor_elt, va_gc> *elements;
7472 struct init_node *pending_elts;
7473 int offset;
7474 int depth;
7475 /* If value nonzero, this value should replace the entire
7476 constructor at this level. */
7477 struct c_expr replacement_value;
7478 struct constructor_range_stack *range_stack;
7479 char constant;
7480 char simple;
7481 char nonconst;
7482 char implicit;
7483 char erroneous;
7484 char outer;
7485 char incremental;
7486 char designated;
7487 int designator_depth;
7490 static struct constructor_stack *constructor_stack;
7492 /* This stack represents designators from some range designator up to
7493 the last designator in the list. */
7495 struct constructor_range_stack
7497 struct constructor_range_stack *next, *prev;
7498 struct constructor_stack *stack;
7499 tree range_start;
7500 tree index;
7501 tree range_end;
7502 tree fields;
7505 static struct constructor_range_stack *constructor_range_stack;
7507 /* This stack records separate initializers that are nested.
7508 Nested initializers can't happen in ANSI C, but GNU C allows them
7509 in cases like { ... (struct foo) { ... } ... }. */
7511 struct initializer_stack
7513 struct initializer_stack *next;
7514 tree decl;
7515 struct constructor_stack *constructor_stack;
7516 struct constructor_range_stack *constructor_range_stack;
7517 vec<constructor_elt, va_gc> *elements;
7518 struct spelling *spelling;
7519 struct spelling *spelling_base;
7520 int spelling_size;
7521 char top_level;
7522 char require_constant_value;
7523 char require_constant_elements;
7524 rich_location *missing_brace_richloc;
7527 static struct initializer_stack *initializer_stack;
7529 /* Prepare to parse and output the initializer for variable DECL. */
7531 void
7532 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7533 rich_location *richloc)
7535 const char *locus;
7536 struct initializer_stack *p = XNEW (struct initializer_stack);
7538 p->decl = constructor_decl;
7539 p->require_constant_value = require_constant_value;
7540 p->require_constant_elements = require_constant_elements;
7541 p->constructor_stack = constructor_stack;
7542 p->constructor_range_stack = constructor_range_stack;
7543 p->elements = constructor_elements;
7544 p->spelling = spelling;
7545 p->spelling_base = spelling_base;
7546 p->spelling_size = spelling_size;
7547 p->top_level = constructor_top_level;
7548 p->next = initializer_stack;
7549 p->missing_brace_richloc = richloc;
7550 initializer_stack = p;
7552 constructor_decl = decl;
7553 constructor_designated = 0;
7554 constructor_top_level = top_level;
7556 if (decl != 0 && decl != error_mark_node)
7558 require_constant_value = TREE_STATIC (decl);
7559 require_constant_elements
7560 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7561 /* For a scalar, you can always use any value to initialize,
7562 even within braces. */
7563 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7564 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7566 else
7568 require_constant_value = 0;
7569 require_constant_elements = 0;
7570 locus = _("(anonymous)");
7573 constructor_stack = 0;
7574 constructor_range_stack = 0;
7576 found_missing_braces = 0;
7578 spelling_base = 0;
7579 spelling_size = 0;
7580 RESTORE_SPELLING_DEPTH (0);
7582 if (locus)
7583 push_string (locus);
7586 void
7587 finish_init (void)
7589 struct initializer_stack *p = initializer_stack;
7591 /* Free the whole constructor stack of this initializer. */
7592 while (constructor_stack)
7594 struct constructor_stack *q = constructor_stack;
7595 constructor_stack = q->next;
7596 free (q);
7599 gcc_assert (!constructor_range_stack);
7601 /* Pop back to the data of the outer initializer (if any). */
7602 free (spelling_base);
7604 constructor_decl = p->decl;
7605 require_constant_value = p->require_constant_value;
7606 require_constant_elements = p->require_constant_elements;
7607 constructor_stack = p->constructor_stack;
7608 constructor_range_stack = p->constructor_range_stack;
7609 constructor_elements = p->elements;
7610 spelling = p->spelling;
7611 spelling_base = p->spelling_base;
7612 spelling_size = p->spelling_size;
7613 constructor_top_level = p->top_level;
7614 initializer_stack = p->next;
7615 free (p);
7618 /* Call here when we see the initializer is surrounded by braces.
7619 This is instead of a call to push_init_level;
7620 it is matched by a call to pop_init_level.
7622 TYPE is the type to initialize, for a constructor expression.
7623 For an initializer for a decl, TYPE is zero. */
7625 void
7626 really_start_incremental_init (tree type)
7628 struct constructor_stack *p = XNEW (struct constructor_stack);
7630 if (type == 0)
7631 type = TREE_TYPE (constructor_decl);
7633 if (VECTOR_TYPE_P (type)
7634 && TYPE_VECTOR_OPAQUE (type))
7635 error ("opaque vector types cannot be initialized");
7637 p->type = constructor_type;
7638 p->fields = constructor_fields;
7639 p->index = constructor_index;
7640 p->max_index = constructor_max_index;
7641 p->unfilled_index = constructor_unfilled_index;
7642 p->unfilled_fields = constructor_unfilled_fields;
7643 p->bit_index = constructor_bit_index;
7644 p->elements = constructor_elements;
7645 p->constant = constructor_constant;
7646 p->simple = constructor_simple;
7647 p->nonconst = constructor_nonconst;
7648 p->erroneous = constructor_erroneous;
7649 p->pending_elts = constructor_pending_elts;
7650 p->depth = constructor_depth;
7651 p->replacement_value.value = 0;
7652 p->replacement_value.original_code = ERROR_MARK;
7653 p->replacement_value.original_type = NULL;
7654 p->implicit = 0;
7655 p->range_stack = 0;
7656 p->outer = 0;
7657 p->incremental = constructor_incremental;
7658 p->designated = constructor_designated;
7659 p->designator_depth = designator_depth;
7660 p->next = 0;
7661 constructor_stack = p;
7663 constructor_constant = 1;
7664 constructor_simple = 1;
7665 constructor_nonconst = 0;
7666 constructor_depth = SPELLING_DEPTH ();
7667 constructor_elements = NULL;
7668 constructor_pending_elts = 0;
7669 constructor_type = type;
7670 constructor_incremental = 1;
7671 constructor_designated = 0;
7672 constructor_zeroinit = 1;
7673 designator_depth = 0;
7674 designator_erroneous = 0;
7676 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7678 constructor_fields = TYPE_FIELDS (constructor_type);
7679 /* Skip any nameless bit fields at the beginning. */
7680 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7681 && DECL_NAME (constructor_fields) == 0)
7682 constructor_fields = DECL_CHAIN (constructor_fields);
7684 constructor_unfilled_fields = constructor_fields;
7685 constructor_bit_index = bitsize_zero_node;
7687 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7689 if (TYPE_DOMAIN (constructor_type))
7691 constructor_max_index
7692 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7694 /* Detect non-empty initializations of zero-length arrays. */
7695 if (constructor_max_index == NULL_TREE
7696 && TYPE_SIZE (constructor_type))
7697 constructor_max_index = integer_minus_one_node;
7699 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7700 to initialize VLAs will cause a proper error; avoid tree
7701 checking errors as well by setting a safe value. */
7702 if (constructor_max_index
7703 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7704 constructor_max_index = integer_minus_one_node;
7706 constructor_index
7707 = convert (bitsizetype,
7708 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7710 else
7712 constructor_index = bitsize_zero_node;
7713 constructor_max_index = NULL_TREE;
7716 constructor_unfilled_index = constructor_index;
7718 else if (VECTOR_TYPE_P (constructor_type))
7720 /* Vectors are like simple fixed-size arrays. */
7721 constructor_max_index =
7722 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7723 constructor_index = bitsize_zero_node;
7724 constructor_unfilled_index = constructor_index;
7726 else
7728 /* Handle the case of int x = {5}; */
7729 constructor_fields = constructor_type;
7730 constructor_unfilled_fields = constructor_type;
7734 extern location_t last_init_list_comma;
7736 /* Called when we see an open brace for a nested initializer. Finish
7737 off any pending levels with implicit braces. */
7738 void
7739 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7741 while (constructor_stack->implicit)
7743 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7744 && constructor_fields == 0)
7745 process_init_element (input_location,
7746 pop_init_level (loc, 1, braced_init_obstack,
7747 last_init_list_comma),
7748 true, braced_init_obstack);
7749 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7750 && constructor_max_index
7751 && tree_int_cst_lt (constructor_max_index,
7752 constructor_index))
7753 process_init_element (input_location,
7754 pop_init_level (loc, 1, braced_init_obstack,
7755 last_init_list_comma),
7756 true, braced_init_obstack);
7757 else
7758 break;
7762 /* Push down into a subobject, for initialization.
7763 If this is for an explicit set of braces, IMPLICIT is 0.
7764 If it is because the next element belongs at a lower level,
7765 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7767 void
7768 push_init_level (location_t loc, int implicit,
7769 struct obstack *braced_init_obstack)
7771 struct constructor_stack *p;
7772 tree value = NULL_TREE;
7774 /* Unless this is an explicit brace, we need to preserve previous
7775 content if any. */
7776 if (implicit)
7778 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7779 value = find_init_member (constructor_fields, braced_init_obstack);
7780 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7781 value = find_init_member (constructor_index, braced_init_obstack);
7784 p = XNEW (struct constructor_stack);
7785 p->type = constructor_type;
7786 p->fields = constructor_fields;
7787 p->index = constructor_index;
7788 p->max_index = constructor_max_index;
7789 p->unfilled_index = constructor_unfilled_index;
7790 p->unfilled_fields = constructor_unfilled_fields;
7791 p->bit_index = constructor_bit_index;
7792 p->elements = constructor_elements;
7793 p->constant = constructor_constant;
7794 p->simple = constructor_simple;
7795 p->nonconst = constructor_nonconst;
7796 p->erroneous = constructor_erroneous;
7797 p->pending_elts = constructor_pending_elts;
7798 p->depth = constructor_depth;
7799 p->replacement_value.value = 0;
7800 p->replacement_value.original_code = ERROR_MARK;
7801 p->replacement_value.original_type = NULL;
7802 p->implicit = implicit;
7803 p->outer = 0;
7804 p->incremental = constructor_incremental;
7805 p->designated = constructor_designated;
7806 p->designator_depth = designator_depth;
7807 p->next = constructor_stack;
7808 p->range_stack = 0;
7809 constructor_stack = p;
7811 constructor_constant = 1;
7812 constructor_simple = 1;
7813 constructor_nonconst = 0;
7814 constructor_depth = SPELLING_DEPTH ();
7815 constructor_elements = NULL;
7816 constructor_incremental = 1;
7817 constructor_designated = 0;
7818 constructor_pending_elts = 0;
7819 if (!implicit)
7821 p->range_stack = constructor_range_stack;
7822 constructor_range_stack = 0;
7823 designator_depth = 0;
7824 designator_erroneous = 0;
7827 /* Don't die if an entire brace-pair level is superfluous
7828 in the containing level. */
7829 if (constructor_type == 0)
7831 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7833 /* Don't die if there are extra init elts at the end. */
7834 if (constructor_fields == 0)
7835 constructor_type = 0;
7836 else
7838 constructor_type = TREE_TYPE (constructor_fields);
7839 push_member_name (constructor_fields);
7840 constructor_depth++;
7842 /* If upper initializer is designated, then mark this as
7843 designated too to prevent bogus warnings. */
7844 constructor_designated = p->designated;
7846 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7848 constructor_type = TREE_TYPE (constructor_type);
7849 push_array_bounds (tree_to_uhwi (constructor_index));
7850 constructor_depth++;
7853 if (constructor_type == 0)
7855 error_init (loc, "extra brace group at end of initializer");
7856 constructor_fields = 0;
7857 constructor_unfilled_fields = 0;
7858 return;
7861 if (value && TREE_CODE (value) == CONSTRUCTOR)
7863 constructor_constant = TREE_CONSTANT (value);
7864 constructor_simple = TREE_STATIC (value);
7865 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7866 constructor_elements = CONSTRUCTOR_ELTS (value);
7867 if (!vec_safe_is_empty (constructor_elements)
7868 && (TREE_CODE (constructor_type) == RECORD_TYPE
7869 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7870 set_nonincremental_init (braced_init_obstack);
7873 if (implicit == 1)
7875 found_missing_braces = 1;
7876 if (initializer_stack->missing_brace_richloc)
7877 initializer_stack->missing_brace_richloc->add_fixit_insert_before
7878 (loc, "{");
7881 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7883 constructor_fields = TYPE_FIELDS (constructor_type);
7884 /* Skip any nameless bit fields at the beginning. */
7885 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7886 && DECL_NAME (constructor_fields) == 0)
7887 constructor_fields = DECL_CHAIN (constructor_fields);
7889 constructor_unfilled_fields = constructor_fields;
7890 constructor_bit_index = bitsize_zero_node;
7892 else if (VECTOR_TYPE_P (constructor_type))
7894 /* Vectors are like simple fixed-size arrays. */
7895 constructor_max_index =
7896 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7897 constructor_index = bitsize_int (0);
7898 constructor_unfilled_index = constructor_index;
7900 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7902 if (TYPE_DOMAIN (constructor_type))
7904 constructor_max_index
7905 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7907 /* Detect non-empty initializations of zero-length arrays. */
7908 if (constructor_max_index == NULL_TREE
7909 && TYPE_SIZE (constructor_type))
7910 constructor_max_index = integer_minus_one_node;
7912 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7913 to initialize VLAs will cause a proper error; avoid tree
7914 checking errors as well by setting a safe value. */
7915 if (constructor_max_index
7916 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7917 constructor_max_index = integer_minus_one_node;
7919 constructor_index
7920 = convert (bitsizetype,
7921 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7923 else
7924 constructor_index = bitsize_zero_node;
7926 constructor_unfilled_index = constructor_index;
7927 if (value && TREE_CODE (value) == STRING_CST)
7929 /* We need to split the char/wchar array into individual
7930 characters, so that we don't have to special case it
7931 everywhere. */
7932 set_nonincremental_init_from_string (value, braced_init_obstack);
7935 else
7937 if (constructor_type != error_mark_node)
7938 warning_init (input_location, 0, "braces around scalar initializer");
7939 constructor_fields = constructor_type;
7940 constructor_unfilled_fields = constructor_type;
7944 /* At the end of an implicit or explicit brace level,
7945 finish up that level of constructor. If a single expression
7946 with redundant braces initialized that level, return the
7947 c_expr structure for that expression. Otherwise, the original_code
7948 element is set to ERROR_MARK.
7949 If we were outputting the elements as they are read, return 0 as the value
7950 from inner levels (process_init_element ignores that),
7951 but return error_mark_node as the value from the outermost level
7952 (that's what we want to put in DECL_INITIAL).
7953 Otherwise, return a CONSTRUCTOR expression as the value. */
7955 struct c_expr
7956 pop_init_level (location_t loc, int implicit,
7957 struct obstack *braced_init_obstack,
7958 location_t insert_before)
7960 struct constructor_stack *p;
7961 struct c_expr ret;
7962 ret.value = 0;
7963 ret.original_code = ERROR_MARK;
7964 ret.original_type = NULL;
7966 if (implicit == 0)
7968 /* When we come to an explicit close brace,
7969 pop any inner levels that didn't have explicit braces. */
7970 while (constructor_stack->implicit)
7971 process_init_element (input_location,
7972 pop_init_level (loc, 1, braced_init_obstack,
7973 insert_before),
7974 true, braced_init_obstack);
7975 gcc_assert (!constructor_range_stack);
7977 else
7978 if (initializer_stack->missing_brace_richloc)
7979 initializer_stack->missing_brace_richloc->add_fixit_insert_before
7980 (insert_before, "}");
7982 /* Now output all pending elements. */
7983 constructor_incremental = 1;
7984 output_pending_init_elements (1, braced_init_obstack);
7986 p = constructor_stack;
7988 /* Error for initializing a flexible array member, or a zero-length
7989 array member in an inappropriate context. */
7990 if (constructor_type && constructor_fields
7991 && TREE_CODE (constructor_type) == ARRAY_TYPE
7992 && TYPE_DOMAIN (constructor_type)
7993 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7995 /* Silently discard empty initializations. The parser will
7996 already have pedwarned for empty brackets. */
7997 if (integer_zerop (constructor_unfilled_index))
7998 constructor_type = NULL_TREE;
7999 else
8001 gcc_assert (!TYPE_SIZE (constructor_type));
8003 if (constructor_depth > 2)
8004 error_init (loc, "initialization of flexible array member in a nested context");
8005 else
8006 pedwarn_init (loc, OPT_Wpedantic,
8007 "initialization of a flexible array member");
8009 /* We have already issued an error message for the existence
8010 of a flexible array member not at the end of the structure.
8011 Discard the initializer so that we do not die later. */
8012 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8013 constructor_type = NULL_TREE;
8017 switch (vec_safe_length (constructor_elements))
8019 case 0:
8020 /* Initialization with { } counts as zeroinit. */
8021 constructor_zeroinit = 1;
8022 break;
8023 case 1:
8024 /* This might be zeroinit as well. */
8025 if (integer_zerop ((*constructor_elements)[0].value))
8026 constructor_zeroinit = 1;
8027 break;
8028 default:
8029 /* If the constructor has more than one element, it can't be { 0 }. */
8030 constructor_zeroinit = 0;
8031 break;
8034 /* Warn when some structs are initialized with direct aggregation. */
8035 if (!implicit && found_missing_braces && warn_missing_braces
8036 && !constructor_zeroinit)
8038 gcc_assert (initializer_stack->missing_brace_richloc);
8039 warning_at_rich_loc (initializer_stack->missing_brace_richloc,
8040 OPT_Wmissing_braces,
8041 "missing braces around initializer");
8044 /* Warn when some struct elements are implicitly initialized to zero. */
8045 if (warn_missing_field_initializers
8046 && constructor_type
8047 && TREE_CODE (constructor_type) == RECORD_TYPE
8048 && constructor_unfilled_fields)
8050 /* Do not warn for flexible array members or zero-length arrays. */
8051 while (constructor_unfilled_fields
8052 && (!DECL_SIZE (constructor_unfilled_fields)
8053 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8054 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8056 if (constructor_unfilled_fields
8057 /* Do not warn if this level of the initializer uses member
8058 designators; it is likely to be deliberate. */
8059 && !constructor_designated
8060 /* Do not warn about initializing with { 0 } or with { }. */
8061 && !constructor_zeroinit)
8063 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8064 "missing initializer for field %qD of %qT",
8065 constructor_unfilled_fields,
8066 constructor_type))
8067 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8068 "%qD declared here", constructor_unfilled_fields);
8072 /* Pad out the end of the structure. */
8073 if (p->replacement_value.value)
8074 /* If this closes a superfluous brace pair,
8075 just pass out the element between them. */
8076 ret = p->replacement_value;
8077 else if (constructor_type == 0)
8079 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8080 && TREE_CODE (constructor_type) != ARRAY_TYPE
8081 && !VECTOR_TYPE_P (constructor_type))
8083 /* A nonincremental scalar initializer--just return
8084 the element, after verifying there is just one. */
8085 if (vec_safe_is_empty (constructor_elements))
8087 if (!constructor_erroneous)
8088 error_init (loc, "empty scalar initializer");
8089 ret.value = error_mark_node;
8091 else if (vec_safe_length (constructor_elements) != 1)
8093 error_init (loc, "extra elements in scalar initializer");
8094 ret.value = (*constructor_elements)[0].value;
8096 else
8097 ret.value = (*constructor_elements)[0].value;
8099 else
8101 if (constructor_erroneous)
8102 ret.value = error_mark_node;
8103 else
8105 ret.value = build_constructor (constructor_type,
8106 constructor_elements);
8107 if (constructor_constant)
8108 TREE_CONSTANT (ret.value) = 1;
8109 if (constructor_constant && constructor_simple)
8110 TREE_STATIC (ret.value) = 1;
8111 if (constructor_nonconst)
8112 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8116 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8118 if (constructor_nonconst)
8119 ret.original_code = C_MAYBE_CONST_EXPR;
8120 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8121 ret.original_code = ERROR_MARK;
8124 constructor_type = p->type;
8125 constructor_fields = p->fields;
8126 constructor_index = p->index;
8127 constructor_max_index = p->max_index;
8128 constructor_unfilled_index = p->unfilled_index;
8129 constructor_unfilled_fields = p->unfilled_fields;
8130 constructor_bit_index = p->bit_index;
8131 constructor_elements = p->elements;
8132 constructor_constant = p->constant;
8133 constructor_simple = p->simple;
8134 constructor_nonconst = p->nonconst;
8135 constructor_erroneous = p->erroneous;
8136 constructor_incremental = p->incremental;
8137 constructor_designated = p->designated;
8138 designator_depth = p->designator_depth;
8139 constructor_pending_elts = p->pending_elts;
8140 constructor_depth = p->depth;
8141 if (!p->implicit)
8142 constructor_range_stack = p->range_stack;
8143 RESTORE_SPELLING_DEPTH (constructor_depth);
8145 constructor_stack = p->next;
8146 free (p);
8148 if (ret.value == 0 && constructor_stack == 0)
8149 ret.value = error_mark_node;
8150 return ret;
8153 /* Common handling for both array range and field name designators.
8154 ARRAY argument is nonzero for array ranges. Returns zero for success. */
8156 static int
8157 set_designator (location_t loc, int array,
8158 struct obstack *braced_init_obstack)
8160 tree subtype;
8161 enum tree_code subcode;
8163 /* Don't die if an entire brace-pair level is superfluous
8164 in the containing level. */
8165 if (constructor_type == 0)
8166 return 1;
8168 /* If there were errors in this designator list already, bail out
8169 silently. */
8170 if (designator_erroneous)
8171 return 1;
8173 if (!designator_depth)
8175 gcc_assert (!constructor_range_stack);
8177 /* Designator list starts at the level of closest explicit
8178 braces. */
8179 while (constructor_stack->implicit)
8180 process_init_element (input_location,
8181 pop_init_level (loc, 1, braced_init_obstack,
8182 last_init_list_comma),
8183 true, braced_init_obstack);
8184 constructor_designated = 1;
8185 return 0;
8188 switch (TREE_CODE (constructor_type))
8190 case RECORD_TYPE:
8191 case UNION_TYPE:
8192 subtype = TREE_TYPE (constructor_fields);
8193 if (subtype != error_mark_node)
8194 subtype = TYPE_MAIN_VARIANT (subtype);
8195 break;
8196 case ARRAY_TYPE:
8197 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8198 break;
8199 default:
8200 gcc_unreachable ();
8203 subcode = TREE_CODE (subtype);
8204 if (array && subcode != ARRAY_TYPE)
8206 error_init (loc, "array index in non-array initializer");
8207 return 1;
8209 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8211 error_init (loc, "field name not in record or union initializer");
8212 return 1;
8215 constructor_designated = 1;
8216 finish_implicit_inits (loc, braced_init_obstack);
8217 push_init_level (loc, 2, braced_init_obstack);
8218 return 0;
8221 /* If there are range designators in designator list, push a new designator
8222 to constructor_range_stack. RANGE_END is end of such stack range or
8223 NULL_TREE if there is no range designator at this level. */
8225 static void
8226 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8228 struct constructor_range_stack *p;
8230 p = (struct constructor_range_stack *)
8231 obstack_alloc (braced_init_obstack,
8232 sizeof (struct constructor_range_stack));
8233 p->prev = constructor_range_stack;
8234 p->next = 0;
8235 p->fields = constructor_fields;
8236 p->range_start = constructor_index;
8237 p->index = constructor_index;
8238 p->stack = constructor_stack;
8239 p->range_end = range_end;
8240 if (constructor_range_stack)
8241 constructor_range_stack->next = p;
8242 constructor_range_stack = p;
8245 /* Within an array initializer, specify the next index to be initialized.
8246 FIRST is that index. If LAST is nonzero, then initialize a range
8247 of indices, running from FIRST through LAST. */
8249 void
8250 set_init_index (location_t loc, tree first, tree last,
8251 struct obstack *braced_init_obstack)
8253 if (set_designator (loc, 1, braced_init_obstack))
8254 return;
8256 designator_erroneous = 1;
8258 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8259 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8261 error_init (loc, "array index in initializer not of integer type");
8262 return;
8265 if (TREE_CODE (first) != INTEGER_CST)
8267 first = c_fully_fold (first, false, NULL);
8268 if (TREE_CODE (first) == INTEGER_CST)
8269 pedwarn_init (loc, OPT_Wpedantic,
8270 "array index in initializer is not "
8271 "an integer constant expression");
8274 if (last && TREE_CODE (last) != INTEGER_CST)
8276 last = c_fully_fold (last, false, NULL);
8277 if (TREE_CODE (last) == INTEGER_CST)
8278 pedwarn_init (loc, OPT_Wpedantic,
8279 "array index in initializer is not "
8280 "an integer constant expression");
8283 if (TREE_CODE (first) != INTEGER_CST)
8284 error_init (loc, "nonconstant array index in initializer");
8285 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
8286 error_init (loc, "nonconstant array index in initializer");
8287 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8288 error_init (loc, "array index in non-array initializer");
8289 else if (tree_int_cst_sgn (first) == -1)
8290 error_init (loc, "array index in initializer exceeds array bounds");
8291 else if (constructor_max_index
8292 && tree_int_cst_lt (constructor_max_index, first))
8293 error_init (loc, "array index in initializer exceeds array bounds");
8294 else
8296 constant_expression_warning (first);
8297 if (last)
8298 constant_expression_warning (last);
8299 constructor_index = convert (bitsizetype, first);
8300 if (tree_int_cst_lt (constructor_index, first))
8302 constructor_index = copy_node (constructor_index);
8303 TREE_OVERFLOW (constructor_index) = 1;
8306 if (last)
8308 if (tree_int_cst_equal (first, last))
8309 last = 0;
8310 else if (tree_int_cst_lt (last, first))
8312 error_init (loc, "empty index range in initializer");
8313 last = 0;
8315 else
8317 last = convert (bitsizetype, last);
8318 if (constructor_max_index != 0
8319 && tree_int_cst_lt (constructor_max_index, last))
8321 error_init (loc, "array index range in initializer exceeds "
8322 "array bounds");
8323 last = 0;
8328 designator_depth++;
8329 designator_erroneous = 0;
8330 if (constructor_range_stack || last)
8331 push_range_stack (last, braced_init_obstack);
8335 /* Within a struct initializer, specify the next field to be initialized. */
8337 void
8338 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8339 struct obstack *braced_init_obstack)
8341 tree field;
8343 if (set_designator (loc, 0, braced_init_obstack))
8344 return;
8346 designator_erroneous = 1;
8348 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8350 error_init (loc, "field name not in record or union initializer");
8351 return;
8354 field = lookup_field (constructor_type, fieldname);
8356 if (field == 0)
8358 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8359 if (guessed_id)
8361 gcc_rich_location rich_loc (fieldname_loc);
8362 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8363 error_at_rich_loc
8364 (&rich_loc,
8365 "%qT has no member named %qE; did you mean %qE?",
8366 constructor_type, fieldname, guessed_id);
8368 else
8369 error_at (fieldname_loc, "%qT has no member named %qE",
8370 constructor_type, fieldname);
8372 else
8375 constructor_fields = TREE_VALUE (field);
8376 designator_depth++;
8377 designator_erroneous = 0;
8378 if (constructor_range_stack)
8379 push_range_stack (NULL_TREE, braced_init_obstack);
8380 field = TREE_CHAIN (field);
8381 if (field)
8383 if (set_designator (loc, 0, braced_init_obstack))
8384 return;
8387 while (field != NULL_TREE);
8390 /* Add a new initializer to the tree of pending initializers. PURPOSE
8391 identifies the initializer, either array index or field in a structure.
8392 VALUE is the value of that index or field. If ORIGTYPE is not
8393 NULL_TREE, it is the original type of VALUE.
8395 IMPLICIT is true if value comes from pop_init_level (1),
8396 the new initializer has been merged with the existing one
8397 and thus no warnings should be emitted about overriding an
8398 existing initializer. */
8400 static void
8401 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8402 bool implicit, struct obstack *braced_init_obstack)
8404 struct init_node *p, **q, *r;
8406 q = &constructor_pending_elts;
8407 p = 0;
8409 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8411 while (*q != 0)
8413 p = *q;
8414 if (tree_int_cst_lt (purpose, p->purpose))
8415 q = &p->left;
8416 else if (tree_int_cst_lt (p->purpose, purpose))
8417 q = &p->right;
8418 else
8420 if (!implicit)
8422 if (TREE_SIDE_EFFECTS (p->value))
8423 warning_init (loc, OPT_Woverride_init_side_effects,
8424 "initialized field with side-effects "
8425 "overwritten");
8426 else if (warn_override_init)
8427 warning_init (loc, OPT_Woverride_init,
8428 "initialized field overwritten");
8430 p->value = value;
8431 p->origtype = origtype;
8432 return;
8436 else
8438 tree bitpos;
8440 bitpos = bit_position (purpose);
8441 while (*q != NULL)
8443 p = *q;
8444 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8445 q = &p->left;
8446 else if (p->purpose != purpose)
8447 q = &p->right;
8448 else
8450 if (!implicit)
8452 if (TREE_SIDE_EFFECTS (p->value))
8453 warning_init (loc, OPT_Woverride_init_side_effects,
8454 "initialized field with side-effects "
8455 "overwritten");
8456 else if (warn_override_init)
8457 warning_init (loc, OPT_Woverride_init,
8458 "initialized field overwritten");
8460 p->value = value;
8461 p->origtype = origtype;
8462 return;
8467 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8468 sizeof (struct init_node));
8469 r->purpose = purpose;
8470 r->value = value;
8471 r->origtype = origtype;
8473 *q = r;
8474 r->parent = p;
8475 r->left = 0;
8476 r->right = 0;
8477 r->balance = 0;
8479 while (p)
8481 struct init_node *s;
8483 if (r == p->left)
8485 if (p->balance == 0)
8486 p->balance = -1;
8487 else if (p->balance < 0)
8489 if (r->balance < 0)
8491 /* L rotation. */
8492 p->left = r->right;
8493 if (p->left)
8494 p->left->parent = p;
8495 r->right = p;
8497 p->balance = 0;
8498 r->balance = 0;
8500 s = p->parent;
8501 p->parent = r;
8502 r->parent = s;
8503 if (s)
8505 if (s->left == p)
8506 s->left = r;
8507 else
8508 s->right = r;
8510 else
8511 constructor_pending_elts = r;
8513 else
8515 /* LR rotation. */
8516 struct init_node *t = r->right;
8518 r->right = t->left;
8519 if (r->right)
8520 r->right->parent = r;
8521 t->left = r;
8523 p->left = t->right;
8524 if (p->left)
8525 p->left->parent = p;
8526 t->right = p;
8528 p->balance = t->balance < 0;
8529 r->balance = -(t->balance > 0);
8530 t->balance = 0;
8532 s = p->parent;
8533 p->parent = t;
8534 r->parent = t;
8535 t->parent = s;
8536 if (s)
8538 if (s->left == p)
8539 s->left = t;
8540 else
8541 s->right = t;
8543 else
8544 constructor_pending_elts = t;
8546 break;
8548 else
8550 /* p->balance == +1; growth of left side balances the node. */
8551 p->balance = 0;
8552 break;
8555 else /* r == p->right */
8557 if (p->balance == 0)
8558 /* Growth propagation from right side. */
8559 p->balance++;
8560 else if (p->balance > 0)
8562 if (r->balance > 0)
8564 /* R rotation. */
8565 p->right = r->left;
8566 if (p->right)
8567 p->right->parent = p;
8568 r->left = p;
8570 p->balance = 0;
8571 r->balance = 0;
8573 s = p->parent;
8574 p->parent = r;
8575 r->parent = s;
8576 if (s)
8578 if (s->left == p)
8579 s->left = r;
8580 else
8581 s->right = r;
8583 else
8584 constructor_pending_elts = r;
8586 else /* r->balance == -1 */
8588 /* RL rotation */
8589 struct init_node *t = r->left;
8591 r->left = t->right;
8592 if (r->left)
8593 r->left->parent = r;
8594 t->right = r;
8596 p->right = t->left;
8597 if (p->right)
8598 p->right->parent = p;
8599 t->left = p;
8601 r->balance = (t->balance < 0);
8602 p->balance = -(t->balance > 0);
8603 t->balance = 0;
8605 s = p->parent;
8606 p->parent = t;
8607 r->parent = t;
8608 t->parent = s;
8609 if (s)
8611 if (s->left == p)
8612 s->left = t;
8613 else
8614 s->right = t;
8616 else
8617 constructor_pending_elts = t;
8619 break;
8621 else
8623 /* p->balance == -1; growth of right side balances the node. */
8624 p->balance = 0;
8625 break;
8629 r = p;
8630 p = p->parent;
8634 /* Build AVL tree from a sorted chain. */
8636 static void
8637 set_nonincremental_init (struct obstack * braced_init_obstack)
8639 unsigned HOST_WIDE_INT ix;
8640 tree index, value;
8642 if (TREE_CODE (constructor_type) != RECORD_TYPE
8643 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8644 return;
8646 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8647 add_pending_init (input_location, index, value, NULL_TREE, true,
8648 braced_init_obstack);
8649 constructor_elements = NULL;
8650 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8652 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8653 /* Skip any nameless bit fields at the beginning. */
8654 while (constructor_unfilled_fields != 0
8655 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8656 && DECL_NAME (constructor_unfilled_fields) == 0)
8657 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8660 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8662 if (TYPE_DOMAIN (constructor_type))
8663 constructor_unfilled_index
8664 = convert (bitsizetype,
8665 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8666 else
8667 constructor_unfilled_index = bitsize_zero_node;
8669 constructor_incremental = 0;
8672 /* Build AVL tree from a string constant. */
8674 static void
8675 set_nonincremental_init_from_string (tree str,
8676 struct obstack * braced_init_obstack)
8678 tree value, purpose, type;
8679 HOST_WIDE_INT val[2];
8680 const char *p, *end;
8681 int byte, wchar_bytes, charwidth, bitpos;
8683 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8685 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8686 charwidth = TYPE_PRECISION (char_type_node);
8687 gcc_assert ((size_t) wchar_bytes * charwidth
8688 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8689 type = TREE_TYPE (constructor_type);
8690 p = TREE_STRING_POINTER (str);
8691 end = p + TREE_STRING_LENGTH (str);
8693 for (purpose = bitsize_zero_node;
8694 p < end
8695 && !(constructor_max_index
8696 && tree_int_cst_lt (constructor_max_index, purpose));
8697 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8699 if (wchar_bytes == 1)
8701 val[0] = (unsigned char) *p++;
8702 val[1] = 0;
8704 else
8706 val[1] = 0;
8707 val[0] = 0;
8708 for (byte = 0; byte < wchar_bytes; byte++)
8710 if (BYTES_BIG_ENDIAN)
8711 bitpos = (wchar_bytes - byte - 1) * charwidth;
8712 else
8713 bitpos = byte * charwidth;
8714 val[bitpos / HOST_BITS_PER_WIDE_INT]
8715 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8716 << (bitpos % HOST_BITS_PER_WIDE_INT);
8720 if (!TYPE_UNSIGNED (type))
8722 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8723 if (bitpos < HOST_BITS_PER_WIDE_INT)
8725 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8727 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8728 val[1] = -1;
8731 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8733 if (val[0] < 0)
8734 val[1] = -1;
8736 else if (val[1] & (HOST_WIDE_INT_1
8737 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8738 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8741 value = wide_int_to_tree (type,
8742 wide_int::from_array (val, 2,
8743 HOST_BITS_PER_WIDE_INT * 2));
8744 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8745 braced_init_obstack);
8748 constructor_incremental = 0;
8751 /* Return value of FIELD in pending initializer or zero if the field was
8752 not initialized yet. */
8754 static tree
8755 find_init_member (tree field, struct obstack * braced_init_obstack)
8757 struct init_node *p;
8759 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8761 if (constructor_incremental
8762 && tree_int_cst_lt (field, constructor_unfilled_index))
8763 set_nonincremental_init (braced_init_obstack);
8765 p = constructor_pending_elts;
8766 while (p)
8768 if (tree_int_cst_lt (field, p->purpose))
8769 p = p->left;
8770 else if (tree_int_cst_lt (p->purpose, field))
8771 p = p->right;
8772 else
8773 return p->value;
8776 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8778 tree bitpos = bit_position (field);
8780 if (constructor_incremental
8781 && (!constructor_unfilled_fields
8782 || tree_int_cst_lt (bitpos,
8783 bit_position (constructor_unfilled_fields))))
8784 set_nonincremental_init (braced_init_obstack);
8786 p = constructor_pending_elts;
8787 while (p)
8789 if (field == p->purpose)
8790 return p->value;
8791 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8792 p = p->left;
8793 else
8794 p = p->right;
8797 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8799 if (!vec_safe_is_empty (constructor_elements)
8800 && (constructor_elements->last ().index == field))
8801 return constructor_elements->last ().value;
8803 return 0;
8806 /* "Output" the next constructor element.
8807 At top level, really output it to assembler code now.
8808 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8809 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8810 TYPE is the data type that the containing data type wants here.
8811 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8812 If VALUE is a string constant, STRICT_STRING is true if it is
8813 unparenthesized or we should not warn here for it being parenthesized.
8814 For other types of VALUE, STRICT_STRING is not used.
8816 PENDING if non-nil means output pending elements that belong
8817 right after this element. (PENDING is normally 1;
8818 it is 0 while outputting pending elements, to avoid recursion.)
8820 IMPLICIT is true if value comes from pop_init_level (1),
8821 the new initializer has been merged with the existing one
8822 and thus no warnings should be emitted about overriding an
8823 existing initializer. */
8825 static void
8826 output_init_element (location_t loc, tree value, tree origtype,
8827 bool strict_string, tree type, tree field, int pending,
8828 bool implicit, struct obstack * braced_init_obstack)
8830 tree semantic_type = NULL_TREE;
8831 bool maybe_const = true;
8832 bool npc;
8834 if (type == error_mark_node || value == error_mark_node)
8836 constructor_erroneous = 1;
8837 return;
8839 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8840 && (TREE_CODE (value) == STRING_CST
8841 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8842 && !(TREE_CODE (value) == STRING_CST
8843 && TREE_CODE (type) == ARRAY_TYPE
8844 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8845 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8846 TYPE_MAIN_VARIANT (type)))
8847 value = array_to_pointer_conversion (input_location, value);
8849 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8850 && require_constant_value && pending)
8852 /* As an extension, allow initializing objects with static storage
8853 duration with compound literals (which are then treated just as
8854 the brace enclosed list they contain). */
8855 if (flag_isoc99)
8856 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8857 "constant");
8858 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8859 value = DECL_INITIAL (decl);
8862 npc = null_pointer_constant_p (value);
8863 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8865 semantic_type = TREE_TYPE (value);
8866 value = TREE_OPERAND (value, 0);
8868 value = c_fully_fold (value, require_constant_value, &maybe_const);
8870 if (value == error_mark_node)
8871 constructor_erroneous = 1;
8872 else if (!TREE_CONSTANT (value))
8873 constructor_constant = 0;
8874 else if (!initializer_constant_valid_p (value,
8875 TREE_TYPE (value),
8876 AGGREGATE_TYPE_P (constructor_type)
8877 && TYPE_REVERSE_STORAGE_ORDER
8878 (constructor_type))
8879 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8880 && DECL_C_BIT_FIELD (field)
8881 && TREE_CODE (value) != INTEGER_CST))
8882 constructor_simple = 0;
8883 if (!maybe_const)
8884 constructor_nonconst = 1;
8886 /* Digest the initializer and issue any errors about incompatible
8887 types before issuing errors about non-constant initializers. */
8888 tree new_value = value;
8889 if (semantic_type)
8890 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8891 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8892 require_constant_value);
8893 if (new_value == error_mark_node)
8895 constructor_erroneous = 1;
8896 return;
8898 if (require_constant_value || require_constant_elements)
8899 constant_expression_warning (new_value);
8901 /* Proceed to check the constness of the original initializer. */
8902 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8904 if (require_constant_value)
8906 error_init (loc, "initializer element is not constant");
8907 value = error_mark_node;
8909 else if (require_constant_elements)
8910 pedwarn (loc, OPT_Wpedantic,
8911 "initializer element is not computable at load time");
8913 else if (!maybe_const
8914 && (require_constant_value || require_constant_elements))
8915 pedwarn_init (loc, OPT_Wpedantic,
8916 "initializer element is not a constant expression");
8918 /* Issue -Wc++-compat warnings about initializing a bitfield with
8919 enum type. */
8920 if (warn_cxx_compat
8921 && field != NULL_TREE
8922 && TREE_CODE (field) == FIELD_DECL
8923 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8924 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8925 != TYPE_MAIN_VARIANT (type))
8926 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8928 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8929 if (checktype != error_mark_node
8930 && (TYPE_MAIN_VARIANT (checktype)
8931 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8932 warning_init (loc, OPT_Wc___compat,
8933 "enum conversion in initialization is invalid in C++");
8936 /* If this field is empty (and not at the end of structure),
8937 don't do anything other than checking the initializer. */
8938 if (field
8939 && (TREE_TYPE (field) == error_mark_node
8940 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8941 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8942 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8943 || DECL_CHAIN (field)))))
8944 return;
8946 /* Finally, set VALUE to the initializer value digested above. */
8947 value = new_value;
8949 /* If this element doesn't come next in sequence,
8950 put it on constructor_pending_elts. */
8951 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8952 && (!constructor_incremental
8953 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8955 if (constructor_incremental
8956 && tree_int_cst_lt (field, constructor_unfilled_index))
8957 set_nonincremental_init (braced_init_obstack);
8959 add_pending_init (loc, field, value, origtype, implicit,
8960 braced_init_obstack);
8961 return;
8963 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8964 && (!constructor_incremental
8965 || field != constructor_unfilled_fields))
8967 /* We do this for records but not for unions. In a union,
8968 no matter which field is specified, it can be initialized
8969 right away since it starts at the beginning of the union. */
8970 if (constructor_incremental)
8972 if (!constructor_unfilled_fields)
8973 set_nonincremental_init (braced_init_obstack);
8974 else
8976 tree bitpos, unfillpos;
8978 bitpos = bit_position (field);
8979 unfillpos = bit_position (constructor_unfilled_fields);
8981 if (tree_int_cst_lt (bitpos, unfillpos))
8982 set_nonincremental_init (braced_init_obstack);
8986 add_pending_init (loc, field, value, origtype, implicit,
8987 braced_init_obstack);
8988 return;
8990 else if (TREE_CODE (constructor_type) == UNION_TYPE
8991 && !vec_safe_is_empty (constructor_elements))
8993 if (!implicit)
8995 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8996 warning_init (loc, OPT_Woverride_init_side_effects,
8997 "initialized field with side-effects overwritten");
8998 else if (warn_override_init)
8999 warning_init (loc, OPT_Woverride_init,
9000 "initialized field overwritten");
9003 /* We can have just one union field set. */
9004 constructor_elements = NULL;
9007 /* Otherwise, output this element either to
9008 constructor_elements or to the assembler file. */
9010 constructor_elt celt = {field, value};
9011 vec_safe_push (constructor_elements, celt);
9013 /* Advance the variable that indicates sequential elements output. */
9014 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9015 constructor_unfilled_index
9016 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9017 bitsize_one_node);
9018 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9020 constructor_unfilled_fields
9021 = DECL_CHAIN (constructor_unfilled_fields);
9023 /* Skip any nameless bit fields. */
9024 while (constructor_unfilled_fields != 0
9025 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9026 && DECL_NAME (constructor_unfilled_fields) == 0)
9027 constructor_unfilled_fields =
9028 DECL_CHAIN (constructor_unfilled_fields);
9030 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9031 constructor_unfilled_fields = 0;
9033 /* Now output any pending elements which have become next. */
9034 if (pending)
9035 output_pending_init_elements (0, braced_init_obstack);
9038 /* Output any pending elements which have become next.
9039 As we output elements, constructor_unfilled_{fields,index}
9040 advances, which may cause other elements to become next;
9041 if so, they too are output.
9043 If ALL is 0, we return when there are
9044 no more pending elements to output now.
9046 If ALL is 1, we output space as necessary so that
9047 we can output all the pending elements. */
9048 static void
9049 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9051 struct init_node *elt = constructor_pending_elts;
9052 tree next;
9054 retry:
9056 /* Look through the whole pending tree.
9057 If we find an element that should be output now,
9058 output it. Otherwise, set NEXT to the element
9059 that comes first among those still pending. */
9061 next = 0;
9062 while (elt)
9064 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9066 if (tree_int_cst_equal (elt->purpose,
9067 constructor_unfilled_index))
9068 output_init_element (input_location, elt->value, elt->origtype,
9069 true, TREE_TYPE (constructor_type),
9070 constructor_unfilled_index, 0, false,
9071 braced_init_obstack);
9072 else if (tree_int_cst_lt (constructor_unfilled_index,
9073 elt->purpose))
9075 /* Advance to the next smaller node. */
9076 if (elt->left)
9077 elt = elt->left;
9078 else
9080 /* We have reached the smallest node bigger than the
9081 current unfilled index. Fill the space first. */
9082 next = elt->purpose;
9083 break;
9086 else
9088 /* Advance to the next bigger node. */
9089 if (elt->right)
9090 elt = elt->right;
9091 else
9093 /* We have reached the biggest node in a subtree. Find
9094 the parent of it, which is the next bigger node. */
9095 while (elt->parent && elt->parent->right == elt)
9096 elt = elt->parent;
9097 elt = elt->parent;
9098 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9099 elt->purpose))
9101 next = elt->purpose;
9102 break;
9107 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9109 tree ctor_unfilled_bitpos, elt_bitpos;
9111 /* If the current record is complete we are done. */
9112 if (constructor_unfilled_fields == 0)
9113 break;
9115 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9116 elt_bitpos = bit_position (elt->purpose);
9117 /* We can't compare fields here because there might be empty
9118 fields in between. */
9119 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9121 constructor_unfilled_fields = elt->purpose;
9122 output_init_element (input_location, elt->value, elt->origtype,
9123 true, TREE_TYPE (elt->purpose),
9124 elt->purpose, 0, false,
9125 braced_init_obstack);
9127 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9129 /* Advance to the next smaller node. */
9130 if (elt->left)
9131 elt = elt->left;
9132 else
9134 /* We have reached the smallest node bigger than the
9135 current unfilled field. Fill the space first. */
9136 next = elt->purpose;
9137 break;
9140 else
9142 /* Advance to the next bigger node. */
9143 if (elt->right)
9144 elt = elt->right;
9145 else
9147 /* We have reached the biggest node in a subtree. Find
9148 the parent of it, which is the next bigger node. */
9149 while (elt->parent && elt->parent->right == elt)
9150 elt = elt->parent;
9151 elt = elt->parent;
9152 if (elt
9153 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9154 bit_position (elt->purpose))))
9156 next = elt->purpose;
9157 break;
9164 /* Ordinarily return, but not if we want to output all
9165 and there are elements left. */
9166 if (!(all && next != 0))
9167 return;
9169 /* If it's not incremental, just skip over the gap, so that after
9170 jumping to retry we will output the next successive element. */
9171 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9172 constructor_unfilled_fields = next;
9173 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9174 constructor_unfilled_index = next;
9176 /* ELT now points to the node in the pending tree with the next
9177 initializer to output. */
9178 goto retry;
9181 /* Add one non-braced element to the current constructor level.
9182 This adjusts the current position within the constructor's type.
9183 This may also start or terminate implicit levels
9184 to handle a partly-braced initializer.
9186 Once this has found the correct level for the new element,
9187 it calls output_init_element.
9189 IMPLICIT is true if value comes from pop_init_level (1),
9190 the new initializer has been merged with the existing one
9191 and thus no warnings should be emitted about overriding an
9192 existing initializer. */
9194 void
9195 process_init_element (location_t loc, struct c_expr value, bool implicit,
9196 struct obstack * braced_init_obstack)
9198 tree orig_value = value.value;
9199 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9200 bool strict_string = value.original_code == STRING_CST;
9201 bool was_designated = designator_depth != 0;
9203 designator_depth = 0;
9204 designator_erroneous = 0;
9206 if (!implicit && value.value && !integer_zerop (value.value))
9207 constructor_zeroinit = 0;
9209 /* Handle superfluous braces around string cst as in
9210 char x[] = {"foo"}; */
9211 if (string_flag
9212 && constructor_type
9213 && !was_designated
9214 && TREE_CODE (constructor_type) == ARRAY_TYPE
9215 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9216 && integer_zerop (constructor_unfilled_index))
9218 if (constructor_stack->replacement_value.value)
9219 error_init (loc, "excess elements in char array initializer");
9220 constructor_stack->replacement_value = value;
9221 return;
9224 if (constructor_stack->replacement_value.value != 0)
9226 error_init (loc, "excess elements in struct initializer");
9227 return;
9230 /* Ignore elements of a brace group if it is entirely superfluous
9231 and has already been diagnosed. */
9232 if (constructor_type == 0)
9233 return;
9235 if (!implicit && warn_designated_init && !was_designated
9236 && TREE_CODE (constructor_type) == RECORD_TYPE
9237 && lookup_attribute ("designated_init",
9238 TYPE_ATTRIBUTES (constructor_type)))
9239 warning_init (loc,
9240 OPT_Wdesignated_init,
9241 "positional initialization of field "
9242 "in %<struct%> declared with %<designated_init%> attribute");
9244 /* If we've exhausted any levels that didn't have braces,
9245 pop them now. */
9246 while (constructor_stack->implicit)
9248 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9249 && constructor_fields == 0)
9250 process_init_element (loc,
9251 pop_init_level (loc, 1, braced_init_obstack,
9252 last_init_list_comma),
9253 true, braced_init_obstack);
9254 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9255 || VECTOR_TYPE_P (constructor_type))
9256 && constructor_max_index
9257 && tree_int_cst_lt (constructor_max_index,
9258 constructor_index))
9259 process_init_element (loc,
9260 pop_init_level (loc, 1, braced_init_obstack,
9261 last_init_list_comma),
9262 true, braced_init_obstack);
9263 else
9264 break;
9267 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9268 if (constructor_range_stack)
9270 /* If value is a compound literal and we'll be just using its
9271 content, don't put it into a SAVE_EXPR. */
9272 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9273 || !require_constant_value)
9275 tree semantic_type = NULL_TREE;
9276 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9278 semantic_type = TREE_TYPE (value.value);
9279 value.value = TREE_OPERAND (value.value, 0);
9281 value.value = c_save_expr (value.value);
9282 if (semantic_type)
9283 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9284 value.value);
9288 while (1)
9290 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9292 tree fieldtype;
9293 enum tree_code fieldcode;
9295 if (constructor_fields == 0)
9297 pedwarn_init (loc, 0, "excess elements in struct initializer");
9298 break;
9301 fieldtype = TREE_TYPE (constructor_fields);
9302 if (fieldtype != error_mark_node)
9303 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9304 fieldcode = TREE_CODE (fieldtype);
9306 /* Error for non-static initialization of a flexible array member. */
9307 if (fieldcode == ARRAY_TYPE
9308 && !require_constant_value
9309 && TYPE_SIZE (fieldtype) == NULL_TREE
9310 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9312 error_init (loc, "non-static initialization of a flexible "
9313 "array member");
9314 break;
9317 /* Error for initialization of a flexible array member with
9318 a string constant if the structure is in an array. E.g.:
9319 struct S { int x; char y[]; };
9320 struct S s[] = { { 1, "foo" } };
9321 is invalid. */
9322 if (string_flag
9323 && fieldcode == ARRAY_TYPE
9324 && constructor_depth > 1
9325 && TYPE_SIZE (fieldtype) == NULL_TREE
9326 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9328 bool in_array_p = false;
9329 for (struct constructor_stack *p = constructor_stack;
9330 p && p->type; p = p->next)
9331 if (TREE_CODE (p->type) == ARRAY_TYPE)
9333 in_array_p = true;
9334 break;
9336 if (in_array_p)
9338 error_init (loc, "initialization of flexible array "
9339 "member in a nested context");
9340 break;
9344 /* Accept a string constant to initialize a subarray. */
9345 if (value.value != 0
9346 && fieldcode == ARRAY_TYPE
9347 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9348 && string_flag)
9349 value.value = orig_value;
9350 /* Otherwise, if we have come to a subaggregate,
9351 and we don't have an element of its type, push into it. */
9352 else if (value.value != 0
9353 && value.value != error_mark_node
9354 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9355 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9356 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9358 push_init_level (loc, 1, braced_init_obstack);
9359 continue;
9362 if (value.value)
9364 push_member_name (constructor_fields);
9365 output_init_element (loc, value.value, value.original_type,
9366 strict_string, fieldtype,
9367 constructor_fields, 1, implicit,
9368 braced_init_obstack);
9369 RESTORE_SPELLING_DEPTH (constructor_depth);
9371 else
9372 /* Do the bookkeeping for an element that was
9373 directly output as a constructor. */
9375 /* For a record, keep track of end position of last field. */
9376 if (DECL_SIZE (constructor_fields))
9377 constructor_bit_index
9378 = size_binop_loc (input_location, PLUS_EXPR,
9379 bit_position (constructor_fields),
9380 DECL_SIZE (constructor_fields));
9382 /* If the current field was the first one not yet written out,
9383 it isn't now, so update. */
9384 if (constructor_unfilled_fields == constructor_fields)
9386 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9387 /* Skip any nameless bit fields. */
9388 while (constructor_unfilled_fields != 0
9389 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9390 && DECL_NAME (constructor_unfilled_fields) == 0)
9391 constructor_unfilled_fields =
9392 DECL_CHAIN (constructor_unfilled_fields);
9396 constructor_fields = DECL_CHAIN (constructor_fields);
9397 /* Skip any nameless bit fields at the beginning. */
9398 while (constructor_fields != 0
9399 && DECL_C_BIT_FIELD (constructor_fields)
9400 && DECL_NAME (constructor_fields) == 0)
9401 constructor_fields = DECL_CHAIN (constructor_fields);
9403 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9405 tree fieldtype;
9406 enum tree_code fieldcode;
9408 if (constructor_fields == 0)
9410 pedwarn_init (loc, 0,
9411 "excess elements in union initializer");
9412 break;
9415 fieldtype = TREE_TYPE (constructor_fields);
9416 if (fieldtype != error_mark_node)
9417 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9418 fieldcode = TREE_CODE (fieldtype);
9420 /* Warn that traditional C rejects initialization of unions.
9421 We skip the warning if the value is zero. This is done
9422 under the assumption that the zero initializer in user
9423 code appears conditioned on e.g. __STDC__ to avoid
9424 "missing initializer" warnings and relies on default
9425 initialization to zero in the traditional C case.
9426 We also skip the warning if the initializer is designated,
9427 again on the assumption that this must be conditional on
9428 __STDC__ anyway (and we've already complained about the
9429 member-designator already). */
9430 if (!in_system_header_at (input_location) && !constructor_designated
9431 && !(value.value && (integer_zerop (value.value)
9432 || real_zerop (value.value))))
9433 warning (OPT_Wtraditional, "traditional C rejects initialization "
9434 "of unions");
9436 /* Accept a string constant to initialize a subarray. */
9437 if (value.value != 0
9438 && fieldcode == ARRAY_TYPE
9439 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9440 && string_flag)
9441 value.value = orig_value;
9442 /* Otherwise, if we have come to a subaggregate,
9443 and we don't have an element of its type, push into it. */
9444 else if (value.value != 0
9445 && value.value != error_mark_node
9446 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9447 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9448 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9450 push_init_level (loc, 1, braced_init_obstack);
9451 continue;
9454 if (value.value)
9456 push_member_name (constructor_fields);
9457 output_init_element (loc, value.value, value.original_type,
9458 strict_string, fieldtype,
9459 constructor_fields, 1, implicit,
9460 braced_init_obstack);
9461 RESTORE_SPELLING_DEPTH (constructor_depth);
9463 else
9464 /* Do the bookkeeping for an element that was
9465 directly output as a constructor. */
9467 constructor_bit_index = DECL_SIZE (constructor_fields);
9468 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9471 constructor_fields = 0;
9473 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9475 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9476 enum tree_code eltcode = TREE_CODE (elttype);
9478 /* Accept a string constant to initialize a subarray. */
9479 if (value.value != 0
9480 && eltcode == ARRAY_TYPE
9481 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9482 && string_flag)
9483 value.value = orig_value;
9484 /* Otherwise, if we have come to a subaggregate,
9485 and we don't have an element of its type, push into it. */
9486 else if (value.value != 0
9487 && value.value != error_mark_node
9488 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9489 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9490 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9492 push_init_level (loc, 1, braced_init_obstack);
9493 continue;
9496 if (constructor_max_index != 0
9497 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9498 || integer_all_onesp (constructor_max_index)))
9500 pedwarn_init (loc, 0,
9501 "excess elements in array initializer");
9502 break;
9505 /* Now output the actual element. */
9506 if (value.value)
9508 push_array_bounds (tree_to_uhwi (constructor_index));
9509 output_init_element (loc, value.value, value.original_type,
9510 strict_string, elttype,
9511 constructor_index, 1, implicit,
9512 braced_init_obstack);
9513 RESTORE_SPELLING_DEPTH (constructor_depth);
9516 constructor_index
9517 = size_binop_loc (input_location, PLUS_EXPR,
9518 constructor_index, bitsize_one_node);
9520 if (!value.value)
9521 /* If we are doing the bookkeeping for an element that was
9522 directly output as a constructor, we must update
9523 constructor_unfilled_index. */
9524 constructor_unfilled_index = constructor_index;
9526 else if (VECTOR_TYPE_P (constructor_type))
9528 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9530 /* Do a basic check of initializer size. Note that vectors
9531 always have a fixed size derived from their type. */
9532 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9534 pedwarn_init (loc, 0,
9535 "excess elements in vector initializer");
9536 break;
9539 /* Now output the actual element. */
9540 if (value.value)
9542 if (TREE_CODE (value.value) == VECTOR_CST)
9543 elttype = TYPE_MAIN_VARIANT (constructor_type);
9544 output_init_element (loc, value.value, value.original_type,
9545 strict_string, elttype,
9546 constructor_index, 1, implicit,
9547 braced_init_obstack);
9550 constructor_index
9551 = size_binop_loc (input_location,
9552 PLUS_EXPR, constructor_index, bitsize_one_node);
9554 if (!value.value)
9555 /* If we are doing the bookkeeping for an element that was
9556 directly output as a constructor, we must update
9557 constructor_unfilled_index. */
9558 constructor_unfilled_index = constructor_index;
9561 /* Handle the sole element allowed in a braced initializer
9562 for a scalar variable. */
9563 else if (constructor_type != error_mark_node
9564 && constructor_fields == 0)
9566 pedwarn_init (loc, 0,
9567 "excess elements in scalar initializer");
9568 break;
9570 else
9572 if (value.value)
9573 output_init_element (loc, value.value, value.original_type,
9574 strict_string, constructor_type,
9575 NULL_TREE, 1, implicit,
9576 braced_init_obstack);
9577 constructor_fields = 0;
9580 /* Handle range initializers either at this level or anywhere higher
9581 in the designator stack. */
9582 if (constructor_range_stack)
9584 struct constructor_range_stack *p, *range_stack;
9585 int finish = 0;
9587 range_stack = constructor_range_stack;
9588 constructor_range_stack = 0;
9589 while (constructor_stack != range_stack->stack)
9591 gcc_assert (constructor_stack->implicit);
9592 process_init_element (loc,
9593 pop_init_level (loc, 1,
9594 braced_init_obstack,
9595 last_init_list_comma),
9596 true, braced_init_obstack);
9598 for (p = range_stack;
9599 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9600 p = p->prev)
9602 gcc_assert (constructor_stack->implicit);
9603 process_init_element (loc,
9604 pop_init_level (loc, 1,
9605 braced_init_obstack,
9606 last_init_list_comma),
9607 true, braced_init_obstack);
9610 p->index = size_binop_loc (input_location,
9611 PLUS_EXPR, p->index, bitsize_one_node);
9612 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9613 finish = 1;
9615 while (1)
9617 constructor_index = p->index;
9618 constructor_fields = p->fields;
9619 if (finish && p->range_end && p->index == p->range_start)
9621 finish = 0;
9622 p->prev = 0;
9624 p = p->next;
9625 if (!p)
9626 break;
9627 finish_implicit_inits (loc, braced_init_obstack);
9628 push_init_level (loc, 2, braced_init_obstack);
9629 p->stack = constructor_stack;
9630 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9631 p->index = p->range_start;
9634 if (!finish)
9635 constructor_range_stack = range_stack;
9636 continue;
9639 break;
9642 constructor_range_stack = 0;
9645 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9646 (guaranteed to be 'volatile' or null) and ARGS (represented using
9647 an ASM_EXPR node). */
9648 tree
9649 build_asm_stmt (tree cv_qualifier, tree args)
9651 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9652 ASM_VOLATILE_P (args) = 1;
9653 return add_stmt (args);
9656 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9657 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9658 SIMPLE indicates whether there was anything at all after the
9659 string in the asm expression -- asm("blah") and asm("blah" : )
9660 are subtly different. We use a ASM_EXPR node to represent this. */
9661 tree
9662 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9663 tree clobbers, tree labels, bool simple)
9665 tree tail;
9666 tree args;
9667 int i;
9668 const char *constraint;
9669 const char **oconstraints;
9670 bool allows_mem, allows_reg, is_inout;
9671 int ninputs, noutputs;
9673 ninputs = list_length (inputs);
9674 noutputs = list_length (outputs);
9675 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9677 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9679 /* Remove output conversions that change the type but not the mode. */
9680 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9682 tree output = TREE_VALUE (tail);
9684 output = c_fully_fold (output, false, NULL);
9686 /* ??? Really, this should not be here. Users should be using a
9687 proper lvalue, dammit. But there's a long history of using casts
9688 in the output operands. In cases like longlong.h, this becomes a
9689 primitive form of typechecking -- if the cast can be removed, then
9690 the output operand had a type of the proper width; otherwise we'll
9691 get an error. Gross, but ... */
9692 STRIP_NOPS (output);
9694 if (!lvalue_or_else (loc, output, lv_asm))
9695 output = error_mark_node;
9697 if (output != error_mark_node
9698 && (TREE_READONLY (output)
9699 || TYPE_READONLY (TREE_TYPE (output))
9700 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9701 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9702 readonly_error (loc, output, lv_asm);
9704 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9705 oconstraints[i] = constraint;
9707 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9708 &allows_mem, &allows_reg, &is_inout))
9710 /* If the operand is going to end up in memory,
9711 mark it addressable. */
9712 if (!allows_reg && !c_mark_addressable (output))
9713 output = error_mark_node;
9714 if (!(!allows_reg && allows_mem)
9715 && output != error_mark_node
9716 && VOID_TYPE_P (TREE_TYPE (output)))
9718 error_at (loc, "invalid use of void expression");
9719 output = error_mark_node;
9722 else
9723 output = error_mark_node;
9725 TREE_VALUE (tail) = output;
9728 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9730 tree input;
9732 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9733 input = TREE_VALUE (tail);
9735 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9736 oconstraints, &allows_mem, &allows_reg))
9738 /* If the operand is going to end up in memory,
9739 mark it addressable. */
9740 if (!allows_reg && allows_mem)
9742 input = c_fully_fold (input, false, NULL);
9744 /* Strip the nops as we allow this case. FIXME, this really
9745 should be rejected or made deprecated. */
9746 STRIP_NOPS (input);
9747 if (!c_mark_addressable (input))
9748 input = error_mark_node;
9750 else
9752 struct c_expr expr;
9753 memset (&expr, 0, sizeof (expr));
9754 expr.value = input;
9755 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9756 input = c_fully_fold (expr.value, false, NULL);
9758 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9760 error_at (loc, "invalid use of void expression");
9761 input = error_mark_node;
9765 else
9766 input = error_mark_node;
9768 TREE_VALUE (tail) = input;
9771 /* ASMs with labels cannot have outputs. This should have been
9772 enforced by the parser. */
9773 gcc_assert (outputs == NULL || labels == NULL);
9775 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9777 /* asm statements without outputs, including simple ones, are treated
9778 as volatile. */
9779 ASM_INPUT_P (args) = simple;
9780 ASM_VOLATILE_P (args) = (noutputs == 0);
9782 return args;
9785 /* Generate a goto statement to LABEL. LOC is the location of the
9786 GOTO. */
9788 tree
9789 c_finish_goto_label (location_t loc, tree label)
9791 tree decl = lookup_label_for_goto (loc, label);
9792 if (!decl)
9793 return NULL_TREE;
9794 TREE_USED (decl) = 1;
9796 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9797 SET_EXPR_LOCATION (t, loc);
9798 return add_stmt (t);
9802 /* Generate a computed goto statement to EXPR. LOC is the location of
9803 the GOTO. */
9805 tree
9806 c_finish_goto_ptr (location_t loc, tree expr)
9808 tree t;
9809 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9810 expr = c_fully_fold (expr, false, NULL);
9811 expr = convert (ptr_type_node, expr);
9812 t = build1 (GOTO_EXPR, void_type_node, expr);
9813 SET_EXPR_LOCATION (t, loc);
9814 return add_stmt (t);
9817 /* Generate a C `return' statement. RETVAL is the expression for what
9818 to return, or a null pointer for `return;' with no value. LOC is
9819 the location of the return statement, or the location of the expression,
9820 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9821 is the original type of RETVAL. */
9823 tree
9824 c_finish_return (location_t loc, tree retval, tree origtype)
9826 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9827 bool no_warning = false;
9828 bool npc = false;
9829 size_t rank = 0;
9831 /* Use the expansion point to handle cases such as returning NULL
9832 in a function returning void. */
9833 source_location xloc = expansion_point_location_if_in_system_header (loc);
9835 if (TREE_THIS_VOLATILE (current_function_decl))
9836 warning_at (xloc, 0,
9837 "function declared %<noreturn%> has a %<return%> statement");
9839 if (flag_cilkplus && contains_array_notation_expr (retval))
9841 /* Array notations are allowed in a return statement if it is inside a
9842 built-in array notation reduction function. */
9843 if (!find_rank (loc, retval, retval, false, &rank))
9844 return error_mark_node;
9845 if (rank >= 1)
9847 error_at (loc, "array notation expression cannot be used as a "
9848 "return value");
9849 return error_mark_node;
9852 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9854 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9855 "allowed");
9856 return error_mark_node;
9858 if (retval)
9860 tree semantic_type = NULL_TREE;
9861 npc = null_pointer_constant_p (retval);
9862 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9864 semantic_type = TREE_TYPE (retval);
9865 retval = TREE_OPERAND (retval, 0);
9867 retval = c_fully_fold (retval, false, NULL);
9868 if (semantic_type)
9869 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9872 if (!retval)
9874 current_function_returns_null = 1;
9875 if ((warn_return_type || flag_isoc99)
9876 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9878 bool warned_here;
9879 if (flag_isoc99)
9880 warned_here = pedwarn
9881 (loc, 0,
9882 "%<return%> with no value, in function returning non-void");
9883 else
9884 warned_here = warning_at
9885 (loc, OPT_Wreturn_type,
9886 "%<return%> with no value, in function returning non-void");
9887 no_warning = true;
9888 if (warned_here)
9889 inform (DECL_SOURCE_LOCATION (current_function_decl),
9890 "declared here");
9893 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9895 current_function_returns_null = 1;
9896 bool warned_here;
9897 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9898 warned_here = pedwarn
9899 (xloc, 0,
9900 "%<return%> with a value, in function returning void");
9901 else
9902 warned_here = pedwarn
9903 (xloc, OPT_Wpedantic, "ISO C forbids "
9904 "%<return%> with expression, in function returning void");
9905 if (warned_here)
9906 inform (DECL_SOURCE_LOCATION (current_function_decl),
9907 "declared here");
9909 else
9911 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9912 retval, origtype, ic_return,
9913 npc, NULL_TREE, NULL_TREE, 0);
9914 tree res = DECL_RESULT (current_function_decl);
9915 tree inner;
9916 bool save;
9918 current_function_returns_value = 1;
9919 if (t == error_mark_node)
9920 return NULL_TREE;
9922 save = in_late_binary_op;
9923 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9924 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9925 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9926 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9927 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9928 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9929 in_late_binary_op = true;
9930 inner = t = convert (TREE_TYPE (res), t);
9931 in_late_binary_op = save;
9933 /* Strip any conversions, additions, and subtractions, and see if
9934 we are returning the address of a local variable. Warn if so. */
9935 while (1)
9937 switch (TREE_CODE (inner))
9939 CASE_CONVERT:
9940 case NON_LVALUE_EXPR:
9941 case PLUS_EXPR:
9942 case POINTER_PLUS_EXPR:
9943 inner = TREE_OPERAND (inner, 0);
9944 continue;
9946 case MINUS_EXPR:
9947 /* If the second operand of the MINUS_EXPR has a pointer
9948 type (or is converted from it), this may be valid, so
9949 don't give a warning. */
9951 tree op1 = TREE_OPERAND (inner, 1);
9953 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9954 && (CONVERT_EXPR_P (op1)
9955 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9956 op1 = TREE_OPERAND (op1, 0);
9958 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9959 break;
9961 inner = TREE_OPERAND (inner, 0);
9962 continue;
9965 case ADDR_EXPR:
9966 inner = TREE_OPERAND (inner, 0);
9968 while (REFERENCE_CLASS_P (inner)
9969 && !INDIRECT_REF_P (inner))
9970 inner = TREE_OPERAND (inner, 0);
9972 if (DECL_P (inner)
9973 && !DECL_EXTERNAL (inner)
9974 && !TREE_STATIC (inner)
9975 && DECL_CONTEXT (inner) == current_function_decl)
9977 if (TREE_CODE (inner) == LABEL_DECL)
9978 warning_at (loc, OPT_Wreturn_local_addr,
9979 "function returns address of label");
9980 else
9982 warning_at (loc, OPT_Wreturn_local_addr,
9983 "function returns address of local variable");
9984 tree zero = build_zero_cst (TREE_TYPE (res));
9985 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9988 break;
9990 default:
9991 break;
9994 break;
9997 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9998 SET_EXPR_LOCATION (retval, loc);
10000 if (warn_sequence_point)
10001 verify_sequence_points (retval);
10004 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10005 TREE_NO_WARNING (ret_stmt) |= no_warning;
10006 return add_stmt (ret_stmt);
10009 struct c_switch {
10010 /* The SWITCH_EXPR being built. */
10011 tree switch_expr;
10013 /* The original type of the testing expression, i.e. before the
10014 default conversion is applied. */
10015 tree orig_type;
10017 /* A splay-tree mapping the low element of a case range to the high
10018 element, or NULL_TREE if there is no high element. Used to
10019 determine whether or not a new case label duplicates an old case
10020 label. We need a tree, rather than simply a hash table, because
10021 of the GNU case range extension. */
10022 splay_tree cases;
10024 /* The bindings at the point of the switch. This is used for
10025 warnings crossing decls when branching to a case label. */
10026 struct c_spot_bindings *bindings;
10028 /* The next node on the stack. */
10029 struct c_switch *next;
10031 /* Remember whether the controlling expression had boolean type
10032 before integer promotions for the sake of -Wswitch-bool. */
10033 bool bool_cond_p;
10035 /* Remember whether there was a case value that is outside the
10036 range of the ORIG_TYPE. */
10037 bool outside_range_p;
10040 /* A stack of the currently active switch statements. The innermost
10041 switch statement is on the top of the stack. There is no need to
10042 mark the stack for garbage collection because it is only active
10043 during the processing of the body of a function, and we never
10044 collect at that point. */
10046 struct c_switch *c_switch_stack;
10048 /* Start a C switch statement, testing expression EXP. Return the new
10049 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10050 SWITCH_COND_LOC is the location of the switch's condition.
10051 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10053 tree
10054 c_start_case (location_t switch_loc,
10055 location_t switch_cond_loc,
10056 tree exp, bool explicit_cast_p)
10058 tree orig_type = error_mark_node;
10059 bool bool_cond_p = false;
10060 struct c_switch *cs;
10062 if (exp != error_mark_node)
10064 orig_type = TREE_TYPE (exp);
10066 if (!INTEGRAL_TYPE_P (orig_type))
10068 if (orig_type != error_mark_node)
10070 error_at (switch_cond_loc, "switch quantity not an integer");
10071 orig_type = error_mark_node;
10073 exp = integer_zero_node;
10075 else
10077 tree type = TYPE_MAIN_VARIANT (orig_type);
10078 tree e = exp;
10080 /* Warn if the condition has boolean value. */
10081 while (TREE_CODE (e) == COMPOUND_EXPR)
10082 e = TREE_OPERAND (e, 1);
10084 if ((TREE_CODE (type) == BOOLEAN_TYPE
10085 || truth_value_p (TREE_CODE (e)))
10086 /* Explicit cast to int suppresses this warning. */
10087 && !(TREE_CODE (type) == INTEGER_TYPE
10088 && explicit_cast_p))
10089 bool_cond_p = true;
10091 if (!in_system_header_at (input_location)
10092 && (type == long_integer_type_node
10093 || type == long_unsigned_type_node))
10094 warning_at (switch_cond_loc,
10095 OPT_Wtraditional, "%<long%> switch expression not "
10096 "converted to %<int%> in ISO C");
10098 exp = c_fully_fold (exp, false, NULL);
10099 exp = default_conversion (exp);
10101 if (warn_sequence_point)
10102 verify_sequence_points (exp);
10106 /* Add this new SWITCH_EXPR to the stack. */
10107 cs = XNEW (struct c_switch);
10108 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10109 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10110 cs->orig_type = orig_type;
10111 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10112 cs->bindings = c_get_switch_bindings ();
10113 cs->bool_cond_p = bool_cond_p;
10114 cs->outside_range_p = false;
10115 cs->next = c_switch_stack;
10116 c_switch_stack = cs;
10118 return add_stmt (cs->switch_expr);
10121 /* Process a case label at location LOC. */
10123 tree
10124 do_case (location_t loc, tree low_value, tree high_value)
10126 tree label = NULL_TREE;
10128 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10130 low_value = c_fully_fold (low_value, false, NULL);
10131 if (TREE_CODE (low_value) == INTEGER_CST)
10132 pedwarn (loc, OPT_Wpedantic,
10133 "case label is not an integer constant expression");
10136 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10138 high_value = c_fully_fold (high_value, false, NULL);
10139 if (TREE_CODE (high_value) == INTEGER_CST)
10140 pedwarn (input_location, OPT_Wpedantic,
10141 "case label is not an integer constant expression");
10144 if (c_switch_stack == NULL)
10146 if (low_value)
10147 error_at (loc, "case label not within a switch statement");
10148 else
10149 error_at (loc, "%<default%> label not within a switch statement");
10150 return NULL_TREE;
10153 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10154 EXPR_LOCATION (c_switch_stack->switch_expr),
10155 loc))
10156 return NULL_TREE;
10158 label = c_add_case_label (loc, c_switch_stack->cases,
10159 SWITCH_COND (c_switch_stack->switch_expr),
10160 c_switch_stack->orig_type,
10161 low_value, high_value,
10162 &c_switch_stack->outside_range_p);
10163 if (label == error_mark_node)
10164 label = NULL_TREE;
10165 return label;
10168 /* Finish the switch statement. TYPE is the original type of the
10169 controlling expression of the switch, or NULL_TREE. */
10171 void
10172 c_finish_case (tree body, tree type)
10174 struct c_switch *cs = c_switch_stack;
10175 location_t switch_location;
10177 SWITCH_BODY (cs->switch_expr) = body;
10179 /* Emit warnings as needed. */
10180 switch_location = EXPR_LOCATION (cs->switch_expr);
10181 c_do_switch_warnings (cs->cases, switch_location,
10182 type ? type : TREE_TYPE (cs->switch_expr),
10183 SWITCH_COND (cs->switch_expr),
10184 cs->bool_cond_p, cs->outside_range_p);
10186 /* Pop the stack. */
10187 c_switch_stack = cs->next;
10188 splay_tree_delete (cs->cases);
10189 c_release_switch_bindings (cs->bindings);
10190 XDELETE (cs);
10193 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10194 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10195 may be null. */
10197 void
10198 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10199 tree else_block)
10201 tree stmt;
10203 /* If the condition has array notations, then the rank of the then_block and
10204 else_block must be either 0 or be equal to the rank of the condition. If
10205 the condition does not have array notations then break them up as it is
10206 broken up in a normal expression. */
10207 if (flag_cilkplus && contains_array_notation_expr (cond))
10209 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10210 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10211 return;
10212 if (then_block
10213 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10214 return;
10215 if (else_block
10216 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10217 return;
10218 if (cond_rank != then_rank && then_rank != 0)
10220 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10221 " and the then-block");
10222 return;
10224 else if (cond_rank != else_rank && else_rank != 0)
10226 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10227 " and the else-block");
10228 return;
10232 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10233 SET_EXPR_LOCATION (stmt, if_locus);
10234 add_stmt (stmt);
10237 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10238 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10239 is false for DO loops. INCR is the FOR increment expression. BODY is
10240 the statement controlled by the loop. BLAB is the break label. CLAB is
10241 the continue label. Everything is allowed to be NULL. */
10243 void
10244 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10245 tree blab, tree clab, bool cond_is_first)
10247 tree entry = NULL, exit = NULL, t;
10249 /* In theory could forbid cilk spawn for loop increment expression,
10250 but it should work just fine. */
10252 /* If the condition is zero don't generate a loop construct. */
10253 if (cond && integer_zerop (cond))
10255 if (cond_is_first)
10257 t = build_and_jump (&blab);
10258 SET_EXPR_LOCATION (t, start_locus);
10259 add_stmt (t);
10262 else
10264 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10266 /* If we have an exit condition, then we build an IF with gotos either
10267 out of the loop, or to the top of it. If there's no exit condition,
10268 then we just build a jump back to the top. */
10269 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10271 if (cond && !integer_nonzerop (cond))
10273 /* Canonicalize the loop condition to the end. This means
10274 generating a branch to the loop condition. Reuse the
10275 continue label, if possible. */
10276 if (cond_is_first)
10278 if (incr || !clab)
10280 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10281 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10283 else
10284 t = build1 (GOTO_EXPR, void_type_node, clab);
10285 SET_EXPR_LOCATION (t, start_locus);
10286 add_stmt (t);
10289 t = build_and_jump (&blab);
10290 if (cond_is_first)
10291 exit = fold_build3_loc (start_locus,
10292 COND_EXPR, void_type_node, cond, exit, t);
10293 else
10294 exit = fold_build3_loc (input_location,
10295 COND_EXPR, void_type_node, cond, exit, t);
10297 else
10299 /* For the backward-goto's location of an unconditional loop
10300 use the beginning of the body, or, if there is none, the
10301 top of the loop. */
10302 location_t loc = EXPR_LOCATION (expr_first (body));
10303 if (loc == UNKNOWN_LOCATION)
10304 loc = start_locus;
10305 SET_EXPR_LOCATION (exit, loc);
10308 add_stmt (top);
10311 if (body)
10312 add_stmt (body);
10313 if (clab)
10314 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10315 if (incr)
10316 add_stmt (incr);
10317 if (entry)
10318 add_stmt (entry);
10319 if (exit)
10320 add_stmt (exit);
10321 if (blab)
10322 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10325 tree
10326 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10328 bool skip;
10329 tree label = *label_p;
10331 /* In switch statements break is sometimes stylistically used after
10332 a return statement. This can lead to spurious warnings about
10333 control reaching the end of a non-void function when it is
10334 inlined. Note that we are calling block_may_fallthru with
10335 language specific tree nodes; this works because
10336 block_may_fallthru returns true when given something it does not
10337 understand. */
10338 skip = !block_may_fallthru (cur_stmt_list);
10340 if (!label)
10342 if (!skip)
10343 *label_p = label = create_artificial_label (loc);
10345 else if (TREE_CODE (label) == LABEL_DECL)
10347 else switch (TREE_INT_CST_LOW (label))
10349 case 0:
10350 if (is_break)
10351 error_at (loc, "break statement not within loop or switch");
10352 else
10353 error_at (loc, "continue statement not within a loop");
10354 return NULL_TREE;
10356 case 1:
10357 gcc_assert (is_break);
10358 error_at (loc, "break statement used with OpenMP for loop");
10359 return NULL_TREE;
10361 case 2:
10362 if (is_break)
10363 error ("break statement within %<#pragma simd%> loop body");
10364 else
10365 error ("continue statement within %<#pragma simd%> loop body");
10366 return NULL_TREE;
10368 default:
10369 gcc_unreachable ();
10372 if (skip)
10373 return NULL_TREE;
10375 if (!is_break)
10376 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10378 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10381 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10383 static void
10384 emit_side_effect_warnings (location_t loc, tree expr)
10386 if (expr == error_mark_node)
10388 else if (!TREE_SIDE_EFFECTS (expr))
10390 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10391 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10393 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10395 tree r = expr;
10396 location_t cloc = loc;
10397 while (TREE_CODE (r) == COMPOUND_EXPR)
10399 if (EXPR_HAS_LOCATION (r))
10400 cloc = EXPR_LOCATION (r);
10401 r = TREE_OPERAND (r, 1);
10403 if (!TREE_SIDE_EFFECTS (r)
10404 && !VOID_TYPE_P (TREE_TYPE (r))
10405 && !CONVERT_EXPR_P (r)
10406 && !TREE_NO_WARNING (r)
10407 && !TREE_NO_WARNING (expr))
10408 warning_at (cloc, OPT_Wunused_value,
10409 "right-hand operand of comma expression has no effect");
10411 else
10412 warn_if_unused_value (expr, loc);
10415 /* Process an expression as if it were a complete statement. Emit
10416 diagnostics, but do not call ADD_STMT. LOC is the location of the
10417 statement. */
10419 tree
10420 c_process_expr_stmt (location_t loc, tree expr)
10422 tree exprv;
10424 if (!expr)
10425 return NULL_TREE;
10427 expr = c_fully_fold (expr, false, NULL);
10429 if (warn_sequence_point)
10430 verify_sequence_points (expr);
10432 if (TREE_TYPE (expr) != error_mark_node
10433 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10434 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10435 error_at (loc, "expression statement has incomplete type");
10437 /* If we're not processing a statement expression, warn about unused values.
10438 Warnings for statement expressions will be emitted later, once we figure
10439 out which is the result. */
10440 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10441 && warn_unused_value)
10442 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10444 exprv = expr;
10445 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10446 exprv = TREE_OPERAND (exprv, 1);
10447 while (CONVERT_EXPR_P (exprv))
10448 exprv = TREE_OPERAND (exprv, 0);
10449 if (DECL_P (exprv)
10450 || handled_component_p (exprv)
10451 || TREE_CODE (exprv) == ADDR_EXPR)
10452 mark_exp_read (exprv);
10454 /* If the expression is not of a type to which we cannot assign a line
10455 number, wrap the thing in a no-op NOP_EXPR. */
10456 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10458 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10459 SET_EXPR_LOCATION (expr, loc);
10462 return expr;
10465 /* Emit an expression as a statement. LOC is the location of the
10466 expression. */
10468 tree
10469 c_finish_expr_stmt (location_t loc, tree expr)
10471 if (expr)
10472 return add_stmt (c_process_expr_stmt (loc, expr));
10473 else
10474 return NULL;
10477 /* Do the opposite and emit a statement as an expression. To begin,
10478 create a new binding level and return it. */
10480 tree
10481 c_begin_stmt_expr (void)
10483 tree ret;
10485 /* We must force a BLOCK for this level so that, if it is not expanded
10486 later, there is a way to turn off the entire subtree of blocks that
10487 are contained in it. */
10488 keep_next_level ();
10489 ret = c_begin_compound_stmt (true);
10491 c_bindings_start_stmt_expr (c_switch_stack == NULL
10492 ? NULL
10493 : c_switch_stack->bindings);
10495 /* Mark the current statement list as belonging to a statement list. */
10496 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10498 return ret;
10501 /* LOC is the location of the compound statement to which this body
10502 belongs. */
10504 tree
10505 c_finish_stmt_expr (location_t loc, tree body)
10507 tree last, type, tmp, val;
10508 tree *last_p;
10510 body = c_end_compound_stmt (loc, body, true);
10512 c_bindings_end_stmt_expr (c_switch_stack == NULL
10513 ? NULL
10514 : c_switch_stack->bindings);
10516 /* Locate the last statement in BODY. See c_end_compound_stmt
10517 about always returning a BIND_EXPR. */
10518 last_p = &BIND_EXPR_BODY (body);
10519 last = BIND_EXPR_BODY (body);
10521 continue_searching:
10522 if (TREE_CODE (last) == STATEMENT_LIST)
10524 tree_stmt_iterator i;
10526 /* This can happen with degenerate cases like ({ }). No value. */
10527 if (!TREE_SIDE_EFFECTS (last))
10528 return body;
10530 /* If we're supposed to generate side effects warnings, process
10531 all of the statements except the last. */
10532 if (warn_unused_value)
10534 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10536 location_t tloc;
10537 tree t = tsi_stmt (i);
10539 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10540 emit_side_effect_warnings (tloc, t);
10543 else
10544 i = tsi_last (last);
10545 last_p = tsi_stmt_ptr (i);
10546 last = *last_p;
10549 /* If the end of the list is exception related, then the list was split
10550 by a call to push_cleanup. Continue searching. */
10551 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10552 || TREE_CODE (last) == TRY_CATCH_EXPR)
10554 last_p = &TREE_OPERAND (last, 0);
10555 last = *last_p;
10556 goto continue_searching;
10559 if (last == error_mark_node)
10560 return last;
10562 /* In the case that the BIND_EXPR is not necessary, return the
10563 expression out from inside it. */
10564 if (last == BIND_EXPR_BODY (body)
10565 && BIND_EXPR_VARS (body) == NULL)
10567 /* Even if this looks constant, do not allow it in a constant
10568 expression. */
10569 last = c_wrap_maybe_const (last, true);
10570 /* Do not warn if the return value of a statement expression is
10571 unused. */
10572 TREE_NO_WARNING (last) = 1;
10573 return last;
10576 /* Extract the type of said expression. */
10577 type = TREE_TYPE (last);
10579 /* If we're not returning a value at all, then the BIND_EXPR that
10580 we already have is a fine expression to return. */
10581 if (!type || VOID_TYPE_P (type))
10582 return body;
10584 /* Now that we've located the expression containing the value, it seems
10585 silly to make voidify_wrapper_expr repeat the process. Create a
10586 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10587 tmp = create_tmp_var_raw (type);
10589 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10590 tree_expr_nonnegative_p giving up immediately. */
10591 val = last;
10592 if (TREE_CODE (val) == NOP_EXPR
10593 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10594 val = TREE_OPERAND (val, 0);
10596 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10597 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10600 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10601 SET_EXPR_LOCATION (t, loc);
10602 return t;
10606 /* Begin and end compound statements. This is as simple as pushing
10607 and popping new statement lists from the tree. */
10609 tree
10610 c_begin_compound_stmt (bool do_scope)
10612 tree stmt = push_stmt_list ();
10613 if (do_scope)
10614 push_scope ();
10615 return stmt;
10618 /* End a compound statement. STMT is the statement. LOC is the
10619 location of the compound statement-- this is usually the location
10620 of the opening brace. */
10622 tree
10623 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10625 tree block = NULL;
10627 if (do_scope)
10629 if (c_dialect_objc ())
10630 objc_clear_super_receiver ();
10631 block = pop_scope ();
10634 stmt = pop_stmt_list (stmt);
10635 stmt = c_build_bind_expr (loc, block, stmt);
10637 /* If this compound statement is nested immediately inside a statement
10638 expression, then force a BIND_EXPR to be created. Otherwise we'll
10639 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10640 STATEMENT_LISTs merge, and thus we can lose track of what statement
10641 was really last. */
10642 if (building_stmt_list_p ()
10643 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10644 && TREE_CODE (stmt) != BIND_EXPR)
10646 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10647 TREE_SIDE_EFFECTS (stmt) = 1;
10648 SET_EXPR_LOCATION (stmt, loc);
10651 return stmt;
10654 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10655 when the current scope is exited. EH_ONLY is true when this is not
10656 meant to apply to normal control flow transfer. */
10658 void
10659 push_cleanup (tree decl, tree cleanup, bool eh_only)
10661 enum tree_code code;
10662 tree stmt, list;
10663 bool stmt_expr;
10665 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10666 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10667 add_stmt (stmt);
10668 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10669 list = push_stmt_list ();
10670 TREE_OPERAND (stmt, 0) = list;
10671 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10674 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10675 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10677 static tree
10678 build_vec_cmp (tree_code code, tree type,
10679 tree arg0, tree arg1)
10681 tree zero_vec = build_zero_cst (type);
10682 tree minus_one_vec = build_minus_one_cst (type);
10683 tree cmp_type = build_same_sized_truth_vector_type (type);
10684 tree cmp = build2 (code, cmp_type, arg0, arg1);
10685 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10688 /* Build a binary-operation expression without default conversions.
10689 CODE is the kind of expression to build.
10690 LOCATION is the operator's location.
10691 This function differs from `build' in several ways:
10692 the data type of the result is computed and recorded in it,
10693 warnings are generated if arg data types are invalid,
10694 special handling for addition and subtraction of pointers is known,
10695 and some optimization is done (operations on narrow ints
10696 are done in the narrower type when that gives the same result).
10697 Constant folding is also done before the result is returned.
10699 Note that the operands will never have enumeral types, or function
10700 or array types, because either they will have the default conversions
10701 performed or they have both just been converted to some other type in which
10702 the arithmetic is to be done. */
10704 tree
10705 build_binary_op (location_t location, enum tree_code code,
10706 tree orig_op0, tree orig_op1, int convert_p)
10708 tree type0, type1, orig_type0, orig_type1;
10709 tree eptype;
10710 enum tree_code code0, code1;
10711 tree op0, op1;
10712 tree ret = error_mark_node;
10713 const char *invalid_op_diag;
10714 bool op0_int_operands, op1_int_operands;
10715 bool int_const, int_const_or_overflow, int_operands;
10717 /* Expression code to give to the expression when it is built.
10718 Normally this is CODE, which is what the caller asked for,
10719 but in some special cases we change it. */
10720 enum tree_code resultcode = code;
10722 /* Data type in which the computation is to be performed.
10723 In the simplest cases this is the common type of the arguments. */
10724 tree result_type = NULL;
10726 /* When the computation is in excess precision, the type of the
10727 final EXCESS_PRECISION_EXPR. */
10728 tree semantic_result_type = NULL;
10730 /* Nonzero means operands have already been type-converted
10731 in whatever way is necessary.
10732 Zero means they need to be converted to RESULT_TYPE. */
10733 int converted = 0;
10735 /* Nonzero means create the expression with this type, rather than
10736 RESULT_TYPE. */
10737 tree build_type = 0;
10739 /* Nonzero means after finally constructing the expression
10740 convert it to this type. */
10741 tree final_type = 0;
10743 /* Nonzero if this is an operation like MIN or MAX which can
10744 safely be computed in short if both args are promoted shorts.
10745 Also implies COMMON.
10746 -1 indicates a bitwise operation; this makes a difference
10747 in the exact conditions for when it is safe to do the operation
10748 in a narrower mode. */
10749 int shorten = 0;
10751 /* Nonzero if this is a comparison operation;
10752 if both args are promoted shorts, compare the original shorts.
10753 Also implies COMMON. */
10754 int short_compare = 0;
10756 /* Nonzero if this is a right-shift operation, which can be computed on the
10757 original short and then promoted if the operand is a promoted short. */
10758 int short_shift = 0;
10760 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10761 int common = 0;
10763 /* True means types are compatible as far as ObjC is concerned. */
10764 bool objc_ok;
10766 /* True means this is an arithmetic operation that may need excess
10767 precision. */
10768 bool may_need_excess_precision;
10770 /* True means this is a boolean operation that converts both its
10771 operands to truth-values. */
10772 bool boolean_op = false;
10774 /* Remember whether we're doing / or %. */
10775 bool doing_div_or_mod = false;
10777 /* Remember whether we're doing << or >>. */
10778 bool doing_shift = false;
10780 /* Tree holding instrumentation expression. */
10781 tree instrument_expr = NULL;
10783 if (location == UNKNOWN_LOCATION)
10784 location = input_location;
10786 op0 = orig_op0;
10787 op1 = orig_op1;
10789 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10790 if (op0_int_operands)
10791 op0 = remove_c_maybe_const_expr (op0);
10792 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10793 if (op1_int_operands)
10794 op1 = remove_c_maybe_const_expr (op1);
10795 int_operands = (op0_int_operands && op1_int_operands);
10796 if (int_operands)
10798 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10799 && TREE_CODE (orig_op1) == INTEGER_CST);
10800 int_const = (int_const_or_overflow
10801 && !TREE_OVERFLOW (orig_op0)
10802 && !TREE_OVERFLOW (orig_op1));
10804 else
10805 int_const = int_const_or_overflow = false;
10807 /* Do not apply default conversion in mixed vector/scalar expression. */
10808 if (convert_p
10809 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10811 op0 = default_conversion (op0);
10812 op1 = default_conversion (op1);
10815 /* When Cilk Plus is enabled and there are array notations inside op0, then
10816 we check to see if there are builtin array notation functions. If
10817 so, then we take on the type of the array notation inside it. */
10818 if (flag_cilkplus && contains_array_notation_expr (op0))
10819 orig_type0 = type0 = find_correct_array_notation_type (op0);
10820 else
10821 orig_type0 = type0 = TREE_TYPE (op0);
10823 if (flag_cilkplus && contains_array_notation_expr (op1))
10824 orig_type1 = type1 = find_correct_array_notation_type (op1);
10825 else
10826 orig_type1 = type1 = TREE_TYPE (op1);
10828 /* The expression codes of the data types of the arguments tell us
10829 whether the arguments are integers, floating, pointers, etc. */
10830 code0 = TREE_CODE (type0);
10831 code1 = TREE_CODE (type1);
10833 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10834 STRIP_TYPE_NOPS (op0);
10835 STRIP_TYPE_NOPS (op1);
10837 /* If an error was already reported for one of the arguments,
10838 avoid reporting another error. */
10840 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10841 return error_mark_node;
10843 if (code0 == POINTER_TYPE
10844 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10845 return error_mark_node;
10847 if (code1 == POINTER_TYPE
10848 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10849 return error_mark_node;
10851 if ((invalid_op_diag
10852 = targetm.invalid_binary_op (code, type0, type1)))
10854 error_at (location, invalid_op_diag);
10855 return error_mark_node;
10858 switch (code)
10860 case PLUS_EXPR:
10861 case MINUS_EXPR:
10862 case MULT_EXPR:
10863 case TRUNC_DIV_EXPR:
10864 case CEIL_DIV_EXPR:
10865 case FLOOR_DIV_EXPR:
10866 case ROUND_DIV_EXPR:
10867 case EXACT_DIV_EXPR:
10868 may_need_excess_precision = true;
10869 break;
10870 default:
10871 may_need_excess_precision = false;
10872 break;
10874 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10876 op0 = TREE_OPERAND (op0, 0);
10877 type0 = TREE_TYPE (op0);
10879 else if (may_need_excess_precision
10880 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10882 type0 = eptype;
10883 op0 = convert (eptype, op0);
10885 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10887 op1 = TREE_OPERAND (op1, 0);
10888 type1 = TREE_TYPE (op1);
10890 else if (may_need_excess_precision
10891 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10893 type1 = eptype;
10894 op1 = convert (eptype, op1);
10897 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10899 /* In case when one of the operands of the binary operation is
10900 a vector and another is a scalar -- convert scalar to vector. */
10901 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10903 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10904 true);
10906 switch (convert_flag)
10908 case stv_error:
10909 return error_mark_node;
10910 case stv_firstarg:
10912 bool maybe_const = true;
10913 tree sc;
10914 sc = c_fully_fold (op0, false, &maybe_const);
10915 sc = save_expr (sc);
10916 sc = convert (TREE_TYPE (type1), sc);
10917 op0 = build_vector_from_val (type1, sc);
10918 if (!maybe_const)
10919 op0 = c_wrap_maybe_const (op0, true);
10920 orig_type0 = type0 = TREE_TYPE (op0);
10921 code0 = TREE_CODE (type0);
10922 converted = 1;
10923 break;
10925 case stv_secondarg:
10927 bool maybe_const = true;
10928 tree sc;
10929 sc = c_fully_fold (op1, false, &maybe_const);
10930 sc = save_expr (sc);
10931 sc = convert (TREE_TYPE (type0), sc);
10932 op1 = build_vector_from_val (type0, sc);
10933 if (!maybe_const)
10934 op1 = c_wrap_maybe_const (op1, true);
10935 orig_type1 = type1 = TREE_TYPE (op1);
10936 code1 = TREE_CODE (type1);
10937 converted = 1;
10938 break;
10940 default:
10941 break;
10945 switch (code)
10947 case PLUS_EXPR:
10948 /* Handle the pointer + int case. */
10949 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10951 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10952 goto return_build_binary_op;
10954 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10956 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10957 goto return_build_binary_op;
10959 else
10960 common = 1;
10961 break;
10963 case MINUS_EXPR:
10964 /* Subtraction of two similar pointers.
10965 We must subtract them as integers, then divide by object size. */
10966 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10967 && comp_target_types (location, type0, type1))
10969 ret = pointer_diff (location, op0, op1);
10970 goto return_build_binary_op;
10972 /* Handle pointer minus int. Just like pointer plus int. */
10973 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10975 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10976 goto return_build_binary_op;
10978 else
10979 common = 1;
10980 break;
10982 case MULT_EXPR:
10983 common = 1;
10984 break;
10986 case TRUNC_DIV_EXPR:
10987 case CEIL_DIV_EXPR:
10988 case FLOOR_DIV_EXPR:
10989 case ROUND_DIV_EXPR:
10990 case EXACT_DIV_EXPR:
10991 doing_div_or_mod = true;
10992 warn_for_div_by_zero (location, op1);
10994 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10995 || code0 == FIXED_POINT_TYPE
10996 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10997 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10998 || code1 == FIXED_POINT_TYPE
10999 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11001 enum tree_code tcode0 = code0, tcode1 = code1;
11003 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11004 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11005 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11006 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11008 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11009 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11010 resultcode = RDIV_EXPR;
11011 else
11012 /* Although it would be tempting to shorten always here, that
11013 loses on some targets, since the modulo instruction is
11014 undefined if the quotient can't be represented in the
11015 computation mode. We shorten only if unsigned or if
11016 dividing by something we know != -1. */
11017 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11018 || (TREE_CODE (op1) == INTEGER_CST
11019 && !integer_all_onesp (op1)));
11020 common = 1;
11022 break;
11024 case BIT_AND_EXPR:
11025 case BIT_IOR_EXPR:
11026 case BIT_XOR_EXPR:
11027 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11028 shorten = -1;
11029 /* Allow vector types which are not floating point types. */
11030 else if (code0 == VECTOR_TYPE
11031 && code1 == VECTOR_TYPE
11032 && !VECTOR_FLOAT_TYPE_P (type0)
11033 && !VECTOR_FLOAT_TYPE_P (type1))
11034 common = 1;
11035 break;
11037 case TRUNC_MOD_EXPR:
11038 case FLOOR_MOD_EXPR:
11039 doing_div_or_mod = true;
11040 warn_for_div_by_zero (location, op1);
11042 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11043 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11044 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11045 common = 1;
11046 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11048 /* Although it would be tempting to shorten always here, that loses
11049 on some targets, since the modulo instruction is undefined if the
11050 quotient can't be represented in the computation mode. We shorten
11051 only if unsigned or if dividing by something we know != -1. */
11052 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11053 || (TREE_CODE (op1) == INTEGER_CST
11054 && !integer_all_onesp (op1)));
11055 common = 1;
11057 break;
11059 case TRUTH_ANDIF_EXPR:
11060 case TRUTH_ORIF_EXPR:
11061 case TRUTH_AND_EXPR:
11062 case TRUTH_OR_EXPR:
11063 case TRUTH_XOR_EXPR:
11064 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11065 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11066 || code0 == FIXED_POINT_TYPE)
11067 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11068 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11069 || code1 == FIXED_POINT_TYPE))
11071 /* Result of these operations is always an int,
11072 but that does not mean the operands should be
11073 converted to ints! */
11074 result_type = integer_type_node;
11075 if (op0_int_operands)
11077 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11078 op0 = remove_c_maybe_const_expr (op0);
11080 else
11081 op0 = c_objc_common_truthvalue_conversion (location, op0);
11082 if (op1_int_operands)
11084 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11085 op1 = remove_c_maybe_const_expr (op1);
11087 else
11088 op1 = c_objc_common_truthvalue_conversion (location, op1);
11089 converted = 1;
11090 boolean_op = true;
11092 if (code == TRUTH_ANDIF_EXPR)
11094 int_const_or_overflow = (int_operands
11095 && TREE_CODE (orig_op0) == INTEGER_CST
11096 && (op0 == truthvalue_false_node
11097 || TREE_CODE (orig_op1) == INTEGER_CST));
11098 int_const = (int_const_or_overflow
11099 && !TREE_OVERFLOW (orig_op0)
11100 && (op0 == truthvalue_false_node
11101 || !TREE_OVERFLOW (orig_op1)));
11103 else if (code == TRUTH_ORIF_EXPR)
11105 int_const_or_overflow = (int_operands
11106 && TREE_CODE (orig_op0) == INTEGER_CST
11107 && (op0 == truthvalue_true_node
11108 || TREE_CODE (orig_op1) == INTEGER_CST));
11109 int_const = (int_const_or_overflow
11110 && !TREE_OVERFLOW (orig_op0)
11111 && (op0 == truthvalue_true_node
11112 || !TREE_OVERFLOW (orig_op1)));
11114 break;
11116 /* Shift operations: result has same type as first operand;
11117 always convert second operand to int.
11118 Also set SHORT_SHIFT if shifting rightward. */
11120 case RSHIFT_EXPR:
11121 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11122 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11123 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11124 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11126 result_type = type0;
11127 converted = 1;
11129 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11130 || code0 == VECTOR_TYPE)
11131 && code1 == INTEGER_TYPE)
11133 doing_shift = true;
11134 if (TREE_CODE (op1) == INTEGER_CST)
11136 if (tree_int_cst_sgn (op1) < 0)
11138 int_const = false;
11139 if (c_inhibit_evaluation_warnings == 0)
11140 warning_at (location, OPT_Wshift_count_negative,
11141 "right shift count is negative");
11143 else if (code0 == VECTOR_TYPE)
11145 if (compare_tree_int (op1,
11146 TYPE_PRECISION (TREE_TYPE (type0)))
11147 >= 0)
11149 int_const = false;
11150 if (c_inhibit_evaluation_warnings == 0)
11151 warning_at (location, OPT_Wshift_count_overflow,
11152 "right shift count >= width of vector element");
11155 else
11157 if (!integer_zerop (op1))
11158 short_shift = 1;
11160 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11162 int_const = false;
11163 if (c_inhibit_evaluation_warnings == 0)
11164 warning_at (location, OPT_Wshift_count_overflow,
11165 "right shift count >= width of type");
11170 /* Use the type of the value to be shifted. */
11171 result_type = type0;
11172 /* Avoid converting op1 to result_type later. */
11173 converted = 1;
11175 break;
11177 case LSHIFT_EXPR:
11178 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11179 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11180 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11181 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11183 result_type = type0;
11184 converted = 1;
11186 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11187 || code0 == VECTOR_TYPE)
11188 && code1 == INTEGER_TYPE)
11190 doing_shift = true;
11191 if (TREE_CODE (op0) == INTEGER_CST
11192 && tree_int_cst_sgn (op0) < 0)
11194 /* Don't reject a left shift of a negative value in a context
11195 where a constant expression is needed in C90. */
11196 if (flag_isoc99)
11197 int_const = false;
11198 if (c_inhibit_evaluation_warnings == 0)
11199 warning_at (location, OPT_Wshift_negative_value,
11200 "left shift of negative value");
11202 if (TREE_CODE (op1) == INTEGER_CST)
11204 if (tree_int_cst_sgn (op1) < 0)
11206 int_const = false;
11207 if (c_inhibit_evaluation_warnings == 0)
11208 warning_at (location, OPT_Wshift_count_negative,
11209 "left shift count is negative");
11211 else if (code0 == VECTOR_TYPE)
11213 if (compare_tree_int (op1,
11214 TYPE_PRECISION (TREE_TYPE (type0)))
11215 >= 0)
11217 int_const = false;
11218 if (c_inhibit_evaluation_warnings == 0)
11219 warning_at (location, OPT_Wshift_count_overflow,
11220 "left shift count >= width of vector element");
11223 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11225 int_const = false;
11226 if (c_inhibit_evaluation_warnings == 0)
11227 warning_at (location, OPT_Wshift_count_overflow,
11228 "left shift count >= width of type");
11230 else if (TREE_CODE (op0) == INTEGER_CST
11231 && maybe_warn_shift_overflow (location, op0, op1)
11232 && flag_isoc99)
11233 int_const = false;
11236 /* Use the type of the value to be shifted. */
11237 result_type = type0;
11238 /* Avoid converting op1 to result_type later. */
11239 converted = 1;
11241 break;
11243 case EQ_EXPR:
11244 case NE_EXPR:
11245 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11247 tree intt;
11248 if (!vector_types_compatible_elements_p (type0, type1))
11250 error_at (location, "comparing vectors with different "
11251 "element types");
11252 return error_mark_node;
11255 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11257 error_at (location, "comparing vectors with different "
11258 "number of elements");
11259 return error_mark_node;
11262 /* It's not precisely specified how the usual arithmetic
11263 conversions apply to the vector types. Here, we use
11264 the unsigned type if one of the operands is signed and
11265 the other one is unsigned. */
11266 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11268 if (!TYPE_UNSIGNED (type0))
11269 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11270 else
11271 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11272 warning_at (location, OPT_Wsign_compare, "comparison between "
11273 "types %qT and %qT", type0, type1);
11276 /* Always construct signed integer vector type. */
11277 intt = c_common_type_for_size (GET_MODE_BITSIZE
11278 (TYPE_MODE (TREE_TYPE (type0))), 0);
11279 result_type = build_opaque_vector_type (intt,
11280 TYPE_VECTOR_SUBPARTS (type0));
11281 converted = 1;
11282 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11283 goto return_build_binary_op;
11285 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11286 warning_at (location,
11287 OPT_Wfloat_equal,
11288 "comparing floating point with == or != is unsafe");
11289 /* Result of comparison is always int,
11290 but don't convert the args to int! */
11291 build_type = integer_type_node;
11292 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11293 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11294 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11295 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11296 short_compare = 1;
11297 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11299 if (TREE_CODE (op0) == ADDR_EXPR
11300 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11301 && !from_macro_expansion_at (location))
11303 if (code == EQ_EXPR)
11304 warning_at (location,
11305 OPT_Waddress,
11306 "the comparison will always evaluate as %<false%> "
11307 "for the address of %qD will never be NULL",
11308 TREE_OPERAND (op0, 0));
11309 else
11310 warning_at (location,
11311 OPT_Waddress,
11312 "the comparison will always evaluate as %<true%> "
11313 "for the address of %qD will never be NULL",
11314 TREE_OPERAND (op0, 0));
11316 result_type = type0;
11318 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11320 if (TREE_CODE (op1) == ADDR_EXPR
11321 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11322 && !from_macro_expansion_at (location))
11324 if (code == EQ_EXPR)
11325 warning_at (location,
11326 OPT_Waddress,
11327 "the comparison will always evaluate as %<false%> "
11328 "for the address of %qD will never be NULL",
11329 TREE_OPERAND (op1, 0));
11330 else
11331 warning_at (location,
11332 OPT_Waddress,
11333 "the comparison will always evaluate as %<true%> "
11334 "for the address of %qD will never be NULL",
11335 TREE_OPERAND (op1, 0));
11337 result_type = type1;
11339 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11341 tree tt0 = TREE_TYPE (type0);
11342 tree tt1 = TREE_TYPE (type1);
11343 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11344 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11345 addr_space_t as_common = ADDR_SPACE_GENERIC;
11347 /* Anything compares with void *. void * compares with anything.
11348 Otherwise, the targets must be compatible
11349 and both must be object or both incomplete. */
11350 if (comp_target_types (location, type0, type1))
11351 result_type = common_pointer_type (type0, type1);
11352 else if (!addr_space_superset (as0, as1, &as_common))
11354 error_at (location, "comparison of pointers to "
11355 "disjoint address spaces");
11356 return error_mark_node;
11358 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11360 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11361 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11362 "comparison of %<void *%> with function pointer");
11364 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11366 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11367 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11368 "comparison of %<void *%> with function pointer");
11370 else
11371 /* Avoid warning about the volatile ObjC EH puts on decls. */
11372 if (!objc_ok)
11373 pedwarn (location, 0,
11374 "comparison of distinct pointer types lacks a cast");
11376 if (result_type == NULL_TREE)
11378 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11379 result_type = build_pointer_type
11380 (build_qualified_type (void_type_node, qual));
11383 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11385 result_type = type0;
11386 pedwarn (location, 0, "comparison between pointer and integer");
11388 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11390 result_type = type1;
11391 pedwarn (location, 0, "comparison between pointer and integer");
11393 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11394 || truth_value_p (TREE_CODE (orig_op0)))
11395 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11396 || truth_value_p (TREE_CODE (orig_op1))))
11397 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11398 break;
11400 case LE_EXPR:
11401 case GE_EXPR:
11402 case LT_EXPR:
11403 case GT_EXPR:
11404 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11406 tree intt;
11407 if (!vector_types_compatible_elements_p (type0, type1))
11409 error_at (location, "comparing vectors with different "
11410 "element types");
11411 return error_mark_node;
11414 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11416 error_at (location, "comparing vectors with different "
11417 "number of elements");
11418 return error_mark_node;
11421 /* It's not precisely specified how the usual arithmetic
11422 conversions apply to the vector types. Here, we use
11423 the unsigned type if one of the operands is signed and
11424 the other one is unsigned. */
11425 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11427 if (!TYPE_UNSIGNED (type0))
11428 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11429 else
11430 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11431 warning_at (location, OPT_Wsign_compare, "comparison between "
11432 "types %qT and %qT", type0, type1);
11435 /* Always construct signed integer vector type. */
11436 intt = c_common_type_for_size (GET_MODE_BITSIZE
11437 (TYPE_MODE (TREE_TYPE (type0))), 0);
11438 result_type = build_opaque_vector_type (intt,
11439 TYPE_VECTOR_SUBPARTS (type0));
11440 converted = 1;
11441 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11442 goto return_build_binary_op;
11444 build_type = integer_type_node;
11445 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11446 || code0 == FIXED_POINT_TYPE)
11447 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11448 || code1 == FIXED_POINT_TYPE))
11449 short_compare = 1;
11450 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11452 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11453 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11454 addr_space_t as_common;
11456 if (comp_target_types (location, type0, type1))
11458 result_type = common_pointer_type (type0, type1);
11459 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11460 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11461 pedwarn (location, 0,
11462 "comparison of complete and incomplete pointers");
11463 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11464 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11465 "ordered comparisons of pointers to functions");
11466 else if (null_pointer_constant_p (orig_op0)
11467 || null_pointer_constant_p (orig_op1))
11468 warning_at (location, OPT_Wextra,
11469 "ordered comparison of pointer with null pointer");
11472 else if (!addr_space_superset (as0, as1, &as_common))
11474 error_at (location, "comparison of pointers to "
11475 "disjoint address spaces");
11476 return error_mark_node;
11478 else
11480 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11481 result_type = build_pointer_type
11482 (build_qualified_type (void_type_node, qual));
11483 pedwarn (location, 0,
11484 "comparison of distinct pointer types lacks a cast");
11487 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11489 result_type = type0;
11490 if (pedantic)
11491 pedwarn (location, OPT_Wpedantic,
11492 "ordered comparison of pointer with integer zero");
11493 else if (extra_warnings)
11494 warning_at (location, OPT_Wextra,
11495 "ordered comparison of pointer with integer zero");
11497 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11499 result_type = type1;
11500 if (pedantic)
11501 pedwarn (location, OPT_Wpedantic,
11502 "ordered comparison of pointer with integer zero");
11503 else if (extra_warnings)
11504 warning_at (location, OPT_Wextra,
11505 "ordered comparison of pointer with integer zero");
11507 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11509 result_type = type0;
11510 pedwarn (location, 0, "comparison between pointer and integer");
11512 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11514 result_type = type1;
11515 pedwarn (location, 0, "comparison between pointer and integer");
11517 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11518 || truth_value_p (TREE_CODE (orig_op0)))
11519 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11520 || truth_value_p (TREE_CODE (orig_op1))))
11521 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11522 break;
11524 default:
11525 gcc_unreachable ();
11528 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11529 return error_mark_node;
11531 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11532 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11533 || !vector_types_compatible_elements_p (type0, type1)))
11535 gcc_rich_location richloc (location);
11536 richloc.maybe_add_expr (orig_op0);
11537 richloc.maybe_add_expr (orig_op1);
11538 binary_op_error (&richloc, code, type0, type1);
11539 return error_mark_node;
11542 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11543 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11545 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11546 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11548 bool first_complex = (code0 == COMPLEX_TYPE);
11549 bool second_complex = (code1 == COMPLEX_TYPE);
11550 int none_complex = (!first_complex && !second_complex);
11552 if (shorten || common || short_compare)
11554 result_type = c_common_type (type0, type1);
11555 do_warn_double_promotion (result_type, type0, type1,
11556 "implicit conversion from %qT to %qT "
11557 "to match other operand of binary "
11558 "expression",
11559 location);
11560 if (result_type == error_mark_node)
11561 return error_mark_node;
11564 if (first_complex != second_complex
11565 && (code == PLUS_EXPR
11566 || code == MINUS_EXPR
11567 || code == MULT_EXPR
11568 || (code == TRUNC_DIV_EXPR && first_complex))
11569 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11570 && flag_signed_zeros)
11572 /* An operation on mixed real/complex operands must be
11573 handled specially, but the language-independent code can
11574 more easily optimize the plain complex arithmetic if
11575 -fno-signed-zeros. */
11576 tree real_type = TREE_TYPE (result_type);
11577 tree real, imag;
11578 if (type0 != orig_type0 || type1 != orig_type1)
11580 gcc_assert (may_need_excess_precision && common);
11581 semantic_result_type = c_common_type (orig_type0, orig_type1);
11583 if (first_complex)
11585 if (TREE_TYPE (op0) != result_type)
11586 op0 = convert_and_check (location, result_type, op0);
11587 if (TREE_TYPE (op1) != real_type)
11588 op1 = convert_and_check (location, real_type, op1);
11590 else
11592 if (TREE_TYPE (op0) != real_type)
11593 op0 = convert_and_check (location, real_type, op0);
11594 if (TREE_TYPE (op1) != result_type)
11595 op1 = convert_and_check (location, result_type, op1);
11597 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11598 return error_mark_node;
11599 if (first_complex)
11601 op0 = c_save_expr (op0);
11602 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11603 op0, true);
11604 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11605 op0, true);
11606 switch (code)
11608 case MULT_EXPR:
11609 case TRUNC_DIV_EXPR:
11610 op1 = c_save_expr (op1);
11611 imag = build2 (resultcode, real_type, imag, op1);
11612 /* Fall through. */
11613 case PLUS_EXPR:
11614 case MINUS_EXPR:
11615 real = build2 (resultcode, real_type, real, op1);
11616 break;
11617 default:
11618 gcc_unreachable();
11621 else
11623 op1 = c_save_expr (op1);
11624 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11625 op1, true);
11626 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11627 op1, true);
11628 switch (code)
11630 case MULT_EXPR:
11631 op0 = c_save_expr (op0);
11632 imag = build2 (resultcode, real_type, op0, imag);
11633 /* Fall through. */
11634 case PLUS_EXPR:
11635 real = build2 (resultcode, real_type, op0, real);
11636 break;
11637 case MINUS_EXPR:
11638 real = build2 (resultcode, real_type, op0, real);
11639 imag = build1 (NEGATE_EXPR, real_type, imag);
11640 break;
11641 default:
11642 gcc_unreachable();
11645 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11646 goto return_build_binary_op;
11649 /* For certain operations (which identify themselves by shorten != 0)
11650 if both args were extended from the same smaller type,
11651 do the arithmetic in that type and then extend.
11653 shorten !=0 and !=1 indicates a bitwise operation.
11654 For them, this optimization is safe only if
11655 both args are zero-extended or both are sign-extended.
11656 Otherwise, we might change the result.
11657 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11658 but calculated in (unsigned short) it would be (unsigned short)-1. */
11660 if (shorten && none_complex)
11662 final_type = result_type;
11663 result_type = shorten_binary_op (result_type, op0, op1,
11664 shorten == -1);
11667 /* Shifts can be shortened if shifting right. */
11669 if (short_shift)
11671 int unsigned_arg;
11672 tree arg0 = get_narrower (op0, &unsigned_arg);
11674 final_type = result_type;
11676 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11677 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11679 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11680 && tree_int_cst_sgn (op1) > 0
11681 /* We can shorten only if the shift count is less than the
11682 number of bits in the smaller type size. */
11683 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11684 /* We cannot drop an unsigned shift after sign-extension. */
11685 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11687 /* Do an unsigned shift if the operand was zero-extended. */
11688 result_type
11689 = c_common_signed_or_unsigned_type (unsigned_arg,
11690 TREE_TYPE (arg0));
11691 /* Convert value-to-be-shifted to that type. */
11692 if (TREE_TYPE (op0) != result_type)
11693 op0 = convert (result_type, op0);
11694 converted = 1;
11698 /* Comparison operations are shortened too but differently.
11699 They identify themselves by setting short_compare = 1. */
11701 if (short_compare)
11703 /* Don't write &op0, etc., because that would prevent op0
11704 from being kept in a register.
11705 Instead, make copies of the our local variables and
11706 pass the copies by reference, then copy them back afterward. */
11707 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11708 enum tree_code xresultcode = resultcode;
11709 tree val
11710 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11711 &xresultcode);
11713 if (val != 0)
11715 ret = val;
11716 goto return_build_binary_op;
11719 op0 = xop0, op1 = xop1;
11720 converted = 1;
11721 resultcode = xresultcode;
11723 if (c_inhibit_evaluation_warnings == 0)
11725 bool op0_maybe_const = true;
11726 bool op1_maybe_const = true;
11727 tree orig_op0_folded, orig_op1_folded;
11729 if (in_late_binary_op)
11731 orig_op0_folded = orig_op0;
11732 orig_op1_folded = orig_op1;
11734 else
11736 /* Fold for the sake of possible warnings, as in
11737 build_conditional_expr. This requires the
11738 "original" values to be folded, not just op0 and
11739 op1. */
11740 c_inhibit_evaluation_warnings++;
11741 op0 = c_fully_fold (op0, require_constant_value,
11742 &op0_maybe_const);
11743 op1 = c_fully_fold (op1, require_constant_value,
11744 &op1_maybe_const);
11745 c_inhibit_evaluation_warnings--;
11746 orig_op0_folded = c_fully_fold (orig_op0,
11747 require_constant_value,
11748 NULL);
11749 orig_op1_folded = c_fully_fold (orig_op1,
11750 require_constant_value,
11751 NULL);
11754 if (warn_sign_compare)
11755 warn_for_sign_compare (location, orig_op0_folded,
11756 orig_op1_folded, op0, op1,
11757 result_type, resultcode);
11758 if (!in_late_binary_op && !int_operands)
11760 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11761 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11762 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11763 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11769 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11770 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11771 Then the expression will be built.
11772 It will be given type FINAL_TYPE if that is nonzero;
11773 otherwise, it will be given type RESULT_TYPE. */
11775 if (!result_type)
11777 gcc_rich_location richloc (location);
11778 richloc.maybe_add_expr (orig_op0);
11779 richloc.maybe_add_expr (orig_op1);
11780 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11781 return error_mark_node;
11784 if (build_type == NULL_TREE)
11786 build_type = result_type;
11787 if ((type0 != orig_type0 || type1 != orig_type1)
11788 && !boolean_op)
11790 gcc_assert (may_need_excess_precision && common);
11791 semantic_result_type = c_common_type (orig_type0, orig_type1);
11795 if (!converted)
11797 op0 = ep_convert_and_check (location, result_type, op0,
11798 semantic_result_type);
11799 op1 = ep_convert_and_check (location, result_type, op1,
11800 semantic_result_type);
11802 /* This can happen if one operand has a vector type, and the other
11803 has a different type. */
11804 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11805 return error_mark_node;
11808 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11809 | SANITIZE_FLOAT_DIVIDE))
11810 && do_ubsan_in_current_function ()
11811 && (doing_div_or_mod || doing_shift)
11812 && !require_constant_value)
11814 /* OP0 and/or OP1 might have side-effects. */
11815 op0 = c_save_expr (op0);
11816 op1 = c_save_expr (op1);
11817 op0 = c_fully_fold (op0, false, NULL);
11818 op1 = c_fully_fold (op1, false, NULL);
11819 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11820 | SANITIZE_FLOAT_DIVIDE)))
11821 instrument_expr = ubsan_instrument_division (location, op0, op1);
11822 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11823 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11826 /* Treat expressions in initializers specially as they can't trap. */
11827 if (int_const_or_overflow)
11828 ret = (require_constant_value
11829 ? fold_build2_initializer_loc (location, resultcode, build_type,
11830 op0, op1)
11831 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11832 else
11833 ret = build2 (resultcode, build_type, op0, op1);
11834 if (final_type != 0)
11835 ret = convert (final_type, ret);
11837 return_build_binary_op:
11838 gcc_assert (ret != error_mark_node);
11839 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11840 ret = (int_operands
11841 ? note_integer_operands (ret)
11842 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11843 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11844 && !in_late_binary_op)
11845 ret = note_integer_operands (ret);
11846 if (semantic_result_type)
11847 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11848 protected_set_expr_location (ret, location);
11850 if (instrument_expr != NULL)
11851 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11852 instrument_expr, ret);
11854 return ret;
11858 /* Convert EXPR to be a truth-value, validating its type for this
11859 purpose. LOCATION is the source location for the expression. */
11861 tree
11862 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11864 bool int_const, int_operands;
11866 switch (TREE_CODE (TREE_TYPE (expr)))
11868 case ARRAY_TYPE:
11869 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11870 return error_mark_node;
11872 case RECORD_TYPE:
11873 error_at (location, "used struct type value where scalar is required");
11874 return error_mark_node;
11876 case UNION_TYPE:
11877 error_at (location, "used union type value where scalar is required");
11878 return error_mark_node;
11880 case VOID_TYPE:
11881 error_at (location, "void value not ignored as it ought to be");
11882 return error_mark_node;
11884 case POINTER_TYPE:
11885 if (reject_gcc_builtin (expr))
11886 return error_mark_node;
11887 break;
11889 case FUNCTION_TYPE:
11890 gcc_unreachable ();
11892 case VECTOR_TYPE:
11893 error_at (location, "used vector type where scalar is required");
11894 return error_mark_node;
11896 default:
11897 break;
11900 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11901 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11902 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11904 expr = remove_c_maybe_const_expr (expr);
11905 expr = build2 (NE_EXPR, integer_type_node, expr,
11906 convert (TREE_TYPE (expr), integer_zero_node));
11907 expr = note_integer_operands (expr);
11909 else
11910 /* ??? Should we also give an error for vectors rather than leaving
11911 those to give errors later? */
11912 expr = c_common_truthvalue_conversion (location, expr);
11914 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11916 if (TREE_OVERFLOW (expr))
11917 return expr;
11918 else
11919 return note_integer_operands (expr);
11921 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11922 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11923 return expr;
11927 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11928 required. */
11930 tree
11931 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11933 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11935 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11936 /* Executing a compound literal inside a function reinitializes
11937 it. */
11938 if (!TREE_STATIC (decl))
11939 *se = true;
11940 return decl;
11942 else
11943 return expr;
11946 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11947 statement. LOC is the location of the construct. */
11949 tree
11950 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11951 tree clauses)
11953 body = c_end_compound_stmt (loc, body, true);
11955 tree stmt = make_node (code);
11956 TREE_TYPE (stmt) = void_type_node;
11957 OMP_BODY (stmt) = body;
11958 OMP_CLAUSES (stmt) = clauses;
11959 SET_EXPR_LOCATION (stmt, loc);
11961 return add_stmt (stmt);
11964 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11965 statement. LOC is the location of the OACC_DATA. */
11967 tree
11968 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11970 tree stmt;
11972 block = c_end_compound_stmt (loc, block, true);
11974 stmt = make_node (OACC_DATA);
11975 TREE_TYPE (stmt) = void_type_node;
11976 OACC_DATA_CLAUSES (stmt) = clauses;
11977 OACC_DATA_BODY (stmt) = block;
11978 SET_EXPR_LOCATION (stmt, loc);
11980 return add_stmt (stmt);
11983 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11984 statement. LOC is the location of the OACC_HOST_DATA. */
11986 tree
11987 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11989 tree stmt;
11991 block = c_end_compound_stmt (loc, block, true);
11993 stmt = make_node (OACC_HOST_DATA);
11994 TREE_TYPE (stmt) = void_type_node;
11995 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11996 OACC_HOST_DATA_BODY (stmt) = block;
11997 SET_EXPR_LOCATION (stmt, loc);
11999 return add_stmt (stmt);
12002 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12004 tree
12005 c_begin_omp_parallel (void)
12007 tree block;
12009 keep_next_level ();
12010 block = c_begin_compound_stmt (true);
12012 return block;
12015 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12016 statement. LOC is the location of the OMP_PARALLEL. */
12018 tree
12019 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12021 tree stmt;
12023 block = c_end_compound_stmt (loc, block, true);
12025 stmt = make_node (OMP_PARALLEL);
12026 TREE_TYPE (stmt) = void_type_node;
12027 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12028 OMP_PARALLEL_BODY (stmt) = block;
12029 SET_EXPR_LOCATION (stmt, loc);
12031 return add_stmt (stmt);
12034 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12036 tree
12037 c_begin_omp_task (void)
12039 tree block;
12041 keep_next_level ();
12042 block = c_begin_compound_stmt (true);
12044 return block;
12047 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12048 statement. LOC is the location of the #pragma. */
12050 tree
12051 c_finish_omp_task (location_t loc, tree clauses, tree block)
12053 tree stmt;
12055 block = c_end_compound_stmt (loc, block, true);
12057 stmt = make_node (OMP_TASK);
12058 TREE_TYPE (stmt) = void_type_node;
12059 OMP_TASK_CLAUSES (stmt) = clauses;
12060 OMP_TASK_BODY (stmt) = block;
12061 SET_EXPR_LOCATION (stmt, loc);
12063 return add_stmt (stmt);
12066 /* Generate GOMP_cancel call for #pragma omp cancel. */
12068 void
12069 c_finish_omp_cancel (location_t loc, tree clauses)
12071 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12072 int mask = 0;
12073 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12074 mask = 1;
12075 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12076 mask = 2;
12077 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12078 mask = 4;
12079 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12080 mask = 8;
12081 else
12083 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12084 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12085 "clauses");
12086 return;
12088 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12089 if (ifc != NULL_TREE)
12091 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12092 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12093 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12094 build_zero_cst (type));
12096 else
12097 ifc = boolean_true_node;
12098 tree stmt = build_call_expr_loc (loc, fn, 2,
12099 build_int_cst (integer_type_node, mask),
12100 ifc);
12101 add_stmt (stmt);
12104 /* Generate GOMP_cancellation_point call for
12105 #pragma omp cancellation point. */
12107 void
12108 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12110 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12111 int mask = 0;
12112 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12113 mask = 1;
12114 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12115 mask = 2;
12116 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12117 mask = 4;
12118 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12119 mask = 8;
12120 else
12122 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12123 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12124 "clauses");
12125 return;
12127 tree stmt = build_call_expr_loc (loc, fn, 1,
12128 build_int_cst (integer_type_node, mask));
12129 add_stmt (stmt);
12132 /* Helper function for handle_omp_array_sections. Called recursively
12133 to handle multiple array-section-subscripts. C is the clause,
12134 T current expression (initially OMP_CLAUSE_DECL), which is either
12135 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12136 expression if specified, TREE_VALUE length expression if specified,
12137 TREE_CHAIN is what it has been specified after, or some decl.
12138 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12139 set to true if any of the array-section-subscript could have length
12140 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12141 first array-section-subscript which is known not to have length
12142 of one. Given say:
12143 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12144 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12145 all are or may have length of 1, array-section-subscript [:2] is the
12146 first one known not to have length 1. For array-section-subscript
12147 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12148 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12149 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12150 case though, as some lengths could be zero. */
12152 static tree
12153 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12154 bool &maybe_zero_len, unsigned int &first_non_one,
12155 enum c_omp_region_type ort)
12157 tree ret, low_bound, length, type;
12158 if (TREE_CODE (t) != TREE_LIST)
12160 if (error_operand_p (t))
12161 return error_mark_node;
12162 ret = t;
12163 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12164 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12166 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12167 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12168 return error_mark_node;
12170 if (TREE_CODE (t) == COMPONENT_REF
12171 && ort == C_ORT_OMP
12172 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12173 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12174 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12176 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12178 error_at (OMP_CLAUSE_LOCATION (c),
12179 "bit-field %qE in %qs clause",
12180 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12181 return error_mark_node;
12183 while (TREE_CODE (t) == COMPONENT_REF)
12185 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12187 error_at (OMP_CLAUSE_LOCATION (c),
12188 "%qE is a member of a union", t);
12189 return error_mark_node;
12191 t = TREE_OPERAND (t, 0);
12194 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12196 if (DECL_P (t))
12197 error_at (OMP_CLAUSE_LOCATION (c),
12198 "%qD is not a variable in %qs clause", t,
12199 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12200 else
12201 error_at (OMP_CLAUSE_LOCATION (c),
12202 "%qE is not a variable in %qs clause", t,
12203 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12204 return error_mark_node;
12206 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12207 && TYPE_ATOMIC (TREE_TYPE (t)))
12209 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12210 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12211 return error_mark_node;
12213 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12214 && VAR_P (t)
12215 && DECL_THREAD_LOCAL_P (t))
12217 error_at (OMP_CLAUSE_LOCATION (c),
12218 "%qD is threadprivate variable in %qs clause", t,
12219 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12220 return error_mark_node;
12222 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12223 && TYPE_ATOMIC (TREE_TYPE (t))
12224 && POINTER_TYPE_P (TREE_TYPE (t)))
12226 /* If the array section is pointer based and the pointer
12227 itself is _Atomic qualified, we need to atomically load
12228 the pointer. */
12229 c_expr expr;
12230 memset (&expr, 0, sizeof (expr));
12231 expr.value = ret;
12232 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12233 expr, false, false);
12234 ret = expr.value;
12236 return ret;
12239 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12240 maybe_zero_len, first_non_one, ort);
12241 if (ret == error_mark_node || ret == NULL_TREE)
12242 return ret;
12244 type = TREE_TYPE (ret);
12245 low_bound = TREE_PURPOSE (t);
12246 length = TREE_VALUE (t);
12248 if (low_bound == error_mark_node || length == error_mark_node)
12249 return error_mark_node;
12251 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12253 error_at (OMP_CLAUSE_LOCATION (c),
12254 "low bound %qE of array section does not have integral type",
12255 low_bound);
12256 return error_mark_node;
12258 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12260 error_at (OMP_CLAUSE_LOCATION (c),
12261 "length %qE of array section does not have integral type",
12262 length);
12263 return error_mark_node;
12265 if (low_bound
12266 && TREE_CODE (low_bound) == INTEGER_CST
12267 && TYPE_PRECISION (TREE_TYPE (low_bound))
12268 > TYPE_PRECISION (sizetype))
12269 low_bound = fold_convert (sizetype, low_bound);
12270 if (length
12271 && TREE_CODE (length) == INTEGER_CST
12272 && TYPE_PRECISION (TREE_TYPE (length))
12273 > TYPE_PRECISION (sizetype))
12274 length = fold_convert (sizetype, length);
12275 if (low_bound == NULL_TREE)
12276 low_bound = integer_zero_node;
12278 if (length != NULL_TREE)
12280 if (!integer_nonzerop (length))
12282 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12283 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12285 if (integer_zerop (length))
12287 error_at (OMP_CLAUSE_LOCATION (c),
12288 "zero length array section in %qs clause",
12289 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12290 return error_mark_node;
12293 else
12294 maybe_zero_len = true;
12296 if (first_non_one == types.length ()
12297 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12298 first_non_one++;
12300 if (TREE_CODE (type) == ARRAY_TYPE)
12302 if (length == NULL_TREE
12303 && (TYPE_DOMAIN (type) == NULL_TREE
12304 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12306 error_at (OMP_CLAUSE_LOCATION (c),
12307 "for unknown bound array type length expression must "
12308 "be specified");
12309 return error_mark_node;
12311 if (TREE_CODE (low_bound) == INTEGER_CST
12312 && tree_int_cst_sgn (low_bound) == -1)
12314 error_at (OMP_CLAUSE_LOCATION (c),
12315 "negative low bound in array section in %qs clause",
12316 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12317 return error_mark_node;
12319 if (length != NULL_TREE
12320 && TREE_CODE (length) == INTEGER_CST
12321 && tree_int_cst_sgn (length) == -1)
12323 error_at (OMP_CLAUSE_LOCATION (c),
12324 "negative length in array section in %qs clause",
12325 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12326 return error_mark_node;
12328 if (TYPE_DOMAIN (type)
12329 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12330 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12331 == INTEGER_CST)
12333 tree size = size_binop (PLUS_EXPR,
12334 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12335 size_one_node);
12336 if (TREE_CODE (low_bound) == INTEGER_CST)
12338 if (tree_int_cst_lt (size, low_bound))
12340 error_at (OMP_CLAUSE_LOCATION (c),
12341 "low bound %qE above array section size "
12342 "in %qs clause", low_bound,
12343 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12344 return error_mark_node;
12346 if (tree_int_cst_equal (size, low_bound))
12348 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12349 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12351 error_at (OMP_CLAUSE_LOCATION (c),
12352 "zero length array section in %qs clause",
12353 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12354 return error_mark_node;
12356 maybe_zero_len = true;
12358 else if (length == NULL_TREE
12359 && first_non_one == types.length ()
12360 && tree_int_cst_equal
12361 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12362 low_bound))
12363 first_non_one++;
12365 else if (length == NULL_TREE)
12367 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12368 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12369 maybe_zero_len = true;
12370 if (first_non_one == types.length ())
12371 first_non_one++;
12373 if (length && TREE_CODE (length) == INTEGER_CST)
12375 if (tree_int_cst_lt (size, length))
12377 error_at (OMP_CLAUSE_LOCATION (c),
12378 "length %qE above array section size "
12379 "in %qs clause", length,
12380 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12381 return error_mark_node;
12383 if (TREE_CODE (low_bound) == INTEGER_CST)
12385 tree lbpluslen
12386 = size_binop (PLUS_EXPR,
12387 fold_convert (sizetype, low_bound),
12388 fold_convert (sizetype, length));
12389 if (TREE_CODE (lbpluslen) == INTEGER_CST
12390 && tree_int_cst_lt (size, lbpluslen))
12392 error_at (OMP_CLAUSE_LOCATION (c),
12393 "high bound %qE above array section size "
12394 "in %qs clause", lbpluslen,
12395 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12396 return error_mark_node;
12401 else if (length == NULL_TREE)
12403 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12404 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12405 maybe_zero_len = true;
12406 if (first_non_one == types.length ())
12407 first_non_one++;
12410 /* For [lb:] we will need to evaluate lb more than once. */
12411 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12413 tree lb = c_save_expr (low_bound);
12414 if (lb != low_bound)
12416 TREE_PURPOSE (t) = lb;
12417 low_bound = lb;
12421 else if (TREE_CODE (type) == POINTER_TYPE)
12423 if (length == NULL_TREE)
12425 error_at (OMP_CLAUSE_LOCATION (c),
12426 "for pointer type length expression must be specified");
12427 return error_mark_node;
12429 if (length != NULL_TREE
12430 && TREE_CODE (length) == INTEGER_CST
12431 && tree_int_cst_sgn (length) == -1)
12433 error_at (OMP_CLAUSE_LOCATION (c),
12434 "negative length in array section in %qs clause",
12435 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12436 return error_mark_node;
12438 /* If there is a pointer type anywhere but in the very first
12439 array-section-subscript, the array section can't be contiguous. */
12440 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12441 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12443 error_at (OMP_CLAUSE_LOCATION (c),
12444 "array section is not contiguous in %qs clause",
12445 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12446 return error_mark_node;
12449 else
12451 error_at (OMP_CLAUSE_LOCATION (c),
12452 "%qE does not have pointer or array type", ret);
12453 return error_mark_node;
12455 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12456 types.safe_push (TREE_TYPE (ret));
12457 /* We will need to evaluate lb more than once. */
12458 tree lb = c_save_expr (low_bound);
12459 if (lb != low_bound)
12461 TREE_PURPOSE (t) = lb;
12462 low_bound = lb;
12464 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12465 return ret;
12468 /* Handle array sections for clause C. */
12470 static bool
12471 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12473 bool maybe_zero_len = false;
12474 unsigned int first_non_one = 0;
12475 auto_vec<tree, 10> types;
12476 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12477 maybe_zero_len, first_non_one,
12478 ort);
12479 if (first == error_mark_node)
12480 return true;
12481 if (first == NULL_TREE)
12482 return false;
12483 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12485 tree t = OMP_CLAUSE_DECL (c);
12486 tree tem = NULL_TREE;
12487 /* Need to evaluate side effects in the length expressions
12488 if any. */
12489 while (TREE_CODE (t) == TREE_LIST)
12491 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12493 if (tem == NULL_TREE)
12494 tem = TREE_VALUE (t);
12495 else
12496 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12497 TREE_VALUE (t), tem);
12499 t = TREE_CHAIN (t);
12501 if (tem)
12502 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12503 first = c_fully_fold (first, false, NULL);
12504 OMP_CLAUSE_DECL (c) = first;
12506 else
12508 unsigned int num = types.length (), i;
12509 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12510 tree condition = NULL_TREE;
12512 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12513 maybe_zero_len = true;
12515 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12516 t = TREE_CHAIN (t))
12518 tree low_bound = TREE_PURPOSE (t);
12519 tree length = TREE_VALUE (t);
12521 i--;
12522 if (low_bound
12523 && TREE_CODE (low_bound) == INTEGER_CST
12524 && TYPE_PRECISION (TREE_TYPE (low_bound))
12525 > TYPE_PRECISION (sizetype))
12526 low_bound = fold_convert (sizetype, low_bound);
12527 if (length
12528 && TREE_CODE (length) == INTEGER_CST
12529 && TYPE_PRECISION (TREE_TYPE (length))
12530 > TYPE_PRECISION (sizetype))
12531 length = fold_convert (sizetype, length);
12532 if (low_bound == NULL_TREE)
12533 low_bound = integer_zero_node;
12534 if (!maybe_zero_len && i > first_non_one)
12536 if (integer_nonzerop (low_bound))
12537 goto do_warn_noncontiguous;
12538 if (length != NULL_TREE
12539 && TREE_CODE (length) == INTEGER_CST
12540 && TYPE_DOMAIN (types[i])
12541 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12542 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12543 == INTEGER_CST)
12545 tree size;
12546 size = size_binop (PLUS_EXPR,
12547 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12548 size_one_node);
12549 if (!tree_int_cst_equal (length, size))
12551 do_warn_noncontiguous:
12552 error_at (OMP_CLAUSE_LOCATION (c),
12553 "array section is not contiguous in %qs "
12554 "clause",
12555 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12556 return true;
12559 if (length != NULL_TREE
12560 && TREE_SIDE_EFFECTS (length))
12562 if (side_effects == NULL_TREE)
12563 side_effects = length;
12564 else
12565 side_effects = build2 (COMPOUND_EXPR,
12566 TREE_TYPE (side_effects),
12567 length, side_effects);
12570 else
12572 tree l;
12574 if (i > first_non_one
12575 && ((length && integer_nonzerop (length))
12576 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12577 continue;
12578 if (length)
12579 l = fold_convert (sizetype, length);
12580 else
12582 l = size_binop (PLUS_EXPR,
12583 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12584 size_one_node);
12585 l = size_binop (MINUS_EXPR, l,
12586 fold_convert (sizetype, low_bound));
12588 if (i > first_non_one)
12590 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12591 size_zero_node);
12592 if (condition == NULL_TREE)
12593 condition = l;
12594 else
12595 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12596 l, condition);
12598 else if (size == NULL_TREE)
12600 size = size_in_bytes (TREE_TYPE (types[i]));
12601 tree eltype = TREE_TYPE (types[num - 1]);
12602 while (TREE_CODE (eltype) == ARRAY_TYPE)
12603 eltype = TREE_TYPE (eltype);
12604 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12606 if (integer_zerop (size)
12607 || integer_zerop (size_in_bytes (eltype)))
12609 error_at (OMP_CLAUSE_LOCATION (c),
12610 "zero length array section in %qs clause",
12611 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12612 return error_mark_node;
12614 size = size_binop (EXACT_DIV_EXPR, size,
12615 size_in_bytes (eltype));
12617 size = size_binop (MULT_EXPR, size, l);
12618 if (condition)
12619 size = fold_build3 (COND_EXPR, sizetype, condition,
12620 size, size_zero_node);
12622 else
12623 size = size_binop (MULT_EXPR, size, l);
12626 if (side_effects)
12627 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12628 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12630 size = size_binop (MINUS_EXPR, size, size_one_node);
12631 size = c_fully_fold (size, false, NULL);
12632 tree index_type = build_index_type (size);
12633 tree eltype = TREE_TYPE (first);
12634 while (TREE_CODE (eltype) == ARRAY_TYPE)
12635 eltype = TREE_TYPE (eltype);
12636 tree type = build_array_type (eltype, index_type);
12637 tree ptype = build_pointer_type (eltype);
12638 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12639 t = build_fold_addr_expr (t);
12640 tree t2 = build_fold_addr_expr (first);
12641 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12642 ptrdiff_type_node, t2);
12643 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12644 ptrdiff_type_node, t2,
12645 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12646 ptrdiff_type_node, t));
12647 t2 = c_fully_fold (t2, false, NULL);
12648 if (tree_fits_shwi_p (t2))
12649 t = build2 (MEM_REF, type, t,
12650 build_int_cst (ptype, tree_to_shwi (t2)));
12651 else
12653 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12654 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12655 TREE_TYPE (t), t, t2);
12656 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12658 OMP_CLAUSE_DECL (c) = t;
12659 return false;
12661 first = c_fully_fold (first, false, NULL);
12662 OMP_CLAUSE_DECL (c) = first;
12663 if (size)
12664 size = c_fully_fold (size, false, NULL);
12665 OMP_CLAUSE_SIZE (c) = size;
12666 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12667 || (TREE_CODE (t) == COMPONENT_REF
12668 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12669 return false;
12670 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12671 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12672 switch (OMP_CLAUSE_MAP_KIND (c))
12674 case GOMP_MAP_ALLOC:
12675 case GOMP_MAP_TO:
12676 case GOMP_MAP_FROM:
12677 case GOMP_MAP_TOFROM:
12678 case GOMP_MAP_ALWAYS_TO:
12679 case GOMP_MAP_ALWAYS_FROM:
12680 case GOMP_MAP_ALWAYS_TOFROM:
12681 case GOMP_MAP_RELEASE:
12682 case GOMP_MAP_DELETE:
12683 case GOMP_MAP_FORCE_TO:
12684 case GOMP_MAP_FORCE_FROM:
12685 case GOMP_MAP_FORCE_TOFROM:
12686 case GOMP_MAP_FORCE_PRESENT:
12687 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12688 break;
12689 default:
12690 break;
12692 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12693 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12694 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12695 else if (TREE_CODE (t) == COMPONENT_REF)
12696 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12697 else
12698 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12699 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12700 && !c_mark_addressable (t))
12701 return false;
12702 OMP_CLAUSE_DECL (c2) = t;
12703 t = build_fold_addr_expr (first);
12704 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12705 tree ptr = OMP_CLAUSE_DECL (c2);
12706 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12707 ptr = build_fold_addr_expr (ptr);
12708 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12709 ptrdiff_type_node, t,
12710 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12711 ptrdiff_type_node, ptr));
12712 t = c_fully_fold (t, false, NULL);
12713 OMP_CLAUSE_SIZE (c2) = t;
12714 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12715 OMP_CLAUSE_CHAIN (c) = c2;
12717 return false;
12720 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12721 an inline call. But, remap
12722 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12723 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12725 static tree
12726 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12727 tree decl, tree placeholder)
12729 copy_body_data id;
12730 hash_map<tree, tree> decl_map;
12732 decl_map.put (omp_decl1, placeholder);
12733 decl_map.put (omp_decl2, decl);
12734 memset (&id, 0, sizeof (id));
12735 id.src_fn = DECL_CONTEXT (omp_decl1);
12736 id.dst_fn = current_function_decl;
12737 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12738 id.decl_map = &decl_map;
12740 id.copy_decl = copy_decl_no_change;
12741 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12742 id.transform_new_cfg = true;
12743 id.transform_return_to_modify = false;
12744 id.transform_lang_insert_block = NULL;
12745 id.eh_lp_nr = 0;
12746 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12747 return stmt;
12750 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12751 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12753 static tree
12754 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12756 if (*tp == (tree) data)
12757 return *tp;
12758 return NULL_TREE;
12761 /* For all elements of CLAUSES, validate them against their constraints.
12762 Remove any elements from the list that are invalid. */
12764 tree
12765 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12767 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12768 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12769 tree c, t, type, *pc;
12770 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12771 bool branch_seen = false;
12772 bool copyprivate_seen = false;
12773 bool linear_variable_step_check = false;
12774 tree *nowait_clause = NULL;
12775 bool ordered_seen = false;
12776 tree schedule_clause = NULL_TREE;
12777 bool oacc_async = false;
12779 bitmap_obstack_initialize (NULL);
12780 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12781 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12782 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12783 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12784 bitmap_initialize (&map_head, &bitmap_default_obstack);
12785 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12786 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12788 if (ort & C_ORT_ACC)
12789 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12790 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12792 oacc_async = true;
12793 break;
12796 for (pc = &clauses, c = clauses; c ; c = *pc)
12798 bool remove = false;
12799 bool need_complete = false;
12800 bool need_implicitly_determined = false;
12802 switch (OMP_CLAUSE_CODE (c))
12804 case OMP_CLAUSE_SHARED:
12805 need_implicitly_determined = true;
12806 goto check_dup_generic;
12808 case OMP_CLAUSE_PRIVATE:
12809 need_complete = true;
12810 need_implicitly_determined = true;
12811 goto check_dup_generic;
12813 case OMP_CLAUSE_REDUCTION:
12814 need_implicitly_determined = true;
12815 t = OMP_CLAUSE_DECL (c);
12816 if (TREE_CODE (t) == TREE_LIST)
12818 if (handle_omp_array_sections (c, ort))
12820 remove = true;
12821 break;
12824 t = OMP_CLAUSE_DECL (c);
12826 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12827 if (t == error_mark_node)
12829 remove = true;
12830 break;
12832 if (oacc_async)
12833 c_mark_addressable (t);
12834 type = TREE_TYPE (t);
12835 if (TREE_CODE (t) == MEM_REF)
12836 type = TREE_TYPE (type);
12837 if (TREE_CODE (type) == ARRAY_TYPE)
12839 tree oatype = type;
12840 gcc_assert (TREE_CODE (t) != MEM_REF);
12841 while (TREE_CODE (type) == ARRAY_TYPE)
12842 type = TREE_TYPE (type);
12843 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12845 error_at (OMP_CLAUSE_LOCATION (c),
12846 "%qD in %<reduction%> clause is a zero size array",
12848 remove = true;
12849 break;
12851 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12852 TYPE_SIZE_UNIT (type));
12853 if (integer_zerop (size))
12855 error_at (OMP_CLAUSE_LOCATION (c),
12856 "%qD in %<reduction%> clause is a zero size array",
12858 remove = true;
12859 break;
12861 size = size_binop (MINUS_EXPR, size, size_one_node);
12862 tree index_type = build_index_type (size);
12863 tree atype = build_array_type (type, index_type);
12864 tree ptype = build_pointer_type (type);
12865 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12866 t = build_fold_addr_expr (t);
12867 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12868 OMP_CLAUSE_DECL (c) = t;
12870 if (TYPE_ATOMIC (type))
12872 error_at (OMP_CLAUSE_LOCATION (c),
12873 "%<_Atomic%> %qE in %<reduction%> clause", t);
12874 remove = true;
12875 break;
12877 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12878 && (FLOAT_TYPE_P (type)
12879 || TREE_CODE (type) == COMPLEX_TYPE))
12881 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12882 const char *r_name = NULL;
12884 switch (r_code)
12886 case PLUS_EXPR:
12887 case MULT_EXPR:
12888 case MINUS_EXPR:
12889 break;
12890 case MIN_EXPR:
12891 if (TREE_CODE (type) == COMPLEX_TYPE)
12892 r_name = "min";
12893 break;
12894 case MAX_EXPR:
12895 if (TREE_CODE (type) == COMPLEX_TYPE)
12896 r_name = "max";
12897 break;
12898 case BIT_AND_EXPR:
12899 r_name = "&";
12900 break;
12901 case BIT_XOR_EXPR:
12902 r_name = "^";
12903 break;
12904 case BIT_IOR_EXPR:
12905 r_name = "|";
12906 break;
12907 case TRUTH_ANDIF_EXPR:
12908 if (FLOAT_TYPE_P (type))
12909 r_name = "&&";
12910 break;
12911 case TRUTH_ORIF_EXPR:
12912 if (FLOAT_TYPE_P (type))
12913 r_name = "||";
12914 break;
12915 default:
12916 gcc_unreachable ();
12918 if (r_name)
12920 error_at (OMP_CLAUSE_LOCATION (c),
12921 "%qE has invalid type for %<reduction(%s)%>",
12922 t, r_name);
12923 remove = true;
12924 break;
12927 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12929 error_at (OMP_CLAUSE_LOCATION (c),
12930 "user defined reduction not found for %qE", t);
12931 remove = true;
12932 break;
12934 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12936 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12937 type = TYPE_MAIN_VARIANT (type);
12938 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12939 VAR_DECL, NULL_TREE, type);
12940 tree decl_placeholder = NULL_TREE;
12941 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12942 DECL_ARTIFICIAL (placeholder) = 1;
12943 DECL_IGNORED_P (placeholder) = 1;
12944 if (TREE_CODE (t) == MEM_REF)
12946 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12947 VAR_DECL, NULL_TREE, type);
12948 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12949 DECL_ARTIFICIAL (decl_placeholder) = 1;
12950 DECL_IGNORED_P (decl_placeholder) = 1;
12952 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12953 c_mark_addressable (placeholder);
12954 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12955 c_mark_addressable (decl_placeholder ? decl_placeholder
12956 : OMP_CLAUSE_DECL (c));
12957 OMP_CLAUSE_REDUCTION_MERGE (c)
12958 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12959 TREE_VEC_ELT (list, 0),
12960 TREE_VEC_ELT (list, 1),
12961 decl_placeholder ? decl_placeholder
12962 : OMP_CLAUSE_DECL (c), placeholder);
12963 OMP_CLAUSE_REDUCTION_MERGE (c)
12964 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12965 void_type_node, NULL_TREE,
12966 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12967 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12968 if (TREE_VEC_LENGTH (list) == 6)
12970 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12971 c_mark_addressable (decl_placeholder ? decl_placeholder
12972 : OMP_CLAUSE_DECL (c));
12973 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12974 c_mark_addressable (placeholder);
12975 tree init = TREE_VEC_ELT (list, 5);
12976 if (init == error_mark_node)
12977 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12978 OMP_CLAUSE_REDUCTION_INIT (c)
12979 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12980 TREE_VEC_ELT (list, 3),
12981 decl_placeholder ? decl_placeholder
12982 : OMP_CLAUSE_DECL (c), placeholder);
12983 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12985 tree v = decl_placeholder ? decl_placeholder : t;
12986 OMP_CLAUSE_REDUCTION_INIT (c)
12987 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12988 OMP_CLAUSE_REDUCTION_INIT (c));
12990 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12991 c_find_omp_placeholder_r,
12992 placeholder, NULL))
12993 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12995 else
12997 tree init;
12998 tree v = decl_placeholder ? decl_placeholder : t;
12999 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13000 init = build_constructor (TREE_TYPE (v), NULL);
13001 else
13002 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13003 OMP_CLAUSE_REDUCTION_INIT (c)
13004 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13006 OMP_CLAUSE_REDUCTION_INIT (c)
13007 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13008 void_type_node, NULL_TREE,
13009 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13010 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13012 if (TREE_CODE (t) == MEM_REF)
13014 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13015 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13016 != INTEGER_CST)
13018 sorry ("variable length element type in array "
13019 "%<reduction%> clause");
13020 remove = true;
13021 break;
13023 t = TREE_OPERAND (t, 0);
13024 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13025 t = TREE_OPERAND (t, 0);
13026 if (TREE_CODE (t) == ADDR_EXPR)
13027 t = TREE_OPERAND (t, 0);
13029 goto check_dup_generic_t;
13031 case OMP_CLAUSE_COPYPRIVATE:
13032 copyprivate_seen = true;
13033 if (nowait_clause)
13035 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13036 "%<nowait%> clause must not be used together "
13037 "with %<copyprivate%>");
13038 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13039 nowait_clause = NULL;
13041 goto check_dup_generic;
13043 case OMP_CLAUSE_COPYIN:
13044 t = OMP_CLAUSE_DECL (c);
13045 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13047 error_at (OMP_CLAUSE_LOCATION (c),
13048 "%qE must be %<threadprivate%> for %<copyin%>", t);
13049 remove = true;
13050 break;
13052 goto check_dup_generic;
13054 case OMP_CLAUSE_LINEAR:
13055 if (ort != C_ORT_OMP_DECLARE_SIMD)
13056 need_implicitly_determined = true;
13057 t = OMP_CLAUSE_DECL (c);
13058 if (ort != C_ORT_OMP_DECLARE_SIMD
13059 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13061 error_at (OMP_CLAUSE_LOCATION (c),
13062 "modifier should not be specified in %<linear%> "
13063 "clause on %<simd%> or %<for%> constructs");
13064 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13066 if (ort & C_ORT_CILK)
13068 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13069 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13070 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13072 error_at (OMP_CLAUSE_LOCATION (c),
13073 "linear clause applied to non-integral, "
13074 "non-floating, non-pointer variable with type %qT",
13075 TREE_TYPE (t));
13076 remove = true;
13077 break;
13080 else
13082 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13083 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13085 error_at (OMP_CLAUSE_LOCATION (c),
13086 "linear clause applied to non-integral non-pointer "
13087 "variable with type %qT", TREE_TYPE (t));
13088 remove = true;
13089 break;
13091 if (TYPE_ATOMIC (TREE_TYPE (t)))
13093 error_at (OMP_CLAUSE_LOCATION (c),
13094 "%<_Atomic%> %qD in %<linear%> clause", t);
13095 remove = true;
13096 break;
13099 if (ort == C_ORT_OMP_DECLARE_SIMD)
13101 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13102 if (TREE_CODE (s) == PARM_DECL)
13104 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13105 /* map_head bitmap is used as uniform_head if
13106 declare_simd. */
13107 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13108 linear_variable_step_check = true;
13109 goto check_dup_generic;
13111 if (TREE_CODE (s) != INTEGER_CST)
13113 error_at (OMP_CLAUSE_LOCATION (c),
13114 "%<linear%> clause step %qE is neither constant "
13115 "nor a parameter", s);
13116 remove = true;
13117 break;
13120 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13122 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13123 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13124 OMP_CLAUSE_DECL (c), s);
13125 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13126 sizetype, fold_convert (sizetype, s),
13127 fold_convert
13128 (sizetype, OMP_CLAUSE_DECL (c)));
13129 if (s == error_mark_node)
13130 s = size_one_node;
13131 OMP_CLAUSE_LINEAR_STEP (c) = s;
13133 else
13134 OMP_CLAUSE_LINEAR_STEP (c)
13135 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13136 goto check_dup_generic;
13138 check_dup_generic:
13139 t = OMP_CLAUSE_DECL (c);
13140 check_dup_generic_t:
13141 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13143 error_at (OMP_CLAUSE_LOCATION (c),
13144 "%qE is not a variable in clause %qs", t,
13145 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13146 remove = true;
13148 else if (ort == C_ORT_ACC
13149 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13151 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13153 error ("%qD appears more than once in reduction clauses", t);
13154 remove = true;
13156 else
13157 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13159 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13160 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13161 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13163 error_at (OMP_CLAUSE_LOCATION (c),
13164 "%qE appears more than once in data clauses", t);
13165 remove = true;
13167 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13168 && bitmap_bit_p (&map_head, DECL_UID (t)))
13170 if (ort == C_ORT_ACC)
13171 error ("%qD appears more than once in data clauses", t);
13172 else
13173 error ("%qD appears both in data and map clauses", t);
13174 remove = true;
13176 else
13177 bitmap_set_bit (&generic_head, DECL_UID (t));
13178 break;
13180 case OMP_CLAUSE_FIRSTPRIVATE:
13181 t = OMP_CLAUSE_DECL (c);
13182 need_complete = true;
13183 need_implicitly_determined = true;
13184 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13186 error_at (OMP_CLAUSE_LOCATION (c),
13187 "%qE is not a variable in clause %<firstprivate%>", t);
13188 remove = true;
13190 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13191 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13193 error_at (OMP_CLAUSE_LOCATION (c),
13194 "%qE appears more than once in data clauses", t);
13195 remove = true;
13197 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13199 if (ort == C_ORT_ACC)
13200 error ("%qD appears more than once in data clauses", t);
13201 else
13202 error ("%qD appears both in data and map clauses", t);
13203 remove = true;
13205 else
13206 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13207 break;
13209 case OMP_CLAUSE_LASTPRIVATE:
13210 t = OMP_CLAUSE_DECL (c);
13211 need_complete = true;
13212 need_implicitly_determined = true;
13213 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13215 error_at (OMP_CLAUSE_LOCATION (c),
13216 "%qE is not a variable in clause %<lastprivate%>", t);
13217 remove = true;
13219 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13220 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13222 error_at (OMP_CLAUSE_LOCATION (c),
13223 "%qE appears more than once in data clauses", t);
13224 remove = true;
13226 else
13227 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13228 break;
13230 case OMP_CLAUSE_ALIGNED:
13231 t = OMP_CLAUSE_DECL (c);
13232 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13234 error_at (OMP_CLAUSE_LOCATION (c),
13235 "%qE is not a variable in %<aligned%> clause", t);
13236 remove = true;
13238 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13239 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13241 error_at (OMP_CLAUSE_LOCATION (c),
13242 "%qE in %<aligned%> clause is neither a pointer nor "
13243 "an array", t);
13244 remove = true;
13246 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13248 error_at (OMP_CLAUSE_LOCATION (c),
13249 "%<_Atomic%> %qD in %<aligned%> clause", t);
13250 remove = true;
13251 break;
13253 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13255 error_at (OMP_CLAUSE_LOCATION (c),
13256 "%qE appears more than once in %<aligned%> clauses",
13258 remove = true;
13260 else
13261 bitmap_set_bit (&aligned_head, DECL_UID (t));
13262 break;
13264 case OMP_CLAUSE_DEPEND:
13265 t = OMP_CLAUSE_DECL (c);
13266 if (t == NULL_TREE)
13268 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13269 == OMP_CLAUSE_DEPEND_SOURCE);
13270 break;
13272 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13274 gcc_assert (TREE_CODE (t) == TREE_LIST);
13275 for (; t; t = TREE_CHAIN (t))
13277 tree decl = TREE_VALUE (t);
13278 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13280 tree offset = TREE_PURPOSE (t);
13281 bool neg = wi::neg_p ((wide_int) offset);
13282 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13283 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13284 neg ? MINUS_EXPR : PLUS_EXPR,
13285 decl, offset);
13286 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13287 sizetype,
13288 fold_convert (sizetype, t2),
13289 fold_convert (sizetype, decl));
13290 if (t2 == error_mark_node)
13292 remove = true;
13293 break;
13295 TREE_PURPOSE (t) = t2;
13298 break;
13300 if (TREE_CODE (t) == TREE_LIST)
13302 if (handle_omp_array_sections (c, ort))
13303 remove = true;
13304 break;
13306 if (t == error_mark_node)
13307 remove = true;
13308 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13310 error_at (OMP_CLAUSE_LOCATION (c),
13311 "%qE is not a variable in %<depend%> clause", t);
13312 remove = true;
13314 else if (!c_mark_addressable (t))
13315 remove = true;
13316 break;
13318 case OMP_CLAUSE_MAP:
13319 case OMP_CLAUSE_TO:
13320 case OMP_CLAUSE_FROM:
13321 case OMP_CLAUSE__CACHE_:
13322 t = OMP_CLAUSE_DECL (c);
13323 if (TREE_CODE (t) == TREE_LIST)
13325 if (handle_omp_array_sections (c, ort))
13326 remove = true;
13327 else
13329 t = OMP_CLAUSE_DECL (c);
13330 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13332 error_at (OMP_CLAUSE_LOCATION (c),
13333 "array section does not have mappable type "
13334 "in %qs clause",
13335 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13336 remove = true;
13338 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13340 error_at (OMP_CLAUSE_LOCATION (c),
13341 "%<_Atomic%> %qE in %qs clause", t,
13342 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13343 remove = true;
13345 while (TREE_CODE (t) == ARRAY_REF)
13346 t = TREE_OPERAND (t, 0);
13347 if (TREE_CODE (t) == COMPONENT_REF
13348 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13350 while (TREE_CODE (t) == COMPONENT_REF)
13351 t = TREE_OPERAND (t, 0);
13352 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13353 break;
13354 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13356 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13357 error ("%qD appears more than once in motion"
13358 " clauses", t);
13359 else if (ort == C_ORT_ACC)
13360 error ("%qD appears more than once in data"
13361 " clauses", t);
13362 else
13363 error ("%qD appears more than once in map"
13364 " clauses", t);
13365 remove = true;
13367 else
13369 bitmap_set_bit (&map_head, DECL_UID (t));
13370 bitmap_set_bit (&map_field_head, DECL_UID (t));
13374 break;
13376 if (t == error_mark_node)
13378 remove = true;
13379 break;
13381 if (TREE_CODE (t) == COMPONENT_REF
13382 && (ort & C_ORT_OMP)
13383 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13385 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13387 error_at (OMP_CLAUSE_LOCATION (c),
13388 "bit-field %qE in %qs clause",
13389 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13390 remove = true;
13392 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13394 error_at (OMP_CLAUSE_LOCATION (c),
13395 "%qE does not have a mappable type in %qs clause",
13396 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13397 remove = true;
13399 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13401 error_at (OMP_CLAUSE_LOCATION (c),
13402 "%<_Atomic%> %qE in %qs clause", t,
13403 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13404 remove = true;
13406 while (TREE_CODE (t) == COMPONENT_REF)
13408 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13409 == UNION_TYPE)
13411 error_at (OMP_CLAUSE_LOCATION (c),
13412 "%qE is a member of a union", t);
13413 remove = true;
13414 break;
13416 t = TREE_OPERAND (t, 0);
13418 if (remove)
13419 break;
13420 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13422 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13423 break;
13426 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13428 error_at (OMP_CLAUSE_LOCATION (c),
13429 "%qE is not a variable in %qs clause", t,
13430 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13431 remove = true;
13433 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13435 error_at (OMP_CLAUSE_LOCATION (c),
13436 "%qD is threadprivate variable in %qs clause", t,
13437 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13438 remove = true;
13440 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13441 || (OMP_CLAUSE_MAP_KIND (c)
13442 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13443 && !c_mark_addressable (t))
13444 remove = true;
13445 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13446 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13447 || (OMP_CLAUSE_MAP_KIND (c)
13448 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13449 || (OMP_CLAUSE_MAP_KIND (c)
13450 == GOMP_MAP_FORCE_DEVICEPTR)))
13451 && t == OMP_CLAUSE_DECL (c)
13452 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13454 error_at (OMP_CLAUSE_LOCATION (c),
13455 "%qD does not have a mappable type in %qs clause", t,
13456 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13457 remove = true;
13459 else if (TREE_TYPE (t) == error_mark_node)
13460 remove = true;
13461 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13463 error_at (OMP_CLAUSE_LOCATION (c),
13464 "%<_Atomic%> %qE in %qs clause", t,
13465 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13466 remove = true;
13468 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13469 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13471 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13472 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13474 error ("%qD appears more than once in data clauses", t);
13475 remove = true;
13477 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13479 if (ort == C_ORT_ACC)
13480 error ("%qD appears more than once in data clauses", t);
13481 else
13482 error ("%qD appears both in data and map clauses", t);
13483 remove = true;
13485 else
13486 bitmap_set_bit (&generic_head, DECL_UID (t));
13488 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13490 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13491 error ("%qD appears more than once in motion clauses", t);
13492 else if (ort == C_ORT_ACC)
13493 error ("%qD appears more than once in data clauses", t);
13494 else
13495 error ("%qD appears more than once in map clauses", t);
13496 remove = true;
13498 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13499 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13501 if (ort == C_ORT_ACC)
13502 error ("%qD appears more than once in data clauses", t);
13503 else
13504 error ("%qD appears both in data and map clauses", t);
13505 remove = true;
13507 else
13509 bitmap_set_bit (&map_head, DECL_UID (t));
13510 if (t != OMP_CLAUSE_DECL (c)
13511 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13512 bitmap_set_bit (&map_field_head, DECL_UID (t));
13514 break;
13516 case OMP_CLAUSE_TO_DECLARE:
13517 case OMP_CLAUSE_LINK:
13518 t = OMP_CLAUSE_DECL (c);
13519 if (TREE_CODE (t) == FUNCTION_DECL
13520 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13522 else if (!VAR_P (t))
13524 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13525 error_at (OMP_CLAUSE_LOCATION (c),
13526 "%qE is neither a variable nor a function name in "
13527 "clause %qs", t,
13528 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13529 else
13530 error_at (OMP_CLAUSE_LOCATION (c),
13531 "%qE is not a variable in clause %qs", t,
13532 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13533 remove = true;
13535 else if (DECL_THREAD_LOCAL_P (t))
13537 error_at (OMP_CLAUSE_LOCATION (c),
13538 "%qD is threadprivate variable in %qs clause", t,
13539 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13540 remove = true;
13542 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13544 error_at (OMP_CLAUSE_LOCATION (c),
13545 "%qD does not have a mappable type in %qs clause", t,
13546 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13547 remove = true;
13549 if (remove)
13550 break;
13551 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13553 error_at (OMP_CLAUSE_LOCATION (c),
13554 "%qE appears more than once on the same "
13555 "%<declare target%> directive", t);
13556 remove = true;
13558 else
13559 bitmap_set_bit (&generic_head, DECL_UID (t));
13560 break;
13562 case OMP_CLAUSE_UNIFORM:
13563 t = OMP_CLAUSE_DECL (c);
13564 if (TREE_CODE (t) != PARM_DECL)
13566 if (DECL_P (t))
13567 error_at (OMP_CLAUSE_LOCATION (c),
13568 "%qD is not an argument in %<uniform%> clause", t);
13569 else
13570 error_at (OMP_CLAUSE_LOCATION (c),
13571 "%qE is not an argument in %<uniform%> clause", t);
13572 remove = true;
13573 break;
13575 /* map_head bitmap is used as uniform_head if declare_simd. */
13576 bitmap_set_bit (&map_head, DECL_UID (t));
13577 goto check_dup_generic;
13579 case OMP_CLAUSE_IS_DEVICE_PTR:
13580 case OMP_CLAUSE_USE_DEVICE_PTR:
13581 t = OMP_CLAUSE_DECL (c);
13582 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13583 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13585 error_at (OMP_CLAUSE_LOCATION (c),
13586 "%qs variable is neither a pointer nor an array",
13587 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13588 remove = true;
13590 goto check_dup_generic;
13592 case OMP_CLAUSE_NOWAIT:
13593 if (copyprivate_seen)
13595 error_at (OMP_CLAUSE_LOCATION (c),
13596 "%<nowait%> clause must not be used together "
13597 "with %<copyprivate%>");
13598 remove = true;
13599 break;
13601 nowait_clause = pc;
13602 pc = &OMP_CLAUSE_CHAIN (c);
13603 continue;
13605 case OMP_CLAUSE_IF:
13606 case OMP_CLAUSE_NUM_THREADS:
13607 case OMP_CLAUSE_NUM_TEAMS:
13608 case OMP_CLAUSE_THREAD_LIMIT:
13609 case OMP_CLAUSE_DEFAULT:
13610 case OMP_CLAUSE_UNTIED:
13611 case OMP_CLAUSE_COLLAPSE:
13612 case OMP_CLAUSE_FINAL:
13613 case OMP_CLAUSE_MERGEABLE:
13614 case OMP_CLAUSE_DEVICE:
13615 case OMP_CLAUSE_DIST_SCHEDULE:
13616 case OMP_CLAUSE_PARALLEL:
13617 case OMP_CLAUSE_FOR:
13618 case OMP_CLAUSE_SECTIONS:
13619 case OMP_CLAUSE_TASKGROUP:
13620 case OMP_CLAUSE_PROC_BIND:
13621 case OMP_CLAUSE_PRIORITY:
13622 case OMP_CLAUSE_GRAINSIZE:
13623 case OMP_CLAUSE_NUM_TASKS:
13624 case OMP_CLAUSE_NOGROUP:
13625 case OMP_CLAUSE_THREADS:
13626 case OMP_CLAUSE_SIMD:
13627 case OMP_CLAUSE_HINT:
13628 case OMP_CLAUSE_DEFAULTMAP:
13629 case OMP_CLAUSE__CILK_FOR_COUNT_:
13630 case OMP_CLAUSE_NUM_GANGS:
13631 case OMP_CLAUSE_NUM_WORKERS:
13632 case OMP_CLAUSE_VECTOR_LENGTH:
13633 case OMP_CLAUSE_ASYNC:
13634 case OMP_CLAUSE_WAIT:
13635 case OMP_CLAUSE_AUTO:
13636 case OMP_CLAUSE_INDEPENDENT:
13637 case OMP_CLAUSE_SEQ:
13638 case OMP_CLAUSE_GANG:
13639 case OMP_CLAUSE_WORKER:
13640 case OMP_CLAUSE_VECTOR:
13641 case OMP_CLAUSE_TILE:
13642 pc = &OMP_CLAUSE_CHAIN (c);
13643 continue;
13645 case OMP_CLAUSE_SCHEDULE:
13646 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13648 const char *p = NULL;
13649 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13651 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13652 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13653 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13654 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13655 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13656 default: gcc_unreachable ();
13658 if (p)
13660 error_at (OMP_CLAUSE_LOCATION (c),
13661 "%<nonmonotonic%> modifier specified for %qs "
13662 "schedule kind", p);
13663 OMP_CLAUSE_SCHEDULE_KIND (c)
13664 = (enum omp_clause_schedule_kind)
13665 (OMP_CLAUSE_SCHEDULE_KIND (c)
13666 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13669 schedule_clause = c;
13670 pc = &OMP_CLAUSE_CHAIN (c);
13671 continue;
13673 case OMP_CLAUSE_ORDERED:
13674 ordered_seen = true;
13675 pc = &OMP_CLAUSE_CHAIN (c);
13676 continue;
13678 case OMP_CLAUSE_SAFELEN:
13679 safelen = c;
13680 pc = &OMP_CLAUSE_CHAIN (c);
13681 continue;
13682 case OMP_CLAUSE_SIMDLEN:
13683 simdlen = c;
13684 pc = &OMP_CLAUSE_CHAIN (c);
13685 continue;
13687 case OMP_CLAUSE_INBRANCH:
13688 case OMP_CLAUSE_NOTINBRANCH:
13689 if (branch_seen)
13691 error_at (OMP_CLAUSE_LOCATION (c),
13692 "%<inbranch%> clause is incompatible with "
13693 "%<notinbranch%>");
13694 remove = true;
13695 break;
13697 branch_seen = true;
13698 pc = &OMP_CLAUSE_CHAIN (c);
13699 continue;
13701 default:
13702 gcc_unreachable ();
13705 if (!remove)
13707 t = OMP_CLAUSE_DECL (c);
13709 if (need_complete)
13711 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13712 if (t == error_mark_node)
13713 remove = true;
13716 if (need_implicitly_determined)
13718 const char *share_name = NULL;
13720 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13721 share_name = "threadprivate";
13722 else switch (c_omp_predetermined_sharing (t))
13724 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13725 break;
13726 case OMP_CLAUSE_DEFAULT_SHARED:
13727 /* const vars may be specified in firstprivate clause. */
13728 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13729 && TREE_READONLY (t))
13730 break;
13731 share_name = "shared";
13732 break;
13733 case OMP_CLAUSE_DEFAULT_PRIVATE:
13734 share_name = "private";
13735 break;
13736 default:
13737 gcc_unreachable ();
13739 if (share_name)
13741 error_at (OMP_CLAUSE_LOCATION (c),
13742 "%qE is predetermined %qs for %qs",
13743 t, share_name,
13744 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13745 remove = true;
13750 if (remove)
13751 *pc = OMP_CLAUSE_CHAIN (c);
13752 else
13753 pc = &OMP_CLAUSE_CHAIN (c);
13756 if (simdlen
13757 && safelen
13758 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13759 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13761 error_at (OMP_CLAUSE_LOCATION (simdlen),
13762 "%<simdlen%> clause value is bigger than "
13763 "%<safelen%> clause value");
13764 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13765 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13768 if (ordered_seen
13769 && schedule_clause
13770 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13771 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13773 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13774 "%<nonmonotonic%> schedule modifier specified together "
13775 "with %<ordered%> clause");
13776 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13777 = (enum omp_clause_schedule_kind)
13778 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13779 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13782 if (linear_variable_step_check)
13783 for (pc = &clauses, c = clauses; c ; c = *pc)
13785 bool remove = false;
13786 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13787 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13788 && !bitmap_bit_p (&map_head,
13789 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13791 error_at (OMP_CLAUSE_LOCATION (c),
13792 "%<linear%> clause step is a parameter %qD not "
13793 "specified in %<uniform%> clause",
13794 OMP_CLAUSE_LINEAR_STEP (c));
13795 remove = true;
13798 if (remove)
13799 *pc = OMP_CLAUSE_CHAIN (c);
13800 else
13801 pc = &OMP_CLAUSE_CHAIN (c);
13804 bitmap_obstack_release (NULL);
13805 return clauses;
13808 /* Return code to initialize DST with a copy constructor from SRC.
13809 C doesn't have copy constructors nor assignment operators, only for
13810 _Atomic vars we need to perform __atomic_load from src into a temporary
13811 followed by __atomic_store of the temporary to dst. */
13813 tree
13814 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13816 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13817 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13819 location_t loc = OMP_CLAUSE_LOCATION (clause);
13820 tree type = TREE_TYPE (dst);
13821 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13822 tree tmp = create_tmp_var (nonatomic_type);
13823 tree tmp_addr = build_fold_addr_expr (tmp);
13824 TREE_ADDRESSABLE (tmp) = 1;
13825 TREE_NO_WARNING (tmp) = 1;
13826 tree src_addr = build_fold_addr_expr (src);
13827 tree dst_addr = build_fold_addr_expr (dst);
13828 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13829 vec<tree, va_gc> *params;
13830 /* Expansion of a generic atomic load may require an addition
13831 element, so allocate enough to prevent a resize. */
13832 vec_alloc (params, 4);
13834 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13835 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13836 params->quick_push (src_addr);
13837 params->quick_push (tmp_addr);
13838 params->quick_push (seq_cst);
13839 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13841 vec_alloc (params, 4);
13843 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13844 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13845 params->quick_push (dst_addr);
13846 params->quick_push (tmp_addr);
13847 params->quick_push (seq_cst);
13848 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13849 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13852 /* Create a transaction node. */
13854 tree
13855 c_finish_transaction (location_t loc, tree block, int flags)
13857 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13858 if (flags & TM_STMT_ATTR_OUTER)
13859 TRANSACTION_EXPR_OUTER (stmt) = 1;
13860 if (flags & TM_STMT_ATTR_RELAXED)
13861 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13862 return add_stmt (stmt);
13865 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13866 down to the element type of an array. If ORIG_QUAL_TYPE is not
13867 NULL, then it should be used as the qualified type
13868 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13869 preserve information about the typedef name from which an array
13870 type was derived). */
13872 tree
13873 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13874 size_t orig_qual_indirect)
13876 if (type == error_mark_node)
13877 return type;
13879 if (TREE_CODE (type) == ARRAY_TYPE)
13881 tree t;
13882 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13883 type_quals, orig_qual_type,
13884 orig_qual_indirect - 1);
13886 /* See if we already have an identically qualified type. */
13887 if (orig_qual_type && orig_qual_indirect == 0)
13888 t = orig_qual_type;
13889 else
13890 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13892 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13893 && TYPE_NAME (t) == TYPE_NAME (type)
13894 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13895 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13896 TYPE_ATTRIBUTES (type)))
13897 break;
13899 if (!t)
13901 tree domain = TYPE_DOMAIN (type);
13903 t = build_variant_type_copy (type);
13904 TREE_TYPE (t) = element_type;
13906 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13907 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13908 SET_TYPE_STRUCTURAL_EQUALITY (t);
13909 else if (TYPE_CANONICAL (element_type) != element_type
13910 || (domain && TYPE_CANONICAL (domain) != domain))
13912 tree unqualified_canon
13913 = build_array_type (TYPE_CANONICAL (element_type),
13914 domain? TYPE_CANONICAL (domain)
13915 : NULL_TREE);
13916 if (TYPE_REVERSE_STORAGE_ORDER (type))
13918 unqualified_canon
13919 = build_distinct_type_copy (unqualified_canon);
13920 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13922 TYPE_CANONICAL (t)
13923 = c_build_qualified_type (unqualified_canon, type_quals);
13925 else
13926 TYPE_CANONICAL (t) = t;
13928 return t;
13931 /* A restrict-qualified pointer type must be a pointer to object or
13932 incomplete type. Note that the use of POINTER_TYPE_P also allows
13933 REFERENCE_TYPEs, which is appropriate for C++. */
13934 if ((type_quals & TYPE_QUAL_RESTRICT)
13935 && (!POINTER_TYPE_P (type)
13936 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13938 error ("invalid use of %<restrict%>");
13939 type_quals &= ~TYPE_QUAL_RESTRICT;
13942 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13943 ? orig_qual_type
13944 : build_qualified_type (type, type_quals));
13945 /* A variant type does not inherit the list of incomplete vars from the
13946 type main variant. */
13947 if (RECORD_OR_UNION_TYPE_P (var_type)
13948 && TYPE_MAIN_VARIANT (var_type) != var_type)
13949 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13950 return var_type;
13953 /* Build a VA_ARG_EXPR for the C parser. */
13955 tree
13956 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
13958 if (error_operand_p (type))
13959 return error_mark_node;
13960 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13961 order because it takes the address of the expression. */
13962 else if (handled_component_p (expr)
13963 && reverse_storage_order_for_component_p (expr))
13965 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13966 return error_mark_node;
13968 else if (!COMPLETE_TYPE_P (type))
13970 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
13971 "type %qT", type);
13972 return error_mark_node;
13974 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13975 warning_at (loc2, OPT_Wc___compat,
13976 "C++ requires promoted type, not enum type, in %<va_arg%>");
13977 return build_va_arg (loc2, expr, type);
13980 /* Return truthvalue of whether T1 is the same tree structure as T2.
13981 Return 1 if they are the same. Return 0 if they are different. */
13983 bool
13984 c_tree_equal (tree t1, tree t2)
13986 enum tree_code code1, code2;
13988 if (t1 == t2)
13989 return true;
13990 if (!t1 || !t2)
13991 return false;
13993 for (code1 = TREE_CODE (t1);
13994 CONVERT_EXPR_CODE_P (code1)
13995 || code1 == NON_LVALUE_EXPR;
13996 code1 = TREE_CODE (t1))
13997 t1 = TREE_OPERAND (t1, 0);
13998 for (code2 = TREE_CODE (t2);
13999 CONVERT_EXPR_CODE_P (code2)
14000 || code2 == NON_LVALUE_EXPR;
14001 code2 = TREE_CODE (t2))
14002 t2 = TREE_OPERAND (t2, 0);
14004 /* They might have become equal now. */
14005 if (t1 == t2)
14006 return true;
14008 if (code1 != code2)
14009 return false;
14011 switch (code1)
14013 case INTEGER_CST:
14014 return wi::eq_p (t1, t2);
14016 case REAL_CST:
14017 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14019 case STRING_CST:
14020 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14021 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14022 TREE_STRING_LENGTH (t1));
14024 case FIXED_CST:
14025 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14026 TREE_FIXED_CST (t2));
14028 case COMPLEX_CST:
14029 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14030 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14032 case VECTOR_CST:
14033 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14035 case CONSTRUCTOR:
14036 /* We need to do this when determining whether or not two
14037 non-type pointer to member function template arguments
14038 are the same. */
14039 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14040 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14041 return false;
14043 tree field, value;
14044 unsigned int i;
14045 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14047 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14048 if (!c_tree_equal (field, elt2->index)
14049 || !c_tree_equal (value, elt2->value))
14050 return false;
14053 return true;
14055 case TREE_LIST:
14056 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14057 return false;
14058 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14059 return false;
14060 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14062 case SAVE_EXPR:
14063 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14065 case CALL_EXPR:
14067 tree arg1, arg2;
14068 call_expr_arg_iterator iter1, iter2;
14069 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14070 return false;
14071 for (arg1 = first_call_expr_arg (t1, &iter1),
14072 arg2 = first_call_expr_arg (t2, &iter2);
14073 arg1 && arg2;
14074 arg1 = next_call_expr_arg (&iter1),
14075 arg2 = next_call_expr_arg (&iter2))
14076 if (!c_tree_equal (arg1, arg2))
14077 return false;
14078 if (arg1 || arg2)
14079 return false;
14080 return true;
14083 case TARGET_EXPR:
14085 tree o1 = TREE_OPERAND (t1, 0);
14086 tree o2 = TREE_OPERAND (t2, 0);
14088 /* Special case: if either target is an unallocated VAR_DECL,
14089 it means that it's going to be unified with whatever the
14090 TARGET_EXPR is really supposed to initialize, so treat it
14091 as being equivalent to anything. */
14092 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14093 && !DECL_RTL_SET_P (o1))
14094 /*Nop*/;
14095 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14096 && !DECL_RTL_SET_P (o2))
14097 /*Nop*/;
14098 else if (!c_tree_equal (o1, o2))
14099 return false;
14101 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14104 case COMPONENT_REF:
14105 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14106 return false;
14107 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14109 case PARM_DECL:
14110 case VAR_DECL:
14111 case CONST_DECL:
14112 case FIELD_DECL:
14113 case FUNCTION_DECL:
14114 case IDENTIFIER_NODE:
14115 case SSA_NAME:
14116 return false;
14118 case TREE_VEC:
14120 unsigned ix;
14121 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14122 return false;
14123 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14124 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14125 TREE_VEC_ELT (t2, ix)))
14126 return false;
14127 return true;
14130 default:
14131 break;
14134 switch (TREE_CODE_CLASS (code1))
14136 case tcc_unary:
14137 case tcc_binary:
14138 case tcc_comparison:
14139 case tcc_expression:
14140 case tcc_vl_exp:
14141 case tcc_reference:
14142 case tcc_statement:
14144 int i, n = TREE_OPERAND_LENGTH (t1);
14146 switch (code1)
14148 case PREINCREMENT_EXPR:
14149 case PREDECREMENT_EXPR:
14150 case POSTINCREMENT_EXPR:
14151 case POSTDECREMENT_EXPR:
14152 n = 1;
14153 break;
14154 case ARRAY_REF:
14155 n = 2;
14156 break;
14157 default:
14158 break;
14161 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14162 && n != TREE_OPERAND_LENGTH (t2))
14163 return false;
14165 for (i = 0; i < n; ++i)
14166 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14167 return false;
14169 return true;
14172 case tcc_type:
14173 return comptypes (t1, t2);
14174 default:
14175 gcc_unreachable ();
14177 /* We can get here with --disable-checking. */
14178 return false;
14181 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14182 spawn-helper and BODY is the newly created body for FNDECL. */
14184 void
14185 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14187 tree list = alloc_stmt_list ();
14188 tree frame = make_cilk_frame (fndecl);
14189 tree dtor = create_cilk_function_exit (frame, false, true);
14190 add_local_decl (cfun, frame);
14192 DECL_SAVED_TREE (fndecl) = list;
14193 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
14194 frame);
14195 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
14196 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
14198 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
14199 append_to_statement_list (detach_expr, &body_list);
14201 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14202 body = fold_build_cleanup_point_expr (void_type_node, body);
14204 append_to_statement_list (body, &body_list);
14205 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14206 body_list, dtor), &list);
14209 /* Returns true when the function declaration FNDECL is implicit,
14210 introduced as a result of a call to an otherwise undeclared
14211 function, and false otherwise. */
14213 bool
14214 c_decl_implicit (const_tree fndecl)
14216 return C_DECL_IMPLICIT (fndecl);