2017-05-01 Xi Ruoyao <ryxi@stu.xidian.edu.cn>
[official-gcc.git] / gcc / c / c-typeck.c
blob6f9909c6396ef77b005c30c89a24984aab5b8856
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, true))
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, fundecl, fntype,
3114 nargs, argarray);
3116 if (name != NULL_TREE
3117 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3119 if (require_constant_value)
3120 result
3121 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3122 function, nargs, argarray);
3123 else
3124 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3125 function, nargs, argarray);
3126 if (TREE_CODE (result) == NOP_EXPR
3127 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3128 STRIP_TYPE_NOPS (result);
3130 else
3131 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3132 function, nargs, argarray);
3133 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3134 later. */
3135 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3136 TREE_NO_WARNING (result) = 1;
3138 /* In this improbable scenario, a nested function returns a VM type.
3139 Create a TARGET_EXPR so that the call always has a LHS, much as
3140 what the C++ FE does for functions returning non-PODs. */
3141 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3143 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3144 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3145 NULL_TREE, NULL_TREE);
3148 if (VOID_TYPE_P (TREE_TYPE (result)))
3150 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3151 pedwarn (loc, 0,
3152 "function with qualified void return type called");
3153 return result;
3155 return require_complete_type (loc, result);
3158 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3160 tree
3161 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3162 tree function, vec<tree, va_gc> *params,
3163 vec<tree, va_gc> *origtypes)
3165 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3166 STRIP_TYPE_NOPS (function);
3168 /* Convert anything with function type to a pointer-to-function. */
3169 if (TREE_CODE (function) == FUNCTION_DECL)
3171 /* Implement type-directed function overloading for builtins.
3172 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3173 handle all the type checking. The result is a complete expression
3174 that implements this function call. */
3175 tree tem = resolve_overloaded_builtin (loc, function, params);
3176 if (tem)
3177 return tem;
3179 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3182 /* Convert the argument expressions in the vector VALUES
3183 to the types in the list TYPELIST.
3185 If TYPELIST is exhausted, or when an element has NULL as its type,
3186 perform the default conversions.
3188 ORIGTYPES is the original types of the expressions in VALUES. This
3189 holds the type of enum values which have been converted to integral
3190 types. It may be NULL.
3192 FUNCTION is a tree for the called function. It is used only for
3193 error messages, where it is formatted with %qE.
3195 This is also where warnings about wrong number of args are generated.
3197 ARG_LOC are locations of function arguments (if any).
3199 Returns the actual number of arguments processed (which may be less
3200 than the length of VALUES in some error situations), or -1 on
3201 failure. */
3203 static int
3204 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3205 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3206 tree function, tree fundecl)
3208 tree typetail, val;
3209 unsigned int parmnum;
3210 bool error_args = false;
3211 const bool type_generic = fundecl
3212 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3213 bool type_generic_remove_excess_precision = false;
3214 bool type_generic_overflow_p = false;
3215 tree selector;
3217 /* Change pointer to function to the function itself for
3218 diagnostics. */
3219 if (TREE_CODE (function) == ADDR_EXPR
3220 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3221 function = TREE_OPERAND (function, 0);
3223 /* Handle an ObjC selector specially for diagnostics. */
3224 selector = objc_message_selector ();
3226 /* For type-generic built-in functions, determine whether excess
3227 precision should be removed (classification) or not
3228 (comparison). */
3229 if (type_generic
3230 && DECL_BUILT_IN (fundecl)
3231 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3233 switch (DECL_FUNCTION_CODE (fundecl))
3235 case BUILT_IN_ISFINITE:
3236 case BUILT_IN_ISINF:
3237 case BUILT_IN_ISINF_SIGN:
3238 case BUILT_IN_ISNAN:
3239 case BUILT_IN_ISNORMAL:
3240 case BUILT_IN_FPCLASSIFY:
3241 type_generic_remove_excess_precision = true;
3242 break;
3244 case BUILT_IN_ADD_OVERFLOW_P:
3245 case BUILT_IN_SUB_OVERFLOW_P:
3246 case BUILT_IN_MUL_OVERFLOW_P:
3247 /* The last argument of these type-generic builtins
3248 should not be promoted. */
3249 type_generic_overflow_p = true;
3250 break;
3252 default:
3253 break;
3256 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3257 return vec_safe_length (values);
3259 /* Scan the given expressions and types, producing individual
3260 converted arguments. */
3262 for (typetail = typelist, parmnum = 0;
3263 values && values->iterate (parmnum, &val);
3264 ++parmnum)
3266 tree type = typetail ? TREE_VALUE (typetail) : 0;
3267 tree valtype = TREE_TYPE (val);
3268 tree rname = function;
3269 int argnum = parmnum + 1;
3270 const char *invalid_func_diag;
3271 bool excess_precision = false;
3272 bool npc;
3273 tree parmval;
3274 /* Some __atomic_* builtins have additional hidden argument at
3275 position 0. */
3276 location_t ploc
3277 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3278 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3279 : input_location;
3281 if (type == void_type_node)
3283 if (selector)
3284 error_at (loc, "too many arguments to method %qE", selector);
3285 else
3286 error_at (loc, "too many arguments to function %qE", function);
3287 inform_declaration (fundecl);
3288 return error_args ? -1 : (int) parmnum;
3291 if (selector && argnum > 2)
3293 rname = selector;
3294 argnum -= 2;
3297 npc = null_pointer_constant_p (val);
3299 /* If there is excess precision and a prototype, convert once to
3300 the required type rather than converting via the semantic
3301 type. Likewise without a prototype a float value represented
3302 as long double should be converted once to double. But for
3303 type-generic classification functions excess precision must
3304 be removed here. */
3305 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3306 && (type || !type_generic || !type_generic_remove_excess_precision))
3308 val = TREE_OPERAND (val, 0);
3309 excess_precision = true;
3311 val = c_fully_fold (val, false, NULL);
3312 STRIP_TYPE_NOPS (val);
3314 val = require_complete_type (ploc, val);
3316 /* Some floating-point arguments must be promoted to double when
3317 no type is specified by a prototype. This applies to
3318 arguments of type float, and to architecture-specific types
3319 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3320 bool promote_float_arg = false;
3321 if (type == NULL_TREE
3322 && TREE_CODE (valtype) == REAL_TYPE
3323 && (TYPE_PRECISION (valtype)
3324 <= TYPE_PRECISION (double_type_node))
3325 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3326 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3327 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3329 /* Promote this argument, unless it has a _FloatN or
3330 _FloatNx type. */
3331 promote_float_arg = true;
3332 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3333 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3335 promote_float_arg = false;
3336 break;
3340 if (type != 0)
3342 /* Formal parm type is specified by a function prototype. */
3344 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3346 error_at (ploc, "type of formal parameter %d is incomplete",
3347 parmnum + 1);
3348 parmval = val;
3350 else
3352 tree origtype;
3354 /* Optionally warn about conversions that
3355 differ from the default conversions. */
3356 if (warn_traditional_conversion || warn_traditional)
3358 unsigned int formal_prec = TYPE_PRECISION (type);
3360 if (INTEGRAL_TYPE_P (type)
3361 && TREE_CODE (valtype) == REAL_TYPE)
3362 warning_at (ploc, OPT_Wtraditional_conversion,
3363 "passing argument %d of %qE as integer rather "
3364 "than floating due to prototype",
3365 argnum, rname);
3366 if (INTEGRAL_TYPE_P (type)
3367 && TREE_CODE (valtype) == COMPLEX_TYPE)
3368 warning_at (ploc, OPT_Wtraditional_conversion,
3369 "passing argument %d of %qE as integer rather "
3370 "than complex due to prototype",
3371 argnum, rname);
3372 else if (TREE_CODE (type) == COMPLEX_TYPE
3373 && TREE_CODE (valtype) == REAL_TYPE)
3374 warning_at (ploc, OPT_Wtraditional_conversion,
3375 "passing argument %d of %qE as complex rather "
3376 "than floating due to prototype",
3377 argnum, rname);
3378 else if (TREE_CODE (type) == REAL_TYPE
3379 && INTEGRAL_TYPE_P (valtype))
3380 warning_at (ploc, OPT_Wtraditional_conversion,
3381 "passing argument %d of %qE as floating rather "
3382 "than integer due to prototype",
3383 argnum, rname);
3384 else if (TREE_CODE (type) == COMPLEX_TYPE
3385 && INTEGRAL_TYPE_P (valtype))
3386 warning_at (ploc, OPT_Wtraditional_conversion,
3387 "passing argument %d of %qE as complex rather "
3388 "than integer due to prototype",
3389 argnum, rname);
3390 else if (TREE_CODE (type) == REAL_TYPE
3391 && TREE_CODE (valtype) == COMPLEX_TYPE)
3392 warning_at (ploc, OPT_Wtraditional_conversion,
3393 "passing argument %d of %qE as floating rather "
3394 "than complex due to prototype",
3395 argnum, rname);
3396 /* ??? At some point, messages should be written about
3397 conversions between complex types, but that's too messy
3398 to do now. */
3399 else if (TREE_CODE (type) == REAL_TYPE
3400 && TREE_CODE (valtype) == REAL_TYPE)
3402 /* Warn if any argument is passed as `float',
3403 since without a prototype it would be `double'. */
3404 if (formal_prec == TYPE_PRECISION (float_type_node)
3405 && type != dfloat32_type_node)
3406 warning_at (ploc, 0,
3407 "passing argument %d of %qE as %<float%> "
3408 "rather than %<double%> due to prototype",
3409 argnum, rname);
3411 /* Warn if mismatch between argument and prototype
3412 for decimal float types. Warn of conversions with
3413 binary float types and of precision narrowing due to
3414 prototype. */
3415 else if (type != valtype
3416 && (type == dfloat32_type_node
3417 || type == dfloat64_type_node
3418 || type == dfloat128_type_node
3419 || valtype == dfloat32_type_node
3420 || valtype == dfloat64_type_node
3421 || valtype == dfloat128_type_node)
3422 && (formal_prec
3423 <= TYPE_PRECISION (valtype)
3424 || (type == dfloat128_type_node
3425 && (valtype
3426 != dfloat64_type_node
3427 && (valtype
3428 != dfloat32_type_node)))
3429 || (type == dfloat64_type_node
3430 && (valtype
3431 != dfloat32_type_node))))
3432 warning_at (ploc, 0,
3433 "passing argument %d of %qE as %qT "
3434 "rather than %qT due to prototype",
3435 argnum, rname, type, valtype);
3438 /* Detect integer changing in width or signedness.
3439 These warnings are only activated with
3440 -Wtraditional-conversion, not with -Wtraditional. */
3441 else if (warn_traditional_conversion
3442 && INTEGRAL_TYPE_P (type)
3443 && INTEGRAL_TYPE_P (valtype))
3445 tree would_have_been = default_conversion (val);
3446 tree type1 = TREE_TYPE (would_have_been);
3448 if (val == error_mark_node)
3449 /* VAL could have been of incomplete type. */;
3450 else if (TREE_CODE (type) == ENUMERAL_TYPE
3451 && (TYPE_MAIN_VARIANT (type)
3452 == TYPE_MAIN_VARIANT (valtype)))
3453 /* No warning if function asks for enum
3454 and the actual arg is that enum type. */
3456 else if (formal_prec != TYPE_PRECISION (type1))
3457 warning_at (ploc, OPT_Wtraditional_conversion,
3458 "passing argument %d of %qE "
3459 "with different width due to prototype",
3460 argnum, rname);
3461 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3463 /* Don't complain if the formal parameter type
3464 is an enum, because we can't tell now whether
3465 the value was an enum--even the same enum. */
3466 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3468 else if (TREE_CODE (val) == INTEGER_CST
3469 && int_fits_type_p (val, type))
3470 /* Change in signedness doesn't matter
3471 if a constant value is unaffected. */
3473 /* If the value is extended from a narrower
3474 unsigned type, it doesn't matter whether we
3475 pass it as signed or unsigned; the value
3476 certainly is the same either way. */
3477 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3478 && TYPE_UNSIGNED (valtype))
3480 else if (TYPE_UNSIGNED (type))
3481 warning_at (ploc, OPT_Wtraditional_conversion,
3482 "passing argument %d of %qE "
3483 "as unsigned due to prototype",
3484 argnum, rname);
3485 else
3486 warning_at (ploc, OPT_Wtraditional_conversion,
3487 "passing argument %d of %qE "
3488 "as signed due to prototype",
3489 argnum, rname);
3493 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3494 sake of better warnings from convert_and_check. */
3495 if (excess_precision)
3496 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3497 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3498 parmval = convert_for_assignment (loc, ploc, type,
3499 val, origtype, ic_argpass,
3500 npc, fundecl, function,
3501 parmnum + 1);
3503 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3504 && INTEGRAL_TYPE_P (type)
3505 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3506 parmval = default_conversion (parmval);
3509 else if (promote_float_arg)
3511 if (type_generic)
3512 parmval = val;
3513 else
3515 /* Convert `float' to `double'. */
3516 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3517 warning_at (ploc, OPT_Wdouble_promotion,
3518 "implicit conversion from %qT to %qT when passing "
3519 "argument to function",
3520 valtype, double_type_node);
3521 parmval = convert (double_type_node, val);
3524 else if ((excess_precision && !type_generic)
3525 || (type_generic_overflow_p && parmnum == 2))
3526 /* A "double" argument with excess precision being passed
3527 without a prototype or in variable arguments.
3528 The last argument of __builtin_*_overflow_p should not be
3529 promoted. */
3530 parmval = convert (valtype, val);
3531 else if ((invalid_func_diag =
3532 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3534 error (invalid_func_diag);
3535 return -1;
3537 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3539 return -1;
3541 else
3542 /* Convert `short' and `char' to full-size `int'. */
3543 parmval = default_conversion (val);
3545 (*values)[parmnum] = parmval;
3546 if (parmval == error_mark_node)
3547 error_args = true;
3549 if (typetail)
3550 typetail = TREE_CHAIN (typetail);
3553 gcc_assert (parmnum == vec_safe_length (values));
3555 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3557 error_at (loc, "too few arguments to function %qE", function);
3558 inform_declaration (fundecl);
3559 return -1;
3562 return error_args ? -1 : (int) parmnum;
3565 /* This is the entry point used by the parser to build unary operators
3566 in the input. CODE, a tree_code, specifies the unary operator, and
3567 ARG is the operand. For unary plus, the C parser currently uses
3568 CONVERT_EXPR for code.
3570 LOC is the location to use for the tree generated.
3573 struct c_expr
3574 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3576 struct c_expr result;
3578 result.original_code = code;
3579 result.original_type = NULL;
3581 if (reject_gcc_builtin (arg.value))
3583 result.value = error_mark_node;
3585 else
3587 result.value = build_unary_op (loc, code, arg.value, false);
3589 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3590 overflow_warning (loc, result.value);
3593 /* We are typically called when parsing a prefix token at LOC acting on
3594 ARG. Reflect this by updating the source range of the result to
3595 start at LOC and end at the end of ARG. */
3596 set_c_expr_source_range (&result,
3597 loc, arg.get_finish ());
3599 return result;
3602 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3604 static bool
3605 char_type_p (tree type)
3607 return (type == char_type_node
3608 || type == unsigned_char_type_node
3609 || type == signed_char_type_node
3610 || type == char16_type_node
3611 || type == char32_type_node);
3614 /* This is the entry point used by the parser to build binary operators
3615 in the input. CODE, a tree_code, specifies the binary operator, and
3616 ARG1 and ARG2 are the operands. In addition to constructing the
3617 expression, we check for operands that were written with other binary
3618 operators in a way that is likely to confuse the user.
3620 LOCATION is the location of the binary operator. */
3622 struct c_expr
3623 parser_build_binary_op (location_t location, enum tree_code code,
3624 struct c_expr arg1, struct c_expr arg2)
3626 struct c_expr result;
3628 enum tree_code code1 = arg1.original_code;
3629 enum tree_code code2 = arg2.original_code;
3630 tree type1 = (arg1.original_type
3631 ? arg1.original_type
3632 : TREE_TYPE (arg1.value));
3633 tree type2 = (arg2.original_type
3634 ? arg2.original_type
3635 : TREE_TYPE (arg2.value));
3637 result.value = build_binary_op (location, code,
3638 arg1.value, arg2.value, 1);
3639 result.original_code = code;
3640 result.original_type = NULL;
3642 if (TREE_CODE (result.value) == ERROR_MARK)
3644 set_c_expr_source_range (&result,
3645 arg1.get_start (),
3646 arg2.get_finish ());
3647 return result;
3650 if (location != UNKNOWN_LOCATION)
3651 protected_set_expr_location (result.value, location);
3653 set_c_expr_source_range (&result,
3654 arg1.get_start (),
3655 arg2.get_finish ());
3657 /* Check for cases such as x+y<<z which users are likely
3658 to misinterpret. */
3659 if (warn_parentheses)
3660 warn_about_parentheses (location, code, code1, arg1.value, code2,
3661 arg2.value);
3663 if (warn_logical_op)
3664 warn_logical_operator (location, code, TREE_TYPE (result.value),
3665 code1, arg1.value, code2, arg2.value);
3667 if (warn_tautological_compare)
3669 tree lhs = arg1.value;
3670 tree rhs = arg2.value;
3671 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3673 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3674 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3675 lhs = NULL_TREE;
3676 else
3677 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3679 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3681 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3682 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3683 rhs = NULL_TREE;
3684 else
3685 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3687 if (lhs != NULL_TREE && rhs != NULL_TREE)
3688 warn_tautological_cmp (location, code, lhs, rhs);
3691 if (warn_logical_not_paren
3692 && TREE_CODE_CLASS (code) == tcc_comparison
3693 && code1 == TRUTH_NOT_EXPR
3694 && code2 != TRUTH_NOT_EXPR
3695 /* Avoid warning for !!x == y. */
3696 && (TREE_CODE (arg1.value) != NE_EXPR
3697 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3699 /* Avoid warning for !b == y where b has _Bool type. */
3700 tree t = integer_zero_node;
3701 if (TREE_CODE (arg1.value) == EQ_EXPR
3702 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3703 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3705 t = TREE_OPERAND (arg1.value, 0);
3708 if (TREE_TYPE (t) != integer_type_node)
3709 break;
3710 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3711 t = C_MAYBE_CONST_EXPR_EXPR (t);
3712 else if (CONVERT_EXPR_P (t))
3713 t = TREE_OPERAND (t, 0);
3714 else
3715 break;
3717 while (1);
3719 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3720 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3723 /* Warn about comparisons against string literals, with the exception
3724 of testing for equality or inequality of a string literal with NULL. */
3725 if (code == EQ_EXPR || code == NE_EXPR)
3727 if ((code1 == STRING_CST
3728 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3729 || (code2 == STRING_CST
3730 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3731 warning_at (location, OPT_Waddress,
3732 "comparison with string literal results in unspecified behavior");
3733 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3734 if (POINTER_TYPE_P (type1)
3735 && null_pointer_constant_p (arg2.value)
3736 && char_type_p (type2)
3737 && warning_at (location, OPT_Wpointer_compare,
3738 "comparison between pointer and zero character "
3739 "constant"))
3740 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3741 else if (POINTER_TYPE_P (type2)
3742 && null_pointer_constant_p (arg1.value)
3743 && char_type_p (type1)
3744 && warning_at (location, OPT_Wpointer_compare,
3745 "comparison between pointer and zero character "
3746 "constant"))
3747 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3749 else if (TREE_CODE_CLASS (code) == tcc_comparison
3750 && (code1 == STRING_CST || code2 == STRING_CST))
3751 warning_at (location, OPT_Waddress,
3752 "comparison with string literal results in unspecified behavior");
3754 if (TREE_OVERFLOW_P (result.value)
3755 && !TREE_OVERFLOW_P (arg1.value)
3756 && !TREE_OVERFLOW_P (arg2.value))
3757 overflow_warning (location, result.value);
3759 /* Warn about comparisons of different enum types. */
3760 if (warn_enum_compare
3761 && TREE_CODE_CLASS (code) == tcc_comparison
3762 && TREE_CODE (type1) == ENUMERAL_TYPE
3763 && TREE_CODE (type2) == ENUMERAL_TYPE
3764 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3765 warning_at (location, OPT_Wenum_compare,
3766 "comparison between %qT and %qT",
3767 type1, type2);
3769 return result;
3772 /* Return a tree for the difference of pointers OP0 and OP1.
3773 The resulting tree has type int. */
3775 static tree
3776 pointer_diff (location_t loc, tree op0, tree op1)
3778 tree restype = ptrdiff_type_node;
3779 tree result, inttype;
3781 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3782 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3783 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3784 tree orig_op1 = op1;
3786 /* If the operands point into different address spaces, we need to
3787 explicitly convert them to pointers into the common address space
3788 before we can subtract the numerical address values. */
3789 if (as0 != as1)
3791 addr_space_t as_common;
3792 tree common_type;
3794 /* Determine the common superset address space. This is guaranteed
3795 to exist because the caller verified that comp_target_types
3796 returned non-zero. */
3797 if (!addr_space_superset (as0, as1, &as_common))
3798 gcc_unreachable ();
3800 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3801 op0 = convert (common_type, op0);
3802 op1 = convert (common_type, op1);
3805 /* Determine integer type to perform computations in. This will usually
3806 be the same as the result type (ptrdiff_t), but may need to be a wider
3807 type if pointers for the address space are wider than ptrdiff_t. */
3808 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3809 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3810 else
3811 inttype = restype;
3813 if (TREE_CODE (target_type) == VOID_TYPE)
3814 pedwarn (loc, OPT_Wpointer_arith,
3815 "pointer of type %<void *%> used in subtraction");
3816 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3817 pedwarn (loc, OPT_Wpointer_arith,
3818 "pointer to a function used in subtraction");
3820 /* First do the subtraction as integers;
3821 then drop through to build the divide operator.
3822 Do not do default conversions on the minus operator
3823 in case restype is a short type. */
3825 op0 = build_binary_op (loc,
3826 MINUS_EXPR, convert (inttype, op0),
3827 convert (inttype, op1), 0);
3828 /* This generates an error if op1 is pointer to incomplete type. */
3829 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3830 error_at (loc, "arithmetic on pointer to an incomplete type");
3832 op1 = c_size_in_bytes (target_type);
3834 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3835 error_at (loc, "arithmetic on pointer to an empty aggregate");
3837 /* Divide by the size, in easiest possible way. */
3838 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3839 op0, convert (inttype, op1));
3841 /* Convert to final result type if necessary. */
3842 return convert (restype, result);
3845 /* Expand atomic compound assignments into an appropriate sequence as
3846 specified by the C11 standard section 6.5.16.2.
3848 _Atomic T1 E1
3849 T2 E2
3850 E1 op= E2
3852 This sequence is used for all types for which these operations are
3853 supported.
3855 In addition, built-in versions of the 'fe' prefixed routines may
3856 need to be invoked for floating point (real, complex or vector) when
3857 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3859 T1 newval;
3860 T1 old;
3861 T1 *addr
3862 T2 val
3863 fenv_t fenv
3865 addr = &E1;
3866 val = (E2);
3867 __atomic_load (addr, &old, SEQ_CST);
3868 feholdexcept (&fenv);
3869 loop:
3870 newval = old op val;
3871 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3872 SEQ_CST))
3873 goto done;
3874 feclearexcept (FE_ALL_EXCEPT);
3875 goto loop:
3876 done:
3877 feupdateenv (&fenv);
3879 The compiler will issue the __atomic_fetch_* built-in when possible,
3880 otherwise it will generate the generic form of the atomic operations.
3881 This requires temp(s) and has their address taken. The atomic processing
3882 is smart enough to figure out when the size of an object can utilize
3883 a lock-free version, and convert the built-in call to the appropriate
3884 lock-free routine. The optimizers will then dispose of any temps that
3885 are no longer required, and lock-free implementations are utilized as
3886 long as there is target support for the required size.
3888 If the operator is NOP_EXPR, then this is a simple assignment, and
3889 an __atomic_store is issued to perform the assignment rather than
3890 the above loop. */
3892 /* Build an atomic assignment at LOC, expanding into the proper
3893 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3894 the result of the operation, unless RETURN_OLD_P, in which case
3895 return the old value of LHS (this is only for postincrement and
3896 postdecrement). */
3898 static tree
3899 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3900 tree rhs, bool return_old_p)
3902 tree fndecl, func_call;
3903 vec<tree, va_gc> *params;
3904 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3905 tree old, old_addr;
3906 tree compound_stmt;
3907 tree stmt, goto_stmt;
3908 tree loop_label, loop_decl, done_label, done_decl;
3910 tree lhs_type = TREE_TYPE (lhs);
3911 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3912 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3913 tree rhs_type = TREE_TYPE (rhs);
3915 gcc_assert (TYPE_ATOMIC (lhs_type));
3917 if (return_old_p)
3918 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3920 /* Allocate enough vector items for a compare_exchange. */
3921 vec_alloc (params, 6);
3923 /* Create a compound statement to hold the sequence of statements
3924 with a loop. */
3925 compound_stmt = c_begin_compound_stmt (false);
3927 /* Fold the RHS if it hasn't already been folded. */
3928 if (modifycode != NOP_EXPR)
3929 rhs = c_fully_fold (rhs, false, NULL);
3931 /* Remove the qualifiers for the rest of the expressions and create
3932 the VAL temp variable to hold the RHS. */
3933 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3934 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3935 val = create_tmp_var_raw (nonatomic_rhs_type);
3936 TREE_ADDRESSABLE (val) = 1;
3937 TREE_NO_WARNING (val) = 1;
3938 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3939 NULL_TREE);
3940 SET_EXPR_LOCATION (rhs, loc);
3941 add_stmt (rhs);
3943 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3944 an atomic_store. */
3945 if (modifycode == NOP_EXPR)
3947 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3948 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3949 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3950 params->quick_push (lhs_addr);
3951 params->quick_push (rhs);
3952 params->quick_push (seq_cst);
3953 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3954 add_stmt (func_call);
3956 /* Finish the compound statement. */
3957 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3959 /* VAL is the value which was stored, return a COMPOUND_STMT of
3960 the statement and that value. */
3961 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3964 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3965 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3966 isn't applicable for such builtins. ??? Do we want to handle enums? */
3967 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3968 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3970 built_in_function fncode;
3971 switch (modifycode)
3973 case PLUS_EXPR:
3974 case POINTER_PLUS_EXPR:
3975 fncode = (return_old_p
3976 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3977 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3978 break;
3979 case MINUS_EXPR:
3980 fncode = (return_old_p
3981 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3982 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3983 break;
3984 case BIT_AND_EXPR:
3985 fncode = (return_old_p
3986 ? BUILT_IN_ATOMIC_FETCH_AND_N
3987 : BUILT_IN_ATOMIC_AND_FETCH_N);
3988 break;
3989 case BIT_IOR_EXPR:
3990 fncode = (return_old_p
3991 ? BUILT_IN_ATOMIC_FETCH_OR_N
3992 : BUILT_IN_ATOMIC_OR_FETCH_N);
3993 break;
3994 case BIT_XOR_EXPR:
3995 fncode = (return_old_p
3996 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3997 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3998 break;
3999 default:
4000 goto cas_loop;
4003 /* We can only use "_1" through "_16" variants of the atomic fetch
4004 built-ins. */
4005 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4006 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4007 goto cas_loop;
4009 /* If this is a pointer type, we need to multiply by the size of
4010 the pointer target type. */
4011 if (POINTER_TYPE_P (lhs_type))
4013 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4014 /* ??? This would introduce -Wdiscarded-qualifiers
4015 warning: __atomic_fetch_* expect volatile void *
4016 type as the first argument. (Assignments between
4017 atomic and non-atomic objects are OK.) */
4018 || TYPE_RESTRICT (lhs_type))
4019 goto cas_loop;
4020 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4021 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4022 convert (ptrdiff_type_node, rhs),
4023 convert (ptrdiff_type_node, sz));
4026 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4027 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4028 fndecl = builtin_decl_explicit (fncode);
4029 params->quick_push (lhs_addr);
4030 params->quick_push (rhs);
4031 params->quick_push (seq_cst);
4032 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4034 newval = create_tmp_var_raw (nonatomic_lhs_type);
4035 TREE_ADDRESSABLE (newval) = 1;
4036 TREE_NO_WARNING (newval) = 1;
4037 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4038 NULL_TREE, NULL_TREE);
4039 SET_EXPR_LOCATION (rhs, loc);
4040 add_stmt (rhs);
4042 /* Finish the compound statement. */
4043 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4045 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4046 the statement and that value. */
4047 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4050 cas_loop:
4051 /* Create the variables and labels required for the op= form. */
4052 old = create_tmp_var_raw (nonatomic_lhs_type);
4053 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4054 TREE_ADDRESSABLE (old) = 1;
4055 TREE_NO_WARNING (old) = 1;
4057 newval = create_tmp_var_raw (nonatomic_lhs_type);
4058 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4059 TREE_ADDRESSABLE (newval) = 1;
4060 TREE_NO_WARNING (newval) = 1;
4062 loop_decl = create_artificial_label (loc);
4063 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4065 done_decl = create_artificial_label (loc);
4066 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4068 /* __atomic_load (addr, &old, SEQ_CST). */
4069 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4070 params->quick_push (lhs_addr);
4071 params->quick_push (old_addr);
4072 params->quick_push (seq_cst);
4073 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4074 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4075 NULL_TREE);
4076 add_stmt (old);
4077 params->truncate (0);
4079 /* Create the expressions for floating-point environment
4080 manipulation, if required. */
4081 bool need_fenv = (flag_trapping_math
4082 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4083 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4084 if (need_fenv)
4085 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4087 if (hold_call)
4088 add_stmt (hold_call);
4090 /* loop: */
4091 add_stmt (loop_label);
4093 /* newval = old + val; */
4094 rhs = build_binary_op (loc, modifycode, old, val, 1);
4095 rhs = c_fully_fold (rhs, false, NULL);
4096 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4097 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4098 NULL_TREE, 0);
4099 if (rhs != error_mark_node)
4101 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4102 NULL_TREE);
4103 SET_EXPR_LOCATION (rhs, loc);
4104 add_stmt (rhs);
4107 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4108 goto done; */
4109 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4110 params->quick_push (lhs_addr);
4111 params->quick_push (old_addr);
4112 params->quick_push (newval_addr);
4113 params->quick_push (integer_zero_node);
4114 params->quick_push (seq_cst);
4115 params->quick_push (seq_cst);
4116 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4118 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4119 SET_EXPR_LOCATION (goto_stmt, loc);
4121 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4122 SET_EXPR_LOCATION (stmt, loc);
4123 add_stmt (stmt);
4125 if (clear_call)
4126 add_stmt (clear_call);
4128 /* goto loop; */
4129 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4130 SET_EXPR_LOCATION (goto_stmt, loc);
4131 add_stmt (goto_stmt);
4133 /* done: */
4134 add_stmt (done_label);
4136 if (update_call)
4137 add_stmt (update_call);
4139 /* Finish the compound statement. */
4140 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4142 /* NEWVAL is the value that was successfully stored, return a
4143 COMPOUND_EXPR of the statement and the appropriate value. */
4144 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4145 return_old_p ? old : newval);
4148 /* Construct and perhaps optimize a tree representation
4149 for a unary operation. CODE, a tree_code, specifies the operation
4150 and XARG is the operand.
4151 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4152 promotions (such as from short to int).
4153 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4154 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4155 to pointers in C99.
4157 LOCATION is the location of the operator. */
4159 tree
4160 build_unary_op (location_t location, enum tree_code code, tree xarg,
4161 bool noconvert)
4163 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4164 tree arg = xarg;
4165 tree argtype = 0;
4166 enum tree_code typecode;
4167 tree val;
4168 tree ret = error_mark_node;
4169 tree eptype = NULL_TREE;
4170 const char *invalid_op_diag;
4171 bool int_operands;
4173 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4174 if (int_operands)
4175 arg = remove_c_maybe_const_expr (arg);
4177 if (code != ADDR_EXPR)
4178 arg = require_complete_type (location, arg);
4180 typecode = TREE_CODE (TREE_TYPE (arg));
4181 if (typecode == ERROR_MARK)
4182 return error_mark_node;
4183 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4184 typecode = INTEGER_TYPE;
4186 if ((invalid_op_diag
4187 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4189 error_at (location, invalid_op_diag);
4190 return error_mark_node;
4193 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4195 eptype = TREE_TYPE (arg);
4196 arg = TREE_OPERAND (arg, 0);
4199 switch (code)
4201 case CONVERT_EXPR:
4202 /* This is used for unary plus, because a CONVERT_EXPR
4203 is enough to prevent anybody from looking inside for
4204 associativity, but won't generate any code. */
4205 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4206 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4207 || typecode == VECTOR_TYPE))
4209 error_at (location, "wrong type argument to unary plus");
4210 return error_mark_node;
4212 else if (!noconvert)
4213 arg = default_conversion (arg);
4214 arg = non_lvalue_loc (location, arg);
4215 break;
4217 case NEGATE_EXPR:
4218 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4219 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4220 || typecode == VECTOR_TYPE))
4222 error_at (location, "wrong type argument to unary minus");
4223 return error_mark_node;
4225 else if (!noconvert)
4226 arg = default_conversion (arg);
4227 break;
4229 case BIT_NOT_EXPR:
4230 /* ~ works on integer types and non float vectors. */
4231 if (typecode == INTEGER_TYPE
4232 || (typecode == VECTOR_TYPE
4233 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4235 tree e = arg;
4237 /* Warn if the expression has boolean value. */
4238 while (TREE_CODE (e) == COMPOUND_EXPR)
4239 e = TREE_OPERAND (e, 1);
4241 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4242 || truth_value_p (TREE_CODE (e)))
4243 && warning_at (location, OPT_Wbool_operation,
4244 "%<~%> on a boolean expression"))
4246 gcc_rich_location richloc (location);
4247 richloc.add_fixit_insert_before (location, "!");
4248 inform_at_rich_loc (&richloc, "did you mean to use logical "
4249 "not?");
4251 if (!noconvert)
4252 arg = default_conversion (arg);
4254 else if (typecode == COMPLEX_TYPE)
4256 code = CONJ_EXPR;
4257 pedwarn (location, OPT_Wpedantic,
4258 "ISO C does not support %<~%> for complex conjugation");
4259 if (!noconvert)
4260 arg = default_conversion (arg);
4262 else
4264 error_at (location, "wrong type argument to bit-complement");
4265 return error_mark_node;
4267 break;
4269 case ABS_EXPR:
4270 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4272 error_at (location, "wrong type argument to abs");
4273 return error_mark_node;
4275 else if (!noconvert)
4276 arg = default_conversion (arg);
4277 break;
4279 case CONJ_EXPR:
4280 /* Conjugating a real value is a no-op, but allow it anyway. */
4281 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4282 || typecode == COMPLEX_TYPE))
4284 error_at (location, "wrong type argument to conjugation");
4285 return error_mark_node;
4287 else if (!noconvert)
4288 arg = default_conversion (arg);
4289 break;
4291 case TRUTH_NOT_EXPR:
4292 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4293 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4294 && typecode != COMPLEX_TYPE)
4296 error_at (location,
4297 "wrong type argument to unary exclamation mark");
4298 return error_mark_node;
4300 if (int_operands)
4302 arg = c_objc_common_truthvalue_conversion (location, xarg);
4303 arg = remove_c_maybe_const_expr (arg);
4305 else
4306 arg = c_objc_common_truthvalue_conversion (location, arg);
4307 ret = invert_truthvalue_loc (location, arg);
4308 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4309 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4310 location = EXPR_LOCATION (ret);
4311 goto return_build_unary_op;
4313 case REALPART_EXPR:
4314 case IMAGPART_EXPR:
4315 ret = build_real_imag_expr (location, code, arg);
4316 if (ret == error_mark_node)
4317 return error_mark_node;
4318 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4319 eptype = TREE_TYPE (eptype);
4320 goto return_build_unary_op;
4322 case PREINCREMENT_EXPR:
4323 case POSTINCREMENT_EXPR:
4324 case PREDECREMENT_EXPR:
4325 case POSTDECREMENT_EXPR:
4327 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4329 tree inner = build_unary_op (location, code,
4330 C_MAYBE_CONST_EXPR_EXPR (arg),
4331 noconvert);
4332 if (inner == error_mark_node)
4333 return error_mark_node;
4334 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4335 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4336 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4337 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4338 goto return_build_unary_op;
4341 /* Complain about anything that is not a true lvalue. In
4342 Objective-C, skip this check for property_refs. */
4343 if (!objc_is_property_ref (arg)
4344 && !lvalue_or_else (location,
4345 arg, ((code == PREINCREMENT_EXPR
4346 || code == POSTINCREMENT_EXPR)
4347 ? lv_increment
4348 : lv_decrement)))
4349 return error_mark_node;
4351 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4353 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4354 warning_at (location, OPT_Wc___compat,
4355 "increment of enumeration value is invalid in C++");
4356 else
4357 warning_at (location, OPT_Wc___compat,
4358 "decrement of enumeration value is invalid in C++");
4361 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4363 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4364 warning_at (location, OPT_Wbool_operation,
4365 "increment of a boolean expression");
4366 else
4367 warning_at (location, OPT_Wbool_operation,
4368 "decrement of a boolean expression");
4371 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4372 arg = c_fully_fold (arg, false, NULL);
4374 bool atomic_op;
4375 atomic_op = really_atomic_lvalue (arg);
4377 /* Increment or decrement the real part of the value,
4378 and don't change the imaginary part. */
4379 if (typecode == COMPLEX_TYPE)
4381 tree real, imag;
4383 pedwarn (location, OPT_Wpedantic,
4384 "ISO C does not support %<++%> and %<--%> on complex types");
4386 if (!atomic_op)
4388 arg = stabilize_reference (arg);
4389 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4390 true);
4391 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4392 true);
4393 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4394 if (real == error_mark_node || imag == error_mark_node)
4395 return error_mark_node;
4396 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4397 real, imag);
4398 goto return_build_unary_op;
4402 /* Report invalid types. */
4404 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4405 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4406 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4408 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4409 error_at (location, "wrong type argument to increment");
4410 else
4411 error_at (location, "wrong type argument to decrement");
4413 return error_mark_node;
4417 tree inc;
4419 argtype = TREE_TYPE (arg);
4421 /* Compute the increment. */
4423 if (typecode == POINTER_TYPE)
4425 /* If pointer target is an incomplete type,
4426 we just cannot know how to do the arithmetic. */
4427 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4429 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4430 error_at (location,
4431 "increment of pointer to an incomplete type %qT",
4432 TREE_TYPE (argtype));
4433 else
4434 error_at (location,
4435 "decrement of pointer to an incomplete type %qT",
4436 TREE_TYPE (argtype));
4438 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4439 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4441 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4442 pedwarn (location, OPT_Wpointer_arith,
4443 "wrong type argument to increment");
4444 else
4445 pedwarn (location, OPT_Wpointer_arith,
4446 "wrong type argument to decrement");
4449 inc = c_size_in_bytes (TREE_TYPE (argtype));
4450 inc = convert_to_ptrofftype_loc (location, inc);
4452 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4454 /* For signed fract types, we invert ++ to -- or
4455 -- to ++, and change inc from 1 to -1, because
4456 it is not possible to represent 1 in signed fract constants.
4457 For unsigned fract types, the result always overflows and
4458 we get an undefined (original) or the maximum value. */
4459 if (code == PREINCREMENT_EXPR)
4460 code = PREDECREMENT_EXPR;
4461 else if (code == PREDECREMENT_EXPR)
4462 code = PREINCREMENT_EXPR;
4463 else if (code == POSTINCREMENT_EXPR)
4464 code = POSTDECREMENT_EXPR;
4465 else /* code == POSTDECREMENT_EXPR */
4466 code = POSTINCREMENT_EXPR;
4468 inc = integer_minus_one_node;
4469 inc = convert (argtype, inc);
4471 else
4473 inc = VECTOR_TYPE_P (argtype)
4474 ? build_one_cst (argtype)
4475 : integer_one_node;
4476 inc = convert (argtype, inc);
4479 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4480 need to ask Objective-C to build the increment or decrement
4481 expression for it. */
4482 if (objc_is_property_ref (arg))
4483 return objc_build_incr_expr_for_property_ref (location, code,
4484 arg, inc);
4486 /* Report a read-only lvalue. */
4487 if (TYPE_READONLY (argtype))
4489 readonly_error (location, arg,
4490 ((code == PREINCREMENT_EXPR
4491 || code == POSTINCREMENT_EXPR)
4492 ? lv_increment : lv_decrement));
4493 return error_mark_node;
4495 else if (TREE_READONLY (arg))
4496 readonly_warning (arg,
4497 ((code == PREINCREMENT_EXPR
4498 || code == POSTINCREMENT_EXPR)
4499 ? lv_increment : lv_decrement));
4501 /* If the argument is atomic, use the special code sequences for
4502 atomic compound assignment. */
4503 if (atomic_op)
4505 arg = stabilize_reference (arg);
4506 ret = build_atomic_assign (location, arg,
4507 ((code == PREINCREMENT_EXPR
4508 || code == POSTINCREMENT_EXPR)
4509 ? PLUS_EXPR
4510 : MINUS_EXPR),
4511 (FRACT_MODE_P (TYPE_MODE (argtype))
4512 ? inc
4513 : integer_one_node),
4514 (code == POSTINCREMENT_EXPR
4515 || code == POSTDECREMENT_EXPR));
4516 goto return_build_unary_op;
4519 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4520 val = boolean_increment (code, arg);
4521 else
4522 val = build2 (code, TREE_TYPE (arg), arg, inc);
4523 TREE_SIDE_EFFECTS (val) = 1;
4524 if (TREE_CODE (val) != code)
4525 TREE_NO_WARNING (val) = 1;
4526 ret = val;
4527 goto return_build_unary_op;
4530 case ADDR_EXPR:
4531 /* Note that this operation never does default_conversion. */
4533 /* The operand of unary '&' must be an lvalue (which excludes
4534 expressions of type void), or, in C99, the result of a [] or
4535 unary '*' operator. */
4536 if (VOID_TYPE_P (TREE_TYPE (arg))
4537 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4538 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4539 pedwarn (location, 0, "taking address of expression of type %<void%>");
4541 /* Let &* cancel out to simplify resulting code. */
4542 if (INDIRECT_REF_P (arg))
4544 /* Don't let this be an lvalue. */
4545 if (lvalue_p (TREE_OPERAND (arg, 0)))
4546 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4547 ret = TREE_OPERAND (arg, 0);
4548 goto return_build_unary_op;
4551 /* Anything not already handled and not a true memory reference
4552 or a non-lvalue array is an error. */
4553 if (typecode != FUNCTION_TYPE && !noconvert
4554 && !lvalue_or_else (location, arg, lv_addressof))
4555 return error_mark_node;
4557 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4558 folding later. */
4559 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4561 tree inner = build_unary_op (location, code,
4562 C_MAYBE_CONST_EXPR_EXPR (arg),
4563 noconvert);
4564 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4565 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4566 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4567 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4568 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4569 goto return_build_unary_op;
4572 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4573 argtype = TREE_TYPE (arg);
4575 /* If the lvalue is const or volatile, merge that into the type
4576 to which the address will point. This is only needed
4577 for function types. */
4578 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4579 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4580 && TREE_CODE (argtype) == FUNCTION_TYPE)
4582 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4583 int quals = orig_quals;
4585 if (TREE_READONLY (arg))
4586 quals |= TYPE_QUAL_CONST;
4587 if (TREE_THIS_VOLATILE (arg))
4588 quals |= TYPE_QUAL_VOLATILE;
4590 argtype = c_build_qualified_type (argtype, quals);
4593 switch (TREE_CODE (arg))
4595 case COMPONENT_REF:
4596 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4598 error_at (location, "cannot take address of bit-field %qD",
4599 TREE_OPERAND (arg, 1));
4600 return error_mark_node;
4603 /* fall through */
4605 case ARRAY_REF:
4606 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4608 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4609 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4611 error_at (location, "cannot take address of scalar with "
4612 "reverse storage order");
4613 return error_mark_node;
4616 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4617 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4618 warning_at (location, OPT_Wscalar_storage_order,
4619 "address of array with reverse scalar storage "
4620 "order requested");
4623 default:
4624 break;
4627 if (!c_mark_addressable (arg))
4628 return error_mark_node;
4630 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4631 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4633 argtype = build_pointer_type (argtype);
4635 /* ??? Cope with user tricks that amount to offsetof. Delete this
4636 when we have proper support for integer constant expressions. */
4637 val = get_base_address (arg);
4638 if (val && INDIRECT_REF_P (val)
4639 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4641 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4642 goto return_build_unary_op;
4645 val = build1 (ADDR_EXPR, argtype, arg);
4647 ret = val;
4648 goto return_build_unary_op;
4650 default:
4651 gcc_unreachable ();
4654 if (argtype == 0)
4655 argtype = TREE_TYPE (arg);
4656 if (TREE_CODE (arg) == INTEGER_CST)
4657 ret = (require_constant_value
4658 ? fold_build1_initializer_loc (location, code, argtype, arg)
4659 : fold_build1_loc (location, code, argtype, arg));
4660 else
4661 ret = build1 (code, argtype, arg);
4662 return_build_unary_op:
4663 gcc_assert (ret != error_mark_node);
4664 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4665 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4666 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4667 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4668 ret = note_integer_operands (ret);
4669 if (eptype)
4670 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4671 protected_set_expr_location (ret, location);
4672 return ret;
4675 /* Return nonzero if REF is an lvalue valid for this language.
4676 Lvalues can be assigned, unless their type has TYPE_READONLY.
4677 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4679 bool
4680 lvalue_p (const_tree ref)
4682 const enum tree_code code = TREE_CODE (ref);
4684 switch (code)
4686 case REALPART_EXPR:
4687 case IMAGPART_EXPR:
4688 case COMPONENT_REF:
4689 return lvalue_p (TREE_OPERAND (ref, 0));
4691 case C_MAYBE_CONST_EXPR:
4692 return lvalue_p (TREE_OPERAND (ref, 1));
4694 case COMPOUND_LITERAL_EXPR:
4695 case STRING_CST:
4696 return true;
4698 case INDIRECT_REF:
4699 case ARRAY_REF:
4700 case ARRAY_NOTATION_REF:
4701 case VAR_DECL:
4702 case PARM_DECL:
4703 case RESULT_DECL:
4704 case ERROR_MARK:
4705 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4706 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4708 case BIND_EXPR:
4709 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4711 default:
4712 return false;
4716 /* Give a warning for storing in something that is read-only in GCC
4717 terms but not const in ISO C terms. */
4719 static void
4720 readonly_warning (tree arg, enum lvalue_use use)
4722 switch (use)
4724 case lv_assign:
4725 warning (0, "assignment of read-only location %qE", arg);
4726 break;
4727 case lv_increment:
4728 warning (0, "increment of read-only location %qE", arg);
4729 break;
4730 case lv_decrement:
4731 warning (0, "decrement of read-only location %qE", arg);
4732 break;
4733 default:
4734 gcc_unreachable ();
4736 return;
4740 /* Return nonzero if REF is an lvalue valid for this language;
4741 otherwise, print an error message and return zero. USE says
4742 how the lvalue is being used and so selects the error message.
4743 LOCATION is the location at which any error should be reported. */
4745 static int
4746 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4748 int win = lvalue_p (ref);
4750 if (!win)
4751 lvalue_error (loc, use);
4753 return win;
4756 /* Mark EXP saying that we need to be able to take the
4757 address of it; it should not be allocated in a register.
4758 Returns true if successful. ARRAY_REF_P is true if this
4759 is for ARRAY_REF construction - in that case we don't want
4760 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4761 it is fine to use ARRAY_REFs for vector subscripts on vector
4762 register variables. */
4764 bool
4765 c_mark_addressable (tree exp, bool array_ref_p)
4767 tree x = exp;
4769 while (1)
4770 switch (TREE_CODE (x))
4772 case VIEW_CONVERT_EXPR:
4773 if (array_ref_p
4774 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4775 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4776 return true;
4777 /* FALLTHRU */
4778 case COMPONENT_REF:
4779 case ADDR_EXPR:
4780 case ARRAY_REF:
4781 case REALPART_EXPR:
4782 case IMAGPART_EXPR:
4783 x = TREE_OPERAND (x, 0);
4784 break;
4786 case COMPOUND_LITERAL_EXPR:
4787 case CONSTRUCTOR:
4788 TREE_ADDRESSABLE (x) = 1;
4789 return true;
4791 case VAR_DECL:
4792 case CONST_DECL:
4793 case PARM_DECL:
4794 case RESULT_DECL:
4795 if (C_DECL_REGISTER (x)
4796 && DECL_NONLOCAL (x))
4798 if (TREE_PUBLIC (x) || is_global_var (x))
4800 error
4801 ("global register variable %qD used in nested function", x);
4802 return false;
4804 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4806 else if (C_DECL_REGISTER (x))
4808 if (TREE_PUBLIC (x) || is_global_var (x))
4809 error ("address of global register variable %qD requested", x);
4810 else
4811 error ("address of register variable %qD requested", x);
4812 return false;
4815 /* FALLTHRU */
4816 case FUNCTION_DECL:
4817 TREE_ADDRESSABLE (x) = 1;
4818 /* FALLTHRU */
4819 default:
4820 return true;
4824 /* Convert EXPR to TYPE, warning about conversion problems with
4825 constants. SEMANTIC_TYPE is the type this conversion would use
4826 without excess precision. If SEMANTIC_TYPE is NULL, this function
4827 is equivalent to convert_and_check. This function is a wrapper that
4828 handles conversions that may be different than
4829 the usual ones because of excess precision. */
4831 static tree
4832 ep_convert_and_check (location_t loc, tree type, tree expr,
4833 tree semantic_type)
4835 if (TREE_TYPE (expr) == type)
4836 return expr;
4838 if (!semantic_type)
4839 return convert_and_check (loc, type, expr);
4841 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4842 && TREE_TYPE (expr) != semantic_type)
4844 /* For integers, we need to check the real conversion, not
4845 the conversion to the excess precision type. */
4846 expr = convert_and_check (loc, semantic_type, expr);
4848 /* Result type is the excess precision type, which should be
4849 large enough, so do not check. */
4850 return convert (type, expr);
4853 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4854 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4855 if folded to an integer constant then the unselected half may
4856 contain arbitrary operations not normally permitted in constant
4857 expressions. Set the location of the expression to LOC. */
4859 tree
4860 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4861 tree op1, tree op1_original_type, tree op2,
4862 tree op2_original_type)
4864 tree type1;
4865 tree type2;
4866 enum tree_code code1;
4867 enum tree_code code2;
4868 tree result_type = NULL;
4869 tree semantic_result_type = NULL;
4870 tree orig_op1 = op1, orig_op2 = op2;
4871 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4872 bool ifexp_int_operands;
4873 tree ret;
4875 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4876 if (op1_int_operands)
4877 op1 = remove_c_maybe_const_expr (op1);
4878 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4879 if (op2_int_operands)
4880 op2 = remove_c_maybe_const_expr (op2);
4881 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4882 if (ifexp_int_operands)
4883 ifexp = remove_c_maybe_const_expr (ifexp);
4885 /* Promote both alternatives. */
4887 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4888 op1 = default_conversion (op1);
4889 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4890 op2 = default_conversion (op2);
4892 if (TREE_CODE (ifexp) == ERROR_MARK
4893 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4894 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4895 return error_mark_node;
4897 type1 = TREE_TYPE (op1);
4898 code1 = TREE_CODE (type1);
4899 type2 = TREE_TYPE (op2);
4900 code2 = TREE_CODE (type2);
4902 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4903 return error_mark_node;
4905 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4906 return error_mark_node;
4908 /* C90 does not permit non-lvalue arrays in conditional expressions.
4909 In C99 they will be pointers by now. */
4910 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4912 error_at (colon_loc, "non-lvalue array in conditional expression");
4913 return error_mark_node;
4916 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4917 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4918 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4919 || code1 == COMPLEX_TYPE)
4920 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4921 || code2 == COMPLEX_TYPE))
4923 semantic_result_type = c_common_type (type1, type2);
4924 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4926 op1 = TREE_OPERAND (op1, 0);
4927 type1 = TREE_TYPE (op1);
4928 gcc_assert (TREE_CODE (type1) == code1);
4930 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4932 op2 = TREE_OPERAND (op2, 0);
4933 type2 = TREE_TYPE (op2);
4934 gcc_assert (TREE_CODE (type2) == code2);
4938 if (warn_cxx_compat)
4940 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4941 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4943 if (TREE_CODE (t1) == ENUMERAL_TYPE
4944 && TREE_CODE (t2) == ENUMERAL_TYPE
4945 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4946 warning_at (colon_loc, OPT_Wc___compat,
4947 ("different enum types in conditional is "
4948 "invalid in C++: %qT vs %qT"),
4949 t1, t2);
4952 /* Quickly detect the usual case where op1 and op2 have the same type
4953 after promotion. */
4954 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4956 if (type1 == type2)
4957 result_type = type1;
4958 else
4959 result_type = TYPE_MAIN_VARIANT (type1);
4961 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4962 || code1 == COMPLEX_TYPE)
4963 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4964 || code2 == COMPLEX_TYPE))
4966 result_type = c_common_type (type1, type2);
4967 if (result_type == error_mark_node)
4968 return error_mark_node;
4969 do_warn_double_promotion (result_type, type1, type2,
4970 "implicit conversion from %qT to %qT to "
4971 "match other result of conditional",
4972 colon_loc);
4974 /* If -Wsign-compare, warn here if type1 and type2 have
4975 different signedness. We'll promote the signed to unsigned
4976 and later code won't know it used to be different.
4977 Do this check on the original types, so that explicit casts
4978 will be considered, but default promotions won't. */
4979 if (c_inhibit_evaluation_warnings == 0)
4981 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4982 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4984 if (unsigned_op1 ^ unsigned_op2)
4986 bool ovf;
4988 /* Do not warn if the result type is signed, since the
4989 signed type will only be chosen if it can represent
4990 all the values of the unsigned type. */
4991 if (!TYPE_UNSIGNED (result_type))
4992 /* OK */;
4993 else
4995 bool op1_maybe_const = true;
4996 bool op2_maybe_const = true;
4998 /* Do not warn if the signed quantity is an
4999 unsuffixed integer literal (or some static
5000 constant expression involving such literals) and
5001 it is non-negative. This warning requires the
5002 operands to be folded for best results, so do
5003 that folding in this case even without
5004 warn_sign_compare to avoid warning options
5005 possibly affecting code generation. */
5006 c_inhibit_evaluation_warnings
5007 += (ifexp == truthvalue_false_node);
5008 op1 = c_fully_fold (op1, require_constant_value,
5009 &op1_maybe_const);
5010 c_inhibit_evaluation_warnings
5011 -= (ifexp == truthvalue_false_node);
5013 c_inhibit_evaluation_warnings
5014 += (ifexp == truthvalue_true_node);
5015 op2 = c_fully_fold (op2, require_constant_value,
5016 &op2_maybe_const);
5017 c_inhibit_evaluation_warnings
5018 -= (ifexp == truthvalue_true_node);
5020 if (warn_sign_compare)
5022 if ((unsigned_op2
5023 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5024 || (unsigned_op1
5025 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5026 /* OK */;
5027 else
5028 warning_at (colon_loc, OPT_Wsign_compare,
5029 ("signed and unsigned type in "
5030 "conditional expression"));
5032 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5033 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5034 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5035 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5040 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5042 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5043 pedwarn (colon_loc, OPT_Wpedantic,
5044 "ISO C forbids conditional expr with only one void side");
5045 result_type = void_type_node;
5047 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5049 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5050 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5051 addr_space_t as_common;
5053 if (comp_target_types (colon_loc, type1, type2))
5054 result_type = common_pointer_type (type1, type2);
5055 else if (null_pointer_constant_p (orig_op1))
5056 result_type = type2;
5057 else if (null_pointer_constant_p (orig_op2))
5058 result_type = type1;
5059 else if (!addr_space_superset (as1, as2, &as_common))
5061 error_at (colon_loc, "pointers to disjoint address spaces "
5062 "used in conditional expression");
5063 return error_mark_node;
5065 else if (VOID_TYPE_P (TREE_TYPE (type1))
5066 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5068 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5069 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5070 & ~TYPE_QUALS (TREE_TYPE (type1))))
5071 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5072 "pointer to array loses qualifier "
5073 "in conditional expression");
5075 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5076 pedwarn (colon_loc, OPT_Wpedantic,
5077 "ISO C forbids conditional expr between "
5078 "%<void *%> and function pointer");
5079 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5080 TREE_TYPE (type2)));
5082 else if (VOID_TYPE_P (TREE_TYPE (type2))
5083 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5085 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5086 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5087 & ~TYPE_QUALS (TREE_TYPE (type2))))
5088 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5089 "pointer to array loses qualifier "
5090 "in conditional expression");
5092 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5093 pedwarn (colon_loc, OPT_Wpedantic,
5094 "ISO C forbids conditional expr between "
5095 "%<void *%> and function pointer");
5096 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5097 TREE_TYPE (type1)));
5099 /* Objective-C pointer comparisons are a bit more lenient. */
5100 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5101 result_type = objc_common_type (type1, type2);
5102 else
5104 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5106 pedwarn (colon_loc, 0,
5107 "pointer type mismatch in conditional expression");
5108 result_type = build_pointer_type
5109 (build_qualified_type (void_type_node, qual));
5112 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5114 if (!null_pointer_constant_p (orig_op2))
5115 pedwarn (colon_loc, 0,
5116 "pointer/integer type mismatch in conditional expression");
5117 else
5119 op2 = null_pointer_node;
5121 result_type = type1;
5123 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5125 if (!null_pointer_constant_p (orig_op1))
5126 pedwarn (colon_loc, 0,
5127 "pointer/integer type mismatch in conditional expression");
5128 else
5130 op1 = null_pointer_node;
5132 result_type = type2;
5135 if (!result_type)
5137 if (flag_cond_mismatch)
5138 result_type = void_type_node;
5139 else
5141 error_at (colon_loc, "type mismatch in conditional expression");
5142 return error_mark_node;
5146 /* Merge const and volatile flags of the incoming types. */
5147 result_type
5148 = build_type_variant (result_type,
5149 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5150 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5152 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5153 semantic_result_type);
5154 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5155 semantic_result_type);
5157 if (ifexp_bcp && ifexp == truthvalue_true_node)
5159 op2_int_operands = true;
5160 op1 = c_fully_fold (op1, require_constant_value, NULL);
5162 if (ifexp_bcp && ifexp == truthvalue_false_node)
5164 op1_int_operands = true;
5165 op2 = c_fully_fold (op2, require_constant_value, NULL);
5167 int_const = int_operands = (ifexp_int_operands
5168 && op1_int_operands
5169 && op2_int_operands);
5170 if (int_operands)
5172 int_const = ((ifexp == truthvalue_true_node
5173 && TREE_CODE (orig_op1) == INTEGER_CST
5174 && !TREE_OVERFLOW (orig_op1))
5175 || (ifexp == truthvalue_false_node
5176 && TREE_CODE (orig_op2) == INTEGER_CST
5177 && !TREE_OVERFLOW (orig_op2)));
5180 /* Need to convert condition operand into a vector mask. */
5181 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5183 tree vectype = TREE_TYPE (ifexp);
5184 tree elem_type = TREE_TYPE (vectype);
5185 tree zero = build_int_cst (elem_type, 0);
5186 tree zero_vec = build_vector_from_val (vectype, zero);
5187 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5188 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5191 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5192 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5193 else
5195 if (int_operands)
5197 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5198 nested inside of the expression. */
5199 op1 = c_fully_fold (op1, false, NULL);
5200 op2 = c_fully_fold (op2, false, NULL);
5202 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5203 if (int_operands)
5204 ret = note_integer_operands (ret);
5206 if (semantic_result_type)
5207 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5209 protected_set_expr_location (ret, colon_loc);
5211 /* If the OP1 and OP2 are the same and don't have side-effects,
5212 warn here, because the COND_EXPR will be turned into OP1. */
5213 if (warn_duplicated_branches
5214 && TREE_CODE (ret) == COND_EXPR
5215 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5216 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5217 "this condition has identical branches");
5219 return ret;
5222 /* Return a compound expression that performs two expressions and
5223 returns the value of the second of them.
5225 LOC is the location of the COMPOUND_EXPR. */
5227 tree
5228 build_compound_expr (location_t loc, tree expr1, tree expr2)
5230 bool expr1_int_operands, expr2_int_operands;
5231 tree eptype = NULL_TREE;
5232 tree ret;
5234 if (flag_cilkplus
5235 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5236 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5238 error_at (loc,
5239 "spawned function call cannot be part of a comma expression");
5240 return error_mark_node;
5242 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5243 if (expr1_int_operands)
5244 expr1 = remove_c_maybe_const_expr (expr1);
5245 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5246 if (expr2_int_operands)
5247 expr2 = remove_c_maybe_const_expr (expr2);
5249 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5250 expr1 = TREE_OPERAND (expr1, 0);
5251 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5253 eptype = TREE_TYPE (expr2);
5254 expr2 = TREE_OPERAND (expr2, 0);
5257 if (!TREE_SIDE_EFFECTS (expr1))
5259 /* The left-hand operand of a comma expression is like an expression
5260 statement: with -Wunused, we should warn if it doesn't have
5261 any side-effects, unless it was explicitly cast to (void). */
5262 if (warn_unused_value)
5264 if (VOID_TYPE_P (TREE_TYPE (expr1))
5265 && CONVERT_EXPR_P (expr1))
5266 ; /* (void) a, b */
5267 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5268 && TREE_CODE (expr1) == COMPOUND_EXPR
5269 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5270 ; /* (void) a, (void) b, c */
5271 else
5272 warning_at (loc, OPT_Wunused_value,
5273 "left-hand operand of comma expression has no effect");
5276 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5277 && warn_unused_value)
5279 tree r = expr1;
5280 location_t cloc = loc;
5281 while (TREE_CODE (r) == COMPOUND_EXPR)
5283 if (EXPR_HAS_LOCATION (r))
5284 cloc = EXPR_LOCATION (r);
5285 r = TREE_OPERAND (r, 1);
5287 if (!TREE_SIDE_EFFECTS (r)
5288 && !VOID_TYPE_P (TREE_TYPE (r))
5289 && !CONVERT_EXPR_P (r))
5290 warning_at (cloc, OPT_Wunused_value,
5291 "right-hand operand of comma expression has no effect");
5294 /* With -Wunused, we should also warn if the left-hand operand does have
5295 side-effects, but computes a value which is not used. For example, in
5296 `foo() + bar(), baz()' the result of the `+' operator is not used,
5297 so we should issue a warning. */
5298 else if (warn_unused_value)
5299 warn_if_unused_value (expr1, loc);
5301 if (expr2 == error_mark_node)
5302 return error_mark_node;
5304 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5306 if (flag_isoc99
5307 && expr1_int_operands
5308 && expr2_int_operands)
5309 ret = note_integer_operands (ret);
5311 if (eptype)
5312 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5314 protected_set_expr_location (ret, loc);
5315 return ret;
5318 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5319 which we are casting. OTYPE is the type of the expression being
5320 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5321 of the cast. -Wcast-qual appeared on the command line. Named
5322 address space qualifiers are not handled here, because they result
5323 in different warnings. */
5325 static void
5326 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5328 tree in_type = type;
5329 tree in_otype = otype;
5330 int added = 0;
5331 int discarded = 0;
5332 bool is_const;
5334 /* Check that the qualifiers on IN_TYPE are a superset of the
5335 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5336 nodes is uninteresting and we stop as soon as we hit a
5337 non-POINTER_TYPE node on either type. */
5340 in_otype = TREE_TYPE (in_otype);
5341 in_type = TREE_TYPE (in_type);
5343 /* GNU C allows cv-qualified function types. 'const' means the
5344 function is very pure, 'volatile' means it can't return. We
5345 need to warn when such qualifiers are added, not when they're
5346 taken away. */
5347 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5348 && TREE_CODE (in_type) == FUNCTION_TYPE)
5349 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5350 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5351 else
5352 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5353 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5355 while (TREE_CODE (in_type) == POINTER_TYPE
5356 && TREE_CODE (in_otype) == POINTER_TYPE);
5358 if (added)
5359 warning_at (loc, OPT_Wcast_qual,
5360 "cast adds %q#v qualifier to function type", added);
5362 if (discarded)
5363 /* There are qualifiers present in IN_OTYPE that are not present
5364 in IN_TYPE. */
5365 warning_at (loc, OPT_Wcast_qual,
5366 "cast discards %qv qualifier from pointer target type",
5367 discarded);
5369 if (added || discarded)
5370 return;
5372 /* A cast from **T to const **T is unsafe, because it can cause a
5373 const value to be changed with no additional warning. We only
5374 issue this warning if T is the same on both sides, and we only
5375 issue the warning if there are the same number of pointers on
5376 both sides, as otherwise the cast is clearly unsafe anyhow. A
5377 cast is unsafe when a qualifier is added at one level and const
5378 is not present at all outer levels.
5380 To issue this warning, we check at each level whether the cast
5381 adds new qualifiers not already seen. We don't need to special
5382 case function types, as they won't have the same
5383 TYPE_MAIN_VARIANT. */
5385 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5386 return;
5387 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5388 return;
5390 in_type = type;
5391 in_otype = otype;
5392 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5395 in_type = TREE_TYPE (in_type);
5396 in_otype = TREE_TYPE (in_otype);
5397 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5398 && !is_const)
5400 warning_at (loc, OPT_Wcast_qual,
5401 "to be safe all intermediate pointers in cast from "
5402 "%qT to %qT must be %<const%> qualified",
5403 otype, type);
5404 break;
5406 if (is_const)
5407 is_const = TYPE_READONLY (in_type);
5409 while (TREE_CODE (in_type) == POINTER_TYPE);
5412 /* Build an expression representing a cast to type TYPE of expression EXPR.
5413 LOC is the location of the cast-- typically the open paren of the cast. */
5415 tree
5416 build_c_cast (location_t loc, tree type, tree expr)
5418 tree value;
5420 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5421 expr = TREE_OPERAND (expr, 0);
5423 value = expr;
5425 if (type == error_mark_node || expr == error_mark_node)
5426 return error_mark_node;
5428 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5429 only in <protocol> qualifications. But when constructing cast expressions,
5430 the protocols do matter and must be kept around. */
5431 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5432 return build1 (NOP_EXPR, type, expr);
5434 type = TYPE_MAIN_VARIANT (type);
5436 if (TREE_CODE (type) == ARRAY_TYPE)
5438 error_at (loc, "cast specifies array type");
5439 return error_mark_node;
5442 if (TREE_CODE (type) == FUNCTION_TYPE)
5444 error_at (loc, "cast specifies function type");
5445 return error_mark_node;
5448 if (!VOID_TYPE_P (type))
5450 value = require_complete_type (loc, value);
5451 if (value == error_mark_node)
5452 return error_mark_node;
5455 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5457 if (RECORD_OR_UNION_TYPE_P (type))
5458 pedwarn (loc, OPT_Wpedantic,
5459 "ISO C forbids casting nonscalar to the same type");
5461 /* Convert to remove any qualifiers from VALUE's type. */
5462 value = convert (type, value);
5464 else if (TREE_CODE (type) == UNION_TYPE)
5466 tree field;
5468 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5469 if (TREE_TYPE (field) != error_mark_node
5470 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5471 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5472 break;
5474 if (field)
5476 tree t;
5477 bool maybe_const = true;
5479 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5480 t = c_fully_fold (value, false, &maybe_const);
5481 t = build_constructor_single (type, field, t);
5482 if (!maybe_const)
5483 t = c_wrap_maybe_const (t, true);
5484 t = digest_init (loc, type, t,
5485 NULL_TREE, false, true, 0);
5486 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5487 return t;
5489 error_at (loc, "cast to union type from type not present in union");
5490 return error_mark_node;
5492 else
5494 tree otype, ovalue;
5496 if (type == void_type_node)
5498 tree t = build1 (CONVERT_EXPR, type, value);
5499 SET_EXPR_LOCATION (t, loc);
5500 return t;
5503 otype = TREE_TYPE (value);
5505 /* Optionally warn about potentially worrisome casts. */
5506 if (warn_cast_qual
5507 && TREE_CODE (type) == POINTER_TYPE
5508 && TREE_CODE (otype) == POINTER_TYPE)
5509 handle_warn_cast_qual (loc, type, otype);
5511 /* Warn about conversions between pointers to disjoint
5512 address spaces. */
5513 if (TREE_CODE (type) == POINTER_TYPE
5514 && TREE_CODE (otype) == POINTER_TYPE
5515 && !null_pointer_constant_p (value))
5517 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5518 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5519 addr_space_t as_common;
5521 if (!addr_space_superset (as_to, as_from, &as_common))
5523 if (ADDR_SPACE_GENERIC_P (as_from))
5524 warning_at (loc, 0, "cast to %s address space pointer "
5525 "from disjoint generic address space pointer",
5526 c_addr_space_name (as_to));
5528 else if (ADDR_SPACE_GENERIC_P (as_to))
5529 warning_at (loc, 0, "cast to generic address space pointer "
5530 "from disjoint %s address space pointer",
5531 c_addr_space_name (as_from));
5533 else
5534 warning_at (loc, 0, "cast to %s address space pointer "
5535 "from disjoint %s address space pointer",
5536 c_addr_space_name (as_to),
5537 c_addr_space_name (as_from));
5541 /* Warn about possible alignment problems. */
5542 if (STRICT_ALIGNMENT
5543 && TREE_CODE (type) == POINTER_TYPE
5544 && TREE_CODE (otype) == POINTER_TYPE
5545 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5546 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5547 /* Don't warn about opaque types, where the actual alignment
5548 restriction is unknown. */
5549 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5550 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5551 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5552 warning_at (loc, OPT_Wcast_align,
5553 "cast increases required alignment of target type");
5555 if (TREE_CODE (type) == INTEGER_TYPE
5556 && TREE_CODE (otype) == POINTER_TYPE
5557 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5558 /* Unlike conversion of integers to pointers, where the
5559 warning is disabled for converting constants because
5560 of cases such as SIG_*, warn about converting constant
5561 pointers to integers. In some cases it may cause unwanted
5562 sign extension, and a warning is appropriate. */
5563 warning_at (loc, OPT_Wpointer_to_int_cast,
5564 "cast from pointer to integer of different size");
5566 if (TREE_CODE (value) == CALL_EXPR
5567 && TREE_CODE (type) != TREE_CODE (otype))
5568 warning_at (loc, OPT_Wbad_function_cast,
5569 "cast from function call of type %qT "
5570 "to non-matching type %qT", otype, type);
5572 if (TREE_CODE (type) == POINTER_TYPE
5573 && TREE_CODE (otype) == INTEGER_TYPE
5574 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5575 /* Don't warn about converting any constant. */
5576 && !TREE_CONSTANT (value))
5577 warning_at (loc,
5578 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5579 "of different size");
5581 if (warn_strict_aliasing <= 2)
5582 strict_aliasing_warning (otype, type, expr);
5584 /* If pedantic, warn for conversions between function and object
5585 pointer types, except for converting a null pointer constant
5586 to function pointer type. */
5587 if (pedantic
5588 && TREE_CODE (type) == POINTER_TYPE
5589 && TREE_CODE (otype) == POINTER_TYPE
5590 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5591 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5592 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5593 "conversion of function pointer to object pointer type");
5595 if (pedantic
5596 && TREE_CODE (type) == POINTER_TYPE
5597 && TREE_CODE (otype) == POINTER_TYPE
5598 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5599 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5600 && !null_pointer_constant_p (value))
5601 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5602 "conversion of object pointer to function pointer type");
5604 ovalue = value;
5605 value = convert (type, value);
5607 /* Ignore any integer overflow caused by the cast. */
5608 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5610 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5612 if (!TREE_OVERFLOW (value))
5614 /* Avoid clobbering a shared constant. */
5615 value = copy_node (value);
5616 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5619 else if (TREE_OVERFLOW (value))
5620 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5621 value = wide_int_to_tree (TREE_TYPE (value), value);
5625 /* Don't let a cast be an lvalue. */
5626 if (lvalue_p (value))
5627 value = non_lvalue_loc (loc, value);
5629 /* Don't allow the results of casting to floating-point or complex
5630 types be confused with actual constants, or casts involving
5631 integer and pointer types other than direct integer-to-integer
5632 and integer-to-pointer be confused with integer constant
5633 expressions and null pointer constants. */
5634 if (TREE_CODE (value) == REAL_CST
5635 || TREE_CODE (value) == COMPLEX_CST
5636 || (TREE_CODE (value) == INTEGER_CST
5637 && !((TREE_CODE (expr) == INTEGER_CST
5638 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5639 || TREE_CODE (expr) == REAL_CST
5640 || TREE_CODE (expr) == COMPLEX_CST)))
5641 value = build1 (NOP_EXPR, type, value);
5643 protected_set_expr_location (value, loc);
5644 return value;
5647 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5648 location of the open paren of the cast, or the position of the cast
5649 expr. */
5650 tree
5651 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5653 tree type;
5654 tree type_expr = NULL_TREE;
5655 bool type_expr_const = true;
5656 tree ret;
5657 int saved_wsp = warn_strict_prototypes;
5659 /* This avoids warnings about unprototyped casts on
5660 integers. E.g. "#define SIG_DFL (void(*)())0". */
5661 if (TREE_CODE (expr) == INTEGER_CST)
5662 warn_strict_prototypes = 0;
5663 type = groktypename (type_name, &type_expr, &type_expr_const);
5664 warn_strict_prototypes = saved_wsp;
5666 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5667 && reject_gcc_builtin (expr))
5668 return error_mark_node;
5670 ret = build_c_cast (loc, type, expr);
5671 if (type_expr)
5673 bool inner_expr_const = true;
5674 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5675 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5676 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5677 && inner_expr_const);
5678 SET_EXPR_LOCATION (ret, loc);
5681 if (!EXPR_HAS_LOCATION (ret))
5682 protected_set_expr_location (ret, loc);
5684 /* C++ does not permits types to be defined in a cast, but it
5685 allows references to incomplete types. */
5686 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5687 warning_at (loc, OPT_Wc___compat,
5688 "defining a type in a cast is invalid in C++");
5690 return ret;
5693 /* Build an assignment expression of lvalue LHS from value RHS.
5694 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5695 may differ from TREE_TYPE (LHS) for an enum bitfield.
5696 MODIFYCODE is the code for a binary operator that we use
5697 to combine the old value of LHS with RHS to get the new value.
5698 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5699 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5700 which may differ from TREE_TYPE (RHS) for an enum value.
5702 LOCATION is the location of the MODIFYCODE operator.
5703 RHS_LOC is the location of the RHS. */
5705 tree
5706 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5707 enum tree_code modifycode,
5708 location_t rhs_loc, tree rhs, tree rhs_origtype)
5710 tree result;
5711 tree newrhs;
5712 tree rhseval = NULL_TREE;
5713 tree rhs_semantic_type = NULL_TREE;
5714 tree lhstype = TREE_TYPE (lhs);
5715 tree olhstype = lhstype;
5716 bool npc;
5717 bool is_atomic_op;
5719 /* Types that aren't fully specified cannot be used in assignments. */
5720 lhs = require_complete_type (location, lhs);
5722 /* Avoid duplicate error messages from operands that had errors. */
5723 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5724 return error_mark_node;
5726 /* Ensure an error for assigning a non-lvalue array to an array in
5727 C90. */
5728 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5730 error_at (location, "assignment to expression with array type");
5731 return error_mark_node;
5734 /* For ObjC properties, defer this check. */
5735 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5736 return error_mark_node;
5738 is_atomic_op = really_atomic_lvalue (lhs);
5740 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5742 rhs_semantic_type = TREE_TYPE (rhs);
5743 rhs = TREE_OPERAND (rhs, 0);
5746 newrhs = rhs;
5748 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5750 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5751 lhs_origtype, modifycode, rhs_loc, rhs,
5752 rhs_origtype);
5753 if (inner == error_mark_node)
5754 return error_mark_node;
5755 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5756 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5757 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5758 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5759 protected_set_expr_location (result, location);
5760 return result;
5763 /* If a binary op has been requested, combine the old LHS value with the RHS
5764 producing the value we should actually store into the LHS. */
5766 if (modifycode != NOP_EXPR)
5768 lhs = c_fully_fold (lhs, false, NULL);
5769 lhs = stabilize_reference (lhs);
5771 /* Construct the RHS for any non-atomic compound assignemnt. */
5772 if (!is_atomic_op)
5774 /* If in LHS op= RHS the RHS has side-effects, ensure they
5775 are preevaluated before the rest of the assignment expression's
5776 side-effects, because RHS could contain e.g. function calls
5777 that modify LHS. */
5778 if (TREE_SIDE_EFFECTS (rhs))
5780 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5781 rhseval = newrhs;
5783 newrhs = build_binary_op (location,
5784 modifycode, lhs, newrhs, 1);
5786 /* The original type of the right hand side is no longer
5787 meaningful. */
5788 rhs_origtype = NULL_TREE;
5792 if (c_dialect_objc ())
5794 /* Check if we are modifying an Objective-C property reference;
5795 if so, we need to generate setter calls. */
5796 result = objc_maybe_build_modify_expr (lhs, newrhs);
5797 if (result)
5798 goto return_result;
5800 /* Else, do the check that we postponed for Objective-C. */
5801 if (!lvalue_or_else (location, lhs, lv_assign))
5802 return error_mark_node;
5805 /* Give an error for storing in something that is 'const'. */
5807 if (TYPE_READONLY (lhstype)
5808 || (RECORD_OR_UNION_TYPE_P (lhstype)
5809 && C_TYPE_FIELDS_READONLY (lhstype)))
5811 readonly_error (location, lhs, lv_assign);
5812 return error_mark_node;
5814 else if (TREE_READONLY (lhs))
5815 readonly_warning (lhs, lv_assign);
5817 /* If storing into a structure or union member,
5818 it has probably been given type `int'.
5819 Compute the type that would go with
5820 the actual amount of storage the member occupies. */
5822 if (TREE_CODE (lhs) == COMPONENT_REF
5823 && (TREE_CODE (lhstype) == INTEGER_TYPE
5824 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5825 || TREE_CODE (lhstype) == REAL_TYPE
5826 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5827 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5829 /* If storing in a field that is in actuality a short or narrower than one,
5830 we must store in the field in its actual type. */
5832 if (lhstype != TREE_TYPE (lhs))
5834 lhs = copy_node (lhs);
5835 TREE_TYPE (lhs) = lhstype;
5838 /* Issue -Wc++-compat warnings about an assignment to an enum type
5839 when LHS does not have its original type. This happens for,
5840 e.g., an enum bitfield in a struct. */
5841 if (warn_cxx_compat
5842 && lhs_origtype != NULL_TREE
5843 && lhs_origtype != lhstype
5844 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5846 tree checktype = (rhs_origtype != NULL_TREE
5847 ? rhs_origtype
5848 : TREE_TYPE (rhs));
5849 if (checktype != error_mark_node
5850 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5851 || (is_atomic_op && modifycode != NOP_EXPR)))
5852 warning_at (location, OPT_Wc___compat,
5853 "enum conversion in assignment is invalid in C++");
5856 /* If the lhs is atomic, remove that qualifier. */
5857 if (is_atomic_op)
5859 lhstype = build_qualified_type (lhstype,
5860 (TYPE_QUALS (lhstype)
5861 & ~TYPE_QUAL_ATOMIC));
5862 olhstype = build_qualified_type (olhstype,
5863 (TYPE_QUALS (lhstype)
5864 & ~TYPE_QUAL_ATOMIC));
5867 /* Convert new value to destination type. Fold it first, then
5868 restore any excess precision information, for the sake of
5869 conversion warnings. */
5871 if (!(is_atomic_op && modifycode != NOP_EXPR))
5873 npc = null_pointer_constant_p (newrhs);
5874 newrhs = c_fully_fold (newrhs, false, NULL);
5875 if (rhs_semantic_type)
5876 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5877 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5878 rhs_origtype, ic_assign, npc,
5879 NULL_TREE, NULL_TREE, 0);
5880 if (TREE_CODE (newrhs) == ERROR_MARK)
5881 return error_mark_node;
5884 /* Emit ObjC write barrier, if necessary. */
5885 if (c_dialect_objc () && flag_objc_gc)
5887 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5888 if (result)
5890 protected_set_expr_location (result, location);
5891 goto return_result;
5895 /* Scan operands. */
5897 if (is_atomic_op)
5898 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5899 else
5901 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5902 TREE_SIDE_EFFECTS (result) = 1;
5903 protected_set_expr_location (result, location);
5906 /* If we got the LHS in a different type for storing in,
5907 convert the result back to the nominal type of LHS
5908 so that the value we return always has the same type
5909 as the LHS argument. */
5911 if (olhstype == TREE_TYPE (result))
5912 goto return_result;
5914 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5915 rhs_origtype, ic_assign, false, NULL_TREE,
5916 NULL_TREE, 0);
5917 protected_set_expr_location (result, location);
5919 return_result:
5920 if (rhseval)
5921 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5922 return result;
5925 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5926 This is used to implement -fplan9-extensions. */
5928 static bool
5929 find_anonymous_field_with_type (tree struct_type, tree type)
5931 tree field;
5932 bool found;
5934 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5935 found = false;
5936 for (field = TYPE_FIELDS (struct_type);
5937 field != NULL_TREE;
5938 field = TREE_CHAIN (field))
5940 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5941 ? c_build_qualified_type (TREE_TYPE (field),
5942 TYPE_QUAL_ATOMIC)
5943 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5944 if (DECL_NAME (field) == NULL
5945 && comptypes (type, fieldtype))
5947 if (found)
5948 return false;
5949 found = true;
5951 else if (DECL_NAME (field) == NULL
5952 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5953 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5955 if (found)
5956 return false;
5957 found = true;
5960 return found;
5963 /* RHS is an expression whose type is pointer to struct. If there is
5964 an anonymous field in RHS with type TYPE, then return a pointer to
5965 that field in RHS. This is used with -fplan9-extensions. This
5966 returns NULL if no conversion could be found. */
5968 static tree
5969 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5971 tree rhs_struct_type, lhs_main_type;
5972 tree field, found_field;
5973 bool found_sub_field;
5974 tree ret;
5976 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5977 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5978 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5980 gcc_assert (POINTER_TYPE_P (type));
5981 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5982 ? c_build_qualified_type (TREE_TYPE (type),
5983 TYPE_QUAL_ATOMIC)
5984 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5986 found_field = NULL_TREE;
5987 found_sub_field = false;
5988 for (field = TYPE_FIELDS (rhs_struct_type);
5989 field != NULL_TREE;
5990 field = TREE_CHAIN (field))
5992 if (DECL_NAME (field) != NULL_TREE
5993 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
5994 continue;
5995 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5996 ? c_build_qualified_type (TREE_TYPE (field),
5997 TYPE_QUAL_ATOMIC)
5998 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5999 if (comptypes (lhs_main_type, fieldtype))
6001 if (found_field != NULL_TREE)
6002 return NULL_TREE;
6003 found_field = field;
6005 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6006 lhs_main_type))
6008 if (found_field != NULL_TREE)
6009 return NULL_TREE;
6010 found_field = field;
6011 found_sub_field = true;
6015 if (found_field == NULL_TREE)
6016 return NULL_TREE;
6018 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6019 build_fold_indirect_ref (rhs), found_field,
6020 NULL_TREE);
6021 ret = build_fold_addr_expr_loc (location, ret);
6023 if (found_sub_field)
6025 ret = convert_to_anonymous_field (location, type, ret);
6026 gcc_assert (ret != NULL_TREE);
6029 return ret;
6032 /* Issue an error message for a bad initializer component.
6033 GMSGID identifies the message.
6034 The component name is taken from the spelling stack. */
6036 static void
6037 error_init (location_t loc, const char *gmsgid)
6039 char *ofwhat;
6041 /* The gmsgid may be a format string with %< and %>. */
6042 error_at (loc, gmsgid);
6043 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6044 if (*ofwhat)
6045 inform (loc, "(near initialization for %qs)", ofwhat);
6048 /* Issue a pedantic warning for a bad initializer component. OPT is
6049 the option OPT_* (from options.h) controlling this warning or 0 if
6050 it is unconditionally given. GMSGID identifies the message. The
6051 component name is taken from the spelling stack. */
6053 static void
6054 pedwarn_init (location_t loc, int opt, const char *gmsgid)
6056 char *ofwhat;
6057 bool warned;
6059 /* Use the location where a macro was expanded rather than where
6060 it was defined to make sure macros defined in system headers
6061 but used incorrectly elsewhere are diagnosed. */
6062 source_location exploc = expansion_point_location_if_in_system_header (loc);
6064 /* The gmsgid may be a format string with %< and %>. */
6065 warned = pedwarn (exploc, opt, gmsgid);
6066 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6067 if (*ofwhat && warned)
6068 inform (exploc, "(near initialization for %qs)", ofwhat);
6071 /* Issue a warning for a bad initializer component.
6073 OPT is the OPT_W* value corresponding to the warning option that
6074 controls this warning. GMSGID identifies the message. The
6075 component name is taken from the spelling stack. */
6077 static void
6078 warning_init (location_t loc, int opt, const char *gmsgid)
6080 char *ofwhat;
6081 bool warned;
6083 /* Use the location where a macro was expanded rather than where
6084 it was defined to make sure macros defined in system headers
6085 but used incorrectly elsewhere are diagnosed. */
6086 source_location exploc = expansion_point_location_if_in_system_header (loc);
6088 /* The gmsgid may be a format string with %< and %>. */
6089 warned = warning_at (exploc, opt, gmsgid);
6090 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6091 if (*ofwhat && warned)
6092 inform (exploc, "(near initialization for %qs)", ofwhat);
6095 /* If TYPE is an array type and EXPR is a parenthesized string
6096 constant, warn if pedantic that EXPR is being used to initialize an
6097 object of type TYPE. */
6099 void
6100 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6102 if (pedantic
6103 && TREE_CODE (type) == ARRAY_TYPE
6104 && TREE_CODE (expr.value) == STRING_CST
6105 && expr.original_code != STRING_CST)
6106 pedwarn_init (loc, OPT_Wpedantic,
6107 "array initialized from parenthesized string constant");
6110 /* Convert value RHS to type TYPE as preparation for an assignment to
6111 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6112 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6113 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6114 constant before any folding.
6115 The real work of conversion is done by `convert'.
6116 The purpose of this function is to generate error messages
6117 for assignments that are not allowed in C.
6118 ERRTYPE says whether it is argument passing, assignment,
6119 initialization or return.
6121 In the following example, '~' denotes where EXPR_LOC and '^' where
6122 LOCATION point to:
6124 f (var); [ic_argpass]
6125 ^ ~~~
6126 x = var; [ic_assign]
6127 ^ ~~~;
6128 int x = var; [ic_init]
6130 return x; [ic_return]
6133 FUNCTION is a tree for the function being called.
6134 PARMNUM is the number of the argument, for printing in error messages. */
6136 static tree
6137 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6138 tree rhs, tree origtype, enum impl_conv errtype,
6139 bool null_pointer_constant, tree fundecl,
6140 tree function, int parmnum)
6142 enum tree_code codel = TREE_CODE (type);
6143 tree orig_rhs = rhs;
6144 tree rhstype;
6145 enum tree_code coder;
6146 tree rname = NULL_TREE;
6147 bool objc_ok = false;
6149 /* Use the expansion point location to handle cases such as user's
6150 function returning a wrong-type macro defined in a system header. */
6151 location = expansion_point_location_if_in_system_header (location);
6153 if (errtype == ic_argpass)
6155 tree selector;
6156 /* Change pointer to function to the function itself for
6157 diagnostics. */
6158 if (TREE_CODE (function) == ADDR_EXPR
6159 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6160 function = TREE_OPERAND (function, 0);
6162 /* Handle an ObjC selector specially for diagnostics. */
6163 selector = objc_message_selector ();
6164 rname = function;
6165 if (selector && parmnum > 2)
6167 rname = selector;
6168 parmnum -= 2;
6172 /* This macro is used to emit diagnostics to ensure that all format
6173 strings are complete sentences, visible to gettext and checked at
6174 compile time. */
6175 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6176 do { \
6177 switch (errtype) \
6179 case ic_argpass: \
6180 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6181 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6182 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6183 "expected %qT but argument is of type %qT", \
6184 type, rhstype); \
6185 break; \
6186 case ic_assign: \
6187 pedwarn (LOCATION, OPT, AS); \
6188 break; \
6189 case ic_init: \
6190 pedwarn_init (LOCATION, OPT, IN); \
6191 break; \
6192 case ic_return: \
6193 pedwarn (LOCATION, OPT, RE); \
6194 break; \
6195 default: \
6196 gcc_unreachable (); \
6198 } while (0)
6200 /* This macro is used to emit diagnostics to ensure that all format
6201 strings are complete sentences, visible to gettext and checked at
6202 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6203 extra parameter to enumerate qualifiers. */
6204 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6205 do { \
6206 switch (errtype) \
6208 case ic_argpass: \
6209 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6210 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6211 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6212 "expected %qT but argument is of type %qT", \
6213 type, rhstype); \
6214 break; \
6215 case ic_assign: \
6216 pedwarn (LOCATION, OPT, AS, QUALS); \
6217 break; \
6218 case ic_init: \
6219 pedwarn (LOCATION, OPT, IN, QUALS); \
6220 break; \
6221 case ic_return: \
6222 pedwarn (LOCATION, OPT, RE, QUALS); \
6223 break; \
6224 default: \
6225 gcc_unreachable (); \
6227 } while (0)
6229 /* This macro is used to emit diagnostics to ensure that all format
6230 strings are complete sentences, visible to gettext and checked at
6231 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6232 warning_at instead of pedwarn. */
6233 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6234 do { \
6235 switch (errtype) \
6237 case ic_argpass: \
6238 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6239 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6240 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6241 "expected %qT but argument is of type %qT", \
6242 type, rhstype); \
6243 break; \
6244 case ic_assign: \
6245 warning_at (LOCATION, OPT, AS, QUALS); \
6246 break; \
6247 case ic_init: \
6248 warning_at (LOCATION, OPT, IN, QUALS); \
6249 break; \
6250 case ic_return: \
6251 warning_at (LOCATION, OPT, RE, QUALS); \
6252 break; \
6253 default: \
6254 gcc_unreachable (); \
6256 } while (0)
6258 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6259 rhs = TREE_OPERAND (rhs, 0);
6261 rhstype = TREE_TYPE (rhs);
6262 coder = TREE_CODE (rhstype);
6264 if (coder == ERROR_MARK)
6265 return error_mark_node;
6267 if (c_dialect_objc ())
6269 int parmno;
6271 switch (errtype)
6273 case ic_return:
6274 parmno = 0;
6275 break;
6277 case ic_assign:
6278 parmno = -1;
6279 break;
6281 case ic_init:
6282 parmno = -2;
6283 break;
6285 default:
6286 parmno = parmnum;
6287 break;
6290 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6293 if (warn_cxx_compat)
6295 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6296 if (checktype != error_mark_node
6297 && TREE_CODE (type) == ENUMERAL_TYPE
6298 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6300 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6301 G_("enum conversion when passing argument "
6302 "%d of %qE is invalid in C++"),
6303 G_("enum conversion in assignment is "
6304 "invalid in C++"),
6305 G_("enum conversion in initialization is "
6306 "invalid in C++"),
6307 G_("enum conversion in return is "
6308 "invalid in C++"));
6312 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6313 return rhs;
6315 if (coder == VOID_TYPE)
6317 /* Except for passing an argument to an unprototyped function,
6318 this is a constraint violation. When passing an argument to
6319 an unprototyped function, it is compile-time undefined;
6320 making it a constraint in that case was rejected in
6321 DR#252. */
6322 error_at (location, "void value not ignored as it ought to be");
6323 return error_mark_node;
6325 rhs = require_complete_type (location, rhs);
6326 if (rhs == error_mark_node)
6327 return error_mark_node;
6329 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6330 return error_mark_node;
6332 /* A non-reference type can convert to a reference. This handles
6333 va_start, va_copy and possibly port built-ins. */
6334 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6336 if (!lvalue_p (rhs))
6338 error_at (location, "cannot pass rvalue to reference parameter");
6339 return error_mark_node;
6341 if (!c_mark_addressable (rhs))
6342 return error_mark_node;
6343 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6344 SET_EXPR_LOCATION (rhs, location);
6346 rhs = convert_for_assignment (location, expr_loc,
6347 build_pointer_type (TREE_TYPE (type)),
6348 rhs, origtype, errtype,
6349 null_pointer_constant, fundecl, function,
6350 parmnum);
6351 if (rhs == error_mark_node)
6352 return error_mark_node;
6354 rhs = build1 (NOP_EXPR, type, rhs);
6355 SET_EXPR_LOCATION (rhs, location);
6356 return rhs;
6358 /* Some types can interconvert without explicit casts. */
6359 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6360 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6361 return convert (type, rhs);
6362 /* Arithmetic types all interconvert, and enum is treated like int. */
6363 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6364 || codel == FIXED_POINT_TYPE
6365 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6366 || codel == BOOLEAN_TYPE)
6367 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6368 || coder == FIXED_POINT_TYPE
6369 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6370 || coder == BOOLEAN_TYPE))
6372 tree ret;
6373 bool save = in_late_binary_op;
6374 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6375 || (coder == REAL_TYPE
6376 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6377 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
6378 in_late_binary_op = true;
6379 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6380 ? expr_loc : location, type, orig_rhs);
6381 in_late_binary_op = save;
6382 return ret;
6385 /* Aggregates in different TUs might need conversion. */
6386 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6387 && codel == coder
6388 && comptypes (type, rhstype))
6389 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6390 ? expr_loc : location, type, rhs);
6392 /* Conversion to a transparent union or record from its member types.
6393 This applies only to function arguments. */
6394 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6395 && TYPE_TRANSPARENT_AGGR (type))
6396 && errtype == ic_argpass)
6398 tree memb, marginal_memb = NULL_TREE;
6400 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6402 tree memb_type = TREE_TYPE (memb);
6404 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6405 TYPE_MAIN_VARIANT (rhstype)))
6406 break;
6408 if (TREE_CODE (memb_type) != POINTER_TYPE)
6409 continue;
6411 if (coder == POINTER_TYPE)
6413 tree ttl = TREE_TYPE (memb_type);
6414 tree ttr = TREE_TYPE (rhstype);
6416 /* Any non-function converts to a [const][volatile] void *
6417 and vice versa; otherwise, targets must be the same.
6418 Meanwhile, the lhs target must have all the qualifiers of
6419 the rhs. */
6420 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6421 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6422 || comp_target_types (location, memb_type, rhstype))
6424 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6425 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6426 /* If this type won't generate any warnings, use it. */
6427 if (lquals == rquals
6428 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6429 && TREE_CODE (ttl) == FUNCTION_TYPE)
6430 ? ((lquals | rquals) == rquals)
6431 : ((lquals | rquals) == lquals)))
6432 break;
6434 /* Keep looking for a better type, but remember this one. */
6435 if (!marginal_memb)
6436 marginal_memb = memb;
6440 /* Can convert integer zero to any pointer type. */
6441 if (null_pointer_constant)
6443 rhs = null_pointer_node;
6444 break;
6448 if (memb || marginal_memb)
6450 if (!memb)
6452 /* We have only a marginally acceptable member type;
6453 it needs a warning. */
6454 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6455 tree ttr = TREE_TYPE (rhstype);
6457 /* Const and volatile mean something different for function
6458 types, so the usual warnings are not appropriate. */
6459 if (TREE_CODE (ttr) == FUNCTION_TYPE
6460 && TREE_CODE (ttl) == FUNCTION_TYPE)
6462 /* Because const and volatile on functions are
6463 restrictions that say the function will not do
6464 certain things, it is okay to use a const or volatile
6465 function where an ordinary one is wanted, but not
6466 vice-versa. */
6467 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6468 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6469 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6470 OPT_Wdiscarded_qualifiers,
6471 G_("passing argument %d of %qE "
6472 "makes %q#v qualified function "
6473 "pointer from unqualified"),
6474 G_("assignment makes %q#v qualified "
6475 "function pointer from "
6476 "unqualified"),
6477 G_("initialization makes %q#v qualified "
6478 "function pointer from "
6479 "unqualified"),
6480 G_("return makes %q#v qualified function "
6481 "pointer from unqualified"),
6482 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6484 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6485 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6486 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6487 OPT_Wdiscarded_qualifiers,
6488 G_("passing argument %d of %qE discards "
6489 "%qv qualifier from pointer target type"),
6490 G_("assignment discards %qv qualifier "
6491 "from pointer target type"),
6492 G_("initialization discards %qv qualifier "
6493 "from pointer target type"),
6494 G_("return discards %qv qualifier from "
6495 "pointer target type"),
6496 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6498 memb = marginal_memb;
6501 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6502 pedwarn (location, OPT_Wpedantic,
6503 "ISO C prohibits argument conversion to union type");
6505 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6506 return build_constructor_single (type, memb, rhs);
6510 /* Conversions among pointers */
6511 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6512 && (coder == codel))
6514 tree ttl = TREE_TYPE (type);
6515 tree ttr = TREE_TYPE (rhstype);
6516 tree mvl = ttl;
6517 tree mvr = ttr;
6518 bool is_opaque_pointer;
6519 int target_cmp = 0; /* Cache comp_target_types () result. */
6520 addr_space_t asl;
6521 addr_space_t asr;
6523 if (TREE_CODE (mvl) != ARRAY_TYPE)
6524 mvl = (TYPE_ATOMIC (mvl)
6525 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6526 TYPE_QUAL_ATOMIC)
6527 : TYPE_MAIN_VARIANT (mvl));
6528 if (TREE_CODE (mvr) != ARRAY_TYPE)
6529 mvr = (TYPE_ATOMIC (mvr)
6530 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6531 TYPE_QUAL_ATOMIC)
6532 : TYPE_MAIN_VARIANT (mvr));
6533 /* Opaque pointers are treated like void pointers. */
6534 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6536 /* The Plan 9 compiler permits a pointer to a struct to be
6537 automatically converted into a pointer to an anonymous field
6538 within the struct. */
6539 if (flag_plan9_extensions
6540 && RECORD_OR_UNION_TYPE_P (mvl)
6541 && RECORD_OR_UNION_TYPE_P (mvr)
6542 && mvl != mvr)
6544 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6545 if (new_rhs != NULL_TREE)
6547 rhs = new_rhs;
6548 rhstype = TREE_TYPE (rhs);
6549 coder = TREE_CODE (rhstype);
6550 ttr = TREE_TYPE (rhstype);
6551 mvr = TYPE_MAIN_VARIANT (ttr);
6555 /* C++ does not allow the implicit conversion void* -> T*. However,
6556 for the purpose of reducing the number of false positives, we
6557 tolerate the special case of
6559 int *p = NULL;
6561 where NULL is typically defined in C to be '(void *) 0'. */
6562 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6563 warning_at (errtype == ic_argpass ? expr_loc : location,
6564 OPT_Wc___compat,
6565 "request for implicit conversion "
6566 "from %qT to %qT not permitted in C++", rhstype, type);
6568 /* See if the pointers point to incompatible address spaces. */
6569 asl = TYPE_ADDR_SPACE (ttl);
6570 asr = TYPE_ADDR_SPACE (ttr);
6571 if (!null_pointer_constant_p (rhs)
6572 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6574 switch (errtype)
6576 case ic_argpass:
6577 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6578 "non-enclosed address space", parmnum, rname);
6579 break;
6580 case ic_assign:
6581 error_at (location, "assignment from pointer to "
6582 "non-enclosed address space");
6583 break;
6584 case ic_init:
6585 error_at (location, "initialization from pointer to "
6586 "non-enclosed address space");
6587 break;
6588 case ic_return:
6589 error_at (location, "return from pointer to "
6590 "non-enclosed address space");
6591 break;
6592 default:
6593 gcc_unreachable ();
6595 return error_mark_node;
6598 /* Check if the right-hand side has a format attribute but the
6599 left-hand side doesn't. */
6600 if (warn_suggest_attribute_format
6601 && check_missing_format_attribute (type, rhstype))
6603 switch (errtype)
6605 case ic_argpass:
6606 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6607 "argument %d of %qE might be "
6608 "a candidate for a format attribute",
6609 parmnum, rname);
6610 break;
6611 case ic_assign:
6612 warning_at (location, OPT_Wsuggest_attribute_format,
6613 "assignment left-hand side might be "
6614 "a candidate for a format attribute");
6615 break;
6616 case ic_init:
6617 warning_at (location, OPT_Wsuggest_attribute_format,
6618 "initialization left-hand side might be "
6619 "a candidate for a format attribute");
6620 break;
6621 case ic_return:
6622 warning_at (location, OPT_Wsuggest_attribute_format,
6623 "return type might be "
6624 "a candidate for a format attribute");
6625 break;
6626 default:
6627 gcc_unreachable ();
6631 /* Any non-function converts to a [const][volatile] void *
6632 and vice versa; otherwise, targets must be the same.
6633 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6634 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6635 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6636 || (target_cmp = comp_target_types (location, type, rhstype))
6637 || is_opaque_pointer
6638 || ((c_common_unsigned_type (mvl)
6639 == c_common_unsigned_type (mvr))
6640 && (c_common_signed_type (mvl)
6641 == c_common_signed_type (mvr))
6642 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6644 /* Warn about loss of qualifers from pointers to arrays with
6645 qualifiers on the element type. */
6646 if (TREE_CODE (ttr) == ARRAY_TYPE)
6648 ttr = strip_array_types (ttr);
6649 ttl = strip_array_types (ttl);
6651 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6652 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6653 WARNING_FOR_QUALIFIERS (location, expr_loc,
6654 OPT_Wdiscarded_array_qualifiers,
6655 G_("passing argument %d of %qE discards "
6656 "%qv qualifier from pointer target type"),
6657 G_("assignment discards %qv qualifier "
6658 "from pointer target type"),
6659 G_("initialization discards %qv qualifier "
6660 "from pointer target type"),
6661 G_("return discards %qv qualifier from "
6662 "pointer target type"),
6663 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6665 else if (pedantic
6666 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6668 (VOID_TYPE_P (ttr)
6669 && !null_pointer_constant
6670 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6671 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6672 G_("ISO C forbids passing argument %d of "
6673 "%qE between function pointer "
6674 "and %<void *%>"),
6675 G_("ISO C forbids assignment between "
6676 "function pointer and %<void *%>"),
6677 G_("ISO C forbids initialization between "
6678 "function pointer and %<void *%>"),
6679 G_("ISO C forbids return between function "
6680 "pointer and %<void *%>"));
6681 /* Const and volatile mean something different for function types,
6682 so the usual warnings are not appropriate. */
6683 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6684 && TREE_CODE (ttl) != FUNCTION_TYPE)
6686 /* Don't warn about loss of qualifier for conversions from
6687 qualified void* to pointers to arrays with corresponding
6688 qualifier on the element type. */
6689 if (!pedantic)
6690 ttl = strip_array_types (ttl);
6692 /* Assignments between atomic and non-atomic objects are OK. */
6693 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6694 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6696 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6697 OPT_Wdiscarded_qualifiers,
6698 G_("passing argument %d of %qE discards "
6699 "%qv qualifier from pointer target type"),
6700 G_("assignment discards %qv qualifier "
6701 "from pointer target type"),
6702 G_("initialization discards %qv qualifier "
6703 "from pointer target type"),
6704 G_("return discards %qv qualifier from "
6705 "pointer target type"),
6706 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6708 /* If this is not a case of ignoring a mismatch in signedness,
6709 no warning. */
6710 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6711 || target_cmp)
6713 /* If there is a mismatch, do warn. */
6714 else if (warn_pointer_sign)
6715 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6716 G_("pointer targets in passing argument "
6717 "%d of %qE differ in signedness"),
6718 G_("pointer targets in assignment "
6719 "differ in signedness"),
6720 G_("pointer targets in initialization "
6721 "differ in signedness"),
6722 G_("pointer targets in return differ "
6723 "in signedness"));
6725 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6726 && TREE_CODE (ttr) == FUNCTION_TYPE)
6728 /* Because const and volatile on functions are restrictions
6729 that say the function will not do certain things,
6730 it is okay to use a const or volatile function
6731 where an ordinary one is wanted, but not vice-versa. */
6732 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6733 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6734 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6735 OPT_Wdiscarded_qualifiers,
6736 G_("passing argument %d of %qE makes "
6737 "%q#v qualified function pointer "
6738 "from unqualified"),
6739 G_("assignment makes %q#v qualified function "
6740 "pointer from unqualified"),
6741 G_("initialization makes %q#v qualified "
6742 "function pointer from unqualified"),
6743 G_("return makes %q#v qualified function "
6744 "pointer from unqualified"),
6745 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6748 else
6749 /* Avoid warning about the volatile ObjC EH puts on decls. */
6750 if (!objc_ok)
6751 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6752 OPT_Wincompatible_pointer_types,
6753 G_("passing argument %d of %qE from "
6754 "incompatible pointer type"),
6755 G_("assignment from incompatible pointer type"),
6756 G_("initialization from incompatible "
6757 "pointer type"),
6758 G_("return from incompatible pointer type"));
6760 return convert (type, rhs);
6762 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6764 /* ??? This should not be an error when inlining calls to
6765 unprototyped functions. */
6766 error_at (location, "invalid use of non-lvalue array");
6767 return error_mark_node;
6769 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6771 /* An explicit constant 0 can convert to a pointer,
6772 or one that results from arithmetic, even including
6773 a cast to integer type. */
6774 if (!null_pointer_constant)
6775 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6776 OPT_Wint_conversion,
6777 G_("passing argument %d of %qE makes "
6778 "pointer from integer without a cast"),
6779 G_("assignment makes pointer from integer "
6780 "without a cast"),
6781 G_("initialization makes pointer from "
6782 "integer without a cast"),
6783 G_("return makes pointer from integer "
6784 "without a cast"));
6786 return convert (type, rhs);
6788 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6790 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6791 OPT_Wint_conversion,
6792 G_("passing argument %d of %qE makes integer "
6793 "from pointer without a cast"),
6794 G_("assignment makes integer from pointer "
6795 "without a cast"),
6796 G_("initialization makes integer from pointer "
6797 "without a cast"),
6798 G_("return makes integer from pointer "
6799 "without a cast"));
6800 return convert (type, rhs);
6802 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6804 tree ret;
6805 bool save = in_late_binary_op;
6806 in_late_binary_op = true;
6807 ret = convert (type, rhs);
6808 in_late_binary_op = save;
6809 return ret;
6812 switch (errtype)
6814 case ic_argpass:
6815 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6816 rname);
6817 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6818 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6819 "expected %qT but argument is of type %qT", type, rhstype);
6820 break;
6821 case ic_assign:
6822 error_at (location, "incompatible types when assigning to type %qT from "
6823 "type %qT", type, rhstype);
6824 break;
6825 case ic_init:
6826 error_at (location,
6827 "incompatible types when initializing type %qT using type %qT",
6828 type, rhstype);
6829 break;
6830 case ic_return:
6831 error_at (location,
6832 "incompatible types when returning type %qT but %qT was "
6833 "expected", rhstype, type);
6834 break;
6835 default:
6836 gcc_unreachable ();
6839 return error_mark_node;
6842 /* If VALUE is a compound expr all of whose expressions are constant, then
6843 return its value. Otherwise, return error_mark_node.
6845 This is for handling COMPOUND_EXPRs as initializer elements
6846 which is allowed with a warning when -pedantic is specified. */
6848 static tree
6849 valid_compound_expr_initializer (tree value, tree endtype)
6851 if (TREE_CODE (value) == COMPOUND_EXPR)
6853 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6854 == error_mark_node)
6855 return error_mark_node;
6856 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6857 endtype);
6859 else if (!initializer_constant_valid_p (value, endtype))
6860 return error_mark_node;
6861 else
6862 return value;
6865 /* Perform appropriate conversions on the initial value of a variable,
6866 store it in the declaration DECL,
6867 and print any error messages that are appropriate.
6868 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6869 If the init is invalid, store an ERROR_MARK.
6871 INIT_LOC is the location of the initial value. */
6873 void
6874 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6876 tree value, type;
6877 bool npc = false;
6879 /* If variable's type was invalidly declared, just ignore it. */
6881 type = TREE_TYPE (decl);
6882 if (TREE_CODE (type) == ERROR_MARK)
6883 return;
6885 /* Digest the specified initializer into an expression. */
6887 if (init)
6888 npc = null_pointer_constant_p (init);
6889 value = digest_init (init_loc, type, init, origtype, npc,
6890 true, TREE_STATIC (decl));
6892 /* Store the expression if valid; else report error. */
6894 if (!in_system_header_at (input_location)
6895 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6896 warning (OPT_Wtraditional, "traditional C rejects automatic "
6897 "aggregate initialization");
6899 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6900 DECL_INITIAL (decl) = value;
6902 /* ANSI wants warnings about out-of-range constant initializers. */
6903 STRIP_TYPE_NOPS (value);
6904 if (TREE_STATIC (decl))
6905 constant_expression_warning (value);
6907 /* Check if we need to set array size from compound literal size. */
6908 if (TREE_CODE (type) == ARRAY_TYPE
6909 && TYPE_DOMAIN (type) == 0
6910 && value != error_mark_node)
6912 tree inside_init = init;
6914 STRIP_TYPE_NOPS (inside_init);
6915 inside_init = fold (inside_init);
6917 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6919 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6921 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6923 /* For int foo[] = (int [3]){1}; we need to set array size
6924 now since later on array initializer will be just the
6925 brace enclosed list of the compound literal. */
6926 tree etype = strip_array_types (TREE_TYPE (decl));
6927 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6928 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6929 layout_type (type);
6930 layout_decl (cldecl, 0);
6931 TREE_TYPE (decl)
6932 = c_build_qualified_type (type, TYPE_QUALS (etype));
6938 /* Methods for storing and printing names for error messages. */
6940 /* Implement a spelling stack that allows components of a name to be pushed
6941 and popped. Each element on the stack is this structure. */
6943 struct spelling
6945 int kind;
6946 union
6948 unsigned HOST_WIDE_INT i;
6949 const char *s;
6950 } u;
6953 #define SPELLING_STRING 1
6954 #define SPELLING_MEMBER 2
6955 #define SPELLING_BOUNDS 3
6957 static struct spelling *spelling; /* Next stack element (unused). */
6958 static struct spelling *spelling_base; /* Spelling stack base. */
6959 static int spelling_size; /* Size of the spelling stack. */
6961 /* Macros to save and restore the spelling stack around push_... functions.
6962 Alternative to SAVE_SPELLING_STACK. */
6964 #define SPELLING_DEPTH() (spelling - spelling_base)
6965 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6967 /* Push an element on the spelling stack with type KIND and assign VALUE
6968 to MEMBER. */
6970 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6972 int depth = SPELLING_DEPTH (); \
6974 if (depth >= spelling_size) \
6976 spelling_size += 10; \
6977 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6978 spelling_size); \
6979 RESTORE_SPELLING_DEPTH (depth); \
6982 spelling->kind = (KIND); \
6983 spelling->MEMBER = (VALUE); \
6984 spelling++; \
6987 /* Push STRING on the stack. Printed literally. */
6989 static void
6990 push_string (const char *string)
6992 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6995 /* Push a member name on the stack. Printed as '.' STRING. */
6997 static void
6998 push_member_name (tree decl)
7000 const char *const string
7001 = (DECL_NAME (decl)
7002 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7003 : _("<anonymous>"));
7004 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7007 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7009 static void
7010 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7012 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7015 /* Compute the maximum size in bytes of the printed spelling. */
7017 static int
7018 spelling_length (void)
7020 int size = 0;
7021 struct spelling *p;
7023 for (p = spelling_base; p < spelling; p++)
7025 if (p->kind == SPELLING_BOUNDS)
7026 size += 25;
7027 else
7028 size += strlen (p->u.s) + 1;
7031 return size;
7034 /* Print the spelling to BUFFER and return it. */
7036 static char *
7037 print_spelling (char *buffer)
7039 char *d = buffer;
7040 struct spelling *p;
7042 for (p = spelling_base; p < spelling; p++)
7043 if (p->kind == SPELLING_BOUNDS)
7045 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7046 d += strlen (d);
7048 else
7050 const char *s;
7051 if (p->kind == SPELLING_MEMBER)
7052 *d++ = '.';
7053 for (s = p->u.s; (*d = *s++); d++)
7056 *d++ = '\0';
7057 return buffer;
7060 /* Digest the parser output INIT as an initializer for type TYPE.
7061 Return a C expression of type TYPE to represent the initial value.
7063 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7065 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7067 If INIT is a string constant, STRICT_STRING is true if it is
7068 unparenthesized or we should not warn here for it being parenthesized.
7069 For other types of INIT, STRICT_STRING is not used.
7071 INIT_LOC is the location of the INIT.
7073 REQUIRE_CONSTANT requests an error if non-constant initializers or
7074 elements are seen. */
7076 static tree
7077 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7078 bool null_pointer_constant, bool strict_string,
7079 int require_constant)
7081 enum tree_code code = TREE_CODE (type);
7082 tree inside_init = init;
7083 tree semantic_type = NULL_TREE;
7084 bool maybe_const = true;
7086 if (type == error_mark_node
7087 || !init
7088 || error_operand_p (init))
7089 return error_mark_node;
7091 STRIP_TYPE_NOPS (inside_init);
7093 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7095 semantic_type = TREE_TYPE (inside_init);
7096 inside_init = TREE_OPERAND (inside_init, 0);
7098 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7099 inside_init = decl_constant_value_for_optimization (inside_init);
7101 /* Initialization of an array of chars from a string constant
7102 optionally enclosed in braces. */
7104 if (code == ARRAY_TYPE && inside_init
7105 && TREE_CODE (inside_init) == STRING_CST)
7107 tree typ1
7108 = (TYPE_ATOMIC (TREE_TYPE (type))
7109 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7110 TYPE_QUAL_ATOMIC)
7111 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7112 /* Note that an array could be both an array of character type
7113 and an array of wchar_t if wchar_t is signed char or unsigned
7114 char. */
7115 bool char_array = (typ1 == char_type_node
7116 || typ1 == signed_char_type_node
7117 || typ1 == unsigned_char_type_node);
7118 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7119 bool char16_array = !!comptypes (typ1, char16_type_node);
7120 bool char32_array = !!comptypes (typ1, char32_type_node);
7122 if (char_array || wchar_array || char16_array || char32_array)
7124 struct c_expr expr;
7125 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7126 expr.value = inside_init;
7127 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7128 expr.original_type = NULL;
7129 maybe_warn_string_init (init_loc, type, expr);
7131 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7132 pedwarn_init (init_loc, OPT_Wpedantic,
7133 "initialization of a flexible array member");
7135 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7136 TYPE_MAIN_VARIANT (type)))
7137 return inside_init;
7139 if (char_array)
7141 if (typ2 != char_type_node)
7143 error_init (init_loc, "char-array initialized from wide "
7144 "string");
7145 return error_mark_node;
7148 else
7150 if (typ2 == char_type_node)
7152 error_init (init_loc, "wide character array initialized "
7153 "from non-wide string");
7154 return error_mark_node;
7156 else if (!comptypes(typ1, typ2))
7158 error_init (init_loc, "wide character array initialized "
7159 "from incompatible wide string");
7160 return error_mark_node;
7164 TREE_TYPE (inside_init) = type;
7165 if (TYPE_DOMAIN (type) != 0
7166 && TYPE_SIZE (type) != 0
7167 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7169 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7171 /* Subtract the size of a single (possibly wide) character
7172 because it's ok to ignore the terminating null char
7173 that is counted in the length of the constant. */
7174 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7175 (len
7176 - (TYPE_PRECISION (typ1)
7177 / BITS_PER_UNIT))))
7178 pedwarn_init (init_loc, 0,
7179 ("initializer-string for array of chars "
7180 "is too long"));
7181 else if (warn_cxx_compat
7182 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7183 warning_at (init_loc, OPT_Wc___compat,
7184 ("initializer-string for array chars "
7185 "is too long for C++"));
7188 return inside_init;
7190 else if (INTEGRAL_TYPE_P (typ1))
7192 error_init (init_loc, "array of inappropriate type initialized "
7193 "from string constant");
7194 return error_mark_node;
7198 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7199 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7200 below and handle as a constructor. */
7201 if (code == VECTOR_TYPE
7202 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7203 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7204 && TREE_CONSTANT (inside_init))
7206 if (TREE_CODE (inside_init) == VECTOR_CST
7207 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7208 TYPE_MAIN_VARIANT (type)))
7209 return inside_init;
7211 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7213 unsigned HOST_WIDE_INT ix;
7214 tree value;
7215 bool constant_p = true;
7217 /* Iterate through elements and check if all constructor
7218 elements are *_CSTs. */
7219 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7220 if (!CONSTANT_CLASS_P (value))
7222 constant_p = false;
7223 break;
7226 if (constant_p)
7227 return build_vector_from_ctor (type,
7228 CONSTRUCTOR_ELTS (inside_init));
7232 if (warn_sequence_point)
7233 verify_sequence_points (inside_init);
7235 /* Any type can be initialized
7236 from an expression of the same type, optionally with braces. */
7238 if (inside_init && TREE_TYPE (inside_init) != 0
7239 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7240 TYPE_MAIN_VARIANT (type))
7241 || (code == ARRAY_TYPE
7242 && comptypes (TREE_TYPE (inside_init), type))
7243 || (code == VECTOR_TYPE
7244 && comptypes (TREE_TYPE (inside_init), type))
7245 || (code == POINTER_TYPE
7246 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7247 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7248 TREE_TYPE (type)))))
7250 if (code == POINTER_TYPE)
7252 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7254 if (TREE_CODE (inside_init) == STRING_CST
7255 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7256 inside_init = array_to_pointer_conversion
7257 (init_loc, inside_init);
7258 else
7260 error_init (init_loc, "invalid use of non-lvalue array");
7261 return error_mark_node;
7266 if (code == VECTOR_TYPE)
7267 /* Although the types are compatible, we may require a
7268 conversion. */
7269 inside_init = convert (type, inside_init);
7271 if (require_constant
7272 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7274 /* As an extension, allow initializing objects with static storage
7275 duration with compound literals (which are then treated just as
7276 the brace enclosed list they contain). Also allow this for
7277 vectors, as we can only assign them with compound literals. */
7278 if (flag_isoc99 && code != VECTOR_TYPE)
7279 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7280 "is not constant");
7281 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7282 inside_init = DECL_INITIAL (decl);
7285 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7286 && TREE_CODE (inside_init) != CONSTRUCTOR)
7288 error_init (init_loc, "array initialized from non-constant array "
7289 "expression");
7290 return error_mark_node;
7293 /* Compound expressions can only occur here if -Wpedantic or
7294 -pedantic-errors is specified. In the later case, we always want
7295 an error. In the former case, we simply want a warning. */
7296 if (require_constant && pedantic
7297 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7299 inside_init
7300 = valid_compound_expr_initializer (inside_init,
7301 TREE_TYPE (inside_init));
7302 if (inside_init == error_mark_node)
7303 error_init (init_loc, "initializer element is not constant");
7304 else
7305 pedwarn_init (init_loc, OPT_Wpedantic,
7306 "initializer element is not constant");
7307 if (flag_pedantic_errors)
7308 inside_init = error_mark_node;
7310 else if (require_constant
7311 && !initializer_constant_valid_p (inside_init,
7312 TREE_TYPE (inside_init)))
7314 error_init (init_loc, "initializer element is not constant");
7315 inside_init = error_mark_node;
7317 else if (require_constant && !maybe_const)
7318 pedwarn_init (init_loc, OPT_Wpedantic,
7319 "initializer element is not a constant expression");
7321 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7322 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7323 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7324 type, inside_init, origtype,
7325 ic_init, null_pointer_constant,
7326 NULL_TREE, NULL_TREE, 0);
7327 return inside_init;
7330 /* Handle scalar types, including conversions. */
7332 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7333 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7334 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7336 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7337 && (TREE_CODE (init) == STRING_CST
7338 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7339 inside_init = init = array_to_pointer_conversion (init_loc, init);
7340 if (semantic_type)
7341 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7342 inside_init);
7343 inside_init
7344 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7345 inside_init, origtype, ic_init,
7346 null_pointer_constant, NULL_TREE, NULL_TREE,
7349 /* Check to see if we have already given an error message. */
7350 if (inside_init == error_mark_node)
7352 else if (require_constant && !TREE_CONSTANT (inside_init))
7354 error_init (init_loc, "initializer element is not constant");
7355 inside_init = error_mark_node;
7357 else if (require_constant
7358 && !initializer_constant_valid_p (inside_init,
7359 TREE_TYPE (inside_init)))
7361 error_init (init_loc, "initializer element is not computable at "
7362 "load time");
7363 inside_init = error_mark_node;
7365 else if (require_constant && !maybe_const)
7366 pedwarn_init (init_loc, OPT_Wpedantic,
7367 "initializer element is not a constant expression");
7369 return inside_init;
7372 /* Come here only for records and arrays. */
7374 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7376 error_init (init_loc, "variable-sized object may not be initialized");
7377 return error_mark_node;
7380 error_init (init_loc, "invalid initializer");
7381 return error_mark_node;
7384 /* Handle initializers that use braces. */
7386 /* Type of object we are accumulating a constructor for.
7387 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7388 static tree constructor_type;
7390 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7391 left to fill. */
7392 static tree constructor_fields;
7394 /* For an ARRAY_TYPE, this is the specified index
7395 at which to store the next element we get. */
7396 static tree constructor_index;
7398 /* For an ARRAY_TYPE, this is the maximum index. */
7399 static tree constructor_max_index;
7401 /* For a RECORD_TYPE, this is the first field not yet written out. */
7402 static tree constructor_unfilled_fields;
7404 /* For an ARRAY_TYPE, this is the index of the first element
7405 not yet written out. */
7406 static tree constructor_unfilled_index;
7408 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7409 This is so we can generate gaps between fields, when appropriate. */
7410 static tree constructor_bit_index;
7412 /* If we are saving up the elements rather than allocating them,
7413 this is the list of elements so far (in reverse order,
7414 most recent first). */
7415 static vec<constructor_elt, va_gc> *constructor_elements;
7417 /* 1 if constructor should be incrementally stored into a constructor chain,
7418 0 if all the elements should be kept in AVL tree. */
7419 static int constructor_incremental;
7421 /* 1 if so far this constructor's elements are all compile-time constants. */
7422 static int constructor_constant;
7424 /* 1 if so far this constructor's elements are all valid address constants. */
7425 static int constructor_simple;
7427 /* 1 if this constructor has an element that cannot be part of a
7428 constant expression. */
7429 static int constructor_nonconst;
7431 /* 1 if this constructor is erroneous so far. */
7432 static int constructor_erroneous;
7434 /* 1 if this constructor is the universal zero initializer { 0 }. */
7435 static int constructor_zeroinit;
7437 /* Structure for managing pending initializer elements, organized as an
7438 AVL tree. */
7440 struct init_node
7442 struct init_node *left, *right;
7443 struct init_node *parent;
7444 int balance;
7445 tree purpose;
7446 tree value;
7447 tree origtype;
7450 /* Tree of pending elements at this constructor level.
7451 These are elements encountered out of order
7452 which belong at places we haven't reached yet in actually
7453 writing the output.
7454 Will never hold tree nodes across GC runs. */
7455 static struct init_node *constructor_pending_elts;
7457 /* The SPELLING_DEPTH of this constructor. */
7458 static int constructor_depth;
7460 /* DECL node for which an initializer is being read.
7461 0 means we are reading a constructor expression
7462 such as (struct foo) {...}. */
7463 static tree constructor_decl;
7465 /* Nonzero if this is an initializer for a top-level decl. */
7466 static int constructor_top_level;
7468 /* Nonzero if there were any member designators in this initializer. */
7469 static int constructor_designated;
7471 /* Nesting depth of designator list. */
7472 static int designator_depth;
7474 /* Nonzero if there were diagnosed errors in this designator list. */
7475 static int designator_erroneous;
7478 /* This stack has a level for each implicit or explicit level of
7479 structuring in the initializer, including the outermost one. It
7480 saves the values of most of the variables above. */
7482 struct constructor_range_stack;
7484 struct constructor_stack
7486 struct constructor_stack *next;
7487 tree type;
7488 tree fields;
7489 tree index;
7490 tree max_index;
7491 tree unfilled_index;
7492 tree unfilled_fields;
7493 tree bit_index;
7494 vec<constructor_elt, va_gc> *elements;
7495 struct init_node *pending_elts;
7496 int offset;
7497 int depth;
7498 /* If value nonzero, this value should replace the entire
7499 constructor at this level. */
7500 struct c_expr replacement_value;
7501 struct constructor_range_stack *range_stack;
7502 char constant;
7503 char simple;
7504 char nonconst;
7505 char implicit;
7506 char erroneous;
7507 char outer;
7508 char incremental;
7509 char designated;
7510 int designator_depth;
7513 static struct constructor_stack *constructor_stack;
7515 /* This stack represents designators from some range designator up to
7516 the last designator in the list. */
7518 struct constructor_range_stack
7520 struct constructor_range_stack *next, *prev;
7521 struct constructor_stack *stack;
7522 tree range_start;
7523 tree index;
7524 tree range_end;
7525 tree fields;
7528 static struct constructor_range_stack *constructor_range_stack;
7530 /* This stack records separate initializers that are nested.
7531 Nested initializers can't happen in ANSI C, but GNU C allows them
7532 in cases like { ... (struct foo) { ... } ... }. */
7534 struct initializer_stack
7536 struct initializer_stack *next;
7537 tree decl;
7538 struct constructor_stack *constructor_stack;
7539 struct constructor_range_stack *constructor_range_stack;
7540 vec<constructor_elt, va_gc> *elements;
7541 struct spelling *spelling;
7542 struct spelling *spelling_base;
7543 int spelling_size;
7544 char top_level;
7545 char require_constant_value;
7546 char require_constant_elements;
7547 rich_location *missing_brace_richloc;
7550 static struct initializer_stack *initializer_stack;
7552 /* Prepare to parse and output the initializer for variable DECL. */
7554 void
7555 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7556 rich_location *richloc)
7558 const char *locus;
7559 struct initializer_stack *p = XNEW (struct initializer_stack);
7561 p->decl = constructor_decl;
7562 p->require_constant_value = require_constant_value;
7563 p->require_constant_elements = require_constant_elements;
7564 p->constructor_stack = constructor_stack;
7565 p->constructor_range_stack = constructor_range_stack;
7566 p->elements = constructor_elements;
7567 p->spelling = spelling;
7568 p->spelling_base = spelling_base;
7569 p->spelling_size = spelling_size;
7570 p->top_level = constructor_top_level;
7571 p->next = initializer_stack;
7572 p->missing_brace_richloc = richloc;
7573 initializer_stack = p;
7575 constructor_decl = decl;
7576 constructor_designated = 0;
7577 constructor_top_level = top_level;
7579 if (decl != 0 && decl != error_mark_node)
7581 require_constant_value = TREE_STATIC (decl);
7582 require_constant_elements
7583 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7584 /* For a scalar, you can always use any value to initialize,
7585 even within braces. */
7586 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7587 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7589 else
7591 require_constant_value = 0;
7592 require_constant_elements = 0;
7593 locus = _("(anonymous)");
7596 constructor_stack = 0;
7597 constructor_range_stack = 0;
7599 found_missing_braces = 0;
7601 spelling_base = 0;
7602 spelling_size = 0;
7603 RESTORE_SPELLING_DEPTH (0);
7605 if (locus)
7606 push_string (locus);
7609 void
7610 finish_init (void)
7612 struct initializer_stack *p = initializer_stack;
7614 /* Free the whole constructor stack of this initializer. */
7615 while (constructor_stack)
7617 struct constructor_stack *q = constructor_stack;
7618 constructor_stack = q->next;
7619 free (q);
7622 gcc_assert (!constructor_range_stack);
7624 /* Pop back to the data of the outer initializer (if any). */
7625 free (spelling_base);
7627 constructor_decl = p->decl;
7628 require_constant_value = p->require_constant_value;
7629 require_constant_elements = p->require_constant_elements;
7630 constructor_stack = p->constructor_stack;
7631 constructor_range_stack = p->constructor_range_stack;
7632 constructor_elements = p->elements;
7633 spelling = p->spelling;
7634 spelling_base = p->spelling_base;
7635 spelling_size = p->spelling_size;
7636 constructor_top_level = p->top_level;
7637 initializer_stack = p->next;
7638 free (p);
7641 /* Call here when we see the initializer is surrounded by braces.
7642 This is instead of a call to push_init_level;
7643 it is matched by a call to pop_init_level.
7645 TYPE is the type to initialize, for a constructor expression.
7646 For an initializer for a decl, TYPE is zero. */
7648 void
7649 really_start_incremental_init (tree type)
7651 struct constructor_stack *p = XNEW (struct constructor_stack);
7653 if (type == 0)
7654 type = TREE_TYPE (constructor_decl);
7656 if (VECTOR_TYPE_P (type)
7657 && TYPE_VECTOR_OPAQUE (type))
7658 error ("opaque vector types cannot be initialized");
7660 p->type = constructor_type;
7661 p->fields = constructor_fields;
7662 p->index = constructor_index;
7663 p->max_index = constructor_max_index;
7664 p->unfilled_index = constructor_unfilled_index;
7665 p->unfilled_fields = constructor_unfilled_fields;
7666 p->bit_index = constructor_bit_index;
7667 p->elements = constructor_elements;
7668 p->constant = constructor_constant;
7669 p->simple = constructor_simple;
7670 p->nonconst = constructor_nonconst;
7671 p->erroneous = constructor_erroneous;
7672 p->pending_elts = constructor_pending_elts;
7673 p->depth = constructor_depth;
7674 p->replacement_value.value = 0;
7675 p->replacement_value.original_code = ERROR_MARK;
7676 p->replacement_value.original_type = NULL;
7677 p->implicit = 0;
7678 p->range_stack = 0;
7679 p->outer = 0;
7680 p->incremental = constructor_incremental;
7681 p->designated = constructor_designated;
7682 p->designator_depth = designator_depth;
7683 p->next = 0;
7684 constructor_stack = p;
7686 constructor_constant = 1;
7687 constructor_simple = 1;
7688 constructor_nonconst = 0;
7689 constructor_depth = SPELLING_DEPTH ();
7690 constructor_elements = NULL;
7691 constructor_pending_elts = 0;
7692 constructor_type = type;
7693 constructor_incremental = 1;
7694 constructor_designated = 0;
7695 constructor_zeroinit = 1;
7696 designator_depth = 0;
7697 designator_erroneous = 0;
7699 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7701 constructor_fields = TYPE_FIELDS (constructor_type);
7702 /* Skip any nameless bit fields at the beginning. */
7703 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7704 && DECL_NAME (constructor_fields) == 0)
7705 constructor_fields = DECL_CHAIN (constructor_fields);
7707 constructor_unfilled_fields = constructor_fields;
7708 constructor_bit_index = bitsize_zero_node;
7710 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7712 if (TYPE_DOMAIN (constructor_type))
7714 constructor_max_index
7715 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7717 /* Detect non-empty initializations of zero-length arrays. */
7718 if (constructor_max_index == NULL_TREE
7719 && TYPE_SIZE (constructor_type))
7720 constructor_max_index = integer_minus_one_node;
7722 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7723 to initialize VLAs will cause a proper error; avoid tree
7724 checking errors as well by setting a safe value. */
7725 if (constructor_max_index
7726 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7727 constructor_max_index = integer_minus_one_node;
7729 constructor_index
7730 = convert (bitsizetype,
7731 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7733 else
7735 constructor_index = bitsize_zero_node;
7736 constructor_max_index = NULL_TREE;
7739 constructor_unfilled_index = constructor_index;
7741 else if (VECTOR_TYPE_P (constructor_type))
7743 /* Vectors are like simple fixed-size arrays. */
7744 constructor_max_index =
7745 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7746 constructor_index = bitsize_zero_node;
7747 constructor_unfilled_index = constructor_index;
7749 else
7751 /* Handle the case of int x = {5}; */
7752 constructor_fields = constructor_type;
7753 constructor_unfilled_fields = constructor_type;
7757 extern location_t last_init_list_comma;
7759 /* Called when we see an open brace for a nested initializer. Finish
7760 off any pending levels with implicit braces. */
7761 void
7762 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7764 while (constructor_stack->implicit)
7766 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7767 && constructor_fields == 0)
7768 process_init_element (input_location,
7769 pop_init_level (loc, 1, braced_init_obstack,
7770 last_init_list_comma),
7771 true, braced_init_obstack);
7772 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7773 && constructor_max_index
7774 && tree_int_cst_lt (constructor_max_index,
7775 constructor_index))
7776 process_init_element (input_location,
7777 pop_init_level (loc, 1, braced_init_obstack,
7778 last_init_list_comma),
7779 true, braced_init_obstack);
7780 else
7781 break;
7785 /* Push down into a subobject, for initialization.
7786 If this is for an explicit set of braces, IMPLICIT is 0.
7787 If it is because the next element belongs at a lower level,
7788 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7790 void
7791 push_init_level (location_t loc, int implicit,
7792 struct obstack *braced_init_obstack)
7794 struct constructor_stack *p;
7795 tree value = NULL_TREE;
7797 /* Unless this is an explicit brace, we need to preserve previous
7798 content if any. */
7799 if (implicit)
7801 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7802 value = find_init_member (constructor_fields, braced_init_obstack);
7803 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7804 value = find_init_member (constructor_index, braced_init_obstack);
7807 p = XNEW (struct constructor_stack);
7808 p->type = constructor_type;
7809 p->fields = constructor_fields;
7810 p->index = constructor_index;
7811 p->max_index = constructor_max_index;
7812 p->unfilled_index = constructor_unfilled_index;
7813 p->unfilled_fields = constructor_unfilled_fields;
7814 p->bit_index = constructor_bit_index;
7815 p->elements = constructor_elements;
7816 p->constant = constructor_constant;
7817 p->simple = constructor_simple;
7818 p->nonconst = constructor_nonconst;
7819 p->erroneous = constructor_erroneous;
7820 p->pending_elts = constructor_pending_elts;
7821 p->depth = constructor_depth;
7822 p->replacement_value.value = 0;
7823 p->replacement_value.original_code = ERROR_MARK;
7824 p->replacement_value.original_type = NULL;
7825 p->implicit = implicit;
7826 p->outer = 0;
7827 p->incremental = constructor_incremental;
7828 p->designated = constructor_designated;
7829 p->designator_depth = designator_depth;
7830 p->next = constructor_stack;
7831 p->range_stack = 0;
7832 constructor_stack = p;
7834 constructor_constant = 1;
7835 constructor_simple = 1;
7836 constructor_nonconst = 0;
7837 constructor_depth = SPELLING_DEPTH ();
7838 constructor_elements = NULL;
7839 constructor_incremental = 1;
7840 constructor_designated = 0;
7841 constructor_pending_elts = 0;
7842 if (!implicit)
7844 p->range_stack = constructor_range_stack;
7845 constructor_range_stack = 0;
7846 designator_depth = 0;
7847 designator_erroneous = 0;
7850 /* Don't die if an entire brace-pair level is superfluous
7851 in the containing level. */
7852 if (constructor_type == 0)
7854 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7856 /* Don't die if there are extra init elts at the end. */
7857 if (constructor_fields == 0)
7858 constructor_type = 0;
7859 else
7861 constructor_type = TREE_TYPE (constructor_fields);
7862 push_member_name (constructor_fields);
7863 constructor_depth++;
7865 /* If upper initializer is designated, then mark this as
7866 designated too to prevent bogus warnings. */
7867 constructor_designated = p->designated;
7869 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7871 constructor_type = TREE_TYPE (constructor_type);
7872 push_array_bounds (tree_to_uhwi (constructor_index));
7873 constructor_depth++;
7876 if (constructor_type == 0)
7878 error_init (loc, "extra brace group at end of initializer");
7879 constructor_fields = 0;
7880 constructor_unfilled_fields = 0;
7881 return;
7884 if (value && TREE_CODE (value) == CONSTRUCTOR)
7886 constructor_constant = TREE_CONSTANT (value);
7887 constructor_simple = TREE_STATIC (value);
7888 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7889 constructor_elements = CONSTRUCTOR_ELTS (value);
7890 if (!vec_safe_is_empty (constructor_elements)
7891 && (TREE_CODE (constructor_type) == RECORD_TYPE
7892 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7893 set_nonincremental_init (braced_init_obstack);
7896 if (implicit == 1)
7898 found_missing_braces = 1;
7899 if (initializer_stack->missing_brace_richloc)
7900 initializer_stack->missing_brace_richloc->add_fixit_insert_before
7901 (loc, "{");
7904 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7906 constructor_fields = TYPE_FIELDS (constructor_type);
7907 /* Skip any nameless bit fields at the beginning. */
7908 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7909 && DECL_NAME (constructor_fields) == 0)
7910 constructor_fields = DECL_CHAIN (constructor_fields);
7912 constructor_unfilled_fields = constructor_fields;
7913 constructor_bit_index = bitsize_zero_node;
7915 else if (VECTOR_TYPE_P (constructor_type))
7917 /* Vectors are like simple fixed-size arrays. */
7918 constructor_max_index =
7919 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7920 constructor_index = bitsize_int (0);
7921 constructor_unfilled_index = constructor_index;
7923 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7925 if (TYPE_DOMAIN (constructor_type))
7927 constructor_max_index
7928 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7930 /* Detect non-empty initializations of zero-length arrays. */
7931 if (constructor_max_index == NULL_TREE
7932 && TYPE_SIZE (constructor_type))
7933 constructor_max_index = integer_minus_one_node;
7935 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7936 to initialize VLAs will cause a proper error; avoid tree
7937 checking errors as well by setting a safe value. */
7938 if (constructor_max_index
7939 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7940 constructor_max_index = integer_minus_one_node;
7942 constructor_index
7943 = convert (bitsizetype,
7944 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7946 else
7947 constructor_index = bitsize_zero_node;
7949 constructor_unfilled_index = constructor_index;
7950 if (value && TREE_CODE (value) == STRING_CST)
7952 /* We need to split the char/wchar array into individual
7953 characters, so that we don't have to special case it
7954 everywhere. */
7955 set_nonincremental_init_from_string (value, braced_init_obstack);
7958 else
7960 if (constructor_type != error_mark_node)
7961 warning_init (input_location, 0, "braces around scalar initializer");
7962 constructor_fields = constructor_type;
7963 constructor_unfilled_fields = constructor_type;
7967 /* At the end of an implicit or explicit brace level,
7968 finish up that level of constructor. If a single expression
7969 with redundant braces initialized that level, return the
7970 c_expr structure for that expression. Otherwise, the original_code
7971 element is set to ERROR_MARK.
7972 If we were outputting the elements as they are read, return 0 as the value
7973 from inner levels (process_init_element ignores that),
7974 but return error_mark_node as the value from the outermost level
7975 (that's what we want to put in DECL_INITIAL).
7976 Otherwise, return a CONSTRUCTOR expression as the value. */
7978 struct c_expr
7979 pop_init_level (location_t loc, int implicit,
7980 struct obstack *braced_init_obstack,
7981 location_t insert_before)
7983 struct constructor_stack *p;
7984 struct c_expr ret;
7985 ret.value = 0;
7986 ret.original_code = ERROR_MARK;
7987 ret.original_type = NULL;
7989 if (implicit == 0)
7991 /* When we come to an explicit close brace,
7992 pop any inner levels that didn't have explicit braces. */
7993 while (constructor_stack->implicit)
7994 process_init_element (input_location,
7995 pop_init_level (loc, 1, braced_init_obstack,
7996 insert_before),
7997 true, braced_init_obstack);
7998 gcc_assert (!constructor_range_stack);
8000 else
8001 if (initializer_stack->missing_brace_richloc)
8002 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8003 (insert_before, "}");
8005 /* Now output all pending elements. */
8006 constructor_incremental = 1;
8007 output_pending_init_elements (1, braced_init_obstack);
8009 p = constructor_stack;
8011 /* Error for initializing a flexible array member, or a zero-length
8012 array member in an inappropriate context. */
8013 if (constructor_type && constructor_fields
8014 && TREE_CODE (constructor_type) == ARRAY_TYPE
8015 && TYPE_DOMAIN (constructor_type)
8016 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8018 /* Silently discard empty initializations. The parser will
8019 already have pedwarned for empty brackets. */
8020 if (integer_zerop (constructor_unfilled_index))
8021 constructor_type = NULL_TREE;
8022 else
8024 gcc_assert (!TYPE_SIZE (constructor_type));
8026 if (constructor_depth > 2)
8027 error_init (loc, "initialization of flexible array member in a nested context");
8028 else
8029 pedwarn_init (loc, OPT_Wpedantic,
8030 "initialization of a flexible array member");
8032 /* We have already issued an error message for the existence
8033 of a flexible array member not at the end of the structure.
8034 Discard the initializer so that we do not die later. */
8035 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8036 constructor_type = NULL_TREE;
8040 switch (vec_safe_length (constructor_elements))
8042 case 0:
8043 /* Initialization with { } counts as zeroinit. */
8044 constructor_zeroinit = 1;
8045 break;
8046 case 1:
8047 /* This might be zeroinit as well. */
8048 if (integer_zerop ((*constructor_elements)[0].value))
8049 constructor_zeroinit = 1;
8050 break;
8051 default:
8052 /* If the constructor has more than one element, it can't be { 0 }. */
8053 constructor_zeroinit = 0;
8054 break;
8057 /* Warn when some structs are initialized with direct aggregation. */
8058 if (!implicit && found_missing_braces && warn_missing_braces
8059 && !constructor_zeroinit)
8061 gcc_assert (initializer_stack->missing_brace_richloc);
8062 warning_at_rich_loc (initializer_stack->missing_brace_richloc,
8063 OPT_Wmissing_braces,
8064 "missing braces around initializer");
8067 /* Warn when some struct elements are implicitly initialized to zero. */
8068 if (warn_missing_field_initializers
8069 && constructor_type
8070 && TREE_CODE (constructor_type) == RECORD_TYPE
8071 && constructor_unfilled_fields)
8073 /* Do not warn for flexible array members or zero-length arrays. */
8074 while (constructor_unfilled_fields
8075 && (!DECL_SIZE (constructor_unfilled_fields)
8076 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8077 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8079 if (constructor_unfilled_fields
8080 /* Do not warn if this level of the initializer uses member
8081 designators; it is likely to be deliberate. */
8082 && !constructor_designated
8083 /* Do not warn about initializing with { 0 } or with { }. */
8084 && !constructor_zeroinit)
8086 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8087 "missing initializer for field %qD of %qT",
8088 constructor_unfilled_fields,
8089 constructor_type))
8090 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8091 "%qD declared here", constructor_unfilled_fields);
8095 /* Pad out the end of the structure. */
8096 if (p->replacement_value.value)
8097 /* If this closes a superfluous brace pair,
8098 just pass out the element between them. */
8099 ret = p->replacement_value;
8100 else if (constructor_type == 0)
8102 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8103 && TREE_CODE (constructor_type) != ARRAY_TYPE
8104 && !VECTOR_TYPE_P (constructor_type))
8106 /* A nonincremental scalar initializer--just return
8107 the element, after verifying there is just one. */
8108 if (vec_safe_is_empty (constructor_elements))
8110 if (!constructor_erroneous)
8111 error_init (loc, "empty scalar initializer");
8112 ret.value = error_mark_node;
8114 else if (vec_safe_length (constructor_elements) != 1)
8116 error_init (loc, "extra elements in scalar initializer");
8117 ret.value = (*constructor_elements)[0].value;
8119 else
8120 ret.value = (*constructor_elements)[0].value;
8122 else
8124 if (constructor_erroneous)
8125 ret.value = error_mark_node;
8126 else
8128 ret.value = build_constructor (constructor_type,
8129 constructor_elements);
8130 if (constructor_constant)
8131 TREE_CONSTANT (ret.value) = 1;
8132 if (constructor_constant && constructor_simple)
8133 TREE_STATIC (ret.value) = 1;
8134 if (constructor_nonconst)
8135 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8139 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8141 if (constructor_nonconst)
8142 ret.original_code = C_MAYBE_CONST_EXPR;
8143 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8144 ret.original_code = ERROR_MARK;
8147 constructor_type = p->type;
8148 constructor_fields = p->fields;
8149 constructor_index = p->index;
8150 constructor_max_index = p->max_index;
8151 constructor_unfilled_index = p->unfilled_index;
8152 constructor_unfilled_fields = p->unfilled_fields;
8153 constructor_bit_index = p->bit_index;
8154 constructor_elements = p->elements;
8155 constructor_constant = p->constant;
8156 constructor_simple = p->simple;
8157 constructor_nonconst = p->nonconst;
8158 constructor_erroneous = p->erroneous;
8159 constructor_incremental = p->incremental;
8160 constructor_designated = p->designated;
8161 designator_depth = p->designator_depth;
8162 constructor_pending_elts = p->pending_elts;
8163 constructor_depth = p->depth;
8164 if (!p->implicit)
8165 constructor_range_stack = p->range_stack;
8166 RESTORE_SPELLING_DEPTH (constructor_depth);
8168 constructor_stack = p->next;
8169 free (p);
8171 if (ret.value == 0 && constructor_stack == 0)
8172 ret.value = error_mark_node;
8173 return ret;
8176 /* Common handling for both array range and field name designators.
8177 ARRAY argument is nonzero for array ranges. Returns zero for success. */
8179 static int
8180 set_designator (location_t loc, int array,
8181 struct obstack *braced_init_obstack)
8183 tree subtype;
8184 enum tree_code subcode;
8186 /* Don't die if an entire brace-pair level is superfluous
8187 in the containing level. */
8188 if (constructor_type == 0)
8189 return 1;
8191 /* If there were errors in this designator list already, bail out
8192 silently. */
8193 if (designator_erroneous)
8194 return 1;
8196 if (!designator_depth)
8198 gcc_assert (!constructor_range_stack);
8200 /* Designator list starts at the level of closest explicit
8201 braces. */
8202 while (constructor_stack->implicit)
8203 process_init_element (input_location,
8204 pop_init_level (loc, 1, braced_init_obstack,
8205 last_init_list_comma),
8206 true, braced_init_obstack);
8207 constructor_designated = 1;
8208 return 0;
8211 switch (TREE_CODE (constructor_type))
8213 case RECORD_TYPE:
8214 case UNION_TYPE:
8215 subtype = TREE_TYPE (constructor_fields);
8216 if (subtype != error_mark_node)
8217 subtype = TYPE_MAIN_VARIANT (subtype);
8218 break;
8219 case ARRAY_TYPE:
8220 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8221 break;
8222 default:
8223 gcc_unreachable ();
8226 subcode = TREE_CODE (subtype);
8227 if (array && subcode != ARRAY_TYPE)
8229 error_init (loc, "array index in non-array initializer");
8230 return 1;
8232 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8234 error_init (loc, "field name not in record or union initializer");
8235 return 1;
8238 constructor_designated = 1;
8239 finish_implicit_inits (loc, braced_init_obstack);
8240 push_init_level (loc, 2, braced_init_obstack);
8241 return 0;
8244 /* If there are range designators in designator list, push a new designator
8245 to constructor_range_stack. RANGE_END is end of such stack range or
8246 NULL_TREE if there is no range designator at this level. */
8248 static void
8249 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8251 struct constructor_range_stack *p;
8253 p = (struct constructor_range_stack *)
8254 obstack_alloc (braced_init_obstack,
8255 sizeof (struct constructor_range_stack));
8256 p->prev = constructor_range_stack;
8257 p->next = 0;
8258 p->fields = constructor_fields;
8259 p->range_start = constructor_index;
8260 p->index = constructor_index;
8261 p->stack = constructor_stack;
8262 p->range_end = range_end;
8263 if (constructor_range_stack)
8264 constructor_range_stack->next = p;
8265 constructor_range_stack = p;
8268 /* Within an array initializer, specify the next index to be initialized.
8269 FIRST is that index. If LAST is nonzero, then initialize a range
8270 of indices, running from FIRST through LAST. */
8272 void
8273 set_init_index (location_t loc, tree first, tree last,
8274 struct obstack *braced_init_obstack)
8276 if (set_designator (loc, 1, braced_init_obstack))
8277 return;
8279 designator_erroneous = 1;
8281 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8282 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8284 error_init (loc, "array index in initializer not of integer type");
8285 return;
8288 if (TREE_CODE (first) != INTEGER_CST)
8290 first = c_fully_fold (first, false, NULL);
8291 if (TREE_CODE (first) == INTEGER_CST)
8292 pedwarn_init (loc, OPT_Wpedantic,
8293 "array index in initializer is not "
8294 "an integer constant expression");
8297 if (last && TREE_CODE (last) != INTEGER_CST)
8299 last = c_fully_fold (last, false, NULL);
8300 if (TREE_CODE (last) == INTEGER_CST)
8301 pedwarn_init (loc, OPT_Wpedantic,
8302 "array index in initializer is not "
8303 "an integer constant expression");
8306 if (TREE_CODE (first) != INTEGER_CST)
8307 error_init (loc, "nonconstant array index in initializer");
8308 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
8309 error_init (loc, "nonconstant array index in initializer");
8310 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8311 error_init (loc, "array index in non-array initializer");
8312 else if (tree_int_cst_sgn (first) == -1)
8313 error_init (loc, "array index in initializer exceeds array bounds");
8314 else if (constructor_max_index
8315 && tree_int_cst_lt (constructor_max_index, first))
8316 error_init (loc, "array index in initializer exceeds array bounds");
8317 else
8319 constant_expression_warning (first);
8320 if (last)
8321 constant_expression_warning (last);
8322 constructor_index = convert (bitsizetype, first);
8323 if (tree_int_cst_lt (constructor_index, first))
8325 constructor_index = copy_node (constructor_index);
8326 TREE_OVERFLOW (constructor_index) = 1;
8329 if (last)
8331 if (tree_int_cst_equal (first, last))
8332 last = 0;
8333 else if (tree_int_cst_lt (last, first))
8335 error_init (loc, "empty index range in initializer");
8336 last = 0;
8338 else
8340 last = convert (bitsizetype, last);
8341 if (constructor_max_index != 0
8342 && tree_int_cst_lt (constructor_max_index, last))
8344 error_init (loc, "array index range in initializer exceeds "
8345 "array bounds");
8346 last = 0;
8351 designator_depth++;
8352 designator_erroneous = 0;
8353 if (constructor_range_stack || last)
8354 push_range_stack (last, braced_init_obstack);
8358 /* Within a struct initializer, specify the next field to be initialized. */
8360 void
8361 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8362 struct obstack *braced_init_obstack)
8364 tree field;
8366 if (set_designator (loc, 0, braced_init_obstack))
8367 return;
8369 designator_erroneous = 1;
8371 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8373 error_init (loc, "field name not in record or union initializer");
8374 return;
8377 field = lookup_field (constructor_type, fieldname);
8379 if (field == 0)
8381 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8382 if (guessed_id)
8384 gcc_rich_location rich_loc (fieldname_loc);
8385 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8386 error_at_rich_loc
8387 (&rich_loc,
8388 "%qT has no member named %qE; did you mean %qE?",
8389 constructor_type, fieldname, guessed_id);
8391 else
8392 error_at (fieldname_loc, "%qT has no member named %qE",
8393 constructor_type, fieldname);
8395 else
8398 constructor_fields = TREE_VALUE (field);
8399 designator_depth++;
8400 designator_erroneous = 0;
8401 if (constructor_range_stack)
8402 push_range_stack (NULL_TREE, braced_init_obstack);
8403 field = TREE_CHAIN (field);
8404 if (field)
8406 if (set_designator (loc, 0, braced_init_obstack))
8407 return;
8410 while (field != NULL_TREE);
8413 /* Add a new initializer to the tree of pending initializers. PURPOSE
8414 identifies the initializer, either array index or field in a structure.
8415 VALUE is the value of that index or field. If ORIGTYPE is not
8416 NULL_TREE, it is the original type of VALUE.
8418 IMPLICIT is true if value comes from pop_init_level (1),
8419 the new initializer has been merged with the existing one
8420 and thus no warnings should be emitted about overriding an
8421 existing initializer. */
8423 static void
8424 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8425 bool implicit, struct obstack *braced_init_obstack)
8427 struct init_node *p, **q, *r;
8429 q = &constructor_pending_elts;
8430 p = 0;
8432 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8434 while (*q != 0)
8436 p = *q;
8437 if (tree_int_cst_lt (purpose, p->purpose))
8438 q = &p->left;
8439 else if (tree_int_cst_lt (p->purpose, purpose))
8440 q = &p->right;
8441 else
8443 if (!implicit)
8445 if (TREE_SIDE_EFFECTS (p->value))
8446 warning_init (loc, OPT_Woverride_init_side_effects,
8447 "initialized field with side-effects "
8448 "overwritten");
8449 else if (warn_override_init)
8450 warning_init (loc, OPT_Woverride_init,
8451 "initialized field overwritten");
8453 p->value = value;
8454 p->origtype = origtype;
8455 return;
8459 else
8461 tree bitpos;
8463 bitpos = bit_position (purpose);
8464 while (*q != NULL)
8466 p = *q;
8467 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8468 q = &p->left;
8469 else if (p->purpose != purpose)
8470 q = &p->right;
8471 else
8473 if (!implicit)
8475 if (TREE_SIDE_EFFECTS (p->value))
8476 warning_init (loc, OPT_Woverride_init_side_effects,
8477 "initialized field with side-effects "
8478 "overwritten");
8479 else if (warn_override_init)
8480 warning_init (loc, OPT_Woverride_init,
8481 "initialized field overwritten");
8483 p->value = value;
8484 p->origtype = origtype;
8485 return;
8490 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8491 sizeof (struct init_node));
8492 r->purpose = purpose;
8493 r->value = value;
8494 r->origtype = origtype;
8496 *q = r;
8497 r->parent = p;
8498 r->left = 0;
8499 r->right = 0;
8500 r->balance = 0;
8502 while (p)
8504 struct init_node *s;
8506 if (r == p->left)
8508 if (p->balance == 0)
8509 p->balance = -1;
8510 else if (p->balance < 0)
8512 if (r->balance < 0)
8514 /* L rotation. */
8515 p->left = r->right;
8516 if (p->left)
8517 p->left->parent = p;
8518 r->right = p;
8520 p->balance = 0;
8521 r->balance = 0;
8523 s = p->parent;
8524 p->parent = r;
8525 r->parent = s;
8526 if (s)
8528 if (s->left == p)
8529 s->left = r;
8530 else
8531 s->right = r;
8533 else
8534 constructor_pending_elts = r;
8536 else
8538 /* LR rotation. */
8539 struct init_node *t = r->right;
8541 r->right = t->left;
8542 if (r->right)
8543 r->right->parent = r;
8544 t->left = r;
8546 p->left = t->right;
8547 if (p->left)
8548 p->left->parent = p;
8549 t->right = p;
8551 p->balance = t->balance < 0;
8552 r->balance = -(t->balance > 0);
8553 t->balance = 0;
8555 s = p->parent;
8556 p->parent = t;
8557 r->parent = t;
8558 t->parent = s;
8559 if (s)
8561 if (s->left == p)
8562 s->left = t;
8563 else
8564 s->right = t;
8566 else
8567 constructor_pending_elts = t;
8569 break;
8571 else
8573 /* p->balance == +1; growth of left side balances the node. */
8574 p->balance = 0;
8575 break;
8578 else /* r == p->right */
8580 if (p->balance == 0)
8581 /* Growth propagation from right side. */
8582 p->balance++;
8583 else if (p->balance > 0)
8585 if (r->balance > 0)
8587 /* R rotation. */
8588 p->right = r->left;
8589 if (p->right)
8590 p->right->parent = p;
8591 r->left = p;
8593 p->balance = 0;
8594 r->balance = 0;
8596 s = p->parent;
8597 p->parent = r;
8598 r->parent = s;
8599 if (s)
8601 if (s->left == p)
8602 s->left = r;
8603 else
8604 s->right = r;
8606 else
8607 constructor_pending_elts = r;
8609 else /* r->balance == -1 */
8611 /* RL rotation */
8612 struct init_node *t = r->left;
8614 r->left = t->right;
8615 if (r->left)
8616 r->left->parent = r;
8617 t->right = r;
8619 p->right = t->left;
8620 if (p->right)
8621 p->right->parent = p;
8622 t->left = p;
8624 r->balance = (t->balance < 0);
8625 p->balance = -(t->balance > 0);
8626 t->balance = 0;
8628 s = p->parent;
8629 p->parent = t;
8630 r->parent = t;
8631 t->parent = s;
8632 if (s)
8634 if (s->left == p)
8635 s->left = t;
8636 else
8637 s->right = t;
8639 else
8640 constructor_pending_elts = t;
8642 break;
8644 else
8646 /* p->balance == -1; growth of right side balances the node. */
8647 p->balance = 0;
8648 break;
8652 r = p;
8653 p = p->parent;
8657 /* Build AVL tree from a sorted chain. */
8659 static void
8660 set_nonincremental_init (struct obstack * braced_init_obstack)
8662 unsigned HOST_WIDE_INT ix;
8663 tree index, value;
8665 if (TREE_CODE (constructor_type) != RECORD_TYPE
8666 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8667 return;
8669 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8670 add_pending_init (input_location, index, value, NULL_TREE, true,
8671 braced_init_obstack);
8672 constructor_elements = NULL;
8673 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8675 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8676 /* Skip any nameless bit fields at the beginning. */
8677 while (constructor_unfilled_fields != 0
8678 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8679 && DECL_NAME (constructor_unfilled_fields) == 0)
8680 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8683 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8685 if (TYPE_DOMAIN (constructor_type))
8686 constructor_unfilled_index
8687 = convert (bitsizetype,
8688 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8689 else
8690 constructor_unfilled_index = bitsize_zero_node;
8692 constructor_incremental = 0;
8695 /* Build AVL tree from a string constant. */
8697 static void
8698 set_nonincremental_init_from_string (tree str,
8699 struct obstack * braced_init_obstack)
8701 tree value, purpose, type;
8702 HOST_WIDE_INT val[2];
8703 const char *p, *end;
8704 int byte, wchar_bytes, charwidth, bitpos;
8706 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8708 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8709 charwidth = TYPE_PRECISION (char_type_node);
8710 gcc_assert ((size_t) wchar_bytes * charwidth
8711 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8712 type = TREE_TYPE (constructor_type);
8713 p = TREE_STRING_POINTER (str);
8714 end = p + TREE_STRING_LENGTH (str);
8716 for (purpose = bitsize_zero_node;
8717 p < end
8718 && !(constructor_max_index
8719 && tree_int_cst_lt (constructor_max_index, purpose));
8720 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8722 if (wchar_bytes == 1)
8724 val[0] = (unsigned char) *p++;
8725 val[1] = 0;
8727 else
8729 val[1] = 0;
8730 val[0] = 0;
8731 for (byte = 0; byte < wchar_bytes; byte++)
8733 if (BYTES_BIG_ENDIAN)
8734 bitpos = (wchar_bytes - byte - 1) * charwidth;
8735 else
8736 bitpos = byte * charwidth;
8737 val[bitpos / HOST_BITS_PER_WIDE_INT]
8738 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8739 << (bitpos % HOST_BITS_PER_WIDE_INT);
8743 if (!TYPE_UNSIGNED (type))
8745 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8746 if (bitpos < HOST_BITS_PER_WIDE_INT)
8748 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8750 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8751 val[1] = -1;
8754 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8756 if (val[0] < 0)
8757 val[1] = -1;
8759 else if (val[1] & (HOST_WIDE_INT_1
8760 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8761 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8764 value = wide_int_to_tree (type,
8765 wide_int::from_array (val, 2,
8766 HOST_BITS_PER_WIDE_INT * 2));
8767 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8768 braced_init_obstack);
8771 constructor_incremental = 0;
8774 /* Return value of FIELD in pending initializer or zero if the field was
8775 not initialized yet. */
8777 static tree
8778 find_init_member (tree field, struct obstack * braced_init_obstack)
8780 struct init_node *p;
8782 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8784 if (constructor_incremental
8785 && tree_int_cst_lt (field, constructor_unfilled_index))
8786 set_nonincremental_init (braced_init_obstack);
8788 p = constructor_pending_elts;
8789 while (p)
8791 if (tree_int_cst_lt (field, p->purpose))
8792 p = p->left;
8793 else if (tree_int_cst_lt (p->purpose, field))
8794 p = p->right;
8795 else
8796 return p->value;
8799 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8801 tree bitpos = bit_position (field);
8803 if (constructor_incremental
8804 && (!constructor_unfilled_fields
8805 || tree_int_cst_lt (bitpos,
8806 bit_position (constructor_unfilled_fields))))
8807 set_nonincremental_init (braced_init_obstack);
8809 p = constructor_pending_elts;
8810 while (p)
8812 if (field == p->purpose)
8813 return p->value;
8814 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8815 p = p->left;
8816 else
8817 p = p->right;
8820 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8822 if (!vec_safe_is_empty (constructor_elements)
8823 && (constructor_elements->last ().index == field))
8824 return constructor_elements->last ().value;
8826 return 0;
8829 /* "Output" the next constructor element.
8830 At top level, really output it to assembler code now.
8831 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8832 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8833 TYPE is the data type that the containing data type wants here.
8834 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8835 If VALUE is a string constant, STRICT_STRING is true if it is
8836 unparenthesized or we should not warn here for it being parenthesized.
8837 For other types of VALUE, STRICT_STRING is not used.
8839 PENDING if non-nil means output pending elements that belong
8840 right after this element. (PENDING is normally 1;
8841 it is 0 while outputting pending elements, to avoid recursion.)
8843 IMPLICIT is true if value comes from pop_init_level (1),
8844 the new initializer has been merged with the existing one
8845 and thus no warnings should be emitted about overriding an
8846 existing initializer. */
8848 static void
8849 output_init_element (location_t loc, tree value, tree origtype,
8850 bool strict_string, tree type, tree field, int pending,
8851 bool implicit, struct obstack * braced_init_obstack)
8853 tree semantic_type = NULL_TREE;
8854 bool maybe_const = true;
8855 bool npc;
8857 if (type == error_mark_node || value == error_mark_node)
8859 constructor_erroneous = 1;
8860 return;
8862 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8863 && (TREE_CODE (value) == STRING_CST
8864 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8865 && !(TREE_CODE (value) == STRING_CST
8866 && TREE_CODE (type) == ARRAY_TYPE
8867 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8868 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8869 TYPE_MAIN_VARIANT (type)))
8870 value = array_to_pointer_conversion (input_location, value);
8872 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8873 && require_constant_value && pending)
8875 /* As an extension, allow initializing objects with static storage
8876 duration with compound literals (which are then treated just as
8877 the brace enclosed list they contain). */
8878 if (flag_isoc99)
8879 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8880 "constant");
8881 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8882 value = DECL_INITIAL (decl);
8885 npc = null_pointer_constant_p (value);
8886 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8888 semantic_type = TREE_TYPE (value);
8889 value = TREE_OPERAND (value, 0);
8891 value = c_fully_fold (value, require_constant_value, &maybe_const);
8893 if (value == error_mark_node)
8894 constructor_erroneous = 1;
8895 else if (!TREE_CONSTANT (value))
8896 constructor_constant = 0;
8897 else if (!initializer_constant_valid_p (value,
8898 TREE_TYPE (value),
8899 AGGREGATE_TYPE_P (constructor_type)
8900 && TYPE_REVERSE_STORAGE_ORDER
8901 (constructor_type))
8902 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8903 && DECL_C_BIT_FIELD (field)
8904 && TREE_CODE (value) != INTEGER_CST))
8905 constructor_simple = 0;
8906 if (!maybe_const)
8907 constructor_nonconst = 1;
8909 /* Digest the initializer and issue any errors about incompatible
8910 types before issuing errors about non-constant initializers. */
8911 tree new_value = value;
8912 if (semantic_type)
8913 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8914 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8915 require_constant_value);
8916 if (new_value == error_mark_node)
8918 constructor_erroneous = 1;
8919 return;
8921 if (require_constant_value || require_constant_elements)
8922 constant_expression_warning (new_value);
8924 /* Proceed to check the constness of the original initializer. */
8925 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8927 if (require_constant_value)
8929 error_init (loc, "initializer element is not constant");
8930 value = error_mark_node;
8932 else if (require_constant_elements)
8933 pedwarn (loc, OPT_Wpedantic,
8934 "initializer element is not computable at load time");
8936 else if (!maybe_const
8937 && (require_constant_value || require_constant_elements))
8938 pedwarn_init (loc, OPT_Wpedantic,
8939 "initializer element is not a constant expression");
8941 /* Issue -Wc++-compat warnings about initializing a bitfield with
8942 enum type. */
8943 if (warn_cxx_compat
8944 && field != NULL_TREE
8945 && TREE_CODE (field) == FIELD_DECL
8946 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8947 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8948 != TYPE_MAIN_VARIANT (type))
8949 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8951 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8952 if (checktype != error_mark_node
8953 && (TYPE_MAIN_VARIANT (checktype)
8954 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8955 warning_init (loc, OPT_Wc___compat,
8956 "enum conversion in initialization is invalid in C++");
8959 /* If this field is empty (and not at the end of structure),
8960 don't do anything other than checking the initializer. */
8961 if (field
8962 && (TREE_TYPE (field) == error_mark_node
8963 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8964 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8965 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8966 || DECL_CHAIN (field)))))
8967 return;
8969 /* Finally, set VALUE to the initializer value digested above. */
8970 value = new_value;
8972 /* If this element doesn't come next in sequence,
8973 put it on constructor_pending_elts. */
8974 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8975 && (!constructor_incremental
8976 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8978 if (constructor_incremental
8979 && tree_int_cst_lt (field, constructor_unfilled_index))
8980 set_nonincremental_init (braced_init_obstack);
8982 add_pending_init (loc, field, value, origtype, implicit,
8983 braced_init_obstack);
8984 return;
8986 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8987 && (!constructor_incremental
8988 || field != constructor_unfilled_fields))
8990 /* We do this for records but not for unions. In a union,
8991 no matter which field is specified, it can be initialized
8992 right away since it starts at the beginning of the union. */
8993 if (constructor_incremental)
8995 if (!constructor_unfilled_fields)
8996 set_nonincremental_init (braced_init_obstack);
8997 else
8999 tree bitpos, unfillpos;
9001 bitpos = bit_position (field);
9002 unfillpos = bit_position (constructor_unfilled_fields);
9004 if (tree_int_cst_lt (bitpos, unfillpos))
9005 set_nonincremental_init (braced_init_obstack);
9009 add_pending_init (loc, field, value, origtype, implicit,
9010 braced_init_obstack);
9011 return;
9013 else if (TREE_CODE (constructor_type) == UNION_TYPE
9014 && !vec_safe_is_empty (constructor_elements))
9016 if (!implicit)
9018 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9019 warning_init (loc, OPT_Woverride_init_side_effects,
9020 "initialized field with side-effects overwritten");
9021 else if (warn_override_init)
9022 warning_init (loc, OPT_Woverride_init,
9023 "initialized field overwritten");
9026 /* We can have just one union field set. */
9027 constructor_elements = NULL;
9030 /* Otherwise, output this element either to
9031 constructor_elements or to the assembler file. */
9033 constructor_elt celt = {field, value};
9034 vec_safe_push (constructor_elements, celt);
9036 /* Advance the variable that indicates sequential elements output. */
9037 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9038 constructor_unfilled_index
9039 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9040 bitsize_one_node);
9041 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9043 constructor_unfilled_fields
9044 = DECL_CHAIN (constructor_unfilled_fields);
9046 /* Skip any nameless bit fields. */
9047 while (constructor_unfilled_fields != 0
9048 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9049 && DECL_NAME (constructor_unfilled_fields) == 0)
9050 constructor_unfilled_fields =
9051 DECL_CHAIN (constructor_unfilled_fields);
9053 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9054 constructor_unfilled_fields = 0;
9056 /* Now output any pending elements which have become next. */
9057 if (pending)
9058 output_pending_init_elements (0, braced_init_obstack);
9061 /* Output any pending elements which have become next.
9062 As we output elements, constructor_unfilled_{fields,index}
9063 advances, which may cause other elements to become next;
9064 if so, they too are output.
9066 If ALL is 0, we return when there are
9067 no more pending elements to output now.
9069 If ALL is 1, we output space as necessary so that
9070 we can output all the pending elements. */
9071 static void
9072 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9074 struct init_node *elt = constructor_pending_elts;
9075 tree next;
9077 retry:
9079 /* Look through the whole pending tree.
9080 If we find an element that should be output now,
9081 output it. Otherwise, set NEXT to the element
9082 that comes first among those still pending. */
9084 next = 0;
9085 while (elt)
9087 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9089 if (tree_int_cst_equal (elt->purpose,
9090 constructor_unfilled_index))
9091 output_init_element (input_location, elt->value, elt->origtype,
9092 true, TREE_TYPE (constructor_type),
9093 constructor_unfilled_index, 0, false,
9094 braced_init_obstack);
9095 else if (tree_int_cst_lt (constructor_unfilled_index,
9096 elt->purpose))
9098 /* Advance to the next smaller node. */
9099 if (elt->left)
9100 elt = elt->left;
9101 else
9103 /* We have reached the smallest node bigger than the
9104 current unfilled index. Fill the space first. */
9105 next = elt->purpose;
9106 break;
9109 else
9111 /* Advance to the next bigger node. */
9112 if (elt->right)
9113 elt = elt->right;
9114 else
9116 /* We have reached the biggest node in a subtree. Find
9117 the parent of it, which is the next bigger node. */
9118 while (elt->parent && elt->parent->right == elt)
9119 elt = elt->parent;
9120 elt = elt->parent;
9121 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9122 elt->purpose))
9124 next = elt->purpose;
9125 break;
9130 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9132 tree ctor_unfilled_bitpos, elt_bitpos;
9134 /* If the current record is complete we are done. */
9135 if (constructor_unfilled_fields == 0)
9136 break;
9138 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9139 elt_bitpos = bit_position (elt->purpose);
9140 /* We can't compare fields here because there might be empty
9141 fields in between. */
9142 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9144 constructor_unfilled_fields = elt->purpose;
9145 output_init_element (input_location, elt->value, elt->origtype,
9146 true, TREE_TYPE (elt->purpose),
9147 elt->purpose, 0, false,
9148 braced_init_obstack);
9150 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9152 /* Advance to the next smaller node. */
9153 if (elt->left)
9154 elt = elt->left;
9155 else
9157 /* We have reached the smallest node bigger than the
9158 current unfilled field. Fill the space first. */
9159 next = elt->purpose;
9160 break;
9163 else
9165 /* Advance to the next bigger node. */
9166 if (elt->right)
9167 elt = elt->right;
9168 else
9170 /* We have reached the biggest node in a subtree. Find
9171 the parent of it, which is the next bigger node. */
9172 while (elt->parent && elt->parent->right == elt)
9173 elt = elt->parent;
9174 elt = elt->parent;
9175 if (elt
9176 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9177 bit_position (elt->purpose))))
9179 next = elt->purpose;
9180 break;
9187 /* Ordinarily return, but not if we want to output all
9188 and there are elements left. */
9189 if (!(all && next != 0))
9190 return;
9192 /* If it's not incremental, just skip over the gap, so that after
9193 jumping to retry we will output the next successive element. */
9194 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9195 constructor_unfilled_fields = next;
9196 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9197 constructor_unfilled_index = next;
9199 /* ELT now points to the node in the pending tree with the next
9200 initializer to output. */
9201 goto retry;
9204 /* Add one non-braced element to the current constructor level.
9205 This adjusts the current position within the constructor's type.
9206 This may also start or terminate implicit levels
9207 to handle a partly-braced initializer.
9209 Once this has found the correct level for the new element,
9210 it calls output_init_element.
9212 IMPLICIT is true if value comes from pop_init_level (1),
9213 the new initializer has been merged with the existing one
9214 and thus no warnings should be emitted about overriding an
9215 existing initializer. */
9217 void
9218 process_init_element (location_t loc, struct c_expr value, bool implicit,
9219 struct obstack * braced_init_obstack)
9221 tree orig_value = value.value;
9222 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9223 bool strict_string = value.original_code == STRING_CST;
9224 bool was_designated = designator_depth != 0;
9226 designator_depth = 0;
9227 designator_erroneous = 0;
9229 if (!implicit && value.value && !integer_zerop (value.value))
9230 constructor_zeroinit = 0;
9232 /* Handle superfluous braces around string cst as in
9233 char x[] = {"foo"}; */
9234 if (string_flag
9235 && constructor_type
9236 && !was_designated
9237 && TREE_CODE (constructor_type) == ARRAY_TYPE
9238 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9239 && integer_zerop (constructor_unfilled_index))
9241 if (constructor_stack->replacement_value.value)
9242 error_init (loc, "excess elements in char array initializer");
9243 constructor_stack->replacement_value = value;
9244 return;
9247 if (constructor_stack->replacement_value.value != 0)
9249 error_init (loc, "excess elements in struct initializer");
9250 return;
9253 /* Ignore elements of a brace group if it is entirely superfluous
9254 and has already been diagnosed. */
9255 if (constructor_type == 0)
9256 return;
9258 if (!implicit && warn_designated_init && !was_designated
9259 && TREE_CODE (constructor_type) == RECORD_TYPE
9260 && lookup_attribute ("designated_init",
9261 TYPE_ATTRIBUTES (constructor_type)))
9262 warning_init (loc,
9263 OPT_Wdesignated_init,
9264 "positional initialization of field "
9265 "in %<struct%> declared with %<designated_init%> attribute");
9267 /* If we've exhausted any levels that didn't have braces,
9268 pop them now. */
9269 while (constructor_stack->implicit)
9271 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9272 && constructor_fields == 0)
9273 process_init_element (loc,
9274 pop_init_level (loc, 1, braced_init_obstack,
9275 last_init_list_comma),
9276 true, braced_init_obstack);
9277 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9278 || VECTOR_TYPE_P (constructor_type))
9279 && constructor_max_index
9280 && tree_int_cst_lt (constructor_max_index,
9281 constructor_index))
9282 process_init_element (loc,
9283 pop_init_level (loc, 1, braced_init_obstack,
9284 last_init_list_comma),
9285 true, braced_init_obstack);
9286 else
9287 break;
9290 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9291 if (constructor_range_stack)
9293 /* If value is a compound literal and we'll be just using its
9294 content, don't put it into a SAVE_EXPR. */
9295 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9296 || !require_constant_value)
9298 tree semantic_type = NULL_TREE;
9299 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9301 semantic_type = TREE_TYPE (value.value);
9302 value.value = TREE_OPERAND (value.value, 0);
9304 value.value = c_save_expr (value.value);
9305 if (semantic_type)
9306 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9307 value.value);
9311 while (1)
9313 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9315 tree fieldtype;
9316 enum tree_code fieldcode;
9318 if (constructor_fields == 0)
9320 pedwarn_init (loc, 0, "excess elements in struct initializer");
9321 break;
9324 fieldtype = TREE_TYPE (constructor_fields);
9325 if (fieldtype != error_mark_node)
9326 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9327 fieldcode = TREE_CODE (fieldtype);
9329 /* Error for non-static initialization of a flexible array member. */
9330 if (fieldcode == ARRAY_TYPE
9331 && !require_constant_value
9332 && TYPE_SIZE (fieldtype) == NULL_TREE
9333 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9335 error_init (loc, "non-static initialization of a flexible "
9336 "array member");
9337 break;
9340 /* Error for initialization of a flexible array member with
9341 a string constant if the structure is in an array. E.g.:
9342 struct S { int x; char y[]; };
9343 struct S s[] = { { 1, "foo" } };
9344 is invalid. */
9345 if (string_flag
9346 && fieldcode == ARRAY_TYPE
9347 && constructor_depth > 1
9348 && TYPE_SIZE (fieldtype) == NULL_TREE
9349 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9351 bool in_array_p = false;
9352 for (struct constructor_stack *p = constructor_stack;
9353 p && p->type; p = p->next)
9354 if (TREE_CODE (p->type) == ARRAY_TYPE)
9356 in_array_p = true;
9357 break;
9359 if (in_array_p)
9361 error_init (loc, "initialization of flexible array "
9362 "member in a nested context");
9363 break;
9367 /* Accept a string constant to initialize a subarray. */
9368 if (value.value != 0
9369 && fieldcode == ARRAY_TYPE
9370 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9371 && string_flag)
9372 value.value = orig_value;
9373 /* Otherwise, if we have come to a subaggregate,
9374 and we don't have an element of its type, push into it. */
9375 else if (value.value != 0
9376 && value.value != error_mark_node
9377 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9378 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9379 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9381 push_init_level (loc, 1, braced_init_obstack);
9382 continue;
9385 if (value.value)
9387 push_member_name (constructor_fields);
9388 output_init_element (loc, value.value, value.original_type,
9389 strict_string, fieldtype,
9390 constructor_fields, 1, implicit,
9391 braced_init_obstack);
9392 RESTORE_SPELLING_DEPTH (constructor_depth);
9394 else
9395 /* Do the bookkeeping for an element that was
9396 directly output as a constructor. */
9398 /* For a record, keep track of end position of last field. */
9399 if (DECL_SIZE (constructor_fields))
9400 constructor_bit_index
9401 = size_binop_loc (input_location, PLUS_EXPR,
9402 bit_position (constructor_fields),
9403 DECL_SIZE (constructor_fields));
9405 /* If the current field was the first one not yet written out,
9406 it isn't now, so update. */
9407 if (constructor_unfilled_fields == constructor_fields)
9409 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9410 /* Skip any nameless bit fields. */
9411 while (constructor_unfilled_fields != 0
9412 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9413 && DECL_NAME (constructor_unfilled_fields) == 0)
9414 constructor_unfilled_fields =
9415 DECL_CHAIN (constructor_unfilled_fields);
9419 constructor_fields = DECL_CHAIN (constructor_fields);
9420 /* Skip any nameless bit fields at the beginning. */
9421 while (constructor_fields != 0
9422 && DECL_C_BIT_FIELD (constructor_fields)
9423 && DECL_NAME (constructor_fields) == 0)
9424 constructor_fields = DECL_CHAIN (constructor_fields);
9426 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9428 tree fieldtype;
9429 enum tree_code fieldcode;
9431 if (constructor_fields == 0)
9433 pedwarn_init (loc, 0,
9434 "excess elements in union initializer");
9435 break;
9438 fieldtype = TREE_TYPE (constructor_fields);
9439 if (fieldtype != error_mark_node)
9440 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9441 fieldcode = TREE_CODE (fieldtype);
9443 /* Warn that traditional C rejects initialization of unions.
9444 We skip the warning if the value is zero. This is done
9445 under the assumption that the zero initializer in user
9446 code appears conditioned on e.g. __STDC__ to avoid
9447 "missing initializer" warnings and relies on default
9448 initialization to zero in the traditional C case.
9449 We also skip the warning if the initializer is designated,
9450 again on the assumption that this must be conditional on
9451 __STDC__ anyway (and we've already complained about the
9452 member-designator already). */
9453 if (!in_system_header_at (input_location) && !constructor_designated
9454 && !(value.value && (integer_zerop (value.value)
9455 || real_zerop (value.value))))
9456 warning (OPT_Wtraditional, "traditional C rejects initialization "
9457 "of unions");
9459 /* Accept a string constant to initialize a subarray. */
9460 if (value.value != 0
9461 && fieldcode == ARRAY_TYPE
9462 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9463 && string_flag)
9464 value.value = orig_value;
9465 /* Otherwise, if we have come to a subaggregate,
9466 and we don't have an element of its type, push into it. */
9467 else if (value.value != 0
9468 && value.value != error_mark_node
9469 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9470 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9471 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9473 push_init_level (loc, 1, braced_init_obstack);
9474 continue;
9477 if (value.value)
9479 push_member_name (constructor_fields);
9480 output_init_element (loc, value.value, value.original_type,
9481 strict_string, fieldtype,
9482 constructor_fields, 1, implicit,
9483 braced_init_obstack);
9484 RESTORE_SPELLING_DEPTH (constructor_depth);
9486 else
9487 /* Do the bookkeeping for an element that was
9488 directly output as a constructor. */
9490 constructor_bit_index = DECL_SIZE (constructor_fields);
9491 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9494 constructor_fields = 0;
9496 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9498 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9499 enum tree_code eltcode = TREE_CODE (elttype);
9501 /* Accept a string constant to initialize a subarray. */
9502 if (value.value != 0
9503 && eltcode == ARRAY_TYPE
9504 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9505 && string_flag)
9506 value.value = orig_value;
9507 /* Otherwise, if we have come to a subaggregate,
9508 and we don't have an element of its type, push into it. */
9509 else if (value.value != 0
9510 && value.value != error_mark_node
9511 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9512 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9513 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9515 push_init_level (loc, 1, braced_init_obstack);
9516 continue;
9519 if (constructor_max_index != 0
9520 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9521 || integer_all_onesp (constructor_max_index)))
9523 pedwarn_init (loc, 0,
9524 "excess elements in array initializer");
9525 break;
9528 /* Now output the actual element. */
9529 if (value.value)
9531 push_array_bounds (tree_to_uhwi (constructor_index));
9532 output_init_element (loc, value.value, value.original_type,
9533 strict_string, elttype,
9534 constructor_index, 1, implicit,
9535 braced_init_obstack);
9536 RESTORE_SPELLING_DEPTH (constructor_depth);
9539 constructor_index
9540 = size_binop_loc (input_location, PLUS_EXPR,
9541 constructor_index, bitsize_one_node);
9543 if (!value.value)
9544 /* If we are doing the bookkeeping for an element that was
9545 directly output as a constructor, we must update
9546 constructor_unfilled_index. */
9547 constructor_unfilled_index = constructor_index;
9549 else if (VECTOR_TYPE_P (constructor_type))
9551 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9553 /* Do a basic check of initializer size. Note that vectors
9554 always have a fixed size derived from their type. */
9555 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9557 pedwarn_init (loc, 0,
9558 "excess elements in vector initializer");
9559 break;
9562 /* Now output the actual element. */
9563 if (value.value)
9565 if (TREE_CODE (value.value) == VECTOR_CST)
9566 elttype = TYPE_MAIN_VARIANT (constructor_type);
9567 output_init_element (loc, value.value, value.original_type,
9568 strict_string, elttype,
9569 constructor_index, 1, implicit,
9570 braced_init_obstack);
9573 constructor_index
9574 = size_binop_loc (input_location,
9575 PLUS_EXPR, constructor_index, bitsize_one_node);
9577 if (!value.value)
9578 /* If we are doing the bookkeeping for an element that was
9579 directly output as a constructor, we must update
9580 constructor_unfilled_index. */
9581 constructor_unfilled_index = constructor_index;
9584 /* Handle the sole element allowed in a braced initializer
9585 for a scalar variable. */
9586 else if (constructor_type != error_mark_node
9587 && constructor_fields == 0)
9589 pedwarn_init (loc, 0,
9590 "excess elements in scalar initializer");
9591 break;
9593 else
9595 if (value.value)
9596 output_init_element (loc, value.value, value.original_type,
9597 strict_string, constructor_type,
9598 NULL_TREE, 1, implicit,
9599 braced_init_obstack);
9600 constructor_fields = 0;
9603 /* Handle range initializers either at this level or anywhere higher
9604 in the designator stack. */
9605 if (constructor_range_stack)
9607 struct constructor_range_stack *p, *range_stack;
9608 int finish = 0;
9610 range_stack = constructor_range_stack;
9611 constructor_range_stack = 0;
9612 while (constructor_stack != range_stack->stack)
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);
9621 for (p = range_stack;
9622 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9623 p = p->prev)
9625 gcc_assert (constructor_stack->implicit);
9626 process_init_element (loc,
9627 pop_init_level (loc, 1,
9628 braced_init_obstack,
9629 last_init_list_comma),
9630 true, braced_init_obstack);
9633 p->index = size_binop_loc (input_location,
9634 PLUS_EXPR, p->index, bitsize_one_node);
9635 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9636 finish = 1;
9638 while (1)
9640 constructor_index = p->index;
9641 constructor_fields = p->fields;
9642 if (finish && p->range_end && p->index == p->range_start)
9644 finish = 0;
9645 p->prev = 0;
9647 p = p->next;
9648 if (!p)
9649 break;
9650 finish_implicit_inits (loc, braced_init_obstack);
9651 push_init_level (loc, 2, braced_init_obstack);
9652 p->stack = constructor_stack;
9653 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9654 p->index = p->range_start;
9657 if (!finish)
9658 constructor_range_stack = range_stack;
9659 continue;
9662 break;
9665 constructor_range_stack = 0;
9668 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9669 (guaranteed to be 'volatile' or null) and ARGS (represented using
9670 an ASM_EXPR node). */
9671 tree
9672 build_asm_stmt (tree cv_qualifier, tree args)
9674 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9675 ASM_VOLATILE_P (args) = 1;
9676 return add_stmt (args);
9679 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9680 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9681 SIMPLE indicates whether there was anything at all after the
9682 string in the asm expression -- asm("blah") and asm("blah" : )
9683 are subtly different. We use a ASM_EXPR node to represent this. */
9684 tree
9685 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9686 tree clobbers, tree labels, bool simple)
9688 tree tail;
9689 tree args;
9690 int i;
9691 const char *constraint;
9692 const char **oconstraints;
9693 bool allows_mem, allows_reg, is_inout;
9694 int ninputs, noutputs;
9696 ninputs = list_length (inputs);
9697 noutputs = list_length (outputs);
9698 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9700 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9702 /* Remove output conversions that change the type but not the mode. */
9703 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9705 tree output = TREE_VALUE (tail);
9707 output = c_fully_fold (output, false, NULL);
9709 /* ??? Really, this should not be here. Users should be using a
9710 proper lvalue, dammit. But there's a long history of using casts
9711 in the output operands. In cases like longlong.h, this becomes a
9712 primitive form of typechecking -- if the cast can be removed, then
9713 the output operand had a type of the proper width; otherwise we'll
9714 get an error. Gross, but ... */
9715 STRIP_NOPS (output);
9717 if (!lvalue_or_else (loc, output, lv_asm))
9718 output = error_mark_node;
9720 if (output != error_mark_node
9721 && (TREE_READONLY (output)
9722 || TYPE_READONLY (TREE_TYPE (output))
9723 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9724 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9725 readonly_error (loc, output, lv_asm);
9727 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9728 oconstraints[i] = constraint;
9730 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9731 &allows_mem, &allows_reg, &is_inout))
9733 /* If the operand is going to end up in memory,
9734 mark it addressable. */
9735 if (!allows_reg && !c_mark_addressable (output))
9736 output = error_mark_node;
9737 if (!(!allows_reg && allows_mem)
9738 && output != error_mark_node
9739 && VOID_TYPE_P (TREE_TYPE (output)))
9741 error_at (loc, "invalid use of void expression");
9742 output = error_mark_node;
9745 else
9746 output = error_mark_node;
9748 TREE_VALUE (tail) = output;
9751 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9753 tree input;
9755 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9756 input = TREE_VALUE (tail);
9758 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9759 oconstraints, &allows_mem, &allows_reg))
9761 /* If the operand is going to end up in memory,
9762 mark it addressable. */
9763 if (!allows_reg && allows_mem)
9765 input = c_fully_fold (input, false, NULL);
9767 /* Strip the nops as we allow this case. FIXME, this really
9768 should be rejected or made deprecated. */
9769 STRIP_NOPS (input);
9770 if (!c_mark_addressable (input))
9771 input = error_mark_node;
9773 else
9775 struct c_expr expr;
9776 memset (&expr, 0, sizeof (expr));
9777 expr.value = input;
9778 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9779 input = c_fully_fold (expr.value, false, NULL);
9781 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9783 error_at (loc, "invalid use of void expression");
9784 input = error_mark_node;
9788 else
9789 input = error_mark_node;
9791 TREE_VALUE (tail) = input;
9794 /* ASMs with labels cannot have outputs. This should have been
9795 enforced by the parser. */
9796 gcc_assert (outputs == NULL || labels == NULL);
9798 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9800 /* asm statements without outputs, including simple ones, are treated
9801 as volatile. */
9802 ASM_INPUT_P (args) = simple;
9803 ASM_VOLATILE_P (args) = (noutputs == 0);
9805 return args;
9808 /* Generate a goto statement to LABEL. LOC is the location of the
9809 GOTO. */
9811 tree
9812 c_finish_goto_label (location_t loc, tree label)
9814 tree decl = lookup_label_for_goto (loc, label);
9815 if (!decl)
9816 return NULL_TREE;
9817 TREE_USED (decl) = 1;
9819 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9820 SET_EXPR_LOCATION (t, loc);
9821 return add_stmt (t);
9825 /* Generate a computed goto statement to EXPR. LOC is the location of
9826 the GOTO. */
9828 tree
9829 c_finish_goto_ptr (location_t loc, tree expr)
9831 tree t;
9832 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9833 expr = c_fully_fold (expr, false, NULL);
9834 expr = convert (ptr_type_node, expr);
9835 t = build1 (GOTO_EXPR, void_type_node, expr);
9836 SET_EXPR_LOCATION (t, loc);
9837 return add_stmt (t);
9840 /* Generate a C `return' statement. RETVAL is the expression for what
9841 to return, or a null pointer for `return;' with no value. LOC is
9842 the location of the return statement, or the location of the expression,
9843 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9844 is the original type of RETVAL. */
9846 tree
9847 c_finish_return (location_t loc, tree retval, tree origtype)
9849 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9850 bool no_warning = false;
9851 bool npc = false;
9852 size_t rank = 0;
9854 /* Use the expansion point to handle cases such as returning NULL
9855 in a function returning void. */
9856 source_location xloc = expansion_point_location_if_in_system_header (loc);
9858 if (TREE_THIS_VOLATILE (current_function_decl))
9859 warning_at (xloc, 0,
9860 "function declared %<noreturn%> has a %<return%> statement");
9862 if (flag_cilkplus && contains_array_notation_expr (retval))
9864 /* Array notations are allowed in a return statement if it is inside a
9865 built-in array notation reduction function. */
9866 if (!find_rank (loc, retval, retval, false, &rank))
9867 return error_mark_node;
9868 if (rank >= 1)
9870 error_at (loc, "array notation expression cannot be used as a "
9871 "return value");
9872 return error_mark_node;
9875 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9877 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9878 "allowed");
9879 return error_mark_node;
9881 if (retval)
9883 tree semantic_type = NULL_TREE;
9884 npc = null_pointer_constant_p (retval);
9885 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9887 semantic_type = TREE_TYPE (retval);
9888 retval = TREE_OPERAND (retval, 0);
9890 retval = c_fully_fold (retval, false, NULL);
9891 if (semantic_type)
9892 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9895 if (!retval)
9897 current_function_returns_null = 1;
9898 if ((warn_return_type || flag_isoc99)
9899 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9901 bool warned_here;
9902 if (flag_isoc99)
9903 warned_here = pedwarn
9904 (loc, 0,
9905 "%<return%> with no value, in function returning non-void");
9906 else
9907 warned_here = warning_at
9908 (loc, OPT_Wreturn_type,
9909 "%<return%> with no value, in function returning non-void");
9910 no_warning = true;
9911 if (warned_here)
9912 inform (DECL_SOURCE_LOCATION (current_function_decl),
9913 "declared here");
9916 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9918 current_function_returns_null = 1;
9919 bool warned_here;
9920 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9921 warned_here = pedwarn
9922 (xloc, 0,
9923 "%<return%> with a value, in function returning void");
9924 else
9925 warned_here = pedwarn
9926 (xloc, OPT_Wpedantic, "ISO C forbids "
9927 "%<return%> with expression, in function returning void");
9928 if (warned_here)
9929 inform (DECL_SOURCE_LOCATION (current_function_decl),
9930 "declared here");
9932 else
9934 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9935 retval, origtype, ic_return,
9936 npc, NULL_TREE, NULL_TREE, 0);
9937 tree res = DECL_RESULT (current_function_decl);
9938 tree inner;
9939 bool save;
9941 current_function_returns_value = 1;
9942 if (t == error_mark_node)
9943 return NULL_TREE;
9945 save = in_late_binary_op;
9946 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9947 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9948 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9949 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9950 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9951 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9952 in_late_binary_op = true;
9953 inner = t = convert (TREE_TYPE (res), t);
9954 in_late_binary_op = save;
9956 /* Strip any conversions, additions, and subtractions, and see if
9957 we are returning the address of a local variable. Warn if so. */
9958 while (1)
9960 switch (TREE_CODE (inner))
9962 CASE_CONVERT:
9963 case NON_LVALUE_EXPR:
9964 case PLUS_EXPR:
9965 case POINTER_PLUS_EXPR:
9966 inner = TREE_OPERAND (inner, 0);
9967 continue;
9969 case MINUS_EXPR:
9970 /* If the second operand of the MINUS_EXPR has a pointer
9971 type (or is converted from it), this may be valid, so
9972 don't give a warning. */
9974 tree op1 = TREE_OPERAND (inner, 1);
9976 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9977 && (CONVERT_EXPR_P (op1)
9978 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9979 op1 = TREE_OPERAND (op1, 0);
9981 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9982 break;
9984 inner = TREE_OPERAND (inner, 0);
9985 continue;
9988 case ADDR_EXPR:
9989 inner = TREE_OPERAND (inner, 0);
9991 while (REFERENCE_CLASS_P (inner)
9992 && !INDIRECT_REF_P (inner))
9993 inner = TREE_OPERAND (inner, 0);
9995 if (DECL_P (inner)
9996 && !DECL_EXTERNAL (inner)
9997 && !TREE_STATIC (inner)
9998 && DECL_CONTEXT (inner) == current_function_decl)
10000 if (TREE_CODE (inner) == LABEL_DECL)
10001 warning_at (loc, OPT_Wreturn_local_addr,
10002 "function returns address of label");
10003 else
10005 warning_at (loc, OPT_Wreturn_local_addr,
10006 "function returns address of local variable");
10007 tree zero = build_zero_cst (TREE_TYPE (res));
10008 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10011 break;
10013 default:
10014 break;
10017 break;
10020 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10021 SET_EXPR_LOCATION (retval, loc);
10023 if (warn_sequence_point)
10024 verify_sequence_points (retval);
10027 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10028 TREE_NO_WARNING (ret_stmt) |= no_warning;
10029 return add_stmt (ret_stmt);
10032 struct c_switch {
10033 /* The SWITCH_EXPR being built. */
10034 tree switch_expr;
10036 /* The original type of the testing expression, i.e. before the
10037 default conversion is applied. */
10038 tree orig_type;
10040 /* A splay-tree mapping the low element of a case range to the high
10041 element, or NULL_TREE if there is no high element. Used to
10042 determine whether or not a new case label duplicates an old case
10043 label. We need a tree, rather than simply a hash table, because
10044 of the GNU case range extension. */
10045 splay_tree cases;
10047 /* The bindings at the point of the switch. This is used for
10048 warnings crossing decls when branching to a case label. */
10049 struct c_spot_bindings *bindings;
10051 /* The next node on the stack. */
10052 struct c_switch *next;
10054 /* Remember whether the controlling expression had boolean type
10055 before integer promotions for the sake of -Wswitch-bool. */
10056 bool bool_cond_p;
10058 /* Remember whether there was a case value that is outside the
10059 range of the ORIG_TYPE. */
10060 bool outside_range_p;
10063 /* A stack of the currently active switch statements. The innermost
10064 switch statement is on the top of the stack. There is no need to
10065 mark the stack for garbage collection because it is only active
10066 during the processing of the body of a function, and we never
10067 collect at that point. */
10069 struct c_switch *c_switch_stack;
10071 /* Start a C switch statement, testing expression EXP. Return the new
10072 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10073 SWITCH_COND_LOC is the location of the switch's condition.
10074 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10076 tree
10077 c_start_case (location_t switch_loc,
10078 location_t switch_cond_loc,
10079 tree exp, bool explicit_cast_p)
10081 tree orig_type = error_mark_node;
10082 bool bool_cond_p = false;
10083 struct c_switch *cs;
10085 if (exp != error_mark_node)
10087 orig_type = TREE_TYPE (exp);
10089 if (!INTEGRAL_TYPE_P (orig_type))
10091 if (orig_type != error_mark_node)
10093 error_at (switch_cond_loc, "switch quantity not an integer");
10094 orig_type = error_mark_node;
10096 exp = integer_zero_node;
10098 else
10100 tree type = TYPE_MAIN_VARIANT (orig_type);
10101 tree e = exp;
10103 /* Warn if the condition has boolean value. */
10104 while (TREE_CODE (e) == COMPOUND_EXPR)
10105 e = TREE_OPERAND (e, 1);
10107 if ((TREE_CODE (type) == BOOLEAN_TYPE
10108 || truth_value_p (TREE_CODE (e)))
10109 /* Explicit cast to int suppresses this warning. */
10110 && !(TREE_CODE (type) == INTEGER_TYPE
10111 && explicit_cast_p))
10112 bool_cond_p = true;
10114 if (!in_system_header_at (input_location)
10115 && (type == long_integer_type_node
10116 || type == long_unsigned_type_node))
10117 warning_at (switch_cond_loc,
10118 OPT_Wtraditional, "%<long%> switch expression not "
10119 "converted to %<int%> in ISO C");
10121 exp = c_fully_fold (exp, false, NULL);
10122 exp = default_conversion (exp);
10124 if (warn_sequence_point)
10125 verify_sequence_points (exp);
10129 /* Add this new SWITCH_EXPR to the stack. */
10130 cs = XNEW (struct c_switch);
10131 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10132 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10133 cs->orig_type = orig_type;
10134 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10135 cs->bindings = c_get_switch_bindings ();
10136 cs->bool_cond_p = bool_cond_p;
10137 cs->outside_range_p = false;
10138 cs->next = c_switch_stack;
10139 c_switch_stack = cs;
10141 return add_stmt (cs->switch_expr);
10144 /* Process a case label at location LOC. */
10146 tree
10147 do_case (location_t loc, tree low_value, tree high_value)
10149 tree label = NULL_TREE;
10151 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10153 low_value = c_fully_fold (low_value, false, NULL);
10154 if (TREE_CODE (low_value) == INTEGER_CST)
10155 pedwarn (loc, OPT_Wpedantic,
10156 "case label is not an integer constant expression");
10159 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10161 high_value = c_fully_fold (high_value, false, NULL);
10162 if (TREE_CODE (high_value) == INTEGER_CST)
10163 pedwarn (input_location, OPT_Wpedantic,
10164 "case label is not an integer constant expression");
10167 if (c_switch_stack == NULL)
10169 if (low_value)
10170 error_at (loc, "case label not within a switch statement");
10171 else
10172 error_at (loc, "%<default%> label not within a switch statement");
10173 return NULL_TREE;
10176 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10177 EXPR_LOCATION (c_switch_stack->switch_expr),
10178 loc))
10179 return NULL_TREE;
10181 label = c_add_case_label (loc, c_switch_stack->cases,
10182 SWITCH_COND (c_switch_stack->switch_expr),
10183 c_switch_stack->orig_type,
10184 low_value, high_value,
10185 &c_switch_stack->outside_range_p);
10186 if (label == error_mark_node)
10187 label = NULL_TREE;
10188 return label;
10191 /* Finish the switch statement. TYPE is the original type of the
10192 controlling expression of the switch, or NULL_TREE. */
10194 void
10195 c_finish_case (tree body, tree type)
10197 struct c_switch *cs = c_switch_stack;
10198 location_t switch_location;
10200 SWITCH_BODY (cs->switch_expr) = body;
10202 /* Emit warnings as needed. */
10203 switch_location = EXPR_LOCATION (cs->switch_expr);
10204 c_do_switch_warnings (cs->cases, switch_location,
10205 type ? type : TREE_TYPE (cs->switch_expr),
10206 SWITCH_COND (cs->switch_expr),
10207 cs->bool_cond_p, cs->outside_range_p);
10209 /* Pop the stack. */
10210 c_switch_stack = cs->next;
10211 splay_tree_delete (cs->cases);
10212 c_release_switch_bindings (cs->bindings);
10213 XDELETE (cs);
10216 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10217 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10218 may be null. */
10220 void
10221 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10222 tree else_block)
10224 tree stmt;
10226 /* If the condition has array notations, then the rank of the then_block and
10227 else_block must be either 0 or be equal to the rank of the condition. If
10228 the condition does not have array notations then break them up as it is
10229 broken up in a normal expression. */
10230 if (flag_cilkplus && contains_array_notation_expr (cond))
10232 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10233 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10234 return;
10235 if (then_block
10236 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10237 return;
10238 if (else_block
10239 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10240 return;
10241 if (cond_rank != then_rank && then_rank != 0)
10243 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10244 " and the then-block");
10245 return;
10247 else if (cond_rank != else_rank && else_rank != 0)
10249 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10250 " and the else-block");
10251 return;
10255 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10256 SET_EXPR_LOCATION (stmt, if_locus);
10257 add_stmt (stmt);
10260 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10261 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10262 is false for DO loops. INCR is the FOR increment expression. BODY is
10263 the statement controlled by the loop. BLAB is the break label. CLAB is
10264 the continue label. Everything is allowed to be NULL. */
10266 void
10267 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10268 tree blab, tree clab, bool cond_is_first)
10270 tree entry = NULL, exit = NULL, t;
10272 /* In theory could forbid cilk spawn for loop increment expression,
10273 but it should work just fine. */
10275 /* If the condition is zero don't generate a loop construct. */
10276 if (cond && integer_zerop (cond))
10278 if (cond_is_first)
10280 t = build_and_jump (&blab);
10281 SET_EXPR_LOCATION (t, start_locus);
10282 add_stmt (t);
10285 else
10287 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10289 /* If we have an exit condition, then we build an IF with gotos either
10290 out of the loop, or to the top of it. If there's no exit condition,
10291 then we just build a jump back to the top. */
10292 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10294 if (cond && !integer_nonzerop (cond))
10296 /* Canonicalize the loop condition to the end. This means
10297 generating a branch to the loop condition. Reuse the
10298 continue label, if possible. */
10299 if (cond_is_first)
10301 if (incr || !clab)
10303 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10304 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10306 else
10307 t = build1 (GOTO_EXPR, void_type_node, clab);
10308 SET_EXPR_LOCATION (t, start_locus);
10309 add_stmt (t);
10312 t = build_and_jump (&blab);
10313 if (cond_is_first)
10314 exit = fold_build3_loc (start_locus,
10315 COND_EXPR, void_type_node, cond, exit, t);
10316 else
10317 exit = fold_build3_loc (input_location,
10318 COND_EXPR, void_type_node, cond, exit, t);
10320 else
10322 /* For the backward-goto's location of an unconditional loop
10323 use the beginning of the body, or, if there is none, the
10324 top of the loop. */
10325 location_t loc = EXPR_LOCATION (expr_first (body));
10326 if (loc == UNKNOWN_LOCATION)
10327 loc = start_locus;
10328 SET_EXPR_LOCATION (exit, loc);
10331 add_stmt (top);
10334 if (body)
10335 add_stmt (body);
10336 if (clab)
10337 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10338 if (incr)
10339 add_stmt (incr);
10340 if (entry)
10341 add_stmt (entry);
10342 if (exit)
10343 add_stmt (exit);
10344 if (blab)
10345 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10348 tree
10349 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10351 bool skip;
10352 tree label = *label_p;
10354 /* In switch statements break is sometimes stylistically used after
10355 a return statement. This can lead to spurious warnings about
10356 control reaching the end of a non-void function when it is
10357 inlined. Note that we are calling block_may_fallthru with
10358 language specific tree nodes; this works because
10359 block_may_fallthru returns true when given something it does not
10360 understand. */
10361 skip = !block_may_fallthru (cur_stmt_list);
10363 if (!label)
10365 if (!skip)
10366 *label_p = label = create_artificial_label (loc);
10368 else if (TREE_CODE (label) == LABEL_DECL)
10370 else switch (TREE_INT_CST_LOW (label))
10372 case 0:
10373 if (is_break)
10374 error_at (loc, "break statement not within loop or switch");
10375 else
10376 error_at (loc, "continue statement not within a loop");
10377 return NULL_TREE;
10379 case 1:
10380 gcc_assert (is_break);
10381 error_at (loc, "break statement used with OpenMP for loop");
10382 return NULL_TREE;
10384 case 2:
10385 if (is_break)
10386 error ("break statement within %<#pragma simd%> loop body");
10387 else
10388 error ("continue statement within %<#pragma simd%> loop body");
10389 return NULL_TREE;
10391 default:
10392 gcc_unreachable ();
10395 if (skip)
10396 return NULL_TREE;
10398 if (!is_break)
10399 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10401 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10404 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10406 static void
10407 emit_side_effect_warnings (location_t loc, tree expr)
10409 if (expr == error_mark_node)
10411 else if (!TREE_SIDE_EFFECTS (expr))
10413 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10414 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10416 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10418 tree r = expr;
10419 location_t cloc = loc;
10420 while (TREE_CODE (r) == COMPOUND_EXPR)
10422 if (EXPR_HAS_LOCATION (r))
10423 cloc = EXPR_LOCATION (r);
10424 r = TREE_OPERAND (r, 1);
10426 if (!TREE_SIDE_EFFECTS (r)
10427 && !VOID_TYPE_P (TREE_TYPE (r))
10428 && !CONVERT_EXPR_P (r)
10429 && !TREE_NO_WARNING (r)
10430 && !TREE_NO_WARNING (expr))
10431 warning_at (cloc, OPT_Wunused_value,
10432 "right-hand operand of comma expression has no effect");
10434 else
10435 warn_if_unused_value (expr, loc);
10438 /* Process an expression as if it were a complete statement. Emit
10439 diagnostics, but do not call ADD_STMT. LOC is the location of the
10440 statement. */
10442 tree
10443 c_process_expr_stmt (location_t loc, tree expr)
10445 tree exprv;
10447 if (!expr)
10448 return NULL_TREE;
10450 expr = c_fully_fold (expr, false, NULL);
10452 if (warn_sequence_point)
10453 verify_sequence_points (expr);
10455 if (TREE_TYPE (expr) != error_mark_node
10456 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10457 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10458 error_at (loc, "expression statement has incomplete type");
10460 /* If we're not processing a statement expression, warn about unused values.
10461 Warnings for statement expressions will be emitted later, once we figure
10462 out which is the result. */
10463 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10464 && warn_unused_value)
10465 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10467 exprv = expr;
10468 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10469 exprv = TREE_OPERAND (exprv, 1);
10470 while (CONVERT_EXPR_P (exprv))
10471 exprv = TREE_OPERAND (exprv, 0);
10472 if (DECL_P (exprv)
10473 || handled_component_p (exprv)
10474 || TREE_CODE (exprv) == ADDR_EXPR)
10475 mark_exp_read (exprv);
10477 /* If the expression is not of a type to which we cannot assign a line
10478 number, wrap the thing in a no-op NOP_EXPR. */
10479 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10481 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10482 SET_EXPR_LOCATION (expr, loc);
10485 return expr;
10488 /* Emit an expression as a statement. LOC is the location of the
10489 expression. */
10491 tree
10492 c_finish_expr_stmt (location_t loc, tree expr)
10494 if (expr)
10495 return add_stmt (c_process_expr_stmt (loc, expr));
10496 else
10497 return NULL;
10500 /* Do the opposite and emit a statement as an expression. To begin,
10501 create a new binding level and return it. */
10503 tree
10504 c_begin_stmt_expr (void)
10506 tree ret;
10508 /* We must force a BLOCK for this level so that, if it is not expanded
10509 later, there is a way to turn off the entire subtree of blocks that
10510 are contained in it. */
10511 keep_next_level ();
10512 ret = c_begin_compound_stmt (true);
10514 c_bindings_start_stmt_expr (c_switch_stack == NULL
10515 ? NULL
10516 : c_switch_stack->bindings);
10518 /* Mark the current statement list as belonging to a statement list. */
10519 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10521 return ret;
10524 /* LOC is the location of the compound statement to which this body
10525 belongs. */
10527 tree
10528 c_finish_stmt_expr (location_t loc, tree body)
10530 tree last, type, tmp, val;
10531 tree *last_p;
10533 body = c_end_compound_stmt (loc, body, true);
10535 c_bindings_end_stmt_expr (c_switch_stack == NULL
10536 ? NULL
10537 : c_switch_stack->bindings);
10539 /* Locate the last statement in BODY. See c_end_compound_stmt
10540 about always returning a BIND_EXPR. */
10541 last_p = &BIND_EXPR_BODY (body);
10542 last = BIND_EXPR_BODY (body);
10544 continue_searching:
10545 if (TREE_CODE (last) == STATEMENT_LIST)
10547 tree_stmt_iterator i;
10549 /* This can happen with degenerate cases like ({ }). No value. */
10550 if (!TREE_SIDE_EFFECTS (last))
10551 return body;
10553 /* If we're supposed to generate side effects warnings, process
10554 all of the statements except the last. */
10555 if (warn_unused_value)
10557 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10559 location_t tloc;
10560 tree t = tsi_stmt (i);
10562 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10563 emit_side_effect_warnings (tloc, t);
10566 else
10567 i = tsi_last (last);
10568 last_p = tsi_stmt_ptr (i);
10569 last = *last_p;
10572 /* If the end of the list is exception related, then the list was split
10573 by a call to push_cleanup. Continue searching. */
10574 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10575 || TREE_CODE (last) == TRY_CATCH_EXPR)
10577 last_p = &TREE_OPERAND (last, 0);
10578 last = *last_p;
10579 goto continue_searching;
10582 if (last == error_mark_node)
10583 return last;
10585 /* In the case that the BIND_EXPR is not necessary, return the
10586 expression out from inside it. */
10587 if (last == BIND_EXPR_BODY (body)
10588 && BIND_EXPR_VARS (body) == NULL)
10590 /* Even if this looks constant, do not allow it in a constant
10591 expression. */
10592 last = c_wrap_maybe_const (last, true);
10593 /* Do not warn if the return value of a statement expression is
10594 unused. */
10595 TREE_NO_WARNING (last) = 1;
10596 return last;
10599 /* Extract the type of said expression. */
10600 type = TREE_TYPE (last);
10602 /* If we're not returning a value at all, then the BIND_EXPR that
10603 we already have is a fine expression to return. */
10604 if (!type || VOID_TYPE_P (type))
10605 return body;
10607 /* Now that we've located the expression containing the value, it seems
10608 silly to make voidify_wrapper_expr repeat the process. Create a
10609 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10610 tmp = create_tmp_var_raw (type);
10612 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10613 tree_expr_nonnegative_p giving up immediately. */
10614 val = last;
10615 if (TREE_CODE (val) == NOP_EXPR
10616 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10617 val = TREE_OPERAND (val, 0);
10619 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10620 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10623 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10624 SET_EXPR_LOCATION (t, loc);
10625 return t;
10629 /* Begin and end compound statements. This is as simple as pushing
10630 and popping new statement lists from the tree. */
10632 tree
10633 c_begin_compound_stmt (bool do_scope)
10635 tree stmt = push_stmt_list ();
10636 if (do_scope)
10637 push_scope ();
10638 return stmt;
10641 /* End a compound statement. STMT is the statement. LOC is the
10642 location of the compound statement-- this is usually the location
10643 of the opening brace. */
10645 tree
10646 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10648 tree block = NULL;
10650 if (do_scope)
10652 if (c_dialect_objc ())
10653 objc_clear_super_receiver ();
10654 block = pop_scope ();
10657 stmt = pop_stmt_list (stmt);
10658 stmt = c_build_bind_expr (loc, block, stmt);
10660 /* If this compound statement is nested immediately inside a statement
10661 expression, then force a BIND_EXPR to be created. Otherwise we'll
10662 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10663 STATEMENT_LISTs merge, and thus we can lose track of what statement
10664 was really last. */
10665 if (building_stmt_list_p ()
10666 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10667 && TREE_CODE (stmt) != BIND_EXPR)
10669 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10670 TREE_SIDE_EFFECTS (stmt) = 1;
10671 SET_EXPR_LOCATION (stmt, loc);
10674 return stmt;
10677 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10678 when the current scope is exited. EH_ONLY is true when this is not
10679 meant to apply to normal control flow transfer. */
10681 void
10682 push_cleanup (tree decl, tree cleanup, bool eh_only)
10684 enum tree_code code;
10685 tree stmt, list;
10686 bool stmt_expr;
10688 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10689 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10690 add_stmt (stmt);
10691 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10692 list = push_stmt_list ();
10693 TREE_OPERAND (stmt, 0) = list;
10694 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10697 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10698 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10700 static tree
10701 build_vec_cmp (tree_code code, tree type,
10702 tree arg0, tree arg1)
10704 tree zero_vec = build_zero_cst (type);
10705 tree minus_one_vec = build_minus_one_cst (type);
10706 tree cmp_type = build_same_sized_truth_vector_type (type);
10707 tree cmp = build2 (code, cmp_type, arg0, arg1);
10708 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10711 /* Build a binary-operation expression without default conversions.
10712 CODE is the kind of expression to build.
10713 LOCATION is the operator's location.
10714 This function differs from `build' in several ways:
10715 the data type of the result is computed and recorded in it,
10716 warnings are generated if arg data types are invalid,
10717 special handling for addition and subtraction of pointers is known,
10718 and some optimization is done (operations on narrow ints
10719 are done in the narrower type when that gives the same result).
10720 Constant folding is also done before the result is returned.
10722 Note that the operands will never have enumeral types, or function
10723 or array types, because either they will have the default conversions
10724 performed or they have both just been converted to some other type in which
10725 the arithmetic is to be done. */
10727 tree
10728 build_binary_op (location_t location, enum tree_code code,
10729 tree orig_op0, tree orig_op1, int convert_p)
10731 tree type0, type1, orig_type0, orig_type1;
10732 tree eptype;
10733 enum tree_code code0, code1;
10734 tree op0, op1;
10735 tree ret = error_mark_node;
10736 const char *invalid_op_diag;
10737 bool op0_int_operands, op1_int_operands;
10738 bool int_const, int_const_or_overflow, int_operands;
10740 /* Expression code to give to the expression when it is built.
10741 Normally this is CODE, which is what the caller asked for,
10742 but in some special cases we change it. */
10743 enum tree_code resultcode = code;
10745 /* Data type in which the computation is to be performed.
10746 In the simplest cases this is the common type of the arguments. */
10747 tree result_type = NULL;
10749 /* When the computation is in excess precision, the type of the
10750 final EXCESS_PRECISION_EXPR. */
10751 tree semantic_result_type = NULL;
10753 /* Nonzero means operands have already been type-converted
10754 in whatever way is necessary.
10755 Zero means they need to be converted to RESULT_TYPE. */
10756 int converted = 0;
10758 /* Nonzero means create the expression with this type, rather than
10759 RESULT_TYPE. */
10760 tree build_type = 0;
10762 /* Nonzero means after finally constructing the expression
10763 convert it to this type. */
10764 tree final_type = 0;
10766 /* Nonzero if this is an operation like MIN or MAX which can
10767 safely be computed in short if both args are promoted shorts.
10768 Also implies COMMON.
10769 -1 indicates a bitwise operation; this makes a difference
10770 in the exact conditions for when it is safe to do the operation
10771 in a narrower mode. */
10772 int shorten = 0;
10774 /* Nonzero if this is a comparison operation;
10775 if both args are promoted shorts, compare the original shorts.
10776 Also implies COMMON. */
10777 int short_compare = 0;
10779 /* Nonzero if this is a right-shift operation, which can be computed on the
10780 original short and then promoted if the operand is a promoted short. */
10781 int short_shift = 0;
10783 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10784 int common = 0;
10786 /* True means types are compatible as far as ObjC is concerned. */
10787 bool objc_ok;
10789 /* True means this is an arithmetic operation that may need excess
10790 precision. */
10791 bool may_need_excess_precision;
10793 /* True means this is a boolean operation that converts both its
10794 operands to truth-values. */
10795 bool boolean_op = false;
10797 /* Remember whether we're doing / or %. */
10798 bool doing_div_or_mod = false;
10800 /* Remember whether we're doing << or >>. */
10801 bool doing_shift = false;
10803 /* Tree holding instrumentation expression. */
10804 tree instrument_expr = NULL;
10806 if (location == UNKNOWN_LOCATION)
10807 location = input_location;
10809 op0 = orig_op0;
10810 op1 = orig_op1;
10812 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10813 if (op0_int_operands)
10814 op0 = remove_c_maybe_const_expr (op0);
10815 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10816 if (op1_int_operands)
10817 op1 = remove_c_maybe_const_expr (op1);
10818 int_operands = (op0_int_operands && op1_int_operands);
10819 if (int_operands)
10821 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10822 && TREE_CODE (orig_op1) == INTEGER_CST);
10823 int_const = (int_const_or_overflow
10824 && !TREE_OVERFLOW (orig_op0)
10825 && !TREE_OVERFLOW (orig_op1));
10827 else
10828 int_const = int_const_or_overflow = false;
10830 /* Do not apply default conversion in mixed vector/scalar expression. */
10831 if (convert_p
10832 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10834 op0 = default_conversion (op0);
10835 op1 = default_conversion (op1);
10838 /* When Cilk Plus is enabled and there are array notations inside op0, then
10839 we check to see if there are builtin array notation functions. If
10840 so, then we take on the type of the array notation inside it. */
10841 if (flag_cilkplus && contains_array_notation_expr (op0))
10842 orig_type0 = type0 = find_correct_array_notation_type (op0);
10843 else
10844 orig_type0 = type0 = TREE_TYPE (op0);
10846 if (flag_cilkplus && contains_array_notation_expr (op1))
10847 orig_type1 = type1 = find_correct_array_notation_type (op1);
10848 else
10849 orig_type1 = type1 = TREE_TYPE (op1);
10851 /* The expression codes of the data types of the arguments tell us
10852 whether the arguments are integers, floating, pointers, etc. */
10853 code0 = TREE_CODE (type0);
10854 code1 = TREE_CODE (type1);
10856 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10857 STRIP_TYPE_NOPS (op0);
10858 STRIP_TYPE_NOPS (op1);
10860 /* If an error was already reported for one of the arguments,
10861 avoid reporting another error. */
10863 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10864 return error_mark_node;
10866 if (code0 == POINTER_TYPE
10867 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10868 return error_mark_node;
10870 if (code1 == POINTER_TYPE
10871 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10872 return error_mark_node;
10874 if ((invalid_op_diag
10875 = targetm.invalid_binary_op (code, type0, type1)))
10877 error_at (location, invalid_op_diag);
10878 return error_mark_node;
10881 switch (code)
10883 case PLUS_EXPR:
10884 case MINUS_EXPR:
10885 case MULT_EXPR:
10886 case TRUNC_DIV_EXPR:
10887 case CEIL_DIV_EXPR:
10888 case FLOOR_DIV_EXPR:
10889 case ROUND_DIV_EXPR:
10890 case EXACT_DIV_EXPR:
10891 may_need_excess_precision = true;
10892 break;
10893 default:
10894 may_need_excess_precision = false;
10895 break;
10897 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10899 op0 = TREE_OPERAND (op0, 0);
10900 type0 = TREE_TYPE (op0);
10902 else if (may_need_excess_precision
10903 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10905 type0 = eptype;
10906 op0 = convert (eptype, op0);
10908 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10910 op1 = TREE_OPERAND (op1, 0);
10911 type1 = TREE_TYPE (op1);
10913 else if (may_need_excess_precision
10914 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10916 type1 = eptype;
10917 op1 = convert (eptype, op1);
10920 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10922 /* In case when one of the operands of the binary operation is
10923 a vector and another is a scalar -- convert scalar to vector. */
10924 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10926 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10927 true);
10929 switch (convert_flag)
10931 case stv_error:
10932 return error_mark_node;
10933 case stv_firstarg:
10935 bool maybe_const = true;
10936 tree sc;
10937 sc = c_fully_fold (op0, false, &maybe_const);
10938 sc = save_expr (sc);
10939 sc = convert (TREE_TYPE (type1), sc);
10940 op0 = build_vector_from_val (type1, sc);
10941 if (!maybe_const)
10942 op0 = c_wrap_maybe_const (op0, true);
10943 orig_type0 = type0 = TREE_TYPE (op0);
10944 code0 = TREE_CODE (type0);
10945 converted = 1;
10946 break;
10948 case stv_secondarg:
10950 bool maybe_const = true;
10951 tree sc;
10952 sc = c_fully_fold (op1, false, &maybe_const);
10953 sc = save_expr (sc);
10954 sc = convert (TREE_TYPE (type0), sc);
10955 op1 = build_vector_from_val (type0, sc);
10956 if (!maybe_const)
10957 op1 = c_wrap_maybe_const (op1, true);
10958 orig_type1 = type1 = TREE_TYPE (op1);
10959 code1 = TREE_CODE (type1);
10960 converted = 1;
10961 break;
10963 default:
10964 break;
10968 switch (code)
10970 case PLUS_EXPR:
10971 /* Handle the pointer + int case. */
10972 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10974 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10975 goto return_build_binary_op;
10977 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10979 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10980 goto return_build_binary_op;
10982 else
10983 common = 1;
10984 break;
10986 case MINUS_EXPR:
10987 /* Subtraction of two similar pointers.
10988 We must subtract them as integers, then divide by object size. */
10989 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10990 && comp_target_types (location, type0, type1))
10992 ret = pointer_diff (location, op0, op1);
10993 goto return_build_binary_op;
10995 /* Handle pointer minus int. Just like pointer plus int. */
10996 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10998 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10999 goto return_build_binary_op;
11001 else
11002 common = 1;
11003 break;
11005 case MULT_EXPR:
11006 common = 1;
11007 break;
11009 case TRUNC_DIV_EXPR:
11010 case CEIL_DIV_EXPR:
11011 case FLOOR_DIV_EXPR:
11012 case ROUND_DIV_EXPR:
11013 case EXACT_DIV_EXPR:
11014 doing_div_or_mod = true;
11015 warn_for_div_by_zero (location, op1);
11017 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11018 || code0 == FIXED_POINT_TYPE
11019 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11020 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11021 || code1 == FIXED_POINT_TYPE
11022 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11024 enum tree_code tcode0 = code0, tcode1 = code1;
11026 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11027 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11028 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11029 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11031 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11032 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11033 resultcode = RDIV_EXPR;
11034 else
11035 /* Although it would be tempting to shorten always here, that
11036 loses on some targets, since the modulo instruction is
11037 undefined if the quotient can't be represented in the
11038 computation mode. We shorten only if unsigned or if
11039 dividing by something we know != -1. */
11040 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11041 || (TREE_CODE (op1) == INTEGER_CST
11042 && !integer_all_onesp (op1)));
11043 common = 1;
11045 break;
11047 case BIT_AND_EXPR:
11048 case BIT_IOR_EXPR:
11049 case BIT_XOR_EXPR:
11050 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11051 shorten = -1;
11052 /* Allow vector types which are not floating point types. */
11053 else if (code0 == VECTOR_TYPE
11054 && code1 == VECTOR_TYPE
11055 && !VECTOR_FLOAT_TYPE_P (type0)
11056 && !VECTOR_FLOAT_TYPE_P (type1))
11057 common = 1;
11058 break;
11060 case TRUNC_MOD_EXPR:
11061 case FLOOR_MOD_EXPR:
11062 doing_div_or_mod = true;
11063 warn_for_div_by_zero (location, op1);
11065 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11066 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11067 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11068 common = 1;
11069 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11071 /* Although it would be tempting to shorten always here, that loses
11072 on some targets, since the modulo instruction is undefined if the
11073 quotient can't be represented in the computation mode. We shorten
11074 only if unsigned or if dividing by something we know != -1. */
11075 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11076 || (TREE_CODE (op1) == INTEGER_CST
11077 && !integer_all_onesp (op1)));
11078 common = 1;
11080 break;
11082 case TRUTH_ANDIF_EXPR:
11083 case TRUTH_ORIF_EXPR:
11084 case TRUTH_AND_EXPR:
11085 case TRUTH_OR_EXPR:
11086 case TRUTH_XOR_EXPR:
11087 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11088 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11089 || code0 == FIXED_POINT_TYPE)
11090 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11091 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11092 || code1 == FIXED_POINT_TYPE))
11094 /* Result of these operations is always an int,
11095 but that does not mean the operands should be
11096 converted to ints! */
11097 result_type = integer_type_node;
11098 if (op0_int_operands)
11100 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11101 op0 = remove_c_maybe_const_expr (op0);
11103 else
11104 op0 = c_objc_common_truthvalue_conversion (location, op0);
11105 if (op1_int_operands)
11107 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11108 op1 = remove_c_maybe_const_expr (op1);
11110 else
11111 op1 = c_objc_common_truthvalue_conversion (location, op1);
11112 converted = 1;
11113 boolean_op = true;
11115 if (code == TRUTH_ANDIF_EXPR)
11117 int_const_or_overflow = (int_operands
11118 && TREE_CODE (orig_op0) == INTEGER_CST
11119 && (op0 == truthvalue_false_node
11120 || TREE_CODE (orig_op1) == INTEGER_CST));
11121 int_const = (int_const_or_overflow
11122 && !TREE_OVERFLOW (orig_op0)
11123 && (op0 == truthvalue_false_node
11124 || !TREE_OVERFLOW (orig_op1)));
11126 else if (code == TRUTH_ORIF_EXPR)
11128 int_const_or_overflow = (int_operands
11129 && TREE_CODE (orig_op0) == INTEGER_CST
11130 && (op0 == truthvalue_true_node
11131 || TREE_CODE (orig_op1) == INTEGER_CST));
11132 int_const = (int_const_or_overflow
11133 && !TREE_OVERFLOW (orig_op0)
11134 && (op0 == truthvalue_true_node
11135 || !TREE_OVERFLOW (orig_op1)));
11137 break;
11139 /* Shift operations: result has same type as first operand;
11140 always convert second operand to int.
11141 Also set SHORT_SHIFT if shifting rightward. */
11143 case RSHIFT_EXPR:
11144 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11145 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11146 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11147 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11149 result_type = type0;
11150 converted = 1;
11152 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11153 || code0 == VECTOR_TYPE)
11154 && code1 == INTEGER_TYPE)
11156 doing_shift = true;
11157 if (TREE_CODE (op1) == INTEGER_CST)
11159 if (tree_int_cst_sgn (op1) < 0)
11161 int_const = false;
11162 if (c_inhibit_evaluation_warnings == 0)
11163 warning_at (location, OPT_Wshift_count_negative,
11164 "right shift count is negative");
11166 else if (code0 == VECTOR_TYPE)
11168 if (compare_tree_int (op1,
11169 TYPE_PRECISION (TREE_TYPE (type0)))
11170 >= 0)
11172 int_const = false;
11173 if (c_inhibit_evaluation_warnings == 0)
11174 warning_at (location, OPT_Wshift_count_overflow,
11175 "right shift count >= width of vector element");
11178 else
11180 if (!integer_zerop (op1))
11181 short_shift = 1;
11183 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11185 int_const = false;
11186 if (c_inhibit_evaluation_warnings == 0)
11187 warning_at (location, OPT_Wshift_count_overflow,
11188 "right shift count >= width of type");
11193 /* Use the type of the value to be shifted. */
11194 result_type = type0;
11195 /* Avoid converting op1 to result_type later. */
11196 converted = 1;
11198 break;
11200 case LSHIFT_EXPR:
11201 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11202 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11203 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11204 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11206 result_type = type0;
11207 converted = 1;
11209 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11210 || code0 == VECTOR_TYPE)
11211 && code1 == INTEGER_TYPE)
11213 doing_shift = true;
11214 if (TREE_CODE (op0) == INTEGER_CST
11215 && tree_int_cst_sgn (op0) < 0)
11217 /* Don't reject a left shift of a negative value in a context
11218 where a constant expression is needed in C90. */
11219 if (flag_isoc99)
11220 int_const = false;
11221 if (c_inhibit_evaluation_warnings == 0)
11222 warning_at (location, OPT_Wshift_negative_value,
11223 "left shift of negative value");
11225 if (TREE_CODE (op1) == INTEGER_CST)
11227 if (tree_int_cst_sgn (op1) < 0)
11229 int_const = false;
11230 if (c_inhibit_evaluation_warnings == 0)
11231 warning_at (location, OPT_Wshift_count_negative,
11232 "left shift count is negative");
11234 else if (code0 == VECTOR_TYPE)
11236 if (compare_tree_int (op1,
11237 TYPE_PRECISION (TREE_TYPE (type0)))
11238 >= 0)
11240 int_const = false;
11241 if (c_inhibit_evaluation_warnings == 0)
11242 warning_at (location, OPT_Wshift_count_overflow,
11243 "left shift count >= width of vector element");
11246 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11248 int_const = false;
11249 if (c_inhibit_evaluation_warnings == 0)
11250 warning_at (location, OPT_Wshift_count_overflow,
11251 "left shift count >= width of type");
11253 else if (TREE_CODE (op0) == INTEGER_CST
11254 && maybe_warn_shift_overflow (location, op0, op1)
11255 && flag_isoc99)
11256 int_const = false;
11259 /* Use the type of the value to be shifted. */
11260 result_type = type0;
11261 /* Avoid converting op1 to result_type later. */
11262 converted = 1;
11264 break;
11266 case EQ_EXPR:
11267 case NE_EXPR:
11268 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11270 tree intt;
11271 if (!vector_types_compatible_elements_p (type0, type1))
11273 error_at (location, "comparing vectors with different "
11274 "element types");
11275 return error_mark_node;
11278 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11280 error_at (location, "comparing vectors with different "
11281 "number of elements");
11282 return error_mark_node;
11285 /* It's not precisely specified how the usual arithmetic
11286 conversions apply to the vector types. Here, we use
11287 the unsigned type if one of the operands is signed and
11288 the other one is unsigned. */
11289 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11291 if (!TYPE_UNSIGNED (type0))
11292 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11293 else
11294 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11295 warning_at (location, OPT_Wsign_compare, "comparison between "
11296 "types %qT and %qT", type0, type1);
11299 /* Always construct signed integer vector type. */
11300 intt = c_common_type_for_size (GET_MODE_BITSIZE
11301 (TYPE_MODE (TREE_TYPE (type0))), 0);
11302 result_type = build_opaque_vector_type (intt,
11303 TYPE_VECTOR_SUBPARTS (type0));
11304 converted = 1;
11305 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11306 goto return_build_binary_op;
11308 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11309 warning_at (location,
11310 OPT_Wfloat_equal,
11311 "comparing floating point with == or != is unsafe");
11312 /* Result of comparison is always int,
11313 but don't convert the args to int! */
11314 build_type = integer_type_node;
11315 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11316 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11317 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11318 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11319 short_compare = 1;
11320 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11322 if (TREE_CODE (op0) == ADDR_EXPR
11323 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11324 && !from_macro_expansion_at (location))
11326 if (code == EQ_EXPR)
11327 warning_at (location,
11328 OPT_Waddress,
11329 "the comparison will always evaluate as %<false%> "
11330 "for the address of %qD will never be NULL",
11331 TREE_OPERAND (op0, 0));
11332 else
11333 warning_at (location,
11334 OPT_Waddress,
11335 "the comparison will always evaluate as %<true%> "
11336 "for the address of %qD will never be NULL",
11337 TREE_OPERAND (op0, 0));
11339 result_type = type0;
11341 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11343 if (TREE_CODE (op1) == ADDR_EXPR
11344 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11345 && !from_macro_expansion_at (location))
11347 if (code == EQ_EXPR)
11348 warning_at (location,
11349 OPT_Waddress,
11350 "the comparison will always evaluate as %<false%> "
11351 "for the address of %qD will never be NULL",
11352 TREE_OPERAND (op1, 0));
11353 else
11354 warning_at (location,
11355 OPT_Waddress,
11356 "the comparison will always evaluate as %<true%> "
11357 "for the address of %qD will never be NULL",
11358 TREE_OPERAND (op1, 0));
11360 result_type = type1;
11362 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11364 tree tt0 = TREE_TYPE (type0);
11365 tree tt1 = TREE_TYPE (type1);
11366 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11367 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11368 addr_space_t as_common = ADDR_SPACE_GENERIC;
11370 /* Anything compares with void *. void * compares with anything.
11371 Otherwise, the targets must be compatible
11372 and both must be object or both incomplete. */
11373 if (comp_target_types (location, type0, type1))
11374 result_type = common_pointer_type (type0, type1);
11375 else if (!addr_space_superset (as0, as1, &as_common))
11377 error_at (location, "comparison of pointers to "
11378 "disjoint address spaces");
11379 return error_mark_node;
11381 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11383 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11384 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11385 "comparison of %<void *%> with function pointer");
11387 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11389 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11390 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11391 "comparison of %<void *%> with function pointer");
11393 else
11394 /* Avoid warning about the volatile ObjC EH puts on decls. */
11395 if (!objc_ok)
11396 pedwarn (location, 0,
11397 "comparison of distinct pointer types lacks a cast");
11399 if (result_type == NULL_TREE)
11401 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11402 result_type = build_pointer_type
11403 (build_qualified_type (void_type_node, qual));
11406 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11408 result_type = type0;
11409 pedwarn (location, 0, "comparison between pointer and integer");
11411 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11413 result_type = type1;
11414 pedwarn (location, 0, "comparison between pointer and integer");
11416 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11417 || truth_value_p (TREE_CODE (orig_op0)))
11418 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11419 || truth_value_p (TREE_CODE (orig_op1))))
11420 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11421 break;
11423 case LE_EXPR:
11424 case GE_EXPR:
11425 case LT_EXPR:
11426 case GT_EXPR:
11427 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11429 tree intt;
11430 if (!vector_types_compatible_elements_p (type0, type1))
11432 error_at (location, "comparing vectors with different "
11433 "element types");
11434 return error_mark_node;
11437 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11439 error_at (location, "comparing vectors with different "
11440 "number of elements");
11441 return error_mark_node;
11444 /* It's not precisely specified how the usual arithmetic
11445 conversions apply to the vector types. Here, we use
11446 the unsigned type if one of the operands is signed and
11447 the other one is unsigned. */
11448 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11450 if (!TYPE_UNSIGNED (type0))
11451 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11452 else
11453 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11454 warning_at (location, OPT_Wsign_compare, "comparison between "
11455 "types %qT and %qT", type0, type1);
11458 /* Always construct signed integer vector type. */
11459 intt = c_common_type_for_size (GET_MODE_BITSIZE
11460 (TYPE_MODE (TREE_TYPE (type0))), 0);
11461 result_type = build_opaque_vector_type (intt,
11462 TYPE_VECTOR_SUBPARTS (type0));
11463 converted = 1;
11464 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11465 goto return_build_binary_op;
11467 build_type = integer_type_node;
11468 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11469 || code0 == FIXED_POINT_TYPE)
11470 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11471 || code1 == FIXED_POINT_TYPE))
11472 short_compare = 1;
11473 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11475 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11476 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11477 addr_space_t as_common;
11479 if (comp_target_types (location, type0, type1))
11481 result_type = common_pointer_type (type0, type1);
11482 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11483 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11484 pedwarn (location, 0,
11485 "comparison of complete and incomplete pointers");
11486 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11487 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11488 "ordered comparisons of pointers to functions");
11489 else if (null_pointer_constant_p (orig_op0)
11490 || null_pointer_constant_p (orig_op1))
11491 warning_at (location, OPT_Wextra,
11492 "ordered comparison of pointer with null pointer");
11495 else if (!addr_space_superset (as0, as1, &as_common))
11497 error_at (location, "comparison of pointers to "
11498 "disjoint address spaces");
11499 return error_mark_node;
11501 else
11503 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11504 result_type = build_pointer_type
11505 (build_qualified_type (void_type_node, qual));
11506 pedwarn (location, 0,
11507 "comparison of distinct pointer types lacks a cast");
11510 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11512 result_type = type0;
11513 if (pedantic)
11514 pedwarn (location, OPT_Wpedantic,
11515 "ordered comparison of pointer with integer zero");
11516 else if (extra_warnings)
11517 warning_at (location, OPT_Wextra,
11518 "ordered comparison of pointer with integer zero");
11520 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11522 result_type = type1;
11523 if (pedantic)
11524 pedwarn (location, OPT_Wpedantic,
11525 "ordered comparison of pointer with integer zero");
11526 else if (extra_warnings)
11527 warning_at (location, OPT_Wextra,
11528 "ordered comparison of pointer with integer zero");
11530 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11532 result_type = type0;
11533 pedwarn (location, 0, "comparison between pointer and integer");
11535 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11537 result_type = type1;
11538 pedwarn (location, 0, "comparison between pointer and integer");
11540 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11541 || truth_value_p (TREE_CODE (orig_op0)))
11542 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11543 || truth_value_p (TREE_CODE (orig_op1))))
11544 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11545 break;
11547 default:
11548 gcc_unreachable ();
11551 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11552 return error_mark_node;
11554 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11555 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11556 || !vector_types_compatible_elements_p (type0, type1)))
11558 gcc_rich_location richloc (location);
11559 richloc.maybe_add_expr (orig_op0);
11560 richloc.maybe_add_expr (orig_op1);
11561 binary_op_error (&richloc, code, type0, type1);
11562 return error_mark_node;
11565 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11566 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11568 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11569 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11571 bool first_complex = (code0 == COMPLEX_TYPE);
11572 bool second_complex = (code1 == COMPLEX_TYPE);
11573 int none_complex = (!first_complex && !second_complex);
11575 if (shorten || common || short_compare)
11577 result_type = c_common_type (type0, type1);
11578 do_warn_double_promotion (result_type, type0, type1,
11579 "implicit conversion from %qT to %qT "
11580 "to match other operand of binary "
11581 "expression",
11582 location);
11583 if (result_type == error_mark_node)
11584 return error_mark_node;
11587 if (first_complex != second_complex
11588 && (code == PLUS_EXPR
11589 || code == MINUS_EXPR
11590 || code == MULT_EXPR
11591 || (code == TRUNC_DIV_EXPR && first_complex))
11592 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11593 && flag_signed_zeros)
11595 /* An operation on mixed real/complex operands must be
11596 handled specially, but the language-independent code can
11597 more easily optimize the plain complex arithmetic if
11598 -fno-signed-zeros. */
11599 tree real_type = TREE_TYPE (result_type);
11600 tree real, imag;
11601 if (type0 != orig_type0 || type1 != orig_type1)
11603 gcc_assert (may_need_excess_precision && common);
11604 semantic_result_type = c_common_type (orig_type0, orig_type1);
11606 if (first_complex)
11608 if (TREE_TYPE (op0) != result_type)
11609 op0 = convert_and_check (location, result_type, op0);
11610 if (TREE_TYPE (op1) != real_type)
11611 op1 = convert_and_check (location, real_type, op1);
11613 else
11615 if (TREE_TYPE (op0) != real_type)
11616 op0 = convert_and_check (location, real_type, op0);
11617 if (TREE_TYPE (op1) != result_type)
11618 op1 = convert_and_check (location, result_type, op1);
11620 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11621 return error_mark_node;
11622 if (first_complex)
11624 op0 = c_save_expr (op0);
11625 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11626 op0, true);
11627 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11628 op0, true);
11629 switch (code)
11631 case MULT_EXPR:
11632 case TRUNC_DIV_EXPR:
11633 op1 = c_save_expr (op1);
11634 imag = build2 (resultcode, real_type, imag, op1);
11635 /* Fall through. */
11636 case PLUS_EXPR:
11637 case MINUS_EXPR:
11638 real = build2 (resultcode, real_type, real, op1);
11639 break;
11640 default:
11641 gcc_unreachable();
11644 else
11646 op1 = c_save_expr (op1);
11647 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11648 op1, true);
11649 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11650 op1, true);
11651 switch (code)
11653 case MULT_EXPR:
11654 op0 = c_save_expr (op0);
11655 imag = build2 (resultcode, real_type, op0, imag);
11656 /* Fall through. */
11657 case PLUS_EXPR:
11658 real = build2 (resultcode, real_type, op0, real);
11659 break;
11660 case MINUS_EXPR:
11661 real = build2 (resultcode, real_type, op0, real);
11662 imag = build1 (NEGATE_EXPR, real_type, imag);
11663 break;
11664 default:
11665 gcc_unreachable();
11668 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11669 goto return_build_binary_op;
11672 /* For certain operations (which identify themselves by shorten != 0)
11673 if both args were extended from the same smaller type,
11674 do the arithmetic in that type and then extend.
11676 shorten !=0 and !=1 indicates a bitwise operation.
11677 For them, this optimization is safe only if
11678 both args are zero-extended or both are sign-extended.
11679 Otherwise, we might change the result.
11680 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11681 but calculated in (unsigned short) it would be (unsigned short)-1. */
11683 if (shorten && none_complex)
11685 final_type = result_type;
11686 result_type = shorten_binary_op (result_type, op0, op1,
11687 shorten == -1);
11690 /* Shifts can be shortened if shifting right. */
11692 if (short_shift)
11694 int unsigned_arg;
11695 tree arg0 = get_narrower (op0, &unsigned_arg);
11697 final_type = result_type;
11699 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11700 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11702 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11703 && tree_int_cst_sgn (op1) > 0
11704 /* We can shorten only if the shift count is less than the
11705 number of bits in the smaller type size. */
11706 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11707 /* We cannot drop an unsigned shift after sign-extension. */
11708 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11710 /* Do an unsigned shift if the operand was zero-extended. */
11711 result_type
11712 = c_common_signed_or_unsigned_type (unsigned_arg,
11713 TREE_TYPE (arg0));
11714 /* Convert value-to-be-shifted to that type. */
11715 if (TREE_TYPE (op0) != result_type)
11716 op0 = convert (result_type, op0);
11717 converted = 1;
11721 /* Comparison operations are shortened too but differently.
11722 They identify themselves by setting short_compare = 1. */
11724 if (short_compare)
11726 /* Don't write &op0, etc., because that would prevent op0
11727 from being kept in a register.
11728 Instead, make copies of the our local variables and
11729 pass the copies by reference, then copy them back afterward. */
11730 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11731 enum tree_code xresultcode = resultcode;
11732 tree val
11733 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11734 &xresultcode);
11736 if (val != 0)
11738 ret = val;
11739 goto return_build_binary_op;
11742 op0 = xop0, op1 = xop1;
11743 converted = 1;
11744 resultcode = xresultcode;
11746 if (c_inhibit_evaluation_warnings == 0)
11748 bool op0_maybe_const = true;
11749 bool op1_maybe_const = true;
11750 tree orig_op0_folded, orig_op1_folded;
11752 if (in_late_binary_op)
11754 orig_op0_folded = orig_op0;
11755 orig_op1_folded = orig_op1;
11757 else
11759 /* Fold for the sake of possible warnings, as in
11760 build_conditional_expr. This requires the
11761 "original" values to be folded, not just op0 and
11762 op1. */
11763 c_inhibit_evaluation_warnings++;
11764 op0 = c_fully_fold (op0, require_constant_value,
11765 &op0_maybe_const);
11766 op1 = c_fully_fold (op1, require_constant_value,
11767 &op1_maybe_const);
11768 c_inhibit_evaluation_warnings--;
11769 orig_op0_folded = c_fully_fold (orig_op0,
11770 require_constant_value,
11771 NULL);
11772 orig_op1_folded = c_fully_fold (orig_op1,
11773 require_constant_value,
11774 NULL);
11777 if (warn_sign_compare)
11778 warn_for_sign_compare (location, orig_op0_folded,
11779 orig_op1_folded, op0, op1,
11780 result_type, resultcode);
11781 if (!in_late_binary_op && !int_operands)
11783 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11784 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11785 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11786 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11792 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11793 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11794 Then the expression will be built.
11795 It will be given type FINAL_TYPE if that is nonzero;
11796 otherwise, it will be given type RESULT_TYPE. */
11798 if (!result_type)
11800 gcc_rich_location richloc (location);
11801 richloc.maybe_add_expr (orig_op0);
11802 richloc.maybe_add_expr (orig_op1);
11803 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11804 return error_mark_node;
11807 if (build_type == NULL_TREE)
11809 build_type = result_type;
11810 if ((type0 != orig_type0 || type1 != orig_type1)
11811 && !boolean_op)
11813 gcc_assert (may_need_excess_precision && common);
11814 semantic_result_type = c_common_type (orig_type0, orig_type1);
11818 if (!converted)
11820 op0 = ep_convert_and_check (location, result_type, op0,
11821 semantic_result_type);
11822 op1 = ep_convert_and_check (location, result_type, op1,
11823 semantic_result_type);
11825 /* This can happen if one operand has a vector type, and the other
11826 has a different type. */
11827 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11828 return error_mark_node;
11831 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11832 | SANITIZE_FLOAT_DIVIDE))
11833 && do_ubsan_in_current_function ()
11834 && (doing_div_or_mod || doing_shift)
11835 && !require_constant_value)
11837 /* OP0 and/or OP1 might have side-effects. */
11838 op0 = c_save_expr (op0);
11839 op1 = c_save_expr (op1);
11840 op0 = c_fully_fold (op0, false, NULL);
11841 op1 = c_fully_fold (op1, false, NULL);
11842 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11843 | SANITIZE_FLOAT_DIVIDE)))
11844 instrument_expr = ubsan_instrument_division (location, op0, op1);
11845 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11846 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11849 /* Treat expressions in initializers specially as they can't trap. */
11850 if (int_const_or_overflow)
11851 ret = (require_constant_value
11852 ? fold_build2_initializer_loc (location, resultcode, build_type,
11853 op0, op1)
11854 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11855 else
11856 ret = build2 (resultcode, build_type, op0, op1);
11857 if (final_type != 0)
11858 ret = convert (final_type, ret);
11860 return_build_binary_op:
11861 gcc_assert (ret != error_mark_node);
11862 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11863 ret = (int_operands
11864 ? note_integer_operands (ret)
11865 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11866 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11867 && !in_late_binary_op)
11868 ret = note_integer_operands (ret);
11869 protected_set_expr_location (ret, location);
11871 if (instrument_expr != NULL)
11872 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11873 instrument_expr, ret);
11875 if (semantic_result_type)
11876 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
11877 semantic_result_type, ret);
11879 return ret;
11883 /* Convert EXPR to be a truth-value, validating its type for this
11884 purpose. LOCATION is the source location for the expression. */
11886 tree
11887 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11889 bool int_const, int_operands;
11891 switch (TREE_CODE (TREE_TYPE (expr)))
11893 case ARRAY_TYPE:
11894 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11895 return error_mark_node;
11897 case RECORD_TYPE:
11898 error_at (location, "used struct type value where scalar is required");
11899 return error_mark_node;
11901 case UNION_TYPE:
11902 error_at (location, "used union type value where scalar is required");
11903 return error_mark_node;
11905 case VOID_TYPE:
11906 error_at (location, "void value not ignored as it ought to be");
11907 return error_mark_node;
11909 case POINTER_TYPE:
11910 if (reject_gcc_builtin (expr))
11911 return error_mark_node;
11912 break;
11914 case FUNCTION_TYPE:
11915 gcc_unreachable ();
11917 case VECTOR_TYPE:
11918 error_at (location, "used vector type where scalar is required");
11919 return error_mark_node;
11921 default:
11922 break;
11925 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11926 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11927 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11929 expr = remove_c_maybe_const_expr (expr);
11930 expr = build2 (NE_EXPR, integer_type_node, expr,
11931 convert (TREE_TYPE (expr), integer_zero_node));
11932 expr = note_integer_operands (expr);
11934 else
11935 /* ??? Should we also give an error for vectors rather than leaving
11936 those to give errors later? */
11937 expr = c_common_truthvalue_conversion (location, expr);
11939 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11941 if (TREE_OVERFLOW (expr))
11942 return expr;
11943 else
11944 return note_integer_operands (expr);
11946 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11947 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11948 return expr;
11952 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11953 required. */
11955 tree
11956 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11958 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11960 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11961 /* Executing a compound literal inside a function reinitializes
11962 it. */
11963 if (!TREE_STATIC (decl))
11964 *se = true;
11965 return decl;
11967 else
11968 return expr;
11971 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11972 statement. LOC is the location of the construct. */
11974 tree
11975 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11976 tree clauses)
11978 body = c_end_compound_stmt (loc, body, true);
11980 tree stmt = make_node (code);
11981 TREE_TYPE (stmt) = void_type_node;
11982 OMP_BODY (stmt) = body;
11983 OMP_CLAUSES (stmt) = clauses;
11984 SET_EXPR_LOCATION (stmt, loc);
11986 return add_stmt (stmt);
11989 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11990 statement. LOC is the location of the OACC_DATA. */
11992 tree
11993 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11995 tree stmt;
11997 block = c_end_compound_stmt (loc, block, true);
11999 stmt = make_node (OACC_DATA);
12000 TREE_TYPE (stmt) = void_type_node;
12001 OACC_DATA_CLAUSES (stmt) = clauses;
12002 OACC_DATA_BODY (stmt) = block;
12003 SET_EXPR_LOCATION (stmt, loc);
12005 return add_stmt (stmt);
12008 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12009 statement. LOC is the location of the OACC_HOST_DATA. */
12011 tree
12012 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12014 tree stmt;
12016 block = c_end_compound_stmt (loc, block, true);
12018 stmt = make_node (OACC_HOST_DATA);
12019 TREE_TYPE (stmt) = void_type_node;
12020 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12021 OACC_HOST_DATA_BODY (stmt) = block;
12022 SET_EXPR_LOCATION (stmt, loc);
12024 return add_stmt (stmt);
12027 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12029 tree
12030 c_begin_omp_parallel (void)
12032 tree block;
12034 keep_next_level ();
12035 block = c_begin_compound_stmt (true);
12037 return block;
12040 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12041 statement. LOC is the location of the OMP_PARALLEL. */
12043 tree
12044 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12046 tree stmt;
12048 block = c_end_compound_stmt (loc, block, true);
12050 stmt = make_node (OMP_PARALLEL);
12051 TREE_TYPE (stmt) = void_type_node;
12052 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12053 OMP_PARALLEL_BODY (stmt) = block;
12054 SET_EXPR_LOCATION (stmt, loc);
12056 return add_stmt (stmt);
12059 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12061 tree
12062 c_begin_omp_task (void)
12064 tree block;
12066 keep_next_level ();
12067 block = c_begin_compound_stmt (true);
12069 return block;
12072 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12073 statement. LOC is the location of the #pragma. */
12075 tree
12076 c_finish_omp_task (location_t loc, tree clauses, tree block)
12078 tree stmt;
12080 block = c_end_compound_stmt (loc, block, true);
12082 stmt = make_node (OMP_TASK);
12083 TREE_TYPE (stmt) = void_type_node;
12084 OMP_TASK_CLAUSES (stmt) = clauses;
12085 OMP_TASK_BODY (stmt) = block;
12086 SET_EXPR_LOCATION (stmt, loc);
12088 return add_stmt (stmt);
12091 /* Generate GOMP_cancel call for #pragma omp cancel. */
12093 void
12094 c_finish_omp_cancel (location_t loc, tree clauses)
12096 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12097 int mask = 0;
12098 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12099 mask = 1;
12100 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12101 mask = 2;
12102 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12103 mask = 4;
12104 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12105 mask = 8;
12106 else
12108 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12109 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12110 "clauses");
12111 return;
12113 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12114 if (ifc != NULL_TREE)
12116 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12117 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12118 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12119 build_zero_cst (type));
12121 else
12122 ifc = boolean_true_node;
12123 tree stmt = build_call_expr_loc (loc, fn, 2,
12124 build_int_cst (integer_type_node, mask),
12125 ifc);
12126 add_stmt (stmt);
12129 /* Generate GOMP_cancellation_point call for
12130 #pragma omp cancellation point. */
12132 void
12133 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12135 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12136 int mask = 0;
12137 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12138 mask = 1;
12139 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12140 mask = 2;
12141 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12142 mask = 4;
12143 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12144 mask = 8;
12145 else
12147 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12148 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12149 "clauses");
12150 return;
12152 tree stmt = build_call_expr_loc (loc, fn, 1,
12153 build_int_cst (integer_type_node, mask));
12154 add_stmt (stmt);
12157 /* Helper function for handle_omp_array_sections. Called recursively
12158 to handle multiple array-section-subscripts. C is the clause,
12159 T current expression (initially OMP_CLAUSE_DECL), which is either
12160 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12161 expression if specified, TREE_VALUE length expression if specified,
12162 TREE_CHAIN is what it has been specified after, or some decl.
12163 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12164 set to true if any of the array-section-subscript could have length
12165 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12166 first array-section-subscript which is known not to have length
12167 of one. Given say:
12168 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12169 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12170 all are or may have length of 1, array-section-subscript [:2] is the
12171 first one known not to have length 1. For array-section-subscript
12172 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12173 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12174 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12175 case though, as some lengths could be zero. */
12177 static tree
12178 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12179 bool &maybe_zero_len, unsigned int &first_non_one,
12180 enum c_omp_region_type ort)
12182 tree ret, low_bound, length, type;
12183 if (TREE_CODE (t) != TREE_LIST)
12185 if (error_operand_p (t))
12186 return error_mark_node;
12187 ret = t;
12188 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12189 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12191 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12192 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12193 return error_mark_node;
12195 if (TREE_CODE (t) == COMPONENT_REF
12196 && ort == C_ORT_OMP
12197 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12198 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12199 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12201 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12203 error_at (OMP_CLAUSE_LOCATION (c),
12204 "bit-field %qE in %qs clause",
12205 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12206 return error_mark_node;
12208 while (TREE_CODE (t) == COMPONENT_REF)
12210 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12212 error_at (OMP_CLAUSE_LOCATION (c),
12213 "%qE is a member of a union", t);
12214 return error_mark_node;
12216 t = TREE_OPERAND (t, 0);
12219 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12221 if (DECL_P (t))
12222 error_at (OMP_CLAUSE_LOCATION (c),
12223 "%qD is not a variable in %qs clause", t,
12224 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12225 else
12226 error_at (OMP_CLAUSE_LOCATION (c),
12227 "%qE is not a variable in %qs clause", t,
12228 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12229 return error_mark_node;
12231 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12232 && TYPE_ATOMIC (TREE_TYPE (t)))
12234 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12235 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12236 return error_mark_node;
12238 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12239 && VAR_P (t)
12240 && DECL_THREAD_LOCAL_P (t))
12242 error_at (OMP_CLAUSE_LOCATION (c),
12243 "%qD is threadprivate variable in %qs clause", t,
12244 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12245 return error_mark_node;
12247 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12248 && TYPE_ATOMIC (TREE_TYPE (t))
12249 && POINTER_TYPE_P (TREE_TYPE (t)))
12251 /* If the array section is pointer based and the pointer
12252 itself is _Atomic qualified, we need to atomically load
12253 the pointer. */
12254 c_expr expr;
12255 memset (&expr, 0, sizeof (expr));
12256 expr.value = ret;
12257 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12258 expr, false, false);
12259 ret = expr.value;
12261 return ret;
12264 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12265 maybe_zero_len, first_non_one, ort);
12266 if (ret == error_mark_node || ret == NULL_TREE)
12267 return ret;
12269 type = TREE_TYPE (ret);
12270 low_bound = TREE_PURPOSE (t);
12271 length = TREE_VALUE (t);
12273 if (low_bound == error_mark_node || length == error_mark_node)
12274 return error_mark_node;
12276 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12278 error_at (OMP_CLAUSE_LOCATION (c),
12279 "low bound %qE of array section does not have integral type",
12280 low_bound);
12281 return error_mark_node;
12283 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12285 error_at (OMP_CLAUSE_LOCATION (c),
12286 "length %qE of array section does not have integral type",
12287 length);
12288 return error_mark_node;
12290 if (low_bound
12291 && TREE_CODE (low_bound) == INTEGER_CST
12292 && TYPE_PRECISION (TREE_TYPE (low_bound))
12293 > TYPE_PRECISION (sizetype))
12294 low_bound = fold_convert (sizetype, low_bound);
12295 if (length
12296 && TREE_CODE (length) == INTEGER_CST
12297 && TYPE_PRECISION (TREE_TYPE (length))
12298 > TYPE_PRECISION (sizetype))
12299 length = fold_convert (sizetype, length);
12300 if (low_bound == NULL_TREE)
12301 low_bound = integer_zero_node;
12303 if (length != NULL_TREE)
12305 if (!integer_nonzerop (length))
12307 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12308 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12310 if (integer_zerop (length))
12312 error_at (OMP_CLAUSE_LOCATION (c),
12313 "zero length array section in %qs clause",
12314 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12315 return error_mark_node;
12318 else
12319 maybe_zero_len = true;
12321 if (first_non_one == types.length ()
12322 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12323 first_non_one++;
12325 if (TREE_CODE (type) == ARRAY_TYPE)
12327 if (length == NULL_TREE
12328 && (TYPE_DOMAIN (type) == NULL_TREE
12329 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12331 error_at (OMP_CLAUSE_LOCATION (c),
12332 "for unknown bound array type length expression must "
12333 "be specified");
12334 return error_mark_node;
12336 if (TREE_CODE (low_bound) == INTEGER_CST
12337 && tree_int_cst_sgn (low_bound) == -1)
12339 error_at (OMP_CLAUSE_LOCATION (c),
12340 "negative low bound in array section in %qs clause",
12341 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12342 return error_mark_node;
12344 if (length != NULL_TREE
12345 && TREE_CODE (length) == INTEGER_CST
12346 && tree_int_cst_sgn (length) == -1)
12348 error_at (OMP_CLAUSE_LOCATION (c),
12349 "negative length in array section in %qs clause",
12350 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12351 return error_mark_node;
12353 if (TYPE_DOMAIN (type)
12354 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12355 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12356 == INTEGER_CST)
12358 tree size = size_binop (PLUS_EXPR,
12359 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12360 size_one_node);
12361 if (TREE_CODE (low_bound) == INTEGER_CST)
12363 if (tree_int_cst_lt (size, low_bound))
12365 error_at (OMP_CLAUSE_LOCATION (c),
12366 "low bound %qE above array section size "
12367 "in %qs clause", low_bound,
12368 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12369 return error_mark_node;
12371 if (tree_int_cst_equal (size, low_bound))
12373 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12374 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12376 error_at (OMP_CLAUSE_LOCATION (c),
12377 "zero length array section in %qs clause",
12378 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12379 return error_mark_node;
12381 maybe_zero_len = true;
12383 else if (length == NULL_TREE
12384 && first_non_one == types.length ()
12385 && tree_int_cst_equal
12386 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12387 low_bound))
12388 first_non_one++;
12390 else if (length == NULL_TREE)
12392 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12393 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12394 maybe_zero_len = true;
12395 if (first_non_one == types.length ())
12396 first_non_one++;
12398 if (length && TREE_CODE (length) == INTEGER_CST)
12400 if (tree_int_cst_lt (size, length))
12402 error_at (OMP_CLAUSE_LOCATION (c),
12403 "length %qE above array section size "
12404 "in %qs clause", length,
12405 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12406 return error_mark_node;
12408 if (TREE_CODE (low_bound) == INTEGER_CST)
12410 tree lbpluslen
12411 = size_binop (PLUS_EXPR,
12412 fold_convert (sizetype, low_bound),
12413 fold_convert (sizetype, length));
12414 if (TREE_CODE (lbpluslen) == INTEGER_CST
12415 && tree_int_cst_lt (size, lbpluslen))
12417 error_at (OMP_CLAUSE_LOCATION (c),
12418 "high bound %qE above array section size "
12419 "in %qs clause", lbpluslen,
12420 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12421 return error_mark_node;
12426 else if (length == NULL_TREE)
12428 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12429 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12430 maybe_zero_len = true;
12431 if (first_non_one == types.length ())
12432 first_non_one++;
12435 /* For [lb:] we will need to evaluate lb more than once. */
12436 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12438 tree lb = c_save_expr (low_bound);
12439 if (lb != low_bound)
12441 TREE_PURPOSE (t) = lb;
12442 low_bound = lb;
12446 else if (TREE_CODE (type) == POINTER_TYPE)
12448 if (length == NULL_TREE)
12450 error_at (OMP_CLAUSE_LOCATION (c),
12451 "for pointer type length expression must be specified");
12452 return error_mark_node;
12454 if (length != NULL_TREE
12455 && TREE_CODE (length) == INTEGER_CST
12456 && tree_int_cst_sgn (length) == -1)
12458 error_at (OMP_CLAUSE_LOCATION (c),
12459 "negative length in array section in %qs clause",
12460 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12461 return error_mark_node;
12463 /* If there is a pointer type anywhere but in the very first
12464 array-section-subscript, the array section can't be contiguous. */
12465 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12466 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12468 error_at (OMP_CLAUSE_LOCATION (c),
12469 "array section is not contiguous in %qs clause",
12470 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12471 return error_mark_node;
12474 else
12476 error_at (OMP_CLAUSE_LOCATION (c),
12477 "%qE does not have pointer or array type", ret);
12478 return error_mark_node;
12480 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12481 types.safe_push (TREE_TYPE (ret));
12482 /* We will need to evaluate lb more than once. */
12483 tree lb = c_save_expr (low_bound);
12484 if (lb != low_bound)
12486 TREE_PURPOSE (t) = lb;
12487 low_bound = lb;
12489 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12490 return ret;
12493 /* Handle array sections for clause C. */
12495 static bool
12496 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12498 bool maybe_zero_len = false;
12499 unsigned int first_non_one = 0;
12500 auto_vec<tree, 10> types;
12501 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12502 maybe_zero_len, first_non_one,
12503 ort);
12504 if (first == error_mark_node)
12505 return true;
12506 if (first == NULL_TREE)
12507 return false;
12508 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12510 tree t = OMP_CLAUSE_DECL (c);
12511 tree tem = NULL_TREE;
12512 /* Need to evaluate side effects in the length expressions
12513 if any. */
12514 while (TREE_CODE (t) == TREE_LIST)
12516 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12518 if (tem == NULL_TREE)
12519 tem = TREE_VALUE (t);
12520 else
12521 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12522 TREE_VALUE (t), tem);
12524 t = TREE_CHAIN (t);
12526 if (tem)
12527 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12528 first = c_fully_fold (first, false, NULL);
12529 OMP_CLAUSE_DECL (c) = first;
12531 else
12533 unsigned int num = types.length (), i;
12534 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12535 tree condition = NULL_TREE;
12537 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12538 maybe_zero_len = true;
12540 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12541 t = TREE_CHAIN (t))
12543 tree low_bound = TREE_PURPOSE (t);
12544 tree length = TREE_VALUE (t);
12546 i--;
12547 if (low_bound
12548 && TREE_CODE (low_bound) == INTEGER_CST
12549 && TYPE_PRECISION (TREE_TYPE (low_bound))
12550 > TYPE_PRECISION (sizetype))
12551 low_bound = fold_convert (sizetype, low_bound);
12552 if (length
12553 && TREE_CODE (length) == INTEGER_CST
12554 && TYPE_PRECISION (TREE_TYPE (length))
12555 > TYPE_PRECISION (sizetype))
12556 length = fold_convert (sizetype, length);
12557 if (low_bound == NULL_TREE)
12558 low_bound = integer_zero_node;
12559 if (!maybe_zero_len && i > first_non_one)
12561 if (integer_nonzerop (low_bound))
12562 goto do_warn_noncontiguous;
12563 if (length != NULL_TREE
12564 && TREE_CODE (length) == INTEGER_CST
12565 && TYPE_DOMAIN (types[i])
12566 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12567 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12568 == INTEGER_CST)
12570 tree size;
12571 size = size_binop (PLUS_EXPR,
12572 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12573 size_one_node);
12574 if (!tree_int_cst_equal (length, size))
12576 do_warn_noncontiguous:
12577 error_at (OMP_CLAUSE_LOCATION (c),
12578 "array section is not contiguous in %qs "
12579 "clause",
12580 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12581 return true;
12584 if (length != NULL_TREE
12585 && TREE_SIDE_EFFECTS (length))
12587 if (side_effects == NULL_TREE)
12588 side_effects = length;
12589 else
12590 side_effects = build2 (COMPOUND_EXPR,
12591 TREE_TYPE (side_effects),
12592 length, side_effects);
12595 else
12597 tree l;
12599 if (i > first_non_one
12600 && ((length && integer_nonzerop (length))
12601 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12602 continue;
12603 if (length)
12604 l = fold_convert (sizetype, length);
12605 else
12607 l = size_binop (PLUS_EXPR,
12608 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12609 size_one_node);
12610 l = size_binop (MINUS_EXPR, l,
12611 fold_convert (sizetype, low_bound));
12613 if (i > first_non_one)
12615 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12616 size_zero_node);
12617 if (condition == NULL_TREE)
12618 condition = l;
12619 else
12620 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12621 l, condition);
12623 else if (size == NULL_TREE)
12625 size = size_in_bytes (TREE_TYPE (types[i]));
12626 tree eltype = TREE_TYPE (types[num - 1]);
12627 while (TREE_CODE (eltype) == ARRAY_TYPE)
12628 eltype = TREE_TYPE (eltype);
12629 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12631 if (integer_zerop (size)
12632 || integer_zerop (size_in_bytes (eltype)))
12634 error_at (OMP_CLAUSE_LOCATION (c),
12635 "zero length array section in %qs clause",
12636 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12637 return error_mark_node;
12639 size = size_binop (EXACT_DIV_EXPR, size,
12640 size_in_bytes (eltype));
12642 size = size_binop (MULT_EXPR, size, l);
12643 if (condition)
12644 size = fold_build3 (COND_EXPR, sizetype, condition,
12645 size, size_zero_node);
12647 else
12648 size = size_binop (MULT_EXPR, size, l);
12651 if (side_effects)
12652 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12653 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12655 size = size_binop (MINUS_EXPR, size, size_one_node);
12656 size = c_fully_fold (size, false, NULL);
12657 tree index_type = build_index_type (size);
12658 tree eltype = TREE_TYPE (first);
12659 while (TREE_CODE (eltype) == ARRAY_TYPE)
12660 eltype = TREE_TYPE (eltype);
12661 tree type = build_array_type (eltype, index_type);
12662 tree ptype = build_pointer_type (eltype);
12663 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12664 t = build_fold_addr_expr (t);
12665 tree t2 = build_fold_addr_expr (first);
12666 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12667 ptrdiff_type_node, t2);
12668 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12669 ptrdiff_type_node, t2,
12670 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12671 ptrdiff_type_node, t));
12672 t2 = c_fully_fold (t2, false, NULL);
12673 if (tree_fits_shwi_p (t2))
12674 t = build2 (MEM_REF, type, t,
12675 build_int_cst (ptype, tree_to_shwi (t2)));
12676 else
12678 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12679 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12680 TREE_TYPE (t), t, t2);
12681 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12683 OMP_CLAUSE_DECL (c) = t;
12684 return false;
12686 first = c_fully_fold (first, false, NULL);
12687 OMP_CLAUSE_DECL (c) = first;
12688 if (size)
12689 size = c_fully_fold (size, false, NULL);
12690 OMP_CLAUSE_SIZE (c) = size;
12691 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12692 || (TREE_CODE (t) == COMPONENT_REF
12693 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12694 return false;
12695 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12696 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12697 switch (OMP_CLAUSE_MAP_KIND (c))
12699 case GOMP_MAP_ALLOC:
12700 case GOMP_MAP_TO:
12701 case GOMP_MAP_FROM:
12702 case GOMP_MAP_TOFROM:
12703 case GOMP_MAP_ALWAYS_TO:
12704 case GOMP_MAP_ALWAYS_FROM:
12705 case GOMP_MAP_ALWAYS_TOFROM:
12706 case GOMP_MAP_RELEASE:
12707 case GOMP_MAP_DELETE:
12708 case GOMP_MAP_FORCE_TO:
12709 case GOMP_MAP_FORCE_FROM:
12710 case GOMP_MAP_FORCE_TOFROM:
12711 case GOMP_MAP_FORCE_PRESENT:
12712 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12713 break;
12714 default:
12715 break;
12717 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12718 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12719 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12720 else if (TREE_CODE (t) == COMPONENT_REF)
12721 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12722 else
12723 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12724 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12725 && !c_mark_addressable (t))
12726 return false;
12727 OMP_CLAUSE_DECL (c2) = t;
12728 t = build_fold_addr_expr (first);
12729 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12730 tree ptr = OMP_CLAUSE_DECL (c2);
12731 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12732 ptr = build_fold_addr_expr (ptr);
12733 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12734 ptrdiff_type_node, t,
12735 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12736 ptrdiff_type_node, ptr));
12737 t = c_fully_fold (t, false, NULL);
12738 OMP_CLAUSE_SIZE (c2) = t;
12739 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12740 OMP_CLAUSE_CHAIN (c) = c2;
12742 return false;
12745 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12746 an inline call. But, remap
12747 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12748 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12750 static tree
12751 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12752 tree decl, tree placeholder)
12754 copy_body_data id;
12755 hash_map<tree, tree> decl_map;
12757 decl_map.put (omp_decl1, placeholder);
12758 decl_map.put (omp_decl2, decl);
12759 memset (&id, 0, sizeof (id));
12760 id.src_fn = DECL_CONTEXT (omp_decl1);
12761 id.dst_fn = current_function_decl;
12762 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12763 id.decl_map = &decl_map;
12765 id.copy_decl = copy_decl_no_change;
12766 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12767 id.transform_new_cfg = true;
12768 id.transform_return_to_modify = false;
12769 id.transform_lang_insert_block = NULL;
12770 id.eh_lp_nr = 0;
12771 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12772 return stmt;
12775 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12776 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12778 static tree
12779 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12781 if (*tp == (tree) data)
12782 return *tp;
12783 return NULL_TREE;
12786 /* For all elements of CLAUSES, validate them against their constraints.
12787 Remove any elements from the list that are invalid. */
12789 tree
12790 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12792 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12793 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12794 tree c, t, type, *pc;
12795 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12796 bool branch_seen = false;
12797 bool copyprivate_seen = false;
12798 bool linear_variable_step_check = false;
12799 tree *nowait_clause = NULL;
12800 bool ordered_seen = false;
12801 tree schedule_clause = NULL_TREE;
12802 bool oacc_async = false;
12804 bitmap_obstack_initialize (NULL);
12805 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12806 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12807 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12808 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12809 bitmap_initialize (&map_head, &bitmap_default_obstack);
12810 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12811 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12813 if (ort & C_ORT_ACC)
12814 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12815 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12817 oacc_async = true;
12818 break;
12821 for (pc = &clauses, c = clauses; c ; c = *pc)
12823 bool remove = false;
12824 bool need_complete = false;
12825 bool need_implicitly_determined = false;
12827 switch (OMP_CLAUSE_CODE (c))
12829 case OMP_CLAUSE_SHARED:
12830 need_implicitly_determined = true;
12831 goto check_dup_generic;
12833 case OMP_CLAUSE_PRIVATE:
12834 need_complete = true;
12835 need_implicitly_determined = true;
12836 goto check_dup_generic;
12838 case OMP_CLAUSE_REDUCTION:
12839 need_implicitly_determined = true;
12840 t = OMP_CLAUSE_DECL (c);
12841 if (TREE_CODE (t) == TREE_LIST)
12843 if (handle_omp_array_sections (c, ort))
12845 remove = true;
12846 break;
12849 t = OMP_CLAUSE_DECL (c);
12851 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12852 if (t == error_mark_node)
12854 remove = true;
12855 break;
12857 if (oacc_async)
12858 c_mark_addressable (t);
12859 type = TREE_TYPE (t);
12860 if (TREE_CODE (t) == MEM_REF)
12861 type = TREE_TYPE (type);
12862 if (TREE_CODE (type) == ARRAY_TYPE)
12864 tree oatype = type;
12865 gcc_assert (TREE_CODE (t) != MEM_REF);
12866 while (TREE_CODE (type) == ARRAY_TYPE)
12867 type = TREE_TYPE (type);
12868 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12870 error_at (OMP_CLAUSE_LOCATION (c),
12871 "%qD in %<reduction%> clause is a zero size array",
12873 remove = true;
12874 break;
12876 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12877 TYPE_SIZE_UNIT (type));
12878 if (integer_zerop (size))
12880 error_at (OMP_CLAUSE_LOCATION (c),
12881 "%qD in %<reduction%> clause is a zero size array",
12883 remove = true;
12884 break;
12886 size = size_binop (MINUS_EXPR, size, size_one_node);
12887 tree index_type = build_index_type (size);
12888 tree atype = build_array_type (type, index_type);
12889 tree ptype = build_pointer_type (type);
12890 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12891 t = build_fold_addr_expr (t);
12892 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12893 OMP_CLAUSE_DECL (c) = t;
12895 if (TYPE_ATOMIC (type))
12897 error_at (OMP_CLAUSE_LOCATION (c),
12898 "%<_Atomic%> %qE in %<reduction%> clause", t);
12899 remove = true;
12900 break;
12902 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12903 && (FLOAT_TYPE_P (type)
12904 || TREE_CODE (type) == COMPLEX_TYPE))
12906 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12907 const char *r_name = NULL;
12909 switch (r_code)
12911 case PLUS_EXPR:
12912 case MULT_EXPR:
12913 case MINUS_EXPR:
12914 break;
12915 case MIN_EXPR:
12916 if (TREE_CODE (type) == COMPLEX_TYPE)
12917 r_name = "min";
12918 break;
12919 case MAX_EXPR:
12920 if (TREE_CODE (type) == COMPLEX_TYPE)
12921 r_name = "max";
12922 break;
12923 case BIT_AND_EXPR:
12924 r_name = "&";
12925 break;
12926 case BIT_XOR_EXPR:
12927 r_name = "^";
12928 break;
12929 case BIT_IOR_EXPR:
12930 r_name = "|";
12931 break;
12932 case TRUTH_ANDIF_EXPR:
12933 if (FLOAT_TYPE_P (type))
12934 r_name = "&&";
12935 break;
12936 case TRUTH_ORIF_EXPR:
12937 if (FLOAT_TYPE_P (type))
12938 r_name = "||";
12939 break;
12940 default:
12941 gcc_unreachable ();
12943 if (r_name)
12945 error_at (OMP_CLAUSE_LOCATION (c),
12946 "%qE has invalid type for %<reduction(%s)%>",
12947 t, r_name);
12948 remove = true;
12949 break;
12952 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12954 error_at (OMP_CLAUSE_LOCATION (c),
12955 "user defined reduction not found for %qE", t);
12956 remove = true;
12957 break;
12959 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12961 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12962 type = TYPE_MAIN_VARIANT (type);
12963 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12964 VAR_DECL, NULL_TREE, type);
12965 tree decl_placeholder = NULL_TREE;
12966 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12967 DECL_ARTIFICIAL (placeholder) = 1;
12968 DECL_IGNORED_P (placeholder) = 1;
12969 if (TREE_CODE (t) == MEM_REF)
12971 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12972 VAR_DECL, NULL_TREE, type);
12973 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12974 DECL_ARTIFICIAL (decl_placeholder) = 1;
12975 DECL_IGNORED_P (decl_placeholder) = 1;
12977 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12978 c_mark_addressable (placeholder);
12979 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12980 c_mark_addressable (decl_placeholder ? decl_placeholder
12981 : OMP_CLAUSE_DECL (c));
12982 OMP_CLAUSE_REDUCTION_MERGE (c)
12983 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12984 TREE_VEC_ELT (list, 0),
12985 TREE_VEC_ELT (list, 1),
12986 decl_placeholder ? decl_placeholder
12987 : OMP_CLAUSE_DECL (c), placeholder);
12988 OMP_CLAUSE_REDUCTION_MERGE (c)
12989 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12990 void_type_node, NULL_TREE,
12991 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12992 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12993 if (TREE_VEC_LENGTH (list) == 6)
12995 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12996 c_mark_addressable (decl_placeholder ? decl_placeholder
12997 : OMP_CLAUSE_DECL (c));
12998 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12999 c_mark_addressable (placeholder);
13000 tree init = TREE_VEC_ELT (list, 5);
13001 if (init == error_mark_node)
13002 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13003 OMP_CLAUSE_REDUCTION_INIT (c)
13004 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13005 TREE_VEC_ELT (list, 3),
13006 decl_placeholder ? decl_placeholder
13007 : OMP_CLAUSE_DECL (c), placeholder);
13008 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13010 tree v = decl_placeholder ? decl_placeholder : t;
13011 OMP_CLAUSE_REDUCTION_INIT (c)
13012 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13013 OMP_CLAUSE_REDUCTION_INIT (c));
13015 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13016 c_find_omp_placeholder_r,
13017 placeholder, NULL))
13018 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13020 else
13022 tree init;
13023 tree v = decl_placeholder ? decl_placeholder : t;
13024 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13025 init = build_constructor (TREE_TYPE (v), NULL);
13026 else
13027 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13028 OMP_CLAUSE_REDUCTION_INIT (c)
13029 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13031 OMP_CLAUSE_REDUCTION_INIT (c)
13032 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13033 void_type_node, NULL_TREE,
13034 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13035 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13037 if (TREE_CODE (t) == MEM_REF)
13039 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13040 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13041 != INTEGER_CST)
13043 sorry ("variable length element type in array "
13044 "%<reduction%> clause");
13045 remove = true;
13046 break;
13048 t = TREE_OPERAND (t, 0);
13049 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13050 t = TREE_OPERAND (t, 0);
13051 if (TREE_CODE (t) == ADDR_EXPR)
13052 t = TREE_OPERAND (t, 0);
13054 goto check_dup_generic_t;
13056 case OMP_CLAUSE_COPYPRIVATE:
13057 copyprivate_seen = true;
13058 if (nowait_clause)
13060 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13061 "%<nowait%> clause must not be used together "
13062 "with %<copyprivate%>");
13063 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13064 nowait_clause = NULL;
13066 goto check_dup_generic;
13068 case OMP_CLAUSE_COPYIN:
13069 t = OMP_CLAUSE_DECL (c);
13070 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13072 error_at (OMP_CLAUSE_LOCATION (c),
13073 "%qE must be %<threadprivate%> for %<copyin%>", t);
13074 remove = true;
13075 break;
13077 goto check_dup_generic;
13079 case OMP_CLAUSE_LINEAR:
13080 if (ort != C_ORT_OMP_DECLARE_SIMD)
13081 need_implicitly_determined = true;
13082 t = OMP_CLAUSE_DECL (c);
13083 if (ort != C_ORT_OMP_DECLARE_SIMD
13084 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13086 error_at (OMP_CLAUSE_LOCATION (c),
13087 "modifier should not be specified in %<linear%> "
13088 "clause on %<simd%> or %<for%> constructs");
13089 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13091 if (ort & C_ORT_CILK)
13093 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13094 && !SCALAR_FLOAT_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, "
13099 "non-floating, non-pointer variable with type %qT",
13100 TREE_TYPE (t));
13101 remove = true;
13102 break;
13105 else
13107 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13108 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13110 error_at (OMP_CLAUSE_LOCATION (c),
13111 "linear clause applied to non-integral non-pointer "
13112 "variable with type %qT", TREE_TYPE (t));
13113 remove = true;
13114 break;
13116 if (TYPE_ATOMIC (TREE_TYPE (t)))
13118 error_at (OMP_CLAUSE_LOCATION (c),
13119 "%<_Atomic%> %qD in %<linear%> clause", t);
13120 remove = true;
13121 break;
13124 if (ort == C_ORT_OMP_DECLARE_SIMD)
13126 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13127 if (TREE_CODE (s) == PARM_DECL)
13129 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13130 /* map_head bitmap is used as uniform_head if
13131 declare_simd. */
13132 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13133 linear_variable_step_check = true;
13134 goto check_dup_generic;
13136 if (TREE_CODE (s) != INTEGER_CST)
13138 error_at (OMP_CLAUSE_LOCATION (c),
13139 "%<linear%> clause step %qE is neither constant "
13140 "nor a parameter", s);
13141 remove = true;
13142 break;
13145 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13147 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13148 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13149 OMP_CLAUSE_DECL (c), s);
13150 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13151 sizetype, fold_convert (sizetype, s),
13152 fold_convert
13153 (sizetype, OMP_CLAUSE_DECL (c)));
13154 if (s == error_mark_node)
13155 s = size_one_node;
13156 OMP_CLAUSE_LINEAR_STEP (c) = s;
13158 else
13159 OMP_CLAUSE_LINEAR_STEP (c)
13160 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13161 goto check_dup_generic;
13163 check_dup_generic:
13164 t = OMP_CLAUSE_DECL (c);
13165 check_dup_generic_t:
13166 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13168 error_at (OMP_CLAUSE_LOCATION (c),
13169 "%qE is not a variable in clause %qs", t,
13170 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13171 remove = true;
13173 else if (ort == C_ORT_ACC
13174 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13176 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13178 error ("%qD appears more than once in reduction clauses", t);
13179 remove = true;
13181 else
13182 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13184 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13185 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13186 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13188 error_at (OMP_CLAUSE_LOCATION (c),
13189 "%qE appears more than once in data clauses", t);
13190 remove = true;
13192 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13193 && bitmap_bit_p (&map_head, DECL_UID (t)))
13195 if (ort == C_ORT_ACC)
13196 error ("%qD appears more than once in data clauses", t);
13197 else
13198 error ("%qD appears both in data and map clauses", t);
13199 remove = true;
13201 else
13202 bitmap_set_bit (&generic_head, DECL_UID (t));
13203 break;
13205 case OMP_CLAUSE_FIRSTPRIVATE:
13206 t = OMP_CLAUSE_DECL (c);
13207 need_complete = true;
13208 need_implicitly_determined = true;
13209 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13211 error_at (OMP_CLAUSE_LOCATION (c),
13212 "%qE is not a variable in clause %<firstprivate%>", t);
13213 remove = true;
13215 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13216 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13218 error_at (OMP_CLAUSE_LOCATION (c),
13219 "%qE appears more than once in data clauses", t);
13220 remove = true;
13222 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13224 if (ort == C_ORT_ACC)
13225 error ("%qD appears more than once in data clauses", t);
13226 else
13227 error ("%qD appears both in data and map clauses", t);
13228 remove = true;
13230 else
13231 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13232 break;
13234 case OMP_CLAUSE_LASTPRIVATE:
13235 t = OMP_CLAUSE_DECL (c);
13236 need_complete = true;
13237 need_implicitly_determined = true;
13238 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13240 error_at (OMP_CLAUSE_LOCATION (c),
13241 "%qE is not a variable in clause %<lastprivate%>", t);
13242 remove = true;
13244 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13245 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13247 error_at (OMP_CLAUSE_LOCATION (c),
13248 "%qE appears more than once in data clauses", t);
13249 remove = true;
13251 else
13252 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13253 break;
13255 case OMP_CLAUSE_ALIGNED:
13256 t = OMP_CLAUSE_DECL (c);
13257 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13259 error_at (OMP_CLAUSE_LOCATION (c),
13260 "%qE is not a variable in %<aligned%> clause", t);
13261 remove = true;
13263 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13264 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13266 error_at (OMP_CLAUSE_LOCATION (c),
13267 "%qE in %<aligned%> clause is neither a pointer nor "
13268 "an array", t);
13269 remove = true;
13271 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13273 error_at (OMP_CLAUSE_LOCATION (c),
13274 "%<_Atomic%> %qD in %<aligned%> clause", t);
13275 remove = true;
13276 break;
13278 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13280 error_at (OMP_CLAUSE_LOCATION (c),
13281 "%qE appears more than once in %<aligned%> clauses",
13283 remove = true;
13285 else
13286 bitmap_set_bit (&aligned_head, DECL_UID (t));
13287 break;
13289 case OMP_CLAUSE_DEPEND:
13290 t = OMP_CLAUSE_DECL (c);
13291 if (t == NULL_TREE)
13293 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13294 == OMP_CLAUSE_DEPEND_SOURCE);
13295 break;
13297 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13299 gcc_assert (TREE_CODE (t) == TREE_LIST);
13300 for (; t; t = TREE_CHAIN (t))
13302 tree decl = TREE_VALUE (t);
13303 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13305 tree offset = TREE_PURPOSE (t);
13306 bool neg = wi::neg_p ((wide_int) offset);
13307 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13308 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13309 neg ? MINUS_EXPR : PLUS_EXPR,
13310 decl, offset);
13311 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13312 sizetype,
13313 fold_convert (sizetype, t2),
13314 fold_convert (sizetype, decl));
13315 if (t2 == error_mark_node)
13317 remove = true;
13318 break;
13320 TREE_PURPOSE (t) = t2;
13323 break;
13325 if (TREE_CODE (t) == TREE_LIST)
13327 if (handle_omp_array_sections (c, ort))
13328 remove = true;
13329 break;
13331 if (t == error_mark_node)
13332 remove = true;
13333 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13335 error_at (OMP_CLAUSE_LOCATION (c),
13336 "%qE is not a variable in %<depend%> clause", t);
13337 remove = true;
13339 else if (!c_mark_addressable (t))
13340 remove = true;
13341 break;
13343 case OMP_CLAUSE_MAP:
13344 case OMP_CLAUSE_TO:
13345 case OMP_CLAUSE_FROM:
13346 case OMP_CLAUSE__CACHE_:
13347 t = OMP_CLAUSE_DECL (c);
13348 if (TREE_CODE (t) == TREE_LIST)
13350 if (handle_omp_array_sections (c, ort))
13351 remove = true;
13352 else
13354 t = OMP_CLAUSE_DECL (c);
13355 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13357 error_at (OMP_CLAUSE_LOCATION (c),
13358 "array section does not have mappable type "
13359 "in %qs clause",
13360 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13361 remove = true;
13363 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13365 error_at (OMP_CLAUSE_LOCATION (c),
13366 "%<_Atomic%> %qE in %qs clause", t,
13367 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13368 remove = true;
13370 while (TREE_CODE (t) == ARRAY_REF)
13371 t = TREE_OPERAND (t, 0);
13372 if (TREE_CODE (t) == COMPONENT_REF
13373 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13375 while (TREE_CODE (t) == COMPONENT_REF)
13376 t = TREE_OPERAND (t, 0);
13377 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13378 break;
13379 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13381 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13382 error ("%qD appears more than once in motion"
13383 " clauses", t);
13384 else if (ort == C_ORT_ACC)
13385 error ("%qD appears more than once in data"
13386 " clauses", t);
13387 else
13388 error ("%qD appears more than once in map"
13389 " clauses", t);
13390 remove = true;
13392 else
13394 bitmap_set_bit (&map_head, DECL_UID (t));
13395 bitmap_set_bit (&map_field_head, DECL_UID (t));
13399 break;
13401 if (t == error_mark_node)
13403 remove = true;
13404 break;
13406 if (TREE_CODE (t) == COMPONENT_REF
13407 && (ort & C_ORT_OMP)
13408 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13410 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13412 error_at (OMP_CLAUSE_LOCATION (c),
13413 "bit-field %qE in %qs clause",
13414 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13415 remove = true;
13417 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13419 error_at (OMP_CLAUSE_LOCATION (c),
13420 "%qE does not have a mappable type in %qs clause",
13421 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13422 remove = true;
13424 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13426 error_at (OMP_CLAUSE_LOCATION (c),
13427 "%<_Atomic%> %qE in %qs clause", t,
13428 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13429 remove = true;
13431 while (TREE_CODE (t) == COMPONENT_REF)
13433 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13434 == UNION_TYPE)
13436 error_at (OMP_CLAUSE_LOCATION (c),
13437 "%qE is a member of a union", t);
13438 remove = true;
13439 break;
13441 t = TREE_OPERAND (t, 0);
13443 if (remove)
13444 break;
13445 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13447 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13448 break;
13451 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13453 error_at (OMP_CLAUSE_LOCATION (c),
13454 "%qE is not a variable in %qs clause", t,
13455 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13456 remove = true;
13458 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13460 error_at (OMP_CLAUSE_LOCATION (c),
13461 "%qD is threadprivate variable in %qs clause", t,
13462 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13463 remove = true;
13465 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13466 || (OMP_CLAUSE_MAP_KIND (c)
13467 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13468 && !c_mark_addressable (t))
13469 remove = true;
13470 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13471 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13472 || (OMP_CLAUSE_MAP_KIND (c)
13473 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13474 || (OMP_CLAUSE_MAP_KIND (c)
13475 == GOMP_MAP_FORCE_DEVICEPTR)))
13476 && t == OMP_CLAUSE_DECL (c)
13477 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13479 error_at (OMP_CLAUSE_LOCATION (c),
13480 "%qD does not have a mappable type in %qs clause", t,
13481 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13482 remove = true;
13484 else if (TREE_TYPE (t) == error_mark_node)
13485 remove = true;
13486 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13488 error_at (OMP_CLAUSE_LOCATION (c),
13489 "%<_Atomic%> %qE in %qs clause", t,
13490 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13491 remove = true;
13493 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13494 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13496 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13497 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13499 error ("%qD appears more than once in data clauses", t);
13500 remove = true;
13502 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13504 if (ort == C_ORT_ACC)
13505 error ("%qD appears more than once in data clauses", t);
13506 else
13507 error ("%qD appears both in data and map clauses", t);
13508 remove = true;
13510 else
13511 bitmap_set_bit (&generic_head, DECL_UID (t));
13513 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13515 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13516 error ("%qD appears more than once in motion clauses", t);
13517 else if (ort == C_ORT_ACC)
13518 error ("%qD appears more than once in data clauses", t);
13519 else
13520 error ("%qD appears more than once in map clauses", t);
13521 remove = true;
13523 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13524 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13526 if (ort == C_ORT_ACC)
13527 error ("%qD appears more than once in data clauses", t);
13528 else
13529 error ("%qD appears both in data and map clauses", t);
13530 remove = true;
13532 else
13534 bitmap_set_bit (&map_head, DECL_UID (t));
13535 if (t != OMP_CLAUSE_DECL (c)
13536 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13537 bitmap_set_bit (&map_field_head, DECL_UID (t));
13539 break;
13541 case OMP_CLAUSE_TO_DECLARE:
13542 case OMP_CLAUSE_LINK:
13543 t = OMP_CLAUSE_DECL (c);
13544 if (TREE_CODE (t) == FUNCTION_DECL
13545 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13547 else if (!VAR_P (t))
13549 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13550 error_at (OMP_CLAUSE_LOCATION (c),
13551 "%qE is neither a variable nor a function name in "
13552 "clause %qs", t,
13553 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13554 else
13555 error_at (OMP_CLAUSE_LOCATION (c),
13556 "%qE is not a variable in clause %qs", t,
13557 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13558 remove = true;
13560 else if (DECL_THREAD_LOCAL_P (t))
13562 error_at (OMP_CLAUSE_LOCATION (c),
13563 "%qD is threadprivate variable in %qs clause", t,
13564 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13565 remove = true;
13567 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13569 error_at (OMP_CLAUSE_LOCATION (c),
13570 "%qD does not have a mappable type in %qs clause", t,
13571 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13572 remove = true;
13574 if (remove)
13575 break;
13576 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13578 error_at (OMP_CLAUSE_LOCATION (c),
13579 "%qE appears more than once on the same "
13580 "%<declare target%> directive", t);
13581 remove = true;
13583 else
13584 bitmap_set_bit (&generic_head, DECL_UID (t));
13585 break;
13587 case OMP_CLAUSE_UNIFORM:
13588 t = OMP_CLAUSE_DECL (c);
13589 if (TREE_CODE (t) != PARM_DECL)
13591 if (DECL_P (t))
13592 error_at (OMP_CLAUSE_LOCATION (c),
13593 "%qD is not an argument in %<uniform%> clause", t);
13594 else
13595 error_at (OMP_CLAUSE_LOCATION (c),
13596 "%qE is not an argument in %<uniform%> clause", t);
13597 remove = true;
13598 break;
13600 /* map_head bitmap is used as uniform_head if declare_simd. */
13601 bitmap_set_bit (&map_head, DECL_UID (t));
13602 goto check_dup_generic;
13604 case OMP_CLAUSE_IS_DEVICE_PTR:
13605 case OMP_CLAUSE_USE_DEVICE_PTR:
13606 t = OMP_CLAUSE_DECL (c);
13607 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13608 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13610 error_at (OMP_CLAUSE_LOCATION (c),
13611 "%qs variable is neither a pointer nor an array",
13612 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13613 remove = true;
13615 goto check_dup_generic;
13617 case OMP_CLAUSE_NOWAIT:
13618 if (copyprivate_seen)
13620 error_at (OMP_CLAUSE_LOCATION (c),
13621 "%<nowait%> clause must not be used together "
13622 "with %<copyprivate%>");
13623 remove = true;
13624 break;
13626 nowait_clause = pc;
13627 pc = &OMP_CLAUSE_CHAIN (c);
13628 continue;
13630 case OMP_CLAUSE_IF:
13631 case OMP_CLAUSE_NUM_THREADS:
13632 case OMP_CLAUSE_NUM_TEAMS:
13633 case OMP_CLAUSE_THREAD_LIMIT:
13634 case OMP_CLAUSE_DEFAULT:
13635 case OMP_CLAUSE_UNTIED:
13636 case OMP_CLAUSE_COLLAPSE:
13637 case OMP_CLAUSE_FINAL:
13638 case OMP_CLAUSE_MERGEABLE:
13639 case OMP_CLAUSE_DEVICE:
13640 case OMP_CLAUSE_DIST_SCHEDULE:
13641 case OMP_CLAUSE_PARALLEL:
13642 case OMP_CLAUSE_FOR:
13643 case OMP_CLAUSE_SECTIONS:
13644 case OMP_CLAUSE_TASKGROUP:
13645 case OMP_CLAUSE_PROC_BIND:
13646 case OMP_CLAUSE_PRIORITY:
13647 case OMP_CLAUSE_GRAINSIZE:
13648 case OMP_CLAUSE_NUM_TASKS:
13649 case OMP_CLAUSE_NOGROUP:
13650 case OMP_CLAUSE_THREADS:
13651 case OMP_CLAUSE_SIMD:
13652 case OMP_CLAUSE_HINT:
13653 case OMP_CLAUSE_DEFAULTMAP:
13654 case OMP_CLAUSE__CILK_FOR_COUNT_:
13655 case OMP_CLAUSE_NUM_GANGS:
13656 case OMP_CLAUSE_NUM_WORKERS:
13657 case OMP_CLAUSE_VECTOR_LENGTH:
13658 case OMP_CLAUSE_ASYNC:
13659 case OMP_CLAUSE_WAIT:
13660 case OMP_CLAUSE_AUTO:
13661 case OMP_CLAUSE_INDEPENDENT:
13662 case OMP_CLAUSE_SEQ:
13663 case OMP_CLAUSE_GANG:
13664 case OMP_CLAUSE_WORKER:
13665 case OMP_CLAUSE_VECTOR:
13666 case OMP_CLAUSE_TILE:
13667 pc = &OMP_CLAUSE_CHAIN (c);
13668 continue;
13670 case OMP_CLAUSE_SCHEDULE:
13671 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13673 const char *p = NULL;
13674 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13676 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13677 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13678 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13679 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13680 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13681 default: gcc_unreachable ();
13683 if (p)
13685 error_at (OMP_CLAUSE_LOCATION (c),
13686 "%<nonmonotonic%> modifier specified for %qs "
13687 "schedule kind", p);
13688 OMP_CLAUSE_SCHEDULE_KIND (c)
13689 = (enum omp_clause_schedule_kind)
13690 (OMP_CLAUSE_SCHEDULE_KIND (c)
13691 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13694 schedule_clause = c;
13695 pc = &OMP_CLAUSE_CHAIN (c);
13696 continue;
13698 case OMP_CLAUSE_ORDERED:
13699 ordered_seen = true;
13700 pc = &OMP_CLAUSE_CHAIN (c);
13701 continue;
13703 case OMP_CLAUSE_SAFELEN:
13704 safelen = c;
13705 pc = &OMP_CLAUSE_CHAIN (c);
13706 continue;
13707 case OMP_CLAUSE_SIMDLEN:
13708 simdlen = c;
13709 pc = &OMP_CLAUSE_CHAIN (c);
13710 continue;
13712 case OMP_CLAUSE_INBRANCH:
13713 case OMP_CLAUSE_NOTINBRANCH:
13714 if (branch_seen)
13716 error_at (OMP_CLAUSE_LOCATION (c),
13717 "%<inbranch%> clause is incompatible with "
13718 "%<notinbranch%>");
13719 remove = true;
13720 break;
13722 branch_seen = true;
13723 pc = &OMP_CLAUSE_CHAIN (c);
13724 continue;
13726 default:
13727 gcc_unreachable ();
13730 if (!remove)
13732 t = OMP_CLAUSE_DECL (c);
13734 if (need_complete)
13736 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13737 if (t == error_mark_node)
13738 remove = true;
13741 if (need_implicitly_determined)
13743 const char *share_name = NULL;
13745 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13746 share_name = "threadprivate";
13747 else switch (c_omp_predetermined_sharing (t))
13749 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13750 break;
13751 case OMP_CLAUSE_DEFAULT_SHARED:
13752 /* const vars may be specified in firstprivate clause. */
13753 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13754 && TREE_READONLY (t))
13755 break;
13756 share_name = "shared";
13757 break;
13758 case OMP_CLAUSE_DEFAULT_PRIVATE:
13759 share_name = "private";
13760 break;
13761 default:
13762 gcc_unreachable ();
13764 if (share_name)
13766 error_at (OMP_CLAUSE_LOCATION (c),
13767 "%qE is predetermined %qs for %qs",
13768 t, share_name,
13769 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13770 remove = true;
13775 if (remove)
13776 *pc = OMP_CLAUSE_CHAIN (c);
13777 else
13778 pc = &OMP_CLAUSE_CHAIN (c);
13781 if (simdlen
13782 && safelen
13783 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13784 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13786 error_at (OMP_CLAUSE_LOCATION (simdlen),
13787 "%<simdlen%> clause value is bigger than "
13788 "%<safelen%> clause value");
13789 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13790 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13793 if (ordered_seen
13794 && schedule_clause
13795 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13796 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13798 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13799 "%<nonmonotonic%> schedule modifier specified together "
13800 "with %<ordered%> clause");
13801 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13802 = (enum omp_clause_schedule_kind)
13803 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13804 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13807 if (linear_variable_step_check)
13808 for (pc = &clauses, c = clauses; c ; c = *pc)
13810 bool remove = false;
13811 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13812 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13813 && !bitmap_bit_p (&map_head,
13814 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13816 error_at (OMP_CLAUSE_LOCATION (c),
13817 "%<linear%> clause step is a parameter %qD not "
13818 "specified in %<uniform%> clause",
13819 OMP_CLAUSE_LINEAR_STEP (c));
13820 remove = true;
13823 if (remove)
13824 *pc = OMP_CLAUSE_CHAIN (c);
13825 else
13826 pc = &OMP_CLAUSE_CHAIN (c);
13829 bitmap_obstack_release (NULL);
13830 return clauses;
13833 /* Return code to initialize DST with a copy constructor from SRC.
13834 C doesn't have copy constructors nor assignment operators, only for
13835 _Atomic vars we need to perform __atomic_load from src into a temporary
13836 followed by __atomic_store of the temporary to dst. */
13838 tree
13839 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13841 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13842 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13844 location_t loc = OMP_CLAUSE_LOCATION (clause);
13845 tree type = TREE_TYPE (dst);
13846 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13847 tree tmp = create_tmp_var (nonatomic_type);
13848 tree tmp_addr = build_fold_addr_expr (tmp);
13849 TREE_ADDRESSABLE (tmp) = 1;
13850 TREE_NO_WARNING (tmp) = 1;
13851 tree src_addr = build_fold_addr_expr (src);
13852 tree dst_addr = build_fold_addr_expr (dst);
13853 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13854 vec<tree, va_gc> *params;
13855 /* Expansion of a generic atomic load may require an addition
13856 element, so allocate enough to prevent a resize. */
13857 vec_alloc (params, 4);
13859 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13860 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13861 params->quick_push (src_addr);
13862 params->quick_push (tmp_addr);
13863 params->quick_push (seq_cst);
13864 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13866 vec_alloc (params, 4);
13868 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13869 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13870 params->quick_push (dst_addr);
13871 params->quick_push (tmp_addr);
13872 params->quick_push (seq_cst);
13873 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13874 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13877 /* Create a transaction node. */
13879 tree
13880 c_finish_transaction (location_t loc, tree block, int flags)
13882 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13883 if (flags & TM_STMT_ATTR_OUTER)
13884 TRANSACTION_EXPR_OUTER (stmt) = 1;
13885 if (flags & TM_STMT_ATTR_RELAXED)
13886 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13887 return add_stmt (stmt);
13890 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13891 down to the element type of an array. If ORIG_QUAL_TYPE is not
13892 NULL, then it should be used as the qualified type
13893 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13894 preserve information about the typedef name from which an array
13895 type was derived). */
13897 tree
13898 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13899 size_t orig_qual_indirect)
13901 if (type == error_mark_node)
13902 return type;
13904 if (TREE_CODE (type) == ARRAY_TYPE)
13906 tree t;
13907 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13908 type_quals, orig_qual_type,
13909 orig_qual_indirect - 1);
13911 /* See if we already have an identically qualified type. */
13912 if (orig_qual_type && orig_qual_indirect == 0)
13913 t = orig_qual_type;
13914 else
13915 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13917 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13918 && TYPE_NAME (t) == TYPE_NAME (type)
13919 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13920 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13921 TYPE_ATTRIBUTES (type)))
13922 break;
13924 if (!t)
13926 tree domain = TYPE_DOMAIN (type);
13928 t = build_variant_type_copy (type);
13929 TREE_TYPE (t) = element_type;
13931 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13932 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13933 SET_TYPE_STRUCTURAL_EQUALITY (t);
13934 else if (TYPE_CANONICAL (element_type) != element_type
13935 || (domain && TYPE_CANONICAL (domain) != domain))
13937 tree unqualified_canon
13938 = build_array_type (TYPE_CANONICAL (element_type),
13939 domain? TYPE_CANONICAL (domain)
13940 : NULL_TREE);
13941 if (TYPE_REVERSE_STORAGE_ORDER (type))
13943 unqualified_canon
13944 = build_distinct_type_copy (unqualified_canon);
13945 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13947 TYPE_CANONICAL (t)
13948 = c_build_qualified_type (unqualified_canon, type_quals);
13950 else
13951 TYPE_CANONICAL (t) = t;
13953 return t;
13956 /* A restrict-qualified pointer type must be a pointer to object or
13957 incomplete type. Note that the use of POINTER_TYPE_P also allows
13958 REFERENCE_TYPEs, which is appropriate for C++. */
13959 if ((type_quals & TYPE_QUAL_RESTRICT)
13960 && (!POINTER_TYPE_P (type)
13961 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13963 error ("invalid use of %<restrict%>");
13964 type_quals &= ~TYPE_QUAL_RESTRICT;
13967 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13968 ? orig_qual_type
13969 : build_qualified_type (type, type_quals));
13970 /* A variant type does not inherit the list of incomplete vars from the
13971 type main variant. */
13972 if (RECORD_OR_UNION_TYPE_P (var_type)
13973 && TYPE_MAIN_VARIANT (var_type) != var_type)
13974 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13975 return var_type;
13978 /* Build a VA_ARG_EXPR for the C parser. */
13980 tree
13981 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
13983 if (error_operand_p (type))
13984 return error_mark_node;
13985 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13986 order because it takes the address of the expression. */
13987 else if (handled_component_p (expr)
13988 && reverse_storage_order_for_component_p (expr))
13990 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13991 return error_mark_node;
13993 else if (!COMPLETE_TYPE_P (type))
13995 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
13996 "type %qT", type);
13997 return error_mark_node;
13999 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14000 warning_at (loc2, OPT_Wc___compat,
14001 "C++ requires promoted type, not enum type, in %<va_arg%>");
14002 return build_va_arg (loc2, expr, type);
14005 /* Return truthvalue of whether T1 is the same tree structure as T2.
14006 Return 1 if they are the same. Return 0 if they are different. */
14008 bool
14009 c_tree_equal (tree t1, tree t2)
14011 enum tree_code code1, code2;
14013 if (t1 == t2)
14014 return true;
14015 if (!t1 || !t2)
14016 return false;
14018 for (code1 = TREE_CODE (t1);
14019 CONVERT_EXPR_CODE_P (code1)
14020 || code1 == NON_LVALUE_EXPR;
14021 code1 = TREE_CODE (t1))
14022 t1 = TREE_OPERAND (t1, 0);
14023 for (code2 = TREE_CODE (t2);
14024 CONVERT_EXPR_CODE_P (code2)
14025 || code2 == NON_LVALUE_EXPR;
14026 code2 = TREE_CODE (t2))
14027 t2 = TREE_OPERAND (t2, 0);
14029 /* They might have become equal now. */
14030 if (t1 == t2)
14031 return true;
14033 if (code1 != code2)
14034 return false;
14036 switch (code1)
14038 case INTEGER_CST:
14039 return wi::eq_p (t1, t2);
14041 case REAL_CST:
14042 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14044 case STRING_CST:
14045 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14046 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14047 TREE_STRING_LENGTH (t1));
14049 case FIXED_CST:
14050 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14051 TREE_FIXED_CST (t2));
14053 case COMPLEX_CST:
14054 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14055 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14057 case VECTOR_CST:
14058 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14060 case CONSTRUCTOR:
14061 /* We need to do this when determining whether or not two
14062 non-type pointer to member function template arguments
14063 are the same. */
14064 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14065 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14066 return false;
14068 tree field, value;
14069 unsigned int i;
14070 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14072 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14073 if (!c_tree_equal (field, elt2->index)
14074 || !c_tree_equal (value, elt2->value))
14075 return false;
14078 return true;
14080 case TREE_LIST:
14081 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14082 return false;
14083 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14084 return false;
14085 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14087 case SAVE_EXPR:
14088 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14090 case CALL_EXPR:
14092 tree arg1, arg2;
14093 call_expr_arg_iterator iter1, iter2;
14094 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14095 return false;
14096 for (arg1 = first_call_expr_arg (t1, &iter1),
14097 arg2 = first_call_expr_arg (t2, &iter2);
14098 arg1 && arg2;
14099 arg1 = next_call_expr_arg (&iter1),
14100 arg2 = next_call_expr_arg (&iter2))
14101 if (!c_tree_equal (arg1, arg2))
14102 return false;
14103 if (arg1 || arg2)
14104 return false;
14105 return true;
14108 case TARGET_EXPR:
14110 tree o1 = TREE_OPERAND (t1, 0);
14111 tree o2 = TREE_OPERAND (t2, 0);
14113 /* Special case: if either target is an unallocated VAR_DECL,
14114 it means that it's going to be unified with whatever the
14115 TARGET_EXPR is really supposed to initialize, so treat it
14116 as being equivalent to anything. */
14117 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14118 && !DECL_RTL_SET_P (o1))
14119 /*Nop*/;
14120 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14121 && !DECL_RTL_SET_P (o2))
14122 /*Nop*/;
14123 else if (!c_tree_equal (o1, o2))
14124 return false;
14126 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14129 case COMPONENT_REF:
14130 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14131 return false;
14132 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14134 case PARM_DECL:
14135 case VAR_DECL:
14136 case CONST_DECL:
14137 case FIELD_DECL:
14138 case FUNCTION_DECL:
14139 case IDENTIFIER_NODE:
14140 case SSA_NAME:
14141 return false;
14143 case TREE_VEC:
14145 unsigned ix;
14146 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14147 return false;
14148 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14149 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14150 TREE_VEC_ELT (t2, ix)))
14151 return false;
14152 return true;
14155 default:
14156 break;
14159 switch (TREE_CODE_CLASS (code1))
14161 case tcc_unary:
14162 case tcc_binary:
14163 case tcc_comparison:
14164 case tcc_expression:
14165 case tcc_vl_exp:
14166 case tcc_reference:
14167 case tcc_statement:
14169 int i, n = TREE_OPERAND_LENGTH (t1);
14171 switch (code1)
14173 case PREINCREMENT_EXPR:
14174 case PREDECREMENT_EXPR:
14175 case POSTINCREMENT_EXPR:
14176 case POSTDECREMENT_EXPR:
14177 n = 1;
14178 break;
14179 case ARRAY_REF:
14180 n = 2;
14181 break;
14182 default:
14183 break;
14186 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14187 && n != TREE_OPERAND_LENGTH (t2))
14188 return false;
14190 for (i = 0; i < n; ++i)
14191 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14192 return false;
14194 return true;
14197 case tcc_type:
14198 return comptypes (t1, t2);
14199 default:
14200 gcc_unreachable ();
14202 /* We can get here with --disable-checking. */
14203 return false;
14206 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14207 spawn-helper and BODY is the newly created body for FNDECL. */
14209 void
14210 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14212 tree list = alloc_stmt_list ();
14213 tree frame = make_cilk_frame (fndecl);
14214 tree dtor = create_cilk_function_exit (frame, false, true);
14215 add_local_decl (cfun, frame);
14217 DECL_SAVED_TREE (fndecl) = list;
14219 tree body_list = alloc_stmt_list ();
14220 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14221 body = fold_build_cleanup_point_expr (void_type_node, body);
14223 append_to_statement_list (body, &body_list);
14224 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14225 body_list, dtor), &list);
14228 /* Returns true when the function declaration FNDECL is implicit,
14229 introduced as a result of a call to an otherwise undeclared
14230 function, and false otherwise. */
14232 bool
14233 c_decl_implicit (const_tree fndecl)
14235 return C_DECL_IMPLICIT (fndecl);