[AArch64] Remove use of wider vector modes
[official-gcc.git] / gcc / c / c-typeck.c
blob135dd9d665cb099a17d942f1ba452a999db2974c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "cilk.h"
50 #include "gomp-constants.h"
51 #include "spellcheck-tree.h"
52 #include "gcc-rich-location.h"
53 #include "stringpool.h"
54 #include "attribs.h"
55 #include "asan.h"
57 /* Possible cases of implicit bad conversions. Used to select
58 diagnostic messages in convert_for_assignment. */
59 enum impl_conv {
60 ic_argpass,
61 ic_assign,
62 ic_init,
63 ic_return
66 /* The level of nesting inside "__alignof__". */
67 int in_alignof;
69 /* The level of nesting inside "sizeof". */
70 int in_sizeof;
72 /* The level of nesting inside "typeof". */
73 int in_typeof;
75 /* The argument of last parsed sizeof expression, only to be tested
76 if expr.original_code == SIZEOF_EXPR. */
77 tree c_last_sizeof_arg;
78 location_t c_last_sizeof_loc;
80 /* Nonzero if we might need to print a "missing braces around
81 initializer" message within this initializer. */
82 static int found_missing_braces;
84 static int require_constant_value;
85 static int require_constant_elements;
87 static bool null_pointer_constant_p (const_tree);
88 static tree qualify_type (tree, tree);
89 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
90 bool *);
91 static int comp_target_types (location_t, tree, tree);
92 static int function_types_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
95 static tree lookup_field (tree, tree);
96 static int convert_arguments (location_t, vec<location_t>, tree,
97 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
98 tree);
99 static tree pointer_diff (location_t, tree, tree);
100 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
101 enum impl_conv, bool, tree, tree, int);
102 static tree valid_compound_expr_initializer (tree, tree);
103 static void push_string (const char *);
104 static void push_member_name (tree);
105 static int spelling_length (void);
106 static char *print_spelling (char *);
107 static void warning_init (location_t, int, const char *);
108 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
109 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
110 bool, struct obstack *);
111 static void output_pending_init_elements (int, struct obstack *);
112 static bool set_designator (location_t, bool, struct obstack *);
113 static void push_range_stack (tree, struct obstack *);
114 static void add_pending_init (location_t, tree, tree, tree, bool,
115 struct obstack *);
116 static void set_nonincremental_init (struct obstack *);
117 static void set_nonincremental_init_from_string (tree, struct obstack *);
118 static tree find_init_member (tree, struct obstack *);
119 static void readonly_warning (tree, enum lvalue_use);
120 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
121 static void record_maybe_used_decl (tree);
122 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
124 /* Return true if EXP is a null pointer constant, false otherwise. */
126 static bool
127 null_pointer_constant_p (const_tree expr)
129 /* This should really operate on c_expr structures, but they aren't
130 yet available everywhere required. */
131 tree type = TREE_TYPE (expr);
132 return (TREE_CODE (expr) == INTEGER_CST
133 && !TREE_OVERFLOW (expr)
134 && integer_zerop (expr)
135 && (INTEGRAL_TYPE_P (type)
136 || (TREE_CODE (type) == POINTER_TYPE
137 && VOID_TYPE_P (TREE_TYPE (type))
138 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
141 /* EXPR may appear in an unevaluated part of an integer constant
142 expression, but not in an evaluated part. Wrap it in a
143 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
144 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
146 static tree
147 note_integer_operands (tree expr)
149 tree ret;
150 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
152 ret = copy_node (expr);
153 TREE_OVERFLOW (ret) = 1;
155 else
157 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
158 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
160 return ret;
163 /* Having checked whether EXPR may appear in an unevaluated part of an
164 integer constant expression and found that it may, remove any
165 C_MAYBE_CONST_EXPR noting this fact and return the resulting
166 expression. */
168 static inline tree
169 remove_c_maybe_const_expr (tree expr)
171 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
172 return C_MAYBE_CONST_EXPR_EXPR (expr);
173 else
174 return expr;
177 \f/* This is a cache to hold if two types are compatible or not. */
179 struct tagged_tu_seen_cache {
180 const struct tagged_tu_seen_cache * next;
181 const_tree t1;
182 const_tree t2;
183 /* The return value of tagged_types_tu_compatible_p if we had seen
184 these two types already. */
185 int val;
188 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
189 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
191 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
192 does not have an incomplete type. (That includes void types.)
193 LOC is the location of the use. */
195 tree
196 require_complete_type (location_t loc, tree value)
198 tree type = TREE_TYPE (value);
200 if (error_operand_p (value))
201 return error_mark_node;
203 /* First, detect a valid value with a complete type. */
204 if (COMPLETE_TYPE_P (type))
205 return value;
207 c_incomplete_type_error (loc, value, type);
208 return error_mark_node;
211 /* Print an error message for invalid use of an incomplete type.
212 VALUE is the expression that was used (or 0 if that isn't known)
213 and TYPE is the type that was invalid. LOC is the location for
214 the error. */
216 void
217 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
219 /* Avoid duplicate error message. */
220 if (TREE_CODE (type) == ERROR_MARK)
221 return;
223 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
224 error_at (loc, "%qD has an incomplete type %qT", value, type);
225 else
227 retry:
228 /* We must print an error message. Be clever about what it says. */
230 switch (TREE_CODE (type))
232 case RECORD_TYPE:
233 case UNION_TYPE:
234 case ENUMERAL_TYPE:
235 break;
237 case VOID_TYPE:
238 error_at (loc, "invalid use of void expression");
239 return;
241 case ARRAY_TYPE:
242 if (TYPE_DOMAIN (type))
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
246 error_at (loc, "invalid use of flexible array member");
247 return;
249 type = TREE_TYPE (type);
250 goto retry;
252 error_at (loc, "invalid use of array with unspecified bounds");
253 return;
255 default:
256 gcc_unreachable ();
259 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
260 error_at (loc, "invalid use of undefined type %qT", type);
261 else
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error_at (loc, "invalid use of incomplete typedef %qT", type);
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
270 tree
271 c_type_promotes_to (tree type)
273 tree ret = NULL_TREE;
275 if (TYPE_MAIN_VARIANT (type) == float_type_node)
276 ret = double_type_node;
277 else if (c_promoting_integer_type_p (type))
279 /* Preserve unsignedness if not really getting any wider. */
280 if (TYPE_UNSIGNED (type)
281 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
282 ret = unsigned_type_node;
283 else
284 ret = integer_type_node;
287 if (ret != NULL_TREE)
288 return (TYPE_ATOMIC (type)
289 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
290 : ret);
292 return type;
295 /* Return true if between two named address spaces, whether there is a superset
296 named address space that encompasses both address spaces. If there is a
297 superset, return which address space is the superset. */
299 static bool
300 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
302 if (as1 == as2)
304 *common = as1;
305 return true;
307 else if (targetm.addr_space.subset_p (as1, as2))
309 *common = as2;
310 return true;
312 else if (targetm.addr_space.subset_p (as2, as1))
314 *common = as1;
315 return true;
317 else
318 return false;
321 /* Return a variant of TYPE which has all the type qualifiers of LIKE
322 as well as those of TYPE. */
324 static tree
325 qualify_type (tree type, tree like)
327 addr_space_t as_type = TYPE_ADDR_SPACE (type);
328 addr_space_t as_like = TYPE_ADDR_SPACE (like);
329 addr_space_t as_common;
331 /* If the two named address spaces are different, determine the common
332 superset address space. If there isn't one, raise an error. */
333 if (!addr_space_superset (as_type, as_like, &as_common))
335 as_common = as_type;
336 error ("%qT and %qT are in disjoint named address spaces",
337 type, like);
340 return c_build_qualified_type (type,
341 TYPE_QUALS_NO_ADDR_SPACE (type)
342 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
343 | ENCODE_QUAL_ADDR_SPACE (as_common));
346 /* Return true iff the given tree T is a variable length array. */
348 bool
349 c_vla_type_p (const_tree t)
351 if (TREE_CODE (t) == ARRAY_TYPE
352 && C_TYPE_VARIABLE_SIZE (t))
353 return true;
354 return false;
357 /* Return the composite type of two compatible types.
359 We assume that comptypes has already been done and returned
360 nonzero; if that isn't so, this may crash. In particular, we
361 assume that qualifiers match. */
363 tree
364 composite_type (tree t1, tree t2)
366 enum tree_code code1;
367 enum tree_code code2;
368 tree attributes;
370 /* Save time if the two types are the same. */
372 if (t1 == t2) return t1;
374 /* If one type is nonsense, use the other. */
375 if (t1 == error_mark_node)
376 return t2;
377 if (t2 == error_mark_node)
378 return t1;
380 code1 = TREE_CODE (t1);
381 code2 = TREE_CODE (t2);
383 /* Merge the attributes. */
384 attributes = targetm.merge_type_attributes (t1, t2);
386 /* If one is an enumerated type and the other is the compatible
387 integer type, the composite type might be either of the two
388 (DR#013 question 3). For consistency, use the enumerated type as
389 the composite type. */
391 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
392 return t1;
393 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
394 return t2;
396 gcc_assert (code1 == code2);
398 switch (code1)
400 case POINTER_TYPE:
401 /* For two pointers, do this recursively on the target type. */
403 tree pointed_to_1 = TREE_TYPE (t1);
404 tree pointed_to_2 = TREE_TYPE (t2);
405 tree target = composite_type (pointed_to_1, pointed_to_2);
406 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
407 t1 = build_type_attribute_variant (t1, attributes);
408 return qualify_type (t1, t2);
411 case ARRAY_TYPE:
413 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
414 int quals;
415 tree unqual_elt;
416 tree d1 = TYPE_DOMAIN (t1);
417 tree d2 = TYPE_DOMAIN (t2);
418 bool d1_variable, d2_variable;
419 bool d1_zero, d2_zero;
420 bool t1_complete, t2_complete;
422 /* We should not have any type quals on arrays at all. */
423 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
424 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
426 t1_complete = COMPLETE_TYPE_P (t1);
427 t2_complete = COMPLETE_TYPE_P (t2);
429 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
430 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
432 d1_variable = (!d1_zero
433 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
434 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
435 d2_variable = (!d2_zero
436 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
437 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
438 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
439 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
441 /* Save space: see if the result is identical to one of the args. */
442 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
443 && (d2_variable || d2_zero || !d1_variable))
444 return build_type_attribute_variant (t1, attributes);
445 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
446 && (d1_variable || d1_zero || !d2_variable))
447 return build_type_attribute_variant (t2, attributes);
449 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
450 return build_type_attribute_variant (t1, attributes);
451 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
452 return build_type_attribute_variant (t2, attributes);
454 /* Merge the element types, and have a size if either arg has
455 one. We may have qualifiers on the element types. To set
456 up TYPE_MAIN_VARIANT correctly, we need to form the
457 composite of the unqualified types and add the qualifiers
458 back at the end. */
459 quals = TYPE_QUALS (strip_array_types (elt));
460 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
461 t1 = build_array_type (unqual_elt,
462 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
463 && (d2_variable
464 || d2_zero
465 || !d1_variable))
466 ? t1
467 : t2));
468 /* Ensure a composite type involving a zero-length array type
469 is a zero-length type not an incomplete type. */
470 if (d1_zero && d2_zero
471 && (t1_complete || t2_complete)
472 && !COMPLETE_TYPE_P (t1))
474 TYPE_SIZE (t1) = bitsize_zero_node;
475 TYPE_SIZE_UNIT (t1) = size_zero_node;
477 t1 = c_build_qualified_type (t1, quals);
478 return build_type_attribute_variant (t1, attributes);
481 case ENUMERAL_TYPE:
482 case RECORD_TYPE:
483 case UNION_TYPE:
484 if (attributes != NULL)
486 /* Try harder not to create a new aggregate type. */
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
488 return t1;
489 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
490 return t2;
492 return build_type_attribute_variant (t1, attributes);
494 case FUNCTION_TYPE:
495 /* Function types: prefer the one that specified arg types.
496 If both do, merge the arg types. Also merge the return types. */
498 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
499 tree p1 = TYPE_ARG_TYPES (t1);
500 tree p2 = TYPE_ARG_TYPES (t2);
501 int len;
502 tree newargs, n;
503 int i;
505 /* Save space: see if the result is identical to one of the args. */
506 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
507 return build_type_attribute_variant (t1, attributes);
508 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
509 return build_type_attribute_variant (t2, attributes);
511 /* Simple way if one arg fails to specify argument types. */
512 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
514 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
515 t1 = build_type_attribute_variant (t1, attributes);
516 return qualify_type (t1, t2);
518 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
520 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
521 t1 = build_type_attribute_variant (t1, attributes);
522 return qualify_type (t1, t2);
525 /* If both args specify argument types, we must merge the two
526 lists, argument by argument. */
528 for (len = 0, newargs = p1;
529 newargs && newargs != void_list_node;
530 len++, newargs = TREE_CHAIN (newargs))
533 for (i = 0; i < len; i++)
534 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
536 n = newargs;
538 for (; p1 && p1 != void_list_node;
539 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
541 /* A null type means arg type is not specified.
542 Take whatever the other function type has. */
543 if (TREE_VALUE (p1) == NULL_TREE)
545 TREE_VALUE (n) = TREE_VALUE (p2);
546 goto parm_done;
548 if (TREE_VALUE (p2) == NULL_TREE)
550 TREE_VALUE (n) = TREE_VALUE (p1);
551 goto parm_done;
554 /* Given wait (union {union wait *u; int *i} *)
555 and wait (union wait *),
556 prefer union wait * as type of parm. */
557 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
558 && TREE_VALUE (p1) != TREE_VALUE (p2))
560 tree memb;
561 tree mv2 = TREE_VALUE (p2);
562 if (mv2 && mv2 != error_mark_node
563 && TREE_CODE (mv2) != ARRAY_TYPE)
564 mv2 = TYPE_MAIN_VARIANT (mv2);
565 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
566 memb; memb = DECL_CHAIN (memb))
568 tree mv3 = TREE_TYPE (memb);
569 if (mv3 && mv3 != error_mark_node
570 && TREE_CODE (mv3) != ARRAY_TYPE)
571 mv3 = TYPE_MAIN_VARIANT (mv3);
572 if (comptypes (mv3, mv2))
574 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
575 TREE_VALUE (p2));
576 pedwarn (input_location, OPT_Wpedantic,
577 "function types not truly compatible in ISO C");
578 goto parm_done;
582 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
583 && TREE_VALUE (p2) != TREE_VALUE (p1))
585 tree memb;
586 tree mv1 = TREE_VALUE (p1);
587 if (mv1 && mv1 != error_mark_node
588 && TREE_CODE (mv1) != ARRAY_TYPE)
589 mv1 = TYPE_MAIN_VARIANT (mv1);
590 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
591 memb; memb = DECL_CHAIN (memb))
593 tree mv3 = TREE_TYPE (memb);
594 if (mv3 && mv3 != error_mark_node
595 && TREE_CODE (mv3) != ARRAY_TYPE)
596 mv3 = TYPE_MAIN_VARIANT (mv3);
597 if (comptypes (mv3, mv1))
599 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
600 TREE_VALUE (p1));
601 pedwarn (input_location, OPT_Wpedantic,
602 "function types not truly compatible in ISO C");
603 goto parm_done;
607 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
608 parm_done: ;
611 t1 = build_function_type (valtype, newargs);
612 t1 = qualify_type (t1, t2);
614 /* FALLTHRU */
616 default:
617 return build_type_attribute_variant (t1, attributes);
622 /* Return the type of a conditional expression between pointers to
623 possibly differently qualified versions of compatible types.
625 We assume that comp_target_types has already been done and returned
626 nonzero; if that isn't so, this may crash. */
628 static tree
629 common_pointer_type (tree t1, tree t2)
631 tree attributes;
632 tree pointed_to_1, mv1;
633 tree pointed_to_2, mv2;
634 tree target;
635 unsigned target_quals;
636 addr_space_t as1, as2, as_common;
637 int quals1, quals2;
639 /* Save time if the two types are the same. */
641 if (t1 == t2) return t1;
643 /* If one type is nonsense, use the other. */
644 if (t1 == error_mark_node)
645 return t2;
646 if (t2 == error_mark_node)
647 return t1;
649 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
650 && TREE_CODE (t2) == POINTER_TYPE);
652 /* Merge the attributes. */
653 attributes = targetm.merge_type_attributes (t1, t2);
655 /* Find the composite type of the target types, and combine the
656 qualifiers of the two types' targets. Do not lose qualifiers on
657 array element types by taking the TYPE_MAIN_VARIANT. */
658 mv1 = pointed_to_1 = TREE_TYPE (t1);
659 mv2 = pointed_to_2 = TREE_TYPE (t2);
660 if (TREE_CODE (mv1) != ARRAY_TYPE)
661 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
662 if (TREE_CODE (mv2) != ARRAY_TYPE)
663 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
664 target = composite_type (mv1, mv2);
666 /* Strip array types to get correct qualifier for pointers to arrays */
667 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
668 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
670 /* For function types do not merge const qualifiers, but drop them
671 if used inconsistently. The middle-end uses these to mark const
672 and noreturn functions. */
673 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
674 target_quals = (quals1 & quals2);
675 else
676 target_quals = (quals1 | quals2);
678 /* If the two named address spaces are different, determine the common
679 superset address space. This is guaranteed to exist due to the
680 assumption that comp_target_type returned non-zero. */
681 as1 = TYPE_ADDR_SPACE (pointed_to_1);
682 as2 = TYPE_ADDR_SPACE (pointed_to_2);
683 if (!addr_space_superset (as1, as2, &as_common))
684 gcc_unreachable ();
686 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
688 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
689 return build_type_attribute_variant (t1, attributes);
692 /* Return the common type for two arithmetic types under the usual
693 arithmetic conversions. The default conversions have already been
694 applied, and enumerated types converted to their compatible integer
695 types. The resulting type is unqualified and has no attributes.
697 This is the type for the result of most arithmetic operations
698 if the operands have the given two types. */
700 static tree
701 c_common_type (tree t1, tree t2)
703 enum tree_code code1;
704 enum tree_code code2;
706 /* If one type is nonsense, use the other. */
707 if (t1 == error_mark_node)
708 return t2;
709 if (t2 == error_mark_node)
710 return t1;
712 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
713 t1 = TYPE_MAIN_VARIANT (t1);
715 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
716 t2 = TYPE_MAIN_VARIANT (t2);
718 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
719 t1 = build_type_attribute_variant (t1, NULL_TREE);
721 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
722 t2 = build_type_attribute_variant (t2, NULL_TREE);
724 /* Save time if the two types are the same. */
726 if (t1 == t2) return t1;
728 code1 = TREE_CODE (t1);
729 code2 = TREE_CODE (t2);
731 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
732 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
733 || code1 == INTEGER_TYPE);
734 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
735 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
736 || code2 == INTEGER_TYPE);
738 /* When one operand is a decimal float type, the other operand cannot be
739 a generic float type or a complex type. We also disallow vector types
740 here. */
741 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
742 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
744 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
746 error ("can%'t mix operands of decimal float and vector types");
747 return error_mark_node;
749 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
751 error ("can%'t mix operands of decimal float and complex types");
752 return error_mark_node;
754 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
756 error ("can%'t mix operands of decimal float and other float types");
757 return error_mark_node;
761 /* If one type is a vector type, return that type. (How the usual
762 arithmetic conversions apply to the vector types extension is not
763 precisely specified.) */
764 if (code1 == VECTOR_TYPE)
765 return t1;
767 if (code2 == VECTOR_TYPE)
768 return t2;
770 /* If one type is complex, form the common type of the non-complex
771 components, then make that complex. Use T1 or T2 if it is the
772 required type. */
773 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
775 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
776 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
777 tree subtype = c_common_type (subtype1, subtype2);
779 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
780 return t1;
781 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
782 return t2;
783 else
784 return build_complex_type (subtype);
787 /* If only one is real, use it as the result. */
789 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
790 return t1;
792 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
793 return t2;
795 /* If both are real and either are decimal floating point types, use
796 the decimal floating point type with the greater precision. */
798 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
800 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
801 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
802 return dfloat128_type_node;
803 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
804 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
805 return dfloat64_type_node;
806 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
807 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
808 return dfloat32_type_node;
811 /* Deal with fixed-point types. */
812 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
814 unsigned int unsignedp = 0, satp = 0;
815 scalar_mode m1, m2;
816 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
818 m1 = SCALAR_TYPE_MODE (t1);
819 m2 = SCALAR_TYPE_MODE (t2);
821 /* If one input type is saturating, the result type is saturating. */
822 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
823 satp = 1;
825 /* If both fixed-point types are unsigned, the result type is unsigned.
826 When mixing fixed-point and integer types, follow the sign of the
827 fixed-point type.
828 Otherwise, the result type is signed. */
829 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
830 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
831 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t1))
833 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t2)))
835 unsignedp = 1;
837 /* The result type is signed. */
838 if (unsignedp == 0)
840 /* If the input type is unsigned, we need to convert to the
841 signed type. */
842 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
844 enum mode_class mclass = (enum mode_class) 0;
845 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
846 mclass = MODE_FRACT;
847 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
848 mclass = MODE_ACCUM;
849 else
850 gcc_unreachable ();
851 m1 = as_a <scalar_mode>
852 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
854 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
856 enum mode_class mclass = (enum mode_class) 0;
857 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
858 mclass = MODE_FRACT;
859 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
860 mclass = MODE_ACCUM;
861 else
862 gcc_unreachable ();
863 m2 = as_a <scalar_mode>
864 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
868 if (code1 == FIXED_POINT_TYPE)
870 fbit1 = GET_MODE_FBIT (m1);
871 ibit1 = GET_MODE_IBIT (m1);
873 else
875 fbit1 = 0;
876 /* Signed integers need to subtract one sign bit. */
877 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
880 if (code2 == FIXED_POINT_TYPE)
882 fbit2 = GET_MODE_FBIT (m2);
883 ibit2 = GET_MODE_IBIT (m2);
885 else
887 fbit2 = 0;
888 /* Signed integers need to subtract one sign bit. */
889 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
892 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
893 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
894 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
895 satp);
898 /* Both real or both integers; use the one with greater precision. */
900 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
901 return t1;
902 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
903 return t2;
905 /* Same precision. Prefer long longs to longs to ints when the
906 same precision, following the C99 rules on integer type rank
907 (which are equivalent to the C90 rules for C90 types). */
909 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
911 return long_long_unsigned_type_node;
913 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
914 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
916 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
917 return long_long_unsigned_type_node;
918 else
919 return long_long_integer_type_node;
922 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
923 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
924 return long_unsigned_type_node;
926 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
927 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
929 /* But preserve unsignedness from the other type,
930 since long cannot hold all the values of an unsigned int. */
931 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
932 return long_unsigned_type_node;
933 else
934 return long_integer_type_node;
937 /* For floating types of the same TYPE_PRECISION (which we here
938 assume means either the same set of values, or sets of values
939 neither a subset of the other, with behavior being undefined in
940 the latter case), follow the rules from TS 18661-3: prefer
941 interchange types _FloatN, then standard types long double,
942 double, float, then extended types _FloatNx. For extended types,
943 check them starting with _Float128x as that seems most consistent
944 in spirit with preferring long double to double; for interchange
945 types, also check in that order for consistency although it's not
946 possible for more than one of them to have the same
947 precision. */
948 tree mv1 = TYPE_MAIN_VARIANT (t1);
949 tree mv2 = TYPE_MAIN_VARIANT (t2);
951 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
952 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
953 return FLOATN_TYPE_NODE (i);
955 /* Likewise, prefer long double to double even if same size. */
956 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
957 return long_double_type_node;
959 /* Likewise, prefer double to float even if same size.
960 We got a couple of embedded targets with 32 bit doubles, and the
961 pdp11 might have 64 bit floats. */
962 if (mv1 == double_type_node || mv2 == double_type_node)
963 return double_type_node;
965 if (mv1 == float_type_node || mv2 == float_type_node)
966 return float_type_node;
968 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
969 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
970 return FLOATNX_TYPE_NODE (i);
972 /* Otherwise prefer the unsigned one. */
974 if (TYPE_UNSIGNED (t1))
975 return t1;
976 else
977 return t2;
980 /* Wrapper around c_common_type that is used by c-common.c and other
981 front end optimizations that remove promotions. ENUMERAL_TYPEs
982 are allowed here and are converted to their compatible integer types.
983 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
984 preferably a non-Boolean type as the common type. */
985 tree
986 common_type (tree t1, tree t2)
988 if (TREE_CODE (t1) == ENUMERAL_TYPE)
989 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
990 if (TREE_CODE (t2) == ENUMERAL_TYPE)
991 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
993 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
994 if (TREE_CODE (t1) == BOOLEAN_TYPE
995 && TREE_CODE (t2) == BOOLEAN_TYPE)
996 return boolean_type_node;
998 /* If either type is BOOLEAN_TYPE, then return the other. */
999 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1000 return t2;
1001 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1002 return t1;
1004 return c_common_type (t1, t2);
1007 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1008 or various other operations. Return 2 if they are compatible
1009 but a warning may be needed if you use them together. */
1012 comptypes (tree type1, tree type2)
1014 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1015 int val;
1017 val = comptypes_internal (type1, type2, NULL, NULL);
1018 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1020 return val;
1023 /* Like comptypes, but if it returns non-zero because enum and int are
1024 compatible, it sets *ENUM_AND_INT_P to true. */
1026 static int
1027 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1029 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1030 int val;
1032 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1033 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1035 return val;
1038 /* Like comptypes, but if it returns nonzero for different types, it
1039 sets *DIFFERENT_TYPES_P to true. */
1042 comptypes_check_different_types (tree type1, tree type2,
1043 bool *different_types_p)
1045 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1046 int val;
1048 val = comptypes_internal (type1, type2, NULL, different_types_p);
1049 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1051 return val;
1054 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1055 or various other operations. Return 2 if they are compatible
1056 but a warning may be needed if you use them together. If
1057 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1058 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1059 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1060 NULL, and the types are compatible but different enough not to be
1061 permitted in C11 typedef redeclarations, then this sets
1062 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1063 false, but may or may not be set if the types are incompatible.
1064 This differs from comptypes, in that we don't free the seen
1065 types. */
1067 static int
1068 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1069 bool *different_types_p)
1071 const_tree t1 = type1;
1072 const_tree t2 = type2;
1073 int attrval, val;
1075 /* Suppress errors caused by previously reported errors. */
1077 if (t1 == t2 || !t1 || !t2
1078 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1079 return 1;
1081 /* Enumerated types are compatible with integer types, but this is
1082 not transitive: two enumerated types in the same translation unit
1083 are compatible with each other only if they are the same type. */
1085 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1087 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1088 if (TREE_CODE (t2) != VOID_TYPE)
1090 if (enum_and_int_p != NULL)
1091 *enum_and_int_p = true;
1092 if (different_types_p != NULL)
1093 *different_types_p = true;
1096 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1098 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1099 if (TREE_CODE (t1) != VOID_TYPE)
1101 if (enum_and_int_p != NULL)
1102 *enum_and_int_p = true;
1103 if (different_types_p != NULL)
1104 *different_types_p = true;
1108 if (t1 == t2)
1109 return 1;
1111 /* Different classes of types can't be compatible. */
1113 if (TREE_CODE (t1) != TREE_CODE (t2))
1114 return 0;
1116 /* Qualifiers must match. C99 6.7.3p9 */
1118 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1119 return 0;
1121 /* Allow for two different type nodes which have essentially the same
1122 definition. Note that we already checked for equality of the type
1123 qualifiers (just above). */
1125 if (TREE_CODE (t1) != ARRAY_TYPE
1126 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1127 return 1;
1129 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1130 if (!(attrval = comp_type_attributes (t1, t2)))
1131 return 0;
1133 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1134 val = 0;
1136 switch (TREE_CODE (t1))
1138 case INTEGER_TYPE:
1139 case FIXED_POINT_TYPE:
1140 case REAL_TYPE:
1141 /* With these nodes, we can't determine type equivalence by
1142 looking at what is stored in the nodes themselves, because
1143 two nodes might have different TYPE_MAIN_VARIANTs but still
1144 represent the same type. For example, wchar_t and int could
1145 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1146 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1147 and are distinct types. On the other hand, int and the
1148 following typedef
1150 typedef int INT __attribute((may_alias));
1152 have identical properties, different TYPE_MAIN_VARIANTs, but
1153 represent the same type. The canonical type system keeps
1154 track of equivalence in this case, so we fall back on it. */
1155 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1157 case POINTER_TYPE:
1158 /* Do not remove mode information. */
1159 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1160 break;
1161 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1162 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1163 enum_and_int_p, different_types_p));
1164 break;
1166 case FUNCTION_TYPE:
1167 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1168 different_types_p);
1169 break;
1171 case ARRAY_TYPE:
1173 tree d1 = TYPE_DOMAIN (t1);
1174 tree d2 = TYPE_DOMAIN (t2);
1175 bool d1_variable, d2_variable;
1176 bool d1_zero, d2_zero;
1177 val = 1;
1179 /* Target types must match incl. qualifiers. */
1180 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1181 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1182 enum_and_int_p,
1183 different_types_p)))
1184 return 0;
1186 if (different_types_p != NULL
1187 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1188 *different_types_p = true;
1189 /* Sizes must match unless one is missing or variable. */
1190 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1191 break;
1193 d1_zero = !TYPE_MAX_VALUE (d1);
1194 d2_zero = !TYPE_MAX_VALUE (d2);
1196 d1_variable = (!d1_zero
1197 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1198 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1199 d2_variable = (!d2_zero
1200 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1201 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1202 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1203 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1205 if (different_types_p != NULL
1206 && d1_variable != d2_variable)
1207 *different_types_p = true;
1208 if (d1_variable || d2_variable)
1209 break;
1210 if (d1_zero && d2_zero)
1211 break;
1212 if (d1_zero || d2_zero
1213 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1214 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1215 val = 0;
1217 break;
1220 case ENUMERAL_TYPE:
1221 case RECORD_TYPE:
1222 case UNION_TYPE:
1223 if (val != 1 && !same_translation_unit_p (t1, t2))
1225 tree a1 = TYPE_ATTRIBUTES (t1);
1226 tree a2 = TYPE_ATTRIBUTES (t2);
1228 if (! attribute_list_contained (a1, a2)
1229 && ! attribute_list_contained (a2, a1))
1230 break;
1232 if (attrval != 2)
1233 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1234 different_types_p);
1235 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1236 different_types_p);
1238 break;
1240 case VECTOR_TYPE:
1241 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1242 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1243 enum_and_int_p, different_types_p));
1244 break;
1246 default:
1247 break;
1249 return attrval == 2 && val == 1 ? 2 : val;
1252 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1253 their qualifiers, except for named address spaces. If the pointers point to
1254 different named addresses, then we must determine if one address space is a
1255 subset of the other. */
1257 static int
1258 comp_target_types (location_t location, tree ttl, tree ttr)
1260 int val;
1261 int val_ped;
1262 tree mvl = TREE_TYPE (ttl);
1263 tree mvr = TREE_TYPE (ttr);
1264 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1265 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1266 addr_space_t as_common;
1267 bool enum_and_int_p;
1269 /* Fail if pointers point to incompatible address spaces. */
1270 if (!addr_space_superset (asl, asr, &as_common))
1271 return 0;
1273 /* For pedantic record result of comptypes on arrays before losing
1274 qualifiers on the element type below. */
1275 val_ped = 1;
1277 if (TREE_CODE (mvl) == ARRAY_TYPE
1278 && TREE_CODE (mvr) == ARRAY_TYPE)
1279 val_ped = comptypes (mvl, mvr);
1281 /* Qualifiers on element types of array types that are
1282 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1284 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1285 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1286 : TYPE_MAIN_VARIANT (mvl));
1288 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1289 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1290 : TYPE_MAIN_VARIANT (mvr));
1292 enum_and_int_p = false;
1293 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1295 if (val == 1 && val_ped != 1)
1296 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1297 "are incompatible in ISO C");
1299 if (val == 2)
1300 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1302 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1303 warning_at (location, OPT_Wc___compat,
1304 "pointer target types incompatible in C++");
1306 return val;
1309 /* Subroutines of `comptypes'. */
1311 /* Determine whether two trees derive from the same translation unit.
1312 If the CONTEXT chain ends in a null, that tree's context is still
1313 being parsed, so if two trees have context chains ending in null,
1314 they're in the same translation unit. */
1316 bool
1317 same_translation_unit_p (const_tree t1, const_tree t2)
1319 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1320 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1322 case tcc_declaration:
1323 t1 = DECL_CONTEXT (t1); break;
1324 case tcc_type:
1325 t1 = TYPE_CONTEXT (t1); break;
1326 case tcc_exceptional:
1327 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1328 default: gcc_unreachable ();
1331 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1332 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1334 case tcc_declaration:
1335 t2 = DECL_CONTEXT (t2); break;
1336 case tcc_type:
1337 t2 = TYPE_CONTEXT (t2); break;
1338 case tcc_exceptional:
1339 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1340 default: gcc_unreachable ();
1343 return t1 == t2;
1346 /* Allocate the seen two types, assuming that they are compatible. */
1348 static struct tagged_tu_seen_cache *
1349 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1351 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1352 tu->next = tagged_tu_seen_base;
1353 tu->t1 = t1;
1354 tu->t2 = t2;
1356 tagged_tu_seen_base = tu;
1358 /* The C standard says that two structures in different translation
1359 units are compatible with each other only if the types of their
1360 fields are compatible (among other things). We assume that they
1361 are compatible until proven otherwise when building the cache.
1362 An example where this can occur is:
1363 struct a
1365 struct a *next;
1367 If we are comparing this against a similar struct in another TU,
1368 and did not assume they were compatible, we end up with an infinite
1369 loop. */
1370 tu->val = 1;
1371 return tu;
1374 /* Free the seen types until we get to TU_TIL. */
1376 static void
1377 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1379 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1380 while (tu != tu_til)
1382 const struct tagged_tu_seen_cache *const tu1
1383 = (const struct tagged_tu_seen_cache *) tu;
1384 tu = tu1->next;
1385 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1387 tagged_tu_seen_base = tu_til;
1390 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1391 compatible. If the two types are not the same (which has been
1392 checked earlier), this can only happen when multiple translation
1393 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1394 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1395 comptypes_internal. */
1397 static int
1398 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1399 bool *enum_and_int_p, bool *different_types_p)
1401 tree s1, s2;
1402 bool needs_warning = false;
1404 /* We have to verify that the tags of the types are the same. This
1405 is harder than it looks because this may be a typedef, so we have
1406 to go look at the original type. It may even be a typedef of a
1407 typedef...
1408 In the case of compiler-created builtin structs the TYPE_DECL
1409 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1410 while (TYPE_NAME (t1)
1411 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1412 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1413 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1415 while (TYPE_NAME (t2)
1416 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1417 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1418 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1420 /* C90 didn't have the requirement that the two tags be the same. */
1421 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1422 return 0;
1424 /* C90 didn't say what happened if one or both of the types were
1425 incomplete; we choose to follow C99 rules here, which is that they
1426 are compatible. */
1427 if (TYPE_SIZE (t1) == NULL
1428 || TYPE_SIZE (t2) == NULL)
1429 return 1;
1432 const struct tagged_tu_seen_cache * tts_i;
1433 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1434 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1435 return tts_i->val;
1438 switch (TREE_CODE (t1))
1440 case ENUMERAL_TYPE:
1442 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1443 /* Speed up the case where the type values are in the same order. */
1444 tree tv1 = TYPE_VALUES (t1);
1445 tree tv2 = TYPE_VALUES (t2);
1447 if (tv1 == tv2)
1449 return 1;
1452 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1454 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1455 break;
1456 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1458 tu->val = 0;
1459 return 0;
1463 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1465 return 1;
1467 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1469 tu->val = 0;
1470 return 0;
1473 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1475 tu->val = 0;
1476 return 0;
1479 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1481 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1482 if (s2 == NULL
1483 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1485 tu->val = 0;
1486 return 0;
1489 return 1;
1492 case UNION_TYPE:
1494 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1495 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1497 tu->val = 0;
1498 return 0;
1501 /* Speed up the common case where the fields are in the same order. */
1502 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1503 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1505 int result;
1507 if (DECL_NAME (s1) != DECL_NAME (s2))
1508 break;
1509 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1510 enum_and_int_p, different_types_p);
1512 if (result != 1 && !DECL_NAME (s1))
1513 break;
1514 if (result == 0)
1516 tu->val = 0;
1517 return 0;
1519 if (result == 2)
1520 needs_warning = true;
1522 if (TREE_CODE (s1) == FIELD_DECL
1523 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1524 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1526 tu->val = 0;
1527 return 0;
1530 if (!s1 && !s2)
1532 tu->val = needs_warning ? 2 : 1;
1533 return tu->val;
1536 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1538 bool ok = false;
1540 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1541 if (DECL_NAME (s1) == DECL_NAME (s2))
1543 int result;
1545 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1546 enum_and_int_p,
1547 different_types_p);
1549 if (result != 1 && !DECL_NAME (s1))
1550 continue;
1551 if (result == 0)
1553 tu->val = 0;
1554 return 0;
1556 if (result == 2)
1557 needs_warning = true;
1559 if (TREE_CODE (s1) == FIELD_DECL
1560 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1561 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1562 break;
1564 ok = true;
1565 break;
1567 if (!ok)
1569 tu->val = 0;
1570 return 0;
1573 tu->val = needs_warning ? 2 : 10;
1574 return tu->val;
1577 case RECORD_TYPE:
1579 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1581 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1582 s1 && s2;
1583 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1585 int result;
1586 if (TREE_CODE (s1) != TREE_CODE (s2)
1587 || DECL_NAME (s1) != DECL_NAME (s2))
1588 break;
1589 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1590 enum_and_int_p, different_types_p);
1591 if (result == 0)
1592 break;
1593 if (result == 2)
1594 needs_warning = true;
1596 if (TREE_CODE (s1) == FIELD_DECL
1597 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1598 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1599 break;
1601 if (s1 && s2)
1602 tu->val = 0;
1603 else
1604 tu->val = needs_warning ? 2 : 1;
1605 return tu->val;
1608 default:
1609 gcc_unreachable ();
1613 /* Return 1 if two function types F1 and F2 are compatible.
1614 If either type specifies no argument types,
1615 the other must specify a fixed number of self-promoting arg types.
1616 Otherwise, if one type specifies only the number of arguments,
1617 the other must specify that number of self-promoting arg types.
1618 Otherwise, the argument types must match.
1619 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1621 static int
1622 function_types_compatible_p (const_tree f1, const_tree f2,
1623 bool *enum_and_int_p, bool *different_types_p)
1625 tree args1, args2;
1626 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1627 int val = 1;
1628 int val1;
1629 tree ret1, ret2;
1631 ret1 = TREE_TYPE (f1);
1632 ret2 = TREE_TYPE (f2);
1634 /* 'volatile' qualifiers on a function's return type used to mean
1635 the function is noreturn. */
1636 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1637 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1638 if (TYPE_VOLATILE (ret1))
1639 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1640 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1641 if (TYPE_VOLATILE (ret2))
1642 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1643 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1644 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1645 if (val == 0)
1646 return 0;
1648 args1 = TYPE_ARG_TYPES (f1);
1649 args2 = TYPE_ARG_TYPES (f2);
1651 if (different_types_p != NULL
1652 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1653 *different_types_p = true;
1655 /* An unspecified parmlist matches any specified parmlist
1656 whose argument types don't need default promotions. */
1658 if (args1 == NULL_TREE)
1660 if (!self_promoting_args_p (args2))
1661 return 0;
1662 /* If one of these types comes from a non-prototype fn definition,
1663 compare that with the other type's arglist.
1664 If they don't match, ask for a warning (but no error). */
1665 if (TYPE_ACTUAL_ARG_TYPES (f1)
1666 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1667 enum_and_int_p, different_types_p))
1668 val = 2;
1669 return val;
1671 if (args2 == NULL_TREE)
1673 if (!self_promoting_args_p (args1))
1674 return 0;
1675 if (TYPE_ACTUAL_ARG_TYPES (f2)
1676 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1677 enum_and_int_p, different_types_p))
1678 val = 2;
1679 return val;
1682 /* Both types have argument lists: compare them and propagate results. */
1683 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1684 different_types_p);
1685 return val1 != 1 ? val1 : val;
1688 /* Check two lists of types for compatibility, returning 0 for
1689 incompatible, 1 for compatible, or 2 for compatible with
1690 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1691 comptypes_internal. */
1693 static int
1694 type_lists_compatible_p (const_tree args1, const_tree args2,
1695 bool *enum_and_int_p, bool *different_types_p)
1697 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1698 int val = 1;
1699 int newval = 0;
1701 while (1)
1703 tree a1, mv1, a2, mv2;
1704 if (args1 == NULL_TREE && args2 == NULL_TREE)
1705 return val;
1706 /* If one list is shorter than the other,
1707 they fail to match. */
1708 if (args1 == NULL_TREE || args2 == NULL_TREE)
1709 return 0;
1710 mv1 = a1 = TREE_VALUE (args1);
1711 mv2 = a2 = TREE_VALUE (args2);
1712 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1713 mv1 = (TYPE_ATOMIC (mv1)
1714 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1715 TYPE_QUAL_ATOMIC)
1716 : TYPE_MAIN_VARIANT (mv1));
1717 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1718 mv2 = (TYPE_ATOMIC (mv2)
1719 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1720 TYPE_QUAL_ATOMIC)
1721 : TYPE_MAIN_VARIANT (mv2));
1722 /* A null pointer instead of a type
1723 means there is supposed to be an argument
1724 but nothing is specified about what type it has.
1725 So match anything that self-promotes. */
1726 if (different_types_p != NULL
1727 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1728 *different_types_p = true;
1729 if (a1 == NULL_TREE)
1731 if (c_type_promotes_to (a2) != a2)
1732 return 0;
1734 else if (a2 == NULL_TREE)
1736 if (c_type_promotes_to (a1) != a1)
1737 return 0;
1739 /* If one of the lists has an error marker, ignore this arg. */
1740 else if (TREE_CODE (a1) == ERROR_MARK
1741 || TREE_CODE (a2) == ERROR_MARK)
1743 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1744 different_types_p)))
1746 if (different_types_p != NULL)
1747 *different_types_p = true;
1748 /* Allow wait (union {union wait *u; int *i} *)
1749 and wait (union wait *) to be compatible. */
1750 if (TREE_CODE (a1) == UNION_TYPE
1751 && (TYPE_NAME (a1) == NULL_TREE
1752 || TYPE_TRANSPARENT_AGGR (a1))
1753 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1754 && tree_int_cst_equal (TYPE_SIZE (a1),
1755 TYPE_SIZE (a2)))
1757 tree memb;
1758 for (memb = TYPE_FIELDS (a1);
1759 memb; memb = DECL_CHAIN (memb))
1761 tree mv3 = TREE_TYPE (memb);
1762 if (mv3 && mv3 != error_mark_node
1763 && TREE_CODE (mv3) != ARRAY_TYPE)
1764 mv3 = (TYPE_ATOMIC (mv3)
1765 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1766 TYPE_QUAL_ATOMIC)
1767 : TYPE_MAIN_VARIANT (mv3));
1768 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1769 different_types_p))
1770 break;
1772 if (memb == NULL_TREE)
1773 return 0;
1775 else if (TREE_CODE (a2) == UNION_TYPE
1776 && (TYPE_NAME (a2) == NULL_TREE
1777 || TYPE_TRANSPARENT_AGGR (a2))
1778 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1779 && tree_int_cst_equal (TYPE_SIZE (a2),
1780 TYPE_SIZE (a1)))
1782 tree memb;
1783 for (memb = TYPE_FIELDS (a2);
1784 memb; memb = DECL_CHAIN (memb))
1786 tree mv3 = TREE_TYPE (memb);
1787 if (mv3 && mv3 != error_mark_node
1788 && TREE_CODE (mv3) != ARRAY_TYPE)
1789 mv3 = (TYPE_ATOMIC (mv3)
1790 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1791 TYPE_QUAL_ATOMIC)
1792 : TYPE_MAIN_VARIANT (mv3));
1793 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1794 different_types_p))
1795 break;
1797 if (memb == NULL_TREE)
1798 return 0;
1800 else
1801 return 0;
1804 /* comptypes said ok, but record if it said to warn. */
1805 if (newval > val)
1806 val = newval;
1808 args1 = TREE_CHAIN (args1);
1809 args2 = TREE_CHAIN (args2);
1813 /* Compute the size to increment a pointer by. When a function type or void
1814 type or incomplete type is passed, size_one_node is returned.
1815 This function does not emit any diagnostics; the caller is responsible
1816 for that. */
1818 static tree
1819 c_size_in_bytes (const_tree type)
1821 enum tree_code code = TREE_CODE (type);
1823 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1824 || !COMPLETE_TYPE_P (type))
1825 return size_one_node;
1827 /* Convert in case a char is more than one unit. */
1828 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1829 size_int (TYPE_PRECISION (char_type_node)
1830 / BITS_PER_UNIT));
1833 /* Return either DECL or its known constant value (if it has one). */
1835 tree
1836 decl_constant_value (tree decl)
1838 if (/* Don't change a variable array bound or initial value to a constant
1839 in a place where a variable is invalid. Note that DECL_INITIAL
1840 isn't valid for a PARM_DECL. */
1841 current_function_decl != NULL_TREE
1842 && TREE_CODE (decl) != PARM_DECL
1843 && !TREE_THIS_VOLATILE (decl)
1844 && TREE_READONLY (decl)
1845 && DECL_INITIAL (decl) != NULL_TREE
1846 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1847 /* This is invalid if initial value is not constant.
1848 If it has either a function call, a memory reference,
1849 or a variable, then re-evaluating it could give different results. */
1850 && TREE_CONSTANT (DECL_INITIAL (decl))
1851 /* Check for cases where this is sub-optimal, even though valid. */
1852 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1853 return DECL_INITIAL (decl);
1854 return decl;
1857 /* Convert the array expression EXP to a pointer. */
1858 static tree
1859 array_to_pointer_conversion (location_t loc, tree exp)
1861 tree orig_exp = exp;
1862 tree type = TREE_TYPE (exp);
1863 tree adr;
1864 tree restype = TREE_TYPE (type);
1865 tree ptrtype;
1867 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1869 STRIP_TYPE_NOPS (exp);
1871 if (TREE_NO_WARNING (orig_exp))
1872 TREE_NO_WARNING (exp) = 1;
1874 ptrtype = build_pointer_type (restype);
1876 if (INDIRECT_REF_P (exp))
1877 return convert (ptrtype, TREE_OPERAND (exp, 0));
1879 /* In C++ array compound literals are temporary objects unless they are
1880 const or appear in namespace scope, so they are destroyed too soon
1881 to use them for much of anything (c++/53220). */
1882 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1884 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1885 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1886 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1887 "converting an array compound literal to a pointer "
1888 "is ill-formed in C++");
1891 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1892 return convert (ptrtype, adr);
1895 /* Convert the function expression EXP to a pointer. */
1896 static tree
1897 function_to_pointer_conversion (location_t loc, tree exp)
1899 tree orig_exp = exp;
1901 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1903 STRIP_TYPE_NOPS (exp);
1905 if (TREE_NO_WARNING (orig_exp))
1906 TREE_NO_WARNING (exp) = 1;
1908 return build_unary_op (loc, ADDR_EXPR, exp, false);
1911 /* Mark EXP as read, not just set, for set but not used -Wunused
1912 warning purposes. */
1914 void
1915 mark_exp_read (tree exp)
1917 switch (TREE_CODE (exp))
1919 case VAR_DECL:
1920 case PARM_DECL:
1921 DECL_READ_P (exp) = 1;
1922 break;
1923 case ARRAY_REF:
1924 case COMPONENT_REF:
1925 case MODIFY_EXPR:
1926 case REALPART_EXPR:
1927 case IMAGPART_EXPR:
1928 CASE_CONVERT:
1929 case ADDR_EXPR:
1930 case VIEW_CONVERT_EXPR:
1931 mark_exp_read (TREE_OPERAND (exp, 0));
1932 break;
1933 case COMPOUND_EXPR:
1934 case C_MAYBE_CONST_EXPR:
1935 mark_exp_read (TREE_OPERAND (exp, 1));
1936 break;
1937 default:
1938 break;
1942 /* Perform the default conversion of arrays and functions to pointers.
1943 Return the result of converting EXP. For any other expression, just
1944 return EXP.
1946 LOC is the location of the expression. */
1948 struct c_expr
1949 default_function_array_conversion (location_t loc, struct c_expr exp)
1951 tree orig_exp = exp.value;
1952 tree type = TREE_TYPE (exp.value);
1953 enum tree_code code = TREE_CODE (type);
1955 switch (code)
1957 case ARRAY_TYPE:
1959 bool not_lvalue = false;
1960 bool lvalue_array_p;
1962 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1963 || CONVERT_EXPR_P (exp.value))
1964 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1966 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1967 not_lvalue = true;
1968 exp.value = TREE_OPERAND (exp.value, 0);
1971 if (TREE_NO_WARNING (orig_exp))
1972 TREE_NO_WARNING (exp.value) = 1;
1974 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1975 if (!flag_isoc99 && !lvalue_array_p)
1977 /* Before C99, non-lvalue arrays do not decay to pointers.
1978 Normally, using such an array would be invalid; but it can
1979 be used correctly inside sizeof or as a statement expression.
1980 Thus, do not give an error here; an error will result later. */
1981 return exp;
1984 exp.value = array_to_pointer_conversion (loc, exp.value);
1986 break;
1987 case FUNCTION_TYPE:
1988 exp.value = function_to_pointer_conversion (loc, exp.value);
1989 break;
1990 default:
1991 break;
1994 return exp;
1997 struct c_expr
1998 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2000 mark_exp_read (exp.value);
2001 return default_function_array_conversion (loc, exp);
2004 /* Return whether EXPR should be treated as an atomic lvalue for the
2005 purposes of load and store handling. */
2007 static bool
2008 really_atomic_lvalue (tree expr)
2010 if (error_operand_p (expr))
2011 return false;
2012 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2013 return false;
2014 if (!lvalue_p (expr))
2015 return false;
2017 /* Ignore _Atomic on register variables, since their addresses can't
2018 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2019 sequences wouldn't work. Ignore _Atomic on structures containing
2020 bit-fields, since accessing elements of atomic structures or
2021 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2022 it's undefined at translation time or execution time, and the
2023 normal atomic sequences again wouldn't work. */
2024 while (handled_component_p (expr))
2026 if (TREE_CODE (expr) == COMPONENT_REF
2027 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2028 return false;
2029 expr = TREE_OPERAND (expr, 0);
2031 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2032 return false;
2033 return true;
2036 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2037 including converting functions and arrays to pointers if CONVERT_P.
2038 If READ_P, also mark the expression as having been read. */
2040 struct c_expr
2041 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2042 bool convert_p, bool read_p)
2044 if (read_p)
2045 mark_exp_read (exp.value);
2046 if (convert_p)
2047 exp = default_function_array_conversion (loc, exp);
2048 if (really_atomic_lvalue (exp.value))
2050 vec<tree, va_gc> *params;
2051 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2052 tree expr_type = TREE_TYPE (exp.value);
2053 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2054 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2056 gcc_assert (TYPE_ATOMIC (expr_type));
2058 /* Expansion of a generic atomic load may require an addition
2059 element, so allocate enough to prevent a resize. */
2060 vec_alloc (params, 4);
2062 /* Remove the qualifiers for the rest of the expressions and
2063 create the VAL temp variable to hold the RHS. */
2064 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2065 tmp = create_tmp_var_raw (nonatomic_type);
2066 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2067 TREE_ADDRESSABLE (tmp) = 1;
2068 TREE_NO_WARNING (tmp) = 1;
2070 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2071 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2072 params->quick_push (expr_addr);
2073 params->quick_push (tmp_addr);
2074 params->quick_push (seq_cst);
2075 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2077 /* EXPR is always read. */
2078 mark_exp_read (exp.value);
2080 /* Return tmp which contains the value loaded. */
2081 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2082 NULL_TREE, NULL_TREE);
2084 return exp;
2087 /* EXP is an expression of integer type. Apply the integer promotions
2088 to it and return the promoted value. */
2090 tree
2091 perform_integral_promotions (tree exp)
2093 tree type = TREE_TYPE (exp);
2094 enum tree_code code = TREE_CODE (type);
2096 gcc_assert (INTEGRAL_TYPE_P (type));
2098 /* Normally convert enums to int,
2099 but convert wide enums to something wider. */
2100 if (code == ENUMERAL_TYPE)
2102 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2103 TYPE_PRECISION (integer_type_node)),
2104 ((TYPE_PRECISION (type)
2105 >= TYPE_PRECISION (integer_type_node))
2106 && TYPE_UNSIGNED (type)));
2108 return convert (type, exp);
2111 /* ??? This should no longer be needed now bit-fields have their
2112 proper types. */
2113 if (TREE_CODE (exp) == COMPONENT_REF
2114 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2115 /* If it's thinner than an int, promote it like a
2116 c_promoting_integer_type_p, otherwise leave it alone. */
2117 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2118 TYPE_PRECISION (integer_type_node)))
2119 return convert (integer_type_node, exp);
2121 if (c_promoting_integer_type_p (type))
2123 /* Preserve unsignedness if not really getting any wider. */
2124 if (TYPE_UNSIGNED (type)
2125 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2126 return convert (unsigned_type_node, exp);
2128 return convert (integer_type_node, exp);
2131 return exp;
2135 /* Perform default promotions for C data used in expressions.
2136 Enumeral types or short or char are converted to int.
2137 In addition, manifest constants symbols are replaced by their values. */
2139 tree
2140 default_conversion (tree exp)
2142 tree orig_exp;
2143 tree type = TREE_TYPE (exp);
2144 enum tree_code code = TREE_CODE (type);
2145 tree promoted_type;
2147 mark_exp_read (exp);
2149 /* Functions and arrays have been converted during parsing. */
2150 gcc_assert (code != FUNCTION_TYPE);
2151 if (code == ARRAY_TYPE)
2152 return exp;
2154 /* Constants can be used directly unless they're not loadable. */
2155 if (TREE_CODE (exp) == CONST_DECL)
2156 exp = DECL_INITIAL (exp);
2158 /* Strip no-op conversions. */
2159 orig_exp = exp;
2160 STRIP_TYPE_NOPS (exp);
2162 if (TREE_NO_WARNING (orig_exp))
2163 TREE_NO_WARNING (exp) = 1;
2165 if (code == VOID_TYPE)
2167 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2168 "void value not ignored as it ought to be");
2169 return error_mark_node;
2172 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2173 if (exp == error_mark_node)
2174 return error_mark_node;
2176 promoted_type = targetm.promoted_type (type);
2177 if (promoted_type)
2178 return convert (promoted_type, exp);
2180 if (INTEGRAL_TYPE_P (type))
2181 return perform_integral_promotions (exp);
2183 return exp;
2186 /* Look up COMPONENT in a structure or union TYPE.
2188 If the component name is not found, returns NULL_TREE. Otherwise,
2189 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2190 stepping down the chain to the component, which is in the last
2191 TREE_VALUE of the list. Normally the list is of length one, but if
2192 the component is embedded within (nested) anonymous structures or
2193 unions, the list steps down the chain to the component. */
2195 static tree
2196 lookup_field (tree type, tree component)
2198 tree field;
2200 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2201 to the field elements. Use a binary search on this array to quickly
2202 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2203 will always be set for structures which have many elements. */
2205 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2207 int bot, top, half;
2208 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2210 field = TYPE_FIELDS (type);
2211 bot = 0;
2212 top = TYPE_LANG_SPECIFIC (type)->s->len;
2213 while (top - bot > 1)
2215 half = (top - bot + 1) >> 1;
2216 field = field_array[bot+half];
2218 if (DECL_NAME (field) == NULL_TREE)
2220 /* Step through all anon unions in linear fashion. */
2221 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2223 field = field_array[bot++];
2224 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2226 tree anon = lookup_field (TREE_TYPE (field), component);
2228 if (anon)
2229 return tree_cons (NULL_TREE, field, anon);
2231 /* The Plan 9 compiler permits referring
2232 directly to an anonymous struct/union field
2233 using a typedef name. */
2234 if (flag_plan9_extensions
2235 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2236 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2237 == TYPE_DECL)
2238 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2239 == component))
2240 break;
2244 /* Entire record is only anon unions. */
2245 if (bot > top)
2246 return NULL_TREE;
2248 /* Restart the binary search, with new lower bound. */
2249 continue;
2252 if (DECL_NAME (field) == component)
2253 break;
2254 if (DECL_NAME (field) < component)
2255 bot += half;
2256 else
2257 top = bot + half;
2260 if (DECL_NAME (field_array[bot]) == component)
2261 field = field_array[bot];
2262 else if (DECL_NAME (field) != component)
2263 return NULL_TREE;
2265 else
2267 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2269 if (DECL_NAME (field) == NULL_TREE
2270 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2272 tree anon = lookup_field (TREE_TYPE (field), component);
2274 if (anon)
2275 return tree_cons (NULL_TREE, field, anon);
2277 /* The Plan 9 compiler permits referring directly to an
2278 anonymous struct/union field using a typedef
2279 name. */
2280 if (flag_plan9_extensions
2281 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2282 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2283 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2284 == component))
2285 break;
2288 if (DECL_NAME (field) == component)
2289 break;
2292 if (field == NULL_TREE)
2293 return NULL_TREE;
2296 return tree_cons (NULL_TREE, field, NULL_TREE);
2299 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2301 static void
2302 lookup_field_fuzzy_find_candidates (tree type, tree component,
2303 vec<tree> *candidates)
2305 tree field;
2306 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2308 if (DECL_NAME (field) == NULL_TREE
2309 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2310 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2311 candidates);
2313 if (DECL_NAME (field))
2314 candidates->safe_push (DECL_NAME (field));
2318 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2319 rather than returning a TREE_LIST for an exact match. */
2321 static tree
2322 lookup_field_fuzzy (tree type, tree component)
2324 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2326 /* First, gather a list of candidates. */
2327 auto_vec <tree> candidates;
2329 lookup_field_fuzzy_find_candidates (type, component,
2330 &candidates);
2332 return find_closest_identifier (component, &candidates);
2335 /* Support function for build_component_ref's error-handling.
2337 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2338 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2340 static bool
2341 should_suggest_deref_p (tree datum_type)
2343 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2344 allows "." for ptrs; we could be handling a failed attempt
2345 to access a property. */
2346 if (c_dialect_objc ())
2347 return false;
2349 /* Only suggest it for pointers... */
2350 if (TREE_CODE (datum_type) != POINTER_TYPE)
2351 return false;
2353 /* ...to structs/unions. */
2354 tree underlying_type = TREE_TYPE (datum_type);
2355 enum tree_code code = TREE_CODE (underlying_type);
2356 if (code == RECORD_TYPE || code == UNION_TYPE)
2357 return true;
2358 else
2359 return false;
2362 /* Make an expression to refer to the COMPONENT field of structure or
2363 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2364 location of the COMPONENT_REF. COMPONENT_LOC is the location
2365 of COMPONENT. */
2367 tree
2368 build_component_ref (location_t loc, tree datum, tree component,
2369 location_t component_loc)
2371 tree type = TREE_TYPE (datum);
2372 enum tree_code code = TREE_CODE (type);
2373 tree field = NULL;
2374 tree ref;
2375 bool datum_lvalue = lvalue_p (datum);
2377 if (!objc_is_public (datum, component))
2378 return error_mark_node;
2380 /* Detect Objective-C property syntax object.property. */
2381 if (c_dialect_objc ()
2382 && (ref = objc_maybe_build_component_ref (datum, component)))
2383 return ref;
2385 /* See if there is a field or component with name COMPONENT. */
2387 if (code == RECORD_TYPE || code == UNION_TYPE)
2389 if (!COMPLETE_TYPE_P (type))
2391 c_incomplete_type_error (loc, NULL_TREE, type);
2392 return error_mark_node;
2395 field = lookup_field (type, component);
2397 if (!field)
2399 tree guessed_id = lookup_field_fuzzy (type, component);
2400 if (guessed_id)
2402 /* Attempt to provide a fixit replacement hint, if
2403 we have a valid range for the component. */
2404 location_t reported_loc
2405 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2406 gcc_rich_location rich_loc (reported_loc);
2407 if (component_loc != UNKNOWN_LOCATION)
2408 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2409 error_at_rich_loc
2410 (&rich_loc,
2411 "%qT has no member named %qE; did you mean %qE?",
2412 type, component, guessed_id);
2414 else
2415 error_at (loc, "%qT has no member named %qE", type, component);
2416 return error_mark_node;
2419 /* Accessing elements of atomic structures or unions is undefined
2420 behavior (C11 6.5.2.3#5). */
2421 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2423 if (code == RECORD_TYPE)
2424 warning_at (loc, 0, "accessing a member %qE of an atomic "
2425 "structure %qE", component, datum);
2426 else
2427 warning_at (loc, 0, "accessing a member %qE of an atomic "
2428 "union %qE", component, datum);
2431 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2432 This might be better solved in future the way the C++ front
2433 end does it - by giving the anonymous entities each a
2434 separate name and type, and then have build_component_ref
2435 recursively call itself. We can't do that here. */
2438 tree subdatum = TREE_VALUE (field);
2439 int quals;
2440 tree subtype;
2441 bool use_datum_quals;
2443 if (TREE_TYPE (subdatum) == error_mark_node)
2444 return error_mark_node;
2446 /* If this is an rvalue, it does not have qualifiers in C
2447 standard terms and we must avoid propagating such
2448 qualifiers down to a non-lvalue array that is then
2449 converted to a pointer. */
2450 use_datum_quals = (datum_lvalue
2451 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2453 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2454 if (use_datum_quals)
2455 quals |= TYPE_QUALS (TREE_TYPE (datum));
2456 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2458 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2459 NULL_TREE);
2460 SET_EXPR_LOCATION (ref, loc);
2461 if (TREE_READONLY (subdatum)
2462 || (use_datum_quals && TREE_READONLY (datum)))
2463 TREE_READONLY (ref) = 1;
2464 if (TREE_THIS_VOLATILE (subdatum)
2465 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2466 TREE_THIS_VOLATILE (ref) = 1;
2468 if (TREE_DEPRECATED (subdatum))
2469 warn_deprecated_use (subdatum, NULL_TREE);
2471 datum = ref;
2473 field = TREE_CHAIN (field);
2475 while (field);
2477 return ref;
2479 else if (should_suggest_deref_p (type))
2481 /* Special-case the error message for "ptr.field" for the case
2482 where the user has confused "." vs "->". */
2483 rich_location richloc (line_table, loc);
2484 /* "loc" should be the "." token. */
2485 richloc.add_fixit_replace ("->");
2486 error_at_rich_loc (&richloc,
2487 "%qE is a pointer; did you mean to use %<->%>?",
2488 datum);
2489 return error_mark_node;
2491 else if (code != ERROR_MARK)
2492 error_at (loc,
2493 "request for member %qE in something not a structure or union",
2494 component);
2496 return error_mark_node;
2499 /* Given an expression PTR for a pointer, return an expression
2500 for the value pointed to.
2501 ERRORSTRING is the name of the operator to appear in error messages.
2503 LOC is the location to use for the generated tree. */
2505 tree
2506 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2508 tree pointer = default_conversion (ptr);
2509 tree type = TREE_TYPE (pointer);
2510 tree ref;
2512 if (TREE_CODE (type) == POINTER_TYPE)
2514 if (CONVERT_EXPR_P (pointer)
2515 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2517 /* If a warning is issued, mark it to avoid duplicates from
2518 the backend. This only needs to be done at
2519 warn_strict_aliasing > 2. */
2520 if (warn_strict_aliasing > 2)
2521 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2522 type, TREE_OPERAND (pointer, 0)))
2523 TREE_NO_WARNING (pointer) = 1;
2526 if (TREE_CODE (pointer) == ADDR_EXPR
2527 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2528 == TREE_TYPE (type)))
2530 ref = TREE_OPERAND (pointer, 0);
2531 protected_set_expr_location (ref, loc);
2532 return ref;
2534 else
2536 tree t = TREE_TYPE (type);
2538 ref = build1 (INDIRECT_REF, t, pointer);
2540 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2542 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2544 error_at (loc, "dereferencing pointer to incomplete type "
2545 "%qT", t);
2546 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2548 return error_mark_node;
2550 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2551 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2553 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2554 so that we get the proper error message if the result is used
2555 to assign to. Also, &* is supposed to be a no-op.
2556 And ANSI C seems to specify that the type of the result
2557 should be the const type. */
2558 /* A de-reference of a pointer to const is not a const. It is valid
2559 to change it via some other pointer. */
2560 TREE_READONLY (ref) = TYPE_READONLY (t);
2561 TREE_SIDE_EFFECTS (ref)
2562 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2563 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2564 protected_set_expr_location (ref, loc);
2565 return ref;
2568 else if (TREE_CODE (pointer) != ERROR_MARK)
2569 invalid_indirection_error (loc, type, errstring);
2571 return error_mark_node;
2574 /* This handles expressions of the form "a[i]", which denotes
2575 an array reference.
2577 This is logically equivalent in C to *(a+i), but we may do it differently.
2578 If A is a variable or a member, we generate a primitive ARRAY_REF.
2579 This avoids forcing the array out of registers, and can work on
2580 arrays that are not lvalues (for example, members of structures returned
2581 by functions).
2583 For vector types, allow vector[i] but not i[vector], and create
2584 *(((type*)&vectortype) + i) for the expression.
2586 LOC is the location to use for the returned expression. */
2588 tree
2589 build_array_ref (location_t loc, tree array, tree index)
2591 tree ret;
2592 bool swapped = false;
2593 if (TREE_TYPE (array) == error_mark_node
2594 || TREE_TYPE (index) == error_mark_node)
2595 return error_mark_node;
2597 if (flag_cilkplus && contains_array_notation_expr (index))
2599 size_t rank = 0;
2600 if (!find_rank (loc, index, index, true, &rank))
2601 return error_mark_node;
2602 if (rank > 1)
2604 error_at (loc, "rank of the array's index is greater than 1");
2605 return error_mark_node;
2608 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2609 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2610 /* Allow vector[index] but not index[vector]. */
2611 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2613 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2614 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2616 error_at (loc,
2617 "subscripted value is neither array nor pointer nor vector");
2619 return error_mark_node;
2621 std::swap (array, index);
2622 swapped = true;
2625 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2627 error_at (loc, "array subscript is not an integer");
2628 return error_mark_node;
2631 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2633 error_at (loc, "subscripted value is pointer to function");
2634 return error_mark_node;
2637 /* ??? Existing practice has been to warn only when the char
2638 index is syntactically the index, not for char[array]. */
2639 if (!swapped)
2640 warn_array_subscript_with_type_char (loc, index);
2642 /* Apply default promotions *after* noticing character types. */
2643 index = default_conversion (index);
2644 if (index == error_mark_node)
2645 return error_mark_node;
2647 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2649 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2650 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2652 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2654 tree rval, type;
2656 /* An array that is indexed by a non-constant
2657 cannot be stored in a register; we must be able to do
2658 address arithmetic on its address.
2659 Likewise an array of elements of variable size. */
2660 if (TREE_CODE (index) != INTEGER_CST
2661 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2662 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2664 if (!c_mark_addressable (array, true))
2665 return error_mark_node;
2667 /* An array that is indexed by a constant value which is not within
2668 the array bounds cannot be stored in a register either; because we
2669 would get a crash in store_bit_field/extract_bit_field when trying
2670 to access a non-existent part of the register. */
2671 if (TREE_CODE (index) == INTEGER_CST
2672 && TYPE_DOMAIN (TREE_TYPE (array))
2673 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2675 if (!c_mark_addressable (array))
2676 return error_mark_node;
2679 if ((pedantic || warn_c90_c99_compat)
2680 && ! was_vector)
2682 tree foo = array;
2683 while (TREE_CODE (foo) == COMPONENT_REF)
2684 foo = TREE_OPERAND (foo, 0);
2685 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2686 pedwarn (loc, OPT_Wpedantic,
2687 "ISO C forbids subscripting %<register%> array");
2688 else if (!lvalue_p (foo))
2689 pedwarn_c90 (loc, OPT_Wpedantic,
2690 "ISO C90 forbids subscripting non-lvalue "
2691 "array");
2694 type = TREE_TYPE (TREE_TYPE (array));
2695 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2696 /* Array ref is const/volatile if the array elements are
2697 or if the array is. */
2698 TREE_READONLY (rval)
2699 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2700 | TREE_READONLY (array));
2701 TREE_SIDE_EFFECTS (rval)
2702 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2703 | TREE_SIDE_EFFECTS (array));
2704 TREE_THIS_VOLATILE (rval)
2705 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2706 /* This was added by rms on 16 Nov 91.
2707 It fixes vol struct foo *a; a->elts[1]
2708 in an inline function.
2709 Hope it doesn't break something else. */
2710 | TREE_THIS_VOLATILE (array));
2711 ret = require_complete_type (loc, rval);
2712 protected_set_expr_location (ret, loc);
2713 if (non_lvalue)
2714 ret = non_lvalue_loc (loc, ret);
2715 return ret;
2717 else
2719 tree ar = default_conversion (array);
2721 if (ar == error_mark_node)
2722 return ar;
2724 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2725 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2727 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2728 index, false),
2729 RO_ARRAY_INDEXING);
2730 if (non_lvalue)
2731 ret = non_lvalue_loc (loc, ret);
2732 return ret;
2736 /* Build an external reference to identifier ID. FUN indicates
2737 whether this will be used for a function call. LOC is the source
2738 location of the identifier. This sets *TYPE to the type of the
2739 identifier, which is not the same as the type of the returned value
2740 for CONST_DECLs defined as enum constants. If the type of the
2741 identifier is not available, *TYPE is set to NULL. */
2742 tree
2743 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2745 tree ref;
2746 tree decl = lookup_name (id);
2748 /* In Objective-C, an instance variable (ivar) may be preferred to
2749 whatever lookup_name() found. */
2750 decl = objc_lookup_ivar (decl, id);
2752 *type = NULL;
2753 if (decl && decl != error_mark_node)
2755 ref = decl;
2756 *type = TREE_TYPE (ref);
2758 else if (fun)
2759 /* Implicit function declaration. */
2760 ref = implicitly_declare (loc, id);
2761 else if (decl == error_mark_node)
2762 /* Don't complain about something that's already been
2763 complained about. */
2764 return error_mark_node;
2765 else
2767 undeclared_variable (loc, id);
2768 return error_mark_node;
2771 if (TREE_TYPE (ref) == error_mark_node)
2772 return error_mark_node;
2774 if (TREE_DEPRECATED (ref))
2775 warn_deprecated_use (ref, NULL_TREE);
2777 /* Recursive call does not count as usage. */
2778 if (ref != current_function_decl)
2780 TREE_USED (ref) = 1;
2783 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2785 if (!in_sizeof && !in_typeof)
2786 C_DECL_USED (ref) = 1;
2787 else if (DECL_INITIAL (ref) == NULL_TREE
2788 && DECL_EXTERNAL (ref)
2789 && !TREE_PUBLIC (ref))
2790 record_maybe_used_decl (ref);
2793 if (TREE_CODE (ref) == CONST_DECL)
2795 used_types_insert (TREE_TYPE (ref));
2797 if (warn_cxx_compat
2798 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2799 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2801 warning_at (loc, OPT_Wc___compat,
2802 ("enum constant defined in struct or union "
2803 "is not visible in C++"));
2804 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2807 ref = DECL_INITIAL (ref);
2808 TREE_CONSTANT (ref) = 1;
2810 else if (current_function_decl != NULL_TREE
2811 && !DECL_FILE_SCOPE_P (current_function_decl)
2812 && (VAR_OR_FUNCTION_DECL_P (ref)
2813 || TREE_CODE (ref) == PARM_DECL))
2815 tree context = decl_function_context (ref);
2817 if (context != NULL_TREE && context != current_function_decl)
2818 DECL_NONLOCAL (ref) = 1;
2820 /* C99 6.7.4p3: An inline definition of a function with external
2821 linkage ... shall not contain a reference to an identifier with
2822 internal linkage. */
2823 else if (current_function_decl != NULL_TREE
2824 && DECL_DECLARED_INLINE_P (current_function_decl)
2825 && DECL_EXTERNAL (current_function_decl)
2826 && VAR_OR_FUNCTION_DECL_P (ref)
2827 && (!VAR_P (ref) || TREE_STATIC (ref))
2828 && ! TREE_PUBLIC (ref)
2829 && DECL_CONTEXT (ref) != current_function_decl)
2830 record_inline_static (loc, current_function_decl, ref,
2831 csi_internal);
2833 return ref;
2836 /* Record details of decls possibly used inside sizeof or typeof. */
2837 struct maybe_used_decl
2839 /* The decl. */
2840 tree decl;
2841 /* The level seen at (in_sizeof + in_typeof). */
2842 int level;
2843 /* The next one at this level or above, or NULL. */
2844 struct maybe_used_decl *next;
2847 static struct maybe_used_decl *maybe_used_decls;
2849 /* Record that DECL, an undefined static function reference seen
2850 inside sizeof or typeof, might be used if the operand of sizeof is
2851 a VLA type or the operand of typeof is a variably modified
2852 type. */
2854 static void
2855 record_maybe_used_decl (tree decl)
2857 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2858 t->decl = decl;
2859 t->level = in_sizeof + in_typeof;
2860 t->next = maybe_used_decls;
2861 maybe_used_decls = t;
2864 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2865 USED is false, just discard them. If it is true, mark them used
2866 (if no longer inside sizeof or typeof) or move them to the next
2867 level up (if still inside sizeof or typeof). */
2869 void
2870 pop_maybe_used (bool used)
2872 struct maybe_used_decl *p = maybe_used_decls;
2873 int cur_level = in_sizeof + in_typeof;
2874 while (p && p->level > cur_level)
2876 if (used)
2878 if (cur_level == 0)
2879 C_DECL_USED (p->decl) = 1;
2880 else
2881 p->level = cur_level;
2883 p = p->next;
2885 if (!used || cur_level == 0)
2886 maybe_used_decls = p;
2889 /* Return the result of sizeof applied to EXPR. */
2891 struct c_expr
2892 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2894 struct c_expr ret;
2895 if (expr.value == error_mark_node)
2897 ret.value = error_mark_node;
2898 ret.original_code = ERROR_MARK;
2899 ret.original_type = NULL;
2900 pop_maybe_used (false);
2902 else
2904 bool expr_const_operands = true;
2906 if (TREE_CODE (expr.value) == PARM_DECL
2907 && C_ARRAY_PARAMETER (expr.value))
2909 if (warning_at (loc, OPT_Wsizeof_array_argument,
2910 "%<sizeof%> on array function parameter %qE will "
2911 "return size of %qT", expr.value,
2912 expr.original_type))
2913 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2915 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2916 &expr_const_operands);
2917 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2918 c_last_sizeof_arg = expr.value;
2919 c_last_sizeof_loc = loc;
2920 ret.original_code = SIZEOF_EXPR;
2921 ret.original_type = NULL;
2922 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2924 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2925 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2926 folded_expr, ret.value);
2927 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2928 SET_EXPR_LOCATION (ret.value, loc);
2930 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2932 return ret;
2935 /* Return the result of sizeof applied to T, a structure for the type
2936 name passed to sizeof (rather than the type itself). LOC is the
2937 location of the original expression. */
2939 struct c_expr
2940 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2942 tree type;
2943 struct c_expr ret;
2944 tree type_expr = NULL_TREE;
2945 bool type_expr_const = true;
2946 type = groktypename (t, &type_expr, &type_expr_const);
2947 ret.value = c_sizeof (loc, type);
2948 c_last_sizeof_arg = type;
2949 c_last_sizeof_loc = loc;
2950 ret.original_code = SIZEOF_EXPR;
2951 ret.original_type = NULL;
2952 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2953 && c_vla_type_p (type))
2955 /* If the type is a [*] array, it is a VLA but is represented as
2956 having a size of zero. In such a case we must ensure that
2957 the result of sizeof does not get folded to a constant by
2958 c_fully_fold, because if the size is evaluated the result is
2959 not constant and so constraints on zero or negative size
2960 arrays must not be applied when this sizeof call is inside
2961 another array declarator. */
2962 if (!type_expr)
2963 type_expr = integer_zero_node;
2964 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2965 type_expr, ret.value);
2966 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2968 pop_maybe_used (type != error_mark_node
2969 ? C_TYPE_VARIABLE_SIZE (type) : false);
2970 return ret;
2973 /* Build a function call to function FUNCTION with parameters PARAMS.
2974 The function call is at LOC.
2975 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2976 TREE_VALUE of each node is a parameter-expression.
2977 FUNCTION's data type may be a function type or a pointer-to-function. */
2979 tree
2980 build_function_call (location_t loc, tree function, tree params)
2982 vec<tree, va_gc> *v;
2983 tree ret;
2985 vec_alloc (v, list_length (params));
2986 for (; params; params = TREE_CHAIN (params))
2987 v->quick_push (TREE_VALUE (params));
2988 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2989 vec_free (v);
2990 return ret;
2993 /* Give a note about the location of the declaration of DECL. */
2995 static void
2996 inform_declaration (tree decl)
2998 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2999 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3002 /* Build a function call to function FUNCTION with parameters PARAMS.
3003 ORIGTYPES, if not NULL, is a vector of types; each element is
3004 either NULL or the original type of the corresponding element in
3005 PARAMS. The original type may differ from TREE_TYPE of the
3006 parameter for enums. FUNCTION's data type may be a function type
3007 or pointer-to-function. This function changes the elements of
3008 PARAMS. */
3010 tree
3011 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3012 tree function, vec<tree, va_gc> *params,
3013 vec<tree, va_gc> *origtypes)
3015 tree fntype, fundecl = NULL_TREE;
3016 tree name = NULL_TREE, result;
3017 tree tem;
3018 int nargs;
3019 tree *argarray;
3022 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3023 STRIP_TYPE_NOPS (function);
3025 /* Convert anything with function type to a pointer-to-function. */
3026 if (TREE_CODE (function) == FUNCTION_DECL)
3028 name = DECL_NAME (function);
3030 if (flag_tm)
3031 tm_malloc_replacement (function);
3032 fundecl = function;
3033 /* Atomic functions have type checking/casting already done. They are
3034 often rewritten and don't match the original parameter list. */
3035 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3036 origtypes = NULL;
3038 if (flag_cilkplus
3039 && is_cilkplus_reduce_builtin (function))
3040 origtypes = NULL;
3042 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3043 function = function_to_pointer_conversion (loc, function);
3045 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3046 expressions, like those used for ObjC messenger dispatches. */
3047 if (params && !params->is_empty ())
3048 function = objc_rewrite_function_call (function, (*params)[0]);
3050 function = c_fully_fold (function, false, NULL);
3052 fntype = TREE_TYPE (function);
3054 if (TREE_CODE (fntype) == ERROR_MARK)
3055 return error_mark_node;
3057 if (!(TREE_CODE (fntype) == POINTER_TYPE
3058 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3060 if (!flag_diagnostics_show_caret)
3061 error_at (loc,
3062 "called object %qE is not a function or function pointer",
3063 function);
3064 else if (DECL_P (function))
3066 error_at (loc,
3067 "called object %qD is not a function or function pointer",
3068 function);
3069 inform_declaration (function);
3071 else
3072 error_at (loc,
3073 "called object is not a function or function pointer");
3074 return error_mark_node;
3077 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3078 current_function_returns_abnormally = 1;
3080 /* fntype now gets the type of function pointed to. */
3081 fntype = TREE_TYPE (fntype);
3083 /* Convert the parameters to the types declared in the
3084 function prototype, or apply default promotions. */
3086 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3087 origtypes, function, fundecl);
3088 if (nargs < 0)
3089 return error_mark_node;
3091 /* Check that the function is called through a compatible prototype.
3092 If it is not, warn. */
3093 if (CONVERT_EXPR_P (function)
3094 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3095 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3096 && !comptypes (fntype, TREE_TYPE (tem)))
3098 tree return_type = TREE_TYPE (fntype);
3100 /* This situation leads to run-time undefined behavior. We can't,
3101 therefore, simply error unless we can prove that all possible
3102 executions of the program must execute the code. */
3103 warning_at (loc, 0, "function called through a non-compatible type");
3105 if (VOID_TYPE_P (return_type)
3106 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3107 pedwarn (loc, 0,
3108 "function with qualified void return type called");
3111 argarray = vec_safe_address (params);
3113 /* Check that arguments to builtin functions match the expectations. */
3114 if (fundecl
3115 && DECL_BUILT_IN (fundecl)
3116 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3117 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3118 argarray))
3119 return error_mark_node;
3121 /* Check that the arguments to the function are valid. */
3122 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3123 nargs, argarray, &arg_loc);
3125 if (name != NULL_TREE
3126 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3128 if (require_constant_value)
3129 result
3130 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3131 function, nargs, argarray);
3132 else
3133 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3134 function, nargs, argarray);
3135 if (TREE_CODE (result) == NOP_EXPR
3136 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3137 STRIP_TYPE_NOPS (result);
3139 else
3140 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3141 function, nargs, argarray);
3142 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3143 later. */
3144 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3145 TREE_NO_WARNING (result) = 1;
3147 /* In this improbable scenario, a nested function returns a VM type.
3148 Create a TARGET_EXPR so that the call always has a LHS, much as
3149 what the C++ FE does for functions returning non-PODs. */
3150 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3152 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3153 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3154 NULL_TREE, NULL_TREE);
3157 if (VOID_TYPE_P (TREE_TYPE (result)))
3159 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3160 pedwarn (loc, 0,
3161 "function with qualified void return type called");
3162 return result;
3164 return require_complete_type (loc, result);
3167 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3169 tree
3170 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3171 tree function, vec<tree, va_gc> *params,
3172 vec<tree, va_gc> *origtypes)
3174 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3175 STRIP_TYPE_NOPS (function);
3177 /* Convert anything with function type to a pointer-to-function. */
3178 if (TREE_CODE (function) == FUNCTION_DECL)
3180 /* Implement type-directed function overloading for builtins.
3181 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3182 handle all the type checking. The result is a complete expression
3183 that implements this function call. */
3184 tree tem = resolve_overloaded_builtin (loc, function, params);
3185 if (tem)
3186 return tem;
3188 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3191 /* Convert the argument expressions in the vector VALUES
3192 to the types in the list TYPELIST.
3194 If TYPELIST is exhausted, or when an element has NULL as its type,
3195 perform the default conversions.
3197 ORIGTYPES is the original types of the expressions in VALUES. This
3198 holds the type of enum values which have been converted to integral
3199 types. It may be NULL.
3201 FUNCTION is a tree for the called function. It is used only for
3202 error messages, where it is formatted with %qE.
3204 This is also where warnings about wrong number of args are generated.
3206 ARG_LOC are locations of function arguments (if any).
3208 Returns the actual number of arguments processed (which may be less
3209 than the length of VALUES in some error situations), or -1 on
3210 failure. */
3212 static int
3213 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3214 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3215 tree function, tree fundecl)
3217 tree typetail, val;
3218 unsigned int parmnum;
3219 bool error_args = false;
3220 const bool type_generic = fundecl
3221 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3222 bool type_generic_remove_excess_precision = false;
3223 bool type_generic_overflow_p = false;
3224 tree selector;
3226 /* Change pointer to function to the function itself for
3227 diagnostics. */
3228 if (TREE_CODE (function) == ADDR_EXPR
3229 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3230 function = TREE_OPERAND (function, 0);
3232 /* Handle an ObjC selector specially for diagnostics. */
3233 selector = objc_message_selector ();
3235 /* For type-generic built-in functions, determine whether excess
3236 precision should be removed (classification) or not
3237 (comparison). */
3238 if (type_generic
3239 && DECL_BUILT_IN (fundecl)
3240 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3242 switch (DECL_FUNCTION_CODE (fundecl))
3244 case BUILT_IN_ISFINITE:
3245 case BUILT_IN_ISINF:
3246 case BUILT_IN_ISINF_SIGN:
3247 case BUILT_IN_ISNAN:
3248 case BUILT_IN_ISNORMAL:
3249 case BUILT_IN_FPCLASSIFY:
3250 type_generic_remove_excess_precision = true;
3251 break;
3253 case BUILT_IN_ADD_OVERFLOW_P:
3254 case BUILT_IN_SUB_OVERFLOW_P:
3255 case BUILT_IN_MUL_OVERFLOW_P:
3256 /* The last argument of these type-generic builtins
3257 should not be promoted. */
3258 type_generic_overflow_p = true;
3259 break;
3261 default:
3262 break;
3265 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3266 return vec_safe_length (values);
3268 /* Scan the given expressions and types, producing individual
3269 converted arguments. */
3271 for (typetail = typelist, parmnum = 0;
3272 values && values->iterate (parmnum, &val);
3273 ++parmnum)
3275 tree type = typetail ? TREE_VALUE (typetail) : 0;
3276 tree valtype = TREE_TYPE (val);
3277 tree rname = function;
3278 int argnum = parmnum + 1;
3279 const char *invalid_func_diag;
3280 bool excess_precision = false;
3281 bool npc;
3282 tree parmval;
3283 /* Some __atomic_* builtins have additional hidden argument at
3284 position 0. */
3285 location_t ploc
3286 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3287 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3288 : input_location;
3290 if (type == void_type_node)
3292 if (selector)
3293 error_at (loc, "too many arguments to method %qE", selector);
3294 else
3295 error_at (loc, "too many arguments to function %qE", function);
3296 inform_declaration (fundecl);
3297 return error_args ? -1 : (int) parmnum;
3300 if (selector && argnum > 2)
3302 rname = selector;
3303 argnum -= 2;
3306 npc = null_pointer_constant_p (val);
3308 /* If there is excess precision and a prototype, convert once to
3309 the required type rather than converting via the semantic
3310 type. Likewise without a prototype a float value represented
3311 as long double should be converted once to double. But for
3312 type-generic classification functions excess precision must
3313 be removed here. */
3314 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3315 && (type || !type_generic || !type_generic_remove_excess_precision))
3317 val = TREE_OPERAND (val, 0);
3318 excess_precision = true;
3320 val = c_fully_fold (val, false, NULL);
3321 STRIP_TYPE_NOPS (val);
3323 val = require_complete_type (ploc, val);
3325 /* Some floating-point arguments must be promoted to double when
3326 no type is specified by a prototype. This applies to
3327 arguments of type float, and to architecture-specific types
3328 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3329 bool promote_float_arg = false;
3330 if (type == NULL_TREE
3331 && TREE_CODE (valtype) == REAL_TYPE
3332 && (TYPE_PRECISION (valtype)
3333 <= TYPE_PRECISION (double_type_node))
3334 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3335 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3336 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3338 /* Promote this argument, unless it has a _FloatN or
3339 _FloatNx type. */
3340 promote_float_arg = true;
3341 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3342 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3344 promote_float_arg = false;
3345 break;
3349 if (type != NULL_TREE)
3351 /* Formal parm type is specified by a function prototype. */
3353 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3355 error_at (ploc, "type of formal parameter %d is incomplete",
3356 parmnum + 1);
3357 parmval = val;
3359 else
3361 tree origtype;
3363 /* Optionally warn about conversions that
3364 differ from the default conversions. */
3365 if (warn_traditional_conversion || warn_traditional)
3367 unsigned int formal_prec = TYPE_PRECISION (type);
3369 if (INTEGRAL_TYPE_P (type)
3370 && TREE_CODE (valtype) == REAL_TYPE)
3371 warning_at (ploc, OPT_Wtraditional_conversion,
3372 "passing argument %d of %qE as integer rather "
3373 "than floating due to prototype",
3374 argnum, rname);
3375 if (INTEGRAL_TYPE_P (type)
3376 && TREE_CODE (valtype) == COMPLEX_TYPE)
3377 warning_at (ploc, OPT_Wtraditional_conversion,
3378 "passing argument %d of %qE as integer rather "
3379 "than complex due to prototype",
3380 argnum, rname);
3381 else if (TREE_CODE (type) == COMPLEX_TYPE
3382 && TREE_CODE (valtype) == REAL_TYPE)
3383 warning_at (ploc, OPT_Wtraditional_conversion,
3384 "passing argument %d of %qE as complex rather "
3385 "than floating due to prototype",
3386 argnum, rname);
3387 else if (TREE_CODE (type) == REAL_TYPE
3388 && INTEGRAL_TYPE_P (valtype))
3389 warning_at (ploc, OPT_Wtraditional_conversion,
3390 "passing argument %d of %qE as floating rather "
3391 "than integer due to prototype",
3392 argnum, rname);
3393 else if (TREE_CODE (type) == COMPLEX_TYPE
3394 && INTEGRAL_TYPE_P (valtype))
3395 warning_at (ploc, OPT_Wtraditional_conversion,
3396 "passing argument %d of %qE as complex rather "
3397 "than integer due to prototype",
3398 argnum, rname);
3399 else if (TREE_CODE (type) == REAL_TYPE
3400 && TREE_CODE (valtype) == COMPLEX_TYPE)
3401 warning_at (ploc, OPT_Wtraditional_conversion,
3402 "passing argument %d of %qE as floating rather "
3403 "than complex due to prototype",
3404 argnum, rname);
3405 /* ??? At some point, messages should be written about
3406 conversions between complex types, but that's too messy
3407 to do now. */
3408 else if (TREE_CODE (type) == REAL_TYPE
3409 && TREE_CODE (valtype) == REAL_TYPE)
3411 /* Warn if any argument is passed as `float',
3412 since without a prototype it would be `double'. */
3413 if (formal_prec == TYPE_PRECISION (float_type_node)
3414 && type != dfloat32_type_node)
3415 warning_at (ploc, 0,
3416 "passing argument %d of %qE as %<float%> "
3417 "rather than %<double%> due to prototype",
3418 argnum, rname);
3420 /* Warn if mismatch between argument and prototype
3421 for decimal float types. Warn of conversions with
3422 binary float types and of precision narrowing due to
3423 prototype. */
3424 else if (type != valtype
3425 && (type == dfloat32_type_node
3426 || type == dfloat64_type_node
3427 || type == dfloat128_type_node
3428 || valtype == dfloat32_type_node
3429 || valtype == dfloat64_type_node
3430 || valtype == dfloat128_type_node)
3431 && (formal_prec
3432 <= TYPE_PRECISION (valtype)
3433 || (type == dfloat128_type_node
3434 && (valtype
3435 != dfloat64_type_node
3436 && (valtype
3437 != dfloat32_type_node)))
3438 || (type == dfloat64_type_node
3439 && (valtype
3440 != dfloat32_type_node))))
3441 warning_at (ploc, 0,
3442 "passing argument %d of %qE as %qT "
3443 "rather than %qT due to prototype",
3444 argnum, rname, type, valtype);
3447 /* Detect integer changing in width or signedness.
3448 These warnings are only activated with
3449 -Wtraditional-conversion, not with -Wtraditional. */
3450 else if (warn_traditional_conversion
3451 && INTEGRAL_TYPE_P (type)
3452 && INTEGRAL_TYPE_P (valtype))
3454 tree would_have_been = default_conversion (val);
3455 tree type1 = TREE_TYPE (would_have_been);
3457 if (val == error_mark_node)
3458 /* VAL could have been of incomplete type. */;
3459 else if (TREE_CODE (type) == ENUMERAL_TYPE
3460 && (TYPE_MAIN_VARIANT (type)
3461 == TYPE_MAIN_VARIANT (valtype)))
3462 /* No warning if function asks for enum
3463 and the actual arg is that enum type. */
3465 else if (formal_prec != TYPE_PRECISION (type1))
3466 warning_at (ploc, OPT_Wtraditional_conversion,
3467 "passing argument %d of %qE "
3468 "with different width due to prototype",
3469 argnum, rname);
3470 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3472 /* Don't complain if the formal parameter type
3473 is an enum, because we can't tell now whether
3474 the value was an enum--even the same enum. */
3475 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3477 else if (TREE_CODE (val) == INTEGER_CST
3478 && int_fits_type_p (val, type))
3479 /* Change in signedness doesn't matter
3480 if a constant value is unaffected. */
3482 /* If the value is extended from a narrower
3483 unsigned type, it doesn't matter whether we
3484 pass it as signed or unsigned; the value
3485 certainly is the same either way. */
3486 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3487 && TYPE_UNSIGNED (valtype))
3489 else if (TYPE_UNSIGNED (type))
3490 warning_at (ploc, OPT_Wtraditional_conversion,
3491 "passing argument %d of %qE "
3492 "as unsigned due to prototype",
3493 argnum, rname);
3494 else
3495 warning_at (ploc, OPT_Wtraditional_conversion,
3496 "passing argument %d of %qE "
3497 "as signed due to prototype",
3498 argnum, rname);
3502 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3503 sake of better warnings from convert_and_check. */
3504 if (excess_precision)
3505 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3506 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3507 parmval = convert_for_assignment (loc, ploc, type,
3508 val, origtype, ic_argpass,
3509 npc, fundecl, function,
3510 parmnum + 1);
3512 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3513 && INTEGRAL_TYPE_P (type)
3514 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3515 parmval = default_conversion (parmval);
3518 else if (promote_float_arg)
3520 if (type_generic)
3521 parmval = val;
3522 else
3524 /* Convert `float' to `double'. */
3525 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3526 warning_at (ploc, OPT_Wdouble_promotion,
3527 "implicit conversion from %qT to %qT when passing "
3528 "argument to function",
3529 valtype, double_type_node);
3530 parmval = convert (double_type_node, val);
3533 else if ((excess_precision && !type_generic)
3534 || (type_generic_overflow_p && parmnum == 2))
3535 /* A "double" argument with excess precision being passed
3536 without a prototype or in variable arguments.
3537 The last argument of __builtin_*_overflow_p should not be
3538 promoted. */
3539 parmval = convert (valtype, val);
3540 else if ((invalid_func_diag =
3541 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3543 error (invalid_func_diag);
3544 return -1;
3546 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3548 return -1;
3550 else
3551 /* Convert `short' and `char' to full-size `int'. */
3552 parmval = default_conversion (val);
3554 (*values)[parmnum] = parmval;
3555 if (parmval == error_mark_node)
3556 error_args = true;
3558 if (typetail)
3559 typetail = TREE_CHAIN (typetail);
3562 gcc_assert (parmnum == vec_safe_length (values));
3564 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3566 error_at (loc, "too few arguments to function %qE", function);
3567 inform_declaration (fundecl);
3568 return -1;
3571 return error_args ? -1 : (int) parmnum;
3574 /* This is the entry point used by the parser to build unary operators
3575 in the input. CODE, a tree_code, specifies the unary operator, and
3576 ARG is the operand. For unary plus, the C parser currently uses
3577 CONVERT_EXPR for code.
3579 LOC is the location to use for the tree generated.
3582 struct c_expr
3583 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3585 struct c_expr result;
3587 result.original_code = code;
3588 result.original_type = NULL;
3590 if (reject_gcc_builtin (arg.value))
3592 result.value = error_mark_node;
3594 else
3596 result.value = build_unary_op (loc, code, arg.value, false);
3598 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3599 overflow_warning (loc, result.value, arg.value);
3602 /* We are typically called when parsing a prefix token at LOC acting on
3603 ARG. Reflect this by updating the source range of the result to
3604 start at LOC and end at the end of ARG. */
3605 set_c_expr_source_range (&result,
3606 loc, arg.get_finish ());
3608 return result;
3611 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3613 static bool
3614 char_type_p (tree type)
3616 return (type == char_type_node
3617 || type == unsigned_char_type_node
3618 || type == signed_char_type_node
3619 || type == char16_type_node
3620 || type == char32_type_node);
3623 /* This is the entry point used by the parser to build binary operators
3624 in the input. CODE, a tree_code, specifies the binary operator, and
3625 ARG1 and ARG2 are the operands. In addition to constructing the
3626 expression, we check for operands that were written with other binary
3627 operators in a way that is likely to confuse the user.
3629 LOCATION is the location of the binary operator. */
3631 struct c_expr
3632 parser_build_binary_op (location_t location, enum tree_code code,
3633 struct c_expr arg1, struct c_expr arg2)
3635 struct c_expr result;
3637 enum tree_code code1 = arg1.original_code;
3638 enum tree_code code2 = arg2.original_code;
3639 tree type1 = (arg1.original_type
3640 ? arg1.original_type
3641 : TREE_TYPE (arg1.value));
3642 tree type2 = (arg2.original_type
3643 ? arg2.original_type
3644 : TREE_TYPE (arg2.value));
3646 result.value = build_binary_op (location, code,
3647 arg1.value, arg2.value, true);
3648 result.original_code = code;
3649 result.original_type = NULL;
3651 if (TREE_CODE (result.value) == ERROR_MARK)
3653 set_c_expr_source_range (&result,
3654 arg1.get_start (),
3655 arg2.get_finish ());
3656 return result;
3659 if (location != UNKNOWN_LOCATION)
3660 protected_set_expr_location (result.value, location);
3662 set_c_expr_source_range (&result,
3663 arg1.get_start (),
3664 arg2.get_finish ());
3666 /* Check for cases such as x+y<<z which users are likely
3667 to misinterpret. */
3668 if (warn_parentheses)
3669 warn_about_parentheses (location, code, code1, arg1.value, code2,
3670 arg2.value);
3672 if (warn_logical_op)
3673 warn_logical_operator (location, code, TREE_TYPE (result.value),
3674 code1, arg1.value, code2, arg2.value);
3676 if (warn_tautological_compare)
3678 tree lhs = arg1.value;
3679 tree rhs = arg2.value;
3680 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3682 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3683 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3684 lhs = NULL_TREE;
3685 else
3686 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3688 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3690 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3691 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3692 rhs = NULL_TREE;
3693 else
3694 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3696 if (lhs != NULL_TREE && rhs != NULL_TREE)
3697 warn_tautological_cmp (location, code, lhs, rhs);
3700 if (warn_logical_not_paren
3701 && TREE_CODE_CLASS (code) == tcc_comparison
3702 && code1 == TRUTH_NOT_EXPR
3703 && code2 != TRUTH_NOT_EXPR
3704 /* Avoid warning for !!x == y. */
3705 && (TREE_CODE (arg1.value) != NE_EXPR
3706 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3708 /* Avoid warning for !b == y where b has _Bool type. */
3709 tree t = integer_zero_node;
3710 if (TREE_CODE (arg1.value) == EQ_EXPR
3711 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3712 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3714 t = TREE_OPERAND (arg1.value, 0);
3717 if (TREE_TYPE (t) != integer_type_node)
3718 break;
3719 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3720 t = C_MAYBE_CONST_EXPR_EXPR (t);
3721 else if (CONVERT_EXPR_P (t))
3722 t = TREE_OPERAND (t, 0);
3723 else
3724 break;
3726 while (1);
3728 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3729 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3732 /* Warn about comparisons against string literals, with the exception
3733 of testing for equality or inequality of a string literal with NULL. */
3734 if (code == EQ_EXPR || code == NE_EXPR)
3736 if ((code1 == STRING_CST
3737 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3738 || (code2 == STRING_CST
3739 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3740 warning_at (location, OPT_Waddress,
3741 "comparison with string literal results in unspecified behavior");
3742 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3743 if (POINTER_TYPE_P (type1)
3744 && null_pointer_constant_p (arg2.value)
3745 && char_type_p (type2)
3746 && warning_at (location, OPT_Wpointer_compare,
3747 "comparison between pointer and zero character "
3748 "constant"))
3749 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3750 else if (POINTER_TYPE_P (type2)
3751 && null_pointer_constant_p (arg1.value)
3752 && char_type_p (type1)
3753 && warning_at (location, OPT_Wpointer_compare,
3754 "comparison between pointer and zero character "
3755 "constant"))
3756 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3758 else if (TREE_CODE_CLASS (code) == tcc_comparison
3759 && (code1 == STRING_CST || code2 == STRING_CST))
3760 warning_at (location, OPT_Waddress,
3761 "comparison with string literal results in unspecified behavior");
3763 if (TREE_OVERFLOW_P (result.value)
3764 && !TREE_OVERFLOW_P (arg1.value)
3765 && !TREE_OVERFLOW_P (arg2.value))
3766 overflow_warning (location, result.value);
3768 /* Warn about comparisons of different enum types. */
3769 if (warn_enum_compare
3770 && TREE_CODE_CLASS (code) == tcc_comparison
3771 && TREE_CODE (type1) == ENUMERAL_TYPE
3772 && TREE_CODE (type2) == ENUMERAL_TYPE
3773 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3774 warning_at (location, OPT_Wenum_compare,
3775 "comparison between %qT and %qT",
3776 type1, type2);
3778 return result;
3781 /* Return a tree for the difference of pointers OP0 and OP1.
3782 The resulting tree has type int. */
3784 static tree
3785 pointer_diff (location_t loc, tree op0, tree op1)
3787 tree restype = ptrdiff_type_node;
3788 tree result, inttype;
3790 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3791 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3792 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3793 tree orig_op1 = op1;
3795 /* If the operands point into different address spaces, we need to
3796 explicitly convert them to pointers into the common address space
3797 before we can subtract the numerical address values. */
3798 if (as0 != as1)
3800 addr_space_t as_common;
3801 tree common_type;
3803 /* Determine the common superset address space. This is guaranteed
3804 to exist because the caller verified that comp_target_types
3805 returned non-zero. */
3806 if (!addr_space_superset (as0, as1, &as_common))
3807 gcc_unreachable ();
3809 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3810 op0 = convert (common_type, op0);
3811 op1 = convert (common_type, op1);
3814 /* Determine integer type to perform computations in. This will usually
3815 be the same as the result type (ptrdiff_t), but may need to be a wider
3816 type if pointers for the address space are wider than ptrdiff_t. */
3817 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3818 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3819 else
3820 inttype = restype;
3822 if (TREE_CODE (target_type) == VOID_TYPE)
3823 pedwarn (loc, OPT_Wpointer_arith,
3824 "pointer of type %<void *%> used in subtraction");
3825 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3826 pedwarn (loc, OPT_Wpointer_arith,
3827 "pointer to a function used in subtraction");
3829 /* First do the subtraction as integers;
3830 then drop through to build the divide operator.
3831 Do not do default conversions on the minus operator
3832 in case restype is a short type. */
3834 op0 = build_binary_op (loc,
3835 MINUS_EXPR, convert (inttype, op0),
3836 convert (inttype, op1), false);
3837 /* This generates an error if op1 is pointer to incomplete type. */
3838 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3839 error_at (loc, "arithmetic on pointer to an incomplete type");
3841 op1 = c_size_in_bytes (target_type);
3843 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3844 error_at (loc, "arithmetic on pointer to an empty aggregate");
3846 /* Divide by the size, in easiest possible way. */
3847 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3848 op0, convert (inttype, op1));
3850 /* Convert to final result type if necessary. */
3851 return convert (restype, result);
3854 /* Expand atomic compound assignments into an appropriate sequence as
3855 specified by the C11 standard section 6.5.16.2.
3857 _Atomic T1 E1
3858 T2 E2
3859 E1 op= E2
3861 This sequence is used for all types for which these operations are
3862 supported.
3864 In addition, built-in versions of the 'fe' prefixed routines may
3865 need to be invoked for floating point (real, complex or vector) when
3866 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3868 T1 newval;
3869 T1 old;
3870 T1 *addr
3871 T2 val
3872 fenv_t fenv
3874 addr = &E1;
3875 val = (E2);
3876 __atomic_load (addr, &old, SEQ_CST);
3877 feholdexcept (&fenv);
3878 loop:
3879 newval = old op val;
3880 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3881 SEQ_CST))
3882 goto done;
3883 feclearexcept (FE_ALL_EXCEPT);
3884 goto loop:
3885 done:
3886 feupdateenv (&fenv);
3888 The compiler will issue the __atomic_fetch_* built-in when possible,
3889 otherwise it will generate the generic form of the atomic operations.
3890 This requires temp(s) and has their address taken. The atomic processing
3891 is smart enough to figure out when the size of an object can utilize
3892 a lock-free version, and convert the built-in call to the appropriate
3893 lock-free routine. The optimizers will then dispose of any temps that
3894 are no longer required, and lock-free implementations are utilized as
3895 long as there is target support for the required size.
3897 If the operator is NOP_EXPR, then this is a simple assignment, and
3898 an __atomic_store is issued to perform the assignment rather than
3899 the above loop. */
3901 /* Build an atomic assignment at LOC, expanding into the proper
3902 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3903 the result of the operation, unless RETURN_OLD_P, in which case
3904 return the old value of LHS (this is only for postincrement and
3905 postdecrement). */
3907 static tree
3908 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3909 tree rhs, bool return_old_p)
3911 tree fndecl, func_call;
3912 vec<tree, va_gc> *params;
3913 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3914 tree old, old_addr;
3915 tree compound_stmt;
3916 tree stmt, goto_stmt;
3917 tree loop_label, loop_decl, done_label, done_decl;
3919 tree lhs_type = TREE_TYPE (lhs);
3920 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3921 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3922 tree rhs_type = TREE_TYPE (rhs);
3924 gcc_assert (TYPE_ATOMIC (lhs_type));
3926 if (return_old_p)
3927 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3929 /* Allocate enough vector items for a compare_exchange. */
3930 vec_alloc (params, 6);
3932 /* Create a compound statement to hold the sequence of statements
3933 with a loop. */
3934 compound_stmt = c_begin_compound_stmt (false);
3936 /* Fold the RHS if it hasn't already been folded. */
3937 if (modifycode != NOP_EXPR)
3938 rhs = c_fully_fold (rhs, false, NULL);
3940 /* Remove the qualifiers for the rest of the expressions and create
3941 the VAL temp variable to hold the RHS. */
3942 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3943 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3944 val = create_tmp_var_raw (nonatomic_rhs_type);
3945 TREE_ADDRESSABLE (val) = 1;
3946 TREE_NO_WARNING (val) = 1;
3947 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3948 NULL_TREE);
3949 SET_EXPR_LOCATION (rhs, loc);
3950 add_stmt (rhs);
3952 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3953 an atomic_store. */
3954 if (modifycode == NOP_EXPR)
3956 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3957 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3958 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3959 params->quick_push (lhs_addr);
3960 params->quick_push (rhs);
3961 params->quick_push (seq_cst);
3962 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3963 add_stmt (func_call);
3965 /* Finish the compound statement. */
3966 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3968 /* VAL is the value which was stored, return a COMPOUND_STMT of
3969 the statement and that value. */
3970 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3973 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3974 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3975 isn't applicable for such builtins. ??? Do we want to handle enums? */
3976 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3977 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3979 built_in_function fncode;
3980 switch (modifycode)
3982 case PLUS_EXPR:
3983 case POINTER_PLUS_EXPR:
3984 fncode = (return_old_p
3985 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3986 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3987 break;
3988 case MINUS_EXPR:
3989 fncode = (return_old_p
3990 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3991 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3992 break;
3993 case BIT_AND_EXPR:
3994 fncode = (return_old_p
3995 ? BUILT_IN_ATOMIC_FETCH_AND_N
3996 : BUILT_IN_ATOMIC_AND_FETCH_N);
3997 break;
3998 case BIT_IOR_EXPR:
3999 fncode = (return_old_p
4000 ? BUILT_IN_ATOMIC_FETCH_OR_N
4001 : BUILT_IN_ATOMIC_OR_FETCH_N);
4002 break;
4003 case BIT_XOR_EXPR:
4004 fncode = (return_old_p
4005 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4006 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4007 break;
4008 default:
4009 goto cas_loop;
4012 /* We can only use "_1" through "_16" variants of the atomic fetch
4013 built-ins. */
4014 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4015 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4016 goto cas_loop;
4018 /* If this is a pointer type, we need to multiply by the size of
4019 the pointer target type. */
4020 if (POINTER_TYPE_P (lhs_type))
4022 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4023 /* ??? This would introduce -Wdiscarded-qualifiers
4024 warning: __atomic_fetch_* expect volatile void *
4025 type as the first argument. (Assignments between
4026 atomic and non-atomic objects are OK.) */
4027 || TYPE_RESTRICT (lhs_type))
4028 goto cas_loop;
4029 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4030 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4031 convert (ptrdiff_type_node, rhs),
4032 convert (ptrdiff_type_node, sz));
4035 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4036 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4037 fndecl = builtin_decl_explicit (fncode);
4038 params->quick_push (lhs_addr);
4039 params->quick_push (rhs);
4040 params->quick_push (seq_cst);
4041 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4043 newval = create_tmp_var_raw (nonatomic_lhs_type);
4044 TREE_ADDRESSABLE (newval) = 1;
4045 TREE_NO_WARNING (newval) = 1;
4046 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4047 NULL_TREE, NULL_TREE);
4048 SET_EXPR_LOCATION (rhs, loc);
4049 add_stmt (rhs);
4051 /* Finish the compound statement. */
4052 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4054 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4055 the statement and that value. */
4056 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4059 cas_loop:
4060 /* Create the variables and labels required for the op= form. */
4061 old = create_tmp_var_raw (nonatomic_lhs_type);
4062 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4063 TREE_ADDRESSABLE (old) = 1;
4064 TREE_NO_WARNING (old) = 1;
4066 newval = create_tmp_var_raw (nonatomic_lhs_type);
4067 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4068 TREE_ADDRESSABLE (newval) = 1;
4069 TREE_NO_WARNING (newval) = 1;
4071 loop_decl = create_artificial_label (loc);
4072 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4074 done_decl = create_artificial_label (loc);
4075 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4077 /* __atomic_load (addr, &old, SEQ_CST). */
4078 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4079 params->quick_push (lhs_addr);
4080 params->quick_push (old_addr);
4081 params->quick_push (seq_cst);
4082 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4083 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4084 NULL_TREE);
4085 add_stmt (old);
4086 params->truncate (0);
4088 /* Create the expressions for floating-point environment
4089 manipulation, if required. */
4090 bool need_fenv = (flag_trapping_math
4091 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4092 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4093 if (need_fenv)
4094 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4096 if (hold_call)
4097 add_stmt (hold_call);
4099 /* loop: */
4100 add_stmt (loop_label);
4102 /* newval = old + val; */
4103 rhs = build_binary_op (loc, modifycode, old, val, true);
4104 rhs = c_fully_fold (rhs, false, NULL);
4105 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4106 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4107 NULL_TREE, 0);
4108 if (rhs != error_mark_node)
4110 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4111 NULL_TREE);
4112 SET_EXPR_LOCATION (rhs, loc);
4113 add_stmt (rhs);
4116 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4117 goto done; */
4118 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4119 params->quick_push (lhs_addr);
4120 params->quick_push (old_addr);
4121 params->quick_push (newval_addr);
4122 params->quick_push (integer_zero_node);
4123 params->quick_push (seq_cst);
4124 params->quick_push (seq_cst);
4125 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4127 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4128 SET_EXPR_LOCATION (goto_stmt, loc);
4130 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4131 SET_EXPR_LOCATION (stmt, loc);
4132 add_stmt (stmt);
4134 if (clear_call)
4135 add_stmt (clear_call);
4137 /* goto loop; */
4138 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4139 SET_EXPR_LOCATION (goto_stmt, loc);
4140 add_stmt (goto_stmt);
4142 /* done: */
4143 add_stmt (done_label);
4145 if (update_call)
4146 add_stmt (update_call);
4148 /* Finish the compound statement. */
4149 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4151 /* NEWVAL is the value that was successfully stored, return a
4152 COMPOUND_EXPR of the statement and the appropriate value. */
4153 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4154 return_old_p ? old : newval);
4157 /* Construct and perhaps optimize a tree representation
4158 for a unary operation. CODE, a tree_code, specifies the operation
4159 and XARG is the operand.
4160 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4161 promotions (such as from short to int).
4162 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4163 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4164 to pointers in C99.
4166 LOCATION is the location of the operator. */
4168 tree
4169 build_unary_op (location_t location, enum tree_code code, tree xarg,
4170 bool noconvert)
4172 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4173 tree arg = xarg;
4174 tree argtype = NULL_TREE;
4175 enum tree_code typecode;
4176 tree val;
4177 tree ret = error_mark_node;
4178 tree eptype = NULL_TREE;
4179 const char *invalid_op_diag;
4180 bool int_operands;
4182 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4183 if (int_operands)
4184 arg = remove_c_maybe_const_expr (arg);
4186 if (code != ADDR_EXPR)
4187 arg = require_complete_type (location, arg);
4189 typecode = TREE_CODE (TREE_TYPE (arg));
4190 if (typecode == ERROR_MARK)
4191 return error_mark_node;
4192 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4193 typecode = INTEGER_TYPE;
4195 if ((invalid_op_diag
4196 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4198 error_at (location, invalid_op_diag);
4199 return error_mark_node;
4202 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4204 eptype = TREE_TYPE (arg);
4205 arg = TREE_OPERAND (arg, 0);
4208 switch (code)
4210 case CONVERT_EXPR:
4211 /* This is used for unary plus, because a CONVERT_EXPR
4212 is enough to prevent anybody from looking inside for
4213 associativity, but won't generate any code. */
4214 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4215 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4216 || typecode == VECTOR_TYPE))
4218 error_at (location, "wrong type argument to unary plus");
4219 return error_mark_node;
4221 else if (!noconvert)
4222 arg = default_conversion (arg);
4223 arg = non_lvalue_loc (location, arg);
4224 break;
4226 case NEGATE_EXPR:
4227 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4228 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4229 || typecode == VECTOR_TYPE))
4231 error_at (location, "wrong type argument to unary minus");
4232 return error_mark_node;
4234 else if (!noconvert)
4235 arg = default_conversion (arg);
4236 break;
4238 case BIT_NOT_EXPR:
4239 /* ~ works on integer types and non float vectors. */
4240 if (typecode == INTEGER_TYPE
4241 || (typecode == VECTOR_TYPE
4242 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4244 tree e = arg;
4246 /* Warn if the expression has boolean value. */
4247 while (TREE_CODE (e) == COMPOUND_EXPR)
4248 e = TREE_OPERAND (e, 1);
4250 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4251 || truth_value_p (TREE_CODE (e)))
4252 && warning_at (location, OPT_Wbool_operation,
4253 "%<~%> on a boolean expression"))
4255 gcc_rich_location richloc (location);
4256 richloc.add_fixit_insert_before (location, "!");
4257 inform_at_rich_loc (&richloc, "did you mean to use logical "
4258 "not?");
4260 if (!noconvert)
4261 arg = default_conversion (arg);
4263 else if (typecode == COMPLEX_TYPE)
4265 code = CONJ_EXPR;
4266 pedwarn (location, OPT_Wpedantic,
4267 "ISO C does not support %<~%> for complex conjugation");
4268 if (!noconvert)
4269 arg = default_conversion (arg);
4271 else
4273 error_at (location, "wrong type argument to bit-complement");
4274 return error_mark_node;
4276 break;
4278 case ABS_EXPR:
4279 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4281 error_at (location, "wrong type argument to abs");
4282 return error_mark_node;
4284 else if (!noconvert)
4285 arg = default_conversion (arg);
4286 break;
4288 case CONJ_EXPR:
4289 /* Conjugating a real value is a no-op, but allow it anyway. */
4290 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4291 || typecode == COMPLEX_TYPE))
4293 error_at (location, "wrong type argument to conjugation");
4294 return error_mark_node;
4296 else if (!noconvert)
4297 arg = default_conversion (arg);
4298 break;
4300 case TRUTH_NOT_EXPR:
4301 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4302 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4303 && typecode != COMPLEX_TYPE)
4305 error_at (location,
4306 "wrong type argument to unary exclamation mark");
4307 return error_mark_node;
4309 if (int_operands)
4311 arg = c_objc_common_truthvalue_conversion (location, xarg);
4312 arg = remove_c_maybe_const_expr (arg);
4314 else
4315 arg = c_objc_common_truthvalue_conversion (location, arg);
4316 ret = invert_truthvalue_loc (location, arg);
4317 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4318 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4319 location = EXPR_LOCATION (ret);
4320 goto return_build_unary_op;
4322 case REALPART_EXPR:
4323 case IMAGPART_EXPR:
4324 ret = build_real_imag_expr (location, code, arg);
4325 if (ret == error_mark_node)
4326 return error_mark_node;
4327 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4328 eptype = TREE_TYPE (eptype);
4329 goto return_build_unary_op;
4331 case PREINCREMENT_EXPR:
4332 case POSTINCREMENT_EXPR:
4333 case PREDECREMENT_EXPR:
4334 case POSTDECREMENT_EXPR:
4336 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4338 tree inner = build_unary_op (location, code,
4339 C_MAYBE_CONST_EXPR_EXPR (arg),
4340 noconvert);
4341 if (inner == error_mark_node)
4342 return error_mark_node;
4343 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4344 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4345 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4346 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4347 goto return_build_unary_op;
4350 /* Complain about anything that is not a true lvalue. In
4351 Objective-C, skip this check for property_refs. */
4352 if (!objc_is_property_ref (arg)
4353 && !lvalue_or_else (location,
4354 arg, ((code == PREINCREMENT_EXPR
4355 || code == POSTINCREMENT_EXPR)
4356 ? lv_increment
4357 : lv_decrement)))
4358 return error_mark_node;
4360 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4362 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4363 warning_at (location, OPT_Wc___compat,
4364 "increment of enumeration value is invalid in C++");
4365 else
4366 warning_at (location, OPT_Wc___compat,
4367 "decrement of enumeration value is invalid in C++");
4370 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4372 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4373 warning_at (location, OPT_Wbool_operation,
4374 "increment of a boolean expression");
4375 else
4376 warning_at (location, OPT_Wbool_operation,
4377 "decrement of a boolean expression");
4380 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4381 arg = c_fully_fold (arg, false, NULL);
4383 bool atomic_op;
4384 atomic_op = really_atomic_lvalue (arg);
4386 /* Increment or decrement the real part of the value,
4387 and don't change the imaginary part. */
4388 if (typecode == COMPLEX_TYPE)
4390 tree real, imag;
4392 pedwarn (location, OPT_Wpedantic,
4393 "ISO C does not support %<++%> and %<--%> on complex types");
4395 if (!atomic_op)
4397 arg = stabilize_reference (arg);
4398 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4399 true);
4400 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4401 true);
4402 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4403 if (real == error_mark_node || imag == error_mark_node)
4404 return error_mark_node;
4405 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4406 real, imag);
4407 goto return_build_unary_op;
4411 /* Report invalid types. */
4413 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4414 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4415 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4417 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4418 error_at (location, "wrong type argument to increment");
4419 else
4420 error_at (location, "wrong type argument to decrement");
4422 return error_mark_node;
4426 tree inc;
4428 argtype = TREE_TYPE (arg);
4430 /* Compute the increment. */
4432 if (typecode == POINTER_TYPE)
4434 /* If pointer target is an incomplete type,
4435 we just cannot know how to do the arithmetic. */
4436 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4438 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4439 error_at (location,
4440 "increment of pointer to an incomplete type %qT",
4441 TREE_TYPE (argtype));
4442 else
4443 error_at (location,
4444 "decrement of pointer to an incomplete type %qT",
4445 TREE_TYPE (argtype));
4447 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4448 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4450 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4451 pedwarn (location, OPT_Wpointer_arith,
4452 "wrong type argument to increment");
4453 else
4454 pedwarn (location, OPT_Wpointer_arith,
4455 "wrong type argument to decrement");
4458 inc = c_size_in_bytes (TREE_TYPE (argtype));
4459 inc = convert_to_ptrofftype_loc (location, inc);
4461 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4463 /* For signed fract types, we invert ++ to -- or
4464 -- to ++, and change inc from 1 to -1, because
4465 it is not possible to represent 1 in signed fract constants.
4466 For unsigned fract types, the result always overflows and
4467 we get an undefined (original) or the maximum value. */
4468 if (code == PREINCREMENT_EXPR)
4469 code = PREDECREMENT_EXPR;
4470 else if (code == PREDECREMENT_EXPR)
4471 code = PREINCREMENT_EXPR;
4472 else if (code == POSTINCREMENT_EXPR)
4473 code = POSTDECREMENT_EXPR;
4474 else /* code == POSTDECREMENT_EXPR */
4475 code = POSTINCREMENT_EXPR;
4477 inc = integer_minus_one_node;
4478 inc = convert (argtype, inc);
4480 else
4482 inc = VECTOR_TYPE_P (argtype)
4483 ? build_one_cst (argtype)
4484 : integer_one_node;
4485 inc = convert (argtype, inc);
4488 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4489 need to ask Objective-C to build the increment or decrement
4490 expression for it. */
4491 if (objc_is_property_ref (arg))
4492 return objc_build_incr_expr_for_property_ref (location, code,
4493 arg, inc);
4495 /* Report a read-only lvalue. */
4496 if (TYPE_READONLY (argtype))
4498 readonly_error (location, arg,
4499 ((code == PREINCREMENT_EXPR
4500 || code == POSTINCREMENT_EXPR)
4501 ? lv_increment : lv_decrement));
4502 return error_mark_node;
4504 else if (TREE_READONLY (arg))
4505 readonly_warning (arg,
4506 ((code == PREINCREMENT_EXPR
4507 || code == POSTINCREMENT_EXPR)
4508 ? lv_increment : lv_decrement));
4510 /* If the argument is atomic, use the special code sequences for
4511 atomic compound assignment. */
4512 if (atomic_op)
4514 arg = stabilize_reference (arg);
4515 ret = build_atomic_assign (location, arg,
4516 ((code == PREINCREMENT_EXPR
4517 || code == POSTINCREMENT_EXPR)
4518 ? PLUS_EXPR
4519 : MINUS_EXPR),
4520 (FRACT_MODE_P (TYPE_MODE (argtype))
4521 ? inc
4522 : integer_one_node),
4523 (code == POSTINCREMENT_EXPR
4524 || code == POSTDECREMENT_EXPR));
4525 goto return_build_unary_op;
4528 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4529 val = boolean_increment (code, arg);
4530 else
4531 val = build2 (code, TREE_TYPE (arg), arg, inc);
4532 TREE_SIDE_EFFECTS (val) = 1;
4533 if (TREE_CODE (val) != code)
4534 TREE_NO_WARNING (val) = 1;
4535 ret = val;
4536 goto return_build_unary_op;
4539 case ADDR_EXPR:
4540 /* Note that this operation never does default_conversion. */
4542 /* The operand of unary '&' must be an lvalue (which excludes
4543 expressions of type void), or, in C99, the result of a [] or
4544 unary '*' operator. */
4545 if (VOID_TYPE_P (TREE_TYPE (arg))
4546 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4547 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4548 pedwarn (location, 0, "taking address of expression of type %<void%>");
4550 /* Let &* cancel out to simplify resulting code. */
4551 if (INDIRECT_REF_P (arg))
4553 /* Don't let this be an lvalue. */
4554 if (lvalue_p (TREE_OPERAND (arg, 0)))
4555 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4556 ret = TREE_OPERAND (arg, 0);
4557 goto return_build_unary_op;
4560 /* Anything not already handled and not a true memory reference
4561 or a non-lvalue array is an error. */
4562 if (typecode != FUNCTION_TYPE && !noconvert
4563 && !lvalue_or_else (location, arg, lv_addressof))
4564 return error_mark_node;
4566 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4567 folding later. */
4568 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4570 tree inner = build_unary_op (location, code,
4571 C_MAYBE_CONST_EXPR_EXPR (arg),
4572 noconvert);
4573 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4574 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4575 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4576 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4577 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4578 goto return_build_unary_op;
4581 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4582 argtype = TREE_TYPE (arg);
4584 /* If the lvalue is const or volatile, merge that into the type
4585 to which the address will point. This is only needed
4586 for function types. */
4587 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4588 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4589 && TREE_CODE (argtype) == FUNCTION_TYPE)
4591 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4592 int quals = orig_quals;
4594 if (TREE_READONLY (arg))
4595 quals |= TYPE_QUAL_CONST;
4596 if (TREE_THIS_VOLATILE (arg))
4597 quals |= TYPE_QUAL_VOLATILE;
4599 argtype = c_build_qualified_type (argtype, quals);
4602 switch (TREE_CODE (arg))
4604 case COMPONENT_REF:
4605 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4607 error_at (location, "cannot take address of bit-field %qD",
4608 TREE_OPERAND (arg, 1));
4609 return error_mark_node;
4612 /* fall through */
4614 case ARRAY_REF:
4615 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4617 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4618 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4620 error_at (location, "cannot take address of scalar with "
4621 "reverse storage order");
4622 return error_mark_node;
4625 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4626 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4627 warning_at (location, OPT_Wscalar_storage_order,
4628 "address of array with reverse scalar storage "
4629 "order requested");
4632 default:
4633 break;
4636 if (!c_mark_addressable (arg))
4637 return error_mark_node;
4639 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4640 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4642 argtype = build_pointer_type (argtype);
4644 /* ??? Cope with user tricks that amount to offsetof. Delete this
4645 when we have proper support for integer constant expressions. */
4646 val = get_base_address (arg);
4647 if (val && INDIRECT_REF_P (val)
4648 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4650 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4651 goto return_build_unary_op;
4654 val = build1 (ADDR_EXPR, argtype, arg);
4656 ret = val;
4657 goto return_build_unary_op;
4659 default:
4660 gcc_unreachable ();
4663 if (argtype == NULL_TREE)
4664 argtype = TREE_TYPE (arg);
4665 if (TREE_CODE (arg) == INTEGER_CST)
4666 ret = (require_constant_value
4667 ? fold_build1_initializer_loc (location, code, argtype, arg)
4668 : fold_build1_loc (location, code, argtype, arg));
4669 else
4670 ret = build1 (code, argtype, arg);
4671 return_build_unary_op:
4672 gcc_assert (ret != error_mark_node);
4673 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4674 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4675 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4676 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4677 ret = note_integer_operands (ret);
4678 if (eptype)
4679 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4680 protected_set_expr_location (ret, location);
4681 return ret;
4684 /* Return nonzero if REF is an lvalue valid for this language.
4685 Lvalues can be assigned, unless their type has TYPE_READONLY.
4686 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4688 bool
4689 lvalue_p (const_tree ref)
4691 const enum tree_code code = TREE_CODE (ref);
4693 switch (code)
4695 case REALPART_EXPR:
4696 case IMAGPART_EXPR:
4697 case COMPONENT_REF:
4698 return lvalue_p (TREE_OPERAND (ref, 0));
4700 case C_MAYBE_CONST_EXPR:
4701 return lvalue_p (TREE_OPERAND (ref, 1));
4703 case COMPOUND_LITERAL_EXPR:
4704 case STRING_CST:
4705 return true;
4707 case INDIRECT_REF:
4708 case ARRAY_REF:
4709 case ARRAY_NOTATION_REF:
4710 case VAR_DECL:
4711 case PARM_DECL:
4712 case RESULT_DECL:
4713 case ERROR_MARK:
4714 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4715 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4717 case BIND_EXPR:
4718 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4720 default:
4721 return false;
4725 /* Give a warning for storing in something that is read-only in GCC
4726 terms but not const in ISO C terms. */
4728 static void
4729 readonly_warning (tree arg, enum lvalue_use use)
4731 switch (use)
4733 case lv_assign:
4734 warning (0, "assignment of read-only location %qE", arg);
4735 break;
4736 case lv_increment:
4737 warning (0, "increment of read-only location %qE", arg);
4738 break;
4739 case lv_decrement:
4740 warning (0, "decrement of read-only location %qE", arg);
4741 break;
4742 default:
4743 gcc_unreachable ();
4745 return;
4749 /* Return nonzero if REF is an lvalue valid for this language;
4750 otherwise, print an error message and return zero. USE says
4751 how the lvalue is being used and so selects the error message.
4752 LOCATION is the location at which any error should be reported. */
4754 static int
4755 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4757 int win = lvalue_p (ref);
4759 if (!win)
4760 lvalue_error (loc, use);
4762 return win;
4765 /* Mark EXP saying that we need to be able to take the
4766 address of it; it should not be allocated in a register.
4767 Returns true if successful. ARRAY_REF_P is true if this
4768 is for ARRAY_REF construction - in that case we don't want
4769 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4770 it is fine to use ARRAY_REFs for vector subscripts on vector
4771 register variables. */
4773 bool
4774 c_mark_addressable (tree exp, bool array_ref_p)
4776 tree x = exp;
4778 while (1)
4779 switch (TREE_CODE (x))
4781 case VIEW_CONVERT_EXPR:
4782 if (array_ref_p
4783 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4784 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4785 return true;
4786 /* FALLTHRU */
4787 case COMPONENT_REF:
4788 case ADDR_EXPR:
4789 case ARRAY_REF:
4790 case REALPART_EXPR:
4791 case IMAGPART_EXPR:
4792 x = TREE_OPERAND (x, 0);
4793 break;
4795 case COMPOUND_LITERAL_EXPR:
4796 case CONSTRUCTOR:
4797 TREE_ADDRESSABLE (x) = 1;
4798 return true;
4800 case VAR_DECL:
4801 case CONST_DECL:
4802 case PARM_DECL:
4803 case RESULT_DECL:
4804 if (C_DECL_REGISTER (x)
4805 && DECL_NONLOCAL (x))
4807 if (TREE_PUBLIC (x) || is_global_var (x))
4809 error
4810 ("global register variable %qD used in nested function", x);
4811 return false;
4813 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4815 else if (C_DECL_REGISTER (x))
4817 if (TREE_PUBLIC (x) || is_global_var (x))
4818 error ("address of global register variable %qD requested", x);
4819 else
4820 error ("address of register variable %qD requested", x);
4821 return false;
4824 /* FALLTHRU */
4825 case FUNCTION_DECL:
4826 TREE_ADDRESSABLE (x) = 1;
4827 /* FALLTHRU */
4828 default:
4829 return true;
4833 /* Convert EXPR to TYPE, warning about conversion problems with
4834 constants. SEMANTIC_TYPE is the type this conversion would use
4835 without excess precision. If SEMANTIC_TYPE is NULL, this function
4836 is equivalent to convert_and_check. This function is a wrapper that
4837 handles conversions that may be different than
4838 the usual ones because of excess precision. */
4840 static tree
4841 ep_convert_and_check (location_t loc, tree type, tree expr,
4842 tree semantic_type)
4844 if (TREE_TYPE (expr) == type)
4845 return expr;
4847 if (!semantic_type)
4848 return convert_and_check (loc, type, expr);
4850 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4851 && TREE_TYPE (expr) != semantic_type)
4853 /* For integers, we need to check the real conversion, not
4854 the conversion to the excess precision type. */
4855 expr = convert_and_check (loc, semantic_type, expr);
4857 /* Result type is the excess precision type, which should be
4858 large enough, so do not check. */
4859 return convert (type, expr);
4862 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4863 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4864 if folded to an integer constant then the unselected half may
4865 contain arbitrary operations not normally permitted in constant
4866 expressions. Set the location of the expression to LOC. */
4868 tree
4869 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4870 tree op1, tree op1_original_type, location_t op1_loc,
4871 tree op2, tree op2_original_type, location_t op2_loc)
4873 tree type1;
4874 tree type2;
4875 enum tree_code code1;
4876 enum tree_code code2;
4877 tree result_type = NULL;
4878 tree semantic_result_type = NULL;
4879 tree orig_op1 = op1, orig_op2 = op2;
4880 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4881 bool ifexp_int_operands;
4882 tree ret;
4884 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4885 if (op1_int_operands)
4886 op1 = remove_c_maybe_const_expr (op1);
4887 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4888 if (op2_int_operands)
4889 op2 = remove_c_maybe_const_expr (op2);
4890 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4891 if (ifexp_int_operands)
4892 ifexp = remove_c_maybe_const_expr (ifexp);
4894 /* Promote both alternatives. */
4896 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4897 op1 = default_conversion (op1);
4898 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4899 op2 = default_conversion (op2);
4901 if (TREE_CODE (ifexp) == ERROR_MARK
4902 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4903 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4904 return error_mark_node;
4906 type1 = TREE_TYPE (op1);
4907 code1 = TREE_CODE (type1);
4908 type2 = TREE_TYPE (op2);
4909 code2 = TREE_CODE (type2);
4911 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4912 return error_mark_node;
4914 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4915 return error_mark_node;
4917 /* C90 does not permit non-lvalue arrays in conditional expressions.
4918 In C99 they will be pointers by now. */
4919 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4921 error_at (colon_loc, "non-lvalue array in conditional expression");
4922 return error_mark_node;
4925 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4926 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4927 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4928 || code1 == COMPLEX_TYPE)
4929 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4930 || code2 == COMPLEX_TYPE))
4932 semantic_result_type = c_common_type (type1, type2);
4933 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4935 op1 = TREE_OPERAND (op1, 0);
4936 type1 = TREE_TYPE (op1);
4937 gcc_assert (TREE_CODE (type1) == code1);
4939 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4941 op2 = TREE_OPERAND (op2, 0);
4942 type2 = TREE_TYPE (op2);
4943 gcc_assert (TREE_CODE (type2) == code2);
4947 if (warn_cxx_compat)
4949 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4950 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4952 if (TREE_CODE (t1) == ENUMERAL_TYPE
4953 && TREE_CODE (t2) == ENUMERAL_TYPE
4954 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4955 warning_at (colon_loc, OPT_Wc___compat,
4956 ("different enum types in conditional is "
4957 "invalid in C++: %qT vs %qT"),
4958 t1, t2);
4961 /* Quickly detect the usual case where op1 and op2 have the same type
4962 after promotion. */
4963 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4965 if (type1 == type2)
4966 result_type = type1;
4967 else
4968 result_type = TYPE_MAIN_VARIANT (type1);
4970 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4971 || code1 == COMPLEX_TYPE)
4972 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4973 || code2 == COMPLEX_TYPE))
4975 result_type = c_common_type (type1, type2);
4976 if (result_type == error_mark_node)
4977 return error_mark_node;
4978 do_warn_double_promotion (result_type, type1, type2,
4979 "implicit conversion from %qT to %qT to "
4980 "match other result of conditional",
4981 colon_loc);
4983 /* If -Wsign-compare, warn here if type1 and type2 have
4984 different signedness. We'll promote the signed to unsigned
4985 and later code won't know it used to be different.
4986 Do this check on the original types, so that explicit casts
4987 will be considered, but default promotions won't. */
4988 if (c_inhibit_evaluation_warnings == 0)
4990 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4991 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4993 if (unsigned_op1 ^ unsigned_op2)
4995 bool ovf;
4997 /* Do not warn if the result type is signed, since the
4998 signed type will only be chosen if it can represent
4999 all the values of the unsigned type. */
5000 if (!TYPE_UNSIGNED (result_type))
5001 /* OK */;
5002 else
5004 bool op1_maybe_const = true;
5005 bool op2_maybe_const = true;
5007 /* Do not warn if the signed quantity is an
5008 unsuffixed integer literal (or some static
5009 constant expression involving such literals) and
5010 it is non-negative. This warning requires the
5011 operands to be folded for best results, so do
5012 that folding in this case even without
5013 warn_sign_compare to avoid warning options
5014 possibly affecting code generation. */
5015 c_inhibit_evaluation_warnings
5016 += (ifexp == truthvalue_false_node);
5017 op1 = c_fully_fold (op1, require_constant_value,
5018 &op1_maybe_const);
5019 c_inhibit_evaluation_warnings
5020 -= (ifexp == truthvalue_false_node);
5022 c_inhibit_evaluation_warnings
5023 += (ifexp == truthvalue_true_node);
5024 op2 = c_fully_fold (op2, require_constant_value,
5025 &op2_maybe_const);
5026 c_inhibit_evaluation_warnings
5027 -= (ifexp == truthvalue_true_node);
5029 if (warn_sign_compare)
5031 if ((unsigned_op2
5032 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5033 || (unsigned_op1
5034 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5035 /* OK */;
5036 else if (unsigned_op2)
5037 warning_at (op1_loc, OPT_Wsign_compare,
5038 "operand of ?: changes signedness from "
5039 "%qT to %qT due to unsignedness of other "
5040 "operand", TREE_TYPE (orig_op1),
5041 TREE_TYPE (orig_op2));
5042 else
5043 warning_at (op2_loc, OPT_Wsign_compare,
5044 "operand of ?: changes signedness from "
5045 "%qT to %qT due to unsignedness of other "
5046 "operand", TREE_TYPE (orig_op2),
5047 TREE_TYPE (orig_op1));
5049 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5050 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5051 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5052 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5057 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5059 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5060 pedwarn (colon_loc, OPT_Wpedantic,
5061 "ISO C forbids conditional expr with only one void side");
5062 result_type = void_type_node;
5064 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5066 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5067 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5068 addr_space_t as_common;
5070 if (comp_target_types (colon_loc, type1, type2))
5071 result_type = common_pointer_type (type1, type2);
5072 else if (null_pointer_constant_p (orig_op1))
5073 result_type = type2;
5074 else if (null_pointer_constant_p (orig_op2))
5075 result_type = type1;
5076 else if (!addr_space_superset (as1, as2, &as_common))
5078 error_at (colon_loc, "pointers to disjoint address spaces "
5079 "used in conditional expression");
5080 return error_mark_node;
5082 else if (VOID_TYPE_P (TREE_TYPE (type1))
5083 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5085 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5086 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5087 & ~TYPE_QUALS (TREE_TYPE (type1))))
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 (type2)) == 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 (type1),
5097 TREE_TYPE (type2)));
5099 else if (VOID_TYPE_P (TREE_TYPE (type2))
5100 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5102 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5103 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5104 & ~TYPE_QUALS (TREE_TYPE (type2))))
5105 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5106 "pointer to array loses qualifier "
5107 "in conditional expression");
5109 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5110 pedwarn (colon_loc, OPT_Wpedantic,
5111 "ISO C forbids conditional expr between "
5112 "%<void *%> and function pointer");
5113 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5114 TREE_TYPE (type1)));
5116 /* Objective-C pointer comparisons are a bit more lenient. */
5117 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5118 result_type = objc_common_type (type1, type2);
5119 else
5121 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5123 pedwarn (colon_loc, 0,
5124 "pointer type mismatch in conditional expression");
5125 result_type = build_pointer_type
5126 (build_qualified_type (void_type_node, qual));
5129 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5131 if (!null_pointer_constant_p (orig_op2))
5132 pedwarn (colon_loc, 0,
5133 "pointer/integer type mismatch in conditional expression");
5134 else
5136 op2 = null_pointer_node;
5138 result_type = type1;
5140 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5142 if (!null_pointer_constant_p (orig_op1))
5143 pedwarn (colon_loc, 0,
5144 "pointer/integer type mismatch in conditional expression");
5145 else
5147 op1 = null_pointer_node;
5149 result_type = type2;
5152 if (!result_type)
5154 if (flag_cond_mismatch)
5155 result_type = void_type_node;
5156 else
5158 error_at (colon_loc, "type mismatch in conditional expression");
5159 return error_mark_node;
5163 /* Merge const and volatile flags of the incoming types. */
5164 result_type
5165 = build_type_variant (result_type,
5166 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5167 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5169 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5170 semantic_result_type);
5171 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5172 semantic_result_type);
5174 if (ifexp_bcp && ifexp == truthvalue_true_node)
5176 op2_int_operands = true;
5177 op1 = c_fully_fold (op1, require_constant_value, NULL);
5179 if (ifexp_bcp && ifexp == truthvalue_false_node)
5181 op1_int_operands = true;
5182 op2 = c_fully_fold (op2, require_constant_value, NULL);
5184 int_const = int_operands = (ifexp_int_operands
5185 && op1_int_operands
5186 && op2_int_operands);
5187 if (int_operands)
5189 int_const = ((ifexp == truthvalue_true_node
5190 && TREE_CODE (orig_op1) == INTEGER_CST
5191 && !TREE_OVERFLOW (orig_op1))
5192 || (ifexp == truthvalue_false_node
5193 && TREE_CODE (orig_op2) == INTEGER_CST
5194 && !TREE_OVERFLOW (orig_op2)));
5197 /* Need to convert condition operand into a vector mask. */
5198 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5200 tree vectype = TREE_TYPE (ifexp);
5201 tree elem_type = TREE_TYPE (vectype);
5202 tree zero = build_int_cst (elem_type, 0);
5203 tree zero_vec = build_vector_from_val (vectype, zero);
5204 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5205 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5208 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5209 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5210 else
5212 if (int_operands)
5214 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5215 nested inside of the expression. */
5216 op1 = c_fully_fold (op1, false, NULL);
5217 op2 = c_fully_fold (op2, false, NULL);
5219 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5220 if (int_operands)
5221 ret = note_integer_operands (ret);
5223 if (semantic_result_type)
5224 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5226 protected_set_expr_location (ret, colon_loc);
5228 /* If the OP1 and OP2 are the same and don't have side-effects,
5229 warn here, because the COND_EXPR will be turned into OP1. */
5230 if (warn_duplicated_branches
5231 && TREE_CODE (ret) == COND_EXPR
5232 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5233 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5234 "this condition has identical branches");
5236 return ret;
5239 /* Return a compound expression that performs two expressions and
5240 returns the value of the second of them.
5242 LOC is the location of the COMPOUND_EXPR. */
5244 tree
5245 build_compound_expr (location_t loc, tree expr1, tree expr2)
5247 bool expr1_int_operands, expr2_int_operands;
5248 tree eptype = NULL_TREE;
5249 tree ret;
5251 if (flag_cilkplus
5252 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5253 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5255 error_at (loc,
5256 "spawned function call cannot be part of a comma expression");
5257 return error_mark_node;
5259 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5260 if (expr1_int_operands)
5261 expr1 = remove_c_maybe_const_expr (expr1);
5262 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5263 if (expr2_int_operands)
5264 expr2 = remove_c_maybe_const_expr (expr2);
5266 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5267 expr1 = TREE_OPERAND (expr1, 0);
5268 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5270 eptype = TREE_TYPE (expr2);
5271 expr2 = TREE_OPERAND (expr2, 0);
5274 if (!TREE_SIDE_EFFECTS (expr1))
5276 /* The left-hand operand of a comma expression is like an expression
5277 statement: with -Wunused, we should warn if it doesn't have
5278 any side-effects, unless it was explicitly cast to (void). */
5279 if (warn_unused_value)
5281 if (VOID_TYPE_P (TREE_TYPE (expr1))
5282 && CONVERT_EXPR_P (expr1))
5283 ; /* (void) a, b */
5284 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5285 && TREE_CODE (expr1) == COMPOUND_EXPR
5286 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5287 ; /* (void) a, (void) b, c */
5288 else
5289 warning_at (loc, OPT_Wunused_value,
5290 "left-hand operand of comma expression has no effect");
5293 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5294 && warn_unused_value)
5296 tree r = expr1;
5297 location_t cloc = loc;
5298 while (TREE_CODE (r) == COMPOUND_EXPR)
5300 if (EXPR_HAS_LOCATION (r))
5301 cloc = EXPR_LOCATION (r);
5302 r = TREE_OPERAND (r, 1);
5304 if (!TREE_SIDE_EFFECTS (r)
5305 && !VOID_TYPE_P (TREE_TYPE (r))
5306 && !CONVERT_EXPR_P (r))
5307 warning_at (cloc, OPT_Wunused_value,
5308 "right-hand operand of comma expression has no effect");
5311 /* With -Wunused, we should also warn if the left-hand operand does have
5312 side-effects, but computes a value which is not used. For example, in
5313 `foo() + bar(), baz()' the result of the `+' operator is not used,
5314 so we should issue a warning. */
5315 else if (warn_unused_value)
5316 warn_if_unused_value (expr1, loc);
5318 if (expr2 == error_mark_node)
5319 return error_mark_node;
5321 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5323 if (flag_isoc99
5324 && expr1_int_operands
5325 && expr2_int_operands)
5326 ret = note_integer_operands (ret);
5328 if (eptype)
5329 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5331 protected_set_expr_location (ret, loc);
5332 return ret;
5335 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5336 which we are casting. OTYPE is the type of the expression being
5337 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5338 of the cast. -Wcast-qual appeared on the command line. Named
5339 address space qualifiers are not handled here, because they result
5340 in different warnings. */
5342 static void
5343 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5345 tree in_type = type;
5346 tree in_otype = otype;
5347 int added = 0;
5348 int discarded = 0;
5349 bool is_const;
5351 /* Check that the qualifiers on IN_TYPE are a superset of the
5352 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5353 nodes is uninteresting and we stop as soon as we hit a
5354 non-POINTER_TYPE node on either type. */
5357 in_otype = TREE_TYPE (in_otype);
5358 in_type = TREE_TYPE (in_type);
5360 /* GNU C allows cv-qualified function types. 'const' means the
5361 function is very pure, 'volatile' means it can't return. We
5362 need to warn when such qualifiers are added, not when they're
5363 taken away. */
5364 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5365 && TREE_CODE (in_type) == FUNCTION_TYPE)
5366 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5367 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5368 else
5369 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5370 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5372 while (TREE_CODE (in_type) == POINTER_TYPE
5373 && TREE_CODE (in_otype) == POINTER_TYPE);
5375 if (added)
5376 warning_at (loc, OPT_Wcast_qual,
5377 "cast adds %q#v qualifier to function type", added);
5379 if (discarded)
5380 /* There are qualifiers present in IN_OTYPE that are not present
5381 in IN_TYPE. */
5382 warning_at (loc, OPT_Wcast_qual,
5383 "cast discards %qv qualifier from pointer target type",
5384 discarded);
5386 if (added || discarded)
5387 return;
5389 /* A cast from **T to const **T is unsafe, because it can cause a
5390 const value to be changed with no additional warning. We only
5391 issue this warning if T is the same on both sides, and we only
5392 issue the warning if there are the same number of pointers on
5393 both sides, as otherwise the cast is clearly unsafe anyhow. A
5394 cast is unsafe when a qualifier is added at one level and const
5395 is not present at all outer levels.
5397 To issue this warning, we check at each level whether the cast
5398 adds new qualifiers not already seen. We don't need to special
5399 case function types, as they won't have the same
5400 TYPE_MAIN_VARIANT. */
5402 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5403 return;
5404 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5405 return;
5407 in_type = type;
5408 in_otype = otype;
5409 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5412 in_type = TREE_TYPE (in_type);
5413 in_otype = TREE_TYPE (in_otype);
5414 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5415 && !is_const)
5417 warning_at (loc, OPT_Wcast_qual,
5418 "to be safe all intermediate pointers in cast from "
5419 "%qT to %qT must be %<const%> qualified",
5420 otype, type);
5421 break;
5423 if (is_const)
5424 is_const = TYPE_READONLY (in_type);
5426 while (TREE_CODE (in_type) == POINTER_TYPE);
5429 /* Build an expression representing a cast to type TYPE of expression EXPR.
5430 LOC is the location of the cast-- typically the open paren of the cast. */
5432 tree
5433 build_c_cast (location_t loc, tree type, tree expr)
5435 tree value;
5437 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5438 expr = TREE_OPERAND (expr, 0);
5440 value = expr;
5442 if (type == error_mark_node || expr == error_mark_node)
5443 return error_mark_node;
5445 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5446 only in <protocol> qualifications. But when constructing cast expressions,
5447 the protocols do matter and must be kept around. */
5448 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5449 return build1 (NOP_EXPR, type, expr);
5451 type = TYPE_MAIN_VARIANT (type);
5453 if (TREE_CODE (type) == ARRAY_TYPE)
5455 error_at (loc, "cast specifies array type");
5456 return error_mark_node;
5459 if (TREE_CODE (type) == FUNCTION_TYPE)
5461 error_at (loc, "cast specifies function type");
5462 return error_mark_node;
5465 if (!VOID_TYPE_P (type))
5467 value = require_complete_type (loc, value);
5468 if (value == error_mark_node)
5469 return error_mark_node;
5472 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5474 if (RECORD_OR_UNION_TYPE_P (type))
5475 pedwarn (loc, OPT_Wpedantic,
5476 "ISO C forbids casting nonscalar to the same type");
5478 /* Convert to remove any qualifiers from VALUE's type. */
5479 value = convert (type, value);
5481 else if (TREE_CODE (type) == UNION_TYPE)
5483 tree field;
5485 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5486 if (TREE_TYPE (field) != error_mark_node
5487 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5488 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5489 break;
5491 if (field)
5493 tree t;
5494 bool maybe_const = true;
5496 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5497 t = c_fully_fold (value, false, &maybe_const);
5498 t = build_constructor_single (type, field, t);
5499 if (!maybe_const)
5500 t = c_wrap_maybe_const (t, true);
5501 t = digest_init (loc, type, t,
5502 NULL_TREE, false, true, 0);
5503 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5504 return t;
5506 error_at (loc, "cast to union type from type not present in union");
5507 return error_mark_node;
5509 else
5511 tree otype, ovalue;
5513 if (type == void_type_node)
5515 tree t = build1 (CONVERT_EXPR, type, value);
5516 SET_EXPR_LOCATION (t, loc);
5517 return t;
5520 otype = TREE_TYPE (value);
5522 /* Optionally warn about potentially worrisome casts. */
5523 if (warn_cast_qual
5524 && TREE_CODE (type) == POINTER_TYPE
5525 && TREE_CODE (otype) == POINTER_TYPE)
5526 handle_warn_cast_qual (loc, type, otype);
5528 /* Warn about conversions between pointers to disjoint
5529 address spaces. */
5530 if (TREE_CODE (type) == POINTER_TYPE
5531 && TREE_CODE (otype) == POINTER_TYPE
5532 && !null_pointer_constant_p (value))
5534 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5535 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5536 addr_space_t as_common;
5538 if (!addr_space_superset (as_to, as_from, &as_common))
5540 if (ADDR_SPACE_GENERIC_P (as_from))
5541 warning_at (loc, 0, "cast to %s address space pointer "
5542 "from disjoint generic address space pointer",
5543 c_addr_space_name (as_to));
5545 else if (ADDR_SPACE_GENERIC_P (as_to))
5546 warning_at (loc, 0, "cast to generic address space pointer "
5547 "from disjoint %s address space pointer",
5548 c_addr_space_name (as_from));
5550 else
5551 warning_at (loc, 0, "cast to %s address space pointer "
5552 "from disjoint %s address space pointer",
5553 c_addr_space_name (as_to),
5554 c_addr_space_name (as_from));
5558 /* Warn about possible alignment problems. */
5559 if (STRICT_ALIGNMENT
5560 && TREE_CODE (type) == POINTER_TYPE
5561 && TREE_CODE (otype) == POINTER_TYPE
5562 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5563 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5564 /* Don't warn about opaque types, where the actual alignment
5565 restriction is unknown. */
5566 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5567 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5568 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5569 warning_at (loc, OPT_Wcast_align,
5570 "cast increases required alignment of target type");
5572 if (TREE_CODE (type) == INTEGER_TYPE
5573 && TREE_CODE (otype) == POINTER_TYPE
5574 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5575 /* Unlike conversion of integers to pointers, where the
5576 warning is disabled for converting constants because
5577 of cases such as SIG_*, warn about converting constant
5578 pointers to integers. In some cases it may cause unwanted
5579 sign extension, and a warning is appropriate. */
5580 warning_at (loc, OPT_Wpointer_to_int_cast,
5581 "cast from pointer to integer of different size");
5583 if (TREE_CODE (value) == CALL_EXPR
5584 && TREE_CODE (type) != TREE_CODE (otype))
5585 warning_at (loc, OPT_Wbad_function_cast,
5586 "cast from function call of type %qT "
5587 "to non-matching type %qT", otype, type);
5589 if (TREE_CODE (type) == POINTER_TYPE
5590 && TREE_CODE (otype) == INTEGER_TYPE
5591 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5592 /* Don't warn about converting any constant. */
5593 && !TREE_CONSTANT (value))
5594 warning_at (loc,
5595 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5596 "of different size");
5598 if (warn_strict_aliasing <= 2)
5599 strict_aliasing_warning (otype, type, expr);
5601 /* If pedantic, warn for conversions between function and object
5602 pointer types, except for converting a null pointer constant
5603 to function pointer type. */
5604 if (pedantic
5605 && TREE_CODE (type) == POINTER_TYPE
5606 && TREE_CODE (otype) == POINTER_TYPE
5607 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5608 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5609 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5610 "conversion of function pointer to object pointer type");
5612 if (pedantic
5613 && TREE_CODE (type) == POINTER_TYPE
5614 && TREE_CODE (otype) == POINTER_TYPE
5615 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5616 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5617 && !null_pointer_constant_p (value))
5618 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5619 "conversion of object pointer to function pointer type");
5621 ovalue = value;
5622 value = convert (type, value);
5624 /* Ignore any integer overflow caused by the cast. */
5625 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5627 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5629 if (!TREE_OVERFLOW (value))
5631 /* Avoid clobbering a shared constant. */
5632 value = copy_node (value);
5633 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5636 else if (TREE_OVERFLOW (value))
5637 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5638 value = wide_int_to_tree (TREE_TYPE (value), value);
5642 /* Don't let a cast be an lvalue. */
5643 if (lvalue_p (value))
5644 value = non_lvalue_loc (loc, value);
5646 /* Don't allow the results of casting to floating-point or complex
5647 types be confused with actual constants, or casts involving
5648 integer and pointer types other than direct integer-to-integer
5649 and integer-to-pointer be confused with integer constant
5650 expressions and null pointer constants. */
5651 if (TREE_CODE (value) == REAL_CST
5652 || TREE_CODE (value) == COMPLEX_CST
5653 || (TREE_CODE (value) == INTEGER_CST
5654 && !((TREE_CODE (expr) == INTEGER_CST
5655 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5656 || TREE_CODE (expr) == REAL_CST
5657 || TREE_CODE (expr) == COMPLEX_CST)))
5658 value = build1 (NOP_EXPR, type, value);
5660 protected_set_expr_location (value, loc);
5661 return value;
5664 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5665 location of the open paren of the cast, or the position of the cast
5666 expr. */
5667 tree
5668 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5670 tree type;
5671 tree type_expr = NULL_TREE;
5672 bool type_expr_const = true;
5673 tree ret;
5674 int saved_wsp = warn_strict_prototypes;
5676 /* This avoids warnings about unprototyped casts on
5677 integers. E.g. "#define SIG_DFL (void(*)())0". */
5678 if (TREE_CODE (expr) == INTEGER_CST)
5679 warn_strict_prototypes = 0;
5680 type = groktypename (type_name, &type_expr, &type_expr_const);
5681 warn_strict_prototypes = saved_wsp;
5683 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5684 && reject_gcc_builtin (expr))
5685 return error_mark_node;
5687 ret = build_c_cast (loc, type, expr);
5688 if (type_expr)
5690 bool inner_expr_const = true;
5691 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5692 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5693 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5694 && inner_expr_const);
5695 SET_EXPR_LOCATION (ret, loc);
5698 if (!EXPR_HAS_LOCATION (ret))
5699 protected_set_expr_location (ret, loc);
5701 /* C++ does not permits types to be defined in a cast, but it
5702 allows references to incomplete types. */
5703 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5704 warning_at (loc, OPT_Wc___compat,
5705 "defining a type in a cast is invalid in C++");
5707 return ret;
5710 /* Build an assignment expression of lvalue LHS from value RHS.
5711 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5712 may differ from TREE_TYPE (LHS) for an enum bitfield.
5713 MODIFYCODE is the code for a binary operator that we use
5714 to combine the old value of LHS with RHS to get the new value.
5715 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5716 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5717 which may differ from TREE_TYPE (RHS) for an enum value.
5719 LOCATION is the location of the MODIFYCODE operator.
5720 RHS_LOC is the location of the RHS. */
5722 tree
5723 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5724 enum tree_code modifycode,
5725 location_t rhs_loc, tree rhs, tree rhs_origtype)
5727 tree result;
5728 tree newrhs;
5729 tree rhseval = NULL_TREE;
5730 tree rhs_semantic_type = NULL_TREE;
5731 tree lhstype = TREE_TYPE (lhs);
5732 tree olhstype = lhstype;
5733 bool npc;
5734 bool is_atomic_op;
5736 /* Types that aren't fully specified cannot be used in assignments. */
5737 lhs = require_complete_type (location, lhs);
5739 /* Avoid duplicate error messages from operands that had errors. */
5740 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5741 return error_mark_node;
5743 /* Ensure an error for assigning a non-lvalue array to an array in
5744 C90. */
5745 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5747 error_at (location, "assignment to expression with array type");
5748 return error_mark_node;
5751 /* For ObjC properties, defer this check. */
5752 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5753 return error_mark_node;
5755 is_atomic_op = really_atomic_lvalue (lhs);
5757 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5759 rhs_semantic_type = TREE_TYPE (rhs);
5760 rhs = TREE_OPERAND (rhs, 0);
5763 newrhs = rhs;
5765 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5767 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5768 lhs_origtype, modifycode, rhs_loc, rhs,
5769 rhs_origtype);
5770 if (inner == error_mark_node)
5771 return error_mark_node;
5772 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5773 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5774 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5775 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5776 protected_set_expr_location (result, location);
5777 return result;
5780 /* If a binary op has been requested, combine the old LHS value with the RHS
5781 producing the value we should actually store into the LHS. */
5783 if (modifycode != NOP_EXPR)
5785 lhs = c_fully_fold (lhs, false, NULL);
5786 lhs = stabilize_reference (lhs);
5788 /* Construct the RHS for any non-atomic compound assignemnt. */
5789 if (!is_atomic_op)
5791 /* If in LHS op= RHS the RHS has side-effects, ensure they
5792 are preevaluated before the rest of the assignment expression's
5793 side-effects, because RHS could contain e.g. function calls
5794 that modify LHS. */
5795 if (TREE_SIDE_EFFECTS (rhs))
5797 newrhs = save_expr (rhs);
5798 rhseval = newrhs;
5800 newrhs = build_binary_op (location,
5801 modifycode, lhs, newrhs, true);
5803 /* The original type of the right hand side is no longer
5804 meaningful. */
5805 rhs_origtype = NULL_TREE;
5809 if (c_dialect_objc ())
5811 /* Check if we are modifying an Objective-C property reference;
5812 if so, we need to generate setter calls. */
5813 result = objc_maybe_build_modify_expr (lhs, newrhs);
5814 if (result)
5815 goto return_result;
5817 /* Else, do the check that we postponed for Objective-C. */
5818 if (!lvalue_or_else (location, lhs, lv_assign))
5819 return error_mark_node;
5822 /* Give an error for storing in something that is 'const'. */
5824 if (TYPE_READONLY (lhstype)
5825 || (RECORD_OR_UNION_TYPE_P (lhstype)
5826 && C_TYPE_FIELDS_READONLY (lhstype)))
5828 readonly_error (location, lhs, lv_assign);
5829 return error_mark_node;
5831 else if (TREE_READONLY (lhs))
5832 readonly_warning (lhs, lv_assign);
5834 /* If storing into a structure or union member,
5835 it has probably been given type `int'.
5836 Compute the type that would go with
5837 the actual amount of storage the member occupies. */
5839 if (TREE_CODE (lhs) == COMPONENT_REF
5840 && (TREE_CODE (lhstype) == INTEGER_TYPE
5841 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5842 || TREE_CODE (lhstype) == REAL_TYPE
5843 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5844 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5846 /* If storing in a field that is in actuality a short or narrower than one,
5847 we must store in the field in its actual type. */
5849 if (lhstype != TREE_TYPE (lhs))
5851 lhs = copy_node (lhs);
5852 TREE_TYPE (lhs) = lhstype;
5855 /* Issue -Wc++-compat warnings about an assignment to an enum type
5856 when LHS does not have its original type. This happens for,
5857 e.g., an enum bitfield in a struct. */
5858 if (warn_cxx_compat
5859 && lhs_origtype != NULL_TREE
5860 && lhs_origtype != lhstype
5861 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5863 tree checktype = (rhs_origtype != NULL_TREE
5864 ? rhs_origtype
5865 : TREE_TYPE (rhs));
5866 if (checktype != error_mark_node
5867 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5868 || (is_atomic_op && modifycode != NOP_EXPR)))
5869 warning_at (location, OPT_Wc___compat,
5870 "enum conversion in assignment is invalid in C++");
5873 /* If the lhs is atomic, remove that qualifier. */
5874 if (is_atomic_op)
5876 lhstype = build_qualified_type (lhstype,
5877 (TYPE_QUALS (lhstype)
5878 & ~TYPE_QUAL_ATOMIC));
5879 olhstype = build_qualified_type (olhstype,
5880 (TYPE_QUALS (lhstype)
5881 & ~TYPE_QUAL_ATOMIC));
5884 /* Convert new value to destination type. Fold it first, then
5885 restore any excess precision information, for the sake of
5886 conversion warnings. */
5888 if (!(is_atomic_op && modifycode != NOP_EXPR))
5890 npc = null_pointer_constant_p (newrhs);
5891 newrhs = c_fully_fold (newrhs, false, NULL);
5892 if (rhs_semantic_type)
5893 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5894 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5895 rhs_origtype, ic_assign, npc,
5896 NULL_TREE, NULL_TREE, 0);
5897 if (TREE_CODE (newrhs) == ERROR_MARK)
5898 return error_mark_node;
5901 /* Emit ObjC write barrier, if necessary. */
5902 if (c_dialect_objc () && flag_objc_gc)
5904 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5905 if (result)
5907 protected_set_expr_location (result, location);
5908 goto return_result;
5912 /* Scan operands. */
5914 if (is_atomic_op)
5915 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5916 else
5918 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5919 TREE_SIDE_EFFECTS (result) = 1;
5920 protected_set_expr_location (result, location);
5923 /* If we got the LHS in a different type for storing in,
5924 convert the result back to the nominal type of LHS
5925 so that the value we return always has the same type
5926 as the LHS argument. */
5928 if (olhstype == TREE_TYPE (result))
5929 goto return_result;
5931 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5932 rhs_origtype, ic_assign, false, NULL_TREE,
5933 NULL_TREE, 0);
5934 protected_set_expr_location (result, location);
5936 return_result:
5937 if (rhseval)
5938 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5939 return result;
5942 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5943 This is used to implement -fplan9-extensions. */
5945 static bool
5946 find_anonymous_field_with_type (tree struct_type, tree type)
5948 tree field;
5949 bool found;
5951 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5952 found = false;
5953 for (field = TYPE_FIELDS (struct_type);
5954 field != NULL_TREE;
5955 field = TREE_CHAIN (field))
5957 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5958 ? c_build_qualified_type (TREE_TYPE (field),
5959 TYPE_QUAL_ATOMIC)
5960 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5961 if (DECL_NAME (field) == NULL
5962 && comptypes (type, fieldtype))
5964 if (found)
5965 return false;
5966 found = true;
5968 else if (DECL_NAME (field) == NULL
5969 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5970 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5972 if (found)
5973 return false;
5974 found = true;
5977 return found;
5980 /* RHS is an expression whose type is pointer to struct. If there is
5981 an anonymous field in RHS with type TYPE, then return a pointer to
5982 that field in RHS. This is used with -fplan9-extensions. This
5983 returns NULL if no conversion could be found. */
5985 static tree
5986 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5988 tree rhs_struct_type, lhs_main_type;
5989 tree field, found_field;
5990 bool found_sub_field;
5991 tree ret;
5993 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5994 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5995 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5997 gcc_assert (POINTER_TYPE_P (type));
5998 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5999 ? c_build_qualified_type (TREE_TYPE (type),
6000 TYPE_QUAL_ATOMIC)
6001 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6003 found_field = NULL_TREE;
6004 found_sub_field = false;
6005 for (field = TYPE_FIELDS (rhs_struct_type);
6006 field != NULL_TREE;
6007 field = TREE_CHAIN (field))
6009 if (DECL_NAME (field) != NULL_TREE
6010 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6011 continue;
6012 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6013 ? c_build_qualified_type (TREE_TYPE (field),
6014 TYPE_QUAL_ATOMIC)
6015 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6016 if (comptypes (lhs_main_type, fieldtype))
6018 if (found_field != NULL_TREE)
6019 return NULL_TREE;
6020 found_field = field;
6022 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6023 lhs_main_type))
6025 if (found_field != NULL_TREE)
6026 return NULL_TREE;
6027 found_field = field;
6028 found_sub_field = true;
6032 if (found_field == NULL_TREE)
6033 return NULL_TREE;
6035 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6036 build_fold_indirect_ref (rhs), found_field,
6037 NULL_TREE);
6038 ret = build_fold_addr_expr_loc (location, ret);
6040 if (found_sub_field)
6042 ret = convert_to_anonymous_field (location, type, ret);
6043 gcc_assert (ret != NULL_TREE);
6046 return ret;
6049 /* Issue an error message for a bad initializer component.
6050 GMSGID identifies the message.
6051 The component name is taken from the spelling stack. */
6053 static void
6054 error_init (location_t loc, const char *gmsgid)
6056 char *ofwhat;
6058 /* The gmsgid may be a format string with %< and %>. */
6059 error_at (loc, gmsgid);
6060 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6061 if (*ofwhat)
6062 inform (loc, "(near initialization for %qs)", ofwhat);
6065 /* Issue a pedantic warning for a bad initializer component. OPT is
6066 the option OPT_* (from options.h) controlling this warning or 0 if
6067 it is unconditionally given. GMSGID identifies the message. The
6068 component name is taken from the spelling stack. */
6070 static void ATTRIBUTE_GCC_DIAG (3,0)
6071 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6073 /* Use the location where a macro was expanded rather than where
6074 it was defined to make sure macros defined in system headers
6075 but used incorrectly elsewhere are diagnosed. */
6076 source_location exploc = expansion_point_location_if_in_system_header (loc);
6078 va_list ap;
6079 va_start (ap, gmsgid);
6080 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6081 va_end (ap);
6082 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6083 if (*ofwhat && warned)
6084 inform (exploc, "(near initialization for %qs)", ofwhat);
6087 /* Issue a warning for a bad initializer component.
6089 OPT is the OPT_W* value corresponding to the warning option that
6090 controls this warning. GMSGID identifies the message. The
6091 component name is taken from the spelling stack. */
6093 static void
6094 warning_init (location_t loc, int opt, const char *gmsgid)
6096 char *ofwhat;
6097 bool warned;
6099 /* Use the location where a macro was expanded rather than where
6100 it was defined to make sure macros defined in system headers
6101 but used incorrectly elsewhere are diagnosed. */
6102 source_location exploc = expansion_point_location_if_in_system_header (loc);
6104 /* The gmsgid may be a format string with %< and %>. */
6105 warned = warning_at (exploc, opt, gmsgid);
6106 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6107 if (*ofwhat && warned)
6108 inform (exploc, "(near initialization for %qs)", ofwhat);
6111 /* If TYPE is an array type and EXPR is a parenthesized string
6112 constant, warn if pedantic that EXPR is being used to initialize an
6113 object of type TYPE. */
6115 void
6116 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6118 if (pedantic
6119 && TREE_CODE (type) == ARRAY_TYPE
6120 && TREE_CODE (expr.value) == STRING_CST
6121 && expr.original_code != STRING_CST)
6122 pedwarn_init (loc, OPT_Wpedantic,
6123 "array initialized from parenthesized string constant");
6126 /* Convert value RHS to type TYPE as preparation for an assignment to
6127 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6128 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6129 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6130 constant before any folding.
6131 The real work of conversion is done by `convert'.
6132 The purpose of this function is to generate error messages
6133 for assignments that are not allowed in C.
6134 ERRTYPE says whether it is argument passing, assignment,
6135 initialization or return.
6137 In the following example, '~' denotes where EXPR_LOC and '^' where
6138 LOCATION point to:
6140 f (var); [ic_argpass]
6141 ^ ~~~
6142 x = var; [ic_assign]
6143 ^ ~~~;
6144 int x = var; [ic_init]
6146 return x; [ic_return]
6149 FUNCTION is a tree for the function being called.
6150 PARMNUM is the number of the argument, for printing in error messages. */
6152 static tree
6153 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6154 tree rhs, tree origtype, enum impl_conv errtype,
6155 bool null_pointer_constant, tree fundecl,
6156 tree function, int parmnum)
6158 enum tree_code codel = TREE_CODE (type);
6159 tree orig_rhs = rhs;
6160 tree rhstype;
6161 enum tree_code coder;
6162 tree rname = NULL_TREE;
6163 bool objc_ok = false;
6165 /* Use the expansion point location to handle cases such as user's
6166 function returning a wrong-type macro defined in a system header. */
6167 location = expansion_point_location_if_in_system_header (location);
6169 if (errtype == ic_argpass)
6171 tree selector;
6172 /* Change pointer to function to the function itself for
6173 diagnostics. */
6174 if (TREE_CODE (function) == ADDR_EXPR
6175 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6176 function = TREE_OPERAND (function, 0);
6178 /* Handle an ObjC selector specially for diagnostics. */
6179 selector = objc_message_selector ();
6180 rname = function;
6181 if (selector && parmnum > 2)
6183 rname = selector;
6184 parmnum -= 2;
6188 /* This macro is used to emit diagnostics to ensure that all format
6189 strings are complete sentences, visible to gettext and checked at
6190 compile time. */
6191 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6192 do { \
6193 switch (errtype) \
6195 case ic_argpass: \
6196 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6197 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6198 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6199 "expected %qT but argument is of type %qT", \
6200 type, rhstype); \
6201 break; \
6202 case ic_assign: \
6203 pedwarn (LOCATION, OPT, AS); \
6204 break; \
6205 case ic_init: \
6206 pedwarn_init (LOCATION, OPT, IN); \
6207 break; \
6208 case ic_return: \
6209 pedwarn (LOCATION, OPT, RE); \
6210 break; \
6211 default: \
6212 gcc_unreachable (); \
6214 } while (0)
6216 /* This macro is used to emit diagnostics to ensure that all format
6217 strings are complete sentences, visible to gettext and checked at
6218 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6219 extra parameter to enumerate qualifiers. */
6220 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6221 do { \
6222 switch (errtype) \
6224 case ic_argpass: \
6225 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6226 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6227 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6228 "expected %qT but argument is of type %qT", \
6229 type, rhstype); \
6230 break; \
6231 case ic_assign: \
6232 pedwarn (LOCATION, OPT, AS, QUALS); \
6233 break; \
6234 case ic_init: \
6235 pedwarn (LOCATION, OPT, IN, QUALS); \
6236 break; \
6237 case ic_return: \
6238 pedwarn (LOCATION, OPT, RE, QUALS); \
6239 break; \
6240 default: \
6241 gcc_unreachable (); \
6243 } while (0)
6245 /* This macro is used to emit diagnostics to ensure that all format
6246 strings are complete sentences, visible to gettext and checked at
6247 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6248 warning_at instead of pedwarn. */
6249 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6250 do { \
6251 switch (errtype) \
6253 case ic_argpass: \
6254 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6255 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6256 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6257 "expected %qT but argument is of type %qT", \
6258 type, rhstype); \
6259 break; \
6260 case ic_assign: \
6261 warning_at (LOCATION, OPT, AS, QUALS); \
6262 break; \
6263 case ic_init: \
6264 warning_at (LOCATION, OPT, IN, QUALS); \
6265 break; \
6266 case ic_return: \
6267 warning_at (LOCATION, OPT, RE, QUALS); \
6268 break; \
6269 default: \
6270 gcc_unreachable (); \
6272 } while (0)
6274 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6275 rhs = TREE_OPERAND (rhs, 0);
6277 rhstype = TREE_TYPE (rhs);
6278 coder = TREE_CODE (rhstype);
6280 if (coder == ERROR_MARK)
6281 return error_mark_node;
6283 if (c_dialect_objc ())
6285 int parmno;
6287 switch (errtype)
6289 case ic_return:
6290 parmno = 0;
6291 break;
6293 case ic_assign:
6294 parmno = -1;
6295 break;
6297 case ic_init:
6298 parmno = -2;
6299 break;
6301 default:
6302 parmno = parmnum;
6303 break;
6306 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6309 if (warn_cxx_compat)
6311 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6312 if (checktype != error_mark_node
6313 && TREE_CODE (type) == ENUMERAL_TYPE
6314 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6315 switch (errtype)
6317 case ic_argpass:
6318 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6319 "passing argument %d of %qE is invalid in C++",
6320 parmnum, rname))
6321 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6322 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6323 "expected %qT but argument is of type %qT",
6324 type, rhstype);
6325 break;
6326 case ic_assign:
6327 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6328 "%qT in assignment is invalid in C++", rhstype, type);
6329 break;
6330 case ic_init:
6331 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6332 "%qT to %qT in initialization is invalid in C++",
6333 rhstype, type);
6334 break;
6335 case ic_return:
6336 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6337 "%qT in return is invalid in C++", rhstype, type);
6338 break;
6339 default:
6340 gcc_unreachable ();
6344 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6345 return rhs;
6347 if (coder == VOID_TYPE)
6349 /* Except for passing an argument to an unprototyped function,
6350 this is a constraint violation. When passing an argument to
6351 an unprototyped function, it is compile-time undefined;
6352 making it a constraint in that case was rejected in
6353 DR#252. */
6354 error_at (location, "void value not ignored as it ought to be");
6355 return error_mark_node;
6357 rhs = require_complete_type (location, rhs);
6358 if (rhs == error_mark_node)
6359 return error_mark_node;
6361 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6362 return error_mark_node;
6364 /* A non-reference type can convert to a reference. This handles
6365 va_start, va_copy and possibly port built-ins. */
6366 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6368 if (!lvalue_p (rhs))
6370 error_at (location, "cannot pass rvalue to reference parameter");
6371 return error_mark_node;
6373 if (!c_mark_addressable (rhs))
6374 return error_mark_node;
6375 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6376 SET_EXPR_LOCATION (rhs, location);
6378 rhs = convert_for_assignment (location, expr_loc,
6379 build_pointer_type (TREE_TYPE (type)),
6380 rhs, origtype, errtype,
6381 null_pointer_constant, fundecl, function,
6382 parmnum);
6383 if (rhs == error_mark_node)
6384 return error_mark_node;
6386 rhs = build1 (NOP_EXPR, type, rhs);
6387 SET_EXPR_LOCATION (rhs, location);
6388 return rhs;
6390 /* Some types can interconvert without explicit casts. */
6391 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6392 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6393 return convert (type, rhs);
6394 /* Arithmetic types all interconvert, and enum is treated like int. */
6395 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6396 || codel == FIXED_POINT_TYPE
6397 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6398 || codel == BOOLEAN_TYPE)
6399 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6400 || coder == FIXED_POINT_TYPE
6401 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6402 || coder == BOOLEAN_TYPE))
6404 tree ret;
6405 bool save = in_late_binary_op;
6406 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6407 || (coder == REAL_TYPE
6408 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6409 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6410 in_late_binary_op = true;
6411 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6412 ? expr_loc : location, type, orig_rhs);
6413 in_late_binary_op = save;
6414 return ret;
6417 /* Aggregates in different TUs might need conversion. */
6418 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6419 && codel == coder
6420 && comptypes (type, rhstype))
6421 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6422 ? expr_loc : location, type, rhs);
6424 /* Conversion to a transparent union or record from its member types.
6425 This applies only to function arguments. */
6426 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6427 && TYPE_TRANSPARENT_AGGR (type))
6428 && errtype == ic_argpass)
6430 tree memb, marginal_memb = NULL_TREE;
6432 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6434 tree memb_type = TREE_TYPE (memb);
6436 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6437 TYPE_MAIN_VARIANT (rhstype)))
6438 break;
6440 if (TREE_CODE (memb_type) != POINTER_TYPE)
6441 continue;
6443 if (coder == POINTER_TYPE)
6445 tree ttl = TREE_TYPE (memb_type);
6446 tree ttr = TREE_TYPE (rhstype);
6448 /* Any non-function converts to a [const][volatile] void *
6449 and vice versa; otherwise, targets must be the same.
6450 Meanwhile, the lhs target must have all the qualifiers of
6451 the rhs. */
6452 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6453 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6454 || comp_target_types (location, memb_type, rhstype))
6456 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6457 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6458 /* If this type won't generate any warnings, use it. */
6459 if (lquals == rquals
6460 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6461 && TREE_CODE (ttl) == FUNCTION_TYPE)
6462 ? ((lquals | rquals) == rquals)
6463 : ((lquals | rquals) == lquals)))
6464 break;
6466 /* Keep looking for a better type, but remember this one. */
6467 if (!marginal_memb)
6468 marginal_memb = memb;
6472 /* Can convert integer zero to any pointer type. */
6473 if (null_pointer_constant)
6475 rhs = null_pointer_node;
6476 break;
6480 if (memb || marginal_memb)
6482 if (!memb)
6484 /* We have only a marginally acceptable member type;
6485 it needs a warning. */
6486 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6487 tree ttr = TREE_TYPE (rhstype);
6489 /* Const and volatile mean something different for function
6490 types, so the usual warnings are not appropriate. */
6491 if (TREE_CODE (ttr) == FUNCTION_TYPE
6492 && TREE_CODE (ttl) == FUNCTION_TYPE)
6494 /* Because const and volatile on functions are
6495 restrictions that say the function will not do
6496 certain things, it is okay to use a const or volatile
6497 function where an ordinary one is wanted, but not
6498 vice-versa. */
6499 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6500 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6501 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6502 OPT_Wdiscarded_qualifiers,
6503 G_("passing argument %d of %qE "
6504 "makes %q#v qualified function "
6505 "pointer from unqualified"),
6506 G_("assignment makes %q#v qualified "
6507 "function pointer from "
6508 "unqualified"),
6509 G_("initialization makes %q#v qualified "
6510 "function pointer from "
6511 "unqualified"),
6512 G_("return makes %q#v qualified function "
6513 "pointer from unqualified"),
6514 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6516 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6517 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6518 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6519 OPT_Wdiscarded_qualifiers,
6520 G_("passing argument %d of %qE discards "
6521 "%qv qualifier from pointer target type"),
6522 G_("assignment discards %qv qualifier "
6523 "from pointer target type"),
6524 G_("initialization discards %qv qualifier "
6525 "from pointer target type"),
6526 G_("return discards %qv qualifier from "
6527 "pointer target type"),
6528 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6530 memb = marginal_memb;
6533 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6534 pedwarn (location, OPT_Wpedantic,
6535 "ISO C prohibits argument conversion to union type");
6537 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6538 return build_constructor_single (type, memb, rhs);
6542 /* Conversions among pointers */
6543 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6544 && (coder == codel))
6546 tree ttl = TREE_TYPE (type);
6547 tree ttr = TREE_TYPE (rhstype);
6548 tree mvl = ttl;
6549 tree mvr = ttr;
6550 bool is_opaque_pointer;
6551 int target_cmp = 0; /* Cache comp_target_types () result. */
6552 addr_space_t asl;
6553 addr_space_t asr;
6555 if (TREE_CODE (mvl) != ARRAY_TYPE)
6556 mvl = (TYPE_ATOMIC (mvl)
6557 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6558 TYPE_QUAL_ATOMIC)
6559 : TYPE_MAIN_VARIANT (mvl));
6560 if (TREE_CODE (mvr) != ARRAY_TYPE)
6561 mvr = (TYPE_ATOMIC (mvr)
6562 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6563 TYPE_QUAL_ATOMIC)
6564 : TYPE_MAIN_VARIANT (mvr));
6565 /* Opaque pointers are treated like void pointers. */
6566 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6568 /* The Plan 9 compiler permits a pointer to a struct to be
6569 automatically converted into a pointer to an anonymous field
6570 within the struct. */
6571 if (flag_plan9_extensions
6572 && RECORD_OR_UNION_TYPE_P (mvl)
6573 && RECORD_OR_UNION_TYPE_P (mvr)
6574 && mvl != mvr)
6576 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6577 if (new_rhs != NULL_TREE)
6579 rhs = new_rhs;
6580 rhstype = TREE_TYPE (rhs);
6581 coder = TREE_CODE (rhstype);
6582 ttr = TREE_TYPE (rhstype);
6583 mvr = TYPE_MAIN_VARIANT (ttr);
6587 /* C++ does not allow the implicit conversion void* -> T*. However,
6588 for the purpose of reducing the number of false positives, we
6589 tolerate the special case of
6591 int *p = NULL;
6593 where NULL is typically defined in C to be '(void *) 0'. */
6594 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6595 warning_at (errtype == ic_argpass ? expr_loc : location,
6596 OPT_Wc___compat,
6597 "request for implicit conversion "
6598 "from %qT to %qT not permitted in C++", rhstype, type);
6600 /* See if the pointers point to incompatible address spaces. */
6601 asl = TYPE_ADDR_SPACE (ttl);
6602 asr = TYPE_ADDR_SPACE (ttr);
6603 if (!null_pointer_constant_p (rhs)
6604 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6606 switch (errtype)
6608 case ic_argpass:
6609 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6610 "non-enclosed address space", parmnum, rname);
6611 break;
6612 case ic_assign:
6613 error_at (location, "assignment from pointer to "
6614 "non-enclosed address space");
6615 break;
6616 case ic_init:
6617 error_at (location, "initialization from pointer to "
6618 "non-enclosed address space");
6619 break;
6620 case ic_return:
6621 error_at (location, "return from pointer to "
6622 "non-enclosed address space");
6623 break;
6624 default:
6625 gcc_unreachable ();
6627 return error_mark_node;
6630 /* Check if the right-hand side has a format attribute but the
6631 left-hand side doesn't. */
6632 if (warn_suggest_attribute_format
6633 && check_missing_format_attribute (type, rhstype))
6635 switch (errtype)
6637 case ic_argpass:
6638 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6639 "argument %d of %qE might be "
6640 "a candidate for a format attribute",
6641 parmnum, rname);
6642 break;
6643 case ic_assign:
6644 warning_at (location, OPT_Wsuggest_attribute_format,
6645 "assignment left-hand side might be "
6646 "a candidate for a format attribute");
6647 break;
6648 case ic_init:
6649 warning_at (location, OPT_Wsuggest_attribute_format,
6650 "initialization left-hand side might be "
6651 "a candidate for a format attribute");
6652 break;
6653 case ic_return:
6654 warning_at (location, OPT_Wsuggest_attribute_format,
6655 "return type might be "
6656 "a candidate for a format attribute");
6657 break;
6658 default:
6659 gcc_unreachable ();
6663 /* Any non-function converts to a [const][volatile] void *
6664 and vice versa; otherwise, targets must be the same.
6665 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6666 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6667 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6668 || (target_cmp = comp_target_types (location, type, rhstype))
6669 || is_opaque_pointer
6670 || ((c_common_unsigned_type (mvl)
6671 == c_common_unsigned_type (mvr))
6672 && (c_common_signed_type (mvl)
6673 == c_common_signed_type (mvr))
6674 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6676 /* Warn about loss of qualifers from pointers to arrays with
6677 qualifiers on the element type. */
6678 if (TREE_CODE (ttr) == ARRAY_TYPE)
6680 ttr = strip_array_types (ttr);
6681 ttl = strip_array_types (ttl);
6683 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6684 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6685 WARNING_FOR_QUALIFIERS (location, expr_loc,
6686 OPT_Wdiscarded_array_qualifiers,
6687 G_("passing argument %d of %qE discards "
6688 "%qv qualifier from pointer target type"),
6689 G_("assignment discards %qv qualifier "
6690 "from pointer target type"),
6691 G_("initialization discards %qv qualifier "
6692 "from pointer target type"),
6693 G_("return discards %qv qualifier from "
6694 "pointer target type"),
6695 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6697 else if (pedantic
6698 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6700 (VOID_TYPE_P (ttr)
6701 && !null_pointer_constant
6702 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6703 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6704 G_("ISO C forbids passing argument %d of "
6705 "%qE between function pointer "
6706 "and %<void *%>"),
6707 G_("ISO C forbids assignment between "
6708 "function pointer and %<void *%>"),
6709 G_("ISO C forbids initialization between "
6710 "function pointer and %<void *%>"),
6711 G_("ISO C forbids return between function "
6712 "pointer and %<void *%>"));
6713 /* Const and volatile mean something different for function types,
6714 so the usual warnings are not appropriate. */
6715 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6716 && TREE_CODE (ttl) != FUNCTION_TYPE)
6718 /* Don't warn about loss of qualifier for conversions from
6719 qualified void* to pointers to arrays with corresponding
6720 qualifier on the element type. */
6721 if (!pedantic)
6722 ttl = strip_array_types (ttl);
6724 /* Assignments between atomic and non-atomic objects are OK. */
6725 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6726 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6728 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6729 OPT_Wdiscarded_qualifiers,
6730 G_("passing argument %d of %qE discards "
6731 "%qv qualifier from pointer target type"),
6732 G_("assignment discards %qv qualifier "
6733 "from pointer target type"),
6734 G_("initialization discards %qv qualifier "
6735 "from pointer target type"),
6736 G_("return discards %qv qualifier from "
6737 "pointer target type"),
6738 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6740 /* If this is not a case of ignoring a mismatch in signedness,
6741 no warning. */
6742 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6743 || target_cmp)
6745 /* If there is a mismatch, do warn. */
6746 else if (warn_pointer_sign)
6747 switch (errtype)
6749 case ic_argpass:
6750 if (pedwarn (expr_loc, OPT_Wpointer_sign,
6751 "pointer targets in passing argument %d of "
6752 "%qE differ in signedness", parmnum, rname))
6753 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6754 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6755 "expected %qT but argument is of type %qT",
6756 type, rhstype);
6757 break;
6758 case ic_assign:
6759 pedwarn (location, OPT_Wpointer_sign,
6760 "pointer targets in assignment from %qT to %qT "
6761 "differ in signedness", rhstype, type);
6762 break;
6763 case ic_init:
6764 pedwarn_init (location, OPT_Wpointer_sign,
6765 "pointer targets in initialization of %qT "
6766 "from %qT differ in signedness", type,
6767 rhstype);
6768 break;
6769 case ic_return:
6770 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6771 "returning %qT from a function with return type "
6772 "%qT differ in signedness", rhstype, type);
6773 break;
6774 default:
6775 gcc_unreachable ();
6778 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6779 && TREE_CODE (ttr) == FUNCTION_TYPE)
6781 /* Because const and volatile on functions are restrictions
6782 that say the function will not do certain things,
6783 it is okay to use a const or volatile function
6784 where an ordinary one is wanted, but not vice-versa. */
6785 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6786 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6787 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6788 OPT_Wdiscarded_qualifiers,
6789 G_("passing argument %d of %qE makes "
6790 "%q#v qualified function pointer "
6791 "from unqualified"),
6792 G_("assignment makes %q#v qualified function "
6793 "pointer from unqualified"),
6794 G_("initialization makes %q#v qualified "
6795 "function pointer from unqualified"),
6796 G_("return makes %q#v qualified function "
6797 "pointer from unqualified"),
6798 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6801 /* Avoid warning about the volatile ObjC EH puts on decls. */
6802 else if (!objc_ok)
6804 switch (errtype)
6806 case ic_argpass:
6807 if (pedwarn (expr_loc, OPT_Wincompatible_pointer_types,
6808 "passing argument %d of %qE from incompatible "
6809 "pointer type", parmnum, rname))
6810 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6811 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6812 "expected %qT but argument is of type %qT",
6813 type, rhstype);
6814 break;
6815 case ic_assign:
6816 pedwarn (location, OPT_Wincompatible_pointer_types,
6817 "assignment to %qT from incompatible pointer type %qT",
6818 type, rhstype);
6819 break;
6820 case ic_init:
6821 pedwarn_init (location, OPT_Wincompatible_pointer_types,
6822 "initialization of %qT from incompatible pointer "
6823 "type %qT", type, rhstype);
6824 break;
6825 case ic_return:
6826 pedwarn (location, OPT_Wincompatible_pointer_types,
6827 "returning %qT from a function with incompatible "
6828 "return type %qT", rhstype, type);
6829 break;
6830 default:
6831 gcc_unreachable ();
6835 return convert (type, rhs);
6837 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6839 /* ??? This should not be an error when inlining calls to
6840 unprototyped functions. */
6841 error_at (location, "invalid use of non-lvalue array");
6842 return error_mark_node;
6844 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6846 /* An explicit constant 0 can convert to a pointer,
6847 or one that results from arithmetic, even including
6848 a cast to integer type. */
6849 if (!null_pointer_constant)
6850 switch (errtype)
6852 case ic_argpass:
6853 if (pedwarn (expr_loc, OPT_Wint_conversion,
6854 "passing argument %d of %qE makes pointer from "
6855 "integer without a cast", parmnum, rname))
6856 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6857 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6858 "expected %qT but argument is of type %qT",
6859 type, rhstype);
6860 break;
6861 case ic_assign:
6862 pedwarn (location, OPT_Wint_conversion,
6863 "assignment to %qT from %qT makes pointer from integer "
6864 "without a cast", type, rhstype);
6865 break;
6866 case ic_init:
6867 pedwarn_init (location, OPT_Wint_conversion,
6868 "initialization of %qT from %qT makes pointer from "
6869 "integer without a cast", type, rhstype);
6870 break;
6871 case ic_return:
6872 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6873 "function with return type %qT makes pointer from "
6874 "integer without a cast", rhstype, type);
6875 break;
6876 default:
6877 gcc_unreachable ();
6880 return convert (type, rhs);
6882 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6884 switch (errtype)
6886 case ic_argpass:
6887 if (pedwarn (expr_loc, OPT_Wint_conversion,
6888 "passing argument %d of %qE makes integer from "
6889 "pointer without a cast", parmnum, rname))
6890 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6891 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6892 "expected %qT but argument is of type %qT",
6893 type, rhstype);
6894 break;
6895 case ic_assign:
6896 pedwarn (location, OPT_Wint_conversion,
6897 "assignment to %qT from %qT makes integer from pointer "
6898 "without a cast", type, rhstype);
6899 break;
6900 case ic_init:
6901 pedwarn_init (location, OPT_Wint_conversion,
6902 "initialization of %qT from %qT makes integer from "
6903 "pointer without a cast", type, rhstype);
6904 break;
6905 case ic_return:
6906 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6907 "function with return type %qT makes integer from "
6908 "pointer without a cast", rhstype, type);
6909 break;
6910 default:
6911 gcc_unreachable ();
6914 return convert (type, rhs);
6916 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6918 tree ret;
6919 bool save = in_late_binary_op;
6920 in_late_binary_op = true;
6921 ret = convert (type, rhs);
6922 in_late_binary_op = save;
6923 return ret;
6926 switch (errtype)
6928 case ic_argpass:
6929 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6930 rname);
6931 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6932 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6933 "expected %qT but argument is of type %qT", type, rhstype);
6934 break;
6935 case ic_assign:
6936 error_at (location, "incompatible types when assigning to type %qT from "
6937 "type %qT", type, rhstype);
6938 break;
6939 case ic_init:
6940 error_at (location,
6941 "incompatible types when initializing type %qT using type %qT",
6942 type, rhstype);
6943 break;
6944 case ic_return:
6945 error_at (location,
6946 "incompatible types when returning type %qT but %qT was "
6947 "expected", rhstype, type);
6948 break;
6949 default:
6950 gcc_unreachable ();
6953 return error_mark_node;
6956 /* If VALUE is a compound expr all of whose expressions are constant, then
6957 return its value. Otherwise, return error_mark_node.
6959 This is for handling COMPOUND_EXPRs as initializer elements
6960 which is allowed with a warning when -pedantic is specified. */
6962 static tree
6963 valid_compound_expr_initializer (tree value, tree endtype)
6965 if (TREE_CODE (value) == COMPOUND_EXPR)
6967 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6968 == error_mark_node)
6969 return error_mark_node;
6970 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6971 endtype);
6973 else if (!initializer_constant_valid_p (value, endtype))
6974 return error_mark_node;
6975 else
6976 return value;
6979 /* Perform appropriate conversions on the initial value of a variable,
6980 store it in the declaration DECL,
6981 and print any error messages that are appropriate.
6982 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6983 If the init is invalid, store an ERROR_MARK.
6985 INIT_LOC is the location of the initial value. */
6987 void
6988 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6990 tree value, type;
6991 bool npc = false;
6993 /* If variable's type was invalidly declared, just ignore it. */
6995 type = TREE_TYPE (decl);
6996 if (TREE_CODE (type) == ERROR_MARK)
6997 return;
6999 /* Digest the specified initializer into an expression. */
7001 if (init)
7002 npc = null_pointer_constant_p (init);
7003 value = digest_init (init_loc, type, init, origtype, npc,
7004 true, TREE_STATIC (decl));
7006 /* Store the expression if valid; else report error. */
7008 if (!in_system_header_at (input_location)
7009 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7010 warning (OPT_Wtraditional, "traditional C rejects automatic "
7011 "aggregate initialization");
7013 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7014 DECL_INITIAL (decl) = value;
7016 /* ANSI wants warnings about out-of-range constant initializers. */
7017 STRIP_TYPE_NOPS (value);
7018 if (TREE_STATIC (decl))
7019 constant_expression_warning (value);
7021 /* Check if we need to set array size from compound literal size. */
7022 if (TREE_CODE (type) == ARRAY_TYPE
7023 && TYPE_DOMAIN (type) == NULL_TREE
7024 && value != error_mark_node)
7026 tree inside_init = init;
7028 STRIP_TYPE_NOPS (inside_init);
7029 inside_init = fold (inside_init);
7031 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7033 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7035 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7037 /* For int foo[] = (int [3]){1}; we need to set array size
7038 now since later on array initializer will be just the
7039 brace enclosed list of the compound literal. */
7040 tree etype = strip_array_types (TREE_TYPE (decl));
7041 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7042 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7043 layout_type (type);
7044 layout_decl (cldecl, 0);
7045 TREE_TYPE (decl)
7046 = c_build_qualified_type (type, TYPE_QUALS (etype));
7052 /* Methods for storing and printing names for error messages. */
7054 /* Implement a spelling stack that allows components of a name to be pushed
7055 and popped. Each element on the stack is this structure. */
7057 struct spelling
7059 int kind;
7060 union
7062 unsigned HOST_WIDE_INT i;
7063 const char *s;
7064 } u;
7067 #define SPELLING_STRING 1
7068 #define SPELLING_MEMBER 2
7069 #define SPELLING_BOUNDS 3
7071 static struct spelling *spelling; /* Next stack element (unused). */
7072 static struct spelling *spelling_base; /* Spelling stack base. */
7073 static int spelling_size; /* Size of the spelling stack. */
7075 /* Macros to save and restore the spelling stack around push_... functions.
7076 Alternative to SAVE_SPELLING_STACK. */
7078 #define SPELLING_DEPTH() (spelling - spelling_base)
7079 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7081 /* Push an element on the spelling stack with type KIND and assign VALUE
7082 to MEMBER. */
7084 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7086 int depth = SPELLING_DEPTH (); \
7088 if (depth >= spelling_size) \
7090 spelling_size += 10; \
7091 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7092 spelling_size); \
7093 RESTORE_SPELLING_DEPTH (depth); \
7096 spelling->kind = (KIND); \
7097 spelling->MEMBER = (VALUE); \
7098 spelling++; \
7101 /* Push STRING on the stack. Printed literally. */
7103 static void
7104 push_string (const char *string)
7106 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7109 /* Push a member name on the stack. Printed as '.' STRING. */
7111 static void
7112 push_member_name (tree decl)
7114 const char *const string
7115 = (DECL_NAME (decl)
7116 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7117 : _("<anonymous>"));
7118 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7121 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7123 static void
7124 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7126 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7129 /* Compute the maximum size in bytes of the printed spelling. */
7131 static int
7132 spelling_length (void)
7134 int size = 0;
7135 struct spelling *p;
7137 for (p = spelling_base; p < spelling; p++)
7139 if (p->kind == SPELLING_BOUNDS)
7140 size += 25;
7141 else
7142 size += strlen (p->u.s) + 1;
7145 return size;
7148 /* Print the spelling to BUFFER and return it. */
7150 static char *
7151 print_spelling (char *buffer)
7153 char *d = buffer;
7154 struct spelling *p;
7156 for (p = spelling_base; p < spelling; p++)
7157 if (p->kind == SPELLING_BOUNDS)
7159 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7160 d += strlen (d);
7162 else
7164 const char *s;
7165 if (p->kind == SPELLING_MEMBER)
7166 *d++ = '.';
7167 for (s = p->u.s; (*d = *s++); d++)
7170 *d++ = '\0';
7171 return buffer;
7174 /* Digest the parser output INIT as an initializer for type TYPE.
7175 Return a C expression of type TYPE to represent the initial value.
7177 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7179 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7181 If INIT is a string constant, STRICT_STRING is true if it is
7182 unparenthesized or we should not warn here for it being parenthesized.
7183 For other types of INIT, STRICT_STRING is not used.
7185 INIT_LOC is the location of the INIT.
7187 REQUIRE_CONSTANT requests an error if non-constant initializers or
7188 elements are seen. */
7190 static tree
7191 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7192 bool null_pointer_constant, bool strict_string,
7193 int require_constant)
7195 enum tree_code code = TREE_CODE (type);
7196 tree inside_init = init;
7197 tree semantic_type = NULL_TREE;
7198 bool maybe_const = true;
7200 if (type == error_mark_node
7201 || !init
7202 || error_operand_p (init))
7203 return error_mark_node;
7205 STRIP_TYPE_NOPS (inside_init);
7207 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7209 semantic_type = TREE_TYPE (inside_init);
7210 inside_init = TREE_OPERAND (inside_init, 0);
7212 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7213 inside_init = decl_constant_value_for_optimization (inside_init);
7215 /* Initialization of an array of chars from a string constant
7216 optionally enclosed in braces. */
7218 if (code == ARRAY_TYPE && inside_init
7219 && TREE_CODE (inside_init) == STRING_CST)
7221 tree typ1
7222 = (TYPE_ATOMIC (TREE_TYPE (type))
7223 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7224 TYPE_QUAL_ATOMIC)
7225 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7226 /* Note that an array could be both an array of character type
7227 and an array of wchar_t if wchar_t is signed char or unsigned
7228 char. */
7229 bool char_array = (typ1 == char_type_node
7230 || typ1 == signed_char_type_node
7231 || typ1 == unsigned_char_type_node);
7232 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7233 bool char16_array = !!comptypes (typ1, char16_type_node);
7234 bool char32_array = !!comptypes (typ1, char32_type_node);
7236 if (char_array || wchar_array || char16_array || char32_array)
7238 struct c_expr expr;
7239 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7240 expr.value = inside_init;
7241 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7242 expr.original_type = NULL;
7243 maybe_warn_string_init (init_loc, type, expr);
7245 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7246 pedwarn_init (init_loc, OPT_Wpedantic,
7247 "initialization of a flexible array member");
7249 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7250 TYPE_MAIN_VARIANT (type)))
7251 return inside_init;
7253 if (char_array)
7255 if (typ2 != char_type_node)
7257 error_init (init_loc, "char-array initialized from wide "
7258 "string");
7259 return error_mark_node;
7262 else
7264 if (typ2 == char_type_node)
7266 error_init (init_loc, "wide character array initialized "
7267 "from non-wide string");
7268 return error_mark_node;
7270 else if (!comptypes(typ1, typ2))
7272 error_init (init_loc, "wide character array initialized "
7273 "from incompatible wide string");
7274 return error_mark_node;
7278 TREE_TYPE (inside_init) = type;
7279 if (TYPE_DOMAIN (type) != NULL_TREE
7280 && TYPE_SIZE (type) != NULL_TREE
7281 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7283 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7285 /* Subtract the size of a single (possibly wide) character
7286 because it's ok to ignore the terminating null char
7287 that is counted in the length of the constant. */
7288 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7289 (len
7290 - (TYPE_PRECISION (typ1)
7291 / BITS_PER_UNIT))))
7292 pedwarn_init (init_loc, 0,
7293 ("initializer-string for array of chars "
7294 "is too long"));
7295 else if (warn_cxx_compat
7296 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7297 warning_at (init_loc, OPT_Wc___compat,
7298 ("initializer-string for array chars "
7299 "is too long for C++"));
7302 return inside_init;
7304 else if (INTEGRAL_TYPE_P (typ1))
7306 error_init (init_loc, "array of inappropriate type initialized "
7307 "from string constant");
7308 return error_mark_node;
7312 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7313 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7314 below and handle as a constructor. */
7315 if (code == VECTOR_TYPE
7316 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7317 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7318 && TREE_CONSTANT (inside_init))
7320 if (TREE_CODE (inside_init) == VECTOR_CST
7321 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7322 TYPE_MAIN_VARIANT (type)))
7323 return inside_init;
7325 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7327 unsigned HOST_WIDE_INT ix;
7328 tree value;
7329 bool constant_p = true;
7331 /* Iterate through elements and check if all constructor
7332 elements are *_CSTs. */
7333 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7334 if (!CONSTANT_CLASS_P (value))
7336 constant_p = false;
7337 break;
7340 if (constant_p)
7341 return build_vector_from_ctor (type,
7342 CONSTRUCTOR_ELTS (inside_init));
7346 if (warn_sequence_point)
7347 verify_sequence_points (inside_init);
7349 /* Any type can be initialized
7350 from an expression of the same type, optionally with braces. */
7352 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7353 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7354 TYPE_MAIN_VARIANT (type))
7355 || (code == ARRAY_TYPE
7356 && comptypes (TREE_TYPE (inside_init), type))
7357 || (code == VECTOR_TYPE
7358 && comptypes (TREE_TYPE (inside_init), type))
7359 || (code == POINTER_TYPE
7360 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7361 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7362 TREE_TYPE (type)))))
7364 if (code == POINTER_TYPE)
7366 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7368 if (TREE_CODE (inside_init) == STRING_CST
7369 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7370 inside_init = array_to_pointer_conversion
7371 (init_loc, inside_init);
7372 else
7374 error_init (init_loc, "invalid use of non-lvalue array");
7375 return error_mark_node;
7380 if (code == VECTOR_TYPE)
7381 /* Although the types are compatible, we may require a
7382 conversion. */
7383 inside_init = convert (type, inside_init);
7385 if (require_constant
7386 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7388 /* As an extension, allow initializing objects with static storage
7389 duration with compound literals (which are then treated just as
7390 the brace enclosed list they contain). Also allow this for
7391 vectors, as we can only assign them with compound literals. */
7392 if (flag_isoc99 && code != VECTOR_TYPE)
7393 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7394 "is not constant");
7395 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7396 inside_init = DECL_INITIAL (decl);
7399 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7400 && TREE_CODE (inside_init) != CONSTRUCTOR)
7402 error_init (init_loc, "array initialized from non-constant array "
7403 "expression");
7404 return error_mark_node;
7407 /* Compound expressions can only occur here if -Wpedantic or
7408 -pedantic-errors is specified. In the later case, we always want
7409 an error. In the former case, we simply want a warning. */
7410 if (require_constant && pedantic
7411 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7413 inside_init
7414 = valid_compound_expr_initializer (inside_init,
7415 TREE_TYPE (inside_init));
7416 if (inside_init == error_mark_node)
7417 error_init (init_loc, "initializer element is not constant");
7418 else
7419 pedwarn_init (init_loc, OPT_Wpedantic,
7420 "initializer element is not constant");
7421 if (flag_pedantic_errors)
7422 inside_init = error_mark_node;
7424 else if (require_constant
7425 && !initializer_constant_valid_p (inside_init,
7426 TREE_TYPE (inside_init)))
7428 error_init (init_loc, "initializer element is not constant");
7429 inside_init = error_mark_node;
7431 else if (require_constant && !maybe_const)
7432 pedwarn_init (init_loc, OPT_Wpedantic,
7433 "initializer element is not a constant expression");
7435 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7436 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7437 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7438 type, inside_init, origtype,
7439 ic_init, null_pointer_constant,
7440 NULL_TREE, NULL_TREE, 0);
7441 return inside_init;
7444 /* Handle scalar types, including conversions. */
7446 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7447 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7448 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7450 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7451 && (TREE_CODE (init) == STRING_CST
7452 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7453 inside_init = init = array_to_pointer_conversion (init_loc, init);
7454 if (semantic_type)
7455 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7456 inside_init);
7457 inside_init
7458 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7459 inside_init, origtype, ic_init,
7460 null_pointer_constant, NULL_TREE, NULL_TREE,
7463 /* Check to see if we have already given an error message. */
7464 if (inside_init == error_mark_node)
7466 else if (require_constant && !TREE_CONSTANT (inside_init))
7468 error_init (init_loc, "initializer element is not constant");
7469 inside_init = error_mark_node;
7471 else if (require_constant
7472 && !initializer_constant_valid_p (inside_init,
7473 TREE_TYPE (inside_init)))
7475 error_init (init_loc, "initializer element is not computable at "
7476 "load time");
7477 inside_init = error_mark_node;
7479 else if (require_constant && !maybe_const)
7480 pedwarn_init (init_loc, OPT_Wpedantic,
7481 "initializer element is not a constant expression");
7483 return inside_init;
7486 /* Come here only for records and arrays. */
7488 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7490 error_init (init_loc, "variable-sized object may not be initialized");
7491 return error_mark_node;
7494 error_init (init_loc, "invalid initializer");
7495 return error_mark_node;
7498 /* Handle initializers that use braces. */
7500 /* Type of object we are accumulating a constructor for.
7501 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7502 static tree constructor_type;
7504 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7505 left to fill. */
7506 static tree constructor_fields;
7508 /* For an ARRAY_TYPE, this is the specified index
7509 at which to store the next element we get. */
7510 static tree constructor_index;
7512 /* For an ARRAY_TYPE, this is the maximum index. */
7513 static tree constructor_max_index;
7515 /* For a RECORD_TYPE, this is the first field not yet written out. */
7516 static tree constructor_unfilled_fields;
7518 /* For an ARRAY_TYPE, this is the index of the first element
7519 not yet written out. */
7520 static tree constructor_unfilled_index;
7522 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7523 This is so we can generate gaps between fields, when appropriate. */
7524 static tree constructor_bit_index;
7526 /* If we are saving up the elements rather than allocating them,
7527 this is the list of elements so far (in reverse order,
7528 most recent first). */
7529 static vec<constructor_elt, va_gc> *constructor_elements;
7531 /* 1 if constructor should be incrementally stored into a constructor chain,
7532 0 if all the elements should be kept in AVL tree. */
7533 static int constructor_incremental;
7535 /* 1 if so far this constructor's elements are all compile-time constants. */
7536 static int constructor_constant;
7538 /* 1 if so far this constructor's elements are all valid address constants. */
7539 static int constructor_simple;
7541 /* 1 if this constructor has an element that cannot be part of a
7542 constant expression. */
7543 static int constructor_nonconst;
7545 /* 1 if this constructor is erroneous so far. */
7546 static int constructor_erroneous;
7548 /* 1 if this constructor is the universal zero initializer { 0 }. */
7549 static int constructor_zeroinit;
7551 /* Structure for managing pending initializer elements, organized as an
7552 AVL tree. */
7554 struct init_node
7556 struct init_node *left, *right;
7557 struct init_node *parent;
7558 int balance;
7559 tree purpose;
7560 tree value;
7561 tree origtype;
7564 /* Tree of pending elements at this constructor level.
7565 These are elements encountered out of order
7566 which belong at places we haven't reached yet in actually
7567 writing the output.
7568 Will never hold tree nodes across GC runs. */
7569 static struct init_node *constructor_pending_elts;
7571 /* The SPELLING_DEPTH of this constructor. */
7572 static int constructor_depth;
7574 /* DECL node for which an initializer is being read.
7575 0 means we are reading a constructor expression
7576 such as (struct foo) {...}. */
7577 static tree constructor_decl;
7579 /* Nonzero if this is an initializer for a top-level decl. */
7580 static int constructor_top_level;
7582 /* Nonzero if there were any member designators in this initializer. */
7583 static int constructor_designated;
7585 /* Nesting depth of designator list. */
7586 static int designator_depth;
7588 /* Nonzero if there were diagnosed errors in this designator list. */
7589 static int designator_erroneous;
7592 /* This stack has a level for each implicit or explicit level of
7593 structuring in the initializer, including the outermost one. It
7594 saves the values of most of the variables above. */
7596 struct constructor_range_stack;
7598 struct constructor_stack
7600 struct constructor_stack *next;
7601 tree type;
7602 tree fields;
7603 tree index;
7604 tree max_index;
7605 tree unfilled_index;
7606 tree unfilled_fields;
7607 tree bit_index;
7608 vec<constructor_elt, va_gc> *elements;
7609 struct init_node *pending_elts;
7610 int offset;
7611 int depth;
7612 /* If value nonzero, this value should replace the entire
7613 constructor at this level. */
7614 struct c_expr replacement_value;
7615 struct constructor_range_stack *range_stack;
7616 char constant;
7617 char simple;
7618 char nonconst;
7619 char implicit;
7620 char erroneous;
7621 char outer;
7622 char incremental;
7623 char designated;
7624 int designator_depth;
7627 static struct constructor_stack *constructor_stack;
7629 /* This stack represents designators from some range designator up to
7630 the last designator in the list. */
7632 struct constructor_range_stack
7634 struct constructor_range_stack *next, *prev;
7635 struct constructor_stack *stack;
7636 tree range_start;
7637 tree index;
7638 tree range_end;
7639 tree fields;
7642 static struct constructor_range_stack *constructor_range_stack;
7644 /* This stack records separate initializers that are nested.
7645 Nested initializers can't happen in ANSI C, but GNU C allows them
7646 in cases like { ... (struct foo) { ... } ... }. */
7648 struct initializer_stack
7650 struct initializer_stack *next;
7651 tree decl;
7652 struct constructor_stack *constructor_stack;
7653 struct constructor_range_stack *constructor_range_stack;
7654 vec<constructor_elt, va_gc> *elements;
7655 struct spelling *spelling;
7656 struct spelling *spelling_base;
7657 int spelling_size;
7658 char top_level;
7659 char require_constant_value;
7660 char require_constant_elements;
7661 rich_location *missing_brace_richloc;
7664 static struct initializer_stack *initializer_stack;
7666 /* Prepare to parse and output the initializer for variable DECL. */
7668 void
7669 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7670 rich_location *richloc)
7672 const char *locus;
7673 struct initializer_stack *p = XNEW (struct initializer_stack);
7675 p->decl = constructor_decl;
7676 p->require_constant_value = require_constant_value;
7677 p->require_constant_elements = require_constant_elements;
7678 p->constructor_stack = constructor_stack;
7679 p->constructor_range_stack = constructor_range_stack;
7680 p->elements = constructor_elements;
7681 p->spelling = spelling;
7682 p->spelling_base = spelling_base;
7683 p->spelling_size = spelling_size;
7684 p->top_level = constructor_top_level;
7685 p->next = initializer_stack;
7686 p->missing_brace_richloc = richloc;
7687 initializer_stack = p;
7689 constructor_decl = decl;
7690 constructor_designated = 0;
7691 constructor_top_level = top_level;
7693 if (decl != NULL_TREE && decl != error_mark_node)
7695 require_constant_value = TREE_STATIC (decl);
7696 require_constant_elements
7697 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7698 /* For a scalar, you can always use any value to initialize,
7699 even within braces. */
7700 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7701 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7703 else
7705 require_constant_value = 0;
7706 require_constant_elements = 0;
7707 locus = _("(anonymous)");
7710 constructor_stack = 0;
7711 constructor_range_stack = 0;
7713 found_missing_braces = 0;
7715 spelling_base = 0;
7716 spelling_size = 0;
7717 RESTORE_SPELLING_DEPTH (0);
7719 if (locus)
7720 push_string (locus);
7723 void
7724 finish_init (void)
7726 struct initializer_stack *p = initializer_stack;
7728 /* Free the whole constructor stack of this initializer. */
7729 while (constructor_stack)
7731 struct constructor_stack *q = constructor_stack;
7732 constructor_stack = q->next;
7733 free (q);
7736 gcc_assert (!constructor_range_stack);
7738 /* Pop back to the data of the outer initializer (if any). */
7739 free (spelling_base);
7741 constructor_decl = p->decl;
7742 require_constant_value = p->require_constant_value;
7743 require_constant_elements = p->require_constant_elements;
7744 constructor_stack = p->constructor_stack;
7745 constructor_range_stack = p->constructor_range_stack;
7746 constructor_elements = p->elements;
7747 spelling = p->spelling;
7748 spelling_base = p->spelling_base;
7749 spelling_size = p->spelling_size;
7750 constructor_top_level = p->top_level;
7751 initializer_stack = p->next;
7752 free (p);
7755 /* Call here when we see the initializer is surrounded by braces.
7756 This is instead of a call to push_init_level;
7757 it is matched by a call to pop_init_level.
7759 TYPE is the type to initialize, for a constructor expression.
7760 For an initializer for a decl, TYPE is zero. */
7762 void
7763 really_start_incremental_init (tree type)
7765 struct constructor_stack *p = XNEW (struct constructor_stack);
7767 if (type == NULL_TREE)
7768 type = TREE_TYPE (constructor_decl);
7770 if (VECTOR_TYPE_P (type)
7771 && TYPE_VECTOR_OPAQUE (type))
7772 error ("opaque vector types cannot be initialized");
7774 p->type = constructor_type;
7775 p->fields = constructor_fields;
7776 p->index = constructor_index;
7777 p->max_index = constructor_max_index;
7778 p->unfilled_index = constructor_unfilled_index;
7779 p->unfilled_fields = constructor_unfilled_fields;
7780 p->bit_index = constructor_bit_index;
7781 p->elements = constructor_elements;
7782 p->constant = constructor_constant;
7783 p->simple = constructor_simple;
7784 p->nonconst = constructor_nonconst;
7785 p->erroneous = constructor_erroneous;
7786 p->pending_elts = constructor_pending_elts;
7787 p->depth = constructor_depth;
7788 p->replacement_value.value = 0;
7789 p->replacement_value.original_code = ERROR_MARK;
7790 p->replacement_value.original_type = NULL;
7791 p->implicit = 0;
7792 p->range_stack = 0;
7793 p->outer = 0;
7794 p->incremental = constructor_incremental;
7795 p->designated = constructor_designated;
7796 p->designator_depth = designator_depth;
7797 p->next = 0;
7798 constructor_stack = p;
7800 constructor_constant = 1;
7801 constructor_simple = 1;
7802 constructor_nonconst = 0;
7803 constructor_depth = SPELLING_DEPTH ();
7804 constructor_elements = NULL;
7805 constructor_pending_elts = 0;
7806 constructor_type = type;
7807 constructor_incremental = 1;
7808 constructor_designated = 0;
7809 constructor_zeroinit = 1;
7810 designator_depth = 0;
7811 designator_erroneous = 0;
7813 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7815 constructor_fields = TYPE_FIELDS (constructor_type);
7816 /* Skip any nameless bit fields at the beginning. */
7817 while (constructor_fields != NULL_TREE
7818 && DECL_C_BIT_FIELD (constructor_fields)
7819 && DECL_NAME (constructor_fields) == NULL_TREE)
7820 constructor_fields = DECL_CHAIN (constructor_fields);
7822 constructor_unfilled_fields = constructor_fields;
7823 constructor_bit_index = bitsize_zero_node;
7825 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7827 if (TYPE_DOMAIN (constructor_type))
7829 constructor_max_index
7830 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7832 /* Detect non-empty initializations of zero-length arrays. */
7833 if (constructor_max_index == NULL_TREE
7834 && TYPE_SIZE (constructor_type))
7835 constructor_max_index = integer_minus_one_node;
7837 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7838 to initialize VLAs will cause a proper error; avoid tree
7839 checking errors as well by setting a safe value. */
7840 if (constructor_max_index
7841 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7842 constructor_max_index = integer_minus_one_node;
7844 constructor_index
7845 = convert (bitsizetype,
7846 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7848 else
7850 constructor_index = bitsize_zero_node;
7851 constructor_max_index = NULL_TREE;
7854 constructor_unfilled_index = constructor_index;
7856 else if (VECTOR_TYPE_P (constructor_type))
7858 /* Vectors are like simple fixed-size arrays. */
7859 constructor_max_index =
7860 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7861 constructor_index = bitsize_zero_node;
7862 constructor_unfilled_index = constructor_index;
7864 else
7866 /* Handle the case of int x = {5}; */
7867 constructor_fields = constructor_type;
7868 constructor_unfilled_fields = constructor_type;
7872 extern location_t last_init_list_comma;
7874 /* Called when we see an open brace for a nested initializer. Finish
7875 off any pending levels with implicit braces. */
7876 void
7877 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7879 while (constructor_stack->implicit)
7881 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7882 && constructor_fields == NULL_TREE)
7883 process_init_element (input_location,
7884 pop_init_level (loc, 1, braced_init_obstack,
7885 last_init_list_comma),
7886 true, braced_init_obstack);
7887 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7888 && constructor_max_index
7889 && tree_int_cst_lt (constructor_max_index,
7890 constructor_index))
7891 process_init_element (input_location,
7892 pop_init_level (loc, 1, braced_init_obstack,
7893 last_init_list_comma),
7894 true, braced_init_obstack);
7895 else
7896 break;
7900 /* Push down into a subobject, for initialization.
7901 If this is for an explicit set of braces, IMPLICIT is 0.
7902 If it is because the next element belongs at a lower level,
7903 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7905 void
7906 push_init_level (location_t loc, int implicit,
7907 struct obstack *braced_init_obstack)
7909 struct constructor_stack *p;
7910 tree value = NULL_TREE;
7912 /* Unless this is an explicit brace, we need to preserve previous
7913 content if any. */
7914 if (implicit)
7916 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7917 value = find_init_member (constructor_fields, braced_init_obstack);
7918 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7919 value = find_init_member (constructor_index, braced_init_obstack);
7922 p = XNEW (struct constructor_stack);
7923 p->type = constructor_type;
7924 p->fields = constructor_fields;
7925 p->index = constructor_index;
7926 p->max_index = constructor_max_index;
7927 p->unfilled_index = constructor_unfilled_index;
7928 p->unfilled_fields = constructor_unfilled_fields;
7929 p->bit_index = constructor_bit_index;
7930 p->elements = constructor_elements;
7931 p->constant = constructor_constant;
7932 p->simple = constructor_simple;
7933 p->nonconst = constructor_nonconst;
7934 p->erroneous = constructor_erroneous;
7935 p->pending_elts = constructor_pending_elts;
7936 p->depth = constructor_depth;
7937 p->replacement_value.value = NULL_TREE;
7938 p->replacement_value.original_code = ERROR_MARK;
7939 p->replacement_value.original_type = NULL;
7940 p->implicit = implicit;
7941 p->outer = 0;
7942 p->incremental = constructor_incremental;
7943 p->designated = constructor_designated;
7944 p->designator_depth = designator_depth;
7945 p->next = constructor_stack;
7946 p->range_stack = 0;
7947 constructor_stack = p;
7949 constructor_constant = 1;
7950 constructor_simple = 1;
7951 constructor_nonconst = 0;
7952 constructor_depth = SPELLING_DEPTH ();
7953 constructor_elements = NULL;
7954 constructor_incremental = 1;
7955 constructor_designated = 0;
7956 constructor_pending_elts = 0;
7957 if (!implicit)
7959 p->range_stack = constructor_range_stack;
7960 constructor_range_stack = 0;
7961 designator_depth = 0;
7962 designator_erroneous = 0;
7965 /* Don't die if an entire brace-pair level is superfluous
7966 in the containing level. */
7967 if (constructor_type == NULL_TREE)
7969 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7971 /* Don't die if there are extra init elts at the end. */
7972 if (constructor_fields == NULL_TREE)
7973 constructor_type = NULL_TREE;
7974 else
7976 constructor_type = TREE_TYPE (constructor_fields);
7977 push_member_name (constructor_fields);
7978 constructor_depth++;
7980 /* If upper initializer is designated, then mark this as
7981 designated too to prevent bogus warnings. */
7982 constructor_designated = p->designated;
7984 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7986 constructor_type = TREE_TYPE (constructor_type);
7987 push_array_bounds (tree_to_uhwi (constructor_index));
7988 constructor_depth++;
7991 if (constructor_type == NULL_TREE)
7993 error_init (loc, "extra brace group at end of initializer");
7994 constructor_fields = NULL_TREE;
7995 constructor_unfilled_fields = NULL_TREE;
7996 return;
7999 if (value && TREE_CODE (value) == CONSTRUCTOR)
8001 constructor_constant = TREE_CONSTANT (value);
8002 constructor_simple = TREE_STATIC (value);
8003 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8004 constructor_elements = CONSTRUCTOR_ELTS (value);
8005 if (!vec_safe_is_empty (constructor_elements)
8006 && (TREE_CODE (constructor_type) == RECORD_TYPE
8007 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8008 set_nonincremental_init (braced_init_obstack);
8011 if (implicit == 1)
8013 found_missing_braces = 1;
8014 if (initializer_stack->missing_brace_richloc)
8015 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8016 (loc, "{");
8019 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8021 constructor_fields = TYPE_FIELDS (constructor_type);
8022 /* Skip any nameless bit fields at the beginning. */
8023 while (constructor_fields != NULL_TREE
8024 && DECL_C_BIT_FIELD (constructor_fields)
8025 && DECL_NAME (constructor_fields) == NULL_TREE)
8026 constructor_fields = DECL_CHAIN (constructor_fields);
8028 constructor_unfilled_fields = constructor_fields;
8029 constructor_bit_index = bitsize_zero_node;
8031 else if (VECTOR_TYPE_P (constructor_type))
8033 /* Vectors are like simple fixed-size arrays. */
8034 constructor_max_index =
8035 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8036 constructor_index = bitsize_int (0);
8037 constructor_unfilled_index = constructor_index;
8039 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8041 if (TYPE_DOMAIN (constructor_type))
8043 constructor_max_index
8044 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8046 /* Detect non-empty initializations of zero-length arrays. */
8047 if (constructor_max_index == NULL_TREE
8048 && TYPE_SIZE (constructor_type))
8049 constructor_max_index = integer_minus_one_node;
8051 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8052 to initialize VLAs will cause a proper error; avoid tree
8053 checking errors as well by setting a safe value. */
8054 if (constructor_max_index
8055 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8056 constructor_max_index = integer_minus_one_node;
8058 constructor_index
8059 = convert (bitsizetype,
8060 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8062 else
8063 constructor_index = bitsize_zero_node;
8065 constructor_unfilled_index = constructor_index;
8066 if (value && TREE_CODE (value) == STRING_CST)
8068 /* We need to split the char/wchar array into individual
8069 characters, so that we don't have to special case it
8070 everywhere. */
8071 set_nonincremental_init_from_string (value, braced_init_obstack);
8074 else
8076 if (constructor_type != error_mark_node)
8077 warning_init (input_location, 0, "braces around scalar initializer");
8078 constructor_fields = constructor_type;
8079 constructor_unfilled_fields = constructor_type;
8083 /* At the end of an implicit or explicit brace level,
8084 finish up that level of constructor. If a single expression
8085 with redundant braces initialized that level, return the
8086 c_expr structure for that expression. Otherwise, the original_code
8087 element is set to ERROR_MARK.
8088 If we were outputting the elements as they are read, return 0 as the value
8089 from inner levels (process_init_element ignores that),
8090 but return error_mark_node as the value from the outermost level
8091 (that's what we want to put in DECL_INITIAL).
8092 Otherwise, return a CONSTRUCTOR expression as the value. */
8094 struct c_expr
8095 pop_init_level (location_t loc, int implicit,
8096 struct obstack *braced_init_obstack,
8097 location_t insert_before)
8099 struct constructor_stack *p;
8100 struct c_expr ret;
8101 ret.value = NULL_TREE;
8102 ret.original_code = ERROR_MARK;
8103 ret.original_type = NULL;
8105 if (implicit == 0)
8107 /* When we come to an explicit close brace,
8108 pop any inner levels that didn't have explicit braces. */
8109 while (constructor_stack->implicit)
8110 process_init_element (input_location,
8111 pop_init_level (loc, 1, braced_init_obstack,
8112 insert_before),
8113 true, braced_init_obstack);
8114 gcc_assert (!constructor_range_stack);
8116 else
8117 if (initializer_stack->missing_brace_richloc)
8118 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8119 (insert_before, "}");
8121 /* Now output all pending elements. */
8122 constructor_incremental = 1;
8123 output_pending_init_elements (1, braced_init_obstack);
8125 p = constructor_stack;
8127 /* Error for initializing a flexible array member, or a zero-length
8128 array member in an inappropriate context. */
8129 if (constructor_type && constructor_fields
8130 && TREE_CODE (constructor_type) == ARRAY_TYPE
8131 && TYPE_DOMAIN (constructor_type)
8132 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8134 /* Silently discard empty initializations. The parser will
8135 already have pedwarned for empty brackets. */
8136 if (integer_zerop (constructor_unfilled_index))
8137 constructor_type = NULL_TREE;
8138 else
8140 gcc_assert (!TYPE_SIZE (constructor_type));
8142 if (constructor_depth > 2)
8143 error_init (loc, "initialization of flexible array member in a nested context");
8144 else
8145 pedwarn_init (loc, OPT_Wpedantic,
8146 "initialization of a flexible array member");
8148 /* We have already issued an error message for the existence
8149 of a flexible array member not at the end of the structure.
8150 Discard the initializer so that we do not die later. */
8151 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8152 constructor_type = NULL_TREE;
8156 switch (vec_safe_length (constructor_elements))
8158 case 0:
8159 /* Initialization with { } counts as zeroinit. */
8160 constructor_zeroinit = 1;
8161 break;
8162 case 1:
8163 /* This might be zeroinit as well. */
8164 if (integer_zerop ((*constructor_elements)[0].value))
8165 constructor_zeroinit = 1;
8166 break;
8167 default:
8168 /* If the constructor has more than one element, it can't be { 0 }. */
8169 constructor_zeroinit = 0;
8170 break;
8173 /* Warn when some structs are initialized with direct aggregation. */
8174 if (!implicit && found_missing_braces && warn_missing_braces
8175 && !constructor_zeroinit)
8177 gcc_assert (initializer_stack->missing_brace_richloc);
8178 warning_at_rich_loc (initializer_stack->missing_brace_richloc,
8179 OPT_Wmissing_braces,
8180 "missing braces around initializer");
8183 /* Warn when some struct elements are implicitly initialized to zero. */
8184 if (warn_missing_field_initializers
8185 && constructor_type
8186 && TREE_CODE (constructor_type) == RECORD_TYPE
8187 && constructor_unfilled_fields)
8189 /* Do not warn for flexible array members or zero-length arrays. */
8190 while (constructor_unfilled_fields
8191 && (!DECL_SIZE (constructor_unfilled_fields)
8192 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8193 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8195 if (constructor_unfilled_fields
8196 /* Do not warn if this level of the initializer uses member
8197 designators; it is likely to be deliberate. */
8198 && !constructor_designated
8199 /* Do not warn about initializing with { 0 } or with { }. */
8200 && !constructor_zeroinit)
8202 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8203 "missing initializer for field %qD of %qT",
8204 constructor_unfilled_fields,
8205 constructor_type))
8206 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8207 "%qD declared here", constructor_unfilled_fields);
8211 /* Pad out the end of the structure. */
8212 if (p->replacement_value.value)
8213 /* If this closes a superfluous brace pair,
8214 just pass out the element between them. */
8215 ret = p->replacement_value;
8216 else if (constructor_type == NULL_TREE)
8218 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8219 && TREE_CODE (constructor_type) != ARRAY_TYPE
8220 && !VECTOR_TYPE_P (constructor_type))
8222 /* A nonincremental scalar initializer--just return
8223 the element, after verifying there is just one. */
8224 if (vec_safe_is_empty (constructor_elements))
8226 if (!constructor_erroneous)
8227 error_init (loc, "empty scalar initializer");
8228 ret.value = error_mark_node;
8230 else if (vec_safe_length (constructor_elements) != 1)
8232 error_init (loc, "extra elements in scalar initializer");
8233 ret.value = (*constructor_elements)[0].value;
8235 else
8236 ret.value = (*constructor_elements)[0].value;
8238 else
8240 if (constructor_erroneous)
8241 ret.value = error_mark_node;
8242 else
8244 ret.value = build_constructor (constructor_type,
8245 constructor_elements);
8246 if (constructor_constant)
8247 TREE_CONSTANT (ret.value) = 1;
8248 if (constructor_constant && constructor_simple)
8249 TREE_STATIC (ret.value) = 1;
8250 if (constructor_nonconst)
8251 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8255 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8257 if (constructor_nonconst)
8258 ret.original_code = C_MAYBE_CONST_EXPR;
8259 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8260 ret.original_code = ERROR_MARK;
8263 constructor_type = p->type;
8264 constructor_fields = p->fields;
8265 constructor_index = p->index;
8266 constructor_max_index = p->max_index;
8267 constructor_unfilled_index = p->unfilled_index;
8268 constructor_unfilled_fields = p->unfilled_fields;
8269 constructor_bit_index = p->bit_index;
8270 constructor_elements = p->elements;
8271 constructor_constant = p->constant;
8272 constructor_simple = p->simple;
8273 constructor_nonconst = p->nonconst;
8274 constructor_erroneous = p->erroneous;
8275 constructor_incremental = p->incremental;
8276 constructor_designated = p->designated;
8277 designator_depth = p->designator_depth;
8278 constructor_pending_elts = p->pending_elts;
8279 constructor_depth = p->depth;
8280 if (!p->implicit)
8281 constructor_range_stack = p->range_stack;
8282 RESTORE_SPELLING_DEPTH (constructor_depth);
8284 constructor_stack = p->next;
8285 free (p);
8287 if (ret.value == NULL_TREE && constructor_stack == 0)
8288 ret.value = error_mark_node;
8289 return ret;
8292 /* Common handling for both array range and field name designators.
8293 ARRAY argument is nonzero for array ranges. Returns false for success. */
8295 static bool
8296 set_designator (location_t loc, bool array,
8297 struct obstack *braced_init_obstack)
8299 tree subtype;
8300 enum tree_code subcode;
8302 /* Don't die if an entire brace-pair level is superfluous
8303 in the containing level. */
8304 if (constructor_type == NULL_TREE)
8305 return true;
8307 /* If there were errors in this designator list already, bail out
8308 silently. */
8309 if (designator_erroneous)
8310 return true;
8312 if (!designator_depth)
8314 gcc_assert (!constructor_range_stack);
8316 /* Designator list starts at the level of closest explicit
8317 braces. */
8318 while (constructor_stack->implicit)
8319 process_init_element (input_location,
8320 pop_init_level (loc, 1, braced_init_obstack,
8321 last_init_list_comma),
8322 true, braced_init_obstack);
8323 constructor_designated = 1;
8324 return false;
8327 switch (TREE_CODE (constructor_type))
8329 case RECORD_TYPE:
8330 case UNION_TYPE:
8331 subtype = TREE_TYPE (constructor_fields);
8332 if (subtype != error_mark_node)
8333 subtype = TYPE_MAIN_VARIANT (subtype);
8334 break;
8335 case ARRAY_TYPE:
8336 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8337 break;
8338 default:
8339 gcc_unreachable ();
8342 subcode = TREE_CODE (subtype);
8343 if (array && subcode != ARRAY_TYPE)
8345 error_init (loc, "array index in non-array initializer");
8346 return true;
8348 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8350 error_init (loc, "field name not in record or union initializer");
8351 return true;
8354 constructor_designated = 1;
8355 finish_implicit_inits (loc, braced_init_obstack);
8356 push_init_level (loc, 2, braced_init_obstack);
8357 return false;
8360 /* If there are range designators in designator list, push a new designator
8361 to constructor_range_stack. RANGE_END is end of such stack range or
8362 NULL_TREE if there is no range designator at this level. */
8364 static void
8365 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8367 struct constructor_range_stack *p;
8369 p = (struct constructor_range_stack *)
8370 obstack_alloc (braced_init_obstack,
8371 sizeof (struct constructor_range_stack));
8372 p->prev = constructor_range_stack;
8373 p->next = 0;
8374 p->fields = constructor_fields;
8375 p->range_start = constructor_index;
8376 p->index = constructor_index;
8377 p->stack = constructor_stack;
8378 p->range_end = range_end;
8379 if (constructor_range_stack)
8380 constructor_range_stack->next = p;
8381 constructor_range_stack = p;
8384 /* Within an array initializer, specify the next index to be initialized.
8385 FIRST is that index. If LAST is nonzero, then initialize a range
8386 of indices, running from FIRST through LAST. */
8388 void
8389 set_init_index (location_t loc, tree first, tree last,
8390 struct obstack *braced_init_obstack)
8392 if (set_designator (loc, true, braced_init_obstack))
8393 return;
8395 designator_erroneous = 1;
8397 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8398 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8400 error_init (loc, "array index in initializer not of integer type");
8401 return;
8404 if (TREE_CODE (first) != INTEGER_CST)
8406 first = c_fully_fold (first, false, NULL);
8407 if (TREE_CODE (first) == INTEGER_CST)
8408 pedwarn_init (loc, OPT_Wpedantic,
8409 "array index in initializer is not "
8410 "an integer constant expression");
8413 if (last && TREE_CODE (last) != INTEGER_CST)
8415 last = c_fully_fold (last, false, NULL);
8416 if (TREE_CODE (last) == INTEGER_CST)
8417 pedwarn_init (loc, OPT_Wpedantic,
8418 "array index in initializer is not "
8419 "an integer constant expression");
8422 if (TREE_CODE (first) != INTEGER_CST)
8423 error_init (loc, "nonconstant array index in initializer");
8424 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8425 error_init (loc, "nonconstant array index in initializer");
8426 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8427 error_init (loc, "array index in non-array initializer");
8428 else if (tree_int_cst_sgn (first) == -1)
8429 error_init (loc, "array index in initializer exceeds array bounds");
8430 else if (constructor_max_index
8431 && tree_int_cst_lt (constructor_max_index, first))
8432 error_init (loc, "array index in initializer exceeds array bounds");
8433 else
8435 constant_expression_warning (first);
8436 if (last)
8437 constant_expression_warning (last);
8438 constructor_index = convert (bitsizetype, first);
8439 if (tree_int_cst_lt (constructor_index, first))
8441 constructor_index = copy_node (constructor_index);
8442 TREE_OVERFLOW (constructor_index) = 1;
8445 if (last)
8447 if (tree_int_cst_equal (first, last))
8448 last = NULL_TREE;
8449 else if (tree_int_cst_lt (last, first))
8451 error_init (loc, "empty index range in initializer");
8452 last = NULL_TREE;
8454 else
8456 last = convert (bitsizetype, last);
8457 if (constructor_max_index != NULL_TREE
8458 && tree_int_cst_lt (constructor_max_index, last))
8460 error_init (loc, "array index range in initializer exceeds "
8461 "array bounds");
8462 last = NULL_TREE;
8467 designator_depth++;
8468 designator_erroneous = 0;
8469 if (constructor_range_stack || last)
8470 push_range_stack (last, braced_init_obstack);
8474 /* Within a struct initializer, specify the next field to be initialized. */
8476 void
8477 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8478 struct obstack *braced_init_obstack)
8480 tree field;
8482 if (set_designator (loc, false, braced_init_obstack))
8483 return;
8485 designator_erroneous = 1;
8487 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8489 error_init (loc, "field name not in record or union initializer");
8490 return;
8493 field = lookup_field (constructor_type, fieldname);
8495 if (field == NULL_TREE)
8497 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8498 if (guessed_id)
8500 gcc_rich_location rich_loc (fieldname_loc);
8501 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8502 error_at_rich_loc
8503 (&rich_loc,
8504 "%qT has no member named %qE; did you mean %qE?",
8505 constructor_type, fieldname, guessed_id);
8507 else
8508 error_at (fieldname_loc, "%qT has no member named %qE",
8509 constructor_type, fieldname);
8511 else
8514 constructor_fields = TREE_VALUE (field);
8515 designator_depth++;
8516 designator_erroneous = 0;
8517 if (constructor_range_stack)
8518 push_range_stack (NULL_TREE, braced_init_obstack);
8519 field = TREE_CHAIN (field);
8520 if (field)
8522 if (set_designator (loc, false, braced_init_obstack))
8523 return;
8526 while (field != NULL_TREE);
8529 /* Add a new initializer to the tree of pending initializers. PURPOSE
8530 identifies the initializer, either array index or field in a structure.
8531 VALUE is the value of that index or field. If ORIGTYPE is not
8532 NULL_TREE, it is the original type of VALUE.
8534 IMPLICIT is true if value comes from pop_init_level (1),
8535 the new initializer has been merged with the existing one
8536 and thus no warnings should be emitted about overriding an
8537 existing initializer. */
8539 static void
8540 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8541 bool implicit, struct obstack *braced_init_obstack)
8543 struct init_node *p, **q, *r;
8545 q = &constructor_pending_elts;
8546 p = 0;
8548 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8550 while (*q != 0)
8552 p = *q;
8553 if (tree_int_cst_lt (purpose, p->purpose))
8554 q = &p->left;
8555 else if (tree_int_cst_lt (p->purpose, purpose))
8556 q = &p->right;
8557 else
8559 if (!implicit)
8561 if (TREE_SIDE_EFFECTS (p->value))
8562 warning_init (loc, OPT_Woverride_init_side_effects,
8563 "initialized field with side-effects "
8564 "overwritten");
8565 else if (warn_override_init)
8566 warning_init (loc, OPT_Woverride_init,
8567 "initialized field overwritten");
8569 p->value = value;
8570 p->origtype = origtype;
8571 return;
8575 else
8577 tree bitpos;
8579 bitpos = bit_position (purpose);
8580 while (*q != NULL)
8582 p = *q;
8583 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8584 q = &p->left;
8585 else if (p->purpose != purpose)
8586 q = &p->right;
8587 else
8589 if (!implicit)
8591 if (TREE_SIDE_EFFECTS (p->value))
8592 warning_init (loc, OPT_Woverride_init_side_effects,
8593 "initialized field with side-effects "
8594 "overwritten");
8595 else if (warn_override_init)
8596 warning_init (loc, OPT_Woverride_init,
8597 "initialized field overwritten");
8599 p->value = value;
8600 p->origtype = origtype;
8601 return;
8606 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8607 sizeof (struct init_node));
8608 r->purpose = purpose;
8609 r->value = value;
8610 r->origtype = origtype;
8612 *q = r;
8613 r->parent = p;
8614 r->left = 0;
8615 r->right = 0;
8616 r->balance = 0;
8618 while (p)
8620 struct init_node *s;
8622 if (r == p->left)
8624 if (p->balance == 0)
8625 p->balance = -1;
8626 else if (p->balance < 0)
8628 if (r->balance < 0)
8630 /* L rotation. */
8631 p->left = r->right;
8632 if (p->left)
8633 p->left->parent = p;
8634 r->right = p;
8636 p->balance = 0;
8637 r->balance = 0;
8639 s = p->parent;
8640 p->parent = r;
8641 r->parent = s;
8642 if (s)
8644 if (s->left == p)
8645 s->left = r;
8646 else
8647 s->right = r;
8649 else
8650 constructor_pending_elts = r;
8652 else
8654 /* LR rotation. */
8655 struct init_node *t = r->right;
8657 r->right = t->left;
8658 if (r->right)
8659 r->right->parent = r;
8660 t->left = r;
8662 p->left = t->right;
8663 if (p->left)
8664 p->left->parent = p;
8665 t->right = p;
8667 p->balance = t->balance < 0;
8668 r->balance = -(t->balance > 0);
8669 t->balance = 0;
8671 s = p->parent;
8672 p->parent = t;
8673 r->parent = t;
8674 t->parent = s;
8675 if (s)
8677 if (s->left == p)
8678 s->left = t;
8679 else
8680 s->right = t;
8682 else
8683 constructor_pending_elts = t;
8685 break;
8687 else
8689 /* p->balance == +1; growth of left side balances the node. */
8690 p->balance = 0;
8691 break;
8694 else /* r == p->right */
8696 if (p->balance == 0)
8697 /* Growth propagation from right side. */
8698 p->balance++;
8699 else if (p->balance > 0)
8701 if (r->balance > 0)
8703 /* R rotation. */
8704 p->right = r->left;
8705 if (p->right)
8706 p->right->parent = p;
8707 r->left = p;
8709 p->balance = 0;
8710 r->balance = 0;
8712 s = p->parent;
8713 p->parent = r;
8714 r->parent = s;
8715 if (s)
8717 if (s->left == p)
8718 s->left = r;
8719 else
8720 s->right = r;
8722 else
8723 constructor_pending_elts = r;
8725 else /* r->balance == -1 */
8727 /* RL rotation */
8728 struct init_node *t = r->left;
8730 r->left = t->right;
8731 if (r->left)
8732 r->left->parent = r;
8733 t->right = r;
8735 p->right = t->left;
8736 if (p->right)
8737 p->right->parent = p;
8738 t->left = p;
8740 r->balance = (t->balance < 0);
8741 p->balance = -(t->balance > 0);
8742 t->balance = 0;
8744 s = p->parent;
8745 p->parent = t;
8746 r->parent = t;
8747 t->parent = s;
8748 if (s)
8750 if (s->left == p)
8751 s->left = t;
8752 else
8753 s->right = t;
8755 else
8756 constructor_pending_elts = t;
8758 break;
8760 else
8762 /* p->balance == -1; growth of right side balances the node. */
8763 p->balance = 0;
8764 break;
8768 r = p;
8769 p = p->parent;
8773 /* Build AVL tree from a sorted chain. */
8775 static void
8776 set_nonincremental_init (struct obstack * braced_init_obstack)
8778 unsigned HOST_WIDE_INT ix;
8779 tree index, value;
8781 if (TREE_CODE (constructor_type) != RECORD_TYPE
8782 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8783 return;
8785 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8786 add_pending_init (input_location, index, value, NULL_TREE, true,
8787 braced_init_obstack);
8788 constructor_elements = NULL;
8789 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8791 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8792 /* Skip any nameless bit fields at the beginning. */
8793 while (constructor_unfilled_fields != NULL_TREE
8794 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8795 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
8796 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8799 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8801 if (TYPE_DOMAIN (constructor_type))
8802 constructor_unfilled_index
8803 = convert (bitsizetype,
8804 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8805 else
8806 constructor_unfilled_index = bitsize_zero_node;
8808 constructor_incremental = 0;
8811 /* Build AVL tree from a string constant. */
8813 static void
8814 set_nonincremental_init_from_string (tree str,
8815 struct obstack * braced_init_obstack)
8817 tree value, purpose, type;
8818 HOST_WIDE_INT val[2];
8819 const char *p, *end;
8820 int byte, wchar_bytes, charwidth, bitpos;
8822 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8824 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8825 charwidth = TYPE_PRECISION (char_type_node);
8826 gcc_assert ((size_t) wchar_bytes * charwidth
8827 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8828 type = TREE_TYPE (constructor_type);
8829 p = TREE_STRING_POINTER (str);
8830 end = p + TREE_STRING_LENGTH (str);
8832 for (purpose = bitsize_zero_node;
8833 p < end
8834 && !(constructor_max_index
8835 && tree_int_cst_lt (constructor_max_index, purpose));
8836 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8838 if (wchar_bytes == 1)
8840 val[0] = (unsigned char) *p++;
8841 val[1] = 0;
8843 else
8845 val[1] = 0;
8846 val[0] = 0;
8847 for (byte = 0; byte < wchar_bytes; byte++)
8849 if (BYTES_BIG_ENDIAN)
8850 bitpos = (wchar_bytes - byte - 1) * charwidth;
8851 else
8852 bitpos = byte * charwidth;
8853 val[bitpos / HOST_BITS_PER_WIDE_INT]
8854 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8855 << (bitpos % HOST_BITS_PER_WIDE_INT);
8859 if (!TYPE_UNSIGNED (type))
8861 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8862 if (bitpos < HOST_BITS_PER_WIDE_INT)
8864 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8866 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8867 val[1] = -1;
8870 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8872 if (val[0] < 0)
8873 val[1] = -1;
8875 else if (val[1] & (HOST_WIDE_INT_1
8876 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8877 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8880 value = wide_int_to_tree (type,
8881 wide_int::from_array (val, 2,
8882 HOST_BITS_PER_WIDE_INT * 2));
8883 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8884 braced_init_obstack);
8887 constructor_incremental = 0;
8890 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
8891 not initialized yet. */
8893 static tree
8894 find_init_member (tree field, struct obstack * braced_init_obstack)
8896 struct init_node *p;
8898 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8900 if (constructor_incremental
8901 && tree_int_cst_lt (field, constructor_unfilled_index))
8902 set_nonincremental_init (braced_init_obstack);
8904 p = constructor_pending_elts;
8905 while (p)
8907 if (tree_int_cst_lt (field, p->purpose))
8908 p = p->left;
8909 else if (tree_int_cst_lt (p->purpose, field))
8910 p = p->right;
8911 else
8912 return p->value;
8915 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8917 tree bitpos = bit_position (field);
8919 if (constructor_incremental
8920 && (!constructor_unfilled_fields
8921 || tree_int_cst_lt (bitpos,
8922 bit_position (constructor_unfilled_fields))))
8923 set_nonincremental_init (braced_init_obstack);
8925 p = constructor_pending_elts;
8926 while (p)
8928 if (field == p->purpose)
8929 return p->value;
8930 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8931 p = p->left;
8932 else
8933 p = p->right;
8936 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8938 if (!vec_safe_is_empty (constructor_elements)
8939 && (constructor_elements->last ().index == field))
8940 return constructor_elements->last ().value;
8942 return NULL_TREE;
8945 /* "Output" the next constructor element.
8946 At top level, really output it to assembler code now.
8947 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8948 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8949 TYPE is the data type that the containing data type wants here.
8950 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8951 If VALUE is a string constant, STRICT_STRING is true if it is
8952 unparenthesized or we should not warn here for it being parenthesized.
8953 For other types of VALUE, STRICT_STRING is not used.
8955 PENDING if true means output pending elements that belong
8956 right after this element. (PENDING is normally true;
8957 it is false while outputting pending elements, to avoid recursion.)
8959 IMPLICIT is true if value comes from pop_init_level (1),
8960 the new initializer has been merged with the existing one
8961 and thus no warnings should be emitted about overriding an
8962 existing initializer. */
8964 static void
8965 output_init_element (location_t loc, tree value, tree origtype,
8966 bool strict_string, tree type, tree field, bool pending,
8967 bool implicit, struct obstack * braced_init_obstack)
8969 tree semantic_type = NULL_TREE;
8970 bool maybe_const = true;
8971 bool npc;
8973 if (type == error_mark_node || value == error_mark_node)
8975 constructor_erroneous = 1;
8976 return;
8978 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8979 && (TREE_CODE (value) == STRING_CST
8980 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8981 && !(TREE_CODE (value) == STRING_CST
8982 && TREE_CODE (type) == ARRAY_TYPE
8983 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8984 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8985 TYPE_MAIN_VARIANT (type)))
8986 value = array_to_pointer_conversion (input_location, value);
8988 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8989 && require_constant_value && pending)
8991 /* As an extension, allow initializing objects with static storage
8992 duration with compound literals (which are then treated just as
8993 the brace enclosed list they contain). */
8994 if (flag_isoc99)
8995 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8996 "constant");
8997 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8998 value = DECL_INITIAL (decl);
9001 npc = null_pointer_constant_p (value);
9002 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9004 semantic_type = TREE_TYPE (value);
9005 value = TREE_OPERAND (value, 0);
9007 value = c_fully_fold (value, require_constant_value, &maybe_const);
9009 if (value == error_mark_node)
9010 constructor_erroneous = 1;
9011 else if (!TREE_CONSTANT (value))
9012 constructor_constant = 0;
9013 else if (!initializer_constant_valid_p (value,
9014 TREE_TYPE (value),
9015 AGGREGATE_TYPE_P (constructor_type)
9016 && TYPE_REVERSE_STORAGE_ORDER
9017 (constructor_type))
9018 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9019 && DECL_C_BIT_FIELD (field)
9020 && TREE_CODE (value) != INTEGER_CST))
9021 constructor_simple = 0;
9022 if (!maybe_const)
9023 constructor_nonconst = 1;
9025 /* Digest the initializer and issue any errors about incompatible
9026 types before issuing errors about non-constant initializers. */
9027 tree new_value = value;
9028 if (semantic_type)
9029 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9030 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9031 require_constant_value);
9032 if (new_value == error_mark_node)
9034 constructor_erroneous = 1;
9035 return;
9037 if (require_constant_value || require_constant_elements)
9038 constant_expression_warning (new_value);
9040 /* Proceed to check the constness of the original initializer. */
9041 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9043 if (require_constant_value)
9045 error_init (loc, "initializer element is not constant");
9046 value = error_mark_node;
9048 else if (require_constant_elements)
9049 pedwarn (loc, OPT_Wpedantic,
9050 "initializer element is not computable at load time");
9052 else if (!maybe_const
9053 && (require_constant_value || require_constant_elements))
9054 pedwarn_init (loc, OPT_Wpedantic,
9055 "initializer element is not a constant expression");
9057 /* Issue -Wc++-compat warnings about initializing a bitfield with
9058 enum type. */
9059 if (warn_cxx_compat
9060 && field != NULL_TREE
9061 && TREE_CODE (field) == FIELD_DECL
9062 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9063 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9064 != TYPE_MAIN_VARIANT (type))
9065 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9067 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9068 if (checktype != error_mark_node
9069 && (TYPE_MAIN_VARIANT (checktype)
9070 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9071 warning_init (loc, OPT_Wc___compat,
9072 "enum conversion in initialization is invalid in C++");
9075 /* If this field is empty (and not at the end of structure),
9076 don't do anything other than checking the initializer. */
9077 if (field
9078 && (TREE_TYPE (field) == error_mark_node
9079 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9080 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9081 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9082 || DECL_CHAIN (field)))))
9083 return;
9085 /* Finally, set VALUE to the initializer value digested above. */
9086 value = new_value;
9088 /* If this element doesn't come next in sequence,
9089 put it on constructor_pending_elts. */
9090 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9091 && (!constructor_incremental
9092 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9094 if (constructor_incremental
9095 && tree_int_cst_lt (field, constructor_unfilled_index))
9096 set_nonincremental_init (braced_init_obstack);
9098 add_pending_init (loc, field, value, origtype, implicit,
9099 braced_init_obstack);
9100 return;
9102 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9103 && (!constructor_incremental
9104 || field != constructor_unfilled_fields))
9106 /* We do this for records but not for unions. In a union,
9107 no matter which field is specified, it can be initialized
9108 right away since it starts at the beginning of the union. */
9109 if (constructor_incremental)
9111 if (!constructor_unfilled_fields)
9112 set_nonincremental_init (braced_init_obstack);
9113 else
9115 tree bitpos, unfillpos;
9117 bitpos = bit_position (field);
9118 unfillpos = bit_position (constructor_unfilled_fields);
9120 if (tree_int_cst_lt (bitpos, unfillpos))
9121 set_nonincremental_init (braced_init_obstack);
9125 add_pending_init (loc, field, value, origtype, implicit,
9126 braced_init_obstack);
9127 return;
9129 else if (TREE_CODE (constructor_type) == UNION_TYPE
9130 && !vec_safe_is_empty (constructor_elements))
9132 if (!implicit)
9134 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9135 warning_init (loc, OPT_Woverride_init_side_effects,
9136 "initialized field with side-effects overwritten");
9137 else if (warn_override_init)
9138 warning_init (loc, OPT_Woverride_init,
9139 "initialized field overwritten");
9142 /* We can have just one union field set. */
9143 constructor_elements = NULL;
9146 /* Otherwise, output this element either to
9147 constructor_elements or to the assembler file. */
9149 constructor_elt celt = {field, value};
9150 vec_safe_push (constructor_elements, celt);
9152 /* Advance the variable that indicates sequential elements output. */
9153 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9154 constructor_unfilled_index
9155 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9156 bitsize_one_node);
9157 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9159 constructor_unfilled_fields
9160 = DECL_CHAIN (constructor_unfilled_fields);
9162 /* Skip any nameless bit fields. */
9163 while (constructor_unfilled_fields != NULL_TREE
9164 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9165 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
9166 constructor_unfilled_fields =
9167 DECL_CHAIN (constructor_unfilled_fields);
9169 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9170 constructor_unfilled_fields = NULL_TREE;
9172 /* Now output any pending elements which have become next. */
9173 if (pending)
9174 output_pending_init_elements (0, braced_init_obstack);
9177 /* Output any pending elements which have become next.
9178 As we output elements, constructor_unfilled_{fields,index}
9179 advances, which may cause other elements to become next;
9180 if so, they too are output.
9182 If ALL is 0, we return when there are
9183 no more pending elements to output now.
9185 If ALL is 1, we output space as necessary so that
9186 we can output all the pending elements. */
9187 static void
9188 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9190 struct init_node *elt = constructor_pending_elts;
9191 tree next;
9193 retry:
9195 /* Look through the whole pending tree.
9196 If we find an element that should be output now,
9197 output it. Otherwise, set NEXT to the element
9198 that comes first among those still pending. */
9200 next = NULL_TREE;
9201 while (elt)
9203 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9205 if (tree_int_cst_equal (elt->purpose,
9206 constructor_unfilled_index))
9207 output_init_element (input_location, elt->value, elt->origtype,
9208 true, TREE_TYPE (constructor_type),
9209 constructor_unfilled_index, false, false,
9210 braced_init_obstack);
9211 else if (tree_int_cst_lt (constructor_unfilled_index,
9212 elt->purpose))
9214 /* Advance to the next smaller node. */
9215 if (elt->left)
9216 elt = elt->left;
9217 else
9219 /* We have reached the smallest node bigger than the
9220 current unfilled index. Fill the space first. */
9221 next = elt->purpose;
9222 break;
9225 else
9227 /* Advance to the next bigger node. */
9228 if (elt->right)
9229 elt = elt->right;
9230 else
9232 /* We have reached the biggest node in a subtree. Find
9233 the parent of it, which is the next bigger node. */
9234 while (elt->parent && elt->parent->right == elt)
9235 elt = elt->parent;
9236 elt = elt->parent;
9237 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9238 elt->purpose))
9240 next = elt->purpose;
9241 break;
9246 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9248 tree ctor_unfilled_bitpos, elt_bitpos;
9250 /* If the current record is complete we are done. */
9251 if (constructor_unfilled_fields == NULL_TREE)
9252 break;
9254 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9255 elt_bitpos = bit_position (elt->purpose);
9256 /* We can't compare fields here because there might be empty
9257 fields in between. */
9258 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9260 constructor_unfilled_fields = elt->purpose;
9261 output_init_element (input_location, elt->value, elt->origtype,
9262 true, TREE_TYPE (elt->purpose),
9263 elt->purpose, false, false,
9264 braced_init_obstack);
9266 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9268 /* Advance to the next smaller node. */
9269 if (elt->left)
9270 elt = elt->left;
9271 else
9273 /* We have reached the smallest node bigger than the
9274 current unfilled field. Fill the space first. */
9275 next = elt->purpose;
9276 break;
9279 else
9281 /* Advance to the next bigger node. */
9282 if (elt->right)
9283 elt = elt->right;
9284 else
9286 /* We have reached the biggest node in a subtree. Find
9287 the parent of it, which is the next bigger node. */
9288 while (elt->parent && elt->parent->right == elt)
9289 elt = elt->parent;
9290 elt = elt->parent;
9291 if (elt
9292 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9293 bit_position (elt->purpose))))
9295 next = elt->purpose;
9296 break;
9303 /* Ordinarily return, but not if we want to output all
9304 and there are elements left. */
9305 if (!(all && next != NULL_TREE))
9306 return;
9308 /* If it's not incremental, just skip over the gap, so that after
9309 jumping to retry we will output the next successive element. */
9310 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9311 constructor_unfilled_fields = next;
9312 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9313 constructor_unfilled_index = next;
9315 /* ELT now points to the node in the pending tree with the next
9316 initializer to output. */
9317 goto retry;
9320 /* Add one non-braced element to the current constructor level.
9321 This adjusts the current position within the constructor's type.
9322 This may also start or terminate implicit levels
9323 to handle a partly-braced initializer.
9325 Once this has found the correct level for the new element,
9326 it calls output_init_element.
9328 IMPLICIT is true if value comes from pop_init_level (1),
9329 the new initializer has been merged with the existing one
9330 and thus no warnings should be emitted about overriding an
9331 existing initializer. */
9333 void
9334 process_init_element (location_t loc, struct c_expr value, bool implicit,
9335 struct obstack * braced_init_obstack)
9337 tree orig_value = value.value;
9338 int string_flag
9339 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9340 bool strict_string = value.original_code == STRING_CST;
9341 bool was_designated = designator_depth != 0;
9343 designator_depth = 0;
9344 designator_erroneous = 0;
9346 if (!implicit && value.value && !integer_zerop (value.value))
9347 constructor_zeroinit = 0;
9349 /* Handle superfluous braces around string cst as in
9350 char x[] = {"foo"}; */
9351 if (string_flag
9352 && constructor_type
9353 && !was_designated
9354 && TREE_CODE (constructor_type) == ARRAY_TYPE
9355 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9356 && integer_zerop (constructor_unfilled_index))
9358 if (constructor_stack->replacement_value.value)
9359 error_init (loc, "excess elements in char array initializer");
9360 constructor_stack->replacement_value = value;
9361 return;
9364 if (constructor_stack->replacement_value.value != NULL_TREE)
9366 error_init (loc, "excess elements in struct initializer");
9367 return;
9370 /* Ignore elements of a brace group if it is entirely superfluous
9371 and has already been diagnosed. */
9372 if (constructor_type == NULL_TREE)
9373 return;
9375 if (!implicit && warn_designated_init && !was_designated
9376 && TREE_CODE (constructor_type) == RECORD_TYPE
9377 && lookup_attribute ("designated_init",
9378 TYPE_ATTRIBUTES (constructor_type)))
9379 warning_init (loc,
9380 OPT_Wdesignated_init,
9381 "positional initialization of field "
9382 "in %<struct%> declared with %<designated_init%> attribute");
9384 /* If we've exhausted any levels that didn't have braces,
9385 pop them now. */
9386 while (constructor_stack->implicit)
9388 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9389 && constructor_fields == NULL_TREE)
9390 process_init_element (loc,
9391 pop_init_level (loc, 1, braced_init_obstack,
9392 last_init_list_comma),
9393 true, braced_init_obstack);
9394 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9395 || VECTOR_TYPE_P (constructor_type))
9396 && constructor_max_index
9397 && tree_int_cst_lt (constructor_max_index,
9398 constructor_index))
9399 process_init_element (loc,
9400 pop_init_level (loc, 1, braced_init_obstack,
9401 last_init_list_comma),
9402 true, braced_init_obstack);
9403 else
9404 break;
9407 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9408 if (constructor_range_stack)
9410 /* If value is a compound literal and we'll be just using its
9411 content, don't put it into a SAVE_EXPR. */
9412 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9413 || !require_constant_value)
9415 tree semantic_type = NULL_TREE;
9416 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9418 semantic_type = TREE_TYPE (value.value);
9419 value.value = TREE_OPERAND (value.value, 0);
9421 value.value = save_expr (value.value);
9422 if (semantic_type)
9423 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9424 value.value);
9428 while (1)
9430 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9432 tree fieldtype;
9433 enum tree_code fieldcode;
9435 if (constructor_fields == NULL_TREE)
9437 pedwarn_init (loc, 0, "excess elements in struct initializer");
9438 break;
9441 fieldtype = TREE_TYPE (constructor_fields);
9442 if (fieldtype != error_mark_node)
9443 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9444 fieldcode = TREE_CODE (fieldtype);
9446 /* Error for non-static initialization of a flexible array member. */
9447 if (fieldcode == ARRAY_TYPE
9448 && !require_constant_value
9449 && TYPE_SIZE (fieldtype) == NULL_TREE
9450 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9452 error_init (loc, "non-static initialization of a flexible "
9453 "array member");
9454 break;
9457 /* Error for initialization of a flexible array member with
9458 a string constant if the structure is in an array. E.g.:
9459 struct S { int x; char y[]; };
9460 struct S s[] = { { 1, "foo" } };
9461 is invalid. */
9462 if (string_flag
9463 && fieldcode == ARRAY_TYPE
9464 && constructor_depth > 1
9465 && TYPE_SIZE (fieldtype) == NULL_TREE
9466 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9468 bool in_array_p = false;
9469 for (struct constructor_stack *p = constructor_stack;
9470 p && p->type; p = p->next)
9471 if (TREE_CODE (p->type) == ARRAY_TYPE)
9473 in_array_p = true;
9474 break;
9476 if (in_array_p)
9478 error_init (loc, "initialization of flexible array "
9479 "member in a nested context");
9480 break;
9484 /* Accept a string constant to initialize a subarray. */
9485 if (value.value != NULL_TREE
9486 && fieldcode == ARRAY_TYPE
9487 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9488 && string_flag)
9489 value.value = orig_value;
9490 /* Otherwise, if we have come to a subaggregate,
9491 and we don't have an element of its type, push into it. */
9492 else if (value.value != NULL_TREE
9493 && value.value != error_mark_node
9494 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9495 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9496 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9498 push_init_level (loc, 1, braced_init_obstack);
9499 continue;
9502 if (value.value)
9504 push_member_name (constructor_fields);
9505 output_init_element (loc, value.value, value.original_type,
9506 strict_string, fieldtype,
9507 constructor_fields, true, implicit,
9508 braced_init_obstack);
9509 RESTORE_SPELLING_DEPTH (constructor_depth);
9511 else
9512 /* Do the bookkeeping for an element that was
9513 directly output as a constructor. */
9515 /* For a record, keep track of end position of last field. */
9516 if (DECL_SIZE (constructor_fields))
9517 constructor_bit_index
9518 = size_binop_loc (input_location, PLUS_EXPR,
9519 bit_position (constructor_fields),
9520 DECL_SIZE (constructor_fields));
9522 /* If the current field was the first one not yet written out,
9523 it isn't now, so update. */
9524 if (constructor_unfilled_fields == constructor_fields)
9526 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9527 /* Skip any nameless bit fields. */
9528 while (constructor_unfilled_fields != 0
9529 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9530 && DECL_NAME (constructor_unfilled_fields) == 0)
9531 constructor_unfilled_fields =
9532 DECL_CHAIN (constructor_unfilled_fields);
9536 constructor_fields = DECL_CHAIN (constructor_fields);
9537 /* Skip any nameless bit fields at the beginning. */
9538 while (constructor_fields != NULL_TREE
9539 && DECL_C_BIT_FIELD (constructor_fields)
9540 && DECL_NAME (constructor_fields) == NULL_TREE)
9541 constructor_fields = DECL_CHAIN (constructor_fields);
9543 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9545 tree fieldtype;
9546 enum tree_code fieldcode;
9548 if (constructor_fields == NULL_TREE)
9550 pedwarn_init (loc, 0,
9551 "excess elements in union initializer");
9552 break;
9555 fieldtype = TREE_TYPE (constructor_fields);
9556 if (fieldtype != error_mark_node)
9557 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9558 fieldcode = TREE_CODE (fieldtype);
9560 /* Warn that traditional C rejects initialization of unions.
9561 We skip the warning if the value is zero. This is done
9562 under the assumption that the zero initializer in user
9563 code appears conditioned on e.g. __STDC__ to avoid
9564 "missing initializer" warnings and relies on default
9565 initialization to zero in the traditional C case.
9566 We also skip the warning if the initializer is designated,
9567 again on the assumption that this must be conditional on
9568 __STDC__ anyway (and we've already complained about the
9569 member-designator already). */
9570 if (!in_system_header_at (input_location) && !constructor_designated
9571 && !(value.value && (integer_zerop (value.value)
9572 || real_zerop (value.value))))
9573 warning (OPT_Wtraditional, "traditional C rejects initialization "
9574 "of unions");
9576 /* Accept a string constant to initialize a subarray. */
9577 if (value.value != NULL_TREE
9578 && fieldcode == ARRAY_TYPE
9579 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9580 && string_flag)
9581 value.value = orig_value;
9582 /* Otherwise, if we have come to a subaggregate,
9583 and we don't have an element of its type, push into it. */
9584 else if (value.value != NULL_TREE
9585 && value.value != error_mark_node
9586 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9587 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9588 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9590 push_init_level (loc, 1, braced_init_obstack);
9591 continue;
9594 if (value.value)
9596 push_member_name (constructor_fields);
9597 output_init_element (loc, value.value, value.original_type,
9598 strict_string, fieldtype,
9599 constructor_fields, true, implicit,
9600 braced_init_obstack);
9601 RESTORE_SPELLING_DEPTH (constructor_depth);
9603 else
9604 /* Do the bookkeeping for an element that was
9605 directly output as a constructor. */
9607 constructor_bit_index = DECL_SIZE (constructor_fields);
9608 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9611 constructor_fields = NULL_TREE;
9613 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9615 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9616 enum tree_code eltcode = TREE_CODE (elttype);
9618 /* Accept a string constant to initialize a subarray. */
9619 if (value.value != NULL_TREE
9620 && eltcode == ARRAY_TYPE
9621 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9622 && string_flag)
9623 value.value = orig_value;
9624 /* Otherwise, if we have come to a subaggregate,
9625 and we don't have an element of its type, push into it. */
9626 else if (value.value != NULL_TREE
9627 && value.value != error_mark_node
9628 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9629 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9630 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9632 push_init_level (loc, 1, braced_init_obstack);
9633 continue;
9636 if (constructor_max_index != NULL_TREE
9637 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9638 || integer_all_onesp (constructor_max_index)))
9640 pedwarn_init (loc, 0,
9641 "excess elements in array initializer");
9642 break;
9645 /* Now output the actual element. */
9646 if (value.value)
9648 push_array_bounds (tree_to_uhwi (constructor_index));
9649 output_init_element (loc, value.value, value.original_type,
9650 strict_string, elttype,
9651 constructor_index, true, implicit,
9652 braced_init_obstack);
9653 RESTORE_SPELLING_DEPTH (constructor_depth);
9656 constructor_index
9657 = size_binop_loc (input_location, PLUS_EXPR,
9658 constructor_index, bitsize_one_node);
9660 if (!value.value)
9661 /* If we are doing the bookkeeping for an element that was
9662 directly output as a constructor, we must update
9663 constructor_unfilled_index. */
9664 constructor_unfilled_index = constructor_index;
9666 else if (VECTOR_TYPE_P (constructor_type))
9668 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9670 /* Do a basic check of initializer size. Note that vectors
9671 always have a fixed size derived from their type. */
9672 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9674 pedwarn_init (loc, 0,
9675 "excess elements in vector initializer");
9676 break;
9679 /* Now output the actual element. */
9680 if (value.value)
9682 if (TREE_CODE (value.value) == VECTOR_CST)
9683 elttype = TYPE_MAIN_VARIANT (constructor_type);
9684 output_init_element (loc, value.value, value.original_type,
9685 strict_string, elttype,
9686 constructor_index, true, implicit,
9687 braced_init_obstack);
9690 constructor_index
9691 = size_binop_loc (input_location,
9692 PLUS_EXPR, constructor_index, bitsize_one_node);
9694 if (!value.value)
9695 /* If we are doing the bookkeeping for an element that was
9696 directly output as a constructor, we must update
9697 constructor_unfilled_index. */
9698 constructor_unfilled_index = constructor_index;
9701 /* Handle the sole element allowed in a braced initializer
9702 for a scalar variable. */
9703 else if (constructor_type != error_mark_node
9704 && constructor_fields == NULL_TREE)
9706 pedwarn_init (loc, 0,
9707 "excess elements in scalar initializer");
9708 break;
9710 else
9712 if (value.value)
9713 output_init_element (loc, value.value, value.original_type,
9714 strict_string, constructor_type,
9715 NULL_TREE, true, implicit,
9716 braced_init_obstack);
9717 constructor_fields = NULL_TREE;
9720 /* Handle range initializers either at this level or anywhere higher
9721 in the designator stack. */
9722 if (constructor_range_stack)
9724 struct constructor_range_stack *p, *range_stack;
9725 int finish = 0;
9727 range_stack = constructor_range_stack;
9728 constructor_range_stack = 0;
9729 while (constructor_stack != range_stack->stack)
9731 gcc_assert (constructor_stack->implicit);
9732 process_init_element (loc,
9733 pop_init_level (loc, 1,
9734 braced_init_obstack,
9735 last_init_list_comma),
9736 true, braced_init_obstack);
9738 for (p = range_stack;
9739 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9740 p = p->prev)
9742 gcc_assert (constructor_stack->implicit);
9743 process_init_element (loc,
9744 pop_init_level (loc, 1,
9745 braced_init_obstack,
9746 last_init_list_comma),
9747 true, braced_init_obstack);
9750 p->index = size_binop_loc (input_location,
9751 PLUS_EXPR, p->index, bitsize_one_node);
9752 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9753 finish = 1;
9755 while (1)
9757 constructor_index = p->index;
9758 constructor_fields = p->fields;
9759 if (finish && p->range_end && p->index == p->range_start)
9761 finish = 0;
9762 p->prev = 0;
9764 p = p->next;
9765 if (!p)
9766 break;
9767 finish_implicit_inits (loc, braced_init_obstack);
9768 push_init_level (loc, 2, braced_init_obstack);
9769 p->stack = constructor_stack;
9770 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9771 p->index = p->range_start;
9774 if (!finish)
9775 constructor_range_stack = range_stack;
9776 continue;
9779 break;
9782 constructor_range_stack = 0;
9785 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9786 (guaranteed to be 'volatile' or null) and ARGS (represented using
9787 an ASM_EXPR node). */
9788 tree
9789 build_asm_stmt (tree cv_qualifier, tree args)
9791 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9792 ASM_VOLATILE_P (args) = 1;
9793 return add_stmt (args);
9796 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9797 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9798 SIMPLE indicates whether there was anything at all after the
9799 string in the asm expression -- asm("blah") and asm("blah" : )
9800 are subtly different. We use a ASM_EXPR node to represent this. */
9801 tree
9802 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9803 tree clobbers, tree labels, bool simple)
9805 tree tail;
9806 tree args;
9807 int i;
9808 const char *constraint;
9809 const char **oconstraints;
9810 bool allows_mem, allows_reg, is_inout;
9811 int ninputs, noutputs;
9813 ninputs = list_length (inputs);
9814 noutputs = list_length (outputs);
9815 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9817 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9819 /* Remove output conversions that change the type but not the mode. */
9820 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9822 tree output = TREE_VALUE (tail);
9824 output = c_fully_fold (output, false, NULL);
9826 /* ??? Really, this should not be here. Users should be using a
9827 proper lvalue, dammit. But there's a long history of using casts
9828 in the output operands. In cases like longlong.h, this becomes a
9829 primitive form of typechecking -- if the cast can be removed, then
9830 the output operand had a type of the proper width; otherwise we'll
9831 get an error. Gross, but ... */
9832 STRIP_NOPS (output);
9834 if (!lvalue_or_else (loc, output, lv_asm))
9835 output = error_mark_node;
9837 if (output != error_mark_node
9838 && (TREE_READONLY (output)
9839 || TYPE_READONLY (TREE_TYPE (output))
9840 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9841 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9842 readonly_error (loc, output, lv_asm);
9844 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9845 oconstraints[i] = constraint;
9847 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9848 &allows_mem, &allows_reg, &is_inout))
9850 /* If the operand is going to end up in memory,
9851 mark it addressable. */
9852 if (!allows_reg && !c_mark_addressable (output))
9853 output = error_mark_node;
9854 if (!(!allows_reg && allows_mem)
9855 && output != error_mark_node
9856 && VOID_TYPE_P (TREE_TYPE (output)))
9858 error_at (loc, "invalid use of void expression");
9859 output = error_mark_node;
9862 else
9863 output = error_mark_node;
9865 TREE_VALUE (tail) = output;
9868 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9870 tree input;
9872 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9873 input = TREE_VALUE (tail);
9875 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9876 oconstraints, &allows_mem, &allows_reg))
9878 /* If the operand is going to end up in memory,
9879 mark it addressable. */
9880 if (!allows_reg && allows_mem)
9882 input = c_fully_fold (input, false, NULL);
9884 /* Strip the nops as we allow this case. FIXME, this really
9885 should be rejected or made deprecated. */
9886 STRIP_NOPS (input);
9887 if (!c_mark_addressable (input))
9888 input = error_mark_node;
9890 else
9892 struct c_expr expr;
9893 memset (&expr, 0, sizeof (expr));
9894 expr.value = input;
9895 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9896 input = c_fully_fold (expr.value, false, NULL);
9898 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9900 error_at (loc, "invalid use of void expression");
9901 input = error_mark_node;
9905 else
9906 input = error_mark_node;
9908 TREE_VALUE (tail) = input;
9911 /* ASMs with labels cannot have outputs. This should have been
9912 enforced by the parser. */
9913 gcc_assert (outputs == NULL || labels == NULL);
9915 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9917 /* asm statements without outputs, including simple ones, are treated
9918 as volatile. */
9919 ASM_INPUT_P (args) = simple;
9920 ASM_VOLATILE_P (args) = (noutputs == 0);
9922 return args;
9925 /* Generate a goto statement to LABEL. LOC is the location of the
9926 GOTO. */
9928 tree
9929 c_finish_goto_label (location_t loc, tree label)
9931 tree decl = lookup_label_for_goto (loc, label);
9932 if (!decl)
9933 return NULL_TREE;
9934 TREE_USED (decl) = 1;
9936 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
9937 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9938 SET_EXPR_LOCATION (t, loc);
9939 return add_stmt (t);
9943 /* Generate a computed goto statement to EXPR. LOC is the location of
9944 the GOTO. */
9946 tree
9947 c_finish_goto_ptr (location_t loc, tree expr)
9949 tree t;
9950 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9951 expr = c_fully_fold (expr, false, NULL);
9952 expr = convert (ptr_type_node, expr);
9953 t = build1 (GOTO_EXPR, void_type_node, expr);
9954 SET_EXPR_LOCATION (t, loc);
9955 return add_stmt (t);
9958 /* Generate a C `return' statement. RETVAL is the expression for what
9959 to return, or a null pointer for `return;' with no value. LOC is
9960 the location of the return statement, or the location of the expression,
9961 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9962 is the original type of RETVAL. */
9964 tree
9965 c_finish_return (location_t loc, tree retval, tree origtype)
9967 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9968 bool no_warning = false;
9969 bool npc = false;
9970 size_t rank = 0;
9972 /* Use the expansion point to handle cases such as returning NULL
9973 in a function returning void. */
9974 source_location xloc = expansion_point_location_if_in_system_header (loc);
9976 if (TREE_THIS_VOLATILE (current_function_decl))
9977 warning_at (xloc, 0,
9978 "function declared %<noreturn%> has a %<return%> statement");
9980 if (flag_cilkplus && contains_array_notation_expr (retval))
9982 /* Array notations are allowed in a return statement if it is inside a
9983 built-in array notation reduction function. */
9984 if (!find_rank (loc, retval, retval, false, &rank))
9985 return error_mark_node;
9986 if (rank >= 1)
9988 error_at (loc, "array notation expression cannot be used as a "
9989 "return value");
9990 return error_mark_node;
9993 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9995 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9996 "allowed");
9997 return error_mark_node;
9999 if (retval)
10001 tree semantic_type = NULL_TREE;
10002 npc = null_pointer_constant_p (retval);
10003 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10005 semantic_type = TREE_TYPE (retval);
10006 retval = TREE_OPERAND (retval, 0);
10008 retval = c_fully_fold (retval, false, NULL);
10009 if (semantic_type)
10010 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10013 if (!retval)
10015 current_function_returns_null = 1;
10016 if ((warn_return_type || flag_isoc99)
10017 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10019 bool warned_here;
10020 if (flag_isoc99)
10021 warned_here = pedwarn
10022 (loc, 0,
10023 "%<return%> with no value, in function returning non-void");
10024 else
10025 warned_here = warning_at
10026 (loc, OPT_Wreturn_type,
10027 "%<return%> with no value, in function returning non-void");
10028 no_warning = true;
10029 if (warned_here)
10030 inform (DECL_SOURCE_LOCATION (current_function_decl),
10031 "declared here");
10034 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10036 current_function_returns_null = 1;
10037 bool warned_here;
10038 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10039 warned_here = pedwarn
10040 (xloc, 0,
10041 "%<return%> with a value, in function returning void");
10042 else
10043 warned_here = pedwarn
10044 (xloc, OPT_Wpedantic, "ISO C forbids "
10045 "%<return%> with expression, in function returning void");
10046 if (warned_here)
10047 inform (DECL_SOURCE_LOCATION (current_function_decl),
10048 "declared here");
10050 else
10052 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10053 retval, origtype, ic_return,
10054 npc, NULL_TREE, NULL_TREE, 0);
10055 tree res = DECL_RESULT (current_function_decl);
10056 tree inner;
10057 bool save;
10059 current_function_returns_value = 1;
10060 if (t == error_mark_node)
10061 return NULL_TREE;
10063 save = in_late_binary_op;
10064 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10065 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10066 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10067 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10068 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10069 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10070 in_late_binary_op = true;
10071 inner = t = convert (TREE_TYPE (res), t);
10072 in_late_binary_op = save;
10074 /* Strip any conversions, additions, and subtractions, and see if
10075 we are returning the address of a local variable. Warn if so. */
10076 while (1)
10078 switch (TREE_CODE (inner))
10080 CASE_CONVERT:
10081 case NON_LVALUE_EXPR:
10082 case PLUS_EXPR:
10083 case POINTER_PLUS_EXPR:
10084 inner = TREE_OPERAND (inner, 0);
10085 continue;
10087 case MINUS_EXPR:
10088 /* If the second operand of the MINUS_EXPR has a pointer
10089 type (or is converted from it), this may be valid, so
10090 don't give a warning. */
10092 tree op1 = TREE_OPERAND (inner, 1);
10094 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10095 && (CONVERT_EXPR_P (op1)
10096 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10097 op1 = TREE_OPERAND (op1, 0);
10099 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10100 break;
10102 inner = TREE_OPERAND (inner, 0);
10103 continue;
10106 case ADDR_EXPR:
10107 inner = TREE_OPERAND (inner, 0);
10109 while (REFERENCE_CLASS_P (inner)
10110 && !INDIRECT_REF_P (inner))
10111 inner = TREE_OPERAND (inner, 0);
10113 if (DECL_P (inner)
10114 && !DECL_EXTERNAL (inner)
10115 && !TREE_STATIC (inner)
10116 && DECL_CONTEXT (inner) == current_function_decl)
10118 if (TREE_CODE (inner) == LABEL_DECL)
10119 warning_at (loc, OPT_Wreturn_local_addr,
10120 "function returns address of label");
10121 else
10123 warning_at (loc, OPT_Wreturn_local_addr,
10124 "function returns address of local variable");
10125 tree zero = build_zero_cst (TREE_TYPE (res));
10126 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10129 break;
10131 default:
10132 break;
10135 break;
10138 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10139 SET_EXPR_LOCATION (retval, loc);
10141 if (warn_sequence_point)
10142 verify_sequence_points (retval);
10145 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10146 TREE_NO_WARNING (ret_stmt) |= no_warning;
10147 return add_stmt (ret_stmt);
10150 struct c_switch {
10151 /* The SWITCH_EXPR being built. */
10152 tree switch_expr;
10154 /* The original type of the testing expression, i.e. before the
10155 default conversion is applied. */
10156 tree orig_type;
10158 /* A splay-tree mapping the low element of a case range to the high
10159 element, or NULL_TREE if there is no high element. Used to
10160 determine whether or not a new case label duplicates an old case
10161 label. We need a tree, rather than simply a hash table, because
10162 of the GNU case range extension. */
10163 splay_tree cases;
10165 /* The bindings at the point of the switch. This is used for
10166 warnings crossing decls when branching to a case label. */
10167 struct c_spot_bindings *bindings;
10169 /* The next node on the stack. */
10170 struct c_switch *next;
10172 /* Remember whether the controlling expression had boolean type
10173 before integer promotions for the sake of -Wswitch-bool. */
10174 bool bool_cond_p;
10176 /* Remember whether there was a case value that is outside the
10177 range of the ORIG_TYPE. */
10178 bool outside_range_p;
10181 /* A stack of the currently active switch statements. The innermost
10182 switch statement is on the top of the stack. There is no need to
10183 mark the stack for garbage collection because it is only active
10184 during the processing of the body of a function, and we never
10185 collect at that point. */
10187 struct c_switch *c_switch_stack;
10189 /* Start a C switch statement, testing expression EXP. Return the new
10190 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10191 SWITCH_COND_LOC is the location of the switch's condition.
10192 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10194 tree
10195 c_start_case (location_t switch_loc,
10196 location_t switch_cond_loc,
10197 tree exp, bool explicit_cast_p)
10199 tree orig_type = error_mark_node;
10200 bool bool_cond_p = false;
10201 struct c_switch *cs;
10203 if (exp != error_mark_node)
10205 orig_type = TREE_TYPE (exp);
10207 if (!INTEGRAL_TYPE_P (orig_type))
10209 if (orig_type != error_mark_node)
10211 error_at (switch_cond_loc, "switch quantity not an integer");
10212 orig_type = error_mark_node;
10214 exp = integer_zero_node;
10216 else
10218 tree type = TYPE_MAIN_VARIANT (orig_type);
10219 tree e = exp;
10221 /* Warn if the condition has boolean value. */
10222 while (TREE_CODE (e) == COMPOUND_EXPR)
10223 e = TREE_OPERAND (e, 1);
10225 if ((TREE_CODE (type) == BOOLEAN_TYPE
10226 || truth_value_p (TREE_CODE (e)))
10227 /* Explicit cast to int suppresses this warning. */
10228 && !(TREE_CODE (type) == INTEGER_TYPE
10229 && explicit_cast_p))
10230 bool_cond_p = true;
10232 if (!in_system_header_at (input_location)
10233 && (type == long_integer_type_node
10234 || type == long_unsigned_type_node))
10235 warning_at (switch_cond_loc,
10236 OPT_Wtraditional, "%<long%> switch expression not "
10237 "converted to %<int%> in ISO C");
10239 exp = c_fully_fold (exp, false, NULL);
10240 exp = default_conversion (exp);
10242 if (warn_sequence_point)
10243 verify_sequence_points (exp);
10247 /* Add this new SWITCH_EXPR to the stack. */
10248 cs = XNEW (struct c_switch);
10249 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10250 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10251 cs->orig_type = orig_type;
10252 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10253 cs->bindings = c_get_switch_bindings ();
10254 cs->bool_cond_p = bool_cond_p;
10255 cs->outside_range_p = false;
10256 cs->next = c_switch_stack;
10257 c_switch_stack = cs;
10259 return add_stmt (cs->switch_expr);
10262 /* Process a case label at location LOC. */
10264 tree
10265 do_case (location_t loc, tree low_value, tree high_value)
10267 tree label = NULL_TREE;
10269 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10271 low_value = c_fully_fold (low_value, false, NULL);
10272 if (TREE_CODE (low_value) == INTEGER_CST)
10273 pedwarn (loc, OPT_Wpedantic,
10274 "case label is not an integer constant expression");
10277 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10279 high_value = c_fully_fold (high_value, false, NULL);
10280 if (TREE_CODE (high_value) == INTEGER_CST)
10281 pedwarn (input_location, OPT_Wpedantic,
10282 "case label is not an integer constant expression");
10285 if (c_switch_stack == NULL)
10287 if (low_value)
10288 error_at (loc, "case label not within a switch statement");
10289 else
10290 error_at (loc, "%<default%> label not within a switch statement");
10291 return NULL_TREE;
10294 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10295 EXPR_LOCATION (c_switch_stack->switch_expr),
10296 loc))
10297 return NULL_TREE;
10299 label = c_add_case_label (loc, c_switch_stack->cases,
10300 SWITCH_COND (c_switch_stack->switch_expr),
10301 c_switch_stack->orig_type,
10302 low_value, high_value,
10303 &c_switch_stack->outside_range_p);
10304 if (label == error_mark_node)
10305 label = NULL_TREE;
10306 return label;
10309 /* Finish the switch statement. TYPE is the original type of the
10310 controlling expression of the switch, or NULL_TREE. */
10312 void
10313 c_finish_case (tree body, tree type)
10315 struct c_switch *cs = c_switch_stack;
10316 location_t switch_location;
10318 SWITCH_BODY (cs->switch_expr) = body;
10320 /* Emit warnings as needed. */
10321 switch_location = EXPR_LOCATION (cs->switch_expr);
10322 c_do_switch_warnings (cs->cases, switch_location,
10323 type ? type : TREE_TYPE (cs->switch_expr),
10324 SWITCH_COND (cs->switch_expr),
10325 cs->bool_cond_p, cs->outside_range_p);
10327 /* Pop the stack. */
10328 c_switch_stack = cs->next;
10329 splay_tree_delete (cs->cases);
10330 c_release_switch_bindings (cs->bindings);
10331 XDELETE (cs);
10334 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10335 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10336 may be null. */
10338 void
10339 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10340 tree else_block)
10342 tree stmt;
10344 /* If the condition has array notations, then the rank of the then_block and
10345 else_block must be either 0 or be equal to the rank of the condition. If
10346 the condition does not have array notations then break them up as it is
10347 broken up in a normal expression. */
10348 if (flag_cilkplus && contains_array_notation_expr (cond))
10350 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10351 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10352 return;
10353 if (then_block
10354 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10355 return;
10356 if (else_block
10357 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10358 return;
10359 if (cond_rank != then_rank && then_rank != 0)
10361 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10362 " and the then-block");
10363 return;
10365 else if (cond_rank != else_rank && else_rank != 0)
10367 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10368 " and the else-block");
10369 return;
10373 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10374 SET_EXPR_LOCATION (stmt, if_locus);
10375 add_stmt (stmt);
10378 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10379 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10380 is false for DO loops. INCR is the FOR increment expression. BODY is
10381 the statement controlled by the loop. BLAB is the break label. CLAB is
10382 the continue label. Everything is allowed to be NULL. */
10384 void
10385 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10386 tree blab, tree clab, bool cond_is_first)
10388 tree entry = NULL, exit = NULL, t;
10390 /* In theory could forbid cilk spawn for loop increment expression,
10391 but it should work just fine. */
10393 /* If the condition is zero don't generate a loop construct. */
10394 if (cond && integer_zerop (cond))
10396 if (cond_is_first)
10398 t = build_and_jump (&blab);
10399 SET_EXPR_LOCATION (t, start_locus);
10400 add_stmt (t);
10403 else
10405 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10407 /* If we have an exit condition, then we build an IF with gotos either
10408 out of the loop, or to the top of it. If there's no exit condition,
10409 then we just build a jump back to the top. */
10410 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10412 if (cond && !integer_nonzerop (cond))
10414 /* Canonicalize the loop condition to the end. This means
10415 generating a branch to the loop condition. Reuse the
10416 continue label, if possible. */
10417 if (cond_is_first)
10419 if (incr || !clab)
10421 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10422 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10424 else
10425 t = build1 (GOTO_EXPR, void_type_node, clab);
10426 SET_EXPR_LOCATION (t, start_locus);
10427 add_stmt (t);
10430 t = build_and_jump (&blab);
10431 if (cond_is_first)
10432 exit = fold_build3_loc (start_locus,
10433 COND_EXPR, void_type_node, cond, exit, t);
10434 else
10435 exit = fold_build3_loc (input_location,
10436 COND_EXPR, void_type_node, cond, exit, t);
10438 else
10440 /* For the backward-goto's location of an unconditional loop
10441 use the beginning of the body, or, if there is none, the
10442 top of the loop. */
10443 location_t loc = EXPR_LOCATION (expr_first (body));
10444 if (loc == UNKNOWN_LOCATION)
10445 loc = start_locus;
10446 SET_EXPR_LOCATION (exit, loc);
10449 add_stmt (top);
10452 if (body)
10453 add_stmt (body);
10454 if (clab)
10455 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10456 if (incr)
10457 add_stmt (incr);
10458 if (entry)
10459 add_stmt (entry);
10460 if (exit)
10461 add_stmt (exit);
10462 if (blab)
10463 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10466 tree
10467 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10469 bool skip;
10470 tree label = *label_p;
10472 /* In switch statements break is sometimes stylistically used after
10473 a return statement. This can lead to spurious warnings about
10474 control reaching the end of a non-void function when it is
10475 inlined. Note that we are calling block_may_fallthru with
10476 language specific tree nodes; this works because
10477 block_may_fallthru returns true when given something it does not
10478 understand. */
10479 skip = !block_may_fallthru (cur_stmt_list);
10481 if (!label)
10483 if (!skip)
10484 *label_p = label = create_artificial_label (loc);
10486 else if (TREE_CODE (label) == LABEL_DECL)
10488 else switch (TREE_INT_CST_LOW (label))
10490 case 0:
10491 if (is_break)
10492 error_at (loc, "break statement not within loop or switch");
10493 else
10494 error_at (loc, "continue statement not within a loop");
10495 return NULL_TREE;
10497 case 1:
10498 gcc_assert (is_break);
10499 error_at (loc, "break statement used with OpenMP for loop");
10500 return NULL_TREE;
10502 case 2:
10503 if (is_break)
10504 error ("break statement within %<#pragma simd%> loop body");
10505 else
10506 error ("continue statement within %<#pragma simd%> loop body");
10507 return NULL_TREE;
10509 default:
10510 gcc_unreachable ();
10513 if (skip)
10514 return NULL_TREE;
10516 if (!is_break)
10517 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10519 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10522 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10524 static void
10525 emit_side_effect_warnings (location_t loc, tree expr)
10527 if (expr == error_mark_node)
10529 else if (!TREE_SIDE_EFFECTS (expr))
10531 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10532 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10534 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10536 tree r = expr;
10537 location_t cloc = loc;
10538 while (TREE_CODE (r) == COMPOUND_EXPR)
10540 if (EXPR_HAS_LOCATION (r))
10541 cloc = EXPR_LOCATION (r);
10542 r = TREE_OPERAND (r, 1);
10544 if (!TREE_SIDE_EFFECTS (r)
10545 && !VOID_TYPE_P (TREE_TYPE (r))
10546 && !CONVERT_EXPR_P (r)
10547 && !TREE_NO_WARNING (r)
10548 && !TREE_NO_WARNING (expr))
10549 warning_at (cloc, OPT_Wunused_value,
10550 "right-hand operand of comma expression has no effect");
10552 else
10553 warn_if_unused_value (expr, loc);
10556 /* Process an expression as if it were a complete statement. Emit
10557 diagnostics, but do not call ADD_STMT. LOC is the location of the
10558 statement. */
10560 tree
10561 c_process_expr_stmt (location_t loc, tree expr)
10563 tree exprv;
10565 if (!expr)
10566 return NULL_TREE;
10568 expr = c_fully_fold (expr, false, NULL);
10570 if (warn_sequence_point)
10571 verify_sequence_points (expr);
10573 if (TREE_TYPE (expr) != error_mark_node
10574 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10575 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10576 error_at (loc, "expression statement has incomplete type");
10578 /* If we're not processing a statement expression, warn about unused values.
10579 Warnings for statement expressions will be emitted later, once we figure
10580 out which is the result. */
10581 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10582 && warn_unused_value)
10583 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10585 exprv = expr;
10586 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10587 exprv = TREE_OPERAND (exprv, 1);
10588 while (CONVERT_EXPR_P (exprv))
10589 exprv = TREE_OPERAND (exprv, 0);
10590 if (DECL_P (exprv)
10591 || handled_component_p (exprv)
10592 || TREE_CODE (exprv) == ADDR_EXPR)
10593 mark_exp_read (exprv);
10595 /* If the expression is not of a type to which we cannot assign a line
10596 number, wrap the thing in a no-op NOP_EXPR. */
10597 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10599 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10600 SET_EXPR_LOCATION (expr, loc);
10603 return expr;
10606 /* Emit an expression as a statement. LOC is the location of the
10607 expression. */
10609 tree
10610 c_finish_expr_stmt (location_t loc, tree expr)
10612 if (expr)
10613 return add_stmt (c_process_expr_stmt (loc, expr));
10614 else
10615 return NULL;
10618 /* Do the opposite and emit a statement as an expression. To begin,
10619 create a new binding level and return it. */
10621 tree
10622 c_begin_stmt_expr (void)
10624 tree ret;
10626 /* We must force a BLOCK for this level so that, if it is not expanded
10627 later, there is a way to turn off the entire subtree of blocks that
10628 are contained in it. */
10629 keep_next_level ();
10630 ret = c_begin_compound_stmt (true);
10632 c_bindings_start_stmt_expr (c_switch_stack == NULL
10633 ? NULL
10634 : c_switch_stack->bindings);
10636 /* Mark the current statement list as belonging to a statement list. */
10637 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10639 return ret;
10642 /* LOC is the location of the compound statement to which this body
10643 belongs. */
10645 tree
10646 c_finish_stmt_expr (location_t loc, tree body)
10648 tree last, type, tmp, val;
10649 tree *last_p;
10651 body = c_end_compound_stmt (loc, body, true);
10653 c_bindings_end_stmt_expr (c_switch_stack == NULL
10654 ? NULL
10655 : c_switch_stack->bindings);
10657 /* Locate the last statement in BODY. See c_end_compound_stmt
10658 about always returning a BIND_EXPR. */
10659 last_p = &BIND_EXPR_BODY (body);
10660 last = BIND_EXPR_BODY (body);
10662 continue_searching:
10663 if (TREE_CODE (last) == STATEMENT_LIST)
10665 tree_stmt_iterator i;
10667 /* This can happen with degenerate cases like ({ }). No value. */
10668 if (!TREE_SIDE_EFFECTS (last))
10669 return body;
10671 /* If we're supposed to generate side effects warnings, process
10672 all of the statements except the last. */
10673 if (warn_unused_value)
10675 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10677 location_t tloc;
10678 tree t = tsi_stmt (i);
10680 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10681 emit_side_effect_warnings (tloc, t);
10684 else
10685 i = tsi_last (last);
10686 last_p = tsi_stmt_ptr (i);
10687 last = *last_p;
10690 /* If the end of the list is exception related, then the list was split
10691 by a call to push_cleanup. Continue searching. */
10692 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10693 || TREE_CODE (last) == TRY_CATCH_EXPR)
10695 last_p = &TREE_OPERAND (last, 0);
10696 last = *last_p;
10697 goto continue_searching;
10700 if (last == error_mark_node)
10701 return last;
10703 /* In the case that the BIND_EXPR is not necessary, return the
10704 expression out from inside it. */
10705 if (last == BIND_EXPR_BODY (body)
10706 && BIND_EXPR_VARS (body) == NULL)
10708 /* Even if this looks constant, do not allow it in a constant
10709 expression. */
10710 last = c_wrap_maybe_const (last, true);
10711 /* Do not warn if the return value of a statement expression is
10712 unused. */
10713 TREE_NO_WARNING (last) = 1;
10714 return last;
10717 /* Extract the type of said expression. */
10718 type = TREE_TYPE (last);
10720 /* If we're not returning a value at all, then the BIND_EXPR that
10721 we already have is a fine expression to return. */
10722 if (!type || VOID_TYPE_P (type))
10723 return body;
10725 /* Now that we've located the expression containing the value, it seems
10726 silly to make voidify_wrapper_expr repeat the process. Create a
10727 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10728 tmp = create_tmp_var_raw (type);
10730 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10731 tree_expr_nonnegative_p giving up immediately. */
10732 val = last;
10733 if (TREE_CODE (val) == NOP_EXPR
10734 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10735 val = TREE_OPERAND (val, 0);
10737 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10738 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10741 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10742 SET_EXPR_LOCATION (t, loc);
10743 return t;
10747 /* Begin and end compound statements. This is as simple as pushing
10748 and popping new statement lists from the tree. */
10750 tree
10751 c_begin_compound_stmt (bool do_scope)
10753 tree stmt = push_stmt_list ();
10754 if (do_scope)
10755 push_scope ();
10756 return stmt;
10759 /* End a compound statement. STMT is the statement. LOC is the
10760 location of the compound statement-- this is usually the location
10761 of the opening brace. */
10763 tree
10764 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10766 tree block = NULL;
10768 if (do_scope)
10770 if (c_dialect_objc ())
10771 objc_clear_super_receiver ();
10772 block = pop_scope ();
10775 stmt = pop_stmt_list (stmt);
10776 stmt = c_build_bind_expr (loc, block, stmt);
10778 /* If this compound statement is nested immediately inside a statement
10779 expression, then force a BIND_EXPR to be created. Otherwise we'll
10780 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10781 STATEMENT_LISTs merge, and thus we can lose track of what statement
10782 was really last. */
10783 if (building_stmt_list_p ()
10784 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10785 && TREE_CODE (stmt) != BIND_EXPR)
10787 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10788 TREE_SIDE_EFFECTS (stmt) = 1;
10789 SET_EXPR_LOCATION (stmt, loc);
10792 return stmt;
10795 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10796 when the current scope is exited. EH_ONLY is true when this is not
10797 meant to apply to normal control flow transfer. */
10799 void
10800 push_cleanup (tree decl, tree cleanup, bool eh_only)
10802 enum tree_code code;
10803 tree stmt, list;
10804 bool stmt_expr;
10806 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10807 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10808 add_stmt (stmt);
10809 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10810 list = push_stmt_list ();
10811 TREE_OPERAND (stmt, 0) = list;
10812 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10815 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10816 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10818 static tree
10819 build_vec_cmp (tree_code code, tree type,
10820 tree arg0, tree arg1)
10822 tree zero_vec = build_zero_cst (type);
10823 tree minus_one_vec = build_minus_one_cst (type);
10824 tree cmp_type = build_same_sized_truth_vector_type (type);
10825 tree cmp = build2 (code, cmp_type, arg0, arg1);
10826 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10829 /* Build a binary-operation expression without default conversions.
10830 CODE is the kind of expression to build.
10831 LOCATION is the operator's location.
10832 This function differs from `build' in several ways:
10833 the data type of the result is computed and recorded in it,
10834 warnings are generated if arg data types are invalid,
10835 special handling for addition and subtraction of pointers is known,
10836 and some optimization is done (operations on narrow ints
10837 are done in the narrower type when that gives the same result).
10838 Constant folding is also done before the result is returned.
10840 Note that the operands will never have enumeral types, or function
10841 or array types, because either they will have the default conversions
10842 performed or they have both just been converted to some other type in which
10843 the arithmetic is to be done. */
10845 tree
10846 build_binary_op (location_t location, enum tree_code code,
10847 tree orig_op0, tree orig_op1, bool convert_p)
10849 tree type0, type1, orig_type0, orig_type1;
10850 tree eptype;
10851 enum tree_code code0, code1;
10852 tree op0, op1;
10853 tree ret = error_mark_node;
10854 const char *invalid_op_diag;
10855 bool op0_int_operands, op1_int_operands;
10856 bool int_const, int_const_or_overflow, int_operands;
10858 /* Expression code to give to the expression when it is built.
10859 Normally this is CODE, which is what the caller asked for,
10860 but in some special cases we change it. */
10861 enum tree_code resultcode = code;
10863 /* Data type in which the computation is to be performed.
10864 In the simplest cases this is the common type of the arguments. */
10865 tree result_type = NULL;
10867 /* When the computation is in excess precision, the type of the
10868 final EXCESS_PRECISION_EXPR. */
10869 tree semantic_result_type = NULL;
10871 /* Nonzero means operands have already been type-converted
10872 in whatever way is necessary.
10873 Zero means they need to be converted to RESULT_TYPE. */
10874 int converted = 0;
10876 /* Nonzero means create the expression with this type, rather than
10877 RESULT_TYPE. */
10878 tree build_type = NULL_TREE;
10880 /* Nonzero means after finally constructing the expression
10881 convert it to this type. */
10882 tree final_type = NULL_TREE;
10884 /* Nonzero if this is an operation like MIN or MAX which can
10885 safely be computed in short if both args are promoted shorts.
10886 Also implies COMMON.
10887 -1 indicates a bitwise operation; this makes a difference
10888 in the exact conditions for when it is safe to do the operation
10889 in a narrower mode. */
10890 int shorten = 0;
10892 /* Nonzero if this is a comparison operation;
10893 if both args are promoted shorts, compare the original shorts.
10894 Also implies COMMON. */
10895 int short_compare = 0;
10897 /* Nonzero if this is a right-shift operation, which can be computed on the
10898 original short and then promoted if the operand is a promoted short. */
10899 int short_shift = 0;
10901 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10902 int common = 0;
10904 /* True means types are compatible as far as ObjC is concerned. */
10905 bool objc_ok;
10907 /* True means this is an arithmetic operation that may need excess
10908 precision. */
10909 bool may_need_excess_precision;
10911 /* True means this is a boolean operation that converts both its
10912 operands to truth-values. */
10913 bool boolean_op = false;
10915 /* Remember whether we're doing / or %. */
10916 bool doing_div_or_mod = false;
10918 /* Remember whether we're doing << or >>. */
10919 bool doing_shift = false;
10921 /* Tree holding instrumentation expression. */
10922 tree instrument_expr = NULL;
10924 if (location == UNKNOWN_LOCATION)
10925 location = input_location;
10927 op0 = orig_op0;
10928 op1 = orig_op1;
10930 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10931 if (op0_int_operands)
10932 op0 = remove_c_maybe_const_expr (op0);
10933 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10934 if (op1_int_operands)
10935 op1 = remove_c_maybe_const_expr (op1);
10936 int_operands = (op0_int_operands && op1_int_operands);
10937 if (int_operands)
10939 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10940 && TREE_CODE (orig_op1) == INTEGER_CST);
10941 int_const = (int_const_or_overflow
10942 && !TREE_OVERFLOW (orig_op0)
10943 && !TREE_OVERFLOW (orig_op1));
10945 else
10946 int_const = int_const_or_overflow = false;
10948 /* Do not apply default conversion in mixed vector/scalar expression. */
10949 if (convert_p
10950 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10952 op0 = default_conversion (op0);
10953 op1 = default_conversion (op1);
10956 /* When Cilk Plus is enabled and there are array notations inside op0, then
10957 we check to see if there are builtin array notation functions. If
10958 so, then we take on the type of the array notation inside it. */
10959 if (flag_cilkplus && contains_array_notation_expr (op0))
10960 orig_type0 = type0 = find_correct_array_notation_type (op0);
10961 else
10962 orig_type0 = type0 = TREE_TYPE (op0);
10964 if (flag_cilkplus && contains_array_notation_expr (op1))
10965 orig_type1 = type1 = find_correct_array_notation_type (op1);
10966 else
10967 orig_type1 = type1 = TREE_TYPE (op1);
10969 /* The expression codes of the data types of the arguments tell us
10970 whether the arguments are integers, floating, pointers, etc. */
10971 code0 = TREE_CODE (type0);
10972 code1 = TREE_CODE (type1);
10974 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10975 STRIP_TYPE_NOPS (op0);
10976 STRIP_TYPE_NOPS (op1);
10978 /* If an error was already reported for one of the arguments,
10979 avoid reporting another error. */
10981 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10982 return error_mark_node;
10984 if (code0 == POINTER_TYPE
10985 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10986 return error_mark_node;
10988 if (code1 == POINTER_TYPE
10989 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10990 return error_mark_node;
10992 if ((invalid_op_diag
10993 = targetm.invalid_binary_op (code, type0, type1)))
10995 error_at (location, invalid_op_diag);
10996 return error_mark_node;
10999 switch (code)
11001 case PLUS_EXPR:
11002 case MINUS_EXPR:
11003 case MULT_EXPR:
11004 case TRUNC_DIV_EXPR:
11005 case CEIL_DIV_EXPR:
11006 case FLOOR_DIV_EXPR:
11007 case ROUND_DIV_EXPR:
11008 case EXACT_DIV_EXPR:
11009 may_need_excess_precision = true;
11010 break;
11011 default:
11012 may_need_excess_precision = false;
11013 break;
11015 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11017 op0 = TREE_OPERAND (op0, 0);
11018 type0 = TREE_TYPE (op0);
11020 else if (may_need_excess_precision
11021 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11023 type0 = eptype;
11024 op0 = convert (eptype, op0);
11026 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11028 op1 = TREE_OPERAND (op1, 0);
11029 type1 = TREE_TYPE (op1);
11031 else if (may_need_excess_precision
11032 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11034 type1 = eptype;
11035 op1 = convert (eptype, op1);
11038 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11040 /* In case when one of the operands of the binary operation is
11041 a vector and another is a scalar -- convert scalar to vector. */
11042 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11044 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11045 true);
11047 switch (convert_flag)
11049 case stv_error:
11050 return error_mark_node;
11051 case stv_firstarg:
11053 bool maybe_const = true;
11054 tree sc;
11055 sc = c_fully_fold (op0, false, &maybe_const);
11056 sc = save_expr (sc);
11057 sc = convert (TREE_TYPE (type1), sc);
11058 op0 = build_vector_from_val (type1, sc);
11059 if (!maybe_const)
11060 op0 = c_wrap_maybe_const (op0, true);
11061 orig_type0 = type0 = TREE_TYPE (op0);
11062 code0 = TREE_CODE (type0);
11063 converted = 1;
11064 break;
11066 case stv_secondarg:
11068 bool maybe_const = true;
11069 tree sc;
11070 sc = c_fully_fold (op1, false, &maybe_const);
11071 sc = save_expr (sc);
11072 sc = convert (TREE_TYPE (type0), sc);
11073 op1 = build_vector_from_val (type0, sc);
11074 if (!maybe_const)
11075 op1 = c_wrap_maybe_const (op1, true);
11076 orig_type1 = type1 = TREE_TYPE (op1);
11077 code1 = TREE_CODE (type1);
11078 converted = 1;
11079 break;
11081 default:
11082 break;
11086 switch (code)
11088 case PLUS_EXPR:
11089 /* Handle the pointer + int case. */
11090 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11092 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11093 goto return_build_binary_op;
11095 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11097 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11098 goto return_build_binary_op;
11100 else
11101 common = 1;
11102 break;
11104 case MINUS_EXPR:
11105 /* Subtraction of two similar pointers.
11106 We must subtract them as integers, then divide by object size. */
11107 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11108 && comp_target_types (location, type0, type1))
11110 ret = pointer_diff (location, op0, op1);
11111 goto return_build_binary_op;
11113 /* Handle pointer minus int. Just like pointer plus int. */
11114 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11116 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11117 goto return_build_binary_op;
11119 else
11120 common = 1;
11121 break;
11123 case MULT_EXPR:
11124 common = 1;
11125 break;
11127 case TRUNC_DIV_EXPR:
11128 case CEIL_DIV_EXPR:
11129 case FLOOR_DIV_EXPR:
11130 case ROUND_DIV_EXPR:
11131 case EXACT_DIV_EXPR:
11132 doing_div_or_mod = true;
11133 warn_for_div_by_zero (location, op1);
11135 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11136 || code0 == FIXED_POINT_TYPE
11137 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11138 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11139 || code1 == FIXED_POINT_TYPE
11140 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11142 enum tree_code tcode0 = code0, tcode1 = code1;
11144 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11145 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11146 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11147 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11149 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11150 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11151 resultcode = RDIV_EXPR;
11152 else
11153 /* Although it would be tempting to shorten always here, that
11154 loses on some targets, since the modulo instruction is
11155 undefined if the quotient can't be represented in the
11156 computation mode. We shorten only if unsigned or if
11157 dividing by something we know != -1. */
11158 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11159 || (TREE_CODE (op1) == INTEGER_CST
11160 && !integer_all_onesp (op1)));
11161 common = 1;
11163 break;
11165 case BIT_AND_EXPR:
11166 case BIT_IOR_EXPR:
11167 case BIT_XOR_EXPR:
11168 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11169 shorten = -1;
11170 /* Allow vector types which are not floating point types. */
11171 else if (code0 == VECTOR_TYPE
11172 && code1 == VECTOR_TYPE
11173 && !VECTOR_FLOAT_TYPE_P (type0)
11174 && !VECTOR_FLOAT_TYPE_P (type1))
11175 common = 1;
11176 break;
11178 case TRUNC_MOD_EXPR:
11179 case FLOOR_MOD_EXPR:
11180 doing_div_or_mod = true;
11181 warn_for_div_by_zero (location, op1);
11183 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11184 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11185 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11186 common = 1;
11187 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11189 /* Although it would be tempting to shorten always here, that loses
11190 on some targets, since the modulo instruction is undefined if the
11191 quotient can't be represented in the computation mode. We shorten
11192 only if unsigned or if dividing by something we know != -1. */
11193 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11194 || (TREE_CODE (op1) == INTEGER_CST
11195 && !integer_all_onesp (op1)));
11196 common = 1;
11198 break;
11200 case TRUTH_ANDIF_EXPR:
11201 case TRUTH_ORIF_EXPR:
11202 case TRUTH_AND_EXPR:
11203 case TRUTH_OR_EXPR:
11204 case TRUTH_XOR_EXPR:
11205 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11206 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11207 || code0 == FIXED_POINT_TYPE)
11208 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11209 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11210 || code1 == FIXED_POINT_TYPE))
11212 /* Result of these operations is always an int,
11213 but that does not mean the operands should be
11214 converted to ints! */
11215 result_type = integer_type_node;
11216 if (op0_int_operands)
11218 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11219 op0 = remove_c_maybe_const_expr (op0);
11221 else
11222 op0 = c_objc_common_truthvalue_conversion (location, op0);
11223 if (op1_int_operands)
11225 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11226 op1 = remove_c_maybe_const_expr (op1);
11228 else
11229 op1 = c_objc_common_truthvalue_conversion (location, op1);
11230 converted = 1;
11231 boolean_op = true;
11233 if (code == TRUTH_ANDIF_EXPR)
11235 int_const_or_overflow = (int_operands
11236 && TREE_CODE (orig_op0) == INTEGER_CST
11237 && (op0 == truthvalue_false_node
11238 || TREE_CODE (orig_op1) == INTEGER_CST));
11239 int_const = (int_const_or_overflow
11240 && !TREE_OVERFLOW (orig_op0)
11241 && (op0 == truthvalue_false_node
11242 || !TREE_OVERFLOW (orig_op1)));
11244 else if (code == TRUTH_ORIF_EXPR)
11246 int_const_or_overflow = (int_operands
11247 && TREE_CODE (orig_op0) == INTEGER_CST
11248 && (op0 == truthvalue_true_node
11249 || TREE_CODE (orig_op1) == INTEGER_CST));
11250 int_const = (int_const_or_overflow
11251 && !TREE_OVERFLOW (orig_op0)
11252 && (op0 == truthvalue_true_node
11253 || !TREE_OVERFLOW (orig_op1)));
11255 break;
11257 /* Shift operations: result has same type as first operand;
11258 always convert second operand to int.
11259 Also set SHORT_SHIFT if shifting rightward. */
11261 case RSHIFT_EXPR:
11262 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11263 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11264 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11265 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11267 result_type = type0;
11268 converted = 1;
11270 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11271 || code0 == VECTOR_TYPE)
11272 && code1 == INTEGER_TYPE)
11274 doing_shift = true;
11275 if (TREE_CODE (op1) == INTEGER_CST)
11277 if (tree_int_cst_sgn (op1) < 0)
11279 int_const = false;
11280 if (c_inhibit_evaluation_warnings == 0)
11281 warning_at (location, OPT_Wshift_count_negative,
11282 "right shift count is negative");
11284 else if (code0 == VECTOR_TYPE)
11286 if (compare_tree_int (op1,
11287 TYPE_PRECISION (TREE_TYPE (type0)))
11288 >= 0)
11290 int_const = false;
11291 if (c_inhibit_evaluation_warnings == 0)
11292 warning_at (location, OPT_Wshift_count_overflow,
11293 "right shift count >= width of vector element");
11296 else
11298 if (!integer_zerop (op1))
11299 short_shift = 1;
11301 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11303 int_const = false;
11304 if (c_inhibit_evaluation_warnings == 0)
11305 warning_at (location, OPT_Wshift_count_overflow,
11306 "right shift count >= width of type");
11311 /* Use the type of the value to be shifted. */
11312 result_type = type0;
11313 /* Avoid converting op1 to result_type later. */
11314 converted = 1;
11316 break;
11318 case LSHIFT_EXPR:
11319 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11320 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11321 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11322 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11324 result_type = type0;
11325 converted = 1;
11327 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11328 || code0 == VECTOR_TYPE)
11329 && code1 == INTEGER_TYPE)
11331 doing_shift = true;
11332 if (TREE_CODE (op0) == INTEGER_CST
11333 && tree_int_cst_sgn (op0) < 0)
11335 /* Don't reject a left shift of a negative value in a context
11336 where a constant expression is needed in C90. */
11337 if (flag_isoc99)
11338 int_const = false;
11339 if (c_inhibit_evaluation_warnings == 0)
11340 warning_at (location, OPT_Wshift_negative_value,
11341 "left shift of negative value");
11343 if (TREE_CODE (op1) == INTEGER_CST)
11345 if (tree_int_cst_sgn (op1) < 0)
11347 int_const = false;
11348 if (c_inhibit_evaluation_warnings == 0)
11349 warning_at (location, OPT_Wshift_count_negative,
11350 "left shift count is negative");
11352 else if (code0 == VECTOR_TYPE)
11354 if (compare_tree_int (op1,
11355 TYPE_PRECISION (TREE_TYPE (type0)))
11356 >= 0)
11358 int_const = false;
11359 if (c_inhibit_evaluation_warnings == 0)
11360 warning_at (location, OPT_Wshift_count_overflow,
11361 "left shift count >= width of vector element");
11364 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11366 int_const = false;
11367 if (c_inhibit_evaluation_warnings == 0)
11368 warning_at (location, OPT_Wshift_count_overflow,
11369 "left shift count >= width of type");
11371 else if (TREE_CODE (op0) == INTEGER_CST
11372 && maybe_warn_shift_overflow (location, op0, op1)
11373 && flag_isoc99)
11374 int_const = false;
11377 /* Use the type of the value to be shifted. */
11378 result_type = type0;
11379 /* Avoid converting op1 to result_type later. */
11380 converted = 1;
11382 break;
11384 case EQ_EXPR:
11385 case NE_EXPR:
11386 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11388 tree intt;
11389 if (!vector_types_compatible_elements_p (type0, type1))
11391 error_at (location, "comparing vectors with different "
11392 "element types");
11393 return error_mark_node;
11396 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11398 error_at (location, "comparing vectors with different "
11399 "number of elements");
11400 return error_mark_node;
11403 /* It's not precisely specified how the usual arithmetic
11404 conversions apply to the vector types. Here, we use
11405 the unsigned type if one of the operands is signed and
11406 the other one is unsigned. */
11407 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11409 if (!TYPE_UNSIGNED (type0))
11410 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11411 else
11412 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11413 warning_at (location, OPT_Wsign_compare, "comparison between "
11414 "types %qT and %qT", type0, type1);
11417 /* Always construct signed integer vector type. */
11418 intt = c_common_type_for_size (GET_MODE_BITSIZE
11419 (SCALAR_TYPE_MODE
11420 (TREE_TYPE (type0))), 0);
11421 result_type = build_opaque_vector_type (intt,
11422 TYPE_VECTOR_SUBPARTS (type0));
11423 converted = 1;
11424 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11425 goto return_build_binary_op;
11427 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11428 warning_at (location,
11429 OPT_Wfloat_equal,
11430 "comparing floating point with == or != is unsafe");
11431 /* Result of comparison is always int,
11432 but don't convert the args to int! */
11433 build_type = integer_type_node;
11434 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11435 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11436 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11437 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11438 short_compare = 1;
11439 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11441 if (TREE_CODE (op0) == ADDR_EXPR
11442 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11443 && !from_macro_expansion_at (location))
11445 if (code == EQ_EXPR)
11446 warning_at (location,
11447 OPT_Waddress,
11448 "the comparison will always evaluate as %<false%> "
11449 "for the address of %qD will never be NULL",
11450 TREE_OPERAND (op0, 0));
11451 else
11452 warning_at (location,
11453 OPT_Waddress,
11454 "the comparison will always evaluate as %<true%> "
11455 "for the address of %qD will never be NULL",
11456 TREE_OPERAND (op0, 0));
11458 result_type = type0;
11460 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11462 if (TREE_CODE (op1) == ADDR_EXPR
11463 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11464 && !from_macro_expansion_at (location))
11466 if (code == EQ_EXPR)
11467 warning_at (location,
11468 OPT_Waddress,
11469 "the comparison will always evaluate as %<false%> "
11470 "for the address of %qD will never be NULL",
11471 TREE_OPERAND (op1, 0));
11472 else
11473 warning_at (location,
11474 OPT_Waddress,
11475 "the comparison will always evaluate as %<true%> "
11476 "for the address of %qD will never be NULL",
11477 TREE_OPERAND (op1, 0));
11479 result_type = type1;
11481 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11483 tree tt0 = TREE_TYPE (type0);
11484 tree tt1 = TREE_TYPE (type1);
11485 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11486 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11487 addr_space_t as_common = ADDR_SPACE_GENERIC;
11489 /* Anything compares with void *. void * compares with anything.
11490 Otherwise, the targets must be compatible
11491 and both must be object or both incomplete. */
11492 if (comp_target_types (location, type0, type1))
11493 result_type = common_pointer_type (type0, type1);
11494 else if (!addr_space_superset (as0, as1, &as_common))
11496 error_at (location, "comparison of pointers to "
11497 "disjoint address spaces");
11498 return error_mark_node;
11500 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11502 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11503 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11504 "comparison of %<void *%> with function pointer");
11506 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11508 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11509 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11510 "comparison of %<void *%> with function pointer");
11512 else
11513 /* Avoid warning about the volatile ObjC EH puts on decls. */
11514 if (!objc_ok)
11515 pedwarn (location, 0,
11516 "comparison of distinct pointer types lacks a cast");
11518 if (result_type == NULL_TREE)
11520 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11521 result_type = build_pointer_type
11522 (build_qualified_type (void_type_node, qual));
11525 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11527 result_type = type0;
11528 pedwarn (location, 0, "comparison between pointer and integer");
11530 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11532 result_type = type1;
11533 pedwarn (location, 0, "comparison between pointer and integer");
11535 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11536 || truth_value_p (TREE_CODE (orig_op0)))
11537 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11538 || truth_value_p (TREE_CODE (orig_op1))))
11539 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11540 break;
11542 case LE_EXPR:
11543 case GE_EXPR:
11544 case LT_EXPR:
11545 case GT_EXPR:
11546 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11548 tree intt;
11549 if (!vector_types_compatible_elements_p (type0, type1))
11551 error_at (location, "comparing vectors with different "
11552 "element types");
11553 return error_mark_node;
11556 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11558 error_at (location, "comparing vectors with different "
11559 "number of elements");
11560 return error_mark_node;
11563 /* It's not precisely specified how the usual arithmetic
11564 conversions apply to the vector types. Here, we use
11565 the unsigned type if one of the operands is signed and
11566 the other one is unsigned. */
11567 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11569 if (!TYPE_UNSIGNED (type0))
11570 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11571 else
11572 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11573 warning_at (location, OPT_Wsign_compare, "comparison between "
11574 "types %qT and %qT", type0, type1);
11577 /* Always construct signed integer vector type. */
11578 intt = c_common_type_for_size (GET_MODE_BITSIZE
11579 (SCALAR_TYPE_MODE
11580 (TREE_TYPE (type0))), 0);
11581 result_type = build_opaque_vector_type (intt,
11582 TYPE_VECTOR_SUBPARTS (type0));
11583 converted = 1;
11584 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11585 goto return_build_binary_op;
11587 build_type = integer_type_node;
11588 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11589 || code0 == FIXED_POINT_TYPE)
11590 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11591 || code1 == FIXED_POINT_TYPE))
11592 short_compare = 1;
11593 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11595 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11596 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11597 addr_space_t as_common;
11599 if (comp_target_types (location, type0, type1))
11601 result_type = common_pointer_type (type0, type1);
11602 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11603 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11604 pedwarn (location, 0,
11605 "comparison of complete and incomplete pointers");
11606 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11607 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11608 "ordered comparisons of pointers to functions");
11609 else if (null_pointer_constant_p (orig_op0)
11610 || null_pointer_constant_p (orig_op1))
11611 warning_at (location, OPT_Wextra,
11612 "ordered comparison of pointer with null pointer");
11615 else if (!addr_space_superset (as0, as1, &as_common))
11617 error_at (location, "comparison of pointers to "
11618 "disjoint address spaces");
11619 return error_mark_node;
11621 else
11623 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11624 result_type = build_pointer_type
11625 (build_qualified_type (void_type_node, qual));
11626 pedwarn (location, 0,
11627 "comparison of distinct pointer types lacks a cast");
11630 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11632 result_type = type0;
11633 if (pedantic)
11634 pedwarn (location, OPT_Wpedantic,
11635 "ordered comparison of pointer with integer zero");
11636 else if (extra_warnings)
11637 warning_at (location, OPT_Wextra,
11638 "ordered comparison of pointer with integer zero");
11640 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11642 result_type = type1;
11643 if (pedantic)
11644 pedwarn (location, OPT_Wpedantic,
11645 "ordered comparison of pointer with integer zero");
11646 else if (extra_warnings)
11647 warning_at (location, OPT_Wextra,
11648 "ordered comparison of pointer with integer zero");
11650 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11652 result_type = type0;
11653 pedwarn (location, 0, "comparison between pointer and integer");
11655 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11657 result_type = type1;
11658 pedwarn (location, 0, "comparison between pointer and integer");
11660 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11661 || truth_value_p (TREE_CODE (orig_op0)))
11662 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11663 || truth_value_p (TREE_CODE (orig_op1))))
11664 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11665 break;
11667 default:
11668 gcc_unreachable ();
11671 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11672 return error_mark_node;
11674 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11675 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11676 || !vector_types_compatible_elements_p (type0, type1)))
11678 gcc_rich_location richloc (location);
11679 richloc.maybe_add_expr (orig_op0);
11680 richloc.maybe_add_expr (orig_op1);
11681 binary_op_error (&richloc, code, type0, type1);
11682 return error_mark_node;
11685 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11686 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11688 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11689 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11691 bool first_complex = (code0 == COMPLEX_TYPE);
11692 bool second_complex = (code1 == COMPLEX_TYPE);
11693 int none_complex = (!first_complex && !second_complex);
11695 if (shorten || common || short_compare)
11697 result_type = c_common_type (type0, type1);
11698 do_warn_double_promotion (result_type, type0, type1,
11699 "implicit conversion from %qT to %qT "
11700 "to match other operand of binary "
11701 "expression",
11702 location);
11703 if (result_type == error_mark_node)
11704 return error_mark_node;
11707 if (first_complex != second_complex
11708 && (code == PLUS_EXPR
11709 || code == MINUS_EXPR
11710 || code == MULT_EXPR
11711 || (code == TRUNC_DIV_EXPR && first_complex))
11712 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11713 && flag_signed_zeros)
11715 /* An operation on mixed real/complex operands must be
11716 handled specially, but the language-independent code can
11717 more easily optimize the plain complex arithmetic if
11718 -fno-signed-zeros. */
11719 tree real_type = TREE_TYPE (result_type);
11720 tree real, imag;
11721 if (type0 != orig_type0 || type1 != orig_type1)
11723 gcc_assert (may_need_excess_precision && common);
11724 semantic_result_type = c_common_type (orig_type0, orig_type1);
11726 if (first_complex)
11728 if (TREE_TYPE (op0) != result_type)
11729 op0 = convert_and_check (location, result_type, op0);
11730 if (TREE_TYPE (op1) != real_type)
11731 op1 = convert_and_check (location, real_type, op1);
11733 else
11735 if (TREE_TYPE (op0) != real_type)
11736 op0 = convert_and_check (location, real_type, op0);
11737 if (TREE_TYPE (op1) != result_type)
11738 op1 = convert_and_check (location, result_type, op1);
11740 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11741 return error_mark_node;
11742 if (first_complex)
11744 op0 = save_expr (op0);
11745 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11746 op0, true);
11747 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11748 op0, true);
11749 switch (code)
11751 case MULT_EXPR:
11752 case TRUNC_DIV_EXPR:
11753 op1 = save_expr (op1);
11754 imag = build2 (resultcode, real_type, imag, op1);
11755 /* Fall through. */
11756 case PLUS_EXPR:
11757 case MINUS_EXPR:
11758 real = build2 (resultcode, real_type, real, op1);
11759 break;
11760 default:
11761 gcc_unreachable();
11764 else
11766 op1 = save_expr (op1);
11767 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11768 op1, true);
11769 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11770 op1, true);
11771 switch (code)
11773 case MULT_EXPR:
11774 op0 = save_expr (op0);
11775 imag = build2 (resultcode, real_type, op0, imag);
11776 /* Fall through. */
11777 case PLUS_EXPR:
11778 real = build2 (resultcode, real_type, op0, real);
11779 break;
11780 case MINUS_EXPR:
11781 real = build2 (resultcode, real_type, op0, real);
11782 imag = build1 (NEGATE_EXPR, real_type, imag);
11783 break;
11784 default:
11785 gcc_unreachable();
11788 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11789 goto return_build_binary_op;
11792 /* For certain operations (which identify themselves by shorten != 0)
11793 if both args were extended from the same smaller type,
11794 do the arithmetic in that type and then extend.
11796 shorten !=0 and !=1 indicates a bitwise operation.
11797 For them, this optimization is safe only if
11798 both args are zero-extended or both are sign-extended.
11799 Otherwise, we might change the result.
11800 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11801 but calculated in (unsigned short) it would be (unsigned short)-1. */
11803 if (shorten && none_complex)
11805 final_type = result_type;
11806 result_type = shorten_binary_op (result_type, op0, op1,
11807 shorten == -1);
11810 /* Shifts can be shortened if shifting right. */
11812 if (short_shift)
11814 int unsigned_arg;
11815 tree arg0 = get_narrower (op0, &unsigned_arg);
11817 final_type = result_type;
11819 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11820 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11822 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11823 && tree_int_cst_sgn (op1) > 0
11824 /* We can shorten only if the shift count is less than the
11825 number of bits in the smaller type size. */
11826 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11827 /* We cannot drop an unsigned shift after sign-extension. */
11828 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11830 /* Do an unsigned shift if the operand was zero-extended. */
11831 result_type
11832 = c_common_signed_or_unsigned_type (unsigned_arg,
11833 TREE_TYPE (arg0));
11834 /* Convert value-to-be-shifted to that type. */
11835 if (TREE_TYPE (op0) != result_type)
11836 op0 = convert (result_type, op0);
11837 converted = 1;
11841 /* Comparison operations are shortened too but differently.
11842 They identify themselves by setting short_compare = 1. */
11844 if (short_compare)
11846 /* Don't write &op0, etc., because that would prevent op0
11847 from being kept in a register.
11848 Instead, make copies of the our local variables and
11849 pass the copies by reference, then copy them back afterward. */
11850 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11851 enum tree_code xresultcode = resultcode;
11852 tree val
11853 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11854 &xresultcode);
11856 if (val != NULL_TREE)
11858 ret = val;
11859 goto return_build_binary_op;
11862 op0 = xop0, op1 = xop1;
11863 converted = 1;
11864 resultcode = xresultcode;
11866 if (c_inhibit_evaluation_warnings == 0)
11868 bool op0_maybe_const = true;
11869 bool op1_maybe_const = true;
11870 tree orig_op0_folded, orig_op1_folded;
11872 if (in_late_binary_op)
11874 orig_op0_folded = orig_op0;
11875 orig_op1_folded = orig_op1;
11877 else
11879 /* Fold for the sake of possible warnings, as in
11880 build_conditional_expr. This requires the
11881 "original" values to be folded, not just op0 and
11882 op1. */
11883 c_inhibit_evaluation_warnings++;
11884 op0 = c_fully_fold (op0, require_constant_value,
11885 &op0_maybe_const);
11886 op1 = c_fully_fold (op1, require_constant_value,
11887 &op1_maybe_const);
11888 c_inhibit_evaluation_warnings--;
11889 orig_op0_folded = c_fully_fold (orig_op0,
11890 require_constant_value,
11891 NULL);
11892 orig_op1_folded = c_fully_fold (orig_op1,
11893 require_constant_value,
11894 NULL);
11897 if (warn_sign_compare)
11898 warn_for_sign_compare (location, orig_op0_folded,
11899 orig_op1_folded, op0, op1,
11900 result_type, resultcode);
11901 if (!in_late_binary_op && !int_operands)
11903 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11904 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11905 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11906 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11912 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11913 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11914 Then the expression will be built.
11915 It will be given type FINAL_TYPE if that is nonzero;
11916 otherwise, it will be given type RESULT_TYPE. */
11918 if (!result_type)
11920 gcc_rich_location richloc (location);
11921 richloc.maybe_add_expr (orig_op0);
11922 richloc.maybe_add_expr (orig_op1);
11923 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11924 return error_mark_node;
11927 if (build_type == NULL_TREE)
11929 build_type = result_type;
11930 if ((type0 != orig_type0 || type1 != orig_type1)
11931 && !boolean_op)
11933 gcc_assert (may_need_excess_precision && common);
11934 semantic_result_type = c_common_type (orig_type0, orig_type1);
11938 if (!converted)
11940 op0 = ep_convert_and_check (location, result_type, op0,
11941 semantic_result_type);
11942 op1 = ep_convert_and_check (location, result_type, op1,
11943 semantic_result_type);
11945 /* This can happen if one operand has a vector type, and the other
11946 has a different type. */
11947 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11948 return error_mark_node;
11951 if (sanitize_flags_p ((SANITIZE_SHIFT
11952 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
11953 && current_function_decl != NULL_TREE
11954 && (doing_div_or_mod || doing_shift)
11955 && !require_constant_value)
11957 /* OP0 and/or OP1 might have side-effects. */
11958 op0 = save_expr (op0);
11959 op1 = save_expr (op1);
11960 op0 = c_fully_fold (op0, false, NULL);
11961 op1 = c_fully_fold (op1, false, NULL);
11962 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
11963 | SANITIZE_FLOAT_DIVIDE))))
11964 instrument_expr = ubsan_instrument_division (location, op0, op1);
11965 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
11966 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11969 /* Treat expressions in initializers specially as they can't trap. */
11970 if (int_const_or_overflow)
11971 ret = (require_constant_value
11972 ? fold_build2_initializer_loc (location, resultcode, build_type,
11973 op0, op1)
11974 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11975 else
11976 ret = build2 (resultcode, build_type, op0, op1);
11977 if (final_type != NULL_TREE)
11978 ret = convert (final_type, ret);
11980 return_build_binary_op:
11981 gcc_assert (ret != error_mark_node);
11982 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11983 ret = (int_operands
11984 ? note_integer_operands (ret)
11985 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11986 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11987 && !in_late_binary_op)
11988 ret = note_integer_operands (ret);
11989 protected_set_expr_location (ret, location);
11991 if (instrument_expr != NULL)
11992 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11993 instrument_expr, ret);
11995 if (semantic_result_type)
11996 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
11997 semantic_result_type, ret);
11999 return ret;
12003 /* Convert EXPR to be a truth-value, validating its type for this
12004 purpose. LOCATION is the source location for the expression. */
12006 tree
12007 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12009 bool int_const, int_operands;
12011 switch (TREE_CODE (TREE_TYPE (expr)))
12013 case ARRAY_TYPE:
12014 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12015 return error_mark_node;
12017 case RECORD_TYPE:
12018 error_at (location, "used struct type value where scalar is required");
12019 return error_mark_node;
12021 case UNION_TYPE:
12022 error_at (location, "used union type value where scalar is required");
12023 return error_mark_node;
12025 case VOID_TYPE:
12026 error_at (location, "void value not ignored as it ought to be");
12027 return error_mark_node;
12029 case POINTER_TYPE:
12030 if (reject_gcc_builtin (expr))
12031 return error_mark_node;
12032 break;
12034 case FUNCTION_TYPE:
12035 gcc_unreachable ();
12037 case VECTOR_TYPE:
12038 error_at (location, "used vector type where scalar is required");
12039 return error_mark_node;
12041 default:
12042 break;
12045 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12046 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12047 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12049 expr = remove_c_maybe_const_expr (expr);
12050 expr = build2 (NE_EXPR, integer_type_node, expr,
12051 convert (TREE_TYPE (expr), integer_zero_node));
12052 expr = note_integer_operands (expr);
12054 else
12055 /* ??? Should we also give an error for vectors rather than leaving
12056 those to give errors later? */
12057 expr = c_common_truthvalue_conversion (location, expr);
12059 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12061 if (TREE_OVERFLOW (expr))
12062 return expr;
12063 else
12064 return note_integer_operands (expr);
12066 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12067 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12068 return expr;
12072 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12073 required. */
12075 tree
12076 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12078 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12080 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12081 /* Executing a compound literal inside a function reinitializes
12082 it. */
12083 if (!TREE_STATIC (decl))
12084 *se = true;
12085 return decl;
12087 else
12088 return expr;
12091 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12092 statement. LOC is the location of the construct. */
12094 tree
12095 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12096 tree clauses)
12098 body = c_end_compound_stmt (loc, body, true);
12100 tree stmt = make_node (code);
12101 TREE_TYPE (stmt) = void_type_node;
12102 OMP_BODY (stmt) = body;
12103 OMP_CLAUSES (stmt) = clauses;
12104 SET_EXPR_LOCATION (stmt, loc);
12106 return add_stmt (stmt);
12109 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12110 statement. LOC is the location of the OACC_DATA. */
12112 tree
12113 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12115 tree stmt;
12117 block = c_end_compound_stmt (loc, block, true);
12119 stmt = make_node (OACC_DATA);
12120 TREE_TYPE (stmt) = void_type_node;
12121 OACC_DATA_CLAUSES (stmt) = clauses;
12122 OACC_DATA_BODY (stmt) = block;
12123 SET_EXPR_LOCATION (stmt, loc);
12125 return add_stmt (stmt);
12128 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12129 statement. LOC is the location of the OACC_HOST_DATA. */
12131 tree
12132 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12134 tree stmt;
12136 block = c_end_compound_stmt (loc, block, true);
12138 stmt = make_node (OACC_HOST_DATA);
12139 TREE_TYPE (stmt) = void_type_node;
12140 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12141 OACC_HOST_DATA_BODY (stmt) = block;
12142 SET_EXPR_LOCATION (stmt, loc);
12144 return add_stmt (stmt);
12147 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12149 tree
12150 c_begin_omp_parallel (void)
12152 tree block;
12154 keep_next_level ();
12155 block = c_begin_compound_stmt (true);
12157 return block;
12160 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12161 statement. LOC is the location of the OMP_PARALLEL. */
12163 tree
12164 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12166 tree stmt;
12168 block = c_end_compound_stmt (loc, block, true);
12170 stmt = make_node (OMP_PARALLEL);
12171 TREE_TYPE (stmt) = void_type_node;
12172 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12173 OMP_PARALLEL_BODY (stmt) = block;
12174 SET_EXPR_LOCATION (stmt, loc);
12176 return add_stmt (stmt);
12179 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12181 tree
12182 c_begin_omp_task (void)
12184 tree block;
12186 keep_next_level ();
12187 block = c_begin_compound_stmt (true);
12189 return block;
12192 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12193 statement. LOC is the location of the #pragma. */
12195 tree
12196 c_finish_omp_task (location_t loc, tree clauses, tree block)
12198 tree stmt;
12200 block = c_end_compound_stmt (loc, block, true);
12202 stmt = make_node (OMP_TASK);
12203 TREE_TYPE (stmt) = void_type_node;
12204 OMP_TASK_CLAUSES (stmt) = clauses;
12205 OMP_TASK_BODY (stmt) = block;
12206 SET_EXPR_LOCATION (stmt, loc);
12208 return add_stmt (stmt);
12211 /* Generate GOMP_cancel call for #pragma omp cancel. */
12213 void
12214 c_finish_omp_cancel (location_t loc, tree clauses)
12216 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12217 int mask = 0;
12218 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12219 mask = 1;
12220 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12221 mask = 2;
12222 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12223 mask = 4;
12224 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12225 mask = 8;
12226 else
12228 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12229 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12230 "clauses");
12231 return;
12233 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12234 if (ifc != NULL_TREE)
12236 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12237 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12238 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12239 build_zero_cst (type));
12241 else
12242 ifc = boolean_true_node;
12243 tree stmt = build_call_expr_loc (loc, fn, 2,
12244 build_int_cst (integer_type_node, mask),
12245 ifc);
12246 add_stmt (stmt);
12249 /* Generate GOMP_cancellation_point call for
12250 #pragma omp cancellation point. */
12252 void
12253 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12255 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12256 int mask = 0;
12257 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12258 mask = 1;
12259 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12260 mask = 2;
12261 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12262 mask = 4;
12263 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12264 mask = 8;
12265 else
12267 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12268 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12269 "clauses");
12270 return;
12272 tree stmt = build_call_expr_loc (loc, fn, 1,
12273 build_int_cst (integer_type_node, mask));
12274 add_stmt (stmt);
12277 /* Helper function for handle_omp_array_sections. Called recursively
12278 to handle multiple array-section-subscripts. C is the clause,
12279 T current expression (initially OMP_CLAUSE_DECL), which is either
12280 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12281 expression if specified, TREE_VALUE length expression if specified,
12282 TREE_CHAIN is what it has been specified after, or some decl.
12283 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12284 set to true if any of the array-section-subscript could have length
12285 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12286 first array-section-subscript which is known not to have length
12287 of one. Given say:
12288 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12289 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12290 all are or may have length of 1, array-section-subscript [:2] is the
12291 first one known not to have length 1. For array-section-subscript
12292 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12293 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12294 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12295 case though, as some lengths could be zero. */
12297 static tree
12298 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12299 bool &maybe_zero_len, unsigned int &first_non_one,
12300 enum c_omp_region_type ort)
12302 tree ret, low_bound, length, type;
12303 if (TREE_CODE (t) != TREE_LIST)
12305 if (error_operand_p (t))
12306 return error_mark_node;
12307 ret = t;
12308 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12309 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12311 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12312 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12313 return error_mark_node;
12315 if (TREE_CODE (t) == COMPONENT_REF
12316 && ort == C_ORT_OMP
12317 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12318 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12319 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12321 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12323 error_at (OMP_CLAUSE_LOCATION (c),
12324 "bit-field %qE in %qs clause",
12325 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12326 return error_mark_node;
12328 while (TREE_CODE (t) == COMPONENT_REF)
12330 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12332 error_at (OMP_CLAUSE_LOCATION (c),
12333 "%qE is a member of a union", t);
12334 return error_mark_node;
12336 t = TREE_OPERAND (t, 0);
12339 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12341 if (DECL_P (t))
12342 error_at (OMP_CLAUSE_LOCATION (c),
12343 "%qD is not a variable in %qs clause", t,
12344 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12345 else
12346 error_at (OMP_CLAUSE_LOCATION (c),
12347 "%qE is not a variable in %qs clause", t,
12348 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12349 return error_mark_node;
12351 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12352 && TYPE_ATOMIC (TREE_TYPE (t)))
12354 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12355 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12356 return error_mark_node;
12358 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12359 && VAR_P (t)
12360 && DECL_THREAD_LOCAL_P (t))
12362 error_at (OMP_CLAUSE_LOCATION (c),
12363 "%qD is threadprivate variable in %qs clause", t,
12364 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12365 return error_mark_node;
12367 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12368 && TYPE_ATOMIC (TREE_TYPE (t))
12369 && POINTER_TYPE_P (TREE_TYPE (t)))
12371 /* If the array section is pointer based and the pointer
12372 itself is _Atomic qualified, we need to atomically load
12373 the pointer. */
12374 c_expr expr;
12375 memset (&expr, 0, sizeof (expr));
12376 expr.value = ret;
12377 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12378 expr, false, false);
12379 ret = expr.value;
12381 return ret;
12384 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12385 maybe_zero_len, first_non_one, ort);
12386 if (ret == error_mark_node || ret == NULL_TREE)
12387 return ret;
12389 type = TREE_TYPE (ret);
12390 low_bound = TREE_PURPOSE (t);
12391 length = TREE_VALUE (t);
12393 if (low_bound == error_mark_node || length == error_mark_node)
12394 return error_mark_node;
12396 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12398 error_at (OMP_CLAUSE_LOCATION (c),
12399 "low bound %qE of array section does not have integral type",
12400 low_bound);
12401 return error_mark_node;
12403 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12405 error_at (OMP_CLAUSE_LOCATION (c),
12406 "length %qE of array section does not have integral type",
12407 length);
12408 return error_mark_node;
12410 if (low_bound
12411 && TREE_CODE (low_bound) == INTEGER_CST
12412 && TYPE_PRECISION (TREE_TYPE (low_bound))
12413 > TYPE_PRECISION (sizetype))
12414 low_bound = fold_convert (sizetype, low_bound);
12415 if (length
12416 && TREE_CODE (length) == INTEGER_CST
12417 && TYPE_PRECISION (TREE_TYPE (length))
12418 > TYPE_PRECISION (sizetype))
12419 length = fold_convert (sizetype, length);
12420 if (low_bound == NULL_TREE)
12421 low_bound = integer_zero_node;
12423 if (length != NULL_TREE)
12425 if (!integer_nonzerop (length))
12427 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12428 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12430 if (integer_zerop (length))
12432 error_at (OMP_CLAUSE_LOCATION (c),
12433 "zero length array section in %qs clause",
12434 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12435 return error_mark_node;
12438 else
12439 maybe_zero_len = true;
12441 if (first_non_one == types.length ()
12442 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12443 first_non_one++;
12445 if (TREE_CODE (type) == ARRAY_TYPE)
12447 if (length == NULL_TREE
12448 && (TYPE_DOMAIN (type) == NULL_TREE
12449 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12451 error_at (OMP_CLAUSE_LOCATION (c),
12452 "for unknown bound array type length expression must "
12453 "be specified");
12454 return error_mark_node;
12456 if (TREE_CODE (low_bound) == INTEGER_CST
12457 && tree_int_cst_sgn (low_bound) == -1)
12459 error_at (OMP_CLAUSE_LOCATION (c),
12460 "negative low bound in array section in %qs clause",
12461 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12462 return error_mark_node;
12464 if (length != NULL_TREE
12465 && TREE_CODE (length) == INTEGER_CST
12466 && tree_int_cst_sgn (length) == -1)
12468 error_at (OMP_CLAUSE_LOCATION (c),
12469 "negative length in array section in %qs clause",
12470 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12471 return error_mark_node;
12473 if (TYPE_DOMAIN (type)
12474 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12475 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12476 == INTEGER_CST)
12478 tree size
12479 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12480 size = size_binop (PLUS_EXPR, size, size_one_node);
12481 if (TREE_CODE (low_bound) == INTEGER_CST)
12483 if (tree_int_cst_lt (size, low_bound))
12485 error_at (OMP_CLAUSE_LOCATION (c),
12486 "low bound %qE above array section size "
12487 "in %qs clause", low_bound,
12488 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12489 return error_mark_node;
12491 if (tree_int_cst_equal (size, low_bound))
12493 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12494 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12496 error_at (OMP_CLAUSE_LOCATION (c),
12497 "zero length array section in %qs clause",
12498 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12499 return error_mark_node;
12501 maybe_zero_len = true;
12503 else if (length == NULL_TREE
12504 && first_non_one == types.length ()
12505 && tree_int_cst_equal
12506 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12507 low_bound))
12508 first_non_one++;
12510 else if (length == NULL_TREE)
12512 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12513 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12514 maybe_zero_len = true;
12515 if (first_non_one == types.length ())
12516 first_non_one++;
12518 if (length && TREE_CODE (length) == INTEGER_CST)
12520 if (tree_int_cst_lt (size, length))
12522 error_at (OMP_CLAUSE_LOCATION (c),
12523 "length %qE above array section size "
12524 "in %qs clause", length,
12525 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12526 return error_mark_node;
12528 if (TREE_CODE (low_bound) == INTEGER_CST)
12530 tree lbpluslen
12531 = size_binop (PLUS_EXPR,
12532 fold_convert (sizetype, low_bound),
12533 fold_convert (sizetype, length));
12534 if (TREE_CODE (lbpluslen) == INTEGER_CST
12535 && tree_int_cst_lt (size, lbpluslen))
12537 error_at (OMP_CLAUSE_LOCATION (c),
12538 "high bound %qE above array section size "
12539 "in %qs clause", lbpluslen,
12540 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12541 return error_mark_node;
12546 else if (length == NULL_TREE)
12548 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12549 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12550 maybe_zero_len = true;
12551 if (first_non_one == types.length ())
12552 first_non_one++;
12555 /* For [lb:] we will need to evaluate lb more than once. */
12556 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12558 tree lb = save_expr (low_bound);
12559 if (lb != low_bound)
12561 TREE_PURPOSE (t) = lb;
12562 low_bound = lb;
12566 else if (TREE_CODE (type) == POINTER_TYPE)
12568 if (length == NULL_TREE)
12570 error_at (OMP_CLAUSE_LOCATION (c),
12571 "for pointer type length expression must be specified");
12572 return error_mark_node;
12574 if (length != NULL_TREE
12575 && TREE_CODE (length) == INTEGER_CST
12576 && tree_int_cst_sgn (length) == -1)
12578 error_at (OMP_CLAUSE_LOCATION (c),
12579 "negative length in array section in %qs clause",
12580 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12581 return error_mark_node;
12583 /* If there is a pointer type anywhere but in the very first
12584 array-section-subscript, the array section can't be contiguous. */
12585 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12586 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12588 error_at (OMP_CLAUSE_LOCATION (c),
12589 "array section is not contiguous in %qs clause",
12590 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12591 return error_mark_node;
12594 else
12596 error_at (OMP_CLAUSE_LOCATION (c),
12597 "%qE does not have pointer or array type", ret);
12598 return error_mark_node;
12600 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12601 types.safe_push (TREE_TYPE (ret));
12602 /* We will need to evaluate lb more than once. */
12603 tree lb = save_expr (low_bound);
12604 if (lb != low_bound)
12606 TREE_PURPOSE (t) = lb;
12607 low_bound = lb;
12609 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12610 return ret;
12613 /* Handle array sections for clause C. */
12615 static bool
12616 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12618 bool maybe_zero_len = false;
12619 unsigned int first_non_one = 0;
12620 auto_vec<tree, 10> types;
12621 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12622 maybe_zero_len, first_non_one,
12623 ort);
12624 if (first == error_mark_node)
12625 return true;
12626 if (first == NULL_TREE)
12627 return false;
12628 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12630 tree t = OMP_CLAUSE_DECL (c);
12631 tree tem = NULL_TREE;
12632 /* Need to evaluate side effects in the length expressions
12633 if any. */
12634 while (TREE_CODE (t) == TREE_LIST)
12636 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12638 if (tem == NULL_TREE)
12639 tem = TREE_VALUE (t);
12640 else
12641 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12642 TREE_VALUE (t), tem);
12644 t = TREE_CHAIN (t);
12646 if (tem)
12647 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12648 first = c_fully_fold (first, false, NULL);
12649 OMP_CLAUSE_DECL (c) = first;
12651 else
12653 unsigned int num = types.length (), i;
12654 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12655 tree condition = NULL_TREE;
12657 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12658 maybe_zero_len = true;
12660 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12661 t = TREE_CHAIN (t))
12663 tree low_bound = TREE_PURPOSE (t);
12664 tree length = TREE_VALUE (t);
12666 i--;
12667 if (low_bound
12668 && TREE_CODE (low_bound) == INTEGER_CST
12669 && TYPE_PRECISION (TREE_TYPE (low_bound))
12670 > TYPE_PRECISION (sizetype))
12671 low_bound = fold_convert (sizetype, low_bound);
12672 if (length
12673 && TREE_CODE (length) == INTEGER_CST
12674 && TYPE_PRECISION (TREE_TYPE (length))
12675 > TYPE_PRECISION (sizetype))
12676 length = fold_convert (sizetype, length);
12677 if (low_bound == NULL_TREE)
12678 low_bound = integer_zero_node;
12679 if (!maybe_zero_len && i > first_non_one)
12681 if (integer_nonzerop (low_bound))
12682 goto do_warn_noncontiguous;
12683 if (length != NULL_TREE
12684 && TREE_CODE (length) == INTEGER_CST
12685 && TYPE_DOMAIN (types[i])
12686 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12687 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12688 == INTEGER_CST)
12690 tree size;
12691 size = size_binop (PLUS_EXPR,
12692 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12693 size_one_node);
12694 if (!tree_int_cst_equal (length, size))
12696 do_warn_noncontiguous:
12697 error_at (OMP_CLAUSE_LOCATION (c),
12698 "array section is not contiguous in %qs "
12699 "clause",
12700 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12701 return true;
12704 if (length != NULL_TREE
12705 && TREE_SIDE_EFFECTS (length))
12707 if (side_effects == NULL_TREE)
12708 side_effects = length;
12709 else
12710 side_effects = build2 (COMPOUND_EXPR,
12711 TREE_TYPE (side_effects),
12712 length, side_effects);
12715 else
12717 tree l;
12719 if (i > first_non_one
12720 && ((length && integer_nonzerop (length))
12721 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12722 continue;
12723 if (length)
12724 l = fold_convert (sizetype, length);
12725 else
12727 l = size_binop (PLUS_EXPR,
12728 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12729 size_one_node);
12730 l = size_binop (MINUS_EXPR, l,
12731 fold_convert (sizetype, low_bound));
12733 if (i > first_non_one)
12735 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12736 size_zero_node);
12737 if (condition == NULL_TREE)
12738 condition = l;
12739 else
12740 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12741 l, condition);
12743 else if (size == NULL_TREE)
12745 size = size_in_bytes (TREE_TYPE (types[i]));
12746 tree eltype = TREE_TYPE (types[num - 1]);
12747 while (TREE_CODE (eltype) == ARRAY_TYPE)
12748 eltype = TREE_TYPE (eltype);
12749 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12751 if (integer_zerop (size)
12752 || integer_zerop (size_in_bytes (eltype)))
12754 error_at (OMP_CLAUSE_LOCATION (c),
12755 "zero length array section in %qs clause",
12756 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12757 return error_mark_node;
12759 size = size_binop (EXACT_DIV_EXPR, size,
12760 size_in_bytes (eltype));
12762 size = size_binop (MULT_EXPR, size, l);
12763 if (condition)
12764 size = fold_build3 (COND_EXPR, sizetype, condition,
12765 size, size_zero_node);
12767 else
12768 size = size_binop (MULT_EXPR, size, l);
12771 if (side_effects)
12772 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12773 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12775 size = size_binop (MINUS_EXPR, size, size_one_node);
12776 size = c_fully_fold (size, false, NULL);
12777 tree index_type = build_index_type (size);
12778 tree eltype = TREE_TYPE (first);
12779 while (TREE_CODE (eltype) == ARRAY_TYPE)
12780 eltype = TREE_TYPE (eltype);
12781 tree type = build_array_type (eltype, index_type);
12782 tree ptype = build_pointer_type (eltype);
12783 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12784 t = build_fold_addr_expr (t);
12785 tree t2 = build_fold_addr_expr (first);
12786 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12787 ptrdiff_type_node, t2);
12788 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12789 ptrdiff_type_node, t2,
12790 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12791 ptrdiff_type_node, t));
12792 t2 = c_fully_fold (t2, false, NULL);
12793 if (tree_fits_shwi_p (t2))
12794 t = build2 (MEM_REF, type, t,
12795 build_int_cst (ptype, tree_to_shwi (t2)));
12796 else
12798 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12799 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12800 TREE_TYPE (t), t, t2);
12801 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12803 OMP_CLAUSE_DECL (c) = t;
12804 return false;
12806 first = c_fully_fold (first, false, NULL);
12807 OMP_CLAUSE_DECL (c) = first;
12808 if (size)
12809 size = c_fully_fold (size, false, NULL);
12810 OMP_CLAUSE_SIZE (c) = size;
12811 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12812 || (TREE_CODE (t) == COMPONENT_REF
12813 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12814 return false;
12815 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12816 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12817 switch (OMP_CLAUSE_MAP_KIND (c))
12819 case GOMP_MAP_ALLOC:
12820 case GOMP_MAP_TO:
12821 case GOMP_MAP_FROM:
12822 case GOMP_MAP_TOFROM:
12823 case GOMP_MAP_ALWAYS_TO:
12824 case GOMP_MAP_ALWAYS_FROM:
12825 case GOMP_MAP_ALWAYS_TOFROM:
12826 case GOMP_MAP_RELEASE:
12827 case GOMP_MAP_DELETE:
12828 case GOMP_MAP_FORCE_TO:
12829 case GOMP_MAP_FORCE_FROM:
12830 case GOMP_MAP_FORCE_TOFROM:
12831 case GOMP_MAP_FORCE_PRESENT:
12832 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12833 break;
12834 default:
12835 break;
12837 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12838 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12839 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12840 else if (TREE_CODE (t) == COMPONENT_REF)
12841 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12842 else
12843 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12844 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12845 && !c_mark_addressable (t))
12846 return false;
12847 OMP_CLAUSE_DECL (c2) = t;
12848 t = build_fold_addr_expr (first);
12849 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12850 tree ptr = OMP_CLAUSE_DECL (c2);
12851 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12852 ptr = build_fold_addr_expr (ptr);
12853 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12854 ptrdiff_type_node, t,
12855 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12856 ptrdiff_type_node, ptr));
12857 t = c_fully_fold (t, false, NULL);
12858 OMP_CLAUSE_SIZE (c2) = t;
12859 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12860 OMP_CLAUSE_CHAIN (c) = c2;
12862 return false;
12865 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12866 an inline call. But, remap
12867 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12868 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12870 static tree
12871 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12872 tree decl, tree placeholder)
12874 copy_body_data id;
12875 hash_map<tree, tree> decl_map;
12877 decl_map.put (omp_decl1, placeholder);
12878 decl_map.put (omp_decl2, decl);
12879 memset (&id, 0, sizeof (id));
12880 id.src_fn = DECL_CONTEXT (omp_decl1);
12881 id.dst_fn = current_function_decl;
12882 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12883 id.decl_map = &decl_map;
12885 id.copy_decl = copy_decl_no_change;
12886 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12887 id.transform_new_cfg = true;
12888 id.transform_return_to_modify = false;
12889 id.transform_lang_insert_block = NULL;
12890 id.eh_lp_nr = 0;
12891 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12892 return stmt;
12895 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12896 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12898 static tree
12899 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12901 if (*tp == (tree) data)
12902 return *tp;
12903 return NULL_TREE;
12906 /* For all elements of CLAUSES, validate them against their constraints.
12907 Remove any elements from the list that are invalid. */
12909 tree
12910 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12912 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12913 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12914 tree c, t, type, *pc;
12915 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12916 bool branch_seen = false;
12917 bool copyprivate_seen = false;
12918 bool linear_variable_step_check = false;
12919 tree *nowait_clause = NULL;
12920 bool ordered_seen = false;
12921 tree schedule_clause = NULL_TREE;
12922 bool oacc_async = false;
12924 bitmap_obstack_initialize (NULL);
12925 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12926 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12927 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12928 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12929 bitmap_initialize (&map_head, &bitmap_default_obstack);
12930 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12931 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12933 if (ort & C_ORT_ACC)
12934 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12935 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12937 oacc_async = true;
12938 break;
12941 for (pc = &clauses, c = clauses; c ; c = *pc)
12943 bool remove = false;
12944 bool need_complete = false;
12945 bool need_implicitly_determined = false;
12947 switch (OMP_CLAUSE_CODE (c))
12949 case OMP_CLAUSE_SHARED:
12950 need_implicitly_determined = true;
12951 goto check_dup_generic;
12953 case OMP_CLAUSE_PRIVATE:
12954 need_complete = true;
12955 need_implicitly_determined = true;
12956 goto check_dup_generic;
12958 case OMP_CLAUSE_REDUCTION:
12959 need_implicitly_determined = true;
12960 t = OMP_CLAUSE_DECL (c);
12961 if (TREE_CODE (t) == TREE_LIST)
12963 if (handle_omp_array_sections (c, ort))
12965 remove = true;
12966 break;
12969 t = OMP_CLAUSE_DECL (c);
12971 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12972 if (t == error_mark_node)
12974 remove = true;
12975 break;
12977 if (oacc_async)
12978 c_mark_addressable (t);
12979 type = TREE_TYPE (t);
12980 if (TREE_CODE (t) == MEM_REF)
12981 type = TREE_TYPE (type);
12982 if (TREE_CODE (type) == ARRAY_TYPE)
12984 tree oatype = type;
12985 gcc_assert (TREE_CODE (t) != MEM_REF);
12986 while (TREE_CODE (type) == ARRAY_TYPE)
12987 type = TREE_TYPE (type);
12988 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12990 error_at (OMP_CLAUSE_LOCATION (c),
12991 "%qD in %<reduction%> clause is a zero size array",
12993 remove = true;
12994 break;
12996 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12997 TYPE_SIZE_UNIT (type));
12998 if (integer_zerop (size))
13000 error_at (OMP_CLAUSE_LOCATION (c),
13001 "%qD in %<reduction%> clause is a zero size array",
13003 remove = true;
13004 break;
13006 size = size_binop (MINUS_EXPR, size, size_one_node);
13007 tree index_type = build_index_type (size);
13008 tree atype = build_array_type (type, index_type);
13009 tree ptype = build_pointer_type (type);
13010 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13011 t = build_fold_addr_expr (t);
13012 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13013 OMP_CLAUSE_DECL (c) = t;
13015 if (TYPE_ATOMIC (type))
13017 error_at (OMP_CLAUSE_LOCATION (c),
13018 "%<_Atomic%> %qE in %<reduction%> clause", t);
13019 remove = true;
13020 break;
13022 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13023 && (FLOAT_TYPE_P (type)
13024 || TREE_CODE (type) == COMPLEX_TYPE))
13026 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13027 const char *r_name = NULL;
13029 switch (r_code)
13031 case PLUS_EXPR:
13032 case MULT_EXPR:
13033 case MINUS_EXPR:
13034 break;
13035 case MIN_EXPR:
13036 if (TREE_CODE (type) == COMPLEX_TYPE)
13037 r_name = "min";
13038 break;
13039 case MAX_EXPR:
13040 if (TREE_CODE (type) == COMPLEX_TYPE)
13041 r_name = "max";
13042 break;
13043 case BIT_AND_EXPR:
13044 r_name = "&";
13045 break;
13046 case BIT_XOR_EXPR:
13047 r_name = "^";
13048 break;
13049 case BIT_IOR_EXPR:
13050 r_name = "|";
13051 break;
13052 case TRUTH_ANDIF_EXPR:
13053 if (FLOAT_TYPE_P (type))
13054 r_name = "&&";
13055 break;
13056 case TRUTH_ORIF_EXPR:
13057 if (FLOAT_TYPE_P (type))
13058 r_name = "||";
13059 break;
13060 default:
13061 gcc_unreachable ();
13063 if (r_name)
13065 error_at (OMP_CLAUSE_LOCATION (c),
13066 "%qE has invalid type for %<reduction(%s)%>",
13067 t, r_name);
13068 remove = true;
13069 break;
13072 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13074 error_at (OMP_CLAUSE_LOCATION (c),
13075 "user defined reduction not found for %qE", t);
13076 remove = true;
13077 break;
13079 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13081 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13082 type = TYPE_MAIN_VARIANT (type);
13083 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13084 VAR_DECL, NULL_TREE, type);
13085 tree decl_placeholder = NULL_TREE;
13086 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13087 DECL_ARTIFICIAL (placeholder) = 1;
13088 DECL_IGNORED_P (placeholder) = 1;
13089 if (TREE_CODE (t) == MEM_REF)
13091 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13092 VAR_DECL, NULL_TREE, type);
13093 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13094 DECL_ARTIFICIAL (decl_placeholder) = 1;
13095 DECL_IGNORED_P (decl_placeholder) = 1;
13097 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13098 c_mark_addressable (placeholder);
13099 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13100 c_mark_addressable (decl_placeholder ? decl_placeholder
13101 : OMP_CLAUSE_DECL (c));
13102 OMP_CLAUSE_REDUCTION_MERGE (c)
13103 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13104 TREE_VEC_ELT (list, 0),
13105 TREE_VEC_ELT (list, 1),
13106 decl_placeholder ? decl_placeholder
13107 : OMP_CLAUSE_DECL (c), placeholder);
13108 OMP_CLAUSE_REDUCTION_MERGE (c)
13109 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13110 void_type_node, NULL_TREE,
13111 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13112 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13113 if (TREE_VEC_LENGTH (list) == 6)
13115 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13116 c_mark_addressable (decl_placeholder ? decl_placeholder
13117 : OMP_CLAUSE_DECL (c));
13118 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13119 c_mark_addressable (placeholder);
13120 tree init = TREE_VEC_ELT (list, 5);
13121 if (init == error_mark_node)
13122 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13123 OMP_CLAUSE_REDUCTION_INIT (c)
13124 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13125 TREE_VEC_ELT (list, 3),
13126 decl_placeholder ? decl_placeholder
13127 : OMP_CLAUSE_DECL (c), placeholder);
13128 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13130 tree v = decl_placeholder ? decl_placeholder : t;
13131 OMP_CLAUSE_REDUCTION_INIT (c)
13132 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13133 OMP_CLAUSE_REDUCTION_INIT (c));
13135 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13136 c_find_omp_placeholder_r,
13137 placeholder, NULL))
13138 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13140 else
13142 tree init;
13143 tree v = decl_placeholder ? decl_placeholder : t;
13144 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13145 init = build_constructor (TREE_TYPE (v), NULL);
13146 else
13147 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13148 OMP_CLAUSE_REDUCTION_INIT (c)
13149 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13151 OMP_CLAUSE_REDUCTION_INIT (c)
13152 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13153 void_type_node, NULL_TREE,
13154 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13155 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13157 if (TREE_CODE (t) == MEM_REF)
13159 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13160 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13161 != INTEGER_CST)
13163 sorry ("variable length element type in array "
13164 "%<reduction%> clause");
13165 remove = true;
13166 break;
13168 t = TREE_OPERAND (t, 0);
13169 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13170 t = TREE_OPERAND (t, 0);
13171 if (TREE_CODE (t) == ADDR_EXPR)
13172 t = TREE_OPERAND (t, 0);
13174 goto check_dup_generic_t;
13176 case OMP_CLAUSE_COPYPRIVATE:
13177 copyprivate_seen = true;
13178 if (nowait_clause)
13180 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13181 "%<nowait%> clause must not be used together "
13182 "with %<copyprivate%>");
13183 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13184 nowait_clause = NULL;
13186 goto check_dup_generic;
13188 case OMP_CLAUSE_COPYIN:
13189 t = OMP_CLAUSE_DECL (c);
13190 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13192 error_at (OMP_CLAUSE_LOCATION (c),
13193 "%qE must be %<threadprivate%> for %<copyin%>", t);
13194 remove = true;
13195 break;
13197 goto check_dup_generic;
13199 case OMP_CLAUSE_LINEAR:
13200 if (ort != C_ORT_OMP_DECLARE_SIMD)
13201 need_implicitly_determined = true;
13202 t = OMP_CLAUSE_DECL (c);
13203 if (ort != C_ORT_OMP_DECLARE_SIMD
13204 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13206 error_at (OMP_CLAUSE_LOCATION (c),
13207 "modifier should not be specified in %<linear%> "
13208 "clause on %<simd%> or %<for%> constructs");
13209 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13211 if (ort & C_ORT_CILK)
13213 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13214 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13215 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13217 error_at (OMP_CLAUSE_LOCATION (c),
13218 "linear clause applied to non-integral, "
13219 "non-floating, non-pointer variable with type %qT",
13220 TREE_TYPE (t));
13221 remove = true;
13222 break;
13225 else
13227 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13228 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13230 error_at (OMP_CLAUSE_LOCATION (c),
13231 "linear clause applied to non-integral non-pointer "
13232 "variable with type %qT", TREE_TYPE (t));
13233 remove = true;
13234 break;
13236 if (TYPE_ATOMIC (TREE_TYPE (t)))
13238 error_at (OMP_CLAUSE_LOCATION (c),
13239 "%<_Atomic%> %qD in %<linear%> clause", t);
13240 remove = true;
13241 break;
13244 if (ort == C_ORT_OMP_DECLARE_SIMD)
13246 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13247 if (TREE_CODE (s) == PARM_DECL)
13249 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13250 /* map_head bitmap is used as uniform_head if
13251 declare_simd. */
13252 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13253 linear_variable_step_check = true;
13254 goto check_dup_generic;
13256 if (TREE_CODE (s) != INTEGER_CST)
13258 error_at (OMP_CLAUSE_LOCATION (c),
13259 "%<linear%> clause step %qE is neither constant "
13260 "nor a parameter", s);
13261 remove = true;
13262 break;
13265 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13267 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13268 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13269 OMP_CLAUSE_DECL (c), s);
13270 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13271 sizetype, fold_convert (sizetype, s),
13272 fold_convert
13273 (sizetype, OMP_CLAUSE_DECL (c)));
13274 if (s == error_mark_node)
13275 s = size_one_node;
13276 OMP_CLAUSE_LINEAR_STEP (c) = s;
13278 else
13279 OMP_CLAUSE_LINEAR_STEP (c)
13280 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13281 goto check_dup_generic;
13283 check_dup_generic:
13284 t = OMP_CLAUSE_DECL (c);
13285 check_dup_generic_t:
13286 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13288 error_at (OMP_CLAUSE_LOCATION (c),
13289 "%qE is not a variable in clause %qs", t,
13290 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13291 remove = true;
13293 else if (ort == C_ORT_ACC
13294 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13296 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13298 error ("%qD appears more than once in reduction clauses", t);
13299 remove = true;
13301 else
13302 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13304 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13305 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13306 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13308 error_at (OMP_CLAUSE_LOCATION (c),
13309 "%qE appears more than once in data clauses", t);
13310 remove = true;
13312 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13313 && bitmap_bit_p (&map_head, DECL_UID (t)))
13315 if (ort == C_ORT_ACC)
13316 error ("%qD appears more than once in data clauses", t);
13317 else
13318 error ("%qD appears both in data and map clauses", t);
13319 remove = true;
13321 else
13322 bitmap_set_bit (&generic_head, DECL_UID (t));
13323 break;
13325 case OMP_CLAUSE_FIRSTPRIVATE:
13326 t = OMP_CLAUSE_DECL (c);
13327 need_complete = true;
13328 need_implicitly_determined = true;
13329 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13331 error_at (OMP_CLAUSE_LOCATION (c),
13332 "%qE is not a variable in clause %<firstprivate%>", t);
13333 remove = true;
13335 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13336 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13338 error_at (OMP_CLAUSE_LOCATION (c),
13339 "%qE appears more than once in data clauses", t);
13340 remove = true;
13342 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13344 if (ort == C_ORT_ACC)
13345 error ("%qD appears more than once in data clauses", t);
13346 else
13347 error ("%qD appears both in data and map clauses", t);
13348 remove = true;
13350 else
13351 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13352 break;
13354 case OMP_CLAUSE_LASTPRIVATE:
13355 t = OMP_CLAUSE_DECL (c);
13356 need_complete = true;
13357 need_implicitly_determined = true;
13358 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13360 error_at (OMP_CLAUSE_LOCATION (c),
13361 "%qE is not a variable in clause %<lastprivate%>", t);
13362 remove = true;
13364 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13365 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13367 error_at (OMP_CLAUSE_LOCATION (c),
13368 "%qE appears more than once in data clauses", t);
13369 remove = true;
13371 else
13372 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13373 break;
13375 case OMP_CLAUSE_ALIGNED:
13376 t = OMP_CLAUSE_DECL (c);
13377 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13379 error_at (OMP_CLAUSE_LOCATION (c),
13380 "%qE is not a variable in %<aligned%> clause", t);
13381 remove = true;
13383 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13384 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13386 error_at (OMP_CLAUSE_LOCATION (c),
13387 "%qE in %<aligned%> clause is neither a pointer nor "
13388 "an array", t);
13389 remove = true;
13391 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13393 error_at (OMP_CLAUSE_LOCATION (c),
13394 "%<_Atomic%> %qD in %<aligned%> clause", t);
13395 remove = true;
13396 break;
13398 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13400 error_at (OMP_CLAUSE_LOCATION (c),
13401 "%qE appears more than once in %<aligned%> clauses",
13403 remove = true;
13405 else
13406 bitmap_set_bit (&aligned_head, DECL_UID (t));
13407 break;
13409 case OMP_CLAUSE_DEPEND:
13410 t = OMP_CLAUSE_DECL (c);
13411 if (t == NULL_TREE)
13413 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13414 == OMP_CLAUSE_DEPEND_SOURCE);
13415 break;
13417 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13419 gcc_assert (TREE_CODE (t) == TREE_LIST);
13420 for (; t; t = TREE_CHAIN (t))
13422 tree decl = TREE_VALUE (t);
13423 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13425 tree offset = TREE_PURPOSE (t);
13426 bool neg = wi::neg_p ((wide_int) offset);
13427 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13428 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13429 neg ? MINUS_EXPR : PLUS_EXPR,
13430 decl, offset);
13431 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13432 sizetype,
13433 fold_convert (sizetype, t2),
13434 fold_convert (sizetype, decl));
13435 if (t2 == error_mark_node)
13437 remove = true;
13438 break;
13440 TREE_PURPOSE (t) = t2;
13443 break;
13445 if (TREE_CODE (t) == TREE_LIST)
13447 if (handle_omp_array_sections (c, ort))
13448 remove = true;
13449 break;
13451 if (t == error_mark_node)
13452 remove = true;
13453 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13455 error_at (OMP_CLAUSE_LOCATION (c),
13456 "%qE is not a variable in %<depend%> clause", t);
13457 remove = true;
13459 else if (!c_mark_addressable (t))
13460 remove = true;
13461 break;
13463 case OMP_CLAUSE_MAP:
13464 case OMP_CLAUSE_TO:
13465 case OMP_CLAUSE_FROM:
13466 case OMP_CLAUSE__CACHE_:
13467 t = OMP_CLAUSE_DECL (c);
13468 if (TREE_CODE (t) == TREE_LIST)
13470 if (handle_omp_array_sections (c, ort))
13471 remove = true;
13472 else
13474 t = OMP_CLAUSE_DECL (c);
13475 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13477 error_at (OMP_CLAUSE_LOCATION (c),
13478 "array section does not have mappable type "
13479 "in %qs clause",
13480 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13481 remove = true;
13483 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13485 error_at (OMP_CLAUSE_LOCATION (c),
13486 "%<_Atomic%> %qE in %qs clause", t,
13487 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13488 remove = true;
13490 while (TREE_CODE (t) == ARRAY_REF)
13491 t = TREE_OPERAND (t, 0);
13492 if (TREE_CODE (t) == COMPONENT_REF
13493 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13495 while (TREE_CODE (t) == COMPONENT_REF)
13496 t = TREE_OPERAND (t, 0);
13497 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13498 break;
13499 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13501 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13502 error ("%qD appears more than once in motion"
13503 " clauses", t);
13504 else if (ort == C_ORT_ACC)
13505 error ("%qD appears more than once in data"
13506 " clauses", t);
13507 else
13508 error ("%qD appears more than once in map"
13509 " clauses", t);
13510 remove = true;
13512 else
13514 bitmap_set_bit (&map_head, DECL_UID (t));
13515 bitmap_set_bit (&map_field_head, DECL_UID (t));
13519 break;
13521 if (t == error_mark_node)
13523 remove = true;
13524 break;
13526 if (TREE_CODE (t) == COMPONENT_REF
13527 && (ort & C_ORT_OMP)
13528 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13530 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13532 error_at (OMP_CLAUSE_LOCATION (c),
13533 "bit-field %qE in %qs clause",
13534 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13535 remove = true;
13537 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13539 error_at (OMP_CLAUSE_LOCATION (c),
13540 "%qE does not have a mappable type in %qs clause",
13541 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13542 remove = true;
13544 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13546 error_at (OMP_CLAUSE_LOCATION (c),
13547 "%<_Atomic%> %qE in %qs clause", t,
13548 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13549 remove = true;
13551 while (TREE_CODE (t) == COMPONENT_REF)
13553 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13554 == UNION_TYPE)
13556 error_at (OMP_CLAUSE_LOCATION (c),
13557 "%qE is a member of a union", t);
13558 remove = true;
13559 break;
13561 t = TREE_OPERAND (t, 0);
13563 if (remove)
13564 break;
13565 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13567 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13568 break;
13571 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13573 error_at (OMP_CLAUSE_LOCATION (c),
13574 "%qE is not a variable in %qs clause", t,
13575 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13576 remove = true;
13578 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13580 error_at (OMP_CLAUSE_LOCATION (c),
13581 "%qD is threadprivate variable in %qs clause", t,
13582 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13583 remove = true;
13585 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13586 || (OMP_CLAUSE_MAP_KIND (c)
13587 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13588 && !c_mark_addressable (t))
13589 remove = true;
13590 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13591 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13592 || (OMP_CLAUSE_MAP_KIND (c)
13593 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13594 || (OMP_CLAUSE_MAP_KIND (c)
13595 == GOMP_MAP_FORCE_DEVICEPTR)))
13596 && t == OMP_CLAUSE_DECL (c)
13597 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13599 error_at (OMP_CLAUSE_LOCATION (c),
13600 "%qD does not have a mappable type in %qs clause", t,
13601 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13602 remove = true;
13604 else if (TREE_TYPE (t) == error_mark_node)
13605 remove = true;
13606 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13608 error_at (OMP_CLAUSE_LOCATION (c),
13609 "%<_Atomic%> %qE in %qs clause", t,
13610 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13611 remove = true;
13613 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13614 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13616 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13617 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13619 error ("%qD appears more than once in data clauses", t);
13620 remove = true;
13622 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13624 if (ort == C_ORT_ACC)
13625 error ("%qD appears more than once in data clauses", t);
13626 else
13627 error ("%qD appears both in data and map clauses", t);
13628 remove = true;
13630 else
13631 bitmap_set_bit (&generic_head, DECL_UID (t));
13633 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13635 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13636 error ("%qD appears more than once in motion clauses", t);
13637 else if (ort == C_ORT_ACC)
13638 error ("%qD appears more than once in data clauses", t);
13639 else
13640 error ("%qD appears more than once in map clauses", t);
13641 remove = true;
13643 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13644 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13646 if (ort == C_ORT_ACC)
13647 error ("%qD appears more than once in data clauses", t);
13648 else
13649 error ("%qD appears both in data and map clauses", t);
13650 remove = true;
13652 else
13654 bitmap_set_bit (&map_head, DECL_UID (t));
13655 if (t != OMP_CLAUSE_DECL (c)
13656 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13657 bitmap_set_bit (&map_field_head, DECL_UID (t));
13659 break;
13661 case OMP_CLAUSE_TO_DECLARE:
13662 case OMP_CLAUSE_LINK:
13663 t = OMP_CLAUSE_DECL (c);
13664 if (TREE_CODE (t) == FUNCTION_DECL
13665 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13667 else if (!VAR_P (t))
13669 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13670 error_at (OMP_CLAUSE_LOCATION (c),
13671 "%qE is neither a variable nor a function name in "
13672 "clause %qs", t,
13673 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13674 else
13675 error_at (OMP_CLAUSE_LOCATION (c),
13676 "%qE is not a variable in clause %qs", t,
13677 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13678 remove = true;
13680 else if (DECL_THREAD_LOCAL_P (t))
13682 error_at (OMP_CLAUSE_LOCATION (c),
13683 "%qD is threadprivate variable in %qs clause", t,
13684 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13685 remove = true;
13687 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13689 error_at (OMP_CLAUSE_LOCATION (c),
13690 "%qD does not have a mappable type in %qs clause", t,
13691 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13692 remove = true;
13694 if (remove)
13695 break;
13696 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13698 error_at (OMP_CLAUSE_LOCATION (c),
13699 "%qE appears more than once on the same "
13700 "%<declare target%> directive", t);
13701 remove = true;
13703 else
13704 bitmap_set_bit (&generic_head, DECL_UID (t));
13705 break;
13707 case OMP_CLAUSE_UNIFORM:
13708 t = OMP_CLAUSE_DECL (c);
13709 if (TREE_CODE (t) != PARM_DECL)
13711 if (DECL_P (t))
13712 error_at (OMP_CLAUSE_LOCATION (c),
13713 "%qD is not an argument in %<uniform%> clause", t);
13714 else
13715 error_at (OMP_CLAUSE_LOCATION (c),
13716 "%qE is not an argument in %<uniform%> clause", t);
13717 remove = true;
13718 break;
13720 /* map_head bitmap is used as uniform_head if declare_simd. */
13721 bitmap_set_bit (&map_head, DECL_UID (t));
13722 goto check_dup_generic;
13724 case OMP_CLAUSE_IS_DEVICE_PTR:
13725 case OMP_CLAUSE_USE_DEVICE_PTR:
13726 t = OMP_CLAUSE_DECL (c);
13727 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13728 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13730 error_at (OMP_CLAUSE_LOCATION (c),
13731 "%qs variable is neither a pointer nor an array",
13732 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13733 remove = true;
13735 goto check_dup_generic;
13737 case OMP_CLAUSE_NOWAIT:
13738 if (copyprivate_seen)
13740 error_at (OMP_CLAUSE_LOCATION (c),
13741 "%<nowait%> clause must not be used together "
13742 "with %<copyprivate%>");
13743 remove = true;
13744 break;
13746 nowait_clause = pc;
13747 pc = &OMP_CLAUSE_CHAIN (c);
13748 continue;
13750 case OMP_CLAUSE_IF:
13751 case OMP_CLAUSE_NUM_THREADS:
13752 case OMP_CLAUSE_NUM_TEAMS:
13753 case OMP_CLAUSE_THREAD_LIMIT:
13754 case OMP_CLAUSE_DEFAULT:
13755 case OMP_CLAUSE_UNTIED:
13756 case OMP_CLAUSE_COLLAPSE:
13757 case OMP_CLAUSE_FINAL:
13758 case OMP_CLAUSE_MERGEABLE:
13759 case OMP_CLAUSE_DEVICE:
13760 case OMP_CLAUSE_DIST_SCHEDULE:
13761 case OMP_CLAUSE_PARALLEL:
13762 case OMP_CLAUSE_FOR:
13763 case OMP_CLAUSE_SECTIONS:
13764 case OMP_CLAUSE_TASKGROUP:
13765 case OMP_CLAUSE_PROC_BIND:
13766 case OMP_CLAUSE_PRIORITY:
13767 case OMP_CLAUSE_GRAINSIZE:
13768 case OMP_CLAUSE_NUM_TASKS:
13769 case OMP_CLAUSE_NOGROUP:
13770 case OMP_CLAUSE_THREADS:
13771 case OMP_CLAUSE_SIMD:
13772 case OMP_CLAUSE_HINT:
13773 case OMP_CLAUSE_DEFAULTMAP:
13774 case OMP_CLAUSE__CILK_FOR_COUNT_:
13775 case OMP_CLAUSE_NUM_GANGS:
13776 case OMP_CLAUSE_NUM_WORKERS:
13777 case OMP_CLAUSE_VECTOR_LENGTH:
13778 case OMP_CLAUSE_ASYNC:
13779 case OMP_CLAUSE_WAIT:
13780 case OMP_CLAUSE_AUTO:
13781 case OMP_CLAUSE_INDEPENDENT:
13782 case OMP_CLAUSE_SEQ:
13783 case OMP_CLAUSE_GANG:
13784 case OMP_CLAUSE_WORKER:
13785 case OMP_CLAUSE_VECTOR:
13786 case OMP_CLAUSE_TILE:
13787 pc = &OMP_CLAUSE_CHAIN (c);
13788 continue;
13790 case OMP_CLAUSE_SCHEDULE:
13791 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13793 const char *p = NULL;
13794 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13796 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13797 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13798 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13799 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13800 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13801 default: gcc_unreachable ();
13803 if (p)
13805 error_at (OMP_CLAUSE_LOCATION (c),
13806 "%<nonmonotonic%> modifier specified for %qs "
13807 "schedule kind", p);
13808 OMP_CLAUSE_SCHEDULE_KIND (c)
13809 = (enum omp_clause_schedule_kind)
13810 (OMP_CLAUSE_SCHEDULE_KIND (c)
13811 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13814 schedule_clause = c;
13815 pc = &OMP_CLAUSE_CHAIN (c);
13816 continue;
13818 case OMP_CLAUSE_ORDERED:
13819 ordered_seen = true;
13820 pc = &OMP_CLAUSE_CHAIN (c);
13821 continue;
13823 case OMP_CLAUSE_SAFELEN:
13824 safelen = c;
13825 pc = &OMP_CLAUSE_CHAIN (c);
13826 continue;
13827 case OMP_CLAUSE_SIMDLEN:
13828 simdlen = c;
13829 pc = &OMP_CLAUSE_CHAIN (c);
13830 continue;
13832 case OMP_CLAUSE_INBRANCH:
13833 case OMP_CLAUSE_NOTINBRANCH:
13834 if (branch_seen)
13836 error_at (OMP_CLAUSE_LOCATION (c),
13837 "%<inbranch%> clause is incompatible with "
13838 "%<notinbranch%>");
13839 remove = true;
13840 break;
13842 branch_seen = true;
13843 pc = &OMP_CLAUSE_CHAIN (c);
13844 continue;
13846 default:
13847 gcc_unreachable ();
13850 if (!remove)
13852 t = OMP_CLAUSE_DECL (c);
13854 if (need_complete)
13856 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13857 if (t == error_mark_node)
13858 remove = true;
13861 if (need_implicitly_determined)
13863 const char *share_name = NULL;
13865 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13866 share_name = "threadprivate";
13867 else switch (c_omp_predetermined_sharing (t))
13869 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13870 break;
13871 case OMP_CLAUSE_DEFAULT_SHARED:
13872 /* const vars may be specified in firstprivate clause. */
13873 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13874 && TREE_READONLY (t))
13875 break;
13876 share_name = "shared";
13877 break;
13878 case OMP_CLAUSE_DEFAULT_PRIVATE:
13879 share_name = "private";
13880 break;
13881 default:
13882 gcc_unreachable ();
13884 if (share_name)
13886 error_at (OMP_CLAUSE_LOCATION (c),
13887 "%qE is predetermined %qs for %qs",
13888 t, share_name,
13889 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13890 remove = true;
13895 if (remove)
13896 *pc = OMP_CLAUSE_CHAIN (c);
13897 else
13898 pc = &OMP_CLAUSE_CHAIN (c);
13901 if (simdlen
13902 && safelen
13903 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13904 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13906 error_at (OMP_CLAUSE_LOCATION (simdlen),
13907 "%<simdlen%> clause value is bigger than "
13908 "%<safelen%> clause value");
13909 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13910 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13913 if (ordered_seen
13914 && schedule_clause
13915 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13916 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13918 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13919 "%<nonmonotonic%> schedule modifier specified together "
13920 "with %<ordered%> clause");
13921 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13922 = (enum omp_clause_schedule_kind)
13923 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13924 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13927 if (linear_variable_step_check)
13928 for (pc = &clauses, c = clauses; c ; c = *pc)
13930 bool remove = false;
13931 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13932 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13933 && !bitmap_bit_p (&map_head,
13934 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13936 error_at (OMP_CLAUSE_LOCATION (c),
13937 "%<linear%> clause step is a parameter %qD not "
13938 "specified in %<uniform%> clause",
13939 OMP_CLAUSE_LINEAR_STEP (c));
13940 remove = true;
13943 if (remove)
13944 *pc = OMP_CLAUSE_CHAIN (c);
13945 else
13946 pc = &OMP_CLAUSE_CHAIN (c);
13949 bitmap_obstack_release (NULL);
13950 return clauses;
13953 /* Return code to initialize DST with a copy constructor from SRC.
13954 C doesn't have copy constructors nor assignment operators, only for
13955 _Atomic vars we need to perform __atomic_load from src into a temporary
13956 followed by __atomic_store of the temporary to dst. */
13958 tree
13959 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13961 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13962 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13964 location_t loc = OMP_CLAUSE_LOCATION (clause);
13965 tree type = TREE_TYPE (dst);
13966 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13967 tree tmp = create_tmp_var (nonatomic_type);
13968 tree tmp_addr = build_fold_addr_expr (tmp);
13969 TREE_ADDRESSABLE (tmp) = 1;
13970 TREE_NO_WARNING (tmp) = 1;
13971 tree src_addr = build_fold_addr_expr (src);
13972 tree dst_addr = build_fold_addr_expr (dst);
13973 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13974 vec<tree, va_gc> *params;
13975 /* Expansion of a generic atomic load may require an addition
13976 element, so allocate enough to prevent a resize. */
13977 vec_alloc (params, 4);
13979 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13980 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13981 params->quick_push (src_addr);
13982 params->quick_push (tmp_addr);
13983 params->quick_push (seq_cst);
13984 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13986 vec_alloc (params, 4);
13988 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13989 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13990 params->quick_push (dst_addr);
13991 params->quick_push (tmp_addr);
13992 params->quick_push (seq_cst);
13993 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13994 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13997 /* Create a transaction node. */
13999 tree
14000 c_finish_transaction (location_t loc, tree block, int flags)
14002 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14003 if (flags & TM_STMT_ATTR_OUTER)
14004 TRANSACTION_EXPR_OUTER (stmt) = 1;
14005 if (flags & TM_STMT_ATTR_RELAXED)
14006 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14007 return add_stmt (stmt);
14010 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14011 down to the element type of an array. If ORIG_QUAL_TYPE is not
14012 NULL, then it should be used as the qualified type
14013 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14014 preserve information about the typedef name from which an array
14015 type was derived). */
14017 tree
14018 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14019 size_t orig_qual_indirect)
14021 if (type == error_mark_node)
14022 return type;
14024 if (TREE_CODE (type) == ARRAY_TYPE)
14026 tree t;
14027 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14028 type_quals, orig_qual_type,
14029 orig_qual_indirect - 1);
14031 /* See if we already have an identically qualified type. */
14032 if (orig_qual_type && orig_qual_indirect == 0)
14033 t = orig_qual_type;
14034 else
14035 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14037 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14038 && TYPE_NAME (t) == TYPE_NAME (type)
14039 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14040 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14041 TYPE_ATTRIBUTES (type)))
14042 break;
14044 if (!t)
14046 tree domain = TYPE_DOMAIN (type);
14048 t = build_variant_type_copy (type);
14049 TREE_TYPE (t) = element_type;
14051 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14052 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14053 SET_TYPE_STRUCTURAL_EQUALITY (t);
14054 else if (TYPE_CANONICAL (element_type) != element_type
14055 || (domain && TYPE_CANONICAL (domain) != domain))
14057 tree unqualified_canon
14058 = build_array_type (TYPE_CANONICAL (element_type),
14059 domain? TYPE_CANONICAL (domain)
14060 : NULL_TREE);
14061 if (TYPE_REVERSE_STORAGE_ORDER (type))
14063 unqualified_canon
14064 = build_distinct_type_copy (unqualified_canon);
14065 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14067 TYPE_CANONICAL (t)
14068 = c_build_qualified_type (unqualified_canon, type_quals);
14070 else
14071 TYPE_CANONICAL (t) = t;
14073 return t;
14076 /* A restrict-qualified pointer type must be a pointer to object or
14077 incomplete type. Note that the use of POINTER_TYPE_P also allows
14078 REFERENCE_TYPEs, which is appropriate for C++. */
14079 if ((type_quals & TYPE_QUAL_RESTRICT)
14080 && (!POINTER_TYPE_P (type)
14081 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14083 error ("invalid use of %<restrict%>");
14084 type_quals &= ~TYPE_QUAL_RESTRICT;
14087 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14088 ? orig_qual_type
14089 : build_qualified_type (type, type_quals));
14090 /* A variant type does not inherit the list of incomplete vars from the
14091 type main variant. */
14092 if (RECORD_OR_UNION_TYPE_P (var_type)
14093 && TYPE_MAIN_VARIANT (var_type) != var_type)
14094 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14095 return var_type;
14098 /* Build a VA_ARG_EXPR for the C parser. */
14100 tree
14101 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14103 if (error_operand_p (type))
14104 return error_mark_node;
14105 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14106 order because it takes the address of the expression. */
14107 else if (handled_component_p (expr)
14108 && reverse_storage_order_for_component_p (expr))
14110 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14111 return error_mark_node;
14113 else if (!COMPLETE_TYPE_P (type))
14115 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14116 "type %qT", type);
14117 return error_mark_node;
14119 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14120 warning_at (loc2, OPT_Wc___compat,
14121 "C++ requires promoted type, not enum type, in %<va_arg%>");
14122 return build_va_arg (loc2, expr, type);
14125 /* Return truthvalue of whether T1 is the same tree structure as T2.
14126 Return 1 if they are the same. Return false if they are different. */
14128 bool
14129 c_tree_equal (tree t1, tree t2)
14131 enum tree_code code1, code2;
14133 if (t1 == t2)
14134 return true;
14135 if (!t1 || !t2)
14136 return false;
14138 for (code1 = TREE_CODE (t1);
14139 CONVERT_EXPR_CODE_P (code1)
14140 || code1 == NON_LVALUE_EXPR;
14141 code1 = TREE_CODE (t1))
14142 t1 = TREE_OPERAND (t1, 0);
14143 for (code2 = TREE_CODE (t2);
14144 CONVERT_EXPR_CODE_P (code2)
14145 || code2 == NON_LVALUE_EXPR;
14146 code2 = TREE_CODE (t2))
14147 t2 = TREE_OPERAND (t2, 0);
14149 /* They might have become equal now. */
14150 if (t1 == t2)
14151 return true;
14153 if (code1 != code2)
14154 return false;
14156 switch (code1)
14158 case INTEGER_CST:
14159 return wi::eq_p (t1, t2);
14161 case REAL_CST:
14162 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14164 case STRING_CST:
14165 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14166 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14167 TREE_STRING_LENGTH (t1));
14169 case FIXED_CST:
14170 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14171 TREE_FIXED_CST (t2));
14173 case COMPLEX_CST:
14174 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14175 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14177 case VECTOR_CST:
14178 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14180 case CONSTRUCTOR:
14181 /* We need to do this when determining whether or not two
14182 non-type pointer to member function template arguments
14183 are the same. */
14184 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14185 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14186 return false;
14188 tree field, value;
14189 unsigned int i;
14190 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14192 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14193 if (!c_tree_equal (field, elt2->index)
14194 || !c_tree_equal (value, elt2->value))
14195 return false;
14198 return true;
14200 case TREE_LIST:
14201 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14202 return false;
14203 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14204 return false;
14205 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14207 case SAVE_EXPR:
14208 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14210 case CALL_EXPR:
14212 tree arg1, arg2;
14213 call_expr_arg_iterator iter1, iter2;
14214 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14215 return false;
14216 for (arg1 = first_call_expr_arg (t1, &iter1),
14217 arg2 = first_call_expr_arg (t2, &iter2);
14218 arg1 && arg2;
14219 arg1 = next_call_expr_arg (&iter1),
14220 arg2 = next_call_expr_arg (&iter2))
14221 if (!c_tree_equal (arg1, arg2))
14222 return false;
14223 if (arg1 || arg2)
14224 return false;
14225 return true;
14228 case TARGET_EXPR:
14230 tree o1 = TREE_OPERAND (t1, 0);
14231 tree o2 = TREE_OPERAND (t2, 0);
14233 /* Special case: if either target is an unallocated VAR_DECL,
14234 it means that it's going to be unified with whatever the
14235 TARGET_EXPR is really supposed to initialize, so treat it
14236 as being equivalent to anything. */
14237 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14238 && !DECL_RTL_SET_P (o1))
14239 /*Nop*/;
14240 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14241 && !DECL_RTL_SET_P (o2))
14242 /*Nop*/;
14243 else if (!c_tree_equal (o1, o2))
14244 return false;
14246 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14249 case COMPONENT_REF:
14250 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14251 return false;
14252 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14254 case PARM_DECL:
14255 case VAR_DECL:
14256 case CONST_DECL:
14257 case FIELD_DECL:
14258 case FUNCTION_DECL:
14259 case IDENTIFIER_NODE:
14260 case SSA_NAME:
14261 return false;
14263 case TREE_VEC:
14265 unsigned ix;
14266 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14267 return false;
14268 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14269 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14270 TREE_VEC_ELT (t2, ix)))
14271 return false;
14272 return true;
14275 default:
14276 break;
14279 switch (TREE_CODE_CLASS (code1))
14281 case tcc_unary:
14282 case tcc_binary:
14283 case tcc_comparison:
14284 case tcc_expression:
14285 case tcc_vl_exp:
14286 case tcc_reference:
14287 case tcc_statement:
14289 int i, n = TREE_OPERAND_LENGTH (t1);
14291 switch (code1)
14293 case PREINCREMENT_EXPR:
14294 case PREDECREMENT_EXPR:
14295 case POSTINCREMENT_EXPR:
14296 case POSTDECREMENT_EXPR:
14297 n = 1;
14298 break;
14299 case ARRAY_REF:
14300 n = 2;
14301 break;
14302 default:
14303 break;
14306 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14307 && n != TREE_OPERAND_LENGTH (t2))
14308 return false;
14310 for (i = 0; i < n; ++i)
14311 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14312 return false;
14314 return true;
14317 case tcc_type:
14318 return comptypes (t1, t2);
14319 default:
14320 gcc_unreachable ();
14322 /* We can get here with --disable-checking. */
14323 return false;
14326 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14327 spawn-helper and BODY is the newly created body for FNDECL. */
14329 void
14330 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14332 tree list = alloc_stmt_list ();
14333 tree frame = make_cilk_frame (fndecl);
14334 tree dtor = create_cilk_function_exit (frame, false, true);
14335 add_local_decl (cfun, frame);
14337 DECL_SAVED_TREE (fndecl) = list;
14339 tree body_list = alloc_stmt_list ();
14340 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14341 body = fold_build_cleanup_point_expr (void_type_node, body);
14343 append_to_statement_list (body, &body_list);
14344 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14345 body_list, dtor), &list);
14348 /* Returns true when the function declaration FNDECL is implicit,
14349 introduced as a result of a call to an otherwise undeclared
14350 function, and false otherwise. */
14352 bool
14353 c_decl_implicit (const_tree fndecl)
14355 return C_DECL_IMPLICIT (fndecl);