P0409R2 - allow lambda capture [=, this]
[official-gcc.git] / gcc / c / c-typeck.c
blob73e74602f595b0a581ff7ed34f8aceff4cbe3518
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "cilk.h"
50 #include "gomp-constants.h"
51 #include "spellcheck-tree.h"
52 #include "gcc-rich-location.h"
53 #include "stringpool.h"
54 #include "attribs.h"
55 #include "asan.h"
57 /* Possible cases of implicit bad conversions. Used to select
58 diagnostic messages in convert_for_assignment. */
59 enum impl_conv {
60 ic_argpass,
61 ic_assign,
62 ic_init,
63 ic_return
66 /* The level of nesting inside "__alignof__". */
67 int in_alignof;
69 /* The level of nesting inside "sizeof". */
70 int in_sizeof;
72 /* The level of nesting inside "typeof". */
73 int in_typeof;
75 /* The argument of last parsed sizeof expression, only to be tested
76 if expr.original_code == SIZEOF_EXPR. */
77 tree c_last_sizeof_arg;
78 location_t c_last_sizeof_loc;
80 /* Nonzero if we might need to print a "missing braces around
81 initializer" message within this initializer. */
82 static int found_missing_braces;
84 static int require_constant_value;
85 static int require_constant_elements;
87 static bool null_pointer_constant_p (const_tree);
88 static tree qualify_type (tree, tree);
89 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
90 bool *);
91 static int comp_target_types (location_t, tree, tree);
92 static int function_types_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
95 static tree lookup_field (tree, tree);
96 static int convert_arguments (location_t, vec<location_t>, tree,
97 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
98 tree);
99 static tree pointer_diff (location_t, tree, tree);
100 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
101 enum impl_conv, bool, tree, tree, int);
102 static tree valid_compound_expr_initializer (tree, tree);
103 static void push_string (const char *);
104 static void push_member_name (tree);
105 static int spelling_length (void);
106 static char *print_spelling (char *);
107 static void warning_init (location_t, int, const char *);
108 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
109 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
110 bool, struct obstack *);
111 static void output_pending_init_elements (int, struct obstack *);
112 static bool set_designator (location_t, bool, struct obstack *);
113 static void push_range_stack (tree, struct obstack *);
114 static void add_pending_init (location_t, tree, tree, tree, bool,
115 struct obstack *);
116 static void set_nonincremental_init (struct obstack *);
117 static void set_nonincremental_init_from_string (tree, struct obstack *);
118 static tree find_init_member (tree, struct obstack *);
119 static void readonly_warning (tree, enum lvalue_use);
120 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
121 static void record_maybe_used_decl (tree);
122 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
124 /* Return true if EXP is a null pointer constant, false otherwise. */
126 static bool
127 null_pointer_constant_p (const_tree expr)
129 /* This should really operate on c_expr structures, but they aren't
130 yet available everywhere required. */
131 tree type = TREE_TYPE (expr);
132 return (TREE_CODE (expr) == INTEGER_CST
133 && !TREE_OVERFLOW (expr)
134 && integer_zerop (expr)
135 && (INTEGRAL_TYPE_P (type)
136 || (TREE_CODE (type) == POINTER_TYPE
137 && VOID_TYPE_P (TREE_TYPE (type))
138 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
141 /* EXPR may appear in an unevaluated part of an integer constant
142 expression, but not in an evaluated part. Wrap it in a
143 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
144 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
146 static tree
147 note_integer_operands (tree expr)
149 tree ret;
150 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
152 ret = copy_node (expr);
153 TREE_OVERFLOW (ret) = 1;
155 else
157 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
158 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
160 return ret;
163 /* Having checked whether EXPR may appear in an unevaluated part of an
164 integer constant expression and found that it may, remove any
165 C_MAYBE_CONST_EXPR noting this fact and return the resulting
166 expression. */
168 static inline tree
169 remove_c_maybe_const_expr (tree expr)
171 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
172 return C_MAYBE_CONST_EXPR_EXPR (expr);
173 else
174 return expr;
177 \f/* This is a cache to hold if two types are compatible or not. */
179 struct tagged_tu_seen_cache {
180 const struct tagged_tu_seen_cache * next;
181 const_tree t1;
182 const_tree t2;
183 /* The return value of tagged_types_tu_compatible_p if we had seen
184 these two types already. */
185 int val;
188 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
189 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
191 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
192 does not have an incomplete type. (That includes void types.)
193 LOC is the location of the use. */
195 tree
196 require_complete_type (location_t loc, tree value)
198 tree type = TREE_TYPE (value);
200 if (error_operand_p (value))
201 return error_mark_node;
203 /* First, detect a valid value with a complete type. */
204 if (COMPLETE_TYPE_P (type))
205 return value;
207 c_incomplete_type_error (loc, value, type);
208 return error_mark_node;
211 /* Print an error message for invalid use of an incomplete type.
212 VALUE is the expression that was used (or 0 if that isn't known)
213 and TYPE is the type that was invalid. LOC is the location for
214 the error. */
216 void
217 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
219 /* Avoid duplicate error message. */
220 if (TREE_CODE (type) == ERROR_MARK)
221 return;
223 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
224 error_at (loc, "%qD has an incomplete type %qT", value, type);
225 else
227 retry:
228 /* We must print an error message. Be clever about what it says. */
230 switch (TREE_CODE (type))
232 case RECORD_TYPE:
233 case UNION_TYPE:
234 case ENUMERAL_TYPE:
235 break;
237 case VOID_TYPE:
238 error_at (loc, "invalid use of void expression");
239 return;
241 case ARRAY_TYPE:
242 if (TYPE_DOMAIN (type))
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
246 error_at (loc, "invalid use of flexible array member");
247 return;
249 type = TREE_TYPE (type);
250 goto retry;
252 error_at (loc, "invalid use of array with unspecified bounds");
253 return;
255 default:
256 gcc_unreachable ();
259 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
260 error_at (loc, "invalid use of undefined type %qT", type);
261 else
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error_at (loc, "invalid use of incomplete typedef %qT", type);
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
270 tree
271 c_type_promotes_to (tree type)
273 tree ret = NULL_TREE;
275 if (TYPE_MAIN_VARIANT (type) == float_type_node)
276 ret = double_type_node;
277 else if (c_promoting_integer_type_p (type))
279 /* Preserve unsignedness if not really getting any wider. */
280 if (TYPE_UNSIGNED (type)
281 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
282 ret = unsigned_type_node;
283 else
284 ret = integer_type_node;
287 if (ret != NULL_TREE)
288 return (TYPE_ATOMIC (type)
289 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
290 : ret);
292 return type;
295 /* Return true if between two named address spaces, whether there is a superset
296 named address space that encompasses both address spaces. If there is a
297 superset, return which address space is the superset. */
299 static bool
300 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
302 if (as1 == as2)
304 *common = as1;
305 return true;
307 else if (targetm.addr_space.subset_p (as1, as2))
309 *common = as2;
310 return true;
312 else if (targetm.addr_space.subset_p (as2, as1))
314 *common = as1;
315 return true;
317 else
318 return false;
321 /* Return a variant of TYPE which has all the type qualifiers of LIKE
322 as well as those of TYPE. */
324 static tree
325 qualify_type (tree type, tree like)
327 addr_space_t as_type = TYPE_ADDR_SPACE (type);
328 addr_space_t as_like = TYPE_ADDR_SPACE (like);
329 addr_space_t as_common;
331 /* If the two named address spaces are different, determine the common
332 superset address space. If there isn't one, raise an error. */
333 if (!addr_space_superset (as_type, as_like, &as_common))
335 as_common = as_type;
336 error ("%qT and %qT are in disjoint named address spaces",
337 type, like);
340 return c_build_qualified_type (type,
341 TYPE_QUALS_NO_ADDR_SPACE (type)
342 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
343 | ENCODE_QUAL_ADDR_SPACE (as_common));
346 /* Return true iff the given tree T is a variable length array. */
348 bool
349 c_vla_type_p (const_tree t)
351 if (TREE_CODE (t) == ARRAY_TYPE
352 && C_TYPE_VARIABLE_SIZE (t))
353 return true;
354 return false;
357 /* Return the composite type of two compatible types.
359 We assume that comptypes has already been done and returned
360 nonzero; if that isn't so, this may crash. In particular, we
361 assume that qualifiers match. */
363 tree
364 composite_type (tree t1, tree t2)
366 enum tree_code code1;
367 enum tree_code code2;
368 tree attributes;
370 /* Save time if the two types are the same. */
372 if (t1 == t2) return t1;
374 /* If one type is nonsense, use the other. */
375 if (t1 == error_mark_node)
376 return t2;
377 if (t2 == error_mark_node)
378 return t1;
380 code1 = TREE_CODE (t1);
381 code2 = TREE_CODE (t2);
383 /* Merge the attributes. */
384 attributes = targetm.merge_type_attributes (t1, t2);
386 /* If one is an enumerated type and the other is the compatible
387 integer type, the composite type might be either of the two
388 (DR#013 question 3). For consistency, use the enumerated type as
389 the composite type. */
391 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
392 return t1;
393 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
394 return t2;
396 gcc_assert (code1 == code2);
398 switch (code1)
400 case POINTER_TYPE:
401 /* For two pointers, do this recursively on the target type. */
403 tree pointed_to_1 = TREE_TYPE (t1);
404 tree pointed_to_2 = TREE_TYPE (t2);
405 tree target = composite_type (pointed_to_1, pointed_to_2);
406 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
407 t1 = build_type_attribute_variant (t1, attributes);
408 return qualify_type (t1, t2);
411 case ARRAY_TYPE:
413 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
414 int quals;
415 tree unqual_elt;
416 tree d1 = TYPE_DOMAIN (t1);
417 tree d2 = TYPE_DOMAIN (t2);
418 bool d1_variable, d2_variable;
419 bool d1_zero, d2_zero;
420 bool t1_complete, t2_complete;
422 /* We should not have any type quals on arrays at all. */
423 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
424 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
426 t1_complete = COMPLETE_TYPE_P (t1);
427 t2_complete = COMPLETE_TYPE_P (t2);
429 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
430 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
432 d1_variable = (!d1_zero
433 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
434 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
435 d2_variable = (!d2_zero
436 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
437 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
438 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
439 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
441 /* Save space: see if the result is identical to one of the args. */
442 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
443 && (d2_variable || d2_zero || !d1_variable))
444 return build_type_attribute_variant (t1, attributes);
445 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
446 && (d1_variable || d1_zero || !d2_variable))
447 return build_type_attribute_variant (t2, attributes);
449 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
450 return build_type_attribute_variant (t1, attributes);
451 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
452 return build_type_attribute_variant (t2, attributes);
454 /* Merge the element types, and have a size if either arg has
455 one. We may have qualifiers on the element types. To set
456 up TYPE_MAIN_VARIANT correctly, we need to form the
457 composite of the unqualified types and add the qualifiers
458 back at the end. */
459 quals = TYPE_QUALS (strip_array_types (elt));
460 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
461 t1 = build_array_type (unqual_elt,
462 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
463 && (d2_variable
464 || d2_zero
465 || !d1_variable))
466 ? t1
467 : t2));
468 /* Ensure a composite type involving a zero-length array type
469 is a zero-length type not an incomplete type. */
470 if (d1_zero && d2_zero
471 && (t1_complete || t2_complete)
472 && !COMPLETE_TYPE_P (t1))
474 TYPE_SIZE (t1) = bitsize_zero_node;
475 TYPE_SIZE_UNIT (t1) = size_zero_node;
477 t1 = c_build_qualified_type (t1, quals);
478 return build_type_attribute_variant (t1, attributes);
481 case ENUMERAL_TYPE:
482 case RECORD_TYPE:
483 case UNION_TYPE:
484 if (attributes != NULL)
486 /* Try harder not to create a new aggregate type. */
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
488 return t1;
489 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
490 return t2;
492 return build_type_attribute_variant (t1, attributes);
494 case FUNCTION_TYPE:
495 /* Function types: prefer the one that specified arg types.
496 If both do, merge the arg types. Also merge the return types. */
498 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
499 tree p1 = TYPE_ARG_TYPES (t1);
500 tree p2 = TYPE_ARG_TYPES (t2);
501 int len;
502 tree newargs, n;
503 int i;
505 /* Save space: see if the result is identical to one of the args. */
506 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
507 return build_type_attribute_variant (t1, attributes);
508 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
509 return build_type_attribute_variant (t2, attributes);
511 /* Simple way if one arg fails to specify argument types. */
512 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
514 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
515 t1 = build_type_attribute_variant (t1, attributes);
516 return qualify_type (t1, t2);
518 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
520 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
521 t1 = build_type_attribute_variant (t1, attributes);
522 return qualify_type (t1, t2);
525 /* If both args specify argument types, we must merge the two
526 lists, argument by argument. */
528 for (len = 0, newargs = p1;
529 newargs && newargs != void_list_node;
530 len++, newargs = TREE_CHAIN (newargs))
533 for (i = 0; i < len; i++)
534 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
536 n = newargs;
538 for (; p1 && p1 != void_list_node;
539 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
541 /* A null type means arg type is not specified.
542 Take whatever the other function type has. */
543 if (TREE_VALUE (p1) == NULL_TREE)
545 TREE_VALUE (n) = TREE_VALUE (p2);
546 goto parm_done;
548 if (TREE_VALUE (p2) == NULL_TREE)
550 TREE_VALUE (n) = TREE_VALUE (p1);
551 goto parm_done;
554 /* Given wait (union {union wait *u; int *i} *)
555 and wait (union wait *),
556 prefer union wait * as type of parm. */
557 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
558 && TREE_VALUE (p1) != TREE_VALUE (p2))
560 tree memb;
561 tree mv2 = TREE_VALUE (p2);
562 if (mv2 && mv2 != error_mark_node
563 && TREE_CODE (mv2) != ARRAY_TYPE)
564 mv2 = TYPE_MAIN_VARIANT (mv2);
565 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
566 memb; memb = DECL_CHAIN (memb))
568 tree mv3 = TREE_TYPE (memb);
569 if (mv3 && mv3 != error_mark_node
570 && TREE_CODE (mv3) != ARRAY_TYPE)
571 mv3 = TYPE_MAIN_VARIANT (mv3);
572 if (comptypes (mv3, mv2))
574 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
575 TREE_VALUE (p2));
576 pedwarn (input_location, OPT_Wpedantic,
577 "function types not truly compatible in ISO C");
578 goto parm_done;
582 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
583 && TREE_VALUE (p2) != TREE_VALUE (p1))
585 tree memb;
586 tree mv1 = TREE_VALUE (p1);
587 if (mv1 && mv1 != error_mark_node
588 && TREE_CODE (mv1) != ARRAY_TYPE)
589 mv1 = TYPE_MAIN_VARIANT (mv1);
590 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
591 memb; memb = DECL_CHAIN (memb))
593 tree mv3 = TREE_TYPE (memb);
594 if (mv3 && mv3 != error_mark_node
595 && TREE_CODE (mv3) != ARRAY_TYPE)
596 mv3 = TYPE_MAIN_VARIANT (mv3);
597 if (comptypes (mv3, mv1))
599 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
600 TREE_VALUE (p1));
601 pedwarn (input_location, OPT_Wpedantic,
602 "function types not truly compatible in ISO C");
603 goto parm_done;
607 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
608 parm_done: ;
611 t1 = build_function_type (valtype, newargs);
612 t1 = qualify_type (t1, t2);
614 /* FALLTHRU */
616 default:
617 return build_type_attribute_variant (t1, attributes);
622 /* Return the type of a conditional expression between pointers to
623 possibly differently qualified versions of compatible types.
625 We assume that comp_target_types has already been done and returned
626 nonzero; if that isn't so, this may crash. */
628 static tree
629 common_pointer_type (tree t1, tree t2)
631 tree attributes;
632 tree pointed_to_1, mv1;
633 tree pointed_to_2, mv2;
634 tree target;
635 unsigned target_quals;
636 addr_space_t as1, as2, as_common;
637 int quals1, quals2;
639 /* Save time if the two types are the same. */
641 if (t1 == t2) return t1;
643 /* If one type is nonsense, use the other. */
644 if (t1 == error_mark_node)
645 return t2;
646 if (t2 == error_mark_node)
647 return t1;
649 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
650 && TREE_CODE (t2) == POINTER_TYPE);
652 /* Merge the attributes. */
653 attributes = targetm.merge_type_attributes (t1, t2);
655 /* Find the composite type of the target types, and combine the
656 qualifiers of the two types' targets. Do not lose qualifiers on
657 array element types by taking the TYPE_MAIN_VARIANT. */
658 mv1 = pointed_to_1 = TREE_TYPE (t1);
659 mv2 = pointed_to_2 = TREE_TYPE (t2);
660 if (TREE_CODE (mv1) != ARRAY_TYPE)
661 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
662 if (TREE_CODE (mv2) != ARRAY_TYPE)
663 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
664 target = composite_type (mv1, mv2);
666 /* Strip array types to get correct qualifier for pointers to arrays */
667 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
668 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
670 /* For function types do not merge const qualifiers, but drop them
671 if used inconsistently. The middle-end uses these to mark const
672 and noreturn functions. */
673 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
674 target_quals = (quals1 & quals2);
675 else
676 target_quals = (quals1 | quals2);
678 /* If the two named address spaces are different, determine the common
679 superset address space. This is guaranteed to exist due to the
680 assumption that comp_target_type returned non-zero. */
681 as1 = TYPE_ADDR_SPACE (pointed_to_1);
682 as2 = TYPE_ADDR_SPACE (pointed_to_2);
683 if (!addr_space_superset (as1, as2, &as_common))
684 gcc_unreachable ();
686 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
688 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
689 return build_type_attribute_variant (t1, attributes);
692 /* Return the common type for two arithmetic types under the usual
693 arithmetic conversions. The default conversions have already been
694 applied, and enumerated types converted to their compatible integer
695 types. The resulting type is unqualified and has no attributes.
697 This is the type for the result of most arithmetic operations
698 if the operands have the given two types. */
700 static tree
701 c_common_type (tree t1, tree t2)
703 enum tree_code code1;
704 enum tree_code code2;
706 /* If one type is nonsense, use the other. */
707 if (t1 == error_mark_node)
708 return t2;
709 if (t2 == error_mark_node)
710 return t1;
712 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
713 t1 = TYPE_MAIN_VARIANT (t1);
715 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
716 t2 = TYPE_MAIN_VARIANT (t2);
718 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
719 t1 = build_type_attribute_variant (t1, NULL_TREE);
721 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
722 t2 = build_type_attribute_variant (t2, NULL_TREE);
724 /* Save time if the two types are the same. */
726 if (t1 == t2) return t1;
728 code1 = TREE_CODE (t1);
729 code2 = TREE_CODE (t2);
731 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
732 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
733 || code1 == INTEGER_TYPE);
734 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
735 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
736 || code2 == INTEGER_TYPE);
738 /* When one operand is a decimal float type, the other operand cannot be
739 a generic float type or a complex type. We also disallow vector types
740 here. */
741 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
742 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
744 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
746 error ("can%'t mix operands of decimal float and vector types");
747 return error_mark_node;
749 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
751 error ("can%'t mix operands of decimal float and complex types");
752 return error_mark_node;
754 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
756 error ("can%'t mix operands of decimal float and other float types");
757 return error_mark_node;
761 /* If one type is a vector type, return that type. (How the usual
762 arithmetic conversions apply to the vector types extension is not
763 precisely specified.) */
764 if (code1 == VECTOR_TYPE)
765 return t1;
767 if (code2 == VECTOR_TYPE)
768 return t2;
770 /* If one type is complex, form the common type of the non-complex
771 components, then make that complex. Use T1 or T2 if it is the
772 required type. */
773 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
775 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
776 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
777 tree subtype = c_common_type (subtype1, subtype2);
779 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
780 return t1;
781 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
782 return t2;
783 else
784 return build_complex_type (subtype);
787 /* If only one is real, use it as the result. */
789 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
790 return t1;
792 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
793 return t2;
795 /* If both are real and either are decimal floating point types, use
796 the decimal floating point type with the greater precision. */
798 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
800 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
801 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
802 return dfloat128_type_node;
803 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
804 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
805 return dfloat64_type_node;
806 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
807 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
808 return dfloat32_type_node;
811 /* Deal with fixed-point types. */
812 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
814 unsigned int unsignedp = 0, satp = 0;
815 scalar_mode m1, m2;
816 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
818 m1 = SCALAR_TYPE_MODE (t1);
819 m2 = SCALAR_TYPE_MODE (t2);
821 /* If one input type is saturating, the result type is saturating. */
822 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
823 satp = 1;
825 /* If both fixed-point types are unsigned, the result type is unsigned.
826 When mixing fixed-point and integer types, follow the sign of the
827 fixed-point type.
828 Otherwise, the result type is signed. */
829 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
830 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
831 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t1))
833 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t2)))
835 unsignedp = 1;
837 /* The result type is signed. */
838 if (unsignedp == 0)
840 /* If the input type is unsigned, we need to convert to the
841 signed type. */
842 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
844 enum mode_class mclass = (enum mode_class) 0;
845 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
846 mclass = MODE_FRACT;
847 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
848 mclass = MODE_ACCUM;
849 else
850 gcc_unreachable ();
851 m1 = as_a <scalar_mode>
852 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
854 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
856 enum mode_class mclass = (enum mode_class) 0;
857 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
858 mclass = MODE_FRACT;
859 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
860 mclass = MODE_ACCUM;
861 else
862 gcc_unreachable ();
863 m2 = as_a <scalar_mode>
864 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
868 if (code1 == FIXED_POINT_TYPE)
870 fbit1 = GET_MODE_FBIT (m1);
871 ibit1 = GET_MODE_IBIT (m1);
873 else
875 fbit1 = 0;
876 /* Signed integers need to subtract one sign bit. */
877 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
880 if (code2 == FIXED_POINT_TYPE)
882 fbit2 = GET_MODE_FBIT (m2);
883 ibit2 = GET_MODE_IBIT (m2);
885 else
887 fbit2 = 0;
888 /* Signed integers need to subtract one sign bit. */
889 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
892 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
893 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
894 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
895 satp);
898 /* Both real or both integers; use the one with greater precision. */
900 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
901 return t1;
902 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
903 return t2;
905 /* Same precision. Prefer long longs to longs to ints when the
906 same precision, following the C99 rules on integer type rank
907 (which are equivalent to the C90 rules for C90 types). */
909 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
911 return long_long_unsigned_type_node;
913 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
914 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
916 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
917 return long_long_unsigned_type_node;
918 else
919 return long_long_integer_type_node;
922 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
923 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
924 return long_unsigned_type_node;
926 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
927 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
929 /* But preserve unsignedness from the other type,
930 since long cannot hold all the values of an unsigned int. */
931 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
932 return long_unsigned_type_node;
933 else
934 return long_integer_type_node;
937 /* For floating types of the same TYPE_PRECISION (which we here
938 assume means either the same set of values, or sets of values
939 neither a subset of the other, with behavior being undefined in
940 the latter case), follow the rules from TS 18661-3: prefer
941 interchange types _FloatN, then standard types long double,
942 double, float, then extended types _FloatNx. For extended types,
943 check them starting with _Float128x as that seems most consistent
944 in spirit with preferring long double to double; for interchange
945 types, also check in that order for consistency although it's not
946 possible for more than one of them to have the same
947 precision. */
948 tree mv1 = TYPE_MAIN_VARIANT (t1);
949 tree mv2 = TYPE_MAIN_VARIANT (t2);
951 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
952 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
953 return FLOATN_TYPE_NODE (i);
955 /* Likewise, prefer long double to double even if same size. */
956 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
957 return long_double_type_node;
959 /* Likewise, prefer double to float even if same size.
960 We got a couple of embedded targets with 32 bit doubles, and the
961 pdp11 might have 64 bit floats. */
962 if (mv1 == double_type_node || mv2 == double_type_node)
963 return double_type_node;
965 if (mv1 == float_type_node || mv2 == float_type_node)
966 return float_type_node;
968 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
969 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
970 return FLOATNX_TYPE_NODE (i);
972 /* Otherwise prefer the unsigned one. */
974 if (TYPE_UNSIGNED (t1))
975 return t1;
976 else
977 return t2;
980 /* Wrapper around c_common_type that is used by c-common.c and other
981 front end optimizations that remove promotions. ENUMERAL_TYPEs
982 are allowed here and are converted to their compatible integer types.
983 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
984 preferably a non-Boolean type as the common type. */
985 tree
986 common_type (tree t1, tree t2)
988 if (TREE_CODE (t1) == ENUMERAL_TYPE)
989 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
990 if (TREE_CODE (t2) == ENUMERAL_TYPE)
991 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
993 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
994 if (TREE_CODE (t1) == BOOLEAN_TYPE
995 && TREE_CODE (t2) == BOOLEAN_TYPE)
996 return boolean_type_node;
998 /* If either type is BOOLEAN_TYPE, then return the other. */
999 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1000 return t2;
1001 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1002 return t1;
1004 return c_common_type (t1, t2);
1007 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1008 or various other operations. Return 2 if they are compatible
1009 but a warning may be needed if you use them together. */
1012 comptypes (tree type1, tree type2)
1014 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1015 int val;
1017 val = comptypes_internal (type1, type2, NULL, NULL);
1018 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1020 return val;
1023 /* Like comptypes, but if it returns non-zero because enum and int are
1024 compatible, it sets *ENUM_AND_INT_P to true. */
1026 static int
1027 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1029 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1030 int val;
1032 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1033 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1035 return val;
1038 /* Like comptypes, but if it returns nonzero for different types, it
1039 sets *DIFFERENT_TYPES_P to true. */
1042 comptypes_check_different_types (tree type1, tree type2,
1043 bool *different_types_p)
1045 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1046 int val;
1048 val = comptypes_internal (type1, type2, NULL, different_types_p);
1049 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1051 return val;
1054 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1055 or various other operations. Return 2 if they are compatible
1056 but a warning may be needed if you use them together. If
1057 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1058 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1059 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1060 NULL, and the types are compatible but different enough not to be
1061 permitted in C11 typedef redeclarations, then this sets
1062 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1063 false, but may or may not be set if the types are incompatible.
1064 This differs from comptypes, in that we don't free the seen
1065 types. */
1067 static int
1068 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1069 bool *different_types_p)
1071 const_tree t1 = type1;
1072 const_tree t2 = type2;
1073 int attrval, val;
1075 /* Suppress errors caused by previously reported errors. */
1077 if (t1 == t2 || !t1 || !t2
1078 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1079 return 1;
1081 /* Enumerated types are compatible with integer types, but this is
1082 not transitive: two enumerated types in the same translation unit
1083 are compatible with each other only if they are the same type. */
1085 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1087 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1088 if (TREE_CODE (t2) != VOID_TYPE)
1090 if (enum_and_int_p != NULL)
1091 *enum_and_int_p = true;
1092 if (different_types_p != NULL)
1093 *different_types_p = true;
1096 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1098 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1099 if (TREE_CODE (t1) != VOID_TYPE)
1101 if (enum_and_int_p != NULL)
1102 *enum_and_int_p = true;
1103 if (different_types_p != NULL)
1104 *different_types_p = true;
1108 if (t1 == t2)
1109 return 1;
1111 /* Different classes of types can't be compatible. */
1113 if (TREE_CODE (t1) != TREE_CODE (t2))
1114 return 0;
1116 /* Qualifiers must match. C99 6.7.3p9 */
1118 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1119 return 0;
1121 /* Allow for two different type nodes which have essentially the same
1122 definition. Note that we already checked for equality of the type
1123 qualifiers (just above). */
1125 if (TREE_CODE (t1) != ARRAY_TYPE
1126 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1127 return 1;
1129 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1130 if (!(attrval = comp_type_attributes (t1, t2)))
1131 return 0;
1133 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1134 val = 0;
1136 switch (TREE_CODE (t1))
1138 case INTEGER_TYPE:
1139 case FIXED_POINT_TYPE:
1140 case REAL_TYPE:
1141 /* With these nodes, we can't determine type equivalence by
1142 looking at what is stored in the nodes themselves, because
1143 two nodes might have different TYPE_MAIN_VARIANTs but still
1144 represent the same type. For example, wchar_t and int could
1145 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1146 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1147 and are distinct types. On the other hand, int and the
1148 following typedef
1150 typedef int INT __attribute((may_alias));
1152 have identical properties, different TYPE_MAIN_VARIANTs, but
1153 represent the same type. The canonical type system keeps
1154 track of equivalence in this case, so we fall back on it. */
1155 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1157 case POINTER_TYPE:
1158 /* Do not remove mode information. */
1159 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1160 break;
1161 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1162 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1163 enum_and_int_p, different_types_p));
1164 break;
1166 case FUNCTION_TYPE:
1167 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1168 different_types_p);
1169 break;
1171 case ARRAY_TYPE:
1173 tree d1 = TYPE_DOMAIN (t1);
1174 tree d2 = TYPE_DOMAIN (t2);
1175 bool d1_variable, d2_variable;
1176 bool d1_zero, d2_zero;
1177 val = 1;
1179 /* Target types must match incl. qualifiers. */
1180 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1181 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1182 enum_and_int_p,
1183 different_types_p)))
1184 return 0;
1186 if (different_types_p != NULL
1187 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1188 *different_types_p = true;
1189 /* Sizes must match unless one is missing or variable. */
1190 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1191 break;
1193 d1_zero = !TYPE_MAX_VALUE (d1);
1194 d2_zero = !TYPE_MAX_VALUE (d2);
1196 d1_variable = (!d1_zero
1197 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1198 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1199 d2_variable = (!d2_zero
1200 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1201 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1202 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1203 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1205 if (different_types_p != NULL
1206 && d1_variable != d2_variable)
1207 *different_types_p = true;
1208 if (d1_variable || d2_variable)
1209 break;
1210 if (d1_zero && d2_zero)
1211 break;
1212 if (d1_zero || d2_zero
1213 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1214 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1215 val = 0;
1217 break;
1220 case ENUMERAL_TYPE:
1221 case RECORD_TYPE:
1222 case UNION_TYPE:
1223 if (val != 1 && !same_translation_unit_p (t1, t2))
1225 tree a1 = TYPE_ATTRIBUTES (t1);
1226 tree a2 = TYPE_ATTRIBUTES (t2);
1228 if (! attribute_list_contained (a1, a2)
1229 && ! attribute_list_contained (a2, a1))
1230 break;
1232 if (attrval != 2)
1233 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1234 different_types_p);
1235 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1236 different_types_p);
1238 break;
1240 case VECTOR_TYPE:
1241 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1242 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1243 enum_and_int_p, different_types_p));
1244 break;
1246 default:
1247 break;
1249 return attrval == 2 && val == 1 ? 2 : val;
1252 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1253 their qualifiers, except for named address spaces. If the pointers point to
1254 different named addresses, then we must determine if one address space is a
1255 subset of the other. */
1257 static int
1258 comp_target_types (location_t location, tree ttl, tree ttr)
1260 int val;
1261 int val_ped;
1262 tree mvl = TREE_TYPE (ttl);
1263 tree mvr = TREE_TYPE (ttr);
1264 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1265 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1266 addr_space_t as_common;
1267 bool enum_and_int_p;
1269 /* Fail if pointers point to incompatible address spaces. */
1270 if (!addr_space_superset (asl, asr, &as_common))
1271 return 0;
1273 /* For pedantic record result of comptypes on arrays before losing
1274 qualifiers on the element type below. */
1275 val_ped = 1;
1277 if (TREE_CODE (mvl) == ARRAY_TYPE
1278 && TREE_CODE (mvr) == ARRAY_TYPE)
1279 val_ped = comptypes (mvl, mvr);
1281 /* Qualifiers on element types of array types that are
1282 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1284 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1285 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1286 : TYPE_MAIN_VARIANT (mvl));
1288 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1289 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1290 : TYPE_MAIN_VARIANT (mvr));
1292 enum_and_int_p = false;
1293 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1295 if (val == 1 && val_ped != 1)
1296 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1297 "are incompatible in ISO C");
1299 if (val == 2)
1300 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1302 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1303 warning_at (location, OPT_Wc___compat,
1304 "pointer target types incompatible in C++");
1306 return val;
1309 /* Subroutines of `comptypes'. */
1311 /* Determine whether two trees derive from the same translation unit.
1312 If the CONTEXT chain ends in a null, that tree's context is still
1313 being parsed, so if two trees have context chains ending in null,
1314 they're in the same translation unit. */
1316 bool
1317 same_translation_unit_p (const_tree t1, const_tree t2)
1319 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1320 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1322 case tcc_declaration:
1323 t1 = DECL_CONTEXT (t1); break;
1324 case tcc_type:
1325 t1 = TYPE_CONTEXT (t1); break;
1326 case tcc_exceptional:
1327 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1328 default: gcc_unreachable ();
1331 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1332 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1334 case tcc_declaration:
1335 t2 = DECL_CONTEXT (t2); break;
1336 case tcc_type:
1337 t2 = TYPE_CONTEXT (t2); break;
1338 case tcc_exceptional:
1339 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1340 default: gcc_unreachable ();
1343 return t1 == t2;
1346 /* Allocate the seen two types, assuming that they are compatible. */
1348 static struct tagged_tu_seen_cache *
1349 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1351 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1352 tu->next = tagged_tu_seen_base;
1353 tu->t1 = t1;
1354 tu->t2 = t2;
1356 tagged_tu_seen_base = tu;
1358 /* The C standard says that two structures in different translation
1359 units are compatible with each other only if the types of their
1360 fields are compatible (among other things). We assume that they
1361 are compatible until proven otherwise when building the cache.
1362 An example where this can occur is:
1363 struct a
1365 struct a *next;
1367 If we are comparing this against a similar struct in another TU,
1368 and did not assume they were compatible, we end up with an infinite
1369 loop. */
1370 tu->val = 1;
1371 return tu;
1374 /* Free the seen types until we get to TU_TIL. */
1376 static void
1377 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1379 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1380 while (tu != tu_til)
1382 const struct tagged_tu_seen_cache *const tu1
1383 = (const struct tagged_tu_seen_cache *) tu;
1384 tu = tu1->next;
1385 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1387 tagged_tu_seen_base = tu_til;
1390 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1391 compatible. If the two types are not the same (which has been
1392 checked earlier), this can only happen when multiple translation
1393 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1394 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1395 comptypes_internal. */
1397 static int
1398 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1399 bool *enum_and_int_p, bool *different_types_p)
1401 tree s1, s2;
1402 bool needs_warning = false;
1404 /* We have to verify that the tags of the types are the same. This
1405 is harder than it looks because this may be a typedef, so we have
1406 to go look at the original type. It may even be a typedef of a
1407 typedef...
1408 In the case of compiler-created builtin structs the TYPE_DECL
1409 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1410 while (TYPE_NAME (t1)
1411 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1412 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1413 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1415 while (TYPE_NAME (t2)
1416 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1417 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1418 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1420 /* C90 didn't have the requirement that the two tags be the same. */
1421 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1422 return 0;
1424 /* C90 didn't say what happened if one or both of the types were
1425 incomplete; we choose to follow C99 rules here, which is that they
1426 are compatible. */
1427 if (TYPE_SIZE (t1) == NULL
1428 || TYPE_SIZE (t2) == NULL)
1429 return 1;
1432 const struct tagged_tu_seen_cache * tts_i;
1433 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1434 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1435 return tts_i->val;
1438 switch (TREE_CODE (t1))
1440 case ENUMERAL_TYPE:
1442 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1443 /* Speed up the case where the type values are in the same order. */
1444 tree tv1 = TYPE_VALUES (t1);
1445 tree tv2 = TYPE_VALUES (t2);
1447 if (tv1 == tv2)
1449 return 1;
1452 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1454 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1455 break;
1456 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1458 tu->val = 0;
1459 return 0;
1463 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1465 return 1;
1467 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1469 tu->val = 0;
1470 return 0;
1473 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1475 tu->val = 0;
1476 return 0;
1479 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1481 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1482 if (s2 == NULL
1483 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1485 tu->val = 0;
1486 return 0;
1489 return 1;
1492 case UNION_TYPE:
1494 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1495 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1497 tu->val = 0;
1498 return 0;
1501 /* Speed up the common case where the fields are in the same order. */
1502 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1503 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1505 int result;
1507 if (DECL_NAME (s1) != DECL_NAME (s2))
1508 break;
1509 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1510 enum_and_int_p, different_types_p);
1512 if (result != 1 && !DECL_NAME (s1))
1513 break;
1514 if (result == 0)
1516 tu->val = 0;
1517 return 0;
1519 if (result == 2)
1520 needs_warning = true;
1522 if (TREE_CODE (s1) == FIELD_DECL
1523 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1524 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1526 tu->val = 0;
1527 return 0;
1530 if (!s1 && !s2)
1532 tu->val = needs_warning ? 2 : 1;
1533 return tu->val;
1536 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1538 bool ok = false;
1540 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1541 if (DECL_NAME (s1) == DECL_NAME (s2))
1543 int result;
1545 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1546 enum_and_int_p,
1547 different_types_p);
1549 if (result != 1 && !DECL_NAME (s1))
1550 continue;
1551 if (result == 0)
1553 tu->val = 0;
1554 return 0;
1556 if (result == 2)
1557 needs_warning = true;
1559 if (TREE_CODE (s1) == FIELD_DECL
1560 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1561 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1562 break;
1564 ok = true;
1565 break;
1567 if (!ok)
1569 tu->val = 0;
1570 return 0;
1573 tu->val = needs_warning ? 2 : 10;
1574 return tu->val;
1577 case RECORD_TYPE:
1579 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1581 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1582 s1 && s2;
1583 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1585 int result;
1586 if (TREE_CODE (s1) != TREE_CODE (s2)
1587 || DECL_NAME (s1) != DECL_NAME (s2))
1588 break;
1589 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1590 enum_and_int_p, different_types_p);
1591 if (result == 0)
1592 break;
1593 if (result == 2)
1594 needs_warning = true;
1596 if (TREE_CODE (s1) == FIELD_DECL
1597 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1598 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1599 break;
1601 if (s1 && s2)
1602 tu->val = 0;
1603 else
1604 tu->val = needs_warning ? 2 : 1;
1605 return tu->val;
1608 default:
1609 gcc_unreachable ();
1613 /* Return 1 if two function types F1 and F2 are compatible.
1614 If either type specifies no argument types,
1615 the other must specify a fixed number of self-promoting arg types.
1616 Otherwise, if one type specifies only the number of arguments,
1617 the other must specify that number of self-promoting arg types.
1618 Otherwise, the argument types must match.
1619 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1621 static int
1622 function_types_compatible_p (const_tree f1, const_tree f2,
1623 bool *enum_and_int_p, bool *different_types_p)
1625 tree args1, args2;
1626 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1627 int val = 1;
1628 int val1;
1629 tree ret1, ret2;
1631 ret1 = TREE_TYPE (f1);
1632 ret2 = TREE_TYPE (f2);
1634 /* 'volatile' qualifiers on a function's return type used to mean
1635 the function is noreturn. */
1636 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1637 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1638 if (TYPE_VOLATILE (ret1))
1639 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1640 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1641 if (TYPE_VOLATILE (ret2))
1642 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1643 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1644 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1645 if (val == 0)
1646 return 0;
1648 args1 = TYPE_ARG_TYPES (f1);
1649 args2 = TYPE_ARG_TYPES (f2);
1651 if (different_types_p != NULL
1652 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1653 *different_types_p = true;
1655 /* An unspecified parmlist matches any specified parmlist
1656 whose argument types don't need default promotions. */
1658 if (args1 == NULL_TREE)
1660 if (!self_promoting_args_p (args2))
1661 return 0;
1662 /* If one of these types comes from a non-prototype fn definition,
1663 compare that with the other type's arglist.
1664 If they don't match, ask for a warning (but no error). */
1665 if (TYPE_ACTUAL_ARG_TYPES (f1)
1666 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1667 enum_and_int_p, different_types_p))
1668 val = 2;
1669 return val;
1671 if (args2 == NULL_TREE)
1673 if (!self_promoting_args_p (args1))
1674 return 0;
1675 if (TYPE_ACTUAL_ARG_TYPES (f2)
1676 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1677 enum_and_int_p, different_types_p))
1678 val = 2;
1679 return val;
1682 /* Both types have argument lists: compare them and propagate results. */
1683 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1684 different_types_p);
1685 return val1 != 1 ? val1 : val;
1688 /* Check two lists of types for compatibility, returning 0 for
1689 incompatible, 1 for compatible, or 2 for compatible with
1690 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1691 comptypes_internal. */
1693 static int
1694 type_lists_compatible_p (const_tree args1, const_tree args2,
1695 bool *enum_and_int_p, bool *different_types_p)
1697 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1698 int val = 1;
1699 int newval = 0;
1701 while (1)
1703 tree a1, mv1, a2, mv2;
1704 if (args1 == NULL_TREE && args2 == NULL_TREE)
1705 return val;
1706 /* If one list is shorter than the other,
1707 they fail to match. */
1708 if (args1 == NULL_TREE || args2 == NULL_TREE)
1709 return 0;
1710 mv1 = a1 = TREE_VALUE (args1);
1711 mv2 = a2 = TREE_VALUE (args2);
1712 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1713 mv1 = (TYPE_ATOMIC (mv1)
1714 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1715 TYPE_QUAL_ATOMIC)
1716 : TYPE_MAIN_VARIANT (mv1));
1717 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1718 mv2 = (TYPE_ATOMIC (mv2)
1719 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1720 TYPE_QUAL_ATOMIC)
1721 : TYPE_MAIN_VARIANT (mv2));
1722 /* A null pointer instead of a type
1723 means there is supposed to be an argument
1724 but nothing is specified about what type it has.
1725 So match anything that self-promotes. */
1726 if (different_types_p != NULL
1727 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1728 *different_types_p = true;
1729 if (a1 == NULL_TREE)
1731 if (c_type_promotes_to (a2) != a2)
1732 return 0;
1734 else if (a2 == NULL_TREE)
1736 if (c_type_promotes_to (a1) != a1)
1737 return 0;
1739 /* If one of the lists has an error marker, ignore this arg. */
1740 else if (TREE_CODE (a1) == ERROR_MARK
1741 || TREE_CODE (a2) == ERROR_MARK)
1743 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1744 different_types_p)))
1746 if (different_types_p != NULL)
1747 *different_types_p = true;
1748 /* Allow wait (union {union wait *u; int *i} *)
1749 and wait (union wait *) to be compatible. */
1750 if (TREE_CODE (a1) == UNION_TYPE
1751 && (TYPE_NAME (a1) == NULL_TREE
1752 || TYPE_TRANSPARENT_AGGR (a1))
1753 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1754 && tree_int_cst_equal (TYPE_SIZE (a1),
1755 TYPE_SIZE (a2)))
1757 tree memb;
1758 for (memb = TYPE_FIELDS (a1);
1759 memb; memb = DECL_CHAIN (memb))
1761 tree mv3 = TREE_TYPE (memb);
1762 if (mv3 && mv3 != error_mark_node
1763 && TREE_CODE (mv3) != ARRAY_TYPE)
1764 mv3 = (TYPE_ATOMIC (mv3)
1765 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1766 TYPE_QUAL_ATOMIC)
1767 : TYPE_MAIN_VARIANT (mv3));
1768 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1769 different_types_p))
1770 break;
1772 if (memb == NULL_TREE)
1773 return 0;
1775 else if (TREE_CODE (a2) == UNION_TYPE
1776 && (TYPE_NAME (a2) == NULL_TREE
1777 || TYPE_TRANSPARENT_AGGR (a2))
1778 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1779 && tree_int_cst_equal (TYPE_SIZE (a2),
1780 TYPE_SIZE (a1)))
1782 tree memb;
1783 for (memb = TYPE_FIELDS (a2);
1784 memb; memb = DECL_CHAIN (memb))
1786 tree mv3 = TREE_TYPE (memb);
1787 if (mv3 && mv3 != error_mark_node
1788 && TREE_CODE (mv3) != ARRAY_TYPE)
1789 mv3 = (TYPE_ATOMIC (mv3)
1790 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1791 TYPE_QUAL_ATOMIC)
1792 : TYPE_MAIN_VARIANT (mv3));
1793 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1794 different_types_p))
1795 break;
1797 if (memb == NULL_TREE)
1798 return 0;
1800 else
1801 return 0;
1804 /* comptypes said ok, but record if it said to warn. */
1805 if (newval > val)
1806 val = newval;
1808 args1 = TREE_CHAIN (args1);
1809 args2 = TREE_CHAIN (args2);
1813 /* Compute the size to increment a pointer by. When a function type or void
1814 type or incomplete type is passed, size_one_node is returned.
1815 This function does not emit any diagnostics; the caller is responsible
1816 for that. */
1818 static tree
1819 c_size_in_bytes (const_tree type)
1821 enum tree_code code = TREE_CODE (type);
1823 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1824 || !COMPLETE_TYPE_P (type))
1825 return size_one_node;
1827 /* Convert in case a char is more than one unit. */
1828 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1829 size_int (TYPE_PRECISION (char_type_node)
1830 / BITS_PER_UNIT));
1833 /* Return either DECL or its known constant value (if it has one). */
1835 tree
1836 decl_constant_value (tree decl)
1838 if (/* Don't change a variable array bound or initial value to a constant
1839 in a place where a variable is invalid. Note that DECL_INITIAL
1840 isn't valid for a PARM_DECL. */
1841 current_function_decl != NULL_TREE
1842 && TREE_CODE (decl) != PARM_DECL
1843 && !TREE_THIS_VOLATILE (decl)
1844 && TREE_READONLY (decl)
1845 && DECL_INITIAL (decl) != NULL_TREE
1846 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1847 /* This is invalid if initial value is not constant.
1848 If it has either a function call, a memory reference,
1849 or a variable, then re-evaluating it could give different results. */
1850 && TREE_CONSTANT (DECL_INITIAL (decl))
1851 /* Check for cases where this is sub-optimal, even though valid. */
1852 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1853 return DECL_INITIAL (decl);
1854 return decl;
1857 /* Convert the array expression EXP to a pointer. */
1858 static tree
1859 array_to_pointer_conversion (location_t loc, tree exp)
1861 tree orig_exp = exp;
1862 tree type = TREE_TYPE (exp);
1863 tree adr;
1864 tree restype = TREE_TYPE (type);
1865 tree ptrtype;
1867 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1869 STRIP_TYPE_NOPS (exp);
1871 if (TREE_NO_WARNING (orig_exp))
1872 TREE_NO_WARNING (exp) = 1;
1874 ptrtype = build_pointer_type (restype);
1876 if (INDIRECT_REF_P (exp))
1877 return convert (ptrtype, TREE_OPERAND (exp, 0));
1879 /* In C++ array compound literals are temporary objects unless they are
1880 const or appear in namespace scope, so they are destroyed too soon
1881 to use them for much of anything (c++/53220). */
1882 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1884 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1885 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1886 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1887 "converting an array compound literal to a pointer "
1888 "is ill-formed in C++");
1891 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1892 return convert (ptrtype, adr);
1895 /* Convert the function expression EXP to a pointer. */
1896 static tree
1897 function_to_pointer_conversion (location_t loc, tree exp)
1899 tree orig_exp = exp;
1901 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1903 STRIP_TYPE_NOPS (exp);
1905 if (TREE_NO_WARNING (orig_exp))
1906 TREE_NO_WARNING (exp) = 1;
1908 return build_unary_op (loc, ADDR_EXPR, exp, false);
1911 /* Mark EXP as read, not just set, for set but not used -Wunused
1912 warning purposes. */
1914 void
1915 mark_exp_read (tree exp)
1917 switch (TREE_CODE (exp))
1919 case VAR_DECL:
1920 case PARM_DECL:
1921 DECL_READ_P (exp) = 1;
1922 break;
1923 case ARRAY_REF:
1924 case COMPONENT_REF:
1925 case MODIFY_EXPR:
1926 case REALPART_EXPR:
1927 case IMAGPART_EXPR:
1928 CASE_CONVERT:
1929 case ADDR_EXPR:
1930 case VIEW_CONVERT_EXPR:
1931 mark_exp_read (TREE_OPERAND (exp, 0));
1932 break;
1933 case COMPOUND_EXPR:
1934 case C_MAYBE_CONST_EXPR:
1935 mark_exp_read (TREE_OPERAND (exp, 1));
1936 break;
1937 default:
1938 break;
1942 /* Perform the default conversion of arrays and functions to pointers.
1943 Return the result of converting EXP. For any other expression, just
1944 return EXP.
1946 LOC is the location of the expression. */
1948 struct c_expr
1949 default_function_array_conversion (location_t loc, struct c_expr exp)
1951 tree orig_exp = exp.value;
1952 tree type = TREE_TYPE (exp.value);
1953 enum tree_code code = TREE_CODE (type);
1955 switch (code)
1957 case ARRAY_TYPE:
1959 bool not_lvalue = false;
1960 bool lvalue_array_p;
1962 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1963 || CONVERT_EXPR_P (exp.value))
1964 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1966 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1967 not_lvalue = true;
1968 exp.value = TREE_OPERAND (exp.value, 0);
1971 if (TREE_NO_WARNING (orig_exp))
1972 TREE_NO_WARNING (exp.value) = 1;
1974 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1975 if (!flag_isoc99 && !lvalue_array_p)
1977 /* Before C99, non-lvalue arrays do not decay to pointers.
1978 Normally, using such an array would be invalid; but it can
1979 be used correctly inside sizeof or as a statement expression.
1980 Thus, do not give an error here; an error will result later. */
1981 return exp;
1984 exp.value = array_to_pointer_conversion (loc, exp.value);
1986 break;
1987 case FUNCTION_TYPE:
1988 exp.value = function_to_pointer_conversion (loc, exp.value);
1989 break;
1990 default:
1991 break;
1994 return exp;
1997 struct c_expr
1998 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2000 mark_exp_read (exp.value);
2001 return default_function_array_conversion (loc, exp);
2004 /* Return whether EXPR should be treated as an atomic lvalue for the
2005 purposes of load and store handling. */
2007 static bool
2008 really_atomic_lvalue (tree expr)
2010 if (error_operand_p (expr))
2011 return false;
2012 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2013 return false;
2014 if (!lvalue_p (expr))
2015 return false;
2017 /* Ignore _Atomic on register variables, since their addresses can't
2018 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2019 sequences wouldn't work. Ignore _Atomic on structures containing
2020 bit-fields, since accessing elements of atomic structures or
2021 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2022 it's undefined at translation time or execution time, and the
2023 normal atomic sequences again wouldn't work. */
2024 while (handled_component_p (expr))
2026 if (TREE_CODE (expr) == COMPONENT_REF
2027 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2028 return false;
2029 expr = TREE_OPERAND (expr, 0);
2031 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2032 return false;
2033 return true;
2036 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2037 including converting functions and arrays to pointers if CONVERT_P.
2038 If READ_P, also mark the expression as having been read. */
2040 struct c_expr
2041 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2042 bool convert_p, bool read_p)
2044 if (read_p)
2045 mark_exp_read (exp.value);
2046 if (convert_p)
2047 exp = default_function_array_conversion (loc, exp);
2048 if (really_atomic_lvalue (exp.value))
2050 vec<tree, va_gc> *params;
2051 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2052 tree expr_type = TREE_TYPE (exp.value);
2053 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2054 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2056 gcc_assert (TYPE_ATOMIC (expr_type));
2058 /* Expansion of a generic atomic load may require an addition
2059 element, so allocate enough to prevent a resize. */
2060 vec_alloc (params, 4);
2062 /* Remove the qualifiers for the rest of the expressions and
2063 create the VAL temp variable to hold the RHS. */
2064 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2065 tmp = create_tmp_var_raw (nonatomic_type);
2066 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2067 TREE_ADDRESSABLE (tmp) = 1;
2068 TREE_NO_WARNING (tmp) = 1;
2070 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2071 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2072 params->quick_push (expr_addr);
2073 params->quick_push (tmp_addr);
2074 params->quick_push (seq_cst);
2075 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2077 /* EXPR is always read. */
2078 mark_exp_read (exp.value);
2080 /* Return tmp which contains the value loaded. */
2081 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2082 NULL_TREE, NULL_TREE);
2084 return exp;
2087 /* EXP is an expression of integer type. Apply the integer promotions
2088 to it and return the promoted value. */
2090 tree
2091 perform_integral_promotions (tree exp)
2093 tree type = TREE_TYPE (exp);
2094 enum tree_code code = TREE_CODE (type);
2096 gcc_assert (INTEGRAL_TYPE_P (type));
2098 /* Normally convert enums to int,
2099 but convert wide enums to something wider. */
2100 if (code == ENUMERAL_TYPE)
2102 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2103 TYPE_PRECISION (integer_type_node)),
2104 ((TYPE_PRECISION (type)
2105 >= TYPE_PRECISION (integer_type_node))
2106 && TYPE_UNSIGNED (type)));
2108 return convert (type, exp);
2111 /* ??? This should no longer be needed now bit-fields have their
2112 proper types. */
2113 if (TREE_CODE (exp) == COMPONENT_REF
2114 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2115 /* If it's thinner than an int, promote it like a
2116 c_promoting_integer_type_p, otherwise leave it alone. */
2117 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2118 TYPE_PRECISION (integer_type_node)))
2119 return convert (integer_type_node, exp);
2121 if (c_promoting_integer_type_p (type))
2123 /* Preserve unsignedness if not really getting any wider. */
2124 if (TYPE_UNSIGNED (type)
2125 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2126 return convert (unsigned_type_node, exp);
2128 return convert (integer_type_node, exp);
2131 return exp;
2135 /* Perform default promotions for C data used in expressions.
2136 Enumeral types or short or char are converted to int.
2137 In addition, manifest constants symbols are replaced by their values. */
2139 tree
2140 default_conversion (tree exp)
2142 tree orig_exp;
2143 tree type = TREE_TYPE (exp);
2144 enum tree_code code = TREE_CODE (type);
2145 tree promoted_type;
2147 mark_exp_read (exp);
2149 /* Functions and arrays have been converted during parsing. */
2150 gcc_assert (code != FUNCTION_TYPE);
2151 if (code == ARRAY_TYPE)
2152 return exp;
2154 /* Constants can be used directly unless they're not loadable. */
2155 if (TREE_CODE (exp) == CONST_DECL)
2156 exp = DECL_INITIAL (exp);
2158 /* Strip no-op conversions. */
2159 orig_exp = exp;
2160 STRIP_TYPE_NOPS (exp);
2162 if (TREE_NO_WARNING (orig_exp))
2163 TREE_NO_WARNING (exp) = 1;
2165 if (code == VOID_TYPE)
2167 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2168 "void value not ignored as it ought to be");
2169 return error_mark_node;
2172 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2173 if (exp == error_mark_node)
2174 return error_mark_node;
2176 promoted_type = targetm.promoted_type (type);
2177 if (promoted_type)
2178 return convert (promoted_type, exp);
2180 if (INTEGRAL_TYPE_P (type))
2181 return perform_integral_promotions (exp);
2183 return exp;
2186 /* Look up COMPONENT in a structure or union TYPE.
2188 If the component name is not found, returns NULL_TREE. Otherwise,
2189 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2190 stepping down the chain to the component, which is in the last
2191 TREE_VALUE of the list. Normally the list is of length one, but if
2192 the component is embedded within (nested) anonymous structures or
2193 unions, the list steps down the chain to the component. */
2195 static tree
2196 lookup_field (tree type, tree component)
2198 tree field;
2200 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2201 to the field elements. Use a binary search on this array to quickly
2202 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2203 will always be set for structures which have many elements. */
2205 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2207 int bot, top, half;
2208 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2210 field = TYPE_FIELDS (type);
2211 bot = 0;
2212 top = TYPE_LANG_SPECIFIC (type)->s->len;
2213 while (top - bot > 1)
2215 half = (top - bot + 1) >> 1;
2216 field = field_array[bot+half];
2218 if (DECL_NAME (field) == NULL_TREE)
2220 /* Step through all anon unions in linear fashion. */
2221 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2223 field = field_array[bot++];
2224 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2226 tree anon = lookup_field (TREE_TYPE (field), component);
2228 if (anon)
2229 return tree_cons (NULL_TREE, field, anon);
2231 /* The Plan 9 compiler permits referring
2232 directly to an anonymous struct/union field
2233 using a typedef name. */
2234 if (flag_plan9_extensions
2235 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2236 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2237 == TYPE_DECL)
2238 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2239 == component))
2240 break;
2244 /* Entire record is only anon unions. */
2245 if (bot > top)
2246 return NULL_TREE;
2248 /* Restart the binary search, with new lower bound. */
2249 continue;
2252 if (DECL_NAME (field) == component)
2253 break;
2254 if (DECL_NAME (field) < component)
2255 bot += half;
2256 else
2257 top = bot + half;
2260 if (DECL_NAME (field_array[bot]) == component)
2261 field = field_array[bot];
2262 else if (DECL_NAME (field) != component)
2263 return NULL_TREE;
2265 else
2267 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2269 if (DECL_NAME (field) == NULL_TREE
2270 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2272 tree anon = lookup_field (TREE_TYPE (field), component);
2274 if (anon)
2275 return tree_cons (NULL_TREE, field, anon);
2277 /* The Plan 9 compiler permits referring directly to an
2278 anonymous struct/union field using a typedef
2279 name. */
2280 if (flag_plan9_extensions
2281 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2282 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2283 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2284 == component))
2285 break;
2288 if (DECL_NAME (field) == component)
2289 break;
2292 if (field == NULL_TREE)
2293 return NULL_TREE;
2296 return tree_cons (NULL_TREE, field, NULL_TREE);
2299 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2301 static void
2302 lookup_field_fuzzy_find_candidates (tree type, tree component,
2303 vec<tree> *candidates)
2305 tree field;
2306 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2308 if (DECL_NAME (field) == NULL_TREE
2309 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2310 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2311 candidates);
2313 if (DECL_NAME (field))
2314 candidates->safe_push (DECL_NAME (field));
2318 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2319 rather than returning a TREE_LIST for an exact match. */
2321 static tree
2322 lookup_field_fuzzy (tree type, tree component)
2324 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2326 /* First, gather a list of candidates. */
2327 auto_vec <tree> candidates;
2329 lookup_field_fuzzy_find_candidates (type, component,
2330 &candidates);
2332 return find_closest_identifier (component, &candidates);
2335 /* Support function for build_component_ref's error-handling.
2337 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2338 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2340 static bool
2341 should_suggest_deref_p (tree datum_type)
2343 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2344 allows "." for ptrs; we could be handling a failed attempt
2345 to access a property. */
2346 if (c_dialect_objc ())
2347 return false;
2349 /* Only suggest it for pointers... */
2350 if (TREE_CODE (datum_type) != POINTER_TYPE)
2351 return false;
2353 /* ...to structs/unions. */
2354 tree underlying_type = TREE_TYPE (datum_type);
2355 enum tree_code code = TREE_CODE (underlying_type);
2356 if (code == RECORD_TYPE || code == UNION_TYPE)
2357 return true;
2358 else
2359 return false;
2362 /* Make an expression to refer to the COMPONENT field of structure or
2363 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2364 location of the COMPONENT_REF. COMPONENT_LOC is the location
2365 of COMPONENT. */
2367 tree
2368 build_component_ref (location_t loc, tree datum, tree component,
2369 location_t component_loc)
2371 tree type = TREE_TYPE (datum);
2372 enum tree_code code = TREE_CODE (type);
2373 tree field = NULL;
2374 tree ref;
2375 bool datum_lvalue = lvalue_p (datum);
2377 if (!objc_is_public (datum, component))
2378 return error_mark_node;
2380 /* Detect Objective-C property syntax object.property. */
2381 if (c_dialect_objc ()
2382 && (ref = objc_maybe_build_component_ref (datum, component)))
2383 return ref;
2385 /* See if there is a field or component with name COMPONENT. */
2387 if (code == RECORD_TYPE || code == UNION_TYPE)
2389 if (!COMPLETE_TYPE_P (type))
2391 c_incomplete_type_error (loc, NULL_TREE, type);
2392 return error_mark_node;
2395 field = lookup_field (type, component);
2397 if (!field)
2399 tree guessed_id = lookup_field_fuzzy (type, component);
2400 if (guessed_id)
2402 /* Attempt to provide a fixit replacement hint, if
2403 we have a valid range for the component. */
2404 location_t reported_loc
2405 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2406 gcc_rich_location rich_loc (reported_loc);
2407 if (component_loc != UNKNOWN_LOCATION)
2408 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2409 error_at_rich_loc
2410 (&rich_loc,
2411 "%qT has no member named %qE; did you mean %qE?",
2412 type, component, guessed_id);
2414 else
2415 error_at (loc, "%qT has no member named %qE", type, component);
2416 return error_mark_node;
2419 /* Accessing elements of atomic structures or unions is undefined
2420 behavior (C11 6.5.2.3#5). */
2421 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2423 if (code == RECORD_TYPE)
2424 warning_at (loc, 0, "accessing a member %qE of an atomic "
2425 "structure %qE", component, datum);
2426 else
2427 warning_at (loc, 0, "accessing a member %qE of an atomic "
2428 "union %qE", component, datum);
2431 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2432 This might be better solved in future the way the C++ front
2433 end does it - by giving the anonymous entities each a
2434 separate name and type, and then have build_component_ref
2435 recursively call itself. We can't do that here. */
2438 tree subdatum = TREE_VALUE (field);
2439 int quals;
2440 tree subtype;
2441 bool use_datum_quals;
2443 if (TREE_TYPE (subdatum) == error_mark_node)
2444 return error_mark_node;
2446 /* If this is an rvalue, it does not have qualifiers in C
2447 standard terms and we must avoid propagating such
2448 qualifiers down to a non-lvalue array that is then
2449 converted to a pointer. */
2450 use_datum_quals = (datum_lvalue
2451 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2453 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2454 if (use_datum_quals)
2455 quals |= TYPE_QUALS (TREE_TYPE (datum));
2456 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2458 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2459 NULL_TREE);
2460 SET_EXPR_LOCATION (ref, loc);
2461 if (TREE_READONLY (subdatum)
2462 || (use_datum_quals && TREE_READONLY (datum)))
2463 TREE_READONLY (ref) = 1;
2464 if (TREE_THIS_VOLATILE (subdatum)
2465 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2466 TREE_THIS_VOLATILE (ref) = 1;
2468 if (TREE_DEPRECATED (subdatum))
2469 warn_deprecated_use (subdatum, NULL_TREE);
2471 datum = ref;
2473 field = TREE_CHAIN (field);
2475 while (field);
2477 return ref;
2479 else if (should_suggest_deref_p (type))
2481 /* Special-case the error message for "ptr.field" for the case
2482 where the user has confused "." vs "->". */
2483 rich_location richloc (line_table, loc);
2484 /* "loc" should be the "." token. */
2485 richloc.add_fixit_replace ("->");
2486 error_at_rich_loc (&richloc,
2487 "%qE is a pointer; did you mean to use %<->%>?",
2488 datum);
2489 return error_mark_node;
2491 else if (code != ERROR_MARK)
2492 error_at (loc,
2493 "request for member %qE in something not a structure or union",
2494 component);
2496 return error_mark_node;
2499 /* Given an expression PTR for a pointer, return an expression
2500 for the value pointed to.
2501 ERRORSTRING is the name of the operator to appear in error messages.
2503 LOC is the location to use for the generated tree. */
2505 tree
2506 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2508 tree pointer = default_conversion (ptr);
2509 tree type = TREE_TYPE (pointer);
2510 tree ref;
2512 if (TREE_CODE (type) == POINTER_TYPE)
2514 if (CONVERT_EXPR_P (pointer)
2515 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2517 /* If a warning is issued, mark it to avoid duplicates from
2518 the backend. This only needs to be done at
2519 warn_strict_aliasing > 2. */
2520 if (warn_strict_aliasing > 2)
2521 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2522 type, TREE_OPERAND (pointer, 0)))
2523 TREE_NO_WARNING (pointer) = 1;
2526 if (TREE_CODE (pointer) == ADDR_EXPR
2527 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2528 == TREE_TYPE (type)))
2530 ref = TREE_OPERAND (pointer, 0);
2531 protected_set_expr_location (ref, loc);
2532 return ref;
2534 else
2536 tree t = TREE_TYPE (type);
2538 ref = build1 (INDIRECT_REF, t, pointer);
2540 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2542 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2544 error_at (loc, "dereferencing pointer to incomplete type "
2545 "%qT", t);
2546 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2548 return error_mark_node;
2550 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2551 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2553 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2554 so that we get the proper error message if the result is used
2555 to assign to. Also, &* is supposed to be a no-op.
2556 And ANSI C seems to specify that the type of the result
2557 should be the const type. */
2558 /* A de-reference of a pointer to const is not a const. It is valid
2559 to change it via some other pointer. */
2560 TREE_READONLY (ref) = TYPE_READONLY (t);
2561 TREE_SIDE_EFFECTS (ref)
2562 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2563 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2564 protected_set_expr_location (ref, loc);
2565 return ref;
2568 else if (TREE_CODE (pointer) != ERROR_MARK)
2569 invalid_indirection_error (loc, type, errstring);
2571 return error_mark_node;
2574 /* This handles expressions of the form "a[i]", which denotes
2575 an array reference.
2577 This is logically equivalent in C to *(a+i), but we may do it differently.
2578 If A is a variable or a member, we generate a primitive ARRAY_REF.
2579 This avoids forcing the array out of registers, and can work on
2580 arrays that are not lvalues (for example, members of structures returned
2581 by functions).
2583 For vector types, allow vector[i] but not i[vector], and create
2584 *(((type*)&vectortype) + i) for the expression.
2586 LOC is the location to use for the returned expression. */
2588 tree
2589 build_array_ref (location_t loc, tree array, tree index)
2591 tree ret;
2592 bool swapped = false;
2593 if (TREE_TYPE (array) == error_mark_node
2594 || TREE_TYPE (index) == error_mark_node)
2595 return error_mark_node;
2597 if (flag_cilkplus && contains_array_notation_expr (index))
2599 size_t rank = 0;
2600 if (!find_rank (loc, index, index, true, &rank))
2601 return error_mark_node;
2602 if (rank > 1)
2604 error_at (loc, "rank of the array's index is greater than 1");
2605 return error_mark_node;
2608 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2609 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2610 /* Allow vector[index] but not index[vector]. */
2611 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2613 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2614 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2616 error_at (loc,
2617 "subscripted value is neither array nor pointer nor vector");
2619 return error_mark_node;
2621 std::swap (array, index);
2622 swapped = true;
2625 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2627 error_at (loc, "array subscript is not an integer");
2628 return error_mark_node;
2631 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2633 error_at (loc, "subscripted value is pointer to function");
2634 return error_mark_node;
2637 /* ??? Existing practice has been to warn only when the char
2638 index is syntactically the index, not for char[array]. */
2639 if (!swapped)
2640 warn_array_subscript_with_type_char (loc, index);
2642 /* Apply default promotions *after* noticing character types. */
2643 index = default_conversion (index);
2644 if (index == error_mark_node)
2645 return error_mark_node;
2647 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2649 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2650 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2652 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2654 tree rval, type;
2656 /* An array that is indexed by a non-constant
2657 cannot be stored in a register; we must be able to do
2658 address arithmetic on its address.
2659 Likewise an array of elements of variable size. */
2660 if (TREE_CODE (index) != INTEGER_CST
2661 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2662 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2664 if (!c_mark_addressable (array, true))
2665 return error_mark_node;
2667 /* An array that is indexed by a constant value which is not within
2668 the array bounds cannot be stored in a register either; because we
2669 would get a crash in store_bit_field/extract_bit_field when trying
2670 to access a non-existent part of the register. */
2671 if (TREE_CODE (index) == INTEGER_CST
2672 && TYPE_DOMAIN (TREE_TYPE (array))
2673 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2675 if (!c_mark_addressable (array))
2676 return error_mark_node;
2679 if ((pedantic || warn_c90_c99_compat)
2680 && ! was_vector)
2682 tree foo = array;
2683 while (TREE_CODE (foo) == COMPONENT_REF)
2684 foo = TREE_OPERAND (foo, 0);
2685 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2686 pedwarn (loc, OPT_Wpedantic,
2687 "ISO C forbids subscripting %<register%> array");
2688 else if (!lvalue_p (foo))
2689 pedwarn_c90 (loc, OPT_Wpedantic,
2690 "ISO C90 forbids subscripting non-lvalue "
2691 "array");
2694 type = TREE_TYPE (TREE_TYPE (array));
2695 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2696 /* Array ref is const/volatile if the array elements are
2697 or if the array is. */
2698 TREE_READONLY (rval)
2699 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2700 | TREE_READONLY (array));
2701 TREE_SIDE_EFFECTS (rval)
2702 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2703 | TREE_SIDE_EFFECTS (array));
2704 TREE_THIS_VOLATILE (rval)
2705 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2706 /* This was added by rms on 16 Nov 91.
2707 It fixes vol struct foo *a; a->elts[1]
2708 in an inline function.
2709 Hope it doesn't break something else. */
2710 | TREE_THIS_VOLATILE (array));
2711 ret = require_complete_type (loc, rval);
2712 protected_set_expr_location (ret, loc);
2713 if (non_lvalue)
2714 ret = non_lvalue_loc (loc, ret);
2715 return ret;
2717 else
2719 tree ar = default_conversion (array);
2721 if (ar == error_mark_node)
2722 return ar;
2724 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2725 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2727 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2728 index, false),
2729 RO_ARRAY_INDEXING);
2730 if (non_lvalue)
2731 ret = non_lvalue_loc (loc, ret);
2732 return ret;
2736 /* Build an external reference to identifier ID. FUN indicates
2737 whether this will be used for a function call. LOC is the source
2738 location of the identifier. This sets *TYPE to the type of the
2739 identifier, which is not the same as the type of the returned value
2740 for CONST_DECLs defined as enum constants. If the type of the
2741 identifier is not available, *TYPE is set to NULL. */
2742 tree
2743 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2745 tree ref;
2746 tree decl = lookup_name (id);
2748 /* In Objective-C, an instance variable (ivar) may be preferred to
2749 whatever lookup_name() found. */
2750 decl = objc_lookup_ivar (decl, id);
2752 *type = NULL;
2753 if (decl && decl != error_mark_node)
2755 ref = decl;
2756 *type = TREE_TYPE (ref);
2758 else if (fun)
2759 /* Implicit function declaration. */
2760 ref = implicitly_declare (loc, id);
2761 else if (decl == error_mark_node)
2762 /* Don't complain about something that's already been
2763 complained about. */
2764 return error_mark_node;
2765 else
2767 undeclared_variable (loc, id);
2768 return error_mark_node;
2771 if (TREE_TYPE (ref) == error_mark_node)
2772 return error_mark_node;
2774 if (TREE_DEPRECATED (ref))
2775 warn_deprecated_use (ref, NULL_TREE);
2777 /* Recursive call does not count as usage. */
2778 if (ref != current_function_decl)
2780 TREE_USED (ref) = 1;
2783 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2785 if (!in_sizeof && !in_typeof)
2786 C_DECL_USED (ref) = 1;
2787 else if (DECL_INITIAL (ref) == NULL_TREE
2788 && DECL_EXTERNAL (ref)
2789 && !TREE_PUBLIC (ref))
2790 record_maybe_used_decl (ref);
2793 if (TREE_CODE (ref) == CONST_DECL)
2795 used_types_insert (TREE_TYPE (ref));
2797 if (warn_cxx_compat
2798 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2799 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2801 warning_at (loc, OPT_Wc___compat,
2802 ("enum constant defined in struct or union "
2803 "is not visible in C++"));
2804 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2807 ref = DECL_INITIAL (ref);
2808 TREE_CONSTANT (ref) = 1;
2810 else if (current_function_decl != NULL_TREE
2811 && !DECL_FILE_SCOPE_P (current_function_decl)
2812 && (VAR_OR_FUNCTION_DECL_P (ref)
2813 || TREE_CODE (ref) == PARM_DECL))
2815 tree context = decl_function_context (ref);
2817 if (context != NULL_TREE && context != current_function_decl)
2818 DECL_NONLOCAL (ref) = 1;
2820 /* C99 6.7.4p3: An inline definition of a function with external
2821 linkage ... shall not contain a reference to an identifier with
2822 internal linkage. */
2823 else if (current_function_decl != NULL_TREE
2824 && DECL_DECLARED_INLINE_P (current_function_decl)
2825 && DECL_EXTERNAL (current_function_decl)
2826 && VAR_OR_FUNCTION_DECL_P (ref)
2827 && (!VAR_P (ref) || TREE_STATIC (ref))
2828 && ! TREE_PUBLIC (ref)
2829 && DECL_CONTEXT (ref) != current_function_decl)
2830 record_inline_static (loc, current_function_decl, ref,
2831 csi_internal);
2833 return ref;
2836 /* Record details of decls possibly used inside sizeof or typeof. */
2837 struct maybe_used_decl
2839 /* The decl. */
2840 tree decl;
2841 /* The level seen at (in_sizeof + in_typeof). */
2842 int level;
2843 /* The next one at this level or above, or NULL. */
2844 struct maybe_used_decl *next;
2847 static struct maybe_used_decl *maybe_used_decls;
2849 /* Record that DECL, an undefined static function reference seen
2850 inside sizeof or typeof, might be used if the operand of sizeof is
2851 a VLA type or the operand of typeof is a variably modified
2852 type. */
2854 static void
2855 record_maybe_used_decl (tree decl)
2857 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2858 t->decl = decl;
2859 t->level = in_sizeof + in_typeof;
2860 t->next = maybe_used_decls;
2861 maybe_used_decls = t;
2864 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2865 USED is false, just discard them. If it is true, mark them used
2866 (if no longer inside sizeof or typeof) or move them to the next
2867 level up (if still inside sizeof or typeof). */
2869 void
2870 pop_maybe_used (bool used)
2872 struct maybe_used_decl *p = maybe_used_decls;
2873 int cur_level = in_sizeof + in_typeof;
2874 while (p && p->level > cur_level)
2876 if (used)
2878 if (cur_level == 0)
2879 C_DECL_USED (p->decl) = 1;
2880 else
2881 p->level = cur_level;
2883 p = p->next;
2885 if (!used || cur_level == 0)
2886 maybe_used_decls = p;
2889 /* Return the result of sizeof applied to EXPR. */
2891 struct c_expr
2892 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2894 struct c_expr ret;
2895 if (expr.value == error_mark_node)
2897 ret.value = error_mark_node;
2898 ret.original_code = ERROR_MARK;
2899 ret.original_type = NULL;
2900 pop_maybe_used (false);
2902 else
2904 bool expr_const_operands = true;
2906 if (TREE_CODE (expr.value) == PARM_DECL
2907 && C_ARRAY_PARAMETER (expr.value))
2909 if (warning_at (loc, OPT_Wsizeof_array_argument,
2910 "%<sizeof%> on array function parameter %qE will "
2911 "return size of %qT", expr.value,
2912 TREE_TYPE (expr.value)))
2913 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2915 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2916 &expr_const_operands);
2917 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2918 c_last_sizeof_arg = expr.value;
2919 c_last_sizeof_loc = loc;
2920 ret.original_code = SIZEOF_EXPR;
2921 ret.original_type = NULL;
2922 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2924 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2925 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2926 folded_expr, ret.value);
2927 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2928 SET_EXPR_LOCATION (ret.value, loc);
2930 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2932 return ret;
2935 /* Return the result of sizeof applied to T, a structure for the type
2936 name passed to sizeof (rather than the type itself). LOC is the
2937 location of the original expression. */
2939 struct c_expr
2940 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2942 tree type;
2943 struct c_expr ret;
2944 tree type_expr = NULL_TREE;
2945 bool type_expr_const = true;
2946 type = groktypename (t, &type_expr, &type_expr_const);
2947 ret.value = c_sizeof (loc, type);
2948 c_last_sizeof_arg = type;
2949 c_last_sizeof_loc = loc;
2950 ret.original_code = SIZEOF_EXPR;
2951 ret.original_type = NULL;
2952 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2953 && c_vla_type_p (type))
2955 /* If the type is a [*] array, it is a VLA but is represented as
2956 having a size of zero. In such a case we must ensure that
2957 the result of sizeof does not get folded to a constant by
2958 c_fully_fold, because if the size is evaluated the result is
2959 not constant and so constraints on zero or negative size
2960 arrays must not be applied when this sizeof call is inside
2961 another array declarator. */
2962 if (!type_expr)
2963 type_expr = integer_zero_node;
2964 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2965 type_expr, ret.value);
2966 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2968 pop_maybe_used (type != error_mark_node
2969 ? C_TYPE_VARIABLE_SIZE (type) : false);
2970 return ret;
2973 /* Build a function call to function FUNCTION with parameters PARAMS.
2974 The function call is at LOC.
2975 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2976 TREE_VALUE of each node is a parameter-expression.
2977 FUNCTION's data type may be a function type or a pointer-to-function. */
2979 tree
2980 build_function_call (location_t loc, tree function, tree params)
2982 vec<tree, va_gc> *v;
2983 tree ret;
2985 vec_alloc (v, list_length (params));
2986 for (; params; params = TREE_CHAIN (params))
2987 v->quick_push (TREE_VALUE (params));
2988 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2989 vec_free (v);
2990 return ret;
2993 /* Give a note about the location of the declaration of DECL. */
2995 static void
2996 inform_declaration (tree decl)
2998 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2999 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3002 /* Build a function call to function FUNCTION with parameters PARAMS.
3003 ORIGTYPES, if not NULL, is a vector of types; each element is
3004 either NULL or the original type of the corresponding element in
3005 PARAMS. The original type may differ from TREE_TYPE of the
3006 parameter for enums. FUNCTION's data type may be a function type
3007 or pointer-to-function. This function changes the elements of
3008 PARAMS. */
3010 tree
3011 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3012 tree function, vec<tree, va_gc> *params,
3013 vec<tree, va_gc> *origtypes)
3015 tree fntype, fundecl = NULL_TREE;
3016 tree name = NULL_TREE, result;
3017 tree tem;
3018 int nargs;
3019 tree *argarray;
3022 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3023 STRIP_TYPE_NOPS (function);
3025 /* Convert anything with function type to a pointer-to-function. */
3026 if (TREE_CODE (function) == FUNCTION_DECL)
3028 name = DECL_NAME (function);
3030 if (flag_tm)
3031 tm_malloc_replacement (function);
3032 fundecl = function;
3033 /* Atomic functions have type checking/casting already done. They are
3034 often rewritten and don't match the original parameter list. */
3035 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3036 origtypes = NULL;
3038 if (flag_cilkplus
3039 && is_cilkplus_reduce_builtin (function))
3040 origtypes = NULL;
3042 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3043 function = function_to_pointer_conversion (loc, function);
3045 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3046 expressions, like those used for ObjC messenger dispatches. */
3047 if (params && !params->is_empty ())
3048 function = objc_rewrite_function_call (function, (*params)[0]);
3050 function = c_fully_fold (function, false, NULL);
3052 fntype = TREE_TYPE (function);
3054 if (TREE_CODE (fntype) == ERROR_MARK)
3055 return error_mark_node;
3057 if (!(TREE_CODE (fntype) == POINTER_TYPE
3058 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3060 if (!flag_diagnostics_show_caret)
3061 error_at (loc,
3062 "called object %qE is not a function or function pointer",
3063 function);
3064 else if (DECL_P (function))
3066 error_at (loc,
3067 "called object %qD is not a function or function pointer",
3068 function);
3069 inform_declaration (function);
3071 else
3072 error_at (loc,
3073 "called object is not a function or function pointer");
3074 return error_mark_node;
3077 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3078 current_function_returns_abnormally = 1;
3080 /* fntype now gets the type of function pointed to. */
3081 fntype = TREE_TYPE (fntype);
3083 /* Convert the parameters to the types declared in the
3084 function prototype, or apply default promotions. */
3086 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3087 origtypes, function, fundecl);
3088 if (nargs < 0)
3089 return error_mark_node;
3091 /* Check that the function is called through a compatible prototype.
3092 If it is not, warn. */
3093 if (CONVERT_EXPR_P (function)
3094 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3095 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3096 && !comptypes (fntype, TREE_TYPE (tem)))
3098 tree return_type = TREE_TYPE (fntype);
3100 /* This situation leads to run-time undefined behavior. We can't,
3101 therefore, simply error unless we can prove that all possible
3102 executions of the program must execute the code. */
3103 warning_at (loc, 0, "function called through a non-compatible type");
3105 if (VOID_TYPE_P (return_type)
3106 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3107 pedwarn (loc, 0,
3108 "function with qualified void return type called");
3111 argarray = vec_safe_address (params);
3113 /* Check that arguments to builtin functions match the expectations. */
3114 if (fundecl
3115 && DECL_BUILT_IN (fundecl)
3116 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3117 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3118 argarray))
3119 return error_mark_node;
3121 /* Check that the arguments to the function are valid. */
3122 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3123 nargs, argarray, &arg_loc);
3125 if (name != NULL_TREE
3126 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3128 if (require_constant_value)
3129 result
3130 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3131 function, nargs, argarray);
3132 else
3133 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3134 function, nargs, argarray);
3135 if (TREE_CODE (result) == NOP_EXPR
3136 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3137 STRIP_TYPE_NOPS (result);
3139 else
3140 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3141 function, nargs, argarray);
3142 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3143 later. */
3144 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3145 TREE_NO_WARNING (result) = 1;
3147 /* In this improbable scenario, a nested function returns a VM type.
3148 Create a TARGET_EXPR so that the call always has a LHS, much as
3149 what the C++ FE does for functions returning non-PODs. */
3150 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3152 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3153 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3154 NULL_TREE, NULL_TREE);
3157 if (VOID_TYPE_P (TREE_TYPE (result)))
3159 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3160 pedwarn (loc, 0,
3161 "function with qualified void return type called");
3162 return result;
3164 return require_complete_type (loc, result);
3167 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3169 tree
3170 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3171 tree function, vec<tree, va_gc> *params,
3172 vec<tree, va_gc> *origtypes)
3174 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3175 STRIP_TYPE_NOPS (function);
3177 /* Convert anything with function type to a pointer-to-function. */
3178 if (TREE_CODE (function) == FUNCTION_DECL)
3180 /* Implement type-directed function overloading for builtins.
3181 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3182 handle all the type checking. The result is a complete expression
3183 that implements this function call. */
3184 tree tem = resolve_overloaded_builtin (loc, function, params);
3185 if (tem)
3186 return tem;
3188 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3191 /* Convert the argument expressions in the vector VALUES
3192 to the types in the list TYPELIST.
3194 If TYPELIST is exhausted, or when an element has NULL as its type,
3195 perform the default conversions.
3197 ORIGTYPES is the original types of the expressions in VALUES. This
3198 holds the type of enum values which have been converted to integral
3199 types. It may be NULL.
3201 FUNCTION is a tree for the called function. It is used only for
3202 error messages, where it is formatted with %qE.
3204 This is also where warnings about wrong number of args are generated.
3206 ARG_LOC are locations of function arguments (if any).
3208 Returns the actual number of arguments processed (which may be less
3209 than the length of VALUES in some error situations), or -1 on
3210 failure. */
3212 static int
3213 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3214 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3215 tree function, tree fundecl)
3217 tree typetail, val;
3218 unsigned int parmnum;
3219 bool error_args = false;
3220 const bool type_generic = fundecl
3221 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3222 bool type_generic_remove_excess_precision = false;
3223 bool type_generic_overflow_p = false;
3224 tree selector;
3226 /* Change pointer to function to the function itself for
3227 diagnostics. */
3228 if (TREE_CODE (function) == ADDR_EXPR
3229 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3230 function = TREE_OPERAND (function, 0);
3232 /* Handle an ObjC selector specially for diagnostics. */
3233 selector = objc_message_selector ();
3235 /* For type-generic built-in functions, determine whether excess
3236 precision should be removed (classification) or not
3237 (comparison). */
3238 if (type_generic
3239 && DECL_BUILT_IN (fundecl)
3240 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3242 switch (DECL_FUNCTION_CODE (fundecl))
3244 case BUILT_IN_ISFINITE:
3245 case BUILT_IN_ISINF:
3246 case BUILT_IN_ISINF_SIGN:
3247 case BUILT_IN_ISNAN:
3248 case BUILT_IN_ISNORMAL:
3249 case BUILT_IN_FPCLASSIFY:
3250 type_generic_remove_excess_precision = true;
3251 break;
3253 case BUILT_IN_ADD_OVERFLOW_P:
3254 case BUILT_IN_SUB_OVERFLOW_P:
3255 case BUILT_IN_MUL_OVERFLOW_P:
3256 /* The last argument of these type-generic builtins
3257 should not be promoted. */
3258 type_generic_overflow_p = true;
3259 break;
3261 default:
3262 break;
3265 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3266 return vec_safe_length (values);
3268 /* Scan the given expressions and types, producing individual
3269 converted arguments. */
3271 for (typetail = typelist, parmnum = 0;
3272 values && values->iterate (parmnum, &val);
3273 ++parmnum)
3275 tree type = typetail ? TREE_VALUE (typetail) : 0;
3276 tree valtype = TREE_TYPE (val);
3277 tree rname = function;
3278 int argnum = parmnum + 1;
3279 const char *invalid_func_diag;
3280 bool excess_precision = false;
3281 bool npc;
3282 tree parmval;
3283 /* Some __atomic_* builtins have additional hidden argument at
3284 position 0. */
3285 location_t ploc
3286 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3287 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3288 : input_location;
3290 if (type == void_type_node)
3292 if (selector)
3293 error_at (loc, "too many arguments to method %qE", selector);
3294 else
3295 error_at (loc, "too many arguments to function %qE", function);
3296 inform_declaration (fundecl);
3297 return error_args ? -1 : (int) parmnum;
3300 if (selector && argnum > 2)
3302 rname = selector;
3303 argnum -= 2;
3306 npc = null_pointer_constant_p (val);
3308 /* If there is excess precision and a prototype, convert once to
3309 the required type rather than converting via the semantic
3310 type. Likewise without a prototype a float value represented
3311 as long double should be converted once to double. But for
3312 type-generic classification functions excess precision must
3313 be removed here. */
3314 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3315 && (type || !type_generic || !type_generic_remove_excess_precision))
3317 val = TREE_OPERAND (val, 0);
3318 excess_precision = true;
3320 val = c_fully_fold (val, false, NULL);
3321 STRIP_TYPE_NOPS (val);
3323 val = require_complete_type (ploc, val);
3325 /* Some floating-point arguments must be promoted to double when
3326 no type is specified by a prototype. This applies to
3327 arguments of type float, and to architecture-specific types
3328 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3329 bool promote_float_arg = false;
3330 if (type == NULL_TREE
3331 && TREE_CODE (valtype) == REAL_TYPE
3332 && (TYPE_PRECISION (valtype)
3333 <= TYPE_PRECISION (double_type_node))
3334 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3335 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3336 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3338 /* Promote this argument, unless it has a _FloatN or
3339 _FloatNx type. */
3340 promote_float_arg = true;
3341 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3342 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3344 promote_float_arg = false;
3345 break;
3349 if (type != NULL_TREE)
3351 /* Formal parm type is specified by a function prototype. */
3353 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3355 error_at (ploc, "type of formal parameter %d is incomplete",
3356 parmnum + 1);
3357 parmval = val;
3359 else
3361 tree origtype;
3363 /* Optionally warn about conversions that
3364 differ from the default conversions. */
3365 if (warn_traditional_conversion || warn_traditional)
3367 unsigned int formal_prec = TYPE_PRECISION (type);
3369 if (INTEGRAL_TYPE_P (type)
3370 && TREE_CODE (valtype) == REAL_TYPE)
3371 warning_at (ploc, OPT_Wtraditional_conversion,
3372 "passing argument %d of %qE as integer rather "
3373 "than floating due to prototype",
3374 argnum, rname);
3375 if (INTEGRAL_TYPE_P (type)
3376 && TREE_CODE (valtype) == COMPLEX_TYPE)
3377 warning_at (ploc, OPT_Wtraditional_conversion,
3378 "passing argument %d of %qE as integer rather "
3379 "than complex due to prototype",
3380 argnum, rname);
3381 else if (TREE_CODE (type) == COMPLEX_TYPE
3382 && TREE_CODE (valtype) == REAL_TYPE)
3383 warning_at (ploc, OPT_Wtraditional_conversion,
3384 "passing argument %d of %qE as complex rather "
3385 "than floating due to prototype",
3386 argnum, rname);
3387 else if (TREE_CODE (type) == REAL_TYPE
3388 && INTEGRAL_TYPE_P (valtype))
3389 warning_at (ploc, OPT_Wtraditional_conversion,
3390 "passing argument %d of %qE as floating rather "
3391 "than integer due to prototype",
3392 argnum, rname);
3393 else if (TREE_CODE (type) == COMPLEX_TYPE
3394 && INTEGRAL_TYPE_P (valtype))
3395 warning_at (ploc, OPT_Wtraditional_conversion,
3396 "passing argument %d of %qE as complex rather "
3397 "than integer due to prototype",
3398 argnum, rname);
3399 else if (TREE_CODE (type) == REAL_TYPE
3400 && TREE_CODE (valtype) == COMPLEX_TYPE)
3401 warning_at (ploc, OPT_Wtraditional_conversion,
3402 "passing argument %d of %qE as floating rather "
3403 "than complex due to prototype",
3404 argnum, rname);
3405 /* ??? At some point, messages should be written about
3406 conversions between complex types, but that's too messy
3407 to do now. */
3408 else if (TREE_CODE (type) == REAL_TYPE
3409 && TREE_CODE (valtype) == REAL_TYPE)
3411 /* Warn if any argument is passed as `float',
3412 since without a prototype it would be `double'. */
3413 if (formal_prec == TYPE_PRECISION (float_type_node)
3414 && type != dfloat32_type_node)
3415 warning_at (ploc, 0,
3416 "passing argument %d of %qE as %<float%> "
3417 "rather than %<double%> due to prototype",
3418 argnum, rname);
3420 /* Warn if mismatch between argument and prototype
3421 for decimal float types. Warn of conversions with
3422 binary float types and of precision narrowing due to
3423 prototype. */
3424 else if (type != valtype
3425 && (type == dfloat32_type_node
3426 || type == dfloat64_type_node
3427 || type == dfloat128_type_node
3428 || valtype == dfloat32_type_node
3429 || valtype == dfloat64_type_node
3430 || valtype == dfloat128_type_node)
3431 && (formal_prec
3432 <= TYPE_PRECISION (valtype)
3433 || (type == dfloat128_type_node
3434 && (valtype
3435 != dfloat64_type_node
3436 && (valtype
3437 != dfloat32_type_node)))
3438 || (type == dfloat64_type_node
3439 && (valtype
3440 != dfloat32_type_node))))
3441 warning_at (ploc, 0,
3442 "passing argument %d of %qE as %qT "
3443 "rather than %qT due to prototype",
3444 argnum, rname, type, valtype);
3447 /* Detect integer changing in width or signedness.
3448 These warnings are only activated with
3449 -Wtraditional-conversion, not with -Wtraditional. */
3450 else if (warn_traditional_conversion
3451 && INTEGRAL_TYPE_P (type)
3452 && INTEGRAL_TYPE_P (valtype))
3454 tree would_have_been = default_conversion (val);
3455 tree type1 = TREE_TYPE (would_have_been);
3457 if (val == error_mark_node)
3458 /* VAL could have been of incomplete type. */;
3459 else if (TREE_CODE (type) == ENUMERAL_TYPE
3460 && (TYPE_MAIN_VARIANT (type)
3461 == TYPE_MAIN_VARIANT (valtype)))
3462 /* No warning if function asks for enum
3463 and the actual arg is that enum type. */
3465 else if (formal_prec != TYPE_PRECISION (type1))
3466 warning_at (ploc, OPT_Wtraditional_conversion,
3467 "passing argument %d of %qE "
3468 "with different width due to prototype",
3469 argnum, rname);
3470 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3472 /* Don't complain if the formal parameter type
3473 is an enum, because we can't tell now whether
3474 the value was an enum--even the same enum. */
3475 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3477 else if (TREE_CODE (val) == INTEGER_CST
3478 && int_fits_type_p (val, type))
3479 /* Change in signedness doesn't matter
3480 if a constant value is unaffected. */
3482 /* If the value is extended from a narrower
3483 unsigned type, it doesn't matter whether we
3484 pass it as signed or unsigned; the value
3485 certainly is the same either way. */
3486 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3487 && TYPE_UNSIGNED (valtype))
3489 else if (TYPE_UNSIGNED (type))
3490 warning_at (ploc, OPT_Wtraditional_conversion,
3491 "passing argument %d of %qE "
3492 "as unsigned due to prototype",
3493 argnum, rname);
3494 else
3495 warning_at (ploc, OPT_Wtraditional_conversion,
3496 "passing argument %d of %qE "
3497 "as signed due to prototype",
3498 argnum, rname);
3502 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3503 sake of better warnings from convert_and_check. */
3504 if (excess_precision)
3505 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3506 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3507 parmval = convert_for_assignment (loc, ploc, type,
3508 val, origtype, ic_argpass,
3509 npc, fundecl, function,
3510 parmnum + 1);
3512 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3513 && INTEGRAL_TYPE_P (type)
3514 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3515 parmval = default_conversion (parmval);
3518 else if (promote_float_arg)
3520 if (type_generic)
3521 parmval = val;
3522 else
3524 /* Convert `float' to `double'. */
3525 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3526 warning_at (ploc, OPT_Wdouble_promotion,
3527 "implicit conversion from %qT to %qT when passing "
3528 "argument to function",
3529 valtype, double_type_node);
3530 parmval = convert (double_type_node, val);
3533 else if ((excess_precision && !type_generic)
3534 || (type_generic_overflow_p && parmnum == 2))
3535 /* A "double" argument with excess precision being passed
3536 without a prototype or in variable arguments.
3537 The last argument of __builtin_*_overflow_p should not be
3538 promoted. */
3539 parmval = convert (valtype, val);
3540 else if ((invalid_func_diag =
3541 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3543 error (invalid_func_diag);
3544 return -1;
3546 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3548 return -1;
3550 else
3551 /* Convert `short' and `char' to full-size `int'. */
3552 parmval = default_conversion (val);
3554 (*values)[parmnum] = parmval;
3555 if (parmval == error_mark_node)
3556 error_args = true;
3558 if (typetail)
3559 typetail = TREE_CHAIN (typetail);
3562 gcc_assert (parmnum == vec_safe_length (values));
3564 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3566 error_at (loc, "too few arguments to function %qE", function);
3567 inform_declaration (fundecl);
3568 return -1;
3571 return error_args ? -1 : (int) parmnum;
3574 /* This is the entry point used by the parser to build unary operators
3575 in the input. CODE, a tree_code, specifies the unary operator, and
3576 ARG is the operand. For unary plus, the C parser currently uses
3577 CONVERT_EXPR for code.
3579 LOC is the location to use for the tree generated.
3582 struct c_expr
3583 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3585 struct c_expr result;
3587 result.original_code = code;
3588 result.original_type = NULL;
3590 if (reject_gcc_builtin (arg.value))
3592 result.value = error_mark_node;
3594 else
3596 result.value = build_unary_op (loc, code, arg.value, false);
3598 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3599 overflow_warning (loc, result.value, arg.value);
3602 /* We are typically called when parsing a prefix token at LOC acting on
3603 ARG. Reflect this by updating the source range of the result to
3604 start at LOC and end at the end of ARG. */
3605 set_c_expr_source_range (&result,
3606 loc, arg.get_finish ());
3608 return result;
3611 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3613 static bool
3614 char_type_p (tree type)
3616 return (type == char_type_node
3617 || type == unsigned_char_type_node
3618 || type == signed_char_type_node
3619 || type == char16_type_node
3620 || type == char32_type_node);
3623 /* This is the entry point used by the parser to build binary operators
3624 in the input. CODE, a tree_code, specifies the binary operator, and
3625 ARG1 and ARG2 are the operands. In addition to constructing the
3626 expression, we check for operands that were written with other binary
3627 operators in a way that is likely to confuse the user.
3629 LOCATION is the location of the binary operator. */
3631 struct c_expr
3632 parser_build_binary_op (location_t location, enum tree_code code,
3633 struct c_expr arg1, struct c_expr arg2)
3635 struct c_expr result;
3637 enum tree_code code1 = arg1.original_code;
3638 enum tree_code code2 = arg2.original_code;
3639 tree type1 = (arg1.original_type
3640 ? arg1.original_type
3641 : TREE_TYPE (arg1.value));
3642 tree type2 = (arg2.original_type
3643 ? arg2.original_type
3644 : TREE_TYPE (arg2.value));
3646 result.value = build_binary_op (location, code,
3647 arg1.value, arg2.value, true);
3648 result.original_code = code;
3649 result.original_type = NULL;
3651 if (TREE_CODE (result.value) == ERROR_MARK)
3653 set_c_expr_source_range (&result,
3654 arg1.get_start (),
3655 arg2.get_finish ());
3656 return result;
3659 if (location != UNKNOWN_LOCATION)
3660 protected_set_expr_location (result.value, location);
3662 set_c_expr_source_range (&result,
3663 arg1.get_start (),
3664 arg2.get_finish ());
3666 /* Check for cases such as x+y<<z which users are likely
3667 to misinterpret. */
3668 if (warn_parentheses)
3669 warn_about_parentheses (location, code, code1, arg1.value, code2,
3670 arg2.value);
3672 if (warn_logical_op)
3673 warn_logical_operator (location, code, TREE_TYPE (result.value),
3674 code1, arg1.value, code2, arg2.value);
3676 if (warn_tautological_compare)
3678 tree lhs = arg1.value;
3679 tree rhs = arg2.value;
3680 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3682 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3683 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3684 lhs = NULL_TREE;
3685 else
3686 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3688 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3690 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3691 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3692 rhs = NULL_TREE;
3693 else
3694 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3696 if (lhs != NULL_TREE && rhs != NULL_TREE)
3697 warn_tautological_cmp (location, code, lhs, rhs);
3700 if (warn_logical_not_paren
3701 && TREE_CODE_CLASS (code) == tcc_comparison
3702 && code1 == TRUTH_NOT_EXPR
3703 && code2 != TRUTH_NOT_EXPR
3704 /* Avoid warning for !!x == y. */
3705 && (TREE_CODE (arg1.value) != NE_EXPR
3706 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3708 /* Avoid warning for !b == y where b has _Bool type. */
3709 tree t = integer_zero_node;
3710 if (TREE_CODE (arg1.value) == EQ_EXPR
3711 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3712 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3714 t = TREE_OPERAND (arg1.value, 0);
3717 if (TREE_TYPE (t) != integer_type_node)
3718 break;
3719 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3720 t = C_MAYBE_CONST_EXPR_EXPR (t);
3721 else if (CONVERT_EXPR_P (t))
3722 t = TREE_OPERAND (t, 0);
3723 else
3724 break;
3726 while (1);
3728 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3729 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3732 /* Warn about comparisons against string literals, with the exception
3733 of testing for equality or inequality of a string literal with NULL. */
3734 if (code == EQ_EXPR || code == NE_EXPR)
3736 if ((code1 == STRING_CST
3737 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3738 || (code2 == STRING_CST
3739 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3740 warning_at (location, OPT_Waddress,
3741 "comparison with string literal results in unspecified behavior");
3742 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3743 if (POINTER_TYPE_P (type1)
3744 && null_pointer_constant_p (arg2.value)
3745 && char_type_p (type2)
3746 && warning_at (location, OPT_Wpointer_compare,
3747 "comparison between pointer and zero character "
3748 "constant"))
3749 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3750 else if (POINTER_TYPE_P (type2)
3751 && null_pointer_constant_p (arg1.value)
3752 && char_type_p (type1)
3753 && warning_at (location, OPT_Wpointer_compare,
3754 "comparison between pointer and zero character "
3755 "constant"))
3756 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3758 else if (TREE_CODE_CLASS (code) == tcc_comparison
3759 && (code1 == STRING_CST || code2 == STRING_CST))
3760 warning_at (location, OPT_Waddress,
3761 "comparison with string literal results in unspecified behavior");
3763 if (TREE_OVERFLOW_P (result.value)
3764 && !TREE_OVERFLOW_P (arg1.value)
3765 && !TREE_OVERFLOW_P (arg2.value))
3766 overflow_warning (location, result.value);
3768 /* Warn about comparisons of different enum types. */
3769 if (warn_enum_compare
3770 && TREE_CODE_CLASS (code) == tcc_comparison
3771 && TREE_CODE (type1) == ENUMERAL_TYPE
3772 && TREE_CODE (type2) == ENUMERAL_TYPE
3773 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3774 warning_at (location, OPT_Wenum_compare,
3775 "comparison between %qT and %qT",
3776 type1, type2);
3778 return result;
3781 /* Return a tree for the difference of pointers OP0 and OP1.
3782 The resulting tree has type int. */
3784 static tree
3785 pointer_diff (location_t loc, tree op0, tree op1)
3787 tree restype = ptrdiff_type_node;
3788 tree result, inttype;
3790 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3791 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3792 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3793 tree orig_op1 = op1;
3795 /* If the operands point into different address spaces, we need to
3796 explicitly convert them to pointers into the common address space
3797 before we can subtract the numerical address values. */
3798 if (as0 != as1)
3800 addr_space_t as_common;
3801 tree common_type;
3803 /* Determine the common superset address space. This is guaranteed
3804 to exist because the caller verified that comp_target_types
3805 returned non-zero. */
3806 if (!addr_space_superset (as0, as1, &as_common))
3807 gcc_unreachable ();
3809 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3810 op0 = convert (common_type, op0);
3811 op1 = convert (common_type, op1);
3814 /* Determine integer type to perform computations in. This will usually
3815 be the same as the result type (ptrdiff_t), but may need to be a wider
3816 type if pointers for the address space are wider than ptrdiff_t. */
3817 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3818 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3819 else
3820 inttype = restype;
3822 if (TREE_CODE (target_type) == VOID_TYPE)
3823 pedwarn (loc, OPT_Wpointer_arith,
3824 "pointer of type %<void *%> used in subtraction");
3825 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3826 pedwarn (loc, OPT_Wpointer_arith,
3827 "pointer to a function used in subtraction");
3829 /* First do the subtraction as integers;
3830 then drop through to build the divide operator.
3831 Do not do default conversions on the minus operator
3832 in case restype is a short type. */
3834 op0 = build_binary_op (loc,
3835 MINUS_EXPR, convert (inttype, op0),
3836 convert (inttype, op1), false);
3837 /* This generates an error if op1 is pointer to incomplete type. */
3838 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3839 error_at (loc, "arithmetic on pointer to an incomplete type");
3841 op1 = c_size_in_bytes (target_type);
3843 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3844 error_at (loc, "arithmetic on pointer to an empty aggregate");
3846 /* Divide by the size, in easiest possible way. */
3847 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3848 op0, convert (inttype, op1));
3850 /* Convert to final result type if necessary. */
3851 return convert (restype, result);
3854 /* Expand atomic compound assignments into an appropriate sequence as
3855 specified by the C11 standard section 6.5.16.2.
3857 _Atomic T1 E1
3858 T2 E2
3859 E1 op= E2
3861 This sequence is used for all types for which these operations are
3862 supported.
3864 In addition, built-in versions of the 'fe' prefixed routines may
3865 need to be invoked for floating point (real, complex or vector) when
3866 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3868 T1 newval;
3869 T1 old;
3870 T1 *addr
3871 T2 val
3872 fenv_t fenv
3874 addr = &E1;
3875 val = (E2);
3876 __atomic_load (addr, &old, SEQ_CST);
3877 feholdexcept (&fenv);
3878 loop:
3879 newval = old op val;
3880 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3881 SEQ_CST))
3882 goto done;
3883 feclearexcept (FE_ALL_EXCEPT);
3884 goto loop:
3885 done:
3886 feupdateenv (&fenv);
3888 The compiler will issue the __atomic_fetch_* built-in when possible,
3889 otherwise it will generate the generic form of the atomic operations.
3890 This requires temp(s) and has their address taken. The atomic processing
3891 is smart enough to figure out when the size of an object can utilize
3892 a lock-free version, and convert the built-in call to the appropriate
3893 lock-free routine. The optimizers will then dispose of any temps that
3894 are no longer required, and lock-free implementations are utilized as
3895 long as there is target support for the required size.
3897 If the operator is NOP_EXPR, then this is a simple assignment, and
3898 an __atomic_store is issued to perform the assignment rather than
3899 the above loop. */
3901 /* Build an atomic assignment at LOC, expanding into the proper
3902 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3903 the result of the operation, unless RETURN_OLD_P, in which case
3904 return the old value of LHS (this is only for postincrement and
3905 postdecrement). */
3907 static tree
3908 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3909 tree rhs, bool return_old_p)
3911 tree fndecl, func_call;
3912 vec<tree, va_gc> *params;
3913 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3914 tree old, old_addr;
3915 tree compound_stmt;
3916 tree stmt, goto_stmt;
3917 tree loop_label, loop_decl, done_label, done_decl;
3919 tree lhs_type = TREE_TYPE (lhs);
3920 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3921 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3922 tree rhs_semantic_type = TREE_TYPE (rhs);
3923 tree nonatomic_rhs_semantic_type;
3924 tree rhs_type;
3926 gcc_assert (TYPE_ATOMIC (lhs_type));
3928 if (return_old_p)
3929 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3931 /* Allocate enough vector items for a compare_exchange. */
3932 vec_alloc (params, 6);
3934 /* Create a compound statement to hold the sequence of statements
3935 with a loop. */
3936 compound_stmt = c_begin_compound_stmt (false);
3938 /* Remove any excess precision (which is only present here in the
3939 case of compound assignments). */
3940 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3942 gcc_assert (modifycode != NOP_EXPR);
3943 rhs = TREE_OPERAND (rhs, 0);
3945 rhs_type = TREE_TYPE (rhs);
3947 /* Fold the RHS if it hasn't already been folded. */
3948 if (modifycode != NOP_EXPR)
3949 rhs = c_fully_fold (rhs, false, NULL);
3951 /* Remove the qualifiers for the rest of the expressions and create
3952 the VAL temp variable to hold the RHS. */
3953 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3954 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3955 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3956 TYPE_UNQUALIFIED);
3957 val = create_tmp_var_raw (nonatomic_rhs_type);
3958 TREE_ADDRESSABLE (val) = 1;
3959 TREE_NO_WARNING (val) = 1;
3960 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3961 NULL_TREE);
3962 SET_EXPR_LOCATION (rhs, loc);
3963 add_stmt (rhs);
3965 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3966 an atomic_store. */
3967 if (modifycode == NOP_EXPR)
3969 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3970 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3971 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3972 params->quick_push (lhs_addr);
3973 params->quick_push (rhs);
3974 params->quick_push (seq_cst);
3975 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3976 add_stmt (func_call);
3978 /* Finish the compound statement. */
3979 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3981 /* VAL is the value which was stored, return a COMPOUND_STMT of
3982 the statement and that value. */
3983 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3986 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3987 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3988 isn't applicable for such builtins. ??? Do we want to handle enums? */
3989 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3990 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3992 built_in_function fncode;
3993 switch (modifycode)
3995 case PLUS_EXPR:
3996 case POINTER_PLUS_EXPR:
3997 fncode = (return_old_p
3998 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3999 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4000 break;
4001 case MINUS_EXPR:
4002 fncode = (return_old_p
4003 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4004 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4005 break;
4006 case BIT_AND_EXPR:
4007 fncode = (return_old_p
4008 ? BUILT_IN_ATOMIC_FETCH_AND_N
4009 : BUILT_IN_ATOMIC_AND_FETCH_N);
4010 break;
4011 case BIT_IOR_EXPR:
4012 fncode = (return_old_p
4013 ? BUILT_IN_ATOMIC_FETCH_OR_N
4014 : BUILT_IN_ATOMIC_OR_FETCH_N);
4015 break;
4016 case BIT_XOR_EXPR:
4017 fncode = (return_old_p
4018 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4019 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4020 break;
4021 default:
4022 goto cas_loop;
4025 /* We can only use "_1" through "_16" variants of the atomic fetch
4026 built-ins. */
4027 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4028 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4029 goto cas_loop;
4031 /* If this is a pointer type, we need to multiply by the size of
4032 the pointer target type. */
4033 if (POINTER_TYPE_P (lhs_type))
4035 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4036 /* ??? This would introduce -Wdiscarded-qualifiers
4037 warning: __atomic_fetch_* expect volatile void *
4038 type as the first argument. (Assignments between
4039 atomic and non-atomic objects are OK.) */
4040 || TYPE_RESTRICT (lhs_type))
4041 goto cas_loop;
4042 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4043 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4044 convert (ptrdiff_type_node, rhs),
4045 convert (ptrdiff_type_node, sz));
4048 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4049 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4050 fndecl = builtin_decl_explicit (fncode);
4051 params->quick_push (lhs_addr);
4052 params->quick_push (rhs);
4053 params->quick_push (seq_cst);
4054 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4056 newval = create_tmp_var_raw (nonatomic_lhs_type);
4057 TREE_ADDRESSABLE (newval) = 1;
4058 TREE_NO_WARNING (newval) = 1;
4059 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4060 NULL_TREE, NULL_TREE);
4061 SET_EXPR_LOCATION (rhs, loc);
4062 add_stmt (rhs);
4064 /* Finish the compound statement. */
4065 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4067 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4068 the statement and that value. */
4069 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4072 cas_loop:
4073 /* Create the variables and labels required for the op= form. */
4074 old = create_tmp_var_raw (nonatomic_lhs_type);
4075 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4076 TREE_ADDRESSABLE (old) = 1;
4077 TREE_NO_WARNING (old) = 1;
4079 newval = create_tmp_var_raw (nonatomic_lhs_type);
4080 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4081 TREE_ADDRESSABLE (newval) = 1;
4082 TREE_NO_WARNING (newval) = 1;
4084 loop_decl = create_artificial_label (loc);
4085 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4087 done_decl = create_artificial_label (loc);
4088 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4090 /* __atomic_load (addr, &old, SEQ_CST). */
4091 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4092 params->quick_push (lhs_addr);
4093 params->quick_push (old_addr);
4094 params->quick_push (seq_cst);
4095 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4096 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4097 NULL_TREE);
4098 add_stmt (old);
4099 params->truncate (0);
4101 /* Create the expressions for floating-point environment
4102 manipulation, if required. */
4103 bool need_fenv = (flag_trapping_math
4104 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4105 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4106 if (need_fenv)
4107 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4109 if (hold_call)
4110 add_stmt (hold_call);
4112 /* loop: */
4113 add_stmt (loop_label);
4115 /* newval = old + val; */
4116 if (rhs_type != rhs_semantic_type)
4117 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4118 rhs = build_binary_op (loc, modifycode, old, val, true);
4119 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4121 tree eptype = TREE_TYPE (rhs);
4122 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4123 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4125 else
4126 rhs = c_fully_fold (rhs, false, NULL);
4127 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4128 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4129 NULL_TREE, 0);
4130 if (rhs != error_mark_node)
4132 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4133 NULL_TREE);
4134 SET_EXPR_LOCATION (rhs, loc);
4135 add_stmt (rhs);
4138 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4139 goto done; */
4140 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4141 params->quick_push (lhs_addr);
4142 params->quick_push (old_addr);
4143 params->quick_push (newval_addr);
4144 params->quick_push (integer_zero_node);
4145 params->quick_push (seq_cst);
4146 params->quick_push (seq_cst);
4147 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4149 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4150 SET_EXPR_LOCATION (goto_stmt, loc);
4152 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4153 SET_EXPR_LOCATION (stmt, loc);
4154 add_stmt (stmt);
4156 if (clear_call)
4157 add_stmt (clear_call);
4159 /* goto loop; */
4160 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4161 SET_EXPR_LOCATION (goto_stmt, loc);
4162 add_stmt (goto_stmt);
4164 /* done: */
4165 add_stmt (done_label);
4167 if (update_call)
4168 add_stmt (update_call);
4170 /* Finish the compound statement. */
4171 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4173 /* NEWVAL is the value that was successfully stored, return a
4174 COMPOUND_EXPR of the statement and the appropriate value. */
4175 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4176 return_old_p ? old : newval);
4179 /* Construct and perhaps optimize a tree representation
4180 for a unary operation. CODE, a tree_code, specifies the operation
4181 and XARG is the operand.
4182 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4183 promotions (such as from short to int).
4184 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4185 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4186 to pointers in C99.
4188 LOCATION is the location of the operator. */
4190 tree
4191 build_unary_op (location_t location, enum tree_code code, tree xarg,
4192 bool noconvert)
4194 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4195 tree arg = xarg;
4196 tree argtype = NULL_TREE;
4197 enum tree_code typecode;
4198 tree val;
4199 tree ret = error_mark_node;
4200 tree eptype = NULL_TREE;
4201 const char *invalid_op_diag;
4202 bool int_operands;
4204 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4205 if (int_operands)
4206 arg = remove_c_maybe_const_expr (arg);
4208 if (code != ADDR_EXPR)
4209 arg = require_complete_type (location, arg);
4211 typecode = TREE_CODE (TREE_TYPE (arg));
4212 if (typecode == ERROR_MARK)
4213 return error_mark_node;
4214 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4215 typecode = INTEGER_TYPE;
4217 if ((invalid_op_diag
4218 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4220 error_at (location, invalid_op_diag);
4221 return error_mark_node;
4224 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4226 eptype = TREE_TYPE (arg);
4227 arg = TREE_OPERAND (arg, 0);
4230 switch (code)
4232 case CONVERT_EXPR:
4233 /* This is used for unary plus, because a CONVERT_EXPR
4234 is enough to prevent anybody from looking inside for
4235 associativity, but won't generate any code. */
4236 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4237 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4238 || typecode == VECTOR_TYPE))
4240 error_at (location, "wrong type argument to unary plus");
4241 return error_mark_node;
4243 else if (!noconvert)
4244 arg = default_conversion (arg);
4245 arg = non_lvalue_loc (location, arg);
4246 break;
4248 case NEGATE_EXPR:
4249 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4250 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4251 || typecode == VECTOR_TYPE))
4253 error_at (location, "wrong type argument to unary minus");
4254 return error_mark_node;
4256 else if (!noconvert)
4257 arg = default_conversion (arg);
4258 break;
4260 case BIT_NOT_EXPR:
4261 /* ~ works on integer types and non float vectors. */
4262 if (typecode == INTEGER_TYPE
4263 || (typecode == VECTOR_TYPE
4264 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4266 tree e = arg;
4268 /* Warn if the expression has boolean value. */
4269 while (TREE_CODE (e) == COMPOUND_EXPR)
4270 e = TREE_OPERAND (e, 1);
4272 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4273 || truth_value_p (TREE_CODE (e)))
4274 && warning_at (location, OPT_Wbool_operation,
4275 "%<~%> on a boolean expression"))
4277 gcc_rich_location richloc (location);
4278 richloc.add_fixit_insert_before (location, "!");
4279 inform_at_rich_loc (&richloc, "did you mean to use logical "
4280 "not?");
4282 if (!noconvert)
4283 arg = default_conversion (arg);
4285 else if (typecode == COMPLEX_TYPE)
4287 code = CONJ_EXPR;
4288 pedwarn (location, OPT_Wpedantic,
4289 "ISO C does not support %<~%> for complex conjugation");
4290 if (!noconvert)
4291 arg = default_conversion (arg);
4293 else
4295 error_at (location, "wrong type argument to bit-complement");
4296 return error_mark_node;
4298 break;
4300 case ABS_EXPR:
4301 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4303 error_at (location, "wrong type argument to abs");
4304 return error_mark_node;
4306 else if (!noconvert)
4307 arg = default_conversion (arg);
4308 break;
4310 case CONJ_EXPR:
4311 /* Conjugating a real value is a no-op, but allow it anyway. */
4312 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4313 || typecode == COMPLEX_TYPE))
4315 error_at (location, "wrong type argument to conjugation");
4316 return error_mark_node;
4318 else if (!noconvert)
4319 arg = default_conversion (arg);
4320 break;
4322 case TRUTH_NOT_EXPR:
4323 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4324 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4325 && typecode != COMPLEX_TYPE)
4327 error_at (location,
4328 "wrong type argument to unary exclamation mark");
4329 return error_mark_node;
4331 if (int_operands)
4333 arg = c_objc_common_truthvalue_conversion (location, xarg);
4334 arg = remove_c_maybe_const_expr (arg);
4336 else
4337 arg = c_objc_common_truthvalue_conversion (location, arg);
4338 ret = invert_truthvalue_loc (location, arg);
4339 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4340 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4341 location = EXPR_LOCATION (ret);
4342 goto return_build_unary_op;
4344 case REALPART_EXPR:
4345 case IMAGPART_EXPR:
4346 ret = build_real_imag_expr (location, code, arg);
4347 if (ret == error_mark_node)
4348 return error_mark_node;
4349 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4350 eptype = TREE_TYPE (eptype);
4351 goto return_build_unary_op;
4353 case PREINCREMENT_EXPR:
4354 case POSTINCREMENT_EXPR:
4355 case PREDECREMENT_EXPR:
4356 case POSTDECREMENT_EXPR:
4358 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4360 tree inner = build_unary_op (location, code,
4361 C_MAYBE_CONST_EXPR_EXPR (arg),
4362 noconvert);
4363 if (inner == error_mark_node)
4364 return error_mark_node;
4365 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4366 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4367 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4368 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4369 goto return_build_unary_op;
4372 /* Complain about anything that is not a true lvalue. In
4373 Objective-C, skip this check for property_refs. */
4374 if (!objc_is_property_ref (arg)
4375 && !lvalue_or_else (location,
4376 arg, ((code == PREINCREMENT_EXPR
4377 || code == POSTINCREMENT_EXPR)
4378 ? lv_increment
4379 : lv_decrement)))
4380 return error_mark_node;
4382 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4384 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4385 warning_at (location, OPT_Wc___compat,
4386 "increment of enumeration value is invalid in C++");
4387 else
4388 warning_at (location, OPT_Wc___compat,
4389 "decrement of enumeration value is invalid in C++");
4392 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4394 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4395 warning_at (location, OPT_Wbool_operation,
4396 "increment of a boolean expression");
4397 else
4398 warning_at (location, OPT_Wbool_operation,
4399 "decrement of a boolean expression");
4402 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4403 arg = c_fully_fold (arg, false, NULL);
4405 bool atomic_op;
4406 atomic_op = really_atomic_lvalue (arg);
4408 /* Increment or decrement the real part of the value,
4409 and don't change the imaginary part. */
4410 if (typecode == COMPLEX_TYPE)
4412 tree real, imag;
4414 pedwarn (location, OPT_Wpedantic,
4415 "ISO C does not support %<++%> and %<--%> on complex types");
4417 if (!atomic_op)
4419 arg = stabilize_reference (arg);
4420 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4421 true);
4422 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4423 true);
4424 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4425 if (real == error_mark_node || imag == error_mark_node)
4426 return error_mark_node;
4427 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4428 real, imag);
4429 goto return_build_unary_op;
4433 /* Report invalid types. */
4435 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4436 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4437 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4439 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4440 error_at (location, "wrong type argument to increment");
4441 else
4442 error_at (location, "wrong type argument to decrement");
4444 return error_mark_node;
4448 tree inc;
4450 argtype = TREE_TYPE (arg);
4452 /* Compute the increment. */
4454 if (typecode == POINTER_TYPE)
4456 /* If pointer target is an incomplete type,
4457 we just cannot know how to do the arithmetic. */
4458 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4460 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4461 error_at (location,
4462 "increment of pointer to an incomplete type %qT",
4463 TREE_TYPE (argtype));
4464 else
4465 error_at (location,
4466 "decrement of pointer to an incomplete type %qT",
4467 TREE_TYPE (argtype));
4469 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4470 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4472 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4473 pedwarn (location, OPT_Wpointer_arith,
4474 "wrong type argument to increment");
4475 else
4476 pedwarn (location, OPT_Wpointer_arith,
4477 "wrong type argument to decrement");
4480 inc = c_size_in_bytes (TREE_TYPE (argtype));
4481 inc = convert_to_ptrofftype_loc (location, inc);
4483 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4485 /* For signed fract types, we invert ++ to -- or
4486 -- to ++, and change inc from 1 to -1, because
4487 it is not possible to represent 1 in signed fract constants.
4488 For unsigned fract types, the result always overflows and
4489 we get an undefined (original) or the maximum value. */
4490 if (code == PREINCREMENT_EXPR)
4491 code = PREDECREMENT_EXPR;
4492 else if (code == PREDECREMENT_EXPR)
4493 code = PREINCREMENT_EXPR;
4494 else if (code == POSTINCREMENT_EXPR)
4495 code = POSTDECREMENT_EXPR;
4496 else /* code == POSTDECREMENT_EXPR */
4497 code = POSTINCREMENT_EXPR;
4499 inc = integer_minus_one_node;
4500 inc = convert (argtype, inc);
4502 else
4504 inc = VECTOR_TYPE_P (argtype)
4505 ? build_one_cst (argtype)
4506 : integer_one_node;
4507 inc = convert (argtype, inc);
4510 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4511 need to ask Objective-C to build the increment or decrement
4512 expression for it. */
4513 if (objc_is_property_ref (arg))
4514 return objc_build_incr_expr_for_property_ref (location, code,
4515 arg, inc);
4517 /* Report a read-only lvalue. */
4518 if (TYPE_READONLY (argtype))
4520 readonly_error (location, arg,
4521 ((code == PREINCREMENT_EXPR
4522 || code == POSTINCREMENT_EXPR)
4523 ? lv_increment : lv_decrement));
4524 return error_mark_node;
4526 else if (TREE_READONLY (arg))
4527 readonly_warning (arg,
4528 ((code == PREINCREMENT_EXPR
4529 || code == POSTINCREMENT_EXPR)
4530 ? lv_increment : lv_decrement));
4532 /* If the argument is atomic, use the special code sequences for
4533 atomic compound assignment. */
4534 if (atomic_op)
4536 arg = stabilize_reference (arg);
4537 ret = build_atomic_assign (location, arg,
4538 ((code == PREINCREMENT_EXPR
4539 || code == POSTINCREMENT_EXPR)
4540 ? PLUS_EXPR
4541 : MINUS_EXPR),
4542 (FRACT_MODE_P (TYPE_MODE (argtype))
4543 ? inc
4544 : integer_one_node),
4545 (code == POSTINCREMENT_EXPR
4546 || code == POSTDECREMENT_EXPR));
4547 goto return_build_unary_op;
4550 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4551 val = boolean_increment (code, arg);
4552 else
4553 val = build2 (code, TREE_TYPE (arg), arg, inc);
4554 TREE_SIDE_EFFECTS (val) = 1;
4555 if (TREE_CODE (val) != code)
4556 TREE_NO_WARNING (val) = 1;
4557 ret = val;
4558 goto return_build_unary_op;
4561 case ADDR_EXPR:
4562 /* Note that this operation never does default_conversion. */
4564 /* The operand of unary '&' must be an lvalue (which excludes
4565 expressions of type void), or, in C99, the result of a [] or
4566 unary '*' operator. */
4567 if (VOID_TYPE_P (TREE_TYPE (arg))
4568 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4569 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4570 pedwarn (location, 0, "taking address of expression of type %<void%>");
4572 /* Let &* cancel out to simplify resulting code. */
4573 if (INDIRECT_REF_P (arg))
4575 /* Don't let this be an lvalue. */
4576 if (lvalue_p (TREE_OPERAND (arg, 0)))
4577 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4578 ret = TREE_OPERAND (arg, 0);
4579 goto return_build_unary_op;
4582 /* Anything not already handled and not a true memory reference
4583 or a non-lvalue array is an error. */
4584 if (typecode != FUNCTION_TYPE && !noconvert
4585 && !lvalue_or_else (location, arg, lv_addressof))
4586 return error_mark_node;
4588 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4589 folding later. */
4590 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4592 tree inner = build_unary_op (location, code,
4593 C_MAYBE_CONST_EXPR_EXPR (arg),
4594 noconvert);
4595 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4596 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4597 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4598 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4599 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4600 goto return_build_unary_op;
4603 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4604 argtype = TREE_TYPE (arg);
4606 /* If the lvalue is const or volatile, merge that into the type
4607 to which the address will point. This is only needed
4608 for function types. */
4609 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4610 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4611 && TREE_CODE (argtype) == FUNCTION_TYPE)
4613 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4614 int quals = orig_quals;
4616 if (TREE_READONLY (arg))
4617 quals |= TYPE_QUAL_CONST;
4618 if (TREE_THIS_VOLATILE (arg))
4619 quals |= TYPE_QUAL_VOLATILE;
4621 argtype = c_build_qualified_type (argtype, quals);
4624 switch (TREE_CODE (arg))
4626 case COMPONENT_REF:
4627 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4629 error_at (location, "cannot take address of bit-field %qD",
4630 TREE_OPERAND (arg, 1));
4631 return error_mark_node;
4634 /* fall through */
4636 case ARRAY_REF:
4637 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4639 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4640 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4642 error_at (location, "cannot take address of scalar with "
4643 "reverse storage order");
4644 return error_mark_node;
4647 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4648 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4649 warning_at (location, OPT_Wscalar_storage_order,
4650 "address of array with reverse scalar storage "
4651 "order requested");
4654 default:
4655 break;
4658 if (!c_mark_addressable (arg))
4659 return error_mark_node;
4661 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4662 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4664 argtype = build_pointer_type (argtype);
4666 /* ??? Cope with user tricks that amount to offsetof. Delete this
4667 when we have proper support for integer constant expressions. */
4668 val = get_base_address (arg);
4669 if (val && INDIRECT_REF_P (val)
4670 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4672 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4673 goto return_build_unary_op;
4676 val = build1 (ADDR_EXPR, argtype, arg);
4678 ret = val;
4679 goto return_build_unary_op;
4681 default:
4682 gcc_unreachable ();
4685 if (argtype == NULL_TREE)
4686 argtype = TREE_TYPE (arg);
4687 if (TREE_CODE (arg) == INTEGER_CST)
4688 ret = (require_constant_value
4689 ? fold_build1_initializer_loc (location, code, argtype, arg)
4690 : fold_build1_loc (location, code, argtype, arg));
4691 else
4692 ret = build1 (code, argtype, arg);
4693 return_build_unary_op:
4694 gcc_assert (ret != error_mark_node);
4695 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4696 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4697 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4698 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4699 ret = note_integer_operands (ret);
4700 if (eptype)
4701 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4702 protected_set_expr_location (ret, location);
4703 return ret;
4706 /* Return nonzero if REF is an lvalue valid for this language.
4707 Lvalues can be assigned, unless their type has TYPE_READONLY.
4708 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4710 bool
4711 lvalue_p (const_tree ref)
4713 const enum tree_code code = TREE_CODE (ref);
4715 switch (code)
4717 case REALPART_EXPR:
4718 case IMAGPART_EXPR:
4719 case COMPONENT_REF:
4720 return lvalue_p (TREE_OPERAND (ref, 0));
4722 case C_MAYBE_CONST_EXPR:
4723 return lvalue_p (TREE_OPERAND (ref, 1));
4725 case COMPOUND_LITERAL_EXPR:
4726 case STRING_CST:
4727 return true;
4729 case INDIRECT_REF:
4730 case ARRAY_REF:
4731 case ARRAY_NOTATION_REF:
4732 case VAR_DECL:
4733 case PARM_DECL:
4734 case RESULT_DECL:
4735 case ERROR_MARK:
4736 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4737 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4739 case BIND_EXPR:
4740 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4742 default:
4743 return false;
4747 /* Give a warning for storing in something that is read-only in GCC
4748 terms but not const in ISO C terms. */
4750 static void
4751 readonly_warning (tree arg, enum lvalue_use use)
4753 switch (use)
4755 case lv_assign:
4756 warning (0, "assignment of read-only location %qE", arg);
4757 break;
4758 case lv_increment:
4759 warning (0, "increment of read-only location %qE", arg);
4760 break;
4761 case lv_decrement:
4762 warning (0, "decrement of read-only location %qE", arg);
4763 break;
4764 default:
4765 gcc_unreachable ();
4767 return;
4771 /* Return nonzero if REF is an lvalue valid for this language;
4772 otherwise, print an error message and return zero. USE says
4773 how the lvalue is being used and so selects the error message.
4774 LOCATION is the location at which any error should be reported. */
4776 static int
4777 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4779 int win = lvalue_p (ref);
4781 if (!win)
4782 lvalue_error (loc, use);
4784 return win;
4787 /* Mark EXP saying that we need to be able to take the
4788 address of it; it should not be allocated in a register.
4789 Returns true if successful. ARRAY_REF_P is true if this
4790 is for ARRAY_REF construction - in that case we don't want
4791 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4792 it is fine to use ARRAY_REFs for vector subscripts on vector
4793 register variables. */
4795 bool
4796 c_mark_addressable (tree exp, bool array_ref_p)
4798 tree x = exp;
4800 while (1)
4801 switch (TREE_CODE (x))
4803 case VIEW_CONVERT_EXPR:
4804 if (array_ref_p
4805 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4806 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4807 return true;
4808 /* FALLTHRU */
4809 case COMPONENT_REF:
4810 case ADDR_EXPR:
4811 case ARRAY_REF:
4812 case REALPART_EXPR:
4813 case IMAGPART_EXPR:
4814 x = TREE_OPERAND (x, 0);
4815 break;
4817 case COMPOUND_LITERAL_EXPR:
4818 case CONSTRUCTOR:
4819 TREE_ADDRESSABLE (x) = 1;
4820 return true;
4822 case VAR_DECL:
4823 case CONST_DECL:
4824 case PARM_DECL:
4825 case RESULT_DECL:
4826 if (C_DECL_REGISTER (x)
4827 && DECL_NONLOCAL (x))
4829 if (TREE_PUBLIC (x) || is_global_var (x))
4831 error
4832 ("global register variable %qD used in nested function", x);
4833 return false;
4835 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4837 else if (C_DECL_REGISTER (x))
4839 if (TREE_PUBLIC (x) || is_global_var (x))
4840 error ("address of global register variable %qD requested", x);
4841 else
4842 error ("address of register variable %qD requested", x);
4843 return false;
4846 /* FALLTHRU */
4847 case FUNCTION_DECL:
4848 TREE_ADDRESSABLE (x) = 1;
4849 /* FALLTHRU */
4850 default:
4851 return true;
4855 /* Convert EXPR to TYPE, warning about conversion problems with
4856 constants. SEMANTIC_TYPE is the type this conversion would use
4857 without excess precision. If SEMANTIC_TYPE is NULL, this function
4858 is equivalent to convert_and_check. This function is a wrapper that
4859 handles conversions that may be different than
4860 the usual ones because of excess precision. */
4862 static tree
4863 ep_convert_and_check (location_t loc, tree type, tree expr,
4864 tree semantic_type)
4866 if (TREE_TYPE (expr) == type)
4867 return expr;
4869 /* For C11, integer conversions may have results with excess
4870 precision. */
4871 if (flag_isoc11 || !semantic_type)
4872 return convert_and_check (loc, type, expr);
4874 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4875 && TREE_TYPE (expr) != semantic_type)
4877 /* For integers, we need to check the real conversion, not
4878 the conversion to the excess precision type. */
4879 expr = convert_and_check (loc, semantic_type, expr);
4881 /* Result type is the excess precision type, which should be
4882 large enough, so do not check. */
4883 return convert (type, expr);
4886 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4887 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4888 if folded to an integer constant then the unselected half may
4889 contain arbitrary operations not normally permitted in constant
4890 expressions. Set the location of the expression to LOC. */
4892 tree
4893 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4894 tree op1, tree op1_original_type, location_t op1_loc,
4895 tree op2, tree op2_original_type, location_t op2_loc)
4897 tree type1;
4898 tree type2;
4899 enum tree_code code1;
4900 enum tree_code code2;
4901 tree result_type = NULL;
4902 tree semantic_result_type = NULL;
4903 tree orig_op1 = op1, orig_op2 = op2;
4904 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4905 bool ifexp_int_operands;
4906 tree ret;
4908 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4909 if (op1_int_operands)
4910 op1 = remove_c_maybe_const_expr (op1);
4911 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4912 if (op2_int_operands)
4913 op2 = remove_c_maybe_const_expr (op2);
4914 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4915 if (ifexp_int_operands)
4916 ifexp = remove_c_maybe_const_expr (ifexp);
4918 /* Promote both alternatives. */
4920 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4921 op1 = default_conversion (op1);
4922 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4923 op2 = default_conversion (op2);
4925 if (TREE_CODE (ifexp) == ERROR_MARK
4926 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4927 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4928 return error_mark_node;
4930 type1 = TREE_TYPE (op1);
4931 code1 = TREE_CODE (type1);
4932 type2 = TREE_TYPE (op2);
4933 code2 = TREE_CODE (type2);
4935 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4936 return error_mark_node;
4938 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4939 return error_mark_node;
4941 /* C90 does not permit non-lvalue arrays in conditional expressions.
4942 In C99 they will be pointers by now. */
4943 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4945 error_at (colon_loc, "non-lvalue array in conditional expression");
4946 return error_mark_node;
4949 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4950 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4951 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4952 || code1 == COMPLEX_TYPE)
4953 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4954 || code2 == COMPLEX_TYPE))
4956 semantic_result_type = c_common_type (type1, type2);
4957 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4959 op1 = TREE_OPERAND (op1, 0);
4960 type1 = TREE_TYPE (op1);
4961 gcc_assert (TREE_CODE (type1) == code1);
4963 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4965 op2 = TREE_OPERAND (op2, 0);
4966 type2 = TREE_TYPE (op2);
4967 gcc_assert (TREE_CODE (type2) == code2);
4971 if (warn_cxx_compat)
4973 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4974 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4976 if (TREE_CODE (t1) == ENUMERAL_TYPE
4977 && TREE_CODE (t2) == ENUMERAL_TYPE
4978 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4979 warning_at (colon_loc, OPT_Wc___compat,
4980 ("different enum types in conditional is "
4981 "invalid in C++: %qT vs %qT"),
4982 t1, t2);
4985 /* Quickly detect the usual case where op1 and op2 have the same type
4986 after promotion. */
4987 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4989 if (type1 == type2)
4990 result_type = type1;
4991 else
4992 result_type = TYPE_MAIN_VARIANT (type1);
4994 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4995 || code1 == COMPLEX_TYPE)
4996 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4997 || code2 == COMPLEX_TYPE))
4999 /* In C11, a conditional expression between a floating-point
5000 type and an integer type should convert the integer type to
5001 the evaluation format of the floating-point type, with
5002 possible excess precision. */
5003 tree eptype1 = type1;
5004 tree eptype2 = type2;
5005 if (flag_isoc11)
5007 tree eptype;
5008 if (ANY_INTEGRAL_TYPE_P (type1)
5009 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5011 eptype2 = eptype;
5012 if (!semantic_result_type)
5013 semantic_result_type = c_common_type (type1, type2);
5015 else if (ANY_INTEGRAL_TYPE_P (type2)
5016 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5018 eptype1 = eptype;
5019 if (!semantic_result_type)
5020 semantic_result_type = c_common_type (type1, type2);
5023 result_type = c_common_type (eptype1, eptype2);
5024 if (result_type == error_mark_node)
5025 return error_mark_node;
5026 do_warn_double_promotion (result_type, type1, type2,
5027 "implicit conversion from %qT to %qT to "
5028 "match other result of conditional",
5029 colon_loc);
5031 /* If -Wsign-compare, warn here if type1 and type2 have
5032 different signedness. We'll promote the signed to unsigned
5033 and later code won't know it used to be different.
5034 Do this check on the original types, so that explicit casts
5035 will be considered, but default promotions won't. */
5036 if (c_inhibit_evaluation_warnings == 0)
5038 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5039 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5041 if (unsigned_op1 ^ unsigned_op2)
5043 bool ovf;
5045 /* Do not warn if the result type is signed, since the
5046 signed type will only be chosen if it can represent
5047 all the values of the unsigned type. */
5048 if (!TYPE_UNSIGNED (result_type))
5049 /* OK */;
5050 else
5052 bool op1_maybe_const = true;
5053 bool op2_maybe_const = true;
5055 /* Do not warn if the signed quantity is an
5056 unsuffixed integer literal (or some static
5057 constant expression involving such literals) and
5058 it is non-negative. This warning requires the
5059 operands to be folded for best results, so do
5060 that folding in this case even without
5061 warn_sign_compare to avoid warning options
5062 possibly affecting code generation. */
5063 c_inhibit_evaluation_warnings
5064 += (ifexp == truthvalue_false_node);
5065 op1 = c_fully_fold (op1, require_constant_value,
5066 &op1_maybe_const);
5067 c_inhibit_evaluation_warnings
5068 -= (ifexp == truthvalue_false_node);
5070 c_inhibit_evaluation_warnings
5071 += (ifexp == truthvalue_true_node);
5072 op2 = c_fully_fold (op2, require_constant_value,
5073 &op2_maybe_const);
5074 c_inhibit_evaluation_warnings
5075 -= (ifexp == truthvalue_true_node);
5077 if (warn_sign_compare)
5079 if ((unsigned_op2
5080 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5081 || (unsigned_op1
5082 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5083 /* OK */;
5084 else if (unsigned_op2)
5085 warning_at (op1_loc, OPT_Wsign_compare,
5086 "operand of ?: changes signedness from "
5087 "%qT to %qT due to unsignedness of other "
5088 "operand", TREE_TYPE (orig_op1),
5089 TREE_TYPE (orig_op2));
5090 else
5091 warning_at (op2_loc, OPT_Wsign_compare,
5092 "operand of ?: changes signedness from "
5093 "%qT to %qT due to unsignedness of other "
5094 "operand", TREE_TYPE (orig_op2),
5095 TREE_TYPE (orig_op1));
5097 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5098 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5099 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5100 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5105 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5107 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5108 pedwarn (colon_loc, OPT_Wpedantic,
5109 "ISO C forbids conditional expr with only one void side");
5110 result_type = void_type_node;
5112 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5114 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5115 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5116 addr_space_t as_common;
5118 if (comp_target_types (colon_loc, type1, type2))
5119 result_type = common_pointer_type (type1, type2);
5120 else if (null_pointer_constant_p (orig_op1))
5121 result_type = type2;
5122 else if (null_pointer_constant_p (orig_op2))
5123 result_type = type1;
5124 else if (!addr_space_superset (as1, as2, &as_common))
5126 error_at (colon_loc, "pointers to disjoint address spaces "
5127 "used in conditional expression");
5128 return error_mark_node;
5130 else if (VOID_TYPE_P (TREE_TYPE (type1))
5131 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5133 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5134 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5135 & ~TYPE_QUALS (TREE_TYPE (type1))))
5136 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5137 "pointer to array loses qualifier "
5138 "in conditional expression");
5140 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5141 pedwarn (colon_loc, OPT_Wpedantic,
5142 "ISO C forbids conditional expr between "
5143 "%<void *%> and function pointer");
5144 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5145 TREE_TYPE (type2)));
5147 else if (VOID_TYPE_P (TREE_TYPE (type2))
5148 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5150 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5151 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5152 & ~TYPE_QUALS (TREE_TYPE (type2))))
5153 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5154 "pointer to array loses qualifier "
5155 "in conditional expression");
5157 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5158 pedwarn (colon_loc, OPT_Wpedantic,
5159 "ISO C forbids conditional expr between "
5160 "%<void *%> and function pointer");
5161 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5162 TREE_TYPE (type1)));
5164 /* Objective-C pointer comparisons are a bit more lenient. */
5165 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5166 result_type = objc_common_type (type1, type2);
5167 else
5169 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5171 pedwarn (colon_loc, 0,
5172 "pointer type mismatch in conditional expression");
5173 result_type = build_pointer_type
5174 (build_qualified_type (void_type_node, qual));
5177 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5179 if (!null_pointer_constant_p (orig_op2))
5180 pedwarn (colon_loc, 0,
5181 "pointer/integer type mismatch in conditional expression");
5182 else
5184 op2 = null_pointer_node;
5186 result_type = type1;
5188 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5190 if (!null_pointer_constant_p (orig_op1))
5191 pedwarn (colon_loc, 0,
5192 "pointer/integer type mismatch in conditional expression");
5193 else
5195 op1 = null_pointer_node;
5197 result_type = type2;
5200 if (!result_type)
5202 if (flag_cond_mismatch)
5203 result_type = void_type_node;
5204 else
5206 error_at (colon_loc, "type mismatch in conditional expression");
5207 return error_mark_node;
5211 /* Merge const and volatile flags of the incoming types. */
5212 result_type
5213 = build_type_variant (result_type,
5214 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5215 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5217 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5218 semantic_result_type);
5219 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5220 semantic_result_type);
5222 if (ifexp_bcp && ifexp == truthvalue_true_node)
5224 op2_int_operands = true;
5225 op1 = c_fully_fold (op1, require_constant_value, NULL);
5227 if (ifexp_bcp && ifexp == truthvalue_false_node)
5229 op1_int_operands = true;
5230 op2 = c_fully_fold (op2, require_constant_value, NULL);
5232 int_const = int_operands = (ifexp_int_operands
5233 && op1_int_operands
5234 && op2_int_operands);
5235 if (int_operands)
5237 int_const = ((ifexp == truthvalue_true_node
5238 && TREE_CODE (orig_op1) == INTEGER_CST
5239 && !TREE_OVERFLOW (orig_op1))
5240 || (ifexp == truthvalue_false_node
5241 && TREE_CODE (orig_op2) == INTEGER_CST
5242 && !TREE_OVERFLOW (orig_op2)));
5245 /* Need to convert condition operand into a vector mask. */
5246 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5248 tree vectype = TREE_TYPE (ifexp);
5249 tree elem_type = TREE_TYPE (vectype);
5250 tree zero = build_int_cst (elem_type, 0);
5251 tree zero_vec = build_vector_from_val (vectype, zero);
5252 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5253 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5256 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5257 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5258 else
5260 if (int_operands)
5262 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5263 nested inside of the expression. */
5264 op1 = c_fully_fold (op1, false, NULL);
5265 op2 = c_fully_fold (op2, false, NULL);
5267 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5268 if (int_operands)
5269 ret = note_integer_operands (ret);
5271 if (semantic_result_type)
5272 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5274 protected_set_expr_location (ret, colon_loc);
5276 /* If the OP1 and OP2 are the same and don't have side-effects,
5277 warn here, because the COND_EXPR will be turned into OP1. */
5278 if (warn_duplicated_branches
5279 && TREE_CODE (ret) == COND_EXPR
5280 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5281 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5282 "this condition has identical branches");
5284 return ret;
5287 /* Return a compound expression that performs two expressions and
5288 returns the value of the second of them.
5290 LOC is the location of the COMPOUND_EXPR. */
5292 tree
5293 build_compound_expr (location_t loc, tree expr1, tree expr2)
5295 bool expr1_int_operands, expr2_int_operands;
5296 tree eptype = NULL_TREE;
5297 tree ret;
5299 if (flag_cilkplus
5300 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5301 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5303 error_at (loc,
5304 "spawned function call cannot be part of a comma expression");
5305 return error_mark_node;
5307 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5308 if (expr1_int_operands)
5309 expr1 = remove_c_maybe_const_expr (expr1);
5310 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5311 if (expr2_int_operands)
5312 expr2 = remove_c_maybe_const_expr (expr2);
5314 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5315 expr1 = TREE_OPERAND (expr1, 0);
5316 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5318 eptype = TREE_TYPE (expr2);
5319 expr2 = TREE_OPERAND (expr2, 0);
5322 if (!TREE_SIDE_EFFECTS (expr1))
5324 /* The left-hand operand of a comma expression is like an expression
5325 statement: with -Wunused, we should warn if it doesn't have
5326 any side-effects, unless it was explicitly cast to (void). */
5327 if (warn_unused_value)
5329 if (VOID_TYPE_P (TREE_TYPE (expr1))
5330 && CONVERT_EXPR_P (expr1))
5331 ; /* (void) a, b */
5332 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5333 && TREE_CODE (expr1) == COMPOUND_EXPR
5334 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5335 ; /* (void) a, (void) b, c */
5336 else
5337 warning_at (loc, OPT_Wunused_value,
5338 "left-hand operand of comma expression has no effect");
5341 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5342 && warn_unused_value)
5344 tree r = expr1;
5345 location_t cloc = loc;
5346 while (TREE_CODE (r) == COMPOUND_EXPR)
5348 if (EXPR_HAS_LOCATION (r))
5349 cloc = EXPR_LOCATION (r);
5350 r = TREE_OPERAND (r, 1);
5352 if (!TREE_SIDE_EFFECTS (r)
5353 && !VOID_TYPE_P (TREE_TYPE (r))
5354 && !CONVERT_EXPR_P (r))
5355 warning_at (cloc, OPT_Wunused_value,
5356 "right-hand operand of comma expression has no effect");
5359 /* With -Wunused, we should also warn if the left-hand operand does have
5360 side-effects, but computes a value which is not used. For example, in
5361 `foo() + bar(), baz()' the result of the `+' operator is not used,
5362 so we should issue a warning. */
5363 else if (warn_unused_value)
5364 warn_if_unused_value (expr1, loc);
5366 if (expr2 == error_mark_node)
5367 return error_mark_node;
5369 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5371 if (flag_isoc99
5372 && expr1_int_operands
5373 && expr2_int_operands)
5374 ret = note_integer_operands (ret);
5376 if (eptype)
5377 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5379 protected_set_expr_location (ret, loc);
5380 return ret;
5383 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5384 which we are casting. OTYPE is the type of the expression being
5385 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5386 of the cast. -Wcast-qual appeared on the command line. Named
5387 address space qualifiers are not handled here, because they result
5388 in different warnings. */
5390 static void
5391 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5393 tree in_type = type;
5394 tree in_otype = otype;
5395 int added = 0;
5396 int discarded = 0;
5397 bool is_const;
5399 /* Check that the qualifiers on IN_TYPE are a superset of the
5400 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5401 nodes is uninteresting and we stop as soon as we hit a
5402 non-POINTER_TYPE node on either type. */
5405 in_otype = TREE_TYPE (in_otype);
5406 in_type = TREE_TYPE (in_type);
5408 /* GNU C allows cv-qualified function types. 'const' means the
5409 function is very pure, 'volatile' means it can't return. We
5410 need to warn when such qualifiers are added, not when they're
5411 taken away. */
5412 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5413 && TREE_CODE (in_type) == FUNCTION_TYPE)
5414 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5415 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5416 else
5417 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5418 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5420 while (TREE_CODE (in_type) == POINTER_TYPE
5421 && TREE_CODE (in_otype) == POINTER_TYPE);
5423 if (added)
5424 warning_at (loc, OPT_Wcast_qual,
5425 "cast adds %q#v qualifier to function type", added);
5427 if (discarded)
5428 /* There are qualifiers present in IN_OTYPE that are not present
5429 in IN_TYPE. */
5430 warning_at (loc, OPT_Wcast_qual,
5431 "cast discards %qv qualifier from pointer target type",
5432 discarded);
5434 if (added || discarded)
5435 return;
5437 /* A cast from **T to const **T is unsafe, because it can cause a
5438 const value to be changed with no additional warning. We only
5439 issue this warning if T is the same on both sides, and we only
5440 issue the warning if there are the same number of pointers on
5441 both sides, as otherwise the cast is clearly unsafe anyhow. A
5442 cast is unsafe when a qualifier is added at one level and const
5443 is not present at all outer levels.
5445 To issue this warning, we check at each level whether the cast
5446 adds new qualifiers not already seen. We don't need to special
5447 case function types, as they won't have the same
5448 TYPE_MAIN_VARIANT. */
5450 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5451 return;
5452 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5453 return;
5455 in_type = type;
5456 in_otype = otype;
5457 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5460 in_type = TREE_TYPE (in_type);
5461 in_otype = TREE_TYPE (in_otype);
5462 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5463 && !is_const)
5465 warning_at (loc, OPT_Wcast_qual,
5466 "to be safe all intermediate pointers in cast from "
5467 "%qT to %qT must be %<const%> qualified",
5468 otype, type);
5469 break;
5471 if (is_const)
5472 is_const = TYPE_READONLY (in_type);
5474 while (TREE_CODE (in_type) == POINTER_TYPE);
5477 /* Build an expression representing a cast to type TYPE of expression EXPR.
5478 LOC is the location of the cast-- typically the open paren of the cast. */
5480 tree
5481 build_c_cast (location_t loc, tree type, tree expr)
5483 tree value;
5485 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5486 expr = TREE_OPERAND (expr, 0);
5488 value = expr;
5490 if (type == error_mark_node || expr == error_mark_node)
5491 return error_mark_node;
5493 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5494 only in <protocol> qualifications. But when constructing cast expressions,
5495 the protocols do matter and must be kept around. */
5496 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5497 return build1 (NOP_EXPR, type, expr);
5499 type = TYPE_MAIN_VARIANT (type);
5501 if (TREE_CODE (type) == ARRAY_TYPE)
5503 error_at (loc, "cast specifies array type");
5504 return error_mark_node;
5507 if (TREE_CODE (type) == FUNCTION_TYPE)
5509 error_at (loc, "cast specifies function type");
5510 return error_mark_node;
5513 if (!VOID_TYPE_P (type))
5515 value = require_complete_type (loc, value);
5516 if (value == error_mark_node)
5517 return error_mark_node;
5520 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5522 if (RECORD_OR_UNION_TYPE_P (type))
5523 pedwarn (loc, OPT_Wpedantic,
5524 "ISO C forbids casting nonscalar to the same type");
5526 /* Convert to remove any qualifiers from VALUE's type. */
5527 value = convert (type, value);
5529 else if (TREE_CODE (type) == UNION_TYPE)
5531 tree field;
5533 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5534 if (TREE_TYPE (field) != error_mark_node
5535 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5536 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5537 break;
5539 if (field)
5541 tree t;
5542 bool maybe_const = true;
5544 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5545 t = c_fully_fold (value, false, &maybe_const);
5546 t = build_constructor_single (type, field, t);
5547 if (!maybe_const)
5548 t = c_wrap_maybe_const (t, true);
5549 t = digest_init (loc, type, t,
5550 NULL_TREE, false, true, 0);
5551 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5552 return t;
5554 error_at (loc, "cast to union type from type not present in union");
5555 return error_mark_node;
5557 else
5559 tree otype, ovalue;
5561 if (type == void_type_node)
5563 tree t = build1 (CONVERT_EXPR, type, value);
5564 SET_EXPR_LOCATION (t, loc);
5565 return t;
5568 otype = TREE_TYPE (value);
5570 /* Optionally warn about potentially worrisome casts. */
5571 if (warn_cast_qual
5572 && TREE_CODE (type) == POINTER_TYPE
5573 && TREE_CODE (otype) == POINTER_TYPE)
5574 handle_warn_cast_qual (loc, type, otype);
5576 /* Warn about conversions between pointers to disjoint
5577 address spaces. */
5578 if (TREE_CODE (type) == POINTER_TYPE
5579 && TREE_CODE (otype) == POINTER_TYPE
5580 && !null_pointer_constant_p (value))
5582 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5583 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5584 addr_space_t as_common;
5586 if (!addr_space_superset (as_to, as_from, &as_common))
5588 if (ADDR_SPACE_GENERIC_P (as_from))
5589 warning_at (loc, 0, "cast to %s address space pointer "
5590 "from disjoint generic address space pointer",
5591 c_addr_space_name (as_to));
5593 else if (ADDR_SPACE_GENERIC_P (as_to))
5594 warning_at (loc, 0, "cast to generic address space pointer "
5595 "from disjoint %s address space pointer",
5596 c_addr_space_name (as_from));
5598 else
5599 warning_at (loc, 0, "cast to %s address space pointer "
5600 "from disjoint %s address space pointer",
5601 c_addr_space_name (as_to),
5602 c_addr_space_name (as_from));
5606 /* Warn about possible alignment problems. */
5607 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5608 && TREE_CODE (type) == POINTER_TYPE
5609 && TREE_CODE (otype) == POINTER_TYPE
5610 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5611 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5612 /* Don't warn about opaque types, where the actual alignment
5613 restriction is unknown. */
5614 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5615 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5616 && min_align_of_type (TREE_TYPE (type))
5617 > min_align_of_type (TREE_TYPE (otype)))
5618 warning_at (loc, OPT_Wcast_align,
5619 "cast increases required alignment of target type");
5621 if (TREE_CODE (type) == INTEGER_TYPE
5622 && TREE_CODE (otype) == POINTER_TYPE
5623 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5624 /* Unlike conversion of integers to pointers, where the
5625 warning is disabled for converting constants because
5626 of cases such as SIG_*, warn about converting constant
5627 pointers to integers. In some cases it may cause unwanted
5628 sign extension, and a warning is appropriate. */
5629 warning_at (loc, OPT_Wpointer_to_int_cast,
5630 "cast from pointer to integer of different size");
5632 if (TREE_CODE (value) == CALL_EXPR
5633 && TREE_CODE (type) != TREE_CODE (otype))
5634 warning_at (loc, OPT_Wbad_function_cast,
5635 "cast from function call of type %qT "
5636 "to non-matching type %qT", otype, type);
5638 if (TREE_CODE (type) == POINTER_TYPE
5639 && TREE_CODE (otype) == INTEGER_TYPE
5640 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5641 /* Don't warn about converting any constant. */
5642 && !TREE_CONSTANT (value))
5643 warning_at (loc,
5644 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5645 "of different size");
5647 if (warn_strict_aliasing <= 2)
5648 strict_aliasing_warning (otype, type, expr);
5650 /* If pedantic, warn for conversions between function and object
5651 pointer types, except for converting a null pointer constant
5652 to function pointer type. */
5653 if (pedantic
5654 && TREE_CODE (type) == POINTER_TYPE
5655 && TREE_CODE (otype) == POINTER_TYPE
5656 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5657 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5658 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5659 "conversion of function pointer to object pointer type");
5661 if (pedantic
5662 && TREE_CODE (type) == POINTER_TYPE
5663 && TREE_CODE (otype) == POINTER_TYPE
5664 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5665 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5666 && !null_pointer_constant_p (value))
5667 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5668 "conversion of object pointer to function pointer type");
5670 ovalue = value;
5671 value = convert (type, value);
5673 /* Ignore any integer overflow caused by the cast. */
5674 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5676 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5678 if (!TREE_OVERFLOW (value))
5680 /* Avoid clobbering a shared constant. */
5681 value = copy_node (value);
5682 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5685 else if (TREE_OVERFLOW (value))
5686 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5687 value = wide_int_to_tree (TREE_TYPE (value), value);
5691 /* Don't let a cast be an lvalue. */
5692 if (lvalue_p (value))
5693 value = non_lvalue_loc (loc, value);
5695 /* Don't allow the results of casting to floating-point or complex
5696 types be confused with actual constants, or casts involving
5697 integer and pointer types other than direct integer-to-integer
5698 and integer-to-pointer be confused with integer constant
5699 expressions and null pointer constants. */
5700 if (TREE_CODE (value) == REAL_CST
5701 || TREE_CODE (value) == COMPLEX_CST
5702 || (TREE_CODE (value) == INTEGER_CST
5703 && !((TREE_CODE (expr) == INTEGER_CST
5704 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5705 || TREE_CODE (expr) == REAL_CST
5706 || TREE_CODE (expr) == COMPLEX_CST)))
5707 value = build1 (NOP_EXPR, type, value);
5709 protected_set_expr_location (value, loc);
5710 return value;
5713 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5714 location of the open paren of the cast, or the position of the cast
5715 expr. */
5716 tree
5717 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5719 tree type;
5720 tree type_expr = NULL_TREE;
5721 bool type_expr_const = true;
5722 tree ret;
5723 int saved_wsp = warn_strict_prototypes;
5725 /* This avoids warnings about unprototyped casts on
5726 integers. E.g. "#define SIG_DFL (void(*)())0". */
5727 if (TREE_CODE (expr) == INTEGER_CST)
5728 warn_strict_prototypes = 0;
5729 type = groktypename (type_name, &type_expr, &type_expr_const);
5730 warn_strict_prototypes = saved_wsp;
5732 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5733 && reject_gcc_builtin (expr))
5734 return error_mark_node;
5736 ret = build_c_cast (loc, type, expr);
5737 if (type_expr)
5739 bool inner_expr_const = true;
5740 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5741 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5742 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5743 && inner_expr_const);
5744 SET_EXPR_LOCATION (ret, loc);
5747 if (!EXPR_HAS_LOCATION (ret))
5748 protected_set_expr_location (ret, loc);
5750 /* C++ does not permits types to be defined in a cast, but it
5751 allows references to incomplete types. */
5752 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5753 warning_at (loc, OPT_Wc___compat,
5754 "defining a type in a cast is invalid in C++");
5756 return ret;
5759 /* Build an assignment expression of lvalue LHS from value RHS.
5760 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5761 may differ from TREE_TYPE (LHS) for an enum bitfield.
5762 MODIFYCODE is the code for a binary operator that we use
5763 to combine the old value of LHS with RHS to get the new value.
5764 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5765 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5766 which may differ from TREE_TYPE (RHS) for an enum value.
5768 LOCATION is the location of the MODIFYCODE operator.
5769 RHS_LOC is the location of the RHS. */
5771 tree
5772 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5773 enum tree_code modifycode,
5774 location_t rhs_loc, tree rhs, tree rhs_origtype)
5776 tree result;
5777 tree newrhs;
5778 tree rhseval = NULL_TREE;
5779 tree lhstype = TREE_TYPE (lhs);
5780 tree olhstype = lhstype;
5781 bool npc;
5782 bool is_atomic_op;
5784 /* Types that aren't fully specified cannot be used in assignments. */
5785 lhs = require_complete_type (location, lhs);
5787 /* Avoid duplicate error messages from operands that had errors. */
5788 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5789 return error_mark_node;
5791 /* Ensure an error for assigning a non-lvalue array to an array in
5792 C90. */
5793 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5795 error_at (location, "assignment to expression with array type");
5796 return error_mark_node;
5799 /* For ObjC properties, defer this check. */
5800 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5801 return error_mark_node;
5803 is_atomic_op = really_atomic_lvalue (lhs);
5805 newrhs = rhs;
5807 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5809 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5810 lhs_origtype, modifycode, rhs_loc, rhs,
5811 rhs_origtype);
5812 if (inner == error_mark_node)
5813 return error_mark_node;
5814 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5815 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5816 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5817 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5818 protected_set_expr_location (result, location);
5819 return result;
5822 /* If a binary op has been requested, combine the old LHS value with the RHS
5823 producing the value we should actually store into the LHS. */
5825 if (modifycode != NOP_EXPR)
5827 lhs = c_fully_fold (lhs, false, NULL);
5828 lhs = stabilize_reference (lhs);
5830 /* Construct the RHS for any non-atomic compound assignemnt. */
5831 if (!is_atomic_op)
5833 /* If in LHS op= RHS the RHS has side-effects, ensure they
5834 are preevaluated before the rest of the assignment expression's
5835 side-effects, because RHS could contain e.g. function calls
5836 that modify LHS. */
5837 if (TREE_SIDE_EFFECTS (rhs))
5839 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5840 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5841 else
5842 newrhs = save_expr (rhs);
5843 rhseval = newrhs;
5844 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5845 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5846 newrhs);
5848 newrhs = build_binary_op (location,
5849 modifycode, lhs, newrhs, true);
5851 /* The original type of the right hand side is no longer
5852 meaningful. */
5853 rhs_origtype = NULL_TREE;
5857 if (c_dialect_objc ())
5859 /* Check if we are modifying an Objective-C property reference;
5860 if so, we need to generate setter calls. */
5861 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5862 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5863 else
5864 result = objc_maybe_build_modify_expr (lhs, newrhs);
5865 if (result)
5866 goto return_result;
5868 /* Else, do the check that we postponed for Objective-C. */
5869 if (!lvalue_or_else (location, lhs, lv_assign))
5870 return error_mark_node;
5873 /* Give an error for storing in something that is 'const'. */
5875 if (TYPE_READONLY (lhstype)
5876 || (RECORD_OR_UNION_TYPE_P (lhstype)
5877 && C_TYPE_FIELDS_READONLY (lhstype)))
5879 readonly_error (location, lhs, lv_assign);
5880 return error_mark_node;
5882 else if (TREE_READONLY (lhs))
5883 readonly_warning (lhs, lv_assign);
5885 /* If storing into a structure or union member,
5886 it has probably been given type `int'.
5887 Compute the type that would go with
5888 the actual amount of storage the member occupies. */
5890 if (TREE_CODE (lhs) == COMPONENT_REF
5891 && (TREE_CODE (lhstype) == INTEGER_TYPE
5892 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5893 || TREE_CODE (lhstype) == REAL_TYPE
5894 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5895 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5897 /* If storing in a field that is in actuality a short or narrower than one,
5898 we must store in the field in its actual type. */
5900 if (lhstype != TREE_TYPE (lhs))
5902 lhs = copy_node (lhs);
5903 TREE_TYPE (lhs) = lhstype;
5906 /* Issue -Wc++-compat warnings about an assignment to an enum type
5907 when LHS does not have its original type. This happens for,
5908 e.g., an enum bitfield in a struct. */
5909 if (warn_cxx_compat
5910 && lhs_origtype != NULL_TREE
5911 && lhs_origtype != lhstype
5912 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5914 tree checktype = (rhs_origtype != NULL_TREE
5915 ? rhs_origtype
5916 : TREE_TYPE (rhs));
5917 if (checktype != error_mark_node
5918 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5919 || (is_atomic_op && modifycode != NOP_EXPR)))
5920 warning_at (location, OPT_Wc___compat,
5921 "enum conversion in assignment is invalid in C++");
5924 /* If the lhs is atomic, remove that qualifier. */
5925 if (is_atomic_op)
5927 lhstype = build_qualified_type (lhstype,
5928 (TYPE_QUALS (lhstype)
5929 & ~TYPE_QUAL_ATOMIC));
5930 olhstype = build_qualified_type (olhstype,
5931 (TYPE_QUALS (lhstype)
5932 & ~TYPE_QUAL_ATOMIC));
5935 /* Convert new value to destination type. Fold it first, then
5936 restore any excess precision information, for the sake of
5937 conversion warnings. */
5939 if (!(is_atomic_op && modifycode != NOP_EXPR))
5941 tree rhs_semantic_type = NULL_TREE;
5942 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5944 rhs_semantic_type = TREE_TYPE (newrhs);
5945 newrhs = TREE_OPERAND (newrhs, 0);
5947 npc = null_pointer_constant_p (newrhs);
5948 newrhs = c_fully_fold (newrhs, false, NULL);
5949 if (rhs_semantic_type)
5950 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5951 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5952 rhs_origtype, ic_assign, npc,
5953 NULL_TREE, NULL_TREE, 0);
5954 if (TREE_CODE (newrhs) == ERROR_MARK)
5955 return error_mark_node;
5958 /* Emit ObjC write barrier, if necessary. */
5959 if (c_dialect_objc () && flag_objc_gc)
5961 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5962 if (result)
5964 protected_set_expr_location (result, location);
5965 goto return_result;
5969 /* Scan operands. */
5971 if (is_atomic_op)
5972 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5973 else
5975 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5976 TREE_SIDE_EFFECTS (result) = 1;
5977 protected_set_expr_location (result, location);
5980 /* If we got the LHS in a different type for storing in,
5981 convert the result back to the nominal type of LHS
5982 so that the value we return always has the same type
5983 as the LHS argument. */
5985 if (olhstype == TREE_TYPE (result))
5986 goto return_result;
5988 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5989 rhs_origtype, ic_assign, false, NULL_TREE,
5990 NULL_TREE, 0);
5991 protected_set_expr_location (result, location);
5993 return_result:
5994 if (rhseval)
5995 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5996 return result;
5999 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6000 This is used to implement -fplan9-extensions. */
6002 static bool
6003 find_anonymous_field_with_type (tree struct_type, tree type)
6005 tree field;
6006 bool found;
6008 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6009 found = false;
6010 for (field = TYPE_FIELDS (struct_type);
6011 field != NULL_TREE;
6012 field = TREE_CHAIN (field))
6014 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6015 ? c_build_qualified_type (TREE_TYPE (field),
6016 TYPE_QUAL_ATOMIC)
6017 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6018 if (DECL_NAME (field) == NULL
6019 && comptypes (type, fieldtype))
6021 if (found)
6022 return false;
6023 found = true;
6025 else if (DECL_NAME (field) == NULL
6026 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6027 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6029 if (found)
6030 return false;
6031 found = true;
6034 return found;
6037 /* RHS is an expression whose type is pointer to struct. If there is
6038 an anonymous field in RHS with type TYPE, then return a pointer to
6039 that field in RHS. This is used with -fplan9-extensions. This
6040 returns NULL if no conversion could be found. */
6042 static tree
6043 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6045 tree rhs_struct_type, lhs_main_type;
6046 tree field, found_field;
6047 bool found_sub_field;
6048 tree ret;
6050 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6051 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6052 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6054 gcc_assert (POINTER_TYPE_P (type));
6055 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6056 ? c_build_qualified_type (TREE_TYPE (type),
6057 TYPE_QUAL_ATOMIC)
6058 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6060 found_field = NULL_TREE;
6061 found_sub_field = false;
6062 for (field = TYPE_FIELDS (rhs_struct_type);
6063 field != NULL_TREE;
6064 field = TREE_CHAIN (field))
6066 if (DECL_NAME (field) != NULL_TREE
6067 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6068 continue;
6069 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6070 ? c_build_qualified_type (TREE_TYPE (field),
6071 TYPE_QUAL_ATOMIC)
6072 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6073 if (comptypes (lhs_main_type, fieldtype))
6075 if (found_field != NULL_TREE)
6076 return NULL_TREE;
6077 found_field = field;
6079 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6080 lhs_main_type))
6082 if (found_field != NULL_TREE)
6083 return NULL_TREE;
6084 found_field = field;
6085 found_sub_field = true;
6089 if (found_field == NULL_TREE)
6090 return NULL_TREE;
6092 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6093 build_fold_indirect_ref (rhs), found_field,
6094 NULL_TREE);
6095 ret = build_fold_addr_expr_loc (location, ret);
6097 if (found_sub_field)
6099 ret = convert_to_anonymous_field (location, type, ret);
6100 gcc_assert (ret != NULL_TREE);
6103 return ret;
6106 /* Issue an error message for a bad initializer component.
6107 GMSGID identifies the message.
6108 The component name is taken from the spelling stack. */
6110 static void
6111 error_init (location_t loc, const char *gmsgid)
6113 char *ofwhat;
6115 /* The gmsgid may be a format string with %< and %>. */
6116 error_at (loc, gmsgid);
6117 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6118 if (*ofwhat)
6119 inform (loc, "(near initialization for %qs)", ofwhat);
6122 /* Issue a pedantic warning for a bad initializer component. OPT is
6123 the option OPT_* (from options.h) controlling this warning or 0 if
6124 it is unconditionally given. GMSGID identifies the message. The
6125 component name is taken from the spelling stack. */
6127 static void ATTRIBUTE_GCC_DIAG (3,0)
6128 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6130 /* Use the location where a macro was expanded rather than where
6131 it was defined to make sure macros defined in system headers
6132 but used incorrectly elsewhere are diagnosed. */
6133 source_location exploc = expansion_point_location_if_in_system_header (loc);
6135 va_list ap;
6136 va_start (ap, gmsgid);
6137 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6138 va_end (ap);
6139 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6140 if (*ofwhat && warned)
6141 inform (exploc, "(near initialization for %qs)", ofwhat);
6144 /* Issue a warning for a bad initializer component.
6146 OPT is the OPT_W* value corresponding to the warning option that
6147 controls this warning. GMSGID identifies the message. The
6148 component name is taken from the spelling stack. */
6150 static void
6151 warning_init (location_t loc, int opt, const char *gmsgid)
6153 char *ofwhat;
6154 bool warned;
6156 /* Use the location where a macro was expanded rather than where
6157 it was defined to make sure macros defined in system headers
6158 but used incorrectly elsewhere are diagnosed. */
6159 source_location exploc = expansion_point_location_if_in_system_header (loc);
6161 /* The gmsgid may be a format string with %< and %>. */
6162 warned = warning_at (exploc, opt, gmsgid);
6163 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6164 if (*ofwhat && warned)
6165 inform (exploc, "(near initialization for %qs)", ofwhat);
6168 /* If TYPE is an array type and EXPR is a parenthesized string
6169 constant, warn if pedantic that EXPR is being used to initialize an
6170 object of type TYPE. */
6172 void
6173 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6175 if (pedantic
6176 && TREE_CODE (type) == ARRAY_TYPE
6177 && TREE_CODE (expr.value) == STRING_CST
6178 && expr.original_code != STRING_CST)
6179 pedwarn_init (loc, OPT_Wpedantic,
6180 "array initialized from parenthesized string constant");
6183 /* Convert value RHS to type TYPE as preparation for an assignment to
6184 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6185 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6186 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6187 constant before any folding.
6188 The real work of conversion is done by `convert'.
6189 The purpose of this function is to generate error messages
6190 for assignments that are not allowed in C.
6191 ERRTYPE says whether it is argument passing, assignment,
6192 initialization or return.
6194 In the following example, '~' denotes where EXPR_LOC and '^' where
6195 LOCATION point to:
6197 f (var); [ic_argpass]
6198 ^ ~~~
6199 x = var; [ic_assign]
6200 ^ ~~~;
6201 int x = var; [ic_init]
6203 return x; [ic_return]
6206 FUNCTION is a tree for the function being called.
6207 PARMNUM is the number of the argument, for printing in error messages. */
6209 static tree
6210 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6211 tree rhs, tree origtype, enum impl_conv errtype,
6212 bool null_pointer_constant, tree fundecl,
6213 tree function, int parmnum)
6215 enum tree_code codel = TREE_CODE (type);
6216 tree orig_rhs = rhs;
6217 tree rhstype;
6218 enum tree_code coder;
6219 tree rname = NULL_TREE;
6220 bool objc_ok = false;
6222 /* Use the expansion point location to handle cases such as user's
6223 function returning a wrong-type macro defined in a system header. */
6224 location = expansion_point_location_if_in_system_header (location);
6226 if (errtype == ic_argpass)
6228 tree selector;
6229 /* Change pointer to function to the function itself for
6230 diagnostics. */
6231 if (TREE_CODE (function) == ADDR_EXPR
6232 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6233 function = TREE_OPERAND (function, 0);
6235 /* Handle an ObjC selector specially for diagnostics. */
6236 selector = objc_message_selector ();
6237 rname = function;
6238 if (selector && parmnum > 2)
6240 rname = selector;
6241 parmnum -= 2;
6245 /* This macro is used to emit diagnostics to ensure that all format
6246 strings are complete sentences, visible to gettext and checked at
6247 compile time. */
6248 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6249 do { \
6250 switch (errtype) \
6252 case ic_argpass: \
6253 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6254 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6255 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6256 "expected %qT but argument is of type %qT", \
6257 type, rhstype); \
6258 break; \
6259 case ic_assign: \
6260 pedwarn (LOCATION, OPT, AS); \
6261 break; \
6262 case ic_init: \
6263 pedwarn_init (LOCATION, OPT, IN); \
6264 break; \
6265 case ic_return: \
6266 pedwarn (LOCATION, OPT, RE); \
6267 break; \
6268 default: \
6269 gcc_unreachable (); \
6271 } while (0)
6273 /* This macro is used to emit diagnostics to ensure that all format
6274 strings are complete sentences, visible to gettext and checked at
6275 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6276 extra parameter to enumerate qualifiers. */
6277 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6278 do { \
6279 switch (errtype) \
6281 case ic_argpass: \
6282 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6283 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6284 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6285 "expected %qT but argument is of type %qT", \
6286 type, rhstype); \
6287 break; \
6288 case ic_assign: \
6289 pedwarn (LOCATION, OPT, AS, QUALS); \
6290 break; \
6291 case ic_init: \
6292 pedwarn (LOCATION, OPT, IN, QUALS); \
6293 break; \
6294 case ic_return: \
6295 pedwarn (LOCATION, OPT, RE, QUALS); \
6296 break; \
6297 default: \
6298 gcc_unreachable (); \
6300 } while (0)
6302 /* This macro is used to emit diagnostics to ensure that all format
6303 strings are complete sentences, visible to gettext and checked at
6304 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6305 warning_at instead of pedwarn. */
6306 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6307 do { \
6308 switch (errtype) \
6310 case ic_argpass: \
6311 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6312 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6313 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6314 "expected %qT but argument is of type %qT", \
6315 type, rhstype); \
6316 break; \
6317 case ic_assign: \
6318 warning_at (LOCATION, OPT, AS, QUALS); \
6319 break; \
6320 case ic_init: \
6321 warning_at (LOCATION, OPT, IN, QUALS); \
6322 break; \
6323 case ic_return: \
6324 warning_at (LOCATION, OPT, RE, QUALS); \
6325 break; \
6326 default: \
6327 gcc_unreachable (); \
6329 } while (0)
6331 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6332 rhs = TREE_OPERAND (rhs, 0);
6334 rhstype = TREE_TYPE (rhs);
6335 coder = TREE_CODE (rhstype);
6337 if (coder == ERROR_MARK)
6338 return error_mark_node;
6340 if (c_dialect_objc ())
6342 int parmno;
6344 switch (errtype)
6346 case ic_return:
6347 parmno = 0;
6348 break;
6350 case ic_assign:
6351 parmno = -1;
6352 break;
6354 case ic_init:
6355 parmno = -2;
6356 break;
6358 default:
6359 parmno = parmnum;
6360 break;
6363 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6366 if (warn_cxx_compat)
6368 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6369 if (checktype != error_mark_node
6370 && TREE_CODE (type) == ENUMERAL_TYPE
6371 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6372 switch (errtype)
6374 case ic_argpass:
6375 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6376 "passing argument %d of %qE is invalid in C++",
6377 parmnum, rname))
6378 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6379 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6380 "expected %qT but argument is of type %qT",
6381 type, rhstype);
6382 break;
6383 case ic_assign:
6384 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6385 "%qT in assignment is invalid in C++", rhstype, type);
6386 break;
6387 case ic_init:
6388 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6389 "%qT to %qT in initialization is invalid in C++",
6390 rhstype, type);
6391 break;
6392 case ic_return:
6393 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6394 "%qT in return is invalid in C++", rhstype, type);
6395 break;
6396 default:
6397 gcc_unreachable ();
6401 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6402 return rhs;
6404 if (coder == VOID_TYPE)
6406 /* Except for passing an argument to an unprototyped function,
6407 this is a constraint violation. When passing an argument to
6408 an unprototyped function, it is compile-time undefined;
6409 making it a constraint in that case was rejected in
6410 DR#252. */
6411 error_at (location, "void value not ignored as it ought to be");
6412 return error_mark_node;
6414 rhs = require_complete_type (location, rhs);
6415 if (rhs == error_mark_node)
6416 return error_mark_node;
6418 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6419 return error_mark_node;
6421 /* A non-reference type can convert to a reference. This handles
6422 va_start, va_copy and possibly port built-ins. */
6423 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6425 if (!lvalue_p (rhs))
6427 error_at (location, "cannot pass rvalue to reference parameter");
6428 return error_mark_node;
6430 if (!c_mark_addressable (rhs))
6431 return error_mark_node;
6432 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6433 SET_EXPR_LOCATION (rhs, location);
6435 rhs = convert_for_assignment (location, expr_loc,
6436 build_pointer_type (TREE_TYPE (type)),
6437 rhs, origtype, errtype,
6438 null_pointer_constant, fundecl, function,
6439 parmnum);
6440 if (rhs == error_mark_node)
6441 return error_mark_node;
6443 rhs = build1 (NOP_EXPR, type, rhs);
6444 SET_EXPR_LOCATION (rhs, location);
6445 return rhs;
6447 /* Some types can interconvert without explicit casts. */
6448 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6449 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6450 return convert (type, rhs);
6451 /* Arithmetic types all interconvert, and enum is treated like int. */
6452 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6453 || codel == FIXED_POINT_TYPE
6454 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6455 || codel == BOOLEAN_TYPE)
6456 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6457 || coder == FIXED_POINT_TYPE
6458 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6459 || coder == BOOLEAN_TYPE))
6461 tree ret;
6462 bool save = in_late_binary_op;
6463 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6464 || (coder == REAL_TYPE
6465 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6466 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6467 in_late_binary_op = true;
6468 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6469 ? expr_loc : location, type, orig_rhs);
6470 in_late_binary_op = save;
6471 return ret;
6474 /* Aggregates in different TUs might need conversion. */
6475 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6476 && codel == coder
6477 && comptypes (type, rhstype))
6478 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6479 ? expr_loc : location, type, rhs);
6481 /* Conversion to a transparent union or record from its member types.
6482 This applies only to function arguments. */
6483 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6484 && TYPE_TRANSPARENT_AGGR (type))
6485 && errtype == ic_argpass)
6487 tree memb, marginal_memb = NULL_TREE;
6489 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6491 tree memb_type = TREE_TYPE (memb);
6493 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6494 TYPE_MAIN_VARIANT (rhstype)))
6495 break;
6497 if (TREE_CODE (memb_type) != POINTER_TYPE)
6498 continue;
6500 if (coder == POINTER_TYPE)
6502 tree ttl = TREE_TYPE (memb_type);
6503 tree ttr = TREE_TYPE (rhstype);
6505 /* Any non-function converts to a [const][volatile] void *
6506 and vice versa; otherwise, targets must be the same.
6507 Meanwhile, the lhs target must have all the qualifiers of
6508 the rhs. */
6509 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6510 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6511 || comp_target_types (location, memb_type, rhstype))
6513 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6514 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6515 /* If this type won't generate any warnings, use it. */
6516 if (lquals == rquals
6517 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6518 && TREE_CODE (ttl) == FUNCTION_TYPE)
6519 ? ((lquals | rquals) == rquals)
6520 : ((lquals | rquals) == lquals)))
6521 break;
6523 /* Keep looking for a better type, but remember this one. */
6524 if (!marginal_memb)
6525 marginal_memb = memb;
6529 /* Can convert integer zero to any pointer type. */
6530 if (null_pointer_constant)
6532 rhs = null_pointer_node;
6533 break;
6537 if (memb || marginal_memb)
6539 if (!memb)
6541 /* We have only a marginally acceptable member type;
6542 it needs a warning. */
6543 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6544 tree ttr = TREE_TYPE (rhstype);
6546 /* Const and volatile mean something different for function
6547 types, so the usual warnings are not appropriate. */
6548 if (TREE_CODE (ttr) == FUNCTION_TYPE
6549 && TREE_CODE (ttl) == FUNCTION_TYPE)
6551 /* Because const and volatile on functions are
6552 restrictions that say the function will not do
6553 certain things, it is okay to use a const or volatile
6554 function where an ordinary one is wanted, but not
6555 vice-versa. */
6556 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6557 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6558 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6559 OPT_Wdiscarded_qualifiers,
6560 G_("passing argument %d of %qE "
6561 "makes %q#v qualified function "
6562 "pointer from unqualified"),
6563 G_("assignment makes %q#v qualified "
6564 "function pointer from "
6565 "unqualified"),
6566 G_("initialization makes %q#v qualified "
6567 "function pointer from "
6568 "unqualified"),
6569 G_("return makes %q#v qualified function "
6570 "pointer from unqualified"),
6571 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6573 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6574 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6575 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6576 OPT_Wdiscarded_qualifiers,
6577 G_("passing argument %d of %qE discards "
6578 "%qv qualifier from pointer target type"),
6579 G_("assignment discards %qv qualifier "
6580 "from pointer target type"),
6581 G_("initialization discards %qv qualifier "
6582 "from pointer target type"),
6583 G_("return discards %qv qualifier from "
6584 "pointer target type"),
6585 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6587 memb = marginal_memb;
6590 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6591 pedwarn (location, OPT_Wpedantic,
6592 "ISO C prohibits argument conversion to union type");
6594 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6595 return build_constructor_single (type, memb, rhs);
6599 /* Conversions among pointers */
6600 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6601 && (coder == codel))
6603 tree ttl = TREE_TYPE (type);
6604 tree ttr = TREE_TYPE (rhstype);
6605 tree mvl = ttl;
6606 tree mvr = ttr;
6607 bool is_opaque_pointer;
6608 int target_cmp = 0; /* Cache comp_target_types () result. */
6609 addr_space_t asl;
6610 addr_space_t asr;
6612 if (TREE_CODE (mvl) != ARRAY_TYPE)
6613 mvl = (TYPE_ATOMIC (mvl)
6614 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6615 TYPE_QUAL_ATOMIC)
6616 : TYPE_MAIN_VARIANT (mvl));
6617 if (TREE_CODE (mvr) != ARRAY_TYPE)
6618 mvr = (TYPE_ATOMIC (mvr)
6619 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6620 TYPE_QUAL_ATOMIC)
6621 : TYPE_MAIN_VARIANT (mvr));
6622 /* Opaque pointers are treated like void pointers. */
6623 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6625 /* The Plan 9 compiler permits a pointer to a struct to be
6626 automatically converted into a pointer to an anonymous field
6627 within the struct. */
6628 if (flag_plan9_extensions
6629 && RECORD_OR_UNION_TYPE_P (mvl)
6630 && RECORD_OR_UNION_TYPE_P (mvr)
6631 && mvl != mvr)
6633 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6634 if (new_rhs != NULL_TREE)
6636 rhs = new_rhs;
6637 rhstype = TREE_TYPE (rhs);
6638 coder = TREE_CODE (rhstype);
6639 ttr = TREE_TYPE (rhstype);
6640 mvr = TYPE_MAIN_VARIANT (ttr);
6644 /* C++ does not allow the implicit conversion void* -> T*. However,
6645 for the purpose of reducing the number of false positives, we
6646 tolerate the special case of
6648 int *p = NULL;
6650 where NULL is typically defined in C to be '(void *) 0'. */
6651 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6652 warning_at (errtype == ic_argpass ? expr_loc : location,
6653 OPT_Wc___compat,
6654 "request for implicit conversion "
6655 "from %qT to %qT not permitted in C++", rhstype, type);
6657 /* See if the pointers point to incompatible address spaces. */
6658 asl = TYPE_ADDR_SPACE (ttl);
6659 asr = TYPE_ADDR_SPACE (ttr);
6660 if (!null_pointer_constant_p (rhs)
6661 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6663 switch (errtype)
6665 case ic_argpass:
6666 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6667 "non-enclosed address space", parmnum, rname);
6668 break;
6669 case ic_assign:
6670 error_at (location, "assignment from pointer to "
6671 "non-enclosed address space");
6672 break;
6673 case ic_init:
6674 error_at (location, "initialization from pointer to "
6675 "non-enclosed address space");
6676 break;
6677 case ic_return:
6678 error_at (location, "return from pointer to "
6679 "non-enclosed address space");
6680 break;
6681 default:
6682 gcc_unreachable ();
6684 return error_mark_node;
6687 /* Check if the right-hand side has a format attribute but the
6688 left-hand side doesn't. */
6689 if (warn_suggest_attribute_format
6690 && check_missing_format_attribute (type, rhstype))
6692 switch (errtype)
6694 case ic_argpass:
6695 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6696 "argument %d of %qE might be "
6697 "a candidate for a format attribute",
6698 parmnum, rname);
6699 break;
6700 case ic_assign:
6701 warning_at (location, OPT_Wsuggest_attribute_format,
6702 "assignment left-hand side might be "
6703 "a candidate for a format attribute");
6704 break;
6705 case ic_init:
6706 warning_at (location, OPT_Wsuggest_attribute_format,
6707 "initialization left-hand side might be "
6708 "a candidate for a format attribute");
6709 break;
6710 case ic_return:
6711 warning_at (location, OPT_Wsuggest_attribute_format,
6712 "return type might be "
6713 "a candidate for a format attribute");
6714 break;
6715 default:
6716 gcc_unreachable ();
6720 /* Any non-function converts to a [const][volatile] void *
6721 and vice versa; otherwise, targets must be the same.
6722 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6723 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6724 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6725 || (target_cmp = comp_target_types (location, type, rhstype))
6726 || is_opaque_pointer
6727 || ((c_common_unsigned_type (mvl)
6728 == c_common_unsigned_type (mvr))
6729 && (c_common_signed_type (mvl)
6730 == c_common_signed_type (mvr))
6731 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6733 /* Warn about loss of qualifers from pointers to arrays with
6734 qualifiers on the element type. */
6735 if (TREE_CODE (ttr) == ARRAY_TYPE)
6737 ttr = strip_array_types (ttr);
6738 ttl = strip_array_types (ttl);
6740 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6741 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6742 WARNING_FOR_QUALIFIERS (location, expr_loc,
6743 OPT_Wdiscarded_array_qualifiers,
6744 G_("passing argument %d of %qE discards "
6745 "%qv qualifier from pointer target type"),
6746 G_("assignment discards %qv qualifier "
6747 "from pointer target type"),
6748 G_("initialization discards %qv qualifier "
6749 "from pointer target type"),
6750 G_("return discards %qv qualifier from "
6751 "pointer target type"),
6752 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6754 else if (pedantic
6755 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6757 (VOID_TYPE_P (ttr)
6758 && !null_pointer_constant
6759 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6760 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6761 G_("ISO C forbids passing argument %d of "
6762 "%qE between function pointer "
6763 "and %<void *%>"),
6764 G_("ISO C forbids assignment between "
6765 "function pointer and %<void *%>"),
6766 G_("ISO C forbids initialization between "
6767 "function pointer and %<void *%>"),
6768 G_("ISO C forbids return between function "
6769 "pointer and %<void *%>"));
6770 /* Const and volatile mean something different for function types,
6771 so the usual warnings are not appropriate. */
6772 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6773 && TREE_CODE (ttl) != FUNCTION_TYPE)
6775 /* Don't warn about loss of qualifier for conversions from
6776 qualified void* to pointers to arrays with corresponding
6777 qualifier on the element type. */
6778 if (!pedantic)
6779 ttl = strip_array_types (ttl);
6781 /* Assignments between atomic and non-atomic objects are OK. */
6782 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6783 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6785 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6786 OPT_Wdiscarded_qualifiers,
6787 G_("passing argument %d of %qE discards "
6788 "%qv qualifier from pointer target type"),
6789 G_("assignment discards %qv qualifier "
6790 "from pointer target type"),
6791 G_("initialization discards %qv qualifier "
6792 "from pointer target type"),
6793 G_("return discards %qv qualifier from "
6794 "pointer target type"),
6795 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6797 /* If this is not a case of ignoring a mismatch in signedness,
6798 no warning. */
6799 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6800 || target_cmp)
6802 /* If there is a mismatch, do warn. */
6803 else if (warn_pointer_sign)
6804 switch (errtype)
6806 case ic_argpass:
6807 if (pedwarn (expr_loc, OPT_Wpointer_sign,
6808 "pointer targets in passing argument %d of "
6809 "%qE differ in signedness", parmnum, rname))
6810 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6811 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6812 "expected %qT but argument is of type %qT",
6813 type, rhstype);
6814 break;
6815 case ic_assign:
6816 pedwarn (location, OPT_Wpointer_sign,
6817 "pointer targets in assignment from %qT to %qT "
6818 "differ in signedness", rhstype, type);
6819 break;
6820 case ic_init:
6821 pedwarn_init (location, OPT_Wpointer_sign,
6822 "pointer targets in initialization of %qT "
6823 "from %qT differ in signedness", type,
6824 rhstype);
6825 break;
6826 case ic_return:
6827 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6828 "returning %qT from a function with return type "
6829 "%qT differ in signedness", rhstype, type);
6830 break;
6831 default:
6832 gcc_unreachable ();
6835 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6836 && TREE_CODE (ttr) == FUNCTION_TYPE)
6838 /* Because const and volatile on functions are restrictions
6839 that say the function will not do certain things,
6840 it is okay to use a const or volatile function
6841 where an ordinary one is wanted, but not vice-versa. */
6842 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6843 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6844 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6845 OPT_Wdiscarded_qualifiers,
6846 G_("passing argument %d of %qE makes "
6847 "%q#v qualified function pointer "
6848 "from unqualified"),
6849 G_("assignment makes %q#v qualified function "
6850 "pointer from unqualified"),
6851 G_("initialization makes %q#v qualified "
6852 "function pointer from unqualified"),
6853 G_("return makes %q#v qualified function "
6854 "pointer from unqualified"),
6855 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6858 /* Avoid warning about the volatile ObjC EH puts on decls. */
6859 else if (!objc_ok)
6861 switch (errtype)
6863 case ic_argpass:
6864 if (pedwarn (expr_loc, OPT_Wincompatible_pointer_types,
6865 "passing argument %d of %qE from incompatible "
6866 "pointer type", parmnum, rname))
6867 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6868 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6869 "expected %qT but argument is of type %qT",
6870 type, rhstype);
6871 break;
6872 case ic_assign:
6873 pedwarn (location, OPT_Wincompatible_pointer_types,
6874 "assignment to %qT from incompatible pointer type %qT",
6875 type, rhstype);
6876 break;
6877 case ic_init:
6878 pedwarn_init (location, OPT_Wincompatible_pointer_types,
6879 "initialization of %qT from incompatible pointer "
6880 "type %qT", type, rhstype);
6881 break;
6882 case ic_return:
6883 pedwarn (location, OPT_Wincompatible_pointer_types,
6884 "returning %qT from a function with incompatible "
6885 "return type %qT", rhstype, type);
6886 break;
6887 default:
6888 gcc_unreachable ();
6892 return convert (type, rhs);
6894 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6896 /* ??? This should not be an error when inlining calls to
6897 unprototyped functions. */
6898 error_at (location, "invalid use of non-lvalue array");
6899 return error_mark_node;
6901 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6903 /* An explicit constant 0 can convert to a pointer,
6904 or one that results from arithmetic, even including
6905 a cast to integer type. */
6906 if (!null_pointer_constant)
6907 switch (errtype)
6909 case ic_argpass:
6910 if (pedwarn (expr_loc, OPT_Wint_conversion,
6911 "passing argument %d of %qE makes pointer from "
6912 "integer without a cast", parmnum, rname))
6913 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6914 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6915 "expected %qT but argument is of type %qT",
6916 type, rhstype);
6917 break;
6918 case ic_assign:
6919 pedwarn (location, OPT_Wint_conversion,
6920 "assignment to %qT from %qT makes pointer from integer "
6921 "without a cast", type, rhstype);
6922 break;
6923 case ic_init:
6924 pedwarn_init (location, OPT_Wint_conversion,
6925 "initialization of %qT from %qT makes pointer from "
6926 "integer without a cast", type, rhstype);
6927 break;
6928 case ic_return:
6929 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6930 "function with return type %qT makes pointer from "
6931 "integer without a cast", rhstype, type);
6932 break;
6933 default:
6934 gcc_unreachable ();
6937 return convert (type, rhs);
6939 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6941 switch (errtype)
6943 case ic_argpass:
6944 if (pedwarn (expr_loc, OPT_Wint_conversion,
6945 "passing argument %d of %qE makes integer from "
6946 "pointer without a cast", parmnum, rname))
6947 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6948 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6949 "expected %qT but argument is of type %qT",
6950 type, rhstype);
6951 break;
6952 case ic_assign:
6953 pedwarn (location, OPT_Wint_conversion,
6954 "assignment to %qT from %qT makes integer from pointer "
6955 "without a cast", type, rhstype);
6956 break;
6957 case ic_init:
6958 pedwarn_init (location, OPT_Wint_conversion,
6959 "initialization of %qT from %qT makes integer from "
6960 "pointer without a cast", type, rhstype);
6961 break;
6962 case ic_return:
6963 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6964 "function with return type %qT makes integer from "
6965 "pointer without a cast", rhstype, type);
6966 break;
6967 default:
6968 gcc_unreachable ();
6971 return convert (type, rhs);
6973 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6975 tree ret;
6976 bool save = in_late_binary_op;
6977 in_late_binary_op = true;
6978 ret = convert (type, rhs);
6979 in_late_binary_op = save;
6980 return ret;
6983 switch (errtype)
6985 case ic_argpass:
6986 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6987 rname);
6988 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6989 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6990 "expected %qT but argument is of type %qT", type, rhstype);
6991 break;
6992 case ic_assign:
6993 error_at (location, "incompatible types when assigning to type %qT from "
6994 "type %qT", type, rhstype);
6995 break;
6996 case ic_init:
6997 error_at (location,
6998 "incompatible types when initializing type %qT using type %qT",
6999 type, rhstype);
7000 break;
7001 case ic_return:
7002 error_at (location,
7003 "incompatible types when returning type %qT but %qT was "
7004 "expected", rhstype, type);
7005 break;
7006 default:
7007 gcc_unreachable ();
7010 return error_mark_node;
7013 /* If VALUE is a compound expr all of whose expressions are constant, then
7014 return its value. Otherwise, return error_mark_node.
7016 This is for handling COMPOUND_EXPRs as initializer elements
7017 which is allowed with a warning when -pedantic is specified. */
7019 static tree
7020 valid_compound_expr_initializer (tree value, tree endtype)
7022 if (TREE_CODE (value) == COMPOUND_EXPR)
7024 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7025 == error_mark_node)
7026 return error_mark_node;
7027 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7028 endtype);
7030 else if (!initializer_constant_valid_p (value, endtype))
7031 return error_mark_node;
7032 else
7033 return value;
7036 /* Perform appropriate conversions on the initial value of a variable,
7037 store it in the declaration DECL,
7038 and print any error messages that are appropriate.
7039 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7040 If the init is invalid, store an ERROR_MARK.
7042 INIT_LOC is the location of the initial value. */
7044 void
7045 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7047 tree value, type;
7048 bool npc = false;
7050 /* If variable's type was invalidly declared, just ignore it. */
7052 type = TREE_TYPE (decl);
7053 if (TREE_CODE (type) == ERROR_MARK)
7054 return;
7056 /* Digest the specified initializer into an expression. */
7058 if (init)
7059 npc = null_pointer_constant_p (init);
7060 value = digest_init (init_loc, type, init, origtype, npc,
7061 true, TREE_STATIC (decl));
7063 /* Store the expression if valid; else report error. */
7065 if (!in_system_header_at (input_location)
7066 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7067 warning (OPT_Wtraditional, "traditional C rejects automatic "
7068 "aggregate initialization");
7070 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7071 DECL_INITIAL (decl) = value;
7073 /* ANSI wants warnings about out-of-range constant initializers. */
7074 STRIP_TYPE_NOPS (value);
7075 if (TREE_STATIC (decl))
7076 constant_expression_warning (value);
7078 /* Check if we need to set array size from compound literal size. */
7079 if (TREE_CODE (type) == ARRAY_TYPE
7080 && TYPE_DOMAIN (type) == NULL_TREE
7081 && value != error_mark_node)
7083 tree inside_init = init;
7085 STRIP_TYPE_NOPS (inside_init);
7086 inside_init = fold (inside_init);
7088 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7090 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7092 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7094 /* For int foo[] = (int [3]){1}; we need to set array size
7095 now since later on array initializer will be just the
7096 brace enclosed list of the compound literal. */
7097 tree etype = strip_array_types (TREE_TYPE (decl));
7098 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7099 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7100 layout_type (type);
7101 layout_decl (cldecl, 0);
7102 TREE_TYPE (decl)
7103 = c_build_qualified_type (type, TYPE_QUALS (etype));
7109 /* Methods for storing and printing names for error messages. */
7111 /* Implement a spelling stack that allows components of a name to be pushed
7112 and popped. Each element on the stack is this structure. */
7114 struct spelling
7116 int kind;
7117 union
7119 unsigned HOST_WIDE_INT i;
7120 const char *s;
7121 } u;
7124 #define SPELLING_STRING 1
7125 #define SPELLING_MEMBER 2
7126 #define SPELLING_BOUNDS 3
7128 static struct spelling *spelling; /* Next stack element (unused). */
7129 static struct spelling *spelling_base; /* Spelling stack base. */
7130 static int spelling_size; /* Size of the spelling stack. */
7132 /* Macros to save and restore the spelling stack around push_... functions.
7133 Alternative to SAVE_SPELLING_STACK. */
7135 #define SPELLING_DEPTH() (spelling - spelling_base)
7136 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7138 /* Push an element on the spelling stack with type KIND and assign VALUE
7139 to MEMBER. */
7141 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7143 int depth = SPELLING_DEPTH (); \
7145 if (depth >= spelling_size) \
7147 spelling_size += 10; \
7148 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7149 spelling_size); \
7150 RESTORE_SPELLING_DEPTH (depth); \
7153 spelling->kind = (KIND); \
7154 spelling->MEMBER = (VALUE); \
7155 spelling++; \
7158 /* Push STRING on the stack. Printed literally. */
7160 static void
7161 push_string (const char *string)
7163 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7166 /* Push a member name on the stack. Printed as '.' STRING. */
7168 static void
7169 push_member_name (tree decl)
7171 const char *const string
7172 = (DECL_NAME (decl)
7173 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7174 : _("<anonymous>"));
7175 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7178 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7180 static void
7181 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7183 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7186 /* Compute the maximum size in bytes of the printed spelling. */
7188 static int
7189 spelling_length (void)
7191 int size = 0;
7192 struct spelling *p;
7194 for (p = spelling_base; p < spelling; p++)
7196 if (p->kind == SPELLING_BOUNDS)
7197 size += 25;
7198 else
7199 size += strlen (p->u.s) + 1;
7202 return size;
7205 /* Print the spelling to BUFFER and return it. */
7207 static char *
7208 print_spelling (char *buffer)
7210 char *d = buffer;
7211 struct spelling *p;
7213 for (p = spelling_base; p < spelling; p++)
7214 if (p->kind == SPELLING_BOUNDS)
7216 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7217 d += strlen (d);
7219 else
7221 const char *s;
7222 if (p->kind == SPELLING_MEMBER)
7223 *d++ = '.';
7224 for (s = p->u.s; (*d = *s++); d++)
7227 *d++ = '\0';
7228 return buffer;
7231 /* Digest the parser output INIT as an initializer for type TYPE.
7232 Return a C expression of type TYPE to represent the initial value.
7234 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7236 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7238 If INIT is a string constant, STRICT_STRING is true if it is
7239 unparenthesized or we should not warn here for it being parenthesized.
7240 For other types of INIT, STRICT_STRING is not used.
7242 INIT_LOC is the location of the INIT.
7244 REQUIRE_CONSTANT requests an error if non-constant initializers or
7245 elements are seen. */
7247 static tree
7248 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7249 bool null_pointer_constant, bool strict_string,
7250 int require_constant)
7252 enum tree_code code = TREE_CODE (type);
7253 tree inside_init = init;
7254 tree semantic_type = NULL_TREE;
7255 bool maybe_const = true;
7257 if (type == error_mark_node
7258 || !init
7259 || error_operand_p (init))
7260 return error_mark_node;
7262 STRIP_TYPE_NOPS (inside_init);
7264 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7266 semantic_type = TREE_TYPE (inside_init);
7267 inside_init = TREE_OPERAND (inside_init, 0);
7269 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7270 inside_init = decl_constant_value_for_optimization (inside_init);
7272 /* Initialization of an array of chars from a string constant
7273 optionally enclosed in braces. */
7275 if (code == ARRAY_TYPE && inside_init
7276 && TREE_CODE (inside_init) == STRING_CST)
7278 tree typ1
7279 = (TYPE_ATOMIC (TREE_TYPE (type))
7280 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7281 TYPE_QUAL_ATOMIC)
7282 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7283 /* Note that an array could be both an array of character type
7284 and an array of wchar_t if wchar_t is signed char or unsigned
7285 char. */
7286 bool char_array = (typ1 == char_type_node
7287 || typ1 == signed_char_type_node
7288 || typ1 == unsigned_char_type_node);
7289 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7290 bool char16_array = !!comptypes (typ1, char16_type_node);
7291 bool char32_array = !!comptypes (typ1, char32_type_node);
7293 if (char_array || wchar_array || char16_array || char32_array)
7295 struct c_expr expr;
7296 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7297 expr.value = inside_init;
7298 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7299 expr.original_type = NULL;
7300 maybe_warn_string_init (init_loc, type, expr);
7302 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7303 pedwarn_init (init_loc, OPT_Wpedantic,
7304 "initialization of a flexible array member");
7306 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7307 TYPE_MAIN_VARIANT (type)))
7308 return inside_init;
7310 if (char_array)
7312 if (typ2 != char_type_node)
7314 error_init (init_loc, "char-array initialized from wide "
7315 "string");
7316 return error_mark_node;
7319 else
7321 if (typ2 == char_type_node)
7323 error_init (init_loc, "wide character array initialized "
7324 "from non-wide string");
7325 return error_mark_node;
7327 else if (!comptypes(typ1, typ2))
7329 error_init (init_loc, "wide character array initialized "
7330 "from incompatible wide string");
7331 return error_mark_node;
7335 TREE_TYPE (inside_init) = type;
7336 if (TYPE_DOMAIN (type) != NULL_TREE
7337 && TYPE_SIZE (type) != NULL_TREE
7338 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7340 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7342 /* Subtract the size of a single (possibly wide) character
7343 because it's ok to ignore the terminating null char
7344 that is counted in the length of the constant. */
7345 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7346 (len
7347 - (TYPE_PRECISION (typ1)
7348 / BITS_PER_UNIT))))
7349 pedwarn_init (init_loc, 0,
7350 ("initializer-string for array of chars "
7351 "is too long"));
7352 else if (warn_cxx_compat
7353 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7354 warning_at (init_loc, OPT_Wc___compat,
7355 ("initializer-string for array chars "
7356 "is too long for C++"));
7359 return inside_init;
7361 else if (INTEGRAL_TYPE_P (typ1))
7363 error_init (init_loc, "array of inappropriate type initialized "
7364 "from string constant");
7365 return error_mark_node;
7369 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7370 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7371 below and handle as a constructor. */
7372 if (code == VECTOR_TYPE
7373 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7374 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7375 && TREE_CONSTANT (inside_init))
7377 if (TREE_CODE (inside_init) == VECTOR_CST
7378 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7379 TYPE_MAIN_VARIANT (type)))
7380 return inside_init;
7382 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7384 unsigned HOST_WIDE_INT ix;
7385 tree value;
7386 bool constant_p = true;
7388 /* Iterate through elements and check if all constructor
7389 elements are *_CSTs. */
7390 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7391 if (!CONSTANT_CLASS_P (value))
7393 constant_p = false;
7394 break;
7397 if (constant_p)
7398 return build_vector_from_ctor (type,
7399 CONSTRUCTOR_ELTS (inside_init));
7403 if (warn_sequence_point)
7404 verify_sequence_points (inside_init);
7406 /* Any type can be initialized
7407 from an expression of the same type, optionally with braces. */
7409 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7410 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7411 TYPE_MAIN_VARIANT (type))
7412 || (code == ARRAY_TYPE
7413 && comptypes (TREE_TYPE (inside_init), type))
7414 || (code == VECTOR_TYPE
7415 && comptypes (TREE_TYPE (inside_init), type))
7416 || (code == POINTER_TYPE
7417 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7418 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7419 TREE_TYPE (type)))))
7421 if (code == POINTER_TYPE)
7423 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7425 if (TREE_CODE (inside_init) == STRING_CST
7426 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7427 inside_init = array_to_pointer_conversion
7428 (init_loc, inside_init);
7429 else
7431 error_init (init_loc, "invalid use of non-lvalue array");
7432 return error_mark_node;
7437 if (code == VECTOR_TYPE)
7438 /* Although the types are compatible, we may require a
7439 conversion. */
7440 inside_init = convert (type, inside_init);
7442 if (require_constant
7443 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7445 /* As an extension, allow initializing objects with static storage
7446 duration with compound literals (which are then treated just as
7447 the brace enclosed list they contain). Also allow this for
7448 vectors, as we can only assign them with compound literals. */
7449 if (flag_isoc99 && code != VECTOR_TYPE)
7450 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7451 "is not constant");
7452 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7453 inside_init = DECL_INITIAL (decl);
7456 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7457 && TREE_CODE (inside_init) != CONSTRUCTOR)
7459 error_init (init_loc, "array initialized from non-constant array "
7460 "expression");
7461 return error_mark_node;
7464 /* Compound expressions can only occur here if -Wpedantic or
7465 -pedantic-errors is specified. In the later case, we always want
7466 an error. In the former case, we simply want a warning. */
7467 if (require_constant && pedantic
7468 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7470 inside_init
7471 = valid_compound_expr_initializer (inside_init,
7472 TREE_TYPE (inside_init));
7473 if (inside_init == error_mark_node)
7474 error_init (init_loc, "initializer element is not constant");
7475 else
7476 pedwarn_init (init_loc, OPT_Wpedantic,
7477 "initializer element is not constant");
7478 if (flag_pedantic_errors)
7479 inside_init = error_mark_node;
7481 else if (require_constant
7482 && !initializer_constant_valid_p (inside_init,
7483 TREE_TYPE (inside_init)))
7485 error_init (init_loc, "initializer element is not constant");
7486 inside_init = error_mark_node;
7488 else if (require_constant && !maybe_const)
7489 pedwarn_init (init_loc, OPT_Wpedantic,
7490 "initializer element is not a constant expression");
7492 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7493 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7494 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7495 type, inside_init, origtype,
7496 ic_init, null_pointer_constant,
7497 NULL_TREE, NULL_TREE, 0);
7498 return inside_init;
7501 /* Handle scalar types, including conversions. */
7503 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7504 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7505 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7507 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7508 && (TREE_CODE (init) == STRING_CST
7509 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7510 inside_init = init = array_to_pointer_conversion (init_loc, init);
7511 if (semantic_type)
7512 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7513 inside_init);
7514 inside_init
7515 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7516 inside_init, origtype, ic_init,
7517 null_pointer_constant, NULL_TREE, NULL_TREE,
7520 /* Check to see if we have already given an error message. */
7521 if (inside_init == error_mark_node)
7523 else if (require_constant && !TREE_CONSTANT (inside_init))
7525 error_init (init_loc, "initializer element is not constant");
7526 inside_init = error_mark_node;
7528 else if (require_constant
7529 && !initializer_constant_valid_p (inside_init,
7530 TREE_TYPE (inside_init)))
7532 error_init (init_loc, "initializer element is not computable at "
7533 "load time");
7534 inside_init = error_mark_node;
7536 else if (require_constant && !maybe_const)
7537 pedwarn_init (init_loc, OPT_Wpedantic,
7538 "initializer element is not a constant expression");
7540 return inside_init;
7543 /* Come here only for records and arrays. */
7545 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7547 error_init (init_loc, "variable-sized object may not be initialized");
7548 return error_mark_node;
7551 error_init (init_loc, "invalid initializer");
7552 return error_mark_node;
7555 /* Handle initializers that use braces. */
7557 /* Type of object we are accumulating a constructor for.
7558 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7559 static tree constructor_type;
7561 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7562 left to fill. */
7563 static tree constructor_fields;
7565 /* For an ARRAY_TYPE, this is the specified index
7566 at which to store the next element we get. */
7567 static tree constructor_index;
7569 /* For an ARRAY_TYPE, this is the maximum index. */
7570 static tree constructor_max_index;
7572 /* For a RECORD_TYPE, this is the first field not yet written out. */
7573 static tree constructor_unfilled_fields;
7575 /* For an ARRAY_TYPE, this is the index of the first element
7576 not yet written out. */
7577 static tree constructor_unfilled_index;
7579 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7580 This is so we can generate gaps between fields, when appropriate. */
7581 static tree constructor_bit_index;
7583 /* If we are saving up the elements rather than allocating them,
7584 this is the list of elements so far (in reverse order,
7585 most recent first). */
7586 static vec<constructor_elt, va_gc> *constructor_elements;
7588 /* 1 if constructor should be incrementally stored into a constructor chain,
7589 0 if all the elements should be kept in AVL tree. */
7590 static int constructor_incremental;
7592 /* 1 if so far this constructor's elements are all compile-time constants. */
7593 static int constructor_constant;
7595 /* 1 if so far this constructor's elements are all valid address constants. */
7596 static int constructor_simple;
7598 /* 1 if this constructor has an element that cannot be part of a
7599 constant expression. */
7600 static int constructor_nonconst;
7602 /* 1 if this constructor is erroneous so far. */
7603 static int constructor_erroneous;
7605 /* 1 if this constructor is the universal zero initializer { 0 }. */
7606 static int constructor_zeroinit;
7608 /* Structure for managing pending initializer elements, organized as an
7609 AVL tree. */
7611 struct init_node
7613 struct init_node *left, *right;
7614 struct init_node *parent;
7615 int balance;
7616 tree purpose;
7617 tree value;
7618 tree origtype;
7621 /* Tree of pending elements at this constructor level.
7622 These are elements encountered out of order
7623 which belong at places we haven't reached yet in actually
7624 writing the output.
7625 Will never hold tree nodes across GC runs. */
7626 static struct init_node *constructor_pending_elts;
7628 /* The SPELLING_DEPTH of this constructor. */
7629 static int constructor_depth;
7631 /* DECL node for which an initializer is being read.
7632 0 means we are reading a constructor expression
7633 such as (struct foo) {...}. */
7634 static tree constructor_decl;
7636 /* Nonzero if this is an initializer for a top-level decl. */
7637 static int constructor_top_level;
7639 /* Nonzero if there were any member designators in this initializer. */
7640 static int constructor_designated;
7642 /* Nesting depth of designator list. */
7643 static int designator_depth;
7645 /* Nonzero if there were diagnosed errors in this designator list. */
7646 static int designator_erroneous;
7649 /* This stack has a level for each implicit or explicit level of
7650 structuring in the initializer, including the outermost one. It
7651 saves the values of most of the variables above. */
7653 struct constructor_range_stack;
7655 struct constructor_stack
7657 struct constructor_stack *next;
7658 tree type;
7659 tree fields;
7660 tree index;
7661 tree max_index;
7662 tree unfilled_index;
7663 tree unfilled_fields;
7664 tree bit_index;
7665 vec<constructor_elt, va_gc> *elements;
7666 struct init_node *pending_elts;
7667 int offset;
7668 int depth;
7669 /* If value nonzero, this value should replace the entire
7670 constructor at this level. */
7671 struct c_expr replacement_value;
7672 struct constructor_range_stack *range_stack;
7673 char constant;
7674 char simple;
7675 char nonconst;
7676 char implicit;
7677 char erroneous;
7678 char outer;
7679 char incremental;
7680 char designated;
7681 int designator_depth;
7684 static struct constructor_stack *constructor_stack;
7686 /* This stack represents designators from some range designator up to
7687 the last designator in the list. */
7689 struct constructor_range_stack
7691 struct constructor_range_stack *next, *prev;
7692 struct constructor_stack *stack;
7693 tree range_start;
7694 tree index;
7695 tree range_end;
7696 tree fields;
7699 static struct constructor_range_stack *constructor_range_stack;
7701 /* This stack records separate initializers that are nested.
7702 Nested initializers can't happen in ANSI C, but GNU C allows them
7703 in cases like { ... (struct foo) { ... } ... }. */
7705 struct initializer_stack
7707 struct initializer_stack *next;
7708 tree decl;
7709 struct constructor_stack *constructor_stack;
7710 struct constructor_range_stack *constructor_range_stack;
7711 vec<constructor_elt, va_gc> *elements;
7712 struct spelling *spelling;
7713 struct spelling *spelling_base;
7714 int spelling_size;
7715 char top_level;
7716 char require_constant_value;
7717 char require_constant_elements;
7718 rich_location *missing_brace_richloc;
7721 static struct initializer_stack *initializer_stack;
7723 /* Prepare to parse and output the initializer for variable DECL. */
7725 void
7726 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7727 rich_location *richloc)
7729 const char *locus;
7730 struct initializer_stack *p = XNEW (struct initializer_stack);
7732 p->decl = constructor_decl;
7733 p->require_constant_value = require_constant_value;
7734 p->require_constant_elements = require_constant_elements;
7735 p->constructor_stack = constructor_stack;
7736 p->constructor_range_stack = constructor_range_stack;
7737 p->elements = constructor_elements;
7738 p->spelling = spelling;
7739 p->spelling_base = spelling_base;
7740 p->spelling_size = spelling_size;
7741 p->top_level = constructor_top_level;
7742 p->next = initializer_stack;
7743 p->missing_brace_richloc = richloc;
7744 initializer_stack = p;
7746 constructor_decl = decl;
7747 constructor_designated = 0;
7748 constructor_top_level = top_level;
7750 if (decl != NULL_TREE && decl != error_mark_node)
7752 require_constant_value = TREE_STATIC (decl);
7753 require_constant_elements
7754 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7755 /* For a scalar, you can always use any value to initialize,
7756 even within braces. */
7757 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7758 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7760 else
7762 require_constant_value = 0;
7763 require_constant_elements = 0;
7764 locus = _("(anonymous)");
7767 constructor_stack = 0;
7768 constructor_range_stack = 0;
7770 found_missing_braces = 0;
7772 spelling_base = 0;
7773 spelling_size = 0;
7774 RESTORE_SPELLING_DEPTH (0);
7776 if (locus)
7777 push_string (locus);
7780 void
7781 finish_init (void)
7783 struct initializer_stack *p = initializer_stack;
7785 /* Free the whole constructor stack of this initializer. */
7786 while (constructor_stack)
7788 struct constructor_stack *q = constructor_stack;
7789 constructor_stack = q->next;
7790 free (q);
7793 gcc_assert (!constructor_range_stack);
7795 /* Pop back to the data of the outer initializer (if any). */
7796 free (spelling_base);
7798 constructor_decl = p->decl;
7799 require_constant_value = p->require_constant_value;
7800 require_constant_elements = p->require_constant_elements;
7801 constructor_stack = p->constructor_stack;
7802 constructor_range_stack = p->constructor_range_stack;
7803 constructor_elements = p->elements;
7804 spelling = p->spelling;
7805 spelling_base = p->spelling_base;
7806 spelling_size = p->spelling_size;
7807 constructor_top_level = p->top_level;
7808 initializer_stack = p->next;
7809 free (p);
7812 /* Call here when we see the initializer is surrounded by braces.
7813 This is instead of a call to push_init_level;
7814 it is matched by a call to pop_init_level.
7816 TYPE is the type to initialize, for a constructor expression.
7817 For an initializer for a decl, TYPE is zero. */
7819 void
7820 really_start_incremental_init (tree type)
7822 struct constructor_stack *p = XNEW (struct constructor_stack);
7824 if (type == NULL_TREE)
7825 type = TREE_TYPE (constructor_decl);
7827 if (VECTOR_TYPE_P (type)
7828 && TYPE_VECTOR_OPAQUE (type))
7829 error ("opaque vector types cannot be initialized");
7831 p->type = constructor_type;
7832 p->fields = constructor_fields;
7833 p->index = constructor_index;
7834 p->max_index = constructor_max_index;
7835 p->unfilled_index = constructor_unfilled_index;
7836 p->unfilled_fields = constructor_unfilled_fields;
7837 p->bit_index = constructor_bit_index;
7838 p->elements = constructor_elements;
7839 p->constant = constructor_constant;
7840 p->simple = constructor_simple;
7841 p->nonconst = constructor_nonconst;
7842 p->erroneous = constructor_erroneous;
7843 p->pending_elts = constructor_pending_elts;
7844 p->depth = constructor_depth;
7845 p->replacement_value.value = 0;
7846 p->replacement_value.original_code = ERROR_MARK;
7847 p->replacement_value.original_type = NULL;
7848 p->implicit = 0;
7849 p->range_stack = 0;
7850 p->outer = 0;
7851 p->incremental = constructor_incremental;
7852 p->designated = constructor_designated;
7853 p->designator_depth = designator_depth;
7854 p->next = 0;
7855 constructor_stack = p;
7857 constructor_constant = 1;
7858 constructor_simple = 1;
7859 constructor_nonconst = 0;
7860 constructor_depth = SPELLING_DEPTH ();
7861 constructor_elements = NULL;
7862 constructor_pending_elts = 0;
7863 constructor_type = type;
7864 constructor_incremental = 1;
7865 constructor_designated = 0;
7866 constructor_zeroinit = 1;
7867 designator_depth = 0;
7868 designator_erroneous = 0;
7870 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7872 constructor_fields = TYPE_FIELDS (constructor_type);
7873 /* Skip any nameless bit fields at the beginning. */
7874 while (constructor_fields != NULL_TREE
7875 && DECL_C_BIT_FIELD (constructor_fields)
7876 && DECL_NAME (constructor_fields) == NULL_TREE)
7877 constructor_fields = DECL_CHAIN (constructor_fields);
7879 constructor_unfilled_fields = constructor_fields;
7880 constructor_bit_index = bitsize_zero_node;
7882 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7884 if (TYPE_DOMAIN (constructor_type))
7886 constructor_max_index
7887 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7889 /* Detect non-empty initializations of zero-length arrays. */
7890 if (constructor_max_index == NULL_TREE
7891 && TYPE_SIZE (constructor_type))
7892 constructor_max_index = integer_minus_one_node;
7894 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7895 to initialize VLAs will cause a proper error; avoid tree
7896 checking errors as well by setting a safe value. */
7897 if (constructor_max_index
7898 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7899 constructor_max_index = integer_minus_one_node;
7901 constructor_index
7902 = convert (bitsizetype,
7903 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7905 else
7907 constructor_index = bitsize_zero_node;
7908 constructor_max_index = NULL_TREE;
7911 constructor_unfilled_index = constructor_index;
7913 else if (VECTOR_TYPE_P (constructor_type))
7915 /* Vectors are like simple fixed-size arrays. */
7916 constructor_max_index =
7917 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7918 constructor_index = bitsize_zero_node;
7919 constructor_unfilled_index = constructor_index;
7921 else
7923 /* Handle the case of int x = {5}; */
7924 constructor_fields = constructor_type;
7925 constructor_unfilled_fields = constructor_type;
7929 extern location_t last_init_list_comma;
7931 /* Called when we see an open brace for a nested initializer. Finish
7932 off any pending levels with implicit braces. */
7933 void
7934 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7936 while (constructor_stack->implicit)
7938 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7939 && constructor_fields == NULL_TREE)
7940 process_init_element (input_location,
7941 pop_init_level (loc, 1, braced_init_obstack,
7942 last_init_list_comma),
7943 true, braced_init_obstack);
7944 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7945 && constructor_max_index
7946 && tree_int_cst_lt (constructor_max_index,
7947 constructor_index))
7948 process_init_element (input_location,
7949 pop_init_level (loc, 1, braced_init_obstack,
7950 last_init_list_comma),
7951 true, braced_init_obstack);
7952 else
7953 break;
7957 /* Push down into a subobject, for initialization.
7958 If this is for an explicit set of braces, IMPLICIT is 0.
7959 If it is because the next element belongs at a lower level,
7960 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7962 void
7963 push_init_level (location_t loc, int implicit,
7964 struct obstack *braced_init_obstack)
7966 struct constructor_stack *p;
7967 tree value = NULL_TREE;
7969 /* Unless this is an explicit brace, we need to preserve previous
7970 content if any. */
7971 if (implicit)
7973 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7974 value = find_init_member (constructor_fields, braced_init_obstack);
7975 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7976 value = find_init_member (constructor_index, braced_init_obstack);
7979 p = XNEW (struct constructor_stack);
7980 p->type = constructor_type;
7981 p->fields = constructor_fields;
7982 p->index = constructor_index;
7983 p->max_index = constructor_max_index;
7984 p->unfilled_index = constructor_unfilled_index;
7985 p->unfilled_fields = constructor_unfilled_fields;
7986 p->bit_index = constructor_bit_index;
7987 p->elements = constructor_elements;
7988 p->constant = constructor_constant;
7989 p->simple = constructor_simple;
7990 p->nonconst = constructor_nonconst;
7991 p->erroneous = constructor_erroneous;
7992 p->pending_elts = constructor_pending_elts;
7993 p->depth = constructor_depth;
7994 p->replacement_value.value = NULL_TREE;
7995 p->replacement_value.original_code = ERROR_MARK;
7996 p->replacement_value.original_type = NULL;
7997 p->implicit = implicit;
7998 p->outer = 0;
7999 p->incremental = constructor_incremental;
8000 p->designated = constructor_designated;
8001 p->designator_depth = designator_depth;
8002 p->next = constructor_stack;
8003 p->range_stack = 0;
8004 constructor_stack = p;
8006 constructor_constant = 1;
8007 constructor_simple = 1;
8008 constructor_nonconst = 0;
8009 constructor_depth = SPELLING_DEPTH ();
8010 constructor_elements = NULL;
8011 constructor_incremental = 1;
8012 constructor_designated = 0;
8013 constructor_pending_elts = 0;
8014 if (!implicit)
8016 p->range_stack = constructor_range_stack;
8017 constructor_range_stack = 0;
8018 designator_depth = 0;
8019 designator_erroneous = 0;
8022 /* Don't die if an entire brace-pair level is superfluous
8023 in the containing level. */
8024 if (constructor_type == NULL_TREE)
8026 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8028 /* Don't die if there are extra init elts at the end. */
8029 if (constructor_fields == NULL_TREE)
8030 constructor_type = NULL_TREE;
8031 else
8033 constructor_type = TREE_TYPE (constructor_fields);
8034 push_member_name (constructor_fields);
8035 constructor_depth++;
8037 /* If upper initializer is designated, then mark this as
8038 designated too to prevent bogus warnings. */
8039 constructor_designated = p->designated;
8041 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8043 constructor_type = TREE_TYPE (constructor_type);
8044 push_array_bounds (tree_to_uhwi (constructor_index));
8045 constructor_depth++;
8048 if (constructor_type == NULL_TREE)
8050 error_init (loc, "extra brace group at end of initializer");
8051 constructor_fields = NULL_TREE;
8052 constructor_unfilled_fields = NULL_TREE;
8053 return;
8056 if (value && TREE_CODE (value) == CONSTRUCTOR)
8058 constructor_constant = TREE_CONSTANT (value);
8059 constructor_simple = TREE_STATIC (value);
8060 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8061 constructor_elements = CONSTRUCTOR_ELTS (value);
8062 if (!vec_safe_is_empty (constructor_elements)
8063 && (TREE_CODE (constructor_type) == RECORD_TYPE
8064 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8065 set_nonincremental_init (braced_init_obstack);
8068 if (implicit == 1)
8070 found_missing_braces = 1;
8071 if (initializer_stack->missing_brace_richloc)
8072 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8073 (loc, "{");
8076 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8078 constructor_fields = TYPE_FIELDS (constructor_type);
8079 /* Skip any nameless bit fields at the beginning. */
8080 while (constructor_fields != NULL_TREE
8081 && DECL_C_BIT_FIELD (constructor_fields)
8082 && DECL_NAME (constructor_fields) == NULL_TREE)
8083 constructor_fields = DECL_CHAIN (constructor_fields);
8085 constructor_unfilled_fields = constructor_fields;
8086 constructor_bit_index = bitsize_zero_node;
8088 else if (VECTOR_TYPE_P (constructor_type))
8090 /* Vectors are like simple fixed-size arrays. */
8091 constructor_max_index =
8092 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8093 constructor_index = bitsize_int (0);
8094 constructor_unfilled_index = constructor_index;
8096 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8098 if (TYPE_DOMAIN (constructor_type))
8100 constructor_max_index
8101 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8103 /* Detect non-empty initializations of zero-length arrays. */
8104 if (constructor_max_index == NULL_TREE
8105 && TYPE_SIZE (constructor_type))
8106 constructor_max_index = integer_minus_one_node;
8108 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8109 to initialize VLAs will cause a proper error; avoid tree
8110 checking errors as well by setting a safe value. */
8111 if (constructor_max_index
8112 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8113 constructor_max_index = integer_minus_one_node;
8115 constructor_index
8116 = convert (bitsizetype,
8117 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8119 else
8120 constructor_index = bitsize_zero_node;
8122 constructor_unfilled_index = constructor_index;
8123 if (value && TREE_CODE (value) == STRING_CST)
8125 /* We need to split the char/wchar array into individual
8126 characters, so that we don't have to special case it
8127 everywhere. */
8128 set_nonincremental_init_from_string (value, braced_init_obstack);
8131 else
8133 if (constructor_type != error_mark_node)
8134 warning_init (input_location, 0, "braces around scalar initializer");
8135 constructor_fields = constructor_type;
8136 constructor_unfilled_fields = constructor_type;
8140 /* At the end of an implicit or explicit brace level,
8141 finish up that level of constructor. If a single expression
8142 with redundant braces initialized that level, return the
8143 c_expr structure for that expression. Otherwise, the original_code
8144 element is set to ERROR_MARK.
8145 If we were outputting the elements as they are read, return 0 as the value
8146 from inner levels (process_init_element ignores that),
8147 but return error_mark_node as the value from the outermost level
8148 (that's what we want to put in DECL_INITIAL).
8149 Otherwise, return a CONSTRUCTOR expression as the value. */
8151 struct c_expr
8152 pop_init_level (location_t loc, int implicit,
8153 struct obstack *braced_init_obstack,
8154 location_t insert_before)
8156 struct constructor_stack *p;
8157 struct c_expr ret;
8158 ret.value = NULL_TREE;
8159 ret.original_code = ERROR_MARK;
8160 ret.original_type = NULL;
8162 if (implicit == 0)
8164 /* When we come to an explicit close brace,
8165 pop any inner levels that didn't have explicit braces. */
8166 while (constructor_stack->implicit)
8167 process_init_element (input_location,
8168 pop_init_level (loc, 1, braced_init_obstack,
8169 insert_before),
8170 true, braced_init_obstack);
8171 gcc_assert (!constructor_range_stack);
8173 else
8174 if (initializer_stack->missing_brace_richloc)
8175 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8176 (insert_before, "}");
8178 /* Now output all pending elements. */
8179 constructor_incremental = 1;
8180 output_pending_init_elements (1, braced_init_obstack);
8182 p = constructor_stack;
8184 /* Error for initializing a flexible array member, or a zero-length
8185 array member in an inappropriate context. */
8186 if (constructor_type && constructor_fields
8187 && TREE_CODE (constructor_type) == ARRAY_TYPE
8188 && TYPE_DOMAIN (constructor_type)
8189 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8191 /* Silently discard empty initializations. The parser will
8192 already have pedwarned for empty brackets. */
8193 if (integer_zerop (constructor_unfilled_index))
8194 constructor_type = NULL_TREE;
8195 else
8197 gcc_assert (!TYPE_SIZE (constructor_type));
8199 if (constructor_depth > 2)
8200 error_init (loc, "initialization of flexible array member in a nested context");
8201 else
8202 pedwarn_init (loc, OPT_Wpedantic,
8203 "initialization of a flexible array member");
8205 /* We have already issued an error message for the existence
8206 of a flexible array member not at the end of the structure.
8207 Discard the initializer so that we do not die later. */
8208 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8209 constructor_type = NULL_TREE;
8213 switch (vec_safe_length (constructor_elements))
8215 case 0:
8216 /* Initialization with { } counts as zeroinit. */
8217 constructor_zeroinit = 1;
8218 break;
8219 case 1:
8220 /* This might be zeroinit as well. */
8221 if (integer_zerop ((*constructor_elements)[0].value))
8222 constructor_zeroinit = 1;
8223 break;
8224 default:
8225 /* If the constructor has more than one element, it can't be { 0 }. */
8226 constructor_zeroinit = 0;
8227 break;
8230 /* Warn when some structs are initialized with direct aggregation. */
8231 if (!implicit && found_missing_braces && warn_missing_braces
8232 && !constructor_zeroinit)
8234 gcc_assert (initializer_stack->missing_brace_richloc);
8235 warning_at_rich_loc (initializer_stack->missing_brace_richloc,
8236 OPT_Wmissing_braces,
8237 "missing braces around initializer");
8240 /* Warn when some struct elements are implicitly initialized to zero. */
8241 if (warn_missing_field_initializers
8242 && constructor_type
8243 && TREE_CODE (constructor_type) == RECORD_TYPE
8244 && constructor_unfilled_fields)
8246 /* Do not warn for flexible array members or zero-length arrays. */
8247 while (constructor_unfilled_fields
8248 && (!DECL_SIZE (constructor_unfilled_fields)
8249 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8250 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8252 if (constructor_unfilled_fields
8253 /* Do not warn if this level of the initializer uses member
8254 designators; it is likely to be deliberate. */
8255 && !constructor_designated
8256 /* Do not warn about initializing with { 0 } or with { }. */
8257 && !constructor_zeroinit)
8259 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8260 "missing initializer for field %qD of %qT",
8261 constructor_unfilled_fields,
8262 constructor_type))
8263 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8264 "%qD declared here", constructor_unfilled_fields);
8268 /* Pad out the end of the structure. */
8269 if (p->replacement_value.value)
8270 /* If this closes a superfluous brace pair,
8271 just pass out the element between them. */
8272 ret = p->replacement_value;
8273 else if (constructor_type == NULL_TREE)
8275 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8276 && TREE_CODE (constructor_type) != ARRAY_TYPE
8277 && !VECTOR_TYPE_P (constructor_type))
8279 /* A nonincremental scalar initializer--just return
8280 the element, after verifying there is just one. */
8281 if (vec_safe_is_empty (constructor_elements))
8283 if (!constructor_erroneous)
8284 error_init (loc, "empty scalar initializer");
8285 ret.value = error_mark_node;
8287 else if (vec_safe_length (constructor_elements) != 1)
8289 error_init (loc, "extra elements in scalar initializer");
8290 ret.value = (*constructor_elements)[0].value;
8292 else
8293 ret.value = (*constructor_elements)[0].value;
8295 else
8297 if (constructor_erroneous)
8298 ret.value = error_mark_node;
8299 else
8301 ret.value = build_constructor (constructor_type,
8302 constructor_elements);
8303 if (constructor_constant)
8304 TREE_CONSTANT (ret.value) = 1;
8305 if (constructor_constant && constructor_simple)
8306 TREE_STATIC (ret.value) = 1;
8307 if (constructor_nonconst)
8308 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8312 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8314 if (constructor_nonconst)
8315 ret.original_code = C_MAYBE_CONST_EXPR;
8316 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8317 ret.original_code = ERROR_MARK;
8320 constructor_type = p->type;
8321 constructor_fields = p->fields;
8322 constructor_index = p->index;
8323 constructor_max_index = p->max_index;
8324 constructor_unfilled_index = p->unfilled_index;
8325 constructor_unfilled_fields = p->unfilled_fields;
8326 constructor_bit_index = p->bit_index;
8327 constructor_elements = p->elements;
8328 constructor_constant = p->constant;
8329 constructor_simple = p->simple;
8330 constructor_nonconst = p->nonconst;
8331 constructor_erroneous = p->erroneous;
8332 constructor_incremental = p->incremental;
8333 constructor_designated = p->designated;
8334 designator_depth = p->designator_depth;
8335 constructor_pending_elts = p->pending_elts;
8336 constructor_depth = p->depth;
8337 if (!p->implicit)
8338 constructor_range_stack = p->range_stack;
8339 RESTORE_SPELLING_DEPTH (constructor_depth);
8341 constructor_stack = p->next;
8342 free (p);
8344 if (ret.value == NULL_TREE && constructor_stack == 0)
8345 ret.value = error_mark_node;
8346 return ret;
8349 /* Common handling for both array range and field name designators.
8350 ARRAY argument is nonzero for array ranges. Returns false for success. */
8352 static bool
8353 set_designator (location_t loc, bool array,
8354 struct obstack *braced_init_obstack)
8356 tree subtype;
8357 enum tree_code subcode;
8359 /* Don't die if an entire brace-pair level is superfluous
8360 in the containing level. */
8361 if (constructor_type == NULL_TREE)
8362 return true;
8364 /* If there were errors in this designator list already, bail out
8365 silently. */
8366 if (designator_erroneous)
8367 return true;
8369 if (!designator_depth)
8371 gcc_assert (!constructor_range_stack);
8373 /* Designator list starts at the level of closest explicit
8374 braces. */
8375 while (constructor_stack->implicit)
8376 process_init_element (input_location,
8377 pop_init_level (loc, 1, braced_init_obstack,
8378 last_init_list_comma),
8379 true, braced_init_obstack);
8380 constructor_designated = 1;
8381 return false;
8384 switch (TREE_CODE (constructor_type))
8386 case RECORD_TYPE:
8387 case UNION_TYPE:
8388 subtype = TREE_TYPE (constructor_fields);
8389 if (subtype != error_mark_node)
8390 subtype = TYPE_MAIN_VARIANT (subtype);
8391 break;
8392 case ARRAY_TYPE:
8393 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8394 break;
8395 default:
8396 gcc_unreachable ();
8399 subcode = TREE_CODE (subtype);
8400 if (array && subcode != ARRAY_TYPE)
8402 error_init (loc, "array index in non-array initializer");
8403 return true;
8405 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8407 error_init (loc, "field name not in record or union initializer");
8408 return true;
8411 constructor_designated = 1;
8412 finish_implicit_inits (loc, braced_init_obstack);
8413 push_init_level (loc, 2, braced_init_obstack);
8414 return false;
8417 /* If there are range designators in designator list, push a new designator
8418 to constructor_range_stack. RANGE_END is end of such stack range or
8419 NULL_TREE if there is no range designator at this level. */
8421 static void
8422 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8424 struct constructor_range_stack *p;
8426 p = (struct constructor_range_stack *)
8427 obstack_alloc (braced_init_obstack,
8428 sizeof (struct constructor_range_stack));
8429 p->prev = constructor_range_stack;
8430 p->next = 0;
8431 p->fields = constructor_fields;
8432 p->range_start = constructor_index;
8433 p->index = constructor_index;
8434 p->stack = constructor_stack;
8435 p->range_end = range_end;
8436 if (constructor_range_stack)
8437 constructor_range_stack->next = p;
8438 constructor_range_stack = p;
8441 /* Within an array initializer, specify the next index to be initialized.
8442 FIRST is that index. If LAST is nonzero, then initialize a range
8443 of indices, running from FIRST through LAST. */
8445 void
8446 set_init_index (location_t loc, tree first, tree last,
8447 struct obstack *braced_init_obstack)
8449 if (set_designator (loc, true, braced_init_obstack))
8450 return;
8452 designator_erroneous = 1;
8454 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8455 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8457 error_init (loc, "array index in initializer not of integer type");
8458 return;
8461 if (TREE_CODE (first) != INTEGER_CST)
8463 first = c_fully_fold (first, false, NULL);
8464 if (TREE_CODE (first) == INTEGER_CST)
8465 pedwarn_init (loc, OPT_Wpedantic,
8466 "array index in initializer is not "
8467 "an integer constant expression");
8470 if (last && TREE_CODE (last) != INTEGER_CST)
8472 last = c_fully_fold (last, false, NULL);
8473 if (TREE_CODE (last) == INTEGER_CST)
8474 pedwarn_init (loc, OPT_Wpedantic,
8475 "array index in initializer is not "
8476 "an integer constant expression");
8479 if (TREE_CODE (first) != INTEGER_CST)
8480 error_init (loc, "nonconstant array index in initializer");
8481 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8482 error_init (loc, "nonconstant array index in initializer");
8483 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8484 error_init (loc, "array index in non-array initializer");
8485 else if (tree_int_cst_sgn (first) == -1)
8486 error_init (loc, "array index in initializer exceeds array bounds");
8487 else if (constructor_max_index
8488 && tree_int_cst_lt (constructor_max_index, first))
8489 error_init (loc, "array index in initializer exceeds array bounds");
8490 else
8492 constant_expression_warning (first);
8493 if (last)
8494 constant_expression_warning (last);
8495 constructor_index = convert (bitsizetype, first);
8496 if (tree_int_cst_lt (constructor_index, first))
8498 constructor_index = copy_node (constructor_index);
8499 TREE_OVERFLOW (constructor_index) = 1;
8502 if (last)
8504 if (tree_int_cst_equal (first, last))
8505 last = NULL_TREE;
8506 else if (tree_int_cst_lt (last, first))
8508 error_init (loc, "empty index range in initializer");
8509 last = NULL_TREE;
8511 else
8513 last = convert (bitsizetype, last);
8514 if (constructor_max_index != NULL_TREE
8515 && tree_int_cst_lt (constructor_max_index, last))
8517 error_init (loc, "array index range in initializer exceeds "
8518 "array bounds");
8519 last = NULL_TREE;
8524 designator_depth++;
8525 designator_erroneous = 0;
8526 if (constructor_range_stack || last)
8527 push_range_stack (last, braced_init_obstack);
8531 /* Within a struct initializer, specify the next field to be initialized. */
8533 void
8534 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8535 struct obstack *braced_init_obstack)
8537 tree field;
8539 if (set_designator (loc, false, braced_init_obstack))
8540 return;
8542 designator_erroneous = 1;
8544 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8546 error_init (loc, "field name not in record or union initializer");
8547 return;
8550 field = lookup_field (constructor_type, fieldname);
8552 if (field == NULL_TREE)
8554 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8555 if (guessed_id)
8557 gcc_rich_location rich_loc (fieldname_loc);
8558 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8559 error_at_rich_loc
8560 (&rich_loc,
8561 "%qT has no member named %qE; did you mean %qE?",
8562 constructor_type, fieldname, guessed_id);
8564 else
8565 error_at (fieldname_loc, "%qT has no member named %qE",
8566 constructor_type, fieldname);
8568 else
8571 constructor_fields = TREE_VALUE (field);
8572 designator_depth++;
8573 designator_erroneous = 0;
8574 if (constructor_range_stack)
8575 push_range_stack (NULL_TREE, braced_init_obstack);
8576 field = TREE_CHAIN (field);
8577 if (field)
8579 if (set_designator (loc, false, braced_init_obstack))
8580 return;
8583 while (field != NULL_TREE);
8586 /* Add a new initializer to the tree of pending initializers. PURPOSE
8587 identifies the initializer, either array index or field in a structure.
8588 VALUE is the value of that index or field. If ORIGTYPE is not
8589 NULL_TREE, it is the original type of VALUE.
8591 IMPLICIT is true if value comes from pop_init_level (1),
8592 the new initializer has been merged with the existing one
8593 and thus no warnings should be emitted about overriding an
8594 existing initializer. */
8596 static void
8597 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8598 bool implicit, struct obstack *braced_init_obstack)
8600 struct init_node *p, **q, *r;
8602 q = &constructor_pending_elts;
8603 p = 0;
8605 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8607 while (*q != 0)
8609 p = *q;
8610 if (tree_int_cst_lt (purpose, p->purpose))
8611 q = &p->left;
8612 else if (tree_int_cst_lt (p->purpose, purpose))
8613 q = &p->right;
8614 else
8616 if (!implicit)
8618 if (TREE_SIDE_EFFECTS (p->value))
8619 warning_init (loc, OPT_Woverride_init_side_effects,
8620 "initialized field with side-effects "
8621 "overwritten");
8622 else if (warn_override_init)
8623 warning_init (loc, OPT_Woverride_init,
8624 "initialized field overwritten");
8626 p->value = value;
8627 p->origtype = origtype;
8628 return;
8632 else
8634 tree bitpos;
8636 bitpos = bit_position (purpose);
8637 while (*q != NULL)
8639 p = *q;
8640 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8641 q = &p->left;
8642 else if (p->purpose != purpose)
8643 q = &p->right;
8644 else
8646 if (!implicit)
8648 if (TREE_SIDE_EFFECTS (p->value))
8649 warning_init (loc, OPT_Woverride_init_side_effects,
8650 "initialized field with side-effects "
8651 "overwritten");
8652 else if (warn_override_init)
8653 warning_init (loc, OPT_Woverride_init,
8654 "initialized field overwritten");
8656 p->value = value;
8657 p->origtype = origtype;
8658 return;
8663 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8664 sizeof (struct init_node));
8665 r->purpose = purpose;
8666 r->value = value;
8667 r->origtype = origtype;
8669 *q = r;
8670 r->parent = p;
8671 r->left = 0;
8672 r->right = 0;
8673 r->balance = 0;
8675 while (p)
8677 struct init_node *s;
8679 if (r == p->left)
8681 if (p->balance == 0)
8682 p->balance = -1;
8683 else if (p->balance < 0)
8685 if (r->balance < 0)
8687 /* L rotation. */
8688 p->left = r->right;
8689 if (p->left)
8690 p->left->parent = p;
8691 r->right = p;
8693 p->balance = 0;
8694 r->balance = 0;
8696 s = p->parent;
8697 p->parent = r;
8698 r->parent = s;
8699 if (s)
8701 if (s->left == p)
8702 s->left = r;
8703 else
8704 s->right = r;
8706 else
8707 constructor_pending_elts = r;
8709 else
8711 /* LR rotation. */
8712 struct init_node *t = r->right;
8714 r->right = t->left;
8715 if (r->right)
8716 r->right->parent = r;
8717 t->left = r;
8719 p->left = t->right;
8720 if (p->left)
8721 p->left->parent = p;
8722 t->right = p;
8724 p->balance = t->balance < 0;
8725 r->balance = -(t->balance > 0);
8726 t->balance = 0;
8728 s = p->parent;
8729 p->parent = t;
8730 r->parent = t;
8731 t->parent = s;
8732 if (s)
8734 if (s->left == p)
8735 s->left = t;
8736 else
8737 s->right = t;
8739 else
8740 constructor_pending_elts = t;
8742 break;
8744 else
8746 /* p->balance == +1; growth of left side balances the node. */
8747 p->balance = 0;
8748 break;
8751 else /* r == p->right */
8753 if (p->balance == 0)
8754 /* Growth propagation from right side. */
8755 p->balance++;
8756 else if (p->balance > 0)
8758 if (r->balance > 0)
8760 /* R rotation. */
8761 p->right = r->left;
8762 if (p->right)
8763 p->right->parent = p;
8764 r->left = p;
8766 p->balance = 0;
8767 r->balance = 0;
8769 s = p->parent;
8770 p->parent = r;
8771 r->parent = s;
8772 if (s)
8774 if (s->left == p)
8775 s->left = r;
8776 else
8777 s->right = r;
8779 else
8780 constructor_pending_elts = r;
8782 else /* r->balance == -1 */
8784 /* RL rotation */
8785 struct init_node *t = r->left;
8787 r->left = t->right;
8788 if (r->left)
8789 r->left->parent = r;
8790 t->right = r;
8792 p->right = t->left;
8793 if (p->right)
8794 p->right->parent = p;
8795 t->left = p;
8797 r->balance = (t->balance < 0);
8798 p->balance = -(t->balance > 0);
8799 t->balance = 0;
8801 s = p->parent;
8802 p->parent = t;
8803 r->parent = t;
8804 t->parent = s;
8805 if (s)
8807 if (s->left == p)
8808 s->left = t;
8809 else
8810 s->right = t;
8812 else
8813 constructor_pending_elts = t;
8815 break;
8817 else
8819 /* p->balance == -1; growth of right side balances the node. */
8820 p->balance = 0;
8821 break;
8825 r = p;
8826 p = p->parent;
8830 /* Build AVL tree from a sorted chain. */
8832 static void
8833 set_nonincremental_init (struct obstack * braced_init_obstack)
8835 unsigned HOST_WIDE_INT ix;
8836 tree index, value;
8838 if (TREE_CODE (constructor_type) != RECORD_TYPE
8839 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8840 return;
8842 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8843 add_pending_init (input_location, index, value, NULL_TREE, true,
8844 braced_init_obstack);
8845 constructor_elements = NULL;
8846 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8848 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8849 /* Skip any nameless bit fields at the beginning. */
8850 while (constructor_unfilled_fields != NULL_TREE
8851 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8852 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
8853 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8856 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8858 if (TYPE_DOMAIN (constructor_type))
8859 constructor_unfilled_index
8860 = convert (bitsizetype,
8861 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8862 else
8863 constructor_unfilled_index = bitsize_zero_node;
8865 constructor_incremental = 0;
8868 /* Build AVL tree from a string constant. */
8870 static void
8871 set_nonincremental_init_from_string (tree str,
8872 struct obstack * braced_init_obstack)
8874 tree value, purpose, type;
8875 HOST_WIDE_INT val[2];
8876 const char *p, *end;
8877 int byte, wchar_bytes, charwidth, bitpos;
8879 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8881 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8882 charwidth = TYPE_PRECISION (char_type_node);
8883 gcc_assert ((size_t) wchar_bytes * charwidth
8884 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8885 type = TREE_TYPE (constructor_type);
8886 p = TREE_STRING_POINTER (str);
8887 end = p + TREE_STRING_LENGTH (str);
8889 for (purpose = bitsize_zero_node;
8890 p < end
8891 && !(constructor_max_index
8892 && tree_int_cst_lt (constructor_max_index, purpose));
8893 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8895 if (wchar_bytes == 1)
8897 val[0] = (unsigned char) *p++;
8898 val[1] = 0;
8900 else
8902 val[1] = 0;
8903 val[0] = 0;
8904 for (byte = 0; byte < wchar_bytes; byte++)
8906 if (BYTES_BIG_ENDIAN)
8907 bitpos = (wchar_bytes - byte - 1) * charwidth;
8908 else
8909 bitpos = byte * charwidth;
8910 val[bitpos / HOST_BITS_PER_WIDE_INT]
8911 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8912 << (bitpos % HOST_BITS_PER_WIDE_INT);
8916 if (!TYPE_UNSIGNED (type))
8918 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8919 if (bitpos < HOST_BITS_PER_WIDE_INT)
8921 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8923 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8924 val[1] = -1;
8927 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8929 if (val[0] < 0)
8930 val[1] = -1;
8932 else if (val[1] & (HOST_WIDE_INT_1
8933 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8934 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8937 value = wide_int_to_tree (type,
8938 wide_int::from_array (val, 2,
8939 HOST_BITS_PER_WIDE_INT * 2));
8940 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8941 braced_init_obstack);
8944 constructor_incremental = 0;
8947 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
8948 not initialized yet. */
8950 static tree
8951 find_init_member (tree field, struct obstack * braced_init_obstack)
8953 struct init_node *p;
8955 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8957 if (constructor_incremental
8958 && tree_int_cst_lt (field, constructor_unfilled_index))
8959 set_nonincremental_init (braced_init_obstack);
8961 p = constructor_pending_elts;
8962 while (p)
8964 if (tree_int_cst_lt (field, p->purpose))
8965 p = p->left;
8966 else if (tree_int_cst_lt (p->purpose, field))
8967 p = p->right;
8968 else
8969 return p->value;
8972 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8974 tree bitpos = bit_position (field);
8976 if (constructor_incremental
8977 && (!constructor_unfilled_fields
8978 || tree_int_cst_lt (bitpos,
8979 bit_position (constructor_unfilled_fields))))
8980 set_nonincremental_init (braced_init_obstack);
8982 p = constructor_pending_elts;
8983 while (p)
8985 if (field == p->purpose)
8986 return p->value;
8987 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8988 p = p->left;
8989 else
8990 p = p->right;
8993 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8995 if (!vec_safe_is_empty (constructor_elements)
8996 && (constructor_elements->last ().index == field))
8997 return constructor_elements->last ().value;
8999 return NULL_TREE;
9002 /* "Output" the next constructor element.
9003 At top level, really output it to assembler code now.
9004 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9005 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9006 TYPE is the data type that the containing data type wants here.
9007 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9008 If VALUE is a string constant, STRICT_STRING is true if it is
9009 unparenthesized or we should not warn here for it being parenthesized.
9010 For other types of VALUE, STRICT_STRING is not used.
9012 PENDING if true means output pending elements that belong
9013 right after this element. (PENDING is normally true;
9014 it is false while outputting pending elements, to avoid recursion.)
9016 IMPLICIT is true if value comes from pop_init_level (1),
9017 the new initializer has been merged with the existing one
9018 and thus no warnings should be emitted about overriding an
9019 existing initializer. */
9021 static void
9022 output_init_element (location_t loc, tree value, tree origtype,
9023 bool strict_string, tree type, tree field, bool pending,
9024 bool implicit, struct obstack * braced_init_obstack)
9026 tree semantic_type = NULL_TREE;
9027 bool maybe_const = true;
9028 bool npc;
9030 if (type == error_mark_node || value == error_mark_node)
9032 constructor_erroneous = 1;
9033 return;
9035 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9036 && (TREE_CODE (value) == STRING_CST
9037 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9038 && !(TREE_CODE (value) == STRING_CST
9039 && TREE_CODE (type) == ARRAY_TYPE
9040 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9041 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9042 TYPE_MAIN_VARIANT (type)))
9043 value = array_to_pointer_conversion (input_location, value);
9045 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9046 && require_constant_value && pending)
9048 /* As an extension, allow initializing objects with static storage
9049 duration with compound literals (which are then treated just as
9050 the brace enclosed list they contain). */
9051 if (flag_isoc99)
9052 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9053 "constant");
9054 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9055 value = DECL_INITIAL (decl);
9058 npc = null_pointer_constant_p (value);
9059 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9061 semantic_type = TREE_TYPE (value);
9062 value = TREE_OPERAND (value, 0);
9064 value = c_fully_fold (value, require_constant_value, &maybe_const);
9066 if (value == error_mark_node)
9067 constructor_erroneous = 1;
9068 else if (!TREE_CONSTANT (value))
9069 constructor_constant = 0;
9070 else if (!initializer_constant_valid_p (value,
9071 TREE_TYPE (value),
9072 AGGREGATE_TYPE_P (constructor_type)
9073 && TYPE_REVERSE_STORAGE_ORDER
9074 (constructor_type))
9075 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9076 && DECL_C_BIT_FIELD (field)
9077 && TREE_CODE (value) != INTEGER_CST))
9078 constructor_simple = 0;
9079 if (!maybe_const)
9080 constructor_nonconst = 1;
9082 /* Digest the initializer and issue any errors about incompatible
9083 types before issuing errors about non-constant initializers. */
9084 tree new_value = value;
9085 if (semantic_type)
9086 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9087 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9088 require_constant_value);
9089 if (new_value == error_mark_node)
9091 constructor_erroneous = 1;
9092 return;
9094 if (require_constant_value || require_constant_elements)
9095 constant_expression_warning (new_value);
9097 /* Proceed to check the constness of the original initializer. */
9098 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9100 if (require_constant_value)
9102 error_init (loc, "initializer element is not constant");
9103 value = error_mark_node;
9105 else if (require_constant_elements)
9106 pedwarn (loc, OPT_Wpedantic,
9107 "initializer element is not computable at load time");
9109 else if (!maybe_const
9110 && (require_constant_value || require_constant_elements))
9111 pedwarn_init (loc, OPT_Wpedantic,
9112 "initializer element is not a constant expression");
9114 /* Issue -Wc++-compat warnings about initializing a bitfield with
9115 enum type. */
9116 if (warn_cxx_compat
9117 && field != NULL_TREE
9118 && TREE_CODE (field) == FIELD_DECL
9119 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9120 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9121 != TYPE_MAIN_VARIANT (type))
9122 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9124 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9125 if (checktype != error_mark_node
9126 && (TYPE_MAIN_VARIANT (checktype)
9127 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9128 warning_init (loc, OPT_Wc___compat,
9129 "enum conversion in initialization is invalid in C++");
9132 /* If this field is empty (and not at the end of structure),
9133 don't do anything other than checking the initializer. */
9134 if (field
9135 && (TREE_TYPE (field) == error_mark_node
9136 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9137 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9138 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9139 || DECL_CHAIN (field)))))
9140 return;
9142 /* Finally, set VALUE to the initializer value digested above. */
9143 value = new_value;
9145 /* If this element doesn't come next in sequence,
9146 put it on constructor_pending_elts. */
9147 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9148 && (!constructor_incremental
9149 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9151 if (constructor_incremental
9152 && tree_int_cst_lt (field, constructor_unfilled_index))
9153 set_nonincremental_init (braced_init_obstack);
9155 add_pending_init (loc, field, value, origtype, implicit,
9156 braced_init_obstack);
9157 return;
9159 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9160 && (!constructor_incremental
9161 || field != constructor_unfilled_fields))
9163 /* We do this for records but not for unions. In a union,
9164 no matter which field is specified, it can be initialized
9165 right away since it starts at the beginning of the union. */
9166 if (constructor_incremental)
9168 if (!constructor_unfilled_fields)
9169 set_nonincremental_init (braced_init_obstack);
9170 else
9172 tree bitpos, unfillpos;
9174 bitpos = bit_position (field);
9175 unfillpos = bit_position (constructor_unfilled_fields);
9177 if (tree_int_cst_lt (bitpos, unfillpos))
9178 set_nonincremental_init (braced_init_obstack);
9182 add_pending_init (loc, field, value, origtype, implicit,
9183 braced_init_obstack);
9184 return;
9186 else if (TREE_CODE (constructor_type) == UNION_TYPE
9187 && !vec_safe_is_empty (constructor_elements))
9189 if (!implicit)
9191 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9192 warning_init (loc, OPT_Woverride_init_side_effects,
9193 "initialized field with side-effects overwritten");
9194 else if (warn_override_init)
9195 warning_init (loc, OPT_Woverride_init,
9196 "initialized field overwritten");
9199 /* We can have just one union field set. */
9200 constructor_elements = NULL;
9203 /* Otherwise, output this element either to
9204 constructor_elements or to the assembler file. */
9206 constructor_elt celt = {field, value};
9207 vec_safe_push (constructor_elements, celt);
9209 /* Advance the variable that indicates sequential elements output. */
9210 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9211 constructor_unfilled_index
9212 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9213 bitsize_one_node);
9214 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9216 constructor_unfilled_fields
9217 = DECL_CHAIN (constructor_unfilled_fields);
9219 /* Skip any nameless bit fields. */
9220 while (constructor_unfilled_fields != NULL_TREE
9221 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9222 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
9223 constructor_unfilled_fields =
9224 DECL_CHAIN (constructor_unfilled_fields);
9226 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9227 constructor_unfilled_fields = NULL_TREE;
9229 /* Now output any pending elements which have become next. */
9230 if (pending)
9231 output_pending_init_elements (0, braced_init_obstack);
9234 /* Output any pending elements which have become next.
9235 As we output elements, constructor_unfilled_{fields,index}
9236 advances, which may cause other elements to become next;
9237 if so, they too are output.
9239 If ALL is 0, we return when there are
9240 no more pending elements to output now.
9242 If ALL is 1, we output space as necessary so that
9243 we can output all the pending elements. */
9244 static void
9245 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9247 struct init_node *elt = constructor_pending_elts;
9248 tree next;
9250 retry:
9252 /* Look through the whole pending tree.
9253 If we find an element that should be output now,
9254 output it. Otherwise, set NEXT to the element
9255 that comes first among those still pending. */
9257 next = NULL_TREE;
9258 while (elt)
9260 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9262 if (tree_int_cst_equal (elt->purpose,
9263 constructor_unfilled_index))
9264 output_init_element (input_location, elt->value, elt->origtype,
9265 true, TREE_TYPE (constructor_type),
9266 constructor_unfilled_index, false, false,
9267 braced_init_obstack);
9268 else if (tree_int_cst_lt (constructor_unfilled_index,
9269 elt->purpose))
9271 /* Advance to the next smaller node. */
9272 if (elt->left)
9273 elt = elt->left;
9274 else
9276 /* We have reached the smallest node bigger than the
9277 current unfilled index. Fill the space first. */
9278 next = elt->purpose;
9279 break;
9282 else
9284 /* Advance to the next bigger node. */
9285 if (elt->right)
9286 elt = elt->right;
9287 else
9289 /* We have reached the biggest node in a subtree. Find
9290 the parent of it, which is the next bigger node. */
9291 while (elt->parent && elt->parent->right == elt)
9292 elt = elt->parent;
9293 elt = elt->parent;
9294 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9295 elt->purpose))
9297 next = elt->purpose;
9298 break;
9303 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9305 tree ctor_unfilled_bitpos, elt_bitpos;
9307 /* If the current record is complete we are done. */
9308 if (constructor_unfilled_fields == NULL_TREE)
9309 break;
9311 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9312 elt_bitpos = bit_position (elt->purpose);
9313 /* We can't compare fields here because there might be empty
9314 fields in between. */
9315 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9317 constructor_unfilled_fields = elt->purpose;
9318 output_init_element (input_location, elt->value, elt->origtype,
9319 true, TREE_TYPE (elt->purpose),
9320 elt->purpose, false, false,
9321 braced_init_obstack);
9323 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9325 /* Advance to the next smaller node. */
9326 if (elt->left)
9327 elt = elt->left;
9328 else
9330 /* We have reached the smallest node bigger than the
9331 current unfilled field. Fill the space first. */
9332 next = elt->purpose;
9333 break;
9336 else
9338 /* Advance to the next bigger node. */
9339 if (elt->right)
9340 elt = elt->right;
9341 else
9343 /* We have reached the biggest node in a subtree. Find
9344 the parent of it, which is the next bigger node. */
9345 while (elt->parent && elt->parent->right == elt)
9346 elt = elt->parent;
9347 elt = elt->parent;
9348 if (elt
9349 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9350 bit_position (elt->purpose))))
9352 next = elt->purpose;
9353 break;
9360 /* Ordinarily return, but not if we want to output all
9361 and there are elements left. */
9362 if (!(all && next != NULL_TREE))
9363 return;
9365 /* If it's not incremental, just skip over the gap, so that after
9366 jumping to retry we will output the next successive element. */
9367 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9368 constructor_unfilled_fields = next;
9369 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9370 constructor_unfilled_index = next;
9372 /* ELT now points to the node in the pending tree with the next
9373 initializer to output. */
9374 goto retry;
9377 /* Add one non-braced element to the current constructor level.
9378 This adjusts the current position within the constructor's type.
9379 This may also start or terminate implicit levels
9380 to handle a partly-braced initializer.
9382 Once this has found the correct level for the new element,
9383 it calls output_init_element.
9385 IMPLICIT is true if value comes from pop_init_level (1),
9386 the new initializer has been merged with the existing one
9387 and thus no warnings should be emitted about overriding an
9388 existing initializer. */
9390 void
9391 process_init_element (location_t loc, struct c_expr value, bool implicit,
9392 struct obstack * braced_init_obstack)
9394 tree orig_value = value.value;
9395 int string_flag
9396 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9397 bool strict_string = value.original_code == STRING_CST;
9398 bool was_designated = designator_depth != 0;
9400 designator_depth = 0;
9401 designator_erroneous = 0;
9403 if (!implicit && value.value && !integer_zerop (value.value))
9404 constructor_zeroinit = 0;
9406 /* Handle superfluous braces around string cst as in
9407 char x[] = {"foo"}; */
9408 if (string_flag
9409 && constructor_type
9410 && !was_designated
9411 && TREE_CODE (constructor_type) == ARRAY_TYPE
9412 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9413 && integer_zerop (constructor_unfilled_index))
9415 if (constructor_stack->replacement_value.value)
9416 error_init (loc, "excess elements in char array initializer");
9417 constructor_stack->replacement_value = value;
9418 return;
9421 if (constructor_stack->replacement_value.value != NULL_TREE)
9423 error_init (loc, "excess elements in struct initializer");
9424 return;
9427 /* Ignore elements of a brace group if it is entirely superfluous
9428 and has already been diagnosed. */
9429 if (constructor_type == NULL_TREE)
9430 return;
9432 if (!implicit && warn_designated_init && !was_designated
9433 && TREE_CODE (constructor_type) == RECORD_TYPE
9434 && lookup_attribute ("designated_init",
9435 TYPE_ATTRIBUTES (constructor_type)))
9436 warning_init (loc,
9437 OPT_Wdesignated_init,
9438 "positional initialization of field "
9439 "in %<struct%> declared with %<designated_init%> attribute");
9441 /* If we've exhausted any levels that didn't have braces,
9442 pop them now. */
9443 while (constructor_stack->implicit)
9445 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9446 && constructor_fields == NULL_TREE)
9447 process_init_element (loc,
9448 pop_init_level (loc, 1, braced_init_obstack,
9449 last_init_list_comma),
9450 true, braced_init_obstack);
9451 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9452 || VECTOR_TYPE_P (constructor_type))
9453 && constructor_max_index
9454 && tree_int_cst_lt (constructor_max_index,
9455 constructor_index))
9456 process_init_element (loc,
9457 pop_init_level (loc, 1, braced_init_obstack,
9458 last_init_list_comma),
9459 true, braced_init_obstack);
9460 else
9461 break;
9464 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9465 if (constructor_range_stack)
9467 /* If value is a compound literal and we'll be just using its
9468 content, don't put it into a SAVE_EXPR. */
9469 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9470 || !require_constant_value)
9472 tree semantic_type = NULL_TREE;
9473 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9475 semantic_type = TREE_TYPE (value.value);
9476 value.value = TREE_OPERAND (value.value, 0);
9478 value.value = save_expr (value.value);
9479 if (semantic_type)
9480 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9481 value.value);
9485 while (1)
9487 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9489 tree fieldtype;
9490 enum tree_code fieldcode;
9492 if (constructor_fields == NULL_TREE)
9494 pedwarn_init (loc, 0, "excess elements in struct initializer");
9495 break;
9498 fieldtype = TREE_TYPE (constructor_fields);
9499 if (fieldtype != error_mark_node)
9500 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9501 fieldcode = TREE_CODE (fieldtype);
9503 /* Error for non-static initialization of a flexible array member. */
9504 if (fieldcode == ARRAY_TYPE
9505 && !require_constant_value
9506 && TYPE_SIZE (fieldtype) == NULL_TREE
9507 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9509 error_init (loc, "non-static initialization of a flexible "
9510 "array member");
9511 break;
9514 /* Error for initialization of a flexible array member with
9515 a string constant if the structure is in an array. E.g.:
9516 struct S { int x; char y[]; };
9517 struct S s[] = { { 1, "foo" } };
9518 is invalid. */
9519 if (string_flag
9520 && fieldcode == ARRAY_TYPE
9521 && constructor_depth > 1
9522 && TYPE_SIZE (fieldtype) == NULL_TREE
9523 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9525 bool in_array_p = false;
9526 for (struct constructor_stack *p = constructor_stack;
9527 p && p->type; p = p->next)
9528 if (TREE_CODE (p->type) == ARRAY_TYPE)
9530 in_array_p = true;
9531 break;
9533 if (in_array_p)
9535 error_init (loc, "initialization of flexible array "
9536 "member in a nested context");
9537 break;
9541 /* Accept a string constant to initialize a subarray. */
9542 if (value.value != NULL_TREE
9543 && fieldcode == ARRAY_TYPE
9544 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9545 && string_flag)
9546 value.value = orig_value;
9547 /* Otherwise, if we have come to a subaggregate,
9548 and we don't have an element of its type, push into it. */
9549 else if (value.value != NULL_TREE
9550 && value.value != error_mark_node
9551 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9552 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9553 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9555 push_init_level (loc, 1, braced_init_obstack);
9556 continue;
9559 if (value.value)
9561 push_member_name (constructor_fields);
9562 output_init_element (loc, value.value, value.original_type,
9563 strict_string, fieldtype,
9564 constructor_fields, true, implicit,
9565 braced_init_obstack);
9566 RESTORE_SPELLING_DEPTH (constructor_depth);
9568 else
9569 /* Do the bookkeeping for an element that was
9570 directly output as a constructor. */
9572 /* For a record, keep track of end position of last field. */
9573 if (DECL_SIZE (constructor_fields))
9574 constructor_bit_index
9575 = size_binop_loc (input_location, PLUS_EXPR,
9576 bit_position (constructor_fields),
9577 DECL_SIZE (constructor_fields));
9579 /* If the current field was the first one not yet written out,
9580 it isn't now, so update. */
9581 if (constructor_unfilled_fields == constructor_fields)
9583 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9584 /* Skip any nameless bit fields. */
9585 while (constructor_unfilled_fields != 0
9586 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9587 && DECL_NAME (constructor_unfilled_fields) == 0)
9588 constructor_unfilled_fields =
9589 DECL_CHAIN (constructor_unfilled_fields);
9593 constructor_fields = DECL_CHAIN (constructor_fields);
9594 /* Skip any nameless bit fields at the beginning. */
9595 while (constructor_fields != NULL_TREE
9596 && DECL_C_BIT_FIELD (constructor_fields)
9597 && DECL_NAME (constructor_fields) == NULL_TREE)
9598 constructor_fields = DECL_CHAIN (constructor_fields);
9600 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9602 tree fieldtype;
9603 enum tree_code fieldcode;
9605 if (constructor_fields == NULL_TREE)
9607 pedwarn_init (loc, 0,
9608 "excess elements in union initializer");
9609 break;
9612 fieldtype = TREE_TYPE (constructor_fields);
9613 if (fieldtype != error_mark_node)
9614 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9615 fieldcode = TREE_CODE (fieldtype);
9617 /* Warn that traditional C rejects initialization of unions.
9618 We skip the warning if the value is zero. This is done
9619 under the assumption that the zero initializer in user
9620 code appears conditioned on e.g. __STDC__ to avoid
9621 "missing initializer" warnings and relies on default
9622 initialization to zero in the traditional C case.
9623 We also skip the warning if the initializer is designated,
9624 again on the assumption that this must be conditional on
9625 __STDC__ anyway (and we've already complained about the
9626 member-designator already). */
9627 if (!in_system_header_at (input_location) && !constructor_designated
9628 && !(value.value && (integer_zerop (value.value)
9629 || real_zerop (value.value))))
9630 warning (OPT_Wtraditional, "traditional C rejects initialization "
9631 "of unions");
9633 /* Accept a string constant to initialize a subarray. */
9634 if (value.value != NULL_TREE
9635 && fieldcode == ARRAY_TYPE
9636 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9637 && string_flag)
9638 value.value = orig_value;
9639 /* Otherwise, if we have come to a subaggregate,
9640 and we don't have an element of its type, push into it. */
9641 else if (value.value != NULL_TREE
9642 && value.value != error_mark_node
9643 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9644 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9645 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9647 push_init_level (loc, 1, braced_init_obstack);
9648 continue;
9651 if (value.value)
9653 push_member_name (constructor_fields);
9654 output_init_element (loc, value.value, value.original_type,
9655 strict_string, fieldtype,
9656 constructor_fields, true, implicit,
9657 braced_init_obstack);
9658 RESTORE_SPELLING_DEPTH (constructor_depth);
9660 else
9661 /* Do the bookkeeping for an element that was
9662 directly output as a constructor. */
9664 constructor_bit_index = DECL_SIZE (constructor_fields);
9665 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9668 constructor_fields = NULL_TREE;
9670 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9672 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9673 enum tree_code eltcode = TREE_CODE (elttype);
9675 /* Accept a string constant to initialize a subarray. */
9676 if (value.value != NULL_TREE
9677 && eltcode == ARRAY_TYPE
9678 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9679 && string_flag)
9680 value.value = orig_value;
9681 /* Otherwise, if we have come to a subaggregate,
9682 and we don't have an element of its type, push into it. */
9683 else if (value.value != NULL_TREE
9684 && value.value != error_mark_node
9685 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9686 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9687 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9689 push_init_level (loc, 1, braced_init_obstack);
9690 continue;
9693 if (constructor_max_index != NULL_TREE
9694 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9695 || integer_all_onesp (constructor_max_index)))
9697 pedwarn_init (loc, 0,
9698 "excess elements in array initializer");
9699 break;
9702 /* Now output the actual element. */
9703 if (value.value)
9705 push_array_bounds (tree_to_uhwi (constructor_index));
9706 output_init_element (loc, value.value, value.original_type,
9707 strict_string, elttype,
9708 constructor_index, true, implicit,
9709 braced_init_obstack);
9710 RESTORE_SPELLING_DEPTH (constructor_depth);
9713 constructor_index
9714 = size_binop_loc (input_location, PLUS_EXPR,
9715 constructor_index, bitsize_one_node);
9717 if (!value.value)
9718 /* If we are doing the bookkeeping for an element that was
9719 directly output as a constructor, we must update
9720 constructor_unfilled_index. */
9721 constructor_unfilled_index = constructor_index;
9723 else if (VECTOR_TYPE_P (constructor_type))
9725 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9727 /* Do a basic check of initializer size. Note that vectors
9728 always have a fixed size derived from their type. */
9729 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9731 pedwarn_init (loc, 0,
9732 "excess elements in vector initializer");
9733 break;
9736 /* Now output the actual element. */
9737 if (value.value)
9739 if (TREE_CODE (value.value) == VECTOR_CST)
9740 elttype = TYPE_MAIN_VARIANT (constructor_type);
9741 output_init_element (loc, value.value, value.original_type,
9742 strict_string, elttype,
9743 constructor_index, true, implicit,
9744 braced_init_obstack);
9747 constructor_index
9748 = size_binop_loc (input_location,
9749 PLUS_EXPR, constructor_index, bitsize_one_node);
9751 if (!value.value)
9752 /* If we are doing the bookkeeping for an element that was
9753 directly output as a constructor, we must update
9754 constructor_unfilled_index. */
9755 constructor_unfilled_index = constructor_index;
9758 /* Handle the sole element allowed in a braced initializer
9759 for a scalar variable. */
9760 else if (constructor_type != error_mark_node
9761 && constructor_fields == NULL_TREE)
9763 pedwarn_init (loc, 0,
9764 "excess elements in scalar initializer");
9765 break;
9767 else
9769 if (value.value)
9770 output_init_element (loc, value.value, value.original_type,
9771 strict_string, constructor_type,
9772 NULL_TREE, true, implicit,
9773 braced_init_obstack);
9774 constructor_fields = NULL_TREE;
9777 /* Handle range initializers either at this level or anywhere higher
9778 in the designator stack. */
9779 if (constructor_range_stack)
9781 struct constructor_range_stack *p, *range_stack;
9782 int finish = 0;
9784 range_stack = constructor_range_stack;
9785 constructor_range_stack = 0;
9786 while (constructor_stack != range_stack->stack)
9788 gcc_assert (constructor_stack->implicit);
9789 process_init_element (loc,
9790 pop_init_level (loc, 1,
9791 braced_init_obstack,
9792 last_init_list_comma),
9793 true, braced_init_obstack);
9795 for (p = range_stack;
9796 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9797 p = p->prev)
9799 gcc_assert (constructor_stack->implicit);
9800 process_init_element (loc,
9801 pop_init_level (loc, 1,
9802 braced_init_obstack,
9803 last_init_list_comma),
9804 true, braced_init_obstack);
9807 p->index = size_binop_loc (input_location,
9808 PLUS_EXPR, p->index, bitsize_one_node);
9809 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9810 finish = 1;
9812 while (1)
9814 constructor_index = p->index;
9815 constructor_fields = p->fields;
9816 if (finish && p->range_end && p->index == p->range_start)
9818 finish = 0;
9819 p->prev = 0;
9821 p = p->next;
9822 if (!p)
9823 break;
9824 finish_implicit_inits (loc, braced_init_obstack);
9825 push_init_level (loc, 2, braced_init_obstack);
9826 p->stack = constructor_stack;
9827 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9828 p->index = p->range_start;
9831 if (!finish)
9832 constructor_range_stack = range_stack;
9833 continue;
9836 break;
9839 constructor_range_stack = 0;
9842 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9843 (guaranteed to be 'volatile' or null) and ARGS (represented using
9844 an ASM_EXPR node). */
9845 tree
9846 build_asm_stmt (tree cv_qualifier, tree args)
9848 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9849 ASM_VOLATILE_P (args) = 1;
9850 return add_stmt (args);
9853 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9854 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9855 SIMPLE indicates whether there was anything at all after the
9856 string in the asm expression -- asm("blah") and asm("blah" : )
9857 are subtly different. We use a ASM_EXPR node to represent this. */
9858 tree
9859 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9860 tree clobbers, tree labels, bool simple)
9862 tree tail;
9863 tree args;
9864 int i;
9865 const char *constraint;
9866 const char **oconstraints;
9867 bool allows_mem, allows_reg, is_inout;
9868 int ninputs, noutputs;
9870 ninputs = list_length (inputs);
9871 noutputs = list_length (outputs);
9872 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9874 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9876 /* Remove output conversions that change the type but not the mode. */
9877 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9879 tree output = TREE_VALUE (tail);
9881 output = c_fully_fold (output, false, NULL);
9883 /* ??? Really, this should not be here. Users should be using a
9884 proper lvalue, dammit. But there's a long history of using casts
9885 in the output operands. In cases like longlong.h, this becomes a
9886 primitive form of typechecking -- if the cast can be removed, then
9887 the output operand had a type of the proper width; otherwise we'll
9888 get an error. Gross, but ... */
9889 STRIP_NOPS (output);
9891 if (!lvalue_or_else (loc, output, lv_asm))
9892 output = error_mark_node;
9894 if (output != error_mark_node
9895 && (TREE_READONLY (output)
9896 || TYPE_READONLY (TREE_TYPE (output))
9897 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9898 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9899 readonly_error (loc, output, lv_asm);
9901 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9902 oconstraints[i] = constraint;
9904 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9905 &allows_mem, &allows_reg, &is_inout))
9907 /* If the operand is going to end up in memory,
9908 mark it addressable. */
9909 if (!allows_reg && !c_mark_addressable (output))
9910 output = error_mark_node;
9911 if (!(!allows_reg && allows_mem)
9912 && output != error_mark_node
9913 && VOID_TYPE_P (TREE_TYPE (output)))
9915 error_at (loc, "invalid use of void expression");
9916 output = error_mark_node;
9919 else
9920 output = error_mark_node;
9922 TREE_VALUE (tail) = output;
9925 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9927 tree input;
9929 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9930 input = TREE_VALUE (tail);
9932 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9933 oconstraints, &allows_mem, &allows_reg))
9935 /* If the operand is going to end up in memory,
9936 mark it addressable. */
9937 if (!allows_reg && allows_mem)
9939 input = c_fully_fold (input, false, NULL);
9941 /* Strip the nops as we allow this case. FIXME, this really
9942 should be rejected or made deprecated. */
9943 STRIP_NOPS (input);
9944 if (!c_mark_addressable (input))
9945 input = error_mark_node;
9947 else
9949 struct c_expr expr;
9950 memset (&expr, 0, sizeof (expr));
9951 expr.value = input;
9952 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9953 input = c_fully_fold (expr.value, false, NULL);
9955 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9957 error_at (loc, "invalid use of void expression");
9958 input = error_mark_node;
9962 else
9963 input = error_mark_node;
9965 TREE_VALUE (tail) = input;
9968 /* ASMs with labels cannot have outputs. This should have been
9969 enforced by the parser. */
9970 gcc_assert (outputs == NULL || labels == NULL);
9972 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9974 /* asm statements without outputs, including simple ones, are treated
9975 as volatile. */
9976 ASM_INPUT_P (args) = simple;
9977 ASM_VOLATILE_P (args) = (noutputs == 0);
9979 return args;
9982 /* Generate a goto statement to LABEL. LOC is the location of the
9983 GOTO. */
9985 tree
9986 c_finish_goto_label (location_t loc, tree label)
9988 tree decl = lookup_label_for_goto (loc, label);
9989 if (!decl)
9990 return NULL_TREE;
9991 TREE_USED (decl) = 1;
9993 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
9994 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9995 SET_EXPR_LOCATION (t, loc);
9996 return add_stmt (t);
10000 /* Generate a computed goto statement to EXPR. LOC is the location of
10001 the GOTO. */
10003 tree
10004 c_finish_goto_ptr (location_t loc, tree expr)
10006 tree t;
10007 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10008 expr = c_fully_fold (expr, false, NULL);
10009 expr = convert (ptr_type_node, expr);
10010 t = build1 (GOTO_EXPR, void_type_node, expr);
10011 SET_EXPR_LOCATION (t, loc);
10012 return add_stmt (t);
10015 /* Generate a C `return' statement. RETVAL is the expression for what
10016 to return, or a null pointer for `return;' with no value. LOC is
10017 the location of the return statement, or the location of the expression,
10018 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10019 is the original type of RETVAL. */
10021 tree
10022 c_finish_return (location_t loc, tree retval, tree origtype)
10024 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10025 bool no_warning = false;
10026 bool npc = false;
10027 size_t rank = 0;
10029 /* Use the expansion point to handle cases such as returning NULL
10030 in a function returning void. */
10031 source_location xloc = expansion_point_location_if_in_system_header (loc);
10033 if (TREE_THIS_VOLATILE (current_function_decl))
10034 warning_at (xloc, 0,
10035 "function declared %<noreturn%> has a %<return%> statement");
10037 if (flag_cilkplus && contains_array_notation_expr (retval))
10039 /* Array notations are allowed in a return statement if it is inside a
10040 built-in array notation reduction function. */
10041 if (!find_rank (loc, retval, retval, false, &rank))
10042 return error_mark_node;
10043 if (rank >= 1)
10045 error_at (loc, "array notation expression cannot be used as a "
10046 "return value");
10047 return error_mark_node;
10050 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
10052 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
10053 "allowed");
10054 return error_mark_node;
10056 if (retval)
10058 tree semantic_type = NULL_TREE;
10059 npc = null_pointer_constant_p (retval);
10060 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10062 semantic_type = TREE_TYPE (retval);
10063 retval = TREE_OPERAND (retval, 0);
10065 retval = c_fully_fold (retval, false, NULL);
10066 if (semantic_type)
10067 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10070 if (!retval)
10072 current_function_returns_null = 1;
10073 if ((warn_return_type || flag_isoc99)
10074 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10076 bool warned_here;
10077 if (flag_isoc99)
10078 warned_here = pedwarn
10079 (loc, 0,
10080 "%<return%> with no value, in function returning non-void");
10081 else
10082 warned_here = warning_at
10083 (loc, OPT_Wreturn_type,
10084 "%<return%> with no value, in function returning non-void");
10085 no_warning = true;
10086 if (warned_here)
10087 inform (DECL_SOURCE_LOCATION (current_function_decl),
10088 "declared here");
10091 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10093 current_function_returns_null = 1;
10094 bool warned_here;
10095 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10096 warned_here = pedwarn
10097 (xloc, 0,
10098 "%<return%> with a value, in function returning void");
10099 else
10100 warned_here = pedwarn
10101 (xloc, OPT_Wpedantic, "ISO C forbids "
10102 "%<return%> with expression, in function returning void");
10103 if (warned_here)
10104 inform (DECL_SOURCE_LOCATION (current_function_decl),
10105 "declared here");
10107 else
10109 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10110 retval, origtype, ic_return,
10111 npc, NULL_TREE, NULL_TREE, 0);
10112 tree res = DECL_RESULT (current_function_decl);
10113 tree inner;
10114 bool save;
10116 current_function_returns_value = 1;
10117 if (t == error_mark_node)
10118 return NULL_TREE;
10120 save = in_late_binary_op;
10121 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10122 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10123 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10124 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10125 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10126 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10127 in_late_binary_op = true;
10128 inner = t = convert (TREE_TYPE (res), t);
10129 in_late_binary_op = save;
10131 /* Strip any conversions, additions, and subtractions, and see if
10132 we are returning the address of a local variable. Warn if so. */
10133 while (1)
10135 switch (TREE_CODE (inner))
10137 CASE_CONVERT:
10138 case NON_LVALUE_EXPR:
10139 case PLUS_EXPR:
10140 case POINTER_PLUS_EXPR:
10141 inner = TREE_OPERAND (inner, 0);
10142 continue;
10144 case MINUS_EXPR:
10145 /* If the second operand of the MINUS_EXPR has a pointer
10146 type (or is converted from it), this may be valid, so
10147 don't give a warning. */
10149 tree op1 = TREE_OPERAND (inner, 1);
10151 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10152 && (CONVERT_EXPR_P (op1)
10153 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10154 op1 = TREE_OPERAND (op1, 0);
10156 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10157 break;
10159 inner = TREE_OPERAND (inner, 0);
10160 continue;
10163 case ADDR_EXPR:
10164 inner = TREE_OPERAND (inner, 0);
10166 while (REFERENCE_CLASS_P (inner)
10167 && !INDIRECT_REF_P (inner))
10168 inner = TREE_OPERAND (inner, 0);
10170 if (DECL_P (inner)
10171 && !DECL_EXTERNAL (inner)
10172 && !TREE_STATIC (inner)
10173 && DECL_CONTEXT (inner) == current_function_decl)
10175 if (TREE_CODE (inner) == LABEL_DECL)
10176 warning_at (loc, OPT_Wreturn_local_addr,
10177 "function returns address of label");
10178 else
10180 warning_at (loc, OPT_Wreturn_local_addr,
10181 "function returns address of local variable");
10182 tree zero = build_zero_cst (TREE_TYPE (res));
10183 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10186 break;
10188 default:
10189 break;
10192 break;
10195 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10196 SET_EXPR_LOCATION (retval, loc);
10198 if (warn_sequence_point)
10199 verify_sequence_points (retval);
10202 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10203 TREE_NO_WARNING (ret_stmt) |= no_warning;
10204 return add_stmt (ret_stmt);
10207 struct c_switch {
10208 /* The SWITCH_EXPR being built. */
10209 tree switch_expr;
10211 /* The original type of the testing expression, i.e. before the
10212 default conversion is applied. */
10213 tree orig_type;
10215 /* A splay-tree mapping the low element of a case range to the high
10216 element, or NULL_TREE if there is no high element. Used to
10217 determine whether or not a new case label duplicates an old case
10218 label. We need a tree, rather than simply a hash table, because
10219 of the GNU case range extension. */
10220 splay_tree cases;
10222 /* The bindings at the point of the switch. This is used for
10223 warnings crossing decls when branching to a case label. */
10224 struct c_spot_bindings *bindings;
10226 /* The next node on the stack. */
10227 struct c_switch *next;
10229 /* Remember whether the controlling expression had boolean type
10230 before integer promotions for the sake of -Wswitch-bool. */
10231 bool bool_cond_p;
10233 /* Remember whether there was a case value that is outside the
10234 range of the ORIG_TYPE. */
10235 bool outside_range_p;
10238 /* A stack of the currently active switch statements. The innermost
10239 switch statement is on the top of the stack. There is no need to
10240 mark the stack for garbage collection because it is only active
10241 during the processing of the body of a function, and we never
10242 collect at that point. */
10244 struct c_switch *c_switch_stack;
10246 /* Start a C switch statement, testing expression EXP. Return the new
10247 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10248 SWITCH_COND_LOC is the location of the switch's condition.
10249 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10251 tree
10252 c_start_case (location_t switch_loc,
10253 location_t switch_cond_loc,
10254 tree exp, bool explicit_cast_p)
10256 tree orig_type = error_mark_node;
10257 bool bool_cond_p = false;
10258 struct c_switch *cs;
10260 if (exp != error_mark_node)
10262 orig_type = TREE_TYPE (exp);
10264 if (!INTEGRAL_TYPE_P (orig_type))
10266 if (orig_type != error_mark_node)
10268 error_at (switch_cond_loc, "switch quantity not an integer");
10269 orig_type = error_mark_node;
10271 exp = integer_zero_node;
10273 else
10275 tree type = TYPE_MAIN_VARIANT (orig_type);
10276 tree e = exp;
10278 /* Warn if the condition has boolean value. */
10279 while (TREE_CODE (e) == COMPOUND_EXPR)
10280 e = TREE_OPERAND (e, 1);
10282 if ((TREE_CODE (type) == BOOLEAN_TYPE
10283 || truth_value_p (TREE_CODE (e)))
10284 /* Explicit cast to int suppresses this warning. */
10285 && !(TREE_CODE (type) == INTEGER_TYPE
10286 && explicit_cast_p))
10287 bool_cond_p = true;
10289 if (!in_system_header_at (input_location)
10290 && (type == long_integer_type_node
10291 || type == long_unsigned_type_node))
10292 warning_at (switch_cond_loc,
10293 OPT_Wtraditional, "%<long%> switch expression not "
10294 "converted to %<int%> in ISO C");
10296 exp = c_fully_fold (exp, false, NULL);
10297 exp = default_conversion (exp);
10299 if (warn_sequence_point)
10300 verify_sequence_points (exp);
10304 /* Add this new SWITCH_EXPR to the stack. */
10305 cs = XNEW (struct c_switch);
10306 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
10307 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10308 cs->orig_type = orig_type;
10309 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10310 cs->bindings = c_get_switch_bindings ();
10311 cs->bool_cond_p = bool_cond_p;
10312 cs->outside_range_p = false;
10313 cs->next = c_switch_stack;
10314 c_switch_stack = cs;
10316 return add_stmt (cs->switch_expr);
10319 /* Process a case label at location LOC. */
10321 tree
10322 do_case (location_t loc, tree low_value, tree high_value)
10324 tree label = NULL_TREE;
10326 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10328 low_value = c_fully_fold (low_value, false, NULL);
10329 if (TREE_CODE (low_value) == INTEGER_CST)
10330 pedwarn (loc, OPT_Wpedantic,
10331 "case label is not an integer constant expression");
10334 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10336 high_value = c_fully_fold (high_value, false, NULL);
10337 if (TREE_CODE (high_value) == INTEGER_CST)
10338 pedwarn (input_location, OPT_Wpedantic,
10339 "case label is not an integer constant expression");
10342 if (c_switch_stack == NULL)
10344 if (low_value)
10345 error_at (loc, "case label not within a switch statement");
10346 else
10347 error_at (loc, "%<default%> label not within a switch statement");
10348 return NULL_TREE;
10351 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10352 EXPR_LOCATION (c_switch_stack->switch_expr),
10353 loc))
10354 return NULL_TREE;
10356 label = c_add_case_label (loc, c_switch_stack->cases,
10357 SWITCH_COND (c_switch_stack->switch_expr),
10358 c_switch_stack->orig_type,
10359 low_value, high_value,
10360 &c_switch_stack->outside_range_p);
10361 if (label == error_mark_node)
10362 label = NULL_TREE;
10363 return label;
10366 /* Finish the switch statement. TYPE is the original type of the
10367 controlling expression of the switch, or NULL_TREE. */
10369 void
10370 c_finish_case (tree body, tree type)
10372 struct c_switch *cs = c_switch_stack;
10373 location_t switch_location;
10375 SWITCH_BODY (cs->switch_expr) = body;
10377 /* Emit warnings as needed. */
10378 switch_location = EXPR_LOCATION (cs->switch_expr);
10379 c_do_switch_warnings (cs->cases, switch_location,
10380 type ? type : TREE_TYPE (cs->switch_expr),
10381 SWITCH_COND (cs->switch_expr),
10382 cs->bool_cond_p, cs->outside_range_p);
10384 /* Pop the stack. */
10385 c_switch_stack = cs->next;
10386 splay_tree_delete (cs->cases);
10387 c_release_switch_bindings (cs->bindings);
10388 XDELETE (cs);
10391 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10392 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10393 may be null. */
10395 void
10396 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10397 tree else_block)
10399 tree stmt;
10401 /* If the condition has array notations, then the rank of the then_block and
10402 else_block must be either 0 or be equal to the rank of the condition. If
10403 the condition does not have array notations then break them up as it is
10404 broken up in a normal expression. */
10405 if (flag_cilkplus && contains_array_notation_expr (cond))
10407 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10408 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10409 return;
10410 if (then_block
10411 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10412 return;
10413 if (else_block
10414 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10415 return;
10416 if (cond_rank != then_rank && then_rank != 0)
10418 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10419 " and the then-block");
10420 return;
10422 else if (cond_rank != else_rank && else_rank != 0)
10424 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10425 " and the else-block");
10426 return;
10430 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10431 SET_EXPR_LOCATION (stmt, if_locus);
10432 add_stmt (stmt);
10435 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10436 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10437 is false for DO loops. INCR is the FOR increment expression. BODY is
10438 the statement controlled by the loop. BLAB is the break label. CLAB is
10439 the continue label. Everything is allowed to be NULL. */
10441 void
10442 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10443 tree blab, tree clab, bool cond_is_first)
10445 tree entry = NULL, exit = NULL, t;
10447 /* In theory could forbid cilk spawn for loop increment expression,
10448 but it should work just fine. */
10450 /* If the condition is zero don't generate a loop construct. */
10451 if (cond && integer_zerop (cond))
10453 if (cond_is_first)
10455 t = build_and_jump (&blab);
10456 SET_EXPR_LOCATION (t, start_locus);
10457 add_stmt (t);
10460 else
10462 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10464 /* If we have an exit condition, then we build an IF with gotos either
10465 out of the loop, or to the top of it. If there's no exit condition,
10466 then we just build a jump back to the top. */
10467 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10469 if (cond && !integer_nonzerop (cond))
10471 /* Canonicalize the loop condition to the end. This means
10472 generating a branch to the loop condition. Reuse the
10473 continue label, if possible. */
10474 if (cond_is_first)
10476 if (incr || !clab)
10478 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10479 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10481 else
10482 t = build1 (GOTO_EXPR, void_type_node, clab);
10483 SET_EXPR_LOCATION (t, start_locus);
10484 add_stmt (t);
10487 t = build_and_jump (&blab);
10488 if (cond_is_first)
10489 exit = fold_build3_loc (start_locus,
10490 COND_EXPR, void_type_node, cond, exit, t);
10491 else
10492 exit = fold_build3_loc (input_location,
10493 COND_EXPR, void_type_node, cond, exit, t);
10495 else
10497 /* For the backward-goto's location of an unconditional loop
10498 use the beginning of the body, or, if there is none, the
10499 top of the loop. */
10500 location_t loc = EXPR_LOCATION (expr_first (body));
10501 if (loc == UNKNOWN_LOCATION)
10502 loc = start_locus;
10503 SET_EXPR_LOCATION (exit, loc);
10506 add_stmt (top);
10509 if (body)
10510 add_stmt (body);
10511 if (clab)
10512 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10513 if (incr)
10514 add_stmt (incr);
10515 if (entry)
10516 add_stmt (entry);
10517 if (exit)
10518 add_stmt (exit);
10519 if (blab)
10520 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10523 tree
10524 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10526 bool skip;
10527 tree label = *label_p;
10529 /* In switch statements break is sometimes stylistically used after
10530 a return statement. This can lead to spurious warnings about
10531 control reaching the end of a non-void function when it is
10532 inlined. Note that we are calling block_may_fallthru with
10533 language specific tree nodes; this works because
10534 block_may_fallthru returns true when given something it does not
10535 understand. */
10536 skip = !block_may_fallthru (cur_stmt_list);
10538 if (!label)
10540 if (!skip)
10541 *label_p = label = create_artificial_label (loc);
10543 else if (TREE_CODE (label) == LABEL_DECL)
10545 else switch (TREE_INT_CST_LOW (label))
10547 case 0:
10548 if (is_break)
10549 error_at (loc, "break statement not within loop or switch");
10550 else
10551 error_at (loc, "continue statement not within a loop");
10552 return NULL_TREE;
10554 case 1:
10555 gcc_assert (is_break);
10556 error_at (loc, "break statement used with OpenMP for loop");
10557 return NULL_TREE;
10559 case 2:
10560 if (is_break)
10561 error ("break statement within %<#pragma simd%> loop body");
10562 else
10563 error ("continue statement within %<#pragma simd%> loop body");
10564 return NULL_TREE;
10566 default:
10567 gcc_unreachable ();
10570 if (skip)
10571 return NULL_TREE;
10573 if (!is_break)
10574 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10576 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10579 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10581 static void
10582 emit_side_effect_warnings (location_t loc, tree expr)
10584 if (expr == error_mark_node)
10586 else if (!TREE_SIDE_EFFECTS (expr))
10588 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10589 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10591 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10593 tree r = expr;
10594 location_t cloc = loc;
10595 while (TREE_CODE (r) == COMPOUND_EXPR)
10597 if (EXPR_HAS_LOCATION (r))
10598 cloc = EXPR_LOCATION (r);
10599 r = TREE_OPERAND (r, 1);
10601 if (!TREE_SIDE_EFFECTS (r)
10602 && !VOID_TYPE_P (TREE_TYPE (r))
10603 && !CONVERT_EXPR_P (r)
10604 && !TREE_NO_WARNING (r)
10605 && !TREE_NO_WARNING (expr))
10606 warning_at (cloc, OPT_Wunused_value,
10607 "right-hand operand of comma expression has no effect");
10609 else
10610 warn_if_unused_value (expr, loc);
10613 /* Process an expression as if it were a complete statement. Emit
10614 diagnostics, but do not call ADD_STMT. LOC is the location of the
10615 statement. */
10617 tree
10618 c_process_expr_stmt (location_t loc, tree expr)
10620 tree exprv;
10622 if (!expr)
10623 return NULL_TREE;
10625 expr = c_fully_fold (expr, false, NULL);
10627 if (warn_sequence_point)
10628 verify_sequence_points (expr);
10630 if (TREE_TYPE (expr) != error_mark_node
10631 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10632 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10633 error_at (loc, "expression statement has incomplete type");
10635 /* If we're not processing a statement expression, warn about unused values.
10636 Warnings for statement expressions will be emitted later, once we figure
10637 out which is the result. */
10638 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10639 && warn_unused_value)
10640 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10642 exprv = expr;
10643 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10644 exprv = TREE_OPERAND (exprv, 1);
10645 while (CONVERT_EXPR_P (exprv))
10646 exprv = TREE_OPERAND (exprv, 0);
10647 if (DECL_P (exprv)
10648 || handled_component_p (exprv)
10649 || TREE_CODE (exprv) == ADDR_EXPR)
10650 mark_exp_read (exprv);
10652 /* If the expression is not of a type to which we cannot assign a line
10653 number, wrap the thing in a no-op NOP_EXPR. */
10654 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10656 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10657 SET_EXPR_LOCATION (expr, loc);
10660 return expr;
10663 /* Emit an expression as a statement. LOC is the location of the
10664 expression. */
10666 tree
10667 c_finish_expr_stmt (location_t loc, tree expr)
10669 if (expr)
10670 return add_stmt (c_process_expr_stmt (loc, expr));
10671 else
10672 return NULL;
10675 /* Do the opposite and emit a statement as an expression. To begin,
10676 create a new binding level and return it. */
10678 tree
10679 c_begin_stmt_expr (void)
10681 tree ret;
10683 /* We must force a BLOCK for this level so that, if it is not expanded
10684 later, there is a way to turn off the entire subtree of blocks that
10685 are contained in it. */
10686 keep_next_level ();
10687 ret = c_begin_compound_stmt (true);
10689 c_bindings_start_stmt_expr (c_switch_stack == NULL
10690 ? NULL
10691 : c_switch_stack->bindings);
10693 /* Mark the current statement list as belonging to a statement list. */
10694 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10696 return ret;
10699 /* LOC is the location of the compound statement to which this body
10700 belongs. */
10702 tree
10703 c_finish_stmt_expr (location_t loc, tree body)
10705 tree last, type, tmp, val;
10706 tree *last_p;
10708 body = c_end_compound_stmt (loc, body, true);
10710 c_bindings_end_stmt_expr (c_switch_stack == NULL
10711 ? NULL
10712 : c_switch_stack->bindings);
10714 /* Locate the last statement in BODY. See c_end_compound_stmt
10715 about always returning a BIND_EXPR. */
10716 last_p = &BIND_EXPR_BODY (body);
10717 last = BIND_EXPR_BODY (body);
10719 continue_searching:
10720 if (TREE_CODE (last) == STATEMENT_LIST)
10722 tree_stmt_iterator i;
10724 /* This can happen with degenerate cases like ({ }). No value. */
10725 if (!TREE_SIDE_EFFECTS (last))
10726 return body;
10728 /* If we're supposed to generate side effects warnings, process
10729 all of the statements except the last. */
10730 if (warn_unused_value)
10732 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10734 location_t tloc;
10735 tree t = tsi_stmt (i);
10737 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10738 emit_side_effect_warnings (tloc, t);
10741 else
10742 i = tsi_last (last);
10743 last_p = tsi_stmt_ptr (i);
10744 last = *last_p;
10747 /* If the end of the list is exception related, then the list was split
10748 by a call to push_cleanup. Continue searching. */
10749 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10750 || TREE_CODE (last) == TRY_CATCH_EXPR)
10752 last_p = &TREE_OPERAND (last, 0);
10753 last = *last_p;
10754 goto continue_searching;
10757 if (last == error_mark_node)
10758 return last;
10760 /* In the case that the BIND_EXPR is not necessary, return the
10761 expression out from inside it. */
10762 if (last == BIND_EXPR_BODY (body)
10763 && BIND_EXPR_VARS (body) == NULL)
10765 /* Even if this looks constant, do not allow it in a constant
10766 expression. */
10767 last = c_wrap_maybe_const (last, true);
10768 /* Do not warn if the return value of a statement expression is
10769 unused. */
10770 TREE_NO_WARNING (last) = 1;
10771 return last;
10774 /* Extract the type of said expression. */
10775 type = TREE_TYPE (last);
10777 /* If we're not returning a value at all, then the BIND_EXPR that
10778 we already have is a fine expression to return. */
10779 if (!type || VOID_TYPE_P (type))
10780 return body;
10782 /* Now that we've located the expression containing the value, it seems
10783 silly to make voidify_wrapper_expr repeat the process. Create a
10784 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10785 tmp = create_tmp_var_raw (type);
10787 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10788 tree_expr_nonnegative_p giving up immediately. */
10789 val = last;
10790 if (TREE_CODE (val) == NOP_EXPR
10791 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10792 val = TREE_OPERAND (val, 0);
10794 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10795 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10798 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10799 SET_EXPR_LOCATION (t, loc);
10800 return t;
10804 /* Begin and end compound statements. This is as simple as pushing
10805 and popping new statement lists from the tree. */
10807 tree
10808 c_begin_compound_stmt (bool do_scope)
10810 tree stmt = push_stmt_list ();
10811 if (do_scope)
10812 push_scope ();
10813 return stmt;
10816 /* End a compound statement. STMT is the statement. LOC is the
10817 location of the compound statement-- this is usually the location
10818 of the opening brace. */
10820 tree
10821 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10823 tree block = NULL;
10825 if (do_scope)
10827 if (c_dialect_objc ())
10828 objc_clear_super_receiver ();
10829 block = pop_scope ();
10832 stmt = pop_stmt_list (stmt);
10833 stmt = c_build_bind_expr (loc, block, stmt);
10835 /* If this compound statement is nested immediately inside a statement
10836 expression, then force a BIND_EXPR to be created. Otherwise we'll
10837 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10838 STATEMENT_LISTs merge, and thus we can lose track of what statement
10839 was really last. */
10840 if (building_stmt_list_p ()
10841 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10842 && TREE_CODE (stmt) != BIND_EXPR)
10844 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10845 TREE_SIDE_EFFECTS (stmt) = 1;
10846 SET_EXPR_LOCATION (stmt, loc);
10849 return stmt;
10852 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10853 when the current scope is exited. EH_ONLY is true when this is not
10854 meant to apply to normal control flow transfer. */
10856 void
10857 push_cleanup (tree decl, tree cleanup, bool eh_only)
10859 enum tree_code code;
10860 tree stmt, list;
10861 bool stmt_expr;
10863 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10864 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10865 add_stmt (stmt);
10866 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10867 list = push_stmt_list ();
10868 TREE_OPERAND (stmt, 0) = list;
10869 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10872 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10873 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10875 static tree
10876 build_vec_cmp (tree_code code, tree type,
10877 tree arg0, tree arg1)
10879 tree zero_vec = build_zero_cst (type);
10880 tree minus_one_vec = build_minus_one_cst (type);
10881 tree cmp_type = build_same_sized_truth_vector_type (type);
10882 tree cmp = build2 (code, cmp_type, arg0, arg1);
10883 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10886 /* Build a binary-operation expression without default conversions.
10887 CODE is the kind of expression to build.
10888 LOCATION is the operator's location.
10889 This function differs from `build' in several ways:
10890 the data type of the result is computed and recorded in it,
10891 warnings are generated if arg data types are invalid,
10892 special handling for addition and subtraction of pointers is known,
10893 and some optimization is done (operations on narrow ints
10894 are done in the narrower type when that gives the same result).
10895 Constant folding is also done before the result is returned.
10897 Note that the operands will never have enumeral types, or function
10898 or array types, because either they will have the default conversions
10899 performed or they have both just been converted to some other type in which
10900 the arithmetic is to be done. */
10902 tree
10903 build_binary_op (location_t location, enum tree_code code,
10904 tree orig_op0, tree orig_op1, bool convert_p)
10906 tree type0, type1, orig_type0, orig_type1;
10907 tree eptype;
10908 enum tree_code code0, code1;
10909 tree op0, op1;
10910 tree ret = error_mark_node;
10911 const char *invalid_op_diag;
10912 bool op0_int_operands, op1_int_operands;
10913 bool int_const, int_const_or_overflow, int_operands;
10915 /* Expression code to give to the expression when it is built.
10916 Normally this is CODE, which is what the caller asked for,
10917 but in some special cases we change it. */
10918 enum tree_code resultcode = code;
10920 /* Data type in which the computation is to be performed.
10921 In the simplest cases this is the common type of the arguments. */
10922 tree result_type = NULL;
10924 /* When the computation is in excess precision, the type of the
10925 final EXCESS_PRECISION_EXPR. */
10926 tree semantic_result_type = NULL;
10928 /* Nonzero means operands have already been type-converted
10929 in whatever way is necessary.
10930 Zero means they need to be converted to RESULT_TYPE. */
10931 int converted = 0;
10933 /* Nonzero means create the expression with this type, rather than
10934 RESULT_TYPE. */
10935 tree build_type = NULL_TREE;
10937 /* Nonzero means after finally constructing the expression
10938 convert it to this type. */
10939 tree final_type = NULL_TREE;
10941 /* Nonzero if this is an operation like MIN or MAX which can
10942 safely be computed in short if both args are promoted shorts.
10943 Also implies COMMON.
10944 -1 indicates a bitwise operation; this makes a difference
10945 in the exact conditions for when it is safe to do the operation
10946 in a narrower mode. */
10947 int shorten = 0;
10949 /* Nonzero if this is a comparison operation;
10950 if both args are promoted shorts, compare the original shorts.
10951 Also implies COMMON. */
10952 int short_compare = 0;
10954 /* Nonzero if this is a right-shift operation, which can be computed on the
10955 original short and then promoted if the operand is a promoted short. */
10956 int short_shift = 0;
10958 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10959 int common = 0;
10961 /* True means types are compatible as far as ObjC is concerned. */
10962 bool objc_ok;
10964 /* True means this is an arithmetic operation that may need excess
10965 precision. */
10966 bool may_need_excess_precision;
10968 /* True means this is a boolean operation that converts both its
10969 operands to truth-values. */
10970 bool boolean_op = false;
10972 /* Remember whether we're doing / or %. */
10973 bool doing_div_or_mod = false;
10975 /* Remember whether we're doing << or >>. */
10976 bool doing_shift = false;
10978 /* Tree holding instrumentation expression. */
10979 tree instrument_expr = NULL;
10981 if (location == UNKNOWN_LOCATION)
10982 location = input_location;
10984 op0 = orig_op0;
10985 op1 = orig_op1;
10987 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10988 if (op0_int_operands)
10989 op0 = remove_c_maybe_const_expr (op0);
10990 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10991 if (op1_int_operands)
10992 op1 = remove_c_maybe_const_expr (op1);
10993 int_operands = (op0_int_operands && op1_int_operands);
10994 if (int_operands)
10996 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10997 && TREE_CODE (orig_op1) == INTEGER_CST);
10998 int_const = (int_const_or_overflow
10999 && !TREE_OVERFLOW (orig_op0)
11000 && !TREE_OVERFLOW (orig_op1));
11002 else
11003 int_const = int_const_or_overflow = false;
11005 /* Do not apply default conversion in mixed vector/scalar expression. */
11006 if (convert_p
11007 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11009 op0 = default_conversion (op0);
11010 op1 = default_conversion (op1);
11013 /* When Cilk Plus is enabled and there are array notations inside op0, then
11014 we check to see if there are builtin array notation functions. If
11015 so, then we take on the type of the array notation inside it. */
11016 if (flag_cilkplus && contains_array_notation_expr (op0))
11017 orig_type0 = type0 = find_correct_array_notation_type (op0);
11018 else
11019 orig_type0 = type0 = TREE_TYPE (op0);
11021 if (flag_cilkplus && contains_array_notation_expr (op1))
11022 orig_type1 = type1 = find_correct_array_notation_type (op1);
11023 else
11024 orig_type1 = type1 = TREE_TYPE (op1);
11026 /* The expression codes of the data types of the arguments tell us
11027 whether the arguments are integers, floating, pointers, etc. */
11028 code0 = TREE_CODE (type0);
11029 code1 = TREE_CODE (type1);
11031 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11032 STRIP_TYPE_NOPS (op0);
11033 STRIP_TYPE_NOPS (op1);
11035 /* If an error was already reported for one of the arguments,
11036 avoid reporting another error. */
11038 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11039 return error_mark_node;
11041 if (code0 == POINTER_TYPE
11042 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11043 return error_mark_node;
11045 if (code1 == POINTER_TYPE
11046 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11047 return error_mark_node;
11049 if ((invalid_op_diag
11050 = targetm.invalid_binary_op (code, type0, type1)))
11052 error_at (location, invalid_op_diag);
11053 return error_mark_node;
11056 switch (code)
11058 case PLUS_EXPR:
11059 case MINUS_EXPR:
11060 case MULT_EXPR:
11061 case TRUNC_DIV_EXPR:
11062 case CEIL_DIV_EXPR:
11063 case FLOOR_DIV_EXPR:
11064 case ROUND_DIV_EXPR:
11065 case EXACT_DIV_EXPR:
11066 may_need_excess_precision = true;
11067 break;
11068 default:
11069 may_need_excess_precision = false;
11070 break;
11072 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11074 op0 = TREE_OPERAND (op0, 0);
11075 type0 = TREE_TYPE (op0);
11077 else if (may_need_excess_precision
11078 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11080 type0 = eptype;
11081 op0 = convert (eptype, op0);
11083 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11085 op1 = TREE_OPERAND (op1, 0);
11086 type1 = TREE_TYPE (op1);
11088 else if (may_need_excess_precision
11089 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11091 type1 = eptype;
11092 op1 = convert (eptype, op1);
11095 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11097 /* In case when one of the operands of the binary operation is
11098 a vector and another is a scalar -- convert scalar to vector. */
11099 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11101 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11102 true);
11104 switch (convert_flag)
11106 case stv_error:
11107 return error_mark_node;
11108 case stv_firstarg:
11110 bool maybe_const = true;
11111 tree sc;
11112 sc = c_fully_fold (op0, false, &maybe_const);
11113 sc = save_expr (sc);
11114 sc = convert (TREE_TYPE (type1), sc);
11115 op0 = build_vector_from_val (type1, sc);
11116 if (!maybe_const)
11117 op0 = c_wrap_maybe_const (op0, true);
11118 orig_type0 = type0 = TREE_TYPE (op0);
11119 code0 = TREE_CODE (type0);
11120 converted = 1;
11121 break;
11123 case stv_secondarg:
11125 bool maybe_const = true;
11126 tree sc;
11127 sc = c_fully_fold (op1, false, &maybe_const);
11128 sc = save_expr (sc);
11129 sc = convert (TREE_TYPE (type0), sc);
11130 op1 = build_vector_from_val (type0, sc);
11131 if (!maybe_const)
11132 op1 = c_wrap_maybe_const (op1, true);
11133 orig_type1 = type1 = TREE_TYPE (op1);
11134 code1 = TREE_CODE (type1);
11135 converted = 1;
11136 break;
11138 default:
11139 break;
11143 switch (code)
11145 case PLUS_EXPR:
11146 /* Handle the pointer + int case. */
11147 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11149 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11150 goto return_build_binary_op;
11152 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11154 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11155 goto return_build_binary_op;
11157 else
11158 common = 1;
11159 break;
11161 case MINUS_EXPR:
11162 /* Subtraction of two similar pointers.
11163 We must subtract them as integers, then divide by object size. */
11164 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11165 && comp_target_types (location, type0, type1))
11167 ret = pointer_diff (location, op0, op1);
11168 goto return_build_binary_op;
11170 /* Handle pointer minus int. Just like pointer plus int. */
11171 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11173 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11174 goto return_build_binary_op;
11176 else
11177 common = 1;
11178 break;
11180 case MULT_EXPR:
11181 common = 1;
11182 break;
11184 case TRUNC_DIV_EXPR:
11185 case CEIL_DIV_EXPR:
11186 case FLOOR_DIV_EXPR:
11187 case ROUND_DIV_EXPR:
11188 case EXACT_DIV_EXPR:
11189 doing_div_or_mod = true;
11190 warn_for_div_by_zero (location, op1);
11192 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11193 || code0 == FIXED_POINT_TYPE
11194 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11195 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11196 || code1 == FIXED_POINT_TYPE
11197 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11199 enum tree_code tcode0 = code0, tcode1 = code1;
11201 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11202 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11203 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11204 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11206 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11207 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11208 resultcode = RDIV_EXPR;
11209 else
11210 /* Although it would be tempting to shorten always here, that
11211 loses on some targets, since the modulo instruction is
11212 undefined if the quotient can't be represented in the
11213 computation mode. We shorten only if unsigned or if
11214 dividing by something we know != -1. */
11215 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11216 || (TREE_CODE (op1) == INTEGER_CST
11217 && !integer_all_onesp (op1)));
11218 common = 1;
11220 break;
11222 case BIT_AND_EXPR:
11223 case BIT_IOR_EXPR:
11224 case BIT_XOR_EXPR:
11225 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11226 shorten = -1;
11227 /* Allow vector types which are not floating point types. */
11228 else if (code0 == VECTOR_TYPE
11229 && code1 == VECTOR_TYPE
11230 && !VECTOR_FLOAT_TYPE_P (type0)
11231 && !VECTOR_FLOAT_TYPE_P (type1))
11232 common = 1;
11233 break;
11235 case TRUNC_MOD_EXPR:
11236 case FLOOR_MOD_EXPR:
11237 doing_div_or_mod = true;
11238 warn_for_div_by_zero (location, op1);
11240 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11241 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11242 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11243 common = 1;
11244 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11246 /* Although it would be tempting to shorten always here, that loses
11247 on some targets, since the modulo instruction is undefined if the
11248 quotient can't be represented in the computation mode. We shorten
11249 only if unsigned or if dividing by something we know != -1. */
11250 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11251 || (TREE_CODE (op1) == INTEGER_CST
11252 && !integer_all_onesp (op1)));
11253 common = 1;
11255 break;
11257 case TRUTH_ANDIF_EXPR:
11258 case TRUTH_ORIF_EXPR:
11259 case TRUTH_AND_EXPR:
11260 case TRUTH_OR_EXPR:
11261 case TRUTH_XOR_EXPR:
11262 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11263 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11264 || code0 == FIXED_POINT_TYPE)
11265 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11266 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11267 || code1 == FIXED_POINT_TYPE))
11269 /* Result of these operations is always an int,
11270 but that does not mean the operands should be
11271 converted to ints! */
11272 result_type = integer_type_node;
11273 if (op0_int_operands)
11275 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11276 op0 = remove_c_maybe_const_expr (op0);
11278 else
11279 op0 = c_objc_common_truthvalue_conversion (location, op0);
11280 if (op1_int_operands)
11282 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11283 op1 = remove_c_maybe_const_expr (op1);
11285 else
11286 op1 = c_objc_common_truthvalue_conversion (location, op1);
11287 converted = 1;
11288 boolean_op = true;
11290 if (code == TRUTH_ANDIF_EXPR)
11292 int_const_or_overflow = (int_operands
11293 && TREE_CODE (orig_op0) == INTEGER_CST
11294 && (op0 == truthvalue_false_node
11295 || TREE_CODE (orig_op1) == INTEGER_CST));
11296 int_const = (int_const_or_overflow
11297 && !TREE_OVERFLOW (orig_op0)
11298 && (op0 == truthvalue_false_node
11299 || !TREE_OVERFLOW (orig_op1)));
11301 else if (code == TRUTH_ORIF_EXPR)
11303 int_const_or_overflow = (int_operands
11304 && TREE_CODE (orig_op0) == INTEGER_CST
11305 && (op0 == truthvalue_true_node
11306 || TREE_CODE (orig_op1) == INTEGER_CST));
11307 int_const = (int_const_or_overflow
11308 && !TREE_OVERFLOW (orig_op0)
11309 && (op0 == truthvalue_true_node
11310 || !TREE_OVERFLOW (orig_op1)));
11312 break;
11314 /* Shift operations: result has same type as first operand;
11315 always convert second operand to int.
11316 Also set SHORT_SHIFT if shifting rightward. */
11318 case RSHIFT_EXPR:
11319 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11320 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11321 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11322 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11324 result_type = type0;
11325 converted = 1;
11327 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11328 || code0 == VECTOR_TYPE)
11329 && code1 == INTEGER_TYPE)
11331 doing_shift = true;
11332 if (TREE_CODE (op1) == INTEGER_CST)
11334 if (tree_int_cst_sgn (op1) < 0)
11336 int_const = false;
11337 if (c_inhibit_evaluation_warnings == 0)
11338 warning_at (location, OPT_Wshift_count_negative,
11339 "right shift count is negative");
11341 else if (code0 == VECTOR_TYPE)
11343 if (compare_tree_int (op1,
11344 TYPE_PRECISION (TREE_TYPE (type0)))
11345 >= 0)
11347 int_const = false;
11348 if (c_inhibit_evaluation_warnings == 0)
11349 warning_at (location, OPT_Wshift_count_overflow,
11350 "right shift count >= width of vector element");
11353 else
11355 if (!integer_zerop (op1))
11356 short_shift = 1;
11358 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11360 int_const = false;
11361 if (c_inhibit_evaluation_warnings == 0)
11362 warning_at (location, OPT_Wshift_count_overflow,
11363 "right shift count >= width of type");
11368 /* Use the type of the value to be shifted. */
11369 result_type = type0;
11370 /* Avoid converting op1 to result_type later. */
11371 converted = 1;
11373 break;
11375 case LSHIFT_EXPR:
11376 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11377 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11378 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11379 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11381 result_type = type0;
11382 converted = 1;
11384 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11385 || code0 == VECTOR_TYPE)
11386 && code1 == INTEGER_TYPE)
11388 doing_shift = true;
11389 if (TREE_CODE (op0) == INTEGER_CST
11390 && tree_int_cst_sgn (op0) < 0)
11392 /* Don't reject a left shift of a negative value in a context
11393 where a constant expression is needed in C90. */
11394 if (flag_isoc99)
11395 int_const = false;
11396 if (c_inhibit_evaluation_warnings == 0)
11397 warning_at (location, OPT_Wshift_negative_value,
11398 "left shift of negative value");
11400 if (TREE_CODE (op1) == INTEGER_CST)
11402 if (tree_int_cst_sgn (op1) < 0)
11404 int_const = false;
11405 if (c_inhibit_evaluation_warnings == 0)
11406 warning_at (location, OPT_Wshift_count_negative,
11407 "left shift count is negative");
11409 else if (code0 == VECTOR_TYPE)
11411 if (compare_tree_int (op1,
11412 TYPE_PRECISION (TREE_TYPE (type0)))
11413 >= 0)
11415 int_const = false;
11416 if (c_inhibit_evaluation_warnings == 0)
11417 warning_at (location, OPT_Wshift_count_overflow,
11418 "left shift count >= width of vector element");
11421 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11423 int_const = false;
11424 if (c_inhibit_evaluation_warnings == 0)
11425 warning_at (location, OPT_Wshift_count_overflow,
11426 "left shift count >= width of type");
11428 else if (TREE_CODE (op0) == INTEGER_CST
11429 && maybe_warn_shift_overflow (location, op0, op1)
11430 && flag_isoc99)
11431 int_const = false;
11434 /* Use the type of the value to be shifted. */
11435 result_type = type0;
11436 /* Avoid converting op1 to result_type later. */
11437 converted = 1;
11439 break;
11441 case EQ_EXPR:
11442 case NE_EXPR:
11443 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11445 tree intt;
11446 if (!vector_types_compatible_elements_p (type0, type1))
11448 error_at (location, "comparing vectors with different "
11449 "element types");
11450 return error_mark_node;
11453 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11455 error_at (location, "comparing vectors with different "
11456 "number of elements");
11457 return error_mark_node;
11460 /* It's not precisely specified how the usual arithmetic
11461 conversions apply to the vector types. Here, we use
11462 the unsigned type if one of the operands is signed and
11463 the other one is unsigned. */
11464 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11466 if (!TYPE_UNSIGNED (type0))
11467 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11468 else
11469 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11470 warning_at (location, OPT_Wsign_compare, "comparison between "
11471 "types %qT and %qT", type0, type1);
11474 /* Always construct signed integer vector type. */
11475 intt = c_common_type_for_size (GET_MODE_BITSIZE
11476 (SCALAR_TYPE_MODE
11477 (TREE_TYPE (type0))), 0);
11478 result_type = build_opaque_vector_type (intt,
11479 TYPE_VECTOR_SUBPARTS (type0));
11480 converted = 1;
11481 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11482 goto return_build_binary_op;
11484 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11485 warning_at (location,
11486 OPT_Wfloat_equal,
11487 "comparing floating point with == or != is unsafe");
11488 /* Result of comparison is always int,
11489 but don't convert the args to int! */
11490 build_type = integer_type_node;
11491 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11492 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11493 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11494 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11495 short_compare = 1;
11496 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11498 if (TREE_CODE (op0) == ADDR_EXPR
11499 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11500 && !from_macro_expansion_at (location))
11502 if (code == EQ_EXPR)
11503 warning_at (location,
11504 OPT_Waddress,
11505 "the comparison will always evaluate as %<false%> "
11506 "for the address of %qD will never be NULL",
11507 TREE_OPERAND (op0, 0));
11508 else
11509 warning_at (location,
11510 OPT_Waddress,
11511 "the comparison will always evaluate as %<true%> "
11512 "for the address of %qD will never be NULL",
11513 TREE_OPERAND (op0, 0));
11515 result_type = type0;
11517 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11519 if (TREE_CODE (op1) == ADDR_EXPR
11520 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 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 (op1, 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 (op1, 0));
11536 result_type = type1;
11538 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11540 tree tt0 = TREE_TYPE (type0);
11541 tree tt1 = TREE_TYPE (type1);
11542 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11543 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11544 addr_space_t as_common = ADDR_SPACE_GENERIC;
11546 /* Anything compares with void *. void * compares with anything.
11547 Otherwise, the targets must be compatible
11548 and both must be object or both incomplete. */
11549 if (comp_target_types (location, type0, type1))
11550 result_type = common_pointer_type (type0, type1);
11551 else if (!addr_space_superset (as0, as1, &as_common))
11553 error_at (location, "comparison of pointers to "
11554 "disjoint address spaces");
11555 return error_mark_node;
11557 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11559 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11560 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11561 "comparison of %<void *%> with function pointer");
11563 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11565 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11566 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11567 "comparison of %<void *%> with function pointer");
11569 else
11570 /* Avoid warning about the volatile ObjC EH puts on decls. */
11571 if (!objc_ok)
11572 pedwarn (location, 0,
11573 "comparison of distinct pointer types lacks a cast");
11575 if (result_type == NULL_TREE)
11577 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11578 result_type = build_pointer_type
11579 (build_qualified_type (void_type_node, qual));
11582 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11584 result_type = type0;
11585 pedwarn (location, 0, "comparison between pointer and integer");
11587 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11589 result_type = type1;
11590 pedwarn (location, 0, "comparison between pointer and integer");
11592 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11593 || truth_value_p (TREE_CODE (orig_op0)))
11594 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11595 || truth_value_p (TREE_CODE (orig_op1))))
11596 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11597 break;
11599 case LE_EXPR:
11600 case GE_EXPR:
11601 case LT_EXPR:
11602 case GT_EXPR:
11603 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11605 tree intt;
11606 if (!vector_types_compatible_elements_p (type0, type1))
11608 error_at (location, "comparing vectors with different "
11609 "element types");
11610 return error_mark_node;
11613 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11615 error_at (location, "comparing vectors with different "
11616 "number of elements");
11617 return error_mark_node;
11620 /* It's not precisely specified how the usual arithmetic
11621 conversions apply to the vector types. Here, we use
11622 the unsigned type if one of the operands is signed and
11623 the other one is unsigned. */
11624 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11626 if (!TYPE_UNSIGNED (type0))
11627 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11628 else
11629 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11630 warning_at (location, OPT_Wsign_compare, "comparison between "
11631 "types %qT and %qT", type0, type1);
11634 /* Always construct signed integer vector type. */
11635 intt = c_common_type_for_size (GET_MODE_BITSIZE
11636 (SCALAR_TYPE_MODE
11637 (TREE_TYPE (type0))), 0);
11638 result_type = build_opaque_vector_type (intt,
11639 TYPE_VECTOR_SUBPARTS (type0));
11640 converted = 1;
11641 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11642 goto return_build_binary_op;
11644 build_type = integer_type_node;
11645 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11646 || code0 == FIXED_POINT_TYPE)
11647 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11648 || code1 == FIXED_POINT_TYPE))
11649 short_compare = 1;
11650 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11652 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11653 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11654 addr_space_t as_common;
11656 if (comp_target_types (location, type0, type1))
11658 result_type = common_pointer_type (type0, type1);
11659 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11660 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11661 pedwarn (location, 0,
11662 "comparison of complete and incomplete pointers");
11663 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11664 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11665 "ordered comparisons of pointers to functions");
11666 else if (null_pointer_constant_p (orig_op0)
11667 || null_pointer_constant_p (orig_op1))
11668 warning_at (location, OPT_Wextra,
11669 "ordered comparison of pointer with null pointer");
11672 else if (!addr_space_superset (as0, as1, &as_common))
11674 error_at (location, "comparison of pointers to "
11675 "disjoint address spaces");
11676 return error_mark_node;
11678 else
11680 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11681 result_type = build_pointer_type
11682 (build_qualified_type (void_type_node, qual));
11683 pedwarn (location, 0,
11684 "comparison of distinct pointer types lacks a cast");
11687 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11689 result_type = type0;
11690 if (pedantic)
11691 pedwarn (location, OPT_Wpedantic,
11692 "ordered comparison of pointer with integer zero");
11693 else if (extra_warnings)
11694 warning_at (location, OPT_Wextra,
11695 "ordered comparison of pointer with integer zero");
11697 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11699 result_type = type1;
11700 if (pedantic)
11701 pedwarn (location, OPT_Wpedantic,
11702 "ordered comparison of pointer with integer zero");
11703 else if (extra_warnings)
11704 warning_at (location, OPT_Wextra,
11705 "ordered comparison of pointer with integer zero");
11707 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11709 result_type = type0;
11710 pedwarn (location, 0, "comparison between pointer and integer");
11712 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11714 result_type = type1;
11715 pedwarn (location, 0, "comparison between pointer and integer");
11717 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11718 || truth_value_p (TREE_CODE (orig_op0)))
11719 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11720 || truth_value_p (TREE_CODE (orig_op1))))
11721 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11722 break;
11724 default:
11725 gcc_unreachable ();
11728 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11729 return error_mark_node;
11731 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11732 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11733 || !vector_types_compatible_elements_p (type0, type1)))
11735 gcc_rich_location richloc (location);
11736 richloc.maybe_add_expr (orig_op0);
11737 richloc.maybe_add_expr (orig_op1);
11738 binary_op_error (&richloc, code, type0, type1);
11739 return error_mark_node;
11742 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11743 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11745 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11746 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11748 bool first_complex = (code0 == COMPLEX_TYPE);
11749 bool second_complex = (code1 == COMPLEX_TYPE);
11750 int none_complex = (!first_complex && !second_complex);
11752 if (shorten || common || short_compare)
11754 result_type = c_common_type (type0, type1);
11755 do_warn_double_promotion (result_type, type0, type1,
11756 "implicit conversion from %qT to %qT "
11757 "to match other operand of binary "
11758 "expression",
11759 location);
11760 if (result_type == error_mark_node)
11761 return error_mark_node;
11764 if (first_complex != second_complex
11765 && (code == PLUS_EXPR
11766 || code == MINUS_EXPR
11767 || code == MULT_EXPR
11768 || (code == TRUNC_DIV_EXPR && first_complex))
11769 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11770 && flag_signed_zeros)
11772 /* An operation on mixed real/complex operands must be
11773 handled specially, but the language-independent code can
11774 more easily optimize the plain complex arithmetic if
11775 -fno-signed-zeros. */
11776 tree real_type = TREE_TYPE (result_type);
11777 tree real, imag;
11778 if (type0 != orig_type0 || type1 != orig_type1)
11780 gcc_assert (may_need_excess_precision && common);
11781 semantic_result_type = c_common_type (orig_type0, orig_type1);
11783 if (first_complex)
11785 if (TREE_TYPE (op0) != result_type)
11786 op0 = convert_and_check (location, result_type, op0);
11787 if (TREE_TYPE (op1) != real_type)
11788 op1 = convert_and_check (location, real_type, op1);
11790 else
11792 if (TREE_TYPE (op0) != real_type)
11793 op0 = convert_and_check (location, real_type, op0);
11794 if (TREE_TYPE (op1) != result_type)
11795 op1 = convert_and_check (location, result_type, op1);
11797 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11798 return error_mark_node;
11799 if (first_complex)
11801 op0 = save_expr (op0);
11802 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11803 op0, true);
11804 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11805 op0, true);
11806 switch (code)
11808 case MULT_EXPR:
11809 case TRUNC_DIV_EXPR:
11810 op1 = save_expr (op1);
11811 imag = build2 (resultcode, real_type, imag, op1);
11812 /* Fall through. */
11813 case PLUS_EXPR:
11814 case MINUS_EXPR:
11815 real = build2 (resultcode, real_type, real, op1);
11816 break;
11817 default:
11818 gcc_unreachable();
11821 else
11823 op1 = save_expr (op1);
11824 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11825 op1, true);
11826 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11827 op1, true);
11828 switch (code)
11830 case MULT_EXPR:
11831 op0 = save_expr (op0);
11832 imag = build2 (resultcode, real_type, op0, imag);
11833 /* Fall through. */
11834 case PLUS_EXPR:
11835 real = build2 (resultcode, real_type, op0, real);
11836 break;
11837 case MINUS_EXPR:
11838 real = build2 (resultcode, real_type, op0, real);
11839 imag = build1 (NEGATE_EXPR, real_type, imag);
11840 break;
11841 default:
11842 gcc_unreachable();
11845 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11846 goto return_build_binary_op;
11849 /* For certain operations (which identify themselves by shorten != 0)
11850 if both args were extended from the same smaller type,
11851 do the arithmetic in that type and then extend.
11853 shorten !=0 and !=1 indicates a bitwise operation.
11854 For them, this optimization is safe only if
11855 both args are zero-extended or both are sign-extended.
11856 Otherwise, we might change the result.
11857 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11858 but calculated in (unsigned short) it would be (unsigned short)-1. */
11860 if (shorten && none_complex)
11862 final_type = result_type;
11863 result_type = shorten_binary_op (result_type, op0, op1,
11864 shorten == -1);
11867 /* Shifts can be shortened if shifting right. */
11869 if (short_shift)
11871 int unsigned_arg;
11872 tree arg0 = get_narrower (op0, &unsigned_arg);
11874 final_type = result_type;
11876 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11877 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11879 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11880 && tree_int_cst_sgn (op1) > 0
11881 /* We can shorten only if the shift count is less than the
11882 number of bits in the smaller type size. */
11883 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11884 /* We cannot drop an unsigned shift after sign-extension. */
11885 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11887 /* Do an unsigned shift if the operand was zero-extended. */
11888 result_type
11889 = c_common_signed_or_unsigned_type (unsigned_arg,
11890 TREE_TYPE (arg0));
11891 /* Convert value-to-be-shifted to that type. */
11892 if (TREE_TYPE (op0) != result_type)
11893 op0 = convert (result_type, op0);
11894 converted = 1;
11898 /* Comparison operations are shortened too but differently.
11899 They identify themselves by setting short_compare = 1. */
11901 if (short_compare)
11903 /* Don't write &op0, etc., because that would prevent op0
11904 from being kept in a register.
11905 Instead, make copies of the our local variables and
11906 pass the copies by reference, then copy them back afterward. */
11907 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11908 enum tree_code xresultcode = resultcode;
11909 tree val
11910 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11911 &xresultcode);
11913 if (val != NULL_TREE)
11915 ret = val;
11916 goto return_build_binary_op;
11919 op0 = xop0, op1 = xop1;
11920 converted = 1;
11921 resultcode = xresultcode;
11923 if (c_inhibit_evaluation_warnings == 0)
11925 bool op0_maybe_const = true;
11926 bool op1_maybe_const = true;
11927 tree orig_op0_folded, orig_op1_folded;
11929 if (in_late_binary_op)
11931 orig_op0_folded = orig_op0;
11932 orig_op1_folded = orig_op1;
11934 else
11936 /* Fold for the sake of possible warnings, as in
11937 build_conditional_expr. This requires the
11938 "original" values to be folded, not just op0 and
11939 op1. */
11940 c_inhibit_evaluation_warnings++;
11941 op0 = c_fully_fold (op0, require_constant_value,
11942 &op0_maybe_const);
11943 op1 = c_fully_fold (op1, require_constant_value,
11944 &op1_maybe_const);
11945 c_inhibit_evaluation_warnings--;
11946 orig_op0_folded = c_fully_fold (orig_op0,
11947 require_constant_value,
11948 NULL);
11949 orig_op1_folded = c_fully_fold (orig_op1,
11950 require_constant_value,
11951 NULL);
11954 if (warn_sign_compare)
11955 warn_for_sign_compare (location, orig_op0_folded,
11956 orig_op1_folded, op0, op1,
11957 result_type, resultcode);
11958 if (!in_late_binary_op && !int_operands)
11960 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11961 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11962 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11963 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11969 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11970 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11971 Then the expression will be built.
11972 It will be given type FINAL_TYPE if that is nonzero;
11973 otherwise, it will be given type RESULT_TYPE. */
11975 if (!result_type)
11977 gcc_rich_location richloc (location);
11978 richloc.maybe_add_expr (orig_op0);
11979 richloc.maybe_add_expr (orig_op1);
11980 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11981 return error_mark_node;
11984 if (build_type == NULL_TREE)
11986 build_type = result_type;
11987 if ((type0 != orig_type0 || type1 != orig_type1)
11988 && !boolean_op)
11990 gcc_assert (may_need_excess_precision && common);
11991 semantic_result_type = c_common_type (orig_type0, orig_type1);
11995 if (!converted)
11997 op0 = ep_convert_and_check (location, result_type, op0,
11998 semantic_result_type);
11999 op1 = ep_convert_and_check (location, result_type, op1,
12000 semantic_result_type);
12002 /* This can happen if one operand has a vector type, and the other
12003 has a different type. */
12004 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12005 return error_mark_node;
12008 if (sanitize_flags_p ((SANITIZE_SHIFT
12009 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12010 && current_function_decl != NULL_TREE
12011 && (doing_div_or_mod || doing_shift)
12012 && !require_constant_value)
12014 /* OP0 and/or OP1 might have side-effects. */
12015 op0 = save_expr (op0);
12016 op1 = save_expr (op1);
12017 op0 = c_fully_fold (op0, false, NULL);
12018 op1 = c_fully_fold (op1, false, NULL);
12019 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12020 | SANITIZE_FLOAT_DIVIDE))))
12021 instrument_expr = ubsan_instrument_division (location, op0, op1);
12022 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12023 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12026 /* Treat expressions in initializers specially as they can't trap. */
12027 if (int_const_or_overflow)
12028 ret = (require_constant_value
12029 ? fold_build2_initializer_loc (location, resultcode, build_type,
12030 op0, op1)
12031 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12032 else
12033 ret = build2 (resultcode, build_type, op0, op1);
12034 if (final_type != NULL_TREE)
12035 ret = convert (final_type, ret);
12037 return_build_binary_op:
12038 gcc_assert (ret != error_mark_node);
12039 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12040 ret = (int_operands
12041 ? note_integer_operands (ret)
12042 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12043 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12044 && !in_late_binary_op)
12045 ret = note_integer_operands (ret);
12046 protected_set_expr_location (ret, location);
12048 if (instrument_expr != NULL)
12049 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12050 instrument_expr, ret);
12052 if (semantic_result_type)
12053 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12054 semantic_result_type, ret);
12056 return ret;
12060 /* Convert EXPR to be a truth-value, validating its type for this
12061 purpose. LOCATION is the source location for the expression. */
12063 tree
12064 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12066 bool int_const, int_operands;
12068 switch (TREE_CODE (TREE_TYPE (expr)))
12070 case ARRAY_TYPE:
12071 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12072 return error_mark_node;
12074 case RECORD_TYPE:
12075 error_at (location, "used struct type value where scalar is required");
12076 return error_mark_node;
12078 case UNION_TYPE:
12079 error_at (location, "used union type value where scalar is required");
12080 return error_mark_node;
12082 case VOID_TYPE:
12083 error_at (location, "void value not ignored as it ought to be");
12084 return error_mark_node;
12086 case POINTER_TYPE:
12087 if (reject_gcc_builtin (expr))
12088 return error_mark_node;
12089 break;
12091 case FUNCTION_TYPE:
12092 gcc_unreachable ();
12094 case VECTOR_TYPE:
12095 error_at (location, "used vector type where scalar is required");
12096 return error_mark_node;
12098 default:
12099 break;
12102 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12103 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12104 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12106 expr = remove_c_maybe_const_expr (expr);
12107 expr = build2 (NE_EXPR, integer_type_node, expr,
12108 convert (TREE_TYPE (expr), integer_zero_node));
12109 expr = note_integer_operands (expr);
12111 else
12112 /* ??? Should we also give an error for vectors rather than leaving
12113 those to give errors later? */
12114 expr = c_common_truthvalue_conversion (location, expr);
12116 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12118 if (TREE_OVERFLOW (expr))
12119 return expr;
12120 else
12121 return note_integer_operands (expr);
12123 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12124 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12125 return expr;
12129 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12130 required. */
12132 tree
12133 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12135 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12137 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12138 /* Executing a compound literal inside a function reinitializes
12139 it. */
12140 if (!TREE_STATIC (decl))
12141 *se = true;
12142 return decl;
12144 else
12145 return expr;
12148 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12149 statement. LOC is the location of the construct. */
12151 tree
12152 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12153 tree clauses)
12155 body = c_end_compound_stmt (loc, body, true);
12157 tree stmt = make_node (code);
12158 TREE_TYPE (stmt) = void_type_node;
12159 OMP_BODY (stmt) = body;
12160 OMP_CLAUSES (stmt) = clauses;
12161 SET_EXPR_LOCATION (stmt, loc);
12163 return add_stmt (stmt);
12166 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12167 statement. LOC is the location of the OACC_DATA. */
12169 tree
12170 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12172 tree stmt;
12174 block = c_end_compound_stmt (loc, block, true);
12176 stmt = make_node (OACC_DATA);
12177 TREE_TYPE (stmt) = void_type_node;
12178 OACC_DATA_CLAUSES (stmt) = clauses;
12179 OACC_DATA_BODY (stmt) = block;
12180 SET_EXPR_LOCATION (stmt, loc);
12182 return add_stmt (stmt);
12185 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12186 statement. LOC is the location of the OACC_HOST_DATA. */
12188 tree
12189 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12191 tree stmt;
12193 block = c_end_compound_stmt (loc, block, true);
12195 stmt = make_node (OACC_HOST_DATA);
12196 TREE_TYPE (stmt) = void_type_node;
12197 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12198 OACC_HOST_DATA_BODY (stmt) = block;
12199 SET_EXPR_LOCATION (stmt, loc);
12201 return add_stmt (stmt);
12204 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12206 tree
12207 c_begin_omp_parallel (void)
12209 tree block;
12211 keep_next_level ();
12212 block = c_begin_compound_stmt (true);
12214 return block;
12217 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12218 statement. LOC is the location of the OMP_PARALLEL. */
12220 tree
12221 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12223 tree stmt;
12225 block = c_end_compound_stmt (loc, block, true);
12227 stmt = make_node (OMP_PARALLEL);
12228 TREE_TYPE (stmt) = void_type_node;
12229 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12230 OMP_PARALLEL_BODY (stmt) = block;
12231 SET_EXPR_LOCATION (stmt, loc);
12233 return add_stmt (stmt);
12236 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12238 tree
12239 c_begin_omp_task (void)
12241 tree block;
12243 keep_next_level ();
12244 block = c_begin_compound_stmt (true);
12246 return block;
12249 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12250 statement. LOC is the location of the #pragma. */
12252 tree
12253 c_finish_omp_task (location_t loc, tree clauses, tree block)
12255 tree stmt;
12257 block = c_end_compound_stmt (loc, block, true);
12259 stmt = make_node (OMP_TASK);
12260 TREE_TYPE (stmt) = void_type_node;
12261 OMP_TASK_CLAUSES (stmt) = clauses;
12262 OMP_TASK_BODY (stmt) = block;
12263 SET_EXPR_LOCATION (stmt, loc);
12265 return add_stmt (stmt);
12268 /* Generate GOMP_cancel call for #pragma omp cancel. */
12270 void
12271 c_finish_omp_cancel (location_t loc, tree clauses)
12273 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12274 int mask = 0;
12275 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12276 mask = 1;
12277 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12278 mask = 2;
12279 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12280 mask = 4;
12281 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12282 mask = 8;
12283 else
12285 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12286 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12287 "clauses");
12288 return;
12290 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12291 if (ifc != NULL_TREE)
12293 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12294 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12295 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12296 build_zero_cst (type));
12298 else
12299 ifc = boolean_true_node;
12300 tree stmt = build_call_expr_loc (loc, fn, 2,
12301 build_int_cst (integer_type_node, mask),
12302 ifc);
12303 add_stmt (stmt);
12306 /* Generate GOMP_cancellation_point call for
12307 #pragma omp cancellation point. */
12309 void
12310 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12312 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12313 int mask = 0;
12314 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12315 mask = 1;
12316 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12317 mask = 2;
12318 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12319 mask = 4;
12320 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12321 mask = 8;
12322 else
12324 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12325 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12326 "clauses");
12327 return;
12329 tree stmt = build_call_expr_loc (loc, fn, 1,
12330 build_int_cst (integer_type_node, mask));
12331 add_stmt (stmt);
12334 /* Helper function for handle_omp_array_sections. Called recursively
12335 to handle multiple array-section-subscripts. C is the clause,
12336 T current expression (initially OMP_CLAUSE_DECL), which is either
12337 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12338 expression if specified, TREE_VALUE length expression if specified,
12339 TREE_CHAIN is what it has been specified after, or some decl.
12340 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12341 set to true if any of the array-section-subscript could have length
12342 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12343 first array-section-subscript which is known not to have length
12344 of one. Given say:
12345 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12346 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12347 all are or may have length of 1, array-section-subscript [:2] is the
12348 first one known not to have length 1. For array-section-subscript
12349 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12350 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12351 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12352 case though, as some lengths could be zero. */
12354 static tree
12355 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12356 bool &maybe_zero_len, unsigned int &first_non_one,
12357 enum c_omp_region_type ort)
12359 tree ret, low_bound, length, type;
12360 if (TREE_CODE (t) != TREE_LIST)
12362 if (error_operand_p (t))
12363 return error_mark_node;
12364 ret = t;
12365 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12366 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12368 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12369 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12370 return error_mark_node;
12372 if (TREE_CODE (t) == COMPONENT_REF
12373 && ort == C_ORT_OMP
12374 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12375 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12376 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12378 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12380 error_at (OMP_CLAUSE_LOCATION (c),
12381 "bit-field %qE in %qs clause",
12382 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12383 return error_mark_node;
12385 while (TREE_CODE (t) == COMPONENT_REF)
12387 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12389 error_at (OMP_CLAUSE_LOCATION (c),
12390 "%qE is a member of a union", t);
12391 return error_mark_node;
12393 t = TREE_OPERAND (t, 0);
12396 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12398 if (DECL_P (t))
12399 error_at (OMP_CLAUSE_LOCATION (c),
12400 "%qD is not a variable in %qs clause", t,
12401 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12402 else
12403 error_at (OMP_CLAUSE_LOCATION (c),
12404 "%qE is not a variable in %qs clause", t,
12405 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12406 return error_mark_node;
12408 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12409 && TYPE_ATOMIC (TREE_TYPE (t)))
12411 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12412 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12413 return error_mark_node;
12415 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12416 && VAR_P (t)
12417 && DECL_THREAD_LOCAL_P (t))
12419 error_at (OMP_CLAUSE_LOCATION (c),
12420 "%qD is threadprivate variable in %qs clause", t,
12421 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12422 return error_mark_node;
12424 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12425 && TYPE_ATOMIC (TREE_TYPE (t))
12426 && POINTER_TYPE_P (TREE_TYPE (t)))
12428 /* If the array section is pointer based and the pointer
12429 itself is _Atomic qualified, we need to atomically load
12430 the pointer. */
12431 c_expr expr;
12432 memset (&expr, 0, sizeof (expr));
12433 expr.value = ret;
12434 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12435 expr, false, false);
12436 ret = expr.value;
12438 return ret;
12441 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12442 maybe_zero_len, first_non_one, ort);
12443 if (ret == error_mark_node || ret == NULL_TREE)
12444 return ret;
12446 type = TREE_TYPE (ret);
12447 low_bound = TREE_PURPOSE (t);
12448 length = TREE_VALUE (t);
12450 if (low_bound == error_mark_node || length == error_mark_node)
12451 return error_mark_node;
12453 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12455 error_at (OMP_CLAUSE_LOCATION (c),
12456 "low bound %qE of array section does not have integral type",
12457 low_bound);
12458 return error_mark_node;
12460 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12462 error_at (OMP_CLAUSE_LOCATION (c),
12463 "length %qE of array section does not have integral type",
12464 length);
12465 return error_mark_node;
12467 if (low_bound
12468 && TREE_CODE (low_bound) == INTEGER_CST
12469 && TYPE_PRECISION (TREE_TYPE (low_bound))
12470 > TYPE_PRECISION (sizetype))
12471 low_bound = fold_convert (sizetype, low_bound);
12472 if (length
12473 && TREE_CODE (length) == INTEGER_CST
12474 && TYPE_PRECISION (TREE_TYPE (length))
12475 > TYPE_PRECISION (sizetype))
12476 length = fold_convert (sizetype, length);
12477 if (low_bound == NULL_TREE)
12478 low_bound = integer_zero_node;
12480 if (length != NULL_TREE)
12482 if (!integer_nonzerop (length))
12484 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12485 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12487 if (integer_zerop (length))
12489 error_at (OMP_CLAUSE_LOCATION (c),
12490 "zero length array section in %qs clause",
12491 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12492 return error_mark_node;
12495 else
12496 maybe_zero_len = true;
12498 if (first_non_one == types.length ()
12499 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12500 first_non_one++;
12502 if (TREE_CODE (type) == ARRAY_TYPE)
12504 if (length == NULL_TREE
12505 && (TYPE_DOMAIN (type) == NULL_TREE
12506 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12508 error_at (OMP_CLAUSE_LOCATION (c),
12509 "for unknown bound array type length expression must "
12510 "be specified");
12511 return error_mark_node;
12513 if (TREE_CODE (low_bound) == INTEGER_CST
12514 && tree_int_cst_sgn (low_bound) == -1)
12516 error_at (OMP_CLAUSE_LOCATION (c),
12517 "negative low bound in array section in %qs clause",
12518 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12519 return error_mark_node;
12521 if (length != NULL_TREE
12522 && TREE_CODE (length) == INTEGER_CST
12523 && tree_int_cst_sgn (length) == -1)
12525 error_at (OMP_CLAUSE_LOCATION (c),
12526 "negative length in array section in %qs clause",
12527 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12528 return error_mark_node;
12530 if (TYPE_DOMAIN (type)
12531 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12532 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12533 == INTEGER_CST)
12535 tree size
12536 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12537 size = size_binop (PLUS_EXPR, size, size_one_node);
12538 if (TREE_CODE (low_bound) == INTEGER_CST)
12540 if (tree_int_cst_lt (size, low_bound))
12542 error_at (OMP_CLAUSE_LOCATION (c),
12543 "low bound %qE above array section size "
12544 "in %qs clause", low_bound,
12545 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12546 return error_mark_node;
12548 if (tree_int_cst_equal (size, low_bound))
12550 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12551 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12553 error_at (OMP_CLAUSE_LOCATION (c),
12554 "zero length array section in %qs clause",
12555 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12556 return error_mark_node;
12558 maybe_zero_len = true;
12560 else if (length == NULL_TREE
12561 && first_non_one == types.length ()
12562 && tree_int_cst_equal
12563 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12564 low_bound))
12565 first_non_one++;
12567 else if (length == NULL_TREE)
12569 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12570 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12571 maybe_zero_len = true;
12572 if (first_non_one == types.length ())
12573 first_non_one++;
12575 if (length && TREE_CODE (length) == INTEGER_CST)
12577 if (tree_int_cst_lt (size, length))
12579 error_at (OMP_CLAUSE_LOCATION (c),
12580 "length %qE above array section size "
12581 "in %qs clause", length,
12582 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12583 return error_mark_node;
12585 if (TREE_CODE (low_bound) == INTEGER_CST)
12587 tree lbpluslen
12588 = size_binop (PLUS_EXPR,
12589 fold_convert (sizetype, low_bound),
12590 fold_convert (sizetype, length));
12591 if (TREE_CODE (lbpluslen) == INTEGER_CST
12592 && tree_int_cst_lt (size, lbpluslen))
12594 error_at (OMP_CLAUSE_LOCATION (c),
12595 "high bound %qE above array section size "
12596 "in %qs clause", lbpluslen,
12597 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12598 return error_mark_node;
12603 else if (length == NULL_TREE)
12605 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12606 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12607 maybe_zero_len = true;
12608 if (first_non_one == types.length ())
12609 first_non_one++;
12612 /* For [lb:] we will need to evaluate lb more than once. */
12613 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12615 tree lb = save_expr (low_bound);
12616 if (lb != low_bound)
12618 TREE_PURPOSE (t) = lb;
12619 low_bound = lb;
12623 else if (TREE_CODE (type) == POINTER_TYPE)
12625 if (length == NULL_TREE)
12627 error_at (OMP_CLAUSE_LOCATION (c),
12628 "for pointer type length expression must be specified");
12629 return error_mark_node;
12631 if (length != NULL_TREE
12632 && TREE_CODE (length) == INTEGER_CST
12633 && tree_int_cst_sgn (length) == -1)
12635 error_at (OMP_CLAUSE_LOCATION (c),
12636 "negative length in array section in %qs clause",
12637 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12638 return error_mark_node;
12640 /* If there is a pointer type anywhere but in the very first
12641 array-section-subscript, the array section can't be contiguous. */
12642 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12643 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12645 error_at (OMP_CLAUSE_LOCATION (c),
12646 "array section is not contiguous in %qs clause",
12647 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12648 return error_mark_node;
12651 else
12653 error_at (OMP_CLAUSE_LOCATION (c),
12654 "%qE does not have pointer or array type", ret);
12655 return error_mark_node;
12657 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12658 types.safe_push (TREE_TYPE (ret));
12659 /* We will need to evaluate lb more than once. */
12660 tree lb = save_expr (low_bound);
12661 if (lb != low_bound)
12663 TREE_PURPOSE (t) = lb;
12664 low_bound = lb;
12666 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12667 return ret;
12670 /* Handle array sections for clause C. */
12672 static bool
12673 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12675 bool maybe_zero_len = false;
12676 unsigned int first_non_one = 0;
12677 auto_vec<tree, 10> types;
12678 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12679 maybe_zero_len, first_non_one,
12680 ort);
12681 if (first == error_mark_node)
12682 return true;
12683 if (first == NULL_TREE)
12684 return false;
12685 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12687 tree t = OMP_CLAUSE_DECL (c);
12688 tree tem = NULL_TREE;
12689 /* Need to evaluate side effects in the length expressions
12690 if any. */
12691 while (TREE_CODE (t) == TREE_LIST)
12693 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12695 if (tem == NULL_TREE)
12696 tem = TREE_VALUE (t);
12697 else
12698 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12699 TREE_VALUE (t), tem);
12701 t = TREE_CHAIN (t);
12703 if (tem)
12704 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12705 first = c_fully_fold (first, false, NULL);
12706 OMP_CLAUSE_DECL (c) = first;
12708 else
12710 unsigned int num = types.length (), i;
12711 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12712 tree condition = NULL_TREE;
12714 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12715 maybe_zero_len = true;
12717 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12718 t = TREE_CHAIN (t))
12720 tree low_bound = TREE_PURPOSE (t);
12721 tree length = TREE_VALUE (t);
12723 i--;
12724 if (low_bound
12725 && TREE_CODE (low_bound) == INTEGER_CST
12726 && TYPE_PRECISION (TREE_TYPE (low_bound))
12727 > TYPE_PRECISION (sizetype))
12728 low_bound = fold_convert (sizetype, low_bound);
12729 if (length
12730 && TREE_CODE (length) == INTEGER_CST
12731 && TYPE_PRECISION (TREE_TYPE (length))
12732 > TYPE_PRECISION (sizetype))
12733 length = fold_convert (sizetype, length);
12734 if (low_bound == NULL_TREE)
12735 low_bound = integer_zero_node;
12736 if (!maybe_zero_len && i > first_non_one)
12738 if (integer_nonzerop (low_bound))
12739 goto do_warn_noncontiguous;
12740 if (length != NULL_TREE
12741 && TREE_CODE (length) == INTEGER_CST
12742 && TYPE_DOMAIN (types[i])
12743 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12744 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12745 == INTEGER_CST)
12747 tree size;
12748 size = size_binop (PLUS_EXPR,
12749 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12750 size_one_node);
12751 if (!tree_int_cst_equal (length, size))
12753 do_warn_noncontiguous:
12754 error_at (OMP_CLAUSE_LOCATION (c),
12755 "array section is not contiguous in %qs "
12756 "clause",
12757 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12758 return true;
12761 if (length != NULL_TREE
12762 && TREE_SIDE_EFFECTS (length))
12764 if (side_effects == NULL_TREE)
12765 side_effects = length;
12766 else
12767 side_effects = build2 (COMPOUND_EXPR,
12768 TREE_TYPE (side_effects),
12769 length, side_effects);
12772 else
12774 tree l;
12776 if (i > first_non_one
12777 && ((length && integer_nonzerop (length))
12778 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12779 continue;
12780 if (length)
12781 l = fold_convert (sizetype, length);
12782 else
12784 l = size_binop (PLUS_EXPR,
12785 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12786 size_one_node);
12787 l = size_binop (MINUS_EXPR, l,
12788 fold_convert (sizetype, low_bound));
12790 if (i > first_non_one)
12792 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12793 size_zero_node);
12794 if (condition == NULL_TREE)
12795 condition = l;
12796 else
12797 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12798 l, condition);
12800 else if (size == NULL_TREE)
12802 size = size_in_bytes (TREE_TYPE (types[i]));
12803 tree eltype = TREE_TYPE (types[num - 1]);
12804 while (TREE_CODE (eltype) == ARRAY_TYPE)
12805 eltype = TREE_TYPE (eltype);
12806 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12808 if (integer_zerop (size)
12809 || integer_zerop (size_in_bytes (eltype)))
12811 error_at (OMP_CLAUSE_LOCATION (c),
12812 "zero length array section in %qs clause",
12813 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12814 return error_mark_node;
12816 size = size_binop (EXACT_DIV_EXPR, size,
12817 size_in_bytes (eltype));
12819 size = size_binop (MULT_EXPR, size, l);
12820 if (condition)
12821 size = fold_build3 (COND_EXPR, sizetype, condition,
12822 size, size_zero_node);
12824 else
12825 size = size_binop (MULT_EXPR, size, l);
12828 if (side_effects)
12829 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12830 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12832 size = size_binop (MINUS_EXPR, size, size_one_node);
12833 size = c_fully_fold (size, false, NULL);
12834 tree index_type = build_index_type (size);
12835 tree eltype = TREE_TYPE (first);
12836 while (TREE_CODE (eltype) == ARRAY_TYPE)
12837 eltype = TREE_TYPE (eltype);
12838 tree type = build_array_type (eltype, index_type);
12839 tree ptype = build_pointer_type (eltype);
12840 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12841 t = build_fold_addr_expr (t);
12842 tree t2 = build_fold_addr_expr (first);
12843 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12844 ptrdiff_type_node, t2);
12845 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12846 ptrdiff_type_node, t2,
12847 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12848 ptrdiff_type_node, t));
12849 t2 = c_fully_fold (t2, false, NULL);
12850 if (tree_fits_shwi_p (t2))
12851 t = build2 (MEM_REF, type, t,
12852 build_int_cst (ptype, tree_to_shwi (t2)));
12853 else
12855 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12856 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12857 TREE_TYPE (t), t, t2);
12858 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12860 OMP_CLAUSE_DECL (c) = t;
12861 return false;
12863 first = c_fully_fold (first, false, NULL);
12864 OMP_CLAUSE_DECL (c) = first;
12865 if (size)
12866 size = c_fully_fold (size, false, NULL);
12867 OMP_CLAUSE_SIZE (c) = size;
12868 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12869 || (TREE_CODE (t) == COMPONENT_REF
12870 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12871 return false;
12872 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12873 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12874 switch (OMP_CLAUSE_MAP_KIND (c))
12876 case GOMP_MAP_ALLOC:
12877 case GOMP_MAP_TO:
12878 case GOMP_MAP_FROM:
12879 case GOMP_MAP_TOFROM:
12880 case GOMP_MAP_ALWAYS_TO:
12881 case GOMP_MAP_ALWAYS_FROM:
12882 case GOMP_MAP_ALWAYS_TOFROM:
12883 case GOMP_MAP_RELEASE:
12884 case GOMP_MAP_DELETE:
12885 case GOMP_MAP_FORCE_TO:
12886 case GOMP_MAP_FORCE_FROM:
12887 case GOMP_MAP_FORCE_TOFROM:
12888 case GOMP_MAP_FORCE_PRESENT:
12889 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12890 break;
12891 default:
12892 break;
12894 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12895 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12896 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12897 else if (TREE_CODE (t) == COMPONENT_REF)
12898 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12899 else
12900 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12901 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12902 && !c_mark_addressable (t))
12903 return false;
12904 OMP_CLAUSE_DECL (c2) = t;
12905 t = build_fold_addr_expr (first);
12906 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12907 tree ptr = OMP_CLAUSE_DECL (c2);
12908 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12909 ptr = build_fold_addr_expr (ptr);
12910 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12911 ptrdiff_type_node, t,
12912 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12913 ptrdiff_type_node, ptr));
12914 t = c_fully_fold (t, false, NULL);
12915 OMP_CLAUSE_SIZE (c2) = t;
12916 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12917 OMP_CLAUSE_CHAIN (c) = c2;
12919 return false;
12922 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12923 an inline call. But, remap
12924 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12925 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12927 static tree
12928 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12929 tree decl, tree placeholder)
12931 copy_body_data id;
12932 hash_map<tree, tree> decl_map;
12934 decl_map.put (omp_decl1, placeholder);
12935 decl_map.put (omp_decl2, decl);
12936 memset (&id, 0, sizeof (id));
12937 id.src_fn = DECL_CONTEXT (omp_decl1);
12938 id.dst_fn = current_function_decl;
12939 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12940 id.decl_map = &decl_map;
12942 id.copy_decl = copy_decl_no_change;
12943 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12944 id.transform_new_cfg = true;
12945 id.transform_return_to_modify = false;
12946 id.transform_lang_insert_block = NULL;
12947 id.eh_lp_nr = 0;
12948 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12949 return stmt;
12952 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12953 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12955 static tree
12956 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12958 if (*tp == (tree) data)
12959 return *tp;
12960 return NULL_TREE;
12963 /* For all elements of CLAUSES, validate them against their constraints.
12964 Remove any elements from the list that are invalid. */
12966 tree
12967 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12969 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12970 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12971 tree c, t, type, *pc;
12972 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12973 bool branch_seen = false;
12974 bool copyprivate_seen = false;
12975 bool linear_variable_step_check = false;
12976 tree *nowait_clause = NULL;
12977 bool ordered_seen = false;
12978 tree schedule_clause = NULL_TREE;
12979 bool oacc_async = false;
12981 bitmap_obstack_initialize (NULL);
12982 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12983 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12984 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12985 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12986 bitmap_initialize (&map_head, &bitmap_default_obstack);
12987 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12988 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12990 if (ort & C_ORT_ACC)
12991 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12992 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12994 oacc_async = true;
12995 break;
12998 for (pc = &clauses, c = clauses; c ; c = *pc)
13000 bool remove = false;
13001 bool need_complete = false;
13002 bool need_implicitly_determined = false;
13004 switch (OMP_CLAUSE_CODE (c))
13006 case OMP_CLAUSE_SHARED:
13007 need_implicitly_determined = true;
13008 goto check_dup_generic;
13010 case OMP_CLAUSE_PRIVATE:
13011 need_complete = true;
13012 need_implicitly_determined = true;
13013 goto check_dup_generic;
13015 case OMP_CLAUSE_REDUCTION:
13016 need_implicitly_determined = true;
13017 t = OMP_CLAUSE_DECL (c);
13018 if (TREE_CODE (t) == TREE_LIST)
13020 if (handle_omp_array_sections (c, ort))
13022 remove = true;
13023 break;
13026 t = OMP_CLAUSE_DECL (c);
13028 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13029 if (t == error_mark_node)
13031 remove = true;
13032 break;
13034 if (oacc_async)
13035 c_mark_addressable (t);
13036 type = TREE_TYPE (t);
13037 if (TREE_CODE (t) == MEM_REF)
13038 type = TREE_TYPE (type);
13039 if (TREE_CODE (type) == ARRAY_TYPE)
13041 tree oatype = type;
13042 gcc_assert (TREE_CODE (t) != MEM_REF);
13043 while (TREE_CODE (type) == ARRAY_TYPE)
13044 type = TREE_TYPE (type);
13045 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13047 error_at (OMP_CLAUSE_LOCATION (c),
13048 "%qD in %<reduction%> clause is a zero size array",
13050 remove = true;
13051 break;
13053 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13054 TYPE_SIZE_UNIT (type));
13055 if (integer_zerop (size))
13057 error_at (OMP_CLAUSE_LOCATION (c),
13058 "%qD in %<reduction%> clause is a zero size array",
13060 remove = true;
13061 break;
13063 size = size_binop (MINUS_EXPR, size, size_one_node);
13064 tree index_type = build_index_type (size);
13065 tree atype = build_array_type (type, index_type);
13066 tree ptype = build_pointer_type (type);
13067 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13068 t = build_fold_addr_expr (t);
13069 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13070 OMP_CLAUSE_DECL (c) = t;
13072 if (TYPE_ATOMIC (type))
13074 error_at (OMP_CLAUSE_LOCATION (c),
13075 "%<_Atomic%> %qE in %<reduction%> clause", t);
13076 remove = true;
13077 break;
13079 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13080 && (FLOAT_TYPE_P (type)
13081 || TREE_CODE (type) == COMPLEX_TYPE))
13083 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13084 const char *r_name = NULL;
13086 switch (r_code)
13088 case PLUS_EXPR:
13089 case MULT_EXPR:
13090 case MINUS_EXPR:
13091 break;
13092 case MIN_EXPR:
13093 if (TREE_CODE (type) == COMPLEX_TYPE)
13094 r_name = "min";
13095 break;
13096 case MAX_EXPR:
13097 if (TREE_CODE (type) == COMPLEX_TYPE)
13098 r_name = "max";
13099 break;
13100 case BIT_AND_EXPR:
13101 r_name = "&";
13102 break;
13103 case BIT_XOR_EXPR:
13104 r_name = "^";
13105 break;
13106 case BIT_IOR_EXPR:
13107 r_name = "|";
13108 break;
13109 case TRUTH_ANDIF_EXPR:
13110 if (FLOAT_TYPE_P (type))
13111 r_name = "&&";
13112 break;
13113 case TRUTH_ORIF_EXPR:
13114 if (FLOAT_TYPE_P (type))
13115 r_name = "||";
13116 break;
13117 default:
13118 gcc_unreachable ();
13120 if (r_name)
13122 error_at (OMP_CLAUSE_LOCATION (c),
13123 "%qE has invalid type for %<reduction(%s)%>",
13124 t, r_name);
13125 remove = true;
13126 break;
13129 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13131 error_at (OMP_CLAUSE_LOCATION (c),
13132 "user defined reduction not found for %qE", t);
13133 remove = true;
13134 break;
13136 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13138 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13139 type = TYPE_MAIN_VARIANT (type);
13140 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13141 VAR_DECL, NULL_TREE, type);
13142 tree decl_placeholder = NULL_TREE;
13143 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13144 DECL_ARTIFICIAL (placeholder) = 1;
13145 DECL_IGNORED_P (placeholder) = 1;
13146 if (TREE_CODE (t) == MEM_REF)
13148 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13149 VAR_DECL, NULL_TREE, type);
13150 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13151 DECL_ARTIFICIAL (decl_placeholder) = 1;
13152 DECL_IGNORED_P (decl_placeholder) = 1;
13154 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13155 c_mark_addressable (placeholder);
13156 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13157 c_mark_addressable (decl_placeholder ? decl_placeholder
13158 : OMP_CLAUSE_DECL (c));
13159 OMP_CLAUSE_REDUCTION_MERGE (c)
13160 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13161 TREE_VEC_ELT (list, 0),
13162 TREE_VEC_ELT (list, 1),
13163 decl_placeholder ? decl_placeholder
13164 : OMP_CLAUSE_DECL (c), placeholder);
13165 OMP_CLAUSE_REDUCTION_MERGE (c)
13166 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13167 void_type_node, NULL_TREE,
13168 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13169 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13170 if (TREE_VEC_LENGTH (list) == 6)
13172 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13173 c_mark_addressable (decl_placeholder ? decl_placeholder
13174 : OMP_CLAUSE_DECL (c));
13175 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13176 c_mark_addressable (placeholder);
13177 tree init = TREE_VEC_ELT (list, 5);
13178 if (init == error_mark_node)
13179 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13180 OMP_CLAUSE_REDUCTION_INIT (c)
13181 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13182 TREE_VEC_ELT (list, 3),
13183 decl_placeholder ? decl_placeholder
13184 : OMP_CLAUSE_DECL (c), placeholder);
13185 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13187 tree v = decl_placeholder ? decl_placeholder : t;
13188 OMP_CLAUSE_REDUCTION_INIT (c)
13189 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13190 OMP_CLAUSE_REDUCTION_INIT (c));
13192 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13193 c_find_omp_placeholder_r,
13194 placeholder, NULL))
13195 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13197 else
13199 tree init;
13200 tree v = decl_placeholder ? decl_placeholder : t;
13201 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13202 init = build_constructor (TREE_TYPE (v), NULL);
13203 else
13204 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13205 OMP_CLAUSE_REDUCTION_INIT (c)
13206 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13208 OMP_CLAUSE_REDUCTION_INIT (c)
13209 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13210 void_type_node, NULL_TREE,
13211 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13212 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13214 if (TREE_CODE (t) == MEM_REF)
13216 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13217 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13218 != INTEGER_CST)
13220 sorry ("variable length element type in array "
13221 "%<reduction%> clause");
13222 remove = true;
13223 break;
13225 t = TREE_OPERAND (t, 0);
13226 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13227 t = TREE_OPERAND (t, 0);
13228 if (TREE_CODE (t) == ADDR_EXPR)
13229 t = TREE_OPERAND (t, 0);
13231 goto check_dup_generic_t;
13233 case OMP_CLAUSE_COPYPRIVATE:
13234 copyprivate_seen = true;
13235 if (nowait_clause)
13237 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13238 "%<nowait%> clause must not be used together "
13239 "with %<copyprivate%>");
13240 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13241 nowait_clause = NULL;
13243 goto check_dup_generic;
13245 case OMP_CLAUSE_COPYIN:
13246 t = OMP_CLAUSE_DECL (c);
13247 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13249 error_at (OMP_CLAUSE_LOCATION (c),
13250 "%qE must be %<threadprivate%> for %<copyin%>", t);
13251 remove = true;
13252 break;
13254 goto check_dup_generic;
13256 case OMP_CLAUSE_LINEAR:
13257 if (ort != C_ORT_OMP_DECLARE_SIMD)
13258 need_implicitly_determined = true;
13259 t = OMP_CLAUSE_DECL (c);
13260 if (ort != C_ORT_OMP_DECLARE_SIMD
13261 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13263 error_at (OMP_CLAUSE_LOCATION (c),
13264 "modifier should not be specified in %<linear%> "
13265 "clause on %<simd%> or %<for%> constructs");
13266 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13268 if (ort & C_ORT_CILK)
13270 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13271 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13272 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13274 error_at (OMP_CLAUSE_LOCATION (c),
13275 "linear clause applied to non-integral, "
13276 "non-floating, non-pointer variable with type %qT",
13277 TREE_TYPE (t));
13278 remove = true;
13279 break;
13282 else
13284 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13285 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13287 error_at (OMP_CLAUSE_LOCATION (c),
13288 "linear clause applied to non-integral non-pointer "
13289 "variable with type %qT", TREE_TYPE (t));
13290 remove = true;
13291 break;
13293 if (TYPE_ATOMIC (TREE_TYPE (t)))
13295 error_at (OMP_CLAUSE_LOCATION (c),
13296 "%<_Atomic%> %qD in %<linear%> clause", t);
13297 remove = true;
13298 break;
13301 if (ort == C_ORT_OMP_DECLARE_SIMD)
13303 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13304 if (TREE_CODE (s) == PARM_DECL)
13306 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13307 /* map_head bitmap is used as uniform_head if
13308 declare_simd. */
13309 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13310 linear_variable_step_check = true;
13311 goto check_dup_generic;
13313 if (TREE_CODE (s) != INTEGER_CST)
13315 error_at (OMP_CLAUSE_LOCATION (c),
13316 "%<linear%> clause step %qE is neither constant "
13317 "nor a parameter", s);
13318 remove = true;
13319 break;
13322 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13324 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13325 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13326 OMP_CLAUSE_DECL (c), s);
13327 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13328 sizetype, fold_convert (sizetype, s),
13329 fold_convert
13330 (sizetype, OMP_CLAUSE_DECL (c)));
13331 if (s == error_mark_node)
13332 s = size_one_node;
13333 OMP_CLAUSE_LINEAR_STEP (c) = s;
13335 else
13336 OMP_CLAUSE_LINEAR_STEP (c)
13337 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13338 goto check_dup_generic;
13340 check_dup_generic:
13341 t = OMP_CLAUSE_DECL (c);
13342 check_dup_generic_t:
13343 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13345 error_at (OMP_CLAUSE_LOCATION (c),
13346 "%qE is not a variable in clause %qs", t,
13347 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13348 remove = true;
13350 else if (ort == C_ORT_ACC
13351 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13353 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13355 error ("%qD appears more than once in reduction clauses", t);
13356 remove = true;
13358 else
13359 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13361 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13362 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13363 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13365 error_at (OMP_CLAUSE_LOCATION (c),
13366 "%qE appears more than once in data clauses", t);
13367 remove = true;
13369 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13370 && bitmap_bit_p (&map_head, DECL_UID (t)))
13372 if (ort == C_ORT_ACC)
13373 error ("%qD appears more than once in data clauses", t);
13374 else
13375 error ("%qD appears both in data and map clauses", t);
13376 remove = true;
13378 else
13379 bitmap_set_bit (&generic_head, DECL_UID (t));
13380 break;
13382 case OMP_CLAUSE_FIRSTPRIVATE:
13383 t = OMP_CLAUSE_DECL (c);
13384 need_complete = true;
13385 need_implicitly_determined = true;
13386 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13388 error_at (OMP_CLAUSE_LOCATION (c),
13389 "%qE is not a variable in clause %<firstprivate%>", t);
13390 remove = true;
13392 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13393 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13395 error_at (OMP_CLAUSE_LOCATION (c),
13396 "%qE appears more than once in data clauses", t);
13397 remove = true;
13399 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13401 if (ort == C_ORT_ACC)
13402 error ("%qD appears more than once in data clauses", t);
13403 else
13404 error ("%qD appears both in data and map clauses", t);
13405 remove = true;
13407 else
13408 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13409 break;
13411 case OMP_CLAUSE_LASTPRIVATE:
13412 t = OMP_CLAUSE_DECL (c);
13413 need_complete = true;
13414 need_implicitly_determined = true;
13415 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13417 error_at (OMP_CLAUSE_LOCATION (c),
13418 "%qE is not a variable in clause %<lastprivate%>", t);
13419 remove = true;
13421 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13422 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13424 error_at (OMP_CLAUSE_LOCATION (c),
13425 "%qE appears more than once in data clauses", t);
13426 remove = true;
13428 else
13429 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13430 break;
13432 case OMP_CLAUSE_ALIGNED:
13433 t = OMP_CLAUSE_DECL (c);
13434 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13436 error_at (OMP_CLAUSE_LOCATION (c),
13437 "%qE is not a variable in %<aligned%> clause", t);
13438 remove = true;
13440 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13441 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13443 error_at (OMP_CLAUSE_LOCATION (c),
13444 "%qE in %<aligned%> clause is neither a pointer nor "
13445 "an array", t);
13446 remove = true;
13448 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13450 error_at (OMP_CLAUSE_LOCATION (c),
13451 "%<_Atomic%> %qD in %<aligned%> clause", t);
13452 remove = true;
13453 break;
13455 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13457 error_at (OMP_CLAUSE_LOCATION (c),
13458 "%qE appears more than once in %<aligned%> clauses",
13460 remove = true;
13462 else
13463 bitmap_set_bit (&aligned_head, DECL_UID (t));
13464 break;
13466 case OMP_CLAUSE_DEPEND:
13467 t = OMP_CLAUSE_DECL (c);
13468 if (t == NULL_TREE)
13470 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13471 == OMP_CLAUSE_DEPEND_SOURCE);
13472 break;
13474 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13476 gcc_assert (TREE_CODE (t) == TREE_LIST);
13477 for (; t; t = TREE_CHAIN (t))
13479 tree decl = TREE_VALUE (t);
13480 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13482 tree offset = TREE_PURPOSE (t);
13483 bool neg = wi::neg_p ((wide_int) offset);
13484 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13485 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13486 neg ? MINUS_EXPR : PLUS_EXPR,
13487 decl, offset);
13488 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13489 sizetype,
13490 fold_convert (sizetype, t2),
13491 fold_convert (sizetype, decl));
13492 if (t2 == error_mark_node)
13494 remove = true;
13495 break;
13497 TREE_PURPOSE (t) = t2;
13500 break;
13502 if (TREE_CODE (t) == TREE_LIST)
13504 if (handle_omp_array_sections (c, ort))
13505 remove = true;
13506 break;
13508 if (t == error_mark_node)
13509 remove = true;
13510 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13512 error_at (OMP_CLAUSE_LOCATION (c),
13513 "%qE is not a variable in %<depend%> clause", t);
13514 remove = true;
13516 else if (!c_mark_addressable (t))
13517 remove = true;
13518 break;
13520 case OMP_CLAUSE_MAP:
13521 case OMP_CLAUSE_TO:
13522 case OMP_CLAUSE_FROM:
13523 case OMP_CLAUSE__CACHE_:
13524 t = OMP_CLAUSE_DECL (c);
13525 if (TREE_CODE (t) == TREE_LIST)
13527 if (handle_omp_array_sections (c, ort))
13528 remove = true;
13529 else
13531 t = OMP_CLAUSE_DECL (c);
13532 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13534 error_at (OMP_CLAUSE_LOCATION (c),
13535 "array section does not have mappable type "
13536 "in %qs clause",
13537 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13538 remove = true;
13540 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13542 error_at (OMP_CLAUSE_LOCATION (c),
13543 "%<_Atomic%> %qE in %qs clause", t,
13544 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13545 remove = true;
13547 while (TREE_CODE (t) == ARRAY_REF)
13548 t = TREE_OPERAND (t, 0);
13549 if (TREE_CODE (t) == COMPONENT_REF
13550 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13552 while (TREE_CODE (t) == COMPONENT_REF)
13553 t = TREE_OPERAND (t, 0);
13554 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13555 break;
13556 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13558 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13559 error ("%qD appears more than once in motion"
13560 " clauses", t);
13561 else if (ort == C_ORT_ACC)
13562 error ("%qD appears more than once in data"
13563 " clauses", t);
13564 else
13565 error ("%qD appears more than once in map"
13566 " clauses", t);
13567 remove = true;
13569 else
13571 bitmap_set_bit (&map_head, DECL_UID (t));
13572 bitmap_set_bit (&map_field_head, DECL_UID (t));
13576 break;
13578 if (t == error_mark_node)
13580 remove = true;
13581 break;
13583 if (TREE_CODE (t) == COMPONENT_REF
13584 && (ort & C_ORT_OMP)
13585 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13587 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13589 error_at (OMP_CLAUSE_LOCATION (c),
13590 "bit-field %qE in %qs clause",
13591 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13592 remove = true;
13594 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13596 error_at (OMP_CLAUSE_LOCATION (c),
13597 "%qE does not have a mappable type in %qs clause",
13598 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13599 remove = true;
13601 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13603 error_at (OMP_CLAUSE_LOCATION (c),
13604 "%<_Atomic%> %qE in %qs clause", t,
13605 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13606 remove = true;
13608 while (TREE_CODE (t) == COMPONENT_REF)
13610 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13611 == UNION_TYPE)
13613 error_at (OMP_CLAUSE_LOCATION (c),
13614 "%qE is a member of a union", t);
13615 remove = true;
13616 break;
13618 t = TREE_OPERAND (t, 0);
13620 if (remove)
13621 break;
13622 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13624 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13625 break;
13628 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13630 error_at (OMP_CLAUSE_LOCATION (c),
13631 "%qE is not a variable in %qs clause", t,
13632 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13633 remove = true;
13635 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13637 error_at (OMP_CLAUSE_LOCATION (c),
13638 "%qD is threadprivate variable in %qs clause", t,
13639 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13640 remove = true;
13642 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13643 || (OMP_CLAUSE_MAP_KIND (c)
13644 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13645 && !c_mark_addressable (t))
13646 remove = true;
13647 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13648 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13649 || (OMP_CLAUSE_MAP_KIND (c)
13650 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13651 || (OMP_CLAUSE_MAP_KIND (c)
13652 == GOMP_MAP_FORCE_DEVICEPTR)))
13653 && t == OMP_CLAUSE_DECL (c)
13654 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13656 error_at (OMP_CLAUSE_LOCATION (c),
13657 "%qD does not have a mappable type in %qs clause", t,
13658 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13659 remove = true;
13661 else if (TREE_TYPE (t) == error_mark_node)
13662 remove = true;
13663 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13665 error_at (OMP_CLAUSE_LOCATION (c),
13666 "%<_Atomic%> %qE in %qs clause", t,
13667 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13668 remove = true;
13670 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13671 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13673 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13674 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13676 error ("%qD appears more than once in data clauses", t);
13677 remove = true;
13679 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13681 if (ort == C_ORT_ACC)
13682 error ("%qD appears more than once in data clauses", t);
13683 else
13684 error ("%qD appears both in data and map clauses", t);
13685 remove = true;
13687 else
13688 bitmap_set_bit (&generic_head, DECL_UID (t));
13690 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13692 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13693 error ("%qD appears more than once in motion clauses", t);
13694 else if (ort == C_ORT_ACC)
13695 error ("%qD appears more than once in data clauses", t);
13696 else
13697 error ("%qD appears more than once in map clauses", t);
13698 remove = true;
13700 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13701 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13703 if (ort == C_ORT_ACC)
13704 error ("%qD appears more than once in data clauses", t);
13705 else
13706 error ("%qD appears both in data and map clauses", t);
13707 remove = true;
13709 else
13711 bitmap_set_bit (&map_head, DECL_UID (t));
13712 if (t != OMP_CLAUSE_DECL (c)
13713 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13714 bitmap_set_bit (&map_field_head, DECL_UID (t));
13716 break;
13718 case OMP_CLAUSE_TO_DECLARE:
13719 case OMP_CLAUSE_LINK:
13720 t = OMP_CLAUSE_DECL (c);
13721 if (TREE_CODE (t) == FUNCTION_DECL
13722 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13724 else if (!VAR_P (t))
13726 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13727 error_at (OMP_CLAUSE_LOCATION (c),
13728 "%qE is neither a variable nor a function name in "
13729 "clause %qs", t,
13730 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13731 else
13732 error_at (OMP_CLAUSE_LOCATION (c),
13733 "%qE is not a variable in clause %qs", t,
13734 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13735 remove = true;
13737 else if (DECL_THREAD_LOCAL_P (t))
13739 error_at (OMP_CLAUSE_LOCATION (c),
13740 "%qD is threadprivate variable in %qs clause", t,
13741 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13742 remove = true;
13744 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13746 error_at (OMP_CLAUSE_LOCATION (c),
13747 "%qD does not have a mappable type in %qs clause", t,
13748 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13749 remove = true;
13751 if (remove)
13752 break;
13753 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13755 error_at (OMP_CLAUSE_LOCATION (c),
13756 "%qE appears more than once on the same "
13757 "%<declare target%> directive", t);
13758 remove = true;
13760 else
13761 bitmap_set_bit (&generic_head, DECL_UID (t));
13762 break;
13764 case OMP_CLAUSE_UNIFORM:
13765 t = OMP_CLAUSE_DECL (c);
13766 if (TREE_CODE (t) != PARM_DECL)
13768 if (DECL_P (t))
13769 error_at (OMP_CLAUSE_LOCATION (c),
13770 "%qD is not an argument in %<uniform%> clause", t);
13771 else
13772 error_at (OMP_CLAUSE_LOCATION (c),
13773 "%qE is not an argument in %<uniform%> clause", t);
13774 remove = true;
13775 break;
13777 /* map_head bitmap is used as uniform_head if declare_simd. */
13778 bitmap_set_bit (&map_head, DECL_UID (t));
13779 goto check_dup_generic;
13781 case OMP_CLAUSE_IS_DEVICE_PTR:
13782 case OMP_CLAUSE_USE_DEVICE_PTR:
13783 t = OMP_CLAUSE_DECL (c);
13784 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13785 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13787 error_at (OMP_CLAUSE_LOCATION (c),
13788 "%qs variable is neither a pointer nor an array",
13789 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13790 remove = true;
13792 goto check_dup_generic;
13794 case OMP_CLAUSE_NOWAIT:
13795 if (copyprivate_seen)
13797 error_at (OMP_CLAUSE_LOCATION (c),
13798 "%<nowait%> clause must not be used together "
13799 "with %<copyprivate%>");
13800 remove = true;
13801 break;
13803 nowait_clause = pc;
13804 pc = &OMP_CLAUSE_CHAIN (c);
13805 continue;
13807 case OMP_CLAUSE_IF:
13808 case OMP_CLAUSE_NUM_THREADS:
13809 case OMP_CLAUSE_NUM_TEAMS:
13810 case OMP_CLAUSE_THREAD_LIMIT:
13811 case OMP_CLAUSE_DEFAULT:
13812 case OMP_CLAUSE_UNTIED:
13813 case OMP_CLAUSE_COLLAPSE:
13814 case OMP_CLAUSE_FINAL:
13815 case OMP_CLAUSE_MERGEABLE:
13816 case OMP_CLAUSE_DEVICE:
13817 case OMP_CLAUSE_DIST_SCHEDULE:
13818 case OMP_CLAUSE_PARALLEL:
13819 case OMP_CLAUSE_FOR:
13820 case OMP_CLAUSE_SECTIONS:
13821 case OMP_CLAUSE_TASKGROUP:
13822 case OMP_CLAUSE_PROC_BIND:
13823 case OMP_CLAUSE_PRIORITY:
13824 case OMP_CLAUSE_GRAINSIZE:
13825 case OMP_CLAUSE_NUM_TASKS:
13826 case OMP_CLAUSE_NOGROUP:
13827 case OMP_CLAUSE_THREADS:
13828 case OMP_CLAUSE_SIMD:
13829 case OMP_CLAUSE_HINT:
13830 case OMP_CLAUSE_DEFAULTMAP:
13831 case OMP_CLAUSE__CILK_FOR_COUNT_:
13832 case OMP_CLAUSE_NUM_GANGS:
13833 case OMP_CLAUSE_NUM_WORKERS:
13834 case OMP_CLAUSE_VECTOR_LENGTH:
13835 case OMP_CLAUSE_ASYNC:
13836 case OMP_CLAUSE_WAIT:
13837 case OMP_CLAUSE_AUTO:
13838 case OMP_CLAUSE_INDEPENDENT:
13839 case OMP_CLAUSE_SEQ:
13840 case OMP_CLAUSE_GANG:
13841 case OMP_CLAUSE_WORKER:
13842 case OMP_CLAUSE_VECTOR:
13843 case OMP_CLAUSE_TILE:
13844 pc = &OMP_CLAUSE_CHAIN (c);
13845 continue;
13847 case OMP_CLAUSE_SCHEDULE:
13848 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13850 const char *p = NULL;
13851 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13853 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13854 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13855 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13856 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13857 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13858 default: gcc_unreachable ();
13860 if (p)
13862 error_at (OMP_CLAUSE_LOCATION (c),
13863 "%<nonmonotonic%> modifier specified for %qs "
13864 "schedule kind", p);
13865 OMP_CLAUSE_SCHEDULE_KIND (c)
13866 = (enum omp_clause_schedule_kind)
13867 (OMP_CLAUSE_SCHEDULE_KIND (c)
13868 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13871 schedule_clause = c;
13872 pc = &OMP_CLAUSE_CHAIN (c);
13873 continue;
13875 case OMP_CLAUSE_ORDERED:
13876 ordered_seen = true;
13877 pc = &OMP_CLAUSE_CHAIN (c);
13878 continue;
13880 case OMP_CLAUSE_SAFELEN:
13881 safelen = c;
13882 pc = &OMP_CLAUSE_CHAIN (c);
13883 continue;
13884 case OMP_CLAUSE_SIMDLEN:
13885 simdlen = c;
13886 pc = &OMP_CLAUSE_CHAIN (c);
13887 continue;
13889 case OMP_CLAUSE_INBRANCH:
13890 case OMP_CLAUSE_NOTINBRANCH:
13891 if (branch_seen)
13893 error_at (OMP_CLAUSE_LOCATION (c),
13894 "%<inbranch%> clause is incompatible with "
13895 "%<notinbranch%>");
13896 remove = true;
13897 break;
13899 branch_seen = true;
13900 pc = &OMP_CLAUSE_CHAIN (c);
13901 continue;
13903 default:
13904 gcc_unreachable ();
13907 if (!remove)
13909 t = OMP_CLAUSE_DECL (c);
13911 if (need_complete)
13913 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13914 if (t == error_mark_node)
13915 remove = true;
13918 if (need_implicitly_determined)
13920 const char *share_name = NULL;
13922 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13923 share_name = "threadprivate";
13924 else switch (c_omp_predetermined_sharing (t))
13926 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13927 break;
13928 case OMP_CLAUSE_DEFAULT_SHARED:
13929 /* const vars may be specified in firstprivate clause. */
13930 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13931 && TREE_READONLY (t))
13932 break;
13933 share_name = "shared";
13934 break;
13935 case OMP_CLAUSE_DEFAULT_PRIVATE:
13936 share_name = "private";
13937 break;
13938 default:
13939 gcc_unreachable ();
13941 if (share_name)
13943 error_at (OMP_CLAUSE_LOCATION (c),
13944 "%qE is predetermined %qs for %qs",
13945 t, share_name,
13946 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13947 remove = true;
13952 if (remove)
13953 *pc = OMP_CLAUSE_CHAIN (c);
13954 else
13955 pc = &OMP_CLAUSE_CHAIN (c);
13958 if (simdlen
13959 && safelen
13960 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13961 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13963 error_at (OMP_CLAUSE_LOCATION (simdlen),
13964 "%<simdlen%> clause value is bigger than "
13965 "%<safelen%> clause value");
13966 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13967 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13970 if (ordered_seen
13971 && schedule_clause
13972 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13973 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13975 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13976 "%<nonmonotonic%> schedule modifier specified together "
13977 "with %<ordered%> clause");
13978 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13979 = (enum omp_clause_schedule_kind)
13980 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13981 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13984 if (linear_variable_step_check)
13985 for (pc = &clauses, c = clauses; c ; c = *pc)
13987 bool remove = false;
13988 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13989 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13990 && !bitmap_bit_p (&map_head,
13991 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13993 error_at (OMP_CLAUSE_LOCATION (c),
13994 "%<linear%> clause step is a parameter %qD not "
13995 "specified in %<uniform%> clause",
13996 OMP_CLAUSE_LINEAR_STEP (c));
13997 remove = true;
14000 if (remove)
14001 *pc = OMP_CLAUSE_CHAIN (c);
14002 else
14003 pc = &OMP_CLAUSE_CHAIN (c);
14006 bitmap_obstack_release (NULL);
14007 return clauses;
14010 /* Return code to initialize DST with a copy constructor from SRC.
14011 C doesn't have copy constructors nor assignment operators, only for
14012 _Atomic vars we need to perform __atomic_load from src into a temporary
14013 followed by __atomic_store of the temporary to dst. */
14015 tree
14016 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14018 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14019 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14021 location_t loc = OMP_CLAUSE_LOCATION (clause);
14022 tree type = TREE_TYPE (dst);
14023 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14024 tree tmp = create_tmp_var (nonatomic_type);
14025 tree tmp_addr = build_fold_addr_expr (tmp);
14026 TREE_ADDRESSABLE (tmp) = 1;
14027 TREE_NO_WARNING (tmp) = 1;
14028 tree src_addr = build_fold_addr_expr (src);
14029 tree dst_addr = build_fold_addr_expr (dst);
14030 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14031 vec<tree, va_gc> *params;
14032 /* Expansion of a generic atomic load may require an addition
14033 element, so allocate enough to prevent a resize. */
14034 vec_alloc (params, 4);
14036 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14037 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14038 params->quick_push (src_addr);
14039 params->quick_push (tmp_addr);
14040 params->quick_push (seq_cst);
14041 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14043 vec_alloc (params, 4);
14045 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14046 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14047 params->quick_push (dst_addr);
14048 params->quick_push (tmp_addr);
14049 params->quick_push (seq_cst);
14050 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14051 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14054 /* Create a transaction node. */
14056 tree
14057 c_finish_transaction (location_t loc, tree block, int flags)
14059 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14060 if (flags & TM_STMT_ATTR_OUTER)
14061 TRANSACTION_EXPR_OUTER (stmt) = 1;
14062 if (flags & TM_STMT_ATTR_RELAXED)
14063 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14064 return add_stmt (stmt);
14067 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14068 down to the element type of an array. If ORIG_QUAL_TYPE is not
14069 NULL, then it should be used as the qualified type
14070 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14071 preserve information about the typedef name from which an array
14072 type was derived). */
14074 tree
14075 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14076 size_t orig_qual_indirect)
14078 if (type == error_mark_node)
14079 return type;
14081 if (TREE_CODE (type) == ARRAY_TYPE)
14083 tree t;
14084 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14085 type_quals, orig_qual_type,
14086 orig_qual_indirect - 1);
14088 /* See if we already have an identically qualified type. */
14089 if (orig_qual_type && orig_qual_indirect == 0)
14090 t = orig_qual_type;
14091 else
14092 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14094 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14095 && TYPE_NAME (t) == TYPE_NAME (type)
14096 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14097 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14098 TYPE_ATTRIBUTES (type)))
14099 break;
14101 if (!t)
14103 tree domain = TYPE_DOMAIN (type);
14105 t = build_variant_type_copy (type);
14106 TREE_TYPE (t) = element_type;
14108 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14109 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14110 SET_TYPE_STRUCTURAL_EQUALITY (t);
14111 else if (TYPE_CANONICAL (element_type) != element_type
14112 || (domain && TYPE_CANONICAL (domain) != domain))
14114 tree unqualified_canon
14115 = build_array_type (TYPE_CANONICAL (element_type),
14116 domain? TYPE_CANONICAL (domain)
14117 : NULL_TREE);
14118 if (TYPE_REVERSE_STORAGE_ORDER (type))
14120 unqualified_canon
14121 = build_distinct_type_copy (unqualified_canon);
14122 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14124 TYPE_CANONICAL (t)
14125 = c_build_qualified_type (unqualified_canon, type_quals);
14127 else
14128 TYPE_CANONICAL (t) = t;
14130 return t;
14133 /* A restrict-qualified pointer type must be a pointer to object or
14134 incomplete type. Note that the use of POINTER_TYPE_P also allows
14135 REFERENCE_TYPEs, which is appropriate for C++. */
14136 if ((type_quals & TYPE_QUAL_RESTRICT)
14137 && (!POINTER_TYPE_P (type)
14138 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14140 error ("invalid use of %<restrict%>");
14141 type_quals &= ~TYPE_QUAL_RESTRICT;
14144 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14145 ? orig_qual_type
14146 : build_qualified_type (type, type_quals));
14147 /* A variant type does not inherit the list of incomplete vars from the
14148 type main variant. */
14149 if (RECORD_OR_UNION_TYPE_P (var_type)
14150 && TYPE_MAIN_VARIANT (var_type) != var_type)
14151 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14152 return var_type;
14155 /* Build a VA_ARG_EXPR for the C parser. */
14157 tree
14158 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14160 if (error_operand_p (type))
14161 return error_mark_node;
14162 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14163 order because it takes the address of the expression. */
14164 else if (handled_component_p (expr)
14165 && reverse_storage_order_for_component_p (expr))
14167 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14168 return error_mark_node;
14170 else if (!COMPLETE_TYPE_P (type))
14172 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14173 "type %qT", type);
14174 return error_mark_node;
14176 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14177 warning_at (loc2, OPT_Wc___compat,
14178 "C++ requires promoted type, not enum type, in %<va_arg%>");
14179 return build_va_arg (loc2, expr, type);
14182 /* Return truthvalue of whether T1 is the same tree structure as T2.
14183 Return 1 if they are the same. Return false if they are different. */
14185 bool
14186 c_tree_equal (tree t1, tree t2)
14188 enum tree_code code1, code2;
14190 if (t1 == t2)
14191 return true;
14192 if (!t1 || !t2)
14193 return false;
14195 for (code1 = TREE_CODE (t1);
14196 CONVERT_EXPR_CODE_P (code1)
14197 || code1 == NON_LVALUE_EXPR;
14198 code1 = TREE_CODE (t1))
14199 t1 = TREE_OPERAND (t1, 0);
14200 for (code2 = TREE_CODE (t2);
14201 CONVERT_EXPR_CODE_P (code2)
14202 || code2 == NON_LVALUE_EXPR;
14203 code2 = TREE_CODE (t2))
14204 t2 = TREE_OPERAND (t2, 0);
14206 /* They might have become equal now. */
14207 if (t1 == t2)
14208 return true;
14210 if (code1 != code2)
14211 return false;
14213 switch (code1)
14215 case INTEGER_CST:
14216 return wi::eq_p (t1, t2);
14218 case REAL_CST:
14219 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14221 case STRING_CST:
14222 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14223 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14224 TREE_STRING_LENGTH (t1));
14226 case FIXED_CST:
14227 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14228 TREE_FIXED_CST (t2));
14230 case COMPLEX_CST:
14231 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14232 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14234 case VECTOR_CST:
14235 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14237 case CONSTRUCTOR:
14238 /* We need to do this when determining whether or not two
14239 non-type pointer to member function template arguments
14240 are the same. */
14241 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14242 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14243 return false;
14245 tree field, value;
14246 unsigned int i;
14247 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14249 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14250 if (!c_tree_equal (field, elt2->index)
14251 || !c_tree_equal (value, elt2->value))
14252 return false;
14255 return true;
14257 case TREE_LIST:
14258 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14259 return false;
14260 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14261 return false;
14262 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14264 case SAVE_EXPR:
14265 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14267 case CALL_EXPR:
14269 tree arg1, arg2;
14270 call_expr_arg_iterator iter1, iter2;
14271 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14272 return false;
14273 for (arg1 = first_call_expr_arg (t1, &iter1),
14274 arg2 = first_call_expr_arg (t2, &iter2);
14275 arg1 && arg2;
14276 arg1 = next_call_expr_arg (&iter1),
14277 arg2 = next_call_expr_arg (&iter2))
14278 if (!c_tree_equal (arg1, arg2))
14279 return false;
14280 if (arg1 || arg2)
14281 return false;
14282 return true;
14285 case TARGET_EXPR:
14287 tree o1 = TREE_OPERAND (t1, 0);
14288 tree o2 = TREE_OPERAND (t2, 0);
14290 /* Special case: if either target is an unallocated VAR_DECL,
14291 it means that it's going to be unified with whatever the
14292 TARGET_EXPR is really supposed to initialize, so treat it
14293 as being equivalent to anything. */
14294 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14295 && !DECL_RTL_SET_P (o1))
14296 /*Nop*/;
14297 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14298 && !DECL_RTL_SET_P (o2))
14299 /*Nop*/;
14300 else if (!c_tree_equal (o1, o2))
14301 return false;
14303 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14306 case COMPONENT_REF:
14307 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14308 return false;
14309 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14311 case PARM_DECL:
14312 case VAR_DECL:
14313 case CONST_DECL:
14314 case FIELD_DECL:
14315 case FUNCTION_DECL:
14316 case IDENTIFIER_NODE:
14317 case SSA_NAME:
14318 return false;
14320 case TREE_VEC:
14322 unsigned ix;
14323 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14324 return false;
14325 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14326 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14327 TREE_VEC_ELT (t2, ix)))
14328 return false;
14329 return true;
14332 default:
14333 break;
14336 switch (TREE_CODE_CLASS (code1))
14338 case tcc_unary:
14339 case tcc_binary:
14340 case tcc_comparison:
14341 case tcc_expression:
14342 case tcc_vl_exp:
14343 case tcc_reference:
14344 case tcc_statement:
14346 int i, n = TREE_OPERAND_LENGTH (t1);
14348 switch (code1)
14350 case PREINCREMENT_EXPR:
14351 case PREDECREMENT_EXPR:
14352 case POSTINCREMENT_EXPR:
14353 case POSTDECREMENT_EXPR:
14354 n = 1;
14355 break;
14356 case ARRAY_REF:
14357 n = 2;
14358 break;
14359 default:
14360 break;
14363 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14364 && n != TREE_OPERAND_LENGTH (t2))
14365 return false;
14367 for (i = 0; i < n; ++i)
14368 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14369 return false;
14371 return true;
14374 case tcc_type:
14375 return comptypes (t1, t2);
14376 default:
14377 gcc_unreachable ();
14379 /* We can get here with --disable-checking. */
14380 return false;
14383 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14384 spawn-helper and BODY is the newly created body for FNDECL. */
14386 void
14387 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14389 tree list = alloc_stmt_list ();
14390 tree frame = make_cilk_frame (fndecl);
14391 tree dtor = create_cilk_function_exit (frame, false, true);
14392 add_local_decl (cfun, frame);
14394 DECL_SAVED_TREE (fndecl) = list;
14396 tree body_list = alloc_stmt_list ();
14397 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14398 body = fold_build_cleanup_point_expr (void_type_node, body);
14400 append_to_statement_list (body, &body_list);
14401 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14402 body_list, dtor), &list);
14405 /* Returns true when the function declaration FNDECL is implicit,
14406 introduced as a result of a call to an otherwise undeclared
14407 function, and false otherwise. */
14409 bool
14410 c_decl_implicit (const_tree fndecl)
14412 return C_DECL_IMPLICIT (fndecl);