PR c/79662
[official-gcc.git] / gcc / c / c-typeck.c
blob8c2c561eb72e48d65d9fec653e32cd26770d55bd
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
3441 && INTEGRAL_TYPE_P (type)
3442 && INTEGRAL_TYPE_P (valtype))
3444 tree would_have_been = default_conversion (val);
3445 tree type1 = TREE_TYPE (would_have_been);
3447 if (val == error_mark_node)
3448 /* VAL could have been of incomplete type. */;
3449 else if (TREE_CODE (type) == ENUMERAL_TYPE
3450 && (TYPE_MAIN_VARIANT (type)
3451 == TYPE_MAIN_VARIANT (valtype)))
3452 /* No warning if function asks for enum
3453 and the actual arg is that enum type. */
3455 else if (formal_prec != TYPE_PRECISION (type1))
3456 warning_at (ploc, OPT_Wtraditional_conversion,
3457 "passing argument %d of %qE "
3458 "with different width due to prototype",
3459 argnum, rname);
3460 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3462 /* Don't complain if the formal parameter type
3463 is an enum, because we can't tell now whether
3464 the value was an enum--even the same enum. */
3465 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3467 else if (TREE_CODE (val) == INTEGER_CST
3468 && int_fits_type_p (val, type))
3469 /* Change in signedness doesn't matter
3470 if a constant value is unaffected. */
3472 /* If the value is extended from a narrower
3473 unsigned type, it doesn't matter whether we
3474 pass it as signed or unsigned; the value
3475 certainly is the same either way. */
3476 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3477 && TYPE_UNSIGNED (valtype))
3479 else if (TYPE_UNSIGNED (type))
3480 warning_at (ploc, OPT_Wtraditional_conversion,
3481 "passing argument %d of %qE "
3482 "as unsigned due to prototype",
3483 argnum, rname);
3484 else
3485 warning_at (ploc, OPT_Wtraditional_conversion,
3486 "passing argument %d of %qE "
3487 "as signed due to prototype",
3488 argnum, rname);
3492 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3493 sake of better warnings from convert_and_check. */
3494 if (excess_precision)
3495 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3496 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3497 parmval = convert_for_assignment (loc, ploc, type,
3498 val, origtype, ic_argpass,
3499 npc, fundecl, function,
3500 parmnum + 1);
3502 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3503 && INTEGRAL_TYPE_P (type)
3504 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3505 parmval = default_conversion (parmval);
3508 else if (promote_float_arg)
3510 if (type_generic)
3511 parmval = val;
3512 else
3514 /* Convert `float' to `double'. */
3515 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3516 warning_at (ploc, OPT_Wdouble_promotion,
3517 "implicit conversion from %qT to %qT when passing "
3518 "argument to function",
3519 valtype, double_type_node);
3520 parmval = convert (double_type_node, val);
3523 else if ((excess_precision && !type_generic)
3524 || (type_generic_overflow_p && parmnum == 2))
3525 /* A "double" argument with excess precision being passed
3526 without a prototype or in variable arguments.
3527 The last argument of __builtin_*_overflow_p should not be
3528 promoted. */
3529 parmval = convert (valtype, val);
3530 else if ((invalid_func_diag =
3531 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3533 error (invalid_func_diag);
3534 return -1;
3536 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3538 return -1;
3540 else
3541 /* Convert `short' and `char' to full-size `int'. */
3542 parmval = default_conversion (val);
3544 (*values)[parmnum] = parmval;
3545 if (parmval == error_mark_node)
3546 error_args = true;
3548 if (typetail)
3549 typetail = TREE_CHAIN (typetail);
3552 gcc_assert (parmnum == vec_safe_length (values));
3554 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3556 error_at (loc, "too few arguments to function %qE", function);
3557 inform_declaration (fundecl);
3558 return -1;
3561 return error_args ? -1 : (int) parmnum;
3564 /* This is the entry point used by the parser to build unary operators
3565 in the input. CODE, a tree_code, specifies the unary operator, and
3566 ARG is the operand. For unary plus, the C parser currently uses
3567 CONVERT_EXPR for code.
3569 LOC is the location to use for the tree generated.
3572 struct c_expr
3573 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3575 struct c_expr result;
3577 result.original_code = code;
3578 result.original_type = NULL;
3580 if (reject_gcc_builtin (arg.value))
3582 result.value = error_mark_node;
3584 else
3586 result.value = build_unary_op (loc, code, arg.value, false);
3588 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3589 overflow_warning (loc, result.value);
3592 /* We are typically called when parsing a prefix token at LOC acting on
3593 ARG. Reflect this by updating the source range of the result to
3594 start at LOC and end at the end of ARG. */
3595 set_c_expr_source_range (&result,
3596 loc, arg.get_finish ());
3598 return result;
3601 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3603 static bool
3604 char_type_p (tree type)
3606 return (type == char_type_node
3607 || type == unsigned_char_type_node
3608 || type == signed_char_type_node
3609 || type == char16_type_node
3610 || type == char32_type_node);
3613 /* This is the entry point used by the parser to build binary operators
3614 in the input. CODE, a tree_code, specifies the binary operator, and
3615 ARG1 and ARG2 are the operands. In addition to constructing the
3616 expression, we check for operands that were written with other binary
3617 operators in a way that is likely to confuse the user.
3619 LOCATION is the location of the binary operator. */
3621 struct c_expr
3622 parser_build_binary_op (location_t location, enum tree_code code,
3623 struct c_expr arg1, struct c_expr arg2)
3625 struct c_expr result;
3627 enum tree_code code1 = arg1.original_code;
3628 enum tree_code code2 = arg2.original_code;
3629 tree type1 = (arg1.original_type
3630 ? arg1.original_type
3631 : TREE_TYPE (arg1.value));
3632 tree type2 = (arg2.original_type
3633 ? arg2.original_type
3634 : TREE_TYPE (arg2.value));
3636 result.value = build_binary_op (location, code,
3637 arg1.value, arg2.value, 1);
3638 result.original_code = code;
3639 result.original_type = NULL;
3641 if (TREE_CODE (result.value) == ERROR_MARK)
3643 set_c_expr_source_range (&result,
3644 arg1.get_start (),
3645 arg2.get_finish ());
3646 return result;
3649 if (location != UNKNOWN_LOCATION)
3650 protected_set_expr_location (result.value, location);
3652 set_c_expr_source_range (&result,
3653 arg1.get_start (),
3654 arg2.get_finish ());
3656 /* Check for cases such as x+y<<z which users are likely
3657 to misinterpret. */
3658 if (warn_parentheses)
3659 warn_about_parentheses (location, code, code1, arg1.value, code2,
3660 arg2.value);
3662 if (warn_logical_op)
3663 warn_logical_operator (location, code, TREE_TYPE (result.value),
3664 code1, arg1.value, code2, arg2.value);
3666 if (warn_tautological_compare)
3668 tree lhs = arg1.value;
3669 tree rhs = arg2.value;
3670 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3672 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3673 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3674 lhs = NULL_TREE;
3675 else
3676 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3678 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3680 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3681 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3682 rhs = NULL_TREE;
3683 else
3684 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3686 if (lhs != NULL_TREE && rhs != NULL_TREE)
3687 warn_tautological_cmp (location, code, lhs, rhs);
3690 if (warn_logical_not_paren
3691 && TREE_CODE_CLASS (code) == tcc_comparison
3692 && code1 == TRUTH_NOT_EXPR
3693 && code2 != TRUTH_NOT_EXPR
3694 /* Avoid warning for !!x == y. */
3695 && (TREE_CODE (arg1.value) != NE_EXPR
3696 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3698 /* Avoid warning for !b == y where b has _Bool type. */
3699 tree t = integer_zero_node;
3700 if (TREE_CODE (arg1.value) == EQ_EXPR
3701 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3702 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3704 t = TREE_OPERAND (arg1.value, 0);
3707 if (TREE_TYPE (t) != integer_type_node)
3708 break;
3709 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3710 t = C_MAYBE_CONST_EXPR_EXPR (t);
3711 else if (CONVERT_EXPR_P (t))
3712 t = TREE_OPERAND (t, 0);
3713 else
3714 break;
3716 while (1);
3718 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3719 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3722 /* Warn about comparisons against string literals, with the exception
3723 of testing for equality or inequality of a string literal with NULL. */
3724 if (code == EQ_EXPR || code == NE_EXPR)
3726 if ((code1 == STRING_CST
3727 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3728 || (code2 == STRING_CST
3729 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3730 warning_at (location, OPT_Waddress,
3731 "comparison with string literal results in unspecified behavior");
3732 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3733 if (POINTER_TYPE_P (type1)
3734 && null_pointer_constant_p (arg2.value)
3735 && char_type_p (type2)
3736 && warning_at (location, OPT_Wpointer_compare,
3737 "comparison between pointer and zero character "
3738 "constant"))
3739 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3740 else if (POINTER_TYPE_P (type2)
3741 && null_pointer_constant_p (arg1.value)
3742 && char_type_p (type1)
3743 && warning_at (location, OPT_Wpointer_compare,
3744 "comparison between pointer and zero character "
3745 "constant"))
3746 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3748 else if (TREE_CODE_CLASS (code) == tcc_comparison
3749 && (code1 == STRING_CST || code2 == STRING_CST))
3750 warning_at (location, OPT_Waddress,
3751 "comparison with string literal results in unspecified behavior");
3753 if (TREE_OVERFLOW_P (result.value)
3754 && !TREE_OVERFLOW_P (arg1.value)
3755 && !TREE_OVERFLOW_P (arg2.value))
3756 overflow_warning (location, result.value);
3758 /* Warn about comparisons of different enum types. */
3759 if (warn_enum_compare
3760 && TREE_CODE_CLASS (code) == tcc_comparison
3761 && TREE_CODE (type1) == ENUMERAL_TYPE
3762 && TREE_CODE (type2) == ENUMERAL_TYPE
3763 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3764 warning_at (location, OPT_Wenum_compare,
3765 "comparison between %qT and %qT",
3766 type1, type2);
3768 return result;
3771 /* Return a tree for the difference of pointers OP0 and OP1.
3772 The resulting tree has type int. */
3774 static tree
3775 pointer_diff (location_t loc, tree op0, tree op1)
3777 tree restype = ptrdiff_type_node;
3778 tree result, inttype;
3780 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3781 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3782 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3783 tree orig_op1 = op1;
3785 /* If the operands point into different address spaces, we need to
3786 explicitly convert them to pointers into the common address space
3787 before we can subtract the numerical address values. */
3788 if (as0 != as1)
3790 addr_space_t as_common;
3791 tree common_type;
3793 /* Determine the common superset address space. This is guaranteed
3794 to exist because the caller verified that comp_target_types
3795 returned non-zero. */
3796 if (!addr_space_superset (as0, as1, &as_common))
3797 gcc_unreachable ();
3799 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3800 op0 = convert (common_type, op0);
3801 op1 = convert (common_type, op1);
3804 /* Determine integer type to perform computations in. This will usually
3805 be the same as the result type (ptrdiff_t), but may need to be a wider
3806 type if pointers for the address space are wider than ptrdiff_t. */
3807 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3808 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3809 else
3810 inttype = restype;
3812 if (TREE_CODE (target_type) == VOID_TYPE)
3813 pedwarn (loc, OPT_Wpointer_arith,
3814 "pointer of type %<void *%> used in subtraction");
3815 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3816 pedwarn (loc, OPT_Wpointer_arith,
3817 "pointer to a function used in subtraction");
3819 /* First do the subtraction as integers;
3820 then drop through to build the divide operator.
3821 Do not do default conversions on the minus operator
3822 in case restype is a short type. */
3824 op0 = build_binary_op (loc,
3825 MINUS_EXPR, convert (inttype, op0),
3826 convert (inttype, op1), 0);
3827 /* This generates an error if op1 is pointer to incomplete type. */
3828 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3829 error_at (loc, "arithmetic on pointer to an incomplete type");
3831 op1 = c_size_in_bytes (target_type);
3833 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3834 error_at (loc, "arithmetic on pointer to an empty aggregate");
3836 /* Divide by the size, in easiest possible way. */
3837 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3838 op0, convert (inttype, op1));
3840 /* Convert to final result type if necessary. */
3841 return convert (restype, result);
3844 /* Expand atomic compound assignments into an appropriate sequence as
3845 specified by the C11 standard section 6.5.16.2.
3847 _Atomic T1 E1
3848 T2 E2
3849 E1 op= E2
3851 This sequence is used for all types for which these operations are
3852 supported.
3854 In addition, built-in versions of the 'fe' prefixed routines may
3855 need to be invoked for floating point (real, complex or vector) when
3856 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3858 T1 newval;
3859 T1 old;
3860 T1 *addr
3861 T2 val
3862 fenv_t fenv
3864 addr = &E1;
3865 val = (E2);
3866 __atomic_load (addr, &old, SEQ_CST);
3867 feholdexcept (&fenv);
3868 loop:
3869 newval = old op val;
3870 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3871 SEQ_CST))
3872 goto done;
3873 feclearexcept (FE_ALL_EXCEPT);
3874 goto loop:
3875 done:
3876 feupdateenv (&fenv);
3878 The compiler will issue the __atomic_fetch_* built-in when possible,
3879 otherwise it will generate the generic form of the atomic operations.
3880 This requires temp(s) and has their address taken. The atomic processing
3881 is smart enough to figure out when the size of an object can utilize
3882 a lock-free version, and convert the built-in call to the appropriate
3883 lock-free routine. The optimizers will then dispose of any temps that
3884 are no longer required, and lock-free implementations are utilized as
3885 long as there is target support for the required size.
3887 If the operator is NOP_EXPR, then this is a simple assignment, and
3888 an __atomic_store is issued to perform the assignment rather than
3889 the above loop. */
3891 /* Build an atomic assignment at LOC, expanding into the proper
3892 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3893 the result of the operation, unless RETURN_OLD_P, in which case
3894 return the old value of LHS (this is only for postincrement and
3895 postdecrement). */
3897 static tree
3898 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3899 tree rhs, bool return_old_p)
3901 tree fndecl, func_call;
3902 vec<tree, va_gc> *params;
3903 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3904 tree old, old_addr;
3905 tree compound_stmt;
3906 tree stmt, goto_stmt;
3907 tree loop_label, loop_decl, done_label, done_decl;
3909 tree lhs_type = TREE_TYPE (lhs);
3910 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3911 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3912 tree rhs_type = TREE_TYPE (rhs);
3914 gcc_assert (TYPE_ATOMIC (lhs_type));
3916 if (return_old_p)
3917 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3919 /* Allocate enough vector items for a compare_exchange. */
3920 vec_alloc (params, 6);
3922 /* Create a compound statement to hold the sequence of statements
3923 with a loop. */
3924 compound_stmt = c_begin_compound_stmt (false);
3926 /* Fold the RHS if it hasn't already been folded. */
3927 if (modifycode != NOP_EXPR)
3928 rhs = c_fully_fold (rhs, false, NULL);
3930 /* Remove the qualifiers for the rest of the expressions and create
3931 the VAL temp variable to hold the RHS. */
3932 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3933 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3934 val = create_tmp_var_raw (nonatomic_rhs_type);
3935 TREE_ADDRESSABLE (val) = 1;
3936 TREE_NO_WARNING (val) = 1;
3937 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3938 NULL_TREE);
3939 SET_EXPR_LOCATION (rhs, loc);
3940 add_stmt (rhs);
3942 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3943 an atomic_store. */
3944 if (modifycode == NOP_EXPR)
3946 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3947 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3948 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3949 params->quick_push (lhs_addr);
3950 params->quick_push (rhs);
3951 params->quick_push (seq_cst);
3952 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3953 add_stmt (func_call);
3955 /* Finish the compound statement. */
3956 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3958 /* VAL is the value which was stored, return a COMPOUND_STMT of
3959 the statement and that value. */
3960 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3963 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3964 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3965 isn't applicable for such builtins. ??? Do we want to handle enums? */
3966 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3967 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3969 built_in_function fncode;
3970 switch (modifycode)
3972 case PLUS_EXPR:
3973 case POINTER_PLUS_EXPR:
3974 fncode = (return_old_p
3975 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3976 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3977 break;
3978 case MINUS_EXPR:
3979 fncode = (return_old_p
3980 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3981 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3982 break;
3983 case BIT_AND_EXPR:
3984 fncode = (return_old_p
3985 ? BUILT_IN_ATOMIC_FETCH_AND_N
3986 : BUILT_IN_ATOMIC_AND_FETCH_N);
3987 break;
3988 case BIT_IOR_EXPR:
3989 fncode = (return_old_p
3990 ? BUILT_IN_ATOMIC_FETCH_OR_N
3991 : BUILT_IN_ATOMIC_OR_FETCH_N);
3992 break;
3993 case BIT_XOR_EXPR:
3994 fncode = (return_old_p
3995 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3996 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3997 break;
3998 default:
3999 goto cas_loop;
4002 /* We can only use "_1" through "_16" variants of the atomic fetch
4003 built-ins. */
4004 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4005 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4006 goto cas_loop;
4008 /* If this is a pointer type, we need to multiply by the size of
4009 the pointer target type. */
4010 if (POINTER_TYPE_P (lhs_type))
4012 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4013 /* ??? This would introduce -Wdiscarded-qualifiers
4014 warning: __atomic_fetch_* expect volatile void *
4015 type as the first argument. (Assignments between
4016 atomic and non-atomic objects are OK.) */
4017 || TYPE_RESTRICT (lhs_type))
4018 goto cas_loop;
4019 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4020 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4021 convert (ptrdiff_type_node, rhs),
4022 convert (ptrdiff_type_node, sz));
4025 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4026 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4027 fndecl = builtin_decl_explicit (fncode);
4028 params->quick_push (lhs_addr);
4029 params->quick_push (rhs);
4030 params->quick_push (seq_cst);
4031 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4033 newval = create_tmp_var_raw (nonatomic_lhs_type);
4034 TREE_ADDRESSABLE (newval) = 1;
4035 TREE_NO_WARNING (newval) = 1;
4036 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4037 NULL_TREE, NULL_TREE);
4038 SET_EXPR_LOCATION (rhs, loc);
4039 add_stmt (rhs);
4041 /* Finish the compound statement. */
4042 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4044 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4045 the statement and that value. */
4046 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4049 cas_loop:
4050 /* Create the variables and labels required for the op= form. */
4051 old = create_tmp_var_raw (nonatomic_lhs_type);
4052 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4053 TREE_ADDRESSABLE (old) = 1;
4054 TREE_NO_WARNING (old) = 1;
4056 newval = create_tmp_var_raw (nonatomic_lhs_type);
4057 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4058 TREE_ADDRESSABLE (newval) = 1;
4059 TREE_NO_WARNING (newval) = 1;
4061 loop_decl = create_artificial_label (loc);
4062 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4064 done_decl = create_artificial_label (loc);
4065 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4067 /* __atomic_load (addr, &old, SEQ_CST). */
4068 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4069 params->quick_push (lhs_addr);
4070 params->quick_push (old_addr);
4071 params->quick_push (seq_cst);
4072 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4073 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4074 NULL_TREE);
4075 add_stmt (old);
4076 params->truncate (0);
4078 /* Create the expressions for floating-point environment
4079 manipulation, if required. */
4080 bool need_fenv = (flag_trapping_math
4081 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4082 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4083 if (need_fenv)
4084 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4086 if (hold_call)
4087 add_stmt (hold_call);
4089 /* loop: */
4090 add_stmt (loop_label);
4092 /* newval = old + val; */
4093 rhs = build_binary_op (loc, modifycode, old, val, 1);
4094 rhs = c_fully_fold (rhs, false, NULL);
4095 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4096 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4097 NULL_TREE, 0);
4098 if (rhs != error_mark_node)
4100 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4101 NULL_TREE);
4102 SET_EXPR_LOCATION (rhs, loc);
4103 add_stmt (rhs);
4106 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4107 goto done; */
4108 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4109 params->quick_push (lhs_addr);
4110 params->quick_push (old_addr);
4111 params->quick_push (newval_addr);
4112 params->quick_push (integer_zero_node);
4113 params->quick_push (seq_cst);
4114 params->quick_push (seq_cst);
4115 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4117 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4118 SET_EXPR_LOCATION (goto_stmt, loc);
4120 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4121 SET_EXPR_LOCATION (stmt, loc);
4122 add_stmt (stmt);
4124 if (clear_call)
4125 add_stmt (clear_call);
4127 /* goto loop; */
4128 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4129 SET_EXPR_LOCATION (goto_stmt, loc);
4130 add_stmt (goto_stmt);
4132 /* done: */
4133 add_stmt (done_label);
4135 if (update_call)
4136 add_stmt (update_call);
4138 /* Finish the compound statement. */
4139 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4141 /* NEWVAL is the value that was successfully stored, return a
4142 COMPOUND_EXPR of the statement and the appropriate value. */
4143 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4144 return_old_p ? old : newval);
4147 /* Construct and perhaps optimize a tree representation
4148 for a unary operation. CODE, a tree_code, specifies the operation
4149 and XARG is the operand.
4150 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4151 promotions (such as from short to int).
4152 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4153 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4154 to pointers in C99.
4156 LOCATION is the location of the operator. */
4158 tree
4159 build_unary_op (location_t location, enum tree_code code, tree xarg,
4160 bool noconvert)
4162 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4163 tree arg = xarg;
4164 tree argtype = 0;
4165 enum tree_code typecode;
4166 tree val;
4167 tree ret = error_mark_node;
4168 tree eptype = NULL_TREE;
4169 const char *invalid_op_diag;
4170 bool int_operands;
4172 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4173 if (int_operands)
4174 arg = remove_c_maybe_const_expr (arg);
4176 if (code != ADDR_EXPR)
4177 arg = require_complete_type (location, arg);
4179 typecode = TREE_CODE (TREE_TYPE (arg));
4180 if (typecode == ERROR_MARK)
4181 return error_mark_node;
4182 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4183 typecode = INTEGER_TYPE;
4185 if ((invalid_op_diag
4186 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4188 error_at (location, invalid_op_diag);
4189 return error_mark_node;
4192 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4194 eptype = TREE_TYPE (arg);
4195 arg = TREE_OPERAND (arg, 0);
4198 switch (code)
4200 case CONVERT_EXPR:
4201 /* This is used for unary plus, because a CONVERT_EXPR
4202 is enough to prevent anybody from looking inside for
4203 associativity, but won't generate any code. */
4204 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4205 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4206 || typecode == VECTOR_TYPE))
4208 error_at (location, "wrong type argument to unary plus");
4209 return error_mark_node;
4211 else if (!noconvert)
4212 arg = default_conversion (arg);
4213 arg = non_lvalue_loc (location, arg);
4214 break;
4216 case NEGATE_EXPR:
4217 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4218 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4219 || typecode == VECTOR_TYPE))
4221 error_at (location, "wrong type argument to unary minus");
4222 return error_mark_node;
4224 else if (!noconvert)
4225 arg = default_conversion (arg);
4226 break;
4228 case BIT_NOT_EXPR:
4229 /* ~ works on integer types and non float vectors. */
4230 if (typecode == INTEGER_TYPE
4231 || (typecode == VECTOR_TYPE
4232 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4234 tree e = arg;
4236 /* Warn if the expression has boolean value. */
4237 while (TREE_CODE (e) == COMPOUND_EXPR)
4238 e = TREE_OPERAND (e, 1);
4240 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4241 || truth_value_p (TREE_CODE (e)))
4242 && warning_at (location, OPT_Wbool_operation,
4243 "%<~%> on a boolean expression"))
4245 gcc_rich_location richloc (location);
4246 richloc.add_fixit_insert_before (location, "!");
4247 inform_at_rich_loc (&richloc, "did you mean to use logical "
4248 "not?");
4250 if (!noconvert)
4251 arg = default_conversion (arg);
4253 else if (typecode == COMPLEX_TYPE)
4255 code = CONJ_EXPR;
4256 pedwarn (location, OPT_Wpedantic,
4257 "ISO C does not support %<~%> for complex conjugation");
4258 if (!noconvert)
4259 arg = default_conversion (arg);
4261 else
4263 error_at (location, "wrong type argument to bit-complement");
4264 return error_mark_node;
4266 break;
4268 case ABS_EXPR:
4269 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4271 error_at (location, "wrong type argument to abs");
4272 return error_mark_node;
4274 else if (!noconvert)
4275 arg = default_conversion (arg);
4276 break;
4278 case CONJ_EXPR:
4279 /* Conjugating a real value is a no-op, but allow it anyway. */
4280 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4281 || typecode == COMPLEX_TYPE))
4283 error_at (location, "wrong type argument to conjugation");
4284 return error_mark_node;
4286 else if (!noconvert)
4287 arg = default_conversion (arg);
4288 break;
4290 case TRUTH_NOT_EXPR:
4291 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4292 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4293 && typecode != COMPLEX_TYPE)
4295 error_at (location,
4296 "wrong type argument to unary exclamation mark");
4297 return error_mark_node;
4299 if (int_operands)
4301 arg = c_objc_common_truthvalue_conversion (location, xarg);
4302 arg = remove_c_maybe_const_expr (arg);
4304 else
4305 arg = c_objc_common_truthvalue_conversion (location, arg);
4306 ret = invert_truthvalue_loc (location, arg);
4307 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4308 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4309 location = EXPR_LOCATION (ret);
4310 goto return_build_unary_op;
4312 case REALPART_EXPR:
4313 case IMAGPART_EXPR:
4314 ret = build_real_imag_expr (location, code, arg);
4315 if (ret == error_mark_node)
4316 return error_mark_node;
4317 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4318 eptype = TREE_TYPE (eptype);
4319 goto return_build_unary_op;
4321 case PREINCREMENT_EXPR:
4322 case POSTINCREMENT_EXPR:
4323 case PREDECREMENT_EXPR:
4324 case POSTDECREMENT_EXPR:
4326 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4328 tree inner = build_unary_op (location, code,
4329 C_MAYBE_CONST_EXPR_EXPR (arg),
4330 noconvert);
4331 if (inner == error_mark_node)
4332 return error_mark_node;
4333 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4334 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4335 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4336 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4337 goto return_build_unary_op;
4340 /* Complain about anything that is not a true lvalue. In
4341 Objective-C, skip this check for property_refs. */
4342 if (!objc_is_property_ref (arg)
4343 && !lvalue_or_else (location,
4344 arg, ((code == PREINCREMENT_EXPR
4345 || code == POSTINCREMENT_EXPR)
4346 ? lv_increment
4347 : lv_decrement)))
4348 return error_mark_node;
4350 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4352 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4353 warning_at (location, OPT_Wc___compat,
4354 "increment of enumeration value is invalid in C++");
4355 else
4356 warning_at (location, OPT_Wc___compat,
4357 "decrement of enumeration value is invalid in C++");
4360 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4362 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4363 warning_at (location, OPT_Wbool_operation,
4364 "increment of a boolean expression");
4365 else
4366 warning_at (location, OPT_Wbool_operation,
4367 "decrement of a boolean expression");
4370 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4371 arg = c_fully_fold (arg, false, NULL);
4373 bool atomic_op;
4374 atomic_op = really_atomic_lvalue (arg);
4376 /* Increment or decrement the real part of the value,
4377 and don't change the imaginary part. */
4378 if (typecode == COMPLEX_TYPE)
4380 tree real, imag;
4382 pedwarn (location, OPT_Wpedantic,
4383 "ISO C does not support %<++%> and %<--%> on complex types");
4385 if (!atomic_op)
4387 arg = stabilize_reference (arg);
4388 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4389 true);
4390 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4391 true);
4392 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4393 if (real == error_mark_node || imag == error_mark_node)
4394 return error_mark_node;
4395 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4396 real, imag);
4397 goto return_build_unary_op;
4401 /* Report invalid types. */
4403 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4404 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4405 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4407 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4408 error_at (location, "wrong type argument to increment");
4409 else
4410 error_at (location, "wrong type argument to decrement");
4412 return error_mark_node;
4416 tree inc;
4418 argtype = TREE_TYPE (arg);
4420 /* Compute the increment. */
4422 if (typecode == POINTER_TYPE)
4424 /* If pointer target is an incomplete type,
4425 we just cannot know how to do the arithmetic. */
4426 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4428 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4429 error_at (location,
4430 "increment of pointer to an incomplete type %qT",
4431 TREE_TYPE (argtype));
4432 else
4433 error_at (location,
4434 "decrement of pointer to an incomplete type %qT",
4435 TREE_TYPE (argtype));
4437 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4438 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4440 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4441 pedwarn (location, OPT_Wpointer_arith,
4442 "wrong type argument to increment");
4443 else
4444 pedwarn (location, OPT_Wpointer_arith,
4445 "wrong type argument to decrement");
4448 inc = c_size_in_bytes (TREE_TYPE (argtype));
4449 inc = convert_to_ptrofftype_loc (location, inc);
4451 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4453 /* For signed fract types, we invert ++ to -- or
4454 -- to ++, and change inc from 1 to -1, because
4455 it is not possible to represent 1 in signed fract constants.
4456 For unsigned fract types, the result always overflows and
4457 we get an undefined (original) or the maximum value. */
4458 if (code == PREINCREMENT_EXPR)
4459 code = PREDECREMENT_EXPR;
4460 else if (code == PREDECREMENT_EXPR)
4461 code = PREINCREMENT_EXPR;
4462 else if (code == POSTINCREMENT_EXPR)
4463 code = POSTDECREMENT_EXPR;
4464 else /* code == POSTDECREMENT_EXPR */
4465 code = POSTINCREMENT_EXPR;
4467 inc = integer_minus_one_node;
4468 inc = convert (argtype, inc);
4470 else
4472 inc = VECTOR_TYPE_P (argtype)
4473 ? build_one_cst (argtype)
4474 : integer_one_node;
4475 inc = convert (argtype, inc);
4478 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4479 need to ask Objective-C to build the increment or decrement
4480 expression for it. */
4481 if (objc_is_property_ref (arg))
4482 return objc_build_incr_expr_for_property_ref (location, code,
4483 arg, inc);
4485 /* Report a read-only lvalue. */
4486 if (TYPE_READONLY (argtype))
4488 readonly_error (location, arg,
4489 ((code == PREINCREMENT_EXPR
4490 || code == POSTINCREMENT_EXPR)
4491 ? lv_increment : lv_decrement));
4492 return error_mark_node;
4494 else if (TREE_READONLY (arg))
4495 readonly_warning (arg,
4496 ((code == PREINCREMENT_EXPR
4497 || code == POSTINCREMENT_EXPR)
4498 ? lv_increment : lv_decrement));
4500 /* If the argument is atomic, use the special code sequences for
4501 atomic compound assignment. */
4502 if (atomic_op)
4504 arg = stabilize_reference (arg);
4505 ret = build_atomic_assign (location, arg,
4506 ((code == PREINCREMENT_EXPR
4507 || code == POSTINCREMENT_EXPR)
4508 ? PLUS_EXPR
4509 : MINUS_EXPR),
4510 (FRACT_MODE_P (TYPE_MODE (argtype))
4511 ? inc
4512 : integer_one_node),
4513 (code == POSTINCREMENT_EXPR
4514 || code == POSTDECREMENT_EXPR));
4515 goto return_build_unary_op;
4518 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4519 val = boolean_increment (code, arg);
4520 else
4521 val = build2 (code, TREE_TYPE (arg), arg, inc);
4522 TREE_SIDE_EFFECTS (val) = 1;
4523 if (TREE_CODE (val) != code)
4524 TREE_NO_WARNING (val) = 1;
4525 ret = val;
4526 goto return_build_unary_op;
4529 case ADDR_EXPR:
4530 /* Note that this operation never does default_conversion. */
4532 /* The operand of unary '&' must be an lvalue (which excludes
4533 expressions of type void), or, in C99, the result of a [] or
4534 unary '*' operator. */
4535 if (VOID_TYPE_P (TREE_TYPE (arg))
4536 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4537 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4538 pedwarn (location, 0, "taking address of expression of type %<void%>");
4540 /* Let &* cancel out to simplify resulting code. */
4541 if (INDIRECT_REF_P (arg))
4543 /* Don't let this be an lvalue. */
4544 if (lvalue_p (TREE_OPERAND (arg, 0)))
4545 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4546 ret = TREE_OPERAND (arg, 0);
4547 goto return_build_unary_op;
4550 /* Anything not already handled and not a true memory reference
4551 or a non-lvalue array is an error. */
4552 if (typecode != FUNCTION_TYPE && !noconvert
4553 && !lvalue_or_else (location, arg, lv_addressof))
4554 return error_mark_node;
4556 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4557 folding later. */
4558 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4560 tree inner = build_unary_op (location, code,
4561 C_MAYBE_CONST_EXPR_EXPR (arg),
4562 noconvert);
4563 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4564 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4565 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4566 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4567 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4568 goto return_build_unary_op;
4571 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4572 argtype = TREE_TYPE (arg);
4574 /* If the lvalue is const or volatile, merge that into the type
4575 to which the address will point. This is only needed
4576 for function types. */
4577 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4578 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4579 && TREE_CODE (argtype) == FUNCTION_TYPE)
4581 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4582 int quals = orig_quals;
4584 if (TREE_READONLY (arg))
4585 quals |= TYPE_QUAL_CONST;
4586 if (TREE_THIS_VOLATILE (arg))
4587 quals |= TYPE_QUAL_VOLATILE;
4589 argtype = c_build_qualified_type (argtype, quals);
4592 switch (TREE_CODE (arg))
4594 case COMPONENT_REF:
4595 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4597 error_at (location, "cannot take address of bit-field %qD",
4598 TREE_OPERAND (arg, 1));
4599 return error_mark_node;
4602 /* fall through */
4604 case ARRAY_REF:
4605 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4607 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4608 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4610 error_at (location, "cannot take address of scalar with "
4611 "reverse storage order");
4612 return error_mark_node;
4615 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4616 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4617 warning_at (location, OPT_Wscalar_storage_order,
4618 "address of array with reverse scalar storage "
4619 "order requested");
4622 default:
4623 break;
4626 if (!c_mark_addressable (arg))
4627 return error_mark_node;
4629 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4630 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4632 argtype = build_pointer_type (argtype);
4634 /* ??? Cope with user tricks that amount to offsetof. Delete this
4635 when we have proper support for integer constant expressions. */
4636 val = get_base_address (arg);
4637 if (val && INDIRECT_REF_P (val)
4638 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4640 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4641 goto return_build_unary_op;
4644 val = build1 (ADDR_EXPR, argtype, arg);
4646 ret = val;
4647 goto return_build_unary_op;
4649 default:
4650 gcc_unreachable ();
4653 if (argtype == 0)
4654 argtype = TREE_TYPE (arg);
4655 if (TREE_CODE (arg) == INTEGER_CST)
4656 ret = (require_constant_value
4657 ? fold_build1_initializer_loc (location, code, argtype, arg)
4658 : fold_build1_loc (location, code, argtype, arg));
4659 else
4660 ret = build1 (code, argtype, arg);
4661 return_build_unary_op:
4662 gcc_assert (ret != error_mark_node);
4663 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4664 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4665 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4666 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4667 ret = note_integer_operands (ret);
4668 if (eptype)
4669 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4670 protected_set_expr_location (ret, location);
4671 return ret;
4674 /* Return nonzero if REF is an lvalue valid for this language.
4675 Lvalues can be assigned, unless their type has TYPE_READONLY.
4676 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4678 bool
4679 lvalue_p (const_tree ref)
4681 const enum tree_code code = TREE_CODE (ref);
4683 switch (code)
4685 case REALPART_EXPR:
4686 case IMAGPART_EXPR:
4687 case COMPONENT_REF:
4688 return lvalue_p (TREE_OPERAND (ref, 0));
4690 case C_MAYBE_CONST_EXPR:
4691 return lvalue_p (TREE_OPERAND (ref, 1));
4693 case COMPOUND_LITERAL_EXPR:
4694 case STRING_CST:
4695 return true;
4697 case INDIRECT_REF:
4698 case ARRAY_REF:
4699 case ARRAY_NOTATION_REF:
4700 case VAR_DECL:
4701 case PARM_DECL:
4702 case RESULT_DECL:
4703 case ERROR_MARK:
4704 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4705 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4707 case BIND_EXPR:
4708 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4710 default:
4711 return false;
4715 /* Give a warning for storing in something that is read-only in GCC
4716 terms but not const in ISO C terms. */
4718 static void
4719 readonly_warning (tree arg, enum lvalue_use use)
4721 switch (use)
4723 case lv_assign:
4724 warning (0, "assignment of read-only location %qE", arg);
4725 break;
4726 case lv_increment:
4727 warning (0, "increment of read-only location %qE", arg);
4728 break;
4729 case lv_decrement:
4730 warning (0, "decrement of read-only location %qE", arg);
4731 break;
4732 default:
4733 gcc_unreachable ();
4735 return;
4739 /* Return nonzero if REF is an lvalue valid for this language;
4740 otherwise, print an error message and return zero. USE says
4741 how the lvalue is being used and so selects the error message.
4742 LOCATION is the location at which any error should be reported. */
4744 static int
4745 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4747 int win = lvalue_p (ref);
4749 if (!win)
4750 lvalue_error (loc, use);
4752 return win;
4755 /* Mark EXP saying that we need to be able to take the
4756 address of it; it should not be allocated in a register.
4757 Returns true if successful. */
4759 bool
4760 c_mark_addressable (tree exp)
4762 tree x = exp;
4764 while (1)
4765 switch (TREE_CODE (x))
4767 case COMPONENT_REF:
4768 case ADDR_EXPR:
4769 case ARRAY_REF:
4770 case REALPART_EXPR:
4771 case IMAGPART_EXPR:
4772 x = TREE_OPERAND (x, 0);
4773 break;
4775 case COMPOUND_LITERAL_EXPR:
4776 case CONSTRUCTOR:
4777 TREE_ADDRESSABLE (x) = 1;
4778 return true;
4780 case VAR_DECL:
4781 case CONST_DECL:
4782 case PARM_DECL:
4783 case RESULT_DECL:
4784 if (C_DECL_REGISTER (x)
4785 && DECL_NONLOCAL (x))
4787 if (TREE_PUBLIC (x) || is_global_var (x))
4789 error
4790 ("global register variable %qD used in nested function", x);
4791 return false;
4793 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4795 else if (C_DECL_REGISTER (x))
4797 if (TREE_PUBLIC (x) || is_global_var (x))
4798 error ("address of global register variable %qD requested", x);
4799 else
4800 error ("address of register variable %qD requested", x);
4801 return false;
4804 /* FALLTHRU */
4805 case FUNCTION_DECL:
4806 TREE_ADDRESSABLE (x) = 1;
4807 /* FALLTHRU */
4808 default:
4809 return true;
4813 /* Convert EXPR to TYPE, warning about conversion problems with
4814 constants. SEMANTIC_TYPE is the type this conversion would use
4815 without excess precision. If SEMANTIC_TYPE is NULL, this function
4816 is equivalent to convert_and_check. This function is a wrapper that
4817 handles conversions that may be different than
4818 the usual ones because of excess precision. */
4820 static tree
4821 ep_convert_and_check (location_t loc, tree type, tree expr,
4822 tree semantic_type)
4824 if (TREE_TYPE (expr) == type)
4825 return expr;
4827 if (!semantic_type)
4828 return convert_and_check (loc, type, expr);
4830 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4831 && TREE_TYPE (expr) != semantic_type)
4833 /* For integers, we need to check the real conversion, not
4834 the conversion to the excess precision type. */
4835 expr = convert_and_check (loc, semantic_type, expr);
4837 /* Result type is the excess precision type, which should be
4838 large enough, so do not check. */
4839 return convert (type, expr);
4842 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4843 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4844 if folded to an integer constant then the unselected half may
4845 contain arbitrary operations not normally permitted in constant
4846 expressions. Set the location of the expression to LOC. */
4848 tree
4849 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4850 tree op1, tree op1_original_type, tree op2,
4851 tree op2_original_type)
4853 tree type1;
4854 tree type2;
4855 enum tree_code code1;
4856 enum tree_code code2;
4857 tree result_type = NULL;
4858 tree semantic_result_type = NULL;
4859 tree orig_op1 = op1, orig_op2 = op2;
4860 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4861 bool ifexp_int_operands;
4862 tree ret;
4864 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4865 if (op1_int_operands)
4866 op1 = remove_c_maybe_const_expr (op1);
4867 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4868 if (op2_int_operands)
4869 op2 = remove_c_maybe_const_expr (op2);
4870 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4871 if (ifexp_int_operands)
4872 ifexp = remove_c_maybe_const_expr (ifexp);
4874 /* Promote both alternatives. */
4876 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4877 op1 = default_conversion (op1);
4878 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4879 op2 = default_conversion (op2);
4881 if (TREE_CODE (ifexp) == ERROR_MARK
4882 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4883 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4884 return error_mark_node;
4886 type1 = TREE_TYPE (op1);
4887 code1 = TREE_CODE (type1);
4888 type2 = TREE_TYPE (op2);
4889 code2 = TREE_CODE (type2);
4891 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4892 return error_mark_node;
4894 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4895 return error_mark_node;
4897 /* C90 does not permit non-lvalue arrays in conditional expressions.
4898 In C99 they will be pointers by now. */
4899 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4901 error_at (colon_loc, "non-lvalue array in conditional expression");
4902 return error_mark_node;
4905 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4906 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4907 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4908 || code1 == COMPLEX_TYPE)
4909 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4910 || code2 == COMPLEX_TYPE))
4912 semantic_result_type = c_common_type (type1, type2);
4913 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4915 op1 = TREE_OPERAND (op1, 0);
4916 type1 = TREE_TYPE (op1);
4917 gcc_assert (TREE_CODE (type1) == code1);
4919 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4921 op2 = TREE_OPERAND (op2, 0);
4922 type2 = TREE_TYPE (op2);
4923 gcc_assert (TREE_CODE (type2) == code2);
4927 if (warn_cxx_compat)
4929 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4930 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4932 if (TREE_CODE (t1) == ENUMERAL_TYPE
4933 && TREE_CODE (t2) == ENUMERAL_TYPE
4934 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4935 warning_at (colon_loc, OPT_Wc___compat,
4936 ("different enum types in conditional is "
4937 "invalid in C++: %qT vs %qT"),
4938 t1, t2);
4941 /* Quickly detect the usual case where op1 and op2 have the same type
4942 after promotion. */
4943 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4945 if (type1 == type2)
4946 result_type = type1;
4947 else
4948 result_type = TYPE_MAIN_VARIANT (type1);
4950 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4951 || code1 == COMPLEX_TYPE)
4952 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4953 || code2 == COMPLEX_TYPE))
4955 result_type = c_common_type (type1, type2);
4956 if (result_type == error_mark_node)
4957 return error_mark_node;
4958 do_warn_double_promotion (result_type, type1, type2,
4959 "implicit conversion from %qT to %qT to "
4960 "match other result of conditional",
4961 colon_loc);
4963 /* If -Wsign-compare, warn here if type1 and type2 have
4964 different signedness. We'll promote the signed to unsigned
4965 and later code won't know it used to be different.
4966 Do this check on the original types, so that explicit casts
4967 will be considered, but default promotions won't. */
4968 if (c_inhibit_evaluation_warnings == 0)
4970 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4971 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4973 if (unsigned_op1 ^ unsigned_op2)
4975 bool ovf;
4977 /* Do not warn if the result type is signed, since the
4978 signed type will only be chosen if it can represent
4979 all the values of the unsigned type. */
4980 if (!TYPE_UNSIGNED (result_type))
4981 /* OK */;
4982 else
4984 bool op1_maybe_const = true;
4985 bool op2_maybe_const = true;
4987 /* Do not warn if the signed quantity is an
4988 unsuffixed integer literal (or some static
4989 constant expression involving such literals) and
4990 it is non-negative. This warning requires the
4991 operands to be folded for best results, so do
4992 that folding in this case even without
4993 warn_sign_compare to avoid warning options
4994 possibly affecting code generation. */
4995 c_inhibit_evaluation_warnings
4996 += (ifexp == truthvalue_false_node);
4997 op1 = c_fully_fold (op1, require_constant_value,
4998 &op1_maybe_const);
4999 c_inhibit_evaluation_warnings
5000 -= (ifexp == truthvalue_false_node);
5002 c_inhibit_evaluation_warnings
5003 += (ifexp == truthvalue_true_node);
5004 op2 = c_fully_fold (op2, require_constant_value,
5005 &op2_maybe_const);
5006 c_inhibit_evaluation_warnings
5007 -= (ifexp == truthvalue_true_node);
5009 if (warn_sign_compare)
5011 if ((unsigned_op2
5012 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5013 || (unsigned_op1
5014 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5015 /* OK */;
5016 else
5017 warning_at (colon_loc, OPT_Wsign_compare,
5018 ("signed and unsigned type in "
5019 "conditional expression"));
5021 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5022 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5023 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5024 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5029 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5031 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5032 pedwarn (colon_loc, OPT_Wpedantic,
5033 "ISO C forbids conditional expr with only one void side");
5034 result_type = void_type_node;
5036 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5038 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5039 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5040 addr_space_t as_common;
5042 if (comp_target_types (colon_loc, type1, type2))
5043 result_type = common_pointer_type (type1, type2);
5044 else if (null_pointer_constant_p (orig_op1))
5045 result_type = type2;
5046 else if (null_pointer_constant_p (orig_op2))
5047 result_type = type1;
5048 else if (!addr_space_superset (as1, as2, &as_common))
5050 error_at (colon_loc, "pointers to disjoint address spaces "
5051 "used in conditional expression");
5052 return error_mark_node;
5054 else if (VOID_TYPE_P (TREE_TYPE (type1))
5055 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5057 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5058 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5059 & ~TYPE_QUALS (TREE_TYPE (type1))))
5060 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5061 "pointer to array loses qualifier "
5062 "in conditional expression");
5064 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5065 pedwarn (colon_loc, OPT_Wpedantic,
5066 "ISO C forbids conditional expr between "
5067 "%<void *%> and function pointer");
5068 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5069 TREE_TYPE (type2)));
5071 else if (VOID_TYPE_P (TREE_TYPE (type2))
5072 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5074 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5075 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5076 & ~TYPE_QUALS (TREE_TYPE (type2))))
5077 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5078 "pointer to array loses qualifier "
5079 "in conditional expression");
5081 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5082 pedwarn (colon_loc, OPT_Wpedantic,
5083 "ISO C forbids conditional expr between "
5084 "%<void *%> and function pointer");
5085 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5086 TREE_TYPE (type1)));
5088 /* Objective-C pointer comparisons are a bit more lenient. */
5089 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5090 result_type = objc_common_type (type1, type2);
5091 else
5093 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5095 pedwarn (colon_loc, 0,
5096 "pointer type mismatch in conditional expression");
5097 result_type = build_pointer_type
5098 (build_qualified_type (void_type_node, qual));
5101 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5103 if (!null_pointer_constant_p (orig_op2))
5104 pedwarn (colon_loc, 0,
5105 "pointer/integer type mismatch in conditional expression");
5106 else
5108 op2 = null_pointer_node;
5110 result_type = type1;
5112 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5114 if (!null_pointer_constant_p (orig_op1))
5115 pedwarn (colon_loc, 0,
5116 "pointer/integer type mismatch in conditional expression");
5117 else
5119 op1 = null_pointer_node;
5121 result_type = type2;
5124 if (!result_type)
5126 if (flag_cond_mismatch)
5127 result_type = void_type_node;
5128 else
5130 error_at (colon_loc, "type mismatch in conditional expression");
5131 return error_mark_node;
5135 /* Merge const and volatile flags of the incoming types. */
5136 result_type
5137 = build_type_variant (result_type,
5138 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5139 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5141 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5142 semantic_result_type);
5143 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5144 semantic_result_type);
5146 if (ifexp_bcp && ifexp == truthvalue_true_node)
5148 op2_int_operands = true;
5149 op1 = c_fully_fold (op1, require_constant_value, NULL);
5151 if (ifexp_bcp && ifexp == truthvalue_false_node)
5153 op1_int_operands = true;
5154 op2 = c_fully_fold (op2, require_constant_value, NULL);
5156 int_const = int_operands = (ifexp_int_operands
5157 && op1_int_operands
5158 && op2_int_operands);
5159 if (int_operands)
5161 int_const = ((ifexp == truthvalue_true_node
5162 && TREE_CODE (orig_op1) == INTEGER_CST
5163 && !TREE_OVERFLOW (orig_op1))
5164 || (ifexp == truthvalue_false_node
5165 && TREE_CODE (orig_op2) == INTEGER_CST
5166 && !TREE_OVERFLOW (orig_op2)));
5169 /* Need to convert condition operand into a vector mask. */
5170 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5172 tree vectype = TREE_TYPE (ifexp);
5173 tree elem_type = TREE_TYPE (vectype);
5174 tree zero = build_int_cst (elem_type, 0);
5175 tree zero_vec = build_vector_from_val (vectype, zero);
5176 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5177 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5180 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5181 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5182 else
5184 if (int_operands)
5186 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5187 nested inside of the expression. */
5188 op1 = c_fully_fold (op1, false, NULL);
5189 op2 = c_fully_fold (op2, false, NULL);
5191 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5192 if (int_operands)
5193 ret = note_integer_operands (ret);
5195 if (semantic_result_type)
5196 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5198 protected_set_expr_location (ret, colon_loc);
5200 /* If the OP1 and OP2 are the same and don't have side-effects,
5201 warn here, because the COND_EXPR will be turned into OP1. */
5202 if (warn_duplicated_branches
5203 && TREE_CODE (ret) == COND_EXPR
5204 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5205 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5206 "this condition has identical branches");
5208 return ret;
5211 /* Return a compound expression that performs two expressions and
5212 returns the value of the second of them.
5214 LOC is the location of the COMPOUND_EXPR. */
5216 tree
5217 build_compound_expr (location_t loc, tree expr1, tree expr2)
5219 bool expr1_int_operands, expr2_int_operands;
5220 tree eptype = NULL_TREE;
5221 tree ret;
5223 if (flag_cilkplus
5224 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5225 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5227 error_at (loc,
5228 "spawned function call cannot be part of a comma expression");
5229 return error_mark_node;
5231 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5232 if (expr1_int_operands)
5233 expr1 = remove_c_maybe_const_expr (expr1);
5234 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5235 if (expr2_int_operands)
5236 expr2 = remove_c_maybe_const_expr (expr2);
5238 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5239 expr1 = TREE_OPERAND (expr1, 0);
5240 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5242 eptype = TREE_TYPE (expr2);
5243 expr2 = TREE_OPERAND (expr2, 0);
5246 if (!TREE_SIDE_EFFECTS (expr1))
5248 /* The left-hand operand of a comma expression is like an expression
5249 statement: with -Wunused, we should warn if it doesn't have
5250 any side-effects, unless it was explicitly cast to (void). */
5251 if (warn_unused_value)
5253 if (VOID_TYPE_P (TREE_TYPE (expr1))
5254 && CONVERT_EXPR_P (expr1))
5255 ; /* (void) a, b */
5256 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5257 && TREE_CODE (expr1) == COMPOUND_EXPR
5258 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5259 ; /* (void) a, (void) b, c */
5260 else
5261 warning_at (loc, OPT_Wunused_value,
5262 "left-hand operand of comma expression has no effect");
5265 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5266 && warn_unused_value)
5268 tree r = expr1;
5269 location_t cloc = loc;
5270 while (TREE_CODE (r) == COMPOUND_EXPR)
5272 if (EXPR_HAS_LOCATION (r))
5273 cloc = EXPR_LOCATION (r);
5274 r = TREE_OPERAND (r, 1);
5276 if (!TREE_SIDE_EFFECTS (r)
5277 && !VOID_TYPE_P (TREE_TYPE (r))
5278 && !CONVERT_EXPR_P (r))
5279 warning_at (cloc, OPT_Wunused_value,
5280 "right-hand operand of comma expression has no effect");
5283 /* With -Wunused, we should also warn if the left-hand operand does have
5284 side-effects, but computes a value which is not used. For example, in
5285 `foo() + bar(), baz()' the result of the `+' operator is not used,
5286 so we should issue a warning. */
5287 else if (warn_unused_value)
5288 warn_if_unused_value (expr1, loc);
5290 if (expr2 == error_mark_node)
5291 return error_mark_node;
5293 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5295 if (flag_isoc99
5296 && expr1_int_operands
5297 && expr2_int_operands)
5298 ret = note_integer_operands (ret);
5300 if (eptype)
5301 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5303 protected_set_expr_location (ret, loc);
5304 return ret;
5307 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5308 which we are casting. OTYPE is the type of the expression being
5309 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5310 of the cast. -Wcast-qual appeared on the command line. Named
5311 address space qualifiers are not handled here, because they result
5312 in different warnings. */
5314 static void
5315 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5317 tree in_type = type;
5318 tree in_otype = otype;
5319 int added = 0;
5320 int discarded = 0;
5321 bool is_const;
5323 /* Check that the qualifiers on IN_TYPE are a superset of the
5324 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5325 nodes is uninteresting and we stop as soon as we hit a
5326 non-POINTER_TYPE node on either type. */
5329 in_otype = TREE_TYPE (in_otype);
5330 in_type = TREE_TYPE (in_type);
5332 /* GNU C allows cv-qualified function types. 'const' means the
5333 function is very pure, 'volatile' means it can't return. We
5334 need to warn when such qualifiers are added, not when they're
5335 taken away. */
5336 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5337 && TREE_CODE (in_type) == FUNCTION_TYPE)
5338 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5339 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5340 else
5341 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5342 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5344 while (TREE_CODE (in_type) == POINTER_TYPE
5345 && TREE_CODE (in_otype) == POINTER_TYPE);
5347 if (added)
5348 warning_at (loc, OPT_Wcast_qual,
5349 "cast adds %q#v qualifier to function type", added);
5351 if (discarded)
5352 /* There are qualifiers present in IN_OTYPE that are not present
5353 in IN_TYPE. */
5354 warning_at (loc, OPT_Wcast_qual,
5355 "cast discards %qv qualifier from pointer target type",
5356 discarded);
5358 if (added || discarded)
5359 return;
5361 /* A cast from **T to const **T is unsafe, because it can cause a
5362 const value to be changed with no additional warning. We only
5363 issue this warning if T is the same on both sides, and we only
5364 issue the warning if there are the same number of pointers on
5365 both sides, as otherwise the cast is clearly unsafe anyhow. A
5366 cast is unsafe when a qualifier is added at one level and const
5367 is not present at all outer levels.
5369 To issue this warning, we check at each level whether the cast
5370 adds new qualifiers not already seen. We don't need to special
5371 case function types, as they won't have the same
5372 TYPE_MAIN_VARIANT. */
5374 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5375 return;
5376 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5377 return;
5379 in_type = type;
5380 in_otype = otype;
5381 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5384 in_type = TREE_TYPE (in_type);
5385 in_otype = TREE_TYPE (in_otype);
5386 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5387 && !is_const)
5389 warning_at (loc, OPT_Wcast_qual,
5390 "to be safe all intermediate pointers in cast from "
5391 "%qT to %qT must be %<const%> qualified",
5392 otype, type);
5393 break;
5395 if (is_const)
5396 is_const = TYPE_READONLY (in_type);
5398 while (TREE_CODE (in_type) == POINTER_TYPE);
5401 /* Build an expression representing a cast to type TYPE of expression EXPR.
5402 LOC is the location of the cast-- typically the open paren of the cast. */
5404 tree
5405 build_c_cast (location_t loc, tree type, tree expr)
5407 tree value;
5409 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5410 expr = TREE_OPERAND (expr, 0);
5412 value = expr;
5414 if (type == error_mark_node || expr == error_mark_node)
5415 return error_mark_node;
5417 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5418 only in <protocol> qualifications. But when constructing cast expressions,
5419 the protocols do matter and must be kept around. */
5420 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5421 return build1 (NOP_EXPR, type, expr);
5423 type = TYPE_MAIN_VARIANT (type);
5425 if (TREE_CODE (type) == ARRAY_TYPE)
5427 error_at (loc, "cast specifies array type");
5428 return error_mark_node;
5431 if (TREE_CODE (type) == FUNCTION_TYPE)
5433 error_at (loc, "cast specifies function type");
5434 return error_mark_node;
5437 if (!VOID_TYPE_P (type))
5439 value = require_complete_type (loc, value);
5440 if (value == error_mark_node)
5441 return error_mark_node;
5444 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5446 if (RECORD_OR_UNION_TYPE_P (type))
5447 pedwarn (loc, OPT_Wpedantic,
5448 "ISO C forbids casting nonscalar to the same type");
5450 /* Convert to remove any qualifiers from VALUE's type. */
5451 value = convert (type, value);
5453 else if (TREE_CODE (type) == UNION_TYPE)
5455 tree field;
5457 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5458 if (TREE_TYPE (field) != error_mark_node
5459 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5460 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5461 break;
5463 if (field)
5465 tree t;
5466 bool maybe_const = true;
5468 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5469 t = c_fully_fold (value, false, &maybe_const);
5470 t = build_constructor_single (type, field, t);
5471 if (!maybe_const)
5472 t = c_wrap_maybe_const (t, true);
5473 t = digest_init (loc, type, t,
5474 NULL_TREE, false, true, 0);
5475 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5476 return t;
5478 error_at (loc, "cast to union type from type not present in union");
5479 return error_mark_node;
5481 else
5483 tree otype, ovalue;
5485 if (type == void_type_node)
5487 tree t = build1 (CONVERT_EXPR, type, value);
5488 SET_EXPR_LOCATION (t, loc);
5489 return t;
5492 otype = TREE_TYPE (value);
5494 /* Optionally warn about potentially worrisome casts. */
5495 if (warn_cast_qual
5496 && TREE_CODE (type) == POINTER_TYPE
5497 && TREE_CODE (otype) == POINTER_TYPE)
5498 handle_warn_cast_qual (loc, type, otype);
5500 /* Warn about conversions between pointers to disjoint
5501 address spaces. */
5502 if (TREE_CODE (type) == POINTER_TYPE
5503 && TREE_CODE (otype) == POINTER_TYPE
5504 && !null_pointer_constant_p (value))
5506 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5507 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5508 addr_space_t as_common;
5510 if (!addr_space_superset (as_to, as_from, &as_common))
5512 if (ADDR_SPACE_GENERIC_P (as_from))
5513 warning_at (loc, 0, "cast to %s address space pointer "
5514 "from disjoint generic address space pointer",
5515 c_addr_space_name (as_to));
5517 else if (ADDR_SPACE_GENERIC_P (as_to))
5518 warning_at (loc, 0, "cast to generic address space pointer "
5519 "from disjoint %s address space pointer",
5520 c_addr_space_name (as_from));
5522 else
5523 warning_at (loc, 0, "cast to %s address space pointer "
5524 "from disjoint %s address space pointer",
5525 c_addr_space_name (as_to),
5526 c_addr_space_name (as_from));
5530 /* Warn about possible alignment problems. */
5531 if (STRICT_ALIGNMENT
5532 && TREE_CODE (type) == POINTER_TYPE
5533 && TREE_CODE (otype) == POINTER_TYPE
5534 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5535 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5536 /* Don't warn about opaque types, where the actual alignment
5537 restriction is unknown. */
5538 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5539 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5540 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5541 warning_at (loc, OPT_Wcast_align,
5542 "cast increases required alignment of target type");
5544 if (TREE_CODE (type) == INTEGER_TYPE
5545 && TREE_CODE (otype) == POINTER_TYPE
5546 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5547 /* Unlike conversion of integers to pointers, where the
5548 warning is disabled for converting constants because
5549 of cases such as SIG_*, warn about converting constant
5550 pointers to integers. In some cases it may cause unwanted
5551 sign extension, and a warning is appropriate. */
5552 warning_at (loc, OPT_Wpointer_to_int_cast,
5553 "cast from pointer to integer of different size");
5555 if (TREE_CODE (value) == CALL_EXPR
5556 && TREE_CODE (type) != TREE_CODE (otype))
5557 warning_at (loc, OPT_Wbad_function_cast,
5558 "cast from function call of type %qT "
5559 "to non-matching type %qT", otype, type);
5561 if (TREE_CODE (type) == POINTER_TYPE
5562 && TREE_CODE (otype) == INTEGER_TYPE
5563 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5564 /* Don't warn about converting any constant. */
5565 && !TREE_CONSTANT (value))
5566 warning_at (loc,
5567 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5568 "of different size");
5570 if (warn_strict_aliasing <= 2)
5571 strict_aliasing_warning (otype, type, expr);
5573 /* If pedantic, warn for conversions between function and object
5574 pointer types, except for converting a null pointer constant
5575 to function pointer type. */
5576 if (pedantic
5577 && TREE_CODE (type) == POINTER_TYPE
5578 && TREE_CODE (otype) == POINTER_TYPE
5579 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5580 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5581 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5582 "conversion of function pointer to object pointer type");
5584 if (pedantic
5585 && TREE_CODE (type) == POINTER_TYPE
5586 && TREE_CODE (otype) == POINTER_TYPE
5587 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5588 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5589 && !null_pointer_constant_p (value))
5590 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5591 "conversion of object pointer to function pointer type");
5593 ovalue = value;
5594 value = convert (type, value);
5596 /* Ignore any integer overflow caused by the cast. */
5597 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5599 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5601 if (!TREE_OVERFLOW (value))
5603 /* Avoid clobbering a shared constant. */
5604 value = copy_node (value);
5605 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5608 else if (TREE_OVERFLOW (value))
5609 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5610 value = wide_int_to_tree (TREE_TYPE (value), value);
5614 /* Don't let a cast be an lvalue. */
5615 if (lvalue_p (value))
5616 value = non_lvalue_loc (loc, value);
5618 /* Don't allow the results of casting to floating-point or complex
5619 types be confused with actual constants, or casts involving
5620 integer and pointer types other than direct integer-to-integer
5621 and integer-to-pointer be confused with integer constant
5622 expressions and null pointer constants. */
5623 if (TREE_CODE (value) == REAL_CST
5624 || TREE_CODE (value) == COMPLEX_CST
5625 || (TREE_CODE (value) == INTEGER_CST
5626 && !((TREE_CODE (expr) == INTEGER_CST
5627 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5628 || TREE_CODE (expr) == REAL_CST
5629 || TREE_CODE (expr) == COMPLEX_CST)))
5630 value = build1 (NOP_EXPR, type, value);
5632 protected_set_expr_location (value, loc);
5633 return value;
5636 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5637 location of the open paren of the cast, or the position of the cast
5638 expr. */
5639 tree
5640 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5642 tree type;
5643 tree type_expr = NULL_TREE;
5644 bool type_expr_const = true;
5645 tree ret;
5646 int saved_wsp = warn_strict_prototypes;
5648 /* This avoids warnings about unprototyped casts on
5649 integers. E.g. "#define SIG_DFL (void(*)())0". */
5650 if (TREE_CODE (expr) == INTEGER_CST)
5651 warn_strict_prototypes = 0;
5652 type = groktypename (type_name, &type_expr, &type_expr_const);
5653 warn_strict_prototypes = saved_wsp;
5655 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5656 && reject_gcc_builtin (expr))
5657 return error_mark_node;
5659 ret = build_c_cast (loc, type, expr);
5660 if (type_expr)
5662 bool inner_expr_const = true;
5663 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5664 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5665 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5666 && inner_expr_const);
5667 SET_EXPR_LOCATION (ret, loc);
5670 if (!EXPR_HAS_LOCATION (ret))
5671 protected_set_expr_location (ret, loc);
5673 /* C++ does not permits types to be defined in a cast, but it
5674 allows references to incomplete types. */
5675 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5676 warning_at (loc, OPT_Wc___compat,
5677 "defining a type in a cast is invalid in C++");
5679 return ret;
5682 /* Build an assignment expression of lvalue LHS from value RHS.
5683 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5684 may differ from TREE_TYPE (LHS) for an enum bitfield.
5685 MODIFYCODE is the code for a binary operator that we use
5686 to combine the old value of LHS with RHS to get the new value.
5687 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5688 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5689 which may differ from TREE_TYPE (RHS) for an enum value.
5691 LOCATION is the location of the MODIFYCODE operator.
5692 RHS_LOC is the location of the RHS. */
5694 tree
5695 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5696 enum tree_code modifycode,
5697 location_t rhs_loc, tree rhs, tree rhs_origtype)
5699 tree result;
5700 tree newrhs;
5701 tree rhseval = NULL_TREE;
5702 tree rhs_semantic_type = NULL_TREE;
5703 tree lhstype = TREE_TYPE (lhs);
5704 tree olhstype = lhstype;
5705 bool npc;
5706 bool is_atomic_op;
5708 /* Types that aren't fully specified cannot be used in assignments. */
5709 lhs = require_complete_type (location, lhs);
5711 /* Avoid duplicate error messages from operands that had errors. */
5712 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5713 return error_mark_node;
5715 /* Ensure an error for assigning a non-lvalue array to an array in
5716 C90. */
5717 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5719 error_at (location, "assignment to expression with array type");
5720 return error_mark_node;
5723 /* For ObjC properties, defer this check. */
5724 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5725 return error_mark_node;
5727 is_atomic_op = really_atomic_lvalue (lhs);
5729 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5731 rhs_semantic_type = TREE_TYPE (rhs);
5732 rhs = TREE_OPERAND (rhs, 0);
5735 newrhs = rhs;
5737 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5739 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5740 lhs_origtype, modifycode, rhs_loc, rhs,
5741 rhs_origtype);
5742 if (inner == error_mark_node)
5743 return error_mark_node;
5744 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5745 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5746 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5747 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5748 protected_set_expr_location (result, location);
5749 return result;
5752 /* If a binary op has been requested, combine the old LHS value with the RHS
5753 producing the value we should actually store into the LHS. */
5755 if (modifycode != NOP_EXPR)
5757 lhs = c_fully_fold (lhs, false, NULL);
5758 lhs = stabilize_reference (lhs);
5760 /* Construct the RHS for any non-atomic compound assignemnt. */
5761 if (!is_atomic_op)
5763 /* If in LHS op= RHS the RHS has side-effects, ensure they
5764 are preevaluated before the rest of the assignment expression's
5765 side-effects, because RHS could contain e.g. function calls
5766 that modify LHS. */
5767 if (TREE_SIDE_EFFECTS (rhs))
5769 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5770 rhseval = newrhs;
5772 newrhs = build_binary_op (location,
5773 modifycode, lhs, newrhs, 1);
5775 /* The original type of the right hand side is no longer
5776 meaningful. */
5777 rhs_origtype = NULL_TREE;
5781 if (c_dialect_objc ())
5783 /* Check if we are modifying an Objective-C property reference;
5784 if so, we need to generate setter calls. */
5785 result = objc_maybe_build_modify_expr (lhs, newrhs);
5786 if (result)
5787 goto return_result;
5789 /* Else, do the check that we postponed for Objective-C. */
5790 if (!lvalue_or_else (location, lhs, lv_assign))
5791 return error_mark_node;
5794 /* Give an error for storing in something that is 'const'. */
5796 if (TYPE_READONLY (lhstype)
5797 || (RECORD_OR_UNION_TYPE_P (lhstype)
5798 && C_TYPE_FIELDS_READONLY (lhstype)))
5800 readonly_error (location, lhs, lv_assign);
5801 return error_mark_node;
5803 else if (TREE_READONLY (lhs))
5804 readonly_warning (lhs, lv_assign);
5806 /* If storing into a structure or union member,
5807 it has probably been given type `int'.
5808 Compute the type that would go with
5809 the actual amount of storage the member occupies. */
5811 if (TREE_CODE (lhs) == COMPONENT_REF
5812 && (TREE_CODE (lhstype) == INTEGER_TYPE
5813 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5814 || TREE_CODE (lhstype) == REAL_TYPE
5815 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5816 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5818 /* If storing in a field that is in actuality a short or narrower than one,
5819 we must store in the field in its actual type. */
5821 if (lhstype != TREE_TYPE (lhs))
5823 lhs = copy_node (lhs);
5824 TREE_TYPE (lhs) = lhstype;
5827 /* Issue -Wc++-compat warnings about an assignment to an enum type
5828 when LHS does not have its original type. This happens for,
5829 e.g., an enum bitfield in a struct. */
5830 if (warn_cxx_compat
5831 && lhs_origtype != NULL_TREE
5832 && lhs_origtype != lhstype
5833 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5835 tree checktype = (rhs_origtype != NULL_TREE
5836 ? rhs_origtype
5837 : TREE_TYPE (rhs));
5838 if (checktype != error_mark_node
5839 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5840 || (is_atomic_op && modifycode != NOP_EXPR)))
5841 warning_at (location, OPT_Wc___compat,
5842 "enum conversion in assignment is invalid in C++");
5845 /* If the lhs is atomic, remove that qualifier. */
5846 if (is_atomic_op)
5848 lhstype = build_qualified_type (lhstype,
5849 (TYPE_QUALS (lhstype)
5850 & ~TYPE_QUAL_ATOMIC));
5851 olhstype = build_qualified_type (olhstype,
5852 (TYPE_QUALS (lhstype)
5853 & ~TYPE_QUAL_ATOMIC));
5856 /* Convert new value to destination type. Fold it first, then
5857 restore any excess precision information, for the sake of
5858 conversion warnings. */
5860 if (!(is_atomic_op && modifycode != NOP_EXPR))
5862 npc = null_pointer_constant_p (newrhs);
5863 newrhs = c_fully_fold (newrhs, false, NULL);
5864 if (rhs_semantic_type)
5865 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5866 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5867 rhs_origtype, ic_assign, npc,
5868 NULL_TREE, NULL_TREE, 0);
5869 if (TREE_CODE (newrhs) == ERROR_MARK)
5870 return error_mark_node;
5873 /* Emit ObjC write barrier, if necessary. */
5874 if (c_dialect_objc () && flag_objc_gc)
5876 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5877 if (result)
5879 protected_set_expr_location (result, location);
5880 goto return_result;
5884 /* Scan operands. */
5886 if (is_atomic_op)
5887 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5888 else
5890 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5891 TREE_SIDE_EFFECTS (result) = 1;
5892 protected_set_expr_location (result, location);
5895 /* If we got the LHS in a different type for storing in,
5896 convert the result back to the nominal type of LHS
5897 so that the value we return always has the same type
5898 as the LHS argument. */
5900 if (olhstype == TREE_TYPE (result))
5901 goto return_result;
5903 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5904 rhs_origtype, ic_assign, false, NULL_TREE,
5905 NULL_TREE, 0);
5906 protected_set_expr_location (result, location);
5908 return_result:
5909 if (rhseval)
5910 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5911 return result;
5914 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5915 This is used to implement -fplan9-extensions. */
5917 static bool
5918 find_anonymous_field_with_type (tree struct_type, tree type)
5920 tree field;
5921 bool found;
5923 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5924 found = false;
5925 for (field = TYPE_FIELDS (struct_type);
5926 field != NULL_TREE;
5927 field = TREE_CHAIN (field))
5929 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5930 ? c_build_qualified_type (TREE_TYPE (field),
5931 TYPE_QUAL_ATOMIC)
5932 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5933 if (DECL_NAME (field) == NULL
5934 && comptypes (type, fieldtype))
5936 if (found)
5937 return false;
5938 found = true;
5940 else if (DECL_NAME (field) == NULL
5941 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5942 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5944 if (found)
5945 return false;
5946 found = true;
5949 return found;
5952 /* RHS is an expression whose type is pointer to struct. If there is
5953 an anonymous field in RHS with type TYPE, then return a pointer to
5954 that field in RHS. This is used with -fplan9-extensions. This
5955 returns NULL if no conversion could be found. */
5957 static tree
5958 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5960 tree rhs_struct_type, lhs_main_type;
5961 tree field, found_field;
5962 bool found_sub_field;
5963 tree ret;
5965 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5966 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5967 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5969 gcc_assert (POINTER_TYPE_P (type));
5970 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5971 ? c_build_qualified_type (TREE_TYPE (type),
5972 TYPE_QUAL_ATOMIC)
5973 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5975 found_field = NULL_TREE;
5976 found_sub_field = false;
5977 for (field = TYPE_FIELDS (rhs_struct_type);
5978 field != NULL_TREE;
5979 field = TREE_CHAIN (field))
5981 if (DECL_NAME (field) != NULL_TREE
5982 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
5983 continue;
5984 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5985 ? c_build_qualified_type (TREE_TYPE (field),
5986 TYPE_QUAL_ATOMIC)
5987 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5988 if (comptypes (lhs_main_type, fieldtype))
5990 if (found_field != NULL_TREE)
5991 return NULL_TREE;
5992 found_field = field;
5994 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5995 lhs_main_type))
5997 if (found_field != NULL_TREE)
5998 return NULL_TREE;
5999 found_field = field;
6000 found_sub_field = true;
6004 if (found_field == NULL_TREE)
6005 return NULL_TREE;
6007 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6008 build_fold_indirect_ref (rhs), found_field,
6009 NULL_TREE);
6010 ret = build_fold_addr_expr_loc (location, ret);
6012 if (found_sub_field)
6014 ret = convert_to_anonymous_field (location, type, ret);
6015 gcc_assert (ret != NULL_TREE);
6018 return ret;
6021 /* Issue an error message for a bad initializer component.
6022 GMSGID identifies the message.
6023 The component name is taken from the spelling stack. */
6025 static void
6026 error_init (location_t loc, const char *gmsgid)
6028 char *ofwhat;
6030 /* The gmsgid may be a format string with %< and %>. */
6031 error_at (loc, gmsgid);
6032 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6033 if (*ofwhat)
6034 inform (loc, "(near initialization for %qs)", ofwhat);
6037 /* Issue a pedantic warning for a bad initializer component. OPT is
6038 the option OPT_* (from options.h) controlling this warning or 0 if
6039 it is unconditionally given. GMSGID identifies the message. The
6040 component name is taken from the spelling stack. */
6042 static void
6043 pedwarn_init (location_t loc, int opt, const char *gmsgid)
6045 char *ofwhat;
6046 bool warned;
6048 /* Use the location where a macro was expanded rather than where
6049 it was defined to make sure macros defined in system headers
6050 but used incorrectly elsewhere are diagnosed. */
6051 source_location exploc = expansion_point_location_if_in_system_header (loc);
6053 /* The gmsgid may be a format string with %< and %>. */
6054 warned = pedwarn (exploc, opt, gmsgid);
6055 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6056 if (*ofwhat && warned)
6057 inform (exploc, "(near initialization for %qs)", ofwhat);
6060 /* Issue a warning for a bad initializer component.
6062 OPT is the OPT_W* value corresponding to the warning option that
6063 controls this warning. GMSGID identifies the message. The
6064 component name is taken from the spelling stack. */
6066 static void
6067 warning_init (location_t loc, int opt, const char *gmsgid)
6069 char *ofwhat;
6070 bool warned;
6072 /* Use the location where a macro was expanded rather than where
6073 it was defined to make sure macros defined in system headers
6074 but used incorrectly elsewhere are diagnosed. */
6075 source_location exploc = expansion_point_location_if_in_system_header (loc);
6077 /* The gmsgid may be a format string with %< and %>. */
6078 warned = warning_at (exploc, opt, gmsgid);
6079 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6080 if (*ofwhat && warned)
6081 inform (exploc, "(near initialization for %qs)", ofwhat);
6084 /* If TYPE is an array type and EXPR is a parenthesized string
6085 constant, warn if pedantic that EXPR is being used to initialize an
6086 object of type TYPE. */
6088 void
6089 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6091 if (pedantic
6092 && TREE_CODE (type) == ARRAY_TYPE
6093 && TREE_CODE (expr.value) == STRING_CST
6094 && expr.original_code != STRING_CST)
6095 pedwarn_init (loc, OPT_Wpedantic,
6096 "array initialized from parenthesized string constant");
6099 /* Convert value RHS to type TYPE as preparation for an assignment to
6100 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6101 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6102 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6103 constant before any folding.
6104 The real work of conversion is done by `convert'.
6105 The purpose of this function is to generate error messages
6106 for assignments that are not allowed in C.
6107 ERRTYPE says whether it is argument passing, assignment,
6108 initialization or return.
6110 In the following example, '~' denotes where EXPR_LOC and '^' where
6111 LOCATION point to:
6113 f (var); [ic_argpass]
6114 ^ ~~~
6115 x = var; [ic_assign]
6116 ^ ~~~;
6117 int x = var; [ic_init]
6119 return x; [ic_return]
6122 FUNCTION is a tree for the function being called.
6123 PARMNUM is the number of the argument, for printing in error messages. */
6125 static tree
6126 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6127 tree rhs, tree origtype, enum impl_conv errtype,
6128 bool null_pointer_constant, tree fundecl,
6129 tree function, int parmnum)
6131 enum tree_code codel = TREE_CODE (type);
6132 tree orig_rhs = rhs;
6133 tree rhstype;
6134 enum tree_code coder;
6135 tree rname = NULL_TREE;
6136 bool objc_ok = false;
6138 /* Use the expansion point location to handle cases such as user's
6139 function returning a wrong-type macro defined in a system header. */
6140 location = expansion_point_location_if_in_system_header (location);
6142 if (errtype == ic_argpass)
6144 tree selector;
6145 /* Change pointer to function to the function itself for
6146 diagnostics. */
6147 if (TREE_CODE (function) == ADDR_EXPR
6148 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6149 function = TREE_OPERAND (function, 0);
6151 /* Handle an ObjC selector specially for diagnostics. */
6152 selector = objc_message_selector ();
6153 rname = function;
6154 if (selector && parmnum > 2)
6156 rname = selector;
6157 parmnum -= 2;
6161 /* This macro is used to emit diagnostics to ensure that all format
6162 strings are complete sentences, visible to gettext and checked at
6163 compile time. */
6164 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6165 do { \
6166 switch (errtype) \
6168 case ic_argpass: \
6169 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6170 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6171 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6172 "expected %qT but argument is of type %qT", \
6173 type, rhstype); \
6174 break; \
6175 case ic_assign: \
6176 pedwarn (LOCATION, OPT, AS); \
6177 break; \
6178 case ic_init: \
6179 pedwarn_init (LOCATION, OPT, IN); \
6180 break; \
6181 case ic_return: \
6182 pedwarn (LOCATION, OPT, RE); \
6183 break; \
6184 default: \
6185 gcc_unreachable (); \
6187 } while (0)
6189 /* This macro is used to emit diagnostics to ensure that all format
6190 strings are complete sentences, visible to gettext and checked at
6191 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6192 extra parameter to enumerate qualifiers. */
6193 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6194 do { \
6195 switch (errtype) \
6197 case ic_argpass: \
6198 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6199 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6200 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6201 "expected %qT but argument is of type %qT", \
6202 type, rhstype); \
6203 break; \
6204 case ic_assign: \
6205 pedwarn (LOCATION, OPT, AS, QUALS); \
6206 break; \
6207 case ic_init: \
6208 pedwarn (LOCATION, OPT, IN, QUALS); \
6209 break; \
6210 case ic_return: \
6211 pedwarn (LOCATION, OPT, RE, QUALS); \
6212 break; \
6213 default: \
6214 gcc_unreachable (); \
6216 } while (0)
6218 /* This macro is used to emit diagnostics to ensure that all format
6219 strings are complete sentences, visible to gettext and checked at
6220 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6221 warning_at instead of pedwarn. */
6222 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6223 do { \
6224 switch (errtype) \
6226 case ic_argpass: \
6227 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6228 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6229 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6230 "expected %qT but argument is of type %qT", \
6231 type, rhstype); \
6232 break; \
6233 case ic_assign: \
6234 warning_at (LOCATION, OPT, AS, QUALS); \
6235 break; \
6236 case ic_init: \
6237 warning_at (LOCATION, OPT, IN, QUALS); \
6238 break; \
6239 case ic_return: \
6240 warning_at (LOCATION, OPT, RE, QUALS); \
6241 break; \
6242 default: \
6243 gcc_unreachable (); \
6245 } while (0)
6247 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6248 rhs = TREE_OPERAND (rhs, 0);
6250 rhstype = TREE_TYPE (rhs);
6251 coder = TREE_CODE (rhstype);
6253 if (coder == ERROR_MARK)
6254 return error_mark_node;
6256 if (c_dialect_objc ())
6258 int parmno;
6260 switch (errtype)
6262 case ic_return:
6263 parmno = 0;
6264 break;
6266 case ic_assign:
6267 parmno = -1;
6268 break;
6270 case ic_init:
6271 parmno = -2;
6272 break;
6274 default:
6275 parmno = parmnum;
6276 break;
6279 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6282 if (warn_cxx_compat)
6284 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6285 if (checktype != error_mark_node
6286 && TREE_CODE (type) == ENUMERAL_TYPE
6287 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6289 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6290 G_("enum conversion when passing argument "
6291 "%d of %qE is invalid in C++"),
6292 G_("enum conversion in assignment is "
6293 "invalid in C++"),
6294 G_("enum conversion in initialization is "
6295 "invalid in C++"),
6296 G_("enum conversion in return is "
6297 "invalid in C++"));
6301 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6302 return rhs;
6304 if (coder == VOID_TYPE)
6306 /* Except for passing an argument to an unprototyped function,
6307 this is a constraint violation. When passing an argument to
6308 an unprototyped function, it is compile-time undefined;
6309 making it a constraint in that case was rejected in
6310 DR#252. */
6311 error_at (location, "void value not ignored as it ought to be");
6312 return error_mark_node;
6314 rhs = require_complete_type (location, rhs);
6315 if (rhs == error_mark_node)
6316 return error_mark_node;
6318 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6319 return error_mark_node;
6321 /* A non-reference type can convert to a reference. This handles
6322 va_start, va_copy and possibly port built-ins. */
6323 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6325 if (!lvalue_p (rhs))
6327 error_at (location, "cannot pass rvalue to reference parameter");
6328 return error_mark_node;
6330 if (!c_mark_addressable (rhs))
6331 return error_mark_node;
6332 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6333 SET_EXPR_LOCATION (rhs, location);
6335 rhs = convert_for_assignment (location, expr_loc,
6336 build_pointer_type (TREE_TYPE (type)),
6337 rhs, origtype, errtype,
6338 null_pointer_constant, fundecl, function,
6339 parmnum);
6340 if (rhs == error_mark_node)
6341 return error_mark_node;
6343 rhs = build1 (NOP_EXPR, type, rhs);
6344 SET_EXPR_LOCATION (rhs, location);
6345 return rhs;
6347 /* Some types can interconvert without explicit casts. */
6348 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6349 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6350 return convert (type, rhs);
6351 /* Arithmetic types all interconvert, and enum is treated like int. */
6352 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6353 || codel == FIXED_POINT_TYPE
6354 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6355 || codel == BOOLEAN_TYPE)
6356 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6357 || coder == FIXED_POINT_TYPE
6358 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6359 || coder == BOOLEAN_TYPE))
6361 tree ret;
6362 bool save = in_late_binary_op;
6363 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6364 || (coder == REAL_TYPE
6365 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6366 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
6367 in_late_binary_op = true;
6368 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6369 ? expr_loc : location, type, orig_rhs);
6370 in_late_binary_op = save;
6371 return ret;
6374 /* Aggregates in different TUs might need conversion. */
6375 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6376 && codel == coder
6377 && comptypes (type, rhstype))
6378 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6379 ? expr_loc : location, type, rhs);
6381 /* Conversion to a transparent union or record from its member types.
6382 This applies only to function arguments. */
6383 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6384 && TYPE_TRANSPARENT_AGGR (type))
6385 && errtype == ic_argpass)
6387 tree memb, marginal_memb = NULL_TREE;
6389 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6391 tree memb_type = TREE_TYPE (memb);
6393 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6394 TYPE_MAIN_VARIANT (rhstype)))
6395 break;
6397 if (TREE_CODE (memb_type) != POINTER_TYPE)
6398 continue;
6400 if (coder == POINTER_TYPE)
6402 tree ttl = TREE_TYPE (memb_type);
6403 tree ttr = TREE_TYPE (rhstype);
6405 /* Any non-function converts to a [const][volatile] void *
6406 and vice versa; otherwise, targets must be the same.
6407 Meanwhile, the lhs target must have all the qualifiers of
6408 the rhs. */
6409 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6410 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6411 || comp_target_types (location, memb_type, rhstype))
6413 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6414 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6415 /* If this type won't generate any warnings, use it. */
6416 if (lquals == rquals
6417 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6418 && TREE_CODE (ttl) == FUNCTION_TYPE)
6419 ? ((lquals | rquals) == rquals)
6420 : ((lquals | rquals) == lquals)))
6421 break;
6423 /* Keep looking for a better type, but remember this one. */
6424 if (!marginal_memb)
6425 marginal_memb = memb;
6429 /* Can convert integer zero to any pointer type. */
6430 if (null_pointer_constant)
6432 rhs = null_pointer_node;
6433 break;
6437 if (memb || marginal_memb)
6439 if (!memb)
6441 /* We have only a marginally acceptable member type;
6442 it needs a warning. */
6443 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6444 tree ttr = TREE_TYPE (rhstype);
6446 /* Const and volatile mean something different for function
6447 types, so the usual warnings are not appropriate. */
6448 if (TREE_CODE (ttr) == FUNCTION_TYPE
6449 && TREE_CODE (ttl) == FUNCTION_TYPE)
6451 /* Because const and volatile on functions are
6452 restrictions that say the function will not do
6453 certain things, it is okay to use a const or volatile
6454 function where an ordinary one is wanted, but not
6455 vice-versa. */
6456 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6457 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6458 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6459 OPT_Wdiscarded_qualifiers,
6460 G_("passing argument %d of %qE "
6461 "makes %q#v qualified function "
6462 "pointer from unqualified"),
6463 G_("assignment makes %q#v qualified "
6464 "function pointer from "
6465 "unqualified"),
6466 G_("initialization makes %q#v qualified "
6467 "function pointer from "
6468 "unqualified"),
6469 G_("return makes %q#v qualified function "
6470 "pointer from unqualified"),
6471 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6473 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6474 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6475 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6476 OPT_Wdiscarded_qualifiers,
6477 G_("passing argument %d of %qE discards "
6478 "%qv qualifier from pointer target type"),
6479 G_("assignment discards %qv qualifier "
6480 "from pointer target type"),
6481 G_("initialization discards %qv qualifier "
6482 "from pointer target type"),
6483 G_("return discards %qv qualifier from "
6484 "pointer target type"),
6485 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6487 memb = marginal_memb;
6490 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6491 pedwarn (location, OPT_Wpedantic,
6492 "ISO C prohibits argument conversion to union type");
6494 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6495 return build_constructor_single (type, memb, rhs);
6499 /* Conversions among pointers */
6500 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6501 && (coder == codel))
6503 tree ttl = TREE_TYPE (type);
6504 tree ttr = TREE_TYPE (rhstype);
6505 tree mvl = ttl;
6506 tree mvr = ttr;
6507 bool is_opaque_pointer;
6508 int target_cmp = 0; /* Cache comp_target_types () result. */
6509 addr_space_t asl;
6510 addr_space_t asr;
6512 if (TREE_CODE (mvl) != ARRAY_TYPE)
6513 mvl = (TYPE_ATOMIC (mvl)
6514 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6515 TYPE_QUAL_ATOMIC)
6516 : TYPE_MAIN_VARIANT (mvl));
6517 if (TREE_CODE (mvr) != ARRAY_TYPE)
6518 mvr = (TYPE_ATOMIC (mvr)
6519 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6520 TYPE_QUAL_ATOMIC)
6521 : TYPE_MAIN_VARIANT (mvr));
6522 /* Opaque pointers are treated like void pointers. */
6523 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6525 /* The Plan 9 compiler permits a pointer to a struct to be
6526 automatically converted into a pointer to an anonymous field
6527 within the struct. */
6528 if (flag_plan9_extensions
6529 && RECORD_OR_UNION_TYPE_P (mvl)
6530 && RECORD_OR_UNION_TYPE_P (mvr)
6531 && mvl != mvr)
6533 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6534 if (new_rhs != NULL_TREE)
6536 rhs = new_rhs;
6537 rhstype = TREE_TYPE (rhs);
6538 coder = TREE_CODE (rhstype);
6539 ttr = TREE_TYPE (rhstype);
6540 mvr = TYPE_MAIN_VARIANT (ttr);
6544 /* C++ does not allow the implicit conversion void* -> T*. However,
6545 for the purpose of reducing the number of false positives, we
6546 tolerate the special case of
6548 int *p = NULL;
6550 where NULL is typically defined in C to be '(void *) 0'. */
6551 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6552 warning_at (errtype == ic_argpass ? expr_loc : location,
6553 OPT_Wc___compat,
6554 "request for implicit conversion "
6555 "from %qT to %qT not permitted in C++", rhstype, type);
6557 /* See if the pointers point to incompatible address spaces. */
6558 asl = TYPE_ADDR_SPACE (ttl);
6559 asr = TYPE_ADDR_SPACE (ttr);
6560 if (!null_pointer_constant_p (rhs)
6561 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6563 switch (errtype)
6565 case ic_argpass:
6566 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6567 "non-enclosed address space", parmnum, rname);
6568 break;
6569 case ic_assign:
6570 error_at (location, "assignment from pointer to "
6571 "non-enclosed address space");
6572 break;
6573 case ic_init:
6574 error_at (location, "initialization from pointer to "
6575 "non-enclosed address space");
6576 break;
6577 case ic_return:
6578 error_at (location, "return from pointer to "
6579 "non-enclosed address space");
6580 break;
6581 default:
6582 gcc_unreachable ();
6584 return error_mark_node;
6587 /* Check if the right-hand side has a format attribute but the
6588 left-hand side doesn't. */
6589 if (warn_suggest_attribute_format
6590 && check_missing_format_attribute (type, rhstype))
6592 switch (errtype)
6594 case ic_argpass:
6595 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6596 "argument %d of %qE might be "
6597 "a candidate for a format attribute",
6598 parmnum, rname);
6599 break;
6600 case ic_assign:
6601 warning_at (location, OPT_Wsuggest_attribute_format,
6602 "assignment left-hand side might be "
6603 "a candidate for a format attribute");
6604 break;
6605 case ic_init:
6606 warning_at (location, OPT_Wsuggest_attribute_format,
6607 "initialization left-hand side might be "
6608 "a candidate for a format attribute");
6609 break;
6610 case ic_return:
6611 warning_at (location, OPT_Wsuggest_attribute_format,
6612 "return type might be "
6613 "a candidate for a format attribute");
6614 break;
6615 default:
6616 gcc_unreachable ();
6620 /* Any non-function converts to a [const][volatile] void *
6621 and vice versa; otherwise, targets must be the same.
6622 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6623 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6624 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6625 || (target_cmp = comp_target_types (location, type, rhstype))
6626 || is_opaque_pointer
6627 || ((c_common_unsigned_type (mvl)
6628 == c_common_unsigned_type (mvr))
6629 && (c_common_signed_type (mvl)
6630 == c_common_signed_type (mvr))
6631 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6633 /* Warn about loss of qualifers from pointers to arrays with
6634 qualifiers on the element type. */
6635 if (TREE_CODE (ttr) == ARRAY_TYPE)
6637 ttr = strip_array_types (ttr);
6638 ttl = strip_array_types (ttl);
6640 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6641 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6642 WARNING_FOR_QUALIFIERS (location, expr_loc,
6643 OPT_Wdiscarded_array_qualifiers,
6644 G_("passing argument %d of %qE discards "
6645 "%qv qualifier from pointer target type"),
6646 G_("assignment discards %qv qualifier "
6647 "from pointer target type"),
6648 G_("initialization discards %qv qualifier "
6649 "from pointer target type"),
6650 G_("return discards %qv qualifier from "
6651 "pointer target type"),
6652 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6654 else if (pedantic
6655 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6657 (VOID_TYPE_P (ttr)
6658 && !null_pointer_constant
6659 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6660 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6661 G_("ISO C forbids passing argument %d of "
6662 "%qE between function pointer "
6663 "and %<void *%>"),
6664 G_("ISO C forbids assignment between "
6665 "function pointer and %<void *%>"),
6666 G_("ISO C forbids initialization between "
6667 "function pointer and %<void *%>"),
6668 G_("ISO C forbids return between function "
6669 "pointer and %<void *%>"));
6670 /* Const and volatile mean something different for function types,
6671 so the usual warnings are not appropriate. */
6672 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6673 && TREE_CODE (ttl) != FUNCTION_TYPE)
6675 /* Don't warn about loss of qualifier for conversions from
6676 qualified void* to pointers to arrays with corresponding
6677 qualifier on the element type. */
6678 if (!pedantic)
6679 ttl = strip_array_types (ttl);
6681 /* Assignments between atomic and non-atomic objects are OK. */
6682 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6683 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6685 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6686 OPT_Wdiscarded_qualifiers,
6687 G_("passing argument %d of %qE discards "
6688 "%qv qualifier from pointer target type"),
6689 G_("assignment discards %qv qualifier "
6690 "from pointer target type"),
6691 G_("initialization discards %qv qualifier "
6692 "from pointer target type"),
6693 G_("return discards %qv qualifier from "
6694 "pointer target type"),
6695 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6697 /* If this is not a case of ignoring a mismatch in signedness,
6698 no warning. */
6699 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6700 || target_cmp)
6702 /* If there is a mismatch, do warn. */
6703 else if (warn_pointer_sign)
6704 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6705 G_("pointer targets in passing argument "
6706 "%d of %qE differ in signedness"),
6707 G_("pointer targets in assignment "
6708 "differ in signedness"),
6709 G_("pointer targets in initialization "
6710 "differ in signedness"),
6711 G_("pointer targets in return differ "
6712 "in signedness"));
6714 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6715 && TREE_CODE (ttr) == FUNCTION_TYPE)
6717 /* Because const and volatile on functions are restrictions
6718 that say the function will not do certain things,
6719 it is okay to use a const or volatile function
6720 where an ordinary one is wanted, but not vice-versa. */
6721 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6722 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6723 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6724 OPT_Wdiscarded_qualifiers,
6725 G_("passing argument %d of %qE makes "
6726 "%q#v qualified function pointer "
6727 "from unqualified"),
6728 G_("assignment makes %q#v qualified function "
6729 "pointer from unqualified"),
6730 G_("initialization makes %q#v qualified "
6731 "function pointer from unqualified"),
6732 G_("return makes %q#v qualified function "
6733 "pointer from unqualified"),
6734 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6737 else
6738 /* Avoid warning about the volatile ObjC EH puts on decls. */
6739 if (!objc_ok)
6740 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6741 OPT_Wincompatible_pointer_types,
6742 G_("passing argument %d of %qE from "
6743 "incompatible pointer type"),
6744 G_("assignment from incompatible pointer type"),
6745 G_("initialization from incompatible "
6746 "pointer type"),
6747 G_("return from incompatible pointer type"));
6749 return convert (type, rhs);
6751 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6753 /* ??? This should not be an error when inlining calls to
6754 unprototyped functions. */
6755 error_at (location, "invalid use of non-lvalue array");
6756 return error_mark_node;
6758 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6760 /* An explicit constant 0 can convert to a pointer,
6761 or one that results from arithmetic, even including
6762 a cast to integer type. */
6763 if (!null_pointer_constant)
6764 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6765 OPT_Wint_conversion,
6766 G_("passing argument %d of %qE makes "
6767 "pointer from integer without a cast"),
6768 G_("assignment makes pointer from integer "
6769 "without a cast"),
6770 G_("initialization makes pointer from "
6771 "integer without a cast"),
6772 G_("return makes pointer from integer "
6773 "without a cast"));
6775 return convert (type, rhs);
6777 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6779 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6780 OPT_Wint_conversion,
6781 G_("passing argument %d of %qE makes integer "
6782 "from pointer without a cast"),
6783 G_("assignment makes integer from pointer "
6784 "without a cast"),
6785 G_("initialization makes integer from pointer "
6786 "without a cast"),
6787 G_("return makes integer from pointer "
6788 "without a cast"));
6789 return convert (type, rhs);
6791 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6793 tree ret;
6794 bool save = in_late_binary_op;
6795 in_late_binary_op = true;
6796 ret = convert (type, rhs);
6797 in_late_binary_op = save;
6798 return ret;
6801 switch (errtype)
6803 case ic_argpass:
6804 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6805 rname);
6806 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6807 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6808 "expected %qT but argument is of type %qT", type, rhstype);
6809 break;
6810 case ic_assign:
6811 error_at (location, "incompatible types when assigning to type %qT from "
6812 "type %qT", type, rhstype);
6813 break;
6814 case ic_init:
6815 error_at (location,
6816 "incompatible types when initializing type %qT using type %qT",
6817 type, rhstype);
6818 break;
6819 case ic_return:
6820 error_at (location,
6821 "incompatible types when returning type %qT but %qT was "
6822 "expected", rhstype, type);
6823 break;
6824 default:
6825 gcc_unreachable ();
6828 return error_mark_node;
6831 /* If VALUE is a compound expr all of whose expressions are constant, then
6832 return its value. Otherwise, return error_mark_node.
6834 This is for handling COMPOUND_EXPRs as initializer elements
6835 which is allowed with a warning when -pedantic is specified. */
6837 static tree
6838 valid_compound_expr_initializer (tree value, tree endtype)
6840 if (TREE_CODE (value) == COMPOUND_EXPR)
6842 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6843 == error_mark_node)
6844 return error_mark_node;
6845 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6846 endtype);
6848 else if (!initializer_constant_valid_p (value, endtype))
6849 return error_mark_node;
6850 else
6851 return value;
6854 /* Perform appropriate conversions on the initial value of a variable,
6855 store it in the declaration DECL,
6856 and print any error messages that are appropriate.
6857 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6858 If the init is invalid, store an ERROR_MARK.
6860 INIT_LOC is the location of the initial value. */
6862 void
6863 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6865 tree value, type;
6866 bool npc = false;
6868 /* If variable's type was invalidly declared, just ignore it. */
6870 type = TREE_TYPE (decl);
6871 if (TREE_CODE (type) == ERROR_MARK)
6872 return;
6874 /* Digest the specified initializer into an expression. */
6876 if (init)
6877 npc = null_pointer_constant_p (init);
6878 value = digest_init (init_loc, type, init, origtype, npc,
6879 true, TREE_STATIC (decl));
6881 /* Store the expression if valid; else report error. */
6883 if (!in_system_header_at (input_location)
6884 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6885 warning (OPT_Wtraditional, "traditional C rejects automatic "
6886 "aggregate initialization");
6888 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6889 DECL_INITIAL (decl) = value;
6891 /* ANSI wants warnings about out-of-range constant initializers. */
6892 STRIP_TYPE_NOPS (value);
6893 if (TREE_STATIC (decl))
6894 constant_expression_warning (value);
6896 /* Check if we need to set array size from compound literal size. */
6897 if (TREE_CODE (type) == ARRAY_TYPE
6898 && TYPE_DOMAIN (type) == 0
6899 && value != error_mark_node)
6901 tree inside_init = init;
6903 STRIP_TYPE_NOPS (inside_init);
6904 inside_init = fold (inside_init);
6906 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6908 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6910 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6912 /* For int foo[] = (int [3]){1}; we need to set array size
6913 now since later on array initializer will be just the
6914 brace enclosed list of the compound literal. */
6915 tree etype = strip_array_types (TREE_TYPE (decl));
6916 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6917 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6918 layout_type (type);
6919 layout_decl (cldecl, 0);
6920 TREE_TYPE (decl)
6921 = c_build_qualified_type (type, TYPE_QUALS (etype));
6927 /* Methods for storing and printing names for error messages. */
6929 /* Implement a spelling stack that allows components of a name to be pushed
6930 and popped. Each element on the stack is this structure. */
6932 struct spelling
6934 int kind;
6935 union
6937 unsigned HOST_WIDE_INT i;
6938 const char *s;
6939 } u;
6942 #define SPELLING_STRING 1
6943 #define SPELLING_MEMBER 2
6944 #define SPELLING_BOUNDS 3
6946 static struct spelling *spelling; /* Next stack element (unused). */
6947 static struct spelling *spelling_base; /* Spelling stack base. */
6948 static int spelling_size; /* Size of the spelling stack. */
6950 /* Macros to save and restore the spelling stack around push_... functions.
6951 Alternative to SAVE_SPELLING_STACK. */
6953 #define SPELLING_DEPTH() (spelling - spelling_base)
6954 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6956 /* Push an element on the spelling stack with type KIND and assign VALUE
6957 to MEMBER. */
6959 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6961 int depth = SPELLING_DEPTH (); \
6963 if (depth >= spelling_size) \
6965 spelling_size += 10; \
6966 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6967 spelling_size); \
6968 RESTORE_SPELLING_DEPTH (depth); \
6971 spelling->kind = (KIND); \
6972 spelling->MEMBER = (VALUE); \
6973 spelling++; \
6976 /* Push STRING on the stack. Printed literally. */
6978 static void
6979 push_string (const char *string)
6981 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6984 /* Push a member name on the stack. Printed as '.' STRING. */
6986 static void
6987 push_member_name (tree decl)
6989 const char *const string
6990 = (DECL_NAME (decl)
6991 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6992 : _("<anonymous>"));
6993 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6996 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6998 static void
6999 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7001 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7004 /* Compute the maximum size in bytes of the printed spelling. */
7006 static int
7007 spelling_length (void)
7009 int size = 0;
7010 struct spelling *p;
7012 for (p = spelling_base; p < spelling; p++)
7014 if (p->kind == SPELLING_BOUNDS)
7015 size += 25;
7016 else
7017 size += strlen (p->u.s) + 1;
7020 return size;
7023 /* Print the spelling to BUFFER and return it. */
7025 static char *
7026 print_spelling (char *buffer)
7028 char *d = buffer;
7029 struct spelling *p;
7031 for (p = spelling_base; p < spelling; p++)
7032 if (p->kind == SPELLING_BOUNDS)
7034 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7035 d += strlen (d);
7037 else
7039 const char *s;
7040 if (p->kind == SPELLING_MEMBER)
7041 *d++ = '.';
7042 for (s = p->u.s; (*d = *s++); d++)
7045 *d++ = '\0';
7046 return buffer;
7049 /* Digest the parser output INIT as an initializer for type TYPE.
7050 Return a C expression of type TYPE to represent the initial value.
7052 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7054 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7056 If INIT is a string constant, STRICT_STRING is true if it is
7057 unparenthesized or we should not warn here for it being parenthesized.
7058 For other types of INIT, STRICT_STRING is not used.
7060 INIT_LOC is the location of the INIT.
7062 REQUIRE_CONSTANT requests an error if non-constant initializers or
7063 elements are seen. */
7065 static tree
7066 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7067 bool null_pointer_constant, bool strict_string,
7068 int require_constant)
7070 enum tree_code code = TREE_CODE (type);
7071 tree inside_init = init;
7072 tree semantic_type = NULL_TREE;
7073 bool maybe_const = true;
7075 if (type == error_mark_node
7076 || !init
7077 || error_operand_p (init))
7078 return error_mark_node;
7080 STRIP_TYPE_NOPS (inside_init);
7082 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7084 semantic_type = TREE_TYPE (inside_init);
7085 inside_init = TREE_OPERAND (inside_init, 0);
7087 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7088 inside_init = decl_constant_value_for_optimization (inside_init);
7090 /* Initialization of an array of chars from a string constant
7091 optionally enclosed in braces. */
7093 if (code == ARRAY_TYPE && inside_init
7094 && TREE_CODE (inside_init) == STRING_CST)
7096 tree typ1
7097 = (TYPE_ATOMIC (TREE_TYPE (type))
7098 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7099 TYPE_QUAL_ATOMIC)
7100 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7101 /* Note that an array could be both an array of character type
7102 and an array of wchar_t if wchar_t is signed char or unsigned
7103 char. */
7104 bool char_array = (typ1 == char_type_node
7105 || typ1 == signed_char_type_node
7106 || typ1 == unsigned_char_type_node);
7107 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7108 bool char16_array = !!comptypes (typ1, char16_type_node);
7109 bool char32_array = !!comptypes (typ1, char32_type_node);
7111 if (char_array || wchar_array || char16_array || char32_array)
7113 struct c_expr expr;
7114 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7115 expr.value = inside_init;
7116 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7117 expr.original_type = NULL;
7118 maybe_warn_string_init (init_loc, type, expr);
7120 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7121 pedwarn_init (init_loc, OPT_Wpedantic,
7122 "initialization of a flexible array member");
7124 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7125 TYPE_MAIN_VARIANT (type)))
7126 return inside_init;
7128 if (char_array)
7130 if (typ2 != char_type_node)
7132 error_init (init_loc, "char-array initialized from wide "
7133 "string");
7134 return error_mark_node;
7137 else
7139 if (typ2 == char_type_node)
7141 error_init (init_loc, "wide character array initialized "
7142 "from non-wide string");
7143 return error_mark_node;
7145 else if (!comptypes(typ1, typ2))
7147 error_init (init_loc, "wide character array initialized "
7148 "from incompatible wide string");
7149 return error_mark_node;
7153 TREE_TYPE (inside_init) = type;
7154 if (TYPE_DOMAIN (type) != 0
7155 && TYPE_SIZE (type) != 0
7156 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7158 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7160 /* Subtract the size of a single (possibly wide) character
7161 because it's ok to ignore the terminating null char
7162 that is counted in the length of the constant. */
7163 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7164 (len
7165 - (TYPE_PRECISION (typ1)
7166 / BITS_PER_UNIT))))
7167 pedwarn_init (init_loc, 0,
7168 ("initializer-string for array of chars "
7169 "is too long"));
7170 else if (warn_cxx_compat
7171 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7172 warning_at (init_loc, OPT_Wc___compat,
7173 ("initializer-string for array chars "
7174 "is too long for C++"));
7177 return inside_init;
7179 else if (INTEGRAL_TYPE_P (typ1))
7181 error_init (init_loc, "array of inappropriate type initialized "
7182 "from string constant");
7183 return error_mark_node;
7187 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7188 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7189 below and handle as a constructor. */
7190 if (code == VECTOR_TYPE
7191 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7192 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7193 && TREE_CONSTANT (inside_init))
7195 if (TREE_CODE (inside_init) == VECTOR_CST
7196 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7197 TYPE_MAIN_VARIANT (type)))
7198 return inside_init;
7200 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7202 unsigned HOST_WIDE_INT ix;
7203 tree value;
7204 bool constant_p = true;
7206 /* Iterate through elements and check if all constructor
7207 elements are *_CSTs. */
7208 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7209 if (!CONSTANT_CLASS_P (value))
7211 constant_p = false;
7212 break;
7215 if (constant_p)
7216 return build_vector_from_ctor (type,
7217 CONSTRUCTOR_ELTS (inside_init));
7221 if (warn_sequence_point)
7222 verify_sequence_points (inside_init);
7224 /* Any type can be initialized
7225 from an expression of the same type, optionally with braces. */
7227 if (inside_init && TREE_TYPE (inside_init) != 0
7228 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7229 TYPE_MAIN_VARIANT (type))
7230 || (code == ARRAY_TYPE
7231 && comptypes (TREE_TYPE (inside_init), type))
7232 || (code == VECTOR_TYPE
7233 && comptypes (TREE_TYPE (inside_init), type))
7234 || (code == POINTER_TYPE
7235 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7236 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7237 TREE_TYPE (type)))))
7239 if (code == POINTER_TYPE)
7241 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7243 if (TREE_CODE (inside_init) == STRING_CST
7244 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7245 inside_init = array_to_pointer_conversion
7246 (init_loc, inside_init);
7247 else
7249 error_init (init_loc, "invalid use of non-lvalue array");
7250 return error_mark_node;
7255 if (code == VECTOR_TYPE)
7256 /* Although the types are compatible, we may require a
7257 conversion. */
7258 inside_init = convert (type, inside_init);
7260 if (require_constant
7261 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7263 /* As an extension, allow initializing objects with static storage
7264 duration with compound literals (which are then treated just as
7265 the brace enclosed list they contain). Also allow this for
7266 vectors, as we can only assign them with compound literals. */
7267 if (flag_isoc99 && code != VECTOR_TYPE)
7268 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7269 "is not constant");
7270 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7271 inside_init = DECL_INITIAL (decl);
7274 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7275 && TREE_CODE (inside_init) != CONSTRUCTOR)
7277 error_init (init_loc, "array initialized from non-constant array "
7278 "expression");
7279 return error_mark_node;
7282 /* Compound expressions can only occur here if -Wpedantic or
7283 -pedantic-errors is specified. In the later case, we always want
7284 an error. In the former case, we simply want a warning. */
7285 if (require_constant && pedantic
7286 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7288 inside_init
7289 = valid_compound_expr_initializer (inside_init,
7290 TREE_TYPE (inside_init));
7291 if (inside_init == error_mark_node)
7292 error_init (init_loc, "initializer element is not constant");
7293 else
7294 pedwarn_init (init_loc, OPT_Wpedantic,
7295 "initializer element is not constant");
7296 if (flag_pedantic_errors)
7297 inside_init = error_mark_node;
7299 else if (require_constant
7300 && !initializer_constant_valid_p (inside_init,
7301 TREE_TYPE (inside_init)))
7303 error_init (init_loc, "initializer element is not constant");
7304 inside_init = error_mark_node;
7306 else if (require_constant && !maybe_const)
7307 pedwarn_init (init_loc, OPT_Wpedantic,
7308 "initializer element is not a constant expression");
7310 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7311 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7312 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7313 type, inside_init, origtype,
7314 ic_init, null_pointer_constant,
7315 NULL_TREE, NULL_TREE, 0);
7316 return inside_init;
7319 /* Handle scalar types, including conversions. */
7321 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7322 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7323 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7325 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7326 && (TREE_CODE (init) == STRING_CST
7327 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7328 inside_init = init = array_to_pointer_conversion (init_loc, init);
7329 if (semantic_type)
7330 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7331 inside_init);
7332 inside_init
7333 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7334 inside_init, origtype, ic_init,
7335 null_pointer_constant, NULL_TREE, NULL_TREE,
7338 /* Check to see if we have already given an error message. */
7339 if (inside_init == error_mark_node)
7341 else if (require_constant && !TREE_CONSTANT (inside_init))
7343 error_init (init_loc, "initializer element is not constant");
7344 inside_init = error_mark_node;
7346 else if (require_constant
7347 && !initializer_constant_valid_p (inside_init,
7348 TREE_TYPE (inside_init)))
7350 error_init (init_loc, "initializer element is not computable at "
7351 "load time");
7352 inside_init = error_mark_node;
7354 else if (require_constant && !maybe_const)
7355 pedwarn_init (init_loc, OPT_Wpedantic,
7356 "initializer element is not a constant expression");
7358 return inside_init;
7361 /* Come here only for records and arrays. */
7363 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7365 error_init (init_loc, "variable-sized object may not be initialized");
7366 return error_mark_node;
7369 error_init (init_loc, "invalid initializer");
7370 return error_mark_node;
7373 /* Handle initializers that use braces. */
7375 /* Type of object we are accumulating a constructor for.
7376 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7377 static tree constructor_type;
7379 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7380 left to fill. */
7381 static tree constructor_fields;
7383 /* For an ARRAY_TYPE, this is the specified index
7384 at which to store the next element we get. */
7385 static tree constructor_index;
7387 /* For an ARRAY_TYPE, this is the maximum index. */
7388 static tree constructor_max_index;
7390 /* For a RECORD_TYPE, this is the first field not yet written out. */
7391 static tree constructor_unfilled_fields;
7393 /* For an ARRAY_TYPE, this is the index of the first element
7394 not yet written out. */
7395 static tree constructor_unfilled_index;
7397 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7398 This is so we can generate gaps between fields, when appropriate. */
7399 static tree constructor_bit_index;
7401 /* If we are saving up the elements rather than allocating them,
7402 this is the list of elements so far (in reverse order,
7403 most recent first). */
7404 static vec<constructor_elt, va_gc> *constructor_elements;
7406 /* 1 if constructor should be incrementally stored into a constructor chain,
7407 0 if all the elements should be kept in AVL tree. */
7408 static int constructor_incremental;
7410 /* 1 if so far this constructor's elements are all compile-time constants. */
7411 static int constructor_constant;
7413 /* 1 if so far this constructor's elements are all valid address constants. */
7414 static int constructor_simple;
7416 /* 1 if this constructor has an element that cannot be part of a
7417 constant expression. */
7418 static int constructor_nonconst;
7420 /* 1 if this constructor is erroneous so far. */
7421 static int constructor_erroneous;
7423 /* 1 if this constructor is the universal zero initializer { 0 }. */
7424 static int constructor_zeroinit;
7426 /* Structure for managing pending initializer elements, organized as an
7427 AVL tree. */
7429 struct init_node
7431 struct init_node *left, *right;
7432 struct init_node *parent;
7433 int balance;
7434 tree purpose;
7435 tree value;
7436 tree origtype;
7439 /* Tree of pending elements at this constructor level.
7440 These are elements encountered out of order
7441 which belong at places we haven't reached yet in actually
7442 writing the output.
7443 Will never hold tree nodes across GC runs. */
7444 static struct init_node *constructor_pending_elts;
7446 /* The SPELLING_DEPTH of this constructor. */
7447 static int constructor_depth;
7449 /* DECL node for which an initializer is being read.
7450 0 means we are reading a constructor expression
7451 such as (struct foo) {...}. */
7452 static tree constructor_decl;
7454 /* Nonzero if this is an initializer for a top-level decl. */
7455 static int constructor_top_level;
7457 /* Nonzero if there were any member designators in this initializer. */
7458 static int constructor_designated;
7460 /* Nesting depth of designator list. */
7461 static int designator_depth;
7463 /* Nonzero if there were diagnosed errors in this designator list. */
7464 static int designator_erroneous;
7467 /* This stack has a level for each implicit or explicit level of
7468 structuring in the initializer, including the outermost one. It
7469 saves the values of most of the variables above. */
7471 struct constructor_range_stack;
7473 struct constructor_stack
7475 struct constructor_stack *next;
7476 tree type;
7477 tree fields;
7478 tree index;
7479 tree max_index;
7480 tree unfilled_index;
7481 tree unfilled_fields;
7482 tree bit_index;
7483 vec<constructor_elt, va_gc> *elements;
7484 struct init_node *pending_elts;
7485 int offset;
7486 int depth;
7487 /* If value nonzero, this value should replace the entire
7488 constructor at this level. */
7489 struct c_expr replacement_value;
7490 struct constructor_range_stack *range_stack;
7491 char constant;
7492 char simple;
7493 char nonconst;
7494 char implicit;
7495 char erroneous;
7496 char outer;
7497 char incremental;
7498 char designated;
7499 int designator_depth;
7502 static struct constructor_stack *constructor_stack;
7504 /* This stack represents designators from some range designator up to
7505 the last designator in the list. */
7507 struct constructor_range_stack
7509 struct constructor_range_stack *next, *prev;
7510 struct constructor_stack *stack;
7511 tree range_start;
7512 tree index;
7513 tree range_end;
7514 tree fields;
7517 static struct constructor_range_stack *constructor_range_stack;
7519 /* This stack records separate initializers that are nested.
7520 Nested initializers can't happen in ANSI C, but GNU C allows them
7521 in cases like { ... (struct foo) { ... } ... }. */
7523 struct initializer_stack
7525 struct initializer_stack *next;
7526 tree decl;
7527 struct constructor_stack *constructor_stack;
7528 struct constructor_range_stack *constructor_range_stack;
7529 vec<constructor_elt, va_gc> *elements;
7530 struct spelling *spelling;
7531 struct spelling *spelling_base;
7532 int spelling_size;
7533 char top_level;
7534 char require_constant_value;
7535 char require_constant_elements;
7536 rich_location *missing_brace_richloc;
7539 static struct initializer_stack *initializer_stack;
7541 /* Prepare to parse and output the initializer for variable DECL. */
7543 void
7544 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7545 rich_location *richloc)
7547 const char *locus;
7548 struct initializer_stack *p = XNEW (struct initializer_stack);
7550 p->decl = constructor_decl;
7551 p->require_constant_value = require_constant_value;
7552 p->require_constant_elements = require_constant_elements;
7553 p->constructor_stack = constructor_stack;
7554 p->constructor_range_stack = constructor_range_stack;
7555 p->elements = constructor_elements;
7556 p->spelling = spelling;
7557 p->spelling_base = spelling_base;
7558 p->spelling_size = spelling_size;
7559 p->top_level = constructor_top_level;
7560 p->next = initializer_stack;
7561 p->missing_brace_richloc = richloc;
7562 initializer_stack = p;
7564 constructor_decl = decl;
7565 constructor_designated = 0;
7566 constructor_top_level = top_level;
7568 if (decl != 0 && decl != error_mark_node)
7570 require_constant_value = TREE_STATIC (decl);
7571 require_constant_elements
7572 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7573 /* For a scalar, you can always use any value to initialize,
7574 even within braces. */
7575 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7576 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7578 else
7580 require_constant_value = 0;
7581 require_constant_elements = 0;
7582 locus = _("(anonymous)");
7585 constructor_stack = 0;
7586 constructor_range_stack = 0;
7588 found_missing_braces = 0;
7590 spelling_base = 0;
7591 spelling_size = 0;
7592 RESTORE_SPELLING_DEPTH (0);
7594 if (locus)
7595 push_string (locus);
7598 void
7599 finish_init (void)
7601 struct initializer_stack *p = initializer_stack;
7603 /* Free the whole constructor stack of this initializer. */
7604 while (constructor_stack)
7606 struct constructor_stack *q = constructor_stack;
7607 constructor_stack = q->next;
7608 free (q);
7611 gcc_assert (!constructor_range_stack);
7613 /* Pop back to the data of the outer initializer (if any). */
7614 free (spelling_base);
7616 constructor_decl = p->decl;
7617 require_constant_value = p->require_constant_value;
7618 require_constant_elements = p->require_constant_elements;
7619 constructor_stack = p->constructor_stack;
7620 constructor_range_stack = p->constructor_range_stack;
7621 constructor_elements = p->elements;
7622 spelling = p->spelling;
7623 spelling_base = p->spelling_base;
7624 spelling_size = p->spelling_size;
7625 constructor_top_level = p->top_level;
7626 initializer_stack = p->next;
7627 free (p);
7630 /* Call here when we see the initializer is surrounded by braces.
7631 This is instead of a call to push_init_level;
7632 it is matched by a call to pop_init_level.
7634 TYPE is the type to initialize, for a constructor expression.
7635 For an initializer for a decl, TYPE is zero. */
7637 void
7638 really_start_incremental_init (tree type)
7640 struct constructor_stack *p = XNEW (struct constructor_stack);
7642 if (type == 0)
7643 type = TREE_TYPE (constructor_decl);
7645 if (VECTOR_TYPE_P (type)
7646 && TYPE_VECTOR_OPAQUE (type))
7647 error ("opaque vector types cannot be initialized");
7649 p->type = constructor_type;
7650 p->fields = constructor_fields;
7651 p->index = constructor_index;
7652 p->max_index = constructor_max_index;
7653 p->unfilled_index = constructor_unfilled_index;
7654 p->unfilled_fields = constructor_unfilled_fields;
7655 p->bit_index = constructor_bit_index;
7656 p->elements = constructor_elements;
7657 p->constant = constructor_constant;
7658 p->simple = constructor_simple;
7659 p->nonconst = constructor_nonconst;
7660 p->erroneous = constructor_erroneous;
7661 p->pending_elts = constructor_pending_elts;
7662 p->depth = constructor_depth;
7663 p->replacement_value.value = 0;
7664 p->replacement_value.original_code = ERROR_MARK;
7665 p->replacement_value.original_type = NULL;
7666 p->implicit = 0;
7667 p->range_stack = 0;
7668 p->outer = 0;
7669 p->incremental = constructor_incremental;
7670 p->designated = constructor_designated;
7671 p->designator_depth = designator_depth;
7672 p->next = 0;
7673 constructor_stack = p;
7675 constructor_constant = 1;
7676 constructor_simple = 1;
7677 constructor_nonconst = 0;
7678 constructor_depth = SPELLING_DEPTH ();
7679 constructor_elements = NULL;
7680 constructor_pending_elts = 0;
7681 constructor_type = type;
7682 constructor_incremental = 1;
7683 constructor_designated = 0;
7684 constructor_zeroinit = 1;
7685 designator_depth = 0;
7686 designator_erroneous = 0;
7688 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7690 constructor_fields = TYPE_FIELDS (constructor_type);
7691 /* Skip any nameless bit fields at the beginning. */
7692 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7693 && DECL_NAME (constructor_fields) == 0)
7694 constructor_fields = DECL_CHAIN (constructor_fields);
7696 constructor_unfilled_fields = constructor_fields;
7697 constructor_bit_index = bitsize_zero_node;
7699 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7701 if (TYPE_DOMAIN (constructor_type))
7703 constructor_max_index
7704 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7706 /* Detect non-empty initializations of zero-length arrays. */
7707 if (constructor_max_index == NULL_TREE
7708 && TYPE_SIZE (constructor_type))
7709 constructor_max_index = integer_minus_one_node;
7711 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7712 to initialize VLAs will cause a proper error; avoid tree
7713 checking errors as well by setting a safe value. */
7714 if (constructor_max_index
7715 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7716 constructor_max_index = integer_minus_one_node;
7718 constructor_index
7719 = convert (bitsizetype,
7720 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7722 else
7724 constructor_index = bitsize_zero_node;
7725 constructor_max_index = NULL_TREE;
7728 constructor_unfilled_index = constructor_index;
7730 else if (VECTOR_TYPE_P (constructor_type))
7732 /* Vectors are like simple fixed-size arrays. */
7733 constructor_max_index =
7734 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7735 constructor_index = bitsize_zero_node;
7736 constructor_unfilled_index = constructor_index;
7738 else
7740 /* Handle the case of int x = {5}; */
7741 constructor_fields = constructor_type;
7742 constructor_unfilled_fields = constructor_type;
7746 extern location_t last_init_list_comma;
7748 /* Called when we see an open brace for a nested initializer. Finish
7749 off any pending levels with implicit braces. */
7750 void
7751 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7753 while (constructor_stack->implicit)
7755 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7756 && constructor_fields == 0)
7757 process_init_element (input_location,
7758 pop_init_level (loc, 1, braced_init_obstack,
7759 last_init_list_comma),
7760 true, braced_init_obstack);
7761 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7762 && constructor_max_index
7763 && tree_int_cst_lt (constructor_max_index,
7764 constructor_index))
7765 process_init_element (input_location,
7766 pop_init_level (loc, 1, braced_init_obstack,
7767 last_init_list_comma),
7768 true, braced_init_obstack);
7769 else
7770 break;
7774 /* Push down into a subobject, for initialization.
7775 If this is for an explicit set of braces, IMPLICIT is 0.
7776 If it is because the next element belongs at a lower level,
7777 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7779 void
7780 push_init_level (location_t loc, int implicit,
7781 struct obstack *braced_init_obstack)
7783 struct constructor_stack *p;
7784 tree value = NULL_TREE;
7786 /* Unless this is an explicit brace, we need to preserve previous
7787 content if any. */
7788 if (implicit)
7790 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7791 value = find_init_member (constructor_fields, braced_init_obstack);
7792 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7793 value = find_init_member (constructor_index, braced_init_obstack);
7796 p = XNEW (struct constructor_stack);
7797 p->type = constructor_type;
7798 p->fields = constructor_fields;
7799 p->index = constructor_index;
7800 p->max_index = constructor_max_index;
7801 p->unfilled_index = constructor_unfilled_index;
7802 p->unfilled_fields = constructor_unfilled_fields;
7803 p->bit_index = constructor_bit_index;
7804 p->elements = constructor_elements;
7805 p->constant = constructor_constant;
7806 p->simple = constructor_simple;
7807 p->nonconst = constructor_nonconst;
7808 p->erroneous = constructor_erroneous;
7809 p->pending_elts = constructor_pending_elts;
7810 p->depth = constructor_depth;
7811 p->replacement_value.value = 0;
7812 p->replacement_value.original_code = ERROR_MARK;
7813 p->replacement_value.original_type = NULL;
7814 p->implicit = implicit;
7815 p->outer = 0;
7816 p->incremental = constructor_incremental;
7817 p->designated = constructor_designated;
7818 p->designator_depth = designator_depth;
7819 p->next = constructor_stack;
7820 p->range_stack = 0;
7821 constructor_stack = p;
7823 constructor_constant = 1;
7824 constructor_simple = 1;
7825 constructor_nonconst = 0;
7826 constructor_depth = SPELLING_DEPTH ();
7827 constructor_elements = NULL;
7828 constructor_incremental = 1;
7829 constructor_designated = 0;
7830 constructor_pending_elts = 0;
7831 if (!implicit)
7833 p->range_stack = constructor_range_stack;
7834 constructor_range_stack = 0;
7835 designator_depth = 0;
7836 designator_erroneous = 0;
7839 /* Don't die if an entire brace-pair level is superfluous
7840 in the containing level. */
7841 if (constructor_type == 0)
7843 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7845 /* Don't die if there are extra init elts at the end. */
7846 if (constructor_fields == 0)
7847 constructor_type = 0;
7848 else
7850 constructor_type = TREE_TYPE (constructor_fields);
7851 push_member_name (constructor_fields);
7852 constructor_depth++;
7854 /* If upper initializer is designated, then mark this as
7855 designated too to prevent bogus warnings. */
7856 constructor_designated = p->designated;
7858 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7860 constructor_type = TREE_TYPE (constructor_type);
7861 push_array_bounds (tree_to_uhwi (constructor_index));
7862 constructor_depth++;
7865 if (constructor_type == 0)
7867 error_init (loc, "extra brace group at end of initializer");
7868 constructor_fields = 0;
7869 constructor_unfilled_fields = 0;
7870 return;
7873 if (value && TREE_CODE (value) == CONSTRUCTOR)
7875 constructor_constant = TREE_CONSTANT (value);
7876 constructor_simple = TREE_STATIC (value);
7877 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7878 constructor_elements = CONSTRUCTOR_ELTS (value);
7879 if (!vec_safe_is_empty (constructor_elements)
7880 && (TREE_CODE (constructor_type) == RECORD_TYPE
7881 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7882 set_nonincremental_init (braced_init_obstack);
7885 if (implicit == 1)
7887 found_missing_braces = 1;
7888 if (initializer_stack->missing_brace_richloc)
7889 initializer_stack->missing_brace_richloc->add_fixit_insert_before
7890 (loc, "{");
7893 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7895 constructor_fields = TYPE_FIELDS (constructor_type);
7896 /* Skip any nameless bit fields at the beginning. */
7897 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7898 && DECL_NAME (constructor_fields) == 0)
7899 constructor_fields = DECL_CHAIN (constructor_fields);
7901 constructor_unfilled_fields = constructor_fields;
7902 constructor_bit_index = bitsize_zero_node;
7904 else if (VECTOR_TYPE_P (constructor_type))
7906 /* Vectors are like simple fixed-size arrays. */
7907 constructor_max_index =
7908 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7909 constructor_index = bitsize_int (0);
7910 constructor_unfilled_index = constructor_index;
7912 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7914 if (TYPE_DOMAIN (constructor_type))
7916 constructor_max_index
7917 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7919 /* Detect non-empty initializations of zero-length arrays. */
7920 if (constructor_max_index == NULL_TREE
7921 && TYPE_SIZE (constructor_type))
7922 constructor_max_index = integer_minus_one_node;
7924 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7925 to initialize VLAs will cause a proper error; avoid tree
7926 checking errors as well by setting a safe value. */
7927 if (constructor_max_index
7928 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7929 constructor_max_index = integer_minus_one_node;
7931 constructor_index
7932 = convert (bitsizetype,
7933 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7935 else
7936 constructor_index = bitsize_zero_node;
7938 constructor_unfilled_index = constructor_index;
7939 if (value && TREE_CODE (value) == STRING_CST)
7941 /* We need to split the char/wchar array into individual
7942 characters, so that we don't have to special case it
7943 everywhere. */
7944 set_nonincremental_init_from_string (value, braced_init_obstack);
7947 else
7949 if (constructor_type != error_mark_node)
7950 warning_init (input_location, 0, "braces around scalar initializer");
7951 constructor_fields = constructor_type;
7952 constructor_unfilled_fields = constructor_type;
7956 /* At the end of an implicit or explicit brace level,
7957 finish up that level of constructor. If a single expression
7958 with redundant braces initialized that level, return the
7959 c_expr structure for that expression. Otherwise, the original_code
7960 element is set to ERROR_MARK.
7961 If we were outputting the elements as they are read, return 0 as the value
7962 from inner levels (process_init_element ignores that),
7963 but return error_mark_node as the value from the outermost level
7964 (that's what we want to put in DECL_INITIAL).
7965 Otherwise, return a CONSTRUCTOR expression as the value. */
7967 struct c_expr
7968 pop_init_level (location_t loc, int implicit,
7969 struct obstack *braced_init_obstack,
7970 location_t insert_before)
7972 struct constructor_stack *p;
7973 struct c_expr ret;
7974 ret.value = 0;
7975 ret.original_code = ERROR_MARK;
7976 ret.original_type = NULL;
7978 if (implicit == 0)
7980 /* When we come to an explicit close brace,
7981 pop any inner levels that didn't have explicit braces. */
7982 while (constructor_stack->implicit)
7983 process_init_element (input_location,
7984 pop_init_level (loc, 1, braced_init_obstack,
7985 insert_before),
7986 true, braced_init_obstack);
7987 gcc_assert (!constructor_range_stack);
7989 else
7990 if (initializer_stack->missing_brace_richloc)
7991 initializer_stack->missing_brace_richloc->add_fixit_insert_before
7992 (insert_before, "}");
7994 /* Now output all pending elements. */
7995 constructor_incremental = 1;
7996 output_pending_init_elements (1, braced_init_obstack);
7998 p = constructor_stack;
8000 /* Error for initializing a flexible array member, or a zero-length
8001 array member in an inappropriate context. */
8002 if (constructor_type && constructor_fields
8003 && TREE_CODE (constructor_type) == ARRAY_TYPE
8004 && TYPE_DOMAIN (constructor_type)
8005 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8007 /* Silently discard empty initializations. The parser will
8008 already have pedwarned for empty brackets. */
8009 if (integer_zerop (constructor_unfilled_index))
8010 constructor_type = NULL_TREE;
8011 else
8013 gcc_assert (!TYPE_SIZE (constructor_type));
8015 if (constructor_depth > 2)
8016 error_init (loc, "initialization of flexible array member in a nested context");
8017 else
8018 pedwarn_init (loc, OPT_Wpedantic,
8019 "initialization of a flexible array member");
8021 /* We have already issued an error message for the existence
8022 of a flexible array member not at the end of the structure.
8023 Discard the initializer so that we do not die later. */
8024 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8025 constructor_type = NULL_TREE;
8029 switch (vec_safe_length (constructor_elements))
8031 case 0:
8032 /* Initialization with { } counts as zeroinit. */
8033 constructor_zeroinit = 1;
8034 break;
8035 case 1:
8036 /* This might be zeroinit as well. */
8037 if (integer_zerop ((*constructor_elements)[0].value))
8038 constructor_zeroinit = 1;
8039 break;
8040 default:
8041 /* If the constructor has more than one element, it can't be { 0 }. */
8042 constructor_zeroinit = 0;
8043 break;
8046 /* Warn when some structs are initialized with direct aggregation. */
8047 if (!implicit && found_missing_braces && warn_missing_braces
8048 && !constructor_zeroinit)
8050 gcc_assert (initializer_stack->missing_brace_richloc);
8051 warning_at_rich_loc (initializer_stack->missing_brace_richloc,
8052 OPT_Wmissing_braces,
8053 "missing braces around initializer");
8056 /* Warn when some struct elements are implicitly initialized to zero. */
8057 if (warn_missing_field_initializers
8058 && constructor_type
8059 && TREE_CODE (constructor_type) == RECORD_TYPE
8060 && constructor_unfilled_fields)
8062 /* Do not warn for flexible array members or zero-length arrays. */
8063 while (constructor_unfilled_fields
8064 && (!DECL_SIZE (constructor_unfilled_fields)
8065 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8066 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8068 if (constructor_unfilled_fields
8069 /* Do not warn if this level of the initializer uses member
8070 designators; it is likely to be deliberate. */
8071 && !constructor_designated
8072 /* Do not warn about initializing with { 0 } or with { }. */
8073 && !constructor_zeroinit)
8075 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8076 "missing initializer for field %qD of %qT",
8077 constructor_unfilled_fields,
8078 constructor_type))
8079 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8080 "%qD declared here", constructor_unfilled_fields);
8084 /* Pad out the end of the structure. */
8085 if (p->replacement_value.value)
8086 /* If this closes a superfluous brace pair,
8087 just pass out the element between them. */
8088 ret = p->replacement_value;
8089 else if (constructor_type == 0)
8091 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8092 && TREE_CODE (constructor_type) != ARRAY_TYPE
8093 && !VECTOR_TYPE_P (constructor_type))
8095 /* A nonincremental scalar initializer--just return
8096 the element, after verifying there is just one. */
8097 if (vec_safe_is_empty (constructor_elements))
8099 if (!constructor_erroneous)
8100 error_init (loc, "empty scalar initializer");
8101 ret.value = error_mark_node;
8103 else if (vec_safe_length (constructor_elements) != 1)
8105 error_init (loc, "extra elements in scalar initializer");
8106 ret.value = (*constructor_elements)[0].value;
8108 else
8109 ret.value = (*constructor_elements)[0].value;
8111 else
8113 if (constructor_erroneous)
8114 ret.value = error_mark_node;
8115 else
8117 ret.value = build_constructor (constructor_type,
8118 constructor_elements);
8119 if (constructor_constant)
8120 TREE_CONSTANT (ret.value) = 1;
8121 if (constructor_constant && constructor_simple)
8122 TREE_STATIC (ret.value) = 1;
8123 if (constructor_nonconst)
8124 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8128 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8130 if (constructor_nonconst)
8131 ret.original_code = C_MAYBE_CONST_EXPR;
8132 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8133 ret.original_code = ERROR_MARK;
8136 constructor_type = p->type;
8137 constructor_fields = p->fields;
8138 constructor_index = p->index;
8139 constructor_max_index = p->max_index;
8140 constructor_unfilled_index = p->unfilled_index;
8141 constructor_unfilled_fields = p->unfilled_fields;
8142 constructor_bit_index = p->bit_index;
8143 constructor_elements = p->elements;
8144 constructor_constant = p->constant;
8145 constructor_simple = p->simple;
8146 constructor_nonconst = p->nonconst;
8147 constructor_erroneous = p->erroneous;
8148 constructor_incremental = p->incremental;
8149 constructor_designated = p->designated;
8150 designator_depth = p->designator_depth;
8151 constructor_pending_elts = p->pending_elts;
8152 constructor_depth = p->depth;
8153 if (!p->implicit)
8154 constructor_range_stack = p->range_stack;
8155 RESTORE_SPELLING_DEPTH (constructor_depth);
8157 constructor_stack = p->next;
8158 free (p);
8160 if (ret.value == 0 && constructor_stack == 0)
8161 ret.value = error_mark_node;
8162 return ret;
8165 /* Common handling for both array range and field name designators.
8166 ARRAY argument is nonzero for array ranges. Returns zero for success. */
8168 static int
8169 set_designator (location_t loc, int array,
8170 struct obstack *braced_init_obstack)
8172 tree subtype;
8173 enum tree_code subcode;
8175 /* Don't die if an entire brace-pair level is superfluous
8176 in the containing level. */
8177 if (constructor_type == 0)
8178 return 1;
8180 /* If there were errors in this designator list already, bail out
8181 silently. */
8182 if (designator_erroneous)
8183 return 1;
8185 if (!designator_depth)
8187 gcc_assert (!constructor_range_stack);
8189 /* Designator list starts at the level of closest explicit
8190 braces. */
8191 while (constructor_stack->implicit)
8192 process_init_element (input_location,
8193 pop_init_level (loc, 1, braced_init_obstack,
8194 last_init_list_comma),
8195 true, braced_init_obstack);
8196 constructor_designated = 1;
8197 return 0;
8200 switch (TREE_CODE (constructor_type))
8202 case RECORD_TYPE:
8203 case UNION_TYPE:
8204 subtype = TREE_TYPE (constructor_fields);
8205 if (subtype != error_mark_node)
8206 subtype = TYPE_MAIN_VARIANT (subtype);
8207 break;
8208 case ARRAY_TYPE:
8209 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8210 break;
8211 default:
8212 gcc_unreachable ();
8215 subcode = TREE_CODE (subtype);
8216 if (array && subcode != ARRAY_TYPE)
8218 error_init (loc, "array index in non-array initializer");
8219 return 1;
8221 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8223 error_init (loc, "field name not in record or union initializer");
8224 return 1;
8227 constructor_designated = 1;
8228 finish_implicit_inits (loc, braced_init_obstack);
8229 push_init_level (loc, 2, braced_init_obstack);
8230 return 0;
8233 /* If there are range designators in designator list, push a new designator
8234 to constructor_range_stack. RANGE_END is end of such stack range or
8235 NULL_TREE if there is no range designator at this level. */
8237 static void
8238 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8240 struct constructor_range_stack *p;
8242 p = (struct constructor_range_stack *)
8243 obstack_alloc (braced_init_obstack,
8244 sizeof (struct constructor_range_stack));
8245 p->prev = constructor_range_stack;
8246 p->next = 0;
8247 p->fields = constructor_fields;
8248 p->range_start = constructor_index;
8249 p->index = constructor_index;
8250 p->stack = constructor_stack;
8251 p->range_end = range_end;
8252 if (constructor_range_stack)
8253 constructor_range_stack->next = p;
8254 constructor_range_stack = p;
8257 /* Within an array initializer, specify the next index to be initialized.
8258 FIRST is that index. If LAST is nonzero, then initialize a range
8259 of indices, running from FIRST through LAST. */
8261 void
8262 set_init_index (location_t loc, tree first, tree last,
8263 struct obstack *braced_init_obstack)
8265 if (set_designator (loc, 1, braced_init_obstack))
8266 return;
8268 designator_erroneous = 1;
8270 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8271 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8273 error_init (loc, "array index in initializer not of integer type");
8274 return;
8277 if (TREE_CODE (first) != INTEGER_CST)
8279 first = c_fully_fold (first, false, NULL);
8280 if (TREE_CODE (first) == INTEGER_CST)
8281 pedwarn_init (loc, OPT_Wpedantic,
8282 "array index in initializer is not "
8283 "an integer constant expression");
8286 if (last && TREE_CODE (last) != INTEGER_CST)
8288 last = c_fully_fold (last, false, NULL);
8289 if (TREE_CODE (last) == INTEGER_CST)
8290 pedwarn_init (loc, OPT_Wpedantic,
8291 "array index in initializer is not "
8292 "an integer constant expression");
8295 if (TREE_CODE (first) != INTEGER_CST)
8296 error_init (loc, "nonconstant array index in initializer");
8297 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
8298 error_init (loc, "nonconstant array index in initializer");
8299 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8300 error_init (loc, "array index in non-array initializer");
8301 else if (tree_int_cst_sgn (first) == -1)
8302 error_init (loc, "array index in initializer exceeds array bounds");
8303 else if (constructor_max_index
8304 && tree_int_cst_lt (constructor_max_index, first))
8305 error_init (loc, "array index in initializer exceeds array bounds");
8306 else
8308 constant_expression_warning (first);
8309 if (last)
8310 constant_expression_warning (last);
8311 constructor_index = convert (bitsizetype, first);
8312 if (tree_int_cst_lt (constructor_index, first))
8314 constructor_index = copy_node (constructor_index);
8315 TREE_OVERFLOW (constructor_index) = 1;
8318 if (last)
8320 if (tree_int_cst_equal (first, last))
8321 last = 0;
8322 else if (tree_int_cst_lt (last, first))
8324 error_init (loc, "empty index range in initializer");
8325 last = 0;
8327 else
8329 last = convert (bitsizetype, last);
8330 if (constructor_max_index != 0
8331 && tree_int_cst_lt (constructor_max_index, last))
8333 error_init (loc, "array index range in initializer exceeds "
8334 "array bounds");
8335 last = 0;
8340 designator_depth++;
8341 designator_erroneous = 0;
8342 if (constructor_range_stack || last)
8343 push_range_stack (last, braced_init_obstack);
8347 /* Within a struct initializer, specify the next field to be initialized. */
8349 void
8350 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8351 struct obstack *braced_init_obstack)
8353 tree field;
8355 if (set_designator (loc, 0, braced_init_obstack))
8356 return;
8358 designator_erroneous = 1;
8360 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8362 error_init (loc, "field name not in record or union initializer");
8363 return;
8366 field = lookup_field (constructor_type, fieldname);
8368 if (field == 0)
8370 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8371 if (guessed_id)
8373 gcc_rich_location rich_loc (fieldname_loc);
8374 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8375 error_at_rich_loc
8376 (&rich_loc,
8377 "%qT has no member named %qE; did you mean %qE?",
8378 constructor_type, fieldname, guessed_id);
8380 else
8381 error_at (fieldname_loc, "%qT has no member named %qE",
8382 constructor_type, fieldname);
8384 else
8387 constructor_fields = TREE_VALUE (field);
8388 designator_depth++;
8389 designator_erroneous = 0;
8390 if (constructor_range_stack)
8391 push_range_stack (NULL_TREE, braced_init_obstack);
8392 field = TREE_CHAIN (field);
8393 if (field)
8395 if (set_designator (loc, 0, braced_init_obstack))
8396 return;
8399 while (field != NULL_TREE);
8402 /* Add a new initializer to the tree of pending initializers. PURPOSE
8403 identifies the initializer, either array index or field in a structure.
8404 VALUE is the value of that index or field. If ORIGTYPE is not
8405 NULL_TREE, it is the original type of VALUE.
8407 IMPLICIT is true if value comes from pop_init_level (1),
8408 the new initializer has been merged with the existing one
8409 and thus no warnings should be emitted about overriding an
8410 existing initializer. */
8412 static void
8413 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8414 bool implicit, struct obstack *braced_init_obstack)
8416 struct init_node *p, **q, *r;
8418 q = &constructor_pending_elts;
8419 p = 0;
8421 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8423 while (*q != 0)
8425 p = *q;
8426 if (tree_int_cst_lt (purpose, p->purpose))
8427 q = &p->left;
8428 else if (tree_int_cst_lt (p->purpose, purpose))
8429 q = &p->right;
8430 else
8432 if (!implicit)
8434 if (TREE_SIDE_EFFECTS (p->value))
8435 warning_init (loc, OPT_Woverride_init_side_effects,
8436 "initialized field with side-effects "
8437 "overwritten");
8438 else if (warn_override_init)
8439 warning_init (loc, OPT_Woverride_init,
8440 "initialized field overwritten");
8442 p->value = value;
8443 p->origtype = origtype;
8444 return;
8448 else
8450 tree bitpos;
8452 bitpos = bit_position (purpose);
8453 while (*q != NULL)
8455 p = *q;
8456 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8457 q = &p->left;
8458 else if (p->purpose != purpose)
8459 q = &p->right;
8460 else
8462 if (!implicit)
8464 if (TREE_SIDE_EFFECTS (p->value))
8465 warning_init (loc, OPT_Woverride_init_side_effects,
8466 "initialized field with side-effects "
8467 "overwritten");
8468 else if (warn_override_init)
8469 warning_init (loc, OPT_Woverride_init,
8470 "initialized field overwritten");
8472 p->value = value;
8473 p->origtype = origtype;
8474 return;
8479 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8480 sizeof (struct init_node));
8481 r->purpose = purpose;
8482 r->value = value;
8483 r->origtype = origtype;
8485 *q = r;
8486 r->parent = p;
8487 r->left = 0;
8488 r->right = 0;
8489 r->balance = 0;
8491 while (p)
8493 struct init_node *s;
8495 if (r == p->left)
8497 if (p->balance == 0)
8498 p->balance = -1;
8499 else if (p->balance < 0)
8501 if (r->balance < 0)
8503 /* L rotation. */
8504 p->left = r->right;
8505 if (p->left)
8506 p->left->parent = p;
8507 r->right = p;
8509 p->balance = 0;
8510 r->balance = 0;
8512 s = p->parent;
8513 p->parent = r;
8514 r->parent = s;
8515 if (s)
8517 if (s->left == p)
8518 s->left = r;
8519 else
8520 s->right = r;
8522 else
8523 constructor_pending_elts = r;
8525 else
8527 /* LR rotation. */
8528 struct init_node *t = r->right;
8530 r->right = t->left;
8531 if (r->right)
8532 r->right->parent = r;
8533 t->left = r;
8535 p->left = t->right;
8536 if (p->left)
8537 p->left->parent = p;
8538 t->right = p;
8540 p->balance = t->balance < 0;
8541 r->balance = -(t->balance > 0);
8542 t->balance = 0;
8544 s = p->parent;
8545 p->parent = t;
8546 r->parent = t;
8547 t->parent = s;
8548 if (s)
8550 if (s->left == p)
8551 s->left = t;
8552 else
8553 s->right = t;
8555 else
8556 constructor_pending_elts = t;
8558 break;
8560 else
8562 /* p->balance == +1; growth of left side balances the node. */
8563 p->balance = 0;
8564 break;
8567 else /* r == p->right */
8569 if (p->balance == 0)
8570 /* Growth propagation from right side. */
8571 p->balance++;
8572 else if (p->balance > 0)
8574 if (r->balance > 0)
8576 /* R rotation. */
8577 p->right = r->left;
8578 if (p->right)
8579 p->right->parent = p;
8580 r->left = p;
8582 p->balance = 0;
8583 r->balance = 0;
8585 s = p->parent;
8586 p->parent = r;
8587 r->parent = s;
8588 if (s)
8590 if (s->left == p)
8591 s->left = r;
8592 else
8593 s->right = r;
8595 else
8596 constructor_pending_elts = r;
8598 else /* r->balance == -1 */
8600 /* RL rotation */
8601 struct init_node *t = r->left;
8603 r->left = t->right;
8604 if (r->left)
8605 r->left->parent = r;
8606 t->right = r;
8608 p->right = t->left;
8609 if (p->right)
8610 p->right->parent = p;
8611 t->left = p;
8613 r->balance = (t->balance < 0);
8614 p->balance = -(t->balance > 0);
8615 t->balance = 0;
8617 s = p->parent;
8618 p->parent = t;
8619 r->parent = t;
8620 t->parent = s;
8621 if (s)
8623 if (s->left == p)
8624 s->left = t;
8625 else
8626 s->right = t;
8628 else
8629 constructor_pending_elts = t;
8631 break;
8633 else
8635 /* p->balance == -1; growth of right side balances the node. */
8636 p->balance = 0;
8637 break;
8641 r = p;
8642 p = p->parent;
8646 /* Build AVL tree from a sorted chain. */
8648 static void
8649 set_nonincremental_init (struct obstack * braced_init_obstack)
8651 unsigned HOST_WIDE_INT ix;
8652 tree index, value;
8654 if (TREE_CODE (constructor_type) != RECORD_TYPE
8655 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8656 return;
8658 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8659 add_pending_init (input_location, index, value, NULL_TREE, true,
8660 braced_init_obstack);
8661 constructor_elements = NULL;
8662 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8664 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8665 /* Skip any nameless bit fields at the beginning. */
8666 while (constructor_unfilled_fields != 0
8667 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8668 && DECL_NAME (constructor_unfilled_fields) == 0)
8669 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8672 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8674 if (TYPE_DOMAIN (constructor_type))
8675 constructor_unfilled_index
8676 = convert (bitsizetype,
8677 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8678 else
8679 constructor_unfilled_index = bitsize_zero_node;
8681 constructor_incremental = 0;
8684 /* Build AVL tree from a string constant. */
8686 static void
8687 set_nonincremental_init_from_string (tree str,
8688 struct obstack * braced_init_obstack)
8690 tree value, purpose, type;
8691 HOST_WIDE_INT val[2];
8692 const char *p, *end;
8693 int byte, wchar_bytes, charwidth, bitpos;
8695 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8697 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8698 charwidth = TYPE_PRECISION (char_type_node);
8699 gcc_assert ((size_t) wchar_bytes * charwidth
8700 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8701 type = TREE_TYPE (constructor_type);
8702 p = TREE_STRING_POINTER (str);
8703 end = p + TREE_STRING_LENGTH (str);
8705 for (purpose = bitsize_zero_node;
8706 p < end
8707 && !(constructor_max_index
8708 && tree_int_cst_lt (constructor_max_index, purpose));
8709 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8711 if (wchar_bytes == 1)
8713 val[0] = (unsigned char) *p++;
8714 val[1] = 0;
8716 else
8718 val[1] = 0;
8719 val[0] = 0;
8720 for (byte = 0; byte < wchar_bytes; byte++)
8722 if (BYTES_BIG_ENDIAN)
8723 bitpos = (wchar_bytes - byte - 1) * charwidth;
8724 else
8725 bitpos = byte * charwidth;
8726 val[bitpos / HOST_BITS_PER_WIDE_INT]
8727 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8728 << (bitpos % HOST_BITS_PER_WIDE_INT);
8732 if (!TYPE_UNSIGNED (type))
8734 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8735 if (bitpos < HOST_BITS_PER_WIDE_INT)
8737 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8739 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8740 val[1] = -1;
8743 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8745 if (val[0] < 0)
8746 val[1] = -1;
8748 else if (val[1] & (HOST_WIDE_INT_1
8749 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8750 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8753 value = wide_int_to_tree (type,
8754 wide_int::from_array (val, 2,
8755 HOST_BITS_PER_WIDE_INT * 2));
8756 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8757 braced_init_obstack);
8760 constructor_incremental = 0;
8763 /* Return value of FIELD in pending initializer or zero if the field was
8764 not initialized yet. */
8766 static tree
8767 find_init_member (tree field, struct obstack * braced_init_obstack)
8769 struct init_node *p;
8771 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8773 if (constructor_incremental
8774 && tree_int_cst_lt (field, constructor_unfilled_index))
8775 set_nonincremental_init (braced_init_obstack);
8777 p = constructor_pending_elts;
8778 while (p)
8780 if (tree_int_cst_lt (field, p->purpose))
8781 p = p->left;
8782 else if (tree_int_cst_lt (p->purpose, field))
8783 p = p->right;
8784 else
8785 return p->value;
8788 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8790 tree bitpos = bit_position (field);
8792 if (constructor_incremental
8793 && (!constructor_unfilled_fields
8794 || tree_int_cst_lt (bitpos,
8795 bit_position (constructor_unfilled_fields))))
8796 set_nonincremental_init (braced_init_obstack);
8798 p = constructor_pending_elts;
8799 while (p)
8801 if (field == p->purpose)
8802 return p->value;
8803 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8804 p = p->left;
8805 else
8806 p = p->right;
8809 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8811 if (!vec_safe_is_empty (constructor_elements)
8812 && (constructor_elements->last ().index == field))
8813 return constructor_elements->last ().value;
8815 return 0;
8818 /* "Output" the next constructor element.
8819 At top level, really output it to assembler code now.
8820 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8821 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8822 TYPE is the data type that the containing data type wants here.
8823 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8824 If VALUE is a string constant, STRICT_STRING is true if it is
8825 unparenthesized or we should not warn here for it being parenthesized.
8826 For other types of VALUE, STRICT_STRING is not used.
8828 PENDING if non-nil means output pending elements that belong
8829 right after this element. (PENDING is normally 1;
8830 it is 0 while outputting pending elements, to avoid recursion.)
8832 IMPLICIT is true if value comes from pop_init_level (1),
8833 the new initializer has been merged with the existing one
8834 and thus no warnings should be emitted about overriding an
8835 existing initializer. */
8837 static void
8838 output_init_element (location_t loc, tree value, tree origtype,
8839 bool strict_string, tree type, tree field, int pending,
8840 bool implicit, struct obstack * braced_init_obstack)
8842 tree semantic_type = NULL_TREE;
8843 bool maybe_const = true;
8844 bool npc;
8846 if (type == error_mark_node || value == error_mark_node)
8848 constructor_erroneous = 1;
8849 return;
8851 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8852 && (TREE_CODE (value) == STRING_CST
8853 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8854 && !(TREE_CODE (value) == STRING_CST
8855 && TREE_CODE (type) == ARRAY_TYPE
8856 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8857 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8858 TYPE_MAIN_VARIANT (type)))
8859 value = array_to_pointer_conversion (input_location, value);
8861 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8862 && require_constant_value && pending)
8864 /* As an extension, allow initializing objects with static storage
8865 duration with compound literals (which are then treated just as
8866 the brace enclosed list they contain). */
8867 if (flag_isoc99)
8868 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8869 "constant");
8870 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8871 value = DECL_INITIAL (decl);
8874 npc = null_pointer_constant_p (value);
8875 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8877 semantic_type = TREE_TYPE (value);
8878 value = TREE_OPERAND (value, 0);
8880 value = c_fully_fold (value, require_constant_value, &maybe_const);
8882 if (value == error_mark_node)
8883 constructor_erroneous = 1;
8884 else if (!TREE_CONSTANT (value))
8885 constructor_constant = 0;
8886 else if (!initializer_constant_valid_p (value,
8887 TREE_TYPE (value),
8888 AGGREGATE_TYPE_P (constructor_type)
8889 && TYPE_REVERSE_STORAGE_ORDER
8890 (constructor_type))
8891 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8892 && DECL_C_BIT_FIELD (field)
8893 && TREE_CODE (value) != INTEGER_CST))
8894 constructor_simple = 0;
8895 if (!maybe_const)
8896 constructor_nonconst = 1;
8898 /* Digest the initializer and issue any errors about incompatible
8899 types before issuing errors about non-constant initializers. */
8900 tree new_value = value;
8901 if (semantic_type)
8902 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8903 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8904 require_constant_value);
8905 if (new_value == error_mark_node)
8907 constructor_erroneous = 1;
8908 return;
8910 if (require_constant_value || require_constant_elements)
8911 constant_expression_warning (new_value);
8913 /* Proceed to check the constness of the original initializer. */
8914 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8916 if (require_constant_value)
8918 error_init (loc, "initializer element is not constant");
8919 value = error_mark_node;
8921 else if (require_constant_elements)
8922 pedwarn (loc, OPT_Wpedantic,
8923 "initializer element is not computable at load time");
8925 else if (!maybe_const
8926 && (require_constant_value || require_constant_elements))
8927 pedwarn_init (loc, OPT_Wpedantic,
8928 "initializer element is not a constant expression");
8930 /* Issue -Wc++-compat warnings about initializing a bitfield with
8931 enum type. */
8932 if (warn_cxx_compat
8933 && field != NULL_TREE
8934 && TREE_CODE (field) == FIELD_DECL
8935 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8936 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8937 != TYPE_MAIN_VARIANT (type))
8938 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8940 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8941 if (checktype != error_mark_node
8942 && (TYPE_MAIN_VARIANT (checktype)
8943 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8944 warning_init (loc, OPT_Wc___compat,
8945 "enum conversion in initialization is invalid in C++");
8948 /* If this field is empty (and not at the end of structure),
8949 don't do anything other than checking the initializer. */
8950 if (field
8951 && (TREE_TYPE (field) == error_mark_node
8952 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8953 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8954 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8955 || DECL_CHAIN (field)))))
8956 return;
8958 /* Finally, set VALUE to the initializer value digested above. */
8959 value = new_value;
8961 /* If this element doesn't come next in sequence,
8962 put it on constructor_pending_elts. */
8963 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8964 && (!constructor_incremental
8965 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8967 if (constructor_incremental
8968 && tree_int_cst_lt (field, constructor_unfilled_index))
8969 set_nonincremental_init (braced_init_obstack);
8971 add_pending_init (loc, field, value, origtype, implicit,
8972 braced_init_obstack);
8973 return;
8975 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8976 && (!constructor_incremental
8977 || field != constructor_unfilled_fields))
8979 /* We do this for records but not for unions. In a union,
8980 no matter which field is specified, it can be initialized
8981 right away since it starts at the beginning of the union. */
8982 if (constructor_incremental)
8984 if (!constructor_unfilled_fields)
8985 set_nonincremental_init (braced_init_obstack);
8986 else
8988 tree bitpos, unfillpos;
8990 bitpos = bit_position (field);
8991 unfillpos = bit_position (constructor_unfilled_fields);
8993 if (tree_int_cst_lt (bitpos, unfillpos))
8994 set_nonincremental_init (braced_init_obstack);
8998 add_pending_init (loc, field, value, origtype, implicit,
8999 braced_init_obstack);
9000 return;
9002 else if (TREE_CODE (constructor_type) == UNION_TYPE
9003 && !vec_safe_is_empty (constructor_elements))
9005 if (!implicit)
9007 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9008 warning_init (loc, OPT_Woverride_init_side_effects,
9009 "initialized field with side-effects overwritten");
9010 else if (warn_override_init)
9011 warning_init (loc, OPT_Woverride_init,
9012 "initialized field overwritten");
9015 /* We can have just one union field set. */
9016 constructor_elements = NULL;
9019 /* Otherwise, output this element either to
9020 constructor_elements or to the assembler file. */
9022 constructor_elt celt = {field, value};
9023 vec_safe_push (constructor_elements, celt);
9025 /* Advance the variable that indicates sequential elements output. */
9026 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9027 constructor_unfilled_index
9028 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9029 bitsize_one_node);
9030 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9032 constructor_unfilled_fields
9033 = DECL_CHAIN (constructor_unfilled_fields);
9035 /* Skip any nameless bit fields. */
9036 while (constructor_unfilled_fields != 0
9037 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9038 && DECL_NAME (constructor_unfilled_fields) == 0)
9039 constructor_unfilled_fields =
9040 DECL_CHAIN (constructor_unfilled_fields);
9042 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9043 constructor_unfilled_fields = 0;
9045 /* Now output any pending elements which have become next. */
9046 if (pending)
9047 output_pending_init_elements (0, braced_init_obstack);
9050 /* Output any pending elements which have become next.
9051 As we output elements, constructor_unfilled_{fields,index}
9052 advances, which may cause other elements to become next;
9053 if so, they too are output.
9055 If ALL is 0, we return when there are
9056 no more pending elements to output now.
9058 If ALL is 1, we output space as necessary so that
9059 we can output all the pending elements. */
9060 static void
9061 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9063 struct init_node *elt = constructor_pending_elts;
9064 tree next;
9066 retry:
9068 /* Look through the whole pending tree.
9069 If we find an element that should be output now,
9070 output it. Otherwise, set NEXT to the element
9071 that comes first among those still pending. */
9073 next = 0;
9074 while (elt)
9076 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9078 if (tree_int_cst_equal (elt->purpose,
9079 constructor_unfilled_index))
9080 output_init_element (input_location, elt->value, elt->origtype,
9081 true, TREE_TYPE (constructor_type),
9082 constructor_unfilled_index, 0, false,
9083 braced_init_obstack);
9084 else if (tree_int_cst_lt (constructor_unfilled_index,
9085 elt->purpose))
9087 /* Advance to the next smaller node. */
9088 if (elt->left)
9089 elt = elt->left;
9090 else
9092 /* We have reached the smallest node bigger than the
9093 current unfilled index. Fill the space first. */
9094 next = elt->purpose;
9095 break;
9098 else
9100 /* Advance to the next bigger node. */
9101 if (elt->right)
9102 elt = elt->right;
9103 else
9105 /* We have reached the biggest node in a subtree. Find
9106 the parent of it, which is the next bigger node. */
9107 while (elt->parent && elt->parent->right == elt)
9108 elt = elt->parent;
9109 elt = elt->parent;
9110 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9111 elt->purpose))
9113 next = elt->purpose;
9114 break;
9119 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9121 tree ctor_unfilled_bitpos, elt_bitpos;
9123 /* If the current record is complete we are done. */
9124 if (constructor_unfilled_fields == 0)
9125 break;
9127 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9128 elt_bitpos = bit_position (elt->purpose);
9129 /* We can't compare fields here because there might be empty
9130 fields in between. */
9131 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9133 constructor_unfilled_fields = elt->purpose;
9134 output_init_element (input_location, elt->value, elt->origtype,
9135 true, TREE_TYPE (elt->purpose),
9136 elt->purpose, 0, false,
9137 braced_init_obstack);
9139 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9141 /* Advance to the next smaller node. */
9142 if (elt->left)
9143 elt = elt->left;
9144 else
9146 /* We have reached the smallest node bigger than the
9147 current unfilled field. Fill the space first. */
9148 next = elt->purpose;
9149 break;
9152 else
9154 /* Advance to the next bigger node. */
9155 if (elt->right)
9156 elt = elt->right;
9157 else
9159 /* We have reached the biggest node in a subtree. Find
9160 the parent of it, which is the next bigger node. */
9161 while (elt->parent && elt->parent->right == elt)
9162 elt = elt->parent;
9163 elt = elt->parent;
9164 if (elt
9165 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9166 bit_position (elt->purpose))))
9168 next = elt->purpose;
9169 break;
9176 /* Ordinarily return, but not if we want to output all
9177 and there are elements left. */
9178 if (!(all && next != 0))
9179 return;
9181 /* If it's not incremental, just skip over the gap, so that after
9182 jumping to retry we will output the next successive element. */
9183 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9184 constructor_unfilled_fields = next;
9185 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9186 constructor_unfilled_index = next;
9188 /* ELT now points to the node in the pending tree with the next
9189 initializer to output. */
9190 goto retry;
9193 /* Add one non-braced element to the current constructor level.
9194 This adjusts the current position within the constructor's type.
9195 This may also start or terminate implicit levels
9196 to handle a partly-braced initializer.
9198 Once this has found the correct level for the new element,
9199 it calls output_init_element.
9201 IMPLICIT is true if value comes from pop_init_level (1),
9202 the new initializer has been merged with the existing one
9203 and thus no warnings should be emitted about overriding an
9204 existing initializer. */
9206 void
9207 process_init_element (location_t loc, struct c_expr value, bool implicit,
9208 struct obstack * braced_init_obstack)
9210 tree orig_value = value.value;
9211 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9212 bool strict_string = value.original_code == STRING_CST;
9213 bool was_designated = designator_depth != 0;
9215 designator_depth = 0;
9216 designator_erroneous = 0;
9218 if (!implicit && value.value && !integer_zerop (value.value))
9219 constructor_zeroinit = 0;
9221 /* Handle superfluous braces around string cst as in
9222 char x[] = {"foo"}; */
9223 if (string_flag
9224 && constructor_type
9225 && !was_designated
9226 && TREE_CODE (constructor_type) == ARRAY_TYPE
9227 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9228 && integer_zerop (constructor_unfilled_index))
9230 if (constructor_stack->replacement_value.value)
9231 error_init (loc, "excess elements in char array initializer");
9232 constructor_stack->replacement_value = value;
9233 return;
9236 if (constructor_stack->replacement_value.value != 0)
9238 error_init (loc, "excess elements in struct initializer");
9239 return;
9242 /* Ignore elements of a brace group if it is entirely superfluous
9243 and has already been diagnosed. */
9244 if (constructor_type == 0)
9245 return;
9247 if (!implicit && warn_designated_init && !was_designated
9248 && TREE_CODE (constructor_type) == RECORD_TYPE
9249 && lookup_attribute ("designated_init",
9250 TYPE_ATTRIBUTES (constructor_type)))
9251 warning_init (loc,
9252 OPT_Wdesignated_init,
9253 "positional initialization of field "
9254 "in %<struct%> declared with %<designated_init%> attribute");
9256 /* If we've exhausted any levels that didn't have braces,
9257 pop them now. */
9258 while (constructor_stack->implicit)
9260 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9261 && constructor_fields == 0)
9262 process_init_element (loc,
9263 pop_init_level (loc, 1, braced_init_obstack,
9264 last_init_list_comma),
9265 true, braced_init_obstack);
9266 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9267 || VECTOR_TYPE_P (constructor_type))
9268 && constructor_max_index
9269 && tree_int_cst_lt (constructor_max_index,
9270 constructor_index))
9271 process_init_element (loc,
9272 pop_init_level (loc, 1, braced_init_obstack,
9273 last_init_list_comma),
9274 true, braced_init_obstack);
9275 else
9276 break;
9279 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9280 if (constructor_range_stack)
9282 /* If value is a compound literal and we'll be just using its
9283 content, don't put it into a SAVE_EXPR. */
9284 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9285 || !require_constant_value)
9287 tree semantic_type = NULL_TREE;
9288 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9290 semantic_type = TREE_TYPE (value.value);
9291 value.value = TREE_OPERAND (value.value, 0);
9293 value.value = c_save_expr (value.value);
9294 if (semantic_type)
9295 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9296 value.value);
9300 while (1)
9302 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9304 tree fieldtype;
9305 enum tree_code fieldcode;
9307 if (constructor_fields == 0)
9309 pedwarn_init (loc, 0, "excess elements in struct initializer");
9310 break;
9313 fieldtype = TREE_TYPE (constructor_fields);
9314 if (fieldtype != error_mark_node)
9315 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9316 fieldcode = TREE_CODE (fieldtype);
9318 /* Error for non-static initialization of a flexible array member. */
9319 if (fieldcode == ARRAY_TYPE
9320 && !require_constant_value
9321 && TYPE_SIZE (fieldtype) == NULL_TREE
9322 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9324 error_init (loc, "non-static initialization of a flexible "
9325 "array member");
9326 break;
9329 /* Error for initialization of a flexible array member with
9330 a string constant if the structure is in an array. E.g.:
9331 struct S { int x; char y[]; };
9332 struct S s[] = { { 1, "foo" } };
9333 is invalid. */
9334 if (string_flag
9335 && fieldcode == ARRAY_TYPE
9336 && constructor_depth > 1
9337 && TYPE_SIZE (fieldtype) == NULL_TREE
9338 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9340 bool in_array_p = false;
9341 for (struct constructor_stack *p = constructor_stack;
9342 p && p->type; p = p->next)
9343 if (TREE_CODE (p->type) == ARRAY_TYPE)
9345 in_array_p = true;
9346 break;
9348 if (in_array_p)
9350 error_init (loc, "initialization of flexible array "
9351 "member in a nested context");
9352 break;
9356 /* Accept a string constant to initialize a subarray. */
9357 if (value.value != 0
9358 && fieldcode == ARRAY_TYPE
9359 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9360 && string_flag)
9361 value.value = orig_value;
9362 /* Otherwise, if we have come to a subaggregate,
9363 and we don't have an element of its type, push into it. */
9364 else if (value.value != 0
9365 && value.value != error_mark_node
9366 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9367 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9368 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9370 push_init_level (loc, 1, braced_init_obstack);
9371 continue;
9374 if (value.value)
9376 push_member_name (constructor_fields);
9377 output_init_element (loc, value.value, value.original_type,
9378 strict_string, fieldtype,
9379 constructor_fields, 1, implicit,
9380 braced_init_obstack);
9381 RESTORE_SPELLING_DEPTH (constructor_depth);
9383 else
9384 /* Do the bookkeeping for an element that was
9385 directly output as a constructor. */
9387 /* For a record, keep track of end position of last field. */
9388 if (DECL_SIZE (constructor_fields))
9389 constructor_bit_index
9390 = size_binop_loc (input_location, PLUS_EXPR,
9391 bit_position (constructor_fields),
9392 DECL_SIZE (constructor_fields));
9394 /* If the current field was the first one not yet written out,
9395 it isn't now, so update. */
9396 if (constructor_unfilled_fields == constructor_fields)
9398 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9399 /* Skip any nameless bit fields. */
9400 while (constructor_unfilled_fields != 0
9401 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9402 && DECL_NAME (constructor_unfilled_fields) == 0)
9403 constructor_unfilled_fields =
9404 DECL_CHAIN (constructor_unfilled_fields);
9408 constructor_fields = DECL_CHAIN (constructor_fields);
9409 /* Skip any nameless bit fields at the beginning. */
9410 while (constructor_fields != 0
9411 && DECL_C_BIT_FIELD (constructor_fields)
9412 && DECL_NAME (constructor_fields) == 0)
9413 constructor_fields = DECL_CHAIN (constructor_fields);
9415 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9417 tree fieldtype;
9418 enum tree_code fieldcode;
9420 if (constructor_fields == 0)
9422 pedwarn_init (loc, 0,
9423 "excess elements in union initializer");
9424 break;
9427 fieldtype = TREE_TYPE (constructor_fields);
9428 if (fieldtype != error_mark_node)
9429 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9430 fieldcode = TREE_CODE (fieldtype);
9432 /* Warn that traditional C rejects initialization of unions.
9433 We skip the warning if the value is zero. This is done
9434 under the assumption that the zero initializer in user
9435 code appears conditioned on e.g. __STDC__ to avoid
9436 "missing initializer" warnings and relies on default
9437 initialization to zero in the traditional C case.
9438 We also skip the warning if the initializer is designated,
9439 again on the assumption that this must be conditional on
9440 __STDC__ anyway (and we've already complained about the
9441 member-designator already). */
9442 if (!in_system_header_at (input_location) && !constructor_designated
9443 && !(value.value && (integer_zerop (value.value)
9444 || real_zerop (value.value))))
9445 warning (OPT_Wtraditional, "traditional C rejects initialization "
9446 "of unions");
9448 /* Accept a string constant to initialize a subarray. */
9449 if (value.value != 0
9450 && fieldcode == ARRAY_TYPE
9451 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9452 && string_flag)
9453 value.value = orig_value;
9454 /* Otherwise, if we have come to a subaggregate,
9455 and we don't have an element of its type, push into it. */
9456 else if (value.value != 0
9457 && value.value != error_mark_node
9458 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9459 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9460 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9462 push_init_level (loc, 1, braced_init_obstack);
9463 continue;
9466 if (value.value)
9468 push_member_name (constructor_fields);
9469 output_init_element (loc, value.value, value.original_type,
9470 strict_string, fieldtype,
9471 constructor_fields, 1, implicit,
9472 braced_init_obstack);
9473 RESTORE_SPELLING_DEPTH (constructor_depth);
9475 else
9476 /* Do the bookkeeping for an element that was
9477 directly output as a constructor. */
9479 constructor_bit_index = DECL_SIZE (constructor_fields);
9480 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9483 constructor_fields = 0;
9485 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9487 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9488 enum tree_code eltcode = TREE_CODE (elttype);
9490 /* Accept a string constant to initialize a subarray. */
9491 if (value.value != 0
9492 && eltcode == ARRAY_TYPE
9493 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9494 && string_flag)
9495 value.value = orig_value;
9496 /* Otherwise, if we have come to a subaggregate,
9497 and we don't have an element of its type, push into it. */
9498 else if (value.value != 0
9499 && value.value != error_mark_node
9500 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9501 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9502 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9504 push_init_level (loc, 1, braced_init_obstack);
9505 continue;
9508 if (constructor_max_index != 0
9509 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9510 || integer_all_onesp (constructor_max_index)))
9512 pedwarn_init (loc, 0,
9513 "excess elements in array initializer");
9514 break;
9517 /* Now output the actual element. */
9518 if (value.value)
9520 push_array_bounds (tree_to_uhwi (constructor_index));
9521 output_init_element (loc, value.value, value.original_type,
9522 strict_string, elttype,
9523 constructor_index, 1, implicit,
9524 braced_init_obstack);
9525 RESTORE_SPELLING_DEPTH (constructor_depth);
9528 constructor_index
9529 = size_binop_loc (input_location, PLUS_EXPR,
9530 constructor_index, bitsize_one_node);
9532 if (!value.value)
9533 /* If we are doing the bookkeeping for an element that was
9534 directly output as a constructor, we must update
9535 constructor_unfilled_index. */
9536 constructor_unfilled_index = constructor_index;
9538 else if (VECTOR_TYPE_P (constructor_type))
9540 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9542 /* Do a basic check of initializer size. Note that vectors
9543 always have a fixed size derived from their type. */
9544 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9546 pedwarn_init (loc, 0,
9547 "excess elements in vector initializer");
9548 break;
9551 /* Now output the actual element. */
9552 if (value.value)
9554 if (TREE_CODE (value.value) == VECTOR_CST)
9555 elttype = TYPE_MAIN_VARIANT (constructor_type);
9556 output_init_element (loc, value.value, value.original_type,
9557 strict_string, elttype,
9558 constructor_index, 1, implicit,
9559 braced_init_obstack);
9562 constructor_index
9563 = size_binop_loc (input_location,
9564 PLUS_EXPR, constructor_index, bitsize_one_node);
9566 if (!value.value)
9567 /* If we are doing the bookkeeping for an element that was
9568 directly output as a constructor, we must update
9569 constructor_unfilled_index. */
9570 constructor_unfilled_index = constructor_index;
9573 /* Handle the sole element allowed in a braced initializer
9574 for a scalar variable. */
9575 else if (constructor_type != error_mark_node
9576 && constructor_fields == 0)
9578 pedwarn_init (loc, 0,
9579 "excess elements in scalar initializer");
9580 break;
9582 else
9584 if (value.value)
9585 output_init_element (loc, value.value, value.original_type,
9586 strict_string, constructor_type,
9587 NULL_TREE, 1, implicit,
9588 braced_init_obstack);
9589 constructor_fields = 0;
9592 /* Handle range initializers either at this level or anywhere higher
9593 in the designator stack. */
9594 if (constructor_range_stack)
9596 struct constructor_range_stack *p, *range_stack;
9597 int finish = 0;
9599 range_stack = constructor_range_stack;
9600 constructor_range_stack = 0;
9601 while (constructor_stack != range_stack->stack)
9603 gcc_assert (constructor_stack->implicit);
9604 process_init_element (loc,
9605 pop_init_level (loc, 1,
9606 braced_init_obstack,
9607 last_init_list_comma),
9608 true, braced_init_obstack);
9610 for (p = range_stack;
9611 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9612 p = p->prev)
9614 gcc_assert (constructor_stack->implicit);
9615 process_init_element (loc,
9616 pop_init_level (loc, 1,
9617 braced_init_obstack,
9618 last_init_list_comma),
9619 true, braced_init_obstack);
9622 p->index = size_binop_loc (input_location,
9623 PLUS_EXPR, p->index, bitsize_one_node);
9624 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9625 finish = 1;
9627 while (1)
9629 constructor_index = p->index;
9630 constructor_fields = p->fields;
9631 if (finish && p->range_end && p->index == p->range_start)
9633 finish = 0;
9634 p->prev = 0;
9636 p = p->next;
9637 if (!p)
9638 break;
9639 finish_implicit_inits (loc, braced_init_obstack);
9640 push_init_level (loc, 2, braced_init_obstack);
9641 p->stack = constructor_stack;
9642 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9643 p->index = p->range_start;
9646 if (!finish)
9647 constructor_range_stack = range_stack;
9648 continue;
9651 break;
9654 constructor_range_stack = 0;
9657 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9658 (guaranteed to be 'volatile' or null) and ARGS (represented using
9659 an ASM_EXPR node). */
9660 tree
9661 build_asm_stmt (tree cv_qualifier, tree args)
9663 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9664 ASM_VOLATILE_P (args) = 1;
9665 return add_stmt (args);
9668 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9669 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9670 SIMPLE indicates whether there was anything at all after the
9671 string in the asm expression -- asm("blah") and asm("blah" : )
9672 are subtly different. We use a ASM_EXPR node to represent this. */
9673 tree
9674 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9675 tree clobbers, tree labels, bool simple)
9677 tree tail;
9678 tree args;
9679 int i;
9680 const char *constraint;
9681 const char **oconstraints;
9682 bool allows_mem, allows_reg, is_inout;
9683 int ninputs, noutputs;
9685 ninputs = list_length (inputs);
9686 noutputs = list_length (outputs);
9687 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9689 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9691 /* Remove output conversions that change the type but not the mode. */
9692 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9694 tree output = TREE_VALUE (tail);
9696 output = c_fully_fold (output, false, NULL);
9698 /* ??? Really, this should not be here. Users should be using a
9699 proper lvalue, dammit. But there's a long history of using casts
9700 in the output operands. In cases like longlong.h, this becomes a
9701 primitive form of typechecking -- if the cast can be removed, then
9702 the output operand had a type of the proper width; otherwise we'll
9703 get an error. Gross, but ... */
9704 STRIP_NOPS (output);
9706 if (!lvalue_or_else (loc, output, lv_asm))
9707 output = error_mark_node;
9709 if (output != error_mark_node
9710 && (TREE_READONLY (output)
9711 || TYPE_READONLY (TREE_TYPE (output))
9712 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9713 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9714 readonly_error (loc, output, lv_asm);
9716 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9717 oconstraints[i] = constraint;
9719 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9720 &allows_mem, &allows_reg, &is_inout))
9722 /* If the operand is going to end up in memory,
9723 mark it addressable. */
9724 if (!allows_reg && !c_mark_addressable (output))
9725 output = error_mark_node;
9726 if (!(!allows_reg && allows_mem)
9727 && output != error_mark_node
9728 && VOID_TYPE_P (TREE_TYPE (output)))
9730 error_at (loc, "invalid use of void expression");
9731 output = error_mark_node;
9734 else
9735 output = error_mark_node;
9737 TREE_VALUE (tail) = output;
9740 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9742 tree input;
9744 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9745 input = TREE_VALUE (tail);
9747 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9748 oconstraints, &allows_mem, &allows_reg))
9750 /* If the operand is going to end up in memory,
9751 mark it addressable. */
9752 if (!allows_reg && allows_mem)
9754 input = c_fully_fold (input, false, NULL);
9756 /* Strip the nops as we allow this case. FIXME, this really
9757 should be rejected or made deprecated. */
9758 STRIP_NOPS (input);
9759 if (!c_mark_addressable (input))
9760 input = error_mark_node;
9762 else
9764 struct c_expr expr;
9765 memset (&expr, 0, sizeof (expr));
9766 expr.value = input;
9767 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9768 input = c_fully_fold (expr.value, false, NULL);
9770 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9772 error_at (loc, "invalid use of void expression");
9773 input = error_mark_node;
9777 else
9778 input = error_mark_node;
9780 TREE_VALUE (tail) = input;
9783 /* ASMs with labels cannot have outputs. This should have been
9784 enforced by the parser. */
9785 gcc_assert (outputs == NULL || labels == NULL);
9787 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9789 /* asm statements without outputs, including simple ones, are treated
9790 as volatile. */
9791 ASM_INPUT_P (args) = simple;
9792 ASM_VOLATILE_P (args) = (noutputs == 0);
9794 return args;
9797 /* Generate a goto statement to LABEL. LOC is the location of the
9798 GOTO. */
9800 tree
9801 c_finish_goto_label (location_t loc, tree label)
9803 tree decl = lookup_label_for_goto (loc, label);
9804 if (!decl)
9805 return NULL_TREE;
9806 TREE_USED (decl) = 1;
9808 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9809 SET_EXPR_LOCATION (t, loc);
9810 return add_stmt (t);
9814 /* Generate a computed goto statement to EXPR. LOC is the location of
9815 the GOTO. */
9817 tree
9818 c_finish_goto_ptr (location_t loc, tree expr)
9820 tree t;
9821 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9822 expr = c_fully_fold (expr, false, NULL);
9823 expr = convert (ptr_type_node, expr);
9824 t = build1 (GOTO_EXPR, void_type_node, expr);
9825 SET_EXPR_LOCATION (t, loc);
9826 return add_stmt (t);
9829 /* Generate a C `return' statement. RETVAL is the expression for what
9830 to return, or a null pointer for `return;' with no value. LOC is
9831 the location of the return statement, or the location of the expression,
9832 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9833 is the original type of RETVAL. */
9835 tree
9836 c_finish_return (location_t loc, tree retval, tree origtype)
9838 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9839 bool no_warning = false;
9840 bool npc = false;
9841 size_t rank = 0;
9843 /* Use the expansion point to handle cases such as returning NULL
9844 in a function returning void. */
9845 source_location xloc = expansion_point_location_if_in_system_header (loc);
9847 if (TREE_THIS_VOLATILE (current_function_decl))
9848 warning_at (xloc, 0,
9849 "function declared %<noreturn%> has a %<return%> statement");
9851 if (flag_cilkplus && contains_array_notation_expr (retval))
9853 /* Array notations are allowed in a return statement if it is inside a
9854 built-in array notation reduction function. */
9855 if (!find_rank (loc, retval, retval, false, &rank))
9856 return error_mark_node;
9857 if (rank >= 1)
9859 error_at (loc, "array notation expression cannot be used as a "
9860 "return value");
9861 return error_mark_node;
9864 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9866 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9867 "allowed");
9868 return error_mark_node;
9870 if (retval)
9872 tree semantic_type = NULL_TREE;
9873 npc = null_pointer_constant_p (retval);
9874 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9876 semantic_type = TREE_TYPE (retval);
9877 retval = TREE_OPERAND (retval, 0);
9879 retval = c_fully_fold (retval, false, NULL);
9880 if (semantic_type)
9881 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9884 if (!retval)
9886 current_function_returns_null = 1;
9887 if ((warn_return_type || flag_isoc99)
9888 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9890 bool warned_here;
9891 if (flag_isoc99)
9892 warned_here = pedwarn
9893 (loc, 0,
9894 "%<return%> with no value, in function returning non-void");
9895 else
9896 warned_here = warning_at
9897 (loc, OPT_Wreturn_type,
9898 "%<return%> with no value, in function returning non-void");
9899 no_warning = true;
9900 if (warned_here)
9901 inform (DECL_SOURCE_LOCATION (current_function_decl),
9902 "declared here");
9905 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9907 current_function_returns_null = 1;
9908 bool warned_here;
9909 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9910 warned_here = pedwarn
9911 (xloc, 0,
9912 "%<return%> with a value, in function returning void");
9913 else
9914 warned_here = pedwarn
9915 (xloc, OPT_Wpedantic, "ISO C forbids "
9916 "%<return%> with expression, in function returning void");
9917 if (warned_here)
9918 inform (DECL_SOURCE_LOCATION (current_function_decl),
9919 "declared here");
9921 else
9923 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9924 retval, origtype, ic_return,
9925 npc, NULL_TREE, NULL_TREE, 0);
9926 tree res = DECL_RESULT (current_function_decl);
9927 tree inner;
9928 bool save;
9930 current_function_returns_value = 1;
9931 if (t == error_mark_node)
9932 return NULL_TREE;
9934 save = in_late_binary_op;
9935 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9936 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9937 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9938 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9939 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9940 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9941 in_late_binary_op = true;
9942 inner = t = convert (TREE_TYPE (res), t);
9943 in_late_binary_op = save;
9945 /* Strip any conversions, additions, and subtractions, and see if
9946 we are returning the address of a local variable. Warn if so. */
9947 while (1)
9949 switch (TREE_CODE (inner))
9951 CASE_CONVERT:
9952 case NON_LVALUE_EXPR:
9953 case PLUS_EXPR:
9954 case POINTER_PLUS_EXPR:
9955 inner = TREE_OPERAND (inner, 0);
9956 continue;
9958 case MINUS_EXPR:
9959 /* If the second operand of the MINUS_EXPR has a pointer
9960 type (or is converted from it), this may be valid, so
9961 don't give a warning. */
9963 tree op1 = TREE_OPERAND (inner, 1);
9965 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9966 && (CONVERT_EXPR_P (op1)
9967 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9968 op1 = TREE_OPERAND (op1, 0);
9970 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9971 break;
9973 inner = TREE_OPERAND (inner, 0);
9974 continue;
9977 case ADDR_EXPR:
9978 inner = TREE_OPERAND (inner, 0);
9980 while (REFERENCE_CLASS_P (inner)
9981 && !INDIRECT_REF_P (inner))
9982 inner = TREE_OPERAND (inner, 0);
9984 if (DECL_P (inner)
9985 && !DECL_EXTERNAL (inner)
9986 && !TREE_STATIC (inner)
9987 && DECL_CONTEXT (inner) == current_function_decl)
9989 if (TREE_CODE (inner) == LABEL_DECL)
9990 warning_at (loc, OPT_Wreturn_local_addr,
9991 "function returns address of label");
9992 else
9994 warning_at (loc, OPT_Wreturn_local_addr,
9995 "function returns address of local variable");
9996 tree zero = build_zero_cst (TREE_TYPE (res));
9997 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10000 break;
10002 default:
10003 break;
10006 break;
10009 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10010 SET_EXPR_LOCATION (retval, loc);
10012 if (warn_sequence_point)
10013 verify_sequence_points (retval);
10016 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10017 TREE_NO_WARNING (ret_stmt) |= no_warning;
10018 return add_stmt (ret_stmt);
10021 struct c_switch {
10022 /* The SWITCH_EXPR being built. */
10023 tree switch_expr;
10025 /* The original type of the testing expression, i.e. before the
10026 default conversion is applied. */
10027 tree orig_type;
10029 /* A splay-tree mapping the low element of a case range to the high
10030 element, or NULL_TREE if there is no high element. Used to
10031 determine whether or not a new case label duplicates an old case
10032 label. We need a tree, rather than simply a hash table, because
10033 of the GNU case range extension. */
10034 splay_tree cases;
10036 /* The bindings at the point of the switch. This is used for
10037 warnings crossing decls when branching to a case label. */
10038 struct c_spot_bindings *bindings;
10040 /* The next node on the stack. */
10041 struct c_switch *next;
10043 /* Remember whether the controlling expression had boolean type
10044 before integer promotions for the sake of -Wswitch-bool. */
10045 bool bool_cond_p;
10047 /* Remember whether there was a case value that is outside the
10048 range of the ORIG_TYPE. */
10049 bool outside_range_p;
10052 /* A stack of the currently active switch statements. The innermost
10053 switch statement is on the top of the stack. There is no need to
10054 mark the stack for garbage collection because it is only active
10055 during the processing of the body of a function, and we never
10056 collect at that point. */
10058 struct c_switch *c_switch_stack;
10060 /* Start a C switch statement, testing expression EXP. Return the new
10061 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10062 SWITCH_COND_LOC is the location of the switch's condition.
10063 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10065 tree
10066 c_start_case (location_t switch_loc,
10067 location_t switch_cond_loc,
10068 tree exp, bool explicit_cast_p)
10070 tree orig_type = error_mark_node;
10071 bool bool_cond_p = false;
10072 struct c_switch *cs;
10074 if (exp != error_mark_node)
10076 orig_type = TREE_TYPE (exp);
10078 if (!INTEGRAL_TYPE_P (orig_type))
10080 if (orig_type != error_mark_node)
10082 error_at (switch_cond_loc, "switch quantity not an integer");
10083 orig_type = error_mark_node;
10085 exp = integer_zero_node;
10087 else
10089 tree type = TYPE_MAIN_VARIANT (orig_type);
10090 tree e = exp;
10092 /* Warn if the condition has boolean value. */
10093 while (TREE_CODE (e) == COMPOUND_EXPR)
10094 e = TREE_OPERAND (e, 1);
10096 if ((TREE_CODE (type) == BOOLEAN_TYPE
10097 || truth_value_p (TREE_CODE (e)))
10098 /* Explicit cast to int suppresses this warning. */
10099 && !(TREE_CODE (type) == INTEGER_TYPE
10100 && explicit_cast_p))
10101 bool_cond_p = true;
10103 if (!in_system_header_at (input_location)
10104 && (type == long_integer_type_node
10105 || type == long_unsigned_type_node))
10106 warning_at (switch_cond_loc,
10107 OPT_Wtraditional, "%<long%> switch expression not "
10108 "converted to %<int%> in ISO C");
10110 exp = c_fully_fold (exp, false, NULL);
10111 exp = default_conversion (exp);
10113 if (warn_sequence_point)
10114 verify_sequence_points (exp);
10118 /* Add this new SWITCH_EXPR to the stack. */
10119 cs = XNEW (struct c_switch);
10120 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10121 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10122 cs->orig_type = orig_type;
10123 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10124 cs->bindings = c_get_switch_bindings ();
10125 cs->bool_cond_p = bool_cond_p;
10126 cs->outside_range_p = false;
10127 cs->next = c_switch_stack;
10128 c_switch_stack = cs;
10130 return add_stmt (cs->switch_expr);
10133 /* Process a case label at location LOC. */
10135 tree
10136 do_case (location_t loc, tree low_value, tree high_value)
10138 tree label = NULL_TREE;
10140 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10142 low_value = c_fully_fold (low_value, false, NULL);
10143 if (TREE_CODE (low_value) == INTEGER_CST)
10144 pedwarn (loc, OPT_Wpedantic,
10145 "case label is not an integer constant expression");
10148 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10150 high_value = c_fully_fold (high_value, false, NULL);
10151 if (TREE_CODE (high_value) == INTEGER_CST)
10152 pedwarn (input_location, OPT_Wpedantic,
10153 "case label is not an integer constant expression");
10156 if (c_switch_stack == NULL)
10158 if (low_value)
10159 error_at (loc, "case label not within a switch statement");
10160 else
10161 error_at (loc, "%<default%> label not within a switch statement");
10162 return NULL_TREE;
10165 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10166 EXPR_LOCATION (c_switch_stack->switch_expr),
10167 loc))
10168 return NULL_TREE;
10170 label = c_add_case_label (loc, c_switch_stack->cases,
10171 SWITCH_COND (c_switch_stack->switch_expr),
10172 c_switch_stack->orig_type,
10173 low_value, high_value,
10174 &c_switch_stack->outside_range_p);
10175 if (label == error_mark_node)
10176 label = NULL_TREE;
10177 return label;
10180 /* Finish the switch statement. TYPE is the original type of the
10181 controlling expression of the switch, or NULL_TREE. */
10183 void
10184 c_finish_case (tree body, tree type)
10186 struct c_switch *cs = c_switch_stack;
10187 location_t switch_location;
10189 SWITCH_BODY (cs->switch_expr) = body;
10191 /* Emit warnings as needed. */
10192 switch_location = EXPR_LOCATION (cs->switch_expr);
10193 c_do_switch_warnings (cs->cases, switch_location,
10194 type ? type : TREE_TYPE (cs->switch_expr),
10195 SWITCH_COND (cs->switch_expr),
10196 cs->bool_cond_p, cs->outside_range_p);
10198 /* Pop the stack. */
10199 c_switch_stack = cs->next;
10200 splay_tree_delete (cs->cases);
10201 c_release_switch_bindings (cs->bindings);
10202 XDELETE (cs);
10205 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10206 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10207 may be null. */
10209 void
10210 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10211 tree else_block)
10213 tree stmt;
10215 /* If the condition has array notations, then the rank of the then_block and
10216 else_block must be either 0 or be equal to the rank of the condition. If
10217 the condition does not have array notations then break them up as it is
10218 broken up in a normal expression. */
10219 if (flag_cilkplus && contains_array_notation_expr (cond))
10221 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10222 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10223 return;
10224 if (then_block
10225 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10226 return;
10227 if (else_block
10228 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10229 return;
10230 if (cond_rank != then_rank && then_rank != 0)
10232 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10233 " and the then-block");
10234 return;
10236 else if (cond_rank != else_rank && else_rank != 0)
10238 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10239 " and the else-block");
10240 return;
10244 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10245 SET_EXPR_LOCATION (stmt, if_locus);
10246 add_stmt (stmt);
10249 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10250 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10251 is false for DO loops. INCR is the FOR increment expression. BODY is
10252 the statement controlled by the loop. BLAB is the break label. CLAB is
10253 the continue label. Everything is allowed to be NULL. */
10255 void
10256 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10257 tree blab, tree clab, bool cond_is_first)
10259 tree entry = NULL, exit = NULL, t;
10261 /* In theory could forbid cilk spawn for loop increment expression,
10262 but it should work just fine. */
10264 /* If the condition is zero don't generate a loop construct. */
10265 if (cond && integer_zerop (cond))
10267 if (cond_is_first)
10269 t = build_and_jump (&blab);
10270 SET_EXPR_LOCATION (t, start_locus);
10271 add_stmt (t);
10274 else
10276 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10278 /* If we have an exit condition, then we build an IF with gotos either
10279 out of the loop, or to the top of it. If there's no exit condition,
10280 then we just build a jump back to the top. */
10281 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10283 if (cond && !integer_nonzerop (cond))
10285 /* Canonicalize the loop condition to the end. This means
10286 generating a branch to the loop condition. Reuse the
10287 continue label, if possible. */
10288 if (cond_is_first)
10290 if (incr || !clab)
10292 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10293 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10295 else
10296 t = build1 (GOTO_EXPR, void_type_node, clab);
10297 SET_EXPR_LOCATION (t, start_locus);
10298 add_stmt (t);
10301 t = build_and_jump (&blab);
10302 if (cond_is_first)
10303 exit = fold_build3_loc (start_locus,
10304 COND_EXPR, void_type_node, cond, exit, t);
10305 else
10306 exit = fold_build3_loc (input_location,
10307 COND_EXPR, void_type_node, cond, exit, t);
10309 else
10311 /* For the backward-goto's location of an unconditional loop
10312 use the beginning of the body, or, if there is none, the
10313 top of the loop. */
10314 location_t loc = EXPR_LOCATION (expr_first (body));
10315 if (loc == UNKNOWN_LOCATION)
10316 loc = start_locus;
10317 SET_EXPR_LOCATION (exit, loc);
10320 add_stmt (top);
10323 if (body)
10324 add_stmt (body);
10325 if (clab)
10326 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10327 if (incr)
10328 add_stmt (incr);
10329 if (entry)
10330 add_stmt (entry);
10331 if (exit)
10332 add_stmt (exit);
10333 if (blab)
10334 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10337 tree
10338 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10340 bool skip;
10341 tree label = *label_p;
10343 /* In switch statements break is sometimes stylistically used after
10344 a return statement. This can lead to spurious warnings about
10345 control reaching the end of a non-void function when it is
10346 inlined. Note that we are calling block_may_fallthru with
10347 language specific tree nodes; this works because
10348 block_may_fallthru returns true when given something it does not
10349 understand. */
10350 skip = !block_may_fallthru (cur_stmt_list);
10352 if (!label)
10354 if (!skip)
10355 *label_p = label = create_artificial_label (loc);
10357 else if (TREE_CODE (label) == LABEL_DECL)
10359 else switch (TREE_INT_CST_LOW (label))
10361 case 0:
10362 if (is_break)
10363 error_at (loc, "break statement not within loop or switch");
10364 else
10365 error_at (loc, "continue statement not within a loop");
10366 return NULL_TREE;
10368 case 1:
10369 gcc_assert (is_break);
10370 error_at (loc, "break statement used with OpenMP for loop");
10371 return NULL_TREE;
10373 case 2:
10374 if (is_break)
10375 error ("break statement within %<#pragma simd%> loop body");
10376 else
10377 error ("continue statement within %<#pragma simd%> loop body");
10378 return NULL_TREE;
10380 default:
10381 gcc_unreachable ();
10384 if (skip)
10385 return NULL_TREE;
10387 if (!is_break)
10388 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10390 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10393 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10395 static void
10396 emit_side_effect_warnings (location_t loc, tree expr)
10398 if (expr == error_mark_node)
10400 else if (!TREE_SIDE_EFFECTS (expr))
10402 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10403 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10405 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10407 tree r = expr;
10408 location_t cloc = loc;
10409 while (TREE_CODE (r) == COMPOUND_EXPR)
10411 if (EXPR_HAS_LOCATION (r))
10412 cloc = EXPR_LOCATION (r);
10413 r = TREE_OPERAND (r, 1);
10415 if (!TREE_SIDE_EFFECTS (r)
10416 && !VOID_TYPE_P (TREE_TYPE (r))
10417 && !CONVERT_EXPR_P (r)
10418 && !TREE_NO_WARNING (r)
10419 && !TREE_NO_WARNING (expr))
10420 warning_at (cloc, OPT_Wunused_value,
10421 "right-hand operand of comma expression has no effect");
10423 else
10424 warn_if_unused_value (expr, loc);
10427 /* Process an expression as if it were a complete statement. Emit
10428 diagnostics, but do not call ADD_STMT. LOC is the location of the
10429 statement. */
10431 tree
10432 c_process_expr_stmt (location_t loc, tree expr)
10434 tree exprv;
10436 if (!expr)
10437 return NULL_TREE;
10439 expr = c_fully_fold (expr, false, NULL);
10441 if (warn_sequence_point)
10442 verify_sequence_points (expr);
10444 if (TREE_TYPE (expr) != error_mark_node
10445 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10446 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10447 error_at (loc, "expression statement has incomplete type");
10449 /* If we're not processing a statement expression, warn about unused values.
10450 Warnings for statement expressions will be emitted later, once we figure
10451 out which is the result. */
10452 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10453 && warn_unused_value)
10454 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10456 exprv = expr;
10457 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10458 exprv = TREE_OPERAND (exprv, 1);
10459 while (CONVERT_EXPR_P (exprv))
10460 exprv = TREE_OPERAND (exprv, 0);
10461 if (DECL_P (exprv)
10462 || handled_component_p (exprv)
10463 || TREE_CODE (exprv) == ADDR_EXPR)
10464 mark_exp_read (exprv);
10466 /* If the expression is not of a type to which we cannot assign a line
10467 number, wrap the thing in a no-op NOP_EXPR. */
10468 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10470 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10471 SET_EXPR_LOCATION (expr, loc);
10474 return expr;
10477 /* Emit an expression as a statement. LOC is the location of the
10478 expression. */
10480 tree
10481 c_finish_expr_stmt (location_t loc, tree expr)
10483 if (expr)
10484 return add_stmt (c_process_expr_stmt (loc, expr));
10485 else
10486 return NULL;
10489 /* Do the opposite and emit a statement as an expression. To begin,
10490 create a new binding level and return it. */
10492 tree
10493 c_begin_stmt_expr (void)
10495 tree ret;
10497 /* We must force a BLOCK for this level so that, if it is not expanded
10498 later, there is a way to turn off the entire subtree of blocks that
10499 are contained in it. */
10500 keep_next_level ();
10501 ret = c_begin_compound_stmt (true);
10503 c_bindings_start_stmt_expr (c_switch_stack == NULL
10504 ? NULL
10505 : c_switch_stack->bindings);
10507 /* Mark the current statement list as belonging to a statement list. */
10508 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10510 return ret;
10513 /* LOC is the location of the compound statement to which this body
10514 belongs. */
10516 tree
10517 c_finish_stmt_expr (location_t loc, tree body)
10519 tree last, type, tmp, val;
10520 tree *last_p;
10522 body = c_end_compound_stmt (loc, body, true);
10524 c_bindings_end_stmt_expr (c_switch_stack == NULL
10525 ? NULL
10526 : c_switch_stack->bindings);
10528 /* Locate the last statement in BODY. See c_end_compound_stmt
10529 about always returning a BIND_EXPR. */
10530 last_p = &BIND_EXPR_BODY (body);
10531 last = BIND_EXPR_BODY (body);
10533 continue_searching:
10534 if (TREE_CODE (last) == STATEMENT_LIST)
10536 tree_stmt_iterator i;
10538 /* This can happen with degenerate cases like ({ }). No value. */
10539 if (!TREE_SIDE_EFFECTS (last))
10540 return body;
10542 /* If we're supposed to generate side effects warnings, process
10543 all of the statements except the last. */
10544 if (warn_unused_value)
10546 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10548 location_t tloc;
10549 tree t = tsi_stmt (i);
10551 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10552 emit_side_effect_warnings (tloc, t);
10555 else
10556 i = tsi_last (last);
10557 last_p = tsi_stmt_ptr (i);
10558 last = *last_p;
10561 /* If the end of the list is exception related, then the list was split
10562 by a call to push_cleanup. Continue searching. */
10563 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10564 || TREE_CODE (last) == TRY_CATCH_EXPR)
10566 last_p = &TREE_OPERAND (last, 0);
10567 last = *last_p;
10568 goto continue_searching;
10571 if (last == error_mark_node)
10572 return last;
10574 /* In the case that the BIND_EXPR is not necessary, return the
10575 expression out from inside it. */
10576 if (last == BIND_EXPR_BODY (body)
10577 && BIND_EXPR_VARS (body) == NULL)
10579 /* Even if this looks constant, do not allow it in a constant
10580 expression. */
10581 last = c_wrap_maybe_const (last, true);
10582 /* Do not warn if the return value of a statement expression is
10583 unused. */
10584 TREE_NO_WARNING (last) = 1;
10585 return last;
10588 /* Extract the type of said expression. */
10589 type = TREE_TYPE (last);
10591 /* If we're not returning a value at all, then the BIND_EXPR that
10592 we already have is a fine expression to return. */
10593 if (!type || VOID_TYPE_P (type))
10594 return body;
10596 /* Now that we've located the expression containing the value, it seems
10597 silly to make voidify_wrapper_expr repeat the process. Create a
10598 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10599 tmp = create_tmp_var_raw (type);
10601 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10602 tree_expr_nonnegative_p giving up immediately. */
10603 val = last;
10604 if (TREE_CODE (val) == NOP_EXPR
10605 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10606 val = TREE_OPERAND (val, 0);
10608 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10609 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10612 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10613 SET_EXPR_LOCATION (t, loc);
10614 return t;
10618 /* Begin and end compound statements. This is as simple as pushing
10619 and popping new statement lists from the tree. */
10621 tree
10622 c_begin_compound_stmt (bool do_scope)
10624 tree stmt = push_stmt_list ();
10625 if (do_scope)
10626 push_scope ();
10627 return stmt;
10630 /* End a compound statement. STMT is the statement. LOC is the
10631 location of the compound statement-- this is usually the location
10632 of the opening brace. */
10634 tree
10635 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10637 tree block = NULL;
10639 if (do_scope)
10641 if (c_dialect_objc ())
10642 objc_clear_super_receiver ();
10643 block = pop_scope ();
10646 stmt = pop_stmt_list (stmt);
10647 stmt = c_build_bind_expr (loc, block, stmt);
10649 /* If this compound statement is nested immediately inside a statement
10650 expression, then force a BIND_EXPR to be created. Otherwise we'll
10651 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10652 STATEMENT_LISTs merge, and thus we can lose track of what statement
10653 was really last. */
10654 if (building_stmt_list_p ()
10655 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10656 && TREE_CODE (stmt) != BIND_EXPR)
10658 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10659 TREE_SIDE_EFFECTS (stmt) = 1;
10660 SET_EXPR_LOCATION (stmt, loc);
10663 return stmt;
10666 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10667 when the current scope is exited. EH_ONLY is true when this is not
10668 meant to apply to normal control flow transfer. */
10670 void
10671 push_cleanup (tree decl, tree cleanup, bool eh_only)
10673 enum tree_code code;
10674 tree stmt, list;
10675 bool stmt_expr;
10677 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10678 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10679 add_stmt (stmt);
10680 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10681 list = push_stmt_list ();
10682 TREE_OPERAND (stmt, 0) = list;
10683 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10686 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10687 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10689 static tree
10690 build_vec_cmp (tree_code code, tree type,
10691 tree arg0, tree arg1)
10693 tree zero_vec = build_zero_cst (type);
10694 tree minus_one_vec = build_minus_one_cst (type);
10695 tree cmp_type = build_same_sized_truth_vector_type (type);
10696 tree cmp = build2 (code, cmp_type, arg0, arg1);
10697 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10700 /* Build a binary-operation expression without default conversions.
10701 CODE is the kind of expression to build.
10702 LOCATION is the operator's location.
10703 This function differs from `build' in several ways:
10704 the data type of the result is computed and recorded in it,
10705 warnings are generated if arg data types are invalid,
10706 special handling for addition and subtraction of pointers is known,
10707 and some optimization is done (operations on narrow ints
10708 are done in the narrower type when that gives the same result).
10709 Constant folding is also done before the result is returned.
10711 Note that the operands will never have enumeral types, or function
10712 or array types, because either they will have the default conversions
10713 performed or they have both just been converted to some other type in which
10714 the arithmetic is to be done. */
10716 tree
10717 build_binary_op (location_t location, enum tree_code code,
10718 tree orig_op0, tree orig_op1, int convert_p)
10720 tree type0, type1, orig_type0, orig_type1;
10721 tree eptype;
10722 enum tree_code code0, code1;
10723 tree op0, op1;
10724 tree ret = error_mark_node;
10725 const char *invalid_op_diag;
10726 bool op0_int_operands, op1_int_operands;
10727 bool int_const, int_const_or_overflow, int_operands;
10729 /* Expression code to give to the expression when it is built.
10730 Normally this is CODE, which is what the caller asked for,
10731 but in some special cases we change it. */
10732 enum tree_code resultcode = code;
10734 /* Data type in which the computation is to be performed.
10735 In the simplest cases this is the common type of the arguments. */
10736 tree result_type = NULL;
10738 /* When the computation is in excess precision, the type of the
10739 final EXCESS_PRECISION_EXPR. */
10740 tree semantic_result_type = NULL;
10742 /* Nonzero means operands have already been type-converted
10743 in whatever way is necessary.
10744 Zero means they need to be converted to RESULT_TYPE. */
10745 int converted = 0;
10747 /* Nonzero means create the expression with this type, rather than
10748 RESULT_TYPE. */
10749 tree build_type = 0;
10751 /* Nonzero means after finally constructing the expression
10752 convert it to this type. */
10753 tree final_type = 0;
10755 /* Nonzero if this is an operation like MIN or MAX which can
10756 safely be computed in short if both args are promoted shorts.
10757 Also implies COMMON.
10758 -1 indicates a bitwise operation; this makes a difference
10759 in the exact conditions for when it is safe to do the operation
10760 in a narrower mode. */
10761 int shorten = 0;
10763 /* Nonzero if this is a comparison operation;
10764 if both args are promoted shorts, compare the original shorts.
10765 Also implies COMMON. */
10766 int short_compare = 0;
10768 /* Nonzero if this is a right-shift operation, which can be computed on the
10769 original short and then promoted if the operand is a promoted short. */
10770 int short_shift = 0;
10772 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10773 int common = 0;
10775 /* True means types are compatible as far as ObjC is concerned. */
10776 bool objc_ok;
10778 /* True means this is an arithmetic operation that may need excess
10779 precision. */
10780 bool may_need_excess_precision;
10782 /* True means this is a boolean operation that converts both its
10783 operands to truth-values. */
10784 bool boolean_op = false;
10786 /* Remember whether we're doing / or %. */
10787 bool doing_div_or_mod = false;
10789 /* Remember whether we're doing << or >>. */
10790 bool doing_shift = false;
10792 /* Tree holding instrumentation expression. */
10793 tree instrument_expr = NULL;
10795 if (location == UNKNOWN_LOCATION)
10796 location = input_location;
10798 op0 = orig_op0;
10799 op1 = orig_op1;
10801 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10802 if (op0_int_operands)
10803 op0 = remove_c_maybe_const_expr (op0);
10804 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10805 if (op1_int_operands)
10806 op1 = remove_c_maybe_const_expr (op1);
10807 int_operands = (op0_int_operands && op1_int_operands);
10808 if (int_operands)
10810 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10811 && TREE_CODE (orig_op1) == INTEGER_CST);
10812 int_const = (int_const_or_overflow
10813 && !TREE_OVERFLOW (orig_op0)
10814 && !TREE_OVERFLOW (orig_op1));
10816 else
10817 int_const = int_const_or_overflow = false;
10819 /* Do not apply default conversion in mixed vector/scalar expression. */
10820 if (convert_p
10821 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10823 op0 = default_conversion (op0);
10824 op1 = default_conversion (op1);
10827 /* When Cilk Plus is enabled and there are array notations inside op0, then
10828 we check to see if there are builtin array notation functions. If
10829 so, then we take on the type of the array notation inside it. */
10830 if (flag_cilkplus && contains_array_notation_expr (op0))
10831 orig_type0 = type0 = find_correct_array_notation_type (op0);
10832 else
10833 orig_type0 = type0 = TREE_TYPE (op0);
10835 if (flag_cilkplus && contains_array_notation_expr (op1))
10836 orig_type1 = type1 = find_correct_array_notation_type (op1);
10837 else
10838 orig_type1 = type1 = TREE_TYPE (op1);
10840 /* The expression codes of the data types of the arguments tell us
10841 whether the arguments are integers, floating, pointers, etc. */
10842 code0 = TREE_CODE (type0);
10843 code1 = TREE_CODE (type1);
10845 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10846 STRIP_TYPE_NOPS (op0);
10847 STRIP_TYPE_NOPS (op1);
10849 /* If an error was already reported for one of the arguments,
10850 avoid reporting another error. */
10852 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10853 return error_mark_node;
10855 if (code0 == POINTER_TYPE
10856 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10857 return error_mark_node;
10859 if (code1 == POINTER_TYPE
10860 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10861 return error_mark_node;
10863 if ((invalid_op_diag
10864 = targetm.invalid_binary_op (code, type0, type1)))
10866 error_at (location, invalid_op_diag);
10867 return error_mark_node;
10870 switch (code)
10872 case PLUS_EXPR:
10873 case MINUS_EXPR:
10874 case MULT_EXPR:
10875 case TRUNC_DIV_EXPR:
10876 case CEIL_DIV_EXPR:
10877 case FLOOR_DIV_EXPR:
10878 case ROUND_DIV_EXPR:
10879 case EXACT_DIV_EXPR:
10880 may_need_excess_precision = true;
10881 break;
10882 default:
10883 may_need_excess_precision = false;
10884 break;
10886 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10888 op0 = TREE_OPERAND (op0, 0);
10889 type0 = TREE_TYPE (op0);
10891 else if (may_need_excess_precision
10892 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10894 type0 = eptype;
10895 op0 = convert (eptype, op0);
10897 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10899 op1 = TREE_OPERAND (op1, 0);
10900 type1 = TREE_TYPE (op1);
10902 else if (may_need_excess_precision
10903 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10905 type1 = eptype;
10906 op1 = convert (eptype, op1);
10909 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10911 /* In case when one of the operands of the binary operation is
10912 a vector and another is a scalar -- convert scalar to vector. */
10913 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10915 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10916 true);
10918 switch (convert_flag)
10920 case stv_error:
10921 return error_mark_node;
10922 case stv_firstarg:
10924 bool maybe_const = true;
10925 tree sc;
10926 sc = c_fully_fold (op0, false, &maybe_const);
10927 sc = save_expr (sc);
10928 sc = convert (TREE_TYPE (type1), sc);
10929 op0 = build_vector_from_val (type1, sc);
10930 if (!maybe_const)
10931 op0 = c_wrap_maybe_const (op0, true);
10932 orig_type0 = type0 = TREE_TYPE (op0);
10933 code0 = TREE_CODE (type0);
10934 converted = 1;
10935 break;
10937 case stv_secondarg:
10939 bool maybe_const = true;
10940 tree sc;
10941 sc = c_fully_fold (op1, false, &maybe_const);
10942 sc = save_expr (sc);
10943 sc = convert (TREE_TYPE (type0), sc);
10944 op1 = build_vector_from_val (type0, sc);
10945 if (!maybe_const)
10946 op1 = c_wrap_maybe_const (op1, true);
10947 orig_type1 = type1 = TREE_TYPE (op1);
10948 code1 = TREE_CODE (type1);
10949 converted = 1;
10950 break;
10952 default:
10953 break;
10957 switch (code)
10959 case PLUS_EXPR:
10960 /* Handle the pointer + int case. */
10961 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10963 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10964 goto return_build_binary_op;
10966 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10968 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10969 goto return_build_binary_op;
10971 else
10972 common = 1;
10973 break;
10975 case MINUS_EXPR:
10976 /* Subtraction of two similar pointers.
10977 We must subtract them as integers, then divide by object size. */
10978 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10979 && comp_target_types (location, type0, type1))
10981 ret = pointer_diff (location, op0, op1);
10982 goto return_build_binary_op;
10984 /* Handle pointer minus int. Just like pointer plus int. */
10985 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10987 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10988 goto return_build_binary_op;
10990 else
10991 common = 1;
10992 break;
10994 case MULT_EXPR:
10995 common = 1;
10996 break;
10998 case TRUNC_DIV_EXPR:
10999 case CEIL_DIV_EXPR:
11000 case FLOOR_DIV_EXPR:
11001 case ROUND_DIV_EXPR:
11002 case EXACT_DIV_EXPR:
11003 doing_div_or_mod = true;
11004 warn_for_div_by_zero (location, op1);
11006 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11007 || code0 == FIXED_POINT_TYPE
11008 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11009 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11010 || code1 == FIXED_POINT_TYPE
11011 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11013 enum tree_code tcode0 = code0, tcode1 = code1;
11015 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11016 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11017 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11018 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11020 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11021 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11022 resultcode = RDIV_EXPR;
11023 else
11024 /* Although it would be tempting to shorten always here, that
11025 loses on some targets, since the modulo instruction is
11026 undefined if the quotient can't be represented in the
11027 computation mode. We shorten only if unsigned or if
11028 dividing by something we know != -1. */
11029 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11030 || (TREE_CODE (op1) == INTEGER_CST
11031 && !integer_all_onesp (op1)));
11032 common = 1;
11034 break;
11036 case BIT_AND_EXPR:
11037 case BIT_IOR_EXPR:
11038 case BIT_XOR_EXPR:
11039 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11040 shorten = -1;
11041 /* Allow vector types which are not floating point types. */
11042 else if (code0 == VECTOR_TYPE
11043 && code1 == VECTOR_TYPE
11044 && !VECTOR_FLOAT_TYPE_P (type0)
11045 && !VECTOR_FLOAT_TYPE_P (type1))
11046 common = 1;
11047 break;
11049 case TRUNC_MOD_EXPR:
11050 case FLOOR_MOD_EXPR:
11051 doing_div_or_mod = true;
11052 warn_for_div_by_zero (location, op1);
11054 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11055 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11056 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11057 common = 1;
11058 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11060 /* Although it would be tempting to shorten always here, that loses
11061 on some targets, since the modulo instruction is undefined if the
11062 quotient can't be represented in the computation mode. We shorten
11063 only if unsigned or if dividing by something we know != -1. */
11064 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11065 || (TREE_CODE (op1) == INTEGER_CST
11066 && !integer_all_onesp (op1)));
11067 common = 1;
11069 break;
11071 case TRUTH_ANDIF_EXPR:
11072 case TRUTH_ORIF_EXPR:
11073 case TRUTH_AND_EXPR:
11074 case TRUTH_OR_EXPR:
11075 case TRUTH_XOR_EXPR:
11076 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11077 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11078 || code0 == FIXED_POINT_TYPE)
11079 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11080 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11081 || code1 == FIXED_POINT_TYPE))
11083 /* Result of these operations is always an int,
11084 but that does not mean the operands should be
11085 converted to ints! */
11086 result_type = integer_type_node;
11087 if (op0_int_operands)
11089 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11090 op0 = remove_c_maybe_const_expr (op0);
11092 else
11093 op0 = c_objc_common_truthvalue_conversion (location, op0);
11094 if (op1_int_operands)
11096 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11097 op1 = remove_c_maybe_const_expr (op1);
11099 else
11100 op1 = c_objc_common_truthvalue_conversion (location, op1);
11101 converted = 1;
11102 boolean_op = true;
11104 if (code == TRUTH_ANDIF_EXPR)
11106 int_const_or_overflow = (int_operands
11107 && TREE_CODE (orig_op0) == INTEGER_CST
11108 && (op0 == truthvalue_false_node
11109 || TREE_CODE (orig_op1) == INTEGER_CST));
11110 int_const = (int_const_or_overflow
11111 && !TREE_OVERFLOW (orig_op0)
11112 && (op0 == truthvalue_false_node
11113 || !TREE_OVERFLOW (orig_op1)));
11115 else if (code == TRUTH_ORIF_EXPR)
11117 int_const_or_overflow = (int_operands
11118 && TREE_CODE (orig_op0) == INTEGER_CST
11119 && (op0 == truthvalue_true_node
11120 || TREE_CODE (orig_op1) == INTEGER_CST));
11121 int_const = (int_const_or_overflow
11122 && !TREE_OVERFLOW (orig_op0)
11123 && (op0 == truthvalue_true_node
11124 || !TREE_OVERFLOW (orig_op1)));
11126 break;
11128 /* Shift operations: result has same type as first operand;
11129 always convert second operand to int.
11130 Also set SHORT_SHIFT if shifting rightward. */
11132 case RSHIFT_EXPR:
11133 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11134 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11135 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11136 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11138 result_type = type0;
11139 converted = 1;
11141 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11142 || code0 == VECTOR_TYPE)
11143 && code1 == INTEGER_TYPE)
11145 doing_shift = true;
11146 if (TREE_CODE (op1) == INTEGER_CST)
11148 if (tree_int_cst_sgn (op1) < 0)
11150 int_const = false;
11151 if (c_inhibit_evaluation_warnings == 0)
11152 warning_at (location, OPT_Wshift_count_negative,
11153 "right shift count is negative");
11155 else if (code0 == VECTOR_TYPE)
11157 if (compare_tree_int (op1,
11158 TYPE_PRECISION (TREE_TYPE (type0)))
11159 >= 0)
11161 int_const = false;
11162 if (c_inhibit_evaluation_warnings == 0)
11163 warning_at (location, OPT_Wshift_count_overflow,
11164 "right shift count >= width of vector element");
11167 else
11169 if (!integer_zerop (op1))
11170 short_shift = 1;
11172 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11174 int_const = false;
11175 if (c_inhibit_evaluation_warnings == 0)
11176 warning_at (location, OPT_Wshift_count_overflow,
11177 "right shift count >= width of type");
11182 /* Use the type of the value to be shifted. */
11183 result_type = type0;
11184 /* Avoid converting op1 to result_type later. */
11185 converted = 1;
11187 break;
11189 case LSHIFT_EXPR:
11190 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11191 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11192 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11193 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11195 result_type = type0;
11196 converted = 1;
11198 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11199 || code0 == VECTOR_TYPE)
11200 && code1 == INTEGER_TYPE)
11202 doing_shift = true;
11203 if (TREE_CODE (op0) == INTEGER_CST
11204 && tree_int_cst_sgn (op0) < 0)
11206 /* Don't reject a left shift of a negative value in a context
11207 where a constant expression is needed in C90. */
11208 if (flag_isoc99)
11209 int_const = false;
11210 if (c_inhibit_evaluation_warnings == 0)
11211 warning_at (location, OPT_Wshift_negative_value,
11212 "left shift of negative value");
11214 if (TREE_CODE (op1) == INTEGER_CST)
11216 if (tree_int_cst_sgn (op1) < 0)
11218 int_const = false;
11219 if (c_inhibit_evaluation_warnings == 0)
11220 warning_at (location, OPT_Wshift_count_negative,
11221 "left shift count is negative");
11223 else if (code0 == VECTOR_TYPE)
11225 if (compare_tree_int (op1,
11226 TYPE_PRECISION (TREE_TYPE (type0)))
11227 >= 0)
11229 int_const = false;
11230 if (c_inhibit_evaluation_warnings == 0)
11231 warning_at (location, OPT_Wshift_count_overflow,
11232 "left shift count >= width of vector element");
11235 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11237 int_const = false;
11238 if (c_inhibit_evaluation_warnings == 0)
11239 warning_at (location, OPT_Wshift_count_overflow,
11240 "left shift count >= width of type");
11242 else if (TREE_CODE (op0) == INTEGER_CST
11243 && maybe_warn_shift_overflow (location, op0, op1)
11244 && flag_isoc99)
11245 int_const = false;
11248 /* Use the type of the value to be shifted. */
11249 result_type = type0;
11250 /* Avoid converting op1 to result_type later. */
11251 converted = 1;
11253 break;
11255 case EQ_EXPR:
11256 case NE_EXPR:
11257 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11259 tree intt;
11260 if (!vector_types_compatible_elements_p (type0, type1))
11262 error_at (location, "comparing vectors with different "
11263 "element types");
11264 return error_mark_node;
11267 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11269 error_at (location, "comparing vectors with different "
11270 "number of elements");
11271 return error_mark_node;
11274 /* It's not precisely specified how the usual arithmetic
11275 conversions apply to the vector types. Here, we use
11276 the unsigned type if one of the operands is signed and
11277 the other one is unsigned. */
11278 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11280 if (!TYPE_UNSIGNED (type0))
11281 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11282 else
11283 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11284 warning_at (location, OPT_Wsign_compare, "comparison between "
11285 "types %qT and %qT", type0, type1);
11288 /* Always construct signed integer vector type. */
11289 intt = c_common_type_for_size (GET_MODE_BITSIZE
11290 (TYPE_MODE (TREE_TYPE (type0))), 0);
11291 result_type = build_opaque_vector_type (intt,
11292 TYPE_VECTOR_SUBPARTS (type0));
11293 converted = 1;
11294 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11295 goto return_build_binary_op;
11297 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11298 warning_at (location,
11299 OPT_Wfloat_equal,
11300 "comparing floating point with == or != is unsafe");
11301 /* Result of comparison is always int,
11302 but don't convert the args to int! */
11303 build_type = integer_type_node;
11304 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11305 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11306 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11307 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11308 short_compare = 1;
11309 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11311 if (TREE_CODE (op0) == ADDR_EXPR
11312 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11313 && !from_macro_expansion_at (location))
11315 if (code == EQ_EXPR)
11316 warning_at (location,
11317 OPT_Waddress,
11318 "the comparison will always evaluate as %<false%> "
11319 "for the address of %qD will never be NULL",
11320 TREE_OPERAND (op0, 0));
11321 else
11322 warning_at (location,
11323 OPT_Waddress,
11324 "the comparison will always evaluate as %<true%> "
11325 "for the address of %qD will never be NULL",
11326 TREE_OPERAND (op0, 0));
11328 result_type = type0;
11330 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11332 if (TREE_CODE (op1) == ADDR_EXPR
11333 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11334 && !from_macro_expansion_at (location))
11336 if (code == EQ_EXPR)
11337 warning_at (location,
11338 OPT_Waddress,
11339 "the comparison will always evaluate as %<false%> "
11340 "for the address of %qD will never be NULL",
11341 TREE_OPERAND (op1, 0));
11342 else
11343 warning_at (location,
11344 OPT_Waddress,
11345 "the comparison will always evaluate as %<true%> "
11346 "for the address of %qD will never be NULL",
11347 TREE_OPERAND (op1, 0));
11349 result_type = type1;
11351 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11353 tree tt0 = TREE_TYPE (type0);
11354 tree tt1 = TREE_TYPE (type1);
11355 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11356 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11357 addr_space_t as_common = ADDR_SPACE_GENERIC;
11359 /* Anything compares with void *. void * compares with anything.
11360 Otherwise, the targets must be compatible
11361 and both must be object or both incomplete. */
11362 if (comp_target_types (location, type0, type1))
11363 result_type = common_pointer_type (type0, type1);
11364 else if (!addr_space_superset (as0, as1, &as_common))
11366 error_at (location, "comparison of pointers to "
11367 "disjoint address spaces");
11368 return error_mark_node;
11370 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11372 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11373 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11374 "comparison of %<void *%> with function pointer");
11376 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11378 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11379 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11380 "comparison of %<void *%> with function pointer");
11382 else
11383 /* Avoid warning about the volatile ObjC EH puts on decls. */
11384 if (!objc_ok)
11385 pedwarn (location, 0,
11386 "comparison of distinct pointer types lacks a cast");
11388 if (result_type == NULL_TREE)
11390 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11391 result_type = build_pointer_type
11392 (build_qualified_type (void_type_node, qual));
11395 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11397 result_type = type0;
11398 pedwarn (location, 0, "comparison between pointer and integer");
11400 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11402 result_type = type1;
11403 pedwarn (location, 0, "comparison between pointer and integer");
11405 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11406 || truth_value_p (TREE_CODE (orig_op0)))
11407 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11408 || truth_value_p (TREE_CODE (orig_op1))))
11409 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11410 break;
11412 case LE_EXPR:
11413 case GE_EXPR:
11414 case LT_EXPR:
11415 case GT_EXPR:
11416 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11418 tree intt;
11419 if (!vector_types_compatible_elements_p (type0, type1))
11421 error_at (location, "comparing vectors with different "
11422 "element types");
11423 return error_mark_node;
11426 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11428 error_at (location, "comparing vectors with different "
11429 "number of elements");
11430 return error_mark_node;
11433 /* It's not precisely specified how the usual arithmetic
11434 conversions apply to the vector types. Here, we use
11435 the unsigned type if one of the operands is signed and
11436 the other one is unsigned. */
11437 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11439 if (!TYPE_UNSIGNED (type0))
11440 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11441 else
11442 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11443 warning_at (location, OPT_Wsign_compare, "comparison between "
11444 "types %qT and %qT", type0, type1);
11447 /* Always construct signed integer vector type. */
11448 intt = c_common_type_for_size (GET_MODE_BITSIZE
11449 (TYPE_MODE (TREE_TYPE (type0))), 0);
11450 result_type = build_opaque_vector_type (intt,
11451 TYPE_VECTOR_SUBPARTS (type0));
11452 converted = 1;
11453 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11454 goto return_build_binary_op;
11456 build_type = integer_type_node;
11457 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11458 || code0 == FIXED_POINT_TYPE)
11459 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11460 || code1 == FIXED_POINT_TYPE))
11461 short_compare = 1;
11462 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11464 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11465 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11466 addr_space_t as_common;
11468 if (comp_target_types (location, type0, type1))
11470 result_type = common_pointer_type (type0, type1);
11471 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11472 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11473 pedwarn (location, 0,
11474 "comparison of complete and incomplete pointers");
11475 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11476 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11477 "ordered comparisons of pointers to functions");
11478 else if (null_pointer_constant_p (orig_op0)
11479 || null_pointer_constant_p (orig_op1))
11480 warning_at (location, OPT_Wextra,
11481 "ordered comparison of pointer with null pointer");
11484 else if (!addr_space_superset (as0, as1, &as_common))
11486 error_at (location, "comparison of pointers to "
11487 "disjoint address spaces");
11488 return error_mark_node;
11490 else
11492 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11493 result_type = build_pointer_type
11494 (build_qualified_type (void_type_node, qual));
11495 pedwarn (location, 0,
11496 "comparison of distinct pointer types lacks a cast");
11499 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11501 result_type = type0;
11502 if (pedantic)
11503 pedwarn (location, OPT_Wpedantic,
11504 "ordered comparison of pointer with integer zero");
11505 else if (extra_warnings)
11506 warning_at (location, OPT_Wextra,
11507 "ordered comparison of pointer with integer zero");
11509 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11511 result_type = type1;
11512 if (pedantic)
11513 pedwarn (location, OPT_Wpedantic,
11514 "ordered comparison of pointer with integer zero");
11515 else if (extra_warnings)
11516 warning_at (location, OPT_Wextra,
11517 "ordered comparison of pointer with integer zero");
11519 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11521 result_type = type0;
11522 pedwarn (location, 0, "comparison between pointer and integer");
11524 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11526 result_type = type1;
11527 pedwarn (location, 0, "comparison between pointer and integer");
11529 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11530 || truth_value_p (TREE_CODE (orig_op0)))
11531 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11532 || truth_value_p (TREE_CODE (orig_op1))))
11533 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11534 break;
11536 default:
11537 gcc_unreachable ();
11540 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11541 return error_mark_node;
11543 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11544 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11545 || !vector_types_compatible_elements_p (type0, type1)))
11547 gcc_rich_location richloc (location);
11548 richloc.maybe_add_expr (orig_op0);
11549 richloc.maybe_add_expr (orig_op1);
11550 binary_op_error (&richloc, code, type0, type1);
11551 return error_mark_node;
11554 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11555 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11557 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11558 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11560 bool first_complex = (code0 == COMPLEX_TYPE);
11561 bool second_complex = (code1 == COMPLEX_TYPE);
11562 int none_complex = (!first_complex && !second_complex);
11564 if (shorten || common || short_compare)
11566 result_type = c_common_type (type0, type1);
11567 do_warn_double_promotion (result_type, type0, type1,
11568 "implicit conversion from %qT to %qT "
11569 "to match other operand of binary "
11570 "expression",
11571 location);
11572 if (result_type == error_mark_node)
11573 return error_mark_node;
11576 if (first_complex != second_complex
11577 && (code == PLUS_EXPR
11578 || code == MINUS_EXPR
11579 || code == MULT_EXPR
11580 || (code == TRUNC_DIV_EXPR && first_complex))
11581 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11582 && flag_signed_zeros)
11584 /* An operation on mixed real/complex operands must be
11585 handled specially, but the language-independent code can
11586 more easily optimize the plain complex arithmetic if
11587 -fno-signed-zeros. */
11588 tree real_type = TREE_TYPE (result_type);
11589 tree real, imag;
11590 if (type0 != orig_type0 || type1 != orig_type1)
11592 gcc_assert (may_need_excess_precision && common);
11593 semantic_result_type = c_common_type (orig_type0, orig_type1);
11595 if (first_complex)
11597 if (TREE_TYPE (op0) != result_type)
11598 op0 = convert_and_check (location, result_type, op0);
11599 if (TREE_TYPE (op1) != real_type)
11600 op1 = convert_and_check (location, real_type, op1);
11602 else
11604 if (TREE_TYPE (op0) != real_type)
11605 op0 = convert_and_check (location, real_type, op0);
11606 if (TREE_TYPE (op1) != result_type)
11607 op1 = convert_and_check (location, result_type, op1);
11609 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11610 return error_mark_node;
11611 if (first_complex)
11613 op0 = c_save_expr (op0);
11614 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11615 op0, true);
11616 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11617 op0, true);
11618 switch (code)
11620 case MULT_EXPR:
11621 case TRUNC_DIV_EXPR:
11622 op1 = c_save_expr (op1);
11623 imag = build2 (resultcode, real_type, imag, op1);
11624 /* Fall through. */
11625 case PLUS_EXPR:
11626 case MINUS_EXPR:
11627 real = build2 (resultcode, real_type, real, op1);
11628 break;
11629 default:
11630 gcc_unreachable();
11633 else
11635 op1 = c_save_expr (op1);
11636 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11637 op1, true);
11638 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11639 op1, true);
11640 switch (code)
11642 case MULT_EXPR:
11643 op0 = c_save_expr (op0);
11644 imag = build2 (resultcode, real_type, op0, imag);
11645 /* Fall through. */
11646 case PLUS_EXPR:
11647 real = build2 (resultcode, real_type, op0, real);
11648 break;
11649 case MINUS_EXPR:
11650 real = build2 (resultcode, real_type, op0, real);
11651 imag = build1 (NEGATE_EXPR, real_type, imag);
11652 break;
11653 default:
11654 gcc_unreachable();
11657 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11658 goto return_build_binary_op;
11661 /* For certain operations (which identify themselves by shorten != 0)
11662 if both args were extended from the same smaller type,
11663 do the arithmetic in that type and then extend.
11665 shorten !=0 and !=1 indicates a bitwise operation.
11666 For them, this optimization is safe only if
11667 both args are zero-extended or both are sign-extended.
11668 Otherwise, we might change the result.
11669 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11670 but calculated in (unsigned short) it would be (unsigned short)-1. */
11672 if (shorten && none_complex)
11674 final_type = result_type;
11675 result_type = shorten_binary_op (result_type, op0, op1,
11676 shorten == -1);
11679 /* Shifts can be shortened if shifting right. */
11681 if (short_shift)
11683 int unsigned_arg;
11684 tree arg0 = get_narrower (op0, &unsigned_arg);
11686 final_type = result_type;
11688 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11689 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11691 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11692 && tree_int_cst_sgn (op1) > 0
11693 /* We can shorten only if the shift count is less than the
11694 number of bits in the smaller type size. */
11695 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11696 /* We cannot drop an unsigned shift after sign-extension. */
11697 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11699 /* Do an unsigned shift if the operand was zero-extended. */
11700 result_type
11701 = c_common_signed_or_unsigned_type (unsigned_arg,
11702 TREE_TYPE (arg0));
11703 /* Convert value-to-be-shifted to that type. */
11704 if (TREE_TYPE (op0) != result_type)
11705 op0 = convert (result_type, op0);
11706 converted = 1;
11710 /* Comparison operations are shortened too but differently.
11711 They identify themselves by setting short_compare = 1. */
11713 if (short_compare)
11715 /* Don't write &op0, etc., because that would prevent op0
11716 from being kept in a register.
11717 Instead, make copies of the our local variables and
11718 pass the copies by reference, then copy them back afterward. */
11719 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11720 enum tree_code xresultcode = resultcode;
11721 tree val
11722 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11723 &xresultcode);
11725 if (val != 0)
11727 ret = val;
11728 goto return_build_binary_op;
11731 op0 = xop0, op1 = xop1;
11732 converted = 1;
11733 resultcode = xresultcode;
11735 if (c_inhibit_evaluation_warnings == 0)
11737 bool op0_maybe_const = true;
11738 bool op1_maybe_const = true;
11739 tree orig_op0_folded, orig_op1_folded;
11741 if (in_late_binary_op)
11743 orig_op0_folded = orig_op0;
11744 orig_op1_folded = orig_op1;
11746 else
11748 /* Fold for the sake of possible warnings, as in
11749 build_conditional_expr. This requires the
11750 "original" values to be folded, not just op0 and
11751 op1. */
11752 c_inhibit_evaluation_warnings++;
11753 op0 = c_fully_fold (op0, require_constant_value,
11754 &op0_maybe_const);
11755 op1 = c_fully_fold (op1, require_constant_value,
11756 &op1_maybe_const);
11757 c_inhibit_evaluation_warnings--;
11758 orig_op0_folded = c_fully_fold (orig_op0,
11759 require_constant_value,
11760 NULL);
11761 orig_op1_folded = c_fully_fold (orig_op1,
11762 require_constant_value,
11763 NULL);
11766 if (warn_sign_compare)
11767 warn_for_sign_compare (location, orig_op0_folded,
11768 orig_op1_folded, op0, op1,
11769 result_type, resultcode);
11770 if (!in_late_binary_op && !int_operands)
11772 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11773 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11774 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11775 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11781 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11782 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11783 Then the expression will be built.
11784 It will be given type FINAL_TYPE if that is nonzero;
11785 otherwise, it will be given type RESULT_TYPE. */
11787 if (!result_type)
11789 gcc_rich_location richloc (location);
11790 richloc.maybe_add_expr (orig_op0);
11791 richloc.maybe_add_expr (orig_op1);
11792 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11793 return error_mark_node;
11796 if (build_type == NULL_TREE)
11798 build_type = result_type;
11799 if ((type0 != orig_type0 || type1 != orig_type1)
11800 && !boolean_op)
11802 gcc_assert (may_need_excess_precision && common);
11803 semantic_result_type = c_common_type (orig_type0, orig_type1);
11807 if (!converted)
11809 op0 = ep_convert_and_check (location, result_type, op0,
11810 semantic_result_type);
11811 op1 = ep_convert_and_check (location, result_type, op1,
11812 semantic_result_type);
11814 /* This can happen if one operand has a vector type, and the other
11815 has a different type. */
11816 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11817 return error_mark_node;
11820 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11821 | SANITIZE_FLOAT_DIVIDE))
11822 && do_ubsan_in_current_function ()
11823 && (doing_div_or_mod || doing_shift)
11824 && !require_constant_value)
11826 /* OP0 and/or OP1 might have side-effects. */
11827 op0 = c_save_expr (op0);
11828 op1 = c_save_expr (op1);
11829 op0 = c_fully_fold (op0, false, NULL);
11830 op1 = c_fully_fold (op1, false, NULL);
11831 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11832 | SANITIZE_FLOAT_DIVIDE)))
11833 instrument_expr = ubsan_instrument_division (location, op0, op1);
11834 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11835 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11838 /* Treat expressions in initializers specially as they can't trap. */
11839 if (int_const_or_overflow)
11840 ret = (require_constant_value
11841 ? fold_build2_initializer_loc (location, resultcode, build_type,
11842 op0, op1)
11843 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11844 else
11845 ret = build2 (resultcode, build_type, op0, op1);
11846 if (final_type != 0)
11847 ret = convert (final_type, ret);
11849 return_build_binary_op:
11850 gcc_assert (ret != error_mark_node);
11851 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11852 ret = (int_operands
11853 ? note_integer_operands (ret)
11854 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11855 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11856 && !in_late_binary_op)
11857 ret = note_integer_operands (ret);
11858 if (semantic_result_type)
11859 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11860 protected_set_expr_location (ret, location);
11862 if (instrument_expr != NULL)
11863 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11864 instrument_expr, ret);
11866 return ret;
11870 /* Convert EXPR to be a truth-value, validating its type for this
11871 purpose. LOCATION is the source location for the expression. */
11873 tree
11874 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11876 bool int_const, int_operands;
11878 switch (TREE_CODE (TREE_TYPE (expr)))
11880 case ARRAY_TYPE:
11881 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11882 return error_mark_node;
11884 case RECORD_TYPE:
11885 error_at (location, "used struct type value where scalar is required");
11886 return error_mark_node;
11888 case UNION_TYPE:
11889 error_at (location, "used union type value where scalar is required");
11890 return error_mark_node;
11892 case VOID_TYPE:
11893 error_at (location, "void value not ignored as it ought to be");
11894 return error_mark_node;
11896 case POINTER_TYPE:
11897 if (reject_gcc_builtin (expr))
11898 return error_mark_node;
11899 break;
11901 case FUNCTION_TYPE:
11902 gcc_unreachable ();
11904 case VECTOR_TYPE:
11905 error_at (location, "used vector type where scalar is required");
11906 return error_mark_node;
11908 default:
11909 break;
11912 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11913 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11914 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11916 expr = remove_c_maybe_const_expr (expr);
11917 expr = build2 (NE_EXPR, integer_type_node, expr,
11918 convert (TREE_TYPE (expr), integer_zero_node));
11919 expr = note_integer_operands (expr);
11921 else
11922 /* ??? Should we also give an error for vectors rather than leaving
11923 those to give errors later? */
11924 expr = c_common_truthvalue_conversion (location, expr);
11926 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11928 if (TREE_OVERFLOW (expr))
11929 return expr;
11930 else
11931 return note_integer_operands (expr);
11933 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11934 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11935 return expr;
11939 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11940 required. */
11942 tree
11943 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11945 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11947 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11948 /* Executing a compound literal inside a function reinitializes
11949 it. */
11950 if (!TREE_STATIC (decl))
11951 *se = true;
11952 return decl;
11954 else
11955 return expr;
11958 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11959 statement. LOC is the location of the construct. */
11961 tree
11962 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11963 tree clauses)
11965 body = c_end_compound_stmt (loc, body, true);
11967 tree stmt = make_node (code);
11968 TREE_TYPE (stmt) = void_type_node;
11969 OMP_BODY (stmt) = body;
11970 OMP_CLAUSES (stmt) = clauses;
11971 SET_EXPR_LOCATION (stmt, loc);
11973 return add_stmt (stmt);
11976 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11977 statement. LOC is the location of the OACC_DATA. */
11979 tree
11980 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11982 tree stmt;
11984 block = c_end_compound_stmt (loc, block, true);
11986 stmt = make_node (OACC_DATA);
11987 TREE_TYPE (stmt) = void_type_node;
11988 OACC_DATA_CLAUSES (stmt) = clauses;
11989 OACC_DATA_BODY (stmt) = block;
11990 SET_EXPR_LOCATION (stmt, loc);
11992 return add_stmt (stmt);
11995 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11996 statement. LOC is the location of the OACC_HOST_DATA. */
11998 tree
11999 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12001 tree stmt;
12003 block = c_end_compound_stmt (loc, block, true);
12005 stmt = make_node (OACC_HOST_DATA);
12006 TREE_TYPE (stmt) = void_type_node;
12007 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12008 OACC_HOST_DATA_BODY (stmt) = block;
12009 SET_EXPR_LOCATION (stmt, loc);
12011 return add_stmt (stmt);
12014 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12016 tree
12017 c_begin_omp_parallel (void)
12019 tree block;
12021 keep_next_level ();
12022 block = c_begin_compound_stmt (true);
12024 return block;
12027 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12028 statement. LOC is the location of the OMP_PARALLEL. */
12030 tree
12031 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12033 tree stmt;
12035 block = c_end_compound_stmt (loc, block, true);
12037 stmt = make_node (OMP_PARALLEL);
12038 TREE_TYPE (stmt) = void_type_node;
12039 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12040 OMP_PARALLEL_BODY (stmt) = block;
12041 SET_EXPR_LOCATION (stmt, loc);
12043 return add_stmt (stmt);
12046 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12048 tree
12049 c_begin_omp_task (void)
12051 tree block;
12053 keep_next_level ();
12054 block = c_begin_compound_stmt (true);
12056 return block;
12059 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12060 statement. LOC is the location of the #pragma. */
12062 tree
12063 c_finish_omp_task (location_t loc, tree clauses, tree block)
12065 tree stmt;
12067 block = c_end_compound_stmt (loc, block, true);
12069 stmt = make_node (OMP_TASK);
12070 TREE_TYPE (stmt) = void_type_node;
12071 OMP_TASK_CLAUSES (stmt) = clauses;
12072 OMP_TASK_BODY (stmt) = block;
12073 SET_EXPR_LOCATION (stmt, loc);
12075 return add_stmt (stmt);
12078 /* Generate GOMP_cancel call for #pragma omp cancel. */
12080 void
12081 c_finish_omp_cancel (location_t loc, tree clauses)
12083 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12084 int mask = 0;
12085 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12086 mask = 1;
12087 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12088 mask = 2;
12089 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12090 mask = 4;
12091 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12092 mask = 8;
12093 else
12095 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12096 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12097 "clauses");
12098 return;
12100 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12101 if (ifc != NULL_TREE)
12103 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12104 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12105 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12106 build_zero_cst (type));
12108 else
12109 ifc = boolean_true_node;
12110 tree stmt = build_call_expr_loc (loc, fn, 2,
12111 build_int_cst (integer_type_node, mask),
12112 ifc);
12113 add_stmt (stmt);
12116 /* Generate GOMP_cancellation_point call for
12117 #pragma omp cancellation point. */
12119 void
12120 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12122 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12123 int mask = 0;
12124 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12125 mask = 1;
12126 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12127 mask = 2;
12128 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12129 mask = 4;
12130 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12131 mask = 8;
12132 else
12134 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12135 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12136 "clauses");
12137 return;
12139 tree stmt = build_call_expr_loc (loc, fn, 1,
12140 build_int_cst (integer_type_node, mask));
12141 add_stmt (stmt);
12144 /* Helper function for handle_omp_array_sections. Called recursively
12145 to handle multiple array-section-subscripts. C is the clause,
12146 T current expression (initially OMP_CLAUSE_DECL), which is either
12147 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12148 expression if specified, TREE_VALUE length expression if specified,
12149 TREE_CHAIN is what it has been specified after, or some decl.
12150 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12151 set to true if any of the array-section-subscript could have length
12152 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12153 first array-section-subscript which is known not to have length
12154 of one. Given say:
12155 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12156 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12157 all are or may have length of 1, array-section-subscript [:2] is the
12158 first one known not to have length 1. For array-section-subscript
12159 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12160 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12161 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12162 case though, as some lengths could be zero. */
12164 static tree
12165 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12166 bool &maybe_zero_len, unsigned int &first_non_one,
12167 enum c_omp_region_type ort)
12169 tree ret, low_bound, length, type;
12170 if (TREE_CODE (t) != TREE_LIST)
12172 if (error_operand_p (t))
12173 return error_mark_node;
12174 ret = t;
12175 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12176 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12178 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12179 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12180 return error_mark_node;
12182 if (TREE_CODE (t) == COMPONENT_REF
12183 && ort == C_ORT_OMP
12184 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12185 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12186 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12188 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12190 error_at (OMP_CLAUSE_LOCATION (c),
12191 "bit-field %qE in %qs clause",
12192 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12193 return error_mark_node;
12195 while (TREE_CODE (t) == COMPONENT_REF)
12197 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12199 error_at (OMP_CLAUSE_LOCATION (c),
12200 "%qE is a member of a union", t);
12201 return error_mark_node;
12203 t = TREE_OPERAND (t, 0);
12206 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12208 if (DECL_P (t))
12209 error_at (OMP_CLAUSE_LOCATION (c),
12210 "%qD is not a variable in %qs clause", t,
12211 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12212 else
12213 error_at (OMP_CLAUSE_LOCATION (c),
12214 "%qE is not a variable in %qs clause", t,
12215 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12216 return error_mark_node;
12218 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12219 && TYPE_ATOMIC (TREE_TYPE (t)))
12221 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12222 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12223 return error_mark_node;
12225 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12226 && VAR_P (t)
12227 && DECL_THREAD_LOCAL_P (t))
12229 error_at (OMP_CLAUSE_LOCATION (c),
12230 "%qD is threadprivate variable in %qs clause", t,
12231 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12232 return error_mark_node;
12234 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12235 && TYPE_ATOMIC (TREE_TYPE (t))
12236 && POINTER_TYPE_P (TREE_TYPE (t)))
12238 /* If the array section is pointer based and the pointer
12239 itself is _Atomic qualified, we need to atomically load
12240 the pointer. */
12241 c_expr expr;
12242 memset (&expr, 0, sizeof (expr));
12243 expr.value = ret;
12244 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12245 expr, false, false);
12246 ret = expr.value;
12248 return ret;
12251 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12252 maybe_zero_len, first_non_one, ort);
12253 if (ret == error_mark_node || ret == NULL_TREE)
12254 return ret;
12256 type = TREE_TYPE (ret);
12257 low_bound = TREE_PURPOSE (t);
12258 length = TREE_VALUE (t);
12260 if (low_bound == error_mark_node || length == error_mark_node)
12261 return error_mark_node;
12263 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12265 error_at (OMP_CLAUSE_LOCATION (c),
12266 "low bound %qE of array section does not have integral type",
12267 low_bound);
12268 return error_mark_node;
12270 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12272 error_at (OMP_CLAUSE_LOCATION (c),
12273 "length %qE of array section does not have integral type",
12274 length);
12275 return error_mark_node;
12277 if (low_bound
12278 && TREE_CODE (low_bound) == INTEGER_CST
12279 && TYPE_PRECISION (TREE_TYPE (low_bound))
12280 > TYPE_PRECISION (sizetype))
12281 low_bound = fold_convert (sizetype, low_bound);
12282 if (length
12283 && TREE_CODE (length) == INTEGER_CST
12284 && TYPE_PRECISION (TREE_TYPE (length))
12285 > TYPE_PRECISION (sizetype))
12286 length = fold_convert (sizetype, length);
12287 if (low_bound == NULL_TREE)
12288 low_bound = integer_zero_node;
12290 if (length != NULL_TREE)
12292 if (!integer_nonzerop (length))
12294 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12295 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12297 if (integer_zerop (length))
12299 error_at (OMP_CLAUSE_LOCATION (c),
12300 "zero length array section in %qs clause",
12301 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12302 return error_mark_node;
12305 else
12306 maybe_zero_len = true;
12308 if (first_non_one == types.length ()
12309 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12310 first_non_one++;
12312 if (TREE_CODE (type) == ARRAY_TYPE)
12314 if (length == NULL_TREE
12315 && (TYPE_DOMAIN (type) == NULL_TREE
12316 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12318 error_at (OMP_CLAUSE_LOCATION (c),
12319 "for unknown bound array type length expression must "
12320 "be specified");
12321 return error_mark_node;
12323 if (TREE_CODE (low_bound) == INTEGER_CST
12324 && tree_int_cst_sgn (low_bound) == -1)
12326 error_at (OMP_CLAUSE_LOCATION (c),
12327 "negative low bound in array section in %qs clause",
12328 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12329 return error_mark_node;
12331 if (length != NULL_TREE
12332 && TREE_CODE (length) == INTEGER_CST
12333 && tree_int_cst_sgn (length) == -1)
12335 error_at (OMP_CLAUSE_LOCATION (c),
12336 "negative length in array section in %qs clause",
12337 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12338 return error_mark_node;
12340 if (TYPE_DOMAIN (type)
12341 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12342 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12343 == INTEGER_CST)
12345 tree size = size_binop (PLUS_EXPR,
12346 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12347 size_one_node);
12348 if (TREE_CODE (low_bound) == INTEGER_CST)
12350 if (tree_int_cst_lt (size, low_bound))
12352 error_at (OMP_CLAUSE_LOCATION (c),
12353 "low bound %qE above array section size "
12354 "in %qs clause", low_bound,
12355 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12356 return error_mark_node;
12358 if (tree_int_cst_equal (size, low_bound))
12360 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12361 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12363 error_at (OMP_CLAUSE_LOCATION (c),
12364 "zero length array section in %qs clause",
12365 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12366 return error_mark_node;
12368 maybe_zero_len = true;
12370 else if (length == NULL_TREE
12371 && first_non_one == types.length ()
12372 && tree_int_cst_equal
12373 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12374 low_bound))
12375 first_non_one++;
12377 else if (length == NULL_TREE)
12379 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12380 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12381 maybe_zero_len = true;
12382 if (first_non_one == types.length ())
12383 first_non_one++;
12385 if (length && TREE_CODE (length) == INTEGER_CST)
12387 if (tree_int_cst_lt (size, length))
12389 error_at (OMP_CLAUSE_LOCATION (c),
12390 "length %qE above array section size "
12391 "in %qs clause", length,
12392 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12393 return error_mark_node;
12395 if (TREE_CODE (low_bound) == INTEGER_CST)
12397 tree lbpluslen
12398 = size_binop (PLUS_EXPR,
12399 fold_convert (sizetype, low_bound),
12400 fold_convert (sizetype, length));
12401 if (TREE_CODE (lbpluslen) == INTEGER_CST
12402 && tree_int_cst_lt (size, lbpluslen))
12404 error_at (OMP_CLAUSE_LOCATION (c),
12405 "high bound %qE above array section size "
12406 "in %qs clause", lbpluslen,
12407 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12408 return error_mark_node;
12413 else if (length == NULL_TREE)
12415 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12416 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12417 maybe_zero_len = true;
12418 if (first_non_one == types.length ())
12419 first_non_one++;
12422 /* For [lb:] we will need to evaluate lb more than once. */
12423 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12425 tree lb = c_save_expr (low_bound);
12426 if (lb != low_bound)
12428 TREE_PURPOSE (t) = lb;
12429 low_bound = lb;
12433 else if (TREE_CODE (type) == POINTER_TYPE)
12435 if (length == NULL_TREE)
12437 error_at (OMP_CLAUSE_LOCATION (c),
12438 "for pointer type length expression must be specified");
12439 return error_mark_node;
12441 if (length != NULL_TREE
12442 && TREE_CODE (length) == INTEGER_CST
12443 && tree_int_cst_sgn (length) == -1)
12445 error_at (OMP_CLAUSE_LOCATION (c),
12446 "negative length in array section in %qs clause",
12447 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12448 return error_mark_node;
12450 /* If there is a pointer type anywhere but in the very first
12451 array-section-subscript, the array section can't be contiguous. */
12452 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12453 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12455 error_at (OMP_CLAUSE_LOCATION (c),
12456 "array section is not contiguous in %qs clause",
12457 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12458 return error_mark_node;
12461 else
12463 error_at (OMP_CLAUSE_LOCATION (c),
12464 "%qE does not have pointer or array type", ret);
12465 return error_mark_node;
12467 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12468 types.safe_push (TREE_TYPE (ret));
12469 /* We will need to evaluate lb more than once. */
12470 tree lb = c_save_expr (low_bound);
12471 if (lb != low_bound)
12473 TREE_PURPOSE (t) = lb;
12474 low_bound = lb;
12476 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12477 return ret;
12480 /* Handle array sections for clause C. */
12482 static bool
12483 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12485 bool maybe_zero_len = false;
12486 unsigned int first_non_one = 0;
12487 auto_vec<tree, 10> types;
12488 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12489 maybe_zero_len, first_non_one,
12490 ort);
12491 if (first == error_mark_node)
12492 return true;
12493 if (first == NULL_TREE)
12494 return false;
12495 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12497 tree t = OMP_CLAUSE_DECL (c);
12498 tree tem = NULL_TREE;
12499 /* Need to evaluate side effects in the length expressions
12500 if any. */
12501 while (TREE_CODE (t) == TREE_LIST)
12503 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12505 if (tem == NULL_TREE)
12506 tem = TREE_VALUE (t);
12507 else
12508 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12509 TREE_VALUE (t), tem);
12511 t = TREE_CHAIN (t);
12513 if (tem)
12514 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12515 first = c_fully_fold (first, false, NULL);
12516 OMP_CLAUSE_DECL (c) = first;
12518 else
12520 unsigned int num = types.length (), i;
12521 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12522 tree condition = NULL_TREE;
12524 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12525 maybe_zero_len = true;
12527 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12528 t = TREE_CHAIN (t))
12530 tree low_bound = TREE_PURPOSE (t);
12531 tree length = TREE_VALUE (t);
12533 i--;
12534 if (low_bound
12535 && TREE_CODE (low_bound) == INTEGER_CST
12536 && TYPE_PRECISION (TREE_TYPE (low_bound))
12537 > TYPE_PRECISION (sizetype))
12538 low_bound = fold_convert (sizetype, low_bound);
12539 if (length
12540 && TREE_CODE (length) == INTEGER_CST
12541 && TYPE_PRECISION (TREE_TYPE (length))
12542 > TYPE_PRECISION (sizetype))
12543 length = fold_convert (sizetype, length);
12544 if (low_bound == NULL_TREE)
12545 low_bound = integer_zero_node;
12546 if (!maybe_zero_len && i > first_non_one)
12548 if (integer_nonzerop (low_bound))
12549 goto do_warn_noncontiguous;
12550 if (length != NULL_TREE
12551 && TREE_CODE (length) == INTEGER_CST
12552 && TYPE_DOMAIN (types[i])
12553 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12554 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12555 == INTEGER_CST)
12557 tree size;
12558 size = size_binop (PLUS_EXPR,
12559 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12560 size_one_node);
12561 if (!tree_int_cst_equal (length, size))
12563 do_warn_noncontiguous:
12564 error_at (OMP_CLAUSE_LOCATION (c),
12565 "array section is not contiguous in %qs "
12566 "clause",
12567 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12568 return true;
12571 if (length != NULL_TREE
12572 && TREE_SIDE_EFFECTS (length))
12574 if (side_effects == NULL_TREE)
12575 side_effects = length;
12576 else
12577 side_effects = build2 (COMPOUND_EXPR,
12578 TREE_TYPE (side_effects),
12579 length, side_effects);
12582 else
12584 tree l;
12586 if (i > first_non_one
12587 && ((length && integer_nonzerop (length))
12588 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12589 continue;
12590 if (length)
12591 l = fold_convert (sizetype, length);
12592 else
12594 l = size_binop (PLUS_EXPR,
12595 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12596 size_one_node);
12597 l = size_binop (MINUS_EXPR, l,
12598 fold_convert (sizetype, low_bound));
12600 if (i > first_non_one)
12602 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12603 size_zero_node);
12604 if (condition == NULL_TREE)
12605 condition = l;
12606 else
12607 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12608 l, condition);
12610 else if (size == NULL_TREE)
12612 size = size_in_bytes (TREE_TYPE (types[i]));
12613 tree eltype = TREE_TYPE (types[num - 1]);
12614 while (TREE_CODE (eltype) == ARRAY_TYPE)
12615 eltype = TREE_TYPE (eltype);
12616 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12618 if (integer_zerop (size)
12619 || integer_zerop (size_in_bytes (eltype)))
12621 error_at (OMP_CLAUSE_LOCATION (c),
12622 "zero length array section in %qs clause",
12623 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12624 return error_mark_node;
12626 size = size_binop (EXACT_DIV_EXPR, size,
12627 size_in_bytes (eltype));
12629 size = size_binop (MULT_EXPR, size, l);
12630 if (condition)
12631 size = fold_build3 (COND_EXPR, sizetype, condition,
12632 size, size_zero_node);
12634 else
12635 size = size_binop (MULT_EXPR, size, l);
12638 if (side_effects)
12639 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12640 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12642 size = size_binop (MINUS_EXPR, size, size_one_node);
12643 size = c_fully_fold (size, false, NULL);
12644 tree index_type = build_index_type (size);
12645 tree eltype = TREE_TYPE (first);
12646 while (TREE_CODE (eltype) == ARRAY_TYPE)
12647 eltype = TREE_TYPE (eltype);
12648 tree type = build_array_type (eltype, index_type);
12649 tree ptype = build_pointer_type (eltype);
12650 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12651 t = build_fold_addr_expr (t);
12652 tree t2 = build_fold_addr_expr (first);
12653 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12654 ptrdiff_type_node, t2);
12655 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12656 ptrdiff_type_node, t2,
12657 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12658 ptrdiff_type_node, t));
12659 t2 = c_fully_fold (t2, false, NULL);
12660 if (tree_fits_shwi_p (t2))
12661 t = build2 (MEM_REF, type, t,
12662 build_int_cst (ptype, tree_to_shwi (t2)));
12663 else
12665 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12666 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12667 TREE_TYPE (t), t, t2);
12668 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12670 OMP_CLAUSE_DECL (c) = t;
12671 return false;
12673 first = c_fully_fold (first, false, NULL);
12674 OMP_CLAUSE_DECL (c) = first;
12675 if (size)
12676 size = c_fully_fold (size, false, NULL);
12677 OMP_CLAUSE_SIZE (c) = size;
12678 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12679 || (TREE_CODE (t) == COMPONENT_REF
12680 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12681 return false;
12682 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12683 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12684 switch (OMP_CLAUSE_MAP_KIND (c))
12686 case GOMP_MAP_ALLOC:
12687 case GOMP_MAP_TO:
12688 case GOMP_MAP_FROM:
12689 case GOMP_MAP_TOFROM:
12690 case GOMP_MAP_ALWAYS_TO:
12691 case GOMP_MAP_ALWAYS_FROM:
12692 case GOMP_MAP_ALWAYS_TOFROM:
12693 case GOMP_MAP_RELEASE:
12694 case GOMP_MAP_DELETE:
12695 case GOMP_MAP_FORCE_TO:
12696 case GOMP_MAP_FORCE_FROM:
12697 case GOMP_MAP_FORCE_TOFROM:
12698 case GOMP_MAP_FORCE_PRESENT:
12699 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12700 break;
12701 default:
12702 break;
12704 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12705 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12706 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12707 else if (TREE_CODE (t) == COMPONENT_REF)
12708 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12709 else
12710 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12711 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12712 && !c_mark_addressable (t))
12713 return false;
12714 OMP_CLAUSE_DECL (c2) = t;
12715 t = build_fold_addr_expr (first);
12716 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12717 tree ptr = OMP_CLAUSE_DECL (c2);
12718 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12719 ptr = build_fold_addr_expr (ptr);
12720 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12721 ptrdiff_type_node, t,
12722 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12723 ptrdiff_type_node, ptr));
12724 t = c_fully_fold (t, false, NULL);
12725 OMP_CLAUSE_SIZE (c2) = t;
12726 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12727 OMP_CLAUSE_CHAIN (c) = c2;
12729 return false;
12732 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12733 an inline call. But, remap
12734 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12735 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12737 static tree
12738 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12739 tree decl, tree placeholder)
12741 copy_body_data id;
12742 hash_map<tree, tree> decl_map;
12744 decl_map.put (omp_decl1, placeholder);
12745 decl_map.put (omp_decl2, decl);
12746 memset (&id, 0, sizeof (id));
12747 id.src_fn = DECL_CONTEXT (omp_decl1);
12748 id.dst_fn = current_function_decl;
12749 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12750 id.decl_map = &decl_map;
12752 id.copy_decl = copy_decl_no_change;
12753 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12754 id.transform_new_cfg = true;
12755 id.transform_return_to_modify = false;
12756 id.transform_lang_insert_block = NULL;
12757 id.eh_lp_nr = 0;
12758 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12759 return stmt;
12762 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12763 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12765 static tree
12766 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12768 if (*tp == (tree) data)
12769 return *tp;
12770 return NULL_TREE;
12773 /* For all elements of CLAUSES, validate them against their constraints.
12774 Remove any elements from the list that are invalid. */
12776 tree
12777 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12779 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12780 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12781 tree c, t, type, *pc;
12782 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12783 bool branch_seen = false;
12784 bool copyprivate_seen = false;
12785 bool linear_variable_step_check = false;
12786 tree *nowait_clause = NULL;
12787 bool ordered_seen = false;
12788 tree schedule_clause = NULL_TREE;
12789 bool oacc_async = false;
12791 bitmap_obstack_initialize (NULL);
12792 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12793 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12794 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12795 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12796 bitmap_initialize (&map_head, &bitmap_default_obstack);
12797 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12798 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12800 if (ort & C_ORT_ACC)
12801 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12802 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12804 oacc_async = true;
12805 break;
12808 for (pc = &clauses, c = clauses; c ; c = *pc)
12810 bool remove = false;
12811 bool need_complete = false;
12812 bool need_implicitly_determined = false;
12814 switch (OMP_CLAUSE_CODE (c))
12816 case OMP_CLAUSE_SHARED:
12817 need_implicitly_determined = true;
12818 goto check_dup_generic;
12820 case OMP_CLAUSE_PRIVATE:
12821 need_complete = true;
12822 need_implicitly_determined = true;
12823 goto check_dup_generic;
12825 case OMP_CLAUSE_REDUCTION:
12826 need_implicitly_determined = true;
12827 t = OMP_CLAUSE_DECL (c);
12828 if (TREE_CODE (t) == TREE_LIST)
12830 if (handle_omp_array_sections (c, ort))
12832 remove = true;
12833 break;
12836 t = OMP_CLAUSE_DECL (c);
12838 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12839 if (t == error_mark_node)
12841 remove = true;
12842 break;
12844 if (oacc_async)
12845 c_mark_addressable (t);
12846 type = TREE_TYPE (t);
12847 if (TREE_CODE (t) == MEM_REF)
12848 type = TREE_TYPE (type);
12849 if (TREE_CODE (type) == ARRAY_TYPE)
12851 tree oatype = type;
12852 gcc_assert (TREE_CODE (t) != MEM_REF);
12853 while (TREE_CODE (type) == ARRAY_TYPE)
12854 type = TREE_TYPE (type);
12855 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12857 error_at (OMP_CLAUSE_LOCATION (c),
12858 "%qD in %<reduction%> clause is a zero size array",
12860 remove = true;
12861 break;
12863 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12864 TYPE_SIZE_UNIT (type));
12865 if (integer_zerop (size))
12867 error_at (OMP_CLAUSE_LOCATION (c),
12868 "%qD in %<reduction%> clause is a zero size array",
12870 remove = true;
12871 break;
12873 size = size_binop (MINUS_EXPR, size, size_one_node);
12874 tree index_type = build_index_type (size);
12875 tree atype = build_array_type (type, index_type);
12876 tree ptype = build_pointer_type (type);
12877 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12878 t = build_fold_addr_expr (t);
12879 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12880 OMP_CLAUSE_DECL (c) = t;
12882 if (TYPE_ATOMIC (type))
12884 error_at (OMP_CLAUSE_LOCATION (c),
12885 "%<_Atomic%> %qE in %<reduction%> clause", t);
12886 remove = true;
12887 break;
12889 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12890 && (FLOAT_TYPE_P (type)
12891 || TREE_CODE (type) == COMPLEX_TYPE))
12893 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12894 const char *r_name = NULL;
12896 switch (r_code)
12898 case PLUS_EXPR:
12899 case MULT_EXPR:
12900 case MINUS_EXPR:
12901 break;
12902 case MIN_EXPR:
12903 if (TREE_CODE (type) == COMPLEX_TYPE)
12904 r_name = "min";
12905 break;
12906 case MAX_EXPR:
12907 if (TREE_CODE (type) == COMPLEX_TYPE)
12908 r_name = "max";
12909 break;
12910 case BIT_AND_EXPR:
12911 r_name = "&";
12912 break;
12913 case BIT_XOR_EXPR:
12914 r_name = "^";
12915 break;
12916 case BIT_IOR_EXPR:
12917 r_name = "|";
12918 break;
12919 case TRUTH_ANDIF_EXPR:
12920 if (FLOAT_TYPE_P (type))
12921 r_name = "&&";
12922 break;
12923 case TRUTH_ORIF_EXPR:
12924 if (FLOAT_TYPE_P (type))
12925 r_name = "||";
12926 break;
12927 default:
12928 gcc_unreachable ();
12930 if (r_name)
12932 error_at (OMP_CLAUSE_LOCATION (c),
12933 "%qE has invalid type for %<reduction(%s)%>",
12934 t, r_name);
12935 remove = true;
12936 break;
12939 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12941 error_at (OMP_CLAUSE_LOCATION (c),
12942 "user defined reduction not found for %qE", t);
12943 remove = true;
12944 break;
12946 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12948 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12949 type = TYPE_MAIN_VARIANT (type);
12950 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12951 VAR_DECL, NULL_TREE, type);
12952 tree decl_placeholder = NULL_TREE;
12953 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12954 DECL_ARTIFICIAL (placeholder) = 1;
12955 DECL_IGNORED_P (placeholder) = 1;
12956 if (TREE_CODE (t) == MEM_REF)
12958 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12959 VAR_DECL, NULL_TREE, type);
12960 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12961 DECL_ARTIFICIAL (decl_placeholder) = 1;
12962 DECL_IGNORED_P (decl_placeholder) = 1;
12964 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12965 c_mark_addressable (placeholder);
12966 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12967 c_mark_addressable (decl_placeholder ? decl_placeholder
12968 : OMP_CLAUSE_DECL (c));
12969 OMP_CLAUSE_REDUCTION_MERGE (c)
12970 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12971 TREE_VEC_ELT (list, 0),
12972 TREE_VEC_ELT (list, 1),
12973 decl_placeholder ? decl_placeholder
12974 : OMP_CLAUSE_DECL (c), placeholder);
12975 OMP_CLAUSE_REDUCTION_MERGE (c)
12976 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12977 void_type_node, NULL_TREE,
12978 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12979 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12980 if (TREE_VEC_LENGTH (list) == 6)
12982 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12983 c_mark_addressable (decl_placeholder ? decl_placeholder
12984 : OMP_CLAUSE_DECL (c));
12985 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12986 c_mark_addressable (placeholder);
12987 tree init = TREE_VEC_ELT (list, 5);
12988 if (init == error_mark_node)
12989 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12990 OMP_CLAUSE_REDUCTION_INIT (c)
12991 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12992 TREE_VEC_ELT (list, 3),
12993 decl_placeholder ? decl_placeholder
12994 : OMP_CLAUSE_DECL (c), placeholder);
12995 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12997 tree v = decl_placeholder ? decl_placeholder : t;
12998 OMP_CLAUSE_REDUCTION_INIT (c)
12999 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13000 OMP_CLAUSE_REDUCTION_INIT (c));
13002 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13003 c_find_omp_placeholder_r,
13004 placeholder, NULL))
13005 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13007 else
13009 tree init;
13010 tree v = decl_placeholder ? decl_placeholder : t;
13011 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13012 init = build_constructor (TREE_TYPE (v), NULL);
13013 else
13014 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13015 OMP_CLAUSE_REDUCTION_INIT (c)
13016 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13018 OMP_CLAUSE_REDUCTION_INIT (c)
13019 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13020 void_type_node, NULL_TREE,
13021 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13022 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13024 if (TREE_CODE (t) == MEM_REF)
13026 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13027 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13028 != INTEGER_CST)
13030 sorry ("variable length element type in array "
13031 "%<reduction%> clause");
13032 remove = true;
13033 break;
13035 t = TREE_OPERAND (t, 0);
13036 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13037 t = TREE_OPERAND (t, 0);
13038 if (TREE_CODE (t) == ADDR_EXPR)
13039 t = TREE_OPERAND (t, 0);
13041 goto check_dup_generic_t;
13043 case OMP_CLAUSE_COPYPRIVATE:
13044 copyprivate_seen = true;
13045 if (nowait_clause)
13047 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13048 "%<nowait%> clause must not be used together "
13049 "with %<copyprivate%>");
13050 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13051 nowait_clause = NULL;
13053 goto check_dup_generic;
13055 case OMP_CLAUSE_COPYIN:
13056 t = OMP_CLAUSE_DECL (c);
13057 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13059 error_at (OMP_CLAUSE_LOCATION (c),
13060 "%qE must be %<threadprivate%> for %<copyin%>", t);
13061 remove = true;
13062 break;
13064 goto check_dup_generic;
13066 case OMP_CLAUSE_LINEAR:
13067 if (ort != C_ORT_OMP_DECLARE_SIMD)
13068 need_implicitly_determined = true;
13069 t = OMP_CLAUSE_DECL (c);
13070 if (ort != C_ORT_OMP_DECLARE_SIMD
13071 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13073 error_at (OMP_CLAUSE_LOCATION (c),
13074 "modifier should not be specified in %<linear%> "
13075 "clause on %<simd%> or %<for%> constructs");
13076 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13078 if (ort & C_ORT_CILK)
13080 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13081 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13082 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13084 error_at (OMP_CLAUSE_LOCATION (c),
13085 "linear clause applied to non-integral, "
13086 "non-floating, non-pointer variable with type %qT",
13087 TREE_TYPE (t));
13088 remove = true;
13089 break;
13092 else
13094 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13095 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13097 error_at (OMP_CLAUSE_LOCATION (c),
13098 "linear clause applied to non-integral non-pointer "
13099 "variable with type %qT", TREE_TYPE (t));
13100 remove = true;
13101 break;
13103 if (TYPE_ATOMIC (TREE_TYPE (t)))
13105 error_at (OMP_CLAUSE_LOCATION (c),
13106 "%<_Atomic%> %qD in %<linear%> clause", t);
13107 remove = true;
13108 break;
13111 if (ort == C_ORT_OMP_DECLARE_SIMD)
13113 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13114 if (TREE_CODE (s) == PARM_DECL)
13116 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13117 /* map_head bitmap is used as uniform_head if
13118 declare_simd. */
13119 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13120 linear_variable_step_check = true;
13121 goto check_dup_generic;
13123 if (TREE_CODE (s) != INTEGER_CST)
13125 error_at (OMP_CLAUSE_LOCATION (c),
13126 "%<linear%> clause step %qE is neither constant "
13127 "nor a parameter", s);
13128 remove = true;
13129 break;
13132 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13134 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13135 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13136 OMP_CLAUSE_DECL (c), s);
13137 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13138 sizetype, fold_convert (sizetype, s),
13139 fold_convert
13140 (sizetype, OMP_CLAUSE_DECL (c)));
13141 if (s == error_mark_node)
13142 s = size_one_node;
13143 OMP_CLAUSE_LINEAR_STEP (c) = s;
13145 else
13146 OMP_CLAUSE_LINEAR_STEP (c)
13147 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13148 goto check_dup_generic;
13150 check_dup_generic:
13151 t = OMP_CLAUSE_DECL (c);
13152 check_dup_generic_t:
13153 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13155 error_at (OMP_CLAUSE_LOCATION (c),
13156 "%qE is not a variable in clause %qs", t,
13157 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13158 remove = true;
13160 else if (ort == C_ORT_ACC
13161 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13163 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13165 error ("%qD appears more than once in reduction clauses", t);
13166 remove = true;
13168 else
13169 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13171 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13172 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13173 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13175 error_at (OMP_CLAUSE_LOCATION (c),
13176 "%qE appears more than once in data clauses", t);
13177 remove = true;
13179 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13180 && bitmap_bit_p (&map_head, DECL_UID (t)))
13182 if (ort == C_ORT_ACC)
13183 error ("%qD appears more than once in data clauses", t);
13184 else
13185 error ("%qD appears both in data and map clauses", t);
13186 remove = true;
13188 else
13189 bitmap_set_bit (&generic_head, DECL_UID (t));
13190 break;
13192 case OMP_CLAUSE_FIRSTPRIVATE:
13193 t = OMP_CLAUSE_DECL (c);
13194 need_complete = true;
13195 need_implicitly_determined = true;
13196 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13198 error_at (OMP_CLAUSE_LOCATION (c),
13199 "%qE is not a variable in clause %<firstprivate%>", t);
13200 remove = true;
13202 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13203 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13205 error_at (OMP_CLAUSE_LOCATION (c),
13206 "%qE appears more than once in data clauses", t);
13207 remove = true;
13209 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13211 if (ort == C_ORT_ACC)
13212 error ("%qD appears more than once in data clauses", t);
13213 else
13214 error ("%qD appears both in data and map clauses", t);
13215 remove = true;
13217 else
13218 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13219 break;
13221 case OMP_CLAUSE_LASTPRIVATE:
13222 t = OMP_CLAUSE_DECL (c);
13223 need_complete = true;
13224 need_implicitly_determined = true;
13225 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13227 error_at (OMP_CLAUSE_LOCATION (c),
13228 "%qE is not a variable in clause %<lastprivate%>", t);
13229 remove = true;
13231 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13232 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13234 error_at (OMP_CLAUSE_LOCATION (c),
13235 "%qE appears more than once in data clauses", t);
13236 remove = true;
13238 else
13239 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13240 break;
13242 case OMP_CLAUSE_ALIGNED:
13243 t = OMP_CLAUSE_DECL (c);
13244 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13246 error_at (OMP_CLAUSE_LOCATION (c),
13247 "%qE is not a variable in %<aligned%> clause", t);
13248 remove = true;
13250 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13251 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13253 error_at (OMP_CLAUSE_LOCATION (c),
13254 "%qE in %<aligned%> clause is neither a pointer nor "
13255 "an array", t);
13256 remove = true;
13258 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13260 error_at (OMP_CLAUSE_LOCATION (c),
13261 "%<_Atomic%> %qD in %<aligned%> clause", t);
13262 remove = true;
13263 break;
13265 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13267 error_at (OMP_CLAUSE_LOCATION (c),
13268 "%qE appears more than once in %<aligned%> clauses",
13270 remove = true;
13272 else
13273 bitmap_set_bit (&aligned_head, DECL_UID (t));
13274 break;
13276 case OMP_CLAUSE_DEPEND:
13277 t = OMP_CLAUSE_DECL (c);
13278 if (t == NULL_TREE)
13280 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13281 == OMP_CLAUSE_DEPEND_SOURCE);
13282 break;
13284 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13286 gcc_assert (TREE_CODE (t) == TREE_LIST);
13287 for (; t; t = TREE_CHAIN (t))
13289 tree decl = TREE_VALUE (t);
13290 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13292 tree offset = TREE_PURPOSE (t);
13293 bool neg = wi::neg_p ((wide_int) offset);
13294 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13295 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13296 neg ? MINUS_EXPR : PLUS_EXPR,
13297 decl, offset);
13298 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13299 sizetype,
13300 fold_convert (sizetype, t2),
13301 fold_convert (sizetype, decl));
13302 if (t2 == error_mark_node)
13304 remove = true;
13305 break;
13307 TREE_PURPOSE (t) = t2;
13310 break;
13312 if (TREE_CODE (t) == TREE_LIST)
13314 if (handle_omp_array_sections (c, ort))
13315 remove = true;
13316 break;
13318 if (t == error_mark_node)
13319 remove = true;
13320 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13322 error_at (OMP_CLAUSE_LOCATION (c),
13323 "%qE is not a variable in %<depend%> clause", t);
13324 remove = true;
13326 else if (!c_mark_addressable (t))
13327 remove = true;
13328 break;
13330 case OMP_CLAUSE_MAP:
13331 case OMP_CLAUSE_TO:
13332 case OMP_CLAUSE_FROM:
13333 case OMP_CLAUSE__CACHE_:
13334 t = OMP_CLAUSE_DECL (c);
13335 if (TREE_CODE (t) == TREE_LIST)
13337 if (handle_omp_array_sections (c, ort))
13338 remove = true;
13339 else
13341 t = OMP_CLAUSE_DECL (c);
13342 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13344 error_at (OMP_CLAUSE_LOCATION (c),
13345 "array section does not have mappable type "
13346 "in %qs clause",
13347 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13348 remove = true;
13350 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13352 error_at (OMP_CLAUSE_LOCATION (c),
13353 "%<_Atomic%> %qE in %qs clause", t,
13354 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13355 remove = true;
13357 while (TREE_CODE (t) == ARRAY_REF)
13358 t = TREE_OPERAND (t, 0);
13359 if (TREE_CODE (t) == COMPONENT_REF
13360 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13362 while (TREE_CODE (t) == COMPONENT_REF)
13363 t = TREE_OPERAND (t, 0);
13364 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13365 break;
13366 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13368 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13369 error ("%qD appears more than once in motion"
13370 " clauses", t);
13371 else if (ort == C_ORT_ACC)
13372 error ("%qD appears more than once in data"
13373 " clauses", t);
13374 else
13375 error ("%qD appears more than once in map"
13376 " clauses", t);
13377 remove = true;
13379 else
13381 bitmap_set_bit (&map_head, DECL_UID (t));
13382 bitmap_set_bit (&map_field_head, DECL_UID (t));
13386 break;
13388 if (t == error_mark_node)
13390 remove = true;
13391 break;
13393 if (TREE_CODE (t) == COMPONENT_REF
13394 && (ort & C_ORT_OMP)
13395 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13397 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13399 error_at (OMP_CLAUSE_LOCATION (c),
13400 "bit-field %qE in %qs clause",
13401 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13402 remove = true;
13404 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13406 error_at (OMP_CLAUSE_LOCATION (c),
13407 "%qE does not have a mappable type in %qs clause",
13408 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13409 remove = true;
13411 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13413 error_at (OMP_CLAUSE_LOCATION (c),
13414 "%<_Atomic%> %qE in %qs clause", t,
13415 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13416 remove = true;
13418 while (TREE_CODE (t) == COMPONENT_REF)
13420 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13421 == UNION_TYPE)
13423 error_at (OMP_CLAUSE_LOCATION (c),
13424 "%qE is a member of a union", t);
13425 remove = true;
13426 break;
13428 t = TREE_OPERAND (t, 0);
13430 if (remove)
13431 break;
13432 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13434 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13435 break;
13438 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13440 error_at (OMP_CLAUSE_LOCATION (c),
13441 "%qE is not a variable in %qs clause", t,
13442 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13443 remove = true;
13445 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13447 error_at (OMP_CLAUSE_LOCATION (c),
13448 "%qD is threadprivate variable in %qs clause", t,
13449 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13450 remove = true;
13452 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13453 || (OMP_CLAUSE_MAP_KIND (c)
13454 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13455 && !c_mark_addressable (t))
13456 remove = true;
13457 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13458 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13459 || (OMP_CLAUSE_MAP_KIND (c)
13460 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13461 || (OMP_CLAUSE_MAP_KIND (c)
13462 == GOMP_MAP_FORCE_DEVICEPTR)))
13463 && t == OMP_CLAUSE_DECL (c)
13464 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13466 error_at (OMP_CLAUSE_LOCATION (c),
13467 "%qD does not have a mappable type in %qs clause", t,
13468 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13469 remove = true;
13471 else if (TREE_TYPE (t) == error_mark_node)
13472 remove = true;
13473 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13475 error_at (OMP_CLAUSE_LOCATION (c),
13476 "%<_Atomic%> %qE in %qs clause", t,
13477 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13478 remove = true;
13480 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13481 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13483 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13484 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13486 error ("%qD appears more than once in data clauses", t);
13487 remove = true;
13489 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13491 if (ort == C_ORT_ACC)
13492 error ("%qD appears more than once in data clauses", t);
13493 else
13494 error ("%qD appears both in data and map clauses", t);
13495 remove = true;
13497 else
13498 bitmap_set_bit (&generic_head, DECL_UID (t));
13500 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13502 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13503 error ("%qD appears more than once in motion clauses", t);
13504 else if (ort == C_ORT_ACC)
13505 error ("%qD appears more than once in data clauses", t);
13506 else
13507 error ("%qD appears more than once in map clauses", t);
13508 remove = true;
13510 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13511 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13513 if (ort == C_ORT_ACC)
13514 error ("%qD appears more than once in data clauses", t);
13515 else
13516 error ("%qD appears both in data and map clauses", t);
13517 remove = true;
13519 else
13521 bitmap_set_bit (&map_head, DECL_UID (t));
13522 if (t != OMP_CLAUSE_DECL (c)
13523 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13524 bitmap_set_bit (&map_field_head, DECL_UID (t));
13526 break;
13528 case OMP_CLAUSE_TO_DECLARE:
13529 case OMP_CLAUSE_LINK:
13530 t = OMP_CLAUSE_DECL (c);
13531 if (TREE_CODE (t) == FUNCTION_DECL
13532 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13534 else if (!VAR_P (t))
13536 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13537 error_at (OMP_CLAUSE_LOCATION (c),
13538 "%qE is neither a variable nor a function name in "
13539 "clause %qs", t,
13540 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13541 else
13542 error_at (OMP_CLAUSE_LOCATION (c),
13543 "%qE is not a variable in clause %qs", t,
13544 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13545 remove = true;
13547 else if (DECL_THREAD_LOCAL_P (t))
13549 error_at (OMP_CLAUSE_LOCATION (c),
13550 "%qD is threadprivate variable in %qs clause", t,
13551 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13552 remove = true;
13554 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13556 error_at (OMP_CLAUSE_LOCATION (c),
13557 "%qD does not have a mappable type in %qs clause", t,
13558 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13559 remove = true;
13561 if (remove)
13562 break;
13563 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13565 error_at (OMP_CLAUSE_LOCATION (c),
13566 "%qE appears more than once on the same "
13567 "%<declare target%> directive", t);
13568 remove = true;
13570 else
13571 bitmap_set_bit (&generic_head, DECL_UID (t));
13572 break;
13574 case OMP_CLAUSE_UNIFORM:
13575 t = OMP_CLAUSE_DECL (c);
13576 if (TREE_CODE (t) != PARM_DECL)
13578 if (DECL_P (t))
13579 error_at (OMP_CLAUSE_LOCATION (c),
13580 "%qD is not an argument in %<uniform%> clause", t);
13581 else
13582 error_at (OMP_CLAUSE_LOCATION (c),
13583 "%qE is not an argument in %<uniform%> clause", t);
13584 remove = true;
13585 break;
13587 /* map_head bitmap is used as uniform_head if declare_simd. */
13588 bitmap_set_bit (&map_head, DECL_UID (t));
13589 goto check_dup_generic;
13591 case OMP_CLAUSE_IS_DEVICE_PTR:
13592 case OMP_CLAUSE_USE_DEVICE_PTR:
13593 t = OMP_CLAUSE_DECL (c);
13594 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13595 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13597 error_at (OMP_CLAUSE_LOCATION (c),
13598 "%qs variable is neither a pointer nor an array",
13599 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13600 remove = true;
13602 goto check_dup_generic;
13604 case OMP_CLAUSE_NOWAIT:
13605 if (copyprivate_seen)
13607 error_at (OMP_CLAUSE_LOCATION (c),
13608 "%<nowait%> clause must not be used together "
13609 "with %<copyprivate%>");
13610 remove = true;
13611 break;
13613 nowait_clause = pc;
13614 pc = &OMP_CLAUSE_CHAIN (c);
13615 continue;
13617 case OMP_CLAUSE_IF:
13618 case OMP_CLAUSE_NUM_THREADS:
13619 case OMP_CLAUSE_NUM_TEAMS:
13620 case OMP_CLAUSE_THREAD_LIMIT:
13621 case OMP_CLAUSE_DEFAULT:
13622 case OMP_CLAUSE_UNTIED:
13623 case OMP_CLAUSE_COLLAPSE:
13624 case OMP_CLAUSE_FINAL:
13625 case OMP_CLAUSE_MERGEABLE:
13626 case OMP_CLAUSE_DEVICE:
13627 case OMP_CLAUSE_DIST_SCHEDULE:
13628 case OMP_CLAUSE_PARALLEL:
13629 case OMP_CLAUSE_FOR:
13630 case OMP_CLAUSE_SECTIONS:
13631 case OMP_CLAUSE_TASKGROUP:
13632 case OMP_CLAUSE_PROC_BIND:
13633 case OMP_CLAUSE_PRIORITY:
13634 case OMP_CLAUSE_GRAINSIZE:
13635 case OMP_CLAUSE_NUM_TASKS:
13636 case OMP_CLAUSE_NOGROUP:
13637 case OMP_CLAUSE_THREADS:
13638 case OMP_CLAUSE_SIMD:
13639 case OMP_CLAUSE_HINT:
13640 case OMP_CLAUSE_DEFAULTMAP:
13641 case OMP_CLAUSE__CILK_FOR_COUNT_:
13642 case OMP_CLAUSE_NUM_GANGS:
13643 case OMP_CLAUSE_NUM_WORKERS:
13644 case OMP_CLAUSE_VECTOR_LENGTH:
13645 case OMP_CLAUSE_ASYNC:
13646 case OMP_CLAUSE_WAIT:
13647 case OMP_CLAUSE_AUTO:
13648 case OMP_CLAUSE_INDEPENDENT:
13649 case OMP_CLAUSE_SEQ:
13650 case OMP_CLAUSE_GANG:
13651 case OMP_CLAUSE_WORKER:
13652 case OMP_CLAUSE_VECTOR:
13653 case OMP_CLAUSE_TILE:
13654 pc = &OMP_CLAUSE_CHAIN (c);
13655 continue;
13657 case OMP_CLAUSE_SCHEDULE:
13658 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13660 const char *p = NULL;
13661 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13663 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13664 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13665 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13666 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13667 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13668 default: gcc_unreachable ();
13670 if (p)
13672 error_at (OMP_CLAUSE_LOCATION (c),
13673 "%<nonmonotonic%> modifier specified for %qs "
13674 "schedule kind", p);
13675 OMP_CLAUSE_SCHEDULE_KIND (c)
13676 = (enum omp_clause_schedule_kind)
13677 (OMP_CLAUSE_SCHEDULE_KIND (c)
13678 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13681 schedule_clause = c;
13682 pc = &OMP_CLAUSE_CHAIN (c);
13683 continue;
13685 case OMP_CLAUSE_ORDERED:
13686 ordered_seen = true;
13687 pc = &OMP_CLAUSE_CHAIN (c);
13688 continue;
13690 case OMP_CLAUSE_SAFELEN:
13691 safelen = c;
13692 pc = &OMP_CLAUSE_CHAIN (c);
13693 continue;
13694 case OMP_CLAUSE_SIMDLEN:
13695 simdlen = c;
13696 pc = &OMP_CLAUSE_CHAIN (c);
13697 continue;
13699 case OMP_CLAUSE_INBRANCH:
13700 case OMP_CLAUSE_NOTINBRANCH:
13701 if (branch_seen)
13703 error_at (OMP_CLAUSE_LOCATION (c),
13704 "%<inbranch%> clause is incompatible with "
13705 "%<notinbranch%>");
13706 remove = true;
13707 break;
13709 branch_seen = true;
13710 pc = &OMP_CLAUSE_CHAIN (c);
13711 continue;
13713 default:
13714 gcc_unreachable ();
13717 if (!remove)
13719 t = OMP_CLAUSE_DECL (c);
13721 if (need_complete)
13723 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13724 if (t == error_mark_node)
13725 remove = true;
13728 if (need_implicitly_determined)
13730 const char *share_name = NULL;
13732 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13733 share_name = "threadprivate";
13734 else switch (c_omp_predetermined_sharing (t))
13736 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13737 break;
13738 case OMP_CLAUSE_DEFAULT_SHARED:
13739 /* const vars may be specified in firstprivate clause. */
13740 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13741 && TREE_READONLY (t))
13742 break;
13743 share_name = "shared";
13744 break;
13745 case OMP_CLAUSE_DEFAULT_PRIVATE:
13746 share_name = "private";
13747 break;
13748 default:
13749 gcc_unreachable ();
13751 if (share_name)
13753 error_at (OMP_CLAUSE_LOCATION (c),
13754 "%qE is predetermined %qs for %qs",
13755 t, share_name,
13756 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13757 remove = true;
13762 if (remove)
13763 *pc = OMP_CLAUSE_CHAIN (c);
13764 else
13765 pc = &OMP_CLAUSE_CHAIN (c);
13768 if (simdlen
13769 && safelen
13770 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13771 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13773 error_at (OMP_CLAUSE_LOCATION (simdlen),
13774 "%<simdlen%> clause value is bigger than "
13775 "%<safelen%> clause value");
13776 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13777 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13780 if (ordered_seen
13781 && schedule_clause
13782 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13783 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13785 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13786 "%<nonmonotonic%> schedule modifier specified together "
13787 "with %<ordered%> clause");
13788 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13789 = (enum omp_clause_schedule_kind)
13790 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13791 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13794 if (linear_variable_step_check)
13795 for (pc = &clauses, c = clauses; c ; c = *pc)
13797 bool remove = false;
13798 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13799 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13800 && !bitmap_bit_p (&map_head,
13801 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13803 error_at (OMP_CLAUSE_LOCATION (c),
13804 "%<linear%> clause step is a parameter %qD not "
13805 "specified in %<uniform%> clause",
13806 OMP_CLAUSE_LINEAR_STEP (c));
13807 remove = true;
13810 if (remove)
13811 *pc = OMP_CLAUSE_CHAIN (c);
13812 else
13813 pc = &OMP_CLAUSE_CHAIN (c);
13816 bitmap_obstack_release (NULL);
13817 return clauses;
13820 /* Return code to initialize DST with a copy constructor from SRC.
13821 C doesn't have copy constructors nor assignment operators, only for
13822 _Atomic vars we need to perform __atomic_load from src into a temporary
13823 followed by __atomic_store of the temporary to dst. */
13825 tree
13826 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13828 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13829 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13831 location_t loc = OMP_CLAUSE_LOCATION (clause);
13832 tree type = TREE_TYPE (dst);
13833 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13834 tree tmp = create_tmp_var (nonatomic_type);
13835 tree tmp_addr = build_fold_addr_expr (tmp);
13836 TREE_ADDRESSABLE (tmp) = 1;
13837 TREE_NO_WARNING (tmp) = 1;
13838 tree src_addr = build_fold_addr_expr (src);
13839 tree dst_addr = build_fold_addr_expr (dst);
13840 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13841 vec<tree, va_gc> *params;
13842 /* Expansion of a generic atomic load may require an addition
13843 element, so allocate enough to prevent a resize. */
13844 vec_alloc (params, 4);
13846 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13847 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13848 params->quick_push (src_addr);
13849 params->quick_push (tmp_addr);
13850 params->quick_push (seq_cst);
13851 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13853 vec_alloc (params, 4);
13855 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13856 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13857 params->quick_push (dst_addr);
13858 params->quick_push (tmp_addr);
13859 params->quick_push (seq_cst);
13860 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13861 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13864 /* Create a transaction node. */
13866 tree
13867 c_finish_transaction (location_t loc, tree block, int flags)
13869 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13870 if (flags & TM_STMT_ATTR_OUTER)
13871 TRANSACTION_EXPR_OUTER (stmt) = 1;
13872 if (flags & TM_STMT_ATTR_RELAXED)
13873 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13874 return add_stmt (stmt);
13877 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13878 down to the element type of an array. If ORIG_QUAL_TYPE is not
13879 NULL, then it should be used as the qualified type
13880 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13881 preserve information about the typedef name from which an array
13882 type was derived). */
13884 tree
13885 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13886 size_t orig_qual_indirect)
13888 if (type == error_mark_node)
13889 return type;
13891 if (TREE_CODE (type) == ARRAY_TYPE)
13893 tree t;
13894 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13895 type_quals, orig_qual_type,
13896 orig_qual_indirect - 1);
13898 /* See if we already have an identically qualified type. */
13899 if (orig_qual_type && orig_qual_indirect == 0)
13900 t = orig_qual_type;
13901 else
13902 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13904 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13905 && TYPE_NAME (t) == TYPE_NAME (type)
13906 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13907 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13908 TYPE_ATTRIBUTES (type)))
13909 break;
13911 if (!t)
13913 tree domain = TYPE_DOMAIN (type);
13915 t = build_variant_type_copy (type);
13916 TREE_TYPE (t) = element_type;
13918 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13919 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13920 SET_TYPE_STRUCTURAL_EQUALITY (t);
13921 else if (TYPE_CANONICAL (element_type) != element_type
13922 || (domain && TYPE_CANONICAL (domain) != domain))
13924 tree unqualified_canon
13925 = build_array_type (TYPE_CANONICAL (element_type),
13926 domain? TYPE_CANONICAL (domain)
13927 : NULL_TREE);
13928 if (TYPE_REVERSE_STORAGE_ORDER (type))
13930 unqualified_canon
13931 = build_distinct_type_copy (unqualified_canon);
13932 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13934 TYPE_CANONICAL (t)
13935 = c_build_qualified_type (unqualified_canon, type_quals);
13937 else
13938 TYPE_CANONICAL (t) = t;
13940 return t;
13943 /* A restrict-qualified pointer type must be a pointer to object or
13944 incomplete type. Note that the use of POINTER_TYPE_P also allows
13945 REFERENCE_TYPEs, which is appropriate for C++. */
13946 if ((type_quals & TYPE_QUAL_RESTRICT)
13947 && (!POINTER_TYPE_P (type)
13948 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13950 error ("invalid use of %<restrict%>");
13951 type_quals &= ~TYPE_QUAL_RESTRICT;
13954 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13955 ? orig_qual_type
13956 : build_qualified_type (type, type_quals));
13957 /* A variant type does not inherit the list of incomplete vars from the
13958 type main variant. */
13959 if (RECORD_OR_UNION_TYPE_P (var_type)
13960 && TYPE_MAIN_VARIANT (var_type) != var_type)
13961 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13962 return var_type;
13965 /* Build a VA_ARG_EXPR for the C parser. */
13967 tree
13968 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
13970 if (error_operand_p (type))
13971 return error_mark_node;
13972 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13973 order because it takes the address of the expression. */
13974 else if (handled_component_p (expr)
13975 && reverse_storage_order_for_component_p (expr))
13977 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13978 return error_mark_node;
13980 else if (!COMPLETE_TYPE_P (type))
13982 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
13983 "type %qT", type);
13984 return error_mark_node;
13986 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13987 warning_at (loc2, OPT_Wc___compat,
13988 "C++ requires promoted type, not enum type, in %<va_arg%>");
13989 return build_va_arg (loc2, expr, type);
13992 /* Return truthvalue of whether T1 is the same tree structure as T2.
13993 Return 1 if they are the same. Return 0 if they are different. */
13995 bool
13996 c_tree_equal (tree t1, tree t2)
13998 enum tree_code code1, code2;
14000 if (t1 == t2)
14001 return true;
14002 if (!t1 || !t2)
14003 return false;
14005 for (code1 = TREE_CODE (t1);
14006 CONVERT_EXPR_CODE_P (code1)
14007 || code1 == NON_LVALUE_EXPR;
14008 code1 = TREE_CODE (t1))
14009 t1 = TREE_OPERAND (t1, 0);
14010 for (code2 = TREE_CODE (t2);
14011 CONVERT_EXPR_CODE_P (code2)
14012 || code2 == NON_LVALUE_EXPR;
14013 code2 = TREE_CODE (t2))
14014 t2 = TREE_OPERAND (t2, 0);
14016 /* They might have become equal now. */
14017 if (t1 == t2)
14018 return true;
14020 if (code1 != code2)
14021 return false;
14023 switch (code1)
14025 case INTEGER_CST:
14026 return wi::eq_p (t1, t2);
14028 case REAL_CST:
14029 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14031 case STRING_CST:
14032 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14033 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14034 TREE_STRING_LENGTH (t1));
14036 case FIXED_CST:
14037 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14038 TREE_FIXED_CST (t2));
14040 case COMPLEX_CST:
14041 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14042 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14044 case VECTOR_CST:
14045 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14047 case CONSTRUCTOR:
14048 /* We need to do this when determining whether or not two
14049 non-type pointer to member function template arguments
14050 are the same. */
14051 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14052 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14053 return false;
14055 tree field, value;
14056 unsigned int i;
14057 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14059 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14060 if (!c_tree_equal (field, elt2->index)
14061 || !c_tree_equal (value, elt2->value))
14062 return false;
14065 return true;
14067 case TREE_LIST:
14068 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14069 return false;
14070 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14071 return false;
14072 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14074 case SAVE_EXPR:
14075 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14077 case CALL_EXPR:
14079 tree arg1, arg2;
14080 call_expr_arg_iterator iter1, iter2;
14081 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14082 return false;
14083 for (arg1 = first_call_expr_arg (t1, &iter1),
14084 arg2 = first_call_expr_arg (t2, &iter2);
14085 arg1 && arg2;
14086 arg1 = next_call_expr_arg (&iter1),
14087 arg2 = next_call_expr_arg (&iter2))
14088 if (!c_tree_equal (arg1, arg2))
14089 return false;
14090 if (arg1 || arg2)
14091 return false;
14092 return true;
14095 case TARGET_EXPR:
14097 tree o1 = TREE_OPERAND (t1, 0);
14098 tree o2 = TREE_OPERAND (t2, 0);
14100 /* Special case: if either target is an unallocated VAR_DECL,
14101 it means that it's going to be unified with whatever the
14102 TARGET_EXPR is really supposed to initialize, so treat it
14103 as being equivalent to anything. */
14104 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14105 && !DECL_RTL_SET_P (o1))
14106 /*Nop*/;
14107 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14108 && !DECL_RTL_SET_P (o2))
14109 /*Nop*/;
14110 else if (!c_tree_equal (o1, o2))
14111 return false;
14113 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14116 case COMPONENT_REF:
14117 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14118 return false;
14119 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14121 case PARM_DECL:
14122 case VAR_DECL:
14123 case CONST_DECL:
14124 case FIELD_DECL:
14125 case FUNCTION_DECL:
14126 case IDENTIFIER_NODE:
14127 case SSA_NAME:
14128 return false;
14130 case TREE_VEC:
14132 unsigned ix;
14133 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14134 return false;
14135 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14136 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14137 TREE_VEC_ELT (t2, ix)))
14138 return false;
14139 return true;
14142 default:
14143 break;
14146 switch (TREE_CODE_CLASS (code1))
14148 case tcc_unary:
14149 case tcc_binary:
14150 case tcc_comparison:
14151 case tcc_expression:
14152 case tcc_vl_exp:
14153 case tcc_reference:
14154 case tcc_statement:
14156 int i, n = TREE_OPERAND_LENGTH (t1);
14158 switch (code1)
14160 case PREINCREMENT_EXPR:
14161 case PREDECREMENT_EXPR:
14162 case POSTINCREMENT_EXPR:
14163 case POSTDECREMENT_EXPR:
14164 n = 1;
14165 break;
14166 case ARRAY_REF:
14167 n = 2;
14168 break;
14169 default:
14170 break;
14173 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14174 && n != TREE_OPERAND_LENGTH (t2))
14175 return false;
14177 for (i = 0; i < n; ++i)
14178 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14179 return false;
14181 return true;
14184 case tcc_type:
14185 return comptypes (t1, t2);
14186 default:
14187 gcc_unreachable ();
14189 /* We can get here with --disable-checking. */
14190 return false;
14193 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14194 spawn-helper and BODY is the newly created body for FNDECL. */
14196 void
14197 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14199 tree list = alloc_stmt_list ();
14200 tree frame = make_cilk_frame (fndecl);
14201 tree dtor = create_cilk_function_exit (frame, false, true);
14202 add_local_decl (cfun, frame);
14204 DECL_SAVED_TREE (fndecl) = list;
14205 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
14206 frame);
14207 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
14208 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
14210 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
14211 append_to_statement_list (detach_expr, &body_list);
14213 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14214 body = fold_build_cleanup_point_expr (void_type_node, body);
14216 append_to_statement_list (body, &body_list);
14217 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14218 body_list, dtor), &list);
14221 /* Returns true when the function declaration FNDECL is implicit,
14222 introduced as a result of a call to an otherwise undeclared
14223 function, and false otherwise. */
14225 bool
14226 c_decl_implicit (const_tree fndecl)
14228 return C_DECL_IMPLICIT (fndecl);