* cfgloopmanip.c (duplicate_loop_to_header_edge): Cleanup profile
[official-gcc.git] / gcc / c / c-typeck.c
blob4bdc48a9ea38d1e0e235b3b51cbf03090a182799
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 "%qT has no member named %qE; did you mean %qE?",
2411 type, component, guessed_id);
2413 else
2414 error_at (loc, "%qT has no member named %qE", type, component);
2415 return error_mark_node;
2418 /* Accessing elements of atomic structures or unions is undefined
2419 behavior (C11 6.5.2.3#5). */
2420 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2422 if (code == RECORD_TYPE)
2423 warning_at (loc, 0, "accessing a member %qE of an atomic "
2424 "structure %qE", component, datum);
2425 else
2426 warning_at (loc, 0, "accessing a member %qE of an atomic "
2427 "union %qE", component, datum);
2430 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2431 This might be better solved in future the way the C++ front
2432 end does it - by giving the anonymous entities each a
2433 separate name and type, and then have build_component_ref
2434 recursively call itself. We can't do that here. */
2437 tree subdatum = TREE_VALUE (field);
2438 int quals;
2439 tree subtype;
2440 bool use_datum_quals;
2442 if (TREE_TYPE (subdatum) == error_mark_node)
2443 return error_mark_node;
2445 /* If this is an rvalue, it does not have qualifiers in C
2446 standard terms and we must avoid propagating such
2447 qualifiers down to a non-lvalue array that is then
2448 converted to a pointer. */
2449 use_datum_quals = (datum_lvalue
2450 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2452 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2453 if (use_datum_quals)
2454 quals |= TYPE_QUALS (TREE_TYPE (datum));
2455 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2457 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2458 NULL_TREE);
2459 SET_EXPR_LOCATION (ref, loc);
2460 if (TREE_READONLY (subdatum)
2461 || (use_datum_quals && TREE_READONLY (datum)))
2462 TREE_READONLY (ref) = 1;
2463 if (TREE_THIS_VOLATILE (subdatum)
2464 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2465 TREE_THIS_VOLATILE (ref) = 1;
2467 if (TREE_DEPRECATED (subdatum))
2468 warn_deprecated_use (subdatum, NULL_TREE);
2470 datum = ref;
2472 field = TREE_CHAIN (field);
2474 while (field);
2476 return ref;
2478 else if (should_suggest_deref_p (type))
2480 /* Special-case the error message for "ptr.field" for the case
2481 where the user has confused "." vs "->". */
2482 rich_location richloc (line_table, loc);
2483 /* "loc" should be the "." token. */
2484 richloc.add_fixit_replace ("->");
2485 error_at (&richloc,
2486 "%qE is a pointer; did you mean to use %<->%>?",
2487 datum);
2488 return error_mark_node;
2490 else if (code != ERROR_MARK)
2491 error_at (loc,
2492 "request for member %qE in something not a structure or union",
2493 component);
2495 return error_mark_node;
2498 /* Given an expression PTR for a pointer, return an expression
2499 for the value pointed to.
2500 ERRORSTRING is the name of the operator to appear in error messages.
2502 LOC is the location to use for the generated tree. */
2504 tree
2505 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2507 tree pointer = default_conversion (ptr);
2508 tree type = TREE_TYPE (pointer);
2509 tree ref;
2511 if (TREE_CODE (type) == POINTER_TYPE)
2513 if (CONVERT_EXPR_P (pointer)
2514 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2516 /* If a warning is issued, mark it to avoid duplicates from
2517 the backend. This only needs to be done at
2518 warn_strict_aliasing > 2. */
2519 if (warn_strict_aliasing > 2)
2520 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2521 type, TREE_OPERAND (pointer, 0)))
2522 TREE_NO_WARNING (pointer) = 1;
2525 if (TREE_CODE (pointer) == ADDR_EXPR
2526 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2527 == TREE_TYPE (type)))
2529 ref = TREE_OPERAND (pointer, 0);
2530 protected_set_expr_location (ref, loc);
2531 return ref;
2533 else
2535 tree t = TREE_TYPE (type);
2537 ref = build1 (INDIRECT_REF, t, pointer);
2539 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2541 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2543 error_at (loc, "dereferencing pointer to incomplete type "
2544 "%qT", t);
2545 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2547 return error_mark_node;
2549 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2550 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2552 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2553 so that we get the proper error message if the result is used
2554 to assign to. Also, &* is supposed to be a no-op.
2555 And ANSI C seems to specify that the type of the result
2556 should be the const type. */
2557 /* A de-reference of a pointer to const is not a const. It is valid
2558 to change it via some other pointer. */
2559 TREE_READONLY (ref) = TYPE_READONLY (t);
2560 TREE_SIDE_EFFECTS (ref)
2561 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2562 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2563 protected_set_expr_location (ref, loc);
2564 return ref;
2567 else if (TREE_CODE (pointer) != ERROR_MARK)
2568 invalid_indirection_error (loc, type, errstring);
2570 return error_mark_node;
2573 /* This handles expressions of the form "a[i]", which denotes
2574 an array reference.
2576 This is logically equivalent in C to *(a+i), but we may do it differently.
2577 If A is a variable or a member, we generate a primitive ARRAY_REF.
2578 This avoids forcing the array out of registers, and can work on
2579 arrays that are not lvalues (for example, members of structures returned
2580 by functions).
2582 For vector types, allow vector[i] but not i[vector], and create
2583 *(((type*)&vectortype) + i) for the expression.
2585 LOC is the location to use for the returned expression. */
2587 tree
2588 build_array_ref (location_t loc, tree array, tree index)
2590 tree ret;
2591 bool swapped = false;
2592 if (TREE_TYPE (array) == error_mark_node
2593 || TREE_TYPE (index) == error_mark_node)
2594 return error_mark_node;
2596 if (flag_cilkplus && contains_array_notation_expr (index))
2598 size_t rank = 0;
2599 if (!find_rank (loc, index, index, true, &rank))
2600 return error_mark_node;
2601 if (rank > 1)
2603 error_at (loc, "rank of the array's index is greater than 1");
2604 return error_mark_node;
2607 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2608 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2609 /* Allow vector[index] but not index[vector]. */
2610 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2612 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2613 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2615 error_at (loc,
2616 "subscripted value is neither array nor pointer nor vector");
2618 return error_mark_node;
2620 std::swap (array, index);
2621 swapped = true;
2624 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2626 error_at (loc, "array subscript is not an integer");
2627 return error_mark_node;
2630 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2632 error_at (loc, "subscripted value is pointer to function");
2633 return error_mark_node;
2636 /* ??? Existing practice has been to warn only when the char
2637 index is syntactically the index, not for char[array]. */
2638 if (!swapped)
2639 warn_array_subscript_with_type_char (loc, index);
2641 /* Apply default promotions *after* noticing character types. */
2642 index = default_conversion (index);
2643 if (index == error_mark_node)
2644 return error_mark_node;
2646 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2648 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2649 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2651 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2653 tree rval, type;
2655 /* An array that is indexed by a non-constant
2656 cannot be stored in a register; we must be able to do
2657 address arithmetic on its address.
2658 Likewise an array of elements of variable size. */
2659 if (TREE_CODE (index) != INTEGER_CST
2660 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2661 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2663 if (!c_mark_addressable (array, true))
2664 return error_mark_node;
2666 /* An array that is indexed by a constant value which is not within
2667 the array bounds cannot be stored in a register either; because we
2668 would get a crash in store_bit_field/extract_bit_field when trying
2669 to access a non-existent part of the register. */
2670 if (TREE_CODE (index) == INTEGER_CST
2671 && TYPE_DOMAIN (TREE_TYPE (array))
2672 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2674 if (!c_mark_addressable (array))
2675 return error_mark_node;
2678 if ((pedantic || warn_c90_c99_compat)
2679 && ! was_vector)
2681 tree foo = array;
2682 while (TREE_CODE (foo) == COMPONENT_REF)
2683 foo = TREE_OPERAND (foo, 0);
2684 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2685 pedwarn (loc, OPT_Wpedantic,
2686 "ISO C forbids subscripting %<register%> array");
2687 else if (!lvalue_p (foo))
2688 pedwarn_c90 (loc, OPT_Wpedantic,
2689 "ISO C90 forbids subscripting non-lvalue "
2690 "array");
2693 type = TREE_TYPE (TREE_TYPE (array));
2694 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2695 /* Array ref is const/volatile if the array elements are
2696 or if the array is. */
2697 TREE_READONLY (rval)
2698 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2699 | TREE_READONLY (array));
2700 TREE_SIDE_EFFECTS (rval)
2701 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2702 | TREE_SIDE_EFFECTS (array));
2703 TREE_THIS_VOLATILE (rval)
2704 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2705 /* This was added by rms on 16 Nov 91.
2706 It fixes vol struct foo *a; a->elts[1]
2707 in an inline function.
2708 Hope it doesn't break something else. */
2709 | TREE_THIS_VOLATILE (array));
2710 ret = require_complete_type (loc, rval);
2711 protected_set_expr_location (ret, loc);
2712 if (non_lvalue)
2713 ret = non_lvalue_loc (loc, ret);
2714 return ret;
2716 else
2718 tree ar = default_conversion (array);
2720 if (ar == error_mark_node)
2721 return ar;
2723 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2724 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2726 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2727 index, false),
2728 RO_ARRAY_INDEXING);
2729 if (non_lvalue)
2730 ret = non_lvalue_loc (loc, ret);
2731 return ret;
2735 /* Build an external reference to identifier ID. FUN indicates
2736 whether this will be used for a function call. LOC is the source
2737 location of the identifier. This sets *TYPE to the type of the
2738 identifier, which is not the same as the type of the returned value
2739 for CONST_DECLs defined as enum constants. If the type of the
2740 identifier is not available, *TYPE is set to NULL. */
2741 tree
2742 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2744 tree ref;
2745 tree decl = lookup_name (id);
2747 /* In Objective-C, an instance variable (ivar) may be preferred to
2748 whatever lookup_name() found. */
2749 decl = objc_lookup_ivar (decl, id);
2751 *type = NULL;
2752 if (decl && decl != error_mark_node)
2754 ref = decl;
2755 *type = TREE_TYPE (ref);
2757 else if (fun)
2758 /* Implicit function declaration. */
2759 ref = implicitly_declare (loc, id);
2760 else if (decl == error_mark_node)
2761 /* Don't complain about something that's already been
2762 complained about. */
2763 return error_mark_node;
2764 else
2766 undeclared_variable (loc, id);
2767 return error_mark_node;
2770 if (TREE_TYPE (ref) == error_mark_node)
2771 return error_mark_node;
2773 if (TREE_DEPRECATED (ref))
2774 warn_deprecated_use (ref, NULL_TREE);
2776 /* Recursive call does not count as usage. */
2777 if (ref != current_function_decl)
2779 TREE_USED (ref) = 1;
2782 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2784 if (!in_sizeof && !in_typeof)
2785 C_DECL_USED (ref) = 1;
2786 else if (DECL_INITIAL (ref) == NULL_TREE
2787 && DECL_EXTERNAL (ref)
2788 && !TREE_PUBLIC (ref))
2789 record_maybe_used_decl (ref);
2792 if (TREE_CODE (ref) == CONST_DECL)
2794 used_types_insert (TREE_TYPE (ref));
2796 if (warn_cxx_compat
2797 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2798 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2800 warning_at (loc, OPT_Wc___compat,
2801 ("enum constant defined in struct or union "
2802 "is not visible in C++"));
2803 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2806 ref = DECL_INITIAL (ref);
2807 TREE_CONSTANT (ref) = 1;
2809 else if (current_function_decl != NULL_TREE
2810 && !DECL_FILE_SCOPE_P (current_function_decl)
2811 && (VAR_OR_FUNCTION_DECL_P (ref)
2812 || TREE_CODE (ref) == PARM_DECL))
2814 tree context = decl_function_context (ref);
2816 if (context != NULL_TREE && context != current_function_decl)
2817 DECL_NONLOCAL (ref) = 1;
2819 /* C99 6.7.4p3: An inline definition of a function with external
2820 linkage ... shall not contain a reference to an identifier with
2821 internal linkage. */
2822 else if (current_function_decl != NULL_TREE
2823 && DECL_DECLARED_INLINE_P (current_function_decl)
2824 && DECL_EXTERNAL (current_function_decl)
2825 && VAR_OR_FUNCTION_DECL_P (ref)
2826 && (!VAR_P (ref) || TREE_STATIC (ref))
2827 && ! TREE_PUBLIC (ref)
2828 && DECL_CONTEXT (ref) != current_function_decl)
2829 record_inline_static (loc, current_function_decl, ref,
2830 csi_internal);
2832 return ref;
2835 /* Record details of decls possibly used inside sizeof or typeof. */
2836 struct maybe_used_decl
2838 /* The decl. */
2839 tree decl;
2840 /* The level seen at (in_sizeof + in_typeof). */
2841 int level;
2842 /* The next one at this level or above, or NULL. */
2843 struct maybe_used_decl *next;
2846 static struct maybe_used_decl *maybe_used_decls;
2848 /* Record that DECL, an undefined static function reference seen
2849 inside sizeof or typeof, might be used if the operand of sizeof is
2850 a VLA type or the operand of typeof is a variably modified
2851 type. */
2853 static void
2854 record_maybe_used_decl (tree decl)
2856 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2857 t->decl = decl;
2858 t->level = in_sizeof + in_typeof;
2859 t->next = maybe_used_decls;
2860 maybe_used_decls = t;
2863 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2864 USED is false, just discard them. If it is true, mark them used
2865 (if no longer inside sizeof or typeof) or move them to the next
2866 level up (if still inside sizeof or typeof). */
2868 void
2869 pop_maybe_used (bool used)
2871 struct maybe_used_decl *p = maybe_used_decls;
2872 int cur_level = in_sizeof + in_typeof;
2873 while (p && p->level > cur_level)
2875 if (used)
2877 if (cur_level == 0)
2878 C_DECL_USED (p->decl) = 1;
2879 else
2880 p->level = cur_level;
2882 p = p->next;
2884 if (!used || cur_level == 0)
2885 maybe_used_decls = p;
2888 /* Return the result of sizeof applied to EXPR. */
2890 struct c_expr
2891 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2893 struct c_expr ret;
2894 if (expr.value == error_mark_node)
2896 ret.value = error_mark_node;
2897 ret.original_code = ERROR_MARK;
2898 ret.original_type = NULL;
2899 pop_maybe_used (false);
2901 else
2903 bool expr_const_operands = true;
2905 if (TREE_CODE (expr.value) == PARM_DECL
2906 && C_ARRAY_PARAMETER (expr.value))
2908 if (warning_at (loc, OPT_Wsizeof_array_argument,
2909 "%<sizeof%> on array function parameter %qE will "
2910 "return size of %qT", expr.value,
2911 TREE_TYPE (expr.value)))
2912 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2914 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2915 &expr_const_operands);
2916 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2917 c_last_sizeof_arg = expr.value;
2918 c_last_sizeof_loc = loc;
2919 ret.original_code = SIZEOF_EXPR;
2920 ret.original_type = NULL;
2921 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2923 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2924 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2925 folded_expr, ret.value);
2926 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2927 SET_EXPR_LOCATION (ret.value, loc);
2929 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2931 return ret;
2934 /* Return the result of sizeof applied to T, a structure for the type
2935 name passed to sizeof (rather than the type itself). LOC is the
2936 location of the original expression. */
2938 struct c_expr
2939 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2941 tree type;
2942 struct c_expr ret;
2943 tree type_expr = NULL_TREE;
2944 bool type_expr_const = true;
2945 type = groktypename (t, &type_expr, &type_expr_const);
2946 ret.value = c_sizeof (loc, type);
2947 c_last_sizeof_arg = type;
2948 c_last_sizeof_loc = loc;
2949 ret.original_code = SIZEOF_EXPR;
2950 ret.original_type = NULL;
2951 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2952 && c_vla_type_p (type))
2954 /* If the type is a [*] array, it is a VLA but is represented as
2955 having a size of zero. In such a case we must ensure that
2956 the result of sizeof does not get folded to a constant by
2957 c_fully_fold, because if the size is evaluated the result is
2958 not constant and so constraints on zero or negative size
2959 arrays must not be applied when this sizeof call is inside
2960 another array declarator. */
2961 if (!type_expr)
2962 type_expr = integer_zero_node;
2963 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2964 type_expr, ret.value);
2965 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2967 pop_maybe_used (type != error_mark_node
2968 ? C_TYPE_VARIABLE_SIZE (type) : false);
2969 return ret;
2972 /* Build a function call to function FUNCTION with parameters PARAMS.
2973 The function call is at LOC.
2974 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2975 TREE_VALUE of each node is a parameter-expression.
2976 FUNCTION's data type may be a function type or a pointer-to-function. */
2978 tree
2979 build_function_call (location_t loc, tree function, tree params)
2981 vec<tree, va_gc> *v;
2982 tree ret;
2984 vec_alloc (v, list_length (params));
2985 for (; params; params = TREE_CHAIN (params))
2986 v->quick_push (TREE_VALUE (params));
2987 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2988 vec_free (v);
2989 return ret;
2992 /* Give a note about the location of the declaration of DECL. */
2994 static void
2995 inform_declaration (tree decl)
2997 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2998 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3001 /* Build a function call to function FUNCTION with parameters PARAMS.
3002 ORIGTYPES, if not NULL, is a vector of types; each element is
3003 either NULL or the original type of the corresponding element in
3004 PARAMS. The original type may differ from TREE_TYPE of the
3005 parameter for enums. FUNCTION's data type may be a function type
3006 or pointer-to-function. This function changes the elements of
3007 PARAMS. */
3009 tree
3010 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3011 tree function, vec<tree, va_gc> *params,
3012 vec<tree, va_gc> *origtypes)
3014 tree fntype, fundecl = NULL_TREE;
3015 tree name = NULL_TREE, result;
3016 tree tem;
3017 int nargs;
3018 tree *argarray;
3021 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3022 STRIP_TYPE_NOPS (function);
3024 /* Convert anything with function type to a pointer-to-function. */
3025 if (TREE_CODE (function) == FUNCTION_DECL)
3027 name = DECL_NAME (function);
3029 if (flag_tm)
3030 tm_malloc_replacement (function);
3031 fundecl = function;
3032 /* Atomic functions have type checking/casting already done. They are
3033 often rewritten and don't match the original parameter list. */
3034 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3035 origtypes = NULL;
3037 if (flag_cilkplus
3038 && is_cilkplus_reduce_builtin (function))
3039 origtypes = NULL;
3041 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3042 function = function_to_pointer_conversion (loc, function);
3044 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3045 expressions, like those used for ObjC messenger dispatches. */
3046 if (params && !params->is_empty ())
3047 function = objc_rewrite_function_call (function, (*params)[0]);
3049 function = c_fully_fold (function, false, NULL);
3051 fntype = TREE_TYPE (function);
3053 if (TREE_CODE (fntype) == ERROR_MARK)
3054 return error_mark_node;
3056 if (!(TREE_CODE (fntype) == POINTER_TYPE
3057 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3059 if (!flag_diagnostics_show_caret)
3060 error_at (loc,
3061 "called object %qE is not a function or function pointer",
3062 function);
3063 else if (DECL_P (function))
3065 error_at (loc,
3066 "called object %qD is not a function or function pointer",
3067 function);
3068 inform_declaration (function);
3070 else
3071 error_at (loc,
3072 "called object is not a function or function pointer");
3073 return error_mark_node;
3076 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3077 current_function_returns_abnormally = 1;
3079 /* fntype now gets the type of function pointed to. */
3080 fntype = TREE_TYPE (fntype);
3082 /* Convert the parameters to the types declared in the
3083 function prototype, or apply default promotions. */
3085 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3086 origtypes, function, fundecl);
3087 if (nargs < 0)
3088 return error_mark_node;
3090 /* Check that the function is called through a compatible prototype.
3091 If it is not, warn. */
3092 if (CONVERT_EXPR_P (function)
3093 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3094 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3095 && !comptypes (fntype, TREE_TYPE (tem)))
3097 tree return_type = TREE_TYPE (fntype);
3099 /* This situation leads to run-time undefined behavior. We can't,
3100 therefore, simply error unless we can prove that all possible
3101 executions of the program must execute the code. */
3102 warning_at (loc, 0, "function called through a non-compatible type");
3104 if (VOID_TYPE_P (return_type)
3105 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3106 pedwarn (loc, 0,
3107 "function with qualified void return type called");
3110 argarray = vec_safe_address (params);
3112 /* Check that arguments to builtin functions match the expectations. */
3113 if (fundecl
3114 && DECL_BUILT_IN (fundecl)
3115 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3116 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3117 argarray))
3118 return error_mark_node;
3120 /* Check that the arguments to the function are valid. */
3121 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3122 nargs, argarray, &arg_loc);
3124 if (name != NULL_TREE
3125 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3127 if (require_constant_value)
3128 result
3129 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3130 function, nargs, argarray);
3131 else
3132 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3133 function, nargs, argarray);
3134 if (TREE_CODE (result) == NOP_EXPR
3135 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3136 STRIP_TYPE_NOPS (result);
3138 else
3139 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3140 function, nargs, argarray);
3141 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3142 later. */
3143 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3144 TREE_NO_WARNING (result) = 1;
3146 /* In this improbable scenario, a nested function returns a VM type.
3147 Create a TARGET_EXPR so that the call always has a LHS, much as
3148 what the C++ FE does for functions returning non-PODs. */
3149 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3151 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3152 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3153 NULL_TREE, NULL_TREE);
3156 if (VOID_TYPE_P (TREE_TYPE (result)))
3158 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3159 pedwarn (loc, 0,
3160 "function with qualified void return type called");
3161 return result;
3163 return require_complete_type (loc, result);
3166 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3168 tree
3169 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3170 tree function, vec<tree, va_gc> *params,
3171 vec<tree, va_gc> *origtypes)
3173 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3174 STRIP_TYPE_NOPS (function);
3176 /* Convert anything with function type to a pointer-to-function. */
3177 if (TREE_CODE (function) == FUNCTION_DECL)
3179 /* Implement type-directed function overloading for builtins.
3180 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3181 handle all the type checking. The result is a complete expression
3182 that implements this function call. */
3183 tree tem = resolve_overloaded_builtin (loc, function, params);
3184 if (tem)
3185 return tem;
3187 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3190 /* Convert the argument expressions in the vector VALUES
3191 to the types in the list TYPELIST.
3193 If TYPELIST is exhausted, or when an element has NULL as its type,
3194 perform the default conversions.
3196 ORIGTYPES is the original types of the expressions in VALUES. This
3197 holds the type of enum values which have been converted to integral
3198 types. It may be NULL.
3200 FUNCTION is a tree for the called function. It is used only for
3201 error messages, where it is formatted with %qE.
3203 This is also where warnings about wrong number of args are generated.
3205 ARG_LOC are locations of function arguments (if any).
3207 Returns the actual number of arguments processed (which may be less
3208 than the length of VALUES in some error situations), or -1 on
3209 failure. */
3211 static int
3212 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3213 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3214 tree function, tree fundecl)
3216 tree typetail, val;
3217 unsigned int parmnum;
3218 bool error_args = false;
3219 const bool type_generic = fundecl
3220 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3221 bool type_generic_remove_excess_precision = false;
3222 bool type_generic_overflow_p = false;
3223 tree selector;
3225 /* Change pointer to function to the function itself for
3226 diagnostics. */
3227 if (TREE_CODE (function) == ADDR_EXPR
3228 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3229 function = TREE_OPERAND (function, 0);
3231 /* Handle an ObjC selector specially for diagnostics. */
3232 selector = objc_message_selector ();
3234 /* For type-generic built-in functions, determine whether excess
3235 precision should be removed (classification) or not
3236 (comparison). */
3237 if (type_generic
3238 && DECL_BUILT_IN (fundecl)
3239 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3241 switch (DECL_FUNCTION_CODE (fundecl))
3243 case BUILT_IN_ISFINITE:
3244 case BUILT_IN_ISINF:
3245 case BUILT_IN_ISINF_SIGN:
3246 case BUILT_IN_ISNAN:
3247 case BUILT_IN_ISNORMAL:
3248 case BUILT_IN_FPCLASSIFY:
3249 type_generic_remove_excess_precision = true;
3250 break;
3252 case BUILT_IN_ADD_OVERFLOW_P:
3253 case BUILT_IN_SUB_OVERFLOW_P:
3254 case BUILT_IN_MUL_OVERFLOW_P:
3255 /* The last argument of these type-generic builtins
3256 should not be promoted. */
3257 type_generic_overflow_p = true;
3258 break;
3260 default:
3261 break;
3264 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3265 return vec_safe_length (values);
3267 /* Scan the given expressions and types, producing individual
3268 converted arguments. */
3270 for (typetail = typelist, parmnum = 0;
3271 values && values->iterate (parmnum, &val);
3272 ++parmnum)
3274 tree type = typetail ? TREE_VALUE (typetail) : 0;
3275 tree valtype = TREE_TYPE (val);
3276 tree rname = function;
3277 int argnum = parmnum + 1;
3278 const char *invalid_func_diag;
3279 bool excess_precision = false;
3280 bool npc;
3281 tree parmval;
3282 /* Some __atomic_* builtins have additional hidden argument at
3283 position 0. */
3284 location_t ploc
3285 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3286 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3287 : input_location;
3289 if (type == void_type_node)
3291 if (selector)
3292 error_at (loc, "too many arguments to method %qE", selector);
3293 else
3294 error_at (loc, "too many arguments to function %qE", function);
3295 inform_declaration (fundecl);
3296 return error_args ? -1 : (int) parmnum;
3299 if (selector && argnum > 2)
3301 rname = selector;
3302 argnum -= 2;
3305 npc = null_pointer_constant_p (val);
3307 /* If there is excess precision and a prototype, convert once to
3308 the required type rather than converting via the semantic
3309 type. Likewise without a prototype a float value represented
3310 as long double should be converted once to double. But for
3311 type-generic classification functions excess precision must
3312 be removed here. */
3313 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3314 && (type || !type_generic || !type_generic_remove_excess_precision))
3316 val = TREE_OPERAND (val, 0);
3317 excess_precision = true;
3319 val = c_fully_fold (val, false, NULL);
3320 STRIP_TYPE_NOPS (val);
3322 val = require_complete_type (ploc, val);
3324 /* Some floating-point arguments must be promoted to double when
3325 no type is specified by a prototype. This applies to
3326 arguments of type float, and to architecture-specific types
3327 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3328 bool promote_float_arg = false;
3329 if (type == NULL_TREE
3330 && TREE_CODE (valtype) == REAL_TYPE
3331 && (TYPE_PRECISION (valtype)
3332 <= TYPE_PRECISION (double_type_node))
3333 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3334 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3335 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3337 /* Promote this argument, unless it has a _FloatN or
3338 _FloatNx type. */
3339 promote_float_arg = true;
3340 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3341 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3343 promote_float_arg = false;
3344 break;
3348 if (type != NULL_TREE)
3350 /* Formal parm type is specified by a function prototype. */
3352 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3354 error_at (ploc, "type of formal parameter %d is incomplete",
3355 parmnum + 1);
3356 parmval = val;
3358 else
3360 tree origtype;
3362 /* Optionally warn about conversions that
3363 differ from the default conversions. */
3364 if (warn_traditional_conversion || warn_traditional)
3366 unsigned int formal_prec = TYPE_PRECISION (type);
3368 if (INTEGRAL_TYPE_P (type)
3369 && TREE_CODE (valtype) == REAL_TYPE)
3370 warning_at (ploc, OPT_Wtraditional_conversion,
3371 "passing argument %d of %qE as integer rather "
3372 "than floating due to prototype",
3373 argnum, rname);
3374 if (INTEGRAL_TYPE_P (type)
3375 && TREE_CODE (valtype) == COMPLEX_TYPE)
3376 warning_at (ploc, OPT_Wtraditional_conversion,
3377 "passing argument %d of %qE as integer rather "
3378 "than complex due to prototype",
3379 argnum, rname);
3380 else if (TREE_CODE (type) == COMPLEX_TYPE
3381 && TREE_CODE (valtype) == REAL_TYPE)
3382 warning_at (ploc, OPT_Wtraditional_conversion,
3383 "passing argument %d of %qE as complex rather "
3384 "than floating due to prototype",
3385 argnum, rname);
3386 else if (TREE_CODE (type) == REAL_TYPE
3387 && INTEGRAL_TYPE_P (valtype))
3388 warning_at (ploc, OPT_Wtraditional_conversion,
3389 "passing argument %d of %qE as floating rather "
3390 "than integer due to prototype",
3391 argnum, rname);
3392 else if (TREE_CODE (type) == COMPLEX_TYPE
3393 && INTEGRAL_TYPE_P (valtype))
3394 warning_at (ploc, OPT_Wtraditional_conversion,
3395 "passing argument %d of %qE as complex rather "
3396 "than integer due to prototype",
3397 argnum, rname);
3398 else if (TREE_CODE (type) == REAL_TYPE
3399 && TREE_CODE (valtype) == COMPLEX_TYPE)
3400 warning_at (ploc, OPT_Wtraditional_conversion,
3401 "passing argument %d of %qE as floating rather "
3402 "than complex due to prototype",
3403 argnum, rname);
3404 /* ??? At some point, messages should be written about
3405 conversions between complex types, but that's too messy
3406 to do now. */
3407 else if (TREE_CODE (type) == REAL_TYPE
3408 && TREE_CODE (valtype) == REAL_TYPE)
3410 /* Warn if any argument is passed as `float',
3411 since without a prototype it would be `double'. */
3412 if (formal_prec == TYPE_PRECISION (float_type_node)
3413 && type != dfloat32_type_node)
3414 warning_at (ploc, 0,
3415 "passing argument %d of %qE as %<float%> "
3416 "rather than %<double%> due to prototype",
3417 argnum, rname);
3419 /* Warn if mismatch between argument and prototype
3420 for decimal float types. Warn of conversions with
3421 binary float types and of precision narrowing due to
3422 prototype. */
3423 else if (type != valtype
3424 && (type == dfloat32_type_node
3425 || type == dfloat64_type_node
3426 || type == dfloat128_type_node
3427 || valtype == dfloat32_type_node
3428 || valtype == dfloat64_type_node
3429 || valtype == dfloat128_type_node)
3430 && (formal_prec
3431 <= TYPE_PRECISION (valtype)
3432 || (type == dfloat128_type_node
3433 && (valtype
3434 != dfloat64_type_node
3435 && (valtype
3436 != dfloat32_type_node)))
3437 || (type == dfloat64_type_node
3438 && (valtype
3439 != dfloat32_type_node))))
3440 warning_at (ploc, 0,
3441 "passing argument %d of %qE as %qT "
3442 "rather than %qT due to prototype",
3443 argnum, rname, type, valtype);
3446 /* Detect integer changing in width or signedness.
3447 These warnings are only activated with
3448 -Wtraditional-conversion, not with -Wtraditional. */
3449 else if (warn_traditional_conversion
3450 && INTEGRAL_TYPE_P (type)
3451 && INTEGRAL_TYPE_P (valtype))
3453 tree would_have_been = default_conversion (val);
3454 tree type1 = TREE_TYPE (would_have_been);
3456 if (val == error_mark_node)
3457 /* VAL could have been of incomplete type. */;
3458 else if (TREE_CODE (type) == ENUMERAL_TYPE
3459 && (TYPE_MAIN_VARIANT (type)
3460 == TYPE_MAIN_VARIANT (valtype)))
3461 /* No warning if function asks for enum
3462 and the actual arg is that enum type. */
3464 else if (formal_prec != TYPE_PRECISION (type1))
3465 warning_at (ploc, OPT_Wtraditional_conversion,
3466 "passing argument %d of %qE "
3467 "with different width due to prototype",
3468 argnum, rname);
3469 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3471 /* Don't complain if the formal parameter type
3472 is an enum, because we can't tell now whether
3473 the value was an enum--even the same enum. */
3474 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3476 else if (TREE_CODE (val) == INTEGER_CST
3477 && int_fits_type_p (val, type))
3478 /* Change in signedness doesn't matter
3479 if a constant value is unaffected. */
3481 /* If the value is extended from a narrower
3482 unsigned type, it doesn't matter whether we
3483 pass it as signed or unsigned; the value
3484 certainly is the same either way. */
3485 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3486 && TYPE_UNSIGNED (valtype))
3488 else if (TYPE_UNSIGNED (type))
3489 warning_at (ploc, OPT_Wtraditional_conversion,
3490 "passing argument %d of %qE "
3491 "as unsigned due to prototype",
3492 argnum, rname);
3493 else
3494 warning_at (ploc, OPT_Wtraditional_conversion,
3495 "passing argument %d of %qE "
3496 "as signed due to prototype",
3497 argnum, rname);
3501 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3502 sake of better warnings from convert_and_check. */
3503 if (excess_precision)
3504 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3505 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3506 parmval = convert_for_assignment (loc, ploc, type,
3507 val, origtype, ic_argpass,
3508 npc, fundecl, function,
3509 parmnum + 1);
3511 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3512 && INTEGRAL_TYPE_P (type)
3513 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3514 parmval = default_conversion (parmval);
3517 else if (promote_float_arg)
3519 if (type_generic)
3520 parmval = val;
3521 else
3523 /* Convert `float' to `double'. */
3524 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3525 warning_at (ploc, OPT_Wdouble_promotion,
3526 "implicit conversion from %qT to %qT when passing "
3527 "argument to function",
3528 valtype, double_type_node);
3529 parmval = convert (double_type_node, val);
3532 else if ((excess_precision && !type_generic)
3533 || (type_generic_overflow_p && parmnum == 2))
3534 /* A "double" argument with excess precision being passed
3535 without a prototype or in variable arguments.
3536 The last argument of __builtin_*_overflow_p should not be
3537 promoted. */
3538 parmval = convert (valtype, val);
3539 else if ((invalid_func_diag =
3540 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3542 error (invalid_func_diag);
3543 return -1;
3545 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3547 return -1;
3549 else
3550 /* Convert `short' and `char' to full-size `int'. */
3551 parmval = default_conversion (val);
3553 (*values)[parmnum] = parmval;
3554 if (parmval == error_mark_node)
3555 error_args = true;
3557 if (typetail)
3558 typetail = TREE_CHAIN (typetail);
3561 gcc_assert (parmnum == vec_safe_length (values));
3563 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3565 error_at (loc, "too few arguments to function %qE", function);
3566 inform_declaration (fundecl);
3567 return -1;
3570 return error_args ? -1 : (int) parmnum;
3573 /* This is the entry point used by the parser to build unary operators
3574 in the input. CODE, a tree_code, specifies the unary operator, and
3575 ARG is the operand. For unary plus, the C parser currently uses
3576 CONVERT_EXPR for code.
3578 LOC is the location to use for the tree generated.
3581 struct c_expr
3582 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3584 struct c_expr result;
3586 result.original_code = code;
3587 result.original_type = NULL;
3589 if (reject_gcc_builtin (arg.value))
3591 result.value = error_mark_node;
3593 else
3595 result.value = build_unary_op (loc, code, arg.value, false);
3597 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3598 overflow_warning (loc, result.value, arg.value);
3601 /* We are typically called when parsing a prefix token at LOC acting on
3602 ARG. Reflect this by updating the source range of the result to
3603 start at LOC and end at the end of ARG. */
3604 set_c_expr_source_range (&result,
3605 loc, arg.get_finish ());
3607 return result;
3610 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3612 static bool
3613 char_type_p (tree type)
3615 return (type == char_type_node
3616 || type == unsigned_char_type_node
3617 || type == signed_char_type_node
3618 || type == char16_type_node
3619 || type == char32_type_node);
3622 /* This is the entry point used by the parser to build binary operators
3623 in the input. CODE, a tree_code, specifies the binary operator, and
3624 ARG1 and ARG2 are the operands. In addition to constructing the
3625 expression, we check for operands that were written with other binary
3626 operators in a way that is likely to confuse the user.
3628 LOCATION is the location of the binary operator. */
3630 struct c_expr
3631 parser_build_binary_op (location_t location, enum tree_code code,
3632 struct c_expr arg1, struct c_expr arg2)
3634 struct c_expr result;
3636 enum tree_code code1 = arg1.original_code;
3637 enum tree_code code2 = arg2.original_code;
3638 tree type1 = (arg1.original_type
3639 ? arg1.original_type
3640 : TREE_TYPE (arg1.value));
3641 tree type2 = (arg2.original_type
3642 ? arg2.original_type
3643 : TREE_TYPE (arg2.value));
3645 result.value = build_binary_op (location, code,
3646 arg1.value, arg2.value, true);
3647 result.original_code = code;
3648 result.original_type = NULL;
3650 if (TREE_CODE (result.value) == ERROR_MARK)
3652 set_c_expr_source_range (&result,
3653 arg1.get_start (),
3654 arg2.get_finish ());
3655 return result;
3658 if (location != UNKNOWN_LOCATION)
3659 protected_set_expr_location (result.value, location);
3661 set_c_expr_source_range (&result,
3662 arg1.get_start (),
3663 arg2.get_finish ());
3665 /* Check for cases such as x+y<<z which users are likely
3666 to misinterpret. */
3667 if (warn_parentheses)
3668 warn_about_parentheses (location, code, code1, arg1.value, code2,
3669 arg2.value);
3671 if (warn_logical_op)
3672 warn_logical_operator (location, code, TREE_TYPE (result.value),
3673 code1, arg1.value, code2, arg2.value);
3675 if (warn_tautological_compare)
3677 tree lhs = arg1.value;
3678 tree rhs = arg2.value;
3679 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3681 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3682 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3683 lhs = NULL_TREE;
3684 else
3685 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3687 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3689 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3690 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3691 rhs = NULL_TREE;
3692 else
3693 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3695 if (lhs != NULL_TREE && rhs != NULL_TREE)
3696 warn_tautological_cmp (location, code, lhs, rhs);
3699 if (warn_logical_not_paren
3700 && TREE_CODE_CLASS (code) == tcc_comparison
3701 && code1 == TRUTH_NOT_EXPR
3702 && code2 != TRUTH_NOT_EXPR
3703 /* Avoid warning for !!x == y. */
3704 && (TREE_CODE (arg1.value) != NE_EXPR
3705 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3707 /* Avoid warning for !b == y where b has _Bool type. */
3708 tree t = integer_zero_node;
3709 if (TREE_CODE (arg1.value) == EQ_EXPR
3710 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3711 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3713 t = TREE_OPERAND (arg1.value, 0);
3716 if (TREE_TYPE (t) != integer_type_node)
3717 break;
3718 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3719 t = C_MAYBE_CONST_EXPR_EXPR (t);
3720 else if (CONVERT_EXPR_P (t))
3721 t = TREE_OPERAND (t, 0);
3722 else
3723 break;
3725 while (1);
3727 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3728 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3731 /* Warn about comparisons against string literals, with the exception
3732 of testing for equality or inequality of a string literal with NULL. */
3733 if (code == EQ_EXPR || code == NE_EXPR)
3735 if ((code1 == STRING_CST
3736 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3737 || (code2 == STRING_CST
3738 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3739 warning_at (location, OPT_Waddress,
3740 "comparison with string literal results in unspecified behavior");
3741 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3742 if (POINTER_TYPE_P (type1)
3743 && null_pointer_constant_p (arg2.value)
3744 && char_type_p (type2)
3745 && warning_at (location, OPT_Wpointer_compare,
3746 "comparison between pointer and zero character "
3747 "constant"))
3748 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3749 else if (POINTER_TYPE_P (type2)
3750 && null_pointer_constant_p (arg1.value)
3751 && char_type_p (type1)
3752 && warning_at (location, OPT_Wpointer_compare,
3753 "comparison between pointer and zero character "
3754 "constant"))
3755 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3757 else if (TREE_CODE_CLASS (code) == tcc_comparison
3758 && (code1 == STRING_CST || code2 == STRING_CST))
3759 warning_at (location, OPT_Waddress,
3760 "comparison with string literal results in unspecified behavior");
3762 if (TREE_OVERFLOW_P (result.value)
3763 && !TREE_OVERFLOW_P (arg1.value)
3764 && !TREE_OVERFLOW_P (arg2.value))
3765 overflow_warning (location, result.value);
3767 /* Warn about comparisons of different enum types. */
3768 if (warn_enum_compare
3769 && TREE_CODE_CLASS (code) == tcc_comparison
3770 && TREE_CODE (type1) == ENUMERAL_TYPE
3771 && TREE_CODE (type2) == ENUMERAL_TYPE
3772 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3773 warning_at (location, OPT_Wenum_compare,
3774 "comparison between %qT and %qT",
3775 type1, type2);
3777 return result;
3780 /* Return a tree for the difference of pointers OP0 and OP1.
3781 The resulting tree has type int. */
3783 static tree
3784 pointer_diff (location_t loc, tree op0, tree op1)
3786 tree restype = ptrdiff_type_node;
3787 tree result, inttype;
3789 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3790 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3791 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3792 tree orig_op1 = op1;
3794 /* If the operands point into different address spaces, we need to
3795 explicitly convert them to pointers into the common address space
3796 before we can subtract the numerical address values. */
3797 if (as0 != as1)
3799 addr_space_t as_common;
3800 tree common_type;
3802 /* Determine the common superset address space. This is guaranteed
3803 to exist because the caller verified that comp_target_types
3804 returned non-zero. */
3805 if (!addr_space_superset (as0, as1, &as_common))
3806 gcc_unreachable ();
3808 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3809 op0 = convert (common_type, op0);
3810 op1 = convert (common_type, op1);
3813 /* Determine integer type to perform computations in. This will usually
3814 be the same as the result type (ptrdiff_t), but may need to be a wider
3815 type if pointers for the address space are wider than ptrdiff_t. */
3816 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3817 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3818 else
3819 inttype = restype;
3821 if (TREE_CODE (target_type) == VOID_TYPE)
3822 pedwarn (loc, OPT_Wpointer_arith,
3823 "pointer of type %<void *%> used in subtraction");
3824 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3825 pedwarn (loc, OPT_Wpointer_arith,
3826 "pointer to a function used in subtraction");
3828 /* First do the subtraction as integers;
3829 then drop through to build the divide operator.
3830 Do not do default conversions on the minus operator
3831 in case restype is a short type. */
3833 op0 = build_binary_op (loc,
3834 MINUS_EXPR, convert (inttype, op0),
3835 convert (inttype, op1), false);
3836 /* This generates an error if op1 is pointer to incomplete type. */
3837 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3838 error_at (loc, "arithmetic on pointer to an incomplete type");
3840 op1 = c_size_in_bytes (target_type);
3842 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3843 error_at (loc, "arithmetic on pointer to an empty aggregate");
3845 /* Divide by the size, in easiest possible way. */
3846 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3847 op0, convert (inttype, op1));
3849 /* Convert to final result type if necessary. */
3850 return convert (restype, result);
3853 /* Expand atomic compound assignments into an appropriate sequence as
3854 specified by the C11 standard section 6.5.16.2.
3856 _Atomic T1 E1
3857 T2 E2
3858 E1 op= E2
3860 This sequence is used for all types for which these operations are
3861 supported.
3863 In addition, built-in versions of the 'fe' prefixed routines may
3864 need to be invoked for floating point (real, complex or vector) when
3865 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3867 T1 newval;
3868 T1 old;
3869 T1 *addr
3870 T2 val
3871 fenv_t fenv
3873 addr = &E1;
3874 val = (E2);
3875 __atomic_load (addr, &old, SEQ_CST);
3876 feholdexcept (&fenv);
3877 loop:
3878 newval = old op val;
3879 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3880 SEQ_CST))
3881 goto done;
3882 feclearexcept (FE_ALL_EXCEPT);
3883 goto loop:
3884 done:
3885 feupdateenv (&fenv);
3887 The compiler will issue the __atomic_fetch_* built-in when possible,
3888 otherwise it will generate the generic form of the atomic operations.
3889 This requires temp(s) and has their address taken. The atomic processing
3890 is smart enough to figure out when the size of an object can utilize
3891 a lock-free version, and convert the built-in call to the appropriate
3892 lock-free routine. The optimizers will then dispose of any temps that
3893 are no longer required, and lock-free implementations are utilized as
3894 long as there is target support for the required size.
3896 If the operator is NOP_EXPR, then this is a simple assignment, and
3897 an __atomic_store is issued to perform the assignment rather than
3898 the above loop. */
3900 /* Build an atomic assignment at LOC, expanding into the proper
3901 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3902 the result of the operation, unless RETURN_OLD_P, in which case
3903 return the old value of LHS (this is only for postincrement and
3904 postdecrement). */
3906 static tree
3907 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3908 tree rhs, bool return_old_p)
3910 tree fndecl, func_call;
3911 vec<tree, va_gc> *params;
3912 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3913 tree old, old_addr;
3914 tree compound_stmt;
3915 tree stmt, goto_stmt;
3916 tree loop_label, loop_decl, done_label, done_decl;
3918 tree lhs_type = TREE_TYPE (lhs);
3919 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3920 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3921 tree rhs_semantic_type = TREE_TYPE (rhs);
3922 tree nonatomic_rhs_semantic_type;
3923 tree rhs_type;
3925 gcc_assert (TYPE_ATOMIC (lhs_type));
3927 if (return_old_p)
3928 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3930 /* Allocate enough vector items for a compare_exchange. */
3931 vec_alloc (params, 6);
3933 /* Create a compound statement to hold the sequence of statements
3934 with a loop. */
3935 compound_stmt = c_begin_compound_stmt (false);
3937 /* Remove any excess precision (which is only present here in the
3938 case of compound assignments). */
3939 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3941 gcc_assert (modifycode != NOP_EXPR);
3942 rhs = TREE_OPERAND (rhs, 0);
3944 rhs_type = TREE_TYPE (rhs);
3946 /* Fold the RHS if it hasn't already been folded. */
3947 if (modifycode != NOP_EXPR)
3948 rhs = c_fully_fold (rhs, false, NULL);
3950 /* Remove the qualifiers for the rest of the expressions and create
3951 the VAL temp variable to hold the RHS. */
3952 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3953 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3954 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3955 TYPE_UNQUALIFIED);
3956 val = create_tmp_var_raw (nonatomic_rhs_type);
3957 TREE_ADDRESSABLE (val) = 1;
3958 TREE_NO_WARNING (val) = 1;
3959 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3960 NULL_TREE);
3961 SET_EXPR_LOCATION (rhs, loc);
3962 add_stmt (rhs);
3964 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3965 an atomic_store. */
3966 if (modifycode == NOP_EXPR)
3968 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3969 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3970 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3971 params->quick_push (lhs_addr);
3972 params->quick_push (rhs);
3973 params->quick_push (seq_cst);
3974 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3975 add_stmt (func_call);
3977 /* Finish the compound statement. */
3978 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3980 /* VAL is the value which was stored, return a COMPOUND_STMT of
3981 the statement and that value. */
3982 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3985 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3986 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3987 isn't applicable for such builtins. ??? Do we want to handle enums? */
3988 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3989 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3991 built_in_function fncode;
3992 switch (modifycode)
3994 case PLUS_EXPR:
3995 case POINTER_PLUS_EXPR:
3996 fncode = (return_old_p
3997 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3998 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3999 break;
4000 case MINUS_EXPR:
4001 fncode = (return_old_p
4002 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4003 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4004 break;
4005 case BIT_AND_EXPR:
4006 fncode = (return_old_p
4007 ? BUILT_IN_ATOMIC_FETCH_AND_N
4008 : BUILT_IN_ATOMIC_AND_FETCH_N);
4009 break;
4010 case BIT_IOR_EXPR:
4011 fncode = (return_old_p
4012 ? BUILT_IN_ATOMIC_FETCH_OR_N
4013 : BUILT_IN_ATOMIC_OR_FETCH_N);
4014 break;
4015 case BIT_XOR_EXPR:
4016 fncode = (return_old_p
4017 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4018 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4019 break;
4020 default:
4021 goto cas_loop;
4024 /* We can only use "_1" through "_16" variants of the atomic fetch
4025 built-ins. */
4026 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4027 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4028 goto cas_loop;
4030 /* If this is a pointer type, we need to multiply by the size of
4031 the pointer target type. */
4032 if (POINTER_TYPE_P (lhs_type))
4034 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4035 /* ??? This would introduce -Wdiscarded-qualifiers
4036 warning: __atomic_fetch_* expect volatile void *
4037 type as the first argument. (Assignments between
4038 atomic and non-atomic objects are OK.) */
4039 || TYPE_RESTRICT (lhs_type))
4040 goto cas_loop;
4041 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4042 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4043 convert (ptrdiff_type_node, rhs),
4044 convert (ptrdiff_type_node, sz));
4047 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4048 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4049 fndecl = builtin_decl_explicit (fncode);
4050 params->quick_push (lhs_addr);
4051 params->quick_push (rhs);
4052 params->quick_push (seq_cst);
4053 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4055 newval = create_tmp_var_raw (nonatomic_lhs_type);
4056 TREE_ADDRESSABLE (newval) = 1;
4057 TREE_NO_WARNING (newval) = 1;
4058 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4059 NULL_TREE, NULL_TREE);
4060 SET_EXPR_LOCATION (rhs, loc);
4061 add_stmt (rhs);
4063 /* Finish the compound statement. */
4064 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4066 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4067 the statement and that value. */
4068 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4071 cas_loop:
4072 /* Create the variables and labels required for the op= form. */
4073 old = create_tmp_var_raw (nonatomic_lhs_type);
4074 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4075 TREE_ADDRESSABLE (old) = 1;
4076 TREE_NO_WARNING (old) = 1;
4078 newval = create_tmp_var_raw (nonatomic_lhs_type);
4079 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4080 TREE_ADDRESSABLE (newval) = 1;
4081 TREE_NO_WARNING (newval) = 1;
4083 loop_decl = create_artificial_label (loc);
4084 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4086 done_decl = create_artificial_label (loc);
4087 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4089 /* __atomic_load (addr, &old, SEQ_CST). */
4090 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4091 params->quick_push (lhs_addr);
4092 params->quick_push (old_addr);
4093 params->quick_push (seq_cst);
4094 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4095 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4096 NULL_TREE);
4097 add_stmt (old);
4098 params->truncate (0);
4100 /* Create the expressions for floating-point environment
4101 manipulation, if required. */
4102 bool need_fenv = (flag_trapping_math
4103 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4104 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4105 if (need_fenv)
4106 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4108 if (hold_call)
4109 add_stmt (hold_call);
4111 /* loop: */
4112 add_stmt (loop_label);
4114 /* newval = old + val; */
4115 if (rhs_type != rhs_semantic_type)
4116 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4117 rhs = build_binary_op (loc, modifycode, old, val, true);
4118 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4120 tree eptype = TREE_TYPE (rhs);
4121 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4122 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4124 else
4125 rhs = c_fully_fold (rhs, false, NULL);
4126 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4127 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4128 NULL_TREE, 0);
4129 if (rhs != error_mark_node)
4131 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4132 NULL_TREE);
4133 SET_EXPR_LOCATION (rhs, loc);
4134 add_stmt (rhs);
4137 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4138 goto done; */
4139 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4140 params->quick_push (lhs_addr);
4141 params->quick_push (old_addr);
4142 params->quick_push (newval_addr);
4143 params->quick_push (integer_zero_node);
4144 params->quick_push (seq_cst);
4145 params->quick_push (seq_cst);
4146 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4148 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4149 SET_EXPR_LOCATION (goto_stmt, loc);
4151 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4152 SET_EXPR_LOCATION (stmt, loc);
4153 add_stmt (stmt);
4155 if (clear_call)
4156 add_stmt (clear_call);
4158 /* goto loop; */
4159 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4160 SET_EXPR_LOCATION (goto_stmt, loc);
4161 add_stmt (goto_stmt);
4163 /* done: */
4164 add_stmt (done_label);
4166 if (update_call)
4167 add_stmt (update_call);
4169 /* Finish the compound statement. */
4170 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4172 /* NEWVAL is the value that was successfully stored, return a
4173 COMPOUND_EXPR of the statement and the appropriate value. */
4174 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4175 return_old_p ? old : newval);
4178 /* Construct and perhaps optimize a tree representation
4179 for a unary operation. CODE, a tree_code, specifies the operation
4180 and XARG is the operand.
4181 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4182 promotions (such as from short to int).
4183 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4184 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4185 to pointers in C99.
4187 LOCATION is the location of the operator. */
4189 tree
4190 build_unary_op (location_t location, enum tree_code code, tree xarg,
4191 bool noconvert)
4193 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4194 tree arg = xarg;
4195 tree argtype = NULL_TREE;
4196 enum tree_code typecode;
4197 tree val;
4198 tree ret = error_mark_node;
4199 tree eptype = NULL_TREE;
4200 const char *invalid_op_diag;
4201 bool int_operands;
4203 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4204 if (int_operands)
4205 arg = remove_c_maybe_const_expr (arg);
4207 if (code != ADDR_EXPR)
4208 arg = require_complete_type (location, arg);
4210 typecode = TREE_CODE (TREE_TYPE (arg));
4211 if (typecode == ERROR_MARK)
4212 return error_mark_node;
4213 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4214 typecode = INTEGER_TYPE;
4216 if ((invalid_op_diag
4217 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4219 error_at (location, invalid_op_diag);
4220 return error_mark_node;
4223 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4225 eptype = TREE_TYPE (arg);
4226 arg = TREE_OPERAND (arg, 0);
4229 switch (code)
4231 case CONVERT_EXPR:
4232 /* This is used for unary plus, because a CONVERT_EXPR
4233 is enough to prevent anybody from looking inside for
4234 associativity, but won't generate any code. */
4235 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4236 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4237 || typecode == VECTOR_TYPE))
4239 error_at (location, "wrong type argument to unary plus");
4240 return error_mark_node;
4242 else if (!noconvert)
4243 arg = default_conversion (arg);
4244 arg = non_lvalue_loc (location, arg);
4245 break;
4247 case NEGATE_EXPR:
4248 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4249 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4250 || typecode == VECTOR_TYPE))
4252 error_at (location, "wrong type argument to unary minus");
4253 return error_mark_node;
4255 else if (!noconvert)
4256 arg = default_conversion (arg);
4257 break;
4259 case BIT_NOT_EXPR:
4260 /* ~ works on integer types and non float vectors. */
4261 if (typecode == INTEGER_TYPE
4262 || (typecode == VECTOR_TYPE
4263 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4265 tree e = arg;
4267 /* Warn if the expression has boolean value. */
4268 while (TREE_CODE (e) == COMPOUND_EXPR)
4269 e = TREE_OPERAND (e, 1);
4271 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4272 || truth_value_p (TREE_CODE (e)))
4273 && warning_at (location, OPT_Wbool_operation,
4274 "%<~%> on a boolean expression"))
4276 gcc_rich_location richloc (location);
4277 richloc.add_fixit_insert_before (location, "!");
4278 inform (&richloc, "did you mean to use logical not?");
4280 if (!noconvert)
4281 arg = default_conversion (arg);
4283 else if (typecode == COMPLEX_TYPE)
4285 code = CONJ_EXPR;
4286 pedwarn (location, OPT_Wpedantic,
4287 "ISO C does not support %<~%> for complex conjugation");
4288 if (!noconvert)
4289 arg = default_conversion (arg);
4291 else
4293 error_at (location, "wrong type argument to bit-complement");
4294 return error_mark_node;
4296 break;
4298 case ABS_EXPR:
4299 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4301 error_at (location, "wrong type argument to abs");
4302 return error_mark_node;
4304 else if (!noconvert)
4305 arg = default_conversion (arg);
4306 break;
4308 case CONJ_EXPR:
4309 /* Conjugating a real value is a no-op, but allow it anyway. */
4310 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4311 || typecode == COMPLEX_TYPE))
4313 error_at (location, "wrong type argument to conjugation");
4314 return error_mark_node;
4316 else if (!noconvert)
4317 arg = default_conversion (arg);
4318 break;
4320 case TRUTH_NOT_EXPR:
4321 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4322 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4323 && typecode != COMPLEX_TYPE)
4325 error_at (location,
4326 "wrong type argument to unary exclamation mark");
4327 return error_mark_node;
4329 if (int_operands)
4331 arg = c_objc_common_truthvalue_conversion (location, xarg);
4332 arg = remove_c_maybe_const_expr (arg);
4334 else
4335 arg = c_objc_common_truthvalue_conversion (location, arg);
4336 ret = invert_truthvalue_loc (location, arg);
4337 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4338 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4339 location = EXPR_LOCATION (ret);
4340 goto return_build_unary_op;
4342 case REALPART_EXPR:
4343 case IMAGPART_EXPR:
4344 ret = build_real_imag_expr (location, code, arg);
4345 if (ret == error_mark_node)
4346 return error_mark_node;
4347 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4348 eptype = TREE_TYPE (eptype);
4349 goto return_build_unary_op;
4351 case PREINCREMENT_EXPR:
4352 case POSTINCREMENT_EXPR:
4353 case PREDECREMENT_EXPR:
4354 case POSTDECREMENT_EXPR:
4356 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4358 tree inner = build_unary_op (location, code,
4359 C_MAYBE_CONST_EXPR_EXPR (arg),
4360 noconvert);
4361 if (inner == error_mark_node)
4362 return error_mark_node;
4363 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4364 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4365 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4366 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4367 goto return_build_unary_op;
4370 /* Complain about anything that is not a true lvalue. In
4371 Objective-C, skip this check for property_refs. */
4372 if (!objc_is_property_ref (arg)
4373 && !lvalue_or_else (location,
4374 arg, ((code == PREINCREMENT_EXPR
4375 || code == POSTINCREMENT_EXPR)
4376 ? lv_increment
4377 : lv_decrement)))
4378 return error_mark_node;
4380 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4382 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4383 warning_at (location, OPT_Wc___compat,
4384 "increment of enumeration value is invalid in C++");
4385 else
4386 warning_at (location, OPT_Wc___compat,
4387 "decrement of enumeration value is invalid in C++");
4390 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4392 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4393 warning_at (location, OPT_Wbool_operation,
4394 "increment of a boolean expression");
4395 else
4396 warning_at (location, OPT_Wbool_operation,
4397 "decrement of a boolean expression");
4400 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4401 arg = c_fully_fold (arg, false, NULL);
4403 bool atomic_op;
4404 atomic_op = really_atomic_lvalue (arg);
4406 /* Increment or decrement the real part of the value,
4407 and don't change the imaginary part. */
4408 if (typecode == COMPLEX_TYPE)
4410 tree real, imag;
4412 pedwarn (location, OPT_Wpedantic,
4413 "ISO C does not support %<++%> and %<--%> on complex types");
4415 if (!atomic_op)
4417 arg = stabilize_reference (arg);
4418 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4419 true);
4420 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4421 true);
4422 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4423 if (real == error_mark_node || imag == error_mark_node)
4424 return error_mark_node;
4425 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4426 real, imag);
4427 goto return_build_unary_op;
4431 /* Report invalid types. */
4433 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4434 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4435 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4437 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4438 error_at (location, "wrong type argument to increment");
4439 else
4440 error_at (location, "wrong type argument to decrement");
4442 return error_mark_node;
4446 tree inc;
4448 argtype = TREE_TYPE (arg);
4450 /* Compute the increment. */
4452 if (typecode == POINTER_TYPE)
4454 /* If pointer target is an incomplete type,
4455 we just cannot know how to do the arithmetic. */
4456 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4458 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4459 error_at (location,
4460 "increment of pointer to an incomplete type %qT",
4461 TREE_TYPE (argtype));
4462 else
4463 error_at (location,
4464 "decrement of pointer to an incomplete type %qT",
4465 TREE_TYPE (argtype));
4467 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4468 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4470 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4471 pedwarn (location, OPT_Wpointer_arith,
4472 "wrong type argument to increment");
4473 else
4474 pedwarn (location, OPT_Wpointer_arith,
4475 "wrong type argument to decrement");
4478 inc = c_size_in_bytes (TREE_TYPE (argtype));
4479 inc = convert_to_ptrofftype_loc (location, inc);
4481 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4483 /* For signed fract types, we invert ++ to -- or
4484 -- to ++, and change inc from 1 to -1, because
4485 it is not possible to represent 1 in signed fract constants.
4486 For unsigned fract types, the result always overflows and
4487 we get an undefined (original) or the maximum value. */
4488 if (code == PREINCREMENT_EXPR)
4489 code = PREDECREMENT_EXPR;
4490 else if (code == PREDECREMENT_EXPR)
4491 code = PREINCREMENT_EXPR;
4492 else if (code == POSTINCREMENT_EXPR)
4493 code = POSTDECREMENT_EXPR;
4494 else /* code == POSTDECREMENT_EXPR */
4495 code = POSTINCREMENT_EXPR;
4497 inc = integer_minus_one_node;
4498 inc = convert (argtype, inc);
4500 else
4502 inc = VECTOR_TYPE_P (argtype)
4503 ? build_one_cst (argtype)
4504 : integer_one_node;
4505 inc = convert (argtype, inc);
4508 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4509 need to ask Objective-C to build the increment or decrement
4510 expression for it. */
4511 if (objc_is_property_ref (arg))
4512 return objc_build_incr_expr_for_property_ref (location, code,
4513 arg, inc);
4515 /* Report a read-only lvalue. */
4516 if (TYPE_READONLY (argtype))
4518 readonly_error (location, arg,
4519 ((code == PREINCREMENT_EXPR
4520 || code == POSTINCREMENT_EXPR)
4521 ? lv_increment : lv_decrement));
4522 return error_mark_node;
4524 else if (TREE_READONLY (arg))
4525 readonly_warning (arg,
4526 ((code == PREINCREMENT_EXPR
4527 || code == POSTINCREMENT_EXPR)
4528 ? lv_increment : lv_decrement));
4530 /* If the argument is atomic, use the special code sequences for
4531 atomic compound assignment. */
4532 if (atomic_op)
4534 arg = stabilize_reference (arg);
4535 ret = build_atomic_assign (location, arg,
4536 ((code == PREINCREMENT_EXPR
4537 || code == POSTINCREMENT_EXPR)
4538 ? PLUS_EXPR
4539 : MINUS_EXPR),
4540 (FRACT_MODE_P (TYPE_MODE (argtype))
4541 ? inc
4542 : integer_one_node),
4543 (code == POSTINCREMENT_EXPR
4544 || code == POSTDECREMENT_EXPR));
4545 goto return_build_unary_op;
4548 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4549 val = boolean_increment (code, arg);
4550 else
4551 val = build2 (code, TREE_TYPE (arg), arg, inc);
4552 TREE_SIDE_EFFECTS (val) = 1;
4553 if (TREE_CODE (val) != code)
4554 TREE_NO_WARNING (val) = 1;
4555 ret = val;
4556 goto return_build_unary_op;
4559 case ADDR_EXPR:
4560 /* Note that this operation never does default_conversion. */
4562 /* The operand of unary '&' must be an lvalue (which excludes
4563 expressions of type void), or, in C99, the result of a [] or
4564 unary '*' operator. */
4565 if (VOID_TYPE_P (TREE_TYPE (arg))
4566 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4567 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4568 pedwarn (location, 0, "taking address of expression of type %<void%>");
4570 /* Let &* cancel out to simplify resulting code. */
4571 if (INDIRECT_REF_P (arg))
4573 /* Don't let this be an lvalue. */
4574 if (lvalue_p (TREE_OPERAND (arg, 0)))
4575 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4576 ret = TREE_OPERAND (arg, 0);
4577 goto return_build_unary_op;
4580 /* Anything not already handled and not a true memory reference
4581 or a non-lvalue array is an error. */
4582 if (typecode != FUNCTION_TYPE && !noconvert
4583 && !lvalue_or_else (location, arg, lv_addressof))
4584 return error_mark_node;
4586 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4587 folding later. */
4588 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4590 tree inner = build_unary_op (location, code,
4591 C_MAYBE_CONST_EXPR_EXPR (arg),
4592 noconvert);
4593 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4594 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4595 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4596 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4597 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4598 goto return_build_unary_op;
4601 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4602 argtype = TREE_TYPE (arg);
4604 /* If the lvalue is const or volatile, merge that into the type
4605 to which the address will point. This is only needed
4606 for function types. */
4607 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4608 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4609 && TREE_CODE (argtype) == FUNCTION_TYPE)
4611 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4612 int quals = orig_quals;
4614 if (TREE_READONLY (arg))
4615 quals |= TYPE_QUAL_CONST;
4616 if (TREE_THIS_VOLATILE (arg))
4617 quals |= TYPE_QUAL_VOLATILE;
4619 argtype = c_build_qualified_type (argtype, quals);
4622 switch (TREE_CODE (arg))
4624 case COMPONENT_REF:
4625 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4627 error_at (location, "cannot take address of bit-field %qD",
4628 TREE_OPERAND (arg, 1));
4629 return error_mark_node;
4632 /* fall through */
4634 case ARRAY_REF:
4635 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4637 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4638 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4640 error_at (location, "cannot take address of scalar with "
4641 "reverse storage order");
4642 return error_mark_node;
4645 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4646 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4647 warning_at (location, OPT_Wscalar_storage_order,
4648 "address of array with reverse scalar storage "
4649 "order requested");
4652 default:
4653 break;
4656 if (!c_mark_addressable (arg))
4657 return error_mark_node;
4659 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4660 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4662 argtype = build_pointer_type (argtype);
4664 /* ??? Cope with user tricks that amount to offsetof. Delete this
4665 when we have proper support for integer constant expressions. */
4666 val = get_base_address (arg);
4667 if (val && INDIRECT_REF_P (val)
4668 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4670 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4671 goto return_build_unary_op;
4674 val = build1 (ADDR_EXPR, argtype, arg);
4676 ret = val;
4677 goto return_build_unary_op;
4679 default:
4680 gcc_unreachable ();
4683 if (argtype == NULL_TREE)
4684 argtype = TREE_TYPE (arg);
4685 if (TREE_CODE (arg) == INTEGER_CST)
4686 ret = (require_constant_value
4687 ? fold_build1_initializer_loc (location, code, argtype, arg)
4688 : fold_build1_loc (location, code, argtype, arg));
4689 else
4690 ret = build1 (code, argtype, arg);
4691 return_build_unary_op:
4692 gcc_assert (ret != error_mark_node);
4693 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4694 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4695 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4696 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4697 ret = note_integer_operands (ret);
4698 if (eptype)
4699 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4700 protected_set_expr_location (ret, location);
4701 return ret;
4704 /* Return nonzero if REF is an lvalue valid for this language.
4705 Lvalues can be assigned, unless their type has TYPE_READONLY.
4706 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4708 bool
4709 lvalue_p (const_tree ref)
4711 const enum tree_code code = TREE_CODE (ref);
4713 switch (code)
4715 case REALPART_EXPR:
4716 case IMAGPART_EXPR:
4717 case COMPONENT_REF:
4718 return lvalue_p (TREE_OPERAND (ref, 0));
4720 case C_MAYBE_CONST_EXPR:
4721 return lvalue_p (TREE_OPERAND (ref, 1));
4723 case COMPOUND_LITERAL_EXPR:
4724 case STRING_CST:
4725 return true;
4727 case INDIRECT_REF:
4728 case ARRAY_REF:
4729 case ARRAY_NOTATION_REF:
4730 case VAR_DECL:
4731 case PARM_DECL:
4732 case RESULT_DECL:
4733 case ERROR_MARK:
4734 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4735 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4737 case BIND_EXPR:
4738 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4740 default:
4741 return false;
4745 /* Give a warning for storing in something that is read-only in GCC
4746 terms but not const in ISO C terms. */
4748 static void
4749 readonly_warning (tree arg, enum lvalue_use use)
4751 switch (use)
4753 case lv_assign:
4754 warning (0, "assignment of read-only location %qE", arg);
4755 break;
4756 case lv_increment:
4757 warning (0, "increment of read-only location %qE", arg);
4758 break;
4759 case lv_decrement:
4760 warning (0, "decrement of read-only location %qE", arg);
4761 break;
4762 default:
4763 gcc_unreachable ();
4765 return;
4769 /* Return nonzero if REF is an lvalue valid for this language;
4770 otherwise, print an error message and return zero. USE says
4771 how the lvalue is being used and so selects the error message.
4772 LOCATION is the location at which any error should be reported. */
4774 static int
4775 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4777 int win = lvalue_p (ref);
4779 if (!win)
4780 lvalue_error (loc, use);
4782 return win;
4785 /* Mark EXP saying that we need to be able to take the
4786 address of it; it should not be allocated in a register.
4787 Returns true if successful. ARRAY_REF_P is true if this
4788 is for ARRAY_REF construction - in that case we don't want
4789 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4790 it is fine to use ARRAY_REFs for vector subscripts on vector
4791 register variables. */
4793 bool
4794 c_mark_addressable (tree exp, bool array_ref_p)
4796 tree x = exp;
4798 while (1)
4799 switch (TREE_CODE (x))
4801 case VIEW_CONVERT_EXPR:
4802 if (array_ref_p
4803 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4804 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4805 return true;
4806 /* FALLTHRU */
4807 case COMPONENT_REF:
4808 case ADDR_EXPR:
4809 case ARRAY_REF:
4810 case REALPART_EXPR:
4811 case IMAGPART_EXPR:
4812 x = TREE_OPERAND (x, 0);
4813 break;
4815 case COMPOUND_LITERAL_EXPR:
4816 case CONSTRUCTOR:
4817 TREE_ADDRESSABLE (x) = 1;
4818 return true;
4820 case VAR_DECL:
4821 case CONST_DECL:
4822 case PARM_DECL:
4823 case RESULT_DECL:
4824 if (C_DECL_REGISTER (x)
4825 && DECL_NONLOCAL (x))
4827 if (TREE_PUBLIC (x) || is_global_var (x))
4829 error
4830 ("global register variable %qD used in nested function", x);
4831 return false;
4833 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4835 else if (C_DECL_REGISTER (x))
4837 if (TREE_PUBLIC (x) || is_global_var (x))
4838 error ("address of global register variable %qD requested", x);
4839 else
4840 error ("address of register variable %qD requested", x);
4841 return false;
4844 /* FALLTHRU */
4845 case FUNCTION_DECL:
4846 TREE_ADDRESSABLE (x) = 1;
4847 /* FALLTHRU */
4848 default:
4849 return true;
4853 /* Convert EXPR to TYPE, warning about conversion problems with
4854 constants. SEMANTIC_TYPE is the type this conversion would use
4855 without excess precision. If SEMANTIC_TYPE is NULL, this function
4856 is equivalent to convert_and_check. This function is a wrapper that
4857 handles conversions that may be different than
4858 the usual ones because of excess precision. */
4860 static tree
4861 ep_convert_and_check (location_t loc, tree type, tree expr,
4862 tree semantic_type)
4864 if (TREE_TYPE (expr) == type)
4865 return expr;
4867 /* For C11, integer conversions may have results with excess
4868 precision. */
4869 if (flag_isoc11 || !semantic_type)
4870 return convert_and_check (loc, type, expr);
4872 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4873 && TREE_TYPE (expr) != semantic_type)
4875 /* For integers, we need to check the real conversion, not
4876 the conversion to the excess precision type. */
4877 expr = convert_and_check (loc, semantic_type, expr);
4879 /* Result type is the excess precision type, which should be
4880 large enough, so do not check. */
4881 return convert (type, expr);
4884 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4885 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4886 if folded to an integer constant then the unselected half may
4887 contain arbitrary operations not normally permitted in constant
4888 expressions. Set the location of the expression to LOC. */
4890 tree
4891 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4892 tree op1, tree op1_original_type, location_t op1_loc,
4893 tree op2, tree op2_original_type, location_t op2_loc)
4895 tree type1;
4896 tree type2;
4897 enum tree_code code1;
4898 enum tree_code code2;
4899 tree result_type = NULL;
4900 tree semantic_result_type = NULL;
4901 tree orig_op1 = op1, orig_op2 = op2;
4902 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4903 bool ifexp_int_operands;
4904 tree ret;
4906 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4907 if (op1_int_operands)
4908 op1 = remove_c_maybe_const_expr (op1);
4909 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4910 if (op2_int_operands)
4911 op2 = remove_c_maybe_const_expr (op2);
4912 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4913 if (ifexp_int_operands)
4914 ifexp = remove_c_maybe_const_expr (ifexp);
4916 /* Promote both alternatives. */
4918 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4919 op1 = default_conversion (op1);
4920 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4921 op2 = default_conversion (op2);
4923 if (TREE_CODE (ifexp) == ERROR_MARK
4924 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4925 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4926 return error_mark_node;
4928 type1 = TREE_TYPE (op1);
4929 code1 = TREE_CODE (type1);
4930 type2 = TREE_TYPE (op2);
4931 code2 = TREE_CODE (type2);
4933 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4934 return error_mark_node;
4936 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4937 return error_mark_node;
4939 /* C90 does not permit non-lvalue arrays in conditional expressions.
4940 In C99 they will be pointers by now. */
4941 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4943 error_at (colon_loc, "non-lvalue array in conditional expression");
4944 return error_mark_node;
4947 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4948 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4949 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4950 || code1 == COMPLEX_TYPE)
4951 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4952 || code2 == COMPLEX_TYPE))
4954 semantic_result_type = c_common_type (type1, type2);
4955 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4957 op1 = TREE_OPERAND (op1, 0);
4958 type1 = TREE_TYPE (op1);
4959 gcc_assert (TREE_CODE (type1) == code1);
4961 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4963 op2 = TREE_OPERAND (op2, 0);
4964 type2 = TREE_TYPE (op2);
4965 gcc_assert (TREE_CODE (type2) == code2);
4969 if (warn_cxx_compat)
4971 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4972 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4974 if (TREE_CODE (t1) == ENUMERAL_TYPE
4975 && TREE_CODE (t2) == ENUMERAL_TYPE
4976 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4977 warning_at (colon_loc, OPT_Wc___compat,
4978 ("different enum types in conditional is "
4979 "invalid in C++: %qT vs %qT"),
4980 t1, t2);
4983 /* Quickly detect the usual case where op1 and op2 have the same type
4984 after promotion. */
4985 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4987 if (type1 == type2)
4988 result_type = type1;
4989 else
4990 result_type = TYPE_MAIN_VARIANT (type1);
4992 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4993 || code1 == COMPLEX_TYPE)
4994 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4995 || code2 == COMPLEX_TYPE))
4997 /* In C11, a conditional expression between a floating-point
4998 type and an integer type should convert the integer type to
4999 the evaluation format of the floating-point type, with
5000 possible excess precision. */
5001 tree eptype1 = type1;
5002 tree eptype2 = type2;
5003 if (flag_isoc11)
5005 tree eptype;
5006 if (ANY_INTEGRAL_TYPE_P (type1)
5007 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5009 eptype2 = eptype;
5010 if (!semantic_result_type)
5011 semantic_result_type = c_common_type (type1, type2);
5013 else if (ANY_INTEGRAL_TYPE_P (type2)
5014 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5016 eptype1 = eptype;
5017 if (!semantic_result_type)
5018 semantic_result_type = c_common_type (type1, type2);
5021 result_type = c_common_type (eptype1, eptype2);
5022 if (result_type == error_mark_node)
5023 return error_mark_node;
5024 do_warn_double_promotion (result_type, type1, type2,
5025 "implicit conversion from %qT to %qT to "
5026 "match other result of conditional",
5027 colon_loc);
5029 /* If -Wsign-compare, warn here if type1 and type2 have
5030 different signedness. We'll promote the signed to unsigned
5031 and later code won't know it used to be different.
5032 Do this check on the original types, so that explicit casts
5033 will be considered, but default promotions won't. */
5034 if (c_inhibit_evaluation_warnings == 0)
5036 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5037 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5039 if (unsigned_op1 ^ unsigned_op2)
5041 bool ovf;
5043 /* Do not warn if the result type is signed, since the
5044 signed type will only be chosen if it can represent
5045 all the values of the unsigned type. */
5046 if (!TYPE_UNSIGNED (result_type))
5047 /* OK */;
5048 else
5050 bool op1_maybe_const = true;
5051 bool op2_maybe_const = true;
5053 /* Do not warn if the signed quantity is an
5054 unsuffixed integer literal (or some static
5055 constant expression involving such literals) and
5056 it is non-negative. This warning requires the
5057 operands to be folded for best results, so do
5058 that folding in this case even without
5059 warn_sign_compare to avoid warning options
5060 possibly affecting code generation. */
5061 c_inhibit_evaluation_warnings
5062 += (ifexp == truthvalue_false_node);
5063 op1 = c_fully_fold (op1, require_constant_value,
5064 &op1_maybe_const);
5065 c_inhibit_evaluation_warnings
5066 -= (ifexp == truthvalue_false_node);
5068 c_inhibit_evaluation_warnings
5069 += (ifexp == truthvalue_true_node);
5070 op2 = c_fully_fold (op2, require_constant_value,
5071 &op2_maybe_const);
5072 c_inhibit_evaluation_warnings
5073 -= (ifexp == truthvalue_true_node);
5075 if (warn_sign_compare)
5077 if ((unsigned_op2
5078 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5079 || (unsigned_op1
5080 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5081 /* OK */;
5082 else if (unsigned_op2)
5083 warning_at (op1_loc, OPT_Wsign_compare,
5084 "operand of ?: changes signedness from "
5085 "%qT to %qT due to unsignedness of other "
5086 "operand", TREE_TYPE (orig_op1),
5087 TREE_TYPE (orig_op2));
5088 else
5089 warning_at (op2_loc, OPT_Wsign_compare,
5090 "operand of ?: changes signedness from "
5091 "%qT to %qT due to unsignedness of other "
5092 "operand", TREE_TYPE (orig_op2),
5093 TREE_TYPE (orig_op1));
5095 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5096 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5097 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5098 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5103 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5105 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5106 pedwarn (colon_loc, OPT_Wpedantic,
5107 "ISO C forbids conditional expr with only one void side");
5108 result_type = void_type_node;
5110 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5112 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5113 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5114 addr_space_t as_common;
5116 if (comp_target_types (colon_loc, type1, type2))
5117 result_type = common_pointer_type (type1, type2);
5118 else if (null_pointer_constant_p (orig_op1))
5119 result_type = type2;
5120 else if (null_pointer_constant_p (orig_op2))
5121 result_type = type1;
5122 else if (!addr_space_superset (as1, as2, &as_common))
5124 error_at (colon_loc, "pointers to disjoint address spaces "
5125 "used in conditional expression");
5126 return error_mark_node;
5128 else if (VOID_TYPE_P (TREE_TYPE (type1))
5129 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5131 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5132 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5133 & ~TYPE_QUALS (TREE_TYPE (type1))))
5134 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5135 "pointer to array loses qualifier "
5136 "in conditional expression");
5138 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5139 pedwarn (colon_loc, OPT_Wpedantic,
5140 "ISO C forbids conditional expr between "
5141 "%<void *%> and function pointer");
5142 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5143 TREE_TYPE (type2)));
5145 else if (VOID_TYPE_P (TREE_TYPE (type2))
5146 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5148 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5149 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5150 & ~TYPE_QUALS (TREE_TYPE (type2))))
5151 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5152 "pointer to array loses qualifier "
5153 "in conditional expression");
5155 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5156 pedwarn (colon_loc, OPT_Wpedantic,
5157 "ISO C forbids conditional expr between "
5158 "%<void *%> and function pointer");
5159 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5160 TREE_TYPE (type1)));
5162 /* Objective-C pointer comparisons are a bit more lenient. */
5163 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5164 result_type = objc_common_type (type1, type2);
5165 else
5167 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5169 pedwarn (colon_loc, 0,
5170 "pointer type mismatch in conditional expression");
5171 result_type = build_pointer_type
5172 (build_qualified_type (void_type_node, qual));
5175 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5177 if (!null_pointer_constant_p (orig_op2))
5178 pedwarn (colon_loc, 0,
5179 "pointer/integer type mismatch in conditional expression");
5180 else
5182 op2 = null_pointer_node;
5184 result_type = type1;
5186 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5188 if (!null_pointer_constant_p (orig_op1))
5189 pedwarn (colon_loc, 0,
5190 "pointer/integer type mismatch in conditional expression");
5191 else
5193 op1 = null_pointer_node;
5195 result_type = type2;
5198 if (!result_type)
5200 if (flag_cond_mismatch)
5201 result_type = void_type_node;
5202 else
5204 error_at (colon_loc, "type mismatch in conditional expression");
5205 return error_mark_node;
5209 /* Merge const and volatile flags of the incoming types. */
5210 result_type
5211 = build_type_variant (result_type,
5212 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5213 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5215 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5216 semantic_result_type);
5217 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5218 semantic_result_type);
5220 if (ifexp_bcp && ifexp == truthvalue_true_node)
5222 op2_int_operands = true;
5223 op1 = c_fully_fold (op1, require_constant_value, NULL);
5225 if (ifexp_bcp && ifexp == truthvalue_false_node)
5227 op1_int_operands = true;
5228 op2 = c_fully_fold (op2, require_constant_value, NULL);
5230 int_const = int_operands = (ifexp_int_operands
5231 && op1_int_operands
5232 && op2_int_operands);
5233 if (int_operands)
5235 int_const = ((ifexp == truthvalue_true_node
5236 && TREE_CODE (orig_op1) == INTEGER_CST
5237 && !TREE_OVERFLOW (orig_op1))
5238 || (ifexp == truthvalue_false_node
5239 && TREE_CODE (orig_op2) == INTEGER_CST
5240 && !TREE_OVERFLOW (orig_op2)));
5243 /* Need to convert condition operand into a vector mask. */
5244 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5246 tree vectype = TREE_TYPE (ifexp);
5247 tree elem_type = TREE_TYPE (vectype);
5248 tree zero = build_int_cst (elem_type, 0);
5249 tree zero_vec = build_vector_from_val (vectype, zero);
5250 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5251 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5254 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5255 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5256 else
5258 if (int_operands)
5260 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5261 nested inside of the expression. */
5262 op1 = c_fully_fold (op1, false, NULL);
5263 op2 = c_fully_fold (op2, false, NULL);
5265 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5266 if (int_operands)
5267 ret = note_integer_operands (ret);
5269 if (semantic_result_type)
5270 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5272 protected_set_expr_location (ret, colon_loc);
5274 /* If the OP1 and OP2 are the same and don't have side-effects,
5275 warn here, because the COND_EXPR will be turned into OP1. */
5276 if (warn_duplicated_branches
5277 && TREE_CODE (ret) == COND_EXPR
5278 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5279 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5280 "this condition has identical branches");
5282 return ret;
5285 /* Return a compound expression that performs two expressions and
5286 returns the value of the second of them.
5288 LOC is the location of the COMPOUND_EXPR. */
5290 tree
5291 build_compound_expr (location_t loc, tree expr1, tree expr2)
5293 bool expr1_int_operands, expr2_int_operands;
5294 tree eptype = NULL_TREE;
5295 tree ret;
5297 if (flag_cilkplus
5298 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5299 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5301 error_at (loc,
5302 "spawned function call cannot be part of a comma expression");
5303 return error_mark_node;
5305 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5306 if (expr1_int_operands)
5307 expr1 = remove_c_maybe_const_expr (expr1);
5308 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5309 if (expr2_int_operands)
5310 expr2 = remove_c_maybe_const_expr (expr2);
5312 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5313 expr1 = TREE_OPERAND (expr1, 0);
5314 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5316 eptype = TREE_TYPE (expr2);
5317 expr2 = TREE_OPERAND (expr2, 0);
5320 if (!TREE_SIDE_EFFECTS (expr1))
5322 /* The left-hand operand of a comma expression is like an expression
5323 statement: with -Wunused, we should warn if it doesn't have
5324 any side-effects, unless it was explicitly cast to (void). */
5325 if (warn_unused_value)
5327 if (VOID_TYPE_P (TREE_TYPE (expr1))
5328 && CONVERT_EXPR_P (expr1))
5329 ; /* (void) a, b */
5330 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5331 && TREE_CODE (expr1) == COMPOUND_EXPR
5332 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5333 ; /* (void) a, (void) b, c */
5334 else
5335 warning_at (loc, OPT_Wunused_value,
5336 "left-hand operand of comma expression has no effect");
5339 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5340 && warn_unused_value)
5342 tree r = expr1;
5343 location_t cloc = loc;
5344 while (TREE_CODE (r) == COMPOUND_EXPR)
5346 if (EXPR_HAS_LOCATION (r))
5347 cloc = EXPR_LOCATION (r);
5348 r = TREE_OPERAND (r, 1);
5350 if (!TREE_SIDE_EFFECTS (r)
5351 && !VOID_TYPE_P (TREE_TYPE (r))
5352 && !CONVERT_EXPR_P (r))
5353 warning_at (cloc, OPT_Wunused_value,
5354 "right-hand operand of comma expression has no effect");
5357 /* With -Wunused, we should also warn if the left-hand operand does have
5358 side-effects, but computes a value which is not used. For example, in
5359 `foo() + bar(), baz()' the result of the `+' operator is not used,
5360 so we should issue a warning. */
5361 else if (warn_unused_value)
5362 warn_if_unused_value (expr1, loc);
5364 if (expr2 == error_mark_node)
5365 return error_mark_node;
5367 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5369 if (flag_isoc99
5370 && expr1_int_operands
5371 && expr2_int_operands)
5372 ret = note_integer_operands (ret);
5374 if (eptype)
5375 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5377 protected_set_expr_location (ret, loc);
5378 return ret;
5381 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5382 which we are casting. OTYPE is the type of the expression being
5383 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5384 of the cast. -Wcast-qual appeared on the command line. Named
5385 address space qualifiers are not handled here, because they result
5386 in different warnings. */
5388 static void
5389 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5391 tree in_type = type;
5392 tree in_otype = otype;
5393 int added = 0;
5394 int discarded = 0;
5395 bool is_const;
5397 /* Check that the qualifiers on IN_TYPE are a superset of the
5398 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5399 nodes is uninteresting and we stop as soon as we hit a
5400 non-POINTER_TYPE node on either type. */
5403 in_otype = TREE_TYPE (in_otype);
5404 in_type = TREE_TYPE (in_type);
5406 /* GNU C allows cv-qualified function types. 'const' means the
5407 function is very pure, 'volatile' means it can't return. We
5408 need to warn when such qualifiers are added, not when they're
5409 taken away. */
5410 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5411 && TREE_CODE (in_type) == FUNCTION_TYPE)
5412 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5413 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5414 else
5415 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5416 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5418 while (TREE_CODE (in_type) == POINTER_TYPE
5419 && TREE_CODE (in_otype) == POINTER_TYPE);
5421 if (added)
5422 warning_at (loc, OPT_Wcast_qual,
5423 "cast adds %q#v qualifier to function type", added);
5425 if (discarded)
5426 /* There are qualifiers present in IN_OTYPE that are not present
5427 in IN_TYPE. */
5428 warning_at (loc, OPT_Wcast_qual,
5429 "cast discards %qv qualifier from pointer target type",
5430 discarded);
5432 if (added || discarded)
5433 return;
5435 /* A cast from **T to const **T is unsafe, because it can cause a
5436 const value to be changed with no additional warning. We only
5437 issue this warning if T is the same on both sides, and we only
5438 issue the warning if there are the same number of pointers on
5439 both sides, as otherwise the cast is clearly unsafe anyhow. A
5440 cast is unsafe when a qualifier is added at one level and const
5441 is not present at all outer levels.
5443 To issue this warning, we check at each level whether the cast
5444 adds new qualifiers not already seen. We don't need to special
5445 case function types, as they won't have the same
5446 TYPE_MAIN_VARIANT. */
5448 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5449 return;
5450 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5451 return;
5453 in_type = type;
5454 in_otype = otype;
5455 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5458 in_type = TREE_TYPE (in_type);
5459 in_otype = TREE_TYPE (in_otype);
5460 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5461 && !is_const)
5463 warning_at (loc, OPT_Wcast_qual,
5464 "to be safe all intermediate pointers in cast from "
5465 "%qT to %qT must be %<const%> qualified",
5466 otype, type);
5467 break;
5469 if (is_const)
5470 is_const = TYPE_READONLY (in_type);
5472 while (TREE_CODE (in_type) == POINTER_TYPE);
5475 /* Build an expression representing a cast to type TYPE of expression EXPR.
5476 LOC is the location of the cast-- typically the open paren of the cast. */
5478 tree
5479 build_c_cast (location_t loc, tree type, tree expr)
5481 tree value;
5483 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5484 expr = TREE_OPERAND (expr, 0);
5486 value = expr;
5488 if (type == error_mark_node || expr == error_mark_node)
5489 return error_mark_node;
5491 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5492 only in <protocol> qualifications. But when constructing cast expressions,
5493 the protocols do matter and must be kept around. */
5494 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5495 return build1 (NOP_EXPR, type, expr);
5497 type = TYPE_MAIN_VARIANT (type);
5499 if (TREE_CODE (type) == ARRAY_TYPE)
5501 error_at (loc, "cast specifies array type");
5502 return error_mark_node;
5505 if (TREE_CODE (type) == FUNCTION_TYPE)
5507 error_at (loc, "cast specifies function type");
5508 return error_mark_node;
5511 if (!VOID_TYPE_P (type))
5513 value = require_complete_type (loc, value);
5514 if (value == error_mark_node)
5515 return error_mark_node;
5518 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5520 if (RECORD_OR_UNION_TYPE_P (type))
5521 pedwarn (loc, OPT_Wpedantic,
5522 "ISO C forbids casting nonscalar to the same type");
5524 /* Convert to remove any qualifiers from VALUE's type. */
5525 value = convert (type, value);
5527 else if (TREE_CODE (type) == UNION_TYPE)
5529 tree field;
5531 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5532 if (TREE_TYPE (field) != error_mark_node
5533 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5534 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5535 break;
5537 if (field)
5539 tree t;
5540 bool maybe_const = true;
5542 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5543 t = c_fully_fold (value, false, &maybe_const);
5544 t = build_constructor_single (type, field, t);
5545 if (!maybe_const)
5546 t = c_wrap_maybe_const (t, true);
5547 t = digest_init (loc, type, t,
5548 NULL_TREE, false, true, 0);
5549 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5550 return t;
5552 error_at (loc, "cast to union type from type not present in union");
5553 return error_mark_node;
5555 else
5557 tree otype, ovalue;
5559 if (type == void_type_node)
5561 tree t = build1 (CONVERT_EXPR, type, value);
5562 SET_EXPR_LOCATION (t, loc);
5563 return t;
5566 otype = TREE_TYPE (value);
5568 /* Optionally warn about potentially worrisome casts. */
5569 if (warn_cast_qual
5570 && TREE_CODE (type) == POINTER_TYPE
5571 && TREE_CODE (otype) == POINTER_TYPE)
5572 handle_warn_cast_qual (loc, type, otype);
5574 /* Warn about conversions between pointers to disjoint
5575 address spaces. */
5576 if (TREE_CODE (type) == POINTER_TYPE
5577 && TREE_CODE (otype) == POINTER_TYPE
5578 && !null_pointer_constant_p (value))
5580 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5581 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5582 addr_space_t as_common;
5584 if (!addr_space_superset (as_to, as_from, &as_common))
5586 if (ADDR_SPACE_GENERIC_P (as_from))
5587 warning_at (loc, 0, "cast to %s address space pointer "
5588 "from disjoint generic address space pointer",
5589 c_addr_space_name (as_to));
5591 else if (ADDR_SPACE_GENERIC_P (as_to))
5592 warning_at (loc, 0, "cast to generic address space pointer "
5593 "from disjoint %s address space pointer",
5594 c_addr_space_name (as_from));
5596 else
5597 warning_at (loc, 0, "cast to %s address space pointer "
5598 "from disjoint %s address space pointer",
5599 c_addr_space_name (as_to),
5600 c_addr_space_name (as_from));
5604 /* Warn about possible alignment problems. */
5605 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5606 && TREE_CODE (type) == POINTER_TYPE
5607 && TREE_CODE (otype) == POINTER_TYPE
5608 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5609 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5610 /* Don't warn about opaque types, where the actual alignment
5611 restriction is unknown. */
5612 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5613 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5614 && min_align_of_type (TREE_TYPE (type))
5615 > min_align_of_type (TREE_TYPE (otype)))
5616 warning_at (loc, OPT_Wcast_align,
5617 "cast increases required alignment of target type");
5619 if (TREE_CODE (type) == INTEGER_TYPE
5620 && TREE_CODE (otype) == POINTER_TYPE
5621 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5622 /* Unlike conversion of integers to pointers, where the
5623 warning is disabled for converting constants because
5624 of cases such as SIG_*, warn about converting constant
5625 pointers to integers. In some cases it may cause unwanted
5626 sign extension, and a warning is appropriate. */
5627 warning_at (loc, OPT_Wpointer_to_int_cast,
5628 "cast from pointer to integer of different size");
5630 if (TREE_CODE (value) == CALL_EXPR
5631 && TREE_CODE (type) != TREE_CODE (otype))
5632 warning_at (loc, OPT_Wbad_function_cast,
5633 "cast from function call of type %qT "
5634 "to non-matching type %qT", otype, type);
5636 if (TREE_CODE (type) == POINTER_TYPE
5637 && TREE_CODE (otype) == INTEGER_TYPE
5638 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5639 /* Don't warn about converting any constant. */
5640 && !TREE_CONSTANT (value))
5641 warning_at (loc,
5642 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5643 "of different size");
5645 if (warn_strict_aliasing <= 2)
5646 strict_aliasing_warning (otype, type, expr);
5648 /* If pedantic, warn for conversions between function and object
5649 pointer types, except for converting a null pointer constant
5650 to function pointer type. */
5651 if (pedantic
5652 && TREE_CODE (type) == POINTER_TYPE
5653 && TREE_CODE (otype) == POINTER_TYPE
5654 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5655 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5656 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5657 "conversion of function pointer to object pointer type");
5659 if (pedantic
5660 && TREE_CODE (type) == POINTER_TYPE
5661 && TREE_CODE (otype) == POINTER_TYPE
5662 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5663 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5664 && !null_pointer_constant_p (value))
5665 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5666 "conversion of object pointer to function pointer type");
5668 ovalue = value;
5669 value = convert (type, value);
5671 /* Ignore any integer overflow caused by the cast. */
5672 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5674 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5676 if (!TREE_OVERFLOW (value))
5678 /* Avoid clobbering a shared constant. */
5679 value = copy_node (value);
5680 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5683 else if (TREE_OVERFLOW (value))
5684 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5685 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5689 /* Don't let a cast be an lvalue. */
5690 if (lvalue_p (value))
5691 value = non_lvalue_loc (loc, value);
5693 /* Don't allow the results of casting to floating-point or complex
5694 types be confused with actual constants, or casts involving
5695 integer and pointer types other than direct integer-to-integer
5696 and integer-to-pointer be confused with integer constant
5697 expressions and null pointer constants. */
5698 if (TREE_CODE (value) == REAL_CST
5699 || TREE_CODE (value) == COMPLEX_CST
5700 || (TREE_CODE (value) == INTEGER_CST
5701 && !((TREE_CODE (expr) == INTEGER_CST
5702 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5703 || TREE_CODE (expr) == REAL_CST
5704 || TREE_CODE (expr) == COMPLEX_CST)))
5705 value = build1 (NOP_EXPR, type, value);
5707 protected_set_expr_location (value, loc);
5708 return value;
5711 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5712 location of the open paren of the cast, or the position of the cast
5713 expr. */
5714 tree
5715 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5717 tree type;
5718 tree type_expr = NULL_TREE;
5719 bool type_expr_const = true;
5720 tree ret;
5721 int saved_wsp = warn_strict_prototypes;
5723 /* This avoids warnings about unprototyped casts on
5724 integers. E.g. "#define SIG_DFL (void(*)())0". */
5725 if (TREE_CODE (expr) == INTEGER_CST)
5726 warn_strict_prototypes = 0;
5727 type = groktypename (type_name, &type_expr, &type_expr_const);
5728 warn_strict_prototypes = saved_wsp;
5730 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5731 && reject_gcc_builtin (expr))
5732 return error_mark_node;
5734 ret = build_c_cast (loc, type, expr);
5735 if (type_expr)
5737 bool inner_expr_const = true;
5738 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5739 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5740 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5741 && inner_expr_const);
5742 SET_EXPR_LOCATION (ret, loc);
5745 if (!EXPR_HAS_LOCATION (ret))
5746 protected_set_expr_location (ret, loc);
5748 /* C++ does not permits types to be defined in a cast, but it
5749 allows references to incomplete types. */
5750 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5751 warning_at (loc, OPT_Wc___compat,
5752 "defining a type in a cast is invalid in C++");
5754 return ret;
5757 /* Build an assignment expression of lvalue LHS from value RHS.
5758 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5759 may differ from TREE_TYPE (LHS) for an enum bitfield.
5760 MODIFYCODE is the code for a binary operator that we use
5761 to combine the old value of LHS with RHS to get the new value.
5762 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5763 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5764 which may differ from TREE_TYPE (RHS) for an enum value.
5766 LOCATION is the location of the MODIFYCODE operator.
5767 RHS_LOC is the location of the RHS. */
5769 tree
5770 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5771 enum tree_code modifycode,
5772 location_t rhs_loc, tree rhs, tree rhs_origtype)
5774 tree result;
5775 tree newrhs;
5776 tree rhseval = NULL_TREE;
5777 tree lhstype = TREE_TYPE (lhs);
5778 tree olhstype = lhstype;
5779 bool npc;
5780 bool is_atomic_op;
5782 /* Types that aren't fully specified cannot be used in assignments. */
5783 lhs = require_complete_type (location, lhs);
5785 /* Avoid duplicate error messages from operands that had errors. */
5786 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5787 return error_mark_node;
5789 /* Ensure an error for assigning a non-lvalue array to an array in
5790 C90. */
5791 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5793 error_at (location, "assignment to expression with array type");
5794 return error_mark_node;
5797 /* For ObjC properties, defer this check. */
5798 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5799 return error_mark_node;
5801 is_atomic_op = really_atomic_lvalue (lhs);
5803 newrhs = rhs;
5805 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5807 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5808 lhs_origtype, modifycode, rhs_loc, rhs,
5809 rhs_origtype);
5810 if (inner == error_mark_node)
5811 return error_mark_node;
5812 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5813 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5814 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5815 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5816 protected_set_expr_location (result, location);
5817 return result;
5820 /* If a binary op has been requested, combine the old LHS value with the RHS
5821 producing the value we should actually store into the LHS. */
5823 if (modifycode != NOP_EXPR)
5825 lhs = c_fully_fold (lhs, false, NULL);
5826 lhs = stabilize_reference (lhs);
5828 /* Construct the RHS for any non-atomic compound assignemnt. */
5829 if (!is_atomic_op)
5831 /* If in LHS op= RHS the RHS has side-effects, ensure they
5832 are preevaluated before the rest of the assignment expression's
5833 side-effects, because RHS could contain e.g. function calls
5834 that modify LHS. */
5835 if (TREE_SIDE_EFFECTS (rhs))
5837 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5838 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5839 else
5840 newrhs = save_expr (rhs);
5841 rhseval = newrhs;
5842 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5843 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5844 newrhs);
5846 newrhs = build_binary_op (location,
5847 modifycode, lhs, newrhs, true);
5849 /* The original type of the right hand side is no longer
5850 meaningful. */
5851 rhs_origtype = NULL_TREE;
5855 if (c_dialect_objc ())
5857 /* Check if we are modifying an Objective-C property reference;
5858 if so, we need to generate setter calls. */
5859 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5860 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5861 else
5862 result = objc_maybe_build_modify_expr (lhs, newrhs);
5863 if (result)
5864 goto return_result;
5866 /* Else, do the check that we postponed for Objective-C. */
5867 if (!lvalue_or_else (location, lhs, lv_assign))
5868 return error_mark_node;
5871 /* Give an error for storing in something that is 'const'. */
5873 if (TYPE_READONLY (lhstype)
5874 || (RECORD_OR_UNION_TYPE_P (lhstype)
5875 && C_TYPE_FIELDS_READONLY (lhstype)))
5877 readonly_error (location, lhs, lv_assign);
5878 return error_mark_node;
5880 else if (TREE_READONLY (lhs))
5881 readonly_warning (lhs, lv_assign);
5883 /* If storing into a structure or union member,
5884 it has probably been given type `int'.
5885 Compute the type that would go with
5886 the actual amount of storage the member occupies. */
5888 if (TREE_CODE (lhs) == COMPONENT_REF
5889 && (TREE_CODE (lhstype) == INTEGER_TYPE
5890 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5891 || TREE_CODE (lhstype) == REAL_TYPE
5892 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5893 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5895 /* If storing in a field that is in actuality a short or narrower than one,
5896 we must store in the field in its actual type. */
5898 if (lhstype != TREE_TYPE (lhs))
5900 lhs = copy_node (lhs);
5901 TREE_TYPE (lhs) = lhstype;
5904 /* Issue -Wc++-compat warnings about an assignment to an enum type
5905 when LHS does not have its original type. This happens for,
5906 e.g., an enum bitfield in a struct. */
5907 if (warn_cxx_compat
5908 && lhs_origtype != NULL_TREE
5909 && lhs_origtype != lhstype
5910 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5912 tree checktype = (rhs_origtype != NULL_TREE
5913 ? rhs_origtype
5914 : TREE_TYPE (rhs));
5915 if (checktype != error_mark_node
5916 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5917 || (is_atomic_op && modifycode != NOP_EXPR)))
5918 warning_at (location, OPT_Wc___compat,
5919 "enum conversion in assignment is invalid in C++");
5922 /* If the lhs is atomic, remove that qualifier. */
5923 if (is_atomic_op)
5925 lhstype = build_qualified_type (lhstype,
5926 (TYPE_QUALS (lhstype)
5927 & ~TYPE_QUAL_ATOMIC));
5928 olhstype = build_qualified_type (olhstype,
5929 (TYPE_QUALS (lhstype)
5930 & ~TYPE_QUAL_ATOMIC));
5933 /* Convert new value to destination type. Fold it first, then
5934 restore any excess precision information, for the sake of
5935 conversion warnings. */
5937 if (!(is_atomic_op && modifycode != NOP_EXPR))
5939 tree rhs_semantic_type = NULL_TREE;
5940 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5942 rhs_semantic_type = TREE_TYPE (newrhs);
5943 newrhs = TREE_OPERAND (newrhs, 0);
5945 npc = null_pointer_constant_p (newrhs);
5946 newrhs = c_fully_fold (newrhs, false, NULL);
5947 if (rhs_semantic_type)
5948 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5949 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5950 rhs_origtype, ic_assign, npc,
5951 NULL_TREE, NULL_TREE, 0);
5952 if (TREE_CODE (newrhs) == ERROR_MARK)
5953 return error_mark_node;
5956 /* Emit ObjC write barrier, if necessary. */
5957 if (c_dialect_objc () && flag_objc_gc)
5959 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5960 if (result)
5962 protected_set_expr_location (result, location);
5963 goto return_result;
5967 /* Scan operands. */
5969 if (is_atomic_op)
5970 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5971 else
5973 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5974 TREE_SIDE_EFFECTS (result) = 1;
5975 protected_set_expr_location (result, location);
5978 /* If we got the LHS in a different type for storing in,
5979 convert the result back to the nominal type of LHS
5980 so that the value we return always has the same type
5981 as the LHS argument. */
5983 if (olhstype == TREE_TYPE (result))
5984 goto return_result;
5986 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5987 rhs_origtype, ic_assign, false, NULL_TREE,
5988 NULL_TREE, 0);
5989 protected_set_expr_location (result, location);
5991 return_result:
5992 if (rhseval)
5993 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5994 return result;
5997 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5998 This is used to implement -fplan9-extensions. */
6000 static bool
6001 find_anonymous_field_with_type (tree struct_type, tree type)
6003 tree field;
6004 bool found;
6006 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6007 found = false;
6008 for (field = TYPE_FIELDS (struct_type);
6009 field != NULL_TREE;
6010 field = TREE_CHAIN (field))
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 (DECL_NAME (field) == NULL
6017 && comptypes (type, fieldtype))
6019 if (found)
6020 return false;
6021 found = true;
6023 else if (DECL_NAME (field) == NULL
6024 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6025 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6027 if (found)
6028 return false;
6029 found = true;
6032 return found;
6035 /* RHS is an expression whose type is pointer to struct. If there is
6036 an anonymous field in RHS with type TYPE, then return a pointer to
6037 that field in RHS. This is used with -fplan9-extensions. This
6038 returns NULL if no conversion could be found. */
6040 static tree
6041 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6043 tree rhs_struct_type, lhs_main_type;
6044 tree field, found_field;
6045 bool found_sub_field;
6046 tree ret;
6048 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6049 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6050 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6052 gcc_assert (POINTER_TYPE_P (type));
6053 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6054 ? c_build_qualified_type (TREE_TYPE (type),
6055 TYPE_QUAL_ATOMIC)
6056 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6058 found_field = NULL_TREE;
6059 found_sub_field = false;
6060 for (field = TYPE_FIELDS (rhs_struct_type);
6061 field != NULL_TREE;
6062 field = TREE_CHAIN (field))
6064 if (DECL_NAME (field) != NULL_TREE
6065 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6066 continue;
6067 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6068 ? c_build_qualified_type (TREE_TYPE (field),
6069 TYPE_QUAL_ATOMIC)
6070 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6071 if (comptypes (lhs_main_type, fieldtype))
6073 if (found_field != NULL_TREE)
6074 return NULL_TREE;
6075 found_field = field;
6077 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6078 lhs_main_type))
6080 if (found_field != NULL_TREE)
6081 return NULL_TREE;
6082 found_field = field;
6083 found_sub_field = true;
6087 if (found_field == NULL_TREE)
6088 return NULL_TREE;
6090 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6091 build_fold_indirect_ref (rhs), found_field,
6092 NULL_TREE);
6093 ret = build_fold_addr_expr_loc (location, ret);
6095 if (found_sub_field)
6097 ret = convert_to_anonymous_field (location, type, ret);
6098 gcc_assert (ret != NULL_TREE);
6101 return ret;
6104 /* Issue an error message for a bad initializer component.
6105 GMSGID identifies the message.
6106 The component name is taken from the spelling stack. */
6108 static void
6109 error_init (location_t loc, const char *gmsgid)
6111 char *ofwhat;
6113 /* The gmsgid may be a format string with %< and %>. */
6114 error_at (loc, gmsgid);
6115 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6116 if (*ofwhat)
6117 inform (loc, "(near initialization for %qs)", ofwhat);
6120 /* Issue a pedantic warning for a bad initializer component. OPT is
6121 the option OPT_* (from options.h) controlling this warning or 0 if
6122 it is unconditionally given. GMSGID identifies the message. The
6123 component name is taken from the spelling stack. */
6125 static void ATTRIBUTE_GCC_DIAG (3,0)
6126 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6128 /* Use the location where a macro was expanded rather than where
6129 it was defined to make sure macros defined in system headers
6130 but used incorrectly elsewhere are diagnosed. */
6131 source_location exploc = expansion_point_location_if_in_system_header (loc);
6133 va_list ap;
6134 va_start (ap, gmsgid);
6135 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6136 va_end (ap);
6137 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6138 if (*ofwhat && warned)
6139 inform (exploc, "(near initialization for %qs)", ofwhat);
6142 /* Issue a warning for a bad initializer component.
6144 OPT is the OPT_W* value corresponding to the warning option that
6145 controls this warning. GMSGID identifies the message. The
6146 component name is taken from the spelling stack. */
6148 static void
6149 warning_init (location_t loc, int opt, const char *gmsgid)
6151 char *ofwhat;
6152 bool warned;
6154 /* Use the location where a macro was expanded rather than where
6155 it was defined to make sure macros defined in system headers
6156 but used incorrectly elsewhere are diagnosed. */
6157 source_location exploc = expansion_point_location_if_in_system_header (loc);
6159 /* The gmsgid may be a format string with %< and %>. */
6160 warned = warning_at (exploc, opt, gmsgid);
6161 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6162 if (*ofwhat && warned)
6163 inform (exploc, "(near initialization for %qs)", ofwhat);
6166 /* If TYPE is an array type and EXPR is a parenthesized string
6167 constant, warn if pedantic that EXPR is being used to initialize an
6168 object of type TYPE. */
6170 void
6171 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6173 if (pedantic
6174 && TREE_CODE (type) == ARRAY_TYPE
6175 && TREE_CODE (expr.value) == STRING_CST
6176 && expr.original_code != STRING_CST)
6177 pedwarn_init (loc, OPT_Wpedantic,
6178 "array initialized from parenthesized string constant");
6181 /* Attempt to locate the parameter with the given index within FNDECL,
6182 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6184 static location_t
6185 get_fndecl_argument_location (tree fndecl, int argnum)
6187 int i;
6188 tree param;
6190 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6191 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6192 i < argnum && param;
6193 i++, param = TREE_CHAIN (param))
6196 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6197 return DECL_SOURCE_LOCATION (FNDECL). */
6198 if (param == NULL)
6199 return DECL_SOURCE_LOCATION (fndecl);
6201 return DECL_SOURCE_LOCATION (param);
6204 /* Issue a note about a mismatching argument for parameter PARMNUM
6205 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6206 Attempt to issue the note at the pertinent parameter of the decl;
6207 failing that issue it at the location of FUNDECL; failing that
6208 issue it at PLOC. */
6210 static void
6211 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6212 tree expected_type, tree actual_type)
6214 location_t loc;
6215 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6216 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6217 else
6218 loc = ploc;
6220 inform (loc,
6221 "expected %qT but argument is of type %qT",
6222 expected_type, actual_type);
6225 /* Convert value RHS to type TYPE as preparation for an assignment to
6226 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6227 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6228 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6229 constant before any folding.
6230 The real work of conversion is done by `convert'.
6231 The purpose of this function is to generate error messages
6232 for assignments that are not allowed in C.
6233 ERRTYPE says whether it is argument passing, assignment,
6234 initialization or return.
6236 In the following example, '~' denotes where EXPR_LOC and '^' where
6237 LOCATION point to:
6239 f (var); [ic_argpass]
6240 ^ ~~~
6241 x = var; [ic_assign]
6242 ^ ~~~;
6243 int x = var; [ic_init]
6245 return x; [ic_return]
6248 FUNCTION is a tree for the function being called.
6249 PARMNUM is the number of the argument, for printing in error messages. */
6251 static tree
6252 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6253 tree rhs, tree origtype, enum impl_conv errtype,
6254 bool null_pointer_constant, tree fundecl,
6255 tree function, int parmnum)
6257 enum tree_code codel = TREE_CODE (type);
6258 tree orig_rhs = rhs;
6259 tree rhstype;
6260 enum tree_code coder;
6261 tree rname = NULL_TREE;
6262 bool objc_ok = false;
6264 /* Use the expansion point location to handle cases such as user's
6265 function returning a wrong-type macro defined in a system header. */
6266 location = expansion_point_location_if_in_system_header (location);
6268 if (errtype == ic_argpass)
6270 tree selector;
6271 /* Change pointer to function to the function itself for
6272 diagnostics. */
6273 if (TREE_CODE (function) == ADDR_EXPR
6274 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6275 function = TREE_OPERAND (function, 0);
6277 /* Handle an ObjC selector specially for diagnostics. */
6278 selector = objc_message_selector ();
6279 rname = function;
6280 if (selector && parmnum > 2)
6282 rname = selector;
6283 parmnum -= 2;
6287 /* This macro is used to emit diagnostics to ensure that all format
6288 strings are complete sentences, visible to gettext and checked at
6289 compile time. */
6290 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6291 do { \
6292 switch (errtype) \
6294 case ic_argpass: \
6295 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6296 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6297 break; \
6298 case ic_assign: \
6299 pedwarn (LOCATION, OPT, AS); \
6300 break; \
6301 case ic_init: \
6302 pedwarn_init (LOCATION, OPT, IN); \
6303 break; \
6304 case ic_return: \
6305 pedwarn (LOCATION, OPT, RE); \
6306 break; \
6307 default: \
6308 gcc_unreachable (); \
6310 } while (0)
6312 /* This macro is used to emit diagnostics to ensure that all format
6313 strings are complete sentences, visible to gettext and checked at
6314 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6315 extra parameter to enumerate qualifiers. */
6316 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6317 do { \
6318 switch (errtype) \
6320 case ic_argpass: \
6321 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6322 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6323 break; \
6324 case ic_assign: \
6325 pedwarn (LOCATION, OPT, AS, QUALS); \
6326 break; \
6327 case ic_init: \
6328 pedwarn (LOCATION, OPT, IN, QUALS); \
6329 break; \
6330 case ic_return: \
6331 pedwarn (LOCATION, OPT, RE, QUALS); \
6332 break; \
6333 default: \
6334 gcc_unreachable (); \
6336 } while (0)
6338 /* This macro is used to emit diagnostics to ensure that all format
6339 strings are complete sentences, visible to gettext and checked at
6340 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6341 warning_at instead of pedwarn. */
6342 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6343 do { \
6344 switch (errtype) \
6346 case ic_argpass: \
6347 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6348 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6349 break; \
6350 case ic_assign: \
6351 warning_at (LOCATION, OPT, AS, QUALS); \
6352 break; \
6353 case ic_init: \
6354 warning_at (LOCATION, OPT, IN, QUALS); \
6355 break; \
6356 case ic_return: \
6357 warning_at (LOCATION, OPT, RE, QUALS); \
6358 break; \
6359 default: \
6360 gcc_unreachable (); \
6362 } while (0)
6364 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6365 rhs = TREE_OPERAND (rhs, 0);
6367 rhstype = TREE_TYPE (rhs);
6368 coder = TREE_CODE (rhstype);
6370 if (coder == ERROR_MARK)
6371 return error_mark_node;
6373 if (c_dialect_objc ())
6375 int parmno;
6377 switch (errtype)
6379 case ic_return:
6380 parmno = 0;
6381 break;
6383 case ic_assign:
6384 parmno = -1;
6385 break;
6387 case ic_init:
6388 parmno = -2;
6389 break;
6391 default:
6392 parmno = parmnum;
6393 break;
6396 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6399 if (warn_cxx_compat)
6401 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6402 if (checktype != error_mark_node
6403 && TREE_CODE (type) == ENUMERAL_TYPE
6404 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6405 switch (errtype)
6407 case ic_argpass:
6408 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6409 "passing argument %d of %qE is invalid in C++",
6410 parmnum, rname))
6411 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6412 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6413 "expected %qT but argument is of type %qT",
6414 type, rhstype);
6415 break;
6416 case ic_assign:
6417 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6418 "%qT in assignment is invalid in C++", rhstype, type);
6419 break;
6420 case ic_init:
6421 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6422 "%qT to %qT in initialization is invalid in C++",
6423 rhstype, type);
6424 break;
6425 case ic_return:
6426 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6427 "%qT in return is invalid in C++", rhstype, type);
6428 break;
6429 default:
6430 gcc_unreachable ();
6434 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6435 return rhs;
6437 if (coder == VOID_TYPE)
6439 /* Except for passing an argument to an unprototyped function,
6440 this is a constraint violation. When passing an argument to
6441 an unprototyped function, it is compile-time undefined;
6442 making it a constraint in that case was rejected in
6443 DR#252. */
6444 error_at (location, "void value not ignored as it ought to be");
6445 return error_mark_node;
6447 rhs = require_complete_type (location, rhs);
6448 if (rhs == error_mark_node)
6449 return error_mark_node;
6451 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6452 return error_mark_node;
6454 /* A non-reference type can convert to a reference. This handles
6455 va_start, va_copy and possibly port built-ins. */
6456 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6458 if (!lvalue_p (rhs))
6460 error_at (location, "cannot pass rvalue to reference parameter");
6461 return error_mark_node;
6463 if (!c_mark_addressable (rhs))
6464 return error_mark_node;
6465 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6466 SET_EXPR_LOCATION (rhs, location);
6468 rhs = convert_for_assignment (location, expr_loc,
6469 build_pointer_type (TREE_TYPE (type)),
6470 rhs, origtype, errtype,
6471 null_pointer_constant, fundecl, function,
6472 parmnum);
6473 if (rhs == error_mark_node)
6474 return error_mark_node;
6476 rhs = build1 (NOP_EXPR, type, rhs);
6477 SET_EXPR_LOCATION (rhs, location);
6478 return rhs;
6480 /* Some types can interconvert without explicit casts. */
6481 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6482 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6483 return convert (type, rhs);
6484 /* Arithmetic types all interconvert, and enum is treated like int. */
6485 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6486 || codel == FIXED_POINT_TYPE
6487 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6488 || codel == BOOLEAN_TYPE)
6489 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6490 || coder == FIXED_POINT_TYPE
6491 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6492 || coder == BOOLEAN_TYPE))
6494 tree ret;
6495 bool save = in_late_binary_op;
6496 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6497 || (coder == REAL_TYPE
6498 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6499 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6500 in_late_binary_op = true;
6501 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6502 ? expr_loc : location, type, orig_rhs);
6503 in_late_binary_op = save;
6504 return ret;
6507 /* Aggregates in different TUs might need conversion. */
6508 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6509 && codel == coder
6510 && comptypes (type, rhstype))
6511 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6512 ? expr_loc : location, type, rhs);
6514 /* Conversion to a transparent union or record from its member types.
6515 This applies only to function arguments. */
6516 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6517 && TYPE_TRANSPARENT_AGGR (type))
6518 && errtype == ic_argpass)
6520 tree memb, marginal_memb = NULL_TREE;
6522 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6524 tree memb_type = TREE_TYPE (memb);
6526 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6527 TYPE_MAIN_VARIANT (rhstype)))
6528 break;
6530 if (TREE_CODE (memb_type) != POINTER_TYPE)
6531 continue;
6533 if (coder == POINTER_TYPE)
6535 tree ttl = TREE_TYPE (memb_type);
6536 tree ttr = TREE_TYPE (rhstype);
6538 /* Any non-function converts to a [const][volatile] void *
6539 and vice versa; otherwise, targets must be the same.
6540 Meanwhile, the lhs target must have all the qualifiers of
6541 the rhs. */
6542 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6543 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6544 || comp_target_types (location, memb_type, rhstype))
6546 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6547 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6548 /* If this type won't generate any warnings, use it. */
6549 if (lquals == rquals
6550 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6551 && TREE_CODE (ttl) == FUNCTION_TYPE)
6552 ? ((lquals | rquals) == rquals)
6553 : ((lquals | rquals) == lquals)))
6554 break;
6556 /* Keep looking for a better type, but remember this one. */
6557 if (!marginal_memb)
6558 marginal_memb = memb;
6562 /* Can convert integer zero to any pointer type. */
6563 if (null_pointer_constant)
6565 rhs = null_pointer_node;
6566 break;
6570 if (memb || marginal_memb)
6572 if (!memb)
6574 /* We have only a marginally acceptable member type;
6575 it needs a warning. */
6576 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6577 tree ttr = TREE_TYPE (rhstype);
6579 /* Const and volatile mean something different for function
6580 types, so the usual warnings are not appropriate. */
6581 if (TREE_CODE (ttr) == FUNCTION_TYPE
6582 && TREE_CODE (ttl) == FUNCTION_TYPE)
6584 /* Because const and volatile on functions are
6585 restrictions that say the function will not do
6586 certain things, it is okay to use a const or volatile
6587 function where an ordinary one is wanted, but not
6588 vice-versa. */
6589 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6590 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6591 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6592 OPT_Wdiscarded_qualifiers,
6593 G_("passing argument %d of %qE "
6594 "makes %q#v qualified function "
6595 "pointer from unqualified"),
6596 G_("assignment makes %q#v qualified "
6597 "function pointer from "
6598 "unqualified"),
6599 G_("initialization makes %q#v qualified "
6600 "function pointer from "
6601 "unqualified"),
6602 G_("return makes %q#v qualified function "
6603 "pointer from unqualified"),
6604 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6606 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6607 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6608 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6609 OPT_Wdiscarded_qualifiers,
6610 G_("passing argument %d of %qE discards "
6611 "%qv qualifier from pointer target type"),
6612 G_("assignment discards %qv qualifier "
6613 "from pointer target type"),
6614 G_("initialization discards %qv qualifier "
6615 "from pointer target type"),
6616 G_("return discards %qv qualifier from "
6617 "pointer target type"),
6618 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6620 memb = marginal_memb;
6623 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6624 pedwarn (location, OPT_Wpedantic,
6625 "ISO C prohibits argument conversion to union type");
6627 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6628 return build_constructor_single (type, memb, rhs);
6632 /* Conversions among pointers */
6633 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6634 && (coder == codel))
6636 tree ttl = TREE_TYPE (type);
6637 tree ttr = TREE_TYPE (rhstype);
6638 tree mvl = ttl;
6639 tree mvr = ttr;
6640 bool is_opaque_pointer;
6641 int target_cmp = 0; /* Cache comp_target_types () result. */
6642 addr_space_t asl;
6643 addr_space_t asr;
6645 if (TREE_CODE (mvl) != ARRAY_TYPE)
6646 mvl = (TYPE_ATOMIC (mvl)
6647 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6648 TYPE_QUAL_ATOMIC)
6649 : TYPE_MAIN_VARIANT (mvl));
6650 if (TREE_CODE (mvr) != ARRAY_TYPE)
6651 mvr = (TYPE_ATOMIC (mvr)
6652 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6653 TYPE_QUAL_ATOMIC)
6654 : TYPE_MAIN_VARIANT (mvr));
6655 /* Opaque pointers are treated like void pointers. */
6656 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6658 /* The Plan 9 compiler permits a pointer to a struct to be
6659 automatically converted into a pointer to an anonymous field
6660 within the struct. */
6661 if (flag_plan9_extensions
6662 && RECORD_OR_UNION_TYPE_P (mvl)
6663 && RECORD_OR_UNION_TYPE_P (mvr)
6664 && mvl != mvr)
6666 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6667 if (new_rhs != NULL_TREE)
6669 rhs = new_rhs;
6670 rhstype = TREE_TYPE (rhs);
6671 coder = TREE_CODE (rhstype);
6672 ttr = TREE_TYPE (rhstype);
6673 mvr = TYPE_MAIN_VARIANT (ttr);
6677 /* C++ does not allow the implicit conversion void* -> T*. However,
6678 for the purpose of reducing the number of false positives, we
6679 tolerate the special case of
6681 int *p = NULL;
6683 where NULL is typically defined in C to be '(void *) 0'. */
6684 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6685 warning_at (errtype == ic_argpass ? expr_loc : location,
6686 OPT_Wc___compat,
6687 "request for implicit conversion "
6688 "from %qT to %qT not permitted in C++", rhstype, type);
6690 /* See if the pointers point to incompatible address spaces. */
6691 asl = TYPE_ADDR_SPACE (ttl);
6692 asr = TYPE_ADDR_SPACE (ttr);
6693 if (!null_pointer_constant_p (rhs)
6694 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6696 switch (errtype)
6698 case ic_argpass:
6699 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6700 "non-enclosed address space", parmnum, rname);
6701 break;
6702 case ic_assign:
6703 error_at (location, "assignment from pointer to "
6704 "non-enclosed address space");
6705 break;
6706 case ic_init:
6707 error_at (location, "initialization from pointer to "
6708 "non-enclosed address space");
6709 break;
6710 case ic_return:
6711 error_at (location, "return from pointer to "
6712 "non-enclosed address space");
6713 break;
6714 default:
6715 gcc_unreachable ();
6717 return error_mark_node;
6720 /* Check if the right-hand side has a format attribute but the
6721 left-hand side doesn't. */
6722 if (warn_suggest_attribute_format
6723 && check_missing_format_attribute (type, rhstype))
6725 switch (errtype)
6727 case ic_argpass:
6728 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6729 "argument %d of %qE might be "
6730 "a candidate for a format attribute",
6731 parmnum, rname);
6732 break;
6733 case ic_assign:
6734 warning_at (location, OPT_Wsuggest_attribute_format,
6735 "assignment left-hand side might be "
6736 "a candidate for a format attribute");
6737 break;
6738 case ic_init:
6739 warning_at (location, OPT_Wsuggest_attribute_format,
6740 "initialization left-hand side might be "
6741 "a candidate for a format attribute");
6742 break;
6743 case ic_return:
6744 warning_at (location, OPT_Wsuggest_attribute_format,
6745 "return type might be "
6746 "a candidate for a format attribute");
6747 break;
6748 default:
6749 gcc_unreachable ();
6753 /* Any non-function converts to a [const][volatile] void *
6754 and vice versa; otherwise, targets must be the same.
6755 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6756 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6757 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6758 || (target_cmp = comp_target_types (location, type, rhstype))
6759 || is_opaque_pointer
6760 || ((c_common_unsigned_type (mvl)
6761 == c_common_unsigned_type (mvr))
6762 && (c_common_signed_type (mvl)
6763 == c_common_signed_type (mvr))
6764 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6766 /* Warn about loss of qualifers from pointers to arrays with
6767 qualifiers on the element type. */
6768 if (TREE_CODE (ttr) == ARRAY_TYPE)
6770 ttr = strip_array_types (ttr);
6771 ttl = strip_array_types (ttl);
6773 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6774 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6775 WARNING_FOR_QUALIFIERS (location, expr_loc,
6776 OPT_Wdiscarded_array_qualifiers,
6777 G_("passing argument %d of %qE discards "
6778 "%qv qualifier from pointer target type"),
6779 G_("assignment discards %qv qualifier "
6780 "from pointer target type"),
6781 G_("initialization discards %qv qualifier "
6782 "from pointer target type"),
6783 G_("return discards %qv qualifier from "
6784 "pointer target type"),
6785 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6787 else if (pedantic
6788 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6790 (VOID_TYPE_P (ttr)
6791 && !null_pointer_constant
6792 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6793 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6794 G_("ISO C forbids passing argument %d of "
6795 "%qE between function pointer "
6796 "and %<void *%>"),
6797 G_("ISO C forbids assignment between "
6798 "function pointer and %<void *%>"),
6799 G_("ISO C forbids initialization between "
6800 "function pointer and %<void *%>"),
6801 G_("ISO C forbids return between function "
6802 "pointer and %<void *%>"));
6803 /* Const and volatile mean something different for function types,
6804 so the usual warnings are not appropriate. */
6805 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6806 && TREE_CODE (ttl) != FUNCTION_TYPE)
6808 /* Don't warn about loss of qualifier for conversions from
6809 qualified void* to pointers to arrays with corresponding
6810 qualifier on the element type. */
6811 if (!pedantic)
6812 ttl = strip_array_types (ttl);
6814 /* Assignments between atomic and non-atomic objects are OK. */
6815 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6816 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6818 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6819 OPT_Wdiscarded_qualifiers,
6820 G_("passing argument %d of %qE discards "
6821 "%qv qualifier from pointer target type"),
6822 G_("assignment discards %qv qualifier "
6823 "from pointer target type"),
6824 G_("initialization discards %qv qualifier "
6825 "from pointer target type"),
6826 G_("return discards %qv qualifier from "
6827 "pointer target type"),
6828 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6830 /* If this is not a case of ignoring a mismatch in signedness,
6831 no warning. */
6832 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6833 || target_cmp)
6835 /* If there is a mismatch, do warn. */
6836 else if (warn_pointer_sign)
6837 switch (errtype)
6839 case ic_argpass:
6840 if (pedwarn (expr_loc, OPT_Wpointer_sign,
6841 "pointer targets in passing argument %d of "
6842 "%qE differ in signedness", parmnum, rname))
6843 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6844 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6845 "expected %qT but argument is of type %qT",
6846 type, rhstype);
6847 break;
6848 case ic_assign:
6849 pedwarn (location, OPT_Wpointer_sign,
6850 "pointer targets in assignment from %qT to %qT "
6851 "differ in signedness", rhstype, type);
6852 break;
6853 case ic_init:
6854 pedwarn_init (location, OPT_Wpointer_sign,
6855 "pointer targets in initialization of %qT "
6856 "from %qT differ in signedness", type,
6857 rhstype);
6858 break;
6859 case ic_return:
6860 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6861 "returning %qT from a function with return type "
6862 "%qT differ in signedness", rhstype, type);
6863 break;
6864 default:
6865 gcc_unreachable ();
6868 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6869 && TREE_CODE (ttr) == FUNCTION_TYPE)
6871 /* Because const and volatile on functions are restrictions
6872 that say the function will not do certain things,
6873 it is okay to use a const or volatile function
6874 where an ordinary one is wanted, but not vice-versa. */
6875 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6876 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6877 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6878 OPT_Wdiscarded_qualifiers,
6879 G_("passing argument %d of %qE makes "
6880 "%q#v qualified function pointer "
6881 "from unqualified"),
6882 G_("assignment makes %q#v qualified function "
6883 "pointer from unqualified"),
6884 G_("initialization makes %q#v qualified "
6885 "function pointer from unqualified"),
6886 G_("return makes %q#v qualified function "
6887 "pointer from unqualified"),
6888 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6891 /* Avoid warning about the volatile ObjC EH puts on decls. */
6892 else if (!objc_ok)
6894 switch (errtype)
6896 case ic_argpass:
6897 if (pedwarn (expr_loc, OPT_Wincompatible_pointer_types,
6898 "passing argument %d of %qE from incompatible "
6899 "pointer type", parmnum, rname))
6900 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
6901 break;
6902 case ic_assign:
6903 pedwarn (location, OPT_Wincompatible_pointer_types,
6904 "assignment to %qT from incompatible pointer type %qT",
6905 type, rhstype);
6906 break;
6907 case ic_init:
6908 pedwarn_init (location, OPT_Wincompatible_pointer_types,
6909 "initialization of %qT from incompatible pointer "
6910 "type %qT", type, rhstype);
6911 break;
6912 case ic_return:
6913 pedwarn (location, OPT_Wincompatible_pointer_types,
6914 "returning %qT from a function with incompatible "
6915 "return type %qT", rhstype, type);
6916 break;
6917 default:
6918 gcc_unreachable ();
6922 return convert (type, rhs);
6924 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6926 /* ??? This should not be an error when inlining calls to
6927 unprototyped functions. */
6928 error_at (location, "invalid use of non-lvalue array");
6929 return error_mark_node;
6931 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6933 /* An explicit constant 0 can convert to a pointer,
6934 or one that results from arithmetic, even including
6935 a cast to integer type. */
6936 if (!null_pointer_constant)
6937 switch (errtype)
6939 case ic_argpass:
6940 if (pedwarn (expr_loc, OPT_Wint_conversion,
6941 "passing argument %d of %qE makes pointer from "
6942 "integer without a cast", parmnum, rname))
6943 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
6944 break;
6945 case ic_assign:
6946 pedwarn (location, OPT_Wint_conversion,
6947 "assignment to %qT from %qT makes pointer from integer "
6948 "without a cast", type, rhstype);
6949 break;
6950 case ic_init:
6951 pedwarn_init (location, OPT_Wint_conversion,
6952 "initialization of %qT from %qT makes pointer from "
6953 "integer without a cast", type, rhstype);
6954 break;
6955 case ic_return:
6956 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6957 "function with return type %qT makes pointer from "
6958 "integer without a cast", rhstype, type);
6959 break;
6960 default:
6961 gcc_unreachable ();
6964 return convert (type, rhs);
6966 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6968 switch (errtype)
6970 case ic_argpass:
6971 if (pedwarn (expr_loc, OPT_Wint_conversion,
6972 "passing argument %d of %qE makes integer from "
6973 "pointer without a cast", parmnum, rname))
6974 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
6975 break;
6976 case ic_assign:
6977 pedwarn (location, OPT_Wint_conversion,
6978 "assignment to %qT from %qT makes integer from pointer "
6979 "without a cast", type, rhstype);
6980 break;
6981 case ic_init:
6982 pedwarn_init (location, OPT_Wint_conversion,
6983 "initialization of %qT from %qT makes integer from "
6984 "pointer without a cast", type, rhstype);
6985 break;
6986 case ic_return:
6987 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6988 "function with return type %qT makes integer from "
6989 "pointer without a cast", rhstype, type);
6990 break;
6991 default:
6992 gcc_unreachable ();
6995 return convert (type, rhs);
6997 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6999 tree ret;
7000 bool save = in_late_binary_op;
7001 in_late_binary_op = true;
7002 ret = convert (type, rhs);
7003 in_late_binary_op = save;
7004 return ret;
7007 switch (errtype)
7009 case ic_argpass:
7010 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
7011 rname);
7012 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7013 break;
7014 case ic_assign:
7015 error_at (location, "incompatible types when assigning to type %qT from "
7016 "type %qT", type, rhstype);
7017 break;
7018 case ic_init:
7019 error_at (location,
7020 "incompatible types when initializing type %qT using type %qT",
7021 type, rhstype);
7022 break;
7023 case ic_return:
7024 error_at (location,
7025 "incompatible types when returning type %qT but %qT was "
7026 "expected", rhstype, type);
7027 break;
7028 default:
7029 gcc_unreachable ();
7032 return error_mark_node;
7035 /* If VALUE is a compound expr all of whose expressions are constant, then
7036 return its value. Otherwise, return error_mark_node.
7038 This is for handling COMPOUND_EXPRs as initializer elements
7039 which is allowed with a warning when -pedantic is specified. */
7041 static tree
7042 valid_compound_expr_initializer (tree value, tree endtype)
7044 if (TREE_CODE (value) == COMPOUND_EXPR)
7046 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7047 == error_mark_node)
7048 return error_mark_node;
7049 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7050 endtype);
7052 else if (!initializer_constant_valid_p (value, endtype))
7053 return error_mark_node;
7054 else
7055 return value;
7058 /* Perform appropriate conversions on the initial value of a variable,
7059 store it in the declaration DECL,
7060 and print any error messages that are appropriate.
7061 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7062 If the init is invalid, store an ERROR_MARK.
7064 INIT_LOC is the location of the initial value. */
7066 void
7067 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7069 tree value, type;
7070 bool npc = false;
7072 /* If variable's type was invalidly declared, just ignore it. */
7074 type = TREE_TYPE (decl);
7075 if (TREE_CODE (type) == ERROR_MARK)
7076 return;
7078 /* Digest the specified initializer into an expression. */
7080 if (init)
7081 npc = null_pointer_constant_p (init);
7082 value = digest_init (init_loc, type, init, origtype, npc,
7083 true, TREE_STATIC (decl));
7085 /* Store the expression if valid; else report error. */
7087 if (!in_system_header_at (input_location)
7088 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7089 warning (OPT_Wtraditional, "traditional C rejects automatic "
7090 "aggregate initialization");
7092 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7093 DECL_INITIAL (decl) = value;
7095 /* ANSI wants warnings about out-of-range constant initializers. */
7096 STRIP_TYPE_NOPS (value);
7097 if (TREE_STATIC (decl))
7098 constant_expression_warning (value);
7100 /* Check if we need to set array size from compound literal size. */
7101 if (TREE_CODE (type) == ARRAY_TYPE
7102 && TYPE_DOMAIN (type) == NULL_TREE
7103 && value != error_mark_node)
7105 tree inside_init = init;
7107 STRIP_TYPE_NOPS (inside_init);
7108 inside_init = fold (inside_init);
7110 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7112 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7114 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7116 /* For int foo[] = (int [3]){1}; we need to set array size
7117 now since later on array initializer will be just the
7118 brace enclosed list of the compound literal. */
7119 tree etype = strip_array_types (TREE_TYPE (decl));
7120 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7121 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7122 layout_type (type);
7123 layout_decl (cldecl, 0);
7124 TREE_TYPE (decl)
7125 = c_build_qualified_type (type, TYPE_QUALS (etype));
7131 /* Methods for storing and printing names for error messages. */
7133 /* Implement a spelling stack that allows components of a name to be pushed
7134 and popped. Each element on the stack is this structure. */
7136 struct spelling
7138 int kind;
7139 union
7141 unsigned HOST_WIDE_INT i;
7142 const char *s;
7143 } u;
7146 #define SPELLING_STRING 1
7147 #define SPELLING_MEMBER 2
7148 #define SPELLING_BOUNDS 3
7150 static struct spelling *spelling; /* Next stack element (unused). */
7151 static struct spelling *spelling_base; /* Spelling stack base. */
7152 static int spelling_size; /* Size of the spelling stack. */
7154 /* Macros to save and restore the spelling stack around push_... functions.
7155 Alternative to SAVE_SPELLING_STACK. */
7157 #define SPELLING_DEPTH() (spelling - spelling_base)
7158 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7160 /* Push an element on the spelling stack with type KIND and assign VALUE
7161 to MEMBER. */
7163 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7165 int depth = SPELLING_DEPTH (); \
7167 if (depth >= spelling_size) \
7169 spelling_size += 10; \
7170 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7171 spelling_size); \
7172 RESTORE_SPELLING_DEPTH (depth); \
7175 spelling->kind = (KIND); \
7176 spelling->MEMBER = (VALUE); \
7177 spelling++; \
7180 /* Push STRING on the stack. Printed literally. */
7182 static void
7183 push_string (const char *string)
7185 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7188 /* Push a member name on the stack. Printed as '.' STRING. */
7190 static void
7191 push_member_name (tree decl)
7193 const char *const string
7194 = (DECL_NAME (decl)
7195 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7196 : _("<anonymous>"));
7197 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7200 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7202 static void
7203 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7205 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7208 /* Compute the maximum size in bytes of the printed spelling. */
7210 static int
7211 spelling_length (void)
7213 int size = 0;
7214 struct spelling *p;
7216 for (p = spelling_base; p < spelling; p++)
7218 if (p->kind == SPELLING_BOUNDS)
7219 size += 25;
7220 else
7221 size += strlen (p->u.s) + 1;
7224 return size;
7227 /* Print the spelling to BUFFER and return it. */
7229 static char *
7230 print_spelling (char *buffer)
7232 char *d = buffer;
7233 struct spelling *p;
7235 for (p = spelling_base; p < spelling; p++)
7236 if (p->kind == SPELLING_BOUNDS)
7238 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7239 d += strlen (d);
7241 else
7243 const char *s;
7244 if (p->kind == SPELLING_MEMBER)
7245 *d++ = '.';
7246 for (s = p->u.s; (*d = *s++); d++)
7249 *d++ = '\0';
7250 return buffer;
7253 /* Digest the parser output INIT as an initializer for type TYPE.
7254 Return a C expression of type TYPE to represent the initial value.
7256 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7258 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7260 If INIT is a string constant, STRICT_STRING is true if it is
7261 unparenthesized or we should not warn here for it being parenthesized.
7262 For other types of INIT, STRICT_STRING is not used.
7264 INIT_LOC is the location of the INIT.
7266 REQUIRE_CONSTANT requests an error if non-constant initializers or
7267 elements are seen. */
7269 static tree
7270 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7271 bool null_pointer_constant, bool strict_string,
7272 int require_constant)
7274 enum tree_code code = TREE_CODE (type);
7275 tree inside_init = init;
7276 tree semantic_type = NULL_TREE;
7277 bool maybe_const = true;
7279 if (type == error_mark_node
7280 || !init
7281 || error_operand_p (init))
7282 return error_mark_node;
7284 STRIP_TYPE_NOPS (inside_init);
7286 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7288 semantic_type = TREE_TYPE (inside_init);
7289 inside_init = TREE_OPERAND (inside_init, 0);
7291 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7292 inside_init = decl_constant_value_for_optimization (inside_init);
7294 /* Initialization of an array of chars from a string constant
7295 optionally enclosed in braces. */
7297 if (code == ARRAY_TYPE && inside_init
7298 && TREE_CODE (inside_init) == STRING_CST)
7300 tree typ1
7301 = (TYPE_ATOMIC (TREE_TYPE (type))
7302 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7303 TYPE_QUAL_ATOMIC)
7304 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7305 /* Note that an array could be both an array of character type
7306 and an array of wchar_t if wchar_t is signed char or unsigned
7307 char. */
7308 bool char_array = (typ1 == char_type_node
7309 || typ1 == signed_char_type_node
7310 || typ1 == unsigned_char_type_node);
7311 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7312 bool char16_array = !!comptypes (typ1, char16_type_node);
7313 bool char32_array = !!comptypes (typ1, char32_type_node);
7315 if (char_array || wchar_array || char16_array || char32_array)
7317 struct c_expr expr;
7318 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7319 expr.value = inside_init;
7320 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7321 expr.original_type = NULL;
7322 maybe_warn_string_init (init_loc, type, expr);
7324 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7325 pedwarn_init (init_loc, OPT_Wpedantic,
7326 "initialization of a flexible array member");
7328 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7329 TYPE_MAIN_VARIANT (type)))
7330 return inside_init;
7332 if (char_array)
7334 if (typ2 != char_type_node)
7336 error_init (init_loc, "char-array initialized from wide "
7337 "string");
7338 return error_mark_node;
7341 else
7343 if (typ2 == char_type_node)
7345 error_init (init_loc, "wide character array initialized "
7346 "from non-wide string");
7347 return error_mark_node;
7349 else if (!comptypes(typ1, typ2))
7351 error_init (init_loc, "wide character array initialized "
7352 "from incompatible wide string");
7353 return error_mark_node;
7357 TREE_TYPE (inside_init) = type;
7358 if (TYPE_DOMAIN (type) != NULL_TREE
7359 && TYPE_SIZE (type) != NULL_TREE
7360 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7362 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7364 /* Subtract the size of a single (possibly wide) character
7365 because it's ok to ignore the terminating null char
7366 that is counted in the length of the constant. */
7367 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7368 (len
7369 - (TYPE_PRECISION (typ1)
7370 / BITS_PER_UNIT))))
7371 pedwarn_init (init_loc, 0,
7372 ("initializer-string for array of chars "
7373 "is too long"));
7374 else if (warn_cxx_compat
7375 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7376 warning_at (init_loc, OPT_Wc___compat,
7377 ("initializer-string for array chars "
7378 "is too long for C++"));
7381 return inside_init;
7383 else if (INTEGRAL_TYPE_P (typ1))
7385 error_init (init_loc, "array of inappropriate type initialized "
7386 "from string constant");
7387 return error_mark_node;
7391 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7392 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7393 below and handle as a constructor. */
7394 if (code == VECTOR_TYPE
7395 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7396 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7397 && TREE_CONSTANT (inside_init))
7399 if (TREE_CODE (inside_init) == VECTOR_CST
7400 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7401 TYPE_MAIN_VARIANT (type)))
7402 return inside_init;
7404 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7406 unsigned HOST_WIDE_INT ix;
7407 tree value;
7408 bool constant_p = true;
7410 /* Iterate through elements and check if all constructor
7411 elements are *_CSTs. */
7412 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7413 if (!CONSTANT_CLASS_P (value))
7415 constant_p = false;
7416 break;
7419 if (constant_p)
7420 return build_vector_from_ctor (type,
7421 CONSTRUCTOR_ELTS (inside_init));
7425 if (warn_sequence_point)
7426 verify_sequence_points (inside_init);
7428 /* Any type can be initialized
7429 from an expression of the same type, optionally with braces. */
7431 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7432 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7433 TYPE_MAIN_VARIANT (type))
7434 || (code == ARRAY_TYPE
7435 && comptypes (TREE_TYPE (inside_init), type))
7436 || (code == VECTOR_TYPE
7437 && comptypes (TREE_TYPE (inside_init), type))
7438 || (code == POINTER_TYPE
7439 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7440 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7441 TREE_TYPE (type)))))
7443 if (code == POINTER_TYPE)
7445 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7447 if (TREE_CODE (inside_init) == STRING_CST
7448 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7449 inside_init = array_to_pointer_conversion
7450 (init_loc, inside_init);
7451 else
7453 error_init (init_loc, "invalid use of non-lvalue array");
7454 return error_mark_node;
7459 if (code == VECTOR_TYPE)
7460 /* Although the types are compatible, we may require a
7461 conversion. */
7462 inside_init = convert (type, inside_init);
7464 if (require_constant
7465 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7467 /* As an extension, allow initializing objects with static storage
7468 duration with compound literals (which are then treated just as
7469 the brace enclosed list they contain). Also allow this for
7470 vectors, as we can only assign them with compound literals. */
7471 if (flag_isoc99 && code != VECTOR_TYPE)
7472 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7473 "is not constant");
7474 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7475 inside_init = DECL_INITIAL (decl);
7478 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7479 && TREE_CODE (inside_init) != CONSTRUCTOR)
7481 error_init (init_loc, "array initialized from non-constant array "
7482 "expression");
7483 return error_mark_node;
7486 /* Compound expressions can only occur here if -Wpedantic or
7487 -pedantic-errors is specified. In the later case, we always want
7488 an error. In the former case, we simply want a warning. */
7489 if (require_constant && pedantic
7490 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7492 inside_init
7493 = valid_compound_expr_initializer (inside_init,
7494 TREE_TYPE (inside_init));
7495 if (inside_init == error_mark_node)
7496 error_init (init_loc, "initializer element is not constant");
7497 else
7498 pedwarn_init (init_loc, OPT_Wpedantic,
7499 "initializer element is not constant");
7500 if (flag_pedantic_errors)
7501 inside_init = error_mark_node;
7503 else if (require_constant
7504 && !initializer_constant_valid_p (inside_init,
7505 TREE_TYPE (inside_init)))
7507 error_init (init_loc, "initializer element is not constant");
7508 inside_init = error_mark_node;
7510 else if (require_constant && !maybe_const)
7511 pedwarn_init (init_loc, OPT_Wpedantic,
7512 "initializer element is not a constant expression");
7514 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7515 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7516 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7517 type, inside_init, origtype,
7518 ic_init, null_pointer_constant,
7519 NULL_TREE, NULL_TREE, 0);
7520 return inside_init;
7523 /* Handle scalar types, including conversions. */
7525 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7526 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7527 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7529 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7530 && (TREE_CODE (init) == STRING_CST
7531 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7532 inside_init = init = array_to_pointer_conversion (init_loc, init);
7533 if (semantic_type)
7534 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7535 inside_init);
7536 inside_init
7537 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7538 inside_init, origtype, ic_init,
7539 null_pointer_constant, NULL_TREE, NULL_TREE,
7542 /* Check to see if we have already given an error message. */
7543 if (inside_init == error_mark_node)
7545 else if (require_constant && !TREE_CONSTANT (inside_init))
7547 error_init (init_loc, "initializer element is not constant");
7548 inside_init = error_mark_node;
7550 else if (require_constant
7551 && !initializer_constant_valid_p (inside_init,
7552 TREE_TYPE (inside_init)))
7554 error_init (init_loc, "initializer element is not computable at "
7555 "load time");
7556 inside_init = error_mark_node;
7558 else if (require_constant && !maybe_const)
7559 pedwarn_init (init_loc, OPT_Wpedantic,
7560 "initializer element is not a constant expression");
7562 return inside_init;
7565 /* Come here only for records and arrays. */
7567 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7569 error_init (init_loc, "variable-sized object may not be initialized");
7570 return error_mark_node;
7573 error_init (init_loc, "invalid initializer");
7574 return error_mark_node;
7577 /* Handle initializers that use braces. */
7579 /* Type of object we are accumulating a constructor for.
7580 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7581 static tree constructor_type;
7583 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7584 left to fill. */
7585 static tree constructor_fields;
7587 /* For an ARRAY_TYPE, this is the specified index
7588 at which to store the next element we get. */
7589 static tree constructor_index;
7591 /* For an ARRAY_TYPE, this is the maximum index. */
7592 static tree constructor_max_index;
7594 /* For a RECORD_TYPE, this is the first field not yet written out. */
7595 static tree constructor_unfilled_fields;
7597 /* For an ARRAY_TYPE, this is the index of the first element
7598 not yet written out. */
7599 static tree constructor_unfilled_index;
7601 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7602 This is so we can generate gaps between fields, when appropriate. */
7603 static tree constructor_bit_index;
7605 /* If we are saving up the elements rather than allocating them,
7606 this is the list of elements so far (in reverse order,
7607 most recent first). */
7608 static vec<constructor_elt, va_gc> *constructor_elements;
7610 /* 1 if constructor should be incrementally stored into a constructor chain,
7611 0 if all the elements should be kept in AVL tree. */
7612 static int constructor_incremental;
7614 /* 1 if so far this constructor's elements are all compile-time constants. */
7615 static int constructor_constant;
7617 /* 1 if so far this constructor's elements are all valid address constants. */
7618 static int constructor_simple;
7620 /* 1 if this constructor has an element that cannot be part of a
7621 constant expression. */
7622 static int constructor_nonconst;
7624 /* 1 if this constructor is erroneous so far. */
7625 static int constructor_erroneous;
7627 /* 1 if this constructor is the universal zero initializer { 0 }. */
7628 static int constructor_zeroinit;
7630 /* Structure for managing pending initializer elements, organized as an
7631 AVL tree. */
7633 struct init_node
7635 struct init_node *left, *right;
7636 struct init_node *parent;
7637 int balance;
7638 tree purpose;
7639 tree value;
7640 tree origtype;
7643 /* Tree of pending elements at this constructor level.
7644 These are elements encountered out of order
7645 which belong at places we haven't reached yet in actually
7646 writing the output.
7647 Will never hold tree nodes across GC runs. */
7648 static struct init_node *constructor_pending_elts;
7650 /* The SPELLING_DEPTH of this constructor. */
7651 static int constructor_depth;
7653 /* DECL node for which an initializer is being read.
7654 0 means we are reading a constructor expression
7655 such as (struct foo) {...}. */
7656 static tree constructor_decl;
7658 /* Nonzero if this is an initializer for a top-level decl. */
7659 static int constructor_top_level;
7661 /* Nonzero if there were any member designators in this initializer. */
7662 static int constructor_designated;
7664 /* Nesting depth of designator list. */
7665 static int designator_depth;
7667 /* Nonzero if there were diagnosed errors in this designator list. */
7668 static int designator_erroneous;
7671 /* This stack has a level for each implicit or explicit level of
7672 structuring in the initializer, including the outermost one. It
7673 saves the values of most of the variables above. */
7675 struct constructor_range_stack;
7677 struct constructor_stack
7679 struct constructor_stack *next;
7680 tree type;
7681 tree fields;
7682 tree index;
7683 tree max_index;
7684 tree unfilled_index;
7685 tree unfilled_fields;
7686 tree bit_index;
7687 vec<constructor_elt, va_gc> *elements;
7688 struct init_node *pending_elts;
7689 int offset;
7690 int depth;
7691 /* If value nonzero, this value should replace the entire
7692 constructor at this level. */
7693 struct c_expr replacement_value;
7694 struct constructor_range_stack *range_stack;
7695 char constant;
7696 char simple;
7697 char nonconst;
7698 char implicit;
7699 char erroneous;
7700 char outer;
7701 char incremental;
7702 char designated;
7703 int designator_depth;
7706 static struct constructor_stack *constructor_stack;
7708 /* This stack represents designators from some range designator up to
7709 the last designator in the list. */
7711 struct constructor_range_stack
7713 struct constructor_range_stack *next, *prev;
7714 struct constructor_stack *stack;
7715 tree range_start;
7716 tree index;
7717 tree range_end;
7718 tree fields;
7721 static struct constructor_range_stack *constructor_range_stack;
7723 /* This stack records separate initializers that are nested.
7724 Nested initializers can't happen in ANSI C, but GNU C allows them
7725 in cases like { ... (struct foo) { ... } ... }. */
7727 struct initializer_stack
7729 struct initializer_stack *next;
7730 tree decl;
7731 struct constructor_stack *constructor_stack;
7732 struct constructor_range_stack *constructor_range_stack;
7733 vec<constructor_elt, va_gc> *elements;
7734 struct spelling *spelling;
7735 struct spelling *spelling_base;
7736 int spelling_size;
7737 char top_level;
7738 char require_constant_value;
7739 char require_constant_elements;
7740 rich_location *missing_brace_richloc;
7743 static struct initializer_stack *initializer_stack;
7745 /* Prepare to parse and output the initializer for variable DECL. */
7747 void
7748 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7749 rich_location *richloc)
7751 const char *locus;
7752 struct initializer_stack *p = XNEW (struct initializer_stack);
7754 p->decl = constructor_decl;
7755 p->require_constant_value = require_constant_value;
7756 p->require_constant_elements = require_constant_elements;
7757 p->constructor_stack = constructor_stack;
7758 p->constructor_range_stack = constructor_range_stack;
7759 p->elements = constructor_elements;
7760 p->spelling = spelling;
7761 p->spelling_base = spelling_base;
7762 p->spelling_size = spelling_size;
7763 p->top_level = constructor_top_level;
7764 p->next = initializer_stack;
7765 p->missing_brace_richloc = richloc;
7766 initializer_stack = p;
7768 constructor_decl = decl;
7769 constructor_designated = 0;
7770 constructor_top_level = top_level;
7772 if (decl != NULL_TREE && decl != error_mark_node)
7774 require_constant_value = TREE_STATIC (decl);
7775 require_constant_elements
7776 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7777 /* For a scalar, you can always use any value to initialize,
7778 even within braces. */
7779 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7780 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7782 else
7784 require_constant_value = 0;
7785 require_constant_elements = 0;
7786 locus = _("(anonymous)");
7789 constructor_stack = 0;
7790 constructor_range_stack = 0;
7792 found_missing_braces = 0;
7794 spelling_base = 0;
7795 spelling_size = 0;
7796 RESTORE_SPELLING_DEPTH (0);
7798 if (locus)
7799 push_string (locus);
7802 void
7803 finish_init (void)
7805 struct initializer_stack *p = initializer_stack;
7807 /* Free the whole constructor stack of this initializer. */
7808 while (constructor_stack)
7810 struct constructor_stack *q = constructor_stack;
7811 constructor_stack = q->next;
7812 free (q);
7815 gcc_assert (!constructor_range_stack);
7817 /* Pop back to the data of the outer initializer (if any). */
7818 free (spelling_base);
7820 constructor_decl = p->decl;
7821 require_constant_value = p->require_constant_value;
7822 require_constant_elements = p->require_constant_elements;
7823 constructor_stack = p->constructor_stack;
7824 constructor_range_stack = p->constructor_range_stack;
7825 constructor_elements = p->elements;
7826 spelling = p->spelling;
7827 spelling_base = p->spelling_base;
7828 spelling_size = p->spelling_size;
7829 constructor_top_level = p->top_level;
7830 initializer_stack = p->next;
7831 free (p);
7834 /* Call here when we see the initializer is surrounded by braces.
7835 This is instead of a call to push_init_level;
7836 it is matched by a call to pop_init_level.
7838 TYPE is the type to initialize, for a constructor expression.
7839 For an initializer for a decl, TYPE is zero. */
7841 void
7842 really_start_incremental_init (tree type)
7844 struct constructor_stack *p = XNEW (struct constructor_stack);
7846 if (type == NULL_TREE)
7847 type = TREE_TYPE (constructor_decl);
7849 if (VECTOR_TYPE_P (type)
7850 && TYPE_VECTOR_OPAQUE (type))
7851 error ("opaque vector types cannot be initialized");
7853 p->type = constructor_type;
7854 p->fields = constructor_fields;
7855 p->index = constructor_index;
7856 p->max_index = constructor_max_index;
7857 p->unfilled_index = constructor_unfilled_index;
7858 p->unfilled_fields = constructor_unfilled_fields;
7859 p->bit_index = constructor_bit_index;
7860 p->elements = constructor_elements;
7861 p->constant = constructor_constant;
7862 p->simple = constructor_simple;
7863 p->nonconst = constructor_nonconst;
7864 p->erroneous = constructor_erroneous;
7865 p->pending_elts = constructor_pending_elts;
7866 p->depth = constructor_depth;
7867 p->replacement_value.value = 0;
7868 p->replacement_value.original_code = ERROR_MARK;
7869 p->replacement_value.original_type = NULL;
7870 p->implicit = 0;
7871 p->range_stack = 0;
7872 p->outer = 0;
7873 p->incremental = constructor_incremental;
7874 p->designated = constructor_designated;
7875 p->designator_depth = designator_depth;
7876 p->next = 0;
7877 constructor_stack = p;
7879 constructor_constant = 1;
7880 constructor_simple = 1;
7881 constructor_nonconst = 0;
7882 constructor_depth = SPELLING_DEPTH ();
7883 constructor_elements = NULL;
7884 constructor_pending_elts = 0;
7885 constructor_type = type;
7886 constructor_incremental = 1;
7887 constructor_designated = 0;
7888 constructor_zeroinit = 1;
7889 designator_depth = 0;
7890 designator_erroneous = 0;
7892 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7894 constructor_fields = TYPE_FIELDS (constructor_type);
7895 /* Skip any nameless bit fields at the beginning. */
7896 while (constructor_fields != NULL_TREE
7897 && DECL_C_BIT_FIELD (constructor_fields)
7898 && DECL_NAME (constructor_fields) == NULL_TREE)
7899 constructor_fields = DECL_CHAIN (constructor_fields);
7901 constructor_unfilled_fields = constructor_fields;
7902 constructor_bit_index = bitsize_zero_node;
7904 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7906 if (TYPE_DOMAIN (constructor_type))
7908 constructor_max_index
7909 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7911 /* Detect non-empty initializations of zero-length arrays. */
7912 if (constructor_max_index == NULL_TREE
7913 && TYPE_SIZE (constructor_type))
7914 constructor_max_index = integer_minus_one_node;
7916 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7917 to initialize VLAs will cause a proper error; avoid tree
7918 checking errors as well by setting a safe value. */
7919 if (constructor_max_index
7920 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7921 constructor_max_index = integer_minus_one_node;
7923 constructor_index
7924 = convert (bitsizetype,
7925 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7927 else
7929 constructor_index = bitsize_zero_node;
7930 constructor_max_index = NULL_TREE;
7933 constructor_unfilled_index = constructor_index;
7935 else if (VECTOR_TYPE_P (constructor_type))
7937 /* Vectors are like simple fixed-size arrays. */
7938 constructor_max_index =
7939 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7940 constructor_index = bitsize_zero_node;
7941 constructor_unfilled_index = constructor_index;
7943 else
7945 /* Handle the case of int x = {5}; */
7946 constructor_fields = constructor_type;
7947 constructor_unfilled_fields = constructor_type;
7951 extern location_t last_init_list_comma;
7953 /* Called when we see an open brace for a nested initializer. Finish
7954 off any pending levels with implicit braces. */
7955 void
7956 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7958 while (constructor_stack->implicit)
7960 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7961 && constructor_fields == NULL_TREE)
7962 process_init_element (input_location,
7963 pop_init_level (loc, 1, braced_init_obstack,
7964 last_init_list_comma),
7965 true, braced_init_obstack);
7966 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7967 && constructor_max_index
7968 && tree_int_cst_lt (constructor_max_index,
7969 constructor_index))
7970 process_init_element (input_location,
7971 pop_init_level (loc, 1, braced_init_obstack,
7972 last_init_list_comma),
7973 true, braced_init_obstack);
7974 else
7975 break;
7979 /* Push down into a subobject, for initialization.
7980 If this is for an explicit set of braces, IMPLICIT is 0.
7981 If it is because the next element belongs at a lower level,
7982 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7984 void
7985 push_init_level (location_t loc, int implicit,
7986 struct obstack *braced_init_obstack)
7988 struct constructor_stack *p;
7989 tree value = NULL_TREE;
7991 /* Unless this is an explicit brace, we need to preserve previous
7992 content if any. */
7993 if (implicit)
7995 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7996 value = find_init_member (constructor_fields, braced_init_obstack);
7997 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7998 value = find_init_member (constructor_index, braced_init_obstack);
8001 p = XNEW (struct constructor_stack);
8002 p->type = constructor_type;
8003 p->fields = constructor_fields;
8004 p->index = constructor_index;
8005 p->max_index = constructor_max_index;
8006 p->unfilled_index = constructor_unfilled_index;
8007 p->unfilled_fields = constructor_unfilled_fields;
8008 p->bit_index = constructor_bit_index;
8009 p->elements = constructor_elements;
8010 p->constant = constructor_constant;
8011 p->simple = constructor_simple;
8012 p->nonconst = constructor_nonconst;
8013 p->erroneous = constructor_erroneous;
8014 p->pending_elts = constructor_pending_elts;
8015 p->depth = constructor_depth;
8016 p->replacement_value.value = NULL_TREE;
8017 p->replacement_value.original_code = ERROR_MARK;
8018 p->replacement_value.original_type = NULL;
8019 p->implicit = implicit;
8020 p->outer = 0;
8021 p->incremental = constructor_incremental;
8022 p->designated = constructor_designated;
8023 p->designator_depth = designator_depth;
8024 p->next = constructor_stack;
8025 p->range_stack = 0;
8026 constructor_stack = p;
8028 constructor_constant = 1;
8029 constructor_simple = 1;
8030 constructor_nonconst = 0;
8031 constructor_depth = SPELLING_DEPTH ();
8032 constructor_elements = NULL;
8033 constructor_incremental = 1;
8034 constructor_designated = 0;
8035 constructor_pending_elts = 0;
8036 if (!implicit)
8038 p->range_stack = constructor_range_stack;
8039 constructor_range_stack = 0;
8040 designator_depth = 0;
8041 designator_erroneous = 0;
8044 /* Don't die if an entire brace-pair level is superfluous
8045 in the containing level. */
8046 if (constructor_type == NULL_TREE)
8048 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8050 /* Don't die if there are extra init elts at the end. */
8051 if (constructor_fields == NULL_TREE)
8052 constructor_type = NULL_TREE;
8053 else
8055 constructor_type = TREE_TYPE (constructor_fields);
8056 push_member_name (constructor_fields);
8057 constructor_depth++;
8059 /* If upper initializer is designated, then mark this as
8060 designated too to prevent bogus warnings. */
8061 constructor_designated = p->designated;
8063 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8065 constructor_type = TREE_TYPE (constructor_type);
8066 push_array_bounds (tree_to_uhwi (constructor_index));
8067 constructor_depth++;
8070 if (constructor_type == NULL_TREE)
8072 error_init (loc, "extra brace group at end of initializer");
8073 constructor_fields = NULL_TREE;
8074 constructor_unfilled_fields = NULL_TREE;
8075 return;
8078 if (value && TREE_CODE (value) == CONSTRUCTOR)
8080 constructor_constant = TREE_CONSTANT (value);
8081 constructor_simple = TREE_STATIC (value);
8082 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8083 constructor_elements = CONSTRUCTOR_ELTS (value);
8084 if (!vec_safe_is_empty (constructor_elements)
8085 && (TREE_CODE (constructor_type) == RECORD_TYPE
8086 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8087 set_nonincremental_init (braced_init_obstack);
8090 if (implicit == 1)
8092 found_missing_braces = 1;
8093 if (initializer_stack->missing_brace_richloc)
8094 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8095 (loc, "{");
8098 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8100 constructor_fields = TYPE_FIELDS (constructor_type);
8101 /* Skip any nameless bit fields at the beginning. */
8102 while (constructor_fields != NULL_TREE
8103 && DECL_C_BIT_FIELD (constructor_fields)
8104 && DECL_NAME (constructor_fields) == NULL_TREE)
8105 constructor_fields = DECL_CHAIN (constructor_fields);
8107 constructor_unfilled_fields = constructor_fields;
8108 constructor_bit_index = bitsize_zero_node;
8110 else if (VECTOR_TYPE_P (constructor_type))
8112 /* Vectors are like simple fixed-size arrays. */
8113 constructor_max_index =
8114 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8115 constructor_index = bitsize_int (0);
8116 constructor_unfilled_index = constructor_index;
8118 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8120 if (TYPE_DOMAIN (constructor_type))
8122 constructor_max_index
8123 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8125 /* Detect non-empty initializations of zero-length arrays. */
8126 if (constructor_max_index == NULL_TREE
8127 && TYPE_SIZE (constructor_type))
8128 constructor_max_index = integer_minus_one_node;
8130 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8131 to initialize VLAs will cause a proper error; avoid tree
8132 checking errors as well by setting a safe value. */
8133 if (constructor_max_index
8134 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8135 constructor_max_index = integer_minus_one_node;
8137 constructor_index
8138 = convert (bitsizetype,
8139 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8141 else
8142 constructor_index = bitsize_zero_node;
8144 constructor_unfilled_index = constructor_index;
8145 if (value && TREE_CODE (value) == STRING_CST)
8147 /* We need to split the char/wchar array into individual
8148 characters, so that we don't have to special case it
8149 everywhere. */
8150 set_nonincremental_init_from_string (value, braced_init_obstack);
8153 else
8155 if (constructor_type != error_mark_node)
8156 warning_init (input_location, 0, "braces around scalar initializer");
8157 constructor_fields = constructor_type;
8158 constructor_unfilled_fields = constructor_type;
8162 /* At the end of an implicit or explicit brace level,
8163 finish up that level of constructor. If a single expression
8164 with redundant braces initialized that level, return the
8165 c_expr structure for that expression. Otherwise, the original_code
8166 element is set to ERROR_MARK.
8167 If we were outputting the elements as they are read, return 0 as the value
8168 from inner levels (process_init_element ignores that),
8169 but return error_mark_node as the value from the outermost level
8170 (that's what we want to put in DECL_INITIAL).
8171 Otherwise, return a CONSTRUCTOR expression as the value. */
8173 struct c_expr
8174 pop_init_level (location_t loc, int implicit,
8175 struct obstack *braced_init_obstack,
8176 location_t insert_before)
8178 struct constructor_stack *p;
8179 struct c_expr ret;
8180 ret.value = NULL_TREE;
8181 ret.original_code = ERROR_MARK;
8182 ret.original_type = NULL;
8184 if (implicit == 0)
8186 /* When we come to an explicit close brace,
8187 pop any inner levels that didn't have explicit braces. */
8188 while (constructor_stack->implicit)
8189 process_init_element (input_location,
8190 pop_init_level (loc, 1, braced_init_obstack,
8191 insert_before),
8192 true, braced_init_obstack);
8193 gcc_assert (!constructor_range_stack);
8195 else
8196 if (initializer_stack->missing_brace_richloc)
8197 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8198 (insert_before, "}");
8200 /* Now output all pending elements. */
8201 constructor_incremental = 1;
8202 output_pending_init_elements (1, braced_init_obstack);
8204 p = constructor_stack;
8206 /* Error for initializing a flexible array member, or a zero-length
8207 array member in an inappropriate context. */
8208 if (constructor_type && constructor_fields
8209 && TREE_CODE (constructor_type) == ARRAY_TYPE
8210 && TYPE_DOMAIN (constructor_type)
8211 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8213 /* Silently discard empty initializations. The parser will
8214 already have pedwarned for empty brackets. */
8215 if (integer_zerop (constructor_unfilled_index))
8216 constructor_type = NULL_TREE;
8217 else
8219 gcc_assert (!TYPE_SIZE (constructor_type));
8221 if (constructor_depth > 2)
8222 error_init (loc, "initialization of flexible array member in a nested context");
8223 else
8224 pedwarn_init (loc, OPT_Wpedantic,
8225 "initialization of a flexible array member");
8227 /* We have already issued an error message for the existence
8228 of a flexible array member not at the end of the structure.
8229 Discard the initializer so that we do not die later. */
8230 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8231 constructor_type = NULL_TREE;
8235 switch (vec_safe_length (constructor_elements))
8237 case 0:
8238 /* Initialization with { } counts as zeroinit. */
8239 constructor_zeroinit = 1;
8240 break;
8241 case 1:
8242 /* This might be zeroinit as well. */
8243 if (integer_zerop ((*constructor_elements)[0].value))
8244 constructor_zeroinit = 1;
8245 break;
8246 default:
8247 /* If the constructor has more than one element, it can't be { 0 }. */
8248 constructor_zeroinit = 0;
8249 break;
8252 /* Warn when some structs are initialized with direct aggregation. */
8253 if (!implicit && found_missing_braces && warn_missing_braces
8254 && !constructor_zeroinit)
8256 gcc_assert (initializer_stack->missing_brace_richloc);
8257 warning_at (initializer_stack->missing_brace_richloc,
8258 OPT_Wmissing_braces,
8259 "missing braces around initializer");
8262 /* Warn when some struct elements are implicitly initialized to zero. */
8263 if (warn_missing_field_initializers
8264 && constructor_type
8265 && TREE_CODE (constructor_type) == RECORD_TYPE
8266 && constructor_unfilled_fields)
8268 /* Do not warn for flexible array members or zero-length arrays. */
8269 while (constructor_unfilled_fields
8270 && (!DECL_SIZE (constructor_unfilled_fields)
8271 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8272 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8274 if (constructor_unfilled_fields
8275 /* Do not warn if this level of the initializer uses member
8276 designators; it is likely to be deliberate. */
8277 && !constructor_designated
8278 /* Do not warn about initializing with { 0 } or with { }. */
8279 && !constructor_zeroinit)
8281 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8282 "missing initializer for field %qD of %qT",
8283 constructor_unfilled_fields,
8284 constructor_type))
8285 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8286 "%qD declared here", constructor_unfilled_fields);
8290 /* Pad out the end of the structure. */
8291 if (p->replacement_value.value)
8292 /* If this closes a superfluous brace pair,
8293 just pass out the element between them. */
8294 ret = p->replacement_value;
8295 else if (constructor_type == NULL_TREE)
8297 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8298 && TREE_CODE (constructor_type) != ARRAY_TYPE
8299 && !VECTOR_TYPE_P (constructor_type))
8301 /* A nonincremental scalar initializer--just return
8302 the element, after verifying there is just one. */
8303 if (vec_safe_is_empty (constructor_elements))
8305 if (!constructor_erroneous)
8306 error_init (loc, "empty scalar initializer");
8307 ret.value = error_mark_node;
8309 else if (vec_safe_length (constructor_elements) != 1)
8311 error_init (loc, "extra elements in scalar initializer");
8312 ret.value = (*constructor_elements)[0].value;
8314 else
8315 ret.value = (*constructor_elements)[0].value;
8317 else
8319 if (constructor_erroneous)
8320 ret.value = error_mark_node;
8321 else
8323 ret.value = build_constructor (constructor_type,
8324 constructor_elements);
8325 if (constructor_constant)
8326 TREE_CONSTANT (ret.value) = 1;
8327 if (constructor_constant && constructor_simple)
8328 TREE_STATIC (ret.value) = 1;
8329 if (constructor_nonconst)
8330 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8334 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8336 if (constructor_nonconst)
8337 ret.original_code = C_MAYBE_CONST_EXPR;
8338 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8339 ret.original_code = ERROR_MARK;
8342 constructor_type = p->type;
8343 constructor_fields = p->fields;
8344 constructor_index = p->index;
8345 constructor_max_index = p->max_index;
8346 constructor_unfilled_index = p->unfilled_index;
8347 constructor_unfilled_fields = p->unfilled_fields;
8348 constructor_bit_index = p->bit_index;
8349 constructor_elements = p->elements;
8350 constructor_constant = p->constant;
8351 constructor_simple = p->simple;
8352 constructor_nonconst = p->nonconst;
8353 constructor_erroneous = p->erroneous;
8354 constructor_incremental = p->incremental;
8355 constructor_designated = p->designated;
8356 designator_depth = p->designator_depth;
8357 constructor_pending_elts = p->pending_elts;
8358 constructor_depth = p->depth;
8359 if (!p->implicit)
8360 constructor_range_stack = p->range_stack;
8361 RESTORE_SPELLING_DEPTH (constructor_depth);
8363 constructor_stack = p->next;
8364 free (p);
8366 if (ret.value == NULL_TREE && constructor_stack == 0)
8367 ret.value = error_mark_node;
8368 return ret;
8371 /* Common handling for both array range and field name designators.
8372 ARRAY argument is nonzero for array ranges. Returns false for success. */
8374 static bool
8375 set_designator (location_t loc, bool array,
8376 struct obstack *braced_init_obstack)
8378 tree subtype;
8379 enum tree_code subcode;
8381 /* Don't die if an entire brace-pair level is superfluous
8382 in the containing level. */
8383 if (constructor_type == NULL_TREE)
8384 return true;
8386 /* If there were errors in this designator list already, bail out
8387 silently. */
8388 if (designator_erroneous)
8389 return true;
8391 if (!designator_depth)
8393 gcc_assert (!constructor_range_stack);
8395 /* Designator list starts at the level of closest explicit
8396 braces. */
8397 while (constructor_stack->implicit)
8398 process_init_element (input_location,
8399 pop_init_level (loc, 1, braced_init_obstack,
8400 last_init_list_comma),
8401 true, braced_init_obstack);
8402 constructor_designated = 1;
8403 return false;
8406 switch (TREE_CODE (constructor_type))
8408 case RECORD_TYPE:
8409 case UNION_TYPE:
8410 subtype = TREE_TYPE (constructor_fields);
8411 if (subtype != error_mark_node)
8412 subtype = TYPE_MAIN_VARIANT (subtype);
8413 break;
8414 case ARRAY_TYPE:
8415 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8416 break;
8417 default:
8418 gcc_unreachable ();
8421 subcode = TREE_CODE (subtype);
8422 if (array && subcode != ARRAY_TYPE)
8424 error_init (loc, "array index in non-array initializer");
8425 return true;
8427 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8429 error_init (loc, "field name not in record or union initializer");
8430 return true;
8433 constructor_designated = 1;
8434 finish_implicit_inits (loc, braced_init_obstack);
8435 push_init_level (loc, 2, braced_init_obstack);
8436 return false;
8439 /* If there are range designators in designator list, push a new designator
8440 to constructor_range_stack. RANGE_END is end of such stack range or
8441 NULL_TREE if there is no range designator at this level. */
8443 static void
8444 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8446 struct constructor_range_stack *p;
8448 p = (struct constructor_range_stack *)
8449 obstack_alloc (braced_init_obstack,
8450 sizeof (struct constructor_range_stack));
8451 p->prev = constructor_range_stack;
8452 p->next = 0;
8453 p->fields = constructor_fields;
8454 p->range_start = constructor_index;
8455 p->index = constructor_index;
8456 p->stack = constructor_stack;
8457 p->range_end = range_end;
8458 if (constructor_range_stack)
8459 constructor_range_stack->next = p;
8460 constructor_range_stack = p;
8463 /* Within an array initializer, specify the next index to be initialized.
8464 FIRST is that index. If LAST is nonzero, then initialize a range
8465 of indices, running from FIRST through LAST. */
8467 void
8468 set_init_index (location_t loc, tree first, tree last,
8469 struct obstack *braced_init_obstack)
8471 if (set_designator (loc, true, braced_init_obstack))
8472 return;
8474 designator_erroneous = 1;
8476 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8477 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8479 error_init (loc, "array index in initializer not of integer type");
8480 return;
8483 if (TREE_CODE (first) != INTEGER_CST)
8485 first = c_fully_fold (first, false, NULL);
8486 if (TREE_CODE (first) == INTEGER_CST)
8487 pedwarn_init (loc, OPT_Wpedantic,
8488 "array index in initializer is not "
8489 "an integer constant expression");
8492 if (last && TREE_CODE (last) != INTEGER_CST)
8494 last = c_fully_fold (last, false, NULL);
8495 if (TREE_CODE (last) == INTEGER_CST)
8496 pedwarn_init (loc, OPT_Wpedantic,
8497 "array index in initializer is not "
8498 "an integer constant expression");
8501 if (TREE_CODE (first) != INTEGER_CST)
8502 error_init (loc, "nonconstant array index in initializer");
8503 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8504 error_init (loc, "nonconstant array index in initializer");
8505 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8506 error_init (loc, "array index in non-array initializer");
8507 else if (tree_int_cst_sgn (first) == -1)
8508 error_init (loc, "array index in initializer exceeds array bounds");
8509 else if (constructor_max_index
8510 && tree_int_cst_lt (constructor_max_index, first))
8511 error_init (loc, "array index in initializer exceeds array bounds");
8512 else
8514 constant_expression_warning (first);
8515 if (last)
8516 constant_expression_warning (last);
8517 constructor_index = convert (bitsizetype, first);
8518 if (tree_int_cst_lt (constructor_index, first))
8520 constructor_index = copy_node (constructor_index);
8521 TREE_OVERFLOW (constructor_index) = 1;
8524 if (last)
8526 if (tree_int_cst_equal (first, last))
8527 last = NULL_TREE;
8528 else if (tree_int_cst_lt (last, first))
8530 error_init (loc, "empty index range in initializer");
8531 last = NULL_TREE;
8533 else
8535 last = convert (bitsizetype, last);
8536 if (constructor_max_index != NULL_TREE
8537 && tree_int_cst_lt (constructor_max_index, last))
8539 error_init (loc, "array index range in initializer exceeds "
8540 "array bounds");
8541 last = NULL_TREE;
8546 designator_depth++;
8547 designator_erroneous = 0;
8548 if (constructor_range_stack || last)
8549 push_range_stack (last, braced_init_obstack);
8553 /* Within a struct initializer, specify the next field to be initialized. */
8555 void
8556 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8557 struct obstack *braced_init_obstack)
8559 tree field;
8561 if (set_designator (loc, false, braced_init_obstack))
8562 return;
8564 designator_erroneous = 1;
8566 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8568 error_init (loc, "field name not in record or union initializer");
8569 return;
8572 field = lookup_field (constructor_type, fieldname);
8574 if (field == NULL_TREE)
8576 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8577 if (guessed_id)
8579 gcc_rich_location rich_loc (fieldname_loc);
8580 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8581 error_at (&rich_loc,
8582 "%qT has no member named %qE; did you mean %qE?",
8583 constructor_type, fieldname, guessed_id);
8585 else
8586 error_at (fieldname_loc, "%qT has no member named %qE",
8587 constructor_type, fieldname);
8589 else
8592 constructor_fields = TREE_VALUE (field);
8593 designator_depth++;
8594 designator_erroneous = 0;
8595 if (constructor_range_stack)
8596 push_range_stack (NULL_TREE, braced_init_obstack);
8597 field = TREE_CHAIN (field);
8598 if (field)
8600 if (set_designator (loc, false, braced_init_obstack))
8601 return;
8604 while (field != NULL_TREE);
8607 /* Add a new initializer to the tree of pending initializers. PURPOSE
8608 identifies the initializer, either array index or field in a structure.
8609 VALUE is the value of that index or field. If ORIGTYPE is not
8610 NULL_TREE, it is the original type of VALUE.
8612 IMPLICIT is true if value comes from pop_init_level (1),
8613 the new initializer has been merged with the existing one
8614 and thus no warnings should be emitted about overriding an
8615 existing initializer. */
8617 static void
8618 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8619 bool implicit, struct obstack *braced_init_obstack)
8621 struct init_node *p, **q, *r;
8623 q = &constructor_pending_elts;
8624 p = 0;
8626 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8628 while (*q != 0)
8630 p = *q;
8631 if (tree_int_cst_lt (purpose, p->purpose))
8632 q = &p->left;
8633 else if (tree_int_cst_lt (p->purpose, purpose))
8634 q = &p->right;
8635 else
8637 if (!implicit)
8639 if (TREE_SIDE_EFFECTS (p->value))
8640 warning_init (loc, OPT_Woverride_init_side_effects,
8641 "initialized field with side-effects "
8642 "overwritten");
8643 else if (warn_override_init)
8644 warning_init (loc, OPT_Woverride_init,
8645 "initialized field overwritten");
8647 p->value = value;
8648 p->origtype = origtype;
8649 return;
8653 else
8655 tree bitpos;
8657 bitpos = bit_position (purpose);
8658 while (*q != NULL)
8660 p = *q;
8661 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8662 q = &p->left;
8663 else if (p->purpose != purpose)
8664 q = &p->right;
8665 else
8667 if (!implicit)
8669 if (TREE_SIDE_EFFECTS (p->value))
8670 warning_init (loc, OPT_Woverride_init_side_effects,
8671 "initialized field with side-effects "
8672 "overwritten");
8673 else if (warn_override_init)
8674 warning_init (loc, OPT_Woverride_init,
8675 "initialized field overwritten");
8677 p->value = value;
8678 p->origtype = origtype;
8679 return;
8684 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8685 sizeof (struct init_node));
8686 r->purpose = purpose;
8687 r->value = value;
8688 r->origtype = origtype;
8690 *q = r;
8691 r->parent = p;
8692 r->left = 0;
8693 r->right = 0;
8694 r->balance = 0;
8696 while (p)
8698 struct init_node *s;
8700 if (r == p->left)
8702 if (p->balance == 0)
8703 p->balance = -1;
8704 else if (p->balance < 0)
8706 if (r->balance < 0)
8708 /* L rotation. */
8709 p->left = r->right;
8710 if (p->left)
8711 p->left->parent = p;
8712 r->right = p;
8714 p->balance = 0;
8715 r->balance = 0;
8717 s = p->parent;
8718 p->parent = r;
8719 r->parent = s;
8720 if (s)
8722 if (s->left == p)
8723 s->left = r;
8724 else
8725 s->right = r;
8727 else
8728 constructor_pending_elts = r;
8730 else
8732 /* LR rotation. */
8733 struct init_node *t = r->right;
8735 r->right = t->left;
8736 if (r->right)
8737 r->right->parent = r;
8738 t->left = r;
8740 p->left = t->right;
8741 if (p->left)
8742 p->left->parent = p;
8743 t->right = p;
8745 p->balance = t->balance < 0;
8746 r->balance = -(t->balance > 0);
8747 t->balance = 0;
8749 s = p->parent;
8750 p->parent = t;
8751 r->parent = t;
8752 t->parent = s;
8753 if (s)
8755 if (s->left == p)
8756 s->left = t;
8757 else
8758 s->right = t;
8760 else
8761 constructor_pending_elts = t;
8763 break;
8765 else
8767 /* p->balance == +1; growth of left side balances the node. */
8768 p->balance = 0;
8769 break;
8772 else /* r == p->right */
8774 if (p->balance == 0)
8775 /* Growth propagation from right side. */
8776 p->balance++;
8777 else if (p->balance > 0)
8779 if (r->balance > 0)
8781 /* R rotation. */
8782 p->right = r->left;
8783 if (p->right)
8784 p->right->parent = p;
8785 r->left = p;
8787 p->balance = 0;
8788 r->balance = 0;
8790 s = p->parent;
8791 p->parent = r;
8792 r->parent = s;
8793 if (s)
8795 if (s->left == p)
8796 s->left = r;
8797 else
8798 s->right = r;
8800 else
8801 constructor_pending_elts = r;
8803 else /* r->balance == -1 */
8805 /* RL rotation */
8806 struct init_node *t = r->left;
8808 r->left = t->right;
8809 if (r->left)
8810 r->left->parent = r;
8811 t->right = r;
8813 p->right = t->left;
8814 if (p->right)
8815 p->right->parent = p;
8816 t->left = p;
8818 r->balance = (t->balance < 0);
8819 p->balance = -(t->balance > 0);
8820 t->balance = 0;
8822 s = p->parent;
8823 p->parent = t;
8824 r->parent = t;
8825 t->parent = s;
8826 if (s)
8828 if (s->left == p)
8829 s->left = t;
8830 else
8831 s->right = t;
8833 else
8834 constructor_pending_elts = t;
8836 break;
8838 else
8840 /* p->balance == -1; growth of right side balances the node. */
8841 p->balance = 0;
8842 break;
8846 r = p;
8847 p = p->parent;
8851 /* Build AVL tree from a sorted chain. */
8853 static void
8854 set_nonincremental_init (struct obstack * braced_init_obstack)
8856 unsigned HOST_WIDE_INT ix;
8857 tree index, value;
8859 if (TREE_CODE (constructor_type) != RECORD_TYPE
8860 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8861 return;
8863 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8864 add_pending_init (input_location, index, value, NULL_TREE, true,
8865 braced_init_obstack);
8866 constructor_elements = NULL;
8867 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8869 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8870 /* Skip any nameless bit fields at the beginning. */
8871 while (constructor_unfilled_fields != NULL_TREE
8872 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8873 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
8874 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8877 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8879 if (TYPE_DOMAIN (constructor_type))
8880 constructor_unfilled_index
8881 = convert (bitsizetype,
8882 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8883 else
8884 constructor_unfilled_index = bitsize_zero_node;
8886 constructor_incremental = 0;
8889 /* Build AVL tree from a string constant. */
8891 static void
8892 set_nonincremental_init_from_string (tree str,
8893 struct obstack * braced_init_obstack)
8895 tree value, purpose, type;
8896 HOST_WIDE_INT val[2];
8897 const char *p, *end;
8898 int byte, wchar_bytes, charwidth, bitpos;
8900 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8902 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8903 charwidth = TYPE_PRECISION (char_type_node);
8904 gcc_assert ((size_t) wchar_bytes * charwidth
8905 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8906 type = TREE_TYPE (constructor_type);
8907 p = TREE_STRING_POINTER (str);
8908 end = p + TREE_STRING_LENGTH (str);
8910 for (purpose = bitsize_zero_node;
8911 p < end
8912 && !(constructor_max_index
8913 && tree_int_cst_lt (constructor_max_index, purpose));
8914 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8916 if (wchar_bytes == 1)
8918 val[0] = (unsigned char) *p++;
8919 val[1] = 0;
8921 else
8923 val[1] = 0;
8924 val[0] = 0;
8925 for (byte = 0; byte < wchar_bytes; byte++)
8927 if (BYTES_BIG_ENDIAN)
8928 bitpos = (wchar_bytes - byte - 1) * charwidth;
8929 else
8930 bitpos = byte * charwidth;
8931 val[bitpos / HOST_BITS_PER_WIDE_INT]
8932 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8933 << (bitpos % HOST_BITS_PER_WIDE_INT);
8937 if (!TYPE_UNSIGNED (type))
8939 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8940 if (bitpos < HOST_BITS_PER_WIDE_INT)
8942 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8944 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8945 val[1] = -1;
8948 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8950 if (val[0] < 0)
8951 val[1] = -1;
8953 else if (val[1] & (HOST_WIDE_INT_1
8954 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8955 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8958 value = wide_int_to_tree (type,
8959 wide_int::from_array (val, 2,
8960 HOST_BITS_PER_WIDE_INT * 2));
8961 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8962 braced_init_obstack);
8965 constructor_incremental = 0;
8968 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
8969 not initialized yet. */
8971 static tree
8972 find_init_member (tree field, struct obstack * braced_init_obstack)
8974 struct init_node *p;
8976 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8978 if (constructor_incremental
8979 && tree_int_cst_lt (field, constructor_unfilled_index))
8980 set_nonincremental_init (braced_init_obstack);
8982 p = constructor_pending_elts;
8983 while (p)
8985 if (tree_int_cst_lt (field, p->purpose))
8986 p = p->left;
8987 else if (tree_int_cst_lt (p->purpose, field))
8988 p = p->right;
8989 else
8990 return p->value;
8993 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8995 tree bitpos = bit_position (field);
8997 if (constructor_incremental
8998 && (!constructor_unfilled_fields
8999 || tree_int_cst_lt (bitpos,
9000 bit_position (constructor_unfilled_fields))))
9001 set_nonincremental_init (braced_init_obstack);
9003 p = constructor_pending_elts;
9004 while (p)
9006 if (field == p->purpose)
9007 return p->value;
9008 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9009 p = p->left;
9010 else
9011 p = p->right;
9014 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9016 if (!vec_safe_is_empty (constructor_elements)
9017 && (constructor_elements->last ().index == field))
9018 return constructor_elements->last ().value;
9020 return NULL_TREE;
9023 /* "Output" the next constructor element.
9024 At top level, really output it to assembler code now.
9025 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9026 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9027 TYPE is the data type that the containing data type wants here.
9028 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9029 If VALUE is a string constant, STRICT_STRING is true if it is
9030 unparenthesized or we should not warn here for it being parenthesized.
9031 For other types of VALUE, STRICT_STRING is not used.
9033 PENDING if true means output pending elements that belong
9034 right after this element. (PENDING is normally true;
9035 it is false while outputting pending elements, to avoid recursion.)
9037 IMPLICIT is true if value comes from pop_init_level (1),
9038 the new initializer has been merged with the existing one
9039 and thus no warnings should be emitted about overriding an
9040 existing initializer. */
9042 static void
9043 output_init_element (location_t loc, tree value, tree origtype,
9044 bool strict_string, tree type, tree field, bool pending,
9045 bool implicit, struct obstack * braced_init_obstack)
9047 tree semantic_type = NULL_TREE;
9048 bool maybe_const = true;
9049 bool npc;
9051 if (type == error_mark_node || value == error_mark_node)
9053 constructor_erroneous = 1;
9054 return;
9056 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9057 && (TREE_CODE (value) == STRING_CST
9058 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9059 && !(TREE_CODE (value) == STRING_CST
9060 && TREE_CODE (type) == ARRAY_TYPE
9061 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9062 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9063 TYPE_MAIN_VARIANT (type)))
9064 value = array_to_pointer_conversion (input_location, value);
9066 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9067 && require_constant_value && pending)
9069 /* As an extension, allow initializing objects with static storage
9070 duration with compound literals (which are then treated just as
9071 the brace enclosed list they contain). */
9072 if (flag_isoc99)
9073 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9074 "constant");
9075 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9076 value = DECL_INITIAL (decl);
9079 npc = null_pointer_constant_p (value);
9080 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9082 semantic_type = TREE_TYPE (value);
9083 value = TREE_OPERAND (value, 0);
9085 value = c_fully_fold (value, require_constant_value, &maybe_const);
9087 if (value == error_mark_node)
9088 constructor_erroneous = 1;
9089 else if (!TREE_CONSTANT (value))
9090 constructor_constant = 0;
9091 else if (!initializer_constant_valid_p (value,
9092 TREE_TYPE (value),
9093 AGGREGATE_TYPE_P (constructor_type)
9094 && TYPE_REVERSE_STORAGE_ORDER
9095 (constructor_type))
9096 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9097 && DECL_C_BIT_FIELD (field)
9098 && TREE_CODE (value) != INTEGER_CST))
9099 constructor_simple = 0;
9100 if (!maybe_const)
9101 constructor_nonconst = 1;
9103 /* Digest the initializer and issue any errors about incompatible
9104 types before issuing errors about non-constant initializers. */
9105 tree new_value = value;
9106 if (semantic_type)
9107 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9108 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9109 require_constant_value);
9110 if (new_value == error_mark_node)
9112 constructor_erroneous = 1;
9113 return;
9115 if (require_constant_value || require_constant_elements)
9116 constant_expression_warning (new_value);
9118 /* Proceed to check the constness of the original initializer. */
9119 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9121 if (require_constant_value)
9123 error_init (loc, "initializer element is not constant");
9124 value = error_mark_node;
9126 else if (require_constant_elements)
9127 pedwarn (loc, OPT_Wpedantic,
9128 "initializer element is not computable at load time");
9130 else if (!maybe_const
9131 && (require_constant_value || require_constant_elements))
9132 pedwarn_init (loc, OPT_Wpedantic,
9133 "initializer element is not a constant expression");
9135 /* Issue -Wc++-compat warnings about initializing a bitfield with
9136 enum type. */
9137 if (warn_cxx_compat
9138 && field != NULL_TREE
9139 && TREE_CODE (field) == FIELD_DECL
9140 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9141 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9142 != TYPE_MAIN_VARIANT (type))
9143 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9145 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9146 if (checktype != error_mark_node
9147 && (TYPE_MAIN_VARIANT (checktype)
9148 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9149 warning_init (loc, OPT_Wc___compat,
9150 "enum conversion in initialization is invalid in C++");
9153 /* If this field is empty (and not at the end of structure),
9154 don't do anything other than checking the initializer. */
9155 if (field
9156 && (TREE_TYPE (field) == error_mark_node
9157 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9158 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9159 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9160 || DECL_CHAIN (field)))))
9161 return;
9163 /* Finally, set VALUE to the initializer value digested above. */
9164 value = new_value;
9166 /* If this element doesn't come next in sequence,
9167 put it on constructor_pending_elts. */
9168 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9169 && (!constructor_incremental
9170 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9172 if (constructor_incremental
9173 && tree_int_cst_lt (field, constructor_unfilled_index))
9174 set_nonincremental_init (braced_init_obstack);
9176 add_pending_init (loc, field, value, origtype, implicit,
9177 braced_init_obstack);
9178 return;
9180 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9181 && (!constructor_incremental
9182 || field != constructor_unfilled_fields))
9184 /* We do this for records but not for unions. In a union,
9185 no matter which field is specified, it can be initialized
9186 right away since it starts at the beginning of the union. */
9187 if (constructor_incremental)
9189 if (!constructor_unfilled_fields)
9190 set_nonincremental_init (braced_init_obstack);
9191 else
9193 tree bitpos, unfillpos;
9195 bitpos = bit_position (field);
9196 unfillpos = bit_position (constructor_unfilled_fields);
9198 if (tree_int_cst_lt (bitpos, unfillpos))
9199 set_nonincremental_init (braced_init_obstack);
9203 add_pending_init (loc, field, value, origtype, implicit,
9204 braced_init_obstack);
9205 return;
9207 else if (TREE_CODE (constructor_type) == UNION_TYPE
9208 && !vec_safe_is_empty (constructor_elements))
9210 if (!implicit)
9212 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9213 warning_init (loc, OPT_Woverride_init_side_effects,
9214 "initialized field with side-effects overwritten");
9215 else if (warn_override_init)
9216 warning_init (loc, OPT_Woverride_init,
9217 "initialized field overwritten");
9220 /* We can have just one union field set. */
9221 constructor_elements = NULL;
9224 /* Otherwise, output this element either to
9225 constructor_elements or to the assembler file. */
9227 constructor_elt celt = {field, value};
9228 vec_safe_push (constructor_elements, celt);
9230 /* Advance the variable that indicates sequential elements output. */
9231 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9232 constructor_unfilled_index
9233 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9234 bitsize_one_node);
9235 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9237 constructor_unfilled_fields
9238 = DECL_CHAIN (constructor_unfilled_fields);
9240 /* Skip any nameless bit fields. */
9241 while (constructor_unfilled_fields != NULL_TREE
9242 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9243 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
9244 constructor_unfilled_fields =
9245 DECL_CHAIN (constructor_unfilled_fields);
9247 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9248 constructor_unfilled_fields = NULL_TREE;
9250 /* Now output any pending elements which have become next. */
9251 if (pending)
9252 output_pending_init_elements (0, braced_init_obstack);
9255 /* Output any pending elements which have become next.
9256 As we output elements, constructor_unfilled_{fields,index}
9257 advances, which may cause other elements to become next;
9258 if so, they too are output.
9260 If ALL is 0, we return when there are
9261 no more pending elements to output now.
9263 If ALL is 1, we output space as necessary so that
9264 we can output all the pending elements. */
9265 static void
9266 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9268 struct init_node *elt = constructor_pending_elts;
9269 tree next;
9271 retry:
9273 /* Look through the whole pending tree.
9274 If we find an element that should be output now,
9275 output it. Otherwise, set NEXT to the element
9276 that comes first among those still pending. */
9278 next = NULL_TREE;
9279 while (elt)
9281 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9283 if (tree_int_cst_equal (elt->purpose,
9284 constructor_unfilled_index))
9285 output_init_element (input_location, elt->value, elt->origtype,
9286 true, TREE_TYPE (constructor_type),
9287 constructor_unfilled_index, false, false,
9288 braced_init_obstack);
9289 else if (tree_int_cst_lt (constructor_unfilled_index,
9290 elt->purpose))
9292 /* Advance to the next smaller node. */
9293 if (elt->left)
9294 elt = elt->left;
9295 else
9297 /* We have reached the smallest node bigger than the
9298 current unfilled index. Fill the space first. */
9299 next = elt->purpose;
9300 break;
9303 else
9305 /* Advance to the next bigger node. */
9306 if (elt->right)
9307 elt = elt->right;
9308 else
9310 /* We have reached the biggest node in a subtree. Find
9311 the parent of it, which is the next bigger node. */
9312 while (elt->parent && elt->parent->right == elt)
9313 elt = elt->parent;
9314 elt = elt->parent;
9315 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9316 elt->purpose))
9318 next = elt->purpose;
9319 break;
9324 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9326 tree ctor_unfilled_bitpos, elt_bitpos;
9328 /* If the current record is complete we are done. */
9329 if (constructor_unfilled_fields == NULL_TREE)
9330 break;
9332 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9333 elt_bitpos = bit_position (elt->purpose);
9334 /* We can't compare fields here because there might be empty
9335 fields in between. */
9336 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9338 constructor_unfilled_fields = elt->purpose;
9339 output_init_element (input_location, elt->value, elt->origtype,
9340 true, TREE_TYPE (elt->purpose),
9341 elt->purpose, false, false,
9342 braced_init_obstack);
9344 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9346 /* Advance to the next smaller node. */
9347 if (elt->left)
9348 elt = elt->left;
9349 else
9351 /* We have reached the smallest node bigger than the
9352 current unfilled field. Fill the space first. */
9353 next = elt->purpose;
9354 break;
9357 else
9359 /* Advance to the next bigger node. */
9360 if (elt->right)
9361 elt = elt->right;
9362 else
9364 /* We have reached the biggest node in a subtree. Find
9365 the parent of it, which is the next bigger node. */
9366 while (elt->parent && elt->parent->right == elt)
9367 elt = elt->parent;
9368 elt = elt->parent;
9369 if (elt
9370 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9371 bit_position (elt->purpose))))
9373 next = elt->purpose;
9374 break;
9381 /* Ordinarily return, but not if we want to output all
9382 and there are elements left. */
9383 if (!(all && next != NULL_TREE))
9384 return;
9386 /* If it's not incremental, just skip over the gap, so that after
9387 jumping to retry we will output the next successive element. */
9388 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9389 constructor_unfilled_fields = next;
9390 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9391 constructor_unfilled_index = next;
9393 /* ELT now points to the node in the pending tree with the next
9394 initializer to output. */
9395 goto retry;
9398 /* Add one non-braced element to the current constructor level.
9399 This adjusts the current position within the constructor's type.
9400 This may also start or terminate implicit levels
9401 to handle a partly-braced initializer.
9403 Once this has found the correct level for the new element,
9404 it calls output_init_element.
9406 IMPLICIT is true if value comes from pop_init_level (1),
9407 the new initializer has been merged with the existing one
9408 and thus no warnings should be emitted about overriding an
9409 existing initializer. */
9411 void
9412 process_init_element (location_t loc, struct c_expr value, bool implicit,
9413 struct obstack * braced_init_obstack)
9415 tree orig_value = value.value;
9416 int string_flag
9417 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9418 bool strict_string = value.original_code == STRING_CST;
9419 bool was_designated = designator_depth != 0;
9421 designator_depth = 0;
9422 designator_erroneous = 0;
9424 if (!implicit && value.value && !integer_zerop (value.value))
9425 constructor_zeroinit = 0;
9427 /* Handle superfluous braces around string cst as in
9428 char x[] = {"foo"}; */
9429 if (string_flag
9430 && constructor_type
9431 && !was_designated
9432 && TREE_CODE (constructor_type) == ARRAY_TYPE
9433 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9434 && integer_zerop (constructor_unfilled_index))
9436 if (constructor_stack->replacement_value.value)
9437 error_init (loc, "excess elements in char array initializer");
9438 constructor_stack->replacement_value = value;
9439 return;
9442 if (constructor_stack->replacement_value.value != NULL_TREE)
9444 error_init (loc, "excess elements in struct initializer");
9445 return;
9448 /* Ignore elements of a brace group if it is entirely superfluous
9449 and has already been diagnosed. */
9450 if (constructor_type == NULL_TREE)
9451 return;
9453 if (!implicit && warn_designated_init && !was_designated
9454 && TREE_CODE (constructor_type) == RECORD_TYPE
9455 && lookup_attribute ("designated_init",
9456 TYPE_ATTRIBUTES (constructor_type)))
9457 warning_init (loc,
9458 OPT_Wdesignated_init,
9459 "positional initialization of field "
9460 "in %<struct%> declared with %<designated_init%> attribute");
9462 /* If we've exhausted any levels that didn't have braces,
9463 pop them now. */
9464 while (constructor_stack->implicit)
9466 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9467 && constructor_fields == NULL_TREE)
9468 process_init_element (loc,
9469 pop_init_level (loc, 1, braced_init_obstack,
9470 last_init_list_comma),
9471 true, braced_init_obstack);
9472 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9473 || VECTOR_TYPE_P (constructor_type))
9474 && constructor_max_index
9475 && tree_int_cst_lt (constructor_max_index,
9476 constructor_index))
9477 process_init_element (loc,
9478 pop_init_level (loc, 1, braced_init_obstack,
9479 last_init_list_comma),
9480 true, braced_init_obstack);
9481 else
9482 break;
9485 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9486 if (constructor_range_stack)
9488 /* If value is a compound literal and we'll be just using its
9489 content, don't put it into a SAVE_EXPR. */
9490 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9491 || !require_constant_value)
9493 tree semantic_type = NULL_TREE;
9494 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9496 semantic_type = TREE_TYPE (value.value);
9497 value.value = TREE_OPERAND (value.value, 0);
9499 value.value = save_expr (value.value);
9500 if (semantic_type)
9501 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9502 value.value);
9506 while (1)
9508 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9510 tree fieldtype;
9511 enum tree_code fieldcode;
9513 if (constructor_fields == NULL_TREE)
9515 pedwarn_init (loc, 0, "excess elements in struct initializer");
9516 break;
9519 fieldtype = TREE_TYPE (constructor_fields);
9520 if (fieldtype != error_mark_node)
9521 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9522 fieldcode = TREE_CODE (fieldtype);
9524 /* Error for non-static initialization of a flexible array member. */
9525 if (fieldcode == ARRAY_TYPE
9526 && !require_constant_value
9527 && TYPE_SIZE (fieldtype) == NULL_TREE
9528 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9530 error_init (loc, "non-static initialization of a flexible "
9531 "array member");
9532 break;
9535 /* Error for initialization of a flexible array member with
9536 a string constant if the structure is in an array. E.g.:
9537 struct S { int x; char y[]; };
9538 struct S s[] = { { 1, "foo" } };
9539 is invalid. */
9540 if (string_flag
9541 && fieldcode == ARRAY_TYPE
9542 && constructor_depth > 1
9543 && TYPE_SIZE (fieldtype) == NULL_TREE
9544 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9546 bool in_array_p = false;
9547 for (struct constructor_stack *p = constructor_stack;
9548 p && p->type; p = p->next)
9549 if (TREE_CODE (p->type) == ARRAY_TYPE)
9551 in_array_p = true;
9552 break;
9554 if (in_array_p)
9556 error_init (loc, "initialization of flexible array "
9557 "member in a nested context");
9558 break;
9562 /* Accept a string constant to initialize a subarray. */
9563 if (value.value != NULL_TREE
9564 && fieldcode == ARRAY_TYPE
9565 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9566 && string_flag)
9567 value.value = orig_value;
9568 /* Otherwise, if we have come to a subaggregate,
9569 and we don't have an element of its type, push into it. */
9570 else if (value.value != NULL_TREE
9571 && value.value != error_mark_node
9572 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9573 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9574 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9576 push_init_level (loc, 1, braced_init_obstack);
9577 continue;
9580 if (value.value)
9582 push_member_name (constructor_fields);
9583 output_init_element (loc, value.value, value.original_type,
9584 strict_string, fieldtype,
9585 constructor_fields, true, implicit,
9586 braced_init_obstack);
9587 RESTORE_SPELLING_DEPTH (constructor_depth);
9589 else
9590 /* Do the bookkeeping for an element that was
9591 directly output as a constructor. */
9593 /* For a record, keep track of end position of last field. */
9594 if (DECL_SIZE (constructor_fields))
9595 constructor_bit_index
9596 = size_binop_loc (input_location, PLUS_EXPR,
9597 bit_position (constructor_fields),
9598 DECL_SIZE (constructor_fields));
9600 /* If the current field was the first one not yet written out,
9601 it isn't now, so update. */
9602 if (constructor_unfilled_fields == constructor_fields)
9604 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9605 /* Skip any nameless bit fields. */
9606 while (constructor_unfilled_fields != 0
9607 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9608 && DECL_NAME (constructor_unfilled_fields) == 0)
9609 constructor_unfilled_fields =
9610 DECL_CHAIN (constructor_unfilled_fields);
9614 constructor_fields = DECL_CHAIN (constructor_fields);
9615 /* Skip any nameless bit fields at the beginning. */
9616 while (constructor_fields != NULL_TREE
9617 && DECL_C_BIT_FIELD (constructor_fields)
9618 && DECL_NAME (constructor_fields) == NULL_TREE)
9619 constructor_fields = DECL_CHAIN (constructor_fields);
9621 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9623 tree fieldtype;
9624 enum tree_code fieldcode;
9626 if (constructor_fields == NULL_TREE)
9628 pedwarn_init (loc, 0,
9629 "excess elements in union initializer");
9630 break;
9633 fieldtype = TREE_TYPE (constructor_fields);
9634 if (fieldtype != error_mark_node)
9635 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9636 fieldcode = TREE_CODE (fieldtype);
9638 /* Warn that traditional C rejects initialization of unions.
9639 We skip the warning if the value is zero. This is done
9640 under the assumption that the zero initializer in user
9641 code appears conditioned on e.g. __STDC__ to avoid
9642 "missing initializer" warnings and relies on default
9643 initialization to zero in the traditional C case.
9644 We also skip the warning if the initializer is designated,
9645 again on the assumption that this must be conditional on
9646 __STDC__ anyway (and we've already complained about the
9647 member-designator already). */
9648 if (!in_system_header_at (input_location) && !constructor_designated
9649 && !(value.value && (integer_zerop (value.value)
9650 || real_zerop (value.value))))
9651 warning (OPT_Wtraditional, "traditional C rejects initialization "
9652 "of unions");
9654 /* Accept a string constant to initialize a subarray. */
9655 if (value.value != NULL_TREE
9656 && fieldcode == ARRAY_TYPE
9657 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9658 && string_flag)
9659 value.value = orig_value;
9660 /* Otherwise, if we have come to a subaggregate,
9661 and we don't have an element of its type, push into it. */
9662 else if (value.value != NULL_TREE
9663 && value.value != error_mark_node
9664 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9665 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9666 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9668 push_init_level (loc, 1, braced_init_obstack);
9669 continue;
9672 if (value.value)
9674 push_member_name (constructor_fields);
9675 output_init_element (loc, value.value, value.original_type,
9676 strict_string, fieldtype,
9677 constructor_fields, true, implicit,
9678 braced_init_obstack);
9679 RESTORE_SPELLING_DEPTH (constructor_depth);
9681 else
9682 /* Do the bookkeeping for an element that was
9683 directly output as a constructor. */
9685 constructor_bit_index = DECL_SIZE (constructor_fields);
9686 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9689 constructor_fields = NULL_TREE;
9691 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9693 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9694 enum tree_code eltcode = TREE_CODE (elttype);
9696 /* Accept a string constant to initialize a subarray. */
9697 if (value.value != NULL_TREE
9698 && eltcode == ARRAY_TYPE
9699 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9700 && string_flag)
9701 value.value = orig_value;
9702 /* Otherwise, if we have come to a subaggregate,
9703 and we don't have an element of its type, push into it. */
9704 else if (value.value != NULL_TREE
9705 && value.value != error_mark_node
9706 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9707 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9708 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9710 push_init_level (loc, 1, braced_init_obstack);
9711 continue;
9714 if (constructor_max_index != NULL_TREE
9715 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9716 || integer_all_onesp (constructor_max_index)))
9718 pedwarn_init (loc, 0,
9719 "excess elements in array initializer");
9720 break;
9723 /* Now output the actual element. */
9724 if (value.value)
9726 push_array_bounds (tree_to_uhwi (constructor_index));
9727 output_init_element (loc, value.value, value.original_type,
9728 strict_string, elttype,
9729 constructor_index, true, implicit,
9730 braced_init_obstack);
9731 RESTORE_SPELLING_DEPTH (constructor_depth);
9734 constructor_index
9735 = size_binop_loc (input_location, PLUS_EXPR,
9736 constructor_index, bitsize_one_node);
9738 if (!value.value)
9739 /* If we are doing the bookkeeping for an element that was
9740 directly output as a constructor, we must update
9741 constructor_unfilled_index. */
9742 constructor_unfilled_index = constructor_index;
9744 else if (VECTOR_TYPE_P (constructor_type))
9746 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9748 /* Do a basic check of initializer size. Note that vectors
9749 always have a fixed size derived from their type. */
9750 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9752 pedwarn_init (loc, 0,
9753 "excess elements in vector initializer");
9754 break;
9757 /* Now output the actual element. */
9758 if (value.value)
9760 if (TREE_CODE (value.value) == VECTOR_CST)
9761 elttype = TYPE_MAIN_VARIANT (constructor_type);
9762 output_init_element (loc, value.value, value.original_type,
9763 strict_string, elttype,
9764 constructor_index, true, implicit,
9765 braced_init_obstack);
9768 constructor_index
9769 = size_binop_loc (input_location,
9770 PLUS_EXPR, constructor_index, bitsize_one_node);
9772 if (!value.value)
9773 /* If we are doing the bookkeeping for an element that was
9774 directly output as a constructor, we must update
9775 constructor_unfilled_index. */
9776 constructor_unfilled_index = constructor_index;
9779 /* Handle the sole element allowed in a braced initializer
9780 for a scalar variable. */
9781 else if (constructor_type != error_mark_node
9782 && constructor_fields == NULL_TREE)
9784 pedwarn_init (loc, 0,
9785 "excess elements in scalar initializer");
9786 break;
9788 else
9790 if (value.value)
9791 output_init_element (loc, value.value, value.original_type,
9792 strict_string, constructor_type,
9793 NULL_TREE, true, implicit,
9794 braced_init_obstack);
9795 constructor_fields = NULL_TREE;
9798 /* Handle range initializers either at this level or anywhere higher
9799 in the designator stack. */
9800 if (constructor_range_stack)
9802 struct constructor_range_stack *p, *range_stack;
9803 int finish = 0;
9805 range_stack = constructor_range_stack;
9806 constructor_range_stack = 0;
9807 while (constructor_stack != range_stack->stack)
9809 gcc_assert (constructor_stack->implicit);
9810 process_init_element (loc,
9811 pop_init_level (loc, 1,
9812 braced_init_obstack,
9813 last_init_list_comma),
9814 true, braced_init_obstack);
9816 for (p = range_stack;
9817 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9818 p = p->prev)
9820 gcc_assert (constructor_stack->implicit);
9821 process_init_element (loc,
9822 pop_init_level (loc, 1,
9823 braced_init_obstack,
9824 last_init_list_comma),
9825 true, braced_init_obstack);
9828 p->index = size_binop_loc (input_location,
9829 PLUS_EXPR, p->index, bitsize_one_node);
9830 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9831 finish = 1;
9833 while (1)
9835 constructor_index = p->index;
9836 constructor_fields = p->fields;
9837 if (finish && p->range_end && p->index == p->range_start)
9839 finish = 0;
9840 p->prev = 0;
9842 p = p->next;
9843 if (!p)
9844 break;
9845 finish_implicit_inits (loc, braced_init_obstack);
9846 push_init_level (loc, 2, braced_init_obstack);
9847 p->stack = constructor_stack;
9848 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9849 p->index = p->range_start;
9852 if (!finish)
9853 constructor_range_stack = range_stack;
9854 continue;
9857 break;
9860 constructor_range_stack = 0;
9863 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9864 (guaranteed to be 'volatile' or null) and ARGS (represented using
9865 an ASM_EXPR node). */
9866 tree
9867 build_asm_stmt (tree cv_qualifier, tree args)
9869 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9870 ASM_VOLATILE_P (args) = 1;
9871 return add_stmt (args);
9874 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9875 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9876 SIMPLE indicates whether there was anything at all after the
9877 string in the asm expression -- asm("blah") and asm("blah" : )
9878 are subtly different. We use a ASM_EXPR node to represent this. */
9879 tree
9880 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9881 tree clobbers, tree labels, bool simple)
9883 tree tail;
9884 tree args;
9885 int i;
9886 const char *constraint;
9887 const char **oconstraints;
9888 bool allows_mem, allows_reg, is_inout;
9889 int ninputs, noutputs;
9891 ninputs = list_length (inputs);
9892 noutputs = list_length (outputs);
9893 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9895 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9897 /* Remove output conversions that change the type but not the mode. */
9898 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9900 tree output = TREE_VALUE (tail);
9902 output = c_fully_fold (output, false, NULL);
9904 /* ??? Really, this should not be here. Users should be using a
9905 proper lvalue, dammit. But there's a long history of using casts
9906 in the output operands. In cases like longlong.h, this becomes a
9907 primitive form of typechecking -- if the cast can be removed, then
9908 the output operand had a type of the proper width; otherwise we'll
9909 get an error. Gross, but ... */
9910 STRIP_NOPS (output);
9912 if (!lvalue_or_else (loc, output, lv_asm))
9913 output = error_mark_node;
9915 if (output != error_mark_node
9916 && (TREE_READONLY (output)
9917 || TYPE_READONLY (TREE_TYPE (output))
9918 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9919 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9920 readonly_error (loc, output, lv_asm);
9922 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9923 oconstraints[i] = constraint;
9925 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9926 &allows_mem, &allows_reg, &is_inout))
9928 /* If the operand is going to end up in memory,
9929 mark it addressable. */
9930 if (!allows_reg && !c_mark_addressable (output))
9931 output = error_mark_node;
9932 if (!(!allows_reg && allows_mem)
9933 && output != error_mark_node
9934 && VOID_TYPE_P (TREE_TYPE (output)))
9936 error_at (loc, "invalid use of void expression");
9937 output = error_mark_node;
9940 else
9941 output = error_mark_node;
9943 TREE_VALUE (tail) = output;
9946 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9948 tree input;
9950 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9951 input = TREE_VALUE (tail);
9953 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9954 oconstraints, &allows_mem, &allows_reg))
9956 /* If the operand is going to end up in memory,
9957 mark it addressable. */
9958 if (!allows_reg && allows_mem)
9960 input = c_fully_fold (input, false, NULL);
9962 /* Strip the nops as we allow this case. FIXME, this really
9963 should be rejected or made deprecated. */
9964 STRIP_NOPS (input);
9965 if (!c_mark_addressable (input))
9966 input = error_mark_node;
9968 else
9970 struct c_expr expr;
9971 memset (&expr, 0, sizeof (expr));
9972 expr.value = input;
9973 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9974 input = c_fully_fold (expr.value, false, NULL);
9976 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9978 error_at (loc, "invalid use of void expression");
9979 input = error_mark_node;
9983 else
9984 input = error_mark_node;
9986 TREE_VALUE (tail) = input;
9989 /* ASMs with labels cannot have outputs. This should have been
9990 enforced by the parser. */
9991 gcc_assert (outputs == NULL || labels == NULL);
9993 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9995 /* asm statements without outputs, including simple ones, are treated
9996 as volatile. */
9997 ASM_INPUT_P (args) = simple;
9998 ASM_VOLATILE_P (args) = (noutputs == 0);
10000 return args;
10003 /* Generate a goto statement to LABEL. LOC is the location of the
10004 GOTO. */
10006 tree
10007 c_finish_goto_label (location_t loc, tree label)
10009 tree decl = lookup_label_for_goto (loc, label);
10010 if (!decl)
10011 return NULL_TREE;
10012 TREE_USED (decl) = 1;
10014 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10015 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10016 SET_EXPR_LOCATION (t, loc);
10017 return add_stmt (t);
10021 /* Generate a computed goto statement to EXPR. LOC is the location of
10022 the GOTO. */
10024 tree
10025 c_finish_goto_ptr (location_t loc, tree expr)
10027 tree t;
10028 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10029 expr = c_fully_fold (expr, false, NULL);
10030 expr = convert (ptr_type_node, expr);
10031 t = build1 (GOTO_EXPR, void_type_node, expr);
10032 SET_EXPR_LOCATION (t, loc);
10033 return add_stmt (t);
10036 /* Generate a C `return' statement. RETVAL is the expression for what
10037 to return, or a null pointer for `return;' with no value. LOC is
10038 the location of the return statement, or the location of the expression,
10039 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10040 is the original type of RETVAL. */
10042 tree
10043 c_finish_return (location_t loc, tree retval, tree origtype)
10045 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10046 bool no_warning = false;
10047 bool npc = false;
10048 size_t rank = 0;
10050 /* Use the expansion point to handle cases such as returning NULL
10051 in a function returning void. */
10052 source_location xloc = expansion_point_location_if_in_system_header (loc);
10054 if (TREE_THIS_VOLATILE (current_function_decl))
10055 warning_at (xloc, 0,
10056 "function declared %<noreturn%> has a %<return%> statement");
10058 if (flag_cilkplus && contains_array_notation_expr (retval))
10060 /* Array notations are allowed in a return statement if it is inside a
10061 built-in array notation reduction function. */
10062 if (!find_rank (loc, retval, retval, false, &rank))
10063 return error_mark_node;
10064 if (rank >= 1)
10066 error_at (loc, "array notation expression cannot be used as a "
10067 "return value");
10068 return error_mark_node;
10071 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
10073 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
10074 "allowed");
10075 return error_mark_node;
10077 if (retval)
10079 tree semantic_type = NULL_TREE;
10080 npc = null_pointer_constant_p (retval);
10081 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10083 semantic_type = TREE_TYPE (retval);
10084 retval = TREE_OPERAND (retval, 0);
10086 retval = c_fully_fold (retval, false, NULL);
10087 if (semantic_type)
10088 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10091 if (!retval)
10093 current_function_returns_null = 1;
10094 if ((warn_return_type || flag_isoc99)
10095 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10097 bool warned_here;
10098 if (flag_isoc99)
10099 warned_here = pedwarn
10100 (loc, 0,
10101 "%<return%> with no value, in function returning non-void");
10102 else
10103 warned_here = warning_at
10104 (loc, OPT_Wreturn_type,
10105 "%<return%> with no value, in function returning non-void");
10106 no_warning = true;
10107 if (warned_here)
10108 inform (DECL_SOURCE_LOCATION (current_function_decl),
10109 "declared here");
10112 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10114 current_function_returns_null = 1;
10115 bool warned_here;
10116 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10117 warned_here = pedwarn
10118 (xloc, 0,
10119 "%<return%> with a value, in function returning void");
10120 else
10121 warned_here = pedwarn
10122 (xloc, OPT_Wpedantic, "ISO C forbids "
10123 "%<return%> with expression, in function returning void");
10124 if (warned_here)
10125 inform (DECL_SOURCE_LOCATION (current_function_decl),
10126 "declared here");
10128 else
10130 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10131 retval, origtype, ic_return,
10132 npc, NULL_TREE, NULL_TREE, 0);
10133 tree res = DECL_RESULT (current_function_decl);
10134 tree inner;
10135 bool save;
10137 current_function_returns_value = 1;
10138 if (t == error_mark_node)
10139 return NULL_TREE;
10141 save = in_late_binary_op;
10142 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10143 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10144 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10145 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10146 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10147 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10148 in_late_binary_op = true;
10149 inner = t = convert (TREE_TYPE (res), t);
10150 in_late_binary_op = save;
10152 /* Strip any conversions, additions, and subtractions, and see if
10153 we are returning the address of a local variable. Warn if so. */
10154 while (1)
10156 switch (TREE_CODE (inner))
10158 CASE_CONVERT:
10159 case NON_LVALUE_EXPR:
10160 case PLUS_EXPR:
10161 case POINTER_PLUS_EXPR:
10162 inner = TREE_OPERAND (inner, 0);
10163 continue;
10165 case MINUS_EXPR:
10166 /* If the second operand of the MINUS_EXPR has a pointer
10167 type (or is converted from it), this may be valid, so
10168 don't give a warning. */
10170 tree op1 = TREE_OPERAND (inner, 1);
10172 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10173 && (CONVERT_EXPR_P (op1)
10174 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10175 op1 = TREE_OPERAND (op1, 0);
10177 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10178 break;
10180 inner = TREE_OPERAND (inner, 0);
10181 continue;
10184 case ADDR_EXPR:
10185 inner = TREE_OPERAND (inner, 0);
10187 while (REFERENCE_CLASS_P (inner)
10188 && !INDIRECT_REF_P (inner))
10189 inner = TREE_OPERAND (inner, 0);
10191 if (DECL_P (inner)
10192 && !DECL_EXTERNAL (inner)
10193 && !TREE_STATIC (inner)
10194 && DECL_CONTEXT (inner) == current_function_decl)
10196 if (TREE_CODE (inner) == LABEL_DECL)
10197 warning_at (loc, OPT_Wreturn_local_addr,
10198 "function returns address of label");
10199 else
10201 warning_at (loc, OPT_Wreturn_local_addr,
10202 "function returns address of local variable");
10203 tree zero = build_zero_cst (TREE_TYPE (res));
10204 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10207 break;
10209 default:
10210 break;
10213 break;
10216 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10217 SET_EXPR_LOCATION (retval, loc);
10219 if (warn_sequence_point)
10220 verify_sequence_points (retval);
10223 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10224 TREE_NO_WARNING (ret_stmt) |= no_warning;
10225 return add_stmt (ret_stmt);
10228 struct c_switch {
10229 /* The SWITCH_EXPR being built. */
10230 tree switch_expr;
10232 /* The original type of the testing expression, i.e. before the
10233 default conversion is applied. */
10234 tree orig_type;
10236 /* A splay-tree mapping the low element of a case range to the high
10237 element, or NULL_TREE if there is no high element. Used to
10238 determine whether or not a new case label duplicates an old case
10239 label. We need a tree, rather than simply a hash table, because
10240 of the GNU case range extension. */
10241 splay_tree cases;
10243 /* The bindings at the point of the switch. This is used for
10244 warnings crossing decls when branching to a case label. */
10245 struct c_spot_bindings *bindings;
10247 /* The next node on the stack. */
10248 struct c_switch *next;
10250 /* Remember whether the controlling expression had boolean type
10251 before integer promotions for the sake of -Wswitch-bool. */
10252 bool bool_cond_p;
10254 /* Remember whether there was a case value that is outside the
10255 range of the ORIG_TYPE. */
10256 bool outside_range_p;
10259 /* A stack of the currently active switch statements. The innermost
10260 switch statement is on the top of the stack. There is no need to
10261 mark the stack for garbage collection because it is only active
10262 during the processing of the body of a function, and we never
10263 collect at that point. */
10265 struct c_switch *c_switch_stack;
10267 /* Start a C switch statement, testing expression EXP. Return the new
10268 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10269 SWITCH_COND_LOC is the location of the switch's condition.
10270 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10272 tree
10273 c_start_case (location_t switch_loc,
10274 location_t switch_cond_loc,
10275 tree exp, bool explicit_cast_p)
10277 tree orig_type = error_mark_node;
10278 bool bool_cond_p = false;
10279 struct c_switch *cs;
10281 if (exp != error_mark_node)
10283 orig_type = TREE_TYPE (exp);
10285 if (!INTEGRAL_TYPE_P (orig_type))
10287 if (orig_type != error_mark_node)
10289 error_at (switch_cond_loc, "switch quantity not an integer");
10290 orig_type = error_mark_node;
10292 exp = integer_zero_node;
10294 else
10296 tree type = TYPE_MAIN_VARIANT (orig_type);
10297 tree e = exp;
10299 /* Warn if the condition has boolean value. */
10300 while (TREE_CODE (e) == COMPOUND_EXPR)
10301 e = TREE_OPERAND (e, 1);
10303 if ((TREE_CODE (type) == BOOLEAN_TYPE
10304 || truth_value_p (TREE_CODE (e)))
10305 /* Explicit cast to int suppresses this warning. */
10306 && !(TREE_CODE (type) == INTEGER_TYPE
10307 && explicit_cast_p))
10308 bool_cond_p = true;
10310 if (!in_system_header_at (input_location)
10311 && (type == long_integer_type_node
10312 || type == long_unsigned_type_node))
10313 warning_at (switch_cond_loc,
10314 OPT_Wtraditional, "%<long%> switch expression not "
10315 "converted to %<int%> in ISO C");
10317 exp = c_fully_fold (exp, false, NULL);
10318 exp = default_conversion (exp);
10320 if (warn_sequence_point)
10321 verify_sequence_points (exp);
10325 /* Add this new SWITCH_EXPR to the stack. */
10326 cs = XNEW (struct c_switch);
10327 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10328 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10329 cs->orig_type = orig_type;
10330 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10331 cs->bindings = c_get_switch_bindings ();
10332 cs->bool_cond_p = bool_cond_p;
10333 cs->outside_range_p = false;
10334 cs->next = c_switch_stack;
10335 c_switch_stack = cs;
10337 return add_stmt (cs->switch_expr);
10340 /* Process a case label at location LOC. */
10342 tree
10343 do_case (location_t loc, tree low_value, tree high_value)
10345 tree label = NULL_TREE;
10347 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10349 low_value = c_fully_fold (low_value, false, NULL);
10350 if (TREE_CODE (low_value) == INTEGER_CST)
10351 pedwarn (loc, OPT_Wpedantic,
10352 "case label is not an integer constant expression");
10355 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10357 high_value = c_fully_fold (high_value, false, NULL);
10358 if (TREE_CODE (high_value) == INTEGER_CST)
10359 pedwarn (input_location, OPT_Wpedantic,
10360 "case label is not an integer constant expression");
10363 if (c_switch_stack == NULL)
10365 if (low_value)
10366 error_at (loc, "case label not within a switch statement");
10367 else
10368 error_at (loc, "%<default%> label not within a switch statement");
10369 return NULL_TREE;
10372 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10373 EXPR_LOCATION (c_switch_stack->switch_expr),
10374 loc))
10375 return NULL_TREE;
10377 label = c_add_case_label (loc, c_switch_stack->cases,
10378 SWITCH_COND (c_switch_stack->switch_expr),
10379 c_switch_stack->orig_type,
10380 low_value, high_value,
10381 &c_switch_stack->outside_range_p);
10382 if (label == error_mark_node)
10383 label = NULL_TREE;
10384 return label;
10387 /* Finish the switch statement. TYPE is the original type of the
10388 controlling expression of the switch, or NULL_TREE. */
10390 void
10391 c_finish_case (tree body, tree type)
10393 struct c_switch *cs = c_switch_stack;
10394 location_t switch_location;
10396 SWITCH_BODY (cs->switch_expr) = body;
10398 /* Emit warnings as needed. */
10399 switch_location = EXPR_LOCATION (cs->switch_expr);
10400 c_do_switch_warnings (cs->cases, switch_location,
10401 type ? type : TREE_TYPE (cs->switch_expr),
10402 SWITCH_COND (cs->switch_expr),
10403 cs->bool_cond_p, cs->outside_range_p);
10405 /* Pop the stack. */
10406 c_switch_stack = cs->next;
10407 splay_tree_delete (cs->cases);
10408 c_release_switch_bindings (cs->bindings);
10409 XDELETE (cs);
10412 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10413 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10414 may be null. */
10416 void
10417 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10418 tree else_block)
10420 tree stmt;
10422 /* If the condition has array notations, then the rank of the then_block and
10423 else_block must be either 0 or be equal to the rank of the condition. If
10424 the condition does not have array notations then break them up as it is
10425 broken up in a normal expression. */
10426 if (flag_cilkplus && contains_array_notation_expr (cond))
10428 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10429 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10430 return;
10431 if (then_block
10432 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10433 return;
10434 if (else_block
10435 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10436 return;
10437 if (cond_rank != then_rank && then_rank != 0)
10439 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10440 " and the then-block");
10441 return;
10443 else if (cond_rank != else_rank && else_rank != 0)
10445 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10446 " and the else-block");
10447 return;
10451 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10452 SET_EXPR_LOCATION (stmt, if_locus);
10453 add_stmt (stmt);
10456 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10457 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10458 is false for DO loops. INCR is the FOR increment expression. BODY is
10459 the statement controlled by the loop. BLAB is the break label. CLAB is
10460 the continue label. Everything is allowed to be NULL. */
10462 void
10463 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10464 tree blab, tree clab, bool cond_is_first)
10466 tree entry = NULL, exit = NULL, t;
10468 /* In theory could forbid cilk spawn for loop increment expression,
10469 but it should work just fine. */
10471 /* If the condition is zero don't generate a loop construct. */
10472 if (cond && integer_zerop (cond))
10474 if (cond_is_first)
10476 t = build_and_jump (&blab);
10477 SET_EXPR_LOCATION (t, start_locus);
10478 add_stmt (t);
10481 else
10483 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10485 /* If we have an exit condition, then we build an IF with gotos either
10486 out of the loop, or to the top of it. If there's no exit condition,
10487 then we just build a jump back to the top. */
10488 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10490 if (cond && !integer_nonzerop (cond))
10492 /* Canonicalize the loop condition to the end. This means
10493 generating a branch to the loop condition. Reuse the
10494 continue label, if possible. */
10495 if (cond_is_first)
10497 if (incr || !clab)
10499 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10500 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10502 else
10503 t = build1 (GOTO_EXPR, void_type_node, clab);
10504 SET_EXPR_LOCATION (t, start_locus);
10505 add_stmt (t);
10508 t = build_and_jump (&blab);
10509 if (cond_is_first)
10510 exit = fold_build3_loc (start_locus,
10511 COND_EXPR, void_type_node, cond, exit, t);
10512 else
10513 exit = fold_build3_loc (input_location,
10514 COND_EXPR, void_type_node, cond, exit, t);
10516 else
10518 /* For the backward-goto's location of an unconditional loop
10519 use the beginning of the body, or, if there is none, the
10520 top of the loop. */
10521 location_t loc = EXPR_LOCATION (expr_first (body));
10522 if (loc == UNKNOWN_LOCATION)
10523 loc = start_locus;
10524 SET_EXPR_LOCATION (exit, loc);
10527 add_stmt (top);
10530 if (body)
10531 add_stmt (body);
10532 if (clab)
10533 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10534 if (incr)
10535 add_stmt (incr);
10536 if (entry)
10537 add_stmt (entry);
10538 if (exit)
10539 add_stmt (exit);
10540 if (blab)
10541 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10544 tree
10545 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10547 bool skip;
10548 tree label = *label_p;
10550 /* In switch statements break is sometimes stylistically used after
10551 a return statement. This can lead to spurious warnings about
10552 control reaching the end of a non-void function when it is
10553 inlined. Note that we are calling block_may_fallthru with
10554 language specific tree nodes; this works because
10555 block_may_fallthru returns true when given something it does not
10556 understand. */
10557 skip = !block_may_fallthru (cur_stmt_list);
10559 if (!label)
10561 if (!skip)
10562 *label_p = label = create_artificial_label (loc);
10564 else if (TREE_CODE (label) == LABEL_DECL)
10566 else switch (TREE_INT_CST_LOW (label))
10568 case 0:
10569 if (is_break)
10570 error_at (loc, "break statement not within loop or switch");
10571 else
10572 error_at (loc, "continue statement not within a loop");
10573 return NULL_TREE;
10575 case 1:
10576 gcc_assert (is_break);
10577 error_at (loc, "break statement used with OpenMP for loop");
10578 return NULL_TREE;
10580 case 2:
10581 if (is_break)
10582 error ("break statement within %<#pragma simd%> loop body");
10583 else
10584 error ("continue statement within %<#pragma simd%> loop body");
10585 return NULL_TREE;
10587 default:
10588 gcc_unreachable ();
10591 if (skip)
10592 return NULL_TREE;
10594 if (!is_break)
10595 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10597 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10600 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10602 static void
10603 emit_side_effect_warnings (location_t loc, tree expr)
10605 if (expr == error_mark_node)
10607 else if (!TREE_SIDE_EFFECTS (expr))
10609 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10610 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10612 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10614 tree r = expr;
10615 location_t cloc = loc;
10616 while (TREE_CODE (r) == COMPOUND_EXPR)
10618 if (EXPR_HAS_LOCATION (r))
10619 cloc = EXPR_LOCATION (r);
10620 r = TREE_OPERAND (r, 1);
10622 if (!TREE_SIDE_EFFECTS (r)
10623 && !VOID_TYPE_P (TREE_TYPE (r))
10624 && !CONVERT_EXPR_P (r)
10625 && !TREE_NO_WARNING (r)
10626 && !TREE_NO_WARNING (expr))
10627 warning_at (cloc, OPT_Wunused_value,
10628 "right-hand operand of comma expression has no effect");
10630 else
10631 warn_if_unused_value (expr, loc);
10634 /* Process an expression as if it were a complete statement. Emit
10635 diagnostics, but do not call ADD_STMT. LOC is the location of the
10636 statement. */
10638 tree
10639 c_process_expr_stmt (location_t loc, tree expr)
10641 tree exprv;
10643 if (!expr)
10644 return NULL_TREE;
10646 expr = c_fully_fold (expr, false, NULL);
10648 if (warn_sequence_point)
10649 verify_sequence_points (expr);
10651 if (TREE_TYPE (expr) != error_mark_node
10652 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10653 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10654 error_at (loc, "expression statement has incomplete type");
10656 /* If we're not processing a statement expression, warn about unused values.
10657 Warnings for statement expressions will be emitted later, once we figure
10658 out which is the result. */
10659 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10660 && warn_unused_value)
10661 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10663 exprv = expr;
10664 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10665 exprv = TREE_OPERAND (exprv, 1);
10666 while (CONVERT_EXPR_P (exprv))
10667 exprv = TREE_OPERAND (exprv, 0);
10668 if (DECL_P (exprv)
10669 || handled_component_p (exprv)
10670 || TREE_CODE (exprv) == ADDR_EXPR)
10671 mark_exp_read (exprv);
10673 /* If the expression is not of a type to which we cannot assign a line
10674 number, wrap the thing in a no-op NOP_EXPR. */
10675 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10677 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10678 SET_EXPR_LOCATION (expr, loc);
10681 return expr;
10684 /* Emit an expression as a statement. LOC is the location of the
10685 expression. */
10687 tree
10688 c_finish_expr_stmt (location_t loc, tree expr)
10690 if (expr)
10691 return add_stmt (c_process_expr_stmt (loc, expr));
10692 else
10693 return NULL;
10696 /* Do the opposite and emit a statement as an expression. To begin,
10697 create a new binding level and return it. */
10699 tree
10700 c_begin_stmt_expr (void)
10702 tree ret;
10704 /* We must force a BLOCK for this level so that, if it is not expanded
10705 later, there is a way to turn off the entire subtree of blocks that
10706 are contained in it. */
10707 keep_next_level ();
10708 ret = c_begin_compound_stmt (true);
10710 c_bindings_start_stmt_expr (c_switch_stack == NULL
10711 ? NULL
10712 : c_switch_stack->bindings);
10714 /* Mark the current statement list as belonging to a statement list. */
10715 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10717 return ret;
10720 /* LOC is the location of the compound statement to which this body
10721 belongs. */
10723 tree
10724 c_finish_stmt_expr (location_t loc, tree body)
10726 tree last, type, tmp, val;
10727 tree *last_p;
10729 body = c_end_compound_stmt (loc, body, true);
10731 c_bindings_end_stmt_expr (c_switch_stack == NULL
10732 ? NULL
10733 : c_switch_stack->bindings);
10735 /* Locate the last statement in BODY. See c_end_compound_stmt
10736 about always returning a BIND_EXPR. */
10737 last_p = &BIND_EXPR_BODY (body);
10738 last = BIND_EXPR_BODY (body);
10740 continue_searching:
10741 if (TREE_CODE (last) == STATEMENT_LIST)
10743 tree_stmt_iterator i;
10745 /* This can happen with degenerate cases like ({ }). No value. */
10746 if (!TREE_SIDE_EFFECTS (last))
10747 return body;
10749 /* If we're supposed to generate side effects warnings, process
10750 all of the statements except the last. */
10751 if (warn_unused_value)
10753 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10755 location_t tloc;
10756 tree t = tsi_stmt (i);
10758 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10759 emit_side_effect_warnings (tloc, t);
10762 else
10763 i = tsi_last (last);
10764 last_p = tsi_stmt_ptr (i);
10765 last = *last_p;
10768 /* If the end of the list is exception related, then the list was split
10769 by a call to push_cleanup. Continue searching. */
10770 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10771 || TREE_CODE (last) == TRY_CATCH_EXPR)
10773 last_p = &TREE_OPERAND (last, 0);
10774 last = *last_p;
10775 goto continue_searching;
10778 if (last == error_mark_node)
10779 return last;
10781 /* In the case that the BIND_EXPR is not necessary, return the
10782 expression out from inside it. */
10783 if (last == BIND_EXPR_BODY (body)
10784 && BIND_EXPR_VARS (body) == NULL)
10786 /* Even if this looks constant, do not allow it in a constant
10787 expression. */
10788 last = c_wrap_maybe_const (last, true);
10789 /* Do not warn if the return value of a statement expression is
10790 unused. */
10791 TREE_NO_WARNING (last) = 1;
10792 return last;
10795 /* Extract the type of said expression. */
10796 type = TREE_TYPE (last);
10798 /* If we're not returning a value at all, then the BIND_EXPR that
10799 we already have is a fine expression to return. */
10800 if (!type || VOID_TYPE_P (type))
10801 return body;
10803 /* Now that we've located the expression containing the value, it seems
10804 silly to make voidify_wrapper_expr repeat the process. Create a
10805 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10806 tmp = create_tmp_var_raw (type);
10808 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10809 tree_expr_nonnegative_p giving up immediately. */
10810 val = last;
10811 if (TREE_CODE (val) == NOP_EXPR
10812 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10813 val = TREE_OPERAND (val, 0);
10815 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10816 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10819 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10820 SET_EXPR_LOCATION (t, loc);
10821 return t;
10825 /* Begin and end compound statements. This is as simple as pushing
10826 and popping new statement lists from the tree. */
10828 tree
10829 c_begin_compound_stmt (bool do_scope)
10831 tree stmt = push_stmt_list ();
10832 if (do_scope)
10833 push_scope ();
10834 return stmt;
10837 /* End a compound statement. STMT is the statement. LOC is the
10838 location of the compound statement-- this is usually the location
10839 of the opening brace. */
10841 tree
10842 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10844 tree block = NULL;
10846 if (do_scope)
10848 if (c_dialect_objc ())
10849 objc_clear_super_receiver ();
10850 block = pop_scope ();
10853 stmt = pop_stmt_list (stmt);
10854 stmt = c_build_bind_expr (loc, block, stmt);
10856 /* If this compound statement is nested immediately inside a statement
10857 expression, then force a BIND_EXPR to be created. Otherwise we'll
10858 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10859 STATEMENT_LISTs merge, and thus we can lose track of what statement
10860 was really last. */
10861 if (building_stmt_list_p ()
10862 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10863 && TREE_CODE (stmt) != BIND_EXPR)
10865 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10866 TREE_SIDE_EFFECTS (stmt) = 1;
10867 SET_EXPR_LOCATION (stmt, loc);
10870 return stmt;
10873 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10874 when the current scope is exited. EH_ONLY is true when this is not
10875 meant to apply to normal control flow transfer. */
10877 void
10878 push_cleanup (tree decl, tree cleanup, bool eh_only)
10880 enum tree_code code;
10881 tree stmt, list;
10882 bool stmt_expr;
10884 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10885 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10886 add_stmt (stmt);
10887 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10888 list = push_stmt_list ();
10889 TREE_OPERAND (stmt, 0) = list;
10890 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10893 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10894 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10896 static tree
10897 build_vec_cmp (tree_code code, tree type,
10898 tree arg0, tree arg1)
10900 tree zero_vec = build_zero_cst (type);
10901 tree minus_one_vec = build_minus_one_cst (type);
10902 tree cmp_type = build_same_sized_truth_vector_type (type);
10903 tree cmp = build2 (code, cmp_type, arg0, arg1);
10904 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10907 /* Build a binary-operation expression without default conversions.
10908 CODE is the kind of expression to build.
10909 LOCATION is the operator's location.
10910 This function differs from `build' in several ways:
10911 the data type of the result is computed and recorded in it,
10912 warnings are generated if arg data types are invalid,
10913 special handling for addition and subtraction of pointers is known,
10914 and some optimization is done (operations on narrow ints
10915 are done in the narrower type when that gives the same result).
10916 Constant folding is also done before the result is returned.
10918 Note that the operands will never have enumeral types, or function
10919 or array types, because either they will have the default conversions
10920 performed or they have both just been converted to some other type in which
10921 the arithmetic is to be done. */
10923 tree
10924 build_binary_op (location_t location, enum tree_code code,
10925 tree orig_op0, tree orig_op1, bool convert_p)
10927 tree type0, type1, orig_type0, orig_type1;
10928 tree eptype;
10929 enum tree_code code0, code1;
10930 tree op0, op1;
10931 tree ret = error_mark_node;
10932 const char *invalid_op_diag;
10933 bool op0_int_operands, op1_int_operands;
10934 bool int_const, int_const_or_overflow, int_operands;
10936 /* Expression code to give to the expression when it is built.
10937 Normally this is CODE, which is what the caller asked for,
10938 but in some special cases we change it. */
10939 enum tree_code resultcode = code;
10941 /* Data type in which the computation is to be performed.
10942 In the simplest cases this is the common type of the arguments. */
10943 tree result_type = NULL;
10945 /* When the computation is in excess precision, the type of the
10946 final EXCESS_PRECISION_EXPR. */
10947 tree semantic_result_type = NULL;
10949 /* Nonzero means operands have already been type-converted
10950 in whatever way is necessary.
10951 Zero means they need to be converted to RESULT_TYPE. */
10952 int converted = 0;
10954 /* Nonzero means create the expression with this type, rather than
10955 RESULT_TYPE. */
10956 tree build_type = NULL_TREE;
10958 /* Nonzero means after finally constructing the expression
10959 convert it to this type. */
10960 tree final_type = NULL_TREE;
10962 /* Nonzero if this is an operation like MIN or MAX which can
10963 safely be computed in short if both args are promoted shorts.
10964 Also implies COMMON.
10965 -1 indicates a bitwise operation; this makes a difference
10966 in the exact conditions for when it is safe to do the operation
10967 in a narrower mode. */
10968 int shorten = 0;
10970 /* Nonzero if this is a comparison operation;
10971 if both args are promoted shorts, compare the original shorts.
10972 Also implies COMMON. */
10973 int short_compare = 0;
10975 /* Nonzero if this is a right-shift operation, which can be computed on the
10976 original short and then promoted if the operand is a promoted short. */
10977 int short_shift = 0;
10979 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10980 int common = 0;
10982 /* True means types are compatible as far as ObjC is concerned. */
10983 bool objc_ok;
10985 /* True means this is an arithmetic operation that may need excess
10986 precision. */
10987 bool may_need_excess_precision;
10989 /* True means this is a boolean operation that converts both its
10990 operands to truth-values. */
10991 bool boolean_op = false;
10993 /* Remember whether we're doing / or %. */
10994 bool doing_div_or_mod = false;
10996 /* Remember whether we're doing << or >>. */
10997 bool doing_shift = false;
10999 /* Tree holding instrumentation expression. */
11000 tree instrument_expr = NULL;
11002 if (location == UNKNOWN_LOCATION)
11003 location = input_location;
11005 op0 = orig_op0;
11006 op1 = orig_op1;
11008 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11009 if (op0_int_operands)
11010 op0 = remove_c_maybe_const_expr (op0);
11011 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11012 if (op1_int_operands)
11013 op1 = remove_c_maybe_const_expr (op1);
11014 int_operands = (op0_int_operands && op1_int_operands);
11015 if (int_operands)
11017 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11018 && TREE_CODE (orig_op1) == INTEGER_CST);
11019 int_const = (int_const_or_overflow
11020 && !TREE_OVERFLOW (orig_op0)
11021 && !TREE_OVERFLOW (orig_op1));
11023 else
11024 int_const = int_const_or_overflow = false;
11026 /* Do not apply default conversion in mixed vector/scalar expression. */
11027 if (convert_p
11028 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11030 op0 = default_conversion (op0);
11031 op1 = default_conversion (op1);
11034 /* When Cilk Plus is enabled and there are array notations inside op0, then
11035 we check to see if there are builtin array notation functions. If
11036 so, then we take on the type of the array notation inside it. */
11037 if (flag_cilkplus && contains_array_notation_expr (op0))
11038 orig_type0 = type0 = find_correct_array_notation_type (op0);
11039 else
11040 orig_type0 = type0 = TREE_TYPE (op0);
11042 if (flag_cilkplus && contains_array_notation_expr (op1))
11043 orig_type1 = type1 = find_correct_array_notation_type (op1);
11044 else
11045 orig_type1 = type1 = TREE_TYPE (op1);
11047 /* The expression codes of the data types of the arguments tell us
11048 whether the arguments are integers, floating, pointers, etc. */
11049 code0 = TREE_CODE (type0);
11050 code1 = TREE_CODE (type1);
11052 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11053 STRIP_TYPE_NOPS (op0);
11054 STRIP_TYPE_NOPS (op1);
11056 /* If an error was already reported for one of the arguments,
11057 avoid reporting another error. */
11059 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11060 return error_mark_node;
11062 if (code0 == POINTER_TYPE
11063 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11064 return error_mark_node;
11066 if (code1 == POINTER_TYPE
11067 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11068 return error_mark_node;
11070 if ((invalid_op_diag
11071 = targetm.invalid_binary_op (code, type0, type1)))
11073 error_at (location, invalid_op_diag);
11074 return error_mark_node;
11077 switch (code)
11079 case PLUS_EXPR:
11080 case MINUS_EXPR:
11081 case MULT_EXPR:
11082 case TRUNC_DIV_EXPR:
11083 case CEIL_DIV_EXPR:
11084 case FLOOR_DIV_EXPR:
11085 case ROUND_DIV_EXPR:
11086 case EXACT_DIV_EXPR:
11087 may_need_excess_precision = true;
11088 break;
11089 default:
11090 may_need_excess_precision = false;
11091 break;
11093 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11095 op0 = TREE_OPERAND (op0, 0);
11096 type0 = TREE_TYPE (op0);
11098 else if (may_need_excess_precision
11099 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11101 type0 = eptype;
11102 op0 = convert (eptype, op0);
11104 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11106 op1 = TREE_OPERAND (op1, 0);
11107 type1 = TREE_TYPE (op1);
11109 else if (may_need_excess_precision
11110 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11112 type1 = eptype;
11113 op1 = convert (eptype, op1);
11116 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11118 /* In case when one of the operands of the binary operation is
11119 a vector and another is a scalar -- convert scalar to vector. */
11120 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11122 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11123 true);
11125 switch (convert_flag)
11127 case stv_error:
11128 return error_mark_node;
11129 case stv_firstarg:
11131 bool maybe_const = true;
11132 tree sc;
11133 sc = c_fully_fold (op0, false, &maybe_const);
11134 sc = save_expr (sc);
11135 sc = convert (TREE_TYPE (type1), sc);
11136 op0 = build_vector_from_val (type1, sc);
11137 if (!maybe_const)
11138 op0 = c_wrap_maybe_const (op0, true);
11139 orig_type0 = type0 = TREE_TYPE (op0);
11140 code0 = TREE_CODE (type0);
11141 converted = 1;
11142 break;
11144 case stv_secondarg:
11146 bool maybe_const = true;
11147 tree sc;
11148 sc = c_fully_fold (op1, false, &maybe_const);
11149 sc = save_expr (sc);
11150 sc = convert (TREE_TYPE (type0), sc);
11151 op1 = build_vector_from_val (type0, sc);
11152 if (!maybe_const)
11153 op1 = c_wrap_maybe_const (op1, true);
11154 orig_type1 = type1 = TREE_TYPE (op1);
11155 code1 = TREE_CODE (type1);
11156 converted = 1;
11157 break;
11159 default:
11160 break;
11164 switch (code)
11166 case PLUS_EXPR:
11167 /* Handle the pointer + int case. */
11168 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11170 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11171 goto return_build_binary_op;
11173 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11175 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11176 goto return_build_binary_op;
11178 else
11179 common = 1;
11180 break;
11182 case MINUS_EXPR:
11183 /* Subtraction of two similar pointers.
11184 We must subtract them as integers, then divide by object size. */
11185 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11186 && comp_target_types (location, type0, type1))
11188 ret = pointer_diff (location, op0, op1);
11189 goto return_build_binary_op;
11191 /* Handle pointer minus int. Just like pointer plus int. */
11192 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11194 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11195 goto return_build_binary_op;
11197 else
11198 common = 1;
11199 break;
11201 case MULT_EXPR:
11202 common = 1;
11203 break;
11205 case TRUNC_DIV_EXPR:
11206 case CEIL_DIV_EXPR:
11207 case FLOOR_DIV_EXPR:
11208 case ROUND_DIV_EXPR:
11209 case EXACT_DIV_EXPR:
11210 doing_div_or_mod = true;
11211 warn_for_div_by_zero (location, op1);
11213 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11214 || code0 == FIXED_POINT_TYPE
11215 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11216 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11217 || code1 == FIXED_POINT_TYPE
11218 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11220 enum tree_code tcode0 = code0, tcode1 = code1;
11222 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11223 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11224 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11225 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11227 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11228 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11229 resultcode = RDIV_EXPR;
11230 else
11231 /* Although it would be tempting to shorten always here, that
11232 loses on some targets, since the modulo instruction is
11233 undefined if the quotient can't be represented in the
11234 computation mode. We shorten only if unsigned or if
11235 dividing by something we know != -1. */
11236 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11237 || (TREE_CODE (op1) == INTEGER_CST
11238 && !integer_all_onesp (op1)));
11239 common = 1;
11241 break;
11243 case BIT_AND_EXPR:
11244 case BIT_IOR_EXPR:
11245 case BIT_XOR_EXPR:
11246 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11247 shorten = -1;
11248 /* Allow vector types which are not floating point types. */
11249 else if (code0 == VECTOR_TYPE
11250 && code1 == VECTOR_TYPE
11251 && !VECTOR_FLOAT_TYPE_P (type0)
11252 && !VECTOR_FLOAT_TYPE_P (type1))
11253 common = 1;
11254 break;
11256 case TRUNC_MOD_EXPR:
11257 case FLOOR_MOD_EXPR:
11258 doing_div_or_mod = true;
11259 warn_for_div_by_zero (location, op1);
11261 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11262 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11263 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11264 common = 1;
11265 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11267 /* Although it would be tempting to shorten always here, that loses
11268 on some targets, since the modulo instruction is undefined if the
11269 quotient can't be represented in the computation mode. We shorten
11270 only if unsigned or if dividing by something we know != -1. */
11271 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11272 || (TREE_CODE (op1) == INTEGER_CST
11273 && !integer_all_onesp (op1)));
11274 common = 1;
11276 break;
11278 case TRUTH_ANDIF_EXPR:
11279 case TRUTH_ORIF_EXPR:
11280 case TRUTH_AND_EXPR:
11281 case TRUTH_OR_EXPR:
11282 case TRUTH_XOR_EXPR:
11283 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11284 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11285 || code0 == FIXED_POINT_TYPE)
11286 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11287 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11288 || code1 == FIXED_POINT_TYPE))
11290 /* Result of these operations is always an int,
11291 but that does not mean the operands should be
11292 converted to ints! */
11293 result_type = integer_type_node;
11294 if (op0_int_operands)
11296 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11297 op0 = remove_c_maybe_const_expr (op0);
11299 else
11300 op0 = c_objc_common_truthvalue_conversion (location, op0);
11301 if (op1_int_operands)
11303 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11304 op1 = remove_c_maybe_const_expr (op1);
11306 else
11307 op1 = c_objc_common_truthvalue_conversion (location, op1);
11308 converted = 1;
11309 boolean_op = true;
11311 if (code == TRUTH_ANDIF_EXPR)
11313 int_const_or_overflow = (int_operands
11314 && TREE_CODE (orig_op0) == INTEGER_CST
11315 && (op0 == truthvalue_false_node
11316 || TREE_CODE (orig_op1) == INTEGER_CST));
11317 int_const = (int_const_or_overflow
11318 && !TREE_OVERFLOW (orig_op0)
11319 && (op0 == truthvalue_false_node
11320 || !TREE_OVERFLOW (orig_op1)));
11322 else if (code == TRUTH_ORIF_EXPR)
11324 int_const_or_overflow = (int_operands
11325 && TREE_CODE (orig_op0) == INTEGER_CST
11326 && (op0 == truthvalue_true_node
11327 || TREE_CODE (orig_op1) == INTEGER_CST));
11328 int_const = (int_const_or_overflow
11329 && !TREE_OVERFLOW (orig_op0)
11330 && (op0 == truthvalue_true_node
11331 || !TREE_OVERFLOW (orig_op1)));
11333 break;
11335 /* Shift operations: result has same type as first operand;
11336 always convert second operand to int.
11337 Also set SHORT_SHIFT if shifting rightward. */
11339 case RSHIFT_EXPR:
11340 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11341 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11342 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11343 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11345 result_type = type0;
11346 converted = 1;
11348 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11349 || code0 == VECTOR_TYPE)
11350 && code1 == INTEGER_TYPE)
11352 doing_shift = true;
11353 if (TREE_CODE (op1) == INTEGER_CST)
11355 if (tree_int_cst_sgn (op1) < 0)
11357 int_const = false;
11358 if (c_inhibit_evaluation_warnings == 0)
11359 warning_at (location, OPT_Wshift_count_negative,
11360 "right shift count is negative");
11362 else if (code0 == VECTOR_TYPE)
11364 if (compare_tree_int (op1,
11365 TYPE_PRECISION (TREE_TYPE (type0)))
11366 >= 0)
11368 int_const = false;
11369 if (c_inhibit_evaluation_warnings == 0)
11370 warning_at (location, OPT_Wshift_count_overflow,
11371 "right shift count >= width of vector element");
11374 else
11376 if (!integer_zerop (op1))
11377 short_shift = 1;
11379 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11381 int_const = false;
11382 if (c_inhibit_evaluation_warnings == 0)
11383 warning_at (location, OPT_Wshift_count_overflow,
11384 "right shift count >= width of type");
11389 /* Use the type of the value to be shifted. */
11390 result_type = type0;
11391 /* Avoid converting op1 to result_type later. */
11392 converted = 1;
11394 break;
11396 case LSHIFT_EXPR:
11397 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11398 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11399 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11400 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11402 result_type = type0;
11403 converted = 1;
11405 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11406 || code0 == VECTOR_TYPE)
11407 && code1 == INTEGER_TYPE)
11409 doing_shift = true;
11410 if (TREE_CODE (op0) == INTEGER_CST
11411 && tree_int_cst_sgn (op0) < 0)
11413 /* Don't reject a left shift of a negative value in a context
11414 where a constant expression is needed in C90. */
11415 if (flag_isoc99)
11416 int_const = false;
11417 if (c_inhibit_evaluation_warnings == 0)
11418 warning_at (location, OPT_Wshift_negative_value,
11419 "left shift of negative value");
11421 if (TREE_CODE (op1) == INTEGER_CST)
11423 if (tree_int_cst_sgn (op1) < 0)
11425 int_const = false;
11426 if (c_inhibit_evaluation_warnings == 0)
11427 warning_at (location, OPT_Wshift_count_negative,
11428 "left shift count is negative");
11430 else if (code0 == VECTOR_TYPE)
11432 if (compare_tree_int (op1,
11433 TYPE_PRECISION (TREE_TYPE (type0)))
11434 >= 0)
11436 int_const = false;
11437 if (c_inhibit_evaluation_warnings == 0)
11438 warning_at (location, OPT_Wshift_count_overflow,
11439 "left shift count >= width of vector element");
11442 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11444 int_const = false;
11445 if (c_inhibit_evaluation_warnings == 0)
11446 warning_at (location, OPT_Wshift_count_overflow,
11447 "left shift count >= width of type");
11449 else if (TREE_CODE (op0) == INTEGER_CST
11450 && maybe_warn_shift_overflow (location, op0, op1)
11451 && flag_isoc99)
11452 int_const = false;
11455 /* Use the type of the value to be shifted. */
11456 result_type = type0;
11457 /* Avoid converting op1 to result_type later. */
11458 converted = 1;
11460 break;
11462 case EQ_EXPR:
11463 case NE_EXPR:
11464 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11466 tree intt;
11467 if (!vector_types_compatible_elements_p (type0, type1))
11469 error_at (location, "comparing vectors with different "
11470 "element types");
11471 return error_mark_node;
11474 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11476 error_at (location, "comparing vectors with different "
11477 "number of elements");
11478 return error_mark_node;
11481 /* It's not precisely specified how the usual arithmetic
11482 conversions apply to the vector types. Here, we use
11483 the unsigned type if one of the operands is signed and
11484 the other one is unsigned. */
11485 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11487 if (!TYPE_UNSIGNED (type0))
11488 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11489 else
11490 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11491 warning_at (location, OPT_Wsign_compare, "comparison between "
11492 "types %qT and %qT", type0, type1);
11495 /* Always construct signed integer vector type. */
11496 intt = c_common_type_for_size (GET_MODE_BITSIZE
11497 (SCALAR_TYPE_MODE
11498 (TREE_TYPE (type0))), 0);
11499 result_type = build_opaque_vector_type (intt,
11500 TYPE_VECTOR_SUBPARTS (type0));
11501 converted = 1;
11502 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11503 goto return_build_binary_op;
11505 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11506 warning_at (location,
11507 OPT_Wfloat_equal,
11508 "comparing floating point with == or != is unsafe");
11509 /* Result of comparison is always int,
11510 but don't convert the args to int! */
11511 build_type = integer_type_node;
11512 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11513 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11514 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11515 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11516 short_compare = 1;
11517 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11519 if (TREE_CODE (op0) == ADDR_EXPR
11520 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11521 && !from_macro_expansion_at (location))
11523 if (code == EQ_EXPR)
11524 warning_at (location,
11525 OPT_Waddress,
11526 "the comparison will always evaluate as %<false%> "
11527 "for the address of %qD will never be NULL",
11528 TREE_OPERAND (op0, 0));
11529 else
11530 warning_at (location,
11531 OPT_Waddress,
11532 "the comparison will always evaluate as %<true%> "
11533 "for the address of %qD will never be NULL",
11534 TREE_OPERAND (op0, 0));
11536 result_type = type0;
11538 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11540 if (TREE_CODE (op1) == ADDR_EXPR
11541 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11542 && !from_macro_expansion_at (location))
11544 if (code == EQ_EXPR)
11545 warning_at (location,
11546 OPT_Waddress,
11547 "the comparison will always evaluate as %<false%> "
11548 "for the address of %qD will never be NULL",
11549 TREE_OPERAND (op1, 0));
11550 else
11551 warning_at (location,
11552 OPT_Waddress,
11553 "the comparison will always evaluate as %<true%> "
11554 "for the address of %qD will never be NULL",
11555 TREE_OPERAND (op1, 0));
11557 result_type = type1;
11559 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11561 tree tt0 = TREE_TYPE (type0);
11562 tree tt1 = TREE_TYPE (type1);
11563 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11564 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11565 addr_space_t as_common = ADDR_SPACE_GENERIC;
11567 /* Anything compares with void *. void * compares with anything.
11568 Otherwise, the targets must be compatible
11569 and both must be object or both incomplete. */
11570 if (comp_target_types (location, type0, type1))
11571 result_type = common_pointer_type (type0, type1);
11572 else if (!addr_space_superset (as0, as1, &as_common))
11574 error_at (location, "comparison of pointers to "
11575 "disjoint address spaces");
11576 return error_mark_node;
11578 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11580 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11581 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11582 "comparison of %<void *%> with function pointer");
11584 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11586 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11587 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11588 "comparison of %<void *%> with function pointer");
11590 else
11591 /* Avoid warning about the volatile ObjC EH puts on decls. */
11592 if (!objc_ok)
11593 pedwarn (location, 0,
11594 "comparison of distinct pointer types lacks a cast");
11596 if (result_type == NULL_TREE)
11598 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11599 result_type = build_pointer_type
11600 (build_qualified_type (void_type_node, qual));
11603 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11605 result_type = type0;
11606 pedwarn (location, 0, "comparison between pointer and integer");
11608 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11610 result_type = type1;
11611 pedwarn (location, 0, "comparison between pointer and integer");
11613 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11614 || truth_value_p (TREE_CODE (orig_op0)))
11615 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11616 || truth_value_p (TREE_CODE (orig_op1))))
11617 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11618 break;
11620 case LE_EXPR:
11621 case GE_EXPR:
11622 case LT_EXPR:
11623 case GT_EXPR:
11624 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11626 tree intt;
11627 if (!vector_types_compatible_elements_p (type0, type1))
11629 error_at (location, "comparing vectors with different "
11630 "element types");
11631 return error_mark_node;
11634 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11636 error_at (location, "comparing vectors with different "
11637 "number of elements");
11638 return error_mark_node;
11641 /* It's not precisely specified how the usual arithmetic
11642 conversions apply to the vector types. Here, we use
11643 the unsigned type if one of the operands is signed and
11644 the other one is unsigned. */
11645 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11647 if (!TYPE_UNSIGNED (type0))
11648 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11649 else
11650 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11651 warning_at (location, OPT_Wsign_compare, "comparison between "
11652 "types %qT and %qT", type0, type1);
11655 /* Always construct signed integer vector type. */
11656 intt = c_common_type_for_size (GET_MODE_BITSIZE
11657 (SCALAR_TYPE_MODE
11658 (TREE_TYPE (type0))), 0);
11659 result_type = build_opaque_vector_type (intt,
11660 TYPE_VECTOR_SUBPARTS (type0));
11661 converted = 1;
11662 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11663 goto return_build_binary_op;
11665 build_type = integer_type_node;
11666 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11667 || code0 == FIXED_POINT_TYPE)
11668 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11669 || code1 == FIXED_POINT_TYPE))
11670 short_compare = 1;
11671 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11673 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11674 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11675 addr_space_t as_common;
11677 if (comp_target_types (location, type0, type1))
11679 result_type = common_pointer_type (type0, type1);
11680 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11681 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11682 pedwarn (location, 0,
11683 "comparison of complete and incomplete pointers");
11684 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11685 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11686 "ordered comparisons of pointers to functions");
11687 else if (null_pointer_constant_p (orig_op0)
11688 || null_pointer_constant_p (orig_op1))
11689 warning_at (location, OPT_Wextra,
11690 "ordered comparison of pointer with null pointer");
11693 else if (!addr_space_superset (as0, as1, &as_common))
11695 error_at (location, "comparison of pointers to "
11696 "disjoint address spaces");
11697 return error_mark_node;
11699 else
11701 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11702 result_type = build_pointer_type
11703 (build_qualified_type (void_type_node, qual));
11704 pedwarn (location, 0,
11705 "comparison of distinct pointer types lacks a cast");
11708 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11710 result_type = type0;
11711 if (pedantic)
11712 pedwarn (location, OPT_Wpedantic,
11713 "ordered comparison of pointer with integer zero");
11714 else if (extra_warnings)
11715 warning_at (location, OPT_Wextra,
11716 "ordered comparison of pointer with integer zero");
11718 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11720 result_type = type1;
11721 if (pedantic)
11722 pedwarn (location, OPT_Wpedantic,
11723 "ordered comparison of pointer with integer zero");
11724 else if (extra_warnings)
11725 warning_at (location, OPT_Wextra,
11726 "ordered comparison of pointer with integer zero");
11728 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11730 result_type = type0;
11731 pedwarn (location, 0, "comparison between pointer and integer");
11733 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11735 result_type = type1;
11736 pedwarn (location, 0, "comparison between pointer and integer");
11738 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11739 || truth_value_p (TREE_CODE (orig_op0)))
11740 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11741 || truth_value_p (TREE_CODE (orig_op1))))
11742 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11743 break;
11745 default:
11746 gcc_unreachable ();
11749 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11750 return error_mark_node;
11752 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11753 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11754 || !vector_types_compatible_elements_p (type0, type1)))
11756 gcc_rich_location richloc (location);
11757 richloc.maybe_add_expr (orig_op0);
11758 richloc.maybe_add_expr (orig_op1);
11759 binary_op_error (&richloc, code, type0, type1);
11760 return error_mark_node;
11763 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11764 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11766 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11767 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11769 bool first_complex = (code0 == COMPLEX_TYPE);
11770 bool second_complex = (code1 == COMPLEX_TYPE);
11771 int none_complex = (!first_complex && !second_complex);
11773 if (shorten || common || short_compare)
11775 result_type = c_common_type (type0, type1);
11776 do_warn_double_promotion (result_type, type0, type1,
11777 "implicit conversion from %qT to %qT "
11778 "to match other operand of binary "
11779 "expression",
11780 location);
11781 if (result_type == error_mark_node)
11782 return error_mark_node;
11785 if (first_complex != second_complex
11786 && (code == PLUS_EXPR
11787 || code == MINUS_EXPR
11788 || code == MULT_EXPR
11789 || (code == TRUNC_DIV_EXPR && first_complex))
11790 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11791 && flag_signed_zeros)
11793 /* An operation on mixed real/complex operands must be
11794 handled specially, but the language-independent code can
11795 more easily optimize the plain complex arithmetic if
11796 -fno-signed-zeros. */
11797 tree real_type = TREE_TYPE (result_type);
11798 tree real, imag;
11799 if (type0 != orig_type0 || type1 != orig_type1)
11801 gcc_assert (may_need_excess_precision && common);
11802 semantic_result_type = c_common_type (orig_type0, orig_type1);
11804 if (first_complex)
11806 if (TREE_TYPE (op0) != result_type)
11807 op0 = convert_and_check (location, result_type, op0);
11808 if (TREE_TYPE (op1) != real_type)
11809 op1 = convert_and_check (location, real_type, op1);
11811 else
11813 if (TREE_TYPE (op0) != real_type)
11814 op0 = convert_and_check (location, real_type, op0);
11815 if (TREE_TYPE (op1) != result_type)
11816 op1 = convert_and_check (location, result_type, op1);
11818 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11819 return error_mark_node;
11820 if (first_complex)
11822 op0 = save_expr (op0);
11823 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11824 op0, true);
11825 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11826 op0, true);
11827 switch (code)
11829 case MULT_EXPR:
11830 case TRUNC_DIV_EXPR:
11831 op1 = save_expr (op1);
11832 imag = build2 (resultcode, real_type, imag, op1);
11833 /* Fall through. */
11834 case PLUS_EXPR:
11835 case MINUS_EXPR:
11836 real = build2 (resultcode, real_type, real, op1);
11837 break;
11838 default:
11839 gcc_unreachable();
11842 else
11844 op1 = save_expr (op1);
11845 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11846 op1, true);
11847 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11848 op1, true);
11849 switch (code)
11851 case MULT_EXPR:
11852 op0 = save_expr (op0);
11853 imag = build2 (resultcode, real_type, op0, imag);
11854 /* Fall through. */
11855 case PLUS_EXPR:
11856 real = build2 (resultcode, real_type, op0, real);
11857 break;
11858 case MINUS_EXPR:
11859 real = build2 (resultcode, real_type, op0, real);
11860 imag = build1 (NEGATE_EXPR, real_type, imag);
11861 break;
11862 default:
11863 gcc_unreachable();
11866 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11867 goto return_build_binary_op;
11870 /* For certain operations (which identify themselves by shorten != 0)
11871 if both args were extended from the same smaller type,
11872 do the arithmetic in that type and then extend.
11874 shorten !=0 and !=1 indicates a bitwise operation.
11875 For them, this optimization is safe only if
11876 both args are zero-extended or both are sign-extended.
11877 Otherwise, we might change the result.
11878 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11879 but calculated in (unsigned short) it would be (unsigned short)-1. */
11881 if (shorten && none_complex)
11883 final_type = result_type;
11884 result_type = shorten_binary_op (result_type, op0, op1,
11885 shorten == -1);
11888 /* Shifts can be shortened if shifting right. */
11890 if (short_shift)
11892 int unsigned_arg;
11893 tree arg0 = get_narrower (op0, &unsigned_arg);
11895 final_type = result_type;
11897 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11898 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11900 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11901 && tree_int_cst_sgn (op1) > 0
11902 /* We can shorten only if the shift count is less than the
11903 number of bits in the smaller type size. */
11904 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11905 /* We cannot drop an unsigned shift after sign-extension. */
11906 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11908 /* Do an unsigned shift if the operand was zero-extended. */
11909 result_type
11910 = c_common_signed_or_unsigned_type (unsigned_arg,
11911 TREE_TYPE (arg0));
11912 /* Convert value-to-be-shifted to that type. */
11913 if (TREE_TYPE (op0) != result_type)
11914 op0 = convert (result_type, op0);
11915 converted = 1;
11919 /* Comparison operations are shortened too but differently.
11920 They identify themselves by setting short_compare = 1. */
11922 if (short_compare)
11924 /* Don't write &op0, etc., because that would prevent op0
11925 from being kept in a register.
11926 Instead, make copies of the our local variables and
11927 pass the copies by reference, then copy them back afterward. */
11928 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11929 enum tree_code xresultcode = resultcode;
11930 tree val
11931 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11932 &xresultcode);
11934 if (val != NULL_TREE)
11936 ret = val;
11937 goto return_build_binary_op;
11940 op0 = xop0, op1 = xop1;
11941 converted = 1;
11942 resultcode = xresultcode;
11944 if (c_inhibit_evaluation_warnings == 0)
11946 bool op0_maybe_const = true;
11947 bool op1_maybe_const = true;
11948 tree orig_op0_folded, orig_op1_folded;
11950 if (in_late_binary_op)
11952 orig_op0_folded = orig_op0;
11953 orig_op1_folded = orig_op1;
11955 else
11957 /* Fold for the sake of possible warnings, as in
11958 build_conditional_expr. This requires the
11959 "original" values to be folded, not just op0 and
11960 op1. */
11961 c_inhibit_evaluation_warnings++;
11962 op0 = c_fully_fold (op0, require_constant_value,
11963 &op0_maybe_const);
11964 op1 = c_fully_fold (op1, require_constant_value,
11965 &op1_maybe_const);
11966 c_inhibit_evaluation_warnings--;
11967 orig_op0_folded = c_fully_fold (orig_op0,
11968 require_constant_value,
11969 NULL);
11970 orig_op1_folded = c_fully_fold (orig_op1,
11971 require_constant_value,
11972 NULL);
11975 if (warn_sign_compare)
11976 warn_for_sign_compare (location, orig_op0_folded,
11977 orig_op1_folded, op0, op1,
11978 result_type, resultcode);
11979 if (!in_late_binary_op && !int_operands)
11981 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11982 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11983 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11984 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11990 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11991 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11992 Then the expression will be built.
11993 It will be given type FINAL_TYPE if that is nonzero;
11994 otherwise, it will be given type RESULT_TYPE. */
11996 if (!result_type)
11998 gcc_rich_location richloc (location);
11999 richloc.maybe_add_expr (orig_op0);
12000 richloc.maybe_add_expr (orig_op1);
12001 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12002 return error_mark_node;
12005 if (build_type == NULL_TREE)
12007 build_type = result_type;
12008 if ((type0 != orig_type0 || type1 != orig_type1)
12009 && !boolean_op)
12011 gcc_assert (may_need_excess_precision && common);
12012 semantic_result_type = c_common_type (orig_type0, orig_type1);
12016 if (!converted)
12018 op0 = ep_convert_and_check (location, result_type, op0,
12019 semantic_result_type);
12020 op1 = ep_convert_and_check (location, result_type, op1,
12021 semantic_result_type);
12023 /* This can happen if one operand has a vector type, and the other
12024 has a different type. */
12025 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12026 return error_mark_node;
12029 if (sanitize_flags_p ((SANITIZE_SHIFT
12030 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12031 && current_function_decl != NULL_TREE
12032 && (doing_div_or_mod || doing_shift)
12033 && !require_constant_value)
12035 /* OP0 and/or OP1 might have side-effects. */
12036 op0 = save_expr (op0);
12037 op1 = save_expr (op1);
12038 op0 = c_fully_fold (op0, false, NULL);
12039 op1 = c_fully_fold (op1, false, NULL);
12040 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12041 | SANITIZE_FLOAT_DIVIDE))))
12042 instrument_expr = ubsan_instrument_division (location, op0, op1);
12043 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12044 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12047 /* Treat expressions in initializers specially as they can't trap. */
12048 if (int_const_or_overflow)
12049 ret = (require_constant_value
12050 ? fold_build2_initializer_loc (location, resultcode, build_type,
12051 op0, op1)
12052 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12053 else
12054 ret = build2 (resultcode, build_type, op0, op1);
12055 if (final_type != NULL_TREE)
12056 ret = convert (final_type, ret);
12058 return_build_binary_op:
12059 gcc_assert (ret != error_mark_node);
12060 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12061 ret = (int_operands
12062 ? note_integer_operands (ret)
12063 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12064 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12065 && !in_late_binary_op)
12066 ret = note_integer_operands (ret);
12067 protected_set_expr_location (ret, location);
12069 if (instrument_expr != NULL)
12070 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12071 instrument_expr, ret);
12073 if (semantic_result_type)
12074 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12075 semantic_result_type, ret);
12077 return ret;
12081 /* Convert EXPR to be a truth-value, validating its type for this
12082 purpose. LOCATION is the source location for the expression. */
12084 tree
12085 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12087 bool int_const, int_operands;
12089 switch (TREE_CODE (TREE_TYPE (expr)))
12091 case ARRAY_TYPE:
12092 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12093 return error_mark_node;
12095 case RECORD_TYPE:
12096 error_at (location, "used struct type value where scalar is required");
12097 return error_mark_node;
12099 case UNION_TYPE:
12100 error_at (location, "used union type value where scalar is required");
12101 return error_mark_node;
12103 case VOID_TYPE:
12104 error_at (location, "void value not ignored as it ought to be");
12105 return error_mark_node;
12107 case POINTER_TYPE:
12108 if (reject_gcc_builtin (expr))
12109 return error_mark_node;
12110 break;
12112 case FUNCTION_TYPE:
12113 gcc_unreachable ();
12115 case VECTOR_TYPE:
12116 error_at (location, "used vector type where scalar is required");
12117 return error_mark_node;
12119 default:
12120 break;
12123 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12124 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12125 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12127 expr = remove_c_maybe_const_expr (expr);
12128 expr = build2 (NE_EXPR, integer_type_node, expr,
12129 convert (TREE_TYPE (expr), integer_zero_node));
12130 expr = note_integer_operands (expr);
12132 else
12133 /* ??? Should we also give an error for vectors rather than leaving
12134 those to give errors later? */
12135 expr = c_common_truthvalue_conversion (location, expr);
12137 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12139 if (TREE_OVERFLOW (expr))
12140 return expr;
12141 else
12142 return note_integer_operands (expr);
12144 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12145 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12146 return expr;
12150 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12151 required. */
12153 tree
12154 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12156 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12158 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12159 /* Executing a compound literal inside a function reinitializes
12160 it. */
12161 if (!TREE_STATIC (decl))
12162 *se = true;
12163 return decl;
12165 else
12166 return expr;
12169 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12170 statement. LOC is the location of the construct. */
12172 tree
12173 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12174 tree clauses)
12176 body = c_end_compound_stmt (loc, body, true);
12178 tree stmt = make_node (code);
12179 TREE_TYPE (stmt) = void_type_node;
12180 OMP_BODY (stmt) = body;
12181 OMP_CLAUSES (stmt) = clauses;
12182 SET_EXPR_LOCATION (stmt, loc);
12184 return add_stmt (stmt);
12187 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12188 statement. LOC is the location of the OACC_DATA. */
12190 tree
12191 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12193 tree stmt;
12195 block = c_end_compound_stmt (loc, block, true);
12197 stmt = make_node (OACC_DATA);
12198 TREE_TYPE (stmt) = void_type_node;
12199 OACC_DATA_CLAUSES (stmt) = clauses;
12200 OACC_DATA_BODY (stmt) = block;
12201 SET_EXPR_LOCATION (stmt, loc);
12203 return add_stmt (stmt);
12206 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12207 statement. LOC is the location of the OACC_HOST_DATA. */
12209 tree
12210 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12212 tree stmt;
12214 block = c_end_compound_stmt (loc, block, true);
12216 stmt = make_node (OACC_HOST_DATA);
12217 TREE_TYPE (stmt) = void_type_node;
12218 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12219 OACC_HOST_DATA_BODY (stmt) = block;
12220 SET_EXPR_LOCATION (stmt, loc);
12222 return add_stmt (stmt);
12225 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12227 tree
12228 c_begin_omp_parallel (void)
12230 tree block;
12232 keep_next_level ();
12233 block = c_begin_compound_stmt (true);
12235 return block;
12238 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12239 statement. LOC is the location of the OMP_PARALLEL. */
12241 tree
12242 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12244 tree stmt;
12246 block = c_end_compound_stmt (loc, block, true);
12248 stmt = make_node (OMP_PARALLEL);
12249 TREE_TYPE (stmt) = void_type_node;
12250 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12251 OMP_PARALLEL_BODY (stmt) = block;
12252 SET_EXPR_LOCATION (stmt, loc);
12254 return add_stmt (stmt);
12257 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12259 tree
12260 c_begin_omp_task (void)
12262 tree block;
12264 keep_next_level ();
12265 block = c_begin_compound_stmt (true);
12267 return block;
12270 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12271 statement. LOC is the location of the #pragma. */
12273 tree
12274 c_finish_omp_task (location_t loc, tree clauses, tree block)
12276 tree stmt;
12278 block = c_end_compound_stmt (loc, block, true);
12280 stmt = make_node (OMP_TASK);
12281 TREE_TYPE (stmt) = void_type_node;
12282 OMP_TASK_CLAUSES (stmt) = clauses;
12283 OMP_TASK_BODY (stmt) = block;
12284 SET_EXPR_LOCATION (stmt, loc);
12286 return add_stmt (stmt);
12289 /* Generate GOMP_cancel call for #pragma omp cancel. */
12291 void
12292 c_finish_omp_cancel (location_t loc, tree clauses)
12294 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12295 int mask = 0;
12296 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12297 mask = 1;
12298 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12299 mask = 2;
12300 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12301 mask = 4;
12302 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12303 mask = 8;
12304 else
12306 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12307 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12308 "clauses");
12309 return;
12311 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12312 if (ifc != NULL_TREE)
12314 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12315 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12316 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12317 build_zero_cst (type));
12319 else
12320 ifc = boolean_true_node;
12321 tree stmt = build_call_expr_loc (loc, fn, 2,
12322 build_int_cst (integer_type_node, mask),
12323 ifc);
12324 add_stmt (stmt);
12327 /* Generate GOMP_cancellation_point call for
12328 #pragma omp cancellation point. */
12330 void
12331 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12333 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12334 int mask = 0;
12335 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12336 mask = 1;
12337 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12338 mask = 2;
12339 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12340 mask = 4;
12341 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12342 mask = 8;
12343 else
12345 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12346 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12347 "clauses");
12348 return;
12350 tree stmt = build_call_expr_loc (loc, fn, 1,
12351 build_int_cst (integer_type_node, mask));
12352 add_stmt (stmt);
12355 /* Helper function for handle_omp_array_sections. Called recursively
12356 to handle multiple array-section-subscripts. C is the clause,
12357 T current expression (initially OMP_CLAUSE_DECL), which is either
12358 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12359 expression if specified, TREE_VALUE length expression if specified,
12360 TREE_CHAIN is what it has been specified after, or some decl.
12361 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12362 set to true if any of the array-section-subscript could have length
12363 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12364 first array-section-subscript which is known not to have length
12365 of one. Given say:
12366 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12367 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12368 all are or may have length of 1, array-section-subscript [:2] is the
12369 first one known not to have length 1. For array-section-subscript
12370 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12371 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12372 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12373 case though, as some lengths could be zero. */
12375 static tree
12376 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12377 bool &maybe_zero_len, unsigned int &first_non_one,
12378 enum c_omp_region_type ort)
12380 tree ret, low_bound, length, type;
12381 if (TREE_CODE (t) != TREE_LIST)
12383 if (error_operand_p (t))
12384 return error_mark_node;
12385 ret = t;
12386 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12387 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12389 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12390 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12391 return error_mark_node;
12393 if (TREE_CODE (t) == COMPONENT_REF
12394 && ort == C_ORT_OMP
12395 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12396 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12397 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12399 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12401 error_at (OMP_CLAUSE_LOCATION (c),
12402 "bit-field %qE in %qs clause",
12403 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12404 return error_mark_node;
12406 while (TREE_CODE (t) == COMPONENT_REF)
12408 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12410 error_at (OMP_CLAUSE_LOCATION (c),
12411 "%qE is a member of a union", t);
12412 return error_mark_node;
12414 t = TREE_OPERAND (t, 0);
12417 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12419 if (DECL_P (t))
12420 error_at (OMP_CLAUSE_LOCATION (c),
12421 "%qD is not a variable in %qs clause", t,
12422 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12423 else
12424 error_at (OMP_CLAUSE_LOCATION (c),
12425 "%qE is not a variable in %qs clause", t,
12426 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12427 return error_mark_node;
12429 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12430 && TYPE_ATOMIC (TREE_TYPE (t)))
12432 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12433 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12434 return error_mark_node;
12436 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12437 && VAR_P (t)
12438 && DECL_THREAD_LOCAL_P (t))
12440 error_at (OMP_CLAUSE_LOCATION (c),
12441 "%qD is threadprivate variable in %qs clause", t,
12442 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12443 return error_mark_node;
12445 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12446 && TYPE_ATOMIC (TREE_TYPE (t))
12447 && POINTER_TYPE_P (TREE_TYPE (t)))
12449 /* If the array section is pointer based and the pointer
12450 itself is _Atomic qualified, we need to atomically load
12451 the pointer. */
12452 c_expr expr;
12453 memset (&expr, 0, sizeof (expr));
12454 expr.value = ret;
12455 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12456 expr, false, false);
12457 ret = expr.value;
12459 return ret;
12462 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12463 maybe_zero_len, first_non_one, ort);
12464 if (ret == error_mark_node || ret == NULL_TREE)
12465 return ret;
12467 type = TREE_TYPE (ret);
12468 low_bound = TREE_PURPOSE (t);
12469 length = TREE_VALUE (t);
12471 if (low_bound == error_mark_node || length == error_mark_node)
12472 return error_mark_node;
12474 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12476 error_at (OMP_CLAUSE_LOCATION (c),
12477 "low bound %qE of array section does not have integral type",
12478 low_bound);
12479 return error_mark_node;
12481 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12483 error_at (OMP_CLAUSE_LOCATION (c),
12484 "length %qE of array section does not have integral type",
12485 length);
12486 return error_mark_node;
12488 if (low_bound
12489 && TREE_CODE (low_bound) == INTEGER_CST
12490 && TYPE_PRECISION (TREE_TYPE (low_bound))
12491 > TYPE_PRECISION (sizetype))
12492 low_bound = fold_convert (sizetype, low_bound);
12493 if (length
12494 && TREE_CODE (length) == INTEGER_CST
12495 && TYPE_PRECISION (TREE_TYPE (length))
12496 > TYPE_PRECISION (sizetype))
12497 length = fold_convert (sizetype, length);
12498 if (low_bound == NULL_TREE)
12499 low_bound = integer_zero_node;
12501 if (length != NULL_TREE)
12503 if (!integer_nonzerop (length))
12505 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12506 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12508 if (integer_zerop (length))
12510 error_at (OMP_CLAUSE_LOCATION (c),
12511 "zero length array section in %qs clause",
12512 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12513 return error_mark_node;
12516 else
12517 maybe_zero_len = true;
12519 if (first_non_one == types.length ()
12520 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12521 first_non_one++;
12523 if (TREE_CODE (type) == ARRAY_TYPE)
12525 if (length == NULL_TREE
12526 && (TYPE_DOMAIN (type) == NULL_TREE
12527 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12529 error_at (OMP_CLAUSE_LOCATION (c),
12530 "for unknown bound array type length expression must "
12531 "be specified");
12532 return error_mark_node;
12534 if (TREE_CODE (low_bound) == INTEGER_CST
12535 && tree_int_cst_sgn (low_bound) == -1)
12537 error_at (OMP_CLAUSE_LOCATION (c),
12538 "negative low bound in array section in %qs clause",
12539 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12540 return error_mark_node;
12542 if (length != NULL_TREE
12543 && TREE_CODE (length) == INTEGER_CST
12544 && tree_int_cst_sgn (length) == -1)
12546 error_at (OMP_CLAUSE_LOCATION (c),
12547 "negative length in array section in %qs clause",
12548 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12549 return error_mark_node;
12551 if (TYPE_DOMAIN (type)
12552 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12553 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12554 == INTEGER_CST)
12556 tree size
12557 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12558 size = size_binop (PLUS_EXPR, size, size_one_node);
12559 if (TREE_CODE (low_bound) == INTEGER_CST)
12561 if (tree_int_cst_lt (size, low_bound))
12563 error_at (OMP_CLAUSE_LOCATION (c),
12564 "low bound %qE above array section size "
12565 "in %qs clause", low_bound,
12566 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12567 return error_mark_node;
12569 if (tree_int_cst_equal (size, low_bound))
12571 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12572 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12574 error_at (OMP_CLAUSE_LOCATION (c),
12575 "zero length array section in %qs clause",
12576 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12577 return error_mark_node;
12579 maybe_zero_len = true;
12581 else if (length == NULL_TREE
12582 && first_non_one == types.length ()
12583 && tree_int_cst_equal
12584 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12585 low_bound))
12586 first_non_one++;
12588 else if (length == NULL_TREE)
12590 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12591 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12592 maybe_zero_len = true;
12593 if (first_non_one == types.length ())
12594 first_non_one++;
12596 if (length && TREE_CODE (length) == INTEGER_CST)
12598 if (tree_int_cst_lt (size, length))
12600 error_at (OMP_CLAUSE_LOCATION (c),
12601 "length %qE above array section size "
12602 "in %qs clause", length,
12603 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12604 return error_mark_node;
12606 if (TREE_CODE (low_bound) == INTEGER_CST)
12608 tree lbpluslen
12609 = size_binop (PLUS_EXPR,
12610 fold_convert (sizetype, low_bound),
12611 fold_convert (sizetype, length));
12612 if (TREE_CODE (lbpluslen) == INTEGER_CST
12613 && tree_int_cst_lt (size, lbpluslen))
12615 error_at (OMP_CLAUSE_LOCATION (c),
12616 "high bound %qE above array section size "
12617 "in %qs clause", lbpluslen,
12618 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12619 return error_mark_node;
12624 else if (length == NULL_TREE)
12626 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12627 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12628 maybe_zero_len = true;
12629 if (first_non_one == types.length ())
12630 first_non_one++;
12633 /* For [lb:] we will need to evaluate lb more than once. */
12634 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12636 tree lb = save_expr (low_bound);
12637 if (lb != low_bound)
12639 TREE_PURPOSE (t) = lb;
12640 low_bound = lb;
12644 else if (TREE_CODE (type) == POINTER_TYPE)
12646 if (length == NULL_TREE)
12648 error_at (OMP_CLAUSE_LOCATION (c),
12649 "for pointer type length expression must be specified");
12650 return error_mark_node;
12652 if (length != NULL_TREE
12653 && TREE_CODE (length) == INTEGER_CST
12654 && tree_int_cst_sgn (length) == -1)
12656 error_at (OMP_CLAUSE_LOCATION (c),
12657 "negative length in array section in %qs clause",
12658 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12659 return error_mark_node;
12661 /* If there is a pointer type anywhere but in the very first
12662 array-section-subscript, the array section can't be contiguous. */
12663 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12664 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12666 error_at (OMP_CLAUSE_LOCATION (c),
12667 "array section is not contiguous in %qs clause",
12668 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12669 return error_mark_node;
12672 else
12674 error_at (OMP_CLAUSE_LOCATION (c),
12675 "%qE does not have pointer or array type", ret);
12676 return error_mark_node;
12678 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12679 types.safe_push (TREE_TYPE (ret));
12680 /* We will need to evaluate lb more than once. */
12681 tree lb = save_expr (low_bound);
12682 if (lb != low_bound)
12684 TREE_PURPOSE (t) = lb;
12685 low_bound = lb;
12687 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12688 return ret;
12691 /* Handle array sections for clause C. */
12693 static bool
12694 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12696 bool maybe_zero_len = false;
12697 unsigned int first_non_one = 0;
12698 auto_vec<tree, 10> types;
12699 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12700 maybe_zero_len, first_non_one,
12701 ort);
12702 if (first == error_mark_node)
12703 return true;
12704 if (first == NULL_TREE)
12705 return false;
12706 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12708 tree t = OMP_CLAUSE_DECL (c);
12709 tree tem = NULL_TREE;
12710 /* Need to evaluate side effects in the length expressions
12711 if any. */
12712 while (TREE_CODE (t) == TREE_LIST)
12714 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12716 if (tem == NULL_TREE)
12717 tem = TREE_VALUE (t);
12718 else
12719 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12720 TREE_VALUE (t), tem);
12722 t = TREE_CHAIN (t);
12724 if (tem)
12725 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12726 first = c_fully_fold (first, false, NULL);
12727 OMP_CLAUSE_DECL (c) = first;
12729 else
12731 unsigned int num = types.length (), i;
12732 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12733 tree condition = NULL_TREE;
12735 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12736 maybe_zero_len = true;
12738 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12739 t = TREE_CHAIN (t))
12741 tree low_bound = TREE_PURPOSE (t);
12742 tree length = TREE_VALUE (t);
12744 i--;
12745 if (low_bound
12746 && TREE_CODE (low_bound) == INTEGER_CST
12747 && TYPE_PRECISION (TREE_TYPE (low_bound))
12748 > TYPE_PRECISION (sizetype))
12749 low_bound = fold_convert (sizetype, low_bound);
12750 if (length
12751 && TREE_CODE (length) == INTEGER_CST
12752 && TYPE_PRECISION (TREE_TYPE (length))
12753 > TYPE_PRECISION (sizetype))
12754 length = fold_convert (sizetype, length);
12755 if (low_bound == NULL_TREE)
12756 low_bound = integer_zero_node;
12757 if (!maybe_zero_len && i > first_non_one)
12759 if (integer_nonzerop (low_bound))
12760 goto do_warn_noncontiguous;
12761 if (length != NULL_TREE
12762 && TREE_CODE (length) == INTEGER_CST
12763 && TYPE_DOMAIN (types[i])
12764 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12765 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12766 == INTEGER_CST)
12768 tree size;
12769 size = size_binop (PLUS_EXPR,
12770 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12771 size_one_node);
12772 if (!tree_int_cst_equal (length, size))
12774 do_warn_noncontiguous:
12775 error_at (OMP_CLAUSE_LOCATION (c),
12776 "array section is not contiguous in %qs "
12777 "clause",
12778 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12779 return true;
12782 if (length != NULL_TREE
12783 && TREE_SIDE_EFFECTS (length))
12785 if (side_effects == NULL_TREE)
12786 side_effects = length;
12787 else
12788 side_effects = build2 (COMPOUND_EXPR,
12789 TREE_TYPE (side_effects),
12790 length, side_effects);
12793 else
12795 tree l;
12797 if (i > first_non_one
12798 && ((length && integer_nonzerop (length))
12799 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12800 continue;
12801 if (length)
12802 l = fold_convert (sizetype, length);
12803 else
12805 l = size_binop (PLUS_EXPR,
12806 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12807 size_one_node);
12808 l = size_binop (MINUS_EXPR, l,
12809 fold_convert (sizetype, low_bound));
12811 if (i > first_non_one)
12813 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12814 size_zero_node);
12815 if (condition == NULL_TREE)
12816 condition = l;
12817 else
12818 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12819 l, condition);
12821 else if (size == NULL_TREE)
12823 size = size_in_bytes (TREE_TYPE (types[i]));
12824 tree eltype = TREE_TYPE (types[num - 1]);
12825 while (TREE_CODE (eltype) == ARRAY_TYPE)
12826 eltype = TREE_TYPE (eltype);
12827 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12829 if (integer_zerop (size)
12830 || integer_zerop (size_in_bytes (eltype)))
12832 error_at (OMP_CLAUSE_LOCATION (c),
12833 "zero length array section in %qs clause",
12834 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12835 return error_mark_node;
12837 size = size_binop (EXACT_DIV_EXPR, size,
12838 size_in_bytes (eltype));
12840 size = size_binop (MULT_EXPR, size, l);
12841 if (condition)
12842 size = fold_build3 (COND_EXPR, sizetype, condition,
12843 size, size_zero_node);
12845 else
12846 size = size_binop (MULT_EXPR, size, l);
12849 if (side_effects)
12850 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12851 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12853 size = size_binop (MINUS_EXPR, size, size_one_node);
12854 size = c_fully_fold (size, false, NULL);
12855 tree index_type = build_index_type (size);
12856 tree eltype = TREE_TYPE (first);
12857 while (TREE_CODE (eltype) == ARRAY_TYPE)
12858 eltype = TREE_TYPE (eltype);
12859 tree type = build_array_type (eltype, index_type);
12860 tree ptype = build_pointer_type (eltype);
12861 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12862 t = build_fold_addr_expr (t);
12863 tree t2 = build_fold_addr_expr (first);
12864 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12865 ptrdiff_type_node, t2);
12866 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12867 ptrdiff_type_node, t2,
12868 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12869 ptrdiff_type_node, t));
12870 t2 = c_fully_fold (t2, false, NULL);
12871 if (tree_fits_shwi_p (t2))
12872 t = build2 (MEM_REF, type, t,
12873 build_int_cst (ptype, tree_to_shwi (t2)));
12874 else
12876 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12877 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12878 TREE_TYPE (t), t, t2);
12879 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12881 OMP_CLAUSE_DECL (c) = t;
12882 return false;
12884 first = c_fully_fold (first, false, NULL);
12885 OMP_CLAUSE_DECL (c) = first;
12886 if (size)
12887 size = c_fully_fold (size, false, NULL);
12888 OMP_CLAUSE_SIZE (c) = size;
12889 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12890 || (TREE_CODE (t) == COMPONENT_REF
12891 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12892 return false;
12893 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12894 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12895 switch (OMP_CLAUSE_MAP_KIND (c))
12897 case GOMP_MAP_ALLOC:
12898 case GOMP_MAP_TO:
12899 case GOMP_MAP_FROM:
12900 case GOMP_MAP_TOFROM:
12901 case GOMP_MAP_ALWAYS_TO:
12902 case GOMP_MAP_ALWAYS_FROM:
12903 case GOMP_MAP_ALWAYS_TOFROM:
12904 case GOMP_MAP_RELEASE:
12905 case GOMP_MAP_DELETE:
12906 case GOMP_MAP_FORCE_TO:
12907 case GOMP_MAP_FORCE_FROM:
12908 case GOMP_MAP_FORCE_TOFROM:
12909 case GOMP_MAP_FORCE_PRESENT:
12910 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12911 break;
12912 default:
12913 break;
12915 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12916 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12917 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12918 else if (TREE_CODE (t) == COMPONENT_REF)
12919 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12920 else
12921 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12922 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12923 && !c_mark_addressable (t))
12924 return false;
12925 OMP_CLAUSE_DECL (c2) = t;
12926 t = build_fold_addr_expr (first);
12927 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12928 tree ptr = OMP_CLAUSE_DECL (c2);
12929 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12930 ptr = build_fold_addr_expr (ptr);
12931 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12932 ptrdiff_type_node, t,
12933 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12934 ptrdiff_type_node, ptr));
12935 t = c_fully_fold (t, false, NULL);
12936 OMP_CLAUSE_SIZE (c2) = t;
12937 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12938 OMP_CLAUSE_CHAIN (c) = c2;
12940 return false;
12943 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12944 an inline call. But, remap
12945 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12946 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12948 static tree
12949 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12950 tree decl, tree placeholder)
12952 copy_body_data id;
12953 hash_map<tree, tree> decl_map;
12955 decl_map.put (omp_decl1, placeholder);
12956 decl_map.put (omp_decl2, decl);
12957 memset (&id, 0, sizeof (id));
12958 id.src_fn = DECL_CONTEXT (omp_decl1);
12959 id.dst_fn = current_function_decl;
12960 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12961 id.decl_map = &decl_map;
12963 id.copy_decl = copy_decl_no_change;
12964 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12965 id.transform_new_cfg = true;
12966 id.transform_return_to_modify = false;
12967 id.transform_lang_insert_block = NULL;
12968 id.eh_lp_nr = 0;
12969 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12970 return stmt;
12973 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12974 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12976 static tree
12977 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12979 if (*tp == (tree) data)
12980 return *tp;
12981 return NULL_TREE;
12984 /* For all elements of CLAUSES, validate them against their constraints.
12985 Remove any elements from the list that are invalid. */
12987 tree
12988 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12990 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12991 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12992 tree c, t, type, *pc;
12993 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12994 bool branch_seen = false;
12995 bool copyprivate_seen = false;
12996 bool linear_variable_step_check = false;
12997 tree *nowait_clause = NULL;
12998 bool ordered_seen = false;
12999 tree schedule_clause = NULL_TREE;
13000 bool oacc_async = false;
13002 bitmap_obstack_initialize (NULL);
13003 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13004 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13005 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13006 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13007 bitmap_initialize (&map_head, &bitmap_default_obstack);
13008 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13009 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13011 if (ort & C_ORT_ACC)
13012 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13013 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13015 oacc_async = true;
13016 break;
13019 for (pc = &clauses, c = clauses; c ; c = *pc)
13021 bool remove = false;
13022 bool need_complete = false;
13023 bool need_implicitly_determined = false;
13025 switch (OMP_CLAUSE_CODE (c))
13027 case OMP_CLAUSE_SHARED:
13028 need_implicitly_determined = true;
13029 goto check_dup_generic;
13031 case OMP_CLAUSE_PRIVATE:
13032 need_complete = true;
13033 need_implicitly_determined = true;
13034 goto check_dup_generic;
13036 case OMP_CLAUSE_REDUCTION:
13037 need_implicitly_determined = true;
13038 t = OMP_CLAUSE_DECL (c);
13039 if (TREE_CODE (t) == TREE_LIST)
13041 if (handle_omp_array_sections (c, ort))
13043 remove = true;
13044 break;
13047 t = OMP_CLAUSE_DECL (c);
13049 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13050 if (t == error_mark_node)
13052 remove = true;
13053 break;
13055 if (oacc_async)
13056 c_mark_addressable (t);
13057 type = TREE_TYPE (t);
13058 if (TREE_CODE (t) == MEM_REF)
13059 type = TREE_TYPE (type);
13060 if (TREE_CODE (type) == ARRAY_TYPE)
13062 tree oatype = type;
13063 gcc_assert (TREE_CODE (t) != MEM_REF);
13064 while (TREE_CODE (type) == ARRAY_TYPE)
13065 type = TREE_TYPE (type);
13066 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13068 error_at (OMP_CLAUSE_LOCATION (c),
13069 "%qD in %<reduction%> clause is a zero size array",
13071 remove = true;
13072 break;
13074 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13075 TYPE_SIZE_UNIT (type));
13076 if (integer_zerop (size))
13078 error_at (OMP_CLAUSE_LOCATION (c),
13079 "%qD in %<reduction%> clause is a zero size array",
13081 remove = true;
13082 break;
13084 size = size_binop (MINUS_EXPR, size, size_one_node);
13085 tree index_type = build_index_type (size);
13086 tree atype = build_array_type (type, index_type);
13087 tree ptype = build_pointer_type (type);
13088 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13089 t = build_fold_addr_expr (t);
13090 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13091 OMP_CLAUSE_DECL (c) = t;
13093 if (TYPE_ATOMIC (type))
13095 error_at (OMP_CLAUSE_LOCATION (c),
13096 "%<_Atomic%> %qE in %<reduction%> clause", t);
13097 remove = true;
13098 break;
13100 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13101 && (FLOAT_TYPE_P (type)
13102 || TREE_CODE (type) == COMPLEX_TYPE))
13104 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13105 const char *r_name = NULL;
13107 switch (r_code)
13109 case PLUS_EXPR:
13110 case MULT_EXPR:
13111 case MINUS_EXPR:
13112 break;
13113 case MIN_EXPR:
13114 if (TREE_CODE (type) == COMPLEX_TYPE)
13115 r_name = "min";
13116 break;
13117 case MAX_EXPR:
13118 if (TREE_CODE (type) == COMPLEX_TYPE)
13119 r_name = "max";
13120 break;
13121 case BIT_AND_EXPR:
13122 r_name = "&";
13123 break;
13124 case BIT_XOR_EXPR:
13125 r_name = "^";
13126 break;
13127 case BIT_IOR_EXPR:
13128 r_name = "|";
13129 break;
13130 case TRUTH_ANDIF_EXPR:
13131 if (FLOAT_TYPE_P (type))
13132 r_name = "&&";
13133 break;
13134 case TRUTH_ORIF_EXPR:
13135 if (FLOAT_TYPE_P (type))
13136 r_name = "||";
13137 break;
13138 default:
13139 gcc_unreachable ();
13141 if (r_name)
13143 error_at (OMP_CLAUSE_LOCATION (c),
13144 "%qE has invalid type for %<reduction(%s)%>",
13145 t, r_name);
13146 remove = true;
13147 break;
13150 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13152 error_at (OMP_CLAUSE_LOCATION (c),
13153 "user defined reduction not found for %qE", t);
13154 remove = true;
13155 break;
13157 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13159 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13160 type = TYPE_MAIN_VARIANT (type);
13161 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13162 VAR_DECL, NULL_TREE, type);
13163 tree decl_placeholder = NULL_TREE;
13164 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13165 DECL_ARTIFICIAL (placeholder) = 1;
13166 DECL_IGNORED_P (placeholder) = 1;
13167 if (TREE_CODE (t) == MEM_REF)
13169 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13170 VAR_DECL, NULL_TREE, type);
13171 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13172 DECL_ARTIFICIAL (decl_placeholder) = 1;
13173 DECL_IGNORED_P (decl_placeholder) = 1;
13175 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13176 c_mark_addressable (placeholder);
13177 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13178 c_mark_addressable (decl_placeholder ? decl_placeholder
13179 : OMP_CLAUSE_DECL (c));
13180 OMP_CLAUSE_REDUCTION_MERGE (c)
13181 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13182 TREE_VEC_ELT (list, 0),
13183 TREE_VEC_ELT (list, 1),
13184 decl_placeholder ? decl_placeholder
13185 : OMP_CLAUSE_DECL (c), placeholder);
13186 OMP_CLAUSE_REDUCTION_MERGE (c)
13187 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13188 void_type_node, NULL_TREE,
13189 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13190 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13191 if (TREE_VEC_LENGTH (list) == 6)
13193 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13194 c_mark_addressable (decl_placeholder ? decl_placeholder
13195 : OMP_CLAUSE_DECL (c));
13196 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13197 c_mark_addressable (placeholder);
13198 tree init = TREE_VEC_ELT (list, 5);
13199 if (init == error_mark_node)
13200 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13201 OMP_CLAUSE_REDUCTION_INIT (c)
13202 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13203 TREE_VEC_ELT (list, 3),
13204 decl_placeholder ? decl_placeholder
13205 : OMP_CLAUSE_DECL (c), placeholder);
13206 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13208 tree v = decl_placeholder ? decl_placeholder : t;
13209 OMP_CLAUSE_REDUCTION_INIT (c)
13210 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13211 OMP_CLAUSE_REDUCTION_INIT (c));
13213 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13214 c_find_omp_placeholder_r,
13215 placeholder, NULL))
13216 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13218 else
13220 tree init;
13221 tree v = decl_placeholder ? decl_placeholder : t;
13222 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13223 init = build_constructor (TREE_TYPE (v), NULL);
13224 else
13225 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13226 OMP_CLAUSE_REDUCTION_INIT (c)
13227 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13229 OMP_CLAUSE_REDUCTION_INIT (c)
13230 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13231 void_type_node, NULL_TREE,
13232 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13233 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13235 if (TREE_CODE (t) == MEM_REF)
13237 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13238 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13239 != INTEGER_CST)
13241 sorry ("variable length element type in array "
13242 "%<reduction%> clause");
13243 remove = true;
13244 break;
13246 t = TREE_OPERAND (t, 0);
13247 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13248 t = TREE_OPERAND (t, 0);
13249 if (TREE_CODE (t) == ADDR_EXPR)
13250 t = TREE_OPERAND (t, 0);
13252 goto check_dup_generic_t;
13254 case OMP_CLAUSE_COPYPRIVATE:
13255 copyprivate_seen = true;
13256 if (nowait_clause)
13258 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13259 "%<nowait%> clause must not be used together "
13260 "with %<copyprivate%>");
13261 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13262 nowait_clause = NULL;
13264 goto check_dup_generic;
13266 case OMP_CLAUSE_COPYIN:
13267 t = OMP_CLAUSE_DECL (c);
13268 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13270 error_at (OMP_CLAUSE_LOCATION (c),
13271 "%qE must be %<threadprivate%> for %<copyin%>", t);
13272 remove = true;
13273 break;
13275 goto check_dup_generic;
13277 case OMP_CLAUSE_LINEAR:
13278 if (ort != C_ORT_OMP_DECLARE_SIMD)
13279 need_implicitly_determined = true;
13280 t = OMP_CLAUSE_DECL (c);
13281 if (ort != C_ORT_OMP_DECLARE_SIMD
13282 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13284 error_at (OMP_CLAUSE_LOCATION (c),
13285 "modifier should not be specified in %<linear%> "
13286 "clause on %<simd%> or %<for%> constructs");
13287 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13289 if (ort & C_ORT_CILK)
13291 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13292 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13293 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13295 error_at (OMP_CLAUSE_LOCATION (c),
13296 "linear clause applied to non-integral, "
13297 "non-floating, non-pointer variable with type %qT",
13298 TREE_TYPE (t));
13299 remove = true;
13300 break;
13303 else
13305 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13306 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13308 error_at (OMP_CLAUSE_LOCATION (c),
13309 "linear clause applied to non-integral non-pointer "
13310 "variable with type %qT", TREE_TYPE (t));
13311 remove = true;
13312 break;
13314 if (TYPE_ATOMIC (TREE_TYPE (t)))
13316 error_at (OMP_CLAUSE_LOCATION (c),
13317 "%<_Atomic%> %qD in %<linear%> clause", t);
13318 remove = true;
13319 break;
13322 if (ort == C_ORT_OMP_DECLARE_SIMD)
13324 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13325 if (TREE_CODE (s) == PARM_DECL)
13327 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13328 /* map_head bitmap is used as uniform_head if
13329 declare_simd. */
13330 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13331 linear_variable_step_check = true;
13332 goto check_dup_generic;
13334 if (TREE_CODE (s) != INTEGER_CST)
13336 error_at (OMP_CLAUSE_LOCATION (c),
13337 "%<linear%> clause step %qE is neither constant "
13338 "nor a parameter", s);
13339 remove = true;
13340 break;
13343 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13345 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13346 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13347 OMP_CLAUSE_DECL (c), s);
13348 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13349 sizetype, fold_convert (sizetype, s),
13350 fold_convert
13351 (sizetype, OMP_CLAUSE_DECL (c)));
13352 if (s == error_mark_node)
13353 s = size_one_node;
13354 OMP_CLAUSE_LINEAR_STEP (c) = s;
13356 else
13357 OMP_CLAUSE_LINEAR_STEP (c)
13358 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13359 goto check_dup_generic;
13361 check_dup_generic:
13362 t = OMP_CLAUSE_DECL (c);
13363 check_dup_generic_t:
13364 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13366 error_at (OMP_CLAUSE_LOCATION (c),
13367 "%qE is not a variable in clause %qs", t,
13368 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13369 remove = true;
13371 else if (ort == C_ORT_ACC
13372 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13374 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13376 error ("%qD appears more than once in reduction clauses", t);
13377 remove = true;
13379 else
13380 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13382 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13383 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13384 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13386 error_at (OMP_CLAUSE_LOCATION (c),
13387 "%qE appears more than once in data clauses", t);
13388 remove = true;
13390 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13391 && bitmap_bit_p (&map_head, DECL_UID (t)))
13393 if (ort == C_ORT_ACC)
13394 error ("%qD appears more than once in data clauses", t);
13395 else
13396 error ("%qD appears both in data and map clauses", t);
13397 remove = true;
13399 else
13400 bitmap_set_bit (&generic_head, DECL_UID (t));
13401 break;
13403 case OMP_CLAUSE_FIRSTPRIVATE:
13404 t = OMP_CLAUSE_DECL (c);
13405 need_complete = true;
13406 need_implicitly_determined = true;
13407 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13409 error_at (OMP_CLAUSE_LOCATION (c),
13410 "%qE is not a variable in clause %<firstprivate%>", t);
13411 remove = true;
13413 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13414 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13416 error_at (OMP_CLAUSE_LOCATION (c),
13417 "%qE appears more than once in data clauses", t);
13418 remove = true;
13420 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13422 if (ort == C_ORT_ACC)
13423 error ("%qD appears more than once in data clauses", t);
13424 else
13425 error ("%qD appears both in data and map clauses", t);
13426 remove = true;
13428 else
13429 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13430 break;
13432 case OMP_CLAUSE_LASTPRIVATE:
13433 t = OMP_CLAUSE_DECL (c);
13434 need_complete = true;
13435 need_implicitly_determined = true;
13436 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13438 error_at (OMP_CLAUSE_LOCATION (c),
13439 "%qE is not a variable in clause %<lastprivate%>", t);
13440 remove = true;
13442 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13443 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13445 error_at (OMP_CLAUSE_LOCATION (c),
13446 "%qE appears more than once in data clauses", t);
13447 remove = true;
13449 else
13450 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13451 break;
13453 case OMP_CLAUSE_ALIGNED:
13454 t = OMP_CLAUSE_DECL (c);
13455 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13457 error_at (OMP_CLAUSE_LOCATION (c),
13458 "%qE is not a variable in %<aligned%> clause", t);
13459 remove = true;
13461 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13462 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13464 error_at (OMP_CLAUSE_LOCATION (c),
13465 "%qE in %<aligned%> clause is neither a pointer nor "
13466 "an array", t);
13467 remove = true;
13469 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13471 error_at (OMP_CLAUSE_LOCATION (c),
13472 "%<_Atomic%> %qD in %<aligned%> clause", t);
13473 remove = true;
13474 break;
13476 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13478 error_at (OMP_CLAUSE_LOCATION (c),
13479 "%qE appears more than once in %<aligned%> clauses",
13481 remove = true;
13483 else
13484 bitmap_set_bit (&aligned_head, DECL_UID (t));
13485 break;
13487 case OMP_CLAUSE_DEPEND:
13488 t = OMP_CLAUSE_DECL (c);
13489 if (t == NULL_TREE)
13491 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13492 == OMP_CLAUSE_DEPEND_SOURCE);
13493 break;
13495 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13497 gcc_assert (TREE_CODE (t) == TREE_LIST);
13498 for (; t; t = TREE_CHAIN (t))
13500 tree decl = TREE_VALUE (t);
13501 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13503 tree offset = TREE_PURPOSE (t);
13504 bool neg = wi::neg_p (wi::to_wide (offset));
13505 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13506 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13507 neg ? MINUS_EXPR : PLUS_EXPR,
13508 decl, offset);
13509 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13510 sizetype,
13511 fold_convert (sizetype, t2),
13512 fold_convert (sizetype, decl));
13513 if (t2 == error_mark_node)
13515 remove = true;
13516 break;
13518 TREE_PURPOSE (t) = t2;
13521 break;
13523 if (TREE_CODE (t) == TREE_LIST)
13525 if (handle_omp_array_sections (c, ort))
13526 remove = true;
13527 break;
13529 if (t == error_mark_node)
13530 remove = true;
13531 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13533 error_at (OMP_CLAUSE_LOCATION (c),
13534 "%qE is not a variable in %<depend%> clause", t);
13535 remove = true;
13537 else if (!c_mark_addressable (t))
13538 remove = true;
13539 break;
13541 case OMP_CLAUSE_MAP:
13542 case OMP_CLAUSE_TO:
13543 case OMP_CLAUSE_FROM:
13544 case OMP_CLAUSE__CACHE_:
13545 t = OMP_CLAUSE_DECL (c);
13546 if (TREE_CODE (t) == TREE_LIST)
13548 if (handle_omp_array_sections (c, ort))
13549 remove = true;
13550 else
13552 t = OMP_CLAUSE_DECL (c);
13553 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13555 error_at (OMP_CLAUSE_LOCATION (c),
13556 "array section does not have mappable type "
13557 "in %qs clause",
13558 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13559 remove = true;
13561 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13563 error_at (OMP_CLAUSE_LOCATION (c),
13564 "%<_Atomic%> %qE in %qs clause", t,
13565 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13566 remove = true;
13568 while (TREE_CODE (t) == ARRAY_REF)
13569 t = TREE_OPERAND (t, 0);
13570 if (TREE_CODE (t) == COMPONENT_REF
13571 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13573 while (TREE_CODE (t) == COMPONENT_REF)
13574 t = TREE_OPERAND (t, 0);
13575 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13576 break;
13577 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13579 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13580 error ("%qD appears more than once in motion"
13581 " clauses", t);
13582 else if (ort == C_ORT_ACC)
13583 error ("%qD appears more than once in data"
13584 " clauses", t);
13585 else
13586 error ("%qD appears more than once in map"
13587 " clauses", t);
13588 remove = true;
13590 else
13592 bitmap_set_bit (&map_head, DECL_UID (t));
13593 bitmap_set_bit (&map_field_head, DECL_UID (t));
13597 break;
13599 if (t == error_mark_node)
13601 remove = true;
13602 break;
13604 if (TREE_CODE (t) == COMPONENT_REF
13605 && (ort & C_ORT_OMP)
13606 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13608 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13610 error_at (OMP_CLAUSE_LOCATION (c),
13611 "bit-field %qE in %qs clause",
13612 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13613 remove = true;
13615 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13617 error_at (OMP_CLAUSE_LOCATION (c),
13618 "%qE does not have a mappable type in %qs clause",
13619 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13620 remove = true;
13622 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13624 error_at (OMP_CLAUSE_LOCATION (c),
13625 "%<_Atomic%> %qE in %qs clause", t,
13626 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13627 remove = true;
13629 while (TREE_CODE (t) == COMPONENT_REF)
13631 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13632 == UNION_TYPE)
13634 error_at (OMP_CLAUSE_LOCATION (c),
13635 "%qE is a member of a union", t);
13636 remove = true;
13637 break;
13639 t = TREE_OPERAND (t, 0);
13641 if (remove)
13642 break;
13643 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13645 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13646 break;
13649 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13651 error_at (OMP_CLAUSE_LOCATION (c),
13652 "%qE is not a variable in %qs clause", t,
13653 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13654 remove = true;
13656 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13658 error_at (OMP_CLAUSE_LOCATION (c),
13659 "%qD is threadprivate variable in %qs clause", t,
13660 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13661 remove = true;
13663 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13664 || (OMP_CLAUSE_MAP_KIND (c)
13665 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13666 && !c_mark_addressable (t))
13667 remove = true;
13668 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13669 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13670 || (OMP_CLAUSE_MAP_KIND (c)
13671 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13672 || (OMP_CLAUSE_MAP_KIND (c)
13673 == GOMP_MAP_FORCE_DEVICEPTR)))
13674 && t == OMP_CLAUSE_DECL (c)
13675 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13677 error_at (OMP_CLAUSE_LOCATION (c),
13678 "%qD does not have a mappable type in %qs clause", t,
13679 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13680 remove = true;
13682 else if (TREE_TYPE (t) == error_mark_node)
13683 remove = true;
13684 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13686 error_at (OMP_CLAUSE_LOCATION (c),
13687 "%<_Atomic%> %qE in %qs clause", t,
13688 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13689 remove = true;
13691 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13692 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13694 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13695 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13697 error ("%qD appears more than once in data clauses", t);
13698 remove = true;
13700 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13702 if (ort == C_ORT_ACC)
13703 error ("%qD appears more than once in data clauses", t);
13704 else
13705 error ("%qD appears both in data and map clauses", t);
13706 remove = true;
13708 else
13709 bitmap_set_bit (&generic_head, DECL_UID (t));
13711 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13713 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13714 error ("%qD appears more than once in motion clauses", t);
13715 else if (ort == C_ORT_ACC)
13716 error ("%qD appears more than once in data clauses", t);
13717 else
13718 error ("%qD appears more than once in map clauses", t);
13719 remove = true;
13721 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13722 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13724 if (ort == C_ORT_ACC)
13725 error ("%qD appears more than once in data clauses", t);
13726 else
13727 error ("%qD appears both in data and map clauses", t);
13728 remove = true;
13730 else
13732 bitmap_set_bit (&map_head, DECL_UID (t));
13733 if (t != OMP_CLAUSE_DECL (c)
13734 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13735 bitmap_set_bit (&map_field_head, DECL_UID (t));
13737 break;
13739 case OMP_CLAUSE_TO_DECLARE:
13740 case OMP_CLAUSE_LINK:
13741 t = OMP_CLAUSE_DECL (c);
13742 if (TREE_CODE (t) == FUNCTION_DECL
13743 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13745 else if (!VAR_P (t))
13747 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13748 error_at (OMP_CLAUSE_LOCATION (c),
13749 "%qE is neither a variable nor a function name in "
13750 "clause %qs", t,
13751 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13752 else
13753 error_at (OMP_CLAUSE_LOCATION (c),
13754 "%qE is not a variable in clause %qs", t,
13755 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13756 remove = true;
13758 else if (DECL_THREAD_LOCAL_P (t))
13760 error_at (OMP_CLAUSE_LOCATION (c),
13761 "%qD is threadprivate variable in %qs clause", t,
13762 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13763 remove = true;
13765 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13767 error_at (OMP_CLAUSE_LOCATION (c),
13768 "%qD does not have a mappable type in %qs clause", t,
13769 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13770 remove = true;
13772 if (remove)
13773 break;
13774 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13776 error_at (OMP_CLAUSE_LOCATION (c),
13777 "%qE appears more than once on the same "
13778 "%<declare target%> directive", t);
13779 remove = true;
13781 else
13782 bitmap_set_bit (&generic_head, DECL_UID (t));
13783 break;
13785 case OMP_CLAUSE_UNIFORM:
13786 t = OMP_CLAUSE_DECL (c);
13787 if (TREE_CODE (t) != PARM_DECL)
13789 if (DECL_P (t))
13790 error_at (OMP_CLAUSE_LOCATION (c),
13791 "%qD is not an argument in %<uniform%> clause", t);
13792 else
13793 error_at (OMP_CLAUSE_LOCATION (c),
13794 "%qE is not an argument in %<uniform%> clause", t);
13795 remove = true;
13796 break;
13798 /* map_head bitmap is used as uniform_head if declare_simd. */
13799 bitmap_set_bit (&map_head, DECL_UID (t));
13800 goto check_dup_generic;
13802 case OMP_CLAUSE_IS_DEVICE_PTR:
13803 case OMP_CLAUSE_USE_DEVICE_PTR:
13804 t = OMP_CLAUSE_DECL (c);
13805 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13806 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13808 error_at (OMP_CLAUSE_LOCATION (c),
13809 "%qs variable is neither a pointer nor an array",
13810 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13811 remove = true;
13813 goto check_dup_generic;
13815 case OMP_CLAUSE_NOWAIT:
13816 if (copyprivate_seen)
13818 error_at (OMP_CLAUSE_LOCATION (c),
13819 "%<nowait%> clause must not be used together "
13820 "with %<copyprivate%>");
13821 remove = true;
13822 break;
13824 nowait_clause = pc;
13825 pc = &OMP_CLAUSE_CHAIN (c);
13826 continue;
13828 case OMP_CLAUSE_IF:
13829 case OMP_CLAUSE_NUM_THREADS:
13830 case OMP_CLAUSE_NUM_TEAMS:
13831 case OMP_CLAUSE_THREAD_LIMIT:
13832 case OMP_CLAUSE_DEFAULT:
13833 case OMP_CLAUSE_UNTIED:
13834 case OMP_CLAUSE_COLLAPSE:
13835 case OMP_CLAUSE_FINAL:
13836 case OMP_CLAUSE_MERGEABLE:
13837 case OMP_CLAUSE_DEVICE:
13838 case OMP_CLAUSE_DIST_SCHEDULE:
13839 case OMP_CLAUSE_PARALLEL:
13840 case OMP_CLAUSE_FOR:
13841 case OMP_CLAUSE_SECTIONS:
13842 case OMP_CLAUSE_TASKGROUP:
13843 case OMP_CLAUSE_PROC_BIND:
13844 case OMP_CLAUSE_PRIORITY:
13845 case OMP_CLAUSE_GRAINSIZE:
13846 case OMP_CLAUSE_NUM_TASKS:
13847 case OMP_CLAUSE_NOGROUP:
13848 case OMP_CLAUSE_THREADS:
13849 case OMP_CLAUSE_SIMD:
13850 case OMP_CLAUSE_HINT:
13851 case OMP_CLAUSE_DEFAULTMAP:
13852 case OMP_CLAUSE__CILK_FOR_COUNT_:
13853 case OMP_CLAUSE_NUM_GANGS:
13854 case OMP_CLAUSE_NUM_WORKERS:
13855 case OMP_CLAUSE_VECTOR_LENGTH:
13856 case OMP_CLAUSE_ASYNC:
13857 case OMP_CLAUSE_WAIT:
13858 case OMP_CLAUSE_AUTO:
13859 case OMP_CLAUSE_INDEPENDENT:
13860 case OMP_CLAUSE_SEQ:
13861 case OMP_CLAUSE_GANG:
13862 case OMP_CLAUSE_WORKER:
13863 case OMP_CLAUSE_VECTOR:
13864 case OMP_CLAUSE_TILE:
13865 pc = &OMP_CLAUSE_CHAIN (c);
13866 continue;
13868 case OMP_CLAUSE_SCHEDULE:
13869 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13871 const char *p = NULL;
13872 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13874 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13875 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13876 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13877 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13878 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13879 default: gcc_unreachable ();
13881 if (p)
13883 error_at (OMP_CLAUSE_LOCATION (c),
13884 "%<nonmonotonic%> modifier specified for %qs "
13885 "schedule kind", p);
13886 OMP_CLAUSE_SCHEDULE_KIND (c)
13887 = (enum omp_clause_schedule_kind)
13888 (OMP_CLAUSE_SCHEDULE_KIND (c)
13889 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13892 schedule_clause = c;
13893 pc = &OMP_CLAUSE_CHAIN (c);
13894 continue;
13896 case OMP_CLAUSE_ORDERED:
13897 ordered_seen = true;
13898 pc = &OMP_CLAUSE_CHAIN (c);
13899 continue;
13901 case OMP_CLAUSE_SAFELEN:
13902 safelen = c;
13903 pc = &OMP_CLAUSE_CHAIN (c);
13904 continue;
13905 case OMP_CLAUSE_SIMDLEN:
13906 simdlen = c;
13907 pc = &OMP_CLAUSE_CHAIN (c);
13908 continue;
13910 case OMP_CLAUSE_INBRANCH:
13911 case OMP_CLAUSE_NOTINBRANCH:
13912 if (branch_seen)
13914 error_at (OMP_CLAUSE_LOCATION (c),
13915 "%<inbranch%> clause is incompatible with "
13916 "%<notinbranch%>");
13917 remove = true;
13918 break;
13920 branch_seen = true;
13921 pc = &OMP_CLAUSE_CHAIN (c);
13922 continue;
13924 default:
13925 gcc_unreachable ();
13928 if (!remove)
13930 t = OMP_CLAUSE_DECL (c);
13932 if (need_complete)
13934 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13935 if (t == error_mark_node)
13936 remove = true;
13939 if (need_implicitly_determined)
13941 const char *share_name = NULL;
13943 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13944 share_name = "threadprivate";
13945 else switch (c_omp_predetermined_sharing (t))
13947 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13948 break;
13949 case OMP_CLAUSE_DEFAULT_SHARED:
13950 /* const vars may be specified in firstprivate clause. */
13951 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13952 && TREE_READONLY (t))
13953 break;
13954 share_name = "shared";
13955 break;
13956 case OMP_CLAUSE_DEFAULT_PRIVATE:
13957 share_name = "private";
13958 break;
13959 default:
13960 gcc_unreachable ();
13962 if (share_name)
13964 error_at (OMP_CLAUSE_LOCATION (c),
13965 "%qE is predetermined %qs for %qs",
13966 t, share_name,
13967 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13968 remove = true;
13973 if (remove)
13974 *pc = OMP_CLAUSE_CHAIN (c);
13975 else
13976 pc = &OMP_CLAUSE_CHAIN (c);
13979 if (simdlen
13980 && safelen
13981 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13982 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13984 error_at (OMP_CLAUSE_LOCATION (simdlen),
13985 "%<simdlen%> clause value is bigger than "
13986 "%<safelen%> clause value");
13987 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13988 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13991 if (ordered_seen
13992 && schedule_clause
13993 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13994 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13996 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13997 "%<nonmonotonic%> schedule modifier specified together "
13998 "with %<ordered%> clause");
13999 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14000 = (enum omp_clause_schedule_kind)
14001 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14002 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14005 if (linear_variable_step_check)
14006 for (pc = &clauses, c = clauses; c ; c = *pc)
14008 bool remove = false;
14009 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14010 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
14011 && !bitmap_bit_p (&map_head,
14012 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
14014 error_at (OMP_CLAUSE_LOCATION (c),
14015 "%<linear%> clause step is a parameter %qD not "
14016 "specified in %<uniform%> clause",
14017 OMP_CLAUSE_LINEAR_STEP (c));
14018 remove = true;
14021 if (remove)
14022 *pc = OMP_CLAUSE_CHAIN (c);
14023 else
14024 pc = &OMP_CLAUSE_CHAIN (c);
14027 bitmap_obstack_release (NULL);
14028 return clauses;
14031 /* Return code to initialize DST with a copy constructor from SRC.
14032 C doesn't have copy constructors nor assignment operators, only for
14033 _Atomic vars we need to perform __atomic_load from src into a temporary
14034 followed by __atomic_store of the temporary to dst. */
14036 tree
14037 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14039 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14040 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14042 location_t loc = OMP_CLAUSE_LOCATION (clause);
14043 tree type = TREE_TYPE (dst);
14044 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14045 tree tmp = create_tmp_var (nonatomic_type);
14046 tree tmp_addr = build_fold_addr_expr (tmp);
14047 TREE_ADDRESSABLE (tmp) = 1;
14048 TREE_NO_WARNING (tmp) = 1;
14049 tree src_addr = build_fold_addr_expr (src);
14050 tree dst_addr = build_fold_addr_expr (dst);
14051 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14052 vec<tree, va_gc> *params;
14053 /* Expansion of a generic atomic load may require an addition
14054 element, so allocate enough to prevent a resize. */
14055 vec_alloc (params, 4);
14057 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14058 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14059 params->quick_push (src_addr);
14060 params->quick_push (tmp_addr);
14061 params->quick_push (seq_cst);
14062 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14064 vec_alloc (params, 4);
14066 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14067 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14068 params->quick_push (dst_addr);
14069 params->quick_push (tmp_addr);
14070 params->quick_push (seq_cst);
14071 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14072 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14075 /* Create a transaction node. */
14077 tree
14078 c_finish_transaction (location_t loc, tree block, int flags)
14080 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14081 if (flags & TM_STMT_ATTR_OUTER)
14082 TRANSACTION_EXPR_OUTER (stmt) = 1;
14083 if (flags & TM_STMT_ATTR_RELAXED)
14084 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14085 return add_stmt (stmt);
14088 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14089 down to the element type of an array. If ORIG_QUAL_TYPE is not
14090 NULL, then it should be used as the qualified type
14091 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14092 preserve information about the typedef name from which an array
14093 type was derived). */
14095 tree
14096 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14097 size_t orig_qual_indirect)
14099 if (type == error_mark_node)
14100 return type;
14102 if (TREE_CODE (type) == ARRAY_TYPE)
14104 tree t;
14105 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14106 type_quals, orig_qual_type,
14107 orig_qual_indirect - 1);
14109 /* See if we already have an identically qualified type. */
14110 if (orig_qual_type && orig_qual_indirect == 0)
14111 t = orig_qual_type;
14112 else
14113 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14115 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14116 && TYPE_NAME (t) == TYPE_NAME (type)
14117 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14118 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14119 TYPE_ATTRIBUTES (type)))
14120 break;
14122 if (!t)
14124 tree domain = TYPE_DOMAIN (type);
14126 t = build_variant_type_copy (type);
14127 TREE_TYPE (t) = element_type;
14129 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14130 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14131 SET_TYPE_STRUCTURAL_EQUALITY (t);
14132 else if (TYPE_CANONICAL (element_type) != element_type
14133 || (domain && TYPE_CANONICAL (domain) != domain))
14135 tree unqualified_canon
14136 = build_array_type (TYPE_CANONICAL (element_type),
14137 domain? TYPE_CANONICAL (domain)
14138 : NULL_TREE);
14139 if (TYPE_REVERSE_STORAGE_ORDER (type))
14141 unqualified_canon
14142 = build_distinct_type_copy (unqualified_canon);
14143 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14145 TYPE_CANONICAL (t)
14146 = c_build_qualified_type (unqualified_canon, type_quals);
14148 else
14149 TYPE_CANONICAL (t) = t;
14151 return t;
14154 /* A restrict-qualified pointer type must be a pointer to object or
14155 incomplete type. Note that the use of POINTER_TYPE_P also allows
14156 REFERENCE_TYPEs, which is appropriate for C++. */
14157 if ((type_quals & TYPE_QUAL_RESTRICT)
14158 && (!POINTER_TYPE_P (type)
14159 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14161 error ("invalid use of %<restrict%>");
14162 type_quals &= ~TYPE_QUAL_RESTRICT;
14165 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14166 ? orig_qual_type
14167 : build_qualified_type (type, type_quals));
14168 /* A variant type does not inherit the list of incomplete vars from the
14169 type main variant. */
14170 if (RECORD_OR_UNION_TYPE_P (var_type)
14171 && TYPE_MAIN_VARIANT (var_type) != var_type)
14172 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14173 return var_type;
14176 /* Build a VA_ARG_EXPR for the C parser. */
14178 tree
14179 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14181 if (error_operand_p (type))
14182 return error_mark_node;
14183 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14184 order because it takes the address of the expression. */
14185 else if (handled_component_p (expr)
14186 && reverse_storage_order_for_component_p (expr))
14188 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14189 return error_mark_node;
14191 else if (!COMPLETE_TYPE_P (type))
14193 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14194 "type %qT", type);
14195 return error_mark_node;
14197 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14198 warning_at (loc2, OPT_Wc___compat,
14199 "C++ requires promoted type, not enum type, in %<va_arg%>");
14200 return build_va_arg (loc2, expr, type);
14203 /* Return truthvalue of whether T1 is the same tree structure as T2.
14204 Return 1 if they are the same. Return false if they are different. */
14206 bool
14207 c_tree_equal (tree t1, tree t2)
14209 enum tree_code code1, code2;
14211 if (t1 == t2)
14212 return true;
14213 if (!t1 || !t2)
14214 return false;
14216 for (code1 = TREE_CODE (t1);
14217 CONVERT_EXPR_CODE_P (code1)
14218 || code1 == NON_LVALUE_EXPR;
14219 code1 = TREE_CODE (t1))
14220 t1 = TREE_OPERAND (t1, 0);
14221 for (code2 = TREE_CODE (t2);
14222 CONVERT_EXPR_CODE_P (code2)
14223 || code2 == NON_LVALUE_EXPR;
14224 code2 = TREE_CODE (t2))
14225 t2 = TREE_OPERAND (t2, 0);
14227 /* They might have become equal now. */
14228 if (t1 == t2)
14229 return true;
14231 if (code1 != code2)
14232 return false;
14234 switch (code1)
14236 case INTEGER_CST:
14237 return wi::to_wide (t1) == wi::to_wide (t2);
14239 case REAL_CST:
14240 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14242 case STRING_CST:
14243 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14244 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14245 TREE_STRING_LENGTH (t1));
14247 case FIXED_CST:
14248 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14249 TREE_FIXED_CST (t2));
14251 case COMPLEX_CST:
14252 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14253 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14255 case VECTOR_CST:
14256 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14258 case CONSTRUCTOR:
14259 /* We need to do this when determining whether or not two
14260 non-type pointer to member function template arguments
14261 are the same. */
14262 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14263 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14264 return false;
14266 tree field, value;
14267 unsigned int i;
14268 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14270 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14271 if (!c_tree_equal (field, elt2->index)
14272 || !c_tree_equal (value, elt2->value))
14273 return false;
14276 return true;
14278 case TREE_LIST:
14279 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14280 return false;
14281 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14282 return false;
14283 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14285 case SAVE_EXPR:
14286 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14288 case CALL_EXPR:
14290 tree arg1, arg2;
14291 call_expr_arg_iterator iter1, iter2;
14292 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14293 return false;
14294 for (arg1 = first_call_expr_arg (t1, &iter1),
14295 arg2 = first_call_expr_arg (t2, &iter2);
14296 arg1 && arg2;
14297 arg1 = next_call_expr_arg (&iter1),
14298 arg2 = next_call_expr_arg (&iter2))
14299 if (!c_tree_equal (arg1, arg2))
14300 return false;
14301 if (arg1 || arg2)
14302 return false;
14303 return true;
14306 case TARGET_EXPR:
14308 tree o1 = TREE_OPERAND (t1, 0);
14309 tree o2 = TREE_OPERAND (t2, 0);
14311 /* Special case: if either target is an unallocated VAR_DECL,
14312 it means that it's going to be unified with whatever the
14313 TARGET_EXPR is really supposed to initialize, so treat it
14314 as being equivalent to anything. */
14315 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14316 && !DECL_RTL_SET_P (o1))
14317 /*Nop*/;
14318 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14319 && !DECL_RTL_SET_P (o2))
14320 /*Nop*/;
14321 else if (!c_tree_equal (o1, o2))
14322 return false;
14324 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14327 case COMPONENT_REF:
14328 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14329 return false;
14330 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14332 case PARM_DECL:
14333 case VAR_DECL:
14334 case CONST_DECL:
14335 case FIELD_DECL:
14336 case FUNCTION_DECL:
14337 case IDENTIFIER_NODE:
14338 case SSA_NAME:
14339 return false;
14341 case TREE_VEC:
14343 unsigned ix;
14344 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14345 return false;
14346 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14347 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14348 TREE_VEC_ELT (t2, ix)))
14349 return false;
14350 return true;
14353 default:
14354 break;
14357 switch (TREE_CODE_CLASS (code1))
14359 case tcc_unary:
14360 case tcc_binary:
14361 case tcc_comparison:
14362 case tcc_expression:
14363 case tcc_vl_exp:
14364 case tcc_reference:
14365 case tcc_statement:
14367 int i, n = TREE_OPERAND_LENGTH (t1);
14369 switch (code1)
14371 case PREINCREMENT_EXPR:
14372 case PREDECREMENT_EXPR:
14373 case POSTINCREMENT_EXPR:
14374 case POSTDECREMENT_EXPR:
14375 n = 1;
14376 break;
14377 case ARRAY_REF:
14378 n = 2;
14379 break;
14380 default:
14381 break;
14384 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14385 && n != TREE_OPERAND_LENGTH (t2))
14386 return false;
14388 for (i = 0; i < n; ++i)
14389 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14390 return false;
14392 return true;
14395 case tcc_type:
14396 return comptypes (t1, t2);
14397 default:
14398 gcc_unreachable ();
14400 /* We can get here with --disable-checking. */
14401 return false;
14404 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14405 spawn-helper and BODY is the newly created body for FNDECL. */
14407 void
14408 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14410 tree list = alloc_stmt_list ();
14411 tree frame = make_cilk_frame (fndecl);
14412 tree dtor = create_cilk_function_exit (frame, false, true);
14413 add_local_decl (cfun, frame);
14415 DECL_SAVED_TREE (fndecl) = list;
14417 tree body_list = alloc_stmt_list ();
14418 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14419 body = fold_build_cleanup_point_expr (void_type_node, body);
14421 append_to_statement_list (body, &body_list);
14422 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14423 body_list, dtor), &list);
14426 /* Returns true when the function declaration FNDECL is implicit,
14427 introduced as a result of a call to an otherwise undeclared
14428 function, and false otherwise. */
14430 bool
14431 c_decl_implicit (const_tree fndecl)
14433 return C_DECL_IMPLICIT (fndecl);